* Build llhttp with gcc rather than clang to avoid armv7 contamination.
[dgit import unpatched node-undici 5.28.4+dfsg1+~cs23.12.11-2+rpi1]
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/node_modules
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++language: node_js
++++++++node_js:
++++++++ - '6'
++++++++cache:
++++++++ directories:
++++++++ - node_modules
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++binary-search
++++++++=============
++++++++
++++++++This is a really tiny, stupid, simple binary search library for Node.JS. We
++++++++wrote it because existing solutions were bloated and incorrect.
++++++++
++++++++This version is a straight port of the Java version mentioned by Joshua Bloch
++++++++in his article, [Nearly All Binary Searches and Merge Sorts are Broken](http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).
++++++++
++++++++Thanks to [Conrad Irwin](https://github.com/ConradIrwin) and [Michael
++++++++Marino](https://github.com/mgmarino) for, ironically, pointing out bugs.
++++++++
++++++++Example
++++++++-------
++++++++
++++++++```js
++++++++var bs = require("binary-search");
++++++++
++++++++bs([1, 2, 3, 4], 3, function(element, needle) { return element - needle; });
++++++++// => 2
++++++++
++++++++bs([1, 2, 4, 5], 3, function(element, needle) { return element - needle; });
++++++++// => -3
++++++++```
++++++++
++++++++Be advised that passing in a comparator function is *required*. Since you're
++++++++probably using one for your sort function anyway, this isn't a big deal.
++++++++
++++++++The comparator takes a 1st and 2nd argument of element and needle, respectively.
++++++++
++++++++The comparator also takes a 3rd and 4th argument, the current index and array,
++++++++respectively. You shouldn't normally need the index or array to compare values,
++++++++but it's there if you do.
++++++++
++++++++You may also, optionally, specify an input range as the final two parameters,
++++++++in case you want to limit the search to a particular range of inputs. However,
++++++++be advised that this is generally a bad idea (but sometimes bad ideas are
++++++++necessary).
++++++++
++++++++License
++++++++-------
++++++++
++++++++To the extent possible by law, The Dark Sky Company, LLC has [waived all
++++++++copyright and related or neighboring rights][cc0] to this library.
++++++++
++++++++[cc0]: http://creativecommons.org/publicdomain/zero/1.0/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++//Typescript type definition for:
++++++++//https://github.com/darkskyapp/binary-search
++++++++declare module 'binary-search' {
++++++++
++++++++function binarySearch<A, B>(
++++++++ haystack: ArrayLike<A>,
++++++++ needle: B,
++++++++ comparator: (a: A, b: B, index?: number, haystack?: A[]) => any,
++++++++ // Notes about comparator return value:
++++++++ // * when a<b the comparator's returned value should be:
++++++++ // * negative number or a value such that `+value` is a negative number
++++++++ // * examples: `-1` or the string `"-1"`
++++++++ // * when a>b the comparator's returned value should be:
++++++++ // * positive number or a value such that `+value` is a positive number
++++++++ // * examples: `1` or the string `"1"`
++++++++ // * when a===b
++++++++ // * any value other than the return cases for a<b and a>b
++++++++ // * examples: undefined, NaN, 'abc'
++++++++ low?: number,
++++++++ high?: number): number; //returns index of found result or number < 0 if not found
++++++++export = binarySearch;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++module.exports = function(haystack, needle, comparator, low, high) {
++++++++ var mid, cmp;
++++++++
++++++++ if(low === undefined)
++++++++ low = 0;
++++++++
++++++++ else {
++++++++ low = low|0;
++++++++ if(low < 0 || low >= haystack.length)
++++++++ throw new RangeError("invalid lower bound");
++++++++ }
++++++++
++++++++ if(high === undefined)
++++++++ high = haystack.length - 1;
++++++++
++++++++ else {
++++++++ high = high|0;
++++++++ if(high < low || high >= haystack.length)
++++++++ throw new RangeError("invalid upper bound");
++++++++ }
++++++++
++++++++ while(low <= high) {
++++++++ // The naive `low + high >>> 1` could fail for array lengths > 2**31
++++++++ // because `>>>` converts its operands to int32. `low + (high - low >>> 1)`
++++++++ // works for array lengths <= 2**32-1 which is also Javascript's max array
++++++++ // length.
++++++++ mid = low + ((high - low) >>> 1);
++++++++ cmp = +comparator(haystack[mid], needle, mid, haystack);
++++++++
++++++++ // Too low.
++++++++ if(cmp < 0.0)
++++++++ low = mid + 1;
++++++++
++++++++ // Too high.
++++++++ else if(cmp > 0.0)
++++++++ high = mid - 1;
++++++++
++++++++ // Key found.
++++++++ else
++++++++ return mid;
++++++++ }
++++++++
++++++++ // Key not found.
++++++++ return ~low;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "binary-search",
++++++++ "version": "1.3.6",
++++++++ "description": "tiny binary search function with comparators",
++++++++ "license": "CC0-1.0",
++++++++ "typings": "./binary-search.d.ts",
++++++++ "author": {
++++++++ "name": "The Dark Sky Company, LLC",
++++++++ "email": "support@darkskyapp.com"
++++++++ },
++++++++ "contributors": [
++++++++ {
++++++++ "name": "Darcy Parker",
++++++++ "web": "https://github.com/darcyparker"
++++++++ }
++++++++ ],
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git://github.com/darkskyapp/binary-search.git"
++++++++ },
++++++++ "devDependencies": {
++++++++ "chai": "^4.2.0",
++++++++ "mocha": "^5.2.0"
++++++++ },
++++++++ "scripts": {
++++++++ "test": "mocha"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++var expect = require("chai").expect;
++++++++
++++++++describe("binarysearch", function() {
++++++++ var bs = require("./"),
++++++++ arr = [1, 2, 2, 2, 3, 5, 9],
++++++++ cmp = function(a, b) { return a - b; };
++++++++
++++++++ it("should bail if not passed an array", function() {
++++++++ expect(function() { bs(undefined, 3, cmp); }).to.throw(TypeError);
++++++++ });
++++++++
++++++++ it("should bail if not passed a comparator", function() {
++++++++ expect(function() { bs(arr, 3, undefined); }).to.throw(TypeError);
++++++++ });
++++++++
++++++++ it("should return the index of an item in a sorted array", function() {
++++++++ expect(bs(arr, 3, cmp)).to.equal(4);
++++++++ });
++++++++
++++++++ it("should return the index of where the item would go plus one, negated, if the item is not found", function() {
++++++++ expect(bs(arr, 4, cmp)).to.equal(-6);
++++++++ });
++++++++
++++++++ it("should return any valid index if an item exists multiple times in the array", function() {
++++++++ expect(bs(arr, 2, cmp)).to.equal(3);
++++++++ });
++++++++
++++++++ it("should work even on empty arrays", function() {
++++++++ expect(bs([], 42, cmp)).to.equal(-1);
++++++++ });
++++++++
++++++++ it("should work even on arrays of doubles", function() {
++++++++ expect(bs([0.0, 0.1, 0.2, 0.3, 0.4], 0.25, cmp)).to.equal(-4);
++++++++ });
++++++++
++++++++ it("should pass the index and array parameters to the comparator", function() {
++++++++ var indexes = [],
++++++++ indexCmp = function(a, b, i, array) {
++++++++ expect(array).to.equal(arr);
++++++++ indexes.push(i);
++++++++ return cmp(a, b);
++++++++ };
++++++++ bs(arr, 3, indexCmp);
++++++++ expect(indexes).to.deep.equal([3, 5, 4])
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node-undici (5.28.4+dfsg1+~cs23.12.11-2+rpi1) trixie-staging; urgency=medium
++++++++
++++++++ * Build llhttp with gcc rather than clang to avoid armv7 contamination.
++++++++
++++++++ -- Peter Michael Green <plugwash@raspbian.org> Fri, 08 Nov 2024 04:35:57 +0000
++++++++
++++++++node-undici (5.28.4+dfsg1+~cs23.12.11-2) unstable; urgency=medium
++++++++
++++++++ * Team upload
++++++++ * Fix fix-wasm-build.patch
++++++++ * Clean obj-* directory
++++++++ * Build with upstream script. Closes: #1068842
++++++++ * Drop fix_perms llhttp.wasm, not installed
++++++++
++++++++ -- Jérémy Lal <kapouer@melix.org> Fri, 12 Apr 2024 12:05:59 +0200
++++++++
++++++++node-undici (5.28.4+dfsg1+~cs23.12.11-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version (Closes: CVE-2024-30261)
++++++++ * Refresh patches
++++++++
++++++++ -- Yadd <yadd@debian.org> Fri, 05 Apr 2024 15:26:06 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-6) unstable; urgency=medium
++++++++
++++++++ * Update homepages
++++++++ * Drop test/connect-timeout.js from network test (unstable test in debci env)
++++++++ * Clean build links
++++++++
++++++++ -- Yadd <yadd@debian.org> Mon, 22 Jan 2024 14:45:20 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-5) unstable; urgency=medium
++++++++
++++++++ * Drop dependency to node-busboy
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 21 Jan 2024 06:57:54 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-4) unstable; urgency=medium
++++++++
++++++++ * Build libllhttp (Closes: #977716)
++++++++ + add patch to export SONAME
++++++++ + add libllhttp9 and libllthhp-dev
++++++++ + update lintian overrides
++++++++ + add build dependency to cmake
++++++++ * Fix permissions
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 20 Jan 2024 08:50:46 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-3) unstable; urgency=medium
++++++++
++++++++ * Source-only upload
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 03 Dec 2023 07:07:30 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-2) unstable; urgency=medium
++++++++
++++++++ * Build and publish undici-types, needed by new @types/node
++++++++ * Binary upload
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 02 Dec 2023 22:34:40 +0400
++++++++
++++++++node-undici (5.28.2+dfsg1+~cs23.11.12.3-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.28.2+dfsg1+~cs23.11.12.3
++++++++ * Refresh patches
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 02 Dec 2023 22:01:17 +0400
++++++++
++++++++node-undici (5.28.0+dfsg1+~cs23.11.12.3-2) unstable; urgency=medium
++++++++
++++++++ * Update lintian overrides
++++++++ * Add patch to workaround nodejs bug
++++++++ (fixes nodejs build, thanks to Jérémy Lal)
++++++++
++++++++ -- Yadd <yadd@debian.org> Mon, 27 Nov 2023 15:45:33 +0400
++++++++
++++++++node-undici (5.28.0+dfsg1+~cs23.11.12.3-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.28.0+dfsg1+~cs23.11.12.3
++++++++ * Drop workaround-node-bug.patch
++++++++ * Update build
++++++++ * Drop some failing test
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 26 Nov 2023 08:07:47 +0400
++++++++
++++++++node-undici (5.26.3+dfsg1+~cs23.10.12-3) unstable; urgency=medium
++++++++
++++++++ * Add fix for node-proxy >= 2
++++++++
++++++++ -- Yadd <yadd@debian.org> Thu, 23 Nov 2023 11:38:43 +0400
++++++++
++++++++node-undici (5.26.3+dfsg1+~cs23.10.12-2) unstable; urgency=medium
++++++++
++++++++ * Add wokaround to nodejs bug (unable to require node:*)
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 14 Oct 2023 14:49:40 +0400
++++++++
++++++++node-undici (5.26.3+dfsg1+~cs23.10.12-1) unstable; urgency=medium
++++++++
++++++++ * Embed @fastify/busboy
++++++++ * New upstream version (Closes: #1053879, CVE-2023-45143)
++++++++ * Unfuzz patches
++++++++ * Fix for clang 16 (Closes: #1052723)
++++++++ * Update copyright
++++++++ * Update lintian overrides
++++++++ * Update test
++++++++
++++++++ -- Yadd <yadd@debian.org> Fri, 13 Oct 2023 22:03:31 +0400
++++++++
++++++++node-undici (5.22.1+dfsg1+~cs20.10.10.2-1) unstable; urgency=medium
++++++++
++++++++ [ Michael R. Crusoe ]
++++++++ * Drop unused libsimde-dev from build dependencies
++++++++
++++++++ [ Yadd ]
++++++++ * Update wasm exclusion
++++++++ * New upstream version 5.22.1+dfsg1+~cs20.10.10.2
++++++++ * Update test
++++++++ * Refresh patches
++++++++ * Update lintian overrides
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 09 Jul 2023 15:08:57 +0400
++++++++
++++++++node-undici (5.19.1+dfsg1+~cs20.10.9.5-2) unstable; urgency=medium
++++++++
++++++++ * Disable network test on armel (Closes: #1032559)
++++++++
++++++++ -- Yadd <yadd@debian.org> Wed, 22 Mar 2023 10:50:17 +0400
++++++++
++++++++node-undici (5.19.1+dfsg1+~cs20.10.9.5-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version (Closes: #1031418, CVE-2023-23936, CVE-2023-24807)
++++++++ * Refresh patches
++++++++
++++++++ -- Yadd <yadd@debian.org> Fri, 17 Feb 2023 07:23:05 +0400
++++++++
++++++++node-undici (5.15.0+dfsg1+~cs20.10.9.3-1) unstable; urgency=medium
++++++++
++++++++ * Update standards version to 4.6.2, no changes needed.
++++++++ * New upstream version 5.15.0+dfsg1+~cs20.10.9.3
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 15 Jan 2023 10:04:29 +0400
++++++++
++++++++node-undici (5.14.0+dfsg1+~cs20.10.9-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.14.0+dfsg1+~cs20.10.9
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 11 Dec 2022 15:50:58 +0100
++++++++
++++++++node-undici (5.13.0+dfsg1+~cs20.10.9-1) unstable; urgency=medium
++++++++
++++++++ * Update lintian override info format in d/source/lintian-overrides
++++++++ on line 2-3.
++++++++ * New upstream version 5.13.0+dfsg1+~cs20.10.9
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 03 Dec 2022 18:14:54 +0100
++++++++
++++++++node-undici (5.12.0+dfsg1+~cs20.10.9-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.12.0+dfsg1+~cs20.10.9
++++++++ * Unfuzz patches
++++++++
++++++++ -- Yadd <yadd@debian.org> Fri, 28 Oct 2022 22:39:53 +0200
++++++++
++++++++node-undici (5.11.0+dfsg1+~cs20.10.9-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.11.0+dfsg1+~cs20.10.9
++++++++ * Update llhttp build
++++++++ * Unfuzz patches
++++++++ * Add dependency to node-busboy
++++++++
++++++++ -- Yadd <yadd@debian.org> Tue, 18 Oct 2022 06:25:29 +0200
++++++++
++++++++node-undici (5.10.0+dfsg1+~cs18.9.18.10-2) unstable; urgency=medium
++++++++
++++++++ * Launch tap test without timeout (may fix armel autopkgtest)
++++++++
++++++++ -- Yadd <yadd@debian.org> Wed, 14 Sep 2022 10:25:18 +0200
++++++++
++++++++node-undici (5.10.0+dfsg1+~cs18.9.18.10-1) unstable; urgency=medium
++++++++
++++++++ * Enable __proto__ in test, needed here for pkg-js-autopkgtest 0.15
++++++++ * New upstream version 5.10.0+dfsg1+~cs18.9.18.10
++++++++
++++++++ -- Yadd <yadd@debian.org> Mon, 12 Sep 2022 12:01:40 +0200
++++++++
++++++++node-undici (5.10.0+dfsg1+~cs18.9.18.7-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.10.0+dfsg1+~cs18.9.18.7
++++++++
++++++++ -- Yadd <yadd@debian.org> Tue, 30 Aug 2022 06:32:25 +0200
++++++++
++++++++node-undici (5.9.1+dfsg1+~cs18.9.18.1-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.9.1+dfsg1+~cs18.9.18.1
++++++++
++++++++ -- Yadd <yadd@debian.org> Mon, 22 Aug 2022 09:34:29 +0200
++++++++
++++++++node-undici (5.8.2+dfsg1+~cs18.9.18.1-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.8.2+dfsg1+~cs18.9.18.1
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 13 Aug 2022 07:06:18 +0200
++++++++
++++++++node-undici (5.8.1+dfsg1+~cs18.9.18.1-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.8.1+dfsg1+~cs18.9.18.1
++++++++ * Refresh patches
++++++++ * Update test modules
++++++++
++++++++ -- Yadd <yadd@debian.org> Mon, 08 Aug 2022 21:01:44 +0200
++++++++
++++++++node-undici (5.8.0+dfsg1+~cs18.9.16-2) unstable; urgency=medium
++++++++
++++++++ * Update typescript patch (Closes: #1016322)
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 30 Jul 2022 15:17:27 +0200
++++++++
++++++++node-undici (5.8.0+dfsg1+~cs18.9.16-1) unstable; urgency=medium
++++++++
++++++++ * Apply multi-arch hints (foreign)
++++++++ * New upstream version 5.8.0+dfsg1+~cs18.9.16
++++++++
++++++++ -- Yadd <yadd@debian.org> Tue, 19 Jul 2022 15:55:13 +0200
++++++++
++++++++node-undici (5.7.0+dfsg1+~cs18.9.16-3) unstable; urgency=medium
++++++++
++++++++ * Fix bad control field
++++++++
++++++++ -- Yadd <yadd@debian.org> Thu, 14 Jul 2022 21:59:46 +0200
++++++++
++++++++node-undici (5.7.0+dfsg1+~cs18.9.16-2) unstable; urgency=medium
++++++++
++++++++ * node-llhttp: add dependencies to node-debug and node-semver
++++++++ * Use custom autopkgtest since lld is not available on s390x arch
++++++++
++++++++ -- Yadd <yadd@debian.org> Thu, 14 Jul 2022 10:46:45 +0200
++++++++
++++++++node-undici (5.7.0+dfsg1+~cs18.9.16-1) unstable; urgency=medium
++++++++
++++++++ * New upstream version 5.7.0+dfsg1+~cs18.9.16
++++++++
++++++++ -- Yadd <yadd@debian.org> Thu, 14 Jul 2022 08:30:12 +0200
++++++++
++++++++node-undici (5.6.1+dfsg1+~cs18.9.16-1) unstable; urgency=medium
++++++++
++++++++ * New undici version (Closes: CVE-2022-32210)
++++++++ * Add patch to drop SSL tests with too short key
++++++++
++++++++ -- Yadd <yadd@debian.org> Sun, 10 Jul 2022 18:29:46 +0200
++++++++
++++++++node-undici (5.2.0+dfsg1+~cs18.9.15.10-1) unstable; urgency=medium
++++++++
++++++++ [ Yadd, Jérémy Lal ]
++++++++ * Initial release (Closes: #1010470)
++++++++
++++++++ -- Yadd <yadd@debian.org> Sat, 11 Jun 2022 18:54:32 +0200
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++deps/
++++++++lib/llhttp/llhttp.wasm
++++++++lib/llhttp/llhttp-wasm*
++++++++llhttp/build/
++++++++llhttp/lib/
++++++++llhttp/release/
++++++++llhttp/src/Makefile
++++++++llhttp/src/llhttp.Makefile
++++++++llhttp/src/llhttp.target.mk
++++++++llparse/lib/
++++++++llparse-builder/lib/
++++++++llparse-frontend/lib/
++++++++undici-fetch.js
++++++++types/LICENSE
++++++++types/package.json
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Source: node-undici
++++++++Section: javascript
++++++++Priority: optional
++++++++Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org>
++++++++Uploaders: Yadd <yadd@debian.org>
++++++++Build-Depends: debhelper-compat (= 13)
++++++++ , dh-sequence-nodejs (>= 0.14.22~)
++++++++ , clang (>= 1:16)
++++++++ , cmake
++++++++ , lld (>= 1:16)
++++++++ , node-debug
++++++++ , node-esbuild
++++++++ , node-semver
++++++++ , node-typescript
++++++++ , ts-node
++++++++ , wasi-libc
++++++++Standards-Version: 4.6.2
++++++++Homepage: https://undici.nodejs.org
++++++++Vcs-Git: https://salsa.debian.org/js-team/node-undici.git
++++++++Vcs-Browser: https://salsa.debian.org/js-team/node-undici
++++++++Rules-Requires-Root: no
++++++++
++++++++Package: node-undici
++++++++Architecture: all
++++++++Depends: ${misc:Depends}
++++++++Provides: ${nodeUndici:Provides}
++++++++Multi-Arch: foreign
++++++++Description: Node.js HTTP/1.1 client
++++++++ undici provides the Node.js core HTTP client, using WebAssembly to achieve
++++++++ performance and pluggability. It brings features like:
++++++++ - redirect support
++++++++ - native mocking layer using MockAgent
++++++++ - Client, Pool, Agent are unified by a Dispatcher API
++++++++
++++++++Package: node-llhttp
++++++++Architecture: all
++++++++Depends: ${misc:Depends}
++++++++ , node-debug
++++++++ , node-semver
++++++++Provides: ${nodeLlhttp:Provides}
++++++++Multi-Arch: foreign
++++++++Description: HTTP client sources for Node.js
++++++++ llhttp provides sources needed to build Node.js. May not be used directly.
++++++++
++++++++Package: libllhttp-dev
++++++++Section: libdevel
++++++++Architecture: any
++++++++Depends: ${misc:Depends}
++++++++ , libllhttp9.1 (= ${libllhttp:Version})
++++++++Description: HTTP messages parser library dev files
++++++++ libllhttp development files
++++++++
++++++++Package: libllhttp9.1
++++++++Section: libs
++++++++Architecture: any
++++++++Depends: ${misc:Depends}
++++++++ , ${shlibs:Depends}
++++++++Description: HTTP messages parser library
++++++++ libllhttp provides the Node.js library used to parse HTTP messages.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
++++++++Upstream-Name: undici
++++++++Upstream-Contact: https://github.com/nodejs/undici/issues
++++++++Source: https://undici.nodejs.org
++++++++ https://github.com/nodejs/llhttp/tags
++++++++ https://github.com/nodejs/llparse/tags
++++++++ https://github.com/indutny/llparse-frontend/tags
++++++++ https://github.com/indutny/llparse-builder/tags
++++++++ https://github.com/darkskyapp/binary-search/tags
++++++++ https://github.com/fastify/busboy/tags
++++++++Files-Excluded: deps
++++++++ lib/llhttp/*.map
++++++++ lib/llhttp/*.wasm
++++++++ lib/llhttp/*wasm.js
++++++++
++++++++Files: *
++++++++Copyright: Matteo Collina and Undici contributors <hello@matteocollina.com>
++++++++License: Expat
++++++++
++++++++Files: binary-search/*
++++++++Copyright: Dark Sky Company
++++++++ LLC
++++++++License: CC0-1.0
++++++++
++++++++Files: debian/*
++++++++Copyright: 2022-2024 Yadd <yadd@debian.org>
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/abort-controller/*
++++++++ debian/tests/test_modules/event-target-shim/*
++++++++Copyright: 2015-2017 Toru Nagashima
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/atomic-sleep/*
++++++++Copyright: 2020 David Mark Clements
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/chai-iterator/*
++++++++Copyright: 2016-2017 Akim McMath
++++++++ 2018 Harry Sarson
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/chai-string/*
++++++++Copyright: 2013 Oleg Nechiporenko
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/delay/*
++++++++Copyright: Sindre Sorhus <sindresorhus@gmail.com>
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/dicer/*
++++++++ fastify-busboy/*
++++++++Copyright: Brian White
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/https-pem/*
++++++++Copyright: 2016-2017 Thomas Watson Steen
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/node-forge/*
++++++++Copyright: 2010-2019 Digital Bazaar, Inc.
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/baseN.js
++++++++Copyright: 2016 base-x contributors
++++++++License: Expat
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/des.js
++++++++Copyright: 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ 2012-2014 Digital Bazaar, Inc.
++++++++ 2001 Paul Tero
++++++++ 2001 Michael Hayworth
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++Comment: Optimised for performance with large blocks by
++++++++ Michael Hayworth <http://www.netdealing.com> under the following license:
++++++++ THIS SOFTWARE IS PROVIDED "AS IS" AND
++++++++ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++++++++ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++++++++ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
++++++++ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++++++++ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++++++++ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++++++++ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++++++++ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++++++++ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++++++++ SUCH DAMAGE.
++++++++
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/ed25519.js
++++++++Copyright: 2017-2019 Digital Bazaar, Inc.
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++Comment: This implementation is based on the most excellent TweetNaCl which is
++++++++ in the public domain. Many thanks to its contributors
++++++++ https://github.com/dchest/tweetnacl-js
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/jsbn.js
++++++++Copyright: 2005 Tom Wu <tjw@cs.Stanford.EDU>
++++++++License: Expat-Jsbn
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/kem.js
++++++++Copyright: 2014 Lautaro Cozzani <lautaro.cozzani@scytl.com>
++++++++ 2014 Digital Bazaar, Inc.
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/mgf.js
++++++++ debian/tests/test_modules/node-forge/lib/pss.js
++++++++ debian/tests/test_modules/node-forge/lib/rc2.js
++++++++Copyright: 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/mgf1.js
++++++++ debian/tests/test_modules/node-forge/lib/pbe.js
++++++++ debian/tests/test_modules/node-forge/lib/pkcs12.js
++++++++ debian/tests/test_modules/node-forge/lib/pkcs7.js
++++++++ debian/tests/test_modules/node-forge/lib/pkcs7asn1.js
++++++++Copyright: 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ 2012-2015 Digital Bazaar, Inc.
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++
++++++++Files: debian/tests/test_modules/node-forge/lib/pkcs1.js
++++++++Copyright: 2012 Kenji Urushima
++++++++ 2013-2014 Digital Bazaar, Inc.
++++++++License: BSD-3-Clause-DB or GPL-2
++++++++Comment: Modified but based on the following MIT and BSD licensed code:
++++++++ https://github.com/kjur/jsjws/blob/master/rsa.js
++++++++ Copyright (c) 2012 Kenji Urushima
++++++++
++++++++Files: debian/tests/test_modules/selfsigned/*
++++++++Copyright: 2013 José F. Romaniello
++++++++License: Expat
++++++++
++++++++Files: fastify-busboy/bench/dicer/parted-multipart.js
++++++++Copyright: 2011 Christopher Jeffrey
++++++++License: Expat
++++++++
++++++++Files: lib/fetch/*
++++++++Copyright: 2020 Ethan Arrowood
++++++++License: Expat
++++++++
++++++++Files: lib/websocket/receiver.js
++++++++Copyright: 2011 Einar Otto Stangvik <einaros@gmail.com>
++++++++ 2013 Arnout Kazemier and contributors
++++++++ 2016 Luigi Pinca and contributors
++++++++License: Expat
++++++++
++++++++Files: llhttp/*
++++++++ llparse/*
++++++++ llparse-builder/*
++++++++ llparse-frontend/*
++++++++Copyright: 2018-2020 Fedor Indutny
++++++++License: Expat
++++++++
++++++++Files: test/cookie/cookies.js
++++++++Copyright: 2018-2022 the Deno authors
++++++++License: Expat
++++++++
++++++++Files: test/node-fetch/*
++++++++Copyright: 2016-2020 Node Fetch Team
++++++++License: Expat
++++++++
++++++++Files: test/wpt/tests/*
++++++++Copyright: web-platform-tests contributors
++++++++License: BSD-3-Clause
++++++++
++++++++Files: test/wpt/tests/resources/chromium/contacts_manager_mock.js
++++++++Copyright: 2018 The Chromium Authors
++++++++License: BSD-3-Clause
++++++++
++++++++License: BSD-3-Clause
++++++++ Redistribution and use in source and binary forms, with or without
++++++++ modification, are permitted provided that the following conditions are met:
++++++++ .
++++++++ 1. Redistributions of source code must retain the above copyright notice, this
++++++++ list of conditions and the following disclaimer.
++++++++ 2. Redistributions in binary form must reproduce the above copyright notice,
++++++++ this list of conditions and the following disclaimer in the documentation
++++++++ and/or other materials provided with the distribution.
++++++++ 3. Neither the name of the copyright holder nor the names of its contributors
++++++++ may be used to endorse or promote products derived from this software
++++++++ without specific prior written permission.
++++++++ .
++++++++ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
++++++++ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++++++++ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
++++++++ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
++++++++ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++++++++ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
++++++++ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
++++++++ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
++++++++ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
++++++++ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
++++++++
++++++++License: BSD-3-Clause-DB
++++++++ Redistribution and use in source and binary forms, with or without
++++++++ modification, are permitted provided that the following conditions are met:
++++++++ * Redistributions of source code must retain the above copyright
++++++++ notice, this list of conditions and the following disclaimer.
++++++++ * Redistributions in binary form must reproduce the above copyright
++++++++ notice, this list of conditions and the following disclaimer in the
++++++++ documentation and/or other materials provided with the distribution.
++++++++ * Neither the name of Digital Bazaar, Inc. nor the
++++++++ names of its contributors may be used to endorse or promote products
++++++++ derived from this software without specific prior written permission.
++++++++ .
++++++++ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
++++++++ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
++++++++ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
++++++++ DISCLAIMED. IN NO EVENT SHALL DIGITAL BAZAAR BE LIABLE FOR ANY
++++++++ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
++++++++ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
++++++++ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
++++++++ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
++++++++ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
++++++++ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
++++++++
++++++++License: CC0-1.0
++++++++ Any copyright is dedicated to the Public Domain.
++++++++ .
++++++++ 1. Copyright and Related Rights.
++++++++ .
++++++++ A Work made available under CC0 may be protected by copyright and related or
++++++++ neighboring rights ("Copyright and Related Rights"). Copyright and Related
++++++++ Rights include, but are not limited to, the following:
++++++++ .
++++++++ i. the right to reproduce, adapt, distribute, perform, display, communicate,
++++++++ and translate a Work;
++++++++ .
++++++++ ii. moral rights retained by the original author(s) and/or performer(s);
++++++++ .
++++++++ iii. publicity and privacy rights pertaining to a person's image or likeness
++++++++ depicted in a Work;
++++++++ .
++++++++ iv. rights protecting against unfair competition in regards to a Work, subject
++++++++ to the limitations in paragraph 4(a), below;
++++++++ .
++++++++ v. rights protecting the extraction, dissemination, use and reuse of data in
++++++++ a Work;
++++++++ .
++++++++ vi. database rights (such as those arising under Directive 96/9/EC of the
++++++++ European Parliament and of the Council of 11 March 1996 on the legal
++++++++ protection of databases, and under any national implementation thereof,
++++++++ including any amended or successor version of such directive); and
++++++++ .
++++++++ vii. other similar, equivalent or corresponding rights throughout the world
++++++++ based on applicable law or treaty, and any national implementations
++++++++ thereof.
++++++++ .
++++++++ 2. Waiver.
++++++++ .
++++++++ To the greatest extent permitted by, but not in contravention of, applicable
++++++++ law, Affirmer hereby overtly, fully, permanently, irrevocably and
++++++++ unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
++++++++ and Related Rights and associated claims and causes of action, whether now
++++++++ known or unknown (including existing as well as future claims and causes of
++++++++ action), in the Work (i) in all territories worldwide, (ii) for the maximum
++++++++ duration provided by applicable law or treaty (including future time
++++++++ extensions), (iii) in any current or future medium and for any number of
++++++++ copies, and (iv) for any purpose whatsoever, including without limitation
++++++++ commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
++++++++ the Waiver for the benefit of each member of the public at large and to the
++++++++ detriment of Affirmer's heirs and successors, fully intending that such Waiver
++++++++ shall not be subject to revocation, rescission, cancellation, termination, or
++++++++ any other legal or equitable action to disrupt the quiet enjoyment of the Work
++++++++ by the public as contemplated by Affirmer's express Statement of Purpose.
++++++++ .
++++++++ 3. Public License Fallback.
++++++++ .
++++++++ Should any part of the Waiver for any reason be judged legally invalid or
++++++++ ineffective under applicable law, then the Waiver shall be preserved to the
++++++++ maximum extent permitted taking into account Affirmer's express Statement of
++++++++ Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby
++++++++ grants to each affected person a royalty-free, non transferable, non
++++++++ sublicensable, non exclusive, irrevocable and unconditional license to exercise
++++++++ Affirmer's Copyright and Related Rights in the Work (i) in all territories
++++++++ worldwide, (ii) for the maximum duration provided by applicable law or treaty
++++++++ (including future time extensions), (iii) in any current or future medium and
++++++++ for any number of copies, and (iv) for any purpose whatsoever, including
++++++++ without limitation commercial, advertising or promotional purposes (the
++++++++ "License"). The License shall be deemed effective as of the date CC0 was
++++++++ applied by Affirmer to the Work. Should any part of the License for any reason
++++++++ be judged legally invalid or ineffective under applicable law, such partial
++++++++ invalidity or ineffectiveness shall not invalidate the remainder of the
++++++++ License, and in such case Affirmer hereby affirms that he or she will not
++++++++ (i) exercise any of his or her remaining Copyright and Related Rights in the
++++++++ Work or (ii) assert any associated claims and causes of action with respect to
++++++++ the Work, in either case contrary to Affirmer's express Statement of Purpose.
++++++++ .
++++++++ 4. Limitations and Disclaimers.
++++++++ .
++++++++ a. No trademark or patent rights held by Affirmer are waived, abandoned,
++++++++ surrendered, licensed or otherwise affected by this document.
++++++++ .
++++++++ b. Affirmer offers the Work as-is and makes no representations or warranties of
++++++++ any kind concerning the Work, express, implied, statutory or otherwise,
++++++++ including without limitation warranties of title, merchantability, fitness
++++++++ for a particular purpose, non infringement, or the absence of latent or
++++++++ other defects, accuracy, or the present or absence of errors, whether or not
++++++++ discoverable, all to the greatest extent permissible under applicable law.
++++++++ .
++++++++ c. Affirmer disclaims responsibility for clearing rights of other persons that
++++++++ may apply to the Work or any use thereof, including without limitation any
++++++++ person's Copyright and Related Rights in the Work. Further, Affirmer
++++++++ disclaims responsibility for obtaining any necessary consents, permissions
++++++++ or other rights required for any use of the Work.
++++++++ .
++++++++ d. Affirmer understands and acknowledges that Creative Commons is not a party
++++++++ to this document and has no duty or obligation with respect to this CC0 or
++++++++ use of the Work.
++++++++
++++++++License: Expat
++++++++ Permission is hereby granted, free of charge, to any person
++++++++ obtaining a copy of this software and associated documentation files
++++++++ (the "Software"), to deal in the Software without restriction,
++++++++ including without limitation the rights to use, copy, modify, merge,
++++++++ publish, distribute, sublicense, and/or sell copies of the Software,
++++++++ and to permit persons to whom the Software is furnished to do so,
++++++++ subject to the following conditions:
++++++++ .
++++++++ The above copyright notice and this permission notice shall be
++++++++ included in all copies or substantial portions of the Software.
++++++++ .
++++++++ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
++++++++ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
++++++++ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
++++++++ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
++++++++ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
++++++++ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++ SOFTWARE.
++++++++
++++++++License: Expat-Jsbn
++++++++ Permission is hereby granted, free of charge, to any person obtaining
++++++++ a copy of this software and associated documentation files (the
++++++++ "Software"), to deal in the Software without restriction, including
++++++++ without limitation the rights to use, copy, modify, merge, publish,
++++++++ distribute, sublicense, and/or sell copies of the Software, and to
++++++++ permit persons to whom the Software is furnished to do so, subject to
++++++++ the following conditions:
++++++++ .
++++++++ The above copyright notice and this permission notice shall be
++++++++ included in all copies or substantial portions of the Software.
++++++++ .
++++++++ THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
++++++++ EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
++++++++ WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
++++++++ .
++++++++ IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
++++++++ INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
++++++++ RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
++++++++ THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
++++++++ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
++++++++ .
++++++++ In addition, the following condition applies:
++++++++ .
++++++++ All redistributions must retain an intact copy of this copyright notice
++++++++ and disclaimer.
++++++++
++++++++License: GPL-2
++++++++ This program is free software; you can redistribute it and/or modify
++++++++ it under the terms of the GNU General Public License as published by
++++++++ the Free Software Foundation; version 2.
++++++++ .
++++++++ On Debian systems, the complete text of version 2 of the GNU General
++++++++ Public License can be found in `/usr/share/common-licenses/GPL-2'
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++[DEFAULT]
++++++++pristine-tar=True
++++++++filter=[ '.gitignore', '.travis.yml', '.git*' ]
++++++++component=['llhttp', 'llparse', 'llparse-frontend', 'llparse-builder', 'binary-search', 'fastify-busboy']
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llhttp/README.md
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++usr/include
++++++++usr/lib/*/*.a
++++++++usr/lib/*/*.so
++++++++#llhttp/release/include/* usr/share/include/llhttp/
++++++++#llhttp/release/src/* usr/share/llhttp/
++++++++usr/lib/*/cmake/
++++++++usr/lib/*/pkgconfig/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llhttp/CODE_OF_CONDUCT.md
++++++++llhttp/README.md
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++usr/lib/*/*.so.*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llhttp/README.md
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llhttp/examples/*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++usr/share/nodejs/ll*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++*.md
++++++++docs/*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++examples/*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++usr/share/nodejs/binary-search
++++++++usr/share/nodejs/@fastify/busboy
++++++++usr/share/nodejs/undici
++++++++usr/share/nodejs/undici-types
++++++++undici-fetch.js usr/share/nodejs/undici/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# dir_to_symlink pathname new-target [prior-version [package]]
++++++++dir_to_symlink /usr/share/nodejs/undici/types /usr/share/nodejs/undici-types 5.28.2+dfsg1+~cs23.11.12.3-2~ node-undici
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++types
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node scripts/generate-undici-types-package-json.js
++++++++
++++++++mkdir -p deps/llhttp/include deps/llhttp/src
++++++++cp llhttp/build/c/llhttp.c deps/llhttp/src/
++++++++cp llhttp/src/native/*.c deps/llhttp/src/
++++++++cp llhttp/build/llhttp.h deps/llhttp/include/
++++++++CLANG=/usr/bin/clang node build/wasm.js
++++++++
++++++++# Build bundle
++++++++esbuild index-fetch.js --bundle --platform=node --outfile=undici-fetch.js --define:esbuildDetection=1 --keep-names
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llparse-builder llparse-frontend
++++++++llparse-frontend llparse
++++++++llparse llhttp
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++@types/debug
++++++++@types/node
++++++++@types/semver
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++undici-types undici/types
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++ts-node bin/generate.ts
++++++++tsc
++++++++export RELEASE=`pkgjs-pjson . version`
++++++++WASI_ROOT=/usr ts-node bin/build_wasm.ts
++++++++export CFLAGS="$CFLAGS -gdwarf-4"
++++++++export LDFLAGS="$LDFLAGS"
++++++++make release build/libllhttp.a build/libllhttp.so
++++++++(cd ..;dh_auto_configure --buildsystem=cmake -Dllhttp/release -- -DCMAKE_BUILD_TYPE=Release)
++++++++(cd ..;dh_auto_build --buildsystem=cmake)
++++++++find . -type f
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++tsc
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++tsc
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++tsc
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: declare SONAME
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2024-01-18
++++++++
++++++++--- a/llhttp/Makefile
+++++++++++ b/llhttp/Makefile
++++++++@@ -1,6 +1,7 @@
++++++++ CLANG ?= clang
++++++++ CFLAGS ?=
++++++++ OS ?=
+++++++++SONAME ?=
++++++++
++++++++ CFLAGS += -Os -g3 -Wall -Wextra -Wno-unused-parameter
++++++++ ifneq ($(OS),Windows_NT)
++++++++@@ -23,7 +24,7 @@
++++++++
++++++++ build/libllhttp.so: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++- $(CLANG) -shared $^ -o $@
+++++++++ $(CLANG) -shared $^ -Wl,-soname,$(SONAME) -o $@
++++++++
++++++++ build/libllhttp.a: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++@@ -88,6 +89,8 @@
++++++++ $(INSTALL) -d $(DESTDIR)$(LIBDIR)
++++++++ $(INSTALL) -C build/llhttp.h $(DESTDIR)$(INCLUDEDIR)/llhttp.h
++++++++ $(INSTALL) -C build/libllhttp.a $(DESTDIR)$(LIBDIR)/libllhttp.a
++++++++- $(INSTALL) build/libllhttp.so $(DESTDIR)$(LIBDIR)/libllhttp.so
+++++++++ $(INSTALL) build/libllhttp.so $(DESTDIR)$(LIBDIR)/$(SONAME)
+++++++++ ln -s $(SONAME) $(DESTDIR)$(LIBDIR)/$(SONAMEALIAS)
+++++++++ ln -s $(SONAME) $(DESTDIR)$(LIBDIR)/libllhttp.so
++++++++
++++++++ .PHONY: all generate clean release postversion github-release
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: disable SIMD build
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2022-07-09
++++++++
++++++++--- a/build/wasm.js
+++++++++++ b/build/wasm.js
++++++++@@ -66,6 +66,7 @@
++++++++ `module.exports = '${base64Wasm}'\n`
++++++++ )
++++++++
+++++++++/*
++++++++ // Build wasm simd binary
++++++++ execSync(`${CLANG} \
++++++++ -nodefaultlibs \
++++++++@@ -96,3 +97,4 @@
++++++++ join(WASM_OUT, 'llhttp_simd-wasm.js'),
++++++++ `module.exports = '${base64WasmSimd}'\n`
++++++++ )
+++++++++*/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: don't rebuild on install
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2024-01-18
++++++++
++++++++--- a/llhttp/Makefile
+++++++++++ b/llhttp/Makefile
++++++++@@ -83,7 +83,7 @@
++++++++ generate:
++++++++ ts-node bin/generate.ts
++++++++
++++++++-install: build/libllhttp.a build/libllhttp.so
+++++++++install:
++++++++ $(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)
++++++++ $(INSTALL) -d $(DESTDIR)$(LIBDIR)
++++++++ $(INSTALL) -C build/llhttp.h $(DESTDIR)$(INCLUDEDIR)/llhttp.h
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: drop one error test that fail under Debian env
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2023-11-26
++++++++
++++++++--- a/test/client-dispatch.js
+++++++++++ b/test/client-dispatch.js
++++++++@@ -782,34 +782,3 @@
++++++++ })
++++++++ })
++++++++ })
++++++++-
++++++++-test('dispatch onBodySent throws error', (t) => {
++++++++- const server = http.createServer((req, res) => {
++++++++- res.end('ended')
++++++++- })
++++++++- t.teardown(server.close.bind(server))
++++++++-
++++++++- server.listen(0, () => {
++++++++- const client = new Pool(`http://localhost:${server.address().port}`)
++++++++- t.teardown(client.close.bind(client))
++++++++- const body = 'hello'
++++++++- client.dispatch({
++++++++- path: '/',
++++++++- method: 'POST',
++++++++- body
++++++++- }, {
++++++++- onBodySent (chunk) {
++++++++- throw new Error('fail')
++++++++- },
++++++++- onError (err) {
++++++++- t.type(err, Error)
++++++++- t.equal(err.message, 'fail')
++++++++- t.end()
++++++++- },
++++++++- onConnect () {},
++++++++- onHeaders () {},
++++++++- onData () {},
++++++++- onComplete () {}
++++++++- })
++++++++- })
++++++++-})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: drop simd call
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2023-07-09
++++++++
++++++++--- a/lib/client.js
+++++++++++ b/lib/client.js
++++++++@@ -491,18 +491,7 @@
++++++++ async function lazyllhttp () {
++++++++ const llhttpWasmData = process.env.JEST_WORKER_ID ? require('./llhttp/llhttp-wasm.js') : undefined
++++++++
++++++++- let mod
++++++++- try {
++++++++- mod = await WebAssembly.compile(Buffer.from(require('./llhttp/llhttp_simd-wasm.js'), 'base64'))
++++++++- } catch (e) {
++++++++- /* istanbul ignore next */
++++++++-
++++++++- // We could check if the error was caused by the simd option not
++++++++- // being enabled, but the occurring of this other error
++++++++- // * https://github.com/emscripten-core/emscripten/issues/11495
++++++++- // got me to remove that check to avoid breaking Node 12.
++++++++- mod = await WebAssembly.compile(Buffer.from(llhttpWasmData || require('./llhttp/llhttp-wasm.js'), 'base64'))
++++++++- }
+++++++++ const mod = await WebAssembly.compile(Buffer.from(llhttpWasmData || require('./llhttp/llhttp-wasm.js'), 'base64'))
++++++++
++++++++ return await WebAssembly.instantiate(mod, {
++++++++ env: {
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: drop SSL tests: key too short
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: no
++++++++Last-Update: 2022-07-09
++++++++
++++++++--- a/test/proxy-agent.js
+++++++++++ b/test/proxy-agent.js
++++++++@@ -467,169 +467,6 @@
++++++++ t.end()
++++++++ })
++++++++
++++++++-test('Proxy via HTTP to HTTPS endpoint', async (t) => {
++++++++- t.plan(4)
++++++++-
++++++++- const server = await buildSSLServer()
++++++++- const proxy = await buildProxy()
++++++++-
++++++++- const serverUrl = `https://localhost:${server.address().port}`
++++++++- const proxyUrl = `http://localhost:${proxy.address().port}`
++++++++- const proxyAgent = new ProxyAgent({
++++++++- uri: proxyUrl,
++++++++- requestTls: {
++++++++- ca: [
++++++++- readFileSync(join(__dirname, 'fixtures', 'ca.pem'), 'utf8')
++++++++- ],
++++++++- key: readFileSync(join(__dirname, 'fixtures', 'client-key-2048.pem'), 'utf8'),
++++++++- cert: readFileSync(join(__dirname, 'fixtures', 'client-crt-2048.pem'), 'utf8'),
++++++++- servername: 'agent1'
++++++++- }
++++++++- })
++++++++-
++++++++- server.on('request', function (req, res) {
++++++++- t.ok(req.connection.encrypted)
++++++++- res.end(JSON.stringify(req.headers))
++++++++- })
++++++++-
++++++++- server.on('secureConnection', () => {
++++++++- t.pass('server should be connected secured')
++++++++- })
++++++++-
++++++++- proxy.on('secureConnection', () => {
++++++++- t.fail('proxy over http should not call secureConnection')
++++++++- })
++++++++-
++++++++- proxy.on('connect', function () {
++++++++- t.pass('proxy should be connected')
++++++++- })
++++++++-
++++++++- proxy.on('request', function () {
++++++++- t.fail('proxy should never receive requests')
++++++++- })
++++++++-
++++++++- const data = await request(serverUrl, { dispatcher: proxyAgent })
++++++++- const json = await data.body.json()
++++++++- t.strictSame(json, {
++++++++- host: `localhost:${server.address().port}`,
++++++++- connection: 'keep-alive'
++++++++- })
++++++++-
++++++++- server.close()
++++++++- proxy.close()
++++++++- proxyAgent.close()
++++++++-})
++++++++-
++++++++-test('Proxy via HTTPS to HTTPS endpoint', async (t) => {
++++++++- t.plan(5)
++++++++- const server = await buildSSLServer()
++++++++- const proxy = await buildSSLProxy()
++++++++-
++++++++- const serverUrl = `https://localhost:${server.address().port}`
++++++++- const proxyUrl = `https://localhost:${proxy.address().port}`
++++++++- const proxyAgent = new ProxyAgent({
++++++++- uri: proxyUrl,
++++++++- proxyTls: {
++++++++- ca: [
++++++++- readFileSync(join(__dirname, 'fixtures', 'ca.pem'), 'utf8')
++++++++- ],
++++++++- key: readFileSync(join(__dirname, 'fixtures', 'client-key-2048.pem'), 'utf8'),
++++++++- cert: readFileSync(join(__dirname, 'fixtures', 'client-crt-2048.pem'), 'utf8'),
++++++++- servername: 'agent1',
++++++++- rejectUnauthorized: false
++++++++- },
++++++++- requestTls: {
++++++++- ca: [
++++++++- readFileSync(join(__dirname, 'fixtures', 'ca.pem'), 'utf8')
++++++++- ],
++++++++- key: readFileSync(join(__dirname, 'fixtures', 'client-key-2048.pem'), 'utf8'),
++++++++- cert: readFileSync(join(__dirname, 'fixtures', 'client-crt-2048.pem'), 'utf8'),
++++++++- servername: 'agent1'
++++++++- }
++++++++- })
++++++++-
++++++++- server.on('request', function (req, res) {
++++++++- t.ok(req.connection.encrypted)
++++++++- res.end(JSON.stringify(req.headers))
++++++++- })
++++++++-
++++++++- server.on('secureConnection', () => {
++++++++- t.pass('server should be connected secured')
++++++++- })
++++++++-
++++++++- proxy.on('secureConnection', () => {
++++++++- t.pass('proxy over http should call secureConnection')
++++++++- })
++++++++-
++++++++- proxy.on('connect', function () {
++++++++- t.pass('proxy should be connected')
++++++++- })
++++++++-
++++++++- proxy.on('request', function () {
++++++++- t.fail('proxy should never receive requests')
++++++++- })
++++++++-
++++++++- const data = await request(serverUrl, { dispatcher: proxyAgent })
++++++++- const json = await data.body.json()
++++++++- t.strictSame(json, {
++++++++- host: `localhost:${server.address().port}`,
++++++++- connection: 'keep-alive'
++++++++- })
++++++++-
++++++++- server.close()
++++++++- proxy.close()
++++++++- proxyAgent.close()
++++++++-})
++++++++-
++++++++-test('Proxy via HTTPS to HTTP endpoint', async (t) => {
++++++++- t.plan(3)
++++++++- const server = await buildServer()
++++++++- const proxy = await buildSSLProxy()
++++++++-
++++++++- const serverUrl = `http://localhost:${server.address().port}`
++++++++- const proxyUrl = `https://localhost:${proxy.address().port}`
++++++++- const proxyAgent = new ProxyAgent({
++++++++- uri: proxyUrl,
++++++++- proxyTls: {
++++++++- ca: [
++++++++- readFileSync(join(__dirname, 'fixtures', 'ca.pem'), 'utf8')
++++++++- ],
++++++++- key: readFileSync(join(__dirname, 'fixtures', 'client-key-2048.pem'), 'utf8'),
++++++++- cert: readFileSync(join(__dirname, 'fixtures', 'client-crt-2048.pem'), 'utf8'),
++++++++- servername: 'agent1',
++++++++- rejectUnauthorized: false
++++++++- }
++++++++- })
++++++++-
++++++++- server.on('request', function (req, res) {
++++++++- t.ok(!req.connection.encrypted)
++++++++- res.end(JSON.stringify(req.headers))
++++++++- })
++++++++-
++++++++- server.on('secureConnection', () => {
++++++++- t.fail('server is http')
++++++++- })
++++++++-
++++++++- proxy.on('secureConnection', () => {
++++++++- t.pass('proxy over http should call secureConnection')
++++++++- })
++++++++-
++++++++- proxy.on('request', function () {
++++++++- t.fail('proxy should never receive requests')
++++++++- })
++++++++-
++++++++- const data = await request(serverUrl, { dispatcher: proxyAgent })
++++++++- const json = await data.body.json()
++++++++- t.strictSame(json, {
++++++++- host: `localhost:${server.address().port}`,
++++++++- connection: 'keep-alive'
++++++++- })
++++++++-
++++++++- server.close()
++++++++- proxy.close()
++++++++- proxyAgent.close()
++++++++-})
++++++++-
++++++++ test('Proxy via HTTP to HTTP endpoint', async (t) => {
++++++++ t.plan(3)
++++++++ const server = await buildServer()
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix for node-proxy >= 2
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2023-11-23
++++++++
++++++++--- a/test/proxy-agent.js
+++++++++++ b/test/proxy-agent.js
++++++++@@ -10,7 +10,7 @@
++++++++ const Pool = require('../lib/pool')
++++++++ const { createServer } = require('http')
++++++++ const https = require('https')
++++++++-const proxy = require('proxy')
+++++++++const proxy = require('proxy').createProxy
++++++++
++++++++ test('should throw error when no uri is provided', (t) => {
++++++++ t.plan(2)
++++++++--- a/test/proxy.js
+++++++++++ b/test/proxy.js
++++++++@@ -3,7 +3,7 @@
++++++++ const { test } = require('tap')
++++++++ const { Client, Pool } = require('..')
++++++++ const { createServer } = require('http')
++++++++-const proxy = require('proxy')
+++++++++const proxy = require('proxy').createProxy
++++++++
++++++++ test('connect through proxy', async (t) => {
++++++++ t.plan(3)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix tap test
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: no
++++++++Last-Update: 2022-05-17
++++++++
++++++++--- a/test/unix.js
+++++++++++ b/test/unix.js
++++++++@@ -94,48 +94,4 @@
++++++++ })
++++++++ })
++++++++
++++++++- test('https get with tls opts', (t) => {
++++++++- t.plan(6)
++++++++-
++++++++- const server = https.createServer(pem, (req, res) => {
++++++++- t.equal('/', req.url)
++++++++- t.equal('GET', req.method)
++++++++- res.setHeader('content-type', 'text/plain')
++++++++- res.end('hello')
++++++++- })
++++++++- t.teardown(server.close.bind(server))
++++++++-
++++++++- try {
++++++++- fs.unlinkSync('/var/tmp/test3.sock')
++++++++- } catch (err) {
++++++++-
++++++++- }
++++++++-
++++++++- server.listen('/var/tmp/test8.sock', () => {
++++++++- const client = new Client({
++++++++- hostname: 'localhost',
++++++++- protocol: 'https:'
++++++++- }, {
++++++++- socketPath: '/var/tmp/test8.sock',
++++++++- tls: {
++++++++- rejectUnauthorized: false
++++++++- }
++++++++- })
++++++++- t.teardown(client.close.bind(client))
++++++++-
++++++++- client.request({ path: '/', method: 'GET' }, (err, data) => {
++++++++- t.error(err)
++++++++- const { statusCode, headers, body } = data
++++++++- t.equal(statusCode, 200)
++++++++- t.equal(headers['content-type'], 'text/plain')
++++++++- const bufs = []
++++++++- body.on('data', (buf) => {
++++++++- bufs.push(buf)
++++++++- })
++++++++- body.on('end', () => {
++++++++- t.equal('hello', Buffer.concat(bufs).toString('utf8'))
++++++++- })
++++++++- })
++++++++- })
++++++++- })
++++++++ }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix llhttp version
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2024-01-19
++++++++
++++++++--- a/llhttp/CMakeLists.txt
+++++++++++ b/llhttp/CMakeLists.txt
++++++++@@ -1,7 +1,7 @@
++++++++ cmake_minimum_required(VERSION 3.5.1)
++++++++ cmake_policy(SET CMP0069 NEW)
++++++++
++++++++-project(llhttp VERSION _RELEASE_)
+++++++++project(llhttp VERSION 9.1.3)
++++++++ include(GNUInstallDirs)
++++++++
++++++++ set(CMAKE_C_STANDARD 99)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix typescript
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: no
++++++++Last-Update: 2024-04-05
++++++++
++++++++--- a/llhttp/bin/build_wasm.ts
+++++++++++ b/llhttp/bin/build_wasm.ts
++++++++@@ -40,6 +40,7 @@
++++++++ // It will work flawessly if uid === gid === 1000
++++++++ // there will be some warnings otherwise.
++++++++ if (process.platform === 'linux') {
+++++++++// @ts-ignore
++++++++ cmd += ` --user ${process.getuid!()}:${process.getegid!()}`;
++++++++ }
++++++++ cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`;
++++++++--- a/llparse/src/implementation/c/code/base.ts
+++++++++++ b/llparse/src/implementation/c/code/base.ts
++++++++@@ -3,7 +3,7 @@
++++++++ import { Compilation } from '../compilation';
++++++++
++++++++ export abstract class Code<T extends frontend.code.Code> {
++++++++- protected cachedDecl: string | undefined;
+++++++++ public cachedDecl: string | undefined;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++--- a/llparse/src/implementation/c/node/base.ts
+++++++++++ b/llparse/src/implementation/c/node/base.ts
++++++++@@ -13,8 +13,8 @@
++++++++ }
++++++++
++++++++ export abstract class Node<T extends frontend.node.Node> {
++++++++- protected cachedDecl: string | undefined;
++++++++- protected privCompilation: Compilation | undefined;
+++++++++ public cachedDecl: string | undefined;
+++++++++ public privCompilation: Compilation | undefined;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++@@ -39,12 +39,12 @@
++++++++ return res;
++++++++ }
++++++++
++++++++- protected get compilation(): Compilation {
+++++++++ public get compilation(): Compilation {
++++++++ assert(this.privCompilation !== undefined);
++++++++ return this.privCompilation!;
++++++++ }
++++++++
++++++++- protected prologue(out: string[]): void {
+++++++++ public prologue(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ out.push(`if (${ctx.posArg()} == ${ctx.endPosArg()}) {`);
++++++++@@ -56,11 +56,11 @@
++++++++ out.push('}');
++++++++ }
++++++++
++++++++- protected pause(out: string[]): void {
+++++++++ public pause(out: string[]): void {
++++++++ out.push(`return ${this.cachedDecl};`);
++++++++ }
++++++++
++++++++- protected tailTo(out: string[], edge: INodeEdge): void {
+++++++++ public tailTo(out: string[], edge: INodeEdge): void {
++++++++ const ctx = this.compilation;
++++++++ const target = ctx.unwrapNode(edge.node).build(ctx);
++++++++
++++++++--- a/llparse/src/implementation/c/node/error.ts
+++++++++++ b/llparse/src/implementation/c/node/error.ts
++++++++@@ -5,7 +5,7 @@
++++++++ import { Node } from './base';
++++++++
++++++++ class ErrorNode<T extends frontend.node.Error> extends Node<T> {
++++++++- protected storeError(out: string[]): void {
+++++++++ public storeError(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ let hexCode: string;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix wasm build
++++++++Author: Jérémy Lal <kapouer@melix.org>
++++++++Forwarded: not-needed
++++++++Reviewed-By: Yadd <yadd@debian.org>
++++++++Last-Update: 2024-04-05
++++++++
++++++++--- a/build/wasm.js
+++++++++++ b/build/wasm.js
++++++++@@ -8,6 +8,7 @@
++++++++ const WASM_SRC = resolve(__dirname, '../deps/llhttp')
++++++++ const WASM_OUT = resolve(__dirname, '../lib/llhttp')
++++++++ const DOCKERFILE = resolve(__dirname, './Dockerfile')
+++++++++const { CLANG } = process.env
++++++++
++++++++ let platform = process.env.WASM_PLATFORM
++++++++ if (!platform && process.argv[2]) {
++++++++@@ -35,22 +36,16 @@
++++++++ process.exit(0)
++++++++ }
++++++++
++++++++-// Gather information about the tools used for the build
++++++++-const buildInfo = execSync('apk info -v').toString()
++++++++-if (!buildInfo.includes('wasi-sdk')) {
++++++++- console.log('Failed to generate build environment information')
++++++++- process.exit(-1)
++++++++-}
++++++++-writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo)
++++++++-
++++++++ // Build wasm binary
++++++++-execSync(`clang \
++++++++- --sysroot=/usr/share/wasi-sysroot \
+++++++++execSync(`${CLANG} \
+++++++++ -nodefaultlibs \
+++++++++ --sysroot=/usr \
++++++++ -target wasm32-unknown-wasi \
++++++++ -Ofast \
++++++++ -fno-exceptions \
++++++++ -fvisibility=hidden \
++++++++ -mexec-model=reactor \
+++++++++ -Wl,-lc \
++++++++ -Wl,-error-limit=0 \
++++++++ -Wl,-O3 \
++++++++ -Wl,--lto-O3 \
++++++++@@ -72,14 +67,16 @@
++++++++ )
++++++++
++++++++ // Build wasm simd binary
++++++++-execSync(`clang \
++++++++- --sysroot=/usr/share/wasi-sysroot \
+++++++++execSync(`${CLANG} \
+++++++++ -nodefaultlibs \
+++++++++ --sysroot=/usr \
++++++++ -target wasm32-unknown-wasi \
++++++++ -msimd128 \
++++++++ -Ofast \
++++++++ -fno-exceptions \
++++++++ -fvisibility=hidden \
++++++++ -mexec-model=reactor \
+++++++++ -Wl,-lc \
++++++++ -Wl,-error-limit=0 \
++++++++ -Wl,-O3 \
++++++++ -Wl,--lto-O3 \
++++++++--- a/llhttp/Makefile
+++++++++++ b/llhttp/Makefile
++++++++@@ -81,7 +81,7 @@
++++++++ git checkout main
++++++++
++++++++ generate:
++++++++- npx ts-node bin/generate.ts
+++++++++ ts-node bin/generate.ts
++++++++
++++++++ install: build/libllhttp.a build/libllhttp.so
++++++++ $(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)
++++++++--- a/llhttp/bin/build_wasm.ts
+++++++++++ b/llhttp/bin/build_wasm.ts
++++++++@@ -25,6 +25,7 @@
++++++++ mkdirSync(join(WASM_SRC, 'build'));
++++++++ process.exit(0);
++++++++ } catch (error: unknown) {
+++++++++// @ts-ignore
++++++++ if (isErrorWithCode(error) && error.code !== 'EEXIST') {
++++++++ throw error;
++++++++ }
++++++++@@ -52,6 +53,7 @@
++++++++ try {
++++++++ mkdirSync(WASM_OUT);
++++++++ } catch (error: unknown) {
+++++++++// @ts-ignore
++++++++ if (isErrorWithCode(error) && error.code !== 'EEXIST') {
++++++++ throw error;
++++++++ }
++++++++@@ -63,12 +65,14 @@
++++++++ // Build wasm binary
++++++++ execSync(
++++++++ `clang \
++++++++- --sysroot=/usr/share/wasi-sysroot \
+++++++++ -nodefaultlibs \
+++++++++ --sysroot=/usr \
++++++++ -target wasm32-unknown-wasi \
++++++++ -Ofast \
++++++++ -fno-exceptions \
++++++++ -fvisibility=hidden \
++++++++ -mexec-model=reactor \
+++++++++ -Wl,-lc \
++++++++ -Wl,-error-limit=0 \
++++++++ -Wl,-O3 \
++++++++ -Wl,--lto-O3 \
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: replace "npm run" by pkgjs-run
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2023-02-17
++++++++
++++++++--- a/llhttp/bin/build_wasm.ts
+++++++++++ b/llhttp/bin/build_wasm.ts
++++++++@@ -43,7 +43,7 @@
++++++++ // @ts-ignore
++++++++ cmd += ` --user ${process.getuid!()}:${process.getegid!()}`;
++++++++ }
++++++++- cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`;
+++++++++ cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder pkgjs-run wasm`;
++++++++
++++++++ // eslint-disable-next-line no-console
++++++++ console.log(`> ${cmd}\n\n`);
++++++++@@ -61,7 +61,7 @@
++++++++ }
++++++++
++++++++ // Build ts
++++++++-execSync('npm run build', { cwd: WASM_SRC, stdio: 'inherit' });
+++++++++execSync('pkgjs-run build', { cwd: WASM_SRC, stdio: 'inherit' });
++++++++
++++++++ // Build wasm binary
++++++++ execSync(
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++fix-wasm-build.patch
++++++++# simdeverywhere.patch
++++++++disable-simd.patch
++++++++fix-typescript.patch
++++++++fix-for-test-tap.patch
++++++++replace-npm-run.patch
++++++++drop-ssl-tests.patch
++++++++drop-simd.patch
++++++++fix-for-proxy-2.patch
++++++++drop-one-error-test.patch
++++++++workaround-nodejs-bug.patch
++++++++dont-rebuild-on-install.patch
++++++++declare-soname.patch
++++++++fix-llhttp-version.patch
++++++++use-gcc-for-llhttp.patch
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: fix SIMD
++++++++Author: Jérémy Lal
++++++++Forwarded: no
++++++++Last-Update: 2022-05-16
++++++++
++++++++--- a/deps/llhttp/src/llhttp.c
+++++++++++ b/deps/llhttp/src/llhttp.c
++++++++@@ -4,13 +4,7 @@
++++++++ #include <stdint.h>
++++++++ #include <string.h>
++++++++
++++++++-#ifdef __SSE4_2__
++++++++- #ifdef _MSC_VER
++++++++- #include <nmmintrin.h>
++++++++- #else /* !_MSC_VER */
++++++++- #include <x86intrin.h>
++++++++- #endif /* _MSC_VER */
++++++++-#endif /* __SSE4_2__ */
+++++++++#include <simde/x86/sse4.2.h>
++++++++
++++++++ #ifdef _MSC_VER
++++++++ #define ALIGN(n) _declspec(align(n))
++++++++@@ -7678,13 +7672,7 @@
++++++++ #include <stdint.h>
++++++++ #include <string.h>
++++++++
++++++++-#ifdef __SSE4_2__
++++++++- #ifdef _MSC_VER
++++++++- #include <nmmintrin.h>
++++++++- #else /* !_MSC_VER */
++++++++- #include <x86intrin.h>
++++++++- #endif /* _MSC_VER */
++++++++-#endif /* __SSE4_2__ */
+++++++++#include <simde/x86/sse4.2.h>
++++++++
++++++++ #ifdef _MSC_VER
++++++++ #define ALIGN(n) _declspec(align(n))
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++--- node-undici-5.28.4+dfsg1+~cs23.12.11.orig/llhttp/Makefile
+++++++++++ node-undici-5.28.4+dfsg1+~cs23.12.11/llhttp/Makefile
++++++++@@ -24,18 +24,18 @@ clean:
++++++++
++++++++ build/libllhttp.so: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++- $(CLANG) -shared $^ -Wl,-soname,$(SONAME) -o $@
+++++++++ gcc -shared $^ -Wl,-soname,$(SONAME) -o $@
++++++++
++++++++ build/libllhttp.a: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++ $(AR) rcs $@ build/c/llhttp.o build/native/api.o build/native/http.o
++++++++
++++++++ build/c/llhttp.o: build/c/llhttp.c
++++++++- $(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@
+++++++++ gcc $(CFLAGS) $(INCLUDES) -c $< -o $@
++++++++
++++++++ build/native/%.o: src/native/%.c build/llhttp.h src/native/api.h \
++++++++ build/native
++++++++- $(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@
+++++++++ gcc $(CFLAGS) $(INCLUDES) -c $< -o $@
++++++++
++++++++ build/llhttp.h: generate
++++++++ build/c/llhttp.c: generate
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Description: workaround nodejs bug
++++++++Author: Yadd <yadd@debian.org>
++++++++Forwarded: not-needed
++++++++Last-Update: 2023-12-02
++++++++
++++++++--- a/fastify-busboy/deps/dicer/lib/Dicer.js
+++++++++++ b/fastify-busboy/deps/dicer/lib/Dicer.js
++++++++@@ -1,7 +1,7 @@
++++++++ 'use strict'
++++++++
++++++++-const WritableStream = require('node:stream').Writable
++++++++-const inherits = require('node:util').inherits
+++++++++const WritableStream = require('stream').Writable
+++++++++const inherits = require('util').inherits
++++++++
++++++++ const StreamSearch = require('../../streamsearch/sbmh')
++++++++
++++++++--- a/fastify-busboy/deps/dicer/lib/HeaderParser.js
+++++++++++ b/fastify-busboy/deps/dicer/lib/HeaderParser.js
++++++++@@ -1,7 +1,7 @@
++++++++ 'use strict'
++++++++
++++++++-const EventEmitter = require('node:events').EventEmitter
++++++++-const inherits = require('node:util').inherits
+++++++++const EventEmitter = require('events').EventEmitter
+++++++++const inherits = require('util').inherits
++++++++ const getLimit = require('../../../lib/utils/getLimit')
++++++++
++++++++ const StreamSearch = require('../../streamsearch/sbmh')
++++++++--- a/fastify-busboy/deps/dicer/lib/PartStream.js
+++++++++++ b/fastify-busboy/deps/dicer/lib/PartStream.js
++++++++@@ -1,7 +1,7 @@
++++++++ 'use strict'
++++++++
++++++++-const inherits = require('node:util').inherits
++++++++-const ReadableStream = require('node:stream').Readable
+++++++++const inherits = require('util').inherits
+++++++++const ReadableStream = require('stream').Readable
++++++++
++++++++ function PartStream (opts) {
++++++++ ReadableStream.call(this, opts)
++++++++--- a/fastify-busboy/deps/streamsearch/sbmh.js
+++++++++++ b/fastify-busboy/deps/streamsearch/sbmh.js
++++++++@@ -26,8 +26,8 @@
++++++++ * Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
++++++++ * by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
++++++++ */
++++++++-const EventEmitter = require('node:events').EventEmitter
++++++++-const inherits = require('node:util').inherits
+++++++++const EventEmitter = require('events').EventEmitter
+++++++++const inherits = require('util').inherits
++++++++
++++++++ function SBMH (needle) {
++++++++ if (typeof needle === 'string') {
++++++++--- a/fastify-busboy/lib/main.js
+++++++++++ b/fastify-busboy/lib/main.js
++++++++@@ -1,7 +1,7 @@
++++++++ 'use strict'
++++++++
++++++++-const WritableStream = require('node:stream').Writable
++++++++-const { inherits } = require('node:util')
+++++++++const WritableStream = require('stream').Writable
+++++++++const { inherits } = require('util')
++++++++ const Dicer = require('../deps/dicer/lib/Dicer')
++++++++
++++++++ const MultipartParser = require('./types/multipart')
++++++++--- a/fastify-busboy/lib/types/multipart.js
+++++++++++ b/fastify-busboy/lib/types/multipart.js
++++++++@@ -7,8 +7,8 @@
++++++++ // * support limits.fieldNameSize
++++++++ // -- this will require modifications to utils.parseParams
++++++++
++++++++-const { Readable } = require('node:stream')
++++++++-const { inherits } = require('node:util')
+++++++++const { Readable } = require('stream')
+++++++++const { inherits } = require('util')
++++++++
++++++++ const Dicer = require('../../deps/dicer/lib/Dicer')
++++++++
++++++++--- a/fastify-busboy/test/dicer-multipart.test.js
+++++++++++ b/fastify-busboy/test/dicer-multipart.test.js
++++++++@@ -1,10 +1,10 @@
++++++++ 'use strict'
++++++++
++++++++ const Dicer = require('../deps/dicer/lib/Dicer')
++++++++-const assert = require('node:assert')
++++++++-const fs = require('node:fs')
++++++++-const path = require('node:path')
++++++++-const inspect = require('node:util').inspect
+++++++++const assert = require('assert')
+++++++++const fs = require('fs')
+++++++++const path = require('path')
+++++++++const inspect = require('util').inspect
++++++++ const { test } = require('tap')
++++++++
++++++++ const FIXTURES_ROOT = path.join(__dirname, 'fixtures/')
++++++++--- a/fastify-busboy/test/parse-params.test.js
+++++++++++ b/fastify-busboy/test/parse-params.test.js
++++++++@@ -1,6 +1,6 @@
++++++++ 'use strict'
++++++++
++++++++-const { inspect } = require('node:util')
+++++++++const { inspect } = require('util')
++++++++ const { test } = require('tap')
++++++++ const parseParams = require('../lib/utils/parseParams')
++++++++
++++++++--- a/scripts/generate-undici-types-package-json.js
+++++++++++ b/scripts/generate-undici-types-package-json.js
++++++++@@ -1,5 +1,5 @@
++++++++-const fs = require('node:fs')
++++++++-const path = require('node:path')
+++++++++const fs = require('fs')
+++++++++const path = require('path')
++++++++
++++++++ const packageJSONPath = path.join(__dirname, '..', 'package.json')
++++++++ const packageJSONRaw = fs.readFileSync(packageJSONPath, 'utf-8')
++++++++--- a/test/client-node-max-header-size.js
+++++++++++ b/test/client-node-max-header-size.js
++++++++@@ -1,6 +1,6 @@
++++++++ 'use strict'
++++++++
++++++++-const { execSync } = require('node:child_process')
+++++++++const { execSync } = require('child_process')
++++++++ const { test } = require('tap')
++++++++
++++++++ const command = 'node -e "require(\'.\').request(\'https://httpbin.org/get\')"'
++++++++--- a/test/fetch/client-node-max-header-size.js
+++++++++++ b/test/fetch/client-node-max-header-size.js
++++++++@@ -1,6 +1,6 @@
++++++++ 'use strict'
++++++++
++++++++-const { execSync } = require('node:child_process')
+++++++++const { execSync } = require('child_process')
++++++++ const { test, skip } = require('tap')
++++++++ const { nodeMajor } = require('../../lib/core/util')
++++++++
++++++++--- a/test/fetch/http2.js
+++++++++++ b/test/fetch/http2.js
++++++++@@ -1,10 +1,10 @@
++++++++ 'use strict'
++++++++
++++++++-const { createSecureServer } = require('node:http2')
++++++++-const { createReadStream, readFileSync } = require('node:fs')
++++++++-const { once } = require('node:events')
++++++++-const { Blob } = require('node:buffer')
++++++++-const { Readable } = require('node:stream')
+++++++++const { createSecureServer } = require('http2')
+++++++++const { createReadStream, readFileSync } = require('fs')
+++++++++const { once } = require('events')
+++++++++const { Blob } = require('buffer')
+++++++++const { Readable } = require('stream')
++++++++
++++++++ const { test, plan } = require('tap')
++++++++ const pem = require('https-pem')
++++++++--- a/test/http2-alpn.js
+++++++++++ b/test/http2-alpn.js
++++++++@@ -1,10 +1,10 @@
++++++++ 'use strict'
++++++++
++++++++-const https = require('node:https')
++++++++-const { once } = require('node:events')
++++++++-const { createSecureServer } = require('node:http2')
++++++++-const { readFileSync } = require('node:fs')
++++++++-const { join } = require('node:path')
+++++++++const https = require('https')
+++++++++const { once } = require('events')
+++++++++const { createSecureServer } = require('http2')
+++++++++const { readFileSync } = require('fs')
+++++++++const { join } = require('path')
++++++++ const { test } = require('tap')
++++++++
++++++++ const { Client } = require('..')
++++++++--- a/test/http2.js
+++++++++++ b/test/http2.js
++++++++@@ -1,10 +1,10 @@
++++++++ 'use strict'
++++++++
++++++++-const { createSecureServer } = require('node:http2')
++++++++-const { createReadStream, readFileSync } = require('node:fs')
++++++++-const { once } = require('node:events')
++++++++-const { Blob } = require('node:buffer')
++++++++-const { Writable, pipeline, PassThrough, Readable } = require('node:stream')
+++++++++const { createSecureServer } = require('http2')
+++++++++const { createReadStream, readFileSync } = require('fs')
+++++++++const { once } = require('events')
+++++++++const { Blob } = require('buffer')
+++++++++const { Writable, pipeline, PassThrough, Readable } = require('stream')
++++++++
++++++++ const { test, plan } = require('tap')
++++++++ const { gte } = require('semver')
++++++++--- a/test/retry-handler.js
+++++++++++ b/test/retry-handler.js
++++++++@@ -1,6 +1,6 @@
++++++++ 'use strict'
++++++++-const { createServer } = require('node:http')
++++++++-const { once } = require('node:events')
+++++++++const { createServer } = require('http')
+++++++++const { once } = require('events')
++++++++
++++++++ const tap = require('tap')
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#!/usr/bin/make -f
++++++++# -*- makefile -*-
++++++++
++++++++# Uncomment this to turn on verbose mode.
++++++++#export DH_VERBOSE=1
++++++++include /usr/share/dpkg/pkg-info.mk
++++++++
++++++++export DEB_BUILD_MAINT_OPTIONS = hardening=+all
++++++++
++++++++LLHTTPVERSION = $(shell pkgjs-pjson llhttp version)
++++++++LLHTTPMAJORVERSION = $(shell pkgjs-pjson llhttp version|perl -pe 's/\..*//')
++++++++export SONAME = libllhttp.so.$(LLHTTPVERSION)
++++++++export SONAMEALIAS = libllhttp.so.$(LLHTTPMAJORVERSION)
++++++++LIBLLHTTPVERSION = $(LLHTTPVERSION)~$(DEB_VERSION)
++++++++
++++++++zz:
++++++++ echo $(LLHTTPMAJORVERSION)
++++++++
++++++++%:
++++++++ dh $@
++++++++
++++++++override_dh_auto_test:
++++++++ # autopkgtest only
++++++++
++++++++execute_after_dh_auto_install:
++++++++ cd llhttp && $(MAKE) install DESTDIR=../debian/tmp PREFIX=/usr LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)
++++++++ dh_auto_install --buildsystem=cmake -Dllhttp/release
++++++++
++++++++execute_after_dh_install:
++++++++ rm -f debian/node-undici/usr/share/nodejs/undici/lib/fetch/LICENSE
++++++++
++++++++execute_after_dh_clean:
++++++++ rm -rf obj-*
++++++++
++++++++override_dh_makeshlibs:
++++++++ dh_makeshlibs -a -- -plibllhttp9.1
++++++++
++++++++override_dh_gencontrol:
++++++++ dh_gencontrol -pnode-undici -- -v$(DEB_VERSION)
++++++++ dh_gencontrol -plibllhttp-dev -- -v$(LLHTTPVERSION)~$(DEB_VERSION) \
++++++++ -Vlibllhttp:Version=$(LIBLLHTTPVERSION) \
++++++++ -DHomepage=https://github.com/nodejs/llhttp
++++++++ dh_gencontrol -pnode-llhttp -- -v$(LLHTTPVERSION)~$(DEB_VERSION) \
++++++++ -DHomepage=https://github.com/nodejs/llhttp
++++++++ dh_gencontrol -plibllhttp9.1 -- -v$(LLHTTPVERSION)~$(DEB_VERSION) \
++++++++ -DHomepage=https://github.com/nodejs/llhttp
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++---
++++++++include:
++++++++ - https://salsa.debian.org/salsa-ci-team/pipeline/raw/master/salsa-ci.yml
++++++++ - https://salsa.debian.org/salsa-ci-team/pipeline/raw/master/pipeline-jobs.yml
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++3.0 (quilt)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# Fixed
++++++++weak-library-dev-dependency * Depends libllhttp9*
++++++++
++++++++# False positive: data
++++++++source-is-missing [*lib/llhttp/constants.js*]
++++++++source-is-missing [test/wpt/tests/common/third_party/reftest-analyzer.xhtml]
++++++++source-is-missing [test/wpt/tests/fetch/cross-origin-resource-policy/fetch-in-iframe.html]
++++++++source-is-missing [test/wpt/tests/fetch/redirects/data.window.js]
++++++++source-contains-prebuilt-javascript-object [*lib/llhttp/constants.js*]
++++++++very-long-line-length-in-source-file *lib/llhttp/constants.js*
++++++++very-long-line-length-in-source-file * [test/wpt/tests/xhr/resources/over-1-meg.txt:*]
++++++++very-long-line-length-in-source-file * [test/wpt/tests/common/third_party/reftest-analyzer.xhtml:*]
++++++++very-long-line-length-in-source-file * [test/wpt/tests/.taskcluster.yml:*]
++++++++very-long-line-length-in-source-file * [test/wpt/tests/fetch/cross-origin-resource-policy/fetch-in-iframe.html:*]
++++++++very-long-line-length-in-source-file * [test/wpt/tests/fetch/redirects/data.window.js:*]
++++++++
++++++++# Test files
++++++++source-is-missing [debian/tests/test_modules/node-forge/lib/des.js]
++++++++source-is-missing [debian/tests/test_modules/node-forge/lib/prime.worker.js]
++++++++source-is-missing [fastify-busboy/bench/createMultipartBufferForEncodingBench.js]
++++++++source-is-missing [fastify-busboy/test/types-multipart.test.js]
++++++++source-contains-browserified-javascript code fragment:* [test/wpt/tests/resources/webidl2/lib/webidl2.js]
++++++++source-contains-prebuilt-javascript-object [debian/tests/test_modules/node-forge/lib/des.js]
++++++++source-contains-prebuilt-javascript-object [debian/tests/test_modules/node-forge/lib/prime.worker.js]
++++++++source-contains-prebuilt-javascript-object [fastify-busboy/bench/createMultipartBufferForEncodingBench.js]
++++++++source-contains-prebuilt-javascript-object [fastify-busboy/test/types-multipart.test.js]
++++++++source-contains-prebuilt-javascript-object [test/wpt/tests/fetch/redirects/data.window.js]
++++++++very-long-line-length-in-source-file * [fastify-busboy/test/types-multipart.test.js:*]
++++++++very-long-line-length-in-source-file * [fastify-busboy/bench/createMultipartBufferForEncodingBench.js:*]
++++++++
++++++++# Doc
++++++++very-long-line-length-in-source-file *.md*
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Test-Command: /usr/share/pkg-js-autopkgtest/runner require
++++++++Depends: @
++++++++ , pkg-js-autopkgtest
++++++++ , node-debug
++++++++Restrictions: superficial, skippable,
++++++++Features: test-name=pkg-js-autopkgtest-require
++++++++
++++++++Test-Command: /usr/share/pkg-js-autopkgtest/runner
++++++++Depends: @
++++++++ , pkg-js-autopkgtest
++++++++ , jest
++++++++Restrictions: allow-stderr, skippable, needs-internet
++++++++Features: test-name=pkg-js-autopkgtest
++++++++
++++++++Test-Command: NETWORKTEST=1 /usr/share/pkg-js-autopkgtest/runner
++++++++Depends: @
++++++++ , pkg-js-autopkgtest
++++++++ , chai
++++++++ , mocha
++++++++ , node-busboy
++++++++ , node-chai-as-promised
++++++++ , node-p-timeout
++++++++ , node-proxy (>= 2)
++++++++ , node-proxyquire
++++++++ , node-sinon
++++++++ , node-tap
++++++++Restrictions: allow-stderr, skippable, needs-internet
++++++++Features: test-name=pkg-js-autopkgtest-with-network
++++++++Architecture: amd64, arm64, i386, ppc64el, s390x
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++if test "$NETWORKTEST" != ""; then
++++++++ node -e "require('https-pem/install')"
++++++++ mocha test/node-fetch
++++++++ rm -f test/ca-fingerprint.js test/client-errors.js test/esm-wrapper.js test/https.js test/tls-client-cert.js test/tls-session-reuse.js test/gc.js test/connect-timeout.js
++++++++ rm -f test/http2-alpn.js test/proxy-agent.js test/balanced-pool.js test/tls-session-reuse.js test/autoselectfamily.js test/http2.js test/no-strict-content-length.js test/proxy.js
++++++++ tap --no-timeout -R tap --no-cov `ls test/*.js test/diagnostics-channel/*.js`
++++++++else
++++++++ jest --ci test/jest/mock-scope.test.js test/jest/test.js
++++++++fi
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++MIT License
++++++++
++++++++Copyright (c) 2017 Toru Nagashima
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to deal
++++++++in the Software without restriction, including without limitation the rights
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/*globals self, window */
++++++++"use strict"
++++++++
++++++++/*eslint-disable @mysticatea/prettier */
++++++++const { AbortController, AbortSignal } =
++++++++ typeof self !== "undefined" ? self :
++++++++ typeof window !== "undefined" ? window :
++++++++ /* otherwise */ undefined
++++++++/*eslint-enable @mysticatea/prettier */
++++++++
++++++++module.exports = AbortController
++++++++module.exports.AbortSignal = AbortSignal
++++++++module.exports.default = AbortController
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/*globals self, window */
++++++++
++++++++/*eslint-disable @mysticatea/prettier */
++++++++const { AbortController, AbortSignal } =
++++++++ typeof self !== "undefined" ? self :
++++++++ typeof window !== "undefined" ? window :
++++++++ /* otherwise */ undefined
++++++++/*eslint-enable @mysticatea/prettier */
++++++++
++++++++export default AbortController
++++++++export { AbortController, AbortSignal }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { EventTarget } from "event-target-shim"
++++++++
++++++++type Events = {
++++++++ abort: any
++++++++}
++++++++type EventAttributes = {
++++++++ onabort: any
++++++++}
++++++++/**
++++++++ * The signal class.
++++++++ * @see https://dom.spec.whatwg.org/#abortsignal
++++++++ */
++++++++declare class AbortSignal extends EventTarget<Events, EventAttributes> {
++++++++ /**
++++++++ * AbortSignal cannot be constructed directly.
++++++++ */
++++++++ constructor()
++++++++ /**
++++++++ * Returns `true` if this `AbortSignal`"s `AbortController` has signaled to abort, and `false` otherwise.
++++++++ */
++++++++ readonly aborted: boolean
++++++++}
++++++++/**
++++++++ * The AbortController.
++++++++ * @see https://dom.spec.whatwg.org/#abortcontroller
++++++++ */
++++++++declare class AbortController {
++++++++ /**
++++++++ * Initialize this controller.
++++++++ */
++++++++ constructor()
++++++++ /**
++++++++ * Returns the `AbortSignal` object associated with this object.
++++++++ */
++++++++ readonly signal: AbortSignal
++++++++ /**
++++++++ * Abort and signal to any observers that the associated activity is to be aborted.
++++++++ */
++++++++ abort(): void
++++++++}
++++++++
++++++++export default AbortController
++++++++export { AbortController, AbortSignal }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * @author Toru Nagashima <https://github.com/mysticatea>
++++++++ * See LICENSE file in root directory for full license.
++++++++ */
++++++++'use strict';
++++++++
++++++++Object.defineProperty(exports, '__esModule', { value: true });
++++++++
++++++++var eventTargetShim = require('event-target-shim');
++++++++
++++++++/**
++++++++ * The signal class.
++++++++ * @see https://dom.spec.whatwg.org/#abortsignal
++++++++ */
++++++++class AbortSignal extends eventTargetShim.EventTarget {
++++++++ /**
++++++++ * AbortSignal cannot be constructed directly.
++++++++ */
++++++++ constructor() {
++++++++ super();
++++++++ throw new TypeError("AbortSignal cannot be constructed directly");
++++++++ }
++++++++ /**
++++++++ * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
++++++++ */
++++++++ get aborted() {
++++++++ const aborted = abortedFlags.get(this);
++++++++ if (typeof aborted !== "boolean") {
++++++++ throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? "null" : typeof this}`);
++++++++ }
++++++++ return aborted;
++++++++ }
++++++++}
++++++++eventTargetShim.defineEventAttribute(AbortSignal.prototype, "abort");
++++++++/**
++++++++ * Create an AbortSignal object.
++++++++ */
++++++++function createAbortSignal() {
++++++++ const signal = Object.create(AbortSignal.prototype);
++++++++ eventTargetShim.EventTarget.call(signal);
++++++++ abortedFlags.set(signal, false);
++++++++ return signal;
++++++++}
++++++++/**
++++++++ * Abort a given signal.
++++++++ */
++++++++function abortSignal(signal) {
++++++++ if (abortedFlags.get(signal) !== false) {
++++++++ return;
++++++++ }
++++++++ abortedFlags.set(signal, true);
++++++++ signal.dispatchEvent({ type: "abort" });
++++++++}
++++++++/**
++++++++ * Aborted flag for each instances.
++++++++ */
++++++++const abortedFlags = new WeakMap();
++++++++// Properties should be enumerable.
++++++++Object.defineProperties(AbortSignal.prototype, {
++++++++ aborted: { enumerable: true },
++++++++});
++++++++// `toString()` should return `"[object AbortSignal]"`
++++++++if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
++++++++ Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
++++++++ configurable: true,
++++++++ value: "AbortSignal",
++++++++ });
++++++++}
++++++++
++++++++/**
++++++++ * The AbortController.
++++++++ * @see https://dom.spec.whatwg.org/#abortcontroller
++++++++ */
++++++++class AbortController {
++++++++ /**
++++++++ * Initialize this controller.
++++++++ */
++++++++ constructor() {
++++++++ signals.set(this, createAbortSignal());
++++++++ }
++++++++ /**
++++++++ * Returns the `AbortSignal` object associated with this object.
++++++++ */
++++++++ get signal() {
++++++++ return getSignal(this);
++++++++ }
++++++++ /**
++++++++ * Abort and signal to any observers that the associated activity is to be aborted.
++++++++ */
++++++++ abort() {
++++++++ abortSignal(getSignal(this));
++++++++ }
++++++++}
++++++++/**
++++++++ * Associated signals.
++++++++ */
++++++++const signals = new WeakMap();
++++++++/**
++++++++ * Get the associated signal of a given controller.
++++++++ */
++++++++function getSignal(controller) {
++++++++ const signal = signals.get(controller);
++++++++ if (signal == null) {
++++++++ throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? "null" : typeof controller}`);
++++++++ }
++++++++ return signal;
++++++++}
++++++++// Properties should be enumerable.
++++++++Object.defineProperties(AbortController.prototype, {
++++++++ signal: { enumerable: true },
++++++++ abort: { enumerable: true },
++++++++});
++++++++if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
++++++++ Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
++++++++ configurable: true,
++++++++ value: "AbortController",
++++++++ });
++++++++}
++++++++
++++++++exports.AbortController = AbortController;
++++++++exports.AbortSignal = AbortSignal;
++++++++exports.default = AbortController;
++++++++
++++++++module.exports = AbortController
++++++++module.exports.AbortController = module.exports["default"] = AbortController
++++++++module.exports.AbortSignal = AbortSignal
++++++++//# sourceMappingURL=abort-controller.js.map
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * @author Toru Nagashima <https://github.com/mysticatea>
++++++++ * See LICENSE file in root directory for full license.
++++++++ */
++++++++import { EventTarget, defineEventAttribute } from 'event-target-shim';
++++++++
++++++++/**
++++++++ * The signal class.
++++++++ * @see https://dom.spec.whatwg.org/#abortsignal
++++++++ */
++++++++class AbortSignal extends EventTarget {
++++++++ /**
++++++++ * AbortSignal cannot be constructed directly.
++++++++ */
++++++++ constructor() {
++++++++ super();
++++++++ throw new TypeError("AbortSignal cannot be constructed directly");
++++++++ }
++++++++ /**
++++++++ * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
++++++++ */
++++++++ get aborted() {
++++++++ const aborted = abortedFlags.get(this);
++++++++ if (typeof aborted !== "boolean") {
++++++++ throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? "null" : typeof this}`);
++++++++ }
++++++++ return aborted;
++++++++ }
++++++++}
++++++++defineEventAttribute(AbortSignal.prototype, "abort");
++++++++/**
++++++++ * Create an AbortSignal object.
++++++++ */
++++++++function createAbortSignal() {
++++++++ const signal = Object.create(AbortSignal.prototype);
++++++++ EventTarget.call(signal);
++++++++ abortedFlags.set(signal, false);
++++++++ return signal;
++++++++}
++++++++/**
++++++++ * Abort a given signal.
++++++++ */
++++++++function abortSignal(signal) {
++++++++ if (abortedFlags.get(signal) !== false) {
++++++++ return;
++++++++ }
++++++++ abortedFlags.set(signal, true);
++++++++ signal.dispatchEvent({ type: "abort" });
++++++++}
++++++++/**
++++++++ * Aborted flag for each instances.
++++++++ */
++++++++const abortedFlags = new WeakMap();
++++++++// Properties should be enumerable.
++++++++Object.defineProperties(AbortSignal.prototype, {
++++++++ aborted: { enumerable: true },
++++++++});
++++++++// `toString()` should return `"[object AbortSignal]"`
++++++++if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
++++++++ Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
++++++++ configurable: true,
++++++++ value: "AbortSignal",
++++++++ });
++++++++}
++++++++
++++++++/**
++++++++ * The AbortController.
++++++++ * @see https://dom.spec.whatwg.org/#abortcontroller
++++++++ */
++++++++class AbortController {
++++++++ /**
++++++++ * Initialize this controller.
++++++++ */
++++++++ constructor() {
++++++++ signals.set(this, createAbortSignal());
++++++++ }
++++++++ /**
++++++++ * Returns the `AbortSignal` object associated with this object.
++++++++ */
++++++++ get signal() {
++++++++ return getSignal(this);
++++++++ }
++++++++ /**
++++++++ * Abort and signal to any observers that the associated activity is to be aborted.
++++++++ */
++++++++ abort() {
++++++++ abortSignal(getSignal(this));
++++++++ }
++++++++}
++++++++/**
++++++++ * Associated signals.
++++++++ */
++++++++const signals = new WeakMap();
++++++++/**
++++++++ * Get the associated signal of a given controller.
++++++++ */
++++++++function getSignal(controller) {
++++++++ const signal = signals.get(controller);
++++++++ if (signal == null) {
++++++++ throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? "null" : typeof controller}`);
++++++++ }
++++++++ return signal;
++++++++}
++++++++// Properties should be enumerable.
++++++++Object.defineProperties(AbortController.prototype, {
++++++++ signal: { enumerable: true },
++++++++ abort: { enumerable: true },
++++++++});
++++++++if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
++++++++ Object.defineProperty(AbortController.prototype, Symbol.toStringTag, {
++++++++ configurable: true,
++++++++ value: "AbortController",
++++++++ });
++++++++}
++++++++
++++++++export default AbortController;
++++++++export { AbortController, AbortSignal };
++++++++//# sourceMappingURL=abort-controller.mjs.map
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "abort-controller",
++++++++ "version": "3.0.0",
++++++++ "description": "An implementation of WHATWG AbortController interface.",
++++++++ "main": "dist/abort-controller",
++++++++ "files": [
++++++++ "dist",
++++++++ "polyfill.*",
++++++++ "browser.*"
++++++++ ],
++++++++ "engines": {
++++++++ "node": ">=6.5"
++++++++ },
++++++++ "dependencies": {
++++++++ "event-target-shim": "^5.0.0"
++++++++ },
++++++++ "browser": "./browser.js",
++++++++ "devDependencies": {
++++++++ "@babel/core": "^7.2.2",
++++++++ "@babel/plugin-transform-modules-commonjs": "^7.2.0",
++++++++ "@babel/preset-env": "^7.3.0",
++++++++ "@babel/register": "^7.0.0",
++++++++ "@mysticatea/eslint-plugin": "^8.0.1",
++++++++ "@mysticatea/spy": "^0.1.2",
++++++++ "@types/mocha": "^5.2.5",
++++++++ "@types/node": "^10.12.18",
++++++++ "assert": "^1.4.1",
++++++++ "codecov": "^3.1.0",
++++++++ "dts-bundle-generator": "^2.0.0",
++++++++ "eslint": "^5.12.1",
++++++++ "karma": "^3.1.4",
++++++++ "karma-chrome-launcher": "^2.2.0",
++++++++ "karma-coverage": "^1.1.2",
++++++++ "karma-firefox-launcher": "^1.1.0",
++++++++ "karma-growl-reporter": "^1.0.0",
++++++++ "karma-ie-launcher": "^1.0.0",
++++++++ "karma-mocha": "^1.3.0",
++++++++ "karma-rollup-preprocessor": "^7.0.0-rc.2",
++++++++ "mocha": "^5.2.0",
++++++++ "npm-run-all": "^4.1.5",
++++++++ "nyc": "^13.1.0",
++++++++ "opener": "^1.5.1",
++++++++ "rimraf": "^2.6.3",
++++++++ "rollup": "^1.1.2",
++++++++ "rollup-plugin-babel": "^4.3.2",
++++++++ "rollup-plugin-babel-minify": "^7.0.0",
++++++++ "rollup-plugin-commonjs": "^9.2.0",
++++++++ "rollup-plugin-node-resolve": "^4.0.0",
++++++++ "rollup-plugin-sourcemaps": "^0.4.2",
++++++++ "rollup-plugin-typescript": "^1.0.0",
++++++++ "rollup-watch": "^4.3.1",
++++++++ "ts-node": "^8.0.1",
++++++++ "type-tester": "^1.0.0",
++++++++ "typescript": "^3.2.4"
++++++++ },
++++++++ "scripts": {
++++++++ "preversion": "npm test",
++++++++ "version": "npm run -s build && git add dist/*",
++++++++ "postversion": "git push && git push --tags",
++++++++ "clean": "rimraf .nyc_output coverage",
++++++++ "coverage": "opener coverage/lcov-report/index.html",
++++++++ "lint": "eslint . --ext .ts",
++++++++ "build": "run-s -s build:*",
++++++++ "build:rollup": "rollup -c",
++++++++ "build:dts": "dts-bundle-generator -o dist/abort-controller.d.ts src/abort-controller.ts && ts-node scripts/fix-dts",
++++++++ "test": "run-s -s lint test:*",
++++++++ "test:mocha": "nyc mocha test/*.ts",
++++++++ "test:karma": "karma start --single-run",
++++++++ "watch": "run-p -s watch:*",
++++++++ "watch:mocha": "mocha test/*.ts --require ts-node/register --watch-extensions ts --watch --growl",
++++++++ "watch:karma": "karma start --watch",
++++++++ "codecov": "codecov"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+https://github.com/mysticatea/abort-controller.git"
++++++++ },
++++++++ "keywords": [
++++++++ "w3c",
++++++++ "whatwg",
++++++++ "event",
++++++++ "events",
++++++++ "abort",
++++++++ "cancel",
++++++++ "abortcontroller",
++++++++ "abortsignal",
++++++++ "controller",
++++++++ "signal",
++++++++ "shim"
++++++++ ],
++++++++ "author": "Toru Nagashima (https://github.com/mysticatea)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/mysticatea/abort-controller/issues"
++++++++ },
++++++++ "homepage": "https://github.com/mysticatea/abort-controller#readme"
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/*globals require, self, window */
++++++++"use strict"
++++++++
++++++++const ac = require("./dist/abort-controller")
++++++++
++++++++/*eslint-disable @mysticatea/prettier */
++++++++const g =
++++++++ typeof self !== "undefined" ? self :
++++++++ typeof window !== "undefined" ? window :
++++++++ typeof global !== "undefined" ? global :
++++++++ /* otherwise */ undefined
++++++++/*eslint-enable @mysticatea/prettier */
++++++++
++++++++if (g) {
++++++++ if (typeof g.AbortController === "undefined") {
++++++++ g.AbortController = ac.AbortController
++++++++ }
++++++++ if (typeof g.AbortSignal === "undefined") {
++++++++ g.AbortSignal = ac.AbortSignal
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/*globals self, window */
++++++++import * as ac from "./dist/abort-controller"
++++++++
++++++++/*eslint-disable @mysticatea/prettier */
++++++++const g =
++++++++ typeof self !== "undefined" ? self :
++++++++ typeof window !== "undefined" ? window :
++++++++ typeof global !== "undefined" ? global :
++++++++ /* otherwise */ undefined
++++++++/*eslint-enable @mysticatea/prettier */
++++++++
++++++++if (g) {
++++++++ if (typeof g.AbortController === "undefined") {
++++++++ g.AbortController = ac.AbortController
++++++++ }
++++++++ if (typeof g.AbortSignal === "undefined") {
++++++++ g.AbortSignal = ac.AbortSignal
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++The MIT License (MIT)
++++++++Copyright (c) 2020 David Mark Clements
++++++++
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to deal
++++++++in the Software without restriction, including without limitation the rights
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
++++++++EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
++++++++IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
++++++++OR OTHER DEALINGS IN THE SOFTWARE.
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++/* global SharedArrayBuffer, Atomics */
++++++++
++++++++if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
++++++++ const nil = new Int32Array(new SharedArrayBuffer(4))
++++++++
++++++++ function sleep (ms) {
++++++++ // also filters out NaN, non-number types, including empty strings, but allows bigints
++++++++ const valid = ms > 0 && ms < Infinity
++++++++ if (valid === false) {
++++++++ if (typeof ms !== 'number' && typeof ms !== 'bigint') {
++++++++ throw TypeError('sleep: ms must be a number')
++++++++ }
++++++++ throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
++++++++ }
++++++++
++++++++ Atomics.wait(nil, 0, 0, Number(ms))
++++++++ }
++++++++ module.exports = sleep
++++++++} else {
++++++++
++++++++ function sleep (ms) {
++++++++ // also filters out NaN, non-number types, including empty strings, but allows bigints
++++++++ const valid = ms > 0 && ms < Infinity
++++++++ if (valid === false) {
++++++++ if (typeof ms !== 'number' && typeof ms !== 'bigint') {
++++++++ throw TypeError('sleep: ms must be a number')
++++++++ }
++++++++ throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
++++++++ }
++++++++ const target = Date.now() + Number(ms)
++++++++ while (target > Date.now()){}
++++++++ }
++++++++
++++++++ module.exports = sleep
++++++++
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "atomic-sleep",
++++++++ "version": "1.0.0",
++++++++ "description": "Zero CPU overhead, zero dependency, true event-loop blocking sleep",
++++++++ "main": "index.js",
++++++++ "scripts": {
++++++++ "test": "tap -R classic- -j1 test",
++++++++ "lint": "standard",
++++++++ "ci": "npm run lint && npm test"
++++++++ },
++++++++ "keywords": [
++++++++ "sleep",
++++++++ "pause",
++++++++ "wait",
++++++++ "performance",
++++++++ "atomics"
++++++++ ],
++++++++ "engines": {
++++++++ "node": ">=8.0.0"
++++++++ },
++++++++ "author": "David Mark Clements (@davidmarkclem)",
++++++++ "license": "MIT",
++++++++ "devDependencies": {
++++++++ "standard": "^14.3.1",
++++++++ "tap": "^14.10.6",
++++++++ "tape": "^4.13.2"
++++++++ },
++++++++ "dependencies": {},
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+https://github.com/davidmarkclements/atomic-sleep.git"
++++++++ },
++++++++ "bugs": {
++++++++ "url": "https://github.com/davidmarkclements/atomic-sleep/issues"
++++++++ },
++++++++ "homepage": "https://github.com/davidmarkclements/atomic-sleep#readme"
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Copyright (c) 2016-2017 Akim McMath
++++++++Copyright (c) 2018 Harry Sarson
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy of
++++++++this software and associated documentation files (the "Software"), to deal in
++++++++the Software without restriction, including without limitation the rights to
++++++++use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
++++++++of the Software, and to permit persons to whom the Software is furnished to do
++++++++so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++(function (chaiIterator) {
++++++++ 'use strict';
++++++++
++++++++ /* istanbul ignore else */
++++++++ if (typeof module === 'object' && typeof exports === 'object') {
++++++++ module.exports = chaiIterator;
++++++++ } else if (typeof define === 'function' && define.amd) {
++++++++ define(function () {
++++++++ return chaiIterator;
++++++++ });
++++++++ } else {
++++++++ chai.use(chaiIterator);
++++++++ }
++++++++})(function (chai, utils) {
++++++++ 'use strict';
++++++++
++++++++ var Assertion = chai.Assertion;
++++++++ var assert = chai.assert;
++++++++
++++++++ var noop = function () {};
++++++++
++++++++ var ELLIPSIS = unquote('...');
++++++++
++++++++ // Expect/should assertions =============================
++++++++
++++++++ Assertion.addProperty('iterable', function () {
++++++++ this.assert(isIterable(this._obj),
++++++++ 'expected #{this} to be iterable',
++++++++ 'expected #{this} not to be iterable'
++++++++ );
++++++++ });
++++++++
++++++++ Assertion.addProperty('iterate', function () {
++++++++ new Assertion(this._obj).iterable;
++++++++
++++++++ utils.flag(this, 'iterate', true);
++++++++ });
++++++++
++++++++ Assertion.addMethod('over', iterateMethod('to iterate over', function (exp) {
++++++++ var it = this._obj[Symbol.iterator]();
++++++++ var actual = slice(it, exp.length + 2);
++++++++ var suffix = it.next().done ? [] : [ELLIPSIS];
++++++++
++++++++ return actual.concat(suffix);
++++++++ }));
++++++++
++++++++ Assertion.addMethod('from', iterateMethod('to begin iteration with', function (exp) {
++++++++ return slice(this._obj[Symbol.iterator](), exp.length);
++++++++ }));
++++++++
++++++++ Assertion.addMethod('until', iterateMethod('to end iteration with', function (exp) {
++++++++ var it = this._obj[Symbol.iterator]();
++++++++ var actual = slice(it, exp.length);
++++++++ var step = it.next();
++++++++
++++++++ while (!step.done) {
++++++++ actual.push(step.value);
++++++++ actual.shift();
++++++++ step = it.next();
++++++++ }
++++++++
++++++++ return actual;
++++++++ }));
++++++++
++++++++ Assertion.addProperty('for', noop);
++++++++
++++++++ Assertion.overwriteChainableMethod('lengthOf', function (_super) {
++++++++ return function (exp) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var len = iterableLength(this._obj, exp);
++++++++
++++++++ this.assert(
++++++++ len === exp,
++++++++ 'expected #{this} to iterate for length of #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length of #{exp}',
++++++++ exp,
++++++++ len
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ }, function (_super) {
++++++++ return function () {
++++++++ _super.apply(this);
++++++++ };
++++++++ });
++++++++
++++++++ Assertion.overwriteMethod('above', function (_super) {
++++++++ return function (exp) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var len = iterableLength(this._obj, exp);
++++++++
++++++++ this.assert(
++++++++ len > exp,
++++++++ 'expected #{this} to iterate for length above #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length above #{exp}',
++++++++ exp,
++++++++ len
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ });
++++++++
++++++++ Assertion.overwriteMethod('below', function (_super) {
++++++++ return function (exp) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var max = exp + 100;
++++++++ var len = iterableLength(this._obj, max);
++++++++ var act = len === Infinity ? unquote('more than ' + max) : len;
++++++++
++++++++ this.assert(
++++++++ len < exp,
++++++++ 'expected #{this} to iterate for length below #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length below #{exp}',
++++++++ exp,
++++++++ act
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ });
++++++++
++++++++ Assertion.overwriteMethod('least', function (_super) {
++++++++ return function (exp) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var len = iterableLength(this._obj, exp);
++++++++
++++++++ this.assert(
++++++++ len >= exp,
++++++++ 'expected #{this} to iterate for length of at least #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length of at least #{exp}',
++++++++ exp,
++++++++ len
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ });
++++++++
++++++++ Assertion.overwriteMethod('most', function (_super) {
++++++++ return function (exp) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var max = exp + 100;
++++++++ var len = iterableLength(this._obj, max);
++++++++ var act = len === Infinity ? unquote('more than ' + max) : len;
++++++++
++++++++ this.assert(
++++++++ len <= exp,
++++++++ 'expected #{this} to iterate for length of at most #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length of at most #{exp}',
++++++++ exp,
++++++++ act
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ });
++++++++
++++++++ Assertion.overwriteMethod('within', function (_super) {
++++++++ return function (min, max) {
++++++++ if (utils.flag(this, 'iterate')) {
++++++++ var cutoff = max + 100;
++++++++ var len = iterableLength(this._obj, cutoff);
++++++++ var exp = unquote(min + 'and' + max);
++++++++ var act = len === Infinity ? unquote('more than ' + cutoff) : len;
++++++++
++++++++ this.assert(
++++++++ min <= len && len <= max,
++++++++ 'expected #{this} to iterate for length within #{exp}, but got #{act}',
++++++++ 'expected #{this} not to iterate for length within #{exp}',
++++++++ exp,
++++++++ act
++++++++ );
++++++++ } else {
++++++++ _super.apply(this, arguments);
++++++++ }
++++++++ };
++++++++ });
++++++++
++++++++ // Assert methods =======================================
++++++++
++++++++ assert.isIterable = function (value, msg) {
++++++++ new Assertion(value, msg).iterable;
++++++++ };
++++++++
++++++++ assert.isNotIterable = function (value, msg) {
++++++++ new Assertion(value, msg).not.iterable;
++++++++ };
++++++++
++++++++ assert.iteratesFrom = function (value, exp, msg) {
++++++++ new Assertion(value, msg).iterate.from(exp);
++++++++ };
++++++++
++++++++ assert.doesNotIterateFrom = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.iterate.from(exp);
++++++++ };
++++++++
++++++++ assert.deepIteratesFrom = function (value, exp, msg) {
++++++++ new Assertion(value, msg).deep.iterate.from(exp);
++++++++ };
++++++++
++++++++ assert.doesNotDeepIterateFrom = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.deep.iterate.from(exp);
++++++++ };
++++++++
++++++++ assert.iteratesOver = function (value, exp, msg) {
++++++++ new Assertion(value, msg).iterate.over(exp);
++++++++ };
++++++++
++++++++ assert.doesNotIterateOver = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.iterate.over(exp);
++++++++ };
++++++++
++++++++ assert.deepIteratesOver = function (value, exp, msg) {
++++++++ new Assertion(value, msg).deep.iterate.over(exp);
++++++++ };
++++++++
++++++++ assert.doesNotDeepIterateOver = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.deep.iterate.over(exp);
++++++++ };
++++++++
++++++++ assert.iteratesUntil = function (value, exp, msg) {
++++++++ new Assertion(value, msg).iterate.until(exp);
++++++++ };
++++++++
++++++++ assert.doesNotIterateUntil = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.iterate.until(exp);
++++++++ };
++++++++
++++++++ assert.deepIteratesUntil = function (value, exp, msg) {
++++++++ new Assertion(value, msg).deep.iterate.until(exp);
++++++++ };
++++++++
++++++++ assert.doesNotDeepIterateUntil = function (value, exp, msg) {
++++++++ new Assertion(value, msg).not.deep.iterate.until(exp);
++++++++ };
++++++++
++++++++ var _lengthOf = assert.lengthOf;
++++++++
++++++++ assert.lengthOf = function (value, exp, msg) {
++++++++ if (isIterable(value) && typeof value.length !== 'number') {
++++++++ new Assertion(value, msg).iterate.for.lengthOf(exp);
++++++++ } else {
++++++++ _lengthOf.apply(assert, arguments);
++++++++ }
++++++++ };
++++++++
++++++++ // Helpers ==============================================
++++++++
++++++++ function iterateMethod(predicate, getActual) {
++++++++ return function (iterable) {
++++++++ assert(utils.flag(this, 'iterate'), 'the iterate flag must be set');
++++++++ new Assertion(iterable).iterable;
++++++++
++++++++ var exp = slice(iterable[Symbol.iterator]());
++++++++ var act = getActual.call(this, exp);
++++++++
++++++++ var deep = utils.flag(this, 'deep') ? ' deep' : '';
++++++++
++++++++ this.assert(compareArrays(exp, act, deep),
++++++++ 'expected #{this} ' + predicate + deep + ' values #{exp}, but got #{act}',
++++++++ 'expected #{this} not ' + predicate + deep + ' values #{exp}',
++++++++ exp,
++++++++ act
++++++++ );
++++++++ };
++++++++ }
++++++++
++++++++ // Utilities ============================================
++++++++
++++++++ function isIterable(value) {
++++++++ return value !== undefined && value !== null && typeof value[Symbol.iterator] === 'function';
++++++++ }
++++++++
++++++++ function iterableLength(iterable, max) {
++++++++ max = max === undefined ? Infinity : max;
++++++++
++++++++ var it = iterable[Symbol.iterator]();
++++++++
++++++++ for (var i = 0; i <= max; i++) {
++++++++ if (it.next().done) {
++++++++ return i;
++++++++ }
++++++++ }
++++++++
++++++++ return Infinity;
++++++++ }
++++++++
++++++++ function strictEqual(a, b) {
++++++++ return a === b;
++++++++ }
++++++++
++++++++ function compareArrays(exp, act, deep) {
++++++++ var equalFn = deep ? utils.eql : strictEqual;
++++++++
++++++++ return exp.length === act.length && exp.every(function (value, i) {
++++++++ return equalFn(value, act[i]);
++++++++ });
++++++++ }
++++++++
++++++++ function slice(it, stop) {
++++++++ stop = stop === undefined ? Infinity : stop;
++++++++
++++++++ var result = [];
++++++++ var max = stop - 1;
++++++++ var step = it.next();
++++++++
++++++++ for (var i = 0; i <= max && !step.done; i++) {
++++++++ result.push(step.value);
++++++++ if (i < max) {
++++++++ step = it.next();
++++++++ }
++++++++ }
++++++++
++++++++ return result;
++++++++ }
++++++++
++++++++ function unquote(str) {
++++++++ return {
++++++++ inspect: function () {
++++++++ return this.toString();
++++++++ },
++++++++ toString: function () {
++++++++ return str;
++++++++ }
++++++++ };
++++++++ }
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "chai-iterator",
++++++++ "version": "3.0.2",
++++++++ "description": "Chai assertions for iterable objects",
++++++++ "main": "chai-iterator.js",
++++++++ "scripts": {
++++++++ "test:lint": "xo",
++++++++ "test:mocha": "mocha test/**/*.js",
++++++++ "test:cover": "nyc npm run test:mocha",
++++++++ "test": "npm run test:lint && npm run test:cover",
++++++++ "coveralls": "nyc report --reporter=text-lcov | coveralls"
++++++++ },
++++++++ "files": [
++++++++ "chai-iterator.js",
++++++++ "LICENSE",
++++++++ "CHANGELOG.md",
++++++++ "README.md"
++++++++ ],
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "https://github.com/harrysarson/chai-iterator.git"
++++++++ },
++++++++ "keywords": [
++++++++ "chai",
++++++++ "chai-plugin",
++++++++ "browser",
++++++++ "iterator",
++++++++ "iterable",
++++++++ "iteration",
++++++++ "generator",
++++++++ "yield",
++++++++ "es6",
++++++++ "es2015",
++++++++ "typescript"
++++++++ ],
++++++++ "engines": {
++++++++ "node": ">=6.0"
++++++++ },
++++++++ "author": "Harry Sarson <harry.sarson@hotmail.co.uk>",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/harrysarson/chai-iterator/issues"
++++++++ },
++++++++ "homepage": "https://github.com/harrysarson/chai-iterator",
++++++++ "peerDependencies": {
++++++++ "chai": "4.x"
++++++++ },
++++++++ "devDependencies": {
++++++++ "chai": "4.1.2",
++++++++ "coveralls": "3.0.2",
++++++++ "mocha": "6.0.0",
++++++++ "nyc": "13.3.0",
++++++++ "xo": "0.24.0"
++++++++ },
++++++++ "xo": {
++++++++ "esnext": false,
++++++++ "globals": [
++++++++ "chai",
++++++++ "define",
++++++++ "module"
++++++++ ],
++++++++ "env": [
++++++++ "mocha"
++++++++ ],
++++++++ "rules": {
++++++++ "no-unused-expressions": "off",
++++++++ "no-use-extend-native/no-use-extend-native": "off"
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++The MIT License (MIT)\r
++++++++Copyright (c) 2013 Oleg Nechiporenko\r
++++++++\r
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy\r
++++++++of this software and associated documentation files (the "Software"), to deal\r
++++++++in the Software without restriction, including without limitation the rights\r
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
++++++++copies of the Software, and to permit persons to whom the Software is\r
++++++++furnished to do so, subject to the following conditions:\r
++++++++\r
++++++++The above copyright notice and this permission notice shall be included in all\r
++++++++copies or substantial portions of the Software.\r
++++++++\r
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
++++++++EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
++++++++IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\r
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\r
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE\r
++++++++OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++(function (plugin) {\r
++++++++ if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {\r
++++++++ // NodeJS\r
++++++++ module.exports = plugin;\r
++++++++ }\r
++++++++ else {\r
++++++++ if (typeof define === "function" && define.amd) {\r
++++++++ // AMD\r
++++++++ define(function () {\r
++++++++ return plugin;\r
++++++++ });\r
++++++++ }\r
++++++++ else {\r
++++++++ // Other environment (usually <script> tag): plug in to global chai instance directly.\r
++++++++ chai.use(plugin);\r
++++++++ }\r
++++++++ }\r
++++++++}(function (chai, utils) {\r
++++++++ chai.string = chai.string || {};\r
++++++++\r
++++++++ function isString(value) {\r
++++++++ return typeof value === 'string';\r
++++++++ }\r
++++++++\r
++++++++ chai.string.startsWith = function (str, prefix) {\r
++++++++ if (!isString(str) || !isString(prefix)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str.indexOf(prefix) === 0;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.endsWith = function (str, suffix) {\r
++++++++ if (!isString(str) || !isString(suffix)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str.indexOf(suffix, str.length - suffix.length) !== -1;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.equalIgnoreCase = function (str1, str2) {\r
++++++++ if (!isString(str1) || !isString(str2)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str1.toLowerCase() === str2.toLowerCase();\r
++++++++ };\r
++++++++\r
++++++++ chai.string.equalIgnoreSpaces = function (str1, str2) {\r
++++++++ if (!isString(str1) || !isString(str2)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str1.replace(/\s/g, '') === str2.replace(/\s/g, '');\r
++++++++ };\r
++++++++\r
++++++++ chai.string.containIgnoreSpaces = function (str1, str2) {\r
++++++++ if (!isString(str1) || !isString(str2)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str1.replace(/\s/g, '').indexOf(str2.replace(/\s/g, '')) > -1;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.containIgnoreCase = function (str1, str2) {\r
++++++++ if (!isString(str1) || !isString(str2)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str1.toLowerCase().indexOf(str2.toLowerCase()) > -1;\r
++++++++ }\r
++++++++\r
++++++++ chai.string.singleLine = function (str) {\r
++++++++ if (!isString(str)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str.trim().indexOf("\n") === -1;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.reverseOf = function (str, reversed) {\r
++++++++ if (!isString(str) || !isString(reversed)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ return str.split('').reverse().join('') === reversed;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.palindrome = function (str) {\r
++++++++ if (!isString(str)) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ var len = str.length;\r
++++++++ for (var i = 0; i < Math.floor(len / 2); i++) {\r
++++++++ if (str[i] !== str[len - 1 - i]) {\r
++++++++ return false;\r
++++++++ }\r
++++++++ }\r
++++++++ return true;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.entriesCount = function (str, substr, count) {\r
++++++++ var matches = 0;\r
++++++++ if (isString(str) && isString(substr)) {\r
++++++++ var i = 0;\r
++++++++ var len = str.length;\r
++++++++ while (i < len) {\r
++++++++ var indx = str.indexOf(substr, i);\r
++++++++ if (indx === -1) {\r
++++++++ break;\r
++++++++ }\r
++++++++ else {\r
++++++++ matches++;\r
++++++++ i = indx + 1;\r
++++++++ }\r
++++++++ }\r
++++++++ }\r
++++++++ return matches === count;\r
++++++++ };\r
++++++++\r
++++++++ chai.string.indexOf = function (str, substr, index) {\r
++++++++ var indx = !isString(str) || !isString(substr) ? -1 : str.indexOf(substr);\r
++++++++ return indx === index;\r
++++++++ };\r
++++++++\r
++++++++ var startsWithMethodWrapper = function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.startsWith(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to start with ' + expected,\r
++++++++ 'expected ' + this._obj + ' not to start with ' + expected\r
++++++++ );\r
++++++++ };\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('startsWith', startsWithMethodWrapper);\r
++++++++ chai.Assertion.addChainableMethod('startWith', startsWithMethodWrapper);\r
++++++++\r
++++++++ var endsWithMethodWrapper = function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.endsWith(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to end with ' + expected,\r
++++++++ 'expected ' + this._obj + ' not to end with ' + expected\r
++++++++ );\r
++++++++ };\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('endsWith', endsWithMethodWrapper);\r
++++++++ chai.Assertion.addChainableMethod('endWith', endsWithMethodWrapper);\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('equalIgnoreCase', function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.equalIgnoreCase(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to equal ' + expected + ' ignoring case',\r
++++++++ 'expected ' + this._obj + ' not to equal ' + expected + ' ignoring case'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('equalIgnoreSpaces', function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.equalIgnoreSpaces(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to equal ' + expected + ' ignoring spaces',\r
++++++++ 'expected ' + this._obj + ' not to equal ' + expected + ' ignoring spaces'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('containIgnoreSpaces', function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.containIgnoreSpaces(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to contain ' + expected + ' ignoring spaces',\r
++++++++ 'expected ' + this._obj + ' not to contain ' + expected + ' ignoring spaces'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('containIgnoreCase', function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.containIgnoreCase(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to contain ' + expected + ' ignoring case',\r
++++++++ 'expected ' + this._obj + ' not to contain ' + expected + ' ignoring case'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('singleLine', function () {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.singleLine(actual),\r
++++++++ 'expected ' + this._obj + ' to be a single line',\r
++++++++ 'expected ' + this._obj + ' not to be a single line'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('reverseOf', function (expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.reverseOf(actual, expected),\r
++++++++ 'expected ' + this._obj + ' to be the reverse of ' + expected,\r
++++++++ 'expected ' + this._obj + ' not to be the reverse of ' + expected\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('palindrome', function () {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.palindrome(actual),\r
++++++++ 'expected ' + this._obj + ' to be a palindrome',\r
++++++++ 'expected ' + this._obj + ' not to be a palindrome'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('entriesCount', function (substr, expected) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.entriesCount(actual, substr, expected),\r
++++++++ 'expected ' + this._obj + ' to have ' + substr + ' ' + expected + ' time(s)',\r
++++++++ 'expected ' + this._obj + ' to not have ' + substr + ' ' + expected + ' time(s)'\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ chai.Assertion.addChainableMethod('indexOf', function (substr, index) {\r
++++++++ var actual = this._obj;\r
++++++++\r
++++++++ return this.assert(\r
++++++++ chai.string.indexOf(actual, substr, index),\r
++++++++ 'expected ' + this._obj + ' to have ' + substr + ' on index ' + index,\r
++++++++ 'expected ' + this._obj + ' to not have ' + substr + ' on index ' + index\r
++++++++ );\r
++++++++ });\r
++++++++\r
++++++++ // Asserts\r
++++++++ var assert = chai.assert;\r
++++++++\r
++++++++ assert.startsWith = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.startsWith(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notStartsWith = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.startsWith(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.endsWith = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.endsWith(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notEndsWith = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.endsWith(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.equalIgnoreCase = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.equalIgnoreCase(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notEqualIgnoreCase = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.equalIgnoreCase(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.equalIgnoreSpaces = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.equalIgnoreSpaces(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notEqualIgnoreSpaces = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.equalIgnoreSpaces(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.containIgnoreSpaces = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.containIgnoreSpaces(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notContainIgnoreSpaces = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.containIgnoreSpaces(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.containIgnoreCase = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.containIgnoreCase(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notContainIgnoreCase = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.containIgnoreCase(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.singleLine = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.singleLine();\r
++++++++ };\r
++++++++\r
++++++++ assert.notSingleLine = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.singleLine();\r
++++++++ };\r
++++++++\r
++++++++ assert.reverseOf = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.reverseOf(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.notReverseOf = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.reverseOf(exp);\r
++++++++ };\r
++++++++\r
++++++++ assert.palindrome = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.be.palindrome();\r
++++++++ };\r
++++++++\r
++++++++ assert.notPalindrome = function (val, exp, msg) {\r
++++++++ new chai.Assertion(val, msg).to.not.be.palindrome();\r
++++++++ };\r
++++++++\r
++++++++ assert.entriesCount = function (str, substr, count, msg) {\r
++++++++ new chai.Assertion(str, msg).to.have.entriesCount(substr, count);\r
++++++++ };\r
++++++++\r
++++++++ assert.indexOf = function (str, substr, index, msg) {\r
++++++++ new chai.Assertion(str, msg).to.have.indexOf(substr, index);\r
++++++++ };\r
++++++++\r
++++++++}));\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{\r
++++++++ "name": "chai-string",\r
++++++++ "version": "1.5.0",\r
++++++++ "keywords": [\r
++++++++ "chai",\r
++++++++ "testing",\r
++++++++ "string",\r
++++++++ "chai-plugin",\r
++++++++ "browser"\r
++++++++ ],\r
++++++++ "description": "strings comparison matchers for chai",\r
++++++++ "main": "chai-string.js",\r
++++++++ "scripts": {\r
++++++++ "test": "mocha test"\r
++++++++ },\r
++++++++ "repository": {\r
++++++++ "type": "git",\r
++++++++ "url": "git://github.com/onechiporenko/chai-string.git"\r
++++++++ },\r
++++++++ "author": {\r
++++++++ "name": "Oleg Nechiporenko",\r
++++++++ "url": "https://github.com/onechiporenko"\r
++++++++ },\r
++++++++ "license": "MIT",\r
++++++++ "devDependencies": {\r
++++++++ "chai": "^4.1.2",\r
++++++++ "mocha": "^5.2.0"\r
++++++++ },\r
++++++++ "peerDependencies": {\r
++++++++ "chai": "^4.1.2"\r
++++++++ }\r
++++++++}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++declare namespace delay {
++++++++ interface ClearablePromise<T> extends Promise<T> {
++++++++ /**
++++++++ Clears the delay and settles the promise.
++++++++ */
++++++++ clear(): void;
++++++++ }
++++++++
++++++++ /**
++++++++ Minimal subset of `AbortSignal` that delay will use if passed.
++++++++ This avoids a dependency on dom.d.ts.
++++++++ The dom.d.ts `AbortSignal` is compatible with this one.
++++++++ */
++++++++ interface AbortSignal {
++++++++ readonly aborted: boolean;
++++++++ addEventListener(
++++++++ type: 'abort',
++++++++ listener: () => void,
++++++++ options?: {once?: boolean}
++++++++ ): void;
++++++++ removeEventListener(type: 'abort', listener: () => void): void;
++++++++ }
++++++++
++++++++ interface Options {
++++++++ /**
++++++++ An optional AbortSignal to abort the delay.
++++++++ If aborted, the Promise will be rejected with an AbortError.
++++++++ */
++++++++ signal?: AbortSignal;
++++++++ }
++++++++}
++++++++
++++++++type Delay = {
++++++++ /**
++++++++ Create a promise which resolves after the specified `milliseconds`.
++++++++
++++++++ @param milliseconds - Milliseconds to delay the promise.
++++++++ @returns A promise which resolves after the specified `milliseconds`.
++++++++ */
++++++++ (milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>;
++++++++
++++++++ /**
++++++++ Create a promise which resolves after the specified `milliseconds`.
++++++++
++++++++ @param milliseconds - Milliseconds to delay the promise.
++++++++ @returns A promise which resolves after the specified `milliseconds`.
++++++++ */
++++++++ <T>(
++++++++ milliseconds: number,
++++++++ options?: delay.Options & {
++++++++ /**
++++++++ Value to resolve in the returned promise.
++++++++ */
++++++++ value: T;
++++++++ }
++++++++ ): delay.ClearablePromise<T>;
++++++++
++++++++ /**
++++++++ Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.
++++++++
++++++++ Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead.
++++++++
++++++++ @param minimum - Minimum amount of milliseconds to delay the promise.
++++++++ @param maximum - Maximum amount of milliseconds to delay the promise.
++++++++ @returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed.
++++++++ */
++++++++ range<T>(
++++++++ minimum: number,
++++++++ maximum: number,
++++++++ options?: delay.Options & {
++++++++ /**
++++++++ Value to resolve in the returned promise.
++++++++ */
++++++++ value: T;
++++++++ }
++++++++ ): delay.ClearablePromise<T>;
++++++++
++++++++ // TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 is resolved.
++++++++ /**
++++++++ Create a promise which rejects after the specified `milliseconds`.
++++++++
++++++++ @param milliseconds - Milliseconds to delay the promise.
++++++++ @returns A promise which rejects after the specified `milliseconds`.
++++++++ */
++++++++ reject(
++++++++ milliseconds: number,
++++++++ options?: delay.Options & {
++++++++ /**
++++++++ Value to reject in the returned promise.
++++++++ */
++++++++ value?: unknown;
++++++++ }
++++++++ ): delay.ClearablePromise<never>;
++++++++};
++++++++
++++++++declare const delay: Delay & {
++++++++ // The types are intentionally loose to make it work with both Node.js and browser versions of these methods.
++++++++ createWithTimers(timers: {
++++++++ clearTimeout: (timeoutId: any) => void;
++++++++ setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown;
++++++++ }): Delay;
++++++++
++++++++ // TODO: Remove this for the next major release.
++++++++ default: typeof delay;
++++++++};
++++++++
++++++++export = delay;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict';
++++++++
++++++++// From https://github.com/sindresorhus/random-int/blob/c37741b56f76b9160b0b63dae4e9c64875128146/index.js#L13-L15
++++++++const randomInteger = (minimum, maximum) => Math.floor((Math.random() * (maximum - minimum + 1)) + minimum);
++++++++
++++++++const createAbortError = () => {
++++++++ const error = new Error('Delay aborted');
++++++++ error.name = 'AbortError';
++++++++ return error;
++++++++};
++++++++
++++++++const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve}) => (ms, {value, signal} = {}) => {
++++++++ if (signal && signal.aborted) {
++++++++ return Promise.reject(createAbortError());
++++++++ }
++++++++
++++++++ let timeoutId;
++++++++ let settle;
++++++++ let rejectFn;
++++++++ const clear = defaultClear || clearTimeout;
++++++++
++++++++ const signalListener = () => {
++++++++ clear(timeoutId);
++++++++ rejectFn(createAbortError());
++++++++ };
++++++++
++++++++ const cleanup = () => {
++++++++ if (signal) {
++++++++ signal.removeEventListener('abort', signalListener);
++++++++ }
++++++++ };
++++++++
++++++++ const delayPromise = new Promise((resolve, reject) => {
++++++++ settle = () => {
++++++++ cleanup();
++++++++ if (willResolve) {
++++++++ resolve(value);
++++++++ } else {
++++++++ reject(value);
++++++++ }
++++++++ };
++++++++
++++++++ rejectFn = reject;
++++++++ timeoutId = (set || setTimeout)(settle, ms);
++++++++ });
++++++++
++++++++ if (signal) {
++++++++ signal.addEventListener('abort', signalListener, {once: true});
++++++++ }
++++++++
++++++++ delayPromise.clear = () => {
++++++++ clear(timeoutId);
++++++++ timeoutId = null;
++++++++ settle();
++++++++ };
++++++++
++++++++ return delayPromise;
++++++++};
++++++++
++++++++const createWithTimers = clearAndSet => {
++++++++ const delay = createDelay({...clearAndSet, willResolve: true});
++++++++ delay.reject = createDelay({...clearAndSet, willResolve: false});
++++++++ delay.range = (minimum, maximum, options) => delay(randomInteger(minimum, maximum), options);
++++++++ return delay;
++++++++};
++++++++
++++++++const delay = createWithTimers();
++++++++delay.createWithTimers = createWithTimers;
++++++++
++++++++module.exports = delay;
++++++++// TODO: Remove this for the next major release
++++++++module.exports.default = delay;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++MIT License
++++++++
++++++++Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "delay",
++++++++ "version": "5.0.0",
++++++++ "description": "Delay a promise a specified amount of time",
++++++++ "license": "MIT",
++++++++ "repository": "sindresorhus/delay",
++++++++ "funding": "https://github.com/sponsors/sindresorhus",
++++++++ "author": {
++++++++ "name": "Sindre Sorhus",
++++++++ "email": "sindresorhus@gmail.com",
++++++++ "url": "https://sindresorhus.com"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "scripts": {
++++++++ "test": "xo && ava && tsd"
++++++++ },
++++++++ "files": [
++++++++ "index.js",
++++++++ "index.d.ts"
++++++++ ],
++++++++ "keywords": [
++++++++ "promise",
++++++++ "resolve",
++++++++ "delay",
++++++++ "defer",
++++++++ "wait",
++++++++ "stall",
++++++++ "timeout",
++++++++ "settimeout",
++++++++ "event",
++++++++ "loop",
++++++++ "next",
++++++++ "tick",
++++++++ "delay",
++++++++ "async",
++++++++ "await",
++++++++ "promises",
++++++++ "bluebird",
++++++++ "threshold",
++++++++ "range",
++++++++ "random"
++++++++ ],
++++++++ "devDependencies": {
++++++++ "abort-controller": "^3.0.0",
++++++++ "ava": "1.4.1",
++++++++ "currently-unhandled": "^0.4.1",
++++++++ "in-range": "^1.0.0",
++++++++ "time-span": "^3.0.0",
++++++++ "tsd": "^0.7.1",
++++++++ "xo": "^0.24.0"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Copyright Brian White. All rights reserved.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to
++++++++deal in the Software without restriction, including without limitation the
++++++++rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++++++++sell copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in
++++++++all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++++++++FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++++++++IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++var WritableStream = require('stream').Writable,
++++++++ inherits = require('util').inherits;
++++++++
++++++++var StreamSearch = require('streamsearch');
++++++++
++++++++var PartStream = require('./PartStream'),
++++++++ HeaderParser = require('./HeaderParser');
++++++++
++++++++var DASH = 45,
++++++++ B_ONEDASH = Buffer.from('-'),
++++++++ B_CRLF = Buffer.from('\r\n'),
++++++++ EMPTY_FN = function() {};
++++++++
++++++++function Dicer(cfg) {
++++++++ if (!(this instanceof Dicer))
++++++++ return new Dicer(cfg);
++++++++ WritableStream.call(this, cfg);
++++++++
++++++++ if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string'))
++++++++ throw new TypeError('Boundary required');
++++++++
++++++++ if (typeof cfg.boundary === 'string')
++++++++ this.setBoundary(cfg.boundary);
++++++++ else
++++++++ this._bparser = undefined;
++++++++
++++++++ this._headerFirst = cfg.headerFirst;
++++++++
++++++++ var self = this;
++++++++
++++++++ this._dashes = 0;
++++++++ this._parts = 0;
++++++++ this._finished = false;
++++++++ this._realFinish = false;
++++++++ this._isPreamble = true;
++++++++ this._justMatched = false;
++++++++ this._firstWrite = true;
++++++++ this._inHeader = true;
++++++++ this._part = undefined;
++++++++ this._cb = undefined;
++++++++ this._ignoreData = false;
++++++++ this._partOpts = (typeof cfg.partHwm === 'number'
++++++++ ? { highWaterMark: cfg.partHwm }
++++++++ : {});
++++++++ this._pause = false;
++++++++
++++++++ this._hparser = new HeaderParser(cfg);
++++++++ this._hparser.on('header', function(header) {
++++++++ self._inHeader = false;
++++++++ self._part.emit('header', header);
++++++++ });
++++++++
++++++++}
++++++++inherits(Dicer, WritableStream);
++++++++
++++++++Dicer.prototype.emit = function(ev) {
++++++++ if (ev === 'finish' && !this._realFinish) {
++++++++ if (!this._finished) {
++++++++ var self = this;
++++++++ process.nextTick(function() {
++++++++ self.emit('error', new Error('Unexpected end of multipart data'));
++++++++ if (self._part && !self._ignoreData) {
++++++++ var type = (self._isPreamble ? 'Preamble' : 'Part');
++++++++ self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'));
++++++++ self._part.push(null);
++++++++ process.nextTick(function() {
++++++++ self._realFinish = true;
++++++++ self.emit('finish');
++++++++ self._realFinish = false;
++++++++ });
++++++++ return;
++++++++ }
++++++++ self._realFinish = true;
++++++++ self.emit('finish');
++++++++ self._realFinish = false;
++++++++ });
++++++++ }
++++++++ } else
++++++++ WritableStream.prototype.emit.apply(this, arguments);
++++++++};
++++++++
++++++++Dicer.prototype._write = function(data, encoding, cb) {
++++++++ // ignore unexpected data (e.g. extra trailer data after finished)
++++++++ if (!this._hparser && !this._bparser)
++++++++ return cb();
++++++++
++++++++ if (this._headerFirst && this._isPreamble) {
++++++++ if (!this._part) {
++++++++ this._part = new PartStream(this._partOpts);
++++++++ if (this._events.preamble)
++++++++ this.emit('preamble', this._part);
++++++++ else
++++++++ this._ignore();
++++++++ }
++++++++ var r = this._hparser.push(data);
++++++++ if (!this._inHeader && r !== undefined && r < data.length)
++++++++ data = data.slice(r);
++++++++ else
++++++++ return cb();
++++++++ }
++++++++
++++++++ // allows for "easier" testing
++++++++ if (this._firstWrite) {
++++++++ this._bparser.push(B_CRLF);
++++++++ this._firstWrite = false;
++++++++ }
++++++++
++++++++ this._bparser.push(data);
++++++++
++++++++ if (this._pause)
++++++++ this._cb = cb;
++++++++ else
++++++++ cb();
++++++++};
++++++++
++++++++Dicer.prototype.reset = function() {
++++++++ this._part = undefined;
++++++++ this._bparser = undefined;
++++++++ this._hparser = undefined;
++++++++};
++++++++
++++++++Dicer.prototype.setBoundary = function(boundary) {
++++++++ var self = this;
++++++++ this._bparser = new StreamSearch('\r\n--' + boundary);
++++++++ this._bparser.on('info', function(isMatch, data, start, end) {
++++++++ self._oninfo(isMatch, data, start, end);
++++++++ });
++++++++};
++++++++
++++++++Dicer.prototype._ignore = function() {
++++++++ if (this._part && !this._ignoreData) {
++++++++ this._ignoreData = true;
++++++++ this._part.on('error', EMPTY_FN);
++++++++ // we must perform some kind of read on the stream even though we are
++++++++ // ignoring the data, otherwise node's Readable stream will not emit 'end'
++++++++ // after pushing null to the stream
++++++++ this._part.resume();
++++++++ }
++++++++};
++++++++
++++++++Dicer.prototype._oninfo = function(isMatch, data, start, end) {
++++++++ var buf, self = this, i = 0, r, ev, shouldWriteMore = true;
++++++++
++++++++ if (!this._part && this._justMatched && data) {
++++++++ while (this._dashes < 2 && (start + i) < end) {
++++++++ if (data[start + i] === DASH) {
++++++++ ++i;
++++++++ ++this._dashes;
++++++++ } else {
++++++++ if (this._dashes)
++++++++ buf = B_ONEDASH;
++++++++ this._dashes = 0;
++++++++ break;
++++++++ }
++++++++ }
++++++++ if (this._dashes === 2) {
++++++++ if ((start + i) < end && this._events.trailer)
++++++++ this.emit('trailer', data.slice(start + i, end));
++++++++ this.reset();
++++++++ this._finished = true;
++++++++ // no more parts will be added
++++++++ if (self._parts === 0) {
++++++++ self._realFinish = true;
++++++++ self.emit('finish');
++++++++ self._realFinish = false;
++++++++ }
++++++++ }
++++++++ if (this._dashes)
++++++++ return;
++++++++ }
++++++++ if (this._justMatched)
++++++++ this._justMatched = false;
++++++++ if (!this._part) {
++++++++ this._part = new PartStream(this._partOpts);
++++++++ this._part._read = function(n) {
++++++++ self._unpause();
++++++++ };
++++++++ ev = this._isPreamble ? 'preamble' : 'part';
++++++++ if (this._events[ev])
++++++++ this.emit(ev, this._part);
++++++++ else
++++++++ this._ignore();
++++++++ if (!this._isPreamble)
++++++++ this._inHeader = true;
++++++++ }
++++++++ if (data && start < end && !this._ignoreData) {
++++++++ if (this._isPreamble || !this._inHeader) {
++++++++ if (buf)
++++++++ shouldWriteMore = this._part.push(buf);
++++++++ shouldWriteMore = this._part.push(data.slice(start, end));
++++++++ if (!shouldWriteMore)
++++++++ this._pause = true;
++++++++ } else if (!this._isPreamble && this._inHeader) {
++++++++ if (buf)
++++++++ this._hparser.push(buf);
++++++++ r = this._hparser.push(data.slice(start, end));
++++++++ if (!this._inHeader && r !== undefined && r < end)
++++++++ this._oninfo(false, data, start + r, end);
++++++++ }
++++++++ }
++++++++ if (isMatch) {
++++++++ this._hparser.reset();
++++++++ if (this._isPreamble)
++++++++ this._isPreamble = false;
++++++++ else {
++++++++ ++this._parts;
++++++++ this._part.on('end', function() {
++++++++ if (--self._parts === 0) {
++++++++ if (self._finished) {
++++++++ self._realFinish = true;
++++++++ self.emit('finish');
++++++++ self._realFinish = false;
++++++++ } else {
++++++++ self._unpause();
++++++++ }
++++++++ }
++++++++ });
++++++++ }
++++++++ this._part.push(null);
++++++++ this._part = undefined;
++++++++ this._ignoreData = false;
++++++++ this._justMatched = true;
++++++++ this._dashes = 0;
++++++++ }
++++++++};
++++++++
++++++++Dicer.prototype._unpause = function() {
++++++++ if (!this._pause)
++++++++ return;
++++++++
++++++++ this._pause = false;
++++++++ if (this._cb) {
++++++++ var cb = this._cb;
++++++++ this._cb = undefined;
++++++++ cb();
++++++++ }
++++++++};
++++++++
++++++++module.exports = Dicer;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++var EventEmitter = require('events').EventEmitter,
++++++++ inherits = require('util').inherits;
++++++++
++++++++var StreamSearch = require('streamsearch');
++++++++
++++++++var B_DCRLF = Buffer.from('\r\n\r\n'),
++++++++ RE_CRLF = /\r\n/g,
++++++++ RE_HDR = /^([^:]+):[ \t]?([\x00-\xFF]+)?$/,
++++++++ MAX_HEADER_PAIRS = 2000, // from node's http.js
++++++++ MAX_HEADER_SIZE = 80 * 1024; // from node's http_parser
++++++++
++++++++function HeaderParser(cfg) {
++++++++ EventEmitter.call(this);
++++++++
++++++++ var self = this;
++++++++ this.nread = 0;
++++++++ this.maxed = false;
++++++++ this.npairs = 0;
++++++++ this.maxHeaderPairs = (cfg && typeof cfg.maxHeaderPairs === 'number'
++++++++ ? cfg.maxHeaderPairs
++++++++ : MAX_HEADER_PAIRS);
++++++++ this.buffer = '';
++++++++ this.header = {};
++++++++ this.finished = false;
++++++++ this.ss = new StreamSearch(B_DCRLF);
++++++++ this.ss.on('info', function(isMatch, data, start, end) {
++++++++ if (data && !self.maxed) {
++++++++ if (self.nread + (end - start) > MAX_HEADER_SIZE) {
++++++++ end = (MAX_HEADER_SIZE - self.nread);
++++++++ self.nread = MAX_HEADER_SIZE;
++++++++ } else
++++++++ self.nread += (end - start);
++++++++
++++++++ if (self.nread === MAX_HEADER_SIZE)
++++++++ self.maxed = true;
++++++++
++++++++ self.buffer += data.toString('binary', start, end);
++++++++ }
++++++++ if (isMatch)
++++++++ self._finish();
++++++++ });
++++++++}
++++++++inherits(HeaderParser, EventEmitter);
++++++++
++++++++HeaderParser.prototype.push = function(data) {
++++++++ var r = this.ss.push(data);
++++++++ if (this.finished)
++++++++ return r;
++++++++};
++++++++
++++++++HeaderParser.prototype.reset = function() {
++++++++ this.finished = false;
++++++++ this.buffer = '';
++++++++ this.header = {};
++++++++ this.ss.reset();
++++++++};
++++++++
++++++++HeaderParser.prototype._finish = function() {
++++++++ if (this.buffer)
++++++++ this._parseHeader();
++++++++ this.ss.matches = this.ss.maxMatches;
++++++++ var header = this.header;
++++++++ this.header = {};
++++++++ this.buffer = '';
++++++++ this.finished = true;
++++++++ this.nread = this.npairs = 0;
++++++++ this.maxed = false;
++++++++ this.emit('header', header);
++++++++};
++++++++
++++++++HeaderParser.prototype._parseHeader = function() {
++++++++ if (this.npairs === this.maxHeaderPairs)
++++++++ return;
++++++++
++++++++ var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h,
++++++++ modded = false;
++++++++
++++++++ for (var i = 0; i < len; ++i) {
++++++++ if (lines[i].length === 0)
++++++++ continue;
++++++++ if (lines[i][0] === '\t' || lines[i][0] === ' ') {
++++++++ // folded header content
++++++++ // RFC2822 says to just remove the CRLF and not the whitespace following
++++++++ // it, so we follow the RFC and include the leading whitespace ...
++++++++ this.header[h][this.header[h].length - 1] += lines[i];
++++++++ } else {
++++++++ m = RE_HDR.exec(lines[i]);
++++++++ if (m) {
++++++++ h = m[1].toLowerCase();
++++++++ if (m[2]) {
++++++++ if (this.header[h] === undefined)
++++++++ this.header[h] = [m[2]];
++++++++ else
++++++++ this.header[h].push(m[2]);
++++++++ } else
++++++++ this.header[h] = [''];
++++++++ if (++this.npairs === this.maxHeaderPairs)
++++++++ break;
++++++++ } else {
++++++++ this.buffer = lines[i];
++++++++ modded = true;
++++++++ break;
++++++++ }
++++++++ }
++++++++ }
++++++++ if (!modded)
++++++++ this.buffer = '';
++++++++};
++++++++
++++++++module.exports = HeaderParser;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++var inherits = require('util').inherits,
++++++++ ReadableStream = require('stream').Readable;
++++++++
++++++++function PartStream(opts) {
++++++++ ReadableStream.call(this, opts);
++++++++}
++++++++inherits(PartStream, ReadableStream);
++++++++
++++++++PartStream.prototype._read = function(n) {};
++++++++
++++++++module.exports = PartStream;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{ "name": "dicer",
++++++++ "version": "0.3.0",
++++++++ "author": "Brian White <mscdex@mscdex.net>",
++++++++ "description": "A very fast streaming multipart parser for node.js",
++++++++ "main": "./lib/Dicer",
++++++++ "dependencies": {
++++++++ "streamsearch": "0.1.2"
++++++++ },
++++++++ "scripts": {
++++++++ "test": "node test/test.js"
++++++++ },
++++++++ "engines": { "node": ">=4.5.0" },
++++++++ "keywords": [ "parser", "parse", "parsing", "multipart", "form-data", "streaming" ],
++++++++ "licenses": [ { "type": "MIT", "url": "http://github.com/mscdex/dicer/raw/master/LICENSE" } ],
++++++++ "repository" : { "type": "git", "url": "http://github.com/mscdex/dicer.git" }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++The MIT License (MIT)
++++++++
++++++++Copyright (c) 2015 Toru Nagashima
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to deal
++++++++in the Software without restriction, including without limitation the rights
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++SOFTWARE.
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * @author Toru Nagashima <https://github.com/mysticatea>
++++++++ * @copyright 2015 Toru Nagashima. All rights reserved.
++++++++ * See LICENSE file in root directory for full license.
++++++++ */
++++++++'use strict';
++++++++
++++++++Object.defineProperty(exports, '__esModule', { value: true });
++++++++
++++++++/**
++++++++ * @typedef {object} PrivateData
++++++++ * @property {EventTarget} eventTarget The event target.
++++++++ * @property {{type:string}} event The original event object.
++++++++ * @property {number} eventPhase The current event phase.
++++++++ * @property {EventTarget|null} currentTarget The current event target.
++++++++ * @property {boolean} canceled The flag to prevent default.
++++++++ * @property {boolean} stopped The flag to stop propagation.
++++++++ * @property {boolean} immediateStopped The flag to stop propagation immediately.
++++++++ * @property {Function|null} passiveListener The listener if the current listener is passive. Otherwise this is null.
++++++++ * @property {number} timeStamp The unix time.
++++++++ * @private
++++++++ */
++++++++
++++++++/**
++++++++ * Private data for event wrappers.
++++++++ * @type {WeakMap<Event, PrivateData>}
++++++++ * @private
++++++++ */
++++++++const privateData = new WeakMap();
++++++++
++++++++/**
++++++++ * Cache for wrapper classes.
++++++++ * @type {WeakMap<Object, Function>}
++++++++ * @private
++++++++ */
++++++++const wrappers = new WeakMap();
++++++++
++++++++/**
++++++++ * Get private data.
++++++++ * @param {Event} event The event object to get private data.
++++++++ * @returns {PrivateData} The private data of the event.
++++++++ * @private
++++++++ */
++++++++function pd(event) {
++++++++ const retv = privateData.get(event);
++++++++ console.assert(
++++++++ retv != null,
++++++++ "'this' is expected an Event object, but got",
++++++++ event
++++++++ );
++++++++ return retv
++++++++}
++++++++
++++++++/**
++++++++ * https://dom.spec.whatwg.org/#set-the-canceled-flag
++++++++ * @param data {PrivateData} private data.
++++++++ */
++++++++function setCancelFlag(data) {
++++++++ if (data.passiveListener != null) {
++++++++ if (
++++++++ typeof console !== "undefined" &&
++++++++ typeof console.error === "function"
++++++++ ) {
++++++++ console.error(
++++++++ "Unable to preventDefault inside passive event listener invocation.",
++++++++ data.passiveListener
++++++++ );
++++++++ }
++++++++ return
++++++++ }
++++++++ if (!data.event.cancelable) {
++++++++ return
++++++++ }
++++++++
++++++++ data.canceled = true;
++++++++ if (typeof data.event.preventDefault === "function") {
++++++++ data.event.preventDefault();
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * @see https://dom.spec.whatwg.org/#interface-event
++++++++ * @private
++++++++ */
++++++++/**
++++++++ * The event wrapper.
++++++++ * @constructor
++++++++ * @param {EventTarget} eventTarget The event target of this dispatching.
++++++++ * @param {Event|{type:string}} event The original event to wrap.
++++++++ */
++++++++function Event(eventTarget, event) {
++++++++ privateData.set(this, {
++++++++ eventTarget,
++++++++ event,
++++++++ eventPhase: 2,
++++++++ currentTarget: eventTarget,
++++++++ canceled: false,
++++++++ stopped: false,
++++++++ immediateStopped: false,
++++++++ passiveListener: null,
++++++++ timeStamp: event.timeStamp || Date.now(),
++++++++ });
++++++++
++++++++ // https://heycam.github.io/webidl/#Unforgeable
++++++++ Object.defineProperty(this, "isTrusted", { value: false, enumerable: true });
++++++++
++++++++ // Define accessors
++++++++ const keys = Object.keys(event);
++++++++ for (let i = 0; i < keys.length; ++i) {
++++++++ const key = keys[i];
++++++++ if (!(key in this)) {
++++++++ Object.defineProperty(this, key, defineRedirectDescriptor(key));
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++// Should be enumerable, but class methods are not enumerable.
++++++++Event.prototype = {
++++++++ /**
++++++++ * The type of this event.
++++++++ * @type {string}
++++++++ */
++++++++ get type() {
++++++++ return pd(this).event.type
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ */
++++++++ get target() {
++++++++ return pd(this).eventTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ */
++++++++ get currentTarget() {
++++++++ return pd(this).currentTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * @returns {EventTarget[]} The composed path of this event.
++++++++ */
++++++++ composedPath() {
++++++++ const currentTarget = pd(this).currentTarget;
++++++++ if (currentTarget == null) {
++++++++ return []
++++++++ }
++++++++ return [currentTarget]
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of NONE.
++++++++ * @type {number}
++++++++ */
++++++++ get NONE() {
++++++++ return 0
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of CAPTURING_PHASE.
++++++++ * @type {number}
++++++++ */
++++++++ get CAPTURING_PHASE() {
++++++++ return 1
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of AT_TARGET.
++++++++ * @type {number}
++++++++ */
++++++++ get AT_TARGET() {
++++++++ return 2
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of BUBBLING_PHASE.
++++++++ * @type {number}
++++++++ */
++++++++ get BUBBLING_PHASE() {
++++++++ return 3
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {number}
++++++++ */
++++++++ get eventPhase() {
++++++++ return pd(this).eventPhase
++++++++ },
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ * @returns {void}
++++++++ */
++++++++ stopPropagation() {
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ if (typeof data.event.stopPropagation === "function") {
++++++++ data.event.stopPropagation();
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ * @returns {void}
++++++++ */
++++++++ stopImmediatePropagation() {
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ data.immediateStopped = true;
++++++++ if (typeof data.event.stopImmediatePropagation === "function") {
++++++++ data.event.stopImmediatePropagation();
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be bubbling.
++++++++ * @type {boolean}
++++++++ */
++++++++ get bubbles() {
++++++++ return Boolean(pd(this).event.bubbles)
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be cancelable.
++++++++ * @type {boolean}
++++++++ */
++++++++ get cancelable() {
++++++++ return Boolean(pd(this).event.cancelable)
++++++++ },
++++++++
++++++++ /**
++++++++ * Cancel this event.
++++++++ * @returns {void}
++++++++ */
++++++++ preventDefault() {
++++++++ setCancelFlag(pd(this));
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to indicate cancellation state.
++++++++ * @type {boolean}
++++++++ */
++++++++ get defaultPrevented() {
++++++++ return pd(this).canceled
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be composed.
++++++++ * @type {boolean}
++++++++ */
++++++++ get composed() {
++++++++ return Boolean(pd(this).event.composed)
++++++++ },
++++++++
++++++++ /**
++++++++ * The unix time of this event.
++++++++ * @type {number}
++++++++ */
++++++++ get timeStamp() {
++++++++ return pd(this).timeStamp
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ * @deprecated
++++++++ */
++++++++ get srcElement() {
++++++++ return pd(this).eventTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to stop event bubbling.
++++++++ * @type {boolean}
++++++++ * @deprecated
++++++++ */
++++++++ get cancelBubble() {
++++++++ return pd(this).stopped
++++++++ },
++++++++ set cancelBubble(value) {
++++++++ if (!value) {
++++++++ return
++++++++ }
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ if (typeof data.event.cancelBubble === "boolean") {
++++++++ data.event.cancelBubble = true;
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to indicate cancellation state.
++++++++ * @type {boolean}
++++++++ * @deprecated
++++++++ */
++++++++ get returnValue() {
++++++++ return !pd(this).canceled
++++++++ },
++++++++ set returnValue(value) {
++++++++ if (!value) {
++++++++ setCancelFlag(pd(this));
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Initialize this event object. But do nothing under event dispatching.
++++++++ * @param {string} type The event type.
++++++++ * @param {boolean} [bubbles=false] The flag to be possible to bubble up.
++++++++ * @param {boolean} [cancelable=false] The flag to be possible to cancel.
++++++++ * @deprecated
++++++++ */
++++++++ initEvent() {
++++++++ // Do nothing.
++++++++ },
++++++++};
++++++++
++++++++// `constructor` is not enumerable.
++++++++Object.defineProperty(Event.prototype, "constructor", {
++++++++ value: Event,
++++++++ configurable: true,
++++++++ writable: true,
++++++++});
++++++++
++++++++// Ensure `event instanceof window.Event` is `true`.
++++++++if (typeof window !== "undefined" && typeof window.Event !== "undefined") {
++++++++ Object.setPrototypeOf(Event.prototype, window.Event.prototype);
++++++++
++++++++ // Make association for wrappers.
++++++++ wrappers.set(window.Event.prototype, Event);
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor to redirect a given property.
++++++++ * @param {string} key Property name to define property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor to redirect the property.
++++++++ * @private
++++++++ */
++++++++function defineRedirectDescriptor(key) {
++++++++ return {
++++++++ get() {
++++++++ return pd(this).event[key]
++++++++ },
++++++++ set(value) {
++++++++ pd(this).event[key] = value;
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor to call a given method property.
++++++++ * @param {string} key Property name to define property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor to call the method property.
++++++++ * @private
++++++++ */
++++++++function defineCallDescriptor(key) {
++++++++ return {
++++++++ value() {
++++++++ const event = pd(this).event;
++++++++ return event[key].apply(event, arguments)
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Define new wrapper class.
++++++++ * @param {Function} BaseEvent The base wrapper class.
++++++++ * @param {Object} proto The prototype of the original event.
++++++++ * @returns {Function} The defined wrapper class.
++++++++ * @private
++++++++ */
++++++++function defineWrapper(BaseEvent, proto) {
++++++++ const keys = Object.keys(proto);
++++++++ if (keys.length === 0) {
++++++++ return BaseEvent
++++++++ }
++++++++
++++++++ /** CustomEvent */
++++++++ function CustomEvent(eventTarget, event) {
++++++++ BaseEvent.call(this, eventTarget, event);
++++++++ }
++++++++
++++++++ CustomEvent.prototype = Object.create(BaseEvent.prototype, {
++++++++ constructor: { value: CustomEvent, configurable: true, writable: true },
++++++++ });
++++++++
++++++++ // Define accessors.
++++++++ for (let i = 0; i < keys.length; ++i) {
++++++++ const key = keys[i];
++++++++ if (!(key in BaseEvent.prototype)) {
++++++++ const descriptor = Object.getOwnPropertyDescriptor(proto, key);
++++++++ const isFunc = typeof descriptor.value === "function";
++++++++ Object.defineProperty(
++++++++ CustomEvent.prototype,
++++++++ key,
++++++++ isFunc
++++++++ ? defineCallDescriptor(key)
++++++++ : defineRedirectDescriptor(key)
++++++++ );
++++++++ }
++++++++ }
++++++++
++++++++ return CustomEvent
++++++++}
++++++++
++++++++/**
++++++++ * Get the wrapper class of a given prototype.
++++++++ * @param {Object} proto The prototype of the original event to get its wrapper.
++++++++ * @returns {Function} The wrapper class.
++++++++ * @private
++++++++ */
++++++++function getWrapper(proto) {
++++++++ if (proto == null || proto === Object.prototype) {
++++++++ return Event
++++++++ }
++++++++
++++++++ let wrapper = wrappers.get(proto);
++++++++ if (wrapper == null) {
++++++++ wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto);
++++++++ wrappers.set(proto, wrapper);
++++++++ }
++++++++ return wrapper
++++++++}
++++++++
++++++++/**
++++++++ * Wrap a given event to management a dispatching.
++++++++ * @param {EventTarget} eventTarget The event target of this dispatching.
++++++++ * @param {Object} event The event to wrap.
++++++++ * @returns {Event} The wrapper instance.
++++++++ * @private
++++++++ */
++++++++function wrapEvent(eventTarget, event) {
++++++++ const Wrapper = getWrapper(Object.getPrototypeOf(event));
++++++++ return new Wrapper(eventTarget, event)
++++++++}
++++++++
++++++++/**
++++++++ * Get the immediateStopped flag of a given event.
++++++++ * @param {Event} event The event to get.
++++++++ * @returns {boolean} The flag to stop propagation immediately.
++++++++ * @private
++++++++ */
++++++++function isStopped(event) {
++++++++ return pd(event).immediateStopped
++++++++}
++++++++
++++++++/**
++++++++ * Set the current event phase of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {number} eventPhase New event phase.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setEventPhase(event, eventPhase) {
++++++++ pd(event).eventPhase = eventPhase;
++++++++}
++++++++
++++++++/**
++++++++ * Set the current target of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {EventTarget|null} currentTarget New current target.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setCurrentTarget(event, currentTarget) {
++++++++ pd(event).currentTarget = currentTarget;
++++++++}
++++++++
++++++++/**
++++++++ * Set a passive listener of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {Function|null} passiveListener New passive listener.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setPassiveListener(event, passiveListener) {
++++++++ pd(event).passiveListener = passiveListener;
++++++++}
++++++++
++++++++/**
++++++++ * @typedef {object} ListenerNode
++++++++ * @property {Function} listener
++++++++ * @property {1|2|3} listenerType
++++++++ * @property {boolean} passive
++++++++ * @property {boolean} once
++++++++ * @property {ListenerNode|null} next
++++++++ * @private
++++++++ */
++++++++
++++++++/**
++++++++ * @type {WeakMap<object, Map<string, ListenerNode>>}
++++++++ * @private
++++++++ */
++++++++const listenersMap = new WeakMap();
++++++++
++++++++// Listener types
++++++++const CAPTURE = 1;
++++++++const BUBBLE = 2;
++++++++const ATTRIBUTE = 3;
++++++++
++++++++/**
++++++++ * Check whether a given value is an object or not.
++++++++ * @param {any} x The value to check.
++++++++ * @returns {boolean} `true` if the value is an object.
++++++++ */
++++++++function isObject(x) {
++++++++ return x !== null && typeof x === "object" //eslint-disable-line no-restricted-syntax
++++++++}
++++++++
++++++++/**
++++++++ * Get listeners.
++++++++ * @param {EventTarget} eventTarget The event target to get.
++++++++ * @returns {Map<string, ListenerNode>} The listeners.
++++++++ * @private
++++++++ */
++++++++function getListeners(eventTarget) {
++++++++ const listeners = listenersMap.get(eventTarget);
++++++++ if (listeners == null) {
++++++++ throw new TypeError(
++++++++ "'this' is expected an EventTarget object, but got another value."
++++++++ )
++++++++ }
++++++++ return listeners
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor for the event attribute of a given event.
++++++++ * @param {string} eventName The event name to get property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor.
++++++++ * @private
++++++++ */
++++++++function defineEventAttributeDescriptor(eventName) {
++++++++ return {
++++++++ get() {
++++++++ const listeners = getListeners(this);
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (node.listenerType === ATTRIBUTE) {
++++++++ return node.listener
++++++++ }
++++++++ node = node.next;
++++++++ }
++++++++ return null
++++++++ },
++++++++
++++++++ set(listener) {
++++++++ if (typeof listener !== "function" && !isObject(listener)) {
++++++++ listener = null; // eslint-disable-line no-param-reassign
++++++++ }
++++++++ const listeners = getListeners(this);
++++++++
++++++++ // Traverse to the tail while removing old value.
++++++++ let prev = null;
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (node.listenerType === ATTRIBUTE) {
++++++++ // Remove old value.
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ } else {
++++++++ prev = node;
++++++++ }
++++++++
++++++++ node = node.next;
++++++++ }
++++++++
++++++++ // Add new value.
++++++++ if (listener !== null) {
++++++++ const newNode = {
++++++++ listener,
++++++++ listenerType: ATTRIBUTE,
++++++++ passive: false,
++++++++ once: false,
++++++++ next: null,
++++++++ };
++++++++ if (prev === null) {
++++++++ listeners.set(eventName, newNode);
++++++++ } else {
++++++++ prev.next = newNode;
++++++++ }
++++++++ }
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Define an event attribute (e.g. `eventTarget.onclick`).
++++++++ * @param {Object} eventTargetPrototype The event target prototype to define an event attrbite.
++++++++ * @param {string} eventName The event name to define.
++++++++ * @returns {void}
++++++++ */
++++++++function defineEventAttribute(eventTargetPrototype, eventName) {
++++++++ Object.defineProperty(
++++++++ eventTargetPrototype,
++++++++ `on${eventName}`,
++++++++ defineEventAttributeDescriptor(eventName)
++++++++ );
++++++++}
++++++++
++++++++/**
++++++++ * Define a custom EventTarget with event attributes.
++++++++ * @param {string[]} eventNames Event names for event attributes.
++++++++ * @returns {EventTarget} The custom EventTarget.
++++++++ * @private
++++++++ */
++++++++function defineCustomEventTarget(eventNames) {
++++++++ /** CustomEventTarget */
++++++++ function CustomEventTarget() {
++++++++ EventTarget.call(this);
++++++++ }
++++++++
++++++++ CustomEventTarget.prototype = Object.create(EventTarget.prototype, {
++++++++ constructor: {
++++++++ value: CustomEventTarget,
++++++++ configurable: true,
++++++++ writable: true,
++++++++ },
++++++++ });
++++++++
++++++++ for (let i = 0; i < eventNames.length; ++i) {
++++++++ defineEventAttribute(CustomEventTarget.prototype, eventNames[i]);
++++++++ }
++++++++
++++++++ return CustomEventTarget
++++++++}
++++++++
++++++++/**
++++++++ * EventTarget.
++++++++ *
++++++++ * - This is constructor if no arguments.
++++++++ * - This is a function which returns a CustomEventTarget constructor if there are arguments.
++++++++ *
++++++++ * For example:
++++++++ *
++++++++ * class A extends EventTarget {}
++++++++ * class B extends EventTarget("message") {}
++++++++ * class C extends EventTarget("message", "error") {}
++++++++ * class D extends EventTarget(["message", "error"]) {}
++++++++ */
++++++++function EventTarget() {
++++++++ /*eslint-disable consistent-return */
++++++++ if (this instanceof EventTarget) {
++++++++ listenersMap.set(this, new Map());
++++++++ return
++++++++ }
++++++++ if (arguments.length === 1 && Array.isArray(arguments[0])) {
++++++++ return defineCustomEventTarget(arguments[0])
++++++++ }
++++++++ if (arguments.length > 0) {
++++++++ const types = new Array(arguments.length);
++++++++ for (let i = 0; i < arguments.length; ++i) {
++++++++ types[i] = arguments[i];
++++++++ }
++++++++ return defineCustomEventTarget(types)
++++++++ }
++++++++ throw new TypeError("Cannot call a class as a function")
++++++++ /*eslint-enable consistent-return */
++++++++}
++++++++
++++++++// Should be enumerable, but class methods are not enumerable.
++++++++EventTarget.prototype = {
++++++++ /**
++++++++ * Add a given listener to this event target.
++++++++ * @param {string} eventName The event name to add.
++++++++ * @param {Function} listener The listener to add.
++++++++ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
++++++++ * @returns {void}
++++++++ */
++++++++ addEventListener(eventName, listener, options) {
++++++++ if (listener == null) {
++++++++ return
++++++++ }
++++++++ if (typeof listener !== "function" && !isObject(listener)) {
++++++++ throw new TypeError("'listener' should be a function or an object.")
++++++++ }
++++++++
++++++++ const listeners = getListeners(this);
++++++++ const optionsIsObj = isObject(options);
++++++++ const capture = optionsIsObj
++++++++ ? Boolean(options.capture)
++++++++ : Boolean(options);
++++++++ const listenerType = capture ? CAPTURE : BUBBLE;
++++++++ const newNode = {
++++++++ listener,
++++++++ listenerType,
++++++++ passive: optionsIsObj && Boolean(options.passive),
++++++++ once: optionsIsObj && Boolean(options.once),
++++++++ next: null,
++++++++ };
++++++++
++++++++ // Set it as the first node if the first node is null.
++++++++ let node = listeners.get(eventName);
++++++++ if (node === undefined) {
++++++++ listeners.set(eventName, newNode);
++++++++ return
++++++++ }
++++++++
++++++++ // Traverse to the tail while checking duplication..
++++++++ let prev = null;
++++++++ while (node != null) {
++++++++ if (
++++++++ node.listener === listener &&
++++++++ node.listenerType === listenerType
++++++++ ) {
++++++++ // Should ignore duplication.
++++++++ return
++++++++ }
++++++++ prev = node;
++++++++ node = node.next;
++++++++ }
++++++++
++++++++ // Add it.
++++++++ prev.next = newNode;
++++++++ },
++++++++
++++++++ /**
++++++++ * Remove a given listener from this event target.
++++++++ * @param {string} eventName The event name to remove.
++++++++ * @param {Function} listener The listener to remove.
++++++++ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
++++++++ * @returns {void}
++++++++ */
++++++++ removeEventListener(eventName, listener, options) {
++++++++ if (listener == null) {
++++++++ return
++++++++ }
++++++++
++++++++ const listeners = getListeners(this);
++++++++ const capture = isObject(options)
++++++++ ? Boolean(options.capture)
++++++++ : Boolean(options);
++++++++ const listenerType = capture ? CAPTURE : BUBBLE;
++++++++
++++++++ let prev = null;
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (
++++++++ node.listener === listener &&
++++++++ node.listenerType === listenerType
++++++++ ) {
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ return
++++++++ }
++++++++
++++++++ prev = node;
++++++++ node = node.next;
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Dispatch a given event.
++++++++ * @param {Event|{type:string}} event The event to dispatch.
++++++++ * @returns {boolean} `false` if canceled.
++++++++ */
++++++++ dispatchEvent(event) {
++++++++ if (event == null || typeof event.type !== "string") {
++++++++ throw new TypeError('"event.type" should be a string.')
++++++++ }
++++++++
++++++++ // If listeners aren't registered, terminate.
++++++++ const listeners = getListeners(this);
++++++++ const eventName = event.type;
++++++++ let node = listeners.get(eventName);
++++++++ if (node == null) {
++++++++ return true
++++++++ }
++++++++
++++++++ // Since we cannot rewrite several properties, so wrap object.
++++++++ const wrappedEvent = wrapEvent(this, event);
++++++++
++++++++ // This doesn't process capturing phase and bubbling phase.
++++++++ // This isn't participating in a tree.
++++++++ let prev = null;
++++++++ while (node != null) {
++++++++ // Remove this listener if it's once
++++++++ if (node.once) {
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ } else {
++++++++ prev = node;
++++++++ }
++++++++
++++++++ // Call this listener
++++++++ setPassiveListener(
++++++++ wrappedEvent,
++++++++ node.passive ? node.listener : null
++++++++ );
++++++++ if (typeof node.listener === "function") {
++++++++ try {
++++++++ node.listener.call(this, wrappedEvent);
++++++++ } catch (err) {
++++++++ if (
++++++++ typeof console !== "undefined" &&
++++++++ typeof console.error === "function"
++++++++ ) {
++++++++ console.error(err);
++++++++ }
++++++++ }
++++++++ } else if (
++++++++ node.listenerType !== ATTRIBUTE &&
++++++++ typeof node.listener.handleEvent === "function"
++++++++ ) {
++++++++ node.listener.handleEvent(wrappedEvent);
++++++++ }
++++++++
++++++++ // Break if `event.stopImmediatePropagation` was called.
++++++++ if (isStopped(wrappedEvent)) {
++++++++ break
++++++++ }
++++++++
++++++++ node = node.next;
++++++++ }
++++++++ setPassiveListener(wrappedEvent, null);
++++++++ setEventPhase(wrappedEvent, 0);
++++++++ setCurrentTarget(wrappedEvent, null);
++++++++
++++++++ return !wrappedEvent.defaultPrevented
++++++++ },
++++++++};
++++++++
++++++++// `constructor` is not enumerable.
++++++++Object.defineProperty(EventTarget.prototype, "constructor", {
++++++++ value: EventTarget,
++++++++ configurable: true,
++++++++ writable: true,
++++++++});
++++++++
++++++++// Ensure `eventTarget instanceof window.EventTarget` is `true`.
++++++++if (
++++++++ typeof window !== "undefined" &&
++++++++ typeof window.EventTarget !== "undefined"
++++++++) {
++++++++ Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype);
++++++++}
++++++++
++++++++exports.defineEventAttribute = defineEventAttribute;
++++++++exports.EventTarget = EventTarget;
++++++++exports.default = EventTarget;
++++++++
++++++++module.exports = EventTarget
++++++++module.exports.EventTarget = module.exports["default"] = EventTarget
++++++++module.exports.defineEventAttribute = defineEventAttribute
++++++++//# sourceMappingURL=event-target-shim.js.map
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * @author Toru Nagashima <https://github.com/mysticatea>
++++++++ * @copyright 2015 Toru Nagashima. All rights reserved.
++++++++ * See LICENSE file in root directory for full license.
++++++++ */
++++++++/**
++++++++ * @typedef {object} PrivateData
++++++++ * @property {EventTarget} eventTarget The event target.
++++++++ * @property {{type:string}} event The original event object.
++++++++ * @property {number} eventPhase The current event phase.
++++++++ * @property {EventTarget|null} currentTarget The current event target.
++++++++ * @property {boolean} canceled The flag to prevent default.
++++++++ * @property {boolean} stopped The flag to stop propagation.
++++++++ * @property {boolean} immediateStopped The flag to stop propagation immediately.
++++++++ * @property {Function|null} passiveListener The listener if the current listener is passive. Otherwise this is null.
++++++++ * @property {number} timeStamp The unix time.
++++++++ * @private
++++++++ */
++++++++
++++++++/**
++++++++ * Private data for event wrappers.
++++++++ * @type {WeakMap<Event, PrivateData>}
++++++++ * @private
++++++++ */
++++++++const privateData = new WeakMap();
++++++++
++++++++/**
++++++++ * Cache for wrapper classes.
++++++++ * @type {WeakMap<Object, Function>}
++++++++ * @private
++++++++ */
++++++++const wrappers = new WeakMap();
++++++++
++++++++/**
++++++++ * Get private data.
++++++++ * @param {Event} event The event object to get private data.
++++++++ * @returns {PrivateData} The private data of the event.
++++++++ * @private
++++++++ */
++++++++function pd(event) {
++++++++ const retv = privateData.get(event);
++++++++ console.assert(
++++++++ retv != null,
++++++++ "'this' is expected an Event object, but got",
++++++++ event
++++++++ );
++++++++ return retv
++++++++}
++++++++
++++++++/**
++++++++ * https://dom.spec.whatwg.org/#set-the-canceled-flag
++++++++ * @param data {PrivateData} private data.
++++++++ */
++++++++function setCancelFlag(data) {
++++++++ if (data.passiveListener != null) {
++++++++ if (
++++++++ typeof console !== "undefined" &&
++++++++ typeof console.error === "function"
++++++++ ) {
++++++++ console.error(
++++++++ "Unable to preventDefault inside passive event listener invocation.",
++++++++ data.passiveListener
++++++++ );
++++++++ }
++++++++ return
++++++++ }
++++++++ if (!data.event.cancelable) {
++++++++ return
++++++++ }
++++++++
++++++++ data.canceled = true;
++++++++ if (typeof data.event.preventDefault === "function") {
++++++++ data.event.preventDefault();
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * @see https://dom.spec.whatwg.org/#interface-event
++++++++ * @private
++++++++ */
++++++++/**
++++++++ * The event wrapper.
++++++++ * @constructor
++++++++ * @param {EventTarget} eventTarget The event target of this dispatching.
++++++++ * @param {Event|{type:string}} event The original event to wrap.
++++++++ */
++++++++function Event(eventTarget, event) {
++++++++ privateData.set(this, {
++++++++ eventTarget,
++++++++ event,
++++++++ eventPhase: 2,
++++++++ currentTarget: eventTarget,
++++++++ canceled: false,
++++++++ stopped: false,
++++++++ immediateStopped: false,
++++++++ passiveListener: null,
++++++++ timeStamp: event.timeStamp || Date.now(),
++++++++ });
++++++++
++++++++ // https://heycam.github.io/webidl/#Unforgeable
++++++++ Object.defineProperty(this, "isTrusted", { value: false, enumerable: true });
++++++++
++++++++ // Define accessors
++++++++ const keys = Object.keys(event);
++++++++ for (let i = 0; i < keys.length; ++i) {
++++++++ const key = keys[i];
++++++++ if (!(key in this)) {
++++++++ Object.defineProperty(this, key, defineRedirectDescriptor(key));
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++// Should be enumerable, but class methods are not enumerable.
++++++++Event.prototype = {
++++++++ /**
++++++++ * The type of this event.
++++++++ * @type {string}
++++++++ */
++++++++ get type() {
++++++++ return pd(this).event.type
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ */
++++++++ get target() {
++++++++ return pd(this).eventTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ */
++++++++ get currentTarget() {
++++++++ return pd(this).currentTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * @returns {EventTarget[]} The composed path of this event.
++++++++ */
++++++++ composedPath() {
++++++++ const currentTarget = pd(this).currentTarget;
++++++++ if (currentTarget == null) {
++++++++ return []
++++++++ }
++++++++ return [currentTarget]
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of NONE.
++++++++ * @type {number}
++++++++ */
++++++++ get NONE() {
++++++++ return 0
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of CAPTURING_PHASE.
++++++++ * @type {number}
++++++++ */
++++++++ get CAPTURING_PHASE() {
++++++++ return 1
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of AT_TARGET.
++++++++ * @type {number}
++++++++ */
++++++++ get AT_TARGET() {
++++++++ return 2
++++++++ },
++++++++
++++++++ /**
++++++++ * Constant of BUBBLING_PHASE.
++++++++ * @type {number}
++++++++ */
++++++++ get BUBBLING_PHASE() {
++++++++ return 3
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {number}
++++++++ */
++++++++ get eventPhase() {
++++++++ return pd(this).eventPhase
++++++++ },
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ * @returns {void}
++++++++ */
++++++++ stopPropagation() {
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ if (typeof data.event.stopPropagation === "function") {
++++++++ data.event.stopPropagation();
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ * @returns {void}
++++++++ */
++++++++ stopImmediatePropagation() {
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ data.immediateStopped = true;
++++++++ if (typeof data.event.stopImmediatePropagation === "function") {
++++++++ data.event.stopImmediatePropagation();
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be bubbling.
++++++++ * @type {boolean}
++++++++ */
++++++++ get bubbles() {
++++++++ return Boolean(pd(this).event.bubbles)
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be cancelable.
++++++++ * @type {boolean}
++++++++ */
++++++++ get cancelable() {
++++++++ return Boolean(pd(this).event.cancelable)
++++++++ },
++++++++
++++++++ /**
++++++++ * Cancel this event.
++++++++ * @returns {void}
++++++++ */
++++++++ preventDefault() {
++++++++ setCancelFlag(pd(this));
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to indicate cancellation state.
++++++++ * @type {boolean}
++++++++ */
++++++++ get defaultPrevented() {
++++++++ return pd(this).canceled
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to be composed.
++++++++ * @type {boolean}
++++++++ */
++++++++ get composed() {
++++++++ return Boolean(pd(this).event.composed)
++++++++ },
++++++++
++++++++ /**
++++++++ * The unix time of this event.
++++++++ * @type {number}
++++++++ */
++++++++ get timeStamp() {
++++++++ return pd(this).timeStamp
++++++++ },
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @type {EventTarget}
++++++++ * @deprecated
++++++++ */
++++++++ get srcElement() {
++++++++ return pd(this).eventTarget
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to stop event bubbling.
++++++++ * @type {boolean}
++++++++ * @deprecated
++++++++ */
++++++++ get cancelBubble() {
++++++++ return pd(this).stopped
++++++++ },
++++++++ set cancelBubble(value) {
++++++++ if (!value) {
++++++++ return
++++++++ }
++++++++ const data = pd(this);
++++++++
++++++++ data.stopped = true;
++++++++ if (typeof data.event.cancelBubble === "boolean") {
++++++++ data.event.cancelBubble = true;
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * The flag to indicate cancellation state.
++++++++ * @type {boolean}
++++++++ * @deprecated
++++++++ */
++++++++ get returnValue() {
++++++++ return !pd(this).canceled
++++++++ },
++++++++ set returnValue(value) {
++++++++ if (!value) {
++++++++ setCancelFlag(pd(this));
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Initialize this event object. But do nothing under event dispatching.
++++++++ * @param {string} type The event type.
++++++++ * @param {boolean} [bubbles=false] The flag to be possible to bubble up.
++++++++ * @param {boolean} [cancelable=false] The flag to be possible to cancel.
++++++++ * @deprecated
++++++++ */
++++++++ initEvent() {
++++++++ // Do nothing.
++++++++ },
++++++++};
++++++++
++++++++// `constructor` is not enumerable.
++++++++Object.defineProperty(Event.prototype, "constructor", {
++++++++ value: Event,
++++++++ configurable: true,
++++++++ writable: true,
++++++++});
++++++++
++++++++// Ensure `event instanceof window.Event` is `true`.
++++++++if (typeof window !== "undefined" && typeof window.Event !== "undefined") {
++++++++ Object.setPrototypeOf(Event.prototype, window.Event.prototype);
++++++++
++++++++ // Make association for wrappers.
++++++++ wrappers.set(window.Event.prototype, Event);
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor to redirect a given property.
++++++++ * @param {string} key Property name to define property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor to redirect the property.
++++++++ * @private
++++++++ */
++++++++function defineRedirectDescriptor(key) {
++++++++ return {
++++++++ get() {
++++++++ return pd(this).event[key]
++++++++ },
++++++++ set(value) {
++++++++ pd(this).event[key] = value;
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor to call a given method property.
++++++++ * @param {string} key Property name to define property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor to call the method property.
++++++++ * @private
++++++++ */
++++++++function defineCallDescriptor(key) {
++++++++ return {
++++++++ value() {
++++++++ const event = pd(this).event;
++++++++ return event[key].apply(event, arguments)
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Define new wrapper class.
++++++++ * @param {Function} BaseEvent The base wrapper class.
++++++++ * @param {Object} proto The prototype of the original event.
++++++++ * @returns {Function} The defined wrapper class.
++++++++ * @private
++++++++ */
++++++++function defineWrapper(BaseEvent, proto) {
++++++++ const keys = Object.keys(proto);
++++++++ if (keys.length === 0) {
++++++++ return BaseEvent
++++++++ }
++++++++
++++++++ /** CustomEvent */
++++++++ function CustomEvent(eventTarget, event) {
++++++++ BaseEvent.call(this, eventTarget, event);
++++++++ }
++++++++
++++++++ CustomEvent.prototype = Object.create(BaseEvent.prototype, {
++++++++ constructor: { value: CustomEvent, configurable: true, writable: true },
++++++++ });
++++++++
++++++++ // Define accessors.
++++++++ for (let i = 0; i < keys.length; ++i) {
++++++++ const key = keys[i];
++++++++ if (!(key in BaseEvent.prototype)) {
++++++++ const descriptor = Object.getOwnPropertyDescriptor(proto, key);
++++++++ const isFunc = typeof descriptor.value === "function";
++++++++ Object.defineProperty(
++++++++ CustomEvent.prototype,
++++++++ key,
++++++++ isFunc
++++++++ ? defineCallDescriptor(key)
++++++++ : defineRedirectDescriptor(key)
++++++++ );
++++++++ }
++++++++ }
++++++++
++++++++ return CustomEvent
++++++++}
++++++++
++++++++/**
++++++++ * Get the wrapper class of a given prototype.
++++++++ * @param {Object} proto The prototype of the original event to get its wrapper.
++++++++ * @returns {Function} The wrapper class.
++++++++ * @private
++++++++ */
++++++++function getWrapper(proto) {
++++++++ if (proto == null || proto === Object.prototype) {
++++++++ return Event
++++++++ }
++++++++
++++++++ let wrapper = wrappers.get(proto);
++++++++ if (wrapper == null) {
++++++++ wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto);
++++++++ wrappers.set(proto, wrapper);
++++++++ }
++++++++ return wrapper
++++++++}
++++++++
++++++++/**
++++++++ * Wrap a given event to management a dispatching.
++++++++ * @param {EventTarget} eventTarget The event target of this dispatching.
++++++++ * @param {Object} event The event to wrap.
++++++++ * @returns {Event} The wrapper instance.
++++++++ * @private
++++++++ */
++++++++function wrapEvent(eventTarget, event) {
++++++++ const Wrapper = getWrapper(Object.getPrototypeOf(event));
++++++++ return new Wrapper(eventTarget, event)
++++++++}
++++++++
++++++++/**
++++++++ * Get the immediateStopped flag of a given event.
++++++++ * @param {Event} event The event to get.
++++++++ * @returns {boolean} The flag to stop propagation immediately.
++++++++ * @private
++++++++ */
++++++++function isStopped(event) {
++++++++ return pd(event).immediateStopped
++++++++}
++++++++
++++++++/**
++++++++ * Set the current event phase of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {number} eventPhase New event phase.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setEventPhase(event, eventPhase) {
++++++++ pd(event).eventPhase = eventPhase;
++++++++}
++++++++
++++++++/**
++++++++ * Set the current target of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {EventTarget|null} currentTarget New current target.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setCurrentTarget(event, currentTarget) {
++++++++ pd(event).currentTarget = currentTarget;
++++++++}
++++++++
++++++++/**
++++++++ * Set a passive listener of a given event.
++++++++ * @param {Event} event The event to set current target.
++++++++ * @param {Function|null} passiveListener New passive listener.
++++++++ * @returns {void}
++++++++ * @private
++++++++ */
++++++++function setPassiveListener(event, passiveListener) {
++++++++ pd(event).passiveListener = passiveListener;
++++++++}
++++++++
++++++++/**
++++++++ * @typedef {object} ListenerNode
++++++++ * @property {Function} listener
++++++++ * @property {1|2|3} listenerType
++++++++ * @property {boolean} passive
++++++++ * @property {boolean} once
++++++++ * @property {ListenerNode|null} next
++++++++ * @private
++++++++ */
++++++++
++++++++/**
++++++++ * @type {WeakMap<object, Map<string, ListenerNode>>}
++++++++ * @private
++++++++ */
++++++++const listenersMap = new WeakMap();
++++++++
++++++++// Listener types
++++++++const CAPTURE = 1;
++++++++const BUBBLE = 2;
++++++++const ATTRIBUTE = 3;
++++++++
++++++++/**
++++++++ * Check whether a given value is an object or not.
++++++++ * @param {any} x The value to check.
++++++++ * @returns {boolean} `true` if the value is an object.
++++++++ */
++++++++function isObject(x) {
++++++++ return x !== null && typeof x === "object" //eslint-disable-line no-restricted-syntax
++++++++}
++++++++
++++++++/**
++++++++ * Get listeners.
++++++++ * @param {EventTarget} eventTarget The event target to get.
++++++++ * @returns {Map<string, ListenerNode>} The listeners.
++++++++ * @private
++++++++ */
++++++++function getListeners(eventTarget) {
++++++++ const listeners = listenersMap.get(eventTarget);
++++++++ if (listeners == null) {
++++++++ throw new TypeError(
++++++++ "'this' is expected an EventTarget object, but got another value."
++++++++ )
++++++++ }
++++++++ return listeners
++++++++}
++++++++
++++++++/**
++++++++ * Get the property descriptor for the event attribute of a given event.
++++++++ * @param {string} eventName The event name to get property descriptor.
++++++++ * @returns {PropertyDescriptor} The property descriptor.
++++++++ * @private
++++++++ */
++++++++function defineEventAttributeDescriptor(eventName) {
++++++++ return {
++++++++ get() {
++++++++ const listeners = getListeners(this);
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (node.listenerType === ATTRIBUTE) {
++++++++ return node.listener
++++++++ }
++++++++ node = node.next;
++++++++ }
++++++++ return null
++++++++ },
++++++++
++++++++ set(listener) {
++++++++ if (typeof listener !== "function" && !isObject(listener)) {
++++++++ listener = null; // eslint-disable-line no-param-reassign
++++++++ }
++++++++ const listeners = getListeners(this);
++++++++
++++++++ // Traverse to the tail while removing old value.
++++++++ let prev = null;
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (node.listenerType === ATTRIBUTE) {
++++++++ // Remove old value.
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ } else {
++++++++ prev = node;
++++++++ }
++++++++
++++++++ node = node.next;
++++++++ }
++++++++
++++++++ // Add new value.
++++++++ if (listener !== null) {
++++++++ const newNode = {
++++++++ listener,
++++++++ listenerType: ATTRIBUTE,
++++++++ passive: false,
++++++++ once: false,
++++++++ next: null,
++++++++ };
++++++++ if (prev === null) {
++++++++ listeners.set(eventName, newNode);
++++++++ } else {
++++++++ prev.next = newNode;
++++++++ }
++++++++ }
++++++++ },
++++++++ configurable: true,
++++++++ enumerable: true,
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Define an event attribute (e.g. `eventTarget.onclick`).
++++++++ * @param {Object} eventTargetPrototype The event target prototype to define an event attrbite.
++++++++ * @param {string} eventName The event name to define.
++++++++ * @returns {void}
++++++++ */
++++++++function defineEventAttribute(eventTargetPrototype, eventName) {
++++++++ Object.defineProperty(
++++++++ eventTargetPrototype,
++++++++ `on${eventName}`,
++++++++ defineEventAttributeDescriptor(eventName)
++++++++ );
++++++++}
++++++++
++++++++/**
++++++++ * Define a custom EventTarget with event attributes.
++++++++ * @param {string[]} eventNames Event names for event attributes.
++++++++ * @returns {EventTarget} The custom EventTarget.
++++++++ * @private
++++++++ */
++++++++function defineCustomEventTarget(eventNames) {
++++++++ /** CustomEventTarget */
++++++++ function CustomEventTarget() {
++++++++ EventTarget.call(this);
++++++++ }
++++++++
++++++++ CustomEventTarget.prototype = Object.create(EventTarget.prototype, {
++++++++ constructor: {
++++++++ value: CustomEventTarget,
++++++++ configurable: true,
++++++++ writable: true,
++++++++ },
++++++++ });
++++++++
++++++++ for (let i = 0; i < eventNames.length; ++i) {
++++++++ defineEventAttribute(CustomEventTarget.prototype, eventNames[i]);
++++++++ }
++++++++
++++++++ return CustomEventTarget
++++++++}
++++++++
++++++++/**
++++++++ * EventTarget.
++++++++ *
++++++++ * - This is constructor if no arguments.
++++++++ * - This is a function which returns a CustomEventTarget constructor if there are arguments.
++++++++ *
++++++++ * For example:
++++++++ *
++++++++ * class A extends EventTarget {}
++++++++ * class B extends EventTarget("message") {}
++++++++ * class C extends EventTarget("message", "error") {}
++++++++ * class D extends EventTarget(["message", "error"]) {}
++++++++ */
++++++++function EventTarget() {
++++++++ /*eslint-disable consistent-return */
++++++++ if (this instanceof EventTarget) {
++++++++ listenersMap.set(this, new Map());
++++++++ return
++++++++ }
++++++++ if (arguments.length === 1 && Array.isArray(arguments[0])) {
++++++++ return defineCustomEventTarget(arguments[0])
++++++++ }
++++++++ if (arguments.length > 0) {
++++++++ const types = new Array(arguments.length);
++++++++ for (let i = 0; i < arguments.length; ++i) {
++++++++ types[i] = arguments[i];
++++++++ }
++++++++ return defineCustomEventTarget(types)
++++++++ }
++++++++ throw new TypeError("Cannot call a class as a function")
++++++++ /*eslint-enable consistent-return */
++++++++}
++++++++
++++++++// Should be enumerable, but class methods are not enumerable.
++++++++EventTarget.prototype = {
++++++++ /**
++++++++ * Add a given listener to this event target.
++++++++ * @param {string} eventName The event name to add.
++++++++ * @param {Function} listener The listener to add.
++++++++ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
++++++++ * @returns {void}
++++++++ */
++++++++ addEventListener(eventName, listener, options) {
++++++++ if (listener == null) {
++++++++ return
++++++++ }
++++++++ if (typeof listener !== "function" && !isObject(listener)) {
++++++++ throw new TypeError("'listener' should be a function or an object.")
++++++++ }
++++++++
++++++++ const listeners = getListeners(this);
++++++++ const optionsIsObj = isObject(options);
++++++++ const capture = optionsIsObj
++++++++ ? Boolean(options.capture)
++++++++ : Boolean(options);
++++++++ const listenerType = capture ? CAPTURE : BUBBLE;
++++++++ const newNode = {
++++++++ listener,
++++++++ listenerType,
++++++++ passive: optionsIsObj && Boolean(options.passive),
++++++++ once: optionsIsObj && Boolean(options.once),
++++++++ next: null,
++++++++ };
++++++++
++++++++ // Set it as the first node if the first node is null.
++++++++ let node = listeners.get(eventName);
++++++++ if (node === undefined) {
++++++++ listeners.set(eventName, newNode);
++++++++ return
++++++++ }
++++++++
++++++++ // Traverse to the tail while checking duplication..
++++++++ let prev = null;
++++++++ while (node != null) {
++++++++ if (
++++++++ node.listener === listener &&
++++++++ node.listenerType === listenerType
++++++++ ) {
++++++++ // Should ignore duplication.
++++++++ return
++++++++ }
++++++++ prev = node;
++++++++ node = node.next;
++++++++ }
++++++++
++++++++ // Add it.
++++++++ prev.next = newNode;
++++++++ },
++++++++
++++++++ /**
++++++++ * Remove a given listener from this event target.
++++++++ * @param {string} eventName The event name to remove.
++++++++ * @param {Function} listener The listener to remove.
++++++++ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
++++++++ * @returns {void}
++++++++ */
++++++++ removeEventListener(eventName, listener, options) {
++++++++ if (listener == null) {
++++++++ return
++++++++ }
++++++++
++++++++ const listeners = getListeners(this);
++++++++ const capture = isObject(options)
++++++++ ? Boolean(options.capture)
++++++++ : Boolean(options);
++++++++ const listenerType = capture ? CAPTURE : BUBBLE;
++++++++
++++++++ let prev = null;
++++++++ let node = listeners.get(eventName);
++++++++ while (node != null) {
++++++++ if (
++++++++ node.listener === listener &&
++++++++ node.listenerType === listenerType
++++++++ ) {
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ return
++++++++ }
++++++++
++++++++ prev = node;
++++++++ node = node.next;
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Dispatch a given event.
++++++++ * @param {Event|{type:string}} event The event to dispatch.
++++++++ * @returns {boolean} `false` if canceled.
++++++++ */
++++++++ dispatchEvent(event) {
++++++++ if (event == null || typeof event.type !== "string") {
++++++++ throw new TypeError('"event.type" should be a string.')
++++++++ }
++++++++
++++++++ // If listeners aren't registered, terminate.
++++++++ const listeners = getListeners(this);
++++++++ const eventName = event.type;
++++++++ let node = listeners.get(eventName);
++++++++ if (node == null) {
++++++++ return true
++++++++ }
++++++++
++++++++ // Since we cannot rewrite several properties, so wrap object.
++++++++ const wrappedEvent = wrapEvent(this, event);
++++++++
++++++++ // This doesn't process capturing phase and bubbling phase.
++++++++ // This isn't participating in a tree.
++++++++ let prev = null;
++++++++ while (node != null) {
++++++++ // Remove this listener if it's once
++++++++ if (node.once) {
++++++++ if (prev !== null) {
++++++++ prev.next = node.next;
++++++++ } else if (node.next !== null) {
++++++++ listeners.set(eventName, node.next);
++++++++ } else {
++++++++ listeners.delete(eventName);
++++++++ }
++++++++ } else {
++++++++ prev = node;
++++++++ }
++++++++
++++++++ // Call this listener
++++++++ setPassiveListener(
++++++++ wrappedEvent,
++++++++ node.passive ? node.listener : null
++++++++ );
++++++++ if (typeof node.listener === "function") {
++++++++ try {
++++++++ node.listener.call(this, wrappedEvent);
++++++++ } catch (err) {
++++++++ if (
++++++++ typeof console !== "undefined" &&
++++++++ typeof console.error === "function"
++++++++ ) {
++++++++ console.error(err);
++++++++ }
++++++++ }
++++++++ } else if (
++++++++ node.listenerType !== ATTRIBUTE &&
++++++++ typeof node.listener.handleEvent === "function"
++++++++ ) {
++++++++ node.listener.handleEvent(wrappedEvent);
++++++++ }
++++++++
++++++++ // Break if `event.stopImmediatePropagation` was called.
++++++++ if (isStopped(wrappedEvent)) {
++++++++ break
++++++++ }
++++++++
++++++++ node = node.next;
++++++++ }
++++++++ setPassiveListener(wrappedEvent, null);
++++++++ setEventPhase(wrappedEvent, 0);
++++++++ setCurrentTarget(wrappedEvent, null);
++++++++
++++++++ return !wrappedEvent.defaultPrevented
++++++++ },
++++++++};
++++++++
++++++++// `constructor` is not enumerable.
++++++++Object.defineProperty(EventTarget.prototype, "constructor", {
++++++++ value: EventTarget,
++++++++ configurable: true,
++++++++ writable: true,
++++++++});
++++++++
++++++++// Ensure `eventTarget instanceof window.EventTarget` is `true`.
++++++++if (
++++++++ typeof window !== "undefined" &&
++++++++ typeof window.EventTarget !== "undefined"
++++++++) {
++++++++ Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype);
++++++++}
++++++++
++++++++export default EventTarget;
++++++++export { defineEventAttribute, EventTarget };
++++++++//# sourceMappingURL=event-target-shim.mjs.map
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export as namespace EventTargetShim
++++++++
++++++++/**
++++++++ * `Event` interface.
++++++++ * @see https://dom.spec.whatwg.org/#event
++++++++ */
++++++++export interface Event {
++++++++ /**
++++++++ * The type of this event.
++++++++ */
++++++++ readonly type: string
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ */
++++++++ readonly target: EventTarget<{}, {}, "standard"> | null
++++++++
++++++++ /**
++++++++ * The current target of this event.
++++++++ */
++++++++ readonly currentTarget: EventTarget<{}, {}, "standard"> | null
++++++++
++++++++ /**
++++++++ * The target of this event.
++++++++ * @deprecated
++++++++ */
++++++++ readonly srcElement: any | null
++++++++
++++++++ /**
++++++++ * The composed path of this event.
++++++++ */
++++++++ composedPath(): EventTarget<{}, {}, "standard">[]
++++++++
++++++++ /**
++++++++ * Constant of NONE.
++++++++ */
++++++++ readonly NONE: number
++++++++
++++++++ /**
++++++++ * Constant of CAPTURING_PHASE.
++++++++ */
++++++++ readonly CAPTURING_PHASE: number
++++++++
++++++++ /**
++++++++ * Constant of BUBBLING_PHASE.
++++++++ */
++++++++ readonly BUBBLING_PHASE: number
++++++++
++++++++ /**
++++++++ * Constant of AT_TARGET.
++++++++ */
++++++++ readonly AT_TARGET: number
++++++++
++++++++ /**
++++++++ * Indicates which phase of the event flow is currently being evaluated.
++++++++ */
++++++++ readonly eventPhase: number
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ */
++++++++ stopPropagation(): void
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ */
++++++++ stopImmediatePropagation(): void
++++++++
++++++++ /**
++++++++ * Initialize event.
++++++++ * @deprecated
++++++++ */
++++++++ initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void
++++++++
++++++++ /**
++++++++ * The flag indicating bubbling.
++++++++ */
++++++++ readonly bubbles: boolean
++++++++
++++++++ /**
++++++++ * Stop event bubbling.
++++++++ * @deprecated
++++++++ */
++++++++ cancelBubble: boolean
++++++++
++++++++ /**
++++++++ * Set or get cancellation flag.
++++++++ * @deprecated
++++++++ */
++++++++ returnValue: boolean
++++++++
++++++++ /**
++++++++ * The flag indicating whether the event can be canceled.
++++++++ */
++++++++ readonly cancelable: boolean
++++++++
++++++++ /**
++++++++ * Cancel this event.
++++++++ */
++++++++ preventDefault(): void
++++++++
++++++++ /**
++++++++ * The flag to indicating whether the event was canceled.
++++++++ */
++++++++ readonly defaultPrevented: boolean
++++++++
++++++++ /**
++++++++ * The flag to indicating if event is composed.
++++++++ */
++++++++ readonly composed: boolean
++++++++
++++++++ /**
++++++++ * Indicates whether the event was dispatched by the user agent.
++++++++ */
++++++++ readonly isTrusted: boolean
++++++++
++++++++ /**
++++++++ * The unix time of this event.
++++++++ */
++++++++ readonly timeStamp: number
++++++++}
++++++++
++++++++/**
++++++++ * The constructor of `EventTarget` interface.
++++++++ */
++++++++export type EventTargetConstructor<
++++++++ TEvents extends EventTarget.EventDefinition = {},
++++++++ TEventAttributes extends EventTarget.EventDefinition = {},
++++++++ TMode extends EventTarget.Mode = "loose"
++++++++> = {
++++++++ prototype: EventTarget<TEvents, TEventAttributes, TMode>
++++++++ new(): EventTarget<TEvents, TEventAttributes, TMode>
++++++++}
++++++++
++++++++/**
++++++++ * `EventTarget` interface.
++++++++ * @see https://dom.spec.whatwg.org/#interface-eventtarget
++++++++ */
++++++++export type EventTarget<
++++++++ TEvents extends EventTarget.EventDefinition = {},
++++++++ TEventAttributes extends EventTarget.EventDefinition = {},
++++++++ TMode extends EventTarget.Mode = "loose"
++++++++> = EventTarget.EventAttributes<TEventAttributes> & {
++++++++ /**
++++++++ * Add a given listener to this event target.
++++++++ * @param eventName The event name to add.
++++++++ * @param listener The listener to add.
++++++++ * @param options The options for this listener.
++++++++ */
++++++++ addEventListener<TEventType extends EventTarget.EventType<TEvents, TMode>>(
++++++++ type: TEventType,
++++++++ listener:
++++++++ | EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>>
++++++++ | null,
++++++++ options?: boolean | EventTarget.AddOptions
++++++++ ): void
++++++++
++++++++ /**
++++++++ * Remove a given listener from this event target.
++++++++ * @param eventName The event name to remove.
++++++++ * @param listener The listener to remove.
++++++++ * @param options The options for this listener.
++++++++ */
++++++++ removeEventListener<TEventType extends EventTarget.EventType<TEvents, TMode>>(
++++++++ type: TEventType,
++++++++ listener:
++++++++ | EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>>
++++++++ | null,
++++++++ options?: boolean | EventTarget.RemoveOptions
++++++++ ): void
++++++++
++++++++ /**
++++++++ * Dispatch a given event.
++++++++ * @param event The event to dispatch.
++++++++ * @returns `false` if canceled.
++++++++ */
++++++++ dispatchEvent<TEventType extends EventTarget.EventType<TEvents, TMode>>(
++++++++ event: EventTarget.EventData<TEvents, TEventType, TMode>
++++++++ ): boolean
++++++++}
++++++++
++++++++export const EventTarget: EventTargetConstructor & {
++++++++ /**
++++++++ * Create an `EventTarget` instance with detailed event definition.
++++++++ *
++++++++ * The detailed event definition requires to use `defineEventAttribute()`
++++++++ * function later.
++++++++ *
++++++++ * Unfortunately, the second type parameter `TEventAttributes` was needed
++++++++ * because we cannot compute string literal types.
++++++++ *
++++++++ * @example
++++++++ * const signal = new EventTarget<{ abort: Event }, { onabort: Event }>()
++++++++ * defineEventAttribute(signal, "abort")
++++++++ */
++++++++ new <
++++++++ TEvents extends EventTarget.EventDefinition,
++++++++ TEventAttributes extends EventTarget.EventDefinition,
++++++++ TMode extends EventTarget.Mode = "loose"
++++++++ >(): EventTarget<TEvents, TEventAttributes, TMode>
++++++++
++++++++ /**
++++++++ * Define an `EventTarget` constructor with attribute events and detailed event definition.
++++++++ *
++++++++ * Unfortunately, the second type parameter `TEventAttributes` was needed
++++++++ * because we cannot compute string literal types.
++++++++ *
++++++++ * @example
++++++++ * class AbortSignal extends EventTarget<{ abort: Event }, { onabort: Event }>("abort") {
++++++++ * abort(): void {}
++++++++ * }
++++++++ *
++++++++ * @param events Optional event attributes (e.g. passing in `"click"` adds `onclick` to prototype).
++++++++ */
++++++++ <
++++++++ TEvents extends EventTarget.EventDefinition = {},
++++++++ TEventAttributes extends EventTarget.EventDefinition = {},
++++++++ TMode extends EventTarget.Mode = "loose"
++++++++ >(events: string[]): EventTargetConstructor<
++++++++ TEvents,
++++++++ TEventAttributes,
++++++++ TMode
++++++++ >
++++++++
++++++++ /**
++++++++ * Define an `EventTarget` constructor with attribute events and detailed event definition.
++++++++ *
++++++++ * Unfortunately, the second type parameter `TEventAttributes` was needed
++++++++ * because we cannot compute string literal types.
++++++++ *
++++++++ * @example
++++++++ * class AbortSignal extends EventTarget<{ abort: Event }, { onabort: Event }>("abort") {
++++++++ * abort(): void {}
++++++++ * }
++++++++ *
++++++++ * @param events Optional event attributes (e.g. passing in `"click"` adds `onclick` to prototype).
++++++++ */
++++++++ <
++++++++ TEvents extends EventTarget.EventDefinition = {},
++++++++ TEventAttributes extends EventTarget.EventDefinition = {},
++++++++ TMode extends EventTarget.Mode = "loose"
++++++++ >(event0: string, ...events: string[]): EventTargetConstructor<
++++++++ TEvents,
++++++++ TEventAttributes,
++++++++ TMode
++++++++ >
++++++++}
++++++++
++++++++export namespace EventTarget {
++++++++ /**
++++++++ * Options of `removeEventListener()` method.
++++++++ */
++++++++ export interface RemoveOptions {
++++++++ /**
++++++++ * The flag to indicate that the listener is for the capturing phase.
++++++++ */
++++++++ capture?: boolean
++++++++ }
++++++++
++++++++ /**
++++++++ * Options of `addEventListener()` method.
++++++++ */
++++++++ export interface AddOptions extends RemoveOptions {
++++++++ /**
++++++++ * The flag to indicate that the listener doesn't support
++++++++ * `event.preventDefault()` operation.
++++++++ */
++++++++ passive?: boolean
++++++++ /**
++++++++ * The flag to indicate that the listener will be removed on the first
++++++++ * event.
++++++++ */
++++++++ once?: boolean
++++++++ }
++++++++
++++++++ /**
++++++++ * The type of regular listeners.
++++++++ */
++++++++ export interface FunctionListener<TEvent> {
++++++++ (event: TEvent): void
++++++++ }
++++++++
++++++++ /**
++++++++ * The type of object listeners.
++++++++ */
++++++++ export interface ObjectListener<TEvent> {
++++++++ handleEvent(event: TEvent): void
++++++++ }
++++++++
++++++++ /**
++++++++ * The type of listeners.
++++++++ */
++++++++ export type Listener<TEvent> =
++++++++ | FunctionListener<TEvent>
++++++++ | ObjectListener<TEvent>
++++++++
++++++++ /**
++++++++ * Event definition.
++++++++ */
++++++++ export type EventDefinition = {
++++++++ readonly [key: string]: Event
++++++++ }
++++++++
++++++++ /**
++++++++ * Mapped type for event attributes.
++++++++ */
++++++++ export type EventAttributes<TEventAttributes extends EventDefinition> = {
++++++++ [P in keyof TEventAttributes]:
++++++++ | FunctionListener<TEventAttributes[P]>
++++++++ | null
++++++++ }
++++++++
++++++++ /**
++++++++ * The type of event data for `dispatchEvent()` method.
++++++++ */
++++++++ export type EventData<
++++++++ TEvents extends EventDefinition,
++++++++ TEventType extends keyof TEvents | string,
++++++++ TMode extends Mode
++++++++ > =
++++++++ TEventType extends keyof TEvents
++++++++ ? (
++++++++ // Require properties which are not generated automatically.
++++++++ & Pick<
++++++++ TEvents[TEventType],
++++++++ Exclude<keyof TEvents[TEventType], OmittableEventKeys>
++++++++ >
++++++++ // Properties which are generated automatically are optional.
++++++++ & Partial<Pick<Event, OmittableEventKeys>>
++++++++ )
++++++++ : (
++++++++ TMode extends "standard"
++++++++ ? Event
++++++++ : Event | NonStandardEvent
++++++++ )
++++++++
++++++++ /**
++++++++ * The string literal types of the properties which are generated
++++++++ * automatically in `dispatchEvent()` method.
++++++++ */
++++++++ export type OmittableEventKeys = Exclude<keyof Event, "type">
++++++++
++++++++ /**
++++++++ * The type of event data.
++++++++ */
++++++++ export type NonStandardEvent = {
++++++++ [key: string]: any
++++++++ type: string
++++++++ }
++++++++
++++++++ /**
++++++++ * The type of listeners.
++++++++ */
++++++++ export type PickEvent<
++++++++ TEvents extends EventDefinition,
++++++++ TEventType extends keyof TEvents | string,
++++++++ > =
++++++++ TEventType extends keyof TEvents
++++++++ ? TEvents[TEventType]
++++++++ : Event
++++++++
++++++++ /**
++++++++ * Event type candidates.
++++++++ */
++++++++ export type EventType<
++++++++ TEvents extends EventDefinition,
++++++++ TMode extends Mode
++++++++ > =
++++++++ TMode extends "strict"
++++++++ ? keyof TEvents
++++++++ : keyof TEvents | string
++++++++
++++++++ /**
++++++++ * - `"strict"` ..... Methods don't accept unknown events.
++++++++ * `dispatchEvent()` accepts partial objects.
++++++++ * - `"loose"` ...... Methods accept unknown events.
++++++++ * `dispatchEvent()` accepts partial objects.
++++++++ * - `"standard"` ... Methods accept unknown events.
++++++++ * `dispatchEvent()` doesn't accept partial objects.
++++++++ */
++++++++ export type Mode = "strict" | "standard" | "loose"
++++++++}
++++++++
++++++++/**
++++++++ * Specialized `type` property.
++++++++ */
++++++++export type Type<T extends string> = { type: T }
++++++++
++++++++/**
++++++++ * Define an event attribute (e.g. `eventTarget.onclick`).
++++++++ * @param prototype The event target prototype to define an event attribute.
++++++++ * @param eventName The event name to define.
++++++++ */
++++++++export function defineEventAttribute(
++++++++ prototype: EventTarget,
++++++++ eventName: string
++++++++): void
++++++++
++++++++export default EventTarget
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "event-target-shim",
++++++++ "version": "5.0.1",
++++++++ "description": "An implementation of WHATWG EventTarget interface.",
++++++++ "main": "dist/event-target-shim",
++++++++ "types": "index.d.ts",
++++++++ "files": [
++++++++ "dist",
++++++++ "index.d.ts"
++++++++ ],
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ },
++++++++ "scripts": {
++++++++ "preversion": "npm test",
++++++++ "version": "npm run build && git add dist/*",
++++++++ "postversion": "git push && git push --tags",
++++++++ "clean": "rimraf .nyc_output coverage",
++++++++ "coverage": "nyc report --reporter lcov && opener coverage/lcov-report/index.html",
++++++++ "lint": "eslint src test scripts --ext .js,.mjs",
++++++++ "build": "rollup -c scripts/rollup.config.js",
++++++++ "pretest": "npm run lint",
++++++++ "test": "run-s test:*",
++++++++ "test:mocha": "nyc --require ./scripts/babel-register mocha test/*.mjs",
++++++++ "test:karma": "karma start scripts/karma.conf.js --single-run",
++++++++ "watch": "run-p watch:*",
++++++++ "watch:mocha": "mocha test/*.mjs --require ./scripts/babel-register --watch --watch-extensions js,mjs --growl",
++++++++ "watch:karma": "karma start scripts/karma.conf.js --watch",
++++++++ "codecov": "codecov"
++++++++ },
++++++++ "devDependencies": {
++++++++ "@babel/core": "^7.2.2",
++++++++ "@babel/plugin-transform-modules-commonjs": "^7.2.0",
++++++++ "@babel/preset-env": "^7.2.3",
++++++++ "@babel/register": "^7.0.0",
++++++++ "@mysticatea/eslint-plugin": "^8.0.1",
++++++++ "@mysticatea/spy": "^0.1.2",
++++++++ "assert": "^1.4.1",
++++++++ "codecov": "^3.1.0",
++++++++ "eslint": "^5.12.1",
++++++++ "karma": "^3.1.4",
++++++++ "karma-chrome-launcher": "^2.2.0",
++++++++ "karma-coverage": "^1.1.2",
++++++++ "karma-firefox-launcher": "^1.0.0",
++++++++ "karma-growl-reporter": "^1.0.0",
++++++++ "karma-ie-launcher": "^1.0.0",
++++++++ "karma-mocha": "^1.3.0",
++++++++ "karma-rollup-preprocessor": "^7.0.0-rc.2",
++++++++ "mocha": "^5.2.0",
++++++++ "npm-run-all": "^4.1.5",
++++++++ "nyc": "^13.1.0",
++++++++ "opener": "^1.5.1",
++++++++ "rimraf": "^2.6.3",
++++++++ "rollup": "^1.1.1",
++++++++ "rollup-plugin-babel": "^4.3.2",
++++++++ "rollup-plugin-babel-minify": "^7.0.0",
++++++++ "rollup-plugin-commonjs": "^9.2.0",
++++++++ "rollup-plugin-json": "^3.1.0",
++++++++ "rollup-plugin-node-resolve": "^4.0.0",
++++++++ "rollup-watch": "^4.3.1",
++++++++ "type-tester": "^1.0.0",
++++++++ "typescript": "^3.2.4"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "https://github.com/mysticatea/event-target-shim.git"
++++++++ },
++++++++ "keywords": [
++++++++ "w3c",
++++++++ "whatwg",
++++++++ "eventtarget",
++++++++ "event",
++++++++ "events",
++++++++ "shim"
++++++++ ],
++++++++ "author": "Toru Nagashima",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/mysticatea/event-target-shim/issues"
++++++++ },
++++++++ "homepage": "https://github.com/mysticatea/event-target-shim"
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++The MIT License (MIT)
++++++++
++++++++Copyright (c) 2016-2017 Thomas Watson Steen
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to deal
++++++++in the Software without restriction, including without limitation the rights
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# https-pem
++++++++
++++++++Self-signed PEM key and certificate ready for use in your HTTPS server.
++++++++
++++++++A dead simple way to get an HTTPS server running in development with no
++++++++need to generate the self signed PEM key and certificate.
++++++++
++++++++[](https://travis-ci.org/watson/https-pem)
++++++++[](https://github.com/feross/standard)
++++++++
++++++++## Installation
++++++++
++++++++```
++++++++npm install https-pem
++++++++```
++++++++
++++++++**Warning:** Upon installation a private key and a self signed
++++++++certificate will be generated inside `./node_modules/https-pem`. The
++++++++certificate is valid for 365 days and no attempt have been made to make
++++++++this secure in any way. I suggest only using this for testing and
++++++++development where you just need an easy and quick way to run an HTTPS
++++++++server with Node.js.
++++++++
++++++++## Example Usage
++++++++
++++++++```js
++++++++var https = require('https')
++++++++var pem = require('https-pem')
++++++++
++++++++var server = https.createServer(pem, function (req, res) {
++++++++ res.end('This is servered over HTTPS')
++++++++})
++++++++
++++++++server.listen(443, function () {
++++++++ console.log('The server is running on https://localhost')
++++++++})
++++++++```
++++++++
++++++++### Connecting
++++++++
++++++++When connecting to an HTTPS server from Node.js that uses a self-signed
++++++++certificate, `https.request` will normally emit an `error` and refuse to
++++++++complete the reuqest. To get around that simply set the
++++++++`rejectUnauthorized` option to `false`:
++++++++
++++++++```js
++++++++var opts = { rejectUnauthorized: false }
++++++++
++++++++var req = https.request(opts, function (res) {
++++++++ // ...
++++++++})
++++++++
++++++++req.end()
++++++++```
++++++++
++++++++If using `curl` to connect to a Node.js HTTPS server using a
++++++++self-signed certificate, use the `-k` option:
++++++++
++++++++```
++++++++curl -k https://localhost:4443
++++++++```
++++++++
++++++++## API
++++++++
++++++++The `https-pem` module simply exposes an object with two properties:
++++++++`key` and `cert`.
++++++++
++++++++### `pem.key`
++++++++
++++++++The private key (RSA).
++++++++
++++++++### `pem.cert`
++++++++
++++++++The certificate.
++++++++
++++++++## License
++++++++
++++++++MIT
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++-----BEGIN CERTIFICATE-----\r
++++++++MIICyDCCAbCgAwIBAgIJZmmvi99kgORkMA0GCSqGSIb3DQEBBQUAMAAwHhcNMjMw\r
++++++++NzA5MDkyMTQwWhcNMjQwNzA4MDkyMTQwWjAAMIIBIjANBgkqhkiG9w0BAQEFAAOC\r
++++++++AQ8AMIIBCgKCAQEAnqIdKW51MDpwprg/wKKAvcCZAzcNtszWy0hQtWApYLSF5UZR\r
++++++++UyoLDnf24Pnm9D/JDSSG5YWdQW+/K0ehSS7R02huO1uFMQUo1Pyys1LaxhkiMn/V\r
++++++++WCVpQ8m0i/ydfDrmvcj2wmllTHhlUjup22g1qeoDV09bZ4A+2BgD6JYMRE5DPcpt\r
++++++++Q+rYq1Bu8XxYSD4ytiW0z+kC4rhqojHSFmt6zyjP2UF4krcM4aU0kMd2aIvyiBk5\r
++++++++7b9zhEvTABb3jU+ofIV8+g6P63IFy/DKCeK2FEFvg4v+Ouvnli8NkjeuEoZ89Agz\r
++++++++k0pw3uAqLM7W+MQhtOenjfJoeSd1W9TJGM/VNQIDAQABo0UwQzAMBgNVHRMEBTAD\r
++++++++AQH/MAsGA1UdDwQEAwIC9DAmBgNVHREEHzAdhhtodHRwOi8vZXhhbXBsZS5vcmcv\r
++++++++d2ViaWQjbWUwDQYJKoZIhvcNAQEFBQADggEBACoWVfuGQlq/+FGw4Alt1cmd5HYO\r
++++++++jZXPRDsG8wXgOoDT6zDqtqD34ReNGXz/naN9FSkTIWnVgN3502kYZgRcUiaY4Wst\r
++++++++1eMhG9HoslpI8nA4gHB0kf/o/ngXBfjglVv0CrBjKCLM/OubnCFftFdKTEC8el27\r
++++++++dtwxcwkeB6i7YMbLRbDX9X2bXvVa05XNIPD7x4oplL2v/EjmHg/ED6OPzsS1klmq\r
++++++++Z1f4nV0AykbXLzAO+oc30nuebF+ZQWcwUbvK8RgoNJUj/isLc7sfSu2SL9tA2+i/\r
++++++++SJgRb+NgyVO5R5yJxK3OYG9TGByvVBMYLgp0RQRO4ZvcJ7rXh2e+QR9F13E=\r
++++++++-----END CERTIFICATE-----\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++var path = require('path')
++++++++var fs = require('fs')
++++++++
++++++++exports.key = fs.readFileSync(path.join(__dirname, 'key.pem'))
++++++++exports.cert = fs.readFileSync(path.join(__dirname, 'cert.pem'))
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++var fs = require('fs')
++++++++var path = require('path')
++++++++var selfsigned = require('selfsigned')
++++++++
++++++++var pems = selfsigned.generate()
++++++++
++++++++fs.writeFileSync(path.join(__dirname, 'key.pem'), pems.private)
++++++++fs.writeFileSync(path.join(__dirname, 'cert.pem'), pems.cert)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++-----BEGIN RSA PRIVATE KEY-----\r
++++++++MIIEowIBAAKCAQEAnqIdKW51MDpwprg/wKKAvcCZAzcNtszWy0hQtWApYLSF5UZR\r
++++++++UyoLDnf24Pnm9D/JDSSG5YWdQW+/K0ehSS7R02huO1uFMQUo1Pyys1LaxhkiMn/V\r
++++++++WCVpQ8m0i/ydfDrmvcj2wmllTHhlUjup22g1qeoDV09bZ4A+2BgD6JYMRE5DPcpt\r
++++++++Q+rYq1Bu8XxYSD4ytiW0z+kC4rhqojHSFmt6zyjP2UF4krcM4aU0kMd2aIvyiBk5\r
++++++++7b9zhEvTABb3jU+ofIV8+g6P63IFy/DKCeK2FEFvg4v+Ouvnli8NkjeuEoZ89Agz\r
++++++++k0pw3uAqLM7W+MQhtOenjfJoeSd1W9TJGM/VNQIDAQABAoIBABZ3JHSd40RDfnkp\r
++++++++7kNocB2PVUweg5dwDnpHAUESf4GlxzGXYgSKV3sC08h8M7BDCUTOd7kxETp3aiYl\r
++++++++tIpx8j7DkxreZPAyiBwTtnYNZZS48f4fWvYrBCuYuPQ7QA1cJRnbCFEzjmhEQ6sf\r
++++++++7nf3W+Q2kycOAsaXY68ERadueonAp4wbH+gfctGsS15NMC1LQEZm0Q5feodK64kD\r
++++++++c2dndo1X53D74XkYoxB3T8d3ScxZcjiEbvzE9mj8PZPTuMR5cHo4uENQ6sCORwEO\r
++++++++gFYdokGlNEGK3Lvy4aD5D7SO3ZS4vEn4DdljNZp7ck8uAaYVYYW7RVNUhISvgj+D\r
++++++++nPEyIrkCgYEAz5bs6Tve8F2JnBdFMKi0dCKFmqQy2wW8Sp7Wl5NV8ruliups2bZy\r
++++++++qk+J1DwfIUlzV9qDeDD679RfBVZg52W0w4oOVUsWtvhCO9fkg/jzzUxUpySOjyjY\r
++++++++YqoikJwvg3W3CNnyaqIx0XQaiUlBTNdLXEWhDiQ1EmmkFxyjIDN2FckCgYEAw6CA\r
++++++++ts+VDlt+7cGomGNHecEO4a74ATZxdXVnSM3Ecna6a2SVyfAhYODizgL/arxGbHbw\r
++++++++l7sEfK7E8COhnzLxUYjzOLNstmyf6r1bzcoaK0gTA8blmE/IMHYrdVaODEwGvEXS\r
++++++++S6utJBHdhw3st7qhLv1hLGW5SSV5l4yBMCwv6g0CgYBG7IK+4QS3TM3yNYUfLdSR\r
++++++++hSXugsEZUalWOVSJ7v3Dc9cIUjWkYJBmncwyZeQg5E5zKqYWT228uOtKEIwFIxuS\r
++++++++Xz2saH43PzSp64YYjNsHPlgBzxyhzsu3UfeDPsmA+Zvyezw2sHmi3S8fzpZv4XLO\r
++++++++8L8o7LfxNYSpnNAi9VIX+QKBgQCBD9H7ScY3+6so5pv5NX6BWBoeUJX0DtwZtwQW\r
++++++++JxLUxXnFwk2ENbV/ub5IVff2jhIUGwAtykdyjscE+yz03+IPfb+hB9bQH6f9cUIw\r
++++++++6YYMSfephT4SF4imWD/hoOIEQOAstA/Ctpd69YJmSIyExk7ytl22scHk/SFUF3Ff\r
++++++++u7c8vQKBgC6FDk3IkctgLBYDJ+017VXjOBsXHgLNkZVTQe2TfKbNZSS8pVVXLxqQ\r
+++++++++PfKGnWOFzKdBsU6TdumgVYPOC0ibE4/njM3o5Or6NmRRR0iDWKm17siG3Tuep6+\r
++++++++4vR+uSCUbFlOmgoFk5xpBkxd0pV7ldRlHfMf/Ol6QNXaESCdUUIG\r
++++++++-----END RSA PRIVATE KEY-----\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "https-pem",
++++++++ "version": "2.0.0",
++++++++ "description": "Self-signed PEM key and certificate ready for use in your HTTPS server",
++++++++ "main": "index.js",
++++++++ "scripts": {
++++++++ "postinstall": "node install.js",
++++++++ "test": "standard && node test.js"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+https://github.com/watson/https-pem.git"
++++++++ },
++++++++ "keywords": [
++++++++ "tls",
++++++++ "https",
++++++++ "pem",
++++++++ "key",
++++++++ "keys",
++++++++ "cert",
++++++++ "certificate",
++++++++ "selfsigned",
++++++++ "self-signed",
++++++++ "development",
++++++++ "options",
++++++++ "server"
++++++++ ],
++++++++ "author": "Thomas Watson Steen <w@tson.dk> (https://twitter.com/wa7son)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/watson/https-pem/issues"
++++++++ },
++++++++ "homepage": "https://github.com/watson/https-pem#readme",
++++++++ "dependencies": {
++++++++ "selfsigned": "^1.10.1"
++++++++ },
++++++++ "devDependencies": {
++++++++ "standard": "^10.0.2"
++++++++ },
++++++++ "coordinates": [
++++++++ 55.7774667,
++++++++ 12.5906882
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++var assert = require('assert')
++++++++var https = require('https')
++++++++var pem = require('./')
++++++++
++++++++var server = https.createServer(pem, function (req, res) {
++++++++ res.end('foo')
++++++++})
++++++++
++++++++server.listen(function () {
++++++++ var opts = {
++++++++ port: server.address().port,
++++++++ rejectUnauthorized: false
++++++++ }
++++++++ https.request(opts, function (res) {
++++++++ assert.strictEqual(res.statusCode, 200)
++++++++ res.on('data', function (chunk) {
++++++++ assert.strictEqual(chunk.toString(), 'foo')
++++++++ process.exit(0)
++++++++ })
++++++++ }).end()
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++You may use the Forge project under the terms of either the BSD License or the
++++++++GNU General Public License (GPL) Version 2.
++++++++
++++++++The BSD License is recommended for most projects. It is simple and easy to
++++++++understand and it places almost no restrictions on what you can do with the
++++++++Forge project.
++++++++
++++++++If the GPL suits your project better you are also free to use Forge under
++++++++that license.
++++++++
++++++++You don't have to do anything special to choose one license or the other and
++++++++you don't have to notify anyone which license you are using. You are free to
++++++++use this project in commercial projects as long as the copyright header is
++++++++left intact.
++++++++
++++++++If you are a commercial entity and use this set of libraries in your
++++++++commercial software then reasonable payment to Digital Bazaar, if you can
++++++++afford it, is not required but is expected and would be appreciated. If this
++++++++library saves you time, then it's saving you money. The cost of developing
++++++++the Forge software was on the order of several hundred hours and tens of
++++++++thousands of dollars. We are attempting to strike a balance between helping
++++++++the development community while not being taken advantage of by lucrative
++++++++commercial entities for our efforts.
++++++++
++++++++-------------------------------------------------------------------------------
++++++++New BSD License (3-clause)
++++++++Copyright (c) 2010, Digital Bazaar, Inc.
++++++++All rights reserved.
++++++++
++++++++Redistribution and use in source and binary forms, with or without
++++++++modification, are permitted provided that the following conditions are met:
++++++++ * Redistributions of source code must retain the above copyright
++++++++ notice, this list of conditions and the following disclaimer.
++++++++ * Redistributions in binary form must reproduce the above copyright
++++++++ notice, this list of conditions and the following disclaimer in the
++++++++ documentation and/or other materials provided with the distribution.
++++++++ * Neither the name of Digital Bazaar, Inc. nor the
++++++++ names of its contributors may be used to endorse or promote products
++++++++ derived from this software without specific prior written permission.
++++++++
++++++++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
++++++++ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
++++++++WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
++++++++DISCLAIMED. IN NO EVENT SHALL DIGITAL BAZAAR BE LIABLE FOR ANY
++++++++DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
++++++++(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
++++++++LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
++++++++ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
++++++++(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
++++++++SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
++++++++
++++++++-------------------------------------------------------------------------------
++++++++ GNU GENERAL PUBLIC LICENSE
++++++++ Version 2, June 1991
++++++++
++++++++ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
++++++++ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++++++++ Everyone is permitted to copy and distribute verbatim copies
++++++++ of this license document, but changing it is not allowed.
++++++++
++++++++ Preamble
++++++++
++++++++ The licenses for most software are designed to take away your
++++++++freedom to share and change it. By contrast, the GNU General Public
++++++++License is intended to guarantee your freedom to share and change free
++++++++software--to make sure the software is free for all its users. This
++++++++General Public License applies to most of the Free Software
++++++++Foundation's software and to any other program whose authors commit to
++++++++using it. (Some other Free Software Foundation software is covered by
++++++++the GNU Lesser General Public License instead.) You can apply it to
++++++++your programs, too.
++++++++
++++++++ When we speak of free software, we are referring to freedom, not
++++++++price. Our General Public Licenses are designed to make sure that you
++++++++have the freedom to distribute copies of free software (and charge for
++++++++this service if you wish), that you receive source code or can get it
++++++++if you want it, that you can change the software or use pieces of it
++++++++in new free programs; and that you know you can do these things.
++++++++
++++++++ To protect your rights, we need to make restrictions that forbid
++++++++anyone to deny you these rights or to ask you to surrender the rights.
++++++++These restrictions translate to certain responsibilities for you if you
++++++++distribute copies of the software, or if you modify it.
++++++++
++++++++ For example, if you distribute copies of such a program, whether
++++++++gratis or for a fee, you must give the recipients all the rights that
++++++++you have. You must make sure that they, too, receive or can get the
++++++++source code. And you must show them these terms so they know their
++++++++rights.
++++++++
++++++++ We protect your rights with two steps: (1) copyright the software, and
++++++++(2) offer you this license which gives you legal permission to copy,
++++++++distribute and/or modify the software.
++++++++
++++++++ Also, for each author's protection and ours, we want to make certain
++++++++that everyone understands that there is no warranty for this free
++++++++software. If the software is modified by someone else and passed on, we
++++++++want its recipients to know that what they have is not the original, so
++++++++that any problems introduced by others will not reflect on the original
++++++++authors' reputations.
++++++++
++++++++ Finally, any free program is threatened constantly by software
++++++++patents. We wish to avoid the danger that redistributors of a free
++++++++program will individually obtain patent licenses, in effect making the
++++++++program proprietary. To prevent this, we have made it clear that any
++++++++patent must be licensed for everyone's free use or not licensed at all.
++++++++
++++++++ The precise terms and conditions for copying, distribution and
++++++++modification follow.
++++++++
++++++++ GNU GENERAL PUBLIC LICENSE
++++++++ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
++++++++
++++++++ 0. This License applies to any program or other work which contains
++++++++a notice placed by the copyright holder saying it may be distributed
++++++++under the terms of this General Public License. The "Program", below,
++++++++refers to any such program or work, and a "work based on the Program"
++++++++means either the Program or any derivative work under copyright law:
++++++++that is to say, a work containing the Program or a portion of it,
++++++++either verbatim or with modifications and/or translated into another
++++++++language. (Hereinafter, translation is included without limitation in
++++++++the term "modification".) Each licensee is addressed as "you".
++++++++
++++++++Activities other than copying, distribution and modification are not
++++++++covered by this License; they are outside its scope. The act of
++++++++running the Program is not restricted, and the output from the Program
++++++++is covered only if its contents constitute a work based on the
++++++++Program (independent of having been made by running the Program).
++++++++Whether that is true depends on what the Program does.
++++++++
++++++++ 1. You may copy and distribute verbatim copies of the Program's
++++++++source code as you receive it, in any medium, provided that you
++++++++conspicuously and appropriately publish on each copy an appropriate
++++++++copyright notice and disclaimer of warranty; keep intact all the
++++++++notices that refer to this License and to the absence of any warranty;
++++++++and give any other recipients of the Program a copy of this License
++++++++along with the Program.
++++++++
++++++++You may charge a fee for the physical act of transferring a copy, and
++++++++you may at your option offer warranty protection in exchange for a fee.
++++++++
++++++++ 2. You may modify your copy or copies of the Program or any portion
++++++++of it, thus forming a work based on the Program, and copy and
++++++++distribute such modifications or work under the terms of Section 1
++++++++above, provided that you also meet all of these conditions:
++++++++
++++++++ a) You must cause the modified files to carry prominent notices
++++++++ stating that you changed the files and the date of any change.
++++++++
++++++++ b) You must cause any work that you distribute or publish, that in
++++++++ whole or in part contains or is derived from the Program or any
++++++++ part thereof, to be licensed as a whole at no charge to all third
++++++++ parties under the terms of this License.
++++++++
++++++++ c) If the modified program normally reads commands interactively
++++++++ when run, you must cause it, when started running for such
++++++++ interactive use in the most ordinary way, to print or display an
++++++++ announcement including an appropriate copyright notice and a
++++++++ notice that there is no warranty (or else, saying that you provide
++++++++ a warranty) and that users may redistribute the program under
++++++++ these conditions, and telling the user how to view a copy of this
++++++++ License. (Exception: if the Program itself is interactive but
++++++++ does not normally print such an announcement, your work based on
++++++++ the Program is not required to print an announcement.)
++++++++
++++++++These requirements apply to the modified work as a whole. If
++++++++identifiable sections of that work are not derived from the Program,
++++++++and can be reasonably considered independent and separate works in
++++++++themselves, then this License, and its terms, do not apply to those
++++++++sections when you distribute them as separate works. But when you
++++++++distribute the same sections as part of a whole which is a work based
++++++++on the Program, the distribution of the whole must be on the terms of
++++++++this License, whose permissions for other licensees extend to the
++++++++entire whole, and thus to each and every part regardless of who wrote it.
++++++++
++++++++Thus, it is not the intent of this section to claim rights or contest
++++++++your rights to work written entirely by you; rather, the intent is to
++++++++exercise the right to control the distribution of derivative or
++++++++collective works based on the Program.
++++++++
++++++++In addition, mere aggregation of another work not based on the Program
++++++++with the Program (or with a work based on the Program) on a volume of
++++++++a storage or distribution medium does not bring the other work under
++++++++the scope of this License.
++++++++
++++++++ 3. You may copy and distribute the Program (or a work based on it,
++++++++under Section 2) in object code or executable form under the terms of
++++++++Sections 1 and 2 above provided that you also do one of the following:
++++++++
++++++++ a) Accompany it with the complete corresponding machine-readable
++++++++ source code, which must be distributed under the terms of Sections
++++++++ 1 and 2 above on a medium customarily used for software interchange; or,
++++++++
++++++++ b) Accompany it with a written offer, valid for at least three
++++++++ years, to give any third party, for a charge no more than your
++++++++ cost of physically performing source distribution, a complete
++++++++ machine-readable copy of the corresponding source code, to be
++++++++ distributed under the terms of Sections 1 and 2 above on a medium
++++++++ customarily used for software interchange; or,
++++++++
++++++++ c) Accompany it with the information you received as to the offer
++++++++ to distribute corresponding source code. (This alternative is
++++++++ allowed only for noncommercial distribution and only if you
++++++++ received the program in object code or executable form with such
++++++++ an offer, in accord with Subsection b above.)
++++++++
++++++++The source code for a work means the preferred form of the work for
++++++++making modifications to it. For an executable work, complete source
++++++++code means all the source code for all modules it contains, plus any
++++++++associated interface definition files, plus the scripts used to
++++++++control compilation and installation of the executable. However, as a
++++++++special exception, the source code distributed need not include
++++++++anything that is normally distributed (in either source or binary
++++++++form) with the major components (compiler, kernel, and so on) of the
++++++++operating system on which the executable runs, unless that component
++++++++itself accompanies the executable.
++++++++
++++++++If distribution of executable or object code is made by offering
++++++++access to copy from a designated place, then offering equivalent
++++++++access to copy the source code from the same place counts as
++++++++distribution of the source code, even though third parties are not
++++++++compelled to copy the source along with the object code.
++++++++
++++++++ 4. You may not copy, modify, sublicense, or distribute the Program
++++++++except as expressly provided under this License. Any attempt
++++++++otherwise to copy, modify, sublicense or distribute the Program is
++++++++void, and will automatically terminate your rights under this License.
++++++++However, parties who have received copies, or rights, from you under
++++++++this License will not have their licenses terminated so long as such
++++++++parties remain in full compliance.
++++++++
++++++++ 5. You are not required to accept this License, since you have not
++++++++signed it. However, nothing else grants you permission to modify or
++++++++distribute the Program or its derivative works. These actions are
++++++++prohibited by law if you do not accept this License. Therefore, by
++++++++modifying or distributing the Program (or any work based on the
++++++++Program), you indicate your acceptance of this License to do so, and
++++++++all its terms and conditions for copying, distributing or modifying
++++++++the Program or works based on it.
++++++++
++++++++ 6. Each time you redistribute the Program (or any work based on the
++++++++Program), the recipient automatically receives a license from the
++++++++original licensor to copy, distribute or modify the Program subject to
++++++++these terms and conditions. You may not impose any further
++++++++restrictions on the recipients' exercise of the rights granted herein.
++++++++You are not responsible for enforcing compliance by third parties to
++++++++this License.
++++++++
++++++++ 7. If, as a consequence of a court judgment or allegation of patent
++++++++infringement or for any other reason (not limited to patent issues),
++++++++conditions are imposed on you (whether by court order, agreement or
++++++++otherwise) that contradict the conditions of this License, they do not
++++++++excuse you from the conditions of this License. If you cannot
++++++++distribute so as to satisfy simultaneously your obligations under this
++++++++License and any other pertinent obligations, then as a consequence you
++++++++may not distribute the Program at all. For example, if a patent
++++++++license would not permit royalty-free redistribution of the Program by
++++++++all those who receive copies directly or indirectly through you, then
++++++++the only way you could satisfy both it and this License would be to
++++++++refrain entirely from distribution of the Program.
++++++++
++++++++If any portion of this section is held invalid or unenforceable under
++++++++any particular circumstance, the balance of the section is intended to
++++++++apply and the section as a whole is intended to apply in other
++++++++circumstances.
++++++++
++++++++It is not the purpose of this section to induce you to infringe any
++++++++patents or other property right claims or to contest validity of any
++++++++such claims; this section has the sole purpose of protecting the
++++++++integrity of the free software distribution system, which is
++++++++implemented by public license practices. Many people have made
++++++++generous contributions to the wide range of software distributed
++++++++through that system in reliance on consistent application of that
++++++++system; it is up to the author/donor to decide if he or she is willing
++++++++to distribute software through any other system and a licensee cannot
++++++++impose that choice.
++++++++
++++++++This section is intended to make thoroughly clear what is believed to
++++++++be a consequence of the rest of this License.
++++++++
++++++++ 8. If the distribution and/or use of the Program is restricted in
++++++++certain countries either by patents or by copyrighted interfaces, the
++++++++original copyright holder who places the Program under this License
++++++++may add an explicit geographical distribution limitation excluding
++++++++those countries, so that distribution is permitted only in or among
++++++++countries not thus excluded. In such case, this License incorporates
++++++++the limitation as if written in the body of this License.
++++++++
++++++++ 9. The Free Software Foundation may publish revised and/or new versions
++++++++of the General Public License from time to time. Such new versions will
++++++++be similar in spirit to the present version, but may differ in detail to
++++++++address new problems or concerns.
++++++++
++++++++Each version is given a distinguishing version number. If the Program
++++++++specifies a version number of this License which applies to it and "any
++++++++later version", you have the option of following the terms and conditions
++++++++either of that version or of any later version published by the Free
++++++++Software Foundation. If the Program does not specify a version number of
++++++++this License, you may choose any version ever published by the Free Software
++++++++Foundation.
++++++++
++++++++ 10. If you wish to incorporate parts of the Program into other free
++++++++programs whose distribution conditions are different, write to the author
++++++++to ask for permission. For software which is copyrighted by the Free
++++++++Software Foundation, write to the Free Software Foundation; we sometimes
++++++++make exceptions for this. Our decision will be guided by the two goals
++++++++of preserving the free status of all derivatives of our free software and
++++++++of promoting the sharing and reuse of software generally.
++++++++
++++++++ NO WARRANTY
++++++++
++++++++ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
++++++++FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
++++++++OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
++++++++PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
++++++++OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
++++++++MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
++++++++TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
++++++++PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
++++++++REPAIR OR CORRECTION.
++++++++
++++++++ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
++++++++WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
++++++++REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
++++++++INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
++++++++OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
++++++++TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
++++++++YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
++++++++PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
++++++++POSSIBILITY OF SUCH DAMAGES.
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Advanced Encryption Standard (AES) implementation.
++++++++ *
++++++++ * This implementation is based on the public domain library 'jscrypto' which
++++++++ * was written by:
++++++++ *
++++++++ * Emily Stark (estark@stanford.edu)
++++++++ * Mike Hamburg (mhamburg@stanford.edu)
++++++++ * Dan Boneh (dabo@cs.stanford.edu)
++++++++ *
++++++++ * Parts of this code are based on the OpenSSL implementation of AES:
++++++++ * http://www.openssl.org
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./cipher');
++++++++require('./cipherModes');
++++++++require('./util');
++++++++
++++++++/* AES API */
++++++++module.exports = forge.aes = forge.aes || {};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var cipher = forge.cipher.createCipher('AES-<mode>', key);
++++++++ * cipher.start({iv: iv});
++++++++ *
++++++++ * Creates an AES cipher object to encrypt data using the given symmetric key.
++++++++ * The output will be stored in the 'output' member of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as a string of bytes, an array of bytes,
++++++++ * a byte buffer, or an array of 32-bit words.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.aes.startEncrypting = function(key, iv, output, mode) {
++++++++ var cipher = _createCipher({
++++++++ key: key,
++++++++ output: output,
++++++++ decrypt: false,
++++++++ mode: mode
++++++++ });
++++++++ cipher.start(iv);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var cipher = forge.cipher.createCipher('AES-<mode>', key);
++++++++ *
++++++++ * Creates an AES cipher object to encrypt data using the given symmetric key.
++++++++ *
++++++++ * The key may be given as a string of bytes, an array of bytes, a
++++++++ * byte buffer, or an array of 32-bit words.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.aes.createEncryptionCipher = function(key, mode) {
++++++++ return _createCipher({
++++++++ key: key,
++++++++ output: null,
++++++++ decrypt: false,
++++++++ mode: mode
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var decipher = forge.cipher.createDecipher('AES-<mode>', key);
++++++++ * decipher.start({iv: iv});
++++++++ *
++++++++ * Creates an AES cipher object to decrypt data using the given symmetric key.
++++++++ * The output will be stored in the 'output' member of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as a string of bytes, an array of bytes,
++++++++ * a byte buffer, or an array of 32-bit words.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.aes.startDecrypting = function(key, iv, output, mode) {
++++++++ var cipher = _createCipher({
++++++++ key: key,
++++++++ output: output,
++++++++ decrypt: true,
++++++++ mode: mode
++++++++ });
++++++++ cipher.start(iv);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var decipher = forge.cipher.createDecipher('AES-<mode>', key);
++++++++ *
++++++++ * Creates an AES cipher object to decrypt data using the given symmetric key.
++++++++ *
++++++++ * The key may be given as a string of bytes, an array of bytes, a
++++++++ * byte buffer, or an array of 32-bit words.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.aes.createDecryptionCipher = function(key, mode) {
++++++++ return _createCipher({
++++++++ key: key,
++++++++ output: null,
++++++++ decrypt: true,
++++++++ mode: mode
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new AES cipher algorithm object.
++++++++ *
++++++++ * @param name the name of the algorithm.
++++++++ * @param mode the mode factory function.
++++++++ *
++++++++ * @return the AES algorithm object.
++++++++ */
++++++++forge.aes.Algorithm = function(name, mode) {
++++++++ if(!init) {
++++++++ initialize();
++++++++ }
++++++++ var self = this;
++++++++ self.name = name;
++++++++ self.mode = new mode({
++++++++ blockSize: 16,
++++++++ cipher: {
++++++++ encrypt: function(inBlock, outBlock) {
++++++++ return _updateBlock(self._w, inBlock, outBlock, false);
++++++++ },
++++++++ decrypt: function(inBlock, outBlock) {
++++++++ return _updateBlock(self._w, inBlock, outBlock, true);
++++++++ }
++++++++ }
++++++++ });
++++++++ self._init = false;
++++++++};
++++++++
++++++++/**
++++++++ * Initializes this AES algorithm by expanding its key.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * key the key to use with this algorithm.
++++++++ * decrypt true if the algorithm should be initialized for decryption,
++++++++ * false for encryption.
++++++++ */
++++++++forge.aes.Algorithm.prototype.initialize = function(options) {
++++++++ if(this._init) {
++++++++ return;
++++++++ }
++++++++
++++++++ var key = options.key;
++++++++ var tmp;
++++++++
++++++++ /* Note: The key may be a string of bytes, an array of bytes, a byte
++++++++ buffer, or an array of 32-bit integers. If the key is in bytes, then
++++++++ it must be 16, 24, or 32 bytes in length. If it is in 32-bit
++++++++ integers, it must be 4, 6, or 8 integers long. */
++++++++
++++++++ if(typeof key === 'string' &&
++++++++ (key.length === 16 || key.length === 24 || key.length === 32)) {
++++++++ // convert key string into byte buffer
++++++++ key = forge.util.createBuffer(key);
++++++++ } else if(forge.util.isArray(key) &&
++++++++ (key.length === 16 || key.length === 24 || key.length === 32)) {
++++++++ // convert key integer array into byte buffer
++++++++ tmp = key;
++++++++ key = forge.util.createBuffer();
++++++++ for(var i = 0; i < tmp.length; ++i) {
++++++++ key.putByte(tmp[i]);
++++++++ }
++++++++ }
++++++++
++++++++ // convert key byte buffer into 32-bit integer array
++++++++ if(!forge.util.isArray(key)) {
++++++++ tmp = key;
++++++++ key = [];
++++++++
++++++++ // key lengths of 16, 24, 32 bytes allowed
++++++++ var len = tmp.length();
++++++++ if(len === 16 || len === 24 || len === 32) {
++++++++ len = len >>> 2;
++++++++ for(var i = 0; i < len; ++i) {
++++++++ key.push(tmp.getInt32());
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // key must be an array of 32-bit integers by now
++++++++ if(!forge.util.isArray(key) ||
++++++++ !(key.length === 4 || key.length === 6 || key.length === 8)) {
++++++++ throw new Error('Invalid key parameter.');
++++++++ }
++++++++
++++++++ // encryption operation is always used for these modes
++++++++ var mode = this.mode.name;
++++++++ var encryptOp = (['CFB', 'OFB', 'CTR', 'GCM'].indexOf(mode) !== -1);
++++++++
++++++++ // do key expansion
++++++++ this._w = _expandKey(key, options.decrypt && !encryptOp);
++++++++ this._init = true;
++++++++};
++++++++
++++++++/**
++++++++ * Expands a key. Typically only used for testing.
++++++++ *
++++++++ * @param key the symmetric key to expand, as an array of 32-bit words.
++++++++ * @param decrypt true to expand for decryption, false for encryption.
++++++++ *
++++++++ * @return the expanded key.
++++++++ */
++++++++forge.aes._expandKey = function(key, decrypt) {
++++++++ if(!init) {
++++++++ initialize();
++++++++ }
++++++++ return _expandKey(key, decrypt);
++++++++};
++++++++
++++++++/**
++++++++ * Updates a single block. Typically only used for testing.
++++++++ *
++++++++ * @param w the expanded key to use.
++++++++ * @param input an array of block-size 32-bit words.
++++++++ * @param output an array of block-size 32-bit words.
++++++++ * @param decrypt true to decrypt, false to encrypt.
++++++++ */
++++++++forge.aes._updateBlock = _updateBlock;
++++++++
++++++++/** Register AES algorithms **/
++++++++
++++++++registerAlgorithm('AES-ECB', forge.cipher.modes.ecb);
++++++++registerAlgorithm('AES-CBC', forge.cipher.modes.cbc);
++++++++registerAlgorithm('AES-CFB', forge.cipher.modes.cfb);
++++++++registerAlgorithm('AES-OFB', forge.cipher.modes.ofb);
++++++++registerAlgorithm('AES-CTR', forge.cipher.modes.ctr);
++++++++registerAlgorithm('AES-GCM', forge.cipher.modes.gcm);
++++++++
++++++++function registerAlgorithm(name, mode) {
++++++++ var factory = function() {
++++++++ return new forge.aes.Algorithm(name, mode);
++++++++ };
++++++++ forge.cipher.registerAlgorithm(name, factory);
++++++++}
++++++++
++++++++/** AES implementation **/
++++++++
++++++++var init = false; // not yet initialized
++++++++var Nb = 4; // number of words comprising the state (AES = 4)
++++++++var sbox; // non-linear substitution table used in key expansion
++++++++var isbox; // inversion of sbox
++++++++var rcon; // round constant word array
++++++++var mix; // mix-columns table
++++++++var imix; // inverse mix-columns table
++++++++
++++++++/**
++++++++ * Performs initialization, ie: precomputes tables to optimize for speed.
++++++++ *
++++++++ * One way to understand how AES works is to imagine that 'addition' and
++++++++ * 'multiplication' are interfaces that require certain mathematical
++++++++ * properties to hold true (ie: they are associative) but they might have
++++++++ * different implementations and produce different kinds of results ...
++++++++ * provided that their mathematical properties remain true. AES defines
++++++++ * its own methods of addition and multiplication but keeps some important
++++++++ * properties the same, ie: associativity and distributivity. The
++++++++ * explanation below tries to shed some light on how AES defines addition
++++++++ * and multiplication of bytes and 32-bit words in order to perform its
++++++++ * encryption and decryption algorithms.
++++++++ *
++++++++ * The basics:
++++++++ *
++++++++ * The AES algorithm views bytes as binary representations of polynomials
++++++++ * that have either 1 or 0 as the coefficients. It defines the addition
++++++++ * or subtraction of two bytes as the XOR operation. It also defines the
++++++++ * multiplication of two bytes as a finite field referred to as GF(2^8)
++++++++ * (Note: 'GF' means "Galois Field" which is a field that contains a finite
++++++++ * number of elements so GF(2^8) has 256 elements).
++++++++ *
++++++++ * This means that any two bytes can be represented as binary polynomials;
++++++++ * when they multiplied together and modularly reduced by an irreducible
++++++++ * polynomial of the 8th degree, the results are the field GF(2^8). The
++++++++ * specific irreducible polynomial that AES uses in hexadecimal is 0x11b.
++++++++ * This multiplication is associative with 0x01 as the identity:
++++++++ *
++++++++ * (b * 0x01 = GF(b, 0x01) = b).
++++++++ *
++++++++ * The operation GF(b, 0x02) can be performed at the byte level by left
++++++++ * shifting b once and then XOR'ing it (to perform the modular reduction)
++++++++ * with 0x11b if b is >= 128. Repeated application of the multiplication
++++++++ * of 0x02 can be used to implement the multiplication of any two bytes.
++++++++ *
++++++++ * For instance, multiplying 0x57 and 0x13, denoted as GF(0x57, 0x13), can
++++++++ * be performed by factoring 0x13 into 0x01, 0x02, and 0x10. Then these
++++++++ * factors can each be multiplied by 0x57 and then added together. To do
++++++++ * the multiplication, values for 0x57 multiplied by each of these 3 factors
++++++++ * can be precomputed and stored in a table. To add them, the values from
++++++++ * the table are XOR'd together.
++++++++ *
++++++++ * AES also defines addition and multiplication of words, that is 4-byte
++++++++ * numbers represented as polynomials of 3 degrees where the coefficients
++++++++ * are the values of the bytes.
++++++++ *
++++++++ * The word [a0, a1, a2, a3] is a polynomial a3x^3 + a2x^2 + a1x + a0.
++++++++ *
++++++++ * Addition is performed by XOR'ing like powers of x. Multiplication
++++++++ * is performed in two steps, the first is an algebriac expansion as
++++++++ * you would do normally (where addition is XOR). But the result is
++++++++ * a polynomial larger than 3 degrees and thus it cannot fit in a word. So
++++++++ * next the result is modularly reduced by an AES-specific polynomial of
++++++++ * degree 4 which will always produce a polynomial of less than 4 degrees
++++++++ * such that it will fit in a word. In AES, this polynomial is x^4 + 1.
++++++++ *
++++++++ * The modular product of two polynomials 'a' and 'b' is thus:
++++++++ *
++++++++ * d(x) = d3x^3 + d2x^2 + d1x + d0
++++++++ * with
++++++++ * d0 = GF(a0, b0) ^ GF(a3, b1) ^ GF(a2, b2) ^ GF(a1, b3)
++++++++ * d1 = GF(a1, b0) ^ GF(a0, b1) ^ GF(a3, b2) ^ GF(a2, b3)
++++++++ * d2 = GF(a2, b0) ^ GF(a1, b1) ^ GF(a0, b2) ^ GF(a3, b3)
++++++++ * d3 = GF(a3, b0) ^ GF(a2, b1) ^ GF(a1, b2) ^ GF(a0, b3)
++++++++ *
++++++++ * As a matrix:
++++++++ *
++++++++ * [d0] = [a0 a3 a2 a1][b0]
++++++++ * [d1] [a1 a0 a3 a2][b1]
++++++++ * [d2] [a2 a1 a0 a3][b2]
++++++++ * [d3] [a3 a2 a1 a0][b3]
++++++++ *
++++++++ * Special polynomials defined by AES (0x02 == {02}):
++++++++ * a(x) = {03}x^3 + {01}x^2 + {01}x + {02}
++++++++ * a^-1(x) = {0b}x^3 + {0d}x^2 + {09}x + {0e}.
++++++++ *
++++++++ * These polynomials are used in the MixColumns() and InverseMixColumns()
++++++++ * operations, respectively, to cause each element in the state to affect
++++++++ * the output (referred to as diffusing).
++++++++ *
++++++++ * RotWord() uses: a0 = a1 = a2 = {00} and a3 = {01}, which is the
++++++++ * polynomial x3.
++++++++ *
++++++++ * The ShiftRows() method modifies the last 3 rows in the state (where
++++++++ * the state is 4 words with 4 bytes per word) by shifting bytes cyclically.
++++++++ * The 1st byte in the second row is moved to the end of the row. The 1st
++++++++ * and 2nd bytes in the third row are moved to the end of the row. The 1st,
++++++++ * 2nd, and 3rd bytes are moved in the fourth row.
++++++++ *
++++++++ * More details on how AES arithmetic works:
++++++++ *
++++++++ * In the polynomial representation of binary numbers, XOR performs addition
++++++++ * and subtraction and multiplication in GF(2^8) denoted as GF(a, b)
++++++++ * corresponds with the multiplication of polynomials modulo an irreducible
++++++++ * polynomial of degree 8. In other words, for AES, GF(a, b) will multiply
++++++++ * polynomial 'a' with polynomial 'b' and then do a modular reduction by
++++++++ * an AES-specific irreducible polynomial of degree 8.
++++++++ *
++++++++ * A polynomial is irreducible if its only divisors are one and itself. For
++++++++ * the AES algorithm, this irreducible polynomial is:
++++++++ *
++++++++ * m(x) = x^8 + x^4 + x^3 + x + 1,
++++++++ *
++++++++ * or {01}{1b} in hexadecimal notation, where each coefficient is a bit:
++++++++ * 100011011 = 283 = 0x11b.
++++++++ *
++++++++ * For example, GF(0x57, 0x83) = 0xc1 because
++++++++ *
++++++++ * 0x57 = 87 = 01010111 = x^6 + x^4 + x^2 + x + 1
++++++++ * 0x85 = 131 = 10000101 = x^7 + x + 1
++++++++ *
++++++++ * (x^6 + x^4 + x^2 + x + 1) * (x^7 + x + 1)
++++++++ * = x^13 + x^11 + x^9 + x^8 + x^7 +
++++++++ * x^7 + x^5 + x^3 + x^2 + x +
++++++++ * x^6 + x^4 + x^2 + x + 1
++++++++ * = x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 = y
++++++++ * y modulo (x^8 + x^4 + x^3 + x + 1)
++++++++ * = x^7 + x^6 + 1.
++++++++ *
++++++++ * The modular reduction by m(x) guarantees the result will be a binary
++++++++ * polynomial of less than degree 8, so that it can fit in a byte.
++++++++ *
++++++++ * The operation to multiply a binary polynomial b with x (the polynomial
++++++++ * x in binary representation is 00000010) is:
++++++++ *
++++++++ * b_7x^8 + b_6x^7 + b_5x^6 + b_4x^5 + b_3x^4 + b_2x^3 + b_1x^2 + b_0x^1
++++++++ *
++++++++ * To get GF(b, x) we must reduce that by m(x). If b_7 is 0 (that is the
++++++++ * most significant bit is 0 in b) then the result is already reduced. If
++++++++ * it is 1, then we can reduce it by subtracting m(x) via an XOR.
++++++++ *
++++++++ * It follows that multiplication by x (00000010 or 0x02) can be implemented
++++++++ * by performing a left shift followed by a conditional bitwise XOR with
++++++++ * 0x1b. This operation on bytes is denoted by xtime(). Multiplication by
++++++++ * higher powers of x can be implemented by repeated application of xtime().
++++++++ *
++++++++ * By adding intermediate results, multiplication by any constant can be
++++++++ * implemented. For instance:
++++++++ *
++++++++ * GF(0x57, 0x13) = 0xfe because:
++++++++ *
++++++++ * xtime(b) = (b & 128) ? (b << 1 ^ 0x11b) : (b << 1)
++++++++ *
++++++++ * Note: We XOR with 0x11b instead of 0x1b because in javascript our
++++++++ * datatype for b can be larger than 1 byte, so a left shift will not
++++++++ * automatically eliminate bits that overflow a byte ... by XOR'ing the
++++++++ * overflow bit with 1 (the extra one from 0x11b) we zero it out.
++++++++ *
++++++++ * GF(0x57, 0x02) = xtime(0x57) = 0xae
++++++++ * GF(0x57, 0x04) = xtime(0xae) = 0x47
++++++++ * GF(0x57, 0x08) = xtime(0x47) = 0x8e
++++++++ * GF(0x57, 0x10) = xtime(0x8e) = 0x07
++++++++ *
++++++++ * GF(0x57, 0x13) = GF(0x57, (0x01 ^ 0x02 ^ 0x10))
++++++++ *
++++++++ * And by the distributive property (since XOR is addition and GF() is
++++++++ * multiplication):
++++++++ *
++++++++ * = GF(0x57, 0x01) ^ GF(0x57, 0x02) ^ GF(0x57, 0x10)
++++++++ * = 0x57 ^ 0xae ^ 0x07
++++++++ * = 0xfe.
++++++++ */
++++++++function initialize() {
++++++++ init = true;
++++++++
++++++++ /* Populate the Rcon table. These are the values given by
++++++++ [x^(i-1),{00},{00},{00}] where x^(i-1) are powers of x (and x = 0x02)
++++++++ in the field of GF(2^8), where i starts at 1.
++++++++
++++++++ rcon[0] = [0x00, 0x00, 0x00, 0x00]
++++++++ rcon[1] = [0x01, 0x00, 0x00, 0x00] 2^(1-1) = 2^0 = 1
++++++++ rcon[2] = [0x02, 0x00, 0x00, 0x00] 2^(2-1) = 2^1 = 2
++++++++ ...
++++++++ rcon[9] = [0x1B, 0x00, 0x00, 0x00] 2^(9-1) = 2^8 = 0x1B
++++++++ rcon[10] = [0x36, 0x00, 0x00, 0x00] 2^(10-1) = 2^9 = 0x36
++++++++
++++++++ We only store the first byte because it is the only one used.
++++++++ */
++++++++ rcon = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36];
++++++++
++++++++ // compute xtime table which maps i onto GF(i, 0x02)
++++++++ var xtime = new Array(256);
++++++++ for(var i = 0; i < 128; ++i) {
++++++++ xtime[i] = i << 1;
++++++++ xtime[i + 128] = (i + 128) << 1 ^ 0x11B;
++++++++ }
++++++++
++++++++ // compute all other tables
++++++++ sbox = new Array(256);
++++++++ isbox = new Array(256);
++++++++ mix = new Array(4);
++++++++ imix = new Array(4);
++++++++ for(var i = 0; i < 4; ++i) {
++++++++ mix[i] = new Array(256);
++++++++ imix[i] = new Array(256);
++++++++ }
++++++++ var e = 0, ei = 0, e2, e4, e8, sx, sx2, me, ime;
++++++++ for(var i = 0; i < 256; ++i) {
++++++++ /* We need to generate the SubBytes() sbox and isbox tables so that
++++++++ we can perform byte substitutions. This requires us to traverse
++++++++ all of the elements in GF, find their multiplicative inverses,
++++++++ and apply to each the following affine transformation:
++++++++
++++++++ bi' = bi ^ b(i + 4) mod 8 ^ b(i + 5) mod 8 ^ b(i + 6) mod 8 ^
++++++++ b(i + 7) mod 8 ^ ci
++++++++ for 0 <= i < 8, where bi is the ith bit of the byte, and ci is the
++++++++ ith bit of a byte c with the value {63} or {01100011}.
++++++++
++++++++ It is possible to traverse every possible value in a Galois field
++++++++ using what is referred to as a 'generator'. There are many
++++++++ generators (128 out of 256): 3,5,6,9,11,82 to name a few. To fully
++++++++ traverse GF we iterate 255 times, multiplying by our generator
++++++++ each time.
++++++++
++++++++ On each iteration we can determine the multiplicative inverse for
++++++++ the current element.
++++++++
++++++++ Suppose there is an element in GF 'e'. For a given generator 'g',
++++++++ e = g^x. The multiplicative inverse of e is g^(255 - x). It turns
++++++++ out that if use the inverse of a generator as another generator
++++++++ it will produce all of the corresponding multiplicative inverses
++++++++ at the same time. For this reason, we choose 5 as our inverse
++++++++ generator because it only requires 2 multiplies and 1 add and its
++++++++ inverse, 82, requires relatively few operations as well.
++++++++
++++++++ In order to apply the affine transformation, the multiplicative
++++++++ inverse 'ei' of 'e' can be repeatedly XOR'd (4 times) with a
++++++++ bit-cycling of 'ei'. To do this 'ei' is first stored in 's' and
++++++++ 'x'. Then 's' is left shifted and the high bit of 's' is made the
++++++++ low bit. The resulting value is stored in 's'. Then 'x' is XOR'd
++++++++ with 's' and stored in 'x'. On each subsequent iteration the same
++++++++ operation is performed. When 4 iterations are complete, 'x' is
++++++++ XOR'd with 'c' (0x63) and the transformed value is stored in 'x'.
++++++++ For example:
++++++++
++++++++ s = 01000001
++++++++ x = 01000001
++++++++
++++++++ iteration 1: s = 10000010, x ^= s
++++++++ iteration 2: s = 00000101, x ^= s
++++++++ iteration 3: s = 00001010, x ^= s
++++++++ iteration 4: s = 00010100, x ^= s
++++++++ x ^= 0x63
++++++++
++++++++ This can be done with a loop where s = (s << 1) | (s >> 7). However,
++++++++ it can also be done by using a single 16-bit (in this case 32-bit)
++++++++ number 'sx'. Since XOR is an associative operation, we can set 'sx'
++++++++ to 'ei' and then XOR it with 'sx' left-shifted 1,2,3, and 4 times.
++++++++ The most significant bits will flow into the high 8 bit positions
++++++++ and be correctly XOR'd with one another. All that remains will be
++++++++ to cycle the high 8 bits by XOR'ing them all with the lower 8 bits
++++++++ afterwards.
++++++++
++++++++ At the same time we're populating sbox and isbox we can precompute
++++++++ the multiplication we'll need to do to do MixColumns() later.
++++++++ */
++++++++
++++++++ // apply affine transformation
++++++++ sx = ei ^ (ei << 1) ^ (ei << 2) ^ (ei << 3) ^ (ei << 4);
++++++++ sx = (sx >> 8) ^ (sx & 255) ^ 0x63;
++++++++
++++++++ // update tables
++++++++ sbox[e] = sx;
++++++++ isbox[sx] = e;
++++++++
++++++++ /* Mixing columns is done using matrix multiplication. The columns
++++++++ that are to be mixed are each a single word in the current state.
++++++++ The state has Nb columns (4 columns). Therefore each column is a
++++++++ 4 byte word. So to mix the columns in a single column 'c' where
++++++++ its rows are r0, r1, r2, and r3, we use the following matrix
++++++++ multiplication:
++++++++
++++++++ [2 3 1 1]*[r0,c]=[r'0,c]
++++++++ [1 2 3 1] [r1,c] [r'1,c]
++++++++ [1 1 2 3] [r2,c] [r'2,c]
++++++++ [3 1 1 2] [r3,c] [r'3,c]
++++++++
++++++++ r0, r1, r2, and r3 are each 1 byte of one of the words in the
++++++++ state (a column). To do matrix multiplication for each mixed
++++++++ column c' we multiply the corresponding row from the left matrix
++++++++ with the corresponding column from the right matrix. In total, we
++++++++ get 4 equations:
++++++++
++++++++ r0,c' = 2*r0,c + 3*r1,c + 1*r2,c + 1*r3,c
++++++++ r1,c' = 1*r0,c + 2*r1,c + 3*r2,c + 1*r3,c
++++++++ r2,c' = 1*r0,c + 1*r1,c + 2*r2,c + 3*r3,c
++++++++ r3,c' = 3*r0,c + 1*r1,c + 1*r2,c + 2*r3,c
++++++++
++++++++ As usual, the multiplication is as previously defined and the
++++++++ addition is XOR. In order to optimize mixing columns we can store
++++++++ the multiplication results in tables. If you think of the whole
++++++++ column as a word (it might help to visualize by mentally rotating
++++++++ the equations above by counterclockwise 90 degrees) then you can
++++++++ see that it would be useful to map the multiplications performed on
++++++++ each byte (r0, r1, r2, r3) onto a word as well. For instance, we
++++++++ could map 2*r0,1*r0,1*r0,3*r0 onto a word by storing 2*r0 in the
++++++++ highest 8 bits and 3*r0 in the lowest 8 bits (with the other two
++++++++ respectively in the middle). This means that a table can be
++++++++ constructed that uses r0 as an index to the word. We can do the
++++++++ same with r1, r2, and r3, creating a total of 4 tables.
++++++++
++++++++ To construct a full c', we can just look up each byte of c in
++++++++ their respective tables and XOR the results together.
++++++++
++++++++ Also, to build each table we only have to calculate the word
++++++++ for 2,1,1,3 for every byte ... which we can do on each iteration
++++++++ of this loop since we will iterate over every byte. After we have
++++++++ calculated 2,1,1,3 we can get the results for the other tables
++++++++ by cycling the byte at the end to the beginning. For instance
++++++++ we can take the result of table 2,1,1,3 and produce table 3,2,1,1
++++++++ by moving the right most byte to the left most position just like
++++++++ how you can imagine the 3 moved out of 2,1,1,3 and to the front
++++++++ to produce 3,2,1,1.
++++++++
++++++++ There is another optimization in that the same multiples of
++++++++ the current element we need in order to advance our generator
++++++++ to the next iteration can be reused in performing the 2,1,1,3
++++++++ calculation. We also calculate the inverse mix column tables,
++++++++ with e,9,d,b being the inverse of 2,1,1,3.
++++++++
++++++++ When we're done, and we need to actually mix columns, the first
++++++++ byte of each state word should be put through mix[0] (2,1,1,3),
++++++++ the second through mix[1] (3,2,1,1) and so forth. Then they should
++++++++ be XOR'd together to produce the fully mixed column.
++++++++ */
++++++++
++++++++ // calculate mix and imix table values
++++++++ sx2 = xtime[sx];
++++++++ e2 = xtime[e];
++++++++ e4 = xtime[e2];
++++++++ e8 = xtime[e4];
++++++++ me =
++++++++ (sx2 << 24) ^ // 2
++++++++ (sx << 16) ^ // 1
++++++++ (sx << 8) ^ // 1
++++++++ (sx ^ sx2); // 3
++++++++ ime =
++++++++ (e2 ^ e4 ^ e8) << 24 ^ // E (14)
++++++++ (e ^ e8) << 16 ^ // 9
++++++++ (e ^ e4 ^ e8) << 8 ^ // D (13)
++++++++ (e ^ e2 ^ e8); // B (11)
++++++++ // produce each of the mix tables by rotating the 2,1,1,3 value
++++++++ for(var n = 0; n < 4; ++n) {
++++++++ mix[n][e] = me;
++++++++ imix[n][sx] = ime;
++++++++ // cycle the right most byte to the left most position
++++++++ // ie: 2,1,1,3 becomes 3,2,1,1
++++++++ me = me << 24 | me >>> 8;
++++++++ ime = ime << 24 | ime >>> 8;
++++++++ }
++++++++
++++++++ // get next element and inverse
++++++++ if(e === 0) {
++++++++ // 1 is the inverse of 1
++++++++ e = ei = 1;
++++++++ } else {
++++++++ // e = 2e + 2*2*2*(10e)) = multiply e by 82 (chosen generator)
++++++++ // ei = ei + 2*2*ei = multiply ei by 5 (inverse generator)
++++++++ e = e2 ^ xtime[xtime[xtime[e2 ^ e8]]];
++++++++ ei ^= xtime[xtime[ei]];
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Generates a key schedule using the AES key expansion algorithm.
++++++++ *
++++++++ * The AES algorithm takes the Cipher Key, K, and performs a Key Expansion
++++++++ * routine to generate a key schedule. The Key Expansion generates a total
++++++++ * of Nb*(Nr + 1) words: the algorithm requires an initial set of Nb words,
++++++++ * and each of the Nr rounds requires Nb words of key data. The resulting
++++++++ * key schedule consists of a linear array of 4-byte words, denoted [wi ],
++++++++ * with i in the range 0 <= i < Nb(Nr + 1).
++++++++ *
++++++++ * KeyExpansion(byte key[4*Nk], word w[Nb*(Nr+1)], Nk)
++++++++ * AES-128 (Nb=4, Nk=4, Nr=10)
++++++++ * AES-192 (Nb=4, Nk=6, Nr=12)
++++++++ * AES-256 (Nb=4, Nk=8, Nr=14)
++++++++ * Note: Nr=Nk+6.
++++++++ *
++++++++ * Nb is the number of columns (32-bit words) comprising the State (or
++++++++ * number of bytes in a block). For AES, Nb=4.
++++++++ *
++++++++ * @param key the key to schedule (as an array of 32-bit words).
++++++++ * @param decrypt true to modify the key schedule to decrypt, false not to.
++++++++ *
++++++++ * @return the generated key schedule.
++++++++ */
++++++++function _expandKey(key, decrypt) {
++++++++ // copy the key's words to initialize the key schedule
++++++++ var w = key.slice(0);
++++++++
++++++++ /* RotWord() will rotate a word, moving the first byte to the last
++++++++ byte's position (shifting the other bytes left).
++++++++
++++++++ We will be getting the value of Rcon at i / Nk. 'i' will iterate
++++++++ from Nk to (Nb * Nr+1). Nk = 4 (4 byte key), Nb = 4 (4 words in
++++++++ a block), Nr = Nk + 6 (10). Therefore 'i' will iterate from
++++++++ 4 to 44 (exclusive). Each time we iterate 4 times, i / Nk will
++++++++ increase by 1. We use a counter iNk to keep track of this.
++++++++ */
++++++++
++++++++ // go through the rounds expanding the key
++++++++ var temp, iNk = 1;
++++++++ var Nk = w.length;
++++++++ var Nr1 = Nk + 6 + 1;
++++++++ var end = Nb * Nr1;
++++++++ for(var i = Nk; i < end; ++i) {
++++++++ temp = w[i - 1];
++++++++ if(i % Nk === 0) {
++++++++ // temp = SubWord(RotWord(temp)) ^ Rcon[i / Nk]
++++++++ temp =
++++++++ sbox[temp >>> 16 & 255] << 24 ^
++++++++ sbox[temp >>> 8 & 255] << 16 ^
++++++++ sbox[temp & 255] << 8 ^
++++++++ sbox[temp >>> 24] ^ (rcon[iNk] << 24);
++++++++ iNk++;
++++++++ } else if(Nk > 6 && (i % Nk === 4)) {
++++++++ // temp = SubWord(temp)
++++++++ temp =
++++++++ sbox[temp >>> 24] << 24 ^
++++++++ sbox[temp >>> 16 & 255] << 16 ^
++++++++ sbox[temp >>> 8 & 255] << 8 ^
++++++++ sbox[temp & 255];
++++++++ }
++++++++ w[i] = w[i - Nk] ^ temp;
++++++++ }
++++++++
++++++++ /* When we are updating a cipher block we always use the code path for
++++++++ encryption whether we are decrypting or not (to shorten code and
++++++++ simplify the generation of look up tables). However, because there
++++++++ are differences in the decryption algorithm, other than just swapping
++++++++ in different look up tables, we must transform our key schedule to
++++++++ account for these changes:
++++++++
++++++++ 1. The decryption algorithm gets its key rounds in reverse order.
++++++++ 2. The decryption algorithm adds the round key before mixing columns
++++++++ instead of afterwards.
++++++++
++++++++ We don't need to modify our key schedule to handle the first case,
++++++++ we can just traverse the key schedule in reverse order when decrypting.
++++++++
++++++++ The second case requires a little work.
++++++++
++++++++ The tables we built for performing rounds will take an input and then
++++++++ perform SubBytes() and MixColumns() or, for the decrypt version,
++++++++ InvSubBytes() and InvMixColumns(). But the decrypt algorithm requires
++++++++ us to AddRoundKey() before InvMixColumns(). This means we'll need to
++++++++ apply some transformations to the round key to inverse-mix its columns
++++++++ so they'll be correct for moving AddRoundKey() to after the state has
++++++++ had its columns inverse-mixed.
++++++++
++++++++ To inverse-mix the columns of the state when we're decrypting we use a
++++++++ lookup table that will apply InvSubBytes() and InvMixColumns() at the
++++++++ same time. However, the round key's bytes are not inverse-substituted
++++++++ in the decryption algorithm. To get around this problem, we can first
++++++++ substitute the bytes in the round key so that when we apply the
++++++++ transformation via the InvSubBytes()+InvMixColumns() table, it will
++++++++ undo our substitution leaving us with the original value that we
++++++++ want -- and then inverse-mix that value.
++++++++
++++++++ This change will correctly alter our key schedule so that we can XOR
++++++++ each round key with our already transformed decryption state. This
++++++++ allows us to use the same code path as the encryption algorithm.
++++++++
++++++++ We make one more change to the decryption key. Since the decryption
++++++++ algorithm runs in reverse from the encryption algorithm, we reverse
++++++++ the order of the round keys to avoid having to iterate over the key
++++++++ schedule backwards when running the encryption algorithm later in
++++++++ decryption mode. In addition to reversing the order of the round keys,
++++++++ we also swap each round key's 2nd and 4th rows. See the comments
++++++++ section where rounds are performed for more details about why this is
++++++++ done. These changes are done inline with the other substitution
++++++++ described above.
++++++++ */
++++++++ if(decrypt) {
++++++++ var tmp;
++++++++ var m0 = imix[0];
++++++++ var m1 = imix[1];
++++++++ var m2 = imix[2];
++++++++ var m3 = imix[3];
++++++++ var wnew = w.slice(0);
++++++++ end = w.length;
++++++++ for(var i = 0, wi = end - Nb; i < end; i += Nb, wi -= Nb) {
++++++++ // do not sub the first or last round key (round keys are Nb
++++++++ // words) as no column mixing is performed before they are added,
++++++++ // but do change the key order
++++++++ if(i === 0 || i === (end - Nb)) {
++++++++ wnew[i] = w[wi];
++++++++ wnew[i + 1] = w[wi + 3];
++++++++ wnew[i + 2] = w[wi + 2];
++++++++ wnew[i + 3] = w[wi + 1];
++++++++ } else {
++++++++ // substitute each round key byte because the inverse-mix
++++++++ // table will inverse-substitute it (effectively cancel the
++++++++ // substitution because round key bytes aren't sub'd in
++++++++ // decryption mode) and swap indexes 3 and 1
++++++++ for(var n = 0; n < Nb; ++n) {
++++++++ tmp = w[wi + n];
++++++++ wnew[i + (3&-n)] =
++++++++ m0[sbox[tmp >>> 24]] ^
++++++++ m1[sbox[tmp >>> 16 & 255]] ^
++++++++ m2[sbox[tmp >>> 8 & 255]] ^
++++++++ m3[sbox[tmp & 255]];
++++++++ }
++++++++ }
++++++++ }
++++++++ w = wnew;
++++++++ }
++++++++
++++++++ return w;
++++++++}
++++++++
++++++++/**
++++++++ * Updates a single block (16 bytes) using AES. The update will either
++++++++ * encrypt or decrypt the block.
++++++++ *
++++++++ * @param w the key schedule.
++++++++ * @param input the input block (an array of 32-bit words).
++++++++ * @param output the updated output block.
++++++++ * @param decrypt true to decrypt the block, false to encrypt it.
++++++++ */
++++++++function _updateBlock(w, input, output, decrypt) {
++++++++ /*
++++++++ Cipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)])
++++++++ begin
++++++++ byte state[4,Nb]
++++++++ state = in
++++++++ AddRoundKey(state, w[0, Nb-1])
++++++++ for round = 1 step 1 to Nr-1
++++++++ SubBytes(state)
++++++++ ShiftRows(state)
++++++++ MixColumns(state)
++++++++ AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
++++++++ end for
++++++++ SubBytes(state)
++++++++ ShiftRows(state)
++++++++ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
++++++++ out = state
++++++++ end
++++++++
++++++++ InvCipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)])
++++++++ begin
++++++++ byte state[4,Nb]
++++++++ state = in
++++++++ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
++++++++ for round = Nr-1 step -1 downto 1
++++++++ InvShiftRows(state)
++++++++ InvSubBytes(state)
++++++++ AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
++++++++ InvMixColumns(state)
++++++++ end for
++++++++ InvShiftRows(state)
++++++++ InvSubBytes(state)
++++++++ AddRoundKey(state, w[0, Nb-1])
++++++++ out = state
++++++++ end
++++++++ */
++++++++
++++++++ // Encrypt: AddRoundKey(state, w[0, Nb-1])
++++++++ // Decrypt: AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
++++++++ var Nr = w.length / 4 - 1;
++++++++ var m0, m1, m2, m3, sub;
++++++++ if(decrypt) {
++++++++ m0 = imix[0];
++++++++ m1 = imix[1];
++++++++ m2 = imix[2];
++++++++ m3 = imix[3];
++++++++ sub = isbox;
++++++++ } else {
++++++++ m0 = mix[0];
++++++++ m1 = mix[1];
++++++++ m2 = mix[2];
++++++++ m3 = mix[3];
++++++++ sub = sbox;
++++++++ }
++++++++ var a, b, c, d, a2, b2, c2;
++++++++ a = input[0] ^ w[0];
++++++++ b = input[decrypt ? 3 : 1] ^ w[1];
++++++++ c = input[2] ^ w[2];
++++++++ d = input[decrypt ? 1 : 3] ^ w[3];
++++++++ var i = 3;
++++++++
++++++++ /* In order to share code we follow the encryption algorithm when both
++++++++ encrypting and decrypting. To account for the changes required in the
++++++++ decryption algorithm, we use different lookup tables when decrypting
++++++++ and use a modified key schedule to account for the difference in the
++++++++ order of transformations applied when performing rounds. We also get
++++++++ key rounds in reverse order (relative to encryption). */
++++++++ for(var round = 1; round < Nr; ++round) {
++++++++ /* As described above, we'll be using table lookups to perform the
++++++++ column mixing. Each column is stored as a word in the state (the
++++++++ array 'input' has one column as a word at each index). In order to
++++++++ mix a column, we perform these transformations on each row in c,
++++++++ which is 1 byte in each word. The new column for c0 is c'0:
++++++++
++++++++ m0 m1 m2 m3
++++++++ r0,c'0 = 2*r0,c0 + 3*r1,c0 + 1*r2,c0 + 1*r3,c0
++++++++ r1,c'0 = 1*r0,c0 + 2*r1,c0 + 3*r2,c0 + 1*r3,c0
++++++++ r2,c'0 = 1*r0,c0 + 1*r1,c0 + 2*r2,c0 + 3*r3,c0
++++++++ r3,c'0 = 3*r0,c0 + 1*r1,c0 + 1*r2,c0 + 2*r3,c0
++++++++
++++++++ So using mix tables where c0 is a word with r0 being its upper
++++++++ 8 bits and r3 being its lower 8 bits:
++++++++
++++++++ m0[c0 >> 24] will yield this word: [2*r0,1*r0,1*r0,3*r0]
++++++++ ...
++++++++ m3[c0 & 255] will yield this word: [1*r3,1*r3,3*r3,2*r3]
++++++++
++++++++ Therefore to mix the columns in each word in the state we
++++++++ do the following (& 255 omitted for brevity):
++++++++ c'0,r0 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
++++++++ c'0,r1 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
++++++++ c'0,r2 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
++++++++ c'0,r3 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
++++++++
++++++++ However, before mixing, the algorithm requires us to perform
++++++++ ShiftRows(). The ShiftRows() transformation cyclically shifts the
++++++++ last 3 rows of the state over different offsets. The first row
++++++++ (r = 0) is not shifted.
++++++++
++++++++ s'_r,c = s_r,(c + shift(r, Nb) mod Nb
++++++++ for 0 < r < 4 and 0 <= c < Nb and
++++++++ shift(1, 4) = 1
++++++++ shift(2, 4) = 2
++++++++ shift(3, 4) = 3.
++++++++
++++++++ This causes the first byte in r = 1 to be moved to the end of
++++++++ the row, the first 2 bytes in r = 2 to be moved to the end of
++++++++ the row, the first 3 bytes in r = 3 to be moved to the end of
++++++++ the row:
++++++++
++++++++ r1: [c0 c1 c2 c3] => [c1 c2 c3 c0]
++++++++ r2: [c0 c1 c2 c3] [c2 c3 c0 c1]
++++++++ r3: [c0 c1 c2 c3] [c3 c0 c1 c2]
++++++++
++++++++ We can make these substitutions inline with our column mixing to
++++++++ generate an updated set of equations to produce each word in the
++++++++ state (note the columns have changed positions):
++++++++
++++++++ c0 c1 c2 c3 => c0 c1 c2 c3
++++++++ c0 c1 c2 c3 c1 c2 c3 c0 (cycled 1 byte)
++++++++ c0 c1 c2 c3 c2 c3 c0 c1 (cycled 2 bytes)
++++++++ c0 c1 c2 c3 c3 c0 c1 c2 (cycled 3 bytes)
++++++++
++++++++ Therefore:
++++++++
++++++++ c'0 = 2*r0,c0 + 3*r1,c1 + 1*r2,c2 + 1*r3,c3
++++++++ c'0 = 1*r0,c0 + 2*r1,c1 + 3*r2,c2 + 1*r3,c3
++++++++ c'0 = 1*r0,c0 + 1*r1,c1 + 2*r2,c2 + 3*r3,c3
++++++++ c'0 = 3*r0,c0 + 1*r1,c1 + 1*r2,c2 + 2*r3,c3
++++++++
++++++++ c'1 = 2*r0,c1 + 3*r1,c2 + 1*r2,c3 + 1*r3,c0
++++++++ c'1 = 1*r0,c1 + 2*r1,c2 + 3*r2,c3 + 1*r3,c0
++++++++ c'1 = 1*r0,c1 + 1*r1,c2 + 2*r2,c3 + 3*r3,c0
++++++++ c'1 = 3*r0,c1 + 1*r1,c2 + 1*r2,c3 + 2*r3,c0
++++++++
++++++++ ... and so forth for c'2 and c'3. The important distinction is
++++++++ that the columns are cycling, with c0 being used with the m0
++++++++ map when calculating c0, but c1 being used with the m0 map when
++++++++ calculating c1 ... and so forth.
++++++++
++++++++ When performing the inverse we transform the mirror image and
++++++++ skip the bottom row, instead of the top one, and move upwards:
++++++++
++++++++ c3 c2 c1 c0 => c0 c3 c2 c1 (cycled 3 bytes) *same as encryption
++++++++ c3 c2 c1 c0 c1 c0 c3 c2 (cycled 2 bytes)
++++++++ c3 c2 c1 c0 c2 c1 c0 c3 (cycled 1 byte) *same as encryption
++++++++ c3 c2 c1 c0 c3 c2 c1 c0
++++++++
++++++++ If you compare the resulting matrices for ShiftRows()+MixColumns()
++++++++ and for InvShiftRows()+InvMixColumns() the 2nd and 4th columns are
++++++++ different (in encrypt mode vs. decrypt mode). So in order to use
++++++++ the same code to handle both encryption and decryption, we will
++++++++ need to do some mapping.
++++++++
++++++++ If in encryption mode we let a=c0, b=c1, c=c2, d=c3, and r<N> be
++++++++ a row number in the state, then the resulting matrix in encryption
++++++++ mode for applying the above transformations would be:
++++++++
++++++++ r1: a b c d
++++++++ r2: b c d a
++++++++ r3: c d a b
++++++++ r4: d a b c
++++++++
++++++++ If we did the same in decryption mode we would get:
++++++++
++++++++ r1: a d c b
++++++++ r2: b a d c
++++++++ r3: c b a d
++++++++ r4: d c b a
++++++++
++++++++ If instead we swap d and b (set b=c3 and d=c1), then we get:
++++++++
++++++++ r1: a b c d
++++++++ r2: d a b c
++++++++ r3: c d a b
++++++++ r4: b c d a
++++++++
++++++++ Now the 1st and 3rd rows are the same as the encryption matrix. All
++++++++ we need to do then to make the mapping exactly the same is to swap
++++++++ the 2nd and 4th rows when in decryption mode. To do this without
++++++++ having to do it on each iteration, we swapped the 2nd and 4th rows
++++++++ in the decryption key schedule. We also have to do the swap above
++++++++ when we first pull in the input and when we set the final output. */
++++++++ a2 =
++++++++ m0[a >>> 24] ^
++++++++ m1[b >>> 16 & 255] ^
++++++++ m2[c >>> 8 & 255] ^
++++++++ m3[d & 255] ^ w[++i];
++++++++ b2 =
++++++++ m0[b >>> 24] ^
++++++++ m1[c >>> 16 & 255] ^
++++++++ m2[d >>> 8 & 255] ^
++++++++ m3[a & 255] ^ w[++i];
++++++++ c2 =
++++++++ m0[c >>> 24] ^
++++++++ m1[d >>> 16 & 255] ^
++++++++ m2[a >>> 8 & 255] ^
++++++++ m3[b & 255] ^ w[++i];
++++++++ d =
++++++++ m0[d >>> 24] ^
++++++++ m1[a >>> 16 & 255] ^
++++++++ m2[b >>> 8 & 255] ^
++++++++ m3[c & 255] ^ w[++i];
++++++++ a = a2;
++++++++ b = b2;
++++++++ c = c2;
++++++++ }
++++++++
++++++++ /*
++++++++ Encrypt:
++++++++ SubBytes(state)
++++++++ ShiftRows(state)
++++++++ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
++++++++
++++++++ Decrypt:
++++++++ InvShiftRows(state)
++++++++ InvSubBytes(state)
++++++++ AddRoundKey(state, w[0, Nb-1])
++++++++ */
++++++++ // Note: rows are shifted inline
++++++++ output[0] =
++++++++ (sub[a >>> 24] << 24) ^
++++++++ (sub[b >>> 16 & 255] << 16) ^
++++++++ (sub[c >>> 8 & 255] << 8) ^
++++++++ (sub[d & 255]) ^ w[++i];
++++++++ output[decrypt ? 3 : 1] =
++++++++ (sub[b >>> 24] << 24) ^
++++++++ (sub[c >>> 16 & 255] << 16) ^
++++++++ (sub[d >>> 8 & 255] << 8) ^
++++++++ (sub[a & 255]) ^ w[++i];
++++++++ output[2] =
++++++++ (sub[c >>> 24] << 24) ^
++++++++ (sub[d >>> 16 & 255] << 16) ^
++++++++ (sub[a >>> 8 & 255] << 8) ^
++++++++ (sub[b & 255]) ^ w[++i];
++++++++ output[decrypt ? 1 : 3] =
++++++++ (sub[d >>> 24] << 24) ^
++++++++ (sub[a >>> 16 & 255] << 16) ^
++++++++ (sub[b >>> 8 & 255] << 8) ^
++++++++ (sub[c & 255]) ^ w[++i];
++++++++}
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * forge.cipher.createCipher('AES-<mode>', key);
++++++++ * forge.cipher.createDecipher('AES-<mode>', key);
++++++++ *
++++++++ * Creates a deprecated AES cipher object. This object's mode will default to
++++++++ * CBC (cipher-block-chaining).
++++++++ *
++++++++ * The key and iv may be given as a string of bytes, an array of bytes, a
++++++++ * byte buffer, or an array of 32-bit words.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * key the symmetric key to use.
++++++++ * output the buffer to write to.
++++++++ * decrypt true for decryption, false for encryption.
++++++++ * mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++function _createCipher(options) {
++++++++ options = options || {};
++++++++ var mode = (options.mode || 'CBC').toUpperCase();
++++++++ var algorithm = 'AES-' + mode;
++++++++
++++++++ var cipher;
++++++++ if(options.decrypt) {
++++++++ cipher = forge.cipher.createDecipher(algorithm, options.key);
++++++++ } else {
++++++++ cipher = forge.cipher.createCipher(algorithm, options.key);
++++++++ }
++++++++
++++++++ // backwards compatible start API
++++++++ var start = cipher.start;
++++++++ cipher.start = function(iv, options) {
++++++++ // backwards compatibility: support second arg as output buffer
++++++++ var output = null;
++++++++ if(options instanceof forge.util.ByteBuffer) {
++++++++ output = options;
++++++++ options = {};
++++++++ }
++++++++ options = options || {};
++++++++ options.output = output;
++++++++ options.iv = iv;
++++++++ start.call(cipher, options);
++++++++ };
++++++++
++++++++ return cipher;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * A Javascript implementation of AES Cipher Suites for TLS.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2009-2015 Digital Bazaar, Inc.
++++++++ *
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./tls');
++++++++
++++++++var tls = module.exports = forge.tls;
++++++++
++++++++/**
++++++++ * Supported cipher suites.
++++++++ */
++++++++tls.CipherSuites['TLS_RSA_WITH_AES_128_CBC_SHA'] = {
++++++++ id: [0x00, 0x2f],
++++++++ name: 'TLS_RSA_WITH_AES_128_CBC_SHA',
++++++++ initSecurityParameters: function(sp) {
++++++++ sp.bulk_cipher_algorithm = tls.BulkCipherAlgorithm.aes;
++++++++ sp.cipher_type = tls.CipherType.block;
++++++++ sp.enc_key_length = 16;
++++++++ sp.block_length = 16;
++++++++ sp.fixed_iv_length = 16;
++++++++ sp.record_iv_length = 16;
++++++++ sp.mac_algorithm = tls.MACAlgorithm.hmac_sha1;
++++++++ sp.mac_length = 20;
++++++++ sp.mac_key_length = 20;
++++++++ },
++++++++ initConnectionState: initConnectionState
++++++++};
++++++++tls.CipherSuites['TLS_RSA_WITH_AES_256_CBC_SHA'] = {
++++++++ id: [0x00, 0x35],
++++++++ name: 'TLS_RSA_WITH_AES_256_CBC_SHA',
++++++++ initSecurityParameters: function(sp) {
++++++++ sp.bulk_cipher_algorithm = tls.BulkCipherAlgorithm.aes;
++++++++ sp.cipher_type = tls.CipherType.block;
++++++++ sp.enc_key_length = 32;
++++++++ sp.block_length = 16;
++++++++ sp.fixed_iv_length = 16;
++++++++ sp.record_iv_length = 16;
++++++++ sp.mac_algorithm = tls.MACAlgorithm.hmac_sha1;
++++++++ sp.mac_length = 20;
++++++++ sp.mac_key_length = 20;
++++++++ },
++++++++ initConnectionState: initConnectionState
++++++++};
++++++++
++++++++function initConnectionState(state, c, sp) {
++++++++ var client = (c.entity === forge.tls.ConnectionEnd.client);
++++++++
++++++++ // cipher setup
++++++++ state.read.cipherState = {
++++++++ init: false,
++++++++ cipher: forge.cipher.createDecipher('AES-CBC', client ?
++++++++ sp.keys.server_write_key : sp.keys.client_write_key),
++++++++ iv: client ? sp.keys.server_write_IV : sp.keys.client_write_IV
++++++++ };
++++++++ state.write.cipherState = {
++++++++ init: false,
++++++++ cipher: forge.cipher.createCipher('AES-CBC', client ?
++++++++ sp.keys.client_write_key : sp.keys.server_write_key),
++++++++ iv: client ? sp.keys.client_write_IV : sp.keys.server_write_IV
++++++++ };
++++++++ state.read.cipherFunction = decrypt_aes_cbc_sha1;
++++++++ state.write.cipherFunction = encrypt_aes_cbc_sha1;
++++++++
++++++++ // MAC setup
++++++++ state.read.macLength = state.write.macLength = sp.mac_length;
++++++++ state.read.macFunction = state.write.macFunction = tls.hmac_sha1;
++++++++}
++++++++
++++++++/**
++++++++ * Encrypts the TLSCompressed record into a TLSCipherText record using AES
++++++++ * in CBC mode.
++++++++ *
++++++++ * @param record the TLSCompressed record to encrypt.
++++++++ * @param s the ConnectionState to use.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++function encrypt_aes_cbc_sha1(record, s) {
++++++++ var rval = false;
++++++++
++++++++ // append MAC to fragment, update sequence number
++++++++ var mac = s.macFunction(s.macKey, s.sequenceNumber, record);
++++++++ record.fragment.putBytes(mac);
++++++++ s.updateSequenceNumber();
++++++++
++++++++ // TLS 1.1+ use an explicit IV every time to protect against CBC attacks
++++++++ var iv;
++++++++ if(record.version.minor === tls.Versions.TLS_1_0.minor) {
++++++++ // use the pre-generated IV when initializing for TLS 1.0, otherwise use
++++++++ // the residue from the previous encryption
++++++++ iv = s.cipherState.init ? null : s.cipherState.iv;
++++++++ } else {
++++++++ iv = forge.random.getBytesSync(16);
++++++++ }
++++++++
++++++++ s.cipherState.init = true;
++++++++
++++++++ // start cipher
++++++++ var cipher = s.cipherState.cipher;
++++++++ cipher.start({iv: iv});
++++++++
++++++++ // TLS 1.1+ write IV into output
++++++++ if(record.version.minor >= tls.Versions.TLS_1_1.minor) {
++++++++ cipher.output.putBytes(iv);
++++++++ }
++++++++
++++++++ // do encryption (default padding is appropriate)
++++++++ cipher.update(record.fragment);
++++++++ if(cipher.finish(encrypt_aes_cbc_sha1_padding)) {
++++++++ // set record fragment to encrypted output
++++++++ record.fragment = cipher.output;
++++++++ record.length = record.fragment.length();
++++++++ rval = true;
++++++++ }
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Handles padding for aes_cbc_sha1 in encrypt mode.
++++++++ *
++++++++ * @param blockSize the block size.
++++++++ * @param input the input buffer.
++++++++ * @param decrypt true in decrypt mode, false in encrypt mode.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++function encrypt_aes_cbc_sha1_padding(blockSize, input, decrypt) {
++++++++ /* The encrypted data length (TLSCiphertext.length) is one more than the sum
++++++++ of SecurityParameters.block_length, TLSCompressed.length,
++++++++ SecurityParameters.mac_length, and padding_length.
++++++++
++++++++ The padding may be any length up to 255 bytes long, as long as it results in
++++++++ the TLSCiphertext.length being an integral multiple of the block length.
++++++++ Lengths longer than necessary might be desirable to frustrate attacks on a
++++++++ protocol based on analysis of the lengths of exchanged messages. Each uint8
++++++++ in the padding data vector must be filled with the padding length value.
++++++++
++++++++ The padding length should be such that the total size of the
++++++++ GenericBlockCipher structure is a multiple of the cipher's block length.
++++++++ Legal values range from zero to 255, inclusive. This length specifies the
++++++++ length of the padding field exclusive of the padding_length field itself.
++++++++
++++++++ This is slightly different from PKCS#7 because the padding value is 1
++++++++ less than the actual number of padding bytes if you include the
++++++++ padding_length uint8 itself as a padding byte. */
++++++++ if(!decrypt) {
++++++++ // get the number of padding bytes required to reach the blockSize and
++++++++ // subtract 1 for the padding value (to make room for the padding_length
++++++++ // uint8)
++++++++ var padding = blockSize - (input.length() % blockSize);
++++++++ input.fillWithByte(padding - 1, padding);
++++++++ }
++++++++ return true;
++++++++}
++++++++
++++++++/**
++++++++ * Handles padding for aes_cbc_sha1 in decrypt mode.
++++++++ *
++++++++ * @param blockSize the block size.
++++++++ * @param output the output buffer.
++++++++ * @param decrypt true in decrypt mode, false in encrypt mode.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++function decrypt_aes_cbc_sha1_padding(blockSize, output, decrypt) {
++++++++ var rval = true;
++++++++ if(decrypt) {
++++++++ /* The last byte in the output specifies the number of padding bytes not
++++++++ including itself. Each of the padding bytes has the same value as that
++++++++ last byte (known as the padding_length). Here we check all padding
++++++++ bytes to ensure they have the value of padding_length even if one of
++++++++ them is bad in order to ward-off timing attacks. */
++++++++ var len = output.length();
++++++++ var paddingLength = output.last();
++++++++ for(var i = len - 1 - paddingLength; i < len - 1; ++i) {
++++++++ rval = rval && (output.at(i) == paddingLength);
++++++++ }
++++++++ if(rval) {
++++++++ // trim off padding bytes and last padding length byte
++++++++ output.truncate(paddingLength + 1);
++++++++ }
++++++++ }
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Decrypts a TLSCipherText record into a TLSCompressed record using
++++++++ * AES in CBC mode.
++++++++ *
++++++++ * @param record the TLSCipherText record to decrypt.
++++++++ * @param s the ConnectionState to use.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++function decrypt_aes_cbc_sha1(record, s) {
++++++++ var rval = false;
++++++++
++++++++ var iv;
++++++++ if(record.version.minor === tls.Versions.TLS_1_0.minor) {
++++++++ // use pre-generated IV when initializing for TLS 1.0, otherwise use the
++++++++ // residue from the previous decryption
++++++++ iv = s.cipherState.init ? null : s.cipherState.iv;
++++++++ } else {
++++++++ // TLS 1.1+ use an explicit IV every time to protect against CBC attacks
++++++++ // that is appended to the record fragment
++++++++ iv = record.fragment.getBytes(16);
++++++++ }
++++++++
++++++++ s.cipherState.init = true;
++++++++
++++++++ // start cipher
++++++++ var cipher = s.cipherState.cipher;
++++++++ cipher.start({iv: iv});
++++++++
++++++++ // do decryption
++++++++ cipher.update(record.fragment);
++++++++ rval = cipher.finish(decrypt_aes_cbc_sha1_padding);
++++++++
++++++++ // even if decryption fails, keep going to minimize timing attacks
++++++++
++++++++ // decrypted data:
++++++++ // first (len - 20) bytes = application data
++++++++ // last 20 bytes = MAC
++++++++ var macLen = s.macLength;
++++++++
++++++++ // create a random MAC to check against should the mac length check fail
++++++++ // Note: do this regardless of the failure to keep timing consistent
++++++++ var mac = forge.random.getBytesSync(macLen);
++++++++
++++++++ // get fragment and mac
++++++++ var len = cipher.output.length();
++++++++ if(len >= macLen) {
++++++++ record.fragment = cipher.output.getBytes(len - macLen);
++++++++ mac = cipher.output.getBytes(macLen);
++++++++ } else {
++++++++ // bad data, but get bytes anyway to try to keep timing consistent
++++++++ record.fragment = cipher.output.getBytes();
++++++++ }
++++++++ record.fragment = forge.util.createBuffer(record.fragment);
++++++++ record.length = record.fragment.length();
++++++++
++++++++ // see if data integrity checks out, update sequence number
++++++++ var mac2 = s.macFunction(s.macKey, s.sequenceNumber, record);
++++++++ s.updateSequenceNumber();
++++++++ rval = compareMacs(s.macKey, mac, mac2) && rval;
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Safely compare two MACs. This function will compare two MACs in a way
++++++++ * that protects against timing attacks.
++++++++ *
++++++++ * TODO: Expose elsewhere as a utility API.
++++++++ *
++++++++ * See: https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2011/february/double-hmac-verification/
++++++++ *
++++++++ * @param key the MAC key to use.
++++++++ * @param mac1 as a binary-encoded string of bytes.
++++++++ * @param mac2 as a binary-encoded string of bytes.
++++++++ *
++++++++ * @return true if the MACs are the same, false if not.
++++++++ */
++++++++function compareMacs(key, mac1, mac2) {
++++++++ var hmac = forge.hmac.create();
++++++++
++++++++ hmac.start('SHA1', key);
++++++++ hmac.update(mac1);
++++++++ mac1 = hmac.digest().getBytes();
++++++++
++++++++ hmac.start(null, null);
++++++++ hmac.update(mac2);
++++++++ mac2 = hmac.digest().getBytes();
++++++++
++++++++ return mac1 === mac2;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Copyright (c) 2019 Digital Bazaar, Inc.
++++++++ */
++++++++
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++var asn1 = forge.asn1;
++++++++
++++++++exports.privateKeyValidator = {
++++++++ // PrivateKeyInfo
++++++++ name: 'PrivateKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // Version (INTEGER)
++++++++ name: 'PrivateKeyInfo.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyVersion'
++++++++ }, {
++++++++ // privateKeyAlgorithm
++++++++ name: 'PrivateKeyInfo.privateKeyAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'privateKeyOid'
++++++++ }]
++++++++ }, {
++++++++ // PrivateKey
++++++++ name: 'PrivateKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'privateKey'
++++++++ }]
++++++++};
++++++++
++++++++exports.publicKeyValidator = {
++++++++ name: 'SubjectPublicKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'subjectPublicKeyInfo',
++++++++ value: [{
++++++++ name: 'SubjectPublicKeyInfo.AlgorithmIdentifier',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'publicKeyOid'
++++++++ }]
++++++++ },
++++++++ // capture group for ed25519PublicKey
++++++++ {
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ composed: true,
++++++++ captureBitStringValue: 'ed25519PublicKey'
++++++++ }
++++++++ // FIXME: this is capture group for rsaPublicKey, use it in this API or
++++++++ // discard?
++++++++ /* {
++++++++ // subjectPublicKey
++++++++ name: 'SubjectPublicKeyInfo.subjectPublicKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ value: [{
++++++++ // RSAPublicKey
++++++++ name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ captureAsn1: 'rsaPublicKey'
++++++++ }]
++++++++ } */
++++++++ ]
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of Abstract Syntax Notation Number One.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2015 Digital Bazaar, Inc.
++++++++ *
++++++++ * An API for storing data using the Abstract Syntax Notation Number One
++++++++ * format using DER (Distinguished Encoding Rules) encoding. This encoding is
++++++++ * commonly used to store data for PKI, i.e. X.509 Certificates, and this
++++++++ * implementation exists for that purpose.
++++++++ *
++++++++ * Abstract Syntax Notation Number One (ASN.1) is used to define the abstract
++++++++ * syntax of information without restricting the way the information is encoded
++++++++ * for transmission. It provides a standard that allows for open systems
++++++++ * communication. ASN.1 defines the syntax of information data and a number of
++++++++ * simple data types as well as a notation for describing them and specifying
++++++++ * values for them.
++++++++ *
++++++++ * The RSA algorithm creates public and private keys that are often stored in
++++++++ * X.509 or PKCS#X formats -- which use ASN.1 (encoded in DER format). This
++++++++ * class provides the most basic functionality required to store and load DSA
++++++++ * keys that are encoded according to ASN.1.
++++++++ *
++++++++ * The most common binary encodings for ASN.1 are BER (Basic Encoding Rules)
++++++++ * and DER (Distinguished Encoding Rules). DER is just a subset of BER that
++++++++ * has stricter requirements for how data must be encoded.
++++++++ *
++++++++ * Each ASN.1 structure has a tag (a byte identifying the ASN.1 structure type)
++++++++ * and a byte array for the value of this ASN1 structure which may be data or a
++++++++ * list of ASN.1 structures.
++++++++ *
++++++++ * Each ASN.1 structure using BER is (Tag-Length-Value):
++++++++ *
++++++++ * | byte 0 | bytes X | bytes Y |
++++++++ * |--------|---------|----------
++++++++ * | tag | length | value |
++++++++ *
++++++++ * ASN.1 allows for tags to be of "High-tag-number form" which allows a tag to
++++++++ * be two or more octets, but that is not supported by this class. A tag is
++++++++ * only 1 byte. Bits 1-5 give the tag number (ie the data type within a
++++++++ * particular 'class'), 6 indicates whether or not the ASN.1 value is
++++++++ * constructed from other ASN.1 values, and bits 7 and 8 give the 'class'. If
++++++++ * bits 7 and 8 are both zero, the class is UNIVERSAL. If only bit 7 is set,
++++++++ * then the class is APPLICATION. If only bit 8 is set, then the class is
++++++++ * CONTEXT_SPECIFIC. If both bits 7 and 8 are set, then the class is PRIVATE.
++++++++ * The tag numbers for the data types for the class UNIVERSAL are listed below:
++++++++ *
++++++++ * UNIVERSAL 0 Reserved for use by the encoding rules
++++++++ * UNIVERSAL 1 Boolean type
++++++++ * UNIVERSAL 2 Integer type
++++++++ * UNIVERSAL 3 Bitstring type
++++++++ * UNIVERSAL 4 Octetstring type
++++++++ * UNIVERSAL 5 Null type
++++++++ * UNIVERSAL 6 Object identifier type
++++++++ * UNIVERSAL 7 Object descriptor type
++++++++ * UNIVERSAL 8 External type and Instance-of type
++++++++ * UNIVERSAL 9 Real type
++++++++ * UNIVERSAL 10 Enumerated type
++++++++ * UNIVERSAL 11 Embedded-pdv type
++++++++ * UNIVERSAL 12 UTF8String type
++++++++ * UNIVERSAL 13 Relative object identifier type
++++++++ * UNIVERSAL 14-15 Reserved for future editions
++++++++ * UNIVERSAL 16 Sequence and Sequence-of types
++++++++ * UNIVERSAL 17 Set and Set-of types
++++++++ * UNIVERSAL 18-22, 25-30 Character string types
++++++++ * UNIVERSAL 23-24 Time types
++++++++ *
++++++++ * The length of an ASN.1 structure is specified after the tag identifier.
++++++++ * There is a definite form and an indefinite form. The indefinite form may
++++++++ * be used if the encoding is constructed and not all immediately available.
++++++++ * The indefinite form is encoded using a length byte with only the 8th bit
++++++++ * set. The end of the constructed object is marked using end-of-contents
++++++++ * octets (two zero bytes).
++++++++ *
++++++++ * The definite form looks like this:
++++++++ *
++++++++ * The length may take up 1 or more bytes, it depends on the length of the
++++++++ * value of the ASN.1 structure. DER encoding requires that if the ASN.1
++++++++ * structure has a value that has a length greater than 127, more than 1 byte
++++++++ * will be used to store its length, otherwise just one byte will be used.
++++++++ * This is strict.
++++++++ *
++++++++ * In the case that the length of the ASN.1 value is less than 127, 1 octet
++++++++ * (byte) is used to store the "short form" length. The 8th bit has a value of
++++++++ * 0 indicating the length is "short form" and not "long form" and bits 7-1
++++++++ * give the length of the data. (The 8th bit is the left-most, most significant
++++++++ * bit: also known as big endian or network format).
++++++++ *
++++++++ * In the case that the length of the ASN.1 value is greater than 127, 2 to
++++++++ * 127 octets (bytes) are used to store the "long form" length. The first
++++++++ * byte's 8th bit is set to 1 to indicate the length is "long form." Bits 7-1
++++++++ * give the number of additional octets. All following octets are in base 256
++++++++ * with the most significant digit first (typical big-endian binary unsigned
++++++++ * integer storage). So, for instance, if the length of a value was 257, the
++++++++ * first byte would be set to:
++++++++ *
++++++++ * 10000010 = 130 = 0x82.
++++++++ *
++++++++ * This indicates there are 2 octets (base 256) for the length. The second and
++++++++ * third bytes (the octets just mentioned) would store the length in base 256:
++++++++ *
++++++++ * octet 2: 00000001 = 1 * 256^1 = 256
++++++++ * octet 3: 00000001 = 1 * 256^0 = 1
++++++++ * total = 257
++++++++ *
++++++++ * The algorithm for converting a js integer value of 257 to base-256 is:
++++++++ *
++++++++ * var value = 257;
++++++++ * var bytes = [];
++++++++ * bytes[0] = (value >>> 8) & 0xFF; // most significant byte first
++++++++ * bytes[1] = value & 0xFF; // least significant byte last
++++++++ *
++++++++ * On the ASN.1 UNIVERSAL Object Identifier (OID) type:
++++++++ *
++++++++ * An OID can be written like: "value1.value2.value3...valueN"
++++++++ *
++++++++ * The DER encoding rules:
++++++++ *
++++++++ * The first byte has the value 40 * value1 + value2.
++++++++ * The following bytes, if any, encode the remaining values. Each value is
++++++++ * encoded in base 128, most significant digit first (big endian), with as
++++++++ * few digits as possible, and the most significant bit of each byte set
++++++++ * to 1 except the last in each value's encoding. For example: Given the
++++++++ * OID "1.2.840.113549", its DER encoding is (remember each byte except the
++++++++ * last one in each encoding is OR'd with 0x80):
++++++++ *
++++++++ * byte 1: 40 * 1 + 2 = 42 = 0x2A.
++++++++ * bytes 2-3: 128 * 6 + 72 = 840 = 6 72 = 6 72 = 0x0648 = 0x8648
++++++++ * bytes 4-6: 16384 * 6 + 128 * 119 + 13 = 6 119 13 = 0x06770D = 0x86F70D
++++++++ *
++++++++ * The final value is: 0x2A864886F70D.
++++++++ * The full OID (including ASN.1 tag and length of 6 bytes) is:
++++++++ * 0x06062A864886F70D
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++require('./oids');
++++++++
++++++++/* ASN.1 API */
++++++++var asn1 = module.exports = forge.asn1 = forge.asn1 || {};
++++++++
++++++++/**
++++++++ * ASN.1 classes.
++++++++ */
++++++++asn1.Class = {
++++++++ UNIVERSAL: 0x00,
++++++++ APPLICATION: 0x40,
++++++++ CONTEXT_SPECIFIC: 0x80,
++++++++ PRIVATE: 0xC0
++++++++};
++++++++
++++++++/**
++++++++ * ASN.1 types. Not all types are supported by this implementation, only
++++++++ * those necessary to implement a simple PKI are implemented.
++++++++ */
++++++++asn1.Type = {
++++++++ NONE: 0,
++++++++ BOOLEAN: 1,
++++++++ INTEGER: 2,
++++++++ BITSTRING: 3,
++++++++ OCTETSTRING: 4,
++++++++ NULL: 5,
++++++++ OID: 6,
++++++++ ODESC: 7,
++++++++ EXTERNAL: 8,
++++++++ REAL: 9,
++++++++ ENUMERATED: 10,
++++++++ EMBEDDED: 11,
++++++++ UTF8: 12,
++++++++ ROID: 13,
++++++++ SEQUENCE: 16,
++++++++ SET: 17,
++++++++ PRINTABLESTRING: 19,
++++++++ IA5STRING: 22,
++++++++ UTCTIME: 23,
++++++++ GENERALIZEDTIME: 24,
++++++++ BMPSTRING: 30
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new asn1 object.
++++++++ *
++++++++ * @param tagClass the tag class for the object.
++++++++ * @param type the data type (tag number) for the object.
++++++++ * @param constructed true if the asn1 object is in constructed form.
++++++++ * @param value the value for the object, if it is not constructed.
++++++++ * @param [options] the options to use:
++++++++ * [bitStringContents] the plain BIT STRING content including padding
++++++++ * byte.
++++++++ *
++++++++ * @return the asn1 object.
++++++++ */
++++++++asn1.create = function(tagClass, type, constructed, value, options) {
++++++++ /* An asn1 object has a tagClass, a type, a constructed flag, and a
++++++++ value. The value's type depends on the constructed flag. If
++++++++ constructed, it will contain a list of other asn1 objects. If not,
++++++++ it will contain the ASN.1 value as an array of bytes formatted
++++++++ according to the ASN.1 data type. */
++++++++
++++++++ // remove undefined values
++++++++ if(forge.util.isArray(value)) {
++++++++ var tmp = [];
++++++++ for(var i = 0; i < value.length; ++i) {
++++++++ if(value[i] !== undefined) {
++++++++ tmp.push(value[i]);
++++++++ }
++++++++ }
++++++++ value = tmp;
++++++++ }
++++++++
++++++++ var obj = {
++++++++ tagClass: tagClass,
++++++++ type: type,
++++++++ constructed: constructed,
++++++++ composed: constructed || forge.util.isArray(value),
++++++++ value: value
++++++++ };
++++++++ if(options && 'bitStringContents' in options) {
++++++++ // TODO: copy byte buffer if it's a buffer not a string
++++++++ obj.bitStringContents = options.bitStringContents;
++++++++ // TODO: add readonly flag to avoid this overhead
++++++++ // save copy to detect changes
++++++++ obj.original = asn1.copy(obj);
++++++++ }
++++++++ return obj;
++++++++};
++++++++
++++++++/**
++++++++ * Copies an asn1 object.
++++++++ *
++++++++ * @param obj the asn1 object.
++++++++ * @param [options] copy options:
++++++++ * [excludeBitStringContents] true to not copy bitStringContents
++++++++ *
++++++++ * @return the a copy of the asn1 object.
++++++++ */
++++++++asn1.copy = function(obj, options) {
++++++++ var copy;
++++++++
++++++++ if(forge.util.isArray(obj)) {
++++++++ copy = [];
++++++++ for(var i = 0; i < obj.length; ++i) {
++++++++ copy.push(asn1.copy(obj[i], options));
++++++++ }
++++++++ return copy;
++++++++ }
++++++++
++++++++ if(typeof obj === 'string') {
++++++++ // TODO: copy byte buffer if it's a buffer not a string
++++++++ return obj;
++++++++ }
++++++++
++++++++ copy = {
++++++++ tagClass: obj.tagClass,
++++++++ type: obj.type,
++++++++ constructed: obj.constructed,
++++++++ composed: obj.composed,
++++++++ value: asn1.copy(obj.value, options)
++++++++ };
++++++++ if(options && !options.excludeBitStringContents) {
++++++++ // TODO: copy byte buffer if it's a buffer not a string
++++++++ copy.bitStringContents = obj.bitStringContents;
++++++++ }
++++++++ return copy;
++++++++};
++++++++
++++++++/**
++++++++ * Compares asn1 objects for equality.
++++++++ *
++++++++ * Note this function does not run in constant time.
++++++++ *
++++++++ * @param obj1 the first asn1 object.
++++++++ * @param obj2 the second asn1 object.
++++++++ * @param [options] compare options:
++++++++ * [includeBitStringContents] true to compare bitStringContents
++++++++ *
++++++++ * @return true if the asn1 objects are equal.
++++++++ */
++++++++asn1.equals = function(obj1, obj2, options) {
++++++++ if(forge.util.isArray(obj1)) {
++++++++ if(!forge.util.isArray(obj2)) {
++++++++ return false;
++++++++ }
++++++++ if(obj1.length !== obj2.length) {
++++++++ return false;
++++++++ }
++++++++ for(var i = 0; i < obj1.length; ++i) {
++++++++ if(!asn1.equals(obj1[i], obj2[i])) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++ return true;
++++++++ }
++++++++
++++++++ if(typeof obj1 !== typeof obj2) {
++++++++ return false;
++++++++ }
++++++++
++++++++ if(typeof obj1 === 'string') {
++++++++ return obj1 === obj2;
++++++++ }
++++++++
++++++++ var equal = obj1.tagClass === obj2.tagClass &&
++++++++ obj1.type === obj2.type &&
++++++++ obj1.constructed === obj2.constructed &&
++++++++ obj1.composed === obj2.composed &&
++++++++ asn1.equals(obj1.value, obj2.value);
++++++++ if(options && options.includeBitStringContents) {
++++++++ equal = equal && (obj1.bitStringContents === obj2.bitStringContents);
++++++++ }
++++++++
++++++++ return equal;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the length of a BER-encoded ASN.1 value.
++++++++ *
++++++++ * In case the length is not specified, undefined is returned.
++++++++ *
++++++++ * @param b the BER-encoded ASN.1 byte buffer, starting with the first
++++++++ * length byte.
++++++++ *
++++++++ * @return the length of the BER-encoded ASN.1 value or undefined.
++++++++ */
++++++++asn1.getBerValueLength = function(b) {
++++++++ // TODO: move this function and related DER/BER functions to a der.js
++++++++ // file; better abstract ASN.1 away from der/ber.
++++++++ var b2 = b.getByte();
++++++++ if(b2 === 0x80) {
++++++++ return undefined;
++++++++ }
++++++++
++++++++ // see if the length is "short form" or "long form" (bit 8 set)
++++++++ var length;
++++++++ var longForm = b2 & 0x80;
++++++++ if(!longForm) {
++++++++ // length is just the first byte
++++++++ length = b2;
++++++++ } else {
++++++++ // the number of bytes the length is specified in bits 7 through 1
++++++++ // and each length byte is in big-endian base-256
++++++++ length = b.getInt((b2 & 0x7F) << 3);
++++++++ }
++++++++ return length;
++++++++};
++++++++
++++++++/**
++++++++ * Check if the byte buffer has enough bytes. Throws an Error if not.
++++++++ *
++++++++ * @param bytes the byte buffer to parse from.
++++++++ * @param remaining the bytes remaining in the current parsing state.
++++++++ * @param n the number of bytes the buffer must have.
++++++++ */
++++++++function _checkBufferLength(bytes, remaining, n) {
++++++++ if(n > remaining) {
++++++++ var error = new Error('Too few bytes to parse DER.');
++++++++ error.available = bytes.length();
++++++++ error.remaining = remaining;
++++++++ error.requested = n;
++++++++ throw error;
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Gets the length of a BER-encoded ASN.1 value.
++++++++ *
++++++++ * In case the length is not specified, undefined is returned.
++++++++ *
++++++++ * @param bytes the byte buffer to parse from.
++++++++ * @param remaining the bytes remaining in the current parsing state.
++++++++ *
++++++++ * @return the length of the BER-encoded ASN.1 value or undefined.
++++++++ */
++++++++var _getValueLength = function(bytes, remaining) {
++++++++ // TODO: move this function and related DER/BER functions to a der.js
++++++++ // file; better abstract ASN.1 away from der/ber.
++++++++ // fromDer already checked that this byte exists
++++++++ var b2 = bytes.getByte();
++++++++ remaining--;
++++++++ if(b2 === 0x80) {
++++++++ return undefined;
++++++++ }
++++++++
++++++++ // see if the length is "short form" or "long form" (bit 8 set)
++++++++ var length;
++++++++ var longForm = b2 & 0x80;
++++++++ if(!longForm) {
++++++++ // length is just the first byte
++++++++ length = b2;
++++++++ } else {
++++++++ // the number of bytes the length is specified in bits 7 through 1
++++++++ // and each length byte is in big-endian base-256
++++++++ var longFormBytes = b2 & 0x7F;
++++++++ _checkBufferLength(bytes, remaining, longFormBytes);
++++++++ length = bytes.getInt(longFormBytes << 3);
++++++++ }
++++++++ // FIXME: this will only happen for 32 bit getInt with high bit set
++++++++ if(length < 0) {
++++++++ throw new Error('Negative length: ' + length);
++++++++ }
++++++++ return length;
++++++++};
++++++++
++++++++/**
++++++++ * Parses an asn1 object from a byte buffer in DER format.
++++++++ *
++++++++ * @param bytes the byte buffer to parse from.
++++++++ * @param [strict] true to be strict when checking value lengths, false to
++++++++ * allow truncated values (default: true).
++++++++ * @param [options] object with options or boolean strict flag
++++++++ * [strict] true to be strict when checking value lengths, false to
++++++++ * allow truncated values (default: true).
++++++++ * [parseAllBytes] true to ensure all bytes are parsed
++++++++ * (default: true)
++++++++ * [decodeBitStrings] true to attempt to decode the content of
++++++++ * BIT STRINGs (not OCTET STRINGs) using strict mode. Note that
++++++++ * without schema support to understand the data context this can
++++++++ * erroneously decode values that happen to be valid ASN.1. This
++++++++ * flag will be deprecated or removed as soon as schema support is
++++++++ * available. (default: true)
++++++++ *
++++++++ * @throws Will throw an error for various malformed input conditions.
++++++++ *
++++++++ * @return the parsed asn1 object.
++++++++ */
++++++++asn1.fromDer = function(bytes, options) {
++++++++ if(options === undefined) {
++++++++ options = {
++++++++ strict: true,
++++++++ parseAllBytes: true,
++++++++ decodeBitStrings: true
++++++++ };
++++++++ }
++++++++ if(typeof options === 'boolean') {
++++++++ options = {
++++++++ strict: options,
++++++++ parseAllBytes: true,
++++++++ decodeBitStrings: true
++++++++ };
++++++++ }
++++++++ if(!('strict' in options)) {
++++++++ options.strict = true;
++++++++ }
++++++++ if(!('parseAllBytes' in options)) {
++++++++ options.parseAllBytes = true;
++++++++ }
++++++++ if(!('decodeBitStrings' in options)) {
++++++++ options.decodeBitStrings = true;
++++++++ }
++++++++
++++++++ // wrap in buffer if needed
++++++++ if(typeof bytes === 'string') {
++++++++ bytes = forge.util.createBuffer(bytes);
++++++++ }
++++++++
++++++++ var byteCount = bytes.length();
++++++++ var value = _fromDer(bytes, bytes.length(), 0, options);
++++++++ if(options.parseAllBytes && bytes.length() !== 0) {
++++++++ var error = new Error('Unparsed DER bytes remain after ASN.1 parsing.');
++++++++ error.byteCount = byteCount;
++++++++ error.remaining = bytes.length();
++++++++ throw error;
++++++++ }
++++++++ return value;
++++++++};
++++++++
++++++++/**
++++++++ * Internal function to parse an asn1 object from a byte buffer in DER format.
++++++++ *
++++++++ * @param bytes the byte buffer to parse from.
++++++++ * @param remaining the number of bytes remaining for this chunk.
++++++++ * @param depth the current parsing depth.
++++++++ * @param options object with same options as fromDer().
++++++++ *
++++++++ * @return the parsed asn1 object.
++++++++ */
++++++++function _fromDer(bytes, remaining, depth, options) {
++++++++ // temporary storage for consumption calculations
++++++++ var start;
++++++++
++++++++ // minimum length for ASN.1 DER structure is 2
++++++++ _checkBufferLength(bytes, remaining, 2);
++++++++
++++++++ // get the first byte
++++++++ var b1 = bytes.getByte();
++++++++ // consumed one byte
++++++++ remaining--;
++++++++
++++++++ // get the tag class
++++++++ var tagClass = (b1 & 0xC0);
++++++++
++++++++ // get the type (bits 1-5)
++++++++ var type = b1 & 0x1F;
++++++++
++++++++ // get the variable value length and adjust remaining bytes
++++++++ start = bytes.length();
++++++++ var length = _getValueLength(bytes, remaining);
++++++++ remaining -= start - bytes.length();
++++++++
++++++++ // ensure there are enough bytes to get the value
++++++++ if(length !== undefined && length > remaining) {
++++++++ if(options.strict) {
++++++++ var error = new Error('Too few bytes to read ASN.1 value.');
++++++++ error.available = bytes.length();
++++++++ error.remaining = remaining;
++++++++ error.requested = length;
++++++++ throw error;
++++++++ }
++++++++ // Note: be lenient with truncated values and use remaining state bytes
++++++++ length = remaining;
++++++++ }
++++++++
++++++++ // value storage
++++++++ var value;
++++++++ // possible BIT STRING contents storage
++++++++ var bitStringContents;
++++++++
++++++++ // constructed flag is bit 6 (32 = 0x20) of the first byte
++++++++ var constructed = ((b1 & 0x20) === 0x20);
++++++++ if(constructed) {
++++++++ // parse child asn1 objects from the value
++++++++ value = [];
++++++++ if(length === undefined) {
++++++++ // asn1 object of indefinite length, read until end tag
++++++++ for(;;) {
++++++++ _checkBufferLength(bytes, remaining, 2);
++++++++ if(bytes.bytes(2) === String.fromCharCode(0, 0)) {
++++++++ bytes.getBytes(2);
++++++++ remaining -= 2;
++++++++ break;
++++++++ }
++++++++ start = bytes.length();
++++++++ value.push(_fromDer(bytes, remaining, depth + 1, options));
++++++++ remaining -= start - bytes.length();
++++++++ }
++++++++ } else {
++++++++ // parsing asn1 object of definite length
++++++++ while(length > 0) {
++++++++ start = bytes.length();
++++++++ value.push(_fromDer(bytes, length, depth + 1, options));
++++++++ remaining -= start - bytes.length();
++++++++ length -= start - bytes.length();
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // if a BIT STRING, save the contents including padding
++++++++ if(value === undefined && tagClass === asn1.Class.UNIVERSAL &&
++++++++ type === asn1.Type.BITSTRING) {
++++++++ bitStringContents = bytes.bytes(length);
++++++++ }
++++++++
++++++++ // determine if a non-constructed value should be decoded as a composed
++++++++ // value that contains other ASN.1 objects. BIT STRINGs (and OCTET STRINGs)
++++++++ // can be used this way.
++++++++ if(value === undefined && options.decodeBitStrings &&
++++++++ tagClass === asn1.Class.UNIVERSAL &&
++++++++ // FIXME: OCTET STRINGs not yet supported here
++++++++ // .. other parts of forge expect to decode OCTET STRINGs manually
++++++++ (type === asn1.Type.BITSTRING /*|| type === asn1.Type.OCTETSTRING*/) &&
++++++++ length > 1) {
++++++++ // save read position
++++++++ var savedRead = bytes.read;
++++++++ var savedRemaining = remaining;
++++++++ var unused = 0;
++++++++ if(type === asn1.Type.BITSTRING) {
++++++++ /* The first octet gives the number of bits by which the length of the
++++++++ bit string is less than the next multiple of eight (this is called
++++++++ the "number of unused bits").
++++++++
++++++++ The second and following octets give the value of the bit string
++++++++ converted to an octet string. */
++++++++ _checkBufferLength(bytes, remaining, 1);
++++++++ unused = bytes.getByte();
++++++++ remaining--;
++++++++ }
++++++++ // if all bits are used, maybe the BIT/OCTET STRING holds ASN.1 objs
++++++++ if(unused === 0) {
++++++++ try {
++++++++ // attempt to parse child asn1 object from the value
++++++++ // (stored in array to signal composed value)
++++++++ start = bytes.length();
++++++++ var subOptions = {
++++++++ // enforce strict mode to avoid parsing ASN.1 from plain data
++++++++ strict: true,
++++++++ decodeBitStrings: true
++++++++ };
++++++++ var composed = _fromDer(bytes, remaining, depth + 1, subOptions);
++++++++ var used = start - bytes.length();
++++++++ remaining -= used;
++++++++ if(type == asn1.Type.BITSTRING) {
++++++++ used++;
++++++++ }
++++++++
++++++++ // if the data all decoded and the class indicates UNIVERSAL or
++++++++ // CONTEXT_SPECIFIC then assume we've got an encapsulated ASN.1 object
++++++++ var tc = composed.tagClass;
++++++++ if(used === length &&
++++++++ (tc === asn1.Class.UNIVERSAL || tc === asn1.Class.CONTEXT_SPECIFIC)) {
++++++++ value = [composed];
++++++++ }
++++++++ } catch(ex) {
++++++++ }
++++++++ }
++++++++ if(value === undefined) {
++++++++ // restore read position
++++++++ bytes.read = savedRead;
++++++++ remaining = savedRemaining;
++++++++ }
++++++++ }
++++++++
++++++++ if(value === undefined) {
++++++++ // asn1 not constructed or composed, get raw value
++++++++ // TODO: do DER to OID conversion and vice-versa in .toDer?
++++++++
++++++++ if(length === undefined) {
++++++++ if(options.strict) {
++++++++ throw new Error('Non-constructed ASN.1 object of indefinite length.');
++++++++ }
++++++++ // be lenient and use remaining state bytes
++++++++ length = remaining;
++++++++ }
++++++++
++++++++ if(type === asn1.Type.BMPSTRING) {
++++++++ value = '';
++++++++ for(; length > 0; length -= 2) {
++++++++ _checkBufferLength(bytes, remaining, 2);
++++++++ value += String.fromCharCode(bytes.getInt16());
++++++++ remaining -= 2;
++++++++ }
++++++++ } else {
++++++++ value = bytes.getBytes(length);
++++++++ remaining -= length;
++++++++ }
++++++++ }
++++++++
++++++++ // add BIT STRING contents if available
++++++++ var asn1Options = bitStringContents === undefined ? null : {
++++++++ bitStringContents: bitStringContents
++++++++ };
++++++++
++++++++ // create and return asn1 object
++++++++ return asn1.create(tagClass, type, constructed, value, asn1Options);
++++++++}
++++++++
++++++++/**
++++++++ * Converts the given asn1 object to a buffer of bytes in DER format.
++++++++ *
++++++++ * @param asn1 the asn1 object to convert to bytes.
++++++++ *
++++++++ * @return the buffer of bytes.
++++++++ */
++++++++asn1.toDer = function(obj) {
++++++++ var bytes = forge.util.createBuffer();
++++++++
++++++++ // build the first byte
++++++++ var b1 = obj.tagClass | obj.type;
++++++++
++++++++ // for storing the ASN.1 value
++++++++ var value = forge.util.createBuffer();
++++++++
++++++++ // use BIT STRING contents if available and data not changed
++++++++ var useBitStringContents = false;
++++++++ if('bitStringContents' in obj) {
++++++++ useBitStringContents = true;
++++++++ if(obj.original) {
++++++++ useBitStringContents = asn1.equals(obj, obj.original);
++++++++ }
++++++++ }
++++++++
++++++++ if(useBitStringContents) {
++++++++ value.putBytes(obj.bitStringContents);
++++++++ } else if(obj.composed) {
++++++++ // if composed, use each child asn1 object's DER bytes as value
++++++++ // turn on 6th bit (0x20 = 32) to indicate asn1 is constructed
++++++++ // from other asn1 objects
++++++++ if(obj.constructed) {
++++++++ b1 |= 0x20;
++++++++ } else {
++++++++ // type is a bit string, add unused bits of 0x00
++++++++ value.putByte(0x00);
++++++++ }
++++++++
++++++++ // add all of the child DER bytes together
++++++++ for(var i = 0; i < obj.value.length; ++i) {
++++++++ if(obj.value[i] !== undefined) {
++++++++ value.putBuffer(asn1.toDer(obj.value[i]));
++++++++ }
++++++++ }
++++++++ } else {
++++++++ // use asn1.value directly
++++++++ if(obj.type === asn1.Type.BMPSTRING) {
++++++++ for(var i = 0; i < obj.value.length; ++i) {
++++++++ value.putInt16(obj.value.charCodeAt(i));
++++++++ }
++++++++ } else {
++++++++ // ensure integer is minimally-encoded
++++++++ // TODO: should all leading bytes be stripped vs just one?
++++++++ // .. ex '00 00 01' => '01'?
++++++++ if(obj.type === asn1.Type.INTEGER &&
++++++++ obj.value.length > 1 &&
++++++++ // leading 0x00 for positive integer
++++++++ ((obj.value.charCodeAt(0) === 0 &&
++++++++ (obj.value.charCodeAt(1) & 0x80) === 0) ||
++++++++ // leading 0xFF for negative integer
++++++++ (obj.value.charCodeAt(0) === 0xFF &&
++++++++ (obj.value.charCodeAt(1) & 0x80) === 0x80))) {
++++++++ value.putBytes(obj.value.substr(1));
++++++++ } else {
++++++++ value.putBytes(obj.value);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // add tag byte
++++++++ bytes.putByte(b1);
++++++++
++++++++ // use "short form" encoding
++++++++ if(value.length() <= 127) {
++++++++ // one byte describes the length
++++++++ // bit 8 = 0 and bits 7-1 = length
++++++++ bytes.putByte(value.length() & 0x7F);
++++++++ } else {
++++++++ // use "long form" encoding
++++++++ // 2 to 127 bytes describe the length
++++++++ // first byte: bit 8 = 1 and bits 7-1 = # of additional bytes
++++++++ // other bytes: length in base 256, big-endian
++++++++ var len = value.length();
++++++++ var lenBytes = '';
++++++++ do {
++++++++ lenBytes += String.fromCharCode(len & 0xFF);
++++++++ len = len >>> 8;
++++++++ } while(len > 0);
++++++++
++++++++ // set first byte to # bytes used to store the length and turn on
++++++++ // bit 8 to indicate long-form length is used
++++++++ bytes.putByte(lenBytes.length | 0x80);
++++++++
++++++++ // concatenate length bytes in reverse since they were generated
++++++++ // little endian and we need big endian
++++++++ for(var i = lenBytes.length - 1; i >= 0; --i) {
++++++++ bytes.putByte(lenBytes.charCodeAt(i));
++++++++ }
++++++++ }
++++++++
++++++++ // concatenate value bytes
++++++++ bytes.putBuffer(value);
++++++++ return bytes;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an OID dot-separated string to a byte buffer. The byte buffer
++++++++ * contains only the DER-encoded value, not any tag or length bytes.
++++++++ *
++++++++ * @param oid the OID dot-separated string.
++++++++ *
++++++++ * @return the byte buffer.
++++++++ */
++++++++asn1.oidToDer = function(oid) {
++++++++ // split OID into individual values
++++++++ var values = oid.split('.');
++++++++ var bytes = forge.util.createBuffer();
++++++++
++++++++ // first byte is 40 * value1 + value2
++++++++ bytes.putByte(40 * parseInt(values[0], 10) + parseInt(values[1], 10));
++++++++ // other bytes are each value in base 128 with 8th bit set except for
++++++++ // the last byte for each value
++++++++ var last, valueBytes, value, b;
++++++++ for(var i = 2; i < values.length; ++i) {
++++++++ // produce value bytes in reverse because we don't know how many
++++++++ // bytes it will take to store the value
++++++++ last = true;
++++++++ valueBytes = [];
++++++++ value = parseInt(values[i], 10);
++++++++ do {
++++++++ b = value & 0x7F;
++++++++ value = value >>> 7;
++++++++ // if value is not last, then turn on 8th bit
++++++++ if(!last) {
++++++++ b |= 0x80;
++++++++ }
++++++++ valueBytes.push(b);
++++++++ last = false;
++++++++ } while(value > 0);
++++++++
++++++++ // add value bytes in reverse (needs to be in big endian)
++++++++ for(var n = valueBytes.length - 1; n >= 0; --n) {
++++++++ bytes.putByte(valueBytes[n]);
++++++++ }
++++++++ }
++++++++
++++++++ return bytes;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a DER-encoded byte buffer to an OID dot-separated string. The
++++++++ * byte buffer should contain only the DER-encoded value, not any tag or
++++++++ * length bytes.
++++++++ *
++++++++ * @param bytes the byte buffer.
++++++++ *
++++++++ * @return the OID dot-separated string.
++++++++ */
++++++++asn1.derToOid = function(bytes) {
++++++++ var oid;
++++++++
++++++++ // wrap in buffer if needed
++++++++ if(typeof bytes === 'string') {
++++++++ bytes = forge.util.createBuffer(bytes);
++++++++ }
++++++++
++++++++ // first byte is 40 * value1 + value2
++++++++ var b = bytes.getByte();
++++++++ oid = Math.floor(b / 40) + '.' + (b % 40);
++++++++
++++++++ // other bytes are each value in base 128 with 8th bit set except for
++++++++ // the last byte for each value
++++++++ var value = 0;
++++++++ while(bytes.length() > 0) {
++++++++ b = bytes.getByte();
++++++++ value = value << 7;
++++++++ // not the last byte for the value
++++++++ if(b & 0x80) {
++++++++ value += b & 0x7F;
++++++++ } else {
++++++++ // last byte
++++++++ oid += '.' + (value + b);
++++++++ value = 0;
++++++++ }
++++++++ }
++++++++
++++++++ return oid;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a UTCTime value to a date.
++++++++ *
++++++++ * Note: GeneralizedTime has 4 digits for the year and is used for X.509
++++++++ * dates past 2049. Parsing that structure hasn't been implemented yet.
++++++++ *
++++++++ * @param utc the UTCTime value to convert.
++++++++ *
++++++++ * @return the date.
++++++++ */
++++++++asn1.utcTimeToDate = function(utc) {
++++++++ /* The following formats can be used:
++++++++
++++++++ YYMMDDhhmmZ
++++++++ YYMMDDhhmm+hh'mm'
++++++++ YYMMDDhhmm-hh'mm'
++++++++ YYMMDDhhmmssZ
++++++++ YYMMDDhhmmss+hh'mm'
++++++++ YYMMDDhhmmss-hh'mm'
++++++++
++++++++ Where:
++++++++
++++++++ YY is the least significant two digits of the year
++++++++ MM is the month (01 to 12)
++++++++ DD is the day (01 to 31)
++++++++ hh is the hour (00 to 23)
++++++++ mm are the minutes (00 to 59)
++++++++ ss are the seconds (00 to 59)
++++++++ Z indicates that local time is GMT, + indicates that local time is
++++++++ later than GMT, and - indicates that local time is earlier than GMT
++++++++ hh' is the absolute value of the offset from GMT in hours
++++++++ mm' is the absolute value of the offset from GMT in minutes */
++++++++ var date = new Date();
++++++++
++++++++ // if YY >= 50 use 19xx, if YY < 50 use 20xx
++++++++ var year = parseInt(utc.substr(0, 2), 10);
++++++++ year = (year >= 50) ? 1900 + year : 2000 + year;
++++++++ var MM = parseInt(utc.substr(2, 2), 10) - 1; // use 0-11 for month
++++++++ var DD = parseInt(utc.substr(4, 2), 10);
++++++++ var hh = parseInt(utc.substr(6, 2), 10);
++++++++ var mm = parseInt(utc.substr(8, 2), 10);
++++++++ var ss = 0;
++++++++
++++++++ // not just YYMMDDhhmmZ
++++++++ if(utc.length > 11) {
++++++++ // get character after minutes
++++++++ var c = utc.charAt(10);
++++++++ var end = 10;
++++++++
++++++++ // see if seconds are present
++++++++ if(c !== '+' && c !== '-') {
++++++++ // get seconds
++++++++ ss = parseInt(utc.substr(10, 2), 10);
++++++++ end += 2;
++++++++ }
++++++++ }
++++++++
++++++++ // update date
++++++++ date.setUTCFullYear(year, MM, DD);
++++++++ date.setUTCHours(hh, mm, ss, 0);
++++++++
++++++++ if(end) {
++++++++ // get +/- after end of time
++++++++ c = utc.charAt(end);
++++++++ if(c === '+' || c === '-') {
++++++++ // get hours+minutes offset
++++++++ var hhoffset = parseInt(utc.substr(end + 1, 2), 10);
++++++++ var mmoffset = parseInt(utc.substr(end + 4, 2), 10);
++++++++
++++++++ // calculate offset in milliseconds
++++++++ var offset = hhoffset * 60 + mmoffset;
++++++++ offset *= 60000;
++++++++
++++++++ // apply offset
++++++++ if(c === '+') {
++++++++ date.setTime(+date - offset);
++++++++ } else {
++++++++ date.setTime(+date + offset);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return date;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a GeneralizedTime value to a date.
++++++++ *
++++++++ * @param gentime the GeneralizedTime value to convert.
++++++++ *
++++++++ * @return the date.
++++++++ */
++++++++asn1.generalizedTimeToDate = function(gentime) {
++++++++ /* The following formats can be used:
++++++++
++++++++ YYYYMMDDHHMMSS
++++++++ YYYYMMDDHHMMSS.fff
++++++++ YYYYMMDDHHMMSSZ
++++++++ YYYYMMDDHHMMSS.fffZ
++++++++ YYYYMMDDHHMMSS+hh'mm'
++++++++ YYYYMMDDHHMMSS.fff+hh'mm'
++++++++ YYYYMMDDHHMMSS-hh'mm'
++++++++ YYYYMMDDHHMMSS.fff-hh'mm'
++++++++
++++++++ Where:
++++++++
++++++++ YYYY is the year
++++++++ MM is the month (01 to 12)
++++++++ DD is the day (01 to 31)
++++++++ hh is the hour (00 to 23)
++++++++ mm are the minutes (00 to 59)
++++++++ ss are the seconds (00 to 59)
++++++++ .fff is the second fraction, accurate to three decimal places
++++++++ Z indicates that local time is GMT, + indicates that local time is
++++++++ later than GMT, and - indicates that local time is earlier than GMT
++++++++ hh' is the absolute value of the offset from GMT in hours
++++++++ mm' is the absolute value of the offset from GMT in minutes */
++++++++ var date = new Date();
++++++++
++++++++ var YYYY = parseInt(gentime.substr(0, 4), 10);
++++++++ var MM = parseInt(gentime.substr(4, 2), 10) - 1; // use 0-11 for month
++++++++ var DD = parseInt(gentime.substr(6, 2), 10);
++++++++ var hh = parseInt(gentime.substr(8, 2), 10);
++++++++ var mm = parseInt(gentime.substr(10, 2), 10);
++++++++ var ss = parseInt(gentime.substr(12, 2), 10);
++++++++ var fff = 0;
++++++++ var offset = 0;
++++++++ var isUTC = false;
++++++++
++++++++ if(gentime.charAt(gentime.length - 1) === 'Z') {
++++++++ isUTC = true;
++++++++ }
++++++++
++++++++ var end = gentime.length - 5, c = gentime.charAt(end);
++++++++ if(c === '+' || c === '-') {
++++++++ // get hours+minutes offset
++++++++ var hhoffset = parseInt(gentime.substr(end + 1, 2), 10);
++++++++ var mmoffset = parseInt(gentime.substr(end + 4, 2), 10);
++++++++
++++++++ // calculate offset in milliseconds
++++++++ offset = hhoffset * 60 + mmoffset;
++++++++ offset *= 60000;
++++++++
++++++++ // apply offset
++++++++ if(c === '+') {
++++++++ offset *= -1;
++++++++ }
++++++++
++++++++ isUTC = true;
++++++++ }
++++++++
++++++++ // check for second fraction
++++++++ if(gentime.charAt(14) === '.') {
++++++++ fff = parseFloat(gentime.substr(14), 10) * 1000;
++++++++ }
++++++++
++++++++ if(isUTC) {
++++++++ date.setUTCFullYear(YYYY, MM, DD);
++++++++ date.setUTCHours(hh, mm, ss, fff);
++++++++
++++++++ // apply offset
++++++++ date.setTime(+date + offset);
++++++++ } else {
++++++++ date.setFullYear(YYYY, MM, DD);
++++++++ date.setHours(hh, mm, ss, fff);
++++++++ }
++++++++
++++++++ return date;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a date to a UTCTime value.
++++++++ *
++++++++ * Note: GeneralizedTime has 4 digits for the year and is used for X.509
++++++++ * dates past 2049. Converting to a GeneralizedTime hasn't been
++++++++ * implemented yet.
++++++++ *
++++++++ * @param date the date to convert.
++++++++ *
++++++++ * @return the UTCTime value.
++++++++ */
++++++++asn1.dateToUtcTime = function(date) {
++++++++ // TODO: validate; currently assumes proper format
++++++++ if(typeof date === 'string') {
++++++++ return date;
++++++++ }
++++++++
++++++++ var rval = '';
++++++++
++++++++ // create format YYMMDDhhmmssZ
++++++++ var format = [];
++++++++ format.push(('' + date.getUTCFullYear()).substr(2));
++++++++ format.push('' + (date.getUTCMonth() + 1));
++++++++ format.push('' + date.getUTCDate());
++++++++ format.push('' + date.getUTCHours());
++++++++ format.push('' + date.getUTCMinutes());
++++++++ format.push('' + date.getUTCSeconds());
++++++++
++++++++ // ensure 2 digits are used for each format entry
++++++++ for(var i = 0; i < format.length; ++i) {
++++++++ if(format[i].length < 2) {
++++++++ rval += '0';
++++++++ }
++++++++ rval += format[i];
++++++++ }
++++++++ rval += 'Z';
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a date to a GeneralizedTime value.
++++++++ *
++++++++ * @param date the date to convert.
++++++++ *
++++++++ * @return the GeneralizedTime value as a string.
++++++++ */
++++++++asn1.dateToGeneralizedTime = function(date) {
++++++++ // TODO: validate; currently assumes proper format
++++++++ if(typeof date === 'string') {
++++++++ return date;
++++++++ }
++++++++
++++++++ var rval = '';
++++++++
++++++++ // create format YYYYMMDDHHMMSSZ
++++++++ var format = [];
++++++++ format.push('' + date.getUTCFullYear());
++++++++ format.push('' + (date.getUTCMonth() + 1));
++++++++ format.push('' + date.getUTCDate());
++++++++ format.push('' + date.getUTCHours());
++++++++ format.push('' + date.getUTCMinutes());
++++++++ format.push('' + date.getUTCSeconds());
++++++++
++++++++ // ensure 2 digits are used for each format entry
++++++++ for(var i = 0; i < format.length; ++i) {
++++++++ if(format[i].length < 2) {
++++++++ rval += '0';
++++++++ }
++++++++ rval += format[i];
++++++++ }
++++++++ rval += 'Z';
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a javascript integer to a DER-encoded byte buffer to be used
++++++++ * as the value for an INTEGER type.
++++++++ *
++++++++ * @param x the integer.
++++++++ *
++++++++ * @return the byte buffer.
++++++++ */
++++++++asn1.integerToDer = function(x) {
++++++++ var rval = forge.util.createBuffer();
++++++++ if(x >= -0x80 && x < 0x80) {
++++++++ return rval.putSignedInt(x, 8);
++++++++ }
++++++++ if(x >= -0x8000 && x < 0x8000) {
++++++++ return rval.putSignedInt(x, 16);
++++++++ }
++++++++ if(x >= -0x800000 && x < 0x800000) {
++++++++ return rval.putSignedInt(x, 24);
++++++++ }
++++++++ if(x >= -0x80000000 && x < 0x80000000) {
++++++++ return rval.putSignedInt(x, 32);
++++++++ }
++++++++ var error = new Error('Integer too large; max is 32-bits.');
++++++++ error.integer = x;
++++++++ throw error;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a DER-encoded byte buffer to a javascript integer. This is
++++++++ * typically used to decode the value of an INTEGER type.
++++++++ *
++++++++ * @param bytes the byte buffer.
++++++++ *
++++++++ * @return the integer.
++++++++ */
++++++++asn1.derToInteger = function(bytes) {
++++++++ // wrap in buffer if needed
++++++++ if(typeof bytes === 'string') {
++++++++ bytes = forge.util.createBuffer(bytes);
++++++++ }
++++++++
++++++++ var n = bytes.length() * 8;
++++++++ if(n > 32) {
++++++++ throw new Error('Integer too large; max is 32-bits.');
++++++++ }
++++++++ return bytes.getSignedInt(n);
++++++++};
++++++++
++++++++/**
++++++++ * Validates that the given ASN.1 object is at least a super set of the
++++++++ * given ASN.1 structure. Only tag classes and types are checked. An
++++++++ * optional map may also be provided to capture ASN.1 values while the
++++++++ * structure is checked.
++++++++ *
++++++++ * To capture an ASN.1 value, set an object in the validator's 'capture'
++++++++ * parameter to the key to use in the capture map. To capture the full
++++++++ * ASN.1 object, specify 'captureAsn1'. To capture BIT STRING bytes, including
++++++++ * the leading unused bits counter byte, specify 'captureBitStringContents'.
++++++++ * To capture BIT STRING bytes, without the leading unused bits counter byte,
++++++++ * specify 'captureBitStringValue'.
++++++++ *
++++++++ * Objects in the validator may set a field 'optional' to true to indicate
++++++++ * that it isn't necessary to pass validation.
++++++++ *
++++++++ * @param obj the ASN.1 object to validate.
++++++++ * @param v the ASN.1 structure validator.
++++++++ * @param capture an optional map to capture values in.
++++++++ * @param errors an optional array for storing validation errors.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++asn1.validate = function(obj, v, capture, errors) {
++++++++ var rval = false;
++++++++
++++++++ // ensure tag class and type are the same if specified
++++++++ if((obj.tagClass === v.tagClass || typeof(v.tagClass) === 'undefined') &&
++++++++ (obj.type === v.type || typeof(v.type) === 'undefined')) {
++++++++ // ensure constructed flag is the same if specified
++++++++ if(obj.constructed === v.constructed ||
++++++++ typeof(v.constructed) === 'undefined') {
++++++++ rval = true;
++++++++
++++++++ // handle sub values
++++++++ if(v.value && forge.util.isArray(v.value)) {
++++++++ var j = 0;
++++++++ for(var i = 0; rval && i < v.value.length; ++i) {
++++++++ rval = v.value[i].optional || false;
++++++++ if(obj.value[j]) {
++++++++ rval = asn1.validate(obj.value[j], v.value[i], capture, errors);
++++++++ if(rval) {
++++++++ ++j;
++++++++ } else if(v.value[i].optional) {
++++++++ rval = true;
++++++++ }
++++++++ }
++++++++ if(!rval && errors) {
++++++++ errors.push(
++++++++ '[' + v.name + '] ' +
++++++++ 'Tag class "' + v.tagClass + '", type "' +
++++++++ v.type + '" expected value length "' +
++++++++ v.value.length + '", got "' +
++++++++ obj.value.length + '"');
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ if(rval && capture) {
++++++++ if(v.capture) {
++++++++ capture[v.capture] = obj.value;
++++++++ }
++++++++ if(v.captureAsn1) {
++++++++ capture[v.captureAsn1] = obj;
++++++++ }
++++++++ if(v.captureBitStringContents && 'bitStringContents' in obj) {
++++++++ capture[v.captureBitStringContents] = obj.bitStringContents;
++++++++ }
++++++++ if(v.captureBitStringValue && 'bitStringContents' in obj) {
++++++++ var value;
++++++++ if(obj.bitStringContents.length < 2) {
++++++++ capture[v.captureBitStringValue] = '';
++++++++ } else {
++++++++ // FIXME: support unused bits with data shifting
++++++++ var unused = obj.bitStringContents.charCodeAt(0);
++++++++ if(unused !== 0) {
++++++++ throw new Error(
++++++++ 'captureBitStringValue only supported for zero unused bits');
++++++++ }
++++++++ capture[v.captureBitStringValue] = obj.bitStringContents.slice(1);
++++++++ }
++++++++ }
++++++++ }
++++++++ } else if(errors) {
++++++++ errors.push(
++++++++ '[' + v.name + '] ' +
++++++++ 'Expected constructed "' + v.constructed + '", got "' +
++++++++ obj.constructed + '"');
++++++++ }
++++++++ } else if(errors) {
++++++++ if(obj.tagClass !== v.tagClass) {
++++++++ errors.push(
++++++++ '[' + v.name + '] ' +
++++++++ 'Expected tag class "' + v.tagClass + '", got "' +
++++++++ obj.tagClass + '"');
++++++++ }
++++++++ if(obj.type !== v.type) {
++++++++ errors.push(
++++++++ '[' + v.name + '] ' +
++++++++ 'Expected type "' + v.type + '", got "' + obj.type + '"');
++++++++ }
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++// regex for testing for non-latin characters
++++++++var _nonLatinRegex = /[^\\u0000-\\u00ff]/;
++++++++
++++++++/**
++++++++ * Pretty prints an ASN.1 object to a string.
++++++++ *
++++++++ * @param obj the object to write out.
++++++++ * @param level the level in the tree.
++++++++ * @param indentation the indentation to use.
++++++++ *
++++++++ * @return the string.
++++++++ */
++++++++asn1.prettyPrint = function(obj, level, indentation) {
++++++++ var rval = '';
++++++++
++++++++ // set default level and indentation
++++++++ level = level || 0;
++++++++ indentation = indentation || 2;
++++++++
++++++++ // start new line for deep levels
++++++++ if(level > 0) {
++++++++ rval += '\n';
++++++++ }
++++++++
++++++++ // create indent
++++++++ var indent = '';
++++++++ for(var i = 0; i < level * indentation; ++i) {
++++++++ indent += ' ';
++++++++ }
++++++++
++++++++ // print class:type
++++++++ rval += indent + 'Tag: ';
++++++++ switch(obj.tagClass) {
++++++++ case asn1.Class.UNIVERSAL:
++++++++ rval += 'Universal:';
++++++++ break;
++++++++ case asn1.Class.APPLICATION:
++++++++ rval += 'Application:';
++++++++ break;
++++++++ case asn1.Class.CONTEXT_SPECIFIC:
++++++++ rval += 'Context-Specific:';
++++++++ break;
++++++++ case asn1.Class.PRIVATE:
++++++++ rval += 'Private:';
++++++++ break;
++++++++ }
++++++++
++++++++ if(obj.tagClass === asn1.Class.UNIVERSAL) {
++++++++ rval += obj.type;
++++++++
++++++++ // known types
++++++++ switch(obj.type) {
++++++++ case asn1.Type.NONE:
++++++++ rval += ' (None)';
++++++++ break;
++++++++ case asn1.Type.BOOLEAN:
++++++++ rval += ' (Boolean)';
++++++++ break;
++++++++ case asn1.Type.INTEGER:
++++++++ rval += ' (Integer)';
++++++++ break;
++++++++ case asn1.Type.BITSTRING:
++++++++ rval += ' (Bit string)';
++++++++ break;
++++++++ case asn1.Type.OCTETSTRING:
++++++++ rval += ' (Octet string)';
++++++++ break;
++++++++ case asn1.Type.NULL:
++++++++ rval += ' (Null)';
++++++++ break;
++++++++ case asn1.Type.OID:
++++++++ rval += ' (Object Identifier)';
++++++++ break;
++++++++ case asn1.Type.ODESC:
++++++++ rval += ' (Object Descriptor)';
++++++++ break;
++++++++ case asn1.Type.EXTERNAL:
++++++++ rval += ' (External or Instance of)';
++++++++ break;
++++++++ case asn1.Type.REAL:
++++++++ rval += ' (Real)';
++++++++ break;
++++++++ case asn1.Type.ENUMERATED:
++++++++ rval += ' (Enumerated)';
++++++++ break;
++++++++ case asn1.Type.EMBEDDED:
++++++++ rval += ' (Embedded PDV)';
++++++++ break;
++++++++ case asn1.Type.UTF8:
++++++++ rval += ' (UTF8)';
++++++++ break;
++++++++ case asn1.Type.ROID:
++++++++ rval += ' (Relative Object Identifier)';
++++++++ break;
++++++++ case asn1.Type.SEQUENCE:
++++++++ rval += ' (Sequence)';
++++++++ break;
++++++++ case asn1.Type.SET:
++++++++ rval += ' (Set)';
++++++++ break;
++++++++ case asn1.Type.PRINTABLESTRING:
++++++++ rval += ' (Printable String)';
++++++++ break;
++++++++ case asn1.Type.IA5String:
++++++++ rval += ' (IA5String (ASCII))';
++++++++ break;
++++++++ case asn1.Type.UTCTIME:
++++++++ rval += ' (UTC time)';
++++++++ break;
++++++++ case asn1.Type.GENERALIZEDTIME:
++++++++ rval += ' (Generalized time)';
++++++++ break;
++++++++ case asn1.Type.BMPSTRING:
++++++++ rval += ' (BMP String)';
++++++++ break;
++++++++ }
++++++++ } else {
++++++++ rval += obj.type;
++++++++ }
++++++++
++++++++ rval += '\n';
++++++++ rval += indent + 'Constructed: ' + obj.constructed + '\n';
++++++++
++++++++ if(obj.composed) {
++++++++ var subvalues = 0;
++++++++ var sub = '';
++++++++ for(var i = 0; i < obj.value.length; ++i) {
++++++++ if(obj.value[i] !== undefined) {
++++++++ subvalues += 1;
++++++++ sub += asn1.prettyPrint(obj.value[i], level + 1, indentation);
++++++++ if((i + 1) < obj.value.length) {
++++++++ sub += ',';
++++++++ }
++++++++ }
++++++++ }
++++++++ rval += indent + 'Sub values: ' + subvalues + sub;
++++++++ } else {
++++++++ rval += indent + 'Value: ';
++++++++ if(obj.type === asn1.Type.OID) {
++++++++ var oid = asn1.derToOid(obj.value);
++++++++ rval += oid;
++++++++ if(forge.pki && forge.pki.oids) {
++++++++ if(oid in forge.pki.oids) {
++++++++ rval += ' (' + forge.pki.oids[oid] + ') ';
++++++++ }
++++++++ }
++++++++ }
++++++++ if(obj.type === asn1.Type.INTEGER) {
++++++++ try {
++++++++ rval += asn1.derToInteger(obj.value);
++++++++ } catch(ex) {
++++++++ rval += '0x' + forge.util.bytesToHex(obj.value);
++++++++ }
++++++++ } else if(obj.type === asn1.Type.BITSTRING) {
++++++++ // TODO: shift bits as needed to display without padding
++++++++ if(obj.value.length > 1) {
++++++++ // remove unused bits field
++++++++ rval += '0x' + forge.util.bytesToHex(obj.value.slice(1));
++++++++ } else {
++++++++ rval += '(none)';
++++++++ }
++++++++ // show unused bit count
++++++++ if(obj.value.length > 0) {
++++++++ var unused = obj.value.charCodeAt(0);
++++++++ if(unused == 1) {
++++++++ rval += ' (1 unused bit shown)';
++++++++ } else if(unused > 1) {
++++++++ rval += ' (' + unused + ' unused bits shown)';
++++++++ }
++++++++ }
++++++++ } else if(obj.type === asn1.Type.OCTETSTRING) {
++++++++ if(!_nonLatinRegex.test(obj.value)) {
++++++++ rval += '(' + obj.value + ') ';
++++++++ }
++++++++ rval += '0x' + forge.util.bytesToHex(obj.value);
++++++++ } else if(obj.type === asn1.Type.UTF8) {
++++++++ try {
++++++++ rval += forge.util.decodeUtf8(obj.value);
++++++++ } catch(e) {
++++++++ if(e.message === 'URI malformed') {
++++++++ rval +=
++++++++ '0x' + forge.util.bytesToHex(obj.value) + ' (malformed UTF8)';
++++++++ } else {
++++++++ throw e;
++++++++ }
++++++++ }
++++++++ } else if(obj.type === asn1.Type.PRINTABLESTRING ||
++++++++ obj.type === asn1.Type.IA5String) {
++++++++ rval += obj.value;
++++++++ } else if(_nonLatinRegex.test(obj.value)) {
++++++++ rval += '0x' + forge.util.bytesToHex(obj.value);
++++++++ } else if(obj.value.length === 0) {
++++++++ rval += '[null]';
++++++++ } else {
++++++++ rval += obj.value;
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Base-N/Base-X encoding/decoding functions.
++++++++ *
++++++++ * Original implementation from base-x:
++++++++ * https://github.com/cryptocoinjs/base-x
++++++++ *
++++++++ * Which is MIT licensed:
++++++++ *
++++++++ * The MIT License (MIT)
++++++++ *
++++++++ * Copyright base-x contributors (c) 2016
++++++++ *
++++++++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++ * of this software and associated documentation files (the "Software"), to deal
++++++++ * in the Software without restriction, including without limitation the rights
++++++++ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++ * copies of the Software, and to permit persons to whom the Software is
++++++++ * furnished to do so, subject to the following conditions:
++++++++ *
++++++++ * The above copyright notice and this permission notice shall be included in
++++++++ * all copies or substantial portions of the Software.
++++++++ *
++++++++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++++++++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
++++++++ * DEALINGS IN THE SOFTWARE.
++++++++ */
++++++++var api = {};
++++++++module.exports = api;
++++++++
++++++++// baseN alphabet indexes
++++++++var _reverseAlphabets = {};
++++++++
++++++++/**
++++++++ * BaseN-encodes a Uint8Array using the given alphabet.
++++++++ *
++++++++ * @param input the Uint8Array to encode.
++++++++ * @param maxline the maximum number of encoded characters per line to use,
++++++++ * defaults to none.
++++++++ *
++++++++ * @return the baseN-encoded output string.
++++++++ */
++++++++api.encode = function(input, alphabet, maxline) {
++++++++ if(typeof alphabet !== 'string') {
++++++++ throw new TypeError('"alphabet" must be a string.');
++++++++ }
++++++++ if(maxline !== undefined && typeof maxline !== 'number') {
++++++++ throw new TypeError('"maxline" must be a number.');
++++++++ }
++++++++
++++++++ var output = '';
++++++++
++++++++ if(!(input instanceof Uint8Array)) {
++++++++ // assume forge byte buffer
++++++++ output = _encodeWithByteBuffer(input, alphabet);
++++++++ } else {
++++++++ var i = 0;
++++++++ var base = alphabet.length;
++++++++ var first = alphabet.charAt(0);
++++++++ var digits = [0];
++++++++ for(i = 0; i < input.length; ++i) {
++++++++ for(var j = 0, carry = input[i]; j < digits.length; ++j) {
++++++++ carry += digits[j] << 8;
++++++++ digits[j] = carry % base;
++++++++ carry = (carry / base) | 0;
++++++++ }
++++++++
++++++++ while(carry > 0) {
++++++++ digits.push(carry % base);
++++++++ carry = (carry / base) | 0;
++++++++ }
++++++++ }
++++++++
++++++++ // deal with leading zeros
++++++++ for(i = 0; input[i] === 0 && i < input.length - 1; ++i) {
++++++++ output += first;
++++++++ }
++++++++ // convert digits to a string
++++++++ for(i = digits.length - 1; i >= 0; --i) {
++++++++ output += alphabet[digits[i]];
++++++++ }
++++++++ }
++++++++
++++++++ if(maxline) {
++++++++ var regex = new RegExp('.{1,' + maxline + '}', 'g');
++++++++ output = output.match(regex).join('\r\n');
++++++++ }
++++++++
++++++++ return output;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes a baseN-encoded (using the given alphabet) string to a
++++++++ * Uint8Array.
++++++++ *
++++++++ * @param input the baseN-encoded input string.
++++++++ *
++++++++ * @return the Uint8Array.
++++++++ */
++++++++api.decode = function(input, alphabet) {
++++++++ if(typeof input !== 'string') {
++++++++ throw new TypeError('"input" must be a string.');
++++++++ }
++++++++ if(typeof alphabet !== 'string') {
++++++++ throw new TypeError('"alphabet" must be a string.');
++++++++ }
++++++++
++++++++ var table = _reverseAlphabets[alphabet];
++++++++ if(!table) {
++++++++ // compute reverse alphabet
++++++++ table = _reverseAlphabets[alphabet] = [];
++++++++ for(var i = 0; i < alphabet.length; ++i) {
++++++++ table[alphabet.charCodeAt(i)] = i;
++++++++ }
++++++++ }
++++++++
++++++++ // remove whitespace characters
++++++++ input = input.replace(/\s/g, '');
++++++++
++++++++ var base = alphabet.length;
++++++++ var first = alphabet.charAt(0);
++++++++ var bytes = [0];
++++++++ for(var i = 0; i < input.length; i++) {
++++++++ var value = table[input.charCodeAt(i)];
++++++++ if(value === undefined) {
++++++++ return;
++++++++ }
++++++++
++++++++ for(var j = 0, carry = value; j < bytes.length; ++j) {
++++++++ carry += bytes[j] * base;
++++++++ bytes[j] = carry & 0xff;
++++++++ carry >>= 8;
++++++++ }
++++++++
++++++++ while(carry > 0) {
++++++++ bytes.push(carry & 0xff);
++++++++ carry >>= 8;
++++++++ }
++++++++ }
++++++++
++++++++ // deal with leading zeros
++++++++ for(var k = 0; input[k] === first && k < input.length - 1; ++k) {
++++++++ bytes.push(0);
++++++++ }
++++++++
++++++++ if(typeof Buffer !== 'undefined') {
++++++++ return Buffer.from(bytes.reverse());
++++++++ }
++++++++
++++++++ return new Uint8Array(bytes.reverse());
++++++++};
++++++++
++++++++function _encodeWithByteBuffer(input, alphabet) {
++++++++ var i = 0;
++++++++ var base = alphabet.length;
++++++++ var first = alphabet.charAt(0);
++++++++ var digits = [0];
++++++++ for(i = 0; i < input.length(); ++i) {
++++++++ for(var j = 0, carry = input.at(i); j < digits.length; ++j) {
++++++++ carry += digits[j] << 8;
++++++++ digits[j] = carry % base;
++++++++ carry = (carry / base) | 0;
++++++++ }
++++++++
++++++++ while(carry > 0) {
++++++++ digits.push(carry % base);
++++++++ carry = (carry / base) | 0;
++++++++ }
++++++++ }
++++++++
++++++++ var output = '';
++++++++
++++++++ // deal with leading zeros
++++++++ for(i = 0; input.at(i) === 0 && i < input.length() - 1; ++i) {
++++++++ output += first;
++++++++ }
++++++++ // convert digits to a string
++++++++ for(i = digits.length - 1; i >= 0; --i) {
++++++++ output += alphabet[digits[i]];
++++++++ }
++++++++
++++++++ return output;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Cipher base API.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++module.exports = forge.cipher = forge.cipher || {};
++++++++
++++++++// registered algorithms
++++++++forge.cipher.algorithms = forge.cipher.algorithms || {};
++++++++
++++++++/**
++++++++ * Creates a cipher object that can be used to encrypt data using the given
++++++++ * algorithm and key. The algorithm may be provided as a string value for a
++++++++ * previously registered algorithm or it may be given as a cipher algorithm
++++++++ * API object.
++++++++ *
++++++++ * @param algorithm the algorithm to use, either a string or an algorithm API
++++++++ * object.
++++++++ * @param key the key to use, as a binary-encoded string of bytes or a
++++++++ * byte buffer.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.cipher.createCipher = function(algorithm, key) {
++++++++ var api = algorithm;
++++++++ if(typeof api === 'string') {
++++++++ api = forge.cipher.getAlgorithm(api);
++++++++ if(api) {
++++++++ api = api();
++++++++ }
++++++++ }
++++++++ if(!api) {
++++++++ throw new Error('Unsupported algorithm: ' + algorithm);
++++++++ }
++++++++
++++++++ // assume block cipher
++++++++ return new forge.cipher.BlockCipher({
++++++++ algorithm: api,
++++++++ key: key,
++++++++ decrypt: false
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Creates a decipher object that can be used to decrypt data using the given
++++++++ * algorithm and key. The algorithm may be provided as a string value for a
++++++++ * previously registered algorithm or it may be given as a cipher algorithm
++++++++ * API object.
++++++++ *
++++++++ * @param algorithm the algorithm to use, either a string or an algorithm API
++++++++ * object.
++++++++ * @param key the key to use, as a binary-encoded string of bytes or a
++++++++ * byte buffer.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.cipher.createDecipher = function(algorithm, key) {
++++++++ var api = algorithm;
++++++++ if(typeof api === 'string') {
++++++++ api = forge.cipher.getAlgorithm(api);
++++++++ if(api) {
++++++++ api = api();
++++++++ }
++++++++ }
++++++++ if(!api) {
++++++++ throw new Error('Unsupported algorithm: ' + algorithm);
++++++++ }
++++++++
++++++++ // assume block cipher
++++++++ return new forge.cipher.BlockCipher({
++++++++ algorithm: api,
++++++++ key: key,
++++++++ decrypt: true
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Registers an algorithm by name. If the name was already registered, the
++++++++ * algorithm API object will be overwritten.
++++++++ *
++++++++ * @param name the name of the algorithm.
++++++++ * @param algorithm the algorithm API object.
++++++++ */
++++++++forge.cipher.registerAlgorithm = function(name, algorithm) {
++++++++ name = name.toUpperCase();
++++++++ forge.cipher.algorithms[name] = algorithm;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a registered algorithm by name.
++++++++ *
++++++++ * @param name the name of the algorithm.
++++++++ *
++++++++ * @return the algorithm, if found, null if not.
++++++++ */
++++++++forge.cipher.getAlgorithm = function(name) {
++++++++ name = name.toUpperCase();
++++++++ if(name in forge.cipher.algorithms) {
++++++++ return forge.cipher.algorithms[name];
++++++++ }
++++++++ return null;
++++++++};
++++++++
++++++++var BlockCipher = forge.cipher.BlockCipher = function(options) {
++++++++ this.algorithm = options.algorithm;
++++++++ this.mode = this.algorithm.mode;
++++++++ this.blockSize = this.mode.blockSize;
++++++++ this._finish = false;
++++++++ this._input = null;
++++++++ this.output = null;
++++++++ this._op = options.decrypt ? this.mode.decrypt : this.mode.encrypt;
++++++++ this._decrypt = options.decrypt;
++++++++ this.algorithm.initialize(options);
++++++++};
++++++++
++++++++/**
++++++++ * Starts or restarts the encryption or decryption process, whichever
++++++++ * was previously configured.
++++++++ *
++++++++ * For non-GCM mode, the IV may be a binary-encoded string of bytes, an array
++++++++ * of bytes, a byte buffer, or an array of 32-bit integers. If the IV is in
++++++++ * bytes, then it must be Nb (16) bytes in length. If the IV is given in as
++++++++ * 32-bit integers, then it must be 4 integers long.
++++++++ *
++++++++ * Note: an IV is not required or used in ECB mode.
++++++++ *
++++++++ * For GCM-mode, the IV must be given as a binary-encoded string of bytes or
++++++++ * a byte buffer. The number of bytes should be 12 (96 bits) as recommended
++++++++ * by NIST SP-800-38D but another length may be given.
++++++++ *
++++++++ * @param options the options to use:
++++++++ * iv the initialization vector to use as a binary-encoded string of
++++++++ * bytes, null to reuse the last ciphered block from a previous
++++++++ * update() (this "residue" method is for legacy support only).
++++++++ * additionalData additional authentication data as a binary-encoded
++++++++ * string of bytes, for 'GCM' mode, (default: none).
++++++++ * tagLength desired length of authentication tag, in bits, for
++++++++ * 'GCM' mode (0-128, default: 128).
++++++++ * tag the authentication tag to check if decrypting, as a
++++++++ * binary-encoded string of bytes.
++++++++ * output the output the buffer to write to, null to create one.
++++++++ */
++++++++BlockCipher.prototype.start = function(options) {
++++++++ options = options || {};
++++++++ var opts = {};
++++++++ for(var key in options) {
++++++++ opts[key] = options[key];
++++++++ }
++++++++ opts.decrypt = this._decrypt;
++++++++ this._finish = false;
++++++++ this._input = forge.util.createBuffer();
++++++++ this.output = options.output || forge.util.createBuffer();
++++++++ this.mode.start(opts);
++++++++};
++++++++
++++++++/**
++++++++ * Updates the next block according to the cipher mode.
++++++++ *
++++++++ * @param input the buffer to read from.
++++++++ */
++++++++BlockCipher.prototype.update = function(input) {
++++++++ if(input) {
++++++++ // input given, so empty it into the input buffer
++++++++ this._input.putBuffer(input);
++++++++ }
++++++++
++++++++ // do cipher operation until it needs more input and not finished
++++++++ while(!this._op.call(this.mode, this._input, this.output, this._finish) &&
++++++++ !this._finish) {}
++++++++
++++++++ // free consumed memory from input buffer
++++++++ this._input.compact();
++++++++};
++++++++
++++++++/**
++++++++ * Finishes encrypting or decrypting.
++++++++ *
++++++++ * @param pad a padding function to use in CBC mode, null for default,
++++++++ * signature(blockSize, buffer, decrypt).
++++++++ *
++++++++ * @return true if successful, false on error.
++++++++ */
++++++++BlockCipher.prototype.finish = function(pad) {
++++++++ // backwards-compatibility w/deprecated padding API
++++++++ // Note: will overwrite padding functions even after another start() call
++++++++ if(pad && (this.mode.name === 'ECB' || this.mode.name === 'CBC')) {
++++++++ this.mode.pad = function(input) {
++++++++ return pad(this.blockSize, input, false);
++++++++ };
++++++++ this.mode.unpad = function(output) {
++++++++ return pad(this.blockSize, output, true);
++++++++ };
++++++++ }
++++++++
++++++++ // build options for padding and afterFinish functions
++++++++ var options = {};
++++++++ options.decrypt = this._decrypt;
++++++++
++++++++ // get # of bytes that won't fill a block
++++++++ options.overflow = this._input.length() % this.blockSize;
++++++++
++++++++ if(!this._decrypt && this.mode.pad) {
++++++++ if(!this.mode.pad(this._input, options)) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++
++++++++ // do final update
++++++++ this._finish = true;
++++++++ this.update();
++++++++
++++++++ if(this._decrypt && this.mode.unpad) {
++++++++ if(!this.mode.unpad(this.output, options)) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++
++++++++ if(this.mode.afterFinish) {
++++++++ if(!this.mode.afterFinish(this.output, options)) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++
++++++++ return true;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Supported cipher modes.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++forge.cipher = forge.cipher || {};
++++++++
++++++++// supported cipher modes
++++++++var modes = module.exports = forge.cipher.modes = forge.cipher.modes || {};
++++++++
++++++++/** Electronic codebook (ECB) (Don't use this; it's not secure) **/
++++++++
++++++++modes.ecb = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'ECB';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = new Array(this._ints);
++++++++ this._outBlock = new Array(this._ints);
++++++++};
++++++++
++++++++modes.ecb.prototype.start = function(options) {};
++++++++
++++++++modes.ecb.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // get next block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = input.getInt32();
++++++++ }
++++++++
++++++++ // encrypt block
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // write output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._outBlock[i]);
++++++++ }
++++++++};
++++++++
++++++++modes.ecb.prototype.decrypt = function(input, output, finish) {
++++++++ // not enough input to decrypt
++++++++ if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // get next block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = input.getInt32();
++++++++ }
++++++++
++++++++ // decrypt block
++++++++ this.cipher.decrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // write output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._outBlock[i]);
++++++++ }
++++++++};
++++++++
++++++++modes.ecb.prototype.pad = function(input, options) {
++++++++ // add PKCS#7 padding to block (each pad byte is the
++++++++ // value of the number of pad bytes)
++++++++ var padding = (input.length() === this.blockSize ?
++++++++ this.blockSize : (this.blockSize - input.length()));
++++++++ input.fillWithByte(padding, padding);
++++++++ return true;
++++++++};
++++++++
++++++++modes.ecb.prototype.unpad = function(output, options) {
++++++++ // check for error: input data not a multiple of blockSize
++++++++ if(options.overflow > 0) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // ensure padding byte count is valid
++++++++ var len = output.length();
++++++++ var count = output.at(len - 1);
++++++++ if(count > (this.blockSize << 2)) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // trim off padding bytes
++++++++ output.truncate(count);
++++++++ return true;
++++++++};
++++++++
++++++++/** Cipher-block Chaining (CBC) **/
++++++++
++++++++modes.cbc = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'CBC';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = new Array(this._ints);
++++++++ this._outBlock = new Array(this._ints);
++++++++};
++++++++
++++++++modes.cbc.prototype.start = function(options) {
++++++++ // Note: legacy support for using IV residue (has security flaws)
++++++++ // if IV is null, reuse block from previous processing
++++++++ if(options.iv === null) {
++++++++ // must have a previous block
++++++++ if(!this._prev) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ }
++++++++ this._iv = this._prev.slice(0);
++++++++ } else if(!('iv' in options)) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ } else {
++++++++ // save IV as "previous" block
++++++++ this._iv = transformIV(options.iv, this.blockSize);
++++++++ this._prev = this._iv.slice(0);
++++++++ }
++++++++};
++++++++
++++++++modes.cbc.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // get next block
++++++++ // CBC XOR's IV (or previous block) with plaintext
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = this._prev[i] ^ input.getInt32();
++++++++ }
++++++++
++++++++ // encrypt block
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // write output, save previous block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._outBlock[i]);
++++++++ }
++++++++ this._prev = this._outBlock;
++++++++};
++++++++
++++++++modes.cbc.prototype.decrypt = function(input, output, finish) {
++++++++ // not enough input to decrypt
++++++++ if(input.length() < this.blockSize && !(finish && input.length() > 0)) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // get next block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = input.getInt32();
++++++++ }
++++++++
++++++++ // decrypt block
++++++++ this.cipher.decrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // write output, save previous ciphered block
++++++++ // CBC XOR's IV (or previous block) with ciphertext
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._prev[i] ^ this._outBlock[i]);
++++++++ }
++++++++ this._prev = this._inBlock.slice(0);
++++++++};
++++++++
++++++++modes.cbc.prototype.pad = function(input, options) {
++++++++ // add PKCS#7 padding to block (each pad byte is the
++++++++ // value of the number of pad bytes)
++++++++ var padding = (input.length() === this.blockSize ?
++++++++ this.blockSize : (this.blockSize - input.length()));
++++++++ input.fillWithByte(padding, padding);
++++++++ return true;
++++++++};
++++++++
++++++++modes.cbc.prototype.unpad = function(output, options) {
++++++++ // check for error: input data not a multiple of blockSize
++++++++ if(options.overflow > 0) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // ensure padding byte count is valid
++++++++ var len = output.length();
++++++++ var count = output.at(len - 1);
++++++++ if(count > (this.blockSize << 2)) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // trim off padding bytes
++++++++ output.truncate(count);
++++++++ return true;
++++++++};
++++++++
++++++++/** Cipher feedback (CFB) **/
++++++++
++++++++modes.cfb = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'CFB';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = null;
++++++++ this._outBlock = new Array(this._ints);
++++++++ this._partialBlock = new Array(this._ints);
++++++++ this._partialOutput = forge.util.createBuffer();
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.cfb.prototype.start = function(options) {
++++++++ if(!('iv' in options)) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ }
++++++++ // use IV as first input
++++++++ this._iv = transformIV(options.iv, this.blockSize);
++++++++ this._inBlock = this._iv.slice(0);
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.cfb.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ var inputLength = input.length();
++++++++ if(inputLength === 0) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // handle full block
++++++++ if(this._partialBytes === 0 && inputLength >= this.blockSize) {
++++++++ // XOR input with output, write input as output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = input.getInt32() ^ this._outBlock[i];
++++++++ output.putInt32(this._inBlock[i]);
++++++++ }
++++++++ return;
++++++++ }
++++++++
++++++++ // handle partial block
++++++++ var partialBytes = (this.blockSize - inputLength) % this.blockSize;
++++++++ if(partialBytes > 0) {
++++++++ partialBytes = this.blockSize - partialBytes;
++++++++ }
++++++++
++++++++ // XOR input with output, write input as partial output
++++++++ this._partialOutput.clear();
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._partialBlock[i] = input.getInt32() ^ this._outBlock[i];
++++++++ this._partialOutput.putInt32(this._partialBlock[i]);
++++++++ }
++++++++
++++++++ if(partialBytes > 0) {
++++++++ // block still incomplete, restore input buffer
++++++++ input.read -= this.blockSize;
++++++++ } else {
++++++++ // block complete, update input block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = this._partialBlock[i];
++++++++ }
++++++++ }
++++++++
++++++++ // skip any previous partial bytes
++++++++ if(this._partialBytes > 0) {
++++++++ this._partialOutput.getBytes(this._partialBytes);
++++++++ }
++++++++
++++++++ if(partialBytes > 0 && !finish) {
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ partialBytes - this._partialBytes));
++++++++ this._partialBytes = partialBytes;
++++++++ return true;
++++++++ }
++++++++
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ inputLength - this._partialBytes));
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.cfb.prototype.decrypt = function(input, output, finish) {
++++++++ // not enough input to decrypt
++++++++ var inputLength = input.length();
++++++++ if(inputLength === 0) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block (CFB always uses encryption mode)
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // handle full block
++++++++ if(this._partialBytes === 0 && inputLength >= this.blockSize) {
++++++++ // XOR input with output, write input as output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = input.getInt32();
++++++++ output.putInt32(this._inBlock[i] ^ this._outBlock[i]);
++++++++ }
++++++++ return;
++++++++ }
++++++++
++++++++ // handle partial block
++++++++ var partialBytes = (this.blockSize - inputLength) % this.blockSize;
++++++++ if(partialBytes > 0) {
++++++++ partialBytes = this.blockSize - partialBytes;
++++++++ }
++++++++
++++++++ // XOR input with output, write input as partial output
++++++++ this._partialOutput.clear();
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._partialBlock[i] = input.getInt32();
++++++++ this._partialOutput.putInt32(this._partialBlock[i] ^ this._outBlock[i]);
++++++++ }
++++++++
++++++++ if(partialBytes > 0) {
++++++++ // block still incomplete, restore input buffer
++++++++ input.read -= this.blockSize;
++++++++ } else {
++++++++ // block complete, update input block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = this._partialBlock[i];
++++++++ }
++++++++ }
++++++++
++++++++ // skip any previous partial bytes
++++++++ if(this._partialBytes > 0) {
++++++++ this._partialOutput.getBytes(this._partialBytes);
++++++++ }
++++++++
++++++++ if(partialBytes > 0 && !finish) {
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ partialBytes - this._partialBytes));
++++++++ this._partialBytes = partialBytes;
++++++++ return true;
++++++++ }
++++++++
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ inputLength - this._partialBytes));
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++/** Output feedback (OFB) **/
++++++++
++++++++modes.ofb = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'OFB';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = null;
++++++++ this._outBlock = new Array(this._ints);
++++++++ this._partialOutput = forge.util.createBuffer();
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.ofb.prototype.start = function(options) {
++++++++ if(!('iv' in options)) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ }
++++++++ // use IV as first input
++++++++ this._iv = transformIV(options.iv, this.blockSize);
++++++++ this._inBlock = this._iv.slice(0);
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.ofb.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ var inputLength = input.length();
++++++++ if(input.length() === 0) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block (OFB always uses encryption mode)
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // handle full block
++++++++ if(this._partialBytes === 0 && inputLength >= this.blockSize) {
++++++++ // XOR input with output and update next input
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(input.getInt32() ^ this._outBlock[i]);
++++++++ this._inBlock[i] = this._outBlock[i];
++++++++ }
++++++++ return;
++++++++ }
++++++++
++++++++ // handle partial block
++++++++ var partialBytes = (this.blockSize - inputLength) % this.blockSize;
++++++++ if(partialBytes > 0) {
++++++++ partialBytes = this.blockSize - partialBytes;
++++++++ }
++++++++
++++++++ // XOR input with output
++++++++ this._partialOutput.clear();
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
++++++++ }
++++++++
++++++++ if(partialBytes > 0) {
++++++++ // block still incomplete, restore input buffer
++++++++ input.read -= this.blockSize;
++++++++ } else {
++++++++ // block complete, update input block
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._inBlock[i] = this._outBlock[i];
++++++++ }
++++++++ }
++++++++
++++++++ // skip any previous partial bytes
++++++++ if(this._partialBytes > 0) {
++++++++ this._partialOutput.getBytes(this._partialBytes);
++++++++ }
++++++++
++++++++ if(partialBytes > 0 && !finish) {
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ partialBytes - this._partialBytes));
++++++++ this._partialBytes = partialBytes;
++++++++ return true;
++++++++ }
++++++++
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ inputLength - this._partialBytes));
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.ofb.prototype.decrypt = modes.ofb.prototype.encrypt;
++++++++
++++++++/** Counter (CTR) **/
++++++++
++++++++modes.ctr = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'CTR';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = null;
++++++++ this._outBlock = new Array(this._ints);
++++++++ this._partialOutput = forge.util.createBuffer();
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.ctr.prototype.start = function(options) {
++++++++ if(!('iv' in options)) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ }
++++++++ // use IV as first input
++++++++ this._iv = transformIV(options.iv, this.blockSize);
++++++++ this._inBlock = this._iv.slice(0);
++++++++ this._partialBytes = 0;
++++++++};
++++++++
++++++++modes.ctr.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ var inputLength = input.length();
++++++++ if(inputLength === 0) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block (CTR always uses encryption mode)
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // handle full block
++++++++ if(this._partialBytes === 0 && inputLength >= this.blockSize) {
++++++++ // XOR input with output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(input.getInt32() ^ this._outBlock[i]);
++++++++ }
++++++++ } else {
++++++++ // handle partial block
++++++++ var partialBytes = (this.blockSize - inputLength) % this.blockSize;
++++++++ if(partialBytes > 0) {
++++++++ partialBytes = this.blockSize - partialBytes;
++++++++ }
++++++++
++++++++ // XOR input with output
++++++++ this._partialOutput.clear();
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
++++++++ }
++++++++
++++++++ if(partialBytes > 0) {
++++++++ // block still incomplete, restore input buffer
++++++++ input.read -= this.blockSize;
++++++++ }
++++++++
++++++++ // skip any previous partial bytes
++++++++ if(this._partialBytes > 0) {
++++++++ this._partialOutput.getBytes(this._partialBytes);
++++++++ }
++++++++
++++++++ if(partialBytes > 0 && !finish) {
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ partialBytes - this._partialBytes));
++++++++ this._partialBytes = partialBytes;
++++++++ return true;
++++++++ }
++++++++
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ inputLength - this._partialBytes));
++++++++ this._partialBytes = 0;
++++++++ }
++++++++
++++++++ // block complete, increment counter (input block)
++++++++ inc32(this._inBlock);
++++++++};
++++++++
++++++++modes.ctr.prototype.decrypt = modes.ctr.prototype.encrypt;
++++++++
++++++++/** Galois/Counter Mode (GCM) **/
++++++++
++++++++modes.gcm = function(options) {
++++++++ options = options || {};
++++++++ this.name = 'GCM';
++++++++ this.cipher = options.cipher;
++++++++ this.blockSize = options.blockSize || 16;
++++++++ this._ints = this.blockSize / 4;
++++++++ this._inBlock = new Array(this._ints);
++++++++ this._outBlock = new Array(this._ints);
++++++++ this._partialOutput = forge.util.createBuffer();
++++++++ this._partialBytes = 0;
++++++++
++++++++ // R is actually this value concatenated with 120 more zero bits, but
++++++++ // we only XOR against R so the other zeros have no effect -- we just
++++++++ // apply this value to the first integer in a block
++++++++ this._R = 0xE1000000;
++++++++};
++++++++
++++++++modes.gcm.prototype.start = function(options) {
++++++++ if(!('iv' in options)) {
++++++++ throw new Error('Invalid IV parameter.');
++++++++ }
++++++++ // ensure IV is a byte buffer
++++++++ var iv = forge.util.createBuffer(options.iv);
++++++++
++++++++ // no ciphered data processed yet
++++++++ this._cipherLength = 0;
++++++++
++++++++ // default additional data is none
++++++++ var additionalData;
++++++++ if('additionalData' in options) {
++++++++ additionalData = forge.util.createBuffer(options.additionalData);
++++++++ } else {
++++++++ additionalData = forge.util.createBuffer();
++++++++ }
++++++++
++++++++ // default tag length is 128 bits
++++++++ if('tagLength' in options) {
++++++++ this._tagLength = options.tagLength;
++++++++ } else {
++++++++ this._tagLength = 128;
++++++++ }
++++++++
++++++++ // if tag is given, ensure tag matches tag length
++++++++ this._tag = null;
++++++++ if(options.decrypt) {
++++++++ // save tag to check later
++++++++ this._tag = forge.util.createBuffer(options.tag).getBytes();
++++++++ if(this._tag.length !== (this._tagLength / 8)) {
++++++++ throw new Error('Authentication tag does not match tag length.');
++++++++ }
++++++++ }
++++++++
++++++++ // create tmp storage for hash calculation
++++++++ this._hashBlock = new Array(this._ints);
++++++++
++++++++ // no tag generated yet
++++++++ this.tag = null;
++++++++
++++++++ // generate hash subkey
++++++++ // (apply block cipher to "zero" block)
++++++++ this._hashSubkey = new Array(this._ints);
++++++++ this.cipher.encrypt([0, 0, 0, 0], this._hashSubkey);
++++++++
++++++++ // generate table M
++++++++ // use 4-bit tables (32 component decomposition of a 16 byte value)
++++++++ // 8-bit tables take more space and are known to have security
++++++++ // vulnerabilities (in native implementations)
++++++++ this.componentBits = 4;
++++++++ this._m = this.generateHashTable(this._hashSubkey, this.componentBits);
++++++++
++++++++ // Note: support IV length different from 96 bits? (only supporting
++++++++ // 96 bits is recommended by NIST SP-800-38D)
++++++++ // generate J_0
++++++++ var ivLength = iv.length();
++++++++ if(ivLength === 12) {
++++++++ // 96-bit IV
++++++++ this._j0 = [iv.getInt32(), iv.getInt32(), iv.getInt32(), 1];
++++++++ } else {
++++++++ // IV is NOT 96-bits
++++++++ this._j0 = [0, 0, 0, 0];
++++++++ while(iv.length() > 0) {
++++++++ this._j0 = this.ghash(
++++++++ this._hashSubkey, this._j0,
++++++++ [iv.getInt32(), iv.getInt32(), iv.getInt32(), iv.getInt32()]);
++++++++ }
++++++++ this._j0 = this.ghash(
++++++++ this._hashSubkey, this._j0, [0, 0].concat(from64To32(ivLength * 8)));
++++++++ }
++++++++
++++++++ // generate ICB (initial counter block)
++++++++ this._inBlock = this._j0.slice(0);
++++++++ inc32(this._inBlock);
++++++++ this._partialBytes = 0;
++++++++
++++++++ // consume authentication data
++++++++ additionalData = forge.util.createBuffer(additionalData);
++++++++ // save additional data length as a BE 64-bit number
++++++++ this._aDataLength = from64To32(additionalData.length() * 8);
++++++++ // pad additional data to 128 bit (16 byte) block size
++++++++ var overflow = additionalData.length() % this.blockSize;
++++++++ if(overflow) {
++++++++ additionalData.fillWithByte(0, this.blockSize - overflow);
++++++++ }
++++++++ this._s = [0, 0, 0, 0];
++++++++ while(additionalData.length() > 0) {
++++++++ this._s = this.ghash(this._hashSubkey, this._s, [
++++++++ additionalData.getInt32(),
++++++++ additionalData.getInt32(),
++++++++ additionalData.getInt32(),
++++++++ additionalData.getInt32()
++++++++ ]);
++++++++ }
++++++++};
++++++++
++++++++modes.gcm.prototype.encrypt = function(input, output, finish) {
++++++++ // not enough input to encrypt
++++++++ var inputLength = input.length();
++++++++ if(inputLength === 0) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // handle full block
++++++++ if(this._partialBytes === 0 && inputLength >= this.blockSize) {
++++++++ // XOR input with output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._outBlock[i] ^= input.getInt32());
++++++++ }
++++++++ this._cipherLength += this.blockSize;
++++++++ } else {
++++++++ // handle partial block
++++++++ var partialBytes = (this.blockSize - inputLength) % this.blockSize;
++++++++ if(partialBytes > 0) {
++++++++ partialBytes = this.blockSize - partialBytes;
++++++++ }
++++++++
++++++++ // XOR input with output
++++++++ this._partialOutput.clear();
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._partialOutput.putInt32(input.getInt32() ^ this._outBlock[i]);
++++++++ }
++++++++
++++++++ if(partialBytes <= 0 || finish) {
++++++++ // handle overflow prior to hashing
++++++++ if(finish) {
++++++++ // get block overflow
++++++++ var overflow = inputLength % this.blockSize;
++++++++ this._cipherLength += overflow;
++++++++ // truncate for hash function
++++++++ this._partialOutput.truncate(this.blockSize - overflow);
++++++++ } else {
++++++++ this._cipherLength += this.blockSize;
++++++++ }
++++++++
++++++++ // get output block for hashing
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this._outBlock[i] = this._partialOutput.getInt32();
++++++++ }
++++++++ this._partialOutput.read -= this.blockSize;
++++++++ }
++++++++
++++++++ // skip any previous partial bytes
++++++++ if(this._partialBytes > 0) {
++++++++ this._partialOutput.getBytes(this._partialBytes);
++++++++ }
++++++++
++++++++ if(partialBytes > 0 && !finish) {
++++++++ // block still incomplete, restore input buffer, get partial output,
++++++++ // and return early
++++++++ input.read -= this.blockSize;
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ partialBytes - this._partialBytes));
++++++++ this._partialBytes = partialBytes;
++++++++ return true;
++++++++ }
++++++++
++++++++ output.putBytes(this._partialOutput.getBytes(
++++++++ inputLength - this._partialBytes));
++++++++ this._partialBytes = 0;
++++++++ }
++++++++
++++++++ // update hash block S
++++++++ this._s = this.ghash(this._hashSubkey, this._s, this._outBlock);
++++++++
++++++++ // increment counter (input block)
++++++++ inc32(this._inBlock);
++++++++};
++++++++
++++++++modes.gcm.prototype.decrypt = function(input, output, finish) {
++++++++ // not enough input to decrypt
++++++++ var inputLength = input.length();
++++++++ if(inputLength < this.blockSize && !(finish && inputLength > 0)) {
++++++++ return true;
++++++++ }
++++++++
++++++++ // encrypt block (GCM always uses encryption mode)
++++++++ this.cipher.encrypt(this._inBlock, this._outBlock);
++++++++
++++++++ // increment counter (input block)
++++++++ inc32(this._inBlock);
++++++++
++++++++ // update hash block S
++++++++ this._hashBlock[0] = input.getInt32();
++++++++ this._hashBlock[1] = input.getInt32();
++++++++ this._hashBlock[2] = input.getInt32();
++++++++ this._hashBlock[3] = input.getInt32();
++++++++ this._s = this.ghash(this._hashSubkey, this._s, this._hashBlock);
++++++++
++++++++ // XOR hash input with output
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ output.putInt32(this._outBlock[i] ^ this._hashBlock[i]);
++++++++ }
++++++++
++++++++ // increment cipher data length
++++++++ if(inputLength < this.blockSize) {
++++++++ this._cipherLength += inputLength % this.blockSize;
++++++++ } else {
++++++++ this._cipherLength += this.blockSize;
++++++++ }
++++++++};
++++++++
++++++++modes.gcm.prototype.afterFinish = function(output, options) {
++++++++ var rval = true;
++++++++
++++++++ // handle overflow
++++++++ if(options.decrypt && options.overflow) {
++++++++ output.truncate(this.blockSize - options.overflow);
++++++++ }
++++++++
++++++++ // handle authentication tag
++++++++ this.tag = forge.util.createBuffer();
++++++++
++++++++ // concatenate additional data length with cipher length
++++++++ var lengths = this._aDataLength.concat(from64To32(this._cipherLength * 8));
++++++++
++++++++ // include lengths in hash
++++++++ this._s = this.ghash(this._hashSubkey, this._s, lengths);
++++++++
++++++++ // do GCTR(J_0, S)
++++++++ var tag = [];
++++++++ this.cipher.encrypt(this._j0, tag);
++++++++ for(var i = 0; i < this._ints; ++i) {
++++++++ this.tag.putInt32(this._s[i] ^ tag[i]);
++++++++ }
++++++++
++++++++ // trim tag to length
++++++++ this.tag.truncate(this.tag.length() % (this._tagLength / 8));
++++++++
++++++++ // check authentication tag
++++++++ if(options.decrypt && this.tag.bytes() !== this._tag) {
++++++++ rval = false;
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * See NIST SP-800-38D 6.3 (Algorithm 1). This function performs Galois
++++++++ * field multiplication. The field, GF(2^128), is defined by the polynomial:
++++++++ *
++++++++ * x^128 + x^7 + x^2 + x + 1
++++++++ *
++++++++ * Which is represented in little-endian binary form as: 11100001 (0xe1). When
++++++++ * the value of a coefficient is 1, a bit is set. The value R, is the
++++++++ * concatenation of this value and 120 zero bits, yielding a 128-bit value
++++++++ * which matches the block size.
++++++++ *
++++++++ * This function will multiply two elements (vectors of bytes), X and Y, in
++++++++ * the field GF(2^128). The result is initialized to zero. For each bit of
++++++++ * X (out of 128), x_i, if x_i is set, then the result is multiplied (XOR'd)
++++++++ * by the current value of Y. For each bit, the value of Y will be raised by
++++++++ * a power of x (multiplied by the polynomial x). This can be achieved by
++++++++ * shifting Y once to the right. If the current value of Y, prior to being
++++++++ * multiplied by x, has 0 as its LSB, then it is a 127th degree polynomial.
++++++++ * Otherwise, we must divide by R after shifting to find the remainder.
++++++++ *
++++++++ * @param x the first block to multiply by the second.
++++++++ * @param y the second block to multiply by the first.
++++++++ *
++++++++ * @return the block result of the multiplication.
++++++++ */
++++++++modes.gcm.prototype.multiply = function(x, y) {
++++++++ var z_i = [0, 0, 0, 0];
++++++++ var v_i = y.slice(0);
++++++++
++++++++ // calculate Z_128 (block has 128 bits)
++++++++ for(var i = 0; i < 128; ++i) {
++++++++ // if x_i is 0, Z_{i+1} = Z_i (unchanged)
++++++++ // else Z_{i+1} = Z_i ^ V_i
++++++++ // get x_i by finding 32-bit int position, then left shift 1 by remainder
++++++++ var x_i = x[(i / 32) | 0] & (1 << (31 - i % 32));
++++++++ if(x_i) {
++++++++ z_i[0] ^= v_i[0];
++++++++ z_i[1] ^= v_i[1];
++++++++ z_i[2] ^= v_i[2];
++++++++ z_i[3] ^= v_i[3];
++++++++ }
++++++++
++++++++ // if LSB(V_i) is 1, V_i = V_i >> 1
++++++++ // else V_i = (V_i >> 1) ^ R
++++++++ this.pow(v_i, v_i);
++++++++ }
++++++++
++++++++ return z_i;
++++++++};
++++++++
++++++++modes.gcm.prototype.pow = function(x, out) {
++++++++ // if LSB(x) is 1, x = x >>> 1
++++++++ // else x = (x >>> 1) ^ R
++++++++ var lsb = x[3] & 1;
++++++++
++++++++ // always do x >>> 1:
++++++++ // starting with the rightmost integer, shift each integer to the right
++++++++ // one bit, pulling in the bit from the integer to the left as its top
++++++++ // most bit (do this for the last 3 integers)
++++++++ for(var i = 3; i > 0; --i) {
++++++++ out[i] = (x[i] >>> 1) | ((x[i - 1] & 1) << 31);
++++++++ }
++++++++ // shift the first integer normally
++++++++ out[0] = x[0] >>> 1;
++++++++
++++++++ // if lsb was not set, then polynomial had a degree of 127 and doesn't
++++++++ // need to divided; otherwise, XOR with R to find the remainder; we only
++++++++ // need to XOR the first integer since R technically ends w/120 zero bits
++++++++ if(lsb) {
++++++++ out[0] ^= this._R;
++++++++ }
++++++++};
++++++++
++++++++modes.gcm.prototype.tableMultiply = function(x) {
++++++++ // assumes 4-bit tables are used
++++++++ var z = [0, 0, 0, 0];
++++++++ for(var i = 0; i < 32; ++i) {
++++++++ var idx = (i / 8) | 0;
++++++++ var x_i = (x[idx] >>> ((7 - (i % 8)) * 4)) & 0xF;
++++++++ var ah = this._m[i][x_i];
++++++++ z[0] ^= ah[0];
++++++++ z[1] ^= ah[1];
++++++++ z[2] ^= ah[2];
++++++++ z[3] ^= ah[3];
++++++++ }
++++++++ return z;
++++++++};
++++++++
++++++++/**
++++++++ * A continuing version of the GHASH algorithm that operates on a single
++++++++ * block. The hash block, last hash value (Ym) and the new block to hash
++++++++ * are given.
++++++++ *
++++++++ * @param h the hash block.
++++++++ * @param y the previous value for Ym, use [0, 0, 0, 0] for a new hash.
++++++++ * @param x the block to hash.
++++++++ *
++++++++ * @return the hashed value (Ym).
++++++++ */
++++++++modes.gcm.prototype.ghash = function(h, y, x) {
++++++++ y[0] ^= x[0];
++++++++ y[1] ^= x[1];
++++++++ y[2] ^= x[2];
++++++++ y[3] ^= x[3];
++++++++ return this.tableMultiply(y);
++++++++ //return this.multiply(y, h);
++++++++};
++++++++
++++++++/**
++++++++ * Precomputes a table for multiplying against the hash subkey. This
++++++++ * mechanism provides a substantial speed increase over multiplication
++++++++ * performed without a table. The table-based multiplication this table is
++++++++ * for solves X * H by multiplying each component of X by H and then
++++++++ * composing the results together using XOR.
++++++++ *
++++++++ * This function can be used to generate tables with different bit sizes
++++++++ * for the components, however, this implementation assumes there are
++++++++ * 32 components of X (which is a 16 byte vector), therefore each component
++++++++ * takes 4-bits (so the table is constructed with bits=4).
++++++++ *
++++++++ * @param h the hash subkey.
++++++++ * @param bits the bit size for a component.
++++++++ */
++++++++modes.gcm.prototype.generateHashTable = function(h, bits) {
++++++++ // TODO: There are further optimizations that would use only the
++++++++ // first table M_0 (or some variant) along with a remainder table;
++++++++ // this can be explored in the future
++++++++ var multiplier = 8 / bits;
++++++++ var perInt = 4 * multiplier;
++++++++ var size = 16 * multiplier;
++++++++ var m = new Array(size);
++++++++ for(var i = 0; i < size; ++i) {
++++++++ var tmp = [0, 0, 0, 0];
++++++++ var idx = (i / perInt) | 0;
++++++++ var shft = ((perInt - 1 - (i % perInt)) * bits);
++++++++ tmp[idx] = (1 << (bits - 1)) << shft;
++++++++ m[i] = this.generateSubHashTable(this.multiply(tmp, h), bits);
++++++++ }
++++++++ return m;
++++++++};
++++++++
++++++++/**
++++++++ * Generates a table for multiplying against the hash subkey for one
++++++++ * particular component (out of all possible component values).
++++++++ *
++++++++ * @param mid the pre-multiplied value for the middle key of the table.
++++++++ * @param bits the bit size for a component.
++++++++ */
++++++++modes.gcm.prototype.generateSubHashTable = function(mid, bits) {
++++++++ // compute the table quickly by minimizing the number of
++++++++ // POW operations -- they only need to be performed for powers of 2,
++++++++ // all other entries can be composed from those powers using XOR
++++++++ var size = 1 << bits;
++++++++ var half = size >>> 1;
++++++++ var m = new Array(size);
++++++++ m[half] = mid.slice(0);
++++++++ var i = half >>> 1;
++++++++ while(i > 0) {
++++++++ // raise m0[2 * i] and store in m0[i]
++++++++ this.pow(m[2 * i], m[i] = []);
++++++++ i >>= 1;
++++++++ }
++++++++ i = 2;
++++++++ while(i < half) {
++++++++ for(var j = 1; j < i; ++j) {
++++++++ var m_i = m[i];
++++++++ var m_j = m[j];
++++++++ m[i + j] = [
++++++++ m_i[0] ^ m_j[0],
++++++++ m_i[1] ^ m_j[1],
++++++++ m_i[2] ^ m_j[2],
++++++++ m_i[3] ^ m_j[3]
++++++++ ];
++++++++ }
++++++++ i *= 2;
++++++++ }
++++++++ m[0] = [0, 0, 0, 0];
++++++++ /* Note: We could avoid storing these by doing composition during multiply
++++++++ calculate top half using composition by speed is preferred. */
++++++++ for(i = half + 1; i < size; ++i) {
++++++++ var c = m[i ^ half];
++++++++ m[i] = [mid[0] ^ c[0], mid[1] ^ c[1], mid[2] ^ c[2], mid[3] ^ c[3]];
++++++++ }
++++++++ return m;
++++++++};
++++++++
++++++++/** Utility functions */
++++++++
++++++++function transformIV(iv, blockSize) {
++++++++ if(typeof iv === 'string') {
++++++++ // convert iv string into byte buffer
++++++++ iv = forge.util.createBuffer(iv);
++++++++ }
++++++++
++++++++ if(forge.util.isArray(iv) && iv.length > 4) {
++++++++ // convert iv byte array into byte buffer
++++++++ var tmp = iv;
++++++++ iv = forge.util.createBuffer();
++++++++ for(var i = 0; i < tmp.length; ++i) {
++++++++ iv.putByte(tmp[i]);
++++++++ }
++++++++ }
++++++++
++++++++ if(iv.length() < blockSize) {
++++++++ throw new Error(
++++++++ 'Invalid IV length; got ' + iv.length() +
++++++++ ' bytes and expected ' + blockSize + ' bytes.');
++++++++ }
++++++++
++++++++ if(!forge.util.isArray(iv)) {
++++++++ // convert iv byte buffer into 32-bit integer array
++++++++ var ints = [];
++++++++ var blocks = blockSize / 4;
++++++++ for(var i = 0; i < blocks; ++i) {
++++++++ ints.push(iv.getInt32());
++++++++ }
++++++++ iv = ints;
++++++++ }
++++++++
++++++++ return iv;
++++++++}
++++++++
++++++++function inc32(block) {
++++++++ // increment last 32 bits of block only
++++++++ block[block.length - 1] = (block[block.length - 1] + 1) & 0xFFFFFFFF;
++++++++}
++++++++
++++++++function from64To32(num) {
++++++++ // convert 64-bit number to two BE Int32s
++++++++ return [(num / 0x100000000) | 0, num & 0xFFFFFFFF];
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * DES (Data Encryption Standard) implementation.
++++++++ *
++++++++ * This implementation supports DES as well as 3DES-EDE in ECB and CBC mode.
++++++++ * It is based on the BSD-licensed implementation by Paul Tero:
++++++++ *
++++++++ * Paul Tero, July 2001
++++++++ * http://www.tero.co.uk/des/
++++++++ *
++++++++ * Optimised for performance with large blocks by
++++++++ * Michael Hayworth, November 2001
++++++++ * http://www.netdealing.com
++++++++ *
++++++++ * THIS SOFTWARE IS PROVIDED "AS IS" AND
++++++++ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++++++++ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++++++++ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
++++++++ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++++++++ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++++++++ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++++++++ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++++++++ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++++++++ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++++++++ * SUCH DAMAGE.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ * Copyright (c) 2012-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./cipher');
++++++++require('./cipherModes');
++++++++require('./util');
++++++++
++++++++/* DES API */
++++++++module.exports = forge.des = forge.des || {};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var cipher = forge.cipher.createCipher('DES-<mode>', key);
++++++++ * cipher.start({iv: iv});
++++++++ *
++++++++ * Creates an DES cipher object to encrypt data using the given symmetric key.
++++++++ * The output will be stored in the 'output' member of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as binary-encoded strings of bytes or
++++++++ * byte buffers.
++++++++ *
++++++++ * @param key the symmetric key to use (64 or 192 bits).
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ * @param mode the cipher mode to use (default: 'CBC' if IV is
++++++++ * given, 'ECB' if null).
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.des.startEncrypting = function(key, iv, output, mode) {
++++++++ var cipher = _createCipher({
++++++++ key: key,
++++++++ output: output,
++++++++ decrypt: false,
++++++++ mode: mode || (iv === null ? 'ECB' : 'CBC')
++++++++ });
++++++++ cipher.start(iv);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var cipher = forge.cipher.createCipher('DES-<mode>', key);
++++++++ *
++++++++ * Creates an DES cipher object to encrypt data using the given symmetric key.
++++++++ *
++++++++ * The key may be given as a binary-encoded string of bytes or a byte buffer.
++++++++ *
++++++++ * @param key the symmetric key to use (64 or 192 bits).
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.des.createEncryptionCipher = function(key, mode) {
++++++++ return _createCipher({
++++++++ key: key,
++++++++ output: null,
++++++++ decrypt: false,
++++++++ mode: mode
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var decipher = forge.cipher.createDecipher('DES-<mode>', key);
++++++++ * decipher.start({iv: iv});
++++++++ *
++++++++ * Creates an DES cipher object to decrypt data using the given symmetric key.
++++++++ * The output will be stored in the 'output' member of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as binary-encoded strings of bytes or
++++++++ * byte buffers.
++++++++ *
++++++++ * @param key the symmetric key to use (64 or 192 bits).
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ * @param mode the cipher mode to use (default: 'CBC' if IV is
++++++++ * given, 'ECB' if null).
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.des.startDecrypting = function(key, iv, output, mode) {
++++++++ var cipher = _createCipher({
++++++++ key: key,
++++++++ output: output,
++++++++ decrypt: true,
++++++++ mode: mode || (iv === null ? 'ECB' : 'CBC')
++++++++ });
++++++++ cipher.start(iv);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * var decipher = forge.cipher.createDecipher('DES-<mode>', key);
++++++++ *
++++++++ * Creates an DES cipher object to decrypt data using the given symmetric key.
++++++++ *
++++++++ * The key may be given as a binary-encoded string of bytes or a byte buffer.
++++++++ *
++++++++ * @param key the symmetric key to use (64 or 192 bits).
++++++++ * @param mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.des.createDecryptionCipher = function(key, mode) {
++++++++ return _createCipher({
++++++++ key: key,
++++++++ output: null,
++++++++ decrypt: true,
++++++++ mode: mode
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new DES cipher algorithm object.
++++++++ *
++++++++ * @param name the name of the algorithm.
++++++++ * @param mode the mode factory function.
++++++++ *
++++++++ * @return the DES algorithm object.
++++++++ */
++++++++forge.des.Algorithm = function(name, mode) {
++++++++ var self = this;
++++++++ self.name = name;
++++++++ self.mode = new mode({
++++++++ blockSize: 8,
++++++++ cipher: {
++++++++ encrypt: function(inBlock, outBlock) {
++++++++ return _updateBlock(self._keys, inBlock, outBlock, false);
++++++++ },
++++++++ decrypt: function(inBlock, outBlock) {
++++++++ return _updateBlock(self._keys, inBlock, outBlock, true);
++++++++ }
++++++++ }
++++++++ });
++++++++ self._init = false;
++++++++};
++++++++
++++++++/**
++++++++ * Initializes this DES algorithm by expanding its key.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * key the key to use with this algorithm.
++++++++ * decrypt true if the algorithm should be initialized for decryption,
++++++++ * false for encryption.
++++++++ */
++++++++forge.des.Algorithm.prototype.initialize = function(options) {
++++++++ if(this._init) {
++++++++ return;
++++++++ }
++++++++
++++++++ var key = forge.util.createBuffer(options.key);
++++++++ if(this.name.indexOf('3DES') === 0) {
++++++++ if(key.length() !== 24) {
++++++++ throw new Error('Invalid Triple-DES key size: ' + key.length() * 8);
++++++++ }
++++++++ }
++++++++
++++++++ // do key expansion to 16 or 48 subkeys (single or triple DES)
++++++++ this._keys = _createKeys(key);
++++++++ this._init = true;
++++++++};
++++++++
++++++++/** Register DES algorithms **/
++++++++
++++++++registerAlgorithm('DES-ECB', forge.cipher.modes.ecb);
++++++++registerAlgorithm('DES-CBC', forge.cipher.modes.cbc);
++++++++registerAlgorithm('DES-CFB', forge.cipher.modes.cfb);
++++++++registerAlgorithm('DES-OFB', forge.cipher.modes.ofb);
++++++++registerAlgorithm('DES-CTR', forge.cipher.modes.ctr);
++++++++
++++++++registerAlgorithm('3DES-ECB', forge.cipher.modes.ecb);
++++++++registerAlgorithm('3DES-CBC', forge.cipher.modes.cbc);
++++++++registerAlgorithm('3DES-CFB', forge.cipher.modes.cfb);
++++++++registerAlgorithm('3DES-OFB', forge.cipher.modes.ofb);
++++++++registerAlgorithm('3DES-CTR', forge.cipher.modes.ctr);
++++++++
++++++++function registerAlgorithm(name, mode) {
++++++++ var factory = function() {
++++++++ return new forge.des.Algorithm(name, mode);
++++++++ };
++++++++ forge.cipher.registerAlgorithm(name, factory);
++++++++}
++++++++
++++++++/** DES implementation **/
++++++++
++++++++var spfunction1 = [0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004];
++++++++var spfunction2 = [-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000];
++++++++var spfunction3 = [0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200];
++++++++var spfunction4 = [0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080];
++++++++var spfunction5 = [0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100];
++++++++var spfunction6 = [0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010];
++++++++var spfunction7 = [0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002];
++++++++var spfunction8 = [0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000];
++++++++
++++++++/**
++++++++ * Create necessary sub keys.
++++++++ *
++++++++ * @param key the 64-bit or 192-bit key.
++++++++ *
++++++++ * @return the expanded keys.
++++++++ */
++++++++function _createKeys(key) {
++++++++ var pc2bytes0 = [0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204],
++++++++ pc2bytes1 = [0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101],
++++++++ pc2bytes2 = [0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808],
++++++++ pc2bytes3 = [0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000],
++++++++ pc2bytes4 = [0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010],
++++++++ pc2bytes5 = [0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420],
++++++++ pc2bytes6 = [0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002],
++++++++ pc2bytes7 = [0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800],
++++++++ pc2bytes8 = [0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002],
++++++++ pc2bytes9 = [0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408],
++++++++ pc2bytes10 = [0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020],
++++++++ pc2bytes11 = [0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200],
++++++++ pc2bytes12 = [0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010],
++++++++ pc2bytes13 = [0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105];
++++++++
++++++++ // how many iterations (1 for des, 3 for triple des)
++++++++ // changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys
++++++++ var iterations = key.length() > 8 ? 3 : 1;
++++++++
++++++++ // stores the return keys
++++++++ var keys = [];
++++++++
++++++++ // now define the left shifts which need to be done
++++++++ var shifts = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
++++++++
++++++++ var n = 0, tmp;
++++++++ for(var j = 0; j < iterations; j++) {
++++++++ var left = key.getInt32();
++++++++ var right = key.getInt32();
++++++++
++++++++ tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 4);
++++++++
++++++++ tmp = ((right >>> -16) ^ left) & 0x0000ffff;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << -16);
++++++++
++++++++ tmp = ((left >>> 2) ^ right) & 0x33333333;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 2);
++++++++
++++++++ tmp = ((right >>> -16) ^ left) & 0x0000ffff;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << -16);
++++++++
++++++++ tmp = ((left >>> 1) ^ right) & 0x55555555;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 1);
++++++++
++++++++ tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << 8);
++++++++
++++++++ tmp = ((left >>> 1) ^ right) & 0x55555555;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 1);
++++++++
++++++++ // right needs to be shifted and OR'd with last four bits of left
++++++++ tmp = (left << 8) | ((right >>> 20) & 0x000000f0);
++++++++
++++++++ // left needs to be put upside down
++++++++ left = ((right << 24) | ((right << 8) & 0xff0000) |
++++++++ ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0));
++++++++ right = tmp;
++++++++
++++++++ // now go through and perform these shifts on the left and right keys
++++++++ for(var i = 0; i < shifts.length; ++i) {
++++++++ //shift the keys either one or two bits to the left
++++++++ if(shifts[i]) {
++++++++ left = (left << 2) | (left >>> 26);
++++++++ right = (right << 2) | (right >>> 26);
++++++++ } else {
++++++++ left = (left << 1) | (left >>> 27);
++++++++ right = (right << 1) | (right >>> 27);
++++++++ }
++++++++ left &= -0xf;
++++++++ right &= -0xf;
++++++++
++++++++ // now apply PC-2, in such a way that E is easier when encrypting or
++++++++ // decrypting this conversion will look like PC-2 except only the last 6
++++++++ // bits of each byte are used rather than 48 consecutive bits and the
++++++++ // order of lines will be according to how the S selection functions will
++++++++ // be applied: S2, S4, S6, S8, S1, S3, S5, S7
++++++++ var lefttmp = (
++++++++ pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] |
++++++++ pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf] |
++++++++ pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] |
++++++++ pc2bytes6[(left >>> 4) & 0xf]);
++++++++ var righttmp = (
++++++++ pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] |
++++++++ pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf] |
++++++++ pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] |
++++++++ pc2bytes13[(right >>> 4) & 0xf]);
++++++++ tmp = ((righttmp >>> 16) ^ lefttmp) & 0x0000ffff;
++++++++ keys[n++] = lefttmp ^ tmp;
++++++++ keys[n++] = righttmp ^ (tmp << 16);
++++++++ }
++++++++ }
++++++++
++++++++ return keys;
++++++++}
++++++++
++++++++/**
++++++++ * Updates a single block (1 byte) using DES. The update will either
++++++++ * encrypt or decrypt the block.
++++++++ *
++++++++ * @param keys the expanded keys.
++++++++ * @param input the input block (an array of 32-bit words).
++++++++ * @param output the updated output block.
++++++++ * @param decrypt true to decrypt the block, false to encrypt it.
++++++++ */
++++++++function _updateBlock(keys, input, output, decrypt) {
++++++++ // set up loops for single or triple DES
++++++++ var iterations = keys.length === 32 ? 3 : 9;
++++++++ var looping;
++++++++ if(iterations === 3) {
++++++++ looping = decrypt ? [30, -2, -2] : [0, 32, 2];
++++++++ } else {
++++++++ looping = (decrypt ?
++++++++ [94, 62, -2, 32, 64, 2, 30, -2, -2] :
++++++++ [0, 32, 2, 62, 30, -2, 64, 96, 2]);
++++++++ }
++++++++
++++++++ var tmp;
++++++++
++++++++ var left = input[0];
++++++++ var right = input[1];
++++++++
++++++++ // first each 64 bit chunk of the message must be permuted according to IP
++++++++ tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 4);
++++++++
++++++++ tmp = ((left >>> 16) ^ right) & 0x0000ffff;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 16);
++++++++
++++++++ tmp = ((right >>> 2) ^ left) & 0x33333333;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << 2);
++++++++
++++++++ tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << 8);
++++++++
++++++++ tmp = ((left >>> 1) ^ right) & 0x55555555;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 1);
++++++++
++++++++ // rotate left 1 bit
++++++++ left = ((left << 1) | (left >>> 31));
++++++++ right = ((right << 1) | (right >>> 31));
++++++++
++++++++ for(var j = 0; j < iterations; j += 3) {
++++++++ var endloop = looping[j + 1];
++++++++ var loopinc = looping[j + 2];
++++++++
++++++++ // now go through and perform the encryption or decryption
++++++++ for(var i = looping[j]; i != endloop; i += loopinc) {
++++++++ var right1 = right ^ keys[i];
++++++++ var right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
++++++++
++++++++ // passing these bytes through the S selection functions
++++++++ tmp = left;
++++++++ left = right;
++++++++ right = tmp ^ (
++++++++ spfunction2[(right1 >>> 24) & 0x3f] |
++++++++ spfunction4[(right1 >>> 16) & 0x3f] |
++++++++ spfunction6[(right1 >>> 8) & 0x3f] |
++++++++ spfunction8[right1 & 0x3f] |
++++++++ spfunction1[(right2 >>> 24) & 0x3f] |
++++++++ spfunction3[(right2 >>> 16) & 0x3f] |
++++++++ spfunction5[(right2 >>> 8) & 0x3f] |
++++++++ spfunction7[right2 & 0x3f]);
++++++++ }
++++++++ // unreverse left and right
++++++++ tmp = left;
++++++++ left = right;
++++++++ right = tmp;
++++++++ }
++++++++
++++++++ // rotate right 1 bit
++++++++ left = ((left >>> 1) | (left << 31));
++++++++ right = ((right >>> 1) | (right << 31));
++++++++
++++++++ // now perform IP-1, which is IP in the opposite direction
++++++++ tmp = ((left >>> 1) ^ right) & 0x55555555;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 1);
++++++++
++++++++ tmp = ((right >>> 8) ^ left) & 0x00ff00ff;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << 8);
++++++++
++++++++ tmp = ((right >>> 2) ^ left) & 0x33333333;
++++++++ left ^= tmp;
++++++++ right ^= (tmp << 2);
++++++++
++++++++ tmp = ((left >>> 16) ^ right) & 0x0000ffff;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 16);
++++++++
++++++++ tmp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
++++++++ right ^= tmp;
++++++++ left ^= (tmp << 4);
++++++++
++++++++ output[0] = left;
++++++++ output[1] = right;
++++++++}
++++++++
++++++++/**
++++++++ * Deprecated. Instead, use:
++++++++ *
++++++++ * forge.cipher.createCipher('DES-<mode>', key);
++++++++ * forge.cipher.createDecipher('DES-<mode>', key);
++++++++ *
++++++++ * Creates a deprecated DES cipher object. This object's mode will default to
++++++++ * CBC (cipher-block-chaining).
++++++++ *
++++++++ * The key may be given as a binary-encoded string of bytes or a byte buffer.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * key the symmetric key to use (64 or 192 bits).
++++++++ * output the buffer to write to.
++++++++ * decrypt true for decryption, false for encryption.
++++++++ * mode the cipher mode to use (default: 'CBC').
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++function _createCipher(options) {
++++++++ options = options || {};
++++++++ var mode = (options.mode || 'CBC').toUpperCase();
++++++++ var algorithm = 'DES-' + mode;
++++++++
++++++++ var cipher;
++++++++ if(options.decrypt) {
++++++++ cipher = forge.cipher.createDecipher(algorithm, options.key);
++++++++ } else {
++++++++ cipher = forge.cipher.createCipher(algorithm, options.key);
++++++++ }
++++++++
++++++++ // backwards compatible start API
++++++++ var start = cipher.start;
++++++++ cipher.start = function(iv, options) {
++++++++ // backwards compatibility: support second arg as output buffer
++++++++ var output = null;
++++++++ if(options instanceof forge.util.ByteBuffer) {
++++++++ output = options;
++++++++ options = {};
++++++++ }
++++++++ options = options || {};
++++++++ options.output = output;
++++++++ options.iv = iv;
++++++++ start.call(cipher, options);
++++++++ };
++++++++
++++++++ return cipher;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * JavaScript implementation of Ed25519.
++++++++ *
++++++++ * Copyright (c) 2017-2019 Digital Bazaar, Inc.
++++++++ *
++++++++ * This implementation is based on the most excellent TweetNaCl which is
++++++++ * in the public domain. Many thanks to its contributors:
++++++++ *
++++++++ * https://github.com/dchest/tweetnacl-js
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./jsbn');
++++++++require('./random');
++++++++require('./sha512');
++++++++require('./util');
++++++++var asn1Validator = require('./asn1-validator');
++++++++var publicKeyValidator = asn1Validator.publicKeyValidator;
++++++++var privateKeyValidator = asn1Validator.privateKeyValidator;
++++++++
++++++++if(typeof BigInteger === 'undefined') {
++++++++ var BigInteger = forge.jsbn.BigInteger;
++++++++}
++++++++
++++++++var ByteBuffer = forge.util.ByteBuffer;
++++++++var NativeBuffer = typeof Buffer === 'undefined' ? Uint8Array : Buffer;
++++++++
++++++++/*
++++++++ * Ed25519 algorithms, see RFC 8032:
++++++++ * https://tools.ietf.org/html/rfc8032
++++++++ */
++++++++forge.pki = forge.pki || {};
++++++++module.exports = forge.pki.ed25519 = forge.ed25519 = forge.ed25519 || {};
++++++++var ed25519 = forge.ed25519;
++++++++
++++++++ed25519.constants = {};
++++++++ed25519.constants.PUBLIC_KEY_BYTE_LENGTH = 32;
++++++++ed25519.constants.PRIVATE_KEY_BYTE_LENGTH = 64;
++++++++ed25519.constants.SEED_BYTE_LENGTH = 32;
++++++++ed25519.constants.SIGN_BYTE_LENGTH = 64;
++++++++ed25519.constants.HASH_BYTE_LENGTH = 64;
++++++++
++++++++ed25519.generateKeyPair = function(options) {
++++++++ options = options || {};
++++++++ var seed = options.seed;
++++++++ if(seed === undefined) {
++++++++ // generate seed
++++++++ seed = forge.random.getBytesSync(ed25519.constants.SEED_BYTE_LENGTH);
++++++++ } else if(typeof seed === 'string') {
++++++++ if(seed.length !== ed25519.constants.SEED_BYTE_LENGTH) {
++++++++ throw new TypeError(
++++++++ '"seed" must be ' + ed25519.constants.SEED_BYTE_LENGTH +
++++++++ ' bytes in length.');
++++++++ }
++++++++ } else if(!(seed instanceof Uint8Array)) {
++++++++ throw new TypeError(
++++++++ '"seed" must be a node.js Buffer, Uint8Array, or a binary string.');
++++++++ }
++++++++
++++++++ seed = messageToNativeBuffer({message: seed, encoding: 'binary'});
++++++++
++++++++ var pk = new NativeBuffer(ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
++++++++ var sk = new NativeBuffer(ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
++++++++ for(var i = 0; i < 32; ++i) {
++++++++ sk[i] = seed[i];
++++++++ }
++++++++ crypto_sign_keypair(pk, sk);
++++++++ return {publicKey: pk, privateKey: sk};
++++++++};
++++++++
++++++++/**
++++++++ * Converts a private key from a RFC8410 ASN.1 encoding.
++++++++ *
++++++++ * @param obj - The asn1 representation of a private key.
++++++++ *
++++++++ * @returns {Object} keyInfo - The key information.
++++++++ * @returns {Buffer|Uint8Array} keyInfo.privateKeyBytes - 32 private key bytes.
++++++++ */
++++++++ed25519.privateKeyFromAsn1 = function(obj) {
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ var valid = forge.asn1.validate(obj, privateKeyValidator, capture, errors);
++++++++ if(!valid) {
++++++++ var error = new Error('Invalid Key.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++ var oid = forge.asn1.derToOid(capture.privateKeyOid);
++++++++ var ed25519Oid = forge.oids.EdDSA25519;
++++++++ if(oid !== ed25519Oid) {
++++++++ throw new Error('Invalid OID "' + oid + '"; OID must be "' +
++++++++ ed25519Oid + '".');
++++++++ }
++++++++ var privateKey = capture.privateKey;
++++++++ // manually extract the private key bytes from nested octet string, see FIXME:
++++++++ // https://github.com/digitalbazaar/forge/blob/master/lib/asn1.js#L542
++++++++ var privateKeyBytes = messageToNativeBuffer({
++++++++ message: forge.asn1.fromDer(privateKey).value,
++++++++ encoding: 'binary'
++++++++ });
++++++++ // TODO: RFC8410 specifies a format for encoding the public key bytes along
++++++++ // with the private key bytes. `publicKeyBytes` can be returned in the
++++++++ // future. https://tools.ietf.org/html/rfc8410#section-10.3
++++++++ return {privateKeyBytes: privateKeyBytes};
++++++++};
++++++++
++++++++/**
++++++++ * Converts a public key from a RFC8410 ASN.1 encoding.
++++++++ *
++++++++ * @param obj - The asn1 representation of a public key.
++++++++ *
++++++++ * @return {Buffer|Uint8Array} - 32 public key bytes.
++++++++ */
++++++++ed25519.publicKeyFromAsn1 = function(obj) {
++++++++ // get SubjectPublicKeyInfo
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ var valid = forge.asn1.validate(obj, publicKeyValidator, capture, errors);
++++++++ if(!valid) {
++++++++ var error = new Error('Invalid Key.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++ var oid = forge.asn1.derToOid(capture.publicKeyOid);
++++++++ var ed25519Oid = forge.oids.EdDSA25519;
++++++++ if(oid !== ed25519Oid) {
++++++++ throw new Error('Invalid OID "' + oid + '"; OID must be "' +
++++++++ ed25519Oid + '".');
++++++++ }
++++++++ var publicKeyBytes = capture.ed25519PublicKey;
++++++++ if(publicKeyBytes.length !== ed25519.constants.PUBLIC_KEY_BYTE_LENGTH) {
++++++++ throw new Error('Key length is invalid.');
++++++++ }
++++++++ return messageToNativeBuffer({
++++++++ message: publicKeyBytes,
++++++++ encoding: 'binary'
++++++++ });
++++++++};
++++++++
++++++++ed25519.publicKeyFromPrivateKey = function(options) {
++++++++ options = options || {};
++++++++ var privateKey = messageToNativeBuffer({
++++++++ message: options.privateKey, encoding: 'binary'
++++++++ });
++++++++ if(privateKey.length !== ed25519.constants.PRIVATE_KEY_BYTE_LENGTH) {
++++++++ throw new TypeError(
++++++++ '"options.privateKey" must have a byte length of ' +
++++++++ ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
++++++++ }
++++++++
++++++++ var pk = new NativeBuffer(ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
++++++++ for(var i = 0; i < pk.length; ++i) {
++++++++ pk[i] = privateKey[32 + i];
++++++++ }
++++++++ return pk;
++++++++};
++++++++
++++++++ed25519.sign = function(options) {
++++++++ options = options || {};
++++++++ var msg = messageToNativeBuffer(options);
++++++++ var privateKey = messageToNativeBuffer({
++++++++ message: options.privateKey,
++++++++ encoding: 'binary'
++++++++ });
++++++++ if(privateKey.length === ed25519.constants.SEED_BYTE_LENGTH) {
++++++++ var keyPair = ed25519.generateKeyPair({seed: privateKey});
++++++++ privateKey = keyPair.privateKey;
++++++++ } else if(privateKey.length !== ed25519.constants.PRIVATE_KEY_BYTE_LENGTH) {
++++++++ throw new TypeError(
++++++++ '"options.privateKey" must have a byte length of ' +
++++++++ ed25519.constants.SEED_BYTE_LENGTH + ' or ' +
++++++++ ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
++++++++ }
++++++++
++++++++ var signedMsg = new NativeBuffer(
++++++++ ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
++++++++ crypto_sign(signedMsg, msg, msg.length, privateKey);
++++++++
++++++++ var sig = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH);
++++++++ for(var i = 0; i < sig.length; ++i) {
++++++++ sig[i] = signedMsg[i];
++++++++ }
++++++++ return sig;
++++++++};
++++++++
++++++++ed25519.verify = function(options) {
++++++++ options = options || {};
++++++++ var msg = messageToNativeBuffer(options);
++++++++ if(options.signature === undefined) {
++++++++ throw new TypeError(
++++++++ '"options.signature" must be a node.js Buffer, a Uint8Array, a forge ' +
++++++++ 'ByteBuffer, or a binary string.');
++++++++ }
++++++++ var sig = messageToNativeBuffer({
++++++++ message: options.signature,
++++++++ encoding: 'binary'
++++++++ });
++++++++ if(sig.length !== ed25519.constants.SIGN_BYTE_LENGTH) {
++++++++ throw new TypeError(
++++++++ '"options.signature" must have a byte length of ' +
++++++++ ed25519.constants.SIGN_BYTE_LENGTH);
++++++++ }
++++++++ var publicKey = messageToNativeBuffer({
++++++++ message: options.publicKey,
++++++++ encoding: 'binary'
++++++++ });
++++++++ if(publicKey.length !== ed25519.constants.PUBLIC_KEY_BYTE_LENGTH) {
++++++++ throw new TypeError(
++++++++ '"options.publicKey" must have a byte length of ' +
++++++++ ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
++++++++ }
++++++++
++++++++ var sm = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
++++++++ var m = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
++++++++ var i;
++++++++ for(i = 0; i < ed25519.constants.SIGN_BYTE_LENGTH; ++i) {
++++++++ sm[i] = sig[i];
++++++++ }
++++++++ for(i = 0; i < msg.length; ++i) {
++++++++ sm[i + ed25519.constants.SIGN_BYTE_LENGTH] = msg[i];
++++++++ }
++++++++ return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
++++++++};
++++++++
++++++++function messageToNativeBuffer(options) {
++++++++ var message = options.message;
++++++++ if(message instanceof Uint8Array || message instanceof NativeBuffer) {
++++++++ return message;
++++++++ }
++++++++
++++++++ var encoding = options.encoding;
++++++++ if(message === undefined) {
++++++++ if(options.md) {
++++++++ // TODO: more rigorous validation that `md` is a MessageDigest
++++++++ message = options.md.digest().getBytes();
++++++++ encoding = 'binary';
++++++++ } else {
++++++++ throw new TypeError('"options.message" or "options.md" not specified.');
++++++++ }
++++++++ }
++++++++
++++++++ if(typeof message === 'string' && !encoding) {
++++++++ throw new TypeError('"options.encoding" must be "binary" or "utf8".');
++++++++ }
++++++++
++++++++ if(typeof message === 'string') {
++++++++ if(typeof Buffer !== 'undefined') {
++++++++ return Buffer.from(message, encoding);
++++++++ }
++++++++ message = new ByteBuffer(message, encoding);
++++++++ } else if(!(message instanceof ByteBuffer)) {
++++++++ throw new TypeError(
++++++++ '"options.message" must be a node.js Buffer, a Uint8Array, a forge ' +
++++++++ 'ByteBuffer, or a string with "options.encoding" specifying its ' +
++++++++ 'encoding.');
++++++++ }
++++++++
++++++++ // convert to native buffer
++++++++ var buffer = new NativeBuffer(message.length());
++++++++ for(var i = 0; i < buffer.length; ++i) {
++++++++ buffer[i] = message.at(i);
++++++++ }
++++++++ return buffer;
++++++++}
++++++++
++++++++var gf0 = gf();
++++++++var gf1 = gf([1]);
++++++++var D = gf([
++++++++ 0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070,
++++++++ 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]);
++++++++var D2 = gf([
++++++++ 0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0,
++++++++ 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]);
++++++++var X = gf([
++++++++ 0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c,
++++++++ 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]);
++++++++var Y = gf([
++++++++ 0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666,
++++++++ 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]);
++++++++var L = new Float64Array([
++++++++ 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
++++++++ 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
++++++++var I = gf([
++++++++ 0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43,
++++++++ 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
++++++++
++++++++// TODO: update forge buffer implementation to use `Buffer` or `Uint8Array`,
++++++++// whichever is available, to improve performance
++++++++function sha512(msg, msgLen) {
++++++++ // Note: `out` and `msg` are NativeBuffer
++++++++ var md = forge.md.sha512.create();
++++++++ var buffer = new ByteBuffer(msg);
++++++++ md.update(buffer.getBytes(msgLen), 'binary');
++++++++ var hash = md.digest().getBytes();
++++++++ if(typeof Buffer !== 'undefined') {
++++++++ return Buffer.from(hash, 'binary');
++++++++ }
++++++++ var out = new NativeBuffer(ed25519.constants.HASH_BYTE_LENGTH);
++++++++ for(var i = 0; i < 64; ++i) {
++++++++ out[i] = hash.charCodeAt(i);
++++++++ }
++++++++ return out;
++++++++}
++++++++
++++++++function crypto_sign_keypair(pk, sk) {
++++++++ var p = [gf(), gf(), gf(), gf()];
++++++++ var i;
++++++++
++++++++ var d = sha512(sk, 32);
++++++++ d[0] &= 248;
++++++++ d[31] &= 127;
++++++++ d[31] |= 64;
++++++++
++++++++ scalarbase(p, d);
++++++++ pack(pk, p);
++++++++
++++++++ for(i = 0; i < 32; ++i) {
++++++++ sk[i + 32] = pk[i];
++++++++ }
++++++++ return 0;
++++++++}
++++++++
++++++++// Note: difference from C - smlen returned, not passed as argument.
++++++++function crypto_sign(sm, m, n, sk) {
++++++++ var i, j, x = new Float64Array(64);
++++++++ var p = [gf(), gf(), gf(), gf()];
++++++++
++++++++ var d = sha512(sk, 32);
++++++++ d[0] &= 248;
++++++++ d[31] &= 127;
++++++++ d[31] |= 64;
++++++++
++++++++ var smlen = n + 64;
++++++++ for(i = 0; i < n; ++i) {
++++++++ sm[64 + i] = m[i];
++++++++ }
++++++++ for(i = 0; i < 32; ++i) {
++++++++ sm[32 + i] = d[32 + i];
++++++++ }
++++++++
++++++++ var r = sha512(sm.subarray(32), n + 32);
++++++++ reduce(r);
++++++++ scalarbase(p, r);
++++++++ pack(sm, p);
++++++++
++++++++ for(i = 32; i < 64; ++i) {
++++++++ sm[i] = sk[i];
++++++++ }
++++++++ var h = sha512(sm, n + 64);
++++++++ reduce(h);
++++++++
++++++++ for(i = 32; i < 64; ++i) {
++++++++ x[i] = 0;
++++++++ }
++++++++ for(i = 0; i < 32; ++i) {
++++++++ x[i] = r[i];
++++++++ }
++++++++ for(i = 0; i < 32; ++i) {
++++++++ for(j = 0; j < 32; j++) {
++++++++ x[i + j] += h[i] * d[j];
++++++++ }
++++++++ }
++++++++
++++++++ modL(sm.subarray(32), x);
++++++++ return smlen;
++++++++}
++++++++
++++++++function crypto_sign_open(m, sm, n, pk) {
++++++++ var i, mlen;
++++++++ var t = new NativeBuffer(32);
++++++++ var p = [gf(), gf(), gf(), gf()],
++++++++ q = [gf(), gf(), gf(), gf()];
++++++++
++++++++ mlen = -1;
++++++++ if(n < 64) {
++++++++ return -1;
++++++++ }
++++++++
++++++++ if(unpackneg(q, pk)) {
++++++++ return -1;
++++++++ }
++++++++
++++++++ for(i = 0; i < n; ++i) {
++++++++ m[i] = sm[i];
++++++++ }
++++++++ for(i = 0; i < 32; ++i) {
++++++++ m[i + 32] = pk[i];
++++++++ }
++++++++ var h = sha512(m, n);
++++++++ reduce(h);
++++++++ scalarmult(p, q, h);
++++++++
++++++++ scalarbase(q, sm.subarray(32));
++++++++ add(p, q);
++++++++ pack(t, p);
++++++++
++++++++ n -= 64;
++++++++ if(crypto_verify_32(sm, 0, t, 0)) {
++++++++ for(i = 0; i < n; ++i) {
++++++++ m[i] = 0;
++++++++ }
++++++++ return -1;
++++++++ }
++++++++
++++++++ for(i = 0; i < n; ++i) {
++++++++ m[i] = sm[i + 64];
++++++++ }
++++++++ mlen = n;
++++++++ return mlen;
++++++++}
++++++++
++++++++function modL(r, x) {
++++++++ var carry, i, j, k;
++++++++ for(i = 63; i >= 32; --i) {
++++++++ carry = 0;
++++++++ for(j = i - 32, k = i - 12; j < k; ++j) {
++++++++ x[j] += carry - 16 * x[i] * L[j - (i - 32)];
++++++++ carry = (x[j] + 128) >> 8;
++++++++ x[j] -= carry * 256;
++++++++ }
++++++++ x[j] += carry;
++++++++ x[i] = 0;
++++++++ }
++++++++ carry = 0;
++++++++ for(j = 0; j < 32; ++j) {
++++++++ x[j] += carry - (x[31] >> 4) * L[j];
++++++++ carry = x[j] >> 8;
++++++++ x[j] &= 255;
++++++++ }
++++++++ for(j = 0; j < 32; ++j) {
++++++++ x[j] -= carry * L[j];
++++++++ }
++++++++ for(i = 0; i < 32; ++i) {
++++++++ x[i + 1] += x[i] >> 8;
++++++++ r[i] = x[i] & 255;
++++++++ }
++++++++}
++++++++
++++++++function reduce(r) {
++++++++ var x = new Float64Array(64);
++++++++ for(var i = 0; i < 64; ++i) {
++++++++ x[i] = r[i];
++++++++ r[i] = 0;
++++++++ }
++++++++ modL(r, x);
++++++++}
++++++++
++++++++function add(p, q) {
++++++++ var a = gf(), b = gf(), c = gf(),
++++++++ d = gf(), e = gf(), f = gf(),
++++++++ g = gf(), h = gf(), t = gf();
++++++++
++++++++ Z(a, p[1], p[0]);
++++++++ Z(t, q[1], q[0]);
++++++++ M(a, a, t);
++++++++ A(b, p[0], p[1]);
++++++++ A(t, q[0], q[1]);
++++++++ M(b, b, t);
++++++++ M(c, p[3], q[3]);
++++++++ M(c, c, D2);
++++++++ M(d, p[2], q[2]);
++++++++ A(d, d, d);
++++++++ Z(e, b, a);
++++++++ Z(f, d, c);
++++++++ A(g, d, c);
++++++++ A(h, b, a);
++++++++
++++++++ M(p[0], e, f);
++++++++ M(p[1], h, g);
++++++++ M(p[2], g, f);
++++++++ M(p[3], e, h);
++++++++}
++++++++
++++++++function cswap(p, q, b) {
++++++++ for(var i = 0; i < 4; ++i) {
++++++++ sel25519(p[i], q[i], b);
++++++++ }
++++++++}
++++++++
++++++++function pack(r, p) {
++++++++ var tx = gf(), ty = gf(), zi = gf();
++++++++ inv25519(zi, p[2]);
++++++++ M(tx, p[0], zi);
++++++++ M(ty, p[1], zi);
++++++++ pack25519(r, ty);
++++++++ r[31] ^= par25519(tx) << 7;
++++++++}
++++++++
++++++++function pack25519(o, n) {
++++++++ var i, j, b;
++++++++ var m = gf(), t = gf();
++++++++ for(i = 0; i < 16; ++i) {
++++++++ t[i] = n[i];
++++++++ }
++++++++ car25519(t);
++++++++ car25519(t);
++++++++ car25519(t);
++++++++ for(j = 0; j < 2; ++j) {
++++++++ m[0] = t[0] - 0xffed;
++++++++ for(i = 1; i < 15; ++i) {
++++++++ m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
++++++++ m[i-1] &= 0xffff;
++++++++ }
++++++++ m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
++++++++ b = (m[15] >> 16) & 1;
++++++++ m[14] &= 0xffff;
++++++++ sel25519(t, m, 1 - b);
++++++++ }
++++++++ for (i = 0; i < 16; i++) {
++++++++ o[2 * i] = t[i] & 0xff;
++++++++ o[2 * i + 1] = t[i] >> 8;
++++++++ }
++++++++}
++++++++
++++++++function unpackneg(r, p) {
++++++++ var t = gf(), chk = gf(), num = gf(),
++++++++ den = gf(), den2 = gf(), den4 = gf(),
++++++++ den6 = gf();
++++++++
++++++++ set25519(r[2], gf1);
++++++++ unpack25519(r[1], p);
++++++++ S(num, r[1]);
++++++++ M(den, num, D);
++++++++ Z(num, num, r[2]);
++++++++ A(den, r[2], den);
++++++++
++++++++ S(den2, den);
++++++++ S(den4, den2);
++++++++ M(den6, den4, den2);
++++++++ M(t, den6, num);
++++++++ M(t, t, den);
++++++++
++++++++ pow2523(t, t);
++++++++ M(t, t, num);
++++++++ M(t, t, den);
++++++++ M(t, t, den);
++++++++ M(r[0], t, den);
++++++++
++++++++ S(chk, r[0]);
++++++++ M(chk, chk, den);
++++++++ if(neq25519(chk, num)) {
++++++++ M(r[0], r[0], I);
++++++++ }
++++++++
++++++++ S(chk, r[0]);
++++++++ M(chk, chk, den);
++++++++ if(neq25519(chk, num)) {
++++++++ return -1;
++++++++ }
++++++++
++++++++ if(par25519(r[0]) === (p[31] >> 7)) {
++++++++ Z(r[0], gf0, r[0]);
++++++++ }
++++++++
++++++++ M(r[3], r[0], r[1]);
++++++++ return 0;
++++++++}
++++++++
++++++++function unpack25519(o, n) {
++++++++ var i;
++++++++ for(i = 0; i < 16; ++i) {
++++++++ o[i] = n[2 * i] + (n[2 * i + 1] << 8);
++++++++ }
++++++++ o[15] &= 0x7fff;
++++++++}
++++++++
++++++++function pow2523(o, i) {
++++++++ var c = gf();
++++++++ var a;
++++++++ for(a = 0; a < 16; ++a) {
++++++++ c[a] = i[a];
++++++++ }
++++++++ for(a = 250; a >= 0; --a) {
++++++++ S(c, c);
++++++++ if(a !== 1) {
++++++++ M(c, c, i);
++++++++ }
++++++++ }
++++++++ for(a = 0; a < 16; ++a) {
++++++++ o[a] = c[a];
++++++++ }
++++++++}
++++++++
++++++++function neq25519(a, b) {
++++++++ var c = new NativeBuffer(32);
++++++++ var d = new NativeBuffer(32);
++++++++ pack25519(c, a);
++++++++ pack25519(d, b);
++++++++ return crypto_verify_32(c, 0, d, 0);
++++++++}
++++++++
++++++++function crypto_verify_32(x, xi, y, yi) {
++++++++ return vn(x, xi, y, yi, 32);
++++++++}
++++++++
++++++++function vn(x, xi, y, yi, n) {
++++++++ var i, d = 0;
++++++++ for(i = 0; i < n; ++i) {
++++++++ d |= x[xi + i] ^ y[yi + i];
++++++++ }
++++++++ return (1 & ((d - 1) >>> 8)) - 1;
++++++++}
++++++++
++++++++function par25519(a) {
++++++++ var d = new NativeBuffer(32);
++++++++ pack25519(d, a);
++++++++ return d[0] & 1;
++++++++}
++++++++
++++++++function scalarmult(p, q, s) {
++++++++ var b, i;
++++++++ set25519(p[0], gf0);
++++++++ set25519(p[1], gf1);
++++++++ set25519(p[2], gf1);
++++++++ set25519(p[3], gf0);
++++++++ for(i = 255; i >= 0; --i) {
++++++++ b = (s[(i / 8)|0] >> (i & 7)) & 1;
++++++++ cswap(p, q, b);
++++++++ add(q, p);
++++++++ add(p, p);
++++++++ cswap(p, q, b);
++++++++ }
++++++++}
++++++++
++++++++function scalarbase(p, s) {
++++++++ var q = [gf(), gf(), gf(), gf()];
++++++++ set25519(q[0], X);
++++++++ set25519(q[1], Y);
++++++++ set25519(q[2], gf1);
++++++++ M(q[3], X, Y);
++++++++ scalarmult(p, q, s);
++++++++}
++++++++
++++++++function set25519(r, a) {
++++++++ var i;
++++++++ for(i = 0; i < 16; i++) {
++++++++ r[i] = a[i] | 0;
++++++++ }
++++++++}
++++++++
++++++++function inv25519(o, i) {
++++++++ var c = gf();
++++++++ var a;
++++++++ for(a = 0; a < 16; ++a) {
++++++++ c[a] = i[a];
++++++++ }
++++++++ for(a = 253; a >= 0; --a) {
++++++++ S(c, c);
++++++++ if(a !== 2 && a !== 4) {
++++++++ M(c, c, i);
++++++++ }
++++++++ }
++++++++ for(a = 0; a < 16; ++a) {
++++++++ o[a] = c[a];
++++++++ }
++++++++}
++++++++
++++++++function car25519(o) {
++++++++ var i, v, c = 1;
++++++++ for(i = 0; i < 16; ++i) {
++++++++ v = o[i] + c + 65535;
++++++++ c = Math.floor(v / 65536);
++++++++ o[i] = v - c * 65536;
++++++++ }
++++++++ o[0] += c - 1 + 37 * (c - 1);
++++++++}
++++++++
++++++++function sel25519(p, q, b) {
++++++++ var t, c = ~(b - 1);
++++++++ for(var i = 0; i < 16; ++i) {
++++++++ t = c & (p[i] ^ q[i]);
++++++++ p[i] ^= t;
++++++++ q[i] ^= t;
++++++++ }
++++++++}
++++++++
++++++++function gf(init) {
++++++++ var i, r = new Float64Array(16);
++++++++ if(init) {
++++++++ for(i = 0; i < init.length; ++i) {
++++++++ r[i] = init[i];
++++++++ }
++++++++ }
++++++++ return r;
++++++++}
++++++++
++++++++function A(o, a, b) {
++++++++ for(var i = 0; i < 16; ++i) {
++++++++ o[i] = a[i] + b[i];
++++++++ }
++++++++}
++++++++
++++++++function Z(o, a, b) {
++++++++ for(var i = 0; i < 16; ++i) {
++++++++ o[i] = a[i] - b[i];
++++++++ }
++++++++}
++++++++
++++++++function S(o, a) {
++++++++ M(o, a, a);
++++++++}
++++++++
++++++++function M(o, a, b) {
++++++++ var v, c,
++++++++ t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
++++++++ t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
++++++++ t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
++++++++ t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
++++++++ b0 = b[0],
++++++++ b1 = b[1],
++++++++ b2 = b[2],
++++++++ b3 = b[3],
++++++++ b4 = b[4],
++++++++ b5 = b[5],
++++++++ b6 = b[6],
++++++++ b7 = b[7],
++++++++ b8 = b[8],
++++++++ b9 = b[9],
++++++++ b10 = b[10],
++++++++ b11 = b[11],
++++++++ b12 = b[12],
++++++++ b13 = b[13],
++++++++ b14 = b[14],
++++++++ b15 = b[15];
++++++++
++++++++ v = a[0];
++++++++ t0 += v * b0;
++++++++ t1 += v * b1;
++++++++ t2 += v * b2;
++++++++ t3 += v * b3;
++++++++ t4 += v * b4;
++++++++ t5 += v * b5;
++++++++ t6 += v * b6;
++++++++ t7 += v * b7;
++++++++ t8 += v * b8;
++++++++ t9 += v * b9;
++++++++ t10 += v * b10;
++++++++ t11 += v * b11;
++++++++ t12 += v * b12;
++++++++ t13 += v * b13;
++++++++ t14 += v * b14;
++++++++ t15 += v * b15;
++++++++ v = a[1];
++++++++ t1 += v * b0;
++++++++ t2 += v * b1;
++++++++ t3 += v * b2;
++++++++ t4 += v * b3;
++++++++ t5 += v * b4;
++++++++ t6 += v * b5;
++++++++ t7 += v * b6;
++++++++ t8 += v * b7;
++++++++ t9 += v * b8;
++++++++ t10 += v * b9;
++++++++ t11 += v * b10;
++++++++ t12 += v * b11;
++++++++ t13 += v * b12;
++++++++ t14 += v * b13;
++++++++ t15 += v * b14;
++++++++ t16 += v * b15;
++++++++ v = a[2];
++++++++ t2 += v * b0;
++++++++ t3 += v * b1;
++++++++ t4 += v * b2;
++++++++ t5 += v * b3;
++++++++ t6 += v * b4;
++++++++ t7 += v * b5;
++++++++ t8 += v * b6;
++++++++ t9 += v * b7;
++++++++ t10 += v * b8;
++++++++ t11 += v * b9;
++++++++ t12 += v * b10;
++++++++ t13 += v * b11;
++++++++ t14 += v * b12;
++++++++ t15 += v * b13;
++++++++ t16 += v * b14;
++++++++ t17 += v * b15;
++++++++ v = a[3];
++++++++ t3 += v * b0;
++++++++ t4 += v * b1;
++++++++ t5 += v * b2;
++++++++ t6 += v * b3;
++++++++ t7 += v * b4;
++++++++ t8 += v * b5;
++++++++ t9 += v * b6;
++++++++ t10 += v * b7;
++++++++ t11 += v * b8;
++++++++ t12 += v * b9;
++++++++ t13 += v * b10;
++++++++ t14 += v * b11;
++++++++ t15 += v * b12;
++++++++ t16 += v * b13;
++++++++ t17 += v * b14;
++++++++ t18 += v * b15;
++++++++ v = a[4];
++++++++ t4 += v * b0;
++++++++ t5 += v * b1;
++++++++ t6 += v * b2;
++++++++ t7 += v * b3;
++++++++ t8 += v * b4;
++++++++ t9 += v * b5;
++++++++ t10 += v * b6;
++++++++ t11 += v * b7;
++++++++ t12 += v * b8;
++++++++ t13 += v * b9;
++++++++ t14 += v * b10;
++++++++ t15 += v * b11;
++++++++ t16 += v * b12;
++++++++ t17 += v * b13;
++++++++ t18 += v * b14;
++++++++ t19 += v * b15;
++++++++ v = a[5];
++++++++ t5 += v * b0;
++++++++ t6 += v * b1;
++++++++ t7 += v * b2;
++++++++ t8 += v * b3;
++++++++ t9 += v * b4;
++++++++ t10 += v * b5;
++++++++ t11 += v * b6;
++++++++ t12 += v * b7;
++++++++ t13 += v * b8;
++++++++ t14 += v * b9;
++++++++ t15 += v * b10;
++++++++ t16 += v * b11;
++++++++ t17 += v * b12;
++++++++ t18 += v * b13;
++++++++ t19 += v * b14;
++++++++ t20 += v * b15;
++++++++ v = a[6];
++++++++ t6 += v * b0;
++++++++ t7 += v * b1;
++++++++ t8 += v * b2;
++++++++ t9 += v * b3;
++++++++ t10 += v * b4;
++++++++ t11 += v * b5;
++++++++ t12 += v * b6;
++++++++ t13 += v * b7;
++++++++ t14 += v * b8;
++++++++ t15 += v * b9;
++++++++ t16 += v * b10;
++++++++ t17 += v * b11;
++++++++ t18 += v * b12;
++++++++ t19 += v * b13;
++++++++ t20 += v * b14;
++++++++ t21 += v * b15;
++++++++ v = a[7];
++++++++ t7 += v * b0;
++++++++ t8 += v * b1;
++++++++ t9 += v * b2;
++++++++ t10 += v * b3;
++++++++ t11 += v * b4;
++++++++ t12 += v * b5;
++++++++ t13 += v * b6;
++++++++ t14 += v * b7;
++++++++ t15 += v * b8;
++++++++ t16 += v * b9;
++++++++ t17 += v * b10;
++++++++ t18 += v * b11;
++++++++ t19 += v * b12;
++++++++ t20 += v * b13;
++++++++ t21 += v * b14;
++++++++ t22 += v * b15;
++++++++ v = a[8];
++++++++ t8 += v * b0;
++++++++ t9 += v * b1;
++++++++ t10 += v * b2;
++++++++ t11 += v * b3;
++++++++ t12 += v * b4;
++++++++ t13 += v * b5;
++++++++ t14 += v * b6;
++++++++ t15 += v * b7;
++++++++ t16 += v * b8;
++++++++ t17 += v * b9;
++++++++ t18 += v * b10;
++++++++ t19 += v * b11;
++++++++ t20 += v * b12;
++++++++ t21 += v * b13;
++++++++ t22 += v * b14;
++++++++ t23 += v * b15;
++++++++ v = a[9];
++++++++ t9 += v * b0;
++++++++ t10 += v * b1;
++++++++ t11 += v * b2;
++++++++ t12 += v * b3;
++++++++ t13 += v * b4;
++++++++ t14 += v * b5;
++++++++ t15 += v * b6;
++++++++ t16 += v * b7;
++++++++ t17 += v * b8;
++++++++ t18 += v * b9;
++++++++ t19 += v * b10;
++++++++ t20 += v * b11;
++++++++ t21 += v * b12;
++++++++ t22 += v * b13;
++++++++ t23 += v * b14;
++++++++ t24 += v * b15;
++++++++ v = a[10];
++++++++ t10 += v * b0;
++++++++ t11 += v * b1;
++++++++ t12 += v * b2;
++++++++ t13 += v * b3;
++++++++ t14 += v * b4;
++++++++ t15 += v * b5;
++++++++ t16 += v * b6;
++++++++ t17 += v * b7;
++++++++ t18 += v * b8;
++++++++ t19 += v * b9;
++++++++ t20 += v * b10;
++++++++ t21 += v * b11;
++++++++ t22 += v * b12;
++++++++ t23 += v * b13;
++++++++ t24 += v * b14;
++++++++ t25 += v * b15;
++++++++ v = a[11];
++++++++ t11 += v * b0;
++++++++ t12 += v * b1;
++++++++ t13 += v * b2;
++++++++ t14 += v * b3;
++++++++ t15 += v * b4;
++++++++ t16 += v * b5;
++++++++ t17 += v * b6;
++++++++ t18 += v * b7;
++++++++ t19 += v * b8;
++++++++ t20 += v * b9;
++++++++ t21 += v * b10;
++++++++ t22 += v * b11;
++++++++ t23 += v * b12;
++++++++ t24 += v * b13;
++++++++ t25 += v * b14;
++++++++ t26 += v * b15;
++++++++ v = a[12];
++++++++ t12 += v * b0;
++++++++ t13 += v * b1;
++++++++ t14 += v * b2;
++++++++ t15 += v * b3;
++++++++ t16 += v * b4;
++++++++ t17 += v * b5;
++++++++ t18 += v * b6;
++++++++ t19 += v * b7;
++++++++ t20 += v * b8;
++++++++ t21 += v * b9;
++++++++ t22 += v * b10;
++++++++ t23 += v * b11;
++++++++ t24 += v * b12;
++++++++ t25 += v * b13;
++++++++ t26 += v * b14;
++++++++ t27 += v * b15;
++++++++ v = a[13];
++++++++ t13 += v * b0;
++++++++ t14 += v * b1;
++++++++ t15 += v * b2;
++++++++ t16 += v * b3;
++++++++ t17 += v * b4;
++++++++ t18 += v * b5;
++++++++ t19 += v * b6;
++++++++ t20 += v * b7;
++++++++ t21 += v * b8;
++++++++ t22 += v * b9;
++++++++ t23 += v * b10;
++++++++ t24 += v * b11;
++++++++ t25 += v * b12;
++++++++ t26 += v * b13;
++++++++ t27 += v * b14;
++++++++ t28 += v * b15;
++++++++ v = a[14];
++++++++ t14 += v * b0;
++++++++ t15 += v * b1;
++++++++ t16 += v * b2;
++++++++ t17 += v * b3;
++++++++ t18 += v * b4;
++++++++ t19 += v * b5;
++++++++ t20 += v * b6;
++++++++ t21 += v * b7;
++++++++ t22 += v * b8;
++++++++ t23 += v * b9;
++++++++ t24 += v * b10;
++++++++ t25 += v * b11;
++++++++ t26 += v * b12;
++++++++ t27 += v * b13;
++++++++ t28 += v * b14;
++++++++ t29 += v * b15;
++++++++ v = a[15];
++++++++ t15 += v * b0;
++++++++ t16 += v * b1;
++++++++ t17 += v * b2;
++++++++ t18 += v * b3;
++++++++ t19 += v * b4;
++++++++ t20 += v * b5;
++++++++ t21 += v * b6;
++++++++ t22 += v * b7;
++++++++ t23 += v * b8;
++++++++ t24 += v * b9;
++++++++ t25 += v * b10;
++++++++ t26 += v * b11;
++++++++ t27 += v * b12;
++++++++ t28 += v * b13;
++++++++ t29 += v * b14;
++++++++ t30 += v * b15;
++++++++
++++++++ t0 += 38 * t16;
++++++++ t1 += 38 * t17;
++++++++ t2 += 38 * t18;
++++++++ t3 += 38 * t19;
++++++++ t4 += 38 * t20;
++++++++ t5 += 38 * t21;
++++++++ t6 += 38 * t22;
++++++++ t7 += 38 * t23;
++++++++ t8 += 38 * t24;
++++++++ t9 += 38 * t25;
++++++++ t10 += 38 * t26;
++++++++ t11 += 38 * t27;
++++++++ t12 += 38 * t28;
++++++++ t13 += 38 * t29;
++++++++ t14 += 38 * t30;
++++++++ // t15 left as is
++++++++
++++++++ // first car
++++++++ c = 1;
++++++++ v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
++++++++ v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
++++++++ v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
++++++++ v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
++++++++ v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
++++++++ v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
++++++++ v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
++++++++ v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
++++++++ v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
++++++++ v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
++++++++ v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
++++++++ v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
++++++++ v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
++++++++ v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
++++++++ v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
++++++++ v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
++++++++ t0 += c-1 + 37 * (c-1);
++++++++
++++++++ // second car
++++++++ c = 1;
++++++++ v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
++++++++ v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
++++++++ v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
++++++++ v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
++++++++ v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
++++++++ v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
++++++++ v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
++++++++ v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
++++++++ v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
++++++++ v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
++++++++ v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
++++++++ v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
++++++++ v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
++++++++ v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
++++++++ v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
++++++++ v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
++++++++ t0 += c-1 + 37 * (c-1);
++++++++
++++++++ o[ 0] = t0;
++++++++ o[ 1] = t1;
++++++++ o[ 2] = t2;
++++++++ o[ 3] = t3;
++++++++ o[ 4] = t4;
++++++++ o[ 5] = t5;
++++++++ o[ 6] = t6;
++++++++ o[ 7] = t7;
++++++++ o[ 8] = t8;
++++++++ o[ 9] = t9;
++++++++ o[10] = t10;
++++++++ o[11] = t11;
++++++++ o[12] = t12;
++++++++ o[13] = t13;
++++++++ o[14] = t14;
++++++++ o[15] = t15;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for Forge.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright 2011-2016 Digital Bazaar, Inc.
++++++++ */
++++++++module.exports = {
++++++++ // default options
++++++++ options: {
++++++++ usePureJavaScript: false
++++++++ }
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Functions for manipulating web forms.
++++++++ *
++++++++ * @author David I. Lehn <dlehn@digitalbazaar.com>
++++++++ * @author Dave Longley
++++++++ * @author Mike Johnson
++++++++ *
++++++++ * Copyright (c) 2011-2014 Digital Bazaar, Inc. All rights reserved.
++++++++ */
++++++++var forge = require('./forge');
++++++++
++++++++/* Form API */
++++++++var form = module.exports = forge.form = forge.form || {};
++++++++
++++++++(function($) {
++++++++
++++++++/**
++++++++ * Regex for parsing a single name property (handles array brackets).
++++++++ */
++++++++var _regex = /([^\[]*?)\[(.*?)\]/g;
++++++++
++++++++/**
++++++++ * Parses a single name property into an array with the name and any
++++++++ * array indices.
++++++++ *
++++++++ * @param name the name to parse.
++++++++ *
++++++++ * @return the array of the name and its array indices in order.
++++++++ */
++++++++var _parseName = function(name) {
++++++++ var rval = [];
++++++++
++++++++ var matches;
++++++++ while(!!(matches = _regex.exec(name))) {
++++++++ if(matches[1].length > 0) {
++++++++ rval.push(matches[1]);
++++++++ }
++++++++ if(matches.length >= 2) {
++++++++ rval.push(matches[2]);
++++++++ }
++++++++ }
++++++++ if(rval.length === 0) {
++++++++ rval.push(name);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Adds a field from the given form to the given object.
++++++++ *
++++++++ * @param obj the object.
++++++++ * @param names the field as an array of object property names.
++++++++ * @param value the value of the field.
++++++++ * @param dict a dictionary of names to replace.
++++++++ */
++++++++var _addField = function(obj, names, value, dict) {
++++++++ // combine array names that fall within square brackets
++++++++ var tmp = [];
++++++++ for(var i = 0; i < names.length; ++i) {
++++++++ // check name for starting square bracket but no ending one
++++++++ var name = names[i];
++++++++ if(name.indexOf('[') !== -1 && name.indexOf(']') === -1 &&
++++++++ i < names.length - 1) {
++++++++ do {
++++++++ name += '.' + names[++i];
++++++++ } while(i < names.length - 1 && names[i].indexOf(']') === -1);
++++++++ }
++++++++ tmp.push(name);
++++++++ }
++++++++ names = tmp;
++++++++
++++++++ // split out array indexes
++++++++ var tmp = [];
++++++++ $.each(names, function(n, name) {
++++++++ tmp = tmp.concat(_parseName(name));
++++++++ });
++++++++ names = tmp;
++++++++
++++++++ // iterate over object property names until value is set
++++++++ $.each(names, function(n, name) {
++++++++ // do dictionary name replacement
++++++++ if(dict && name.length !== 0 && name in dict) {
++++++++ name = dict[name];
++++++++ }
++++++++
++++++++ // blank name indicates appending to an array, set name to
++++++++ // new last index of array
++++++++ if(name.length === 0) {
++++++++ name = obj.length;
++++++++ }
++++++++
++++++++ // value already exists, append value
++++++++ if(obj[name]) {
++++++++ // last name in the field
++++++++ if(n == names.length - 1) {
++++++++ // more than one value, so convert into an array
++++++++ if(!$.isArray(obj[name])) {
++++++++ obj[name] = [obj[name]];
++++++++ }
++++++++ obj[name].push(value);
++++++++ } else {
++++++++ // not last name, go deeper into object
++++++++ obj = obj[name];
++++++++ }
++++++++ } else if(n == names.length - 1) {
++++++++ // new value, last name in the field, set value
++++++++ obj[name] = value;
++++++++ } else {
++++++++ // new value, not last name, go deeper
++++++++ // get next name
++++++++ var next = names[n + 1];
++++++++
++++++++ // blank next value indicates array-appending, so create array
++++++++ if(next.length === 0) {
++++++++ obj[name] = [];
++++++++ } else {
++++++++ // if next name is a number create an array, otherwise a map
++++++++ var isNum = ((next - 0) == next && next.length > 0);
++++++++ obj[name] = isNum ? [] : {};
++++++++ }
++++++++ obj = obj[name];
++++++++ }
++++++++ });
++++++++};
++++++++
++++++++/**
++++++++ * Serializes a form to a JSON object. Object properties will be separated
++++++++ * using the given separator (defaults to '.') and by square brackets.
++++++++ *
++++++++ * @param input the jquery form to serialize.
++++++++ * @param sep the object-property separator (defaults to '.').
++++++++ * @param dict a dictionary of names to replace (name=replace).
++++++++ *
++++++++ * @return the JSON-serialized form.
++++++++ */
++++++++form.serialize = function(input, sep, dict) {
++++++++ var rval = {};
++++++++
++++++++ // add all fields in the form to the object
++++++++ sep = sep || '.';
++++++++ $.each(input.serializeArray(), function() {
++++++++ _addField(rval, this.name.split(sep), this.value || '', dict);
++++++++ });
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++})(jQuery);
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Hash-based Message Authentication Code implementation. Requires a message
++++++++ * digest object that can be obtained, for example, from forge.md.sha1 or
++++++++ * forge.md.md5.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2012 Digital Bazaar, Inc. All rights reserved.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++/* HMAC API */
++++++++var hmac = module.exports = forge.hmac = forge.hmac || {};
++++++++
++++++++/**
++++++++ * Creates an HMAC object that uses the given message digest object.
++++++++ *
++++++++ * @return an HMAC object.
++++++++ */
++++++++hmac.create = function() {
++++++++ // the hmac key to use
++++++++ var _key = null;
++++++++
++++++++ // the message digest to use
++++++++ var _md = null;
++++++++
++++++++ // the inner padding
++++++++ var _ipadding = null;
++++++++
++++++++ // the outer padding
++++++++ var _opadding = null;
++++++++
++++++++ // hmac context
++++++++ var ctx = {};
++++++++
++++++++ /**
++++++++ * Starts or restarts the HMAC with the given key and message digest.
++++++++ *
++++++++ * @param md the message digest to use, null to reuse the previous one,
++++++++ * a string to use builtin 'sha1', 'md5', 'sha256'.
++++++++ * @param key the key to use as a string, array of bytes, byte buffer,
++++++++ * or null to reuse the previous key.
++++++++ */
++++++++ ctx.start = function(md, key) {
++++++++ if(md !== null) {
++++++++ if(typeof md === 'string') {
++++++++ // create builtin message digest
++++++++ md = md.toLowerCase();
++++++++ if(md in forge.md.algorithms) {
++++++++ _md = forge.md.algorithms[md].create();
++++++++ } else {
++++++++ throw new Error('Unknown hash algorithm "' + md + '"');
++++++++ }
++++++++ } else {
++++++++ // store message digest
++++++++ _md = md;
++++++++ }
++++++++ }
++++++++
++++++++ if(key === null) {
++++++++ // reuse previous key
++++++++ key = _key;
++++++++ } else {
++++++++ if(typeof key === 'string') {
++++++++ // convert string into byte buffer
++++++++ key = forge.util.createBuffer(key);
++++++++ } else if(forge.util.isArray(key)) {
++++++++ // convert byte array into byte buffer
++++++++ var tmp = key;
++++++++ key = forge.util.createBuffer();
++++++++ for(var i = 0; i < tmp.length; ++i) {
++++++++ key.putByte(tmp[i]);
++++++++ }
++++++++ }
++++++++
++++++++ // if key is longer than blocksize, hash it
++++++++ var keylen = key.length();
++++++++ if(keylen > _md.blockLength) {
++++++++ _md.start();
++++++++ _md.update(key.bytes());
++++++++ key = _md.digest();
++++++++ }
++++++++
++++++++ // mix key into inner and outer padding
++++++++ // ipadding = [0x36 * blocksize] ^ key
++++++++ // opadding = [0x5C * blocksize] ^ key
++++++++ _ipadding = forge.util.createBuffer();
++++++++ _opadding = forge.util.createBuffer();
++++++++ keylen = key.length();
++++++++ for(var i = 0; i < keylen; ++i) {
++++++++ var tmp = key.at(i);
++++++++ _ipadding.putByte(0x36 ^ tmp);
++++++++ _opadding.putByte(0x5C ^ tmp);
++++++++ }
++++++++
++++++++ // if key is shorter than blocksize, add additional padding
++++++++ if(keylen < _md.blockLength) {
++++++++ var tmp = _md.blockLength - keylen;
++++++++ for(var i = 0; i < tmp; ++i) {
++++++++ _ipadding.putByte(0x36);
++++++++ _opadding.putByte(0x5C);
++++++++ }
++++++++ }
++++++++ _key = key;
++++++++ _ipadding = _ipadding.bytes();
++++++++ _opadding = _opadding.bytes();
++++++++ }
++++++++
++++++++ // digest is done like so: hash(opadding | hash(ipadding | message))
++++++++
++++++++ // prepare to do inner hash
++++++++ // hash(ipadding | message)
++++++++ _md.start();
++++++++ _md.update(_ipadding);
++++++++ };
++++++++
++++++++ /**
++++++++ * Updates the HMAC with the given message bytes.
++++++++ *
++++++++ * @param bytes the bytes to update with.
++++++++ */
++++++++ ctx.update = function(bytes) {
++++++++ _md.update(bytes);
++++++++ };
++++++++
++++++++ /**
++++++++ * Produces the Message Authentication Code (MAC).
++++++++ *
++++++++ * @return a byte buffer containing the digest value.
++++++++ */
++++++++ ctx.getMac = function() {
++++++++ // digest is done like so: hash(opadding | hash(ipadding | message))
++++++++ // here we do the outer hashing
++++++++ var inner = _md.digest().bytes();
++++++++ _md.start();
++++++++ _md.update(_opadding);
++++++++ _md.update(inner);
++++++++ return _md.digest();
++++++++ };
++++++++ // alias for getMac
++++++++ ctx.digest = ctx.getMac;
++++++++
++++++++ return ctx;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * HTTP client-side implementation that uses forge.net sockets.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc. All rights reserved.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./tls');
++++++++require('./util');
++++++++
++++++++// define http namespace
++++++++var http = module.exports = forge.http = forge.http || {};
++++++++
++++++++// logging category
++++++++var cat = 'forge.http';
++++++++
++++++++// normalizes an http header field name
++++++++var _normalize = function(name) {
++++++++ return name.toLowerCase().replace(/(^.)|(-.)/g,
++++++++ function(a) {return a.toUpperCase();});
++++++++};
++++++++
++++++++/**
++++++++ * Gets the local storage ID for the given client.
++++++++ *
++++++++ * @param client the client to get the local storage ID for.
++++++++ *
++++++++ * @return the local storage ID to use.
++++++++ */
++++++++var _getStorageId = function(client) {
++++++++ // TODO: include browser in ID to avoid sharing cookies between
++++++++ // browsers (if this is undesirable)
++++++++ // navigator.userAgent
++++++++ return 'forge.http.' +
++++++++ client.url.protocol.slice(0, -1) + '.' +
++++++++ client.url.hostname + '.' +
++++++++ client.url.port;
++++++++};
++++++++
++++++++/**
++++++++ * Loads persistent cookies from disk for the given client.
++++++++ *
++++++++ * @param client the client.
++++++++ */
++++++++var _loadCookies = function(client) {
++++++++ if(client.persistCookies) {
++++++++ try {
++++++++ var cookies = forge.util.getItem(
++++++++ client.socketPool.flashApi,
++++++++ _getStorageId(client), 'cookies');
++++++++ client.cookies = cookies || {};
++++++++ } catch(ex) {
++++++++ // no flash storage available, just silently fail
++++++++ // TODO: i assume we want this logged somewhere or
++++++++ // should it actually generate an error
++++++++ //forge.log.error(cat, ex);
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Saves persistent cookies on disk for the given client.
++++++++ *
++++++++ * @param client the client.
++++++++ */
++++++++var _saveCookies = function(client) {
++++++++ if(client.persistCookies) {
++++++++ try {
++++++++ forge.util.setItem(
++++++++ client.socketPool.flashApi,
++++++++ _getStorageId(client), 'cookies', client.cookies);
++++++++ } catch(ex) {
++++++++ // no flash storage available, just silently fail
++++++++ // TODO: i assume we want this logged somewhere or
++++++++ // should it actually generate an error
++++++++ //forge.log.error(cat, ex);
++++++++ }
++++++++ }
++++++++
++++++++ // FIXME: remove me
++++++++ _loadCookies(client);
++++++++};
++++++++
++++++++/**
++++++++ * Clears persistent cookies on disk for the given client.
++++++++ *
++++++++ * @param client the client.
++++++++ */
++++++++var _clearCookies = function(client) {
++++++++ if(client.persistCookies) {
++++++++ try {
++++++++ // only thing stored is 'cookies', so clear whole storage
++++++++ forge.util.clearItems(
++++++++ client.socketPool.flashApi,
++++++++ _getStorageId(client));
++++++++ } catch(ex) {
++++++++ // no flash storage available, just silently fail
++++++++ // TODO: i assume we want this logged somewhere or
++++++++ // should it actually generate an error
++++++++ //forge.log.error(cat, ex);
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Connects and sends a request.
++++++++ *
++++++++ * @param client the http client.
++++++++ * @param socket the socket to use.
++++++++ */
++++++++var _doRequest = function(client, socket) {
++++++++ if(socket.isConnected()) {
++++++++ // already connected
++++++++ socket.options.request.connectTime = +new Date();
++++++++ socket.connected({
++++++++ type: 'connect',
++++++++ id: socket.id
++++++++ });
++++++++ } else {
++++++++ // connect
++++++++ socket.options.request.connectTime = +new Date();
++++++++ socket.connect({
++++++++ host: client.url.hostname,
++++++++ port: client.url.port,
++++++++ policyPort: client.policyPort,
++++++++ policyUrl: client.policyUrl
++++++++ });
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Handles the next request or marks a socket as idle.
++++++++ *
++++++++ * @param client the http client.
++++++++ * @param socket the socket.
++++++++ */
++++++++var _handleNextRequest = function(client, socket) {
++++++++ // clear buffer
++++++++ socket.buffer.clear();
++++++++
++++++++ // get pending request
++++++++ var pending = null;
++++++++ while(pending === null && client.requests.length > 0) {
++++++++ pending = client.requests.shift();
++++++++ if(pending.request.aborted) {
++++++++ pending = null;
++++++++ }
++++++++ }
++++++++
++++++++ // mark socket idle if no pending requests
++++++++ if(pending === null) {
++++++++ if(socket.options !== null) {
++++++++ socket.options = null;
++++++++ }
++++++++ client.idle.push(socket);
++++++++ } else {
++++++++ // handle pending request, allow 1 retry
++++++++ socket.retries = 1;
++++++++ socket.options = pending;
++++++++ _doRequest(client, socket);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Sets up a socket for use with an http client.
++++++++ *
++++++++ * @param client the parent http client.
++++++++ * @param socket the socket to set up.
++++++++ * @param tlsOptions if the socket must use TLS, the TLS options.
++++++++ */
++++++++var _initSocket = function(client, socket, tlsOptions) {
++++++++ // no socket options yet
++++++++ socket.options = null;
++++++++
++++++++ // set up handlers
++++++++ socket.connected = function(e) {
++++++++ // socket primed by caching TLS session, handle next request
++++++++ if(socket.options === null) {
++++++++ _handleNextRequest(client, socket);
++++++++ } else {
++++++++ // socket in use
++++++++ var request = socket.options.request;
++++++++ request.connectTime = +new Date() - request.connectTime;
++++++++ e.socket = socket;
++++++++ socket.options.connected(e);
++++++++ if(request.aborted) {
++++++++ socket.close();
++++++++ } else {
++++++++ var out = request.toString();
++++++++ if(request.body) {
++++++++ out += request.body;
++++++++ }
++++++++ request.time = +new Date();
++++++++ socket.send(out);
++++++++ request.time = +new Date() - request.time;
++++++++ socket.options.response.time = +new Date();
++++++++ socket.sending = true;
++++++++ }
++++++++ }
++++++++ };
++++++++ socket.closed = function(e) {
++++++++ if(socket.sending) {
++++++++ socket.sending = false;
++++++++ if(socket.retries > 0) {
++++++++ --socket.retries;
++++++++ _doRequest(client, socket);
++++++++ } else {
++++++++ // error, closed during send
++++++++ socket.error({
++++++++ id: socket.id,
++++++++ type: 'ioError',
++++++++ message: 'Connection closed during send. Broken pipe.',
++++++++ bytesAvailable: 0
++++++++ });
++++++++ }
++++++++ } else {
++++++++ // handle unspecified content-length transfer
++++++++ var response = socket.options.response;
++++++++ if(response.readBodyUntilClose) {
++++++++ response.time = +new Date() - response.time;
++++++++ response.bodyReceived = true;
++++++++ socket.options.bodyReady({
++++++++ request: socket.options.request,
++++++++ response: response,
++++++++ socket: socket
++++++++ });
++++++++ }
++++++++ socket.options.closed(e);
++++++++ _handleNextRequest(client, socket);
++++++++ }
++++++++ };
++++++++ socket.data = function(e) {
++++++++ socket.sending = false;
++++++++ var request = socket.options.request;
++++++++ if(request.aborted) {
++++++++ socket.close();
++++++++ } else {
++++++++ // receive all bytes available
++++++++ var response = socket.options.response;
++++++++ var bytes = socket.receive(e.bytesAvailable);
++++++++ if(bytes !== null) {
++++++++ // receive header and then body
++++++++ socket.buffer.putBytes(bytes);
++++++++ if(!response.headerReceived) {
++++++++ response.readHeader(socket.buffer);
++++++++ if(response.headerReceived) {
++++++++ socket.options.headerReady({
++++++++ request: socket.options.request,
++++++++ response: response,
++++++++ socket: socket
++++++++ });
++++++++ }
++++++++ }
++++++++ if(response.headerReceived && !response.bodyReceived) {
++++++++ response.readBody(socket.buffer);
++++++++ }
++++++++ if(response.bodyReceived) {
++++++++ socket.options.bodyReady({
++++++++ request: socket.options.request,
++++++++ response: response,
++++++++ socket: socket
++++++++ });
++++++++ // close connection if requested or by default on http/1.0
++++++++ var value = response.getField('Connection') || '';
++++++++ if(value.indexOf('close') != -1 ||
++++++++ (response.version === 'HTTP/1.0' &&
++++++++ response.getField('Keep-Alive') === null)) {
++++++++ socket.close();
++++++++ } else {
++++++++ _handleNextRequest(client, socket);
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ };
++++++++ socket.error = function(e) {
++++++++ // do error callback, include request
++++++++ socket.options.error({
++++++++ type: e.type,
++++++++ message: e.message,
++++++++ request: socket.options.request,
++++++++ response: socket.options.response,
++++++++ socket: socket
++++++++ });
++++++++ socket.close();
++++++++ };
++++++++
++++++++ // wrap socket for TLS
++++++++ if(tlsOptions) {
++++++++ socket = forge.tls.wrapSocket({
++++++++ sessionId: null,
++++++++ sessionCache: {},
++++++++ caStore: tlsOptions.caStore,
++++++++ cipherSuites: tlsOptions.cipherSuites,
++++++++ socket: socket,
++++++++ virtualHost: tlsOptions.virtualHost,
++++++++ verify: tlsOptions.verify,
++++++++ getCertificate: tlsOptions.getCertificate,
++++++++ getPrivateKey: tlsOptions.getPrivateKey,
++++++++ getSignature: tlsOptions.getSignature,
++++++++ deflate: tlsOptions.deflate || null,
++++++++ inflate: tlsOptions.inflate || null
++++++++ });
++++++++
++++++++ socket.options = null;
++++++++ socket.buffer = forge.util.createBuffer();
++++++++ client.sockets.push(socket);
++++++++ if(tlsOptions.prime) {
++++++++ // prime socket by connecting and caching TLS session, will do
++++++++ // next request from there
++++++++ socket.connect({
++++++++ host: client.url.hostname,
++++++++ port: client.url.port,
++++++++ policyPort: client.policyPort,
++++++++ policyUrl: client.policyUrl
++++++++ });
++++++++ } else {
++++++++ // do not prime socket, just add as idle
++++++++ client.idle.push(socket);
++++++++ }
++++++++ } else {
++++++++ // no need to prime non-TLS sockets
++++++++ socket.buffer = forge.util.createBuffer();
++++++++ client.sockets.push(socket);
++++++++ client.idle.push(socket);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Checks to see if the given cookie has expired. If the cookie's max-age
++++++++ * plus its created time is less than the time now, it has expired, unless
++++++++ * its max-age is set to -1 which indicates it will never expire.
++++++++ *
++++++++ * @param cookie the cookie to check.
++++++++ *
++++++++ * @return true if it has expired, false if not.
++++++++ */
++++++++var _hasCookieExpired = function(cookie) {
++++++++ var rval = false;
++++++++
++++++++ if(cookie.maxAge !== -1) {
++++++++ var now = _getUtcTime(new Date());
++++++++ var expires = cookie.created + cookie.maxAge;
++++++++ if(expires <= now) {
++++++++ rval = true;
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Adds cookies in the given client to the given request.
++++++++ *
++++++++ * @param client the client.
++++++++ * @param request the request.
++++++++ */
++++++++var _writeCookies = function(client, request) {
++++++++ var expired = [];
++++++++ var url = client.url;
++++++++ var cookies = client.cookies;
++++++++ for(var name in cookies) {
++++++++ // get cookie paths
++++++++ var paths = cookies[name];
++++++++ for(var p in paths) {
++++++++ var cookie = paths[p];
++++++++ if(_hasCookieExpired(cookie)) {
++++++++ // store for clean up
++++++++ expired.push(cookie);
++++++++ } else if(request.path.indexOf(cookie.path) === 0) {
++++++++ // path or path's ancestor must match cookie.path
++++++++ request.addCookie(cookie);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // clean up expired cookies
++++++++ for(var i = 0; i < expired.length; ++i) {
++++++++ var cookie = expired[i];
++++++++ client.removeCookie(cookie.name, cookie.path);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Gets cookies from the given response and adds the to the given client.
++++++++ *
++++++++ * @param client the client.
++++++++ * @param response the response.
++++++++ */
++++++++var _readCookies = function(client, response) {
++++++++ var cookies = response.getCookies();
++++++++ for(var i = 0; i < cookies.length; ++i) {
++++++++ try {
++++++++ client.setCookie(cookies[i]);
++++++++ } catch(ex) {
++++++++ // ignore failure to add other-domain, etc. cookies
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Creates an http client that uses forge.net sockets as a backend and
++++++++ * forge.tls for security.
++++++++ *
++++++++ * @param options:
++++++++ * url: the url to connect to (scheme://host:port).
++++++++ * socketPool: the flash socket pool to use.
++++++++ * policyPort: the flash policy port to use (if other than the
++++++++ * socket pool default), use 0 for flash default.
++++++++ * policyUrl: the flash policy file URL to use (if provided will
++++++++ * be used instead of a policy port).
++++++++ * connections: number of connections to use to handle requests.
++++++++ * caCerts: an array of certificates to trust for TLS, certs may
++++++++ * be PEM-formatted or cert objects produced via forge.pki.
++++++++ * cipherSuites: an optional array of cipher suites to use,
++++++++ * see forge.tls.CipherSuites.
++++++++ * virtualHost: the virtual server name to use in a TLS SNI
++++++++ * extension, if not provided the url host will be used.
++++++++ * verify: a custom TLS certificate verify callback to use.
++++++++ * getCertificate: an optional callback used to get a client-side
++++++++ * certificate (see forge.tls for details).
++++++++ * getPrivateKey: an optional callback used to get a client-side
++++++++ * private key (see forge.tls for details).
++++++++ * getSignature: an optional callback used to get a client-side
++++++++ * signature (see forge.tls for details).
++++++++ * persistCookies: true to use persistent cookies via flash local
++++++++ * storage, false to only keep cookies in javascript.
++++++++ * primeTlsSockets: true to immediately connect TLS sockets on
++++++++ * their creation so that they will cache TLS sessions for reuse.
++++++++ *
++++++++ * @return the client.
++++++++ */
++++++++http.createClient = function(options) {
++++++++ // create CA store to share with all TLS connections
++++++++ var caStore = null;
++++++++ if(options.caCerts) {
++++++++ caStore = forge.pki.createCaStore(options.caCerts);
++++++++ }
++++++++
++++++++ // get scheme, host, and port from url
++++++++ options.url = (options.url ||
++++++++ window.location.protocol + '//' + window.location.host);
++++++++ var url;
++++++++ try {
++++++++ url = new URL(options.url);
++++++++ } catch(e) {
++++++++ var error = new Error('Invalid url.');
++++++++ error.details = {url: options.url};
++++++++ throw error;
++++++++ }
++++++++
++++++++ // default to 1 connection
++++++++ options.connections = options.connections || 1;
++++++++
++++++++ // create client
++++++++ var sp = options.socketPool;
++++++++ var client = {
++++++++ // url
++++++++ url: url,
++++++++ // socket pool
++++++++ socketPool: sp,
++++++++ // the policy port to use
++++++++ policyPort: options.policyPort,
++++++++ // policy url to use
++++++++ policyUrl: options.policyUrl,
++++++++ // queue of requests to service
++++++++ requests: [],
++++++++ // all sockets
++++++++ sockets: [],
++++++++ // idle sockets
++++++++ idle: [],
++++++++ // whether or not the connections are secure
++++++++ secure: (url.protocol === 'https:'),
++++++++ // cookie jar (key'd off of name and then path, there is only 1 domain
++++++++ // and one setting for secure per client so name+path is unique)
++++++++ cookies: {},
++++++++ // default to flash storage of cookies
++++++++ persistCookies: (typeof(options.persistCookies) === 'undefined') ?
++++++++ true : options.persistCookies
++++++++ };
++++++++
++++++++ // load cookies from disk
++++++++ _loadCookies(client);
++++++++
++++++++ /**
++++++++ * A default certificate verify function that checks a certificate common
++++++++ * name against the client's URL host.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param verified true if cert is verified, otherwise alert number.
++++++++ * @param depth the chain depth.
++++++++ * @param certs the cert chain.
++++++++ *
++++++++ * @return true if verified and the common name matches the host, error
++++++++ * otherwise.
++++++++ */
++++++++ var _defaultCertificateVerify = function(c, verified, depth, certs) {
++++++++ if(depth === 0 && verified === true) {
++++++++ // compare common name to url host
++++++++ var cn = certs[depth].subject.getField('CN');
++++++++ if(cn === null || client.url.hostname !== cn.value) {
++++++++ verified = {
++++++++ message: 'Certificate common name does not match url host.'
++++++++ };
++++++++ }
++++++++ }
++++++++ return verified;
++++++++ };
++++++++
++++++++ // determine if TLS is used
++++++++ var tlsOptions = null;
++++++++ if(client.secure) {
++++++++ tlsOptions = {
++++++++ caStore: caStore,
++++++++ cipherSuites: options.cipherSuites || null,
++++++++ virtualHost: options.virtualHost || url.hostname,
++++++++ verify: options.verify || _defaultCertificateVerify,
++++++++ getCertificate: options.getCertificate || null,
++++++++ getPrivateKey: options.getPrivateKey || null,
++++++++ getSignature: options.getSignature || null,
++++++++ prime: options.primeTlsSockets || false
++++++++ };
++++++++
++++++++ // if socket pool uses a flash api, then add deflate support to TLS
++++++++ if(sp.flashApi !== null) {
++++++++ tlsOptions.deflate = function(bytes) {
++++++++ // strip 2 byte zlib header and 4 byte trailer
++++++++ return forge.util.deflate(sp.flashApi, bytes, true);
++++++++ };
++++++++ tlsOptions.inflate = function(bytes) {
++++++++ return forge.util.inflate(sp.flashApi, bytes, true);
++++++++ };
++++++++ }
++++++++ }
++++++++
++++++++ // create and initialize sockets
++++++++ for(var i = 0; i < options.connections; ++i) {
++++++++ _initSocket(client, sp.createSocket(), tlsOptions);
++++++++ }
++++++++
++++++++ /**
++++++++ * Sends a request. A method 'abort' will be set on the request that
++++++++ * can be called to attempt to abort the request.
++++++++ *
++++++++ * @param options:
++++++++ * request: the request to send.
++++++++ * connected: a callback for when the connection is open.
++++++++ * closed: a callback for when the connection is closed.
++++++++ * headerReady: a callback for when the response header arrives.
++++++++ * bodyReady: a callback for when the response body arrives.
++++++++ * error: a callback for if an error occurs.
++++++++ */
++++++++ client.send = function(options) {
++++++++ // add host header if not set
++++++++ if(options.request.getField('Host') === null) {
++++++++ options.request.setField('Host', client.url.origin);
++++++++ }
++++++++
++++++++ // set default dummy handlers
++++++++ var opts = {};
++++++++ opts.request = options.request;
++++++++ opts.connected = options.connected || function() {};
++++++++ opts.closed = options.close || function() {};
++++++++ opts.headerReady = function(e) {
++++++++ // read cookies
++++++++ _readCookies(client, e.response);
++++++++ if(options.headerReady) {
++++++++ options.headerReady(e);
++++++++ }
++++++++ };
++++++++ opts.bodyReady = options.bodyReady || function() {};
++++++++ opts.error = options.error || function() {};
++++++++
++++++++ // create response
++++++++ opts.response = http.createResponse();
++++++++ opts.response.time = 0;
++++++++ opts.response.flashApi = client.socketPool.flashApi;
++++++++ opts.request.flashApi = client.socketPool.flashApi;
++++++++
++++++++ // create abort function
++++++++ opts.request.abort = function() {
++++++++ // set aborted, clear handlers
++++++++ opts.request.aborted = true;
++++++++ opts.connected = function() {};
++++++++ opts.closed = function() {};
++++++++ opts.headerReady = function() {};
++++++++ opts.bodyReady = function() {};
++++++++ opts.error = function() {};
++++++++ };
++++++++
++++++++ // add cookies to request
++++++++ _writeCookies(client, opts.request);
++++++++
++++++++ // queue request options if there are no idle sockets
++++++++ if(client.idle.length === 0) {
++++++++ client.requests.push(opts);
++++++++ } else {
++++++++ // use an idle socket, prefer an idle *connected* socket first
++++++++ var socket = null;
++++++++ var len = client.idle.length;
++++++++ for(var i = 0; socket === null && i < len; ++i) {
++++++++ socket = client.idle[i];
++++++++ if(socket.isConnected()) {
++++++++ client.idle.splice(i, 1);
++++++++ } else {
++++++++ socket = null;
++++++++ }
++++++++ }
++++++++ // no connected socket available, get unconnected socket
++++++++ if(socket === null) {
++++++++ socket = client.idle.pop();
++++++++ }
++++++++ socket.options = opts;
++++++++ _doRequest(client, socket);
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Destroys this client.
++++++++ */
++++++++ client.destroy = function() {
++++++++ // clear pending requests, close and destroy sockets
++++++++ client.requests = [];
++++++++ for(var i = 0; i < client.sockets.length; ++i) {
++++++++ client.sockets[i].close();
++++++++ client.sockets[i].destroy();
++++++++ }
++++++++ client.socketPool = null;
++++++++ client.sockets = [];
++++++++ client.idle = [];
++++++++ };
++++++++
++++++++ /**
++++++++ * Sets a cookie for use with all connections made by this client. Any
++++++++ * cookie with the same name will be replaced. If the cookie's value
++++++++ * is undefined, null, or the blank string, the cookie will be removed.
++++++++ *
++++++++ * If the cookie's domain doesn't match this client's url host or the
++++++++ * cookie's secure flag doesn't match this client's url scheme, then
++++++++ * setting the cookie will fail with an exception.
++++++++ *
++++++++ * @param cookie the cookie with parameters:
++++++++ * name: the name of the cookie.
++++++++ * value: the value of the cookie.
++++++++ * comment: an optional comment string.
++++++++ * maxAge: the age of the cookie in seconds relative to created time.
++++++++ * secure: true if the cookie must be sent over a secure protocol.
++++++++ * httpOnly: true to restrict access to the cookie from javascript
++++++++ * (inaffective since the cookies are stored in javascript).
++++++++ * path: the path for the cookie.
++++++++ * domain: optional domain the cookie belongs to (must start with dot).
++++++++ * version: optional version of the cookie.
++++++++ * created: creation time, in UTC seconds, of the cookie.
++++++++ */
++++++++ client.setCookie = function(cookie) {
++++++++ var rval;
++++++++ if(typeof(cookie.name) !== 'undefined') {
++++++++ if(cookie.value === null || typeof(cookie.value) === 'undefined' ||
++++++++ cookie.value === '') {
++++++++ // remove cookie
++++++++ rval = client.removeCookie(cookie.name, cookie.path);
++++++++ } else {
++++++++ // set cookie defaults
++++++++ cookie.comment = cookie.comment || '';
++++++++ cookie.maxAge = cookie.maxAge || 0;
++++++++ cookie.secure = (typeof(cookie.secure) === 'undefined') ?
++++++++ true : cookie.secure;
++++++++ cookie.httpOnly = cookie.httpOnly || true;
++++++++ cookie.path = cookie.path || '/';
++++++++ cookie.domain = cookie.domain || null;
++++++++ cookie.version = cookie.version || null;
++++++++ cookie.created = _getUtcTime(new Date());
++++++++
++++++++ // do secure check
++++++++ if(cookie.secure !== client.secure) {
++++++++ var error = new Error('Http client url scheme is incompatible ' +
++++++++ 'with cookie secure flag.');
++++++++ error.url = client.url;
++++++++ error.cookie = cookie;
++++++++ throw error;
++++++++ }
++++++++ // make sure url host is within cookie.domain
++++++++ if(!http.withinCookieDomain(client.url, cookie)) {
++++++++ var error = new Error('Http client url scheme is incompatible ' +
++++++++ 'with cookie secure flag.');
++++++++ error.url = client.url;
++++++++ error.cookie = cookie;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // add new cookie
++++++++ if(!(cookie.name in client.cookies)) {
++++++++ client.cookies[cookie.name] = {};
++++++++ }
++++++++ client.cookies[cookie.name][cookie.path] = cookie;
++++++++ rval = true;
++++++++
++++++++ // save cookies
++++++++ _saveCookies(client);
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets a cookie by its name.
++++++++ *
++++++++ * @param name the name of the cookie to retrieve.
++++++++ * @param path an optional path for the cookie (if there are multiple
++++++++ * cookies with the same name but different paths).
++++++++ *
++++++++ * @return the cookie or null if not found.
++++++++ */
++++++++ client.getCookie = function(name, path) {
++++++++ var rval = null;
++++++++ if(name in client.cookies) {
++++++++ var paths = client.cookies[name];
++++++++
++++++++ // get path-specific cookie
++++++++ if(path) {
++++++++ if(path in paths) {
++++++++ rval = paths[path];
++++++++ }
++++++++ } else {
++++++++ // get first cookie
++++++++ for(var p in paths) {
++++++++ rval = paths[p];
++++++++ break;
++++++++ }
++++++++ }
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Removes a cookie.
++++++++ *
++++++++ * @param name the name of the cookie to remove.
++++++++ * @param path an optional path for the cookie (if there are multiple
++++++++ * cookies with the same name but different paths).
++++++++ *
++++++++ * @return true if a cookie was removed, false if not.
++++++++ */
++++++++ client.removeCookie = function(name, path) {
++++++++ var rval = false;
++++++++ if(name in client.cookies) {
++++++++ // delete the specific path
++++++++ if(path) {
++++++++ var paths = client.cookies[name];
++++++++ if(path in paths) {
++++++++ rval = true;
++++++++ delete client.cookies[name][path];
++++++++ // clean up entry if empty
++++++++ var empty = true;
++++++++ for(var i in client.cookies[name]) {
++++++++ empty = false;
++++++++ break;
++++++++ }
++++++++ if(empty) {
++++++++ delete client.cookies[name];
++++++++ }
++++++++ }
++++++++ } else {
++++++++ // delete all cookies with the given name
++++++++ rval = true;
++++++++ delete client.cookies[name];
++++++++ }
++++++++ }
++++++++ if(rval) {
++++++++ // save cookies
++++++++ _saveCookies(client);
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Clears all cookies stored in this client.
++++++++ */
++++++++ client.clearCookies = function() {
++++++++ client.cookies = {};
++++++++ _clearCookies(client);
++++++++ };
++++++++
++++++++ if(forge.log) {
++++++++ forge.log.debug('forge.http', 'created client', options);
++++++++ }
++++++++
++++++++ return client;
++++++++};
++++++++
++++++++/**
++++++++ * Trims the whitespace off of the beginning and end of a string.
++++++++ *
++++++++ * @param str the string to trim.
++++++++ *
++++++++ * @return the trimmed string.
++++++++ */
++++++++var _trimString = function(str) {
++++++++ return str.replace(/^\s*/, '').replace(/\s*$/, '');
++++++++};
++++++++
++++++++/**
++++++++ * Creates an http header object.
++++++++ *
++++++++ * @return the http header object.
++++++++ */
++++++++var _createHeader = function() {
++++++++ var header = {
++++++++ fields: {},
++++++++ setField: function(name, value) {
++++++++ // normalize field name, trim value
++++++++ header.fields[_normalize(name)] = [_trimString('' + value)];
++++++++ },
++++++++ appendField: function(name, value) {
++++++++ name = _normalize(name);
++++++++ if(!(name in header.fields)) {
++++++++ header.fields[name] = [];
++++++++ }
++++++++ header.fields[name].push(_trimString('' + value));
++++++++ },
++++++++ getField: function(name, index) {
++++++++ var rval = null;
++++++++ name = _normalize(name);
++++++++ if(name in header.fields) {
++++++++ index = index || 0;
++++++++ rval = header.fields[name][index];
++++++++ }
++++++++ return rval;
++++++++ }
++++++++ };
++++++++ return header;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the time in utc seconds given a date.
++++++++ *
++++++++ * @param d the date to use.
++++++++ *
++++++++ * @return the time in utc seconds.
++++++++ */
++++++++var _getUtcTime = function(d) {
++++++++ var utc = +d + d.getTimezoneOffset() * 60000;
++++++++ return Math.floor(+new Date() / 1000);
++++++++};
++++++++
++++++++/**
++++++++ * Creates an http request.
++++++++ *
++++++++ * @param options:
++++++++ * version: the version.
++++++++ * method: the method.
++++++++ * path: the path.
++++++++ * body: the body.
++++++++ * headers: custom header fields to add,
++++++++ * eg: [{'Content-Length': 0}].
++++++++ *
++++++++ * @return the http request.
++++++++ */
++++++++http.createRequest = function(options) {
++++++++ options = options || {};
++++++++ var request = _createHeader();
++++++++ request.version = options.version || 'HTTP/1.1';
++++++++ request.method = options.method || null;
++++++++ request.path = options.path || null;
++++++++ request.body = options.body || null;
++++++++ request.bodyDeflated = false;
++++++++ request.flashApi = null;
++++++++
++++++++ // add custom headers
++++++++ var headers = options.headers || [];
++++++++ if(!forge.util.isArray(headers)) {
++++++++ headers = [headers];
++++++++ }
++++++++ for(var i = 0; i < headers.length; ++i) {
++++++++ for(var name in headers[i]) {
++++++++ request.appendField(name, headers[i][name]);
++++++++ }
++++++++ }
++++++++
++++++++ /**
++++++++ * Adds a cookie to the request 'Cookie' header.
++++++++ *
++++++++ * @param cookie a cookie to add.
++++++++ */
++++++++ request.addCookie = function(cookie) {
++++++++ var value = '';
++++++++ var field = request.getField('Cookie');
++++++++ if(field !== null) {
++++++++ // separate cookies by semi-colons
++++++++ value = field + '; ';
++++++++ }
++++++++
++++++++ // get current time in utc seconds
++++++++ var now = _getUtcTime(new Date());
++++++++
++++++++ // output cookie name and value
++++++++ value += cookie.name + '=' + cookie.value;
++++++++ request.setField('Cookie', value);
++++++++ };
++++++++
++++++++ /**
++++++++ * Converts an http request into a string that can be sent as an
++++++++ * HTTP request. Does not include any data.
++++++++ *
++++++++ * @return the string representation of the request.
++++++++ */
++++++++ request.toString = function() {
++++++++ /* Sample request header:
++++++++ GET /some/path/?query HTTP/1.1
++++++++ Host: www.someurl.com
++++++++ Connection: close
++++++++ Accept-Encoding: deflate
++++++++ Accept: image/gif, text/html
++++++++ User-Agent: Mozilla 4.0
++++++++ */
++++++++
++++++++ // set default headers
++++++++ if(request.getField('User-Agent') === null) {
++++++++ request.setField('User-Agent', 'forge.http 1.0');
++++++++ }
++++++++ if(request.getField('Accept') === null) {
++++++++ request.setField('Accept', '*/*');
++++++++ }
++++++++ if(request.getField('Connection') === null) {
++++++++ request.setField('Connection', 'keep-alive');
++++++++ request.setField('Keep-Alive', '115');
++++++++ }
++++++++
++++++++ // add Accept-Encoding if not specified
++++++++ if(request.flashApi !== null &&
++++++++ request.getField('Accept-Encoding') === null) {
++++++++ request.setField('Accept-Encoding', 'deflate');
++++++++ }
++++++++
++++++++ // if the body isn't null, deflate it if its larger than 100 bytes
++++++++ if(request.flashApi !== null && request.body !== null &&
++++++++ request.getField('Content-Encoding') === null &&
++++++++ !request.bodyDeflated && request.body.length > 100) {
++++++++ // use flash to compress data
++++++++ request.body = forge.util.deflate(request.flashApi, request.body);
++++++++ request.bodyDeflated = true;
++++++++ request.setField('Content-Encoding', 'deflate');
++++++++ request.setField('Content-Length', request.body.length);
++++++++ } else if(request.body !== null) {
++++++++ // set content length for body
++++++++ request.setField('Content-Length', request.body.length);
++++++++ }
++++++++
++++++++ // build start line
++++++++ var rval =
++++++++ request.method.toUpperCase() + ' ' + request.path + ' ' +
++++++++ request.version + '\r\n';
++++++++
++++++++ // add each header
++++++++ for(var name in request.fields) {
++++++++ var fields = request.fields[name];
++++++++ for(var i = 0; i < fields.length; ++i) {
++++++++ rval += name + ': ' + fields[i] + '\r\n';
++++++++ }
++++++++ }
++++++++ // final terminating CRLF
++++++++ rval += '\r\n';
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ return request;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an empty http response header.
++++++++ *
++++++++ * @return the empty http response header.
++++++++ */
++++++++http.createResponse = function() {
++++++++ // private vars
++++++++ var _first = true;
++++++++ var _chunkSize = 0;
++++++++ var _chunksFinished = false;
++++++++
++++++++ // create response
++++++++ var response = _createHeader();
++++++++ response.version = null;
++++++++ response.code = 0;
++++++++ response.message = null;
++++++++ response.body = null;
++++++++ response.headerReceived = false;
++++++++ response.bodyReceived = false;
++++++++ response.flashApi = null;
++++++++
++++++++ /**
++++++++ * Reads a line that ends in CRLF from a byte buffer.
++++++++ *
++++++++ * @param b the byte buffer.
++++++++ *
++++++++ * @return the line or null if none was found.
++++++++ */
++++++++ var _readCrlf = function(b) {
++++++++ var line = null;
++++++++ var i = b.data.indexOf('\r\n', b.read);
++++++++ if(i != -1) {
++++++++ // read line, skip CRLF
++++++++ line = b.getBytes(i - b.read);
++++++++ b.getBytes(2);
++++++++ }
++++++++ return line;
++++++++ };
++++++++
++++++++ /**
++++++++ * Parses a header field and appends it to the response.
++++++++ *
++++++++ * @param line the header field line.
++++++++ */
++++++++ var _parseHeader = function(line) {
++++++++ var tmp = line.indexOf(':');
++++++++ var name = line.substring(0, tmp++);
++++++++ response.appendField(
++++++++ name, (tmp < line.length) ? line.substring(tmp) : '');
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads an http response header from a buffer of bytes.
++++++++ *
++++++++ * @param b the byte buffer to parse the header from.
++++++++ *
++++++++ * @return true if the whole header was read, false if not.
++++++++ */
++++++++ response.readHeader = function(b) {
++++++++ // read header lines (each ends in CRLF)
++++++++ var line = '';
++++++++ while(!response.headerReceived && line !== null) {
++++++++ line = _readCrlf(b);
++++++++ if(line !== null) {
++++++++ // parse first line
++++++++ if(_first) {
++++++++ _first = false;
++++++++ var tmp = line.split(' ');
++++++++ if(tmp.length >= 3) {
++++++++ response.version = tmp[0];
++++++++ response.code = parseInt(tmp[1], 10);
++++++++ response.message = tmp.slice(2).join(' ');
++++++++ } else {
++++++++ // invalid header
++++++++ var error = new Error('Invalid http response header.');
++++++++ error.details = {'line': line};
++++++++ throw error;
++++++++ }
++++++++ } else if(line.length === 0) {
++++++++ // handle final line, end of header
++++++++ response.headerReceived = true;
++++++++ } else {
++++++++ _parseHeader(line);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return response.headerReceived;
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads some chunked http response entity-body from the given buffer of
++++++++ * bytes.
++++++++ *
++++++++ * @param b the byte buffer to read from.
++++++++ *
++++++++ * @return true if the whole body was read, false if not.
++++++++ */
++++++++ var _readChunkedBody = function(b) {
++++++++ /* Chunked transfer-encoding sends data in a series of chunks,
++++++++ followed by a set of 0-N http trailers.
++++++++ The format is as follows:
++++++++
++++++++ chunk-size (in hex) CRLF
++++++++ chunk data (with "chunk-size" many bytes) CRLF
++++++++ ... (N many chunks)
++++++++ chunk-size (of 0 indicating the last chunk) CRLF
++++++++ N many http trailers followed by CRLF
++++++++ blank line + CRLF (terminates the trailers)
++++++++
++++++++ If there are no http trailers, then after the chunk-size of 0,
++++++++ there is still a single CRLF (indicating the blank line + CRLF
++++++++ that terminates the trailers). In other words, you always terminate
++++++++ the trailers with blank line + CRLF, regardless of 0-N trailers. */
++++++++
++++++++ /* From RFC-2616, section 3.6.1, here is the pseudo-code for
++++++++ implementing chunked transfer-encoding:
++++++++
++++++++ length := 0
++++++++ read chunk-size, chunk-extension (if any) and CRLF
++++++++ while (chunk-size > 0) {
++++++++ read chunk-data and CRLF
++++++++ append chunk-data to entity-body
++++++++ length := length + chunk-size
++++++++ read chunk-size and CRLF
++++++++ }
++++++++ read entity-header
++++++++ while (entity-header not empty) {
++++++++ append entity-header to existing header fields
++++++++ read entity-header
++++++++ }
++++++++ Content-Length := length
++++++++ Remove "chunked" from Transfer-Encoding
++++++++ */
++++++++
++++++++ var line = '';
++++++++ while(line !== null && b.length() > 0) {
++++++++ // if in the process of reading a chunk
++++++++ if(_chunkSize > 0) {
++++++++ // if there are not enough bytes to read chunk and its
++++++++ // trailing CRLF, we must wait for more data to be received
++++++++ if(_chunkSize + 2 > b.length()) {
++++++++ break;
++++++++ }
++++++++
++++++++ // read chunk data, skip CRLF
++++++++ response.body += b.getBytes(_chunkSize);
++++++++ b.getBytes(2);
++++++++ _chunkSize = 0;
++++++++ } else if(!_chunksFinished) {
++++++++ // more chunks, read next chunk-size line
++++++++ line = _readCrlf(b);
++++++++ if(line !== null) {
++++++++ // parse chunk-size (ignore any chunk extension)
++++++++ _chunkSize = parseInt(line.split(';', 1)[0], 16);
++++++++ _chunksFinished = (_chunkSize === 0);
++++++++ }
++++++++ } else {
++++++++ // chunks finished, read next trailer
++++++++ line = _readCrlf(b);
++++++++ while(line !== null) {
++++++++ if(line.length > 0) {
++++++++ // parse trailer
++++++++ _parseHeader(line);
++++++++ // read next trailer
++++++++ line = _readCrlf(b);
++++++++ } else {
++++++++ // body received
++++++++ response.bodyReceived = true;
++++++++ line = null;
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return response.bodyReceived;
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads an http response body from a buffer of bytes.
++++++++ *
++++++++ * @param b the byte buffer to read from.
++++++++ *
++++++++ * @return true if the whole body was read, false if not.
++++++++ */
++++++++ response.readBody = function(b) {
++++++++ var contentLength = response.getField('Content-Length');
++++++++ var transferEncoding = response.getField('Transfer-Encoding');
++++++++ if(contentLength !== null) {
++++++++ contentLength = parseInt(contentLength);
++++++++ }
++++++++
++++++++ // read specified length
++++++++ if(contentLength !== null && contentLength >= 0) {
++++++++ response.body = response.body || '';
++++++++ response.body += b.getBytes(contentLength);
++++++++ response.bodyReceived = (response.body.length === contentLength);
++++++++ } else if(transferEncoding !== null) {
++++++++ // read chunked encoding
++++++++ if(transferEncoding.indexOf('chunked') != -1) {
++++++++ response.body = response.body || '';
++++++++ _readChunkedBody(b);
++++++++ } else {
++++++++ var error = new Error('Unknown Transfer-Encoding.');
++++++++ error.details = {'transferEncoding': transferEncoding};
++++++++ throw error;
++++++++ }
++++++++ } else if((contentLength !== null && contentLength < 0) ||
++++++++ (contentLength === null &&
++++++++ response.getField('Content-Type') !== null)) {
++++++++ // read all data in the buffer
++++++++ response.body = response.body || '';
++++++++ response.body += b.getBytes();
++++++++ response.readBodyUntilClose = true;
++++++++ } else {
++++++++ // no body
++++++++ response.body = null;
++++++++ response.bodyReceived = true;
++++++++ }
++++++++
++++++++ if(response.bodyReceived) {
++++++++ response.time = +new Date() - response.time;
++++++++ }
++++++++
++++++++ if(response.flashApi !== null &&
++++++++ response.bodyReceived && response.body !== null &&
++++++++ response.getField('Content-Encoding') === 'deflate') {
++++++++ // inflate using flash api
++++++++ response.body = forge.util.inflate(
++++++++ response.flashApi, response.body);
++++++++ }
++++++++
++++++++ return response.bodyReceived;
++++++++ };
++++++++
++++++++ /**
++++++++ * Parses an array of cookies from the 'Set-Cookie' field, if present.
++++++++ *
++++++++ * @return the array of cookies.
++++++++ */
++++++++ response.getCookies = function() {
++++++++ var rval = [];
++++++++
++++++++ // get Set-Cookie field
++++++++ if('Set-Cookie' in response.fields) {
++++++++ var field = response.fields['Set-Cookie'];
++++++++
++++++++ // get current local time in seconds
++++++++ var now = +new Date() / 1000;
++++++++
++++++++ // regex for parsing 'name1=value1; name2=value2; name3'
++++++++ var regex = /\s*([^=]*)=?([^;]*)(;|$)/g;
++++++++
++++++++ // examples:
++++++++ // Set-Cookie: cookie1_name=cookie1_value; max-age=0; path=/
++++++++ // Set-Cookie: c2=v2; expires=Thu, 21-Aug-2008 23:47:25 GMT; path=/
++++++++ for(var i = 0; i < field.length; ++i) {
++++++++ var fv = field[i];
++++++++ var m;
++++++++ regex.lastIndex = 0;
++++++++ var first = true;
++++++++ var cookie = {};
++++++++ do {
++++++++ m = regex.exec(fv);
++++++++ if(m !== null) {
++++++++ var name = _trimString(m[1]);
++++++++ var value = _trimString(m[2]);
++++++++
++++++++ // cookie_name=value
++++++++ if(first) {
++++++++ cookie.name = name;
++++++++ cookie.value = value;
++++++++ first = false;
++++++++ } else {
++++++++ // property_name=value
++++++++ name = name.toLowerCase();
++++++++ switch(name) {
++++++++ case 'expires':
++++++++ // replace hyphens w/spaces so date will parse
++++++++ value = value.replace(/-/g, ' ');
++++++++ var secs = Date.parse(value) / 1000;
++++++++ cookie.maxAge = Math.max(0, secs - now);
++++++++ break;
++++++++ case 'max-age':
++++++++ cookie.maxAge = parseInt(value, 10);
++++++++ break;
++++++++ case 'secure':
++++++++ cookie.secure = true;
++++++++ break;
++++++++ case 'httponly':
++++++++ cookie.httpOnly = true;
++++++++ break;
++++++++ default:
++++++++ if(name !== '') {
++++++++ cookie[name] = value;
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ } while(m !== null && m[0] !== '');
++++++++ rval.push(cookie);
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Converts an http response into a string that can be sent as an
++++++++ * HTTP response. Does not include any data.
++++++++ *
++++++++ * @return the string representation of the response.
++++++++ */
++++++++ response.toString = function() {
++++++++ /* Sample response header:
++++++++ HTTP/1.0 200 OK
++++++++ Host: www.someurl.com
++++++++ Connection: close
++++++++ */
++++++++
++++++++ // build start line
++++++++ var rval =
++++++++ response.version + ' ' + response.code + ' ' + response.message + '\r\n';
++++++++
++++++++ // add each header
++++++++ for(var name in response.fields) {
++++++++ var fields = response.fields[name];
++++++++ for(var i = 0; i < fields.length; ++i) {
++++++++ rval += name + ': ' + fields[i] + '\r\n';
++++++++ }
++++++++ }
++++++++ // final terminating CRLF
++++++++ rval += '\r\n';
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ return response;
++++++++};
++++++++
++++++++/**
++++++++ * Returns true if the given url is within the given cookie's domain.
++++++++ *
++++++++ * @param url the url to check.
++++++++ * @param cookie the cookie or cookie domain to check.
++++++++ */
++++++++http.withinCookieDomain = function(url, cookie) {
++++++++ var rval = false;
++++++++
++++++++ // cookie may be null, a cookie object, or a domain string
++++++++ var domain = (cookie === null || typeof cookie === 'string') ?
++++++++ cookie : cookie.domain;
++++++++
++++++++ // any domain will do
++++++++ if(domain === null) {
++++++++ rval = true;
++++++++ } else if(domain.charAt(0) === '.') {
++++++++ // ensure domain starts with a '.'
++++++++ // parse URL as necessary
++++++++ if(typeof url === 'string') {
++++++++ url = new URL(url);
++++++++ }
++++++++
++++++++ // add '.' to front of URL hostname to match against domain
++++++++ var host = '.' + url.hostname;
++++++++
++++++++ // if the host ends with domain then it falls within it
++++++++ var idx = host.lastIndexOf(domain);
++++++++ if(idx !== -1 && (idx + domain.length === host.length)) {
++++++++ rval = true;
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for Forge with extra utils and networking.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright 2011-2016 Digital Bazaar, Inc.
++++++++ */
++++++++module.exports = require('./forge');
++++++++// require core forge
++++++++require('./index');
++++++++// additional utils and networking support
++++++++require('./form');
++++++++require('./socket');
++++++++require('./tlssocket');
++++++++require('./http');
++++++++require('./xhr');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for Forge.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright 2011-2016 Digital Bazaar, Inc.
++++++++ */
++++++++module.exports = require('./forge');
++++++++require('./aes');
++++++++require('./aesCipherSuites');
++++++++require('./asn1');
++++++++require('./cipher');
++++++++require('./des');
++++++++require('./ed25519');
++++++++require('./hmac');
++++++++require('./kem');
++++++++require('./log');
++++++++require('./md.all');
++++++++require('./mgf1');
++++++++require('./pbkdf2');
++++++++require('./pem');
++++++++require('./pkcs1');
++++++++require('./pkcs12');
++++++++require('./pkcs7');
++++++++require('./pki');
++++++++require('./prime');
++++++++require('./prng');
++++++++require('./pss');
++++++++require('./random');
++++++++require('./rc2');
++++++++require('./ssh');
++++++++require('./tls');
++++++++require('./util');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++// Copyright (c) 2005 Tom Wu
++++++++// All Rights Reserved.
++++++++// See "LICENSE" for details.
++++++++
++++++++// Basic JavaScript BN library - subset useful for RSA encryption.
++++++++
++++++++/*
++++++++Licensing (LICENSE)
++++++++-------------------
++++++++
++++++++This software is covered under the following copyright:
++++++++*/
++++++++/*
++++++++ * Copyright (c) 2003-2005 Tom Wu
++++++++ * All Rights Reserved.
++++++++ *
++++++++ * Permission is hereby granted, free of charge, to any person obtaining
++++++++ * a copy of this software and associated documentation files (the
++++++++ * "Software"), to deal in the Software without restriction, including
++++++++ * without limitation the rights to use, copy, modify, merge, publish,
++++++++ * distribute, sublicense, and/or sell copies of the Software, and to
++++++++ * permit persons to whom the Software is furnished to do so, subject to
++++++++ * the following conditions:
++++++++ *
++++++++ * The above copyright notice and this permission notice shall be
++++++++ * included in all copies or substantial portions of the Software.
++++++++ *
++++++++ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
++++++++ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
++++++++ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
++++++++ *
++++++++ * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
++++++++ * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
++++++++ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
++++++++ * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
++++++++ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
++++++++ *
++++++++ * In addition, the following condition applies:
++++++++ *
++++++++ * All redistributions must retain an intact copy of this copyright notice
++++++++ * and disclaimer.
++++++++ */
++++++++/*
++++++++Address all questions regarding this license to:
++++++++
++++++++ Tom Wu
++++++++ tjw@cs.Stanford.EDU
++++++++*/
++++++++var forge = require('./forge');
++++++++
++++++++module.exports = forge.jsbn = forge.jsbn || {};
++++++++
++++++++// Bits per digit
++++++++var dbits;
++++++++
++++++++// JavaScript engine analysis
++++++++var canary = 0xdeadbeefcafe;
++++++++var j_lm = ((canary&0xffffff)==0xefcafe);
++++++++
++++++++// (public) Constructor
++++++++function BigInteger(a,b,c) {
++++++++ this.data = [];
++++++++ if(a != null)
++++++++ if("number" == typeof a) this.fromNumber(a,b,c);
++++++++ else if(b == null && "string" != typeof a) this.fromString(a,256);
++++++++ else this.fromString(a,b);
++++++++}
++++++++forge.jsbn.BigInteger = BigInteger;
++++++++
++++++++// return new, unset BigInteger
++++++++function nbi() { return new BigInteger(null); }
++++++++
++++++++// am: Compute w_j += (x*this_i), propagate carries,
++++++++// c is initial carry, returns final carry.
++++++++// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
++++++++// We need to select the fastest one that works in this environment.
++++++++
++++++++// am1: use a single mult and divide to get the high bits,
++++++++// max digit bits should be 26 because
++++++++// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
++++++++function am1(i,x,w,j,c,n) {
++++++++ while(--n >= 0) {
++++++++ var v = x*this.data[i++]+w.data[j]+c;
++++++++ c = Math.floor(v/0x4000000);
++++++++ w.data[j++] = v&0x3ffffff;
++++++++ }
++++++++ return c;
++++++++}
++++++++// am2 avoids a big mult-and-extract completely.
++++++++// Max digit bits should be <= 30 because we do bitwise ops
++++++++// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
++++++++function am2(i,x,w,j,c,n) {
++++++++ var xl = x&0x7fff, xh = x>>15;
++++++++ while(--n >= 0) {
++++++++ var l = this.data[i]&0x7fff;
++++++++ var h = this.data[i++]>>15;
++++++++ var m = xh*l+h*xl;
++++++++ l = xl*l+((m&0x7fff)<<15)+w.data[j]+(c&0x3fffffff);
++++++++ c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
++++++++ w.data[j++] = l&0x3fffffff;
++++++++ }
++++++++ return c;
++++++++}
++++++++// Alternately, set max digit bits to 28 since some
++++++++// browsers slow down when dealing with 32-bit numbers.
++++++++function am3(i,x,w,j,c,n) {
++++++++ var xl = x&0x3fff, xh = x>>14;
++++++++ while(--n >= 0) {
++++++++ var l = this.data[i]&0x3fff;
++++++++ var h = this.data[i++]>>14;
++++++++ var m = xh*l+h*xl;
++++++++ l = xl*l+((m&0x3fff)<<14)+w.data[j]+c;
++++++++ c = (l>>28)+(m>>14)+xh*h;
++++++++ w.data[j++] = l&0xfffffff;
++++++++ }
++++++++ return c;
++++++++}
++++++++
++++++++// node.js (no browser)
++++++++if(typeof(navigator) === 'undefined')
++++++++{
++++++++ BigInteger.prototype.am = am3;
++++++++ dbits = 28;
++++++++} else if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
++++++++ BigInteger.prototype.am = am2;
++++++++ dbits = 30;
++++++++} else if(j_lm && (navigator.appName != "Netscape")) {
++++++++ BigInteger.prototype.am = am1;
++++++++ dbits = 26;
++++++++} else { // Mozilla/Netscape seems to prefer am3
++++++++ BigInteger.prototype.am = am3;
++++++++ dbits = 28;
++++++++}
++++++++
++++++++BigInteger.prototype.DB = dbits;
++++++++BigInteger.prototype.DM = ((1<<dbits)-1);
++++++++BigInteger.prototype.DV = (1<<dbits);
++++++++
++++++++var BI_FP = 52;
++++++++BigInteger.prototype.FV = Math.pow(2,BI_FP);
++++++++BigInteger.prototype.F1 = BI_FP-dbits;
++++++++BigInteger.prototype.F2 = 2*dbits-BI_FP;
++++++++
++++++++// Digit conversions
++++++++var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
++++++++var BI_RC = new Array();
++++++++var rr,vv;
++++++++rr = "0".charCodeAt(0);
++++++++for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
++++++++rr = "a".charCodeAt(0);
++++++++for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
++++++++rr = "A".charCodeAt(0);
++++++++for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
++++++++
++++++++function int2char(n) { return BI_RM.charAt(n); }
++++++++function intAt(s,i) {
++++++++ var c = BI_RC[s.charCodeAt(i)];
++++++++ return (c==null)?-1:c;
++++++++}
++++++++
++++++++// (protected) copy this to r
++++++++function bnpCopyTo(r) {
++++++++ for(var i = this.t-1; i >= 0; --i) r.data[i] = this.data[i];
++++++++ r.t = this.t;
++++++++ r.s = this.s;
++++++++}
++++++++
++++++++// (protected) set from integer value x, -DV <= x < DV
++++++++function bnpFromInt(x) {
++++++++ this.t = 1;
++++++++ this.s = (x<0)?-1:0;
++++++++ if(x > 0) this.data[0] = x;
++++++++ else if(x < -1) this.data[0] = x+this.DV;
++++++++ else this.t = 0;
++++++++}
++++++++
++++++++// return bigint initialized to value
++++++++function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
++++++++
++++++++// (protected) set from string and radix
++++++++function bnpFromString(s,b) {
++++++++ var k;
++++++++ if(b == 16) k = 4;
++++++++ else if(b == 8) k = 3;
++++++++ else if(b == 256) k = 8; // byte array
++++++++ else if(b == 2) k = 1;
++++++++ else if(b == 32) k = 5;
++++++++ else if(b == 4) k = 2;
++++++++ else { this.fromRadix(s,b); return; }
++++++++ this.t = 0;
++++++++ this.s = 0;
++++++++ var i = s.length, mi = false, sh = 0;
++++++++ while(--i >= 0) {
++++++++ var x = (k==8)?s[i]&0xff:intAt(s,i);
++++++++ if(x < 0) {
++++++++ if(s.charAt(i) == "-") mi = true;
++++++++ continue;
++++++++ }
++++++++ mi = false;
++++++++ if(sh == 0)
++++++++ this.data[this.t++] = x;
++++++++ else if(sh+k > this.DB) {
++++++++ this.data[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
++++++++ this.data[this.t++] = (x>>(this.DB-sh));
++++++++ } else
++++++++ this.data[this.t-1] |= x<<sh;
++++++++ sh += k;
++++++++ if(sh >= this.DB) sh -= this.DB;
++++++++ }
++++++++ if(k == 8 && (s[0]&0x80) != 0) {
++++++++ this.s = -1;
++++++++ if(sh > 0) this.data[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
++++++++ }
++++++++ this.clamp();
++++++++ if(mi) BigInteger.ZERO.subTo(this,this);
++++++++}
++++++++
++++++++// (protected) clamp off excess high words
++++++++function bnpClamp() {
++++++++ var c = this.s&this.DM;
++++++++ while(this.t > 0 && this.data[this.t-1] == c) --this.t;
++++++++}
++++++++
++++++++// (public) return string representation in given radix
++++++++function bnToString(b) {
++++++++ if(this.s < 0) return "-"+this.negate().toString(b);
++++++++ var k;
++++++++ if(b == 16) k = 4;
++++++++ else if(b == 8) k = 3;
++++++++ else if(b == 2) k = 1;
++++++++ else if(b == 32) k = 5;
++++++++ else if(b == 4) k = 2;
++++++++ else return this.toRadix(b);
++++++++ var km = (1<<k)-1, d, m = false, r = "", i = this.t;
++++++++ var p = this.DB-(i*this.DB)%k;
++++++++ if(i-- > 0) {
++++++++ if(p < this.DB && (d = this.data[i]>>p) > 0) { m = true; r = int2char(d); }
++++++++ while(i >= 0) {
++++++++ if(p < k) {
++++++++ d = (this.data[i]&((1<<p)-1))<<(k-p);
++++++++ d |= this.data[--i]>>(p+=this.DB-k);
++++++++ } else {
++++++++ d = (this.data[i]>>(p-=k))&km;
++++++++ if(p <= 0) { p += this.DB; --i; }
++++++++ }
++++++++ if(d > 0) m = true;
++++++++ if(m) r += int2char(d);
++++++++ }
++++++++ }
++++++++ return m?r:"0";
++++++++}
++++++++
++++++++// (public) -this
++++++++function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
++++++++
++++++++// (public) |this|
++++++++function bnAbs() { return (this.s<0)?this.negate():this; }
++++++++
++++++++// (public) return + if this > a, - if this < a, 0 if equal
++++++++function bnCompareTo(a) {
++++++++ var r = this.s-a.s;
++++++++ if(r != 0) return r;
++++++++ var i = this.t;
++++++++ r = i-a.t;
++++++++ if(r != 0) return (this.s<0)?-r:r;
++++++++ while(--i >= 0) if((r=this.data[i]-a.data[i]) != 0) return r;
++++++++ return 0;
++++++++}
++++++++
++++++++// returns bit length of the integer x
++++++++function nbits(x) {
++++++++ var r = 1, t;
++++++++ if((t=x>>>16) != 0) { x = t; r += 16; }
++++++++ if((t=x>>8) != 0) { x = t; r += 8; }
++++++++ if((t=x>>4) != 0) { x = t; r += 4; }
++++++++ if((t=x>>2) != 0) { x = t; r += 2; }
++++++++ if((t=x>>1) != 0) { x = t; r += 1; }
++++++++ return r;
++++++++}
++++++++
++++++++// (public) return the number of bits in "this"
++++++++function bnBitLength() {
++++++++ if(this.t <= 0) return 0;
++++++++ return this.DB*(this.t-1)+nbits(this.data[this.t-1]^(this.s&this.DM));
++++++++}
++++++++
++++++++// (protected) r = this << n*DB
++++++++function bnpDLShiftTo(n,r) {
++++++++ var i;
++++++++ for(i = this.t-1; i >= 0; --i) r.data[i+n] = this.data[i];
++++++++ for(i = n-1; i >= 0; --i) r.data[i] = 0;
++++++++ r.t = this.t+n;
++++++++ r.s = this.s;
++++++++}
++++++++
++++++++// (protected) r = this >> n*DB
++++++++function bnpDRShiftTo(n,r) {
++++++++ for(var i = n; i < this.t; ++i) r.data[i-n] = this.data[i];
++++++++ r.t = Math.max(this.t-n,0);
++++++++ r.s = this.s;
++++++++}
++++++++
++++++++// (protected) r = this << n
++++++++function bnpLShiftTo(n,r) {
++++++++ var bs = n%this.DB;
++++++++ var cbs = this.DB-bs;
++++++++ var bm = (1<<cbs)-1;
++++++++ var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
++++++++ for(i = this.t-1; i >= 0; --i) {
++++++++ r.data[i+ds+1] = (this.data[i]>>cbs)|c;
++++++++ c = (this.data[i]&bm)<<bs;
++++++++ }
++++++++ for(i = ds-1; i >= 0; --i) r.data[i] = 0;
++++++++ r.data[ds] = c;
++++++++ r.t = this.t+ds+1;
++++++++ r.s = this.s;
++++++++ r.clamp();
++++++++}
++++++++
++++++++// (protected) r = this >> n
++++++++function bnpRShiftTo(n,r) {
++++++++ r.s = this.s;
++++++++ var ds = Math.floor(n/this.DB);
++++++++ if(ds >= this.t) { r.t = 0; return; }
++++++++ var bs = n%this.DB;
++++++++ var cbs = this.DB-bs;
++++++++ var bm = (1<<bs)-1;
++++++++ r.data[0] = this.data[ds]>>bs;
++++++++ for(var i = ds+1; i < this.t; ++i) {
++++++++ r.data[i-ds-1] |= (this.data[i]&bm)<<cbs;
++++++++ r.data[i-ds] = this.data[i]>>bs;
++++++++ }
++++++++ if(bs > 0) r.data[this.t-ds-1] |= (this.s&bm)<<cbs;
++++++++ r.t = this.t-ds;
++++++++ r.clamp();
++++++++}
++++++++
++++++++// (protected) r = this - a
++++++++function bnpSubTo(a,r) {
++++++++ var i = 0, c = 0, m = Math.min(a.t,this.t);
++++++++ while(i < m) {
++++++++ c += this.data[i]-a.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++ }
++++++++ if(a.t < this.t) {
++++++++ c -= a.s;
++++++++ while(i < this.t) {
++++++++ c += this.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++ }
++++++++ c += this.s;
++++++++ } else {
++++++++ c += this.s;
++++++++ while(i < a.t) {
++++++++ c -= a.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++ }
++++++++ c -= a.s;
++++++++ }
++++++++ r.s = (c<0)?-1:0;
++++++++ if(c < -1) r.data[i++] = this.DV+c;
++++++++ else if(c > 0) r.data[i++] = c;
++++++++ r.t = i;
++++++++ r.clamp();
++++++++}
++++++++
++++++++// (protected) r = this * a, r != this,a (HAC 14.12)
++++++++// "this" should be the larger one if appropriate.
++++++++function bnpMultiplyTo(a,r) {
++++++++ var x = this.abs(), y = a.abs();
++++++++ var i = x.t;
++++++++ r.t = i+y.t;
++++++++ while(--i >= 0) r.data[i] = 0;
++++++++ for(i = 0; i < y.t; ++i) r.data[i+x.t] = x.am(0,y.data[i],r,i,0,x.t);
++++++++ r.s = 0;
++++++++ r.clamp();
++++++++ if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
++++++++}
++++++++
++++++++// (protected) r = this^2, r != this (HAC 14.16)
++++++++function bnpSquareTo(r) {
++++++++ var x = this.abs();
++++++++ var i = r.t = 2*x.t;
++++++++ while(--i >= 0) r.data[i] = 0;
++++++++ for(i = 0; i < x.t-1; ++i) {
++++++++ var c = x.am(i,x.data[i],r,2*i,0,1);
++++++++ if((r.data[i+x.t]+=x.am(i+1,2*x.data[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
++++++++ r.data[i+x.t] -= x.DV;
++++++++ r.data[i+x.t+1] = 1;
++++++++ }
++++++++ }
++++++++ if(r.t > 0) r.data[r.t-1] += x.am(i,x.data[i],r,2*i,0,1);
++++++++ r.s = 0;
++++++++ r.clamp();
++++++++}
++++++++
++++++++// (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
++++++++// r != q, this != m. q or r may be null.
++++++++function bnpDivRemTo(m,q,r) {
++++++++ var pm = m.abs();
++++++++ if(pm.t <= 0) return;
++++++++ var pt = this.abs();
++++++++ if(pt.t < pm.t) {
++++++++ if(q != null) q.fromInt(0);
++++++++ if(r != null) this.copyTo(r);
++++++++ return;
++++++++ }
++++++++ if(r == null) r = nbi();
++++++++ var y = nbi(), ts = this.s, ms = m.s;
++++++++ var nsh = this.DB-nbits(pm.data[pm.t-1]); // normalize modulus
++++++++ if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } else { pm.copyTo(y); pt.copyTo(r); }
++++++++ var ys = y.t;
++++++++ var y0 = y.data[ys-1];
++++++++ if(y0 == 0) return;
++++++++ var yt = y0*(1<<this.F1)+((ys>1)?y.data[ys-2]>>this.F2:0);
++++++++ var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
++++++++ var i = r.t, j = i-ys, t = (q==null)?nbi():q;
++++++++ y.dlShiftTo(j,t);
++++++++ if(r.compareTo(t) >= 0) {
++++++++ r.data[r.t++] = 1;
++++++++ r.subTo(t,r);
++++++++ }
++++++++ BigInteger.ONE.dlShiftTo(ys,t);
++++++++ t.subTo(y,y); // "negative" y so we can replace sub with am later
++++++++ while(y.t < ys) y.data[y.t++] = 0;
++++++++ while(--j >= 0) {
++++++++ // Estimate quotient digit
++++++++ var qd = (r.data[--i]==y0)?this.DM:Math.floor(r.data[i]*d1+(r.data[i-1]+e)*d2);
++++++++ if((r.data[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
++++++++ y.dlShiftTo(j,t);
++++++++ r.subTo(t,r);
++++++++ while(r.data[i] < --qd) r.subTo(t,r);
++++++++ }
++++++++ }
++++++++ if(q != null) {
++++++++ r.drShiftTo(ys,q);
++++++++ if(ts != ms) BigInteger.ZERO.subTo(q,q);
++++++++ }
++++++++ r.t = ys;
++++++++ r.clamp();
++++++++ if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
++++++++ if(ts < 0) BigInteger.ZERO.subTo(r,r);
++++++++}
++++++++
++++++++// (public) this mod a
++++++++function bnMod(a) {
++++++++ var r = nbi();
++++++++ this.abs().divRemTo(a,null,r);
++++++++ if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
++++++++ return r;
++++++++}
++++++++
++++++++// Modular reduction using "classic" algorithm
++++++++function Classic(m) { this.m = m; }
++++++++function cConvert(x) {
++++++++ if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
++++++++ else return x;
++++++++}
++++++++function cRevert(x) { return x; }
++++++++function cReduce(x) { x.divRemTo(this.m,null,x); }
++++++++function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
++++++++function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
++++++++
++++++++Classic.prototype.convert = cConvert;
++++++++Classic.prototype.revert = cRevert;
++++++++Classic.prototype.reduce = cReduce;
++++++++Classic.prototype.mulTo = cMulTo;
++++++++Classic.prototype.sqrTo = cSqrTo;
++++++++
++++++++// (protected) return "-1/this % 2^DB"; useful for Mont. reduction
++++++++// justification:
++++++++// xy == 1 (mod m)
++++++++// xy = 1+km
++++++++// xy(2-xy) = (1+km)(1-km)
++++++++// x[y(2-xy)] = 1-k^2m^2
++++++++// x[y(2-xy)] == 1 (mod m^2)
++++++++// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
++++++++// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
++++++++// JS multiply "overflows" differently from C/C++, so care is needed here.
++++++++function bnpInvDigit() {
++++++++ if(this.t < 1) return 0;
++++++++ var x = this.data[0];
++++++++ if((x&1) == 0) return 0;
++++++++ var y = x&3; // y == 1/x mod 2^2
++++++++ y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
++++++++ y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
++++++++ y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
++++++++ // last step - calculate inverse mod DV directly;
++++++++ // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
++++++++ y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
++++++++ // we really want the negative inverse, and -DV < y < DV
++++++++ return (y>0)?this.DV-y:-y;
++++++++}
++++++++
++++++++// Montgomery reduction
++++++++function Montgomery(m) {
++++++++ this.m = m;
++++++++ this.mp = m.invDigit();
++++++++ this.mpl = this.mp&0x7fff;
++++++++ this.mph = this.mp>>15;
++++++++ this.um = (1<<(m.DB-15))-1;
++++++++ this.mt2 = 2*m.t;
++++++++}
++++++++
++++++++// xR mod m
++++++++function montConvert(x) {
++++++++ var r = nbi();
++++++++ x.abs().dlShiftTo(this.m.t,r);
++++++++ r.divRemTo(this.m,null,r);
++++++++ if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
++++++++ return r;
++++++++}
++++++++
++++++++// x/R mod m
++++++++function montRevert(x) {
++++++++ var r = nbi();
++++++++ x.copyTo(r);
++++++++ this.reduce(r);
++++++++ return r;
++++++++}
++++++++
++++++++// x = x/R mod m (HAC 14.32)
++++++++function montReduce(x) {
++++++++ while(x.t <= this.mt2) // pad x so am has enough room later
++++++++ x.data[x.t++] = 0;
++++++++ for(var i = 0; i < this.m.t; ++i) {
++++++++ // faster way of calculating u0 = x.data[i]*mp mod DV
++++++++ var j = x.data[i]&0x7fff;
++++++++ var u0 = (j*this.mpl+(((j*this.mph+(x.data[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
++++++++ // use am to combine the multiply-shift-add into one call
++++++++ j = i+this.m.t;
++++++++ x.data[j] += this.m.am(0,u0,x,i,0,this.m.t);
++++++++ // propagate carry
++++++++ while(x.data[j] >= x.DV) { x.data[j] -= x.DV; x.data[++j]++; }
++++++++ }
++++++++ x.clamp();
++++++++ x.drShiftTo(this.m.t,x);
++++++++ if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
++++++++}
++++++++
++++++++// r = "x^2/R mod m"; x != r
++++++++function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
++++++++
++++++++// r = "xy/R mod m"; x,y != r
++++++++function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
++++++++
++++++++Montgomery.prototype.convert = montConvert;
++++++++Montgomery.prototype.revert = montRevert;
++++++++Montgomery.prototype.reduce = montReduce;
++++++++Montgomery.prototype.mulTo = montMulTo;
++++++++Montgomery.prototype.sqrTo = montSqrTo;
++++++++
++++++++// (protected) true iff this is even
++++++++function bnpIsEven() { return ((this.t>0)?(this.data[0]&1):this.s) == 0; }
++++++++
++++++++// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
++++++++function bnpExp(e,z) {
++++++++ if(e > 0xffffffff || e < 1) return BigInteger.ONE;
++++++++ var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
++++++++ g.copyTo(r);
++++++++ while(--i >= 0) {
++++++++ z.sqrTo(r,r2);
++++++++ if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
++++++++ else { var t = r; r = r2; r2 = t; }
++++++++ }
++++++++ return z.revert(r);
++++++++}
++++++++
++++++++// (public) this^e % m, 0 <= e < 2^32
++++++++function bnModPowInt(e,m) {
++++++++ var z;
++++++++ if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
++++++++ return this.exp(e,z);
++++++++}
++++++++
++++++++// protected
++++++++BigInteger.prototype.copyTo = bnpCopyTo;
++++++++BigInteger.prototype.fromInt = bnpFromInt;
++++++++BigInteger.prototype.fromString = bnpFromString;
++++++++BigInteger.prototype.clamp = bnpClamp;
++++++++BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
++++++++BigInteger.prototype.drShiftTo = bnpDRShiftTo;
++++++++BigInteger.prototype.lShiftTo = bnpLShiftTo;
++++++++BigInteger.prototype.rShiftTo = bnpRShiftTo;
++++++++BigInteger.prototype.subTo = bnpSubTo;
++++++++BigInteger.prototype.multiplyTo = bnpMultiplyTo;
++++++++BigInteger.prototype.squareTo = bnpSquareTo;
++++++++BigInteger.prototype.divRemTo = bnpDivRemTo;
++++++++BigInteger.prototype.invDigit = bnpInvDigit;
++++++++BigInteger.prototype.isEven = bnpIsEven;
++++++++BigInteger.prototype.exp = bnpExp;
++++++++
++++++++// public
++++++++BigInteger.prototype.toString = bnToString;
++++++++BigInteger.prototype.negate = bnNegate;
++++++++BigInteger.prototype.abs = bnAbs;
++++++++BigInteger.prototype.compareTo = bnCompareTo;
++++++++BigInteger.prototype.bitLength = bnBitLength;
++++++++BigInteger.prototype.mod = bnMod;
++++++++BigInteger.prototype.modPowInt = bnModPowInt;
++++++++
++++++++// "constants"
++++++++BigInteger.ZERO = nbv(0);
++++++++BigInteger.ONE = nbv(1);
++++++++
++++++++// jsbn2 lib
++++++++
++++++++//Copyright (c) 2005-2009 Tom Wu
++++++++//All Rights Reserved.
++++++++//See "LICENSE" for details (See jsbn.js for LICENSE).
++++++++
++++++++//Extended JavaScript BN functions, required for RSA private ops.
++++++++
++++++++//Version 1.1: new BigInteger("0", 10) returns "proper" zero
++++++++
++++++++//(public)
++++++++function bnClone() { var r = nbi(); this.copyTo(r); return r; }
++++++++
++++++++//(public) return value as integer
++++++++function bnIntValue() {
++++++++if(this.s < 0) {
++++++++ if(this.t == 1) return this.data[0]-this.DV;
++++++++ else if(this.t == 0) return -1;
++++++++} else if(this.t == 1) return this.data[0];
++++++++else if(this.t == 0) return 0;
++++++++// assumes 16 < DB < 32
++++++++return ((this.data[1]&((1<<(32-this.DB))-1))<<this.DB)|this.data[0];
++++++++}
++++++++
++++++++//(public) return value as byte
++++++++function bnByteValue() { return (this.t==0)?this.s:(this.data[0]<<24)>>24; }
++++++++
++++++++//(public) return value as short (assumes DB>=16)
++++++++function bnShortValue() { return (this.t==0)?this.s:(this.data[0]<<16)>>16; }
++++++++
++++++++//(protected) return x s.t. r^x < DV
++++++++function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
++++++++
++++++++//(public) 0 if this == 0, 1 if this > 0
++++++++function bnSigNum() {
++++++++if(this.s < 0) return -1;
++++++++else if(this.t <= 0 || (this.t == 1 && this.data[0] <= 0)) return 0;
++++++++else return 1;
++++++++}
++++++++
++++++++//(protected) convert to radix string
++++++++function bnpToRadix(b) {
++++++++if(b == null) b = 10;
++++++++if(this.signum() == 0 || b < 2 || b > 36) return "0";
++++++++var cs = this.chunkSize(b);
++++++++var a = Math.pow(b,cs);
++++++++var d = nbv(a), y = nbi(), z = nbi(), r = "";
++++++++this.divRemTo(d,y,z);
++++++++while(y.signum() > 0) {
++++++++ r = (a+z.intValue()).toString(b).substr(1) + r;
++++++++ y.divRemTo(d,y,z);
++++++++}
++++++++return z.intValue().toString(b) + r;
++++++++}
++++++++
++++++++//(protected) convert from radix string
++++++++function bnpFromRadix(s,b) {
++++++++this.fromInt(0);
++++++++if(b == null) b = 10;
++++++++var cs = this.chunkSize(b);
++++++++var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
++++++++for(var i = 0; i < s.length; ++i) {
++++++++ var x = intAt(s,i);
++++++++ if(x < 0) {
++++++++ if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
++++++++ continue;
++++++++ }
++++++++ w = b*w+x;
++++++++ if(++j >= cs) {
++++++++ this.dMultiply(d);
++++++++ this.dAddOffset(w,0);
++++++++ j = 0;
++++++++ w = 0;
++++++++ }
++++++++}
++++++++if(j > 0) {
++++++++ this.dMultiply(Math.pow(b,j));
++++++++ this.dAddOffset(w,0);
++++++++}
++++++++if(mi) BigInteger.ZERO.subTo(this,this);
++++++++}
++++++++
++++++++//(protected) alternate constructor
++++++++function bnpFromNumber(a,b,c) {
++++++++if("number" == typeof b) {
++++++++ // new BigInteger(int,int,RNG)
++++++++ if(a < 2) this.fromInt(1);
++++++++ else {
++++++++ this.fromNumber(a,c);
++++++++ if(!this.testBit(a-1)) // force MSB set
++++++++ this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
++++++++ if(this.isEven()) this.dAddOffset(1,0); // force odd
++++++++ while(!this.isProbablePrime(b)) {
++++++++ this.dAddOffset(2,0);
++++++++ if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
++++++++ }
++++++++ }
++++++++} else {
++++++++ // new BigInteger(int,RNG)
++++++++ var x = new Array(), t = a&7;
++++++++ x.length = (a>>3)+1;
++++++++ b.nextBytes(x);
++++++++ if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
++++++++ this.fromString(x,256);
++++++++}
++++++++}
++++++++
++++++++//(public) convert to bigendian byte array
++++++++function bnToByteArray() {
++++++++var i = this.t, r = new Array();
++++++++r[0] = this.s;
++++++++var p = this.DB-(i*this.DB)%8, d, k = 0;
++++++++if(i-- > 0) {
++++++++ if(p < this.DB && (d = this.data[i]>>p) != (this.s&this.DM)>>p)
++++++++ r[k++] = d|(this.s<<(this.DB-p));
++++++++ while(i >= 0) {
++++++++ if(p < 8) {
++++++++ d = (this.data[i]&((1<<p)-1))<<(8-p);
++++++++ d |= this.data[--i]>>(p+=this.DB-8);
++++++++ } else {
++++++++ d = (this.data[i]>>(p-=8))&0xff;
++++++++ if(p <= 0) { p += this.DB; --i; }
++++++++ }
++++++++ if((d&0x80) != 0) d |= -256;
++++++++ if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
++++++++ if(k > 0 || d != this.s) r[k++] = d;
++++++++ }
++++++++}
++++++++return r;
++++++++}
++++++++
++++++++function bnEquals(a) { return(this.compareTo(a)==0); }
++++++++function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
++++++++function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
++++++++
++++++++//(protected) r = this op a (bitwise)
++++++++function bnpBitwiseTo(a,op,r) {
++++++++var i, f, m = Math.min(a.t,this.t);
++++++++for(i = 0; i < m; ++i) r.data[i] = op(this.data[i],a.data[i]);
++++++++if(a.t < this.t) {
++++++++ f = a.s&this.DM;
++++++++ for(i = m; i < this.t; ++i) r.data[i] = op(this.data[i],f);
++++++++ r.t = this.t;
++++++++} else {
++++++++ f = this.s&this.DM;
++++++++ for(i = m; i < a.t; ++i) r.data[i] = op(f,a.data[i]);
++++++++ r.t = a.t;
++++++++}
++++++++r.s = op(this.s,a.s);
++++++++r.clamp();
++++++++}
++++++++
++++++++//(public) this & a
++++++++function op_and(x,y) { return x&y; }
++++++++function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
++++++++
++++++++//(public) this | a
++++++++function op_or(x,y) { return x|y; }
++++++++function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
++++++++
++++++++//(public) this ^ a
++++++++function op_xor(x,y) { return x^y; }
++++++++function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
++++++++
++++++++//(public) this & ~a
++++++++function op_andnot(x,y) { return x&~y; }
++++++++function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
++++++++
++++++++//(public) ~this
++++++++function bnNot() {
++++++++var r = nbi();
++++++++for(var i = 0; i < this.t; ++i) r.data[i] = this.DM&~this.data[i];
++++++++r.t = this.t;
++++++++r.s = ~this.s;
++++++++return r;
++++++++}
++++++++
++++++++//(public) this << n
++++++++function bnShiftLeft(n) {
++++++++var r = nbi();
++++++++if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
++++++++return r;
++++++++}
++++++++
++++++++//(public) this >> n
++++++++function bnShiftRight(n) {
++++++++var r = nbi();
++++++++if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
++++++++return r;
++++++++}
++++++++
++++++++//return index of lowest 1-bit in x, x < 2^31
++++++++function lbit(x) {
++++++++if(x == 0) return -1;
++++++++var r = 0;
++++++++if((x&0xffff) == 0) { x >>= 16; r += 16; }
++++++++if((x&0xff) == 0) { x >>= 8; r += 8; }
++++++++if((x&0xf) == 0) { x >>= 4; r += 4; }
++++++++if((x&3) == 0) { x >>= 2; r += 2; }
++++++++if((x&1) == 0) ++r;
++++++++return r;
++++++++}
++++++++
++++++++//(public) returns index of lowest 1-bit (or -1 if none)
++++++++function bnGetLowestSetBit() {
++++++++for(var i = 0; i < this.t; ++i)
++++++++ if(this.data[i] != 0) return i*this.DB+lbit(this.data[i]);
++++++++if(this.s < 0) return this.t*this.DB;
++++++++return -1;
++++++++}
++++++++
++++++++//return number of 1 bits in x
++++++++function cbit(x) {
++++++++var r = 0;
++++++++while(x != 0) { x &= x-1; ++r; }
++++++++return r;
++++++++}
++++++++
++++++++//(public) return number of set bits
++++++++function bnBitCount() {
++++++++var r = 0, x = this.s&this.DM;
++++++++for(var i = 0; i < this.t; ++i) r += cbit(this.data[i]^x);
++++++++return r;
++++++++}
++++++++
++++++++//(public) true iff nth bit is set
++++++++function bnTestBit(n) {
++++++++var j = Math.floor(n/this.DB);
++++++++if(j >= this.t) return(this.s!=0);
++++++++return((this.data[j]&(1<<(n%this.DB)))!=0);
++++++++}
++++++++
++++++++//(protected) this op (1<<n)
++++++++function bnpChangeBit(n,op) {
++++++++var r = BigInteger.ONE.shiftLeft(n);
++++++++this.bitwiseTo(r,op,r);
++++++++return r;
++++++++}
++++++++
++++++++//(public) this | (1<<n)
++++++++function bnSetBit(n) { return this.changeBit(n,op_or); }
++++++++
++++++++//(public) this & ~(1<<n)
++++++++function bnClearBit(n) { return this.changeBit(n,op_andnot); }
++++++++
++++++++//(public) this ^ (1<<n)
++++++++function bnFlipBit(n) { return this.changeBit(n,op_xor); }
++++++++
++++++++//(protected) r = this + a
++++++++function bnpAddTo(a,r) {
++++++++var i = 0, c = 0, m = Math.min(a.t,this.t);
++++++++while(i < m) {
++++++++ c += this.data[i]+a.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++}
++++++++if(a.t < this.t) {
++++++++ c += a.s;
++++++++ while(i < this.t) {
++++++++ c += this.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++ }
++++++++ c += this.s;
++++++++} else {
++++++++ c += this.s;
++++++++ while(i < a.t) {
++++++++ c += a.data[i];
++++++++ r.data[i++] = c&this.DM;
++++++++ c >>= this.DB;
++++++++ }
++++++++ c += a.s;
++++++++}
++++++++r.s = (c<0)?-1:0;
++++++++if(c > 0) r.data[i++] = c;
++++++++else if(c < -1) r.data[i++] = this.DV+c;
++++++++r.t = i;
++++++++r.clamp();
++++++++}
++++++++
++++++++//(public) this + a
++++++++function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
++++++++
++++++++//(public) this - a
++++++++function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
++++++++
++++++++//(public) this * a
++++++++function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
++++++++
++++++++//(public) this / a
++++++++function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
++++++++
++++++++//(public) this % a
++++++++function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
++++++++
++++++++//(public) [this/a,this%a]
++++++++function bnDivideAndRemainder(a) {
++++++++var q = nbi(), r = nbi();
++++++++this.divRemTo(a,q,r);
++++++++return new Array(q,r);
++++++++}
++++++++
++++++++//(protected) this *= n, this >= 0, 1 < n < DV
++++++++function bnpDMultiply(n) {
++++++++this.data[this.t] = this.am(0,n-1,this,0,0,this.t);
++++++++++this.t;
++++++++this.clamp();
++++++++}
++++++++
++++++++//(protected) this += n << w words, this >= 0
++++++++function bnpDAddOffset(n,w) {
++++++++if(n == 0) return;
++++++++while(this.t <= w) this.data[this.t++] = 0;
++++++++this.data[w] += n;
++++++++while(this.data[w] >= this.DV) {
++++++++ this.data[w] -= this.DV;
++++++++ if(++w >= this.t) this.data[this.t++] = 0;
++++++++ ++this.data[w];
++++++++}
++++++++}
++++++++
++++++++//A "null" reducer
++++++++function NullExp() {}
++++++++function nNop(x) { return x; }
++++++++function nMulTo(x,y,r) { x.multiplyTo(y,r); }
++++++++function nSqrTo(x,r) { x.squareTo(r); }
++++++++
++++++++NullExp.prototype.convert = nNop;
++++++++NullExp.prototype.revert = nNop;
++++++++NullExp.prototype.mulTo = nMulTo;
++++++++NullExp.prototype.sqrTo = nSqrTo;
++++++++
++++++++//(public) this^e
++++++++function bnPow(e) { return this.exp(e,new NullExp()); }
++++++++
++++++++//(protected) r = lower n words of "this * a", a.t <= n
++++++++//"this" should be the larger one if appropriate.
++++++++function bnpMultiplyLowerTo(a,n,r) {
++++++++var i = Math.min(this.t+a.t,n);
++++++++r.s = 0; // assumes a,this >= 0
++++++++r.t = i;
++++++++while(i > 0) r.data[--i] = 0;
++++++++var j;
++++++++for(j = r.t-this.t; i < j; ++i) r.data[i+this.t] = this.am(0,a.data[i],r,i,0,this.t);
++++++++for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.data[i],r,i,0,n-i);
++++++++r.clamp();
++++++++}
++++++++
++++++++//(protected) r = "this * a" without lower n words, n > 0
++++++++//"this" should be the larger one if appropriate.
++++++++function bnpMultiplyUpperTo(a,n,r) {
++++++++--n;
++++++++var i = r.t = this.t+a.t-n;
++++++++r.s = 0; // assumes a,this >= 0
++++++++while(--i >= 0) r.data[i] = 0;
++++++++for(i = Math.max(n-this.t,0); i < a.t; ++i)
++++++++ r.data[this.t+i-n] = this.am(n-i,a.data[i],r,0,0,this.t+i-n);
++++++++r.clamp();
++++++++r.drShiftTo(1,r);
++++++++}
++++++++
++++++++//Barrett modular reduction
++++++++function Barrett(m) {
++++++++// setup Barrett
++++++++this.r2 = nbi();
++++++++this.q3 = nbi();
++++++++BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
++++++++this.mu = this.r2.divide(m);
++++++++this.m = m;
++++++++}
++++++++
++++++++function barrettConvert(x) {
++++++++if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
++++++++else if(x.compareTo(this.m) < 0) return x;
++++++++else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
++++++++}
++++++++
++++++++function barrettRevert(x) { return x; }
++++++++
++++++++//x = x mod m (HAC 14.42)
++++++++function barrettReduce(x) {
++++++++x.drShiftTo(this.m.t-1,this.r2);
++++++++if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
++++++++this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
++++++++this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
++++++++while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
++++++++x.subTo(this.r2,x);
++++++++while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
++++++++}
++++++++
++++++++//r = x^2 mod m; x != r
++++++++function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
++++++++
++++++++//r = x*y mod m; x,y != r
++++++++function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
++++++++
++++++++Barrett.prototype.convert = barrettConvert;
++++++++Barrett.prototype.revert = barrettRevert;
++++++++Barrett.prototype.reduce = barrettReduce;
++++++++Barrett.prototype.mulTo = barrettMulTo;
++++++++Barrett.prototype.sqrTo = barrettSqrTo;
++++++++
++++++++//(public) this^e % m (HAC 14.85)
++++++++function bnModPow(e,m) {
++++++++var i = e.bitLength(), k, r = nbv(1), z;
++++++++if(i <= 0) return r;
++++++++else if(i < 18) k = 1;
++++++++else if(i < 48) k = 3;
++++++++else if(i < 144) k = 4;
++++++++else if(i < 768) k = 5;
++++++++else k = 6;
++++++++if(i < 8)
++++++++ z = new Classic(m);
++++++++else if(m.isEven())
++++++++ z = new Barrett(m);
++++++++else
++++++++ z = new Montgomery(m);
++++++++
++++++++// precomputation
++++++++var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
++++++++g[1] = z.convert(this);
++++++++if(k > 1) {
++++++++ var g2 = nbi();
++++++++ z.sqrTo(g[1],g2);
++++++++ while(n <= km) {
++++++++ g[n] = nbi();
++++++++ z.mulTo(g2,g[n-2],g[n]);
++++++++ n += 2;
++++++++ }
++++++++}
++++++++
++++++++var j = e.t-1, w, is1 = true, r2 = nbi(), t;
++++++++i = nbits(e.data[j])-1;
++++++++while(j >= 0) {
++++++++ if(i >= k1) w = (e.data[j]>>(i-k1))&km;
++++++++ else {
++++++++ w = (e.data[j]&((1<<(i+1))-1))<<(k1-i);
++++++++ if(j > 0) w |= e.data[j-1]>>(this.DB+i-k1);
++++++++ }
++++++++
++++++++ n = k;
++++++++ while((w&1) == 0) { w >>= 1; --n; }
++++++++ if((i -= n) < 0) { i += this.DB; --j; }
++++++++ if(is1) { // ret == 1, don't bother squaring or multiplying it
++++++++ g[w].copyTo(r);
++++++++ is1 = false;
++++++++ } else {
++++++++ while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
++++++++ if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
++++++++ z.mulTo(r2,g[w],r);
++++++++ }
++++++++
++++++++ while(j >= 0 && (e.data[j]&(1<<i)) == 0) {
++++++++ z.sqrTo(r,r2); t = r; r = r2; r2 = t;
++++++++ if(--i < 0) { i = this.DB-1; --j; }
++++++++ }
++++++++}
++++++++return z.revert(r);
++++++++}
++++++++
++++++++//(public) gcd(this,a) (HAC 14.54)
++++++++function bnGCD(a) {
++++++++var x = (this.s<0)?this.negate():this.clone();
++++++++var y = (a.s<0)?a.negate():a.clone();
++++++++if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
++++++++var i = x.getLowestSetBit(), g = y.getLowestSetBit();
++++++++if(g < 0) return x;
++++++++if(i < g) g = i;
++++++++if(g > 0) {
++++++++ x.rShiftTo(g,x);
++++++++ y.rShiftTo(g,y);
++++++++}
++++++++while(x.signum() > 0) {
++++++++ if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
++++++++ if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
++++++++ if(x.compareTo(y) >= 0) {
++++++++ x.subTo(y,x);
++++++++ x.rShiftTo(1,x);
++++++++ } else {
++++++++ y.subTo(x,y);
++++++++ y.rShiftTo(1,y);
++++++++ }
++++++++}
++++++++if(g > 0) y.lShiftTo(g,y);
++++++++return y;
++++++++}
++++++++
++++++++//(protected) this % n, n < 2^26
++++++++function bnpModInt(n) {
++++++++if(n <= 0) return 0;
++++++++var d = this.DV%n, r = (this.s<0)?n-1:0;
++++++++if(this.t > 0)
++++++++ if(d == 0) r = this.data[0]%n;
++++++++ else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.data[i])%n;
++++++++return r;
++++++++}
++++++++
++++++++//(public) 1/this % m (HAC 14.61)
++++++++function bnModInverse(m) {
++++++++var ac = m.isEven();
++++++++if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
++++++++var u = m.clone(), v = this.clone();
++++++++var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
++++++++while(u.signum() != 0) {
++++++++ while(u.isEven()) {
++++++++ u.rShiftTo(1,u);
++++++++ if(ac) {
++++++++ if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
++++++++ a.rShiftTo(1,a);
++++++++ } else if(!b.isEven()) b.subTo(m,b);
++++++++ b.rShiftTo(1,b);
++++++++ }
++++++++ while(v.isEven()) {
++++++++ v.rShiftTo(1,v);
++++++++ if(ac) {
++++++++ if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
++++++++ c.rShiftTo(1,c);
++++++++ } else if(!d.isEven()) d.subTo(m,d);
++++++++ d.rShiftTo(1,d);
++++++++ }
++++++++ if(u.compareTo(v) >= 0) {
++++++++ u.subTo(v,u);
++++++++ if(ac) a.subTo(c,a);
++++++++ b.subTo(d,b);
++++++++ } else {
++++++++ v.subTo(u,v);
++++++++ if(ac) c.subTo(a,c);
++++++++ d.subTo(b,d);
++++++++ }
++++++++}
++++++++if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
++++++++if(d.compareTo(m) >= 0) return d.subtract(m);
++++++++if(d.signum() < 0) d.addTo(m,d); else return d;
++++++++if(d.signum() < 0) return d.add(m); else return d;
++++++++}
++++++++
++++++++var lowprimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509];
++++++++var lplim = (1<<26)/lowprimes[lowprimes.length-1];
++++++++
++++++++//(public) test primality with certainty >= 1-.5^t
++++++++function bnIsProbablePrime(t) {
++++++++var i, x = this.abs();
++++++++if(x.t == 1 && x.data[0] <= lowprimes[lowprimes.length-1]) {
++++++++ for(i = 0; i < lowprimes.length; ++i)
++++++++ if(x.data[0] == lowprimes[i]) return true;
++++++++ return false;
++++++++}
++++++++if(x.isEven()) return false;
++++++++i = 1;
++++++++while(i < lowprimes.length) {
++++++++ var m = lowprimes[i], j = i+1;
++++++++ while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
++++++++ m = x.modInt(m);
++++++++ while(i < j) if(m%lowprimes[i++] == 0) return false;
++++++++}
++++++++return x.millerRabin(t);
++++++++}
++++++++
++++++++//(protected) true if probably prime (HAC 4.24, Miller-Rabin)
++++++++function bnpMillerRabin(t) {
++++++++var n1 = this.subtract(BigInteger.ONE);
++++++++var k = n1.getLowestSetBit();
++++++++if(k <= 0) return false;
++++++++var r = n1.shiftRight(k);
++++++++var prng = bnGetPrng();
++++++++var a;
++++++++for(var i = 0; i < t; ++i) {
++++++++ // select witness 'a' at random from between 1 and n1
++++++++ do {
++++++++ a = new BigInteger(this.bitLength(), prng);
++++++++ }
++++++++ while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
++++++++ var y = a.modPow(r,this);
++++++++ if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
++++++++ var j = 1;
++++++++ while(j++ < k && y.compareTo(n1) != 0) {
++++++++ y = y.modPowInt(2,this);
++++++++ if(y.compareTo(BigInteger.ONE) == 0) return false;
++++++++ }
++++++++ if(y.compareTo(n1) != 0) return false;
++++++++ }
++++++++}
++++++++return true;
++++++++}
++++++++
++++++++// get pseudo random number generator
++++++++function bnGetPrng() {
++++++++ // create prng with api that matches BigInteger secure random
++++++++ return {
++++++++ // x is an array to fill with bytes
++++++++ nextBytes: function(x) {
++++++++ for(var i = 0; i < x.length; ++i) {
++++++++ x[i] = Math.floor(Math.random() * 0x0100);
++++++++ }
++++++++ }
++++++++ };
++++++++}
++++++++
++++++++//protected
++++++++BigInteger.prototype.chunkSize = bnpChunkSize;
++++++++BigInteger.prototype.toRadix = bnpToRadix;
++++++++BigInteger.prototype.fromRadix = bnpFromRadix;
++++++++BigInteger.prototype.fromNumber = bnpFromNumber;
++++++++BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
++++++++BigInteger.prototype.changeBit = bnpChangeBit;
++++++++BigInteger.prototype.addTo = bnpAddTo;
++++++++BigInteger.prototype.dMultiply = bnpDMultiply;
++++++++BigInteger.prototype.dAddOffset = bnpDAddOffset;
++++++++BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
++++++++BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
++++++++BigInteger.prototype.modInt = bnpModInt;
++++++++BigInteger.prototype.millerRabin = bnpMillerRabin;
++++++++
++++++++//public
++++++++BigInteger.prototype.clone = bnClone;
++++++++BigInteger.prototype.intValue = bnIntValue;
++++++++BigInteger.prototype.byteValue = bnByteValue;
++++++++BigInteger.prototype.shortValue = bnShortValue;
++++++++BigInteger.prototype.signum = bnSigNum;
++++++++BigInteger.prototype.toByteArray = bnToByteArray;
++++++++BigInteger.prototype.equals = bnEquals;
++++++++BigInteger.prototype.min = bnMin;
++++++++BigInteger.prototype.max = bnMax;
++++++++BigInteger.prototype.and = bnAnd;
++++++++BigInteger.prototype.or = bnOr;
++++++++BigInteger.prototype.xor = bnXor;
++++++++BigInteger.prototype.andNot = bnAndNot;
++++++++BigInteger.prototype.not = bnNot;
++++++++BigInteger.prototype.shiftLeft = bnShiftLeft;
++++++++BigInteger.prototype.shiftRight = bnShiftRight;
++++++++BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
++++++++BigInteger.prototype.bitCount = bnBitCount;
++++++++BigInteger.prototype.testBit = bnTestBit;
++++++++BigInteger.prototype.setBit = bnSetBit;
++++++++BigInteger.prototype.clearBit = bnClearBit;
++++++++BigInteger.prototype.flipBit = bnFlipBit;
++++++++BigInteger.prototype.add = bnAdd;
++++++++BigInteger.prototype.subtract = bnSubtract;
++++++++BigInteger.prototype.multiply = bnMultiply;
++++++++BigInteger.prototype.divide = bnDivide;
++++++++BigInteger.prototype.remainder = bnRemainder;
++++++++BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
++++++++BigInteger.prototype.modPow = bnModPow;
++++++++BigInteger.prototype.modInverse = bnModInverse;
++++++++BigInteger.prototype.pow = bnPow;
++++++++BigInteger.prototype.gcd = bnGCD;
++++++++BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
++++++++
++++++++//BigInteger interfaces not implemented in jsbn:
++++++++
++++++++//BigInteger(int signum, byte[] magnitude)
++++++++//double doubleValue()
++++++++//float floatValue()
++++++++//int hashCode()
++++++++//long longValue()
++++++++//static BigInteger valueOf(long val)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of RSA-KEM.
++++++++ *
++++++++ * @author Lautaro Cozzani Rodriguez
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2014 Lautaro Cozzani <lautaro.cozzani@scytl.com>
++++++++ * Copyright (c) 2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++require('./random');
++++++++require('./jsbn');
++++++++
++++++++module.exports = forge.kem = forge.kem || {};
++++++++
++++++++var BigInteger = forge.jsbn.BigInteger;
++++++++
++++++++/**
++++++++ * The API for the RSA Key Encapsulation Mechanism (RSA-KEM) from ISO 18033-2.
++++++++ */
++++++++forge.kem.rsa = {};
++++++++
++++++++/**
++++++++ * Creates an RSA KEM API object for generating a secret asymmetric key.
++++++++ *
++++++++ * The symmetric key may be generated via a call to 'encrypt', which will
++++++++ * produce a ciphertext to be transmitted to the recipient and a key to be
++++++++ * kept secret. The ciphertext is a parameter to be passed to 'decrypt' which
++++++++ * will produce the same secret key for the recipient to use to decrypt a
++++++++ * message that was encrypted with the secret key.
++++++++ *
++++++++ * @param kdf the KDF API to use (eg: new forge.kem.kdf1()).
++++++++ * @param options the options to use.
++++++++ * [prng] a custom crypto-secure pseudo-random number generator to use,
++++++++ * that must define "getBytesSync".
++++++++ */
++++++++forge.kem.rsa.create = function(kdf, options) {
++++++++ options = options || {};
++++++++ var prng = options.prng || forge.random;
++++++++
++++++++ var kem = {};
++++++++
++++++++ /**
++++++++ * Generates a secret key and its encapsulation.
++++++++ *
++++++++ * @param publicKey the RSA public key to encrypt with.
++++++++ * @param keyLength the length, in bytes, of the secret key to generate.
++++++++ *
++++++++ * @return an object with:
++++++++ * encapsulation: the ciphertext for generating the secret key, as a
++++++++ * binary-encoded string of bytes.
++++++++ * key: the secret key to use for encrypting a message.
++++++++ */
++++++++ kem.encrypt = function(publicKey, keyLength) {
++++++++ // generate a random r where 1 < r < n
++++++++ var byteLength = Math.ceil(publicKey.n.bitLength() / 8);
++++++++ var r;
++++++++ do {
++++++++ r = new BigInteger(
++++++++ forge.util.bytesToHex(prng.getBytesSync(byteLength)),
++++++++ 16).mod(publicKey.n);
++++++++ } while(r.compareTo(BigInteger.ONE) <= 0);
++++++++
++++++++ // prepend r with zeros
++++++++ r = forge.util.hexToBytes(r.toString(16));
++++++++ var zeros = byteLength - r.length;
++++++++ if(zeros > 0) {
++++++++ r = forge.util.fillString(String.fromCharCode(0), zeros) + r;
++++++++ }
++++++++
++++++++ // encrypt the random
++++++++ var encapsulation = publicKey.encrypt(r, 'NONE');
++++++++
++++++++ // generate the secret key
++++++++ var key = kdf.generate(r, keyLength);
++++++++
++++++++ return {encapsulation: encapsulation, key: key};
++++++++ };
++++++++
++++++++ /**
++++++++ * Decrypts an encapsulated secret key.
++++++++ *
++++++++ * @param privateKey the RSA private key to decrypt with.
++++++++ * @param encapsulation the ciphertext for generating the secret key, as
++++++++ * a binary-encoded string of bytes.
++++++++ * @param keyLength the length, in bytes, of the secret key to generate.
++++++++ *
++++++++ * @return the secret key as a binary-encoded string of bytes.
++++++++ */
++++++++ kem.decrypt = function(privateKey, encapsulation, keyLength) {
++++++++ // decrypt the encapsulation and generate the secret key
++++++++ var r = privateKey.decrypt(encapsulation, 'NONE');
++++++++ return kdf.generate(r, keyLength);
++++++++ };
++++++++
++++++++ return kem;
++++++++};
++++++++
++++++++// TODO: add forge.kem.kdf.create('KDF1', {md: ..., ...}) API?
++++++++
++++++++/**
++++++++ * Creates a key derivation API object that implements KDF1 per ISO 18033-2.
++++++++ *
++++++++ * @param md the hash API to use.
++++++++ * @param [digestLength] an optional digest length that must be positive and
++++++++ * less than or equal to md.digestLength.
++++++++ *
++++++++ * @return a KDF1 API object.
++++++++ */
++++++++forge.kem.kdf1 = function(md, digestLength) {
++++++++ _createKDF(this, md, 0, digestLength || md.digestLength);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a key derivation API object that implements KDF2 per ISO 18033-2.
++++++++ *
++++++++ * @param md the hash API to use.
++++++++ * @param [digestLength] an optional digest length that must be positive and
++++++++ * less than or equal to md.digestLength.
++++++++ *
++++++++ * @return a KDF2 API object.
++++++++ */
++++++++forge.kem.kdf2 = function(md, digestLength) {
++++++++ _createKDF(this, md, 1, digestLength || md.digestLength);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a KDF1 or KDF2 API object.
++++++++ *
++++++++ * @param md the hash API to use.
++++++++ * @param counterStart the starting index for the counter.
++++++++ * @param digestLength the digest length to use.
++++++++ *
++++++++ * @return the KDF API object.
++++++++ */
++++++++function _createKDF(kdf, md, counterStart, digestLength) {
++++++++ /**
++++++++ * Generate a key of the specified length.
++++++++ *
++++++++ * @param x the binary-encoded byte string to generate a key from.
++++++++ * @param length the number of bytes to generate (the size of the key).
++++++++ *
++++++++ * @return the key as a binary-encoded string.
++++++++ */
++++++++ kdf.generate = function(x, length) {
++++++++ var key = new forge.util.ByteBuffer();
++++++++
++++++++ // run counter from counterStart to ceil(length / Hash.len)
++++++++ var k = Math.ceil(length / digestLength) + counterStart;
++++++++
++++++++ var c = new forge.util.ByteBuffer();
++++++++ for(var i = counterStart; i < k; ++i) {
++++++++ // I2OSP(i, 4): convert counter to an octet string of 4 octets
++++++++ c.putInt32(i);
++++++++
++++++++ // digest 'x' and the counter and add the result to the key
++++++++ md.start();
++++++++ md.update(x + c.getBytes());
++++++++ var hash = md.digest();
++++++++ key.putBytes(hash.getBytes(digestLength));
++++++++ }
++++++++
++++++++ // truncate to the correct key length
++++++++ key.truncate(key.length() - length);
++++++++ return key.getBytes();
++++++++ };
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Cross-browser support for logging in a web application.
++++++++ *
++++++++ * @author David I. Lehn <dlehn@digitalbazaar.com>
++++++++ *
++++++++ * Copyright (c) 2008-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++/* LOG API */
++++++++module.exports = forge.log = forge.log || {};
++++++++
++++++++/**
++++++++ * Application logging system.
++++++++ *
++++++++ * Each logger level available as it's own function of the form:
++++++++ * forge.log.level(category, args...)
++++++++ * The category is an arbitrary string, and the args are the same as
++++++++ * Firebug's console.log API. By default the call will be output as:
++++++++ * 'LEVEL [category] <args[0]>, args[1], ...'
++++++++ * This enables proper % formatting via the first argument.
++++++++ * Each category is enabled by default but can be enabled or disabled with
++++++++ * the setCategoryEnabled() function.
++++++++ */
++++++++// list of known levels
++++++++forge.log.levels = [
++++++++ 'none', 'error', 'warning', 'info', 'debug', 'verbose', 'max'];
++++++++// info on the levels indexed by name:
++++++++// index: level index
++++++++// name: uppercased display name
++++++++var sLevelInfo = {};
++++++++// list of loggers
++++++++var sLoggers = [];
++++++++/**
++++++++ * Standard console logger. If no console support is enabled this will
++++++++ * remain null. Check before using.
++++++++ */
++++++++var sConsoleLogger = null;
++++++++
++++++++// logger flags
++++++++/**
++++++++ * Lock the level at the current value. Used in cases where user config may
++++++++ * set the level such that only critical messages are seen but more verbose
++++++++ * messages are needed for debugging or other purposes.
++++++++ */
++++++++forge.log.LEVEL_LOCKED = (1 << 1);
++++++++/**
++++++++ * Always call log function. By default, the logging system will check the
++++++++ * message level against logger.level before calling the log function. This
++++++++ * flag allows the function to do its own check.
++++++++ */
++++++++forge.log.NO_LEVEL_CHECK = (1 << 2);
++++++++/**
++++++++ * Perform message interpolation with the passed arguments. "%" style
++++++++ * fields in log messages will be replaced by arguments as needed. Some
++++++++ * loggers, such as Firebug, may do this automatically. The original log
++++++++ * message will be available as 'message' and the interpolated version will
++++++++ * be available as 'fullMessage'.
++++++++ */
++++++++forge.log.INTERPOLATE = (1 << 3);
++++++++
++++++++// setup each log level
++++++++for(var i = 0; i < forge.log.levels.length; ++i) {
++++++++ var level = forge.log.levels[i];
++++++++ sLevelInfo[level] = {
++++++++ index: i,
++++++++ name: level.toUpperCase()
++++++++ };
++++++++}
++++++++
++++++++/**
++++++++ * Message logger. Will dispatch a message to registered loggers as needed.
++++++++ *
++++++++ * @param message message object
++++++++ */
++++++++forge.log.logMessage = function(message) {
++++++++ var messageLevelIndex = sLevelInfo[message.level].index;
++++++++ for(var i = 0; i < sLoggers.length; ++i) {
++++++++ var logger = sLoggers[i];
++++++++ if(logger.flags & forge.log.NO_LEVEL_CHECK) {
++++++++ logger.f(message);
++++++++ } else {
++++++++ // get logger level
++++++++ var loggerLevelIndex = sLevelInfo[logger.level].index;
++++++++ // check level
++++++++ if(messageLevelIndex <= loggerLevelIndex) {
++++++++ // message critical enough, call logger
++++++++ logger.f(logger, message);
++++++++ }
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Sets the 'standard' key on a message object to:
++++++++ * "LEVEL [category] " + message
++++++++ *
++++++++ * @param message a message log object
++++++++ */
++++++++forge.log.prepareStandard = function(message) {
++++++++ if(!('standard' in message)) {
++++++++ message.standard =
++++++++ sLevelInfo[message.level].name +
++++++++ //' ' + +message.timestamp +
++++++++ ' [' + message.category + '] ' +
++++++++ message.message;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Sets the 'full' key on a message object to the original message
++++++++ * interpolated via % formatting with the message arguments.
++++++++ *
++++++++ * @param message a message log object.
++++++++ */
++++++++forge.log.prepareFull = function(message) {
++++++++ if(!('full' in message)) {
++++++++ // copy args and insert message at the front
++++++++ var args = [message.message];
++++++++ args = args.concat([] || message['arguments']);
++++++++ // format the message
++++++++ message.full = forge.util.format.apply(this, args);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Applies both preparseStandard() and prepareFull() to a message object and
++++++++ * store result in 'standardFull'.
++++++++ *
++++++++ * @param message a message log object.
++++++++ */
++++++++forge.log.prepareStandardFull = function(message) {
++++++++ if(!('standardFull' in message)) {
++++++++ // FIXME implement 'standardFull' logging
++++++++ forge.log.prepareStandard(message);
++++++++ message.standardFull = message.standard;
++++++++ }
++++++++};
++++++++
++++++++// create log level functions
++++++++if(true) {
++++++++ // levels for which we want functions
++++++++ var levels = ['error', 'warning', 'info', 'debug', 'verbose'];
++++++++ for(var i = 0; i < levels.length; ++i) {
++++++++ // wrap in a function to ensure proper level var is passed
++++++++ (function(level) {
++++++++ // create function for this level
++++++++ forge.log[level] = function(category, message/*, args...*/) {
++++++++ // convert arguments to real array, remove category and message
++++++++ var args = Array.prototype.slice.call(arguments).slice(2);
++++++++ // create message object
++++++++ // Note: interpolation and standard formatting is done lazily
++++++++ var msg = {
++++++++ timestamp: new Date(),
++++++++ level: level,
++++++++ category: category,
++++++++ message: message,
++++++++ 'arguments': args
++++++++ /*standard*/
++++++++ /*full*/
++++++++ /*fullMessage*/
++++++++ };
++++++++ // process this message
++++++++ forge.log.logMessage(msg);
++++++++ };
++++++++ })(levels[i]);
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Creates a new logger with specified custom logging function.
++++++++ *
++++++++ * The logging function has a signature of:
++++++++ * function(logger, message)
++++++++ * logger: current logger
++++++++ * message: object:
++++++++ * level: level id
++++++++ * category: category
++++++++ * message: string message
++++++++ * arguments: Array of extra arguments
++++++++ * fullMessage: interpolated message and arguments if INTERPOLATE flag set
++++++++ *
++++++++ * @param logFunction a logging function which takes a log message object
++++++++ * as a parameter.
++++++++ *
++++++++ * @return a logger object.
++++++++ */
++++++++forge.log.makeLogger = function(logFunction) {
++++++++ var logger = {
++++++++ flags: 0,
++++++++ f: logFunction
++++++++ };
++++++++ forge.log.setLevel(logger, 'none');
++++++++ return logger;
++++++++};
++++++++
++++++++/**
++++++++ * Sets the current log level on a logger.
++++++++ *
++++++++ * @param logger the target logger.
++++++++ * @param level the new maximum log level as a string.
++++++++ *
++++++++ * @return true if set, false if not.
++++++++ */
++++++++forge.log.setLevel = function(logger, level) {
++++++++ var rval = false;
++++++++ if(logger && !(logger.flags & forge.log.LEVEL_LOCKED)) {
++++++++ for(var i = 0; i < forge.log.levels.length; ++i) {
++++++++ var aValidLevel = forge.log.levels[i];
++++++++ if(level == aValidLevel) {
++++++++ // set level
++++++++ logger.level = level;
++++++++ rval = true;
++++++++ break;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Locks the log level at its current value.
++++++++ *
++++++++ * @param logger the target logger.
++++++++ * @param lock boolean lock value, default to true.
++++++++ */
++++++++forge.log.lock = function(logger, lock) {
++++++++ if(typeof lock === 'undefined' || lock) {
++++++++ logger.flags |= forge.log.LEVEL_LOCKED;
++++++++ } else {
++++++++ logger.flags &= ~forge.log.LEVEL_LOCKED;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Adds a logger.
++++++++ *
++++++++ * @param logger the logger object.
++++++++ */
++++++++forge.log.addLogger = function(logger) {
++++++++ sLoggers.push(logger);
++++++++};
++++++++
++++++++// setup the console logger if possible, else create fake console.log
++++++++if(typeof(console) !== 'undefined' && 'log' in console) {
++++++++ var logger;
++++++++ if(console.error && console.warn && console.info && console.debug) {
++++++++ // looks like Firebug-style logging is available
++++++++ // level handlers map
++++++++ var levelHandlers = {
++++++++ error: console.error,
++++++++ warning: console.warn,
++++++++ info: console.info,
++++++++ debug: console.debug,
++++++++ verbose: console.debug
++++++++ };
++++++++ var f = function(logger, message) {
++++++++ forge.log.prepareStandard(message);
++++++++ var handler = levelHandlers[message.level];
++++++++ // prepend standard message and concat args
++++++++ var args = [message.standard];
++++++++ args = args.concat(message['arguments'].slice());
++++++++ // apply to low-level console function
++++++++ handler.apply(console, args);
++++++++ };
++++++++ logger = forge.log.makeLogger(f);
++++++++ } else {
++++++++ // only appear to have basic console.log
++++++++ var f = function(logger, message) {
++++++++ forge.log.prepareStandardFull(message);
++++++++ console.log(message.standardFull);
++++++++ };
++++++++ logger = forge.log.makeLogger(f);
++++++++ }
++++++++ forge.log.setLevel(logger, 'debug');
++++++++ forge.log.addLogger(logger);
++++++++ sConsoleLogger = logger;
++++++++} else {
++++++++ // define fake console.log to avoid potential script errors on
++++++++ // browsers that do not have console logging
++++++++ console = {
++++++++ log: function() {}
++++++++ };
++++++++}
++++++++
++++++++/*
++++++++ * Check for logging control query vars in current URL.
++++++++ *
++++++++ * console.level=<level-name>
++++++++ * Set's the console log level by name. Useful to override defaults and
++++++++ * allow more verbose logging before a user config is loaded.
++++++++ *
++++++++ * console.lock=<true|false>
++++++++ * Lock the console log level at whatever level it is set at. This is run
++++++++ * after console.level is processed. Useful to force a level of verbosity
++++++++ * that could otherwise be limited by a user config.
++++++++ */
++++++++if(sConsoleLogger !== null &&
++++++++ typeof window !== 'undefined' && window.location
++++++++) {
++++++++ var query = new URL(window.location.href).searchParams;
++++++++ if(query.has('console.level')) {
++++++++ // set with last value
++++++++ forge.log.setLevel(
++++++++ sConsoleLogger, query.get('console.level').slice(-1)[0]);
++++++++ }
++++++++ if(query.has('console.lock')) {
++++++++ // set with last value
++++++++ var lock = query.get('console.lock').slice(-1)[0];
++++++++ if(lock == 'true') {
++++++++ forge.log.lock(sConsoleLogger);
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++// provide public access to console logger
++++++++forge.log.consoleLogger = sConsoleLogger;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for all known Forge message digests.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright 2011-2017 Digital Bazaar, Inc.
++++++++ */
++++++++module.exports = require('./md');
++++++++
++++++++require('./md5');
++++++++require('./sha1');
++++++++require('./sha256');
++++++++require('./sha512');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for Forge message digests.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright 2011-2017 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++
++++++++module.exports = forge.md = forge.md || {};
++++++++forge.md.algorithms = forge.md.algorithms || {};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Message Digest Algorithm 5 with 128-bit digest (MD5) implementation.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++var md5 = module.exports = forge.md5 = forge.md5 || {};
++++++++forge.md.md5 = forge.md.algorithms.md5 = md5;
++++++++
++++++++/**
++++++++ * Creates an MD5 message digest object.
++++++++ *
++++++++ * @return a message digest object.
++++++++ */
++++++++md5.create = function() {
++++++++ // do initialization as necessary
++++++++ if(!_initialized) {
++++++++ _init();
++++++++ }
++++++++
++++++++ // MD5 state contains four 32-bit integers
++++++++ var _state = null;
++++++++
++++++++ // input buffer
++++++++ var _input = forge.util.createBuffer();
++++++++
++++++++ // used for word storage
++++++++ var _w = new Array(16);
++++++++
++++++++ // message digest object
++++++++ var md = {
++++++++ algorithm: 'md5',
++++++++ blockLength: 64,
++++++++ digestLength: 16,
++++++++ // 56-bit length of message so far (does not including padding)
++++++++ messageLength: 0,
++++++++ // true message length
++++++++ fullMessageLength: null,
++++++++ // size of message length in bytes
++++++++ messageLengthSize: 8
++++++++ };
++++++++
++++++++ /**
++++++++ * Starts the digest.
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.start = function() {
++++++++ // up to 56-bit message length for convenience
++++++++ md.messageLength = 0;
++++++++
++++++++ // full message length (set md.messageLength64 for backwards-compatibility)
++++++++ md.fullMessageLength = md.messageLength64 = [];
++++++++ var int32s = md.messageLengthSize / 4;
++++++++ for(var i = 0; i < int32s; ++i) {
++++++++ md.fullMessageLength.push(0);
++++++++ }
++++++++ _input = forge.util.createBuffer();
++++++++ _state = {
++++++++ h0: 0x67452301,
++++++++ h1: 0xEFCDAB89,
++++++++ h2: 0x98BADCFE,
++++++++ h3: 0x10325476
++++++++ };
++++++++ return md;
++++++++ };
++++++++ // start digest automatically for first time
++++++++ md.start();
++++++++
++++++++ /**
++++++++ * Updates the digest with the given message input. The given input can
++++++++ * treated as raw input (no encoding will be applied) or an encoding of
++++++++ * 'utf8' maybe given to encode the input using UTF-8.
++++++++ *
++++++++ * @param msg the message input to update with.
++++++++ * @param encoding the encoding to use (default: 'raw', other: 'utf8').
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.update = function(msg, encoding) {
++++++++ if(encoding === 'utf8') {
++++++++ msg = forge.util.encodeUtf8(msg);
++++++++ }
++++++++
++++++++ // update message length
++++++++ var len = msg.length;
++++++++ md.messageLength += len;
++++++++ len = [(len / 0x100000000) >>> 0, len >>> 0];
++++++++ for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
++++++++ md.fullMessageLength[i] += len[1];
++++++++ len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0);
++++++++ md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0;
++++++++ len[0] = (len[1] / 0x100000000) >>> 0;
++++++++ }
++++++++
++++++++ // add bytes to input buffer
++++++++ _input.putBytes(msg);
++++++++
++++++++ // process bytes
++++++++ _update(_state, _w, _input);
++++++++
++++++++ // compact input buffer every 2K or if empty
++++++++ if(_input.read > 2048 || _input.length() === 0) {
++++++++ _input.compact();
++++++++ }
++++++++
++++++++ return md;
++++++++ };
++++++++
++++++++ /**
++++++++ * Produces the digest.
++++++++ *
++++++++ * @return a byte buffer containing the digest value.
++++++++ */
++++++++ md.digest = function() {
++++++++ /* Note: Here we copy the remaining bytes in the input buffer and
++++++++ add the appropriate MD5 padding. Then we do the final update
++++++++ on a copy of the state so that if the user wants to get
++++++++ intermediate digests they can do so. */
++++++++
++++++++ /* Determine the number of bytes that must be added to the message
++++++++ to ensure its length is congruent to 448 mod 512. In other words,
++++++++ the data to be digested must be a multiple of 512 bits (or 128 bytes).
++++++++ This data includes the message, some padding, and the length of the
++++++++ message. Since the length of the message will be encoded as 8 bytes (64
++++++++ bits), that means that the last segment of the data must have 56 bytes
++++++++ (448 bits) of message and padding. Therefore, the length of the message
++++++++ plus the padding must be congruent to 448 mod 512 because
++++++++ 512 - 128 = 448.
++++++++
++++++++ In order to fill up the message length it must be filled with
++++++++ padding that begins with 1 bit followed by all 0 bits. Padding
++++++++ must *always* be present, so if the message length is already
++++++++ congruent to 448 mod 512, then 512 padding bits must be added. */
++++++++
++++++++ var finalBlock = forge.util.createBuffer();
++++++++ finalBlock.putBytes(_input.bytes());
++++++++
++++++++ // compute remaining size to be digested (include message length size)
++++++++ var remaining = (
++++++++ md.fullMessageLength[md.fullMessageLength.length - 1] +
++++++++ md.messageLengthSize);
++++++++
++++++++ // add padding for overflow blockSize - overflow
++++++++ // _padding starts with 1 byte with first bit is set (byte value 128), then
++++++++ // there may be up to (blockSize - 1) other pad bytes
++++++++ var overflow = remaining & (md.blockLength - 1);
++++++++ finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow));
++++++++
++++++++ // serialize message length in bits in little-endian order; since length
++++++++ // is stored in bytes we multiply by 8 and add carry
++++++++ var bits, carry = 0;
++++++++ for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
++++++++ bits = md.fullMessageLength[i] * 8 + carry;
++++++++ carry = (bits / 0x100000000) >>> 0;
++++++++ finalBlock.putInt32Le(bits >>> 0);
++++++++ }
++++++++
++++++++ var s2 = {
++++++++ h0: _state.h0,
++++++++ h1: _state.h1,
++++++++ h2: _state.h2,
++++++++ h3: _state.h3
++++++++ };
++++++++ _update(s2, _w, finalBlock);
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putInt32Le(s2.h0);
++++++++ rval.putInt32Le(s2.h1);
++++++++ rval.putInt32Le(s2.h2);
++++++++ rval.putInt32Le(s2.h3);
++++++++ return rval;
++++++++ };
++++++++
++++++++ return md;
++++++++};
++++++++
++++++++// padding, constant tables for calculating md5
++++++++var _padding = null;
++++++++var _g = null;
++++++++var _r = null;
++++++++var _k = null;
++++++++var _initialized = false;
++++++++
++++++++/**
++++++++ * Initializes the constant tables.
++++++++ */
++++++++function _init() {
++++++++ // create padding
++++++++ _padding = String.fromCharCode(128);
++++++++ _padding += forge.util.fillString(String.fromCharCode(0x00), 64);
++++++++
++++++++ // g values
++++++++ _g = [
++++++++ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
++++++++ 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
++++++++ 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
++++++++ 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9];
++++++++
++++++++ // rounds table
++++++++ _r = [
++++++++ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
++++++++ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
++++++++ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
++++++++ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21];
++++++++
++++++++ // get the result of abs(sin(i + 1)) as a 32-bit integer
++++++++ _k = new Array(64);
++++++++ for(var i = 0; i < 64; ++i) {
++++++++ _k[i] = Math.floor(Math.abs(Math.sin(i + 1)) * 0x100000000);
++++++++ }
++++++++
++++++++ // now initialized
++++++++ _initialized = true;
++++++++}
++++++++
++++++++/**
++++++++ * Updates an MD5 state with the given byte buffer.
++++++++ *
++++++++ * @param s the MD5 state to update.
++++++++ * @param w the array to use to store words.
++++++++ * @param bytes the byte buffer to update with.
++++++++ */
++++++++function _update(s, w, bytes) {
++++++++ // consume 512 bit (64 byte) chunks
++++++++ var t, a, b, c, d, f, r, i;
++++++++ var len = bytes.length();
++++++++ while(len >= 64) {
++++++++ // initialize hash value for this chunk
++++++++ a = s.h0;
++++++++ b = s.h1;
++++++++ c = s.h2;
++++++++ d = s.h3;
++++++++
++++++++ // round 1
++++++++ for(i = 0; i < 16; ++i) {
++++++++ w[i] = bytes.getInt32Le();
++++++++ f = d ^ (b & (c ^ d));
++++++++ t = (a + f + _k[i] + w[i]);
++++++++ r = _r[i];
++++++++ a = d;
++++++++ d = c;
++++++++ c = b;
++++++++ b += (t << r) | (t >>> (32 - r));
++++++++ }
++++++++ // round 2
++++++++ for(; i < 32; ++i) {
++++++++ f = c ^ (d & (b ^ c));
++++++++ t = (a + f + _k[i] + w[_g[i]]);
++++++++ r = _r[i];
++++++++ a = d;
++++++++ d = c;
++++++++ c = b;
++++++++ b += (t << r) | (t >>> (32 - r));
++++++++ }
++++++++ // round 3
++++++++ for(; i < 48; ++i) {
++++++++ f = b ^ c ^ d;
++++++++ t = (a + f + _k[i] + w[_g[i]]);
++++++++ r = _r[i];
++++++++ a = d;
++++++++ d = c;
++++++++ c = b;
++++++++ b += (t << r) | (t >>> (32 - r));
++++++++ }
++++++++ // round 4
++++++++ for(; i < 64; ++i) {
++++++++ f = c ^ (b | ~d);
++++++++ t = (a + f + _k[i] + w[_g[i]]);
++++++++ r = _r[i];
++++++++ a = d;
++++++++ d = c;
++++++++ c = b;
++++++++ b += (t << r) | (t >>> (32 - r));
++++++++ }
++++++++
++++++++ // update hash state
++++++++ s.h0 = (s.h0 + a) | 0;
++++++++ s.h1 = (s.h1 + b) | 0;
++++++++ s.h2 = (s.h2 + c) | 0;
++++++++ s.h3 = (s.h3 + d) | 0;
++++++++
++++++++ len -= 64;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Node.js module for Forge mask generation functions.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ *
++++++++ * Copyright 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./mgf1');
++++++++
++++++++module.exports = forge.mgf = forge.mgf || {};
++++++++forge.mgf.mgf1 = forge.mgf1;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of mask generation function MGF1.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ * Copyright (c) 2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++forge.mgf = forge.mgf || {};
++++++++var mgf1 = module.exports = forge.mgf.mgf1 = forge.mgf1 = forge.mgf1 || {};
++++++++
++++++++/**
++++++++ * Creates a MGF1 mask generation function object.
++++++++ *
++++++++ * @param md the message digest API to use (eg: forge.md.sha1.create()).
++++++++ *
++++++++ * @return a mask generation function object.
++++++++ */
++++++++mgf1.create = function(md) {
++++++++ var mgf = {
++++++++ /**
++++++++ * Generate mask of specified length.
++++++++ *
++++++++ * @param {String} seed The seed for mask generation.
++++++++ * @param maskLen Number of bytes to generate.
++++++++ * @return {String} The generated mask.
++++++++ */
++++++++ generate: function(seed, maskLen) {
++++++++ /* 2. Let T be the empty octet string. */
++++++++ var t = new forge.util.ByteBuffer();
++++++++
++++++++ /* 3. For counter from 0 to ceil(maskLen / hLen), do the following: */
++++++++ var len = Math.ceil(maskLen / md.digestLength);
++++++++ for(var i = 0; i < len; i++) {
++++++++ /* a. Convert counter to an octet string C of length 4 octets */
++++++++ var c = new forge.util.ByteBuffer();
++++++++ c.putInt32(i);
++++++++
++++++++ /* b. Concatenate the hash of the seed mgfSeed and C to the octet
++++++++ * string T: */
++++++++ md.start();
++++++++ md.update(seed + c.getBytes());
++++++++ t.putBuffer(md.digest());
++++++++ }
++++++++
++++++++ /* Output the leading maskLen octets of T as the octet string mask. */
++++++++ t.truncate(t.length() - maskLen);
++++++++ return t.getBytes();
++++++++ }
++++++++ };
++++++++
++++++++ return mgf;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Object IDs for ASN.1.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++
++++++++forge.pki = forge.pki || {};
++++++++var oids = module.exports = forge.pki.oids = forge.oids = forge.oids || {};
++++++++
++++++++// set id to name mapping and name to id mapping
++++++++function _IN(id, name) {
++++++++ oids[id] = name;
++++++++ oids[name] = id;
++++++++}
++++++++// set id to name mapping only
++++++++function _I_(id, name) {
++++++++ oids[id] = name;
++++++++}
++++++++
++++++++// algorithm OIDs
++++++++_IN('1.2.840.113549.1.1.1', 'rsaEncryption');
++++++++// Note: md2 & md4 not implemented
++++++++//_IN('1.2.840.113549.1.1.2', 'md2WithRSAEncryption');
++++++++//_IN('1.2.840.113549.1.1.3', 'md4WithRSAEncryption');
++++++++_IN('1.2.840.113549.1.1.4', 'md5WithRSAEncryption');
++++++++_IN('1.2.840.113549.1.1.5', 'sha1WithRSAEncryption');
++++++++_IN('1.2.840.113549.1.1.7', 'RSAES-OAEP');
++++++++_IN('1.2.840.113549.1.1.8', 'mgf1');
++++++++_IN('1.2.840.113549.1.1.9', 'pSpecified');
++++++++_IN('1.2.840.113549.1.1.10', 'RSASSA-PSS');
++++++++_IN('1.2.840.113549.1.1.11', 'sha256WithRSAEncryption');
++++++++_IN('1.2.840.113549.1.1.12', 'sha384WithRSAEncryption');
++++++++_IN('1.2.840.113549.1.1.13', 'sha512WithRSAEncryption');
++++++++// Edwards-curve Digital Signature Algorithm (EdDSA) Ed25519
++++++++_IN('1.3.101.112', 'EdDSA25519');
++++++++
++++++++_IN('1.2.840.10040.4.3', 'dsa-with-sha1');
++++++++
++++++++_IN('1.3.14.3.2.7', 'desCBC');
++++++++
++++++++_IN('1.3.14.3.2.26', 'sha1');
++++++++// Deprecated equivalent of sha1WithRSAEncryption
++++++++_IN('1.3.14.3.2.29', 'sha1WithRSASignature');
++++++++_IN('2.16.840.1.101.3.4.2.1', 'sha256');
++++++++_IN('2.16.840.1.101.3.4.2.2', 'sha384');
++++++++_IN('2.16.840.1.101.3.4.2.3', 'sha512');
++++++++_IN('2.16.840.1.101.3.4.2.4', 'sha224');
++++++++_IN('2.16.840.1.101.3.4.2.5', 'sha512-224');
++++++++_IN('2.16.840.1.101.3.4.2.6', 'sha512-256');
++++++++_IN('1.2.840.113549.2.2', 'md2');
++++++++_IN('1.2.840.113549.2.5', 'md5');
++++++++
++++++++// pkcs#7 content types
++++++++_IN('1.2.840.113549.1.7.1', 'data');
++++++++_IN('1.2.840.113549.1.7.2', 'signedData');
++++++++_IN('1.2.840.113549.1.7.3', 'envelopedData');
++++++++_IN('1.2.840.113549.1.7.4', 'signedAndEnvelopedData');
++++++++_IN('1.2.840.113549.1.7.5', 'digestedData');
++++++++_IN('1.2.840.113549.1.7.6', 'encryptedData');
++++++++
++++++++// pkcs#9 oids
++++++++_IN('1.2.840.113549.1.9.1', 'emailAddress');
++++++++_IN('1.2.840.113549.1.9.2', 'unstructuredName');
++++++++_IN('1.2.840.113549.1.9.3', 'contentType');
++++++++_IN('1.2.840.113549.1.9.4', 'messageDigest');
++++++++_IN('1.2.840.113549.1.9.5', 'signingTime');
++++++++_IN('1.2.840.113549.1.9.6', 'counterSignature');
++++++++_IN('1.2.840.113549.1.9.7', 'challengePassword');
++++++++_IN('1.2.840.113549.1.9.8', 'unstructuredAddress');
++++++++_IN('1.2.840.113549.1.9.14', 'extensionRequest');
++++++++
++++++++_IN('1.2.840.113549.1.9.20', 'friendlyName');
++++++++_IN('1.2.840.113549.1.9.21', 'localKeyId');
++++++++_IN('1.2.840.113549.1.9.22.1', 'x509Certificate');
++++++++
++++++++// pkcs#12 safe bags
++++++++_IN('1.2.840.113549.1.12.10.1.1', 'keyBag');
++++++++_IN('1.2.840.113549.1.12.10.1.2', 'pkcs8ShroudedKeyBag');
++++++++_IN('1.2.840.113549.1.12.10.1.3', 'certBag');
++++++++_IN('1.2.840.113549.1.12.10.1.4', 'crlBag');
++++++++_IN('1.2.840.113549.1.12.10.1.5', 'secretBag');
++++++++_IN('1.2.840.113549.1.12.10.1.6', 'safeContentsBag');
++++++++
++++++++// password-based-encryption for pkcs#12
++++++++_IN('1.2.840.113549.1.5.13', 'pkcs5PBES2');
++++++++_IN('1.2.840.113549.1.5.12', 'pkcs5PBKDF2');
++++++++
++++++++_IN('1.2.840.113549.1.12.1.1', 'pbeWithSHAAnd128BitRC4');
++++++++_IN('1.2.840.113549.1.12.1.2', 'pbeWithSHAAnd40BitRC4');
++++++++_IN('1.2.840.113549.1.12.1.3', 'pbeWithSHAAnd3-KeyTripleDES-CBC');
++++++++_IN('1.2.840.113549.1.12.1.4', 'pbeWithSHAAnd2-KeyTripleDES-CBC');
++++++++_IN('1.2.840.113549.1.12.1.5', 'pbeWithSHAAnd128BitRC2-CBC');
++++++++_IN('1.2.840.113549.1.12.1.6', 'pbewithSHAAnd40BitRC2-CBC');
++++++++
++++++++// hmac OIDs
++++++++_IN('1.2.840.113549.2.7', 'hmacWithSHA1');
++++++++_IN('1.2.840.113549.2.8', 'hmacWithSHA224');
++++++++_IN('1.2.840.113549.2.9', 'hmacWithSHA256');
++++++++_IN('1.2.840.113549.2.10', 'hmacWithSHA384');
++++++++_IN('1.2.840.113549.2.11', 'hmacWithSHA512');
++++++++
++++++++// symmetric key algorithm oids
++++++++_IN('1.2.840.113549.3.7', 'des-EDE3-CBC');
++++++++_IN('2.16.840.1.101.3.4.1.2', 'aes128-CBC');
++++++++_IN('2.16.840.1.101.3.4.1.22', 'aes192-CBC');
++++++++_IN('2.16.840.1.101.3.4.1.42', 'aes256-CBC');
++++++++
++++++++// certificate issuer/subject OIDs
++++++++_IN('2.5.4.3', 'commonName');
++++++++_IN('2.5.4.4', 'surname');
++++++++_IN('2.5.4.5', 'serialNumber');
++++++++_IN('2.5.4.6', 'countryName');
++++++++_IN('2.5.4.7', 'localityName');
++++++++_IN('2.5.4.8', 'stateOrProvinceName');
++++++++_IN('2.5.4.9', 'streetAddress');
++++++++_IN('2.5.4.10', 'organizationName');
++++++++_IN('2.5.4.11', 'organizationalUnitName');
++++++++_IN('2.5.4.12', 'title');
++++++++_IN('2.5.4.13', 'description');
++++++++_IN('2.5.4.15', 'businessCategory');
++++++++_IN('2.5.4.17', 'postalCode');
++++++++_IN('2.5.4.42', 'givenName');
++++++++_IN('1.3.6.1.4.1.311.60.2.1.2', 'jurisdictionOfIncorporationStateOrProvinceName');
++++++++_IN('1.3.6.1.4.1.311.60.2.1.3', 'jurisdictionOfIncorporationCountryName');
++++++++
++++++++// X.509 extension OIDs
++++++++_IN('2.16.840.1.113730.1.1', 'nsCertType');
++++++++_IN('2.16.840.1.113730.1.13', 'nsComment'); // deprecated in theory; still widely used
++++++++_I_('2.5.29.1', 'authorityKeyIdentifier'); // deprecated, use .35
++++++++_I_('2.5.29.2', 'keyAttributes'); // obsolete use .37 or .15
++++++++_I_('2.5.29.3', 'certificatePolicies'); // deprecated, use .32
++++++++_I_('2.5.29.4', 'keyUsageRestriction'); // obsolete use .37 or .15
++++++++_I_('2.5.29.5', 'policyMapping'); // deprecated use .33
++++++++_I_('2.5.29.6', 'subtreesConstraint'); // obsolete use .30
++++++++_I_('2.5.29.7', 'subjectAltName'); // deprecated use .17
++++++++_I_('2.5.29.8', 'issuerAltName'); // deprecated use .18
++++++++_I_('2.5.29.9', 'subjectDirectoryAttributes');
++++++++_I_('2.5.29.10', 'basicConstraints'); // deprecated use .19
++++++++_I_('2.5.29.11', 'nameConstraints'); // deprecated use .30
++++++++_I_('2.5.29.12', 'policyConstraints'); // deprecated use .36
++++++++_I_('2.5.29.13', 'basicConstraints'); // deprecated use .19
++++++++_IN('2.5.29.14', 'subjectKeyIdentifier');
++++++++_IN('2.5.29.15', 'keyUsage');
++++++++_I_('2.5.29.16', 'privateKeyUsagePeriod');
++++++++_IN('2.5.29.17', 'subjectAltName');
++++++++_IN('2.5.29.18', 'issuerAltName');
++++++++_IN('2.5.29.19', 'basicConstraints');
++++++++_I_('2.5.29.20', 'cRLNumber');
++++++++_I_('2.5.29.21', 'cRLReason');
++++++++_I_('2.5.29.22', 'expirationDate');
++++++++_I_('2.5.29.23', 'instructionCode');
++++++++_I_('2.5.29.24', 'invalidityDate');
++++++++_I_('2.5.29.25', 'cRLDistributionPoints'); // deprecated use .31
++++++++_I_('2.5.29.26', 'issuingDistributionPoint'); // deprecated use .28
++++++++_I_('2.5.29.27', 'deltaCRLIndicator');
++++++++_I_('2.5.29.28', 'issuingDistributionPoint');
++++++++_I_('2.5.29.29', 'certificateIssuer');
++++++++_I_('2.5.29.30', 'nameConstraints');
++++++++_IN('2.5.29.31', 'cRLDistributionPoints');
++++++++_IN('2.5.29.32', 'certificatePolicies');
++++++++_I_('2.5.29.33', 'policyMappings');
++++++++_I_('2.5.29.34', 'policyConstraints'); // deprecated use .36
++++++++_IN('2.5.29.35', 'authorityKeyIdentifier');
++++++++_I_('2.5.29.36', 'policyConstraints');
++++++++_IN('2.5.29.37', 'extKeyUsage');
++++++++_I_('2.5.29.46', 'freshestCRL');
++++++++_I_('2.5.29.54', 'inhibitAnyPolicy');
++++++++
++++++++// extKeyUsage purposes
++++++++_IN('1.3.6.1.4.1.11129.2.4.2', 'timestampList');
++++++++_IN('1.3.6.1.5.5.7.1.1', 'authorityInfoAccess');
++++++++_IN('1.3.6.1.5.5.7.3.1', 'serverAuth');
++++++++_IN('1.3.6.1.5.5.7.3.2', 'clientAuth');
++++++++_IN('1.3.6.1.5.5.7.3.3', 'codeSigning');
++++++++_IN('1.3.6.1.5.5.7.3.4', 'emailProtection');
++++++++_IN('1.3.6.1.5.5.7.3.8', 'timeStamping');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Password-based encryption functions.
++++++++ *
++++++++ * @author Dave Longley
++++++++ * @author Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * An EncryptedPrivateKeyInfo:
++++++++ *
++++++++ * EncryptedPrivateKeyInfo ::= SEQUENCE {
++++++++ * encryptionAlgorithm EncryptionAlgorithmIdentifier,
++++++++ * encryptedData EncryptedData }
++++++++ *
++++++++ * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ *
++++++++ * EncryptedData ::= OCTET STRING
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./asn1');
++++++++require('./des');
++++++++require('./md');
++++++++require('./oids');
++++++++require('./pbkdf2');
++++++++require('./pem');
++++++++require('./random');
++++++++require('./rc2');
++++++++require('./rsa');
++++++++require('./util');
++++++++
++++++++if(typeof BigInteger === 'undefined') {
++++++++ var BigInteger = forge.jsbn.BigInteger;
++++++++}
++++++++
++++++++// shortcut for asn.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++/* Password-based encryption implementation. */
++++++++var pki = forge.pki = forge.pki || {};
++++++++module.exports = pki.pbe = forge.pbe = forge.pbe || {};
++++++++var oids = pki.oids;
++++++++
++++++++// validator for an EncryptedPrivateKeyInfo structure
++++++++// Note: Currently only works w/algorithm params
++++++++var encryptedPrivateKeyValidator = {
++++++++ name: 'EncryptedPrivateKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'EncryptedPrivateKeyInfo.encryptionAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'encryptionOid'
++++++++ }, {
++++++++ name: 'AlgorithmIdentifier.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'encryptionParams'
++++++++ }]
++++++++ }, {
++++++++ // encryptedData
++++++++ name: 'EncryptedPrivateKeyInfo.encryptedData',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'encryptedData'
++++++++ }]
++++++++};
++++++++
++++++++// validator for a PBES2Algorithms structure
++++++++// Note: Currently only works w/PBKDF2 + AES encryption schemes
++++++++var PBES2AlgorithmsValidator = {
++++++++ name: 'PBES2Algorithms',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PBES2Algorithms.keyDerivationFunc',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PBES2Algorithms.keyDerivationFunc.oid',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'kdfOid'
++++++++ }, {
++++++++ name: 'PBES2Algorithms.params',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PBES2Algorithms.params.salt',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'kdfSalt'
++++++++ }, {
++++++++ name: 'PBES2Algorithms.params.iterationCount',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'kdfIterationCount'
++++++++ }, {
++++++++ name: 'PBES2Algorithms.params.keyLength',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'keyLength'
++++++++ }, {
++++++++ // prf
++++++++ name: 'PBES2Algorithms.params.prf',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'PBES2Algorithms.params.prf.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'prfOid'
++++++++ }]
++++++++ }]
++++++++ }]
++++++++ }, {
++++++++ name: 'PBES2Algorithms.encryptionScheme',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PBES2Algorithms.encryptionScheme.oid',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'encOid'
++++++++ }, {
++++++++ name: 'PBES2Algorithms.encryptionScheme.iv',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'encIv'
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++var pkcs12PbeParamsValidator = {
++++++++ name: 'pkcs-12PbeParams',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'pkcs-12PbeParams.salt',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'salt'
++++++++ }, {
++++++++ name: 'pkcs-12PbeParams.iterations',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'iterations'
++++++++ }]
++++++++};
++++++++
++++++++/**
++++++++ * Encrypts a ASN.1 PrivateKeyInfo object, producing an EncryptedPrivateKeyInfo.
++++++++ *
++++++++ * PBES2Algorithms ALGORITHM-IDENTIFIER ::=
++++++++ * { {PBES2-params IDENTIFIED BY id-PBES2}, ...}
++++++++ *
++++++++ * id-PBES2 OBJECT IDENTIFIER ::= {pkcs-5 13}
++++++++ *
++++++++ * PBES2-params ::= SEQUENCE {
++++++++ * keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
++++++++ * encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
++++++++ * }
++++++++ *
++++++++ * PBES2-KDFs ALGORITHM-IDENTIFIER ::=
++++++++ * { {PBKDF2-params IDENTIFIED BY id-PBKDF2}, ... }
++++++++ *
++++++++ * PBES2-Encs ALGORITHM-IDENTIFIER ::= { ... }
++++++++ *
++++++++ * PBKDF2-params ::= SEQUENCE {
++++++++ * salt CHOICE {
++++++++ * specified OCTET STRING,
++++++++ * otherSource AlgorithmIdentifier {{PBKDF2-SaltSources}}
++++++++ * },
++++++++ * iterationCount INTEGER (1..MAX),
++++++++ * keyLength INTEGER (1..MAX) OPTIONAL,
++++++++ * prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1
++++++++ * }
++++++++ *
++++++++ * @param obj the ASN.1 PrivateKeyInfo object.
++++++++ * @param password the password to encrypt with.
++++++++ * @param options:
++++++++ * algorithm the encryption algorithm to use
++++++++ * ('aes128', 'aes192', 'aes256', '3des'), defaults to 'aes128'.
++++++++ * count the iteration count to use.
++++++++ * saltSize the salt size to use.
++++++++ * prfAlgorithm the PRF message digest algorithm to use
++++++++ * ('sha1', 'sha224', 'sha256', 'sha384', 'sha512')
++++++++ *
++++++++ * @return the ASN.1 EncryptedPrivateKeyInfo.
++++++++ */
++++++++pki.encryptPrivateKeyInfo = function(obj, password, options) {
++++++++ // set default options
++++++++ options = options || {};
++++++++ options.saltSize = options.saltSize || 8;
++++++++ options.count = options.count || 2048;
++++++++ options.algorithm = options.algorithm || 'aes128';
++++++++ options.prfAlgorithm = options.prfAlgorithm || 'sha1';
++++++++
++++++++ // generate PBE params
++++++++ var salt = forge.random.getBytesSync(options.saltSize);
++++++++ var count = options.count;
++++++++ var countBytes = asn1.integerToDer(count);
++++++++ var dkLen;
++++++++ var encryptionAlgorithm;
++++++++ var encryptedData;
++++++++ if(options.algorithm.indexOf('aes') === 0 || options.algorithm === 'des') {
++++++++ // do PBES2
++++++++ var ivLen, encOid, cipherFn;
++++++++ switch(options.algorithm) {
++++++++ case 'aes128':
++++++++ dkLen = 16;
++++++++ ivLen = 16;
++++++++ encOid = oids['aes128-CBC'];
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case 'aes192':
++++++++ dkLen = 24;
++++++++ ivLen = 16;
++++++++ encOid = oids['aes192-CBC'];
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case 'aes256':
++++++++ dkLen = 32;
++++++++ ivLen = 16;
++++++++ encOid = oids['aes256-CBC'];
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case 'des':
++++++++ dkLen = 8;
++++++++ ivLen = 8;
++++++++ encOid = oids['desCBC'];
++++++++ cipherFn = forge.des.createEncryptionCipher;
++++++++ break;
++++++++ default:
++++++++ var error = new Error('Cannot encrypt private key. Unknown encryption algorithm.');
++++++++ error.algorithm = options.algorithm;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get PRF message digest
++++++++ var prfAlgorithm = 'hmacWith' + options.prfAlgorithm.toUpperCase();
++++++++ var md = prfAlgorithmToMessageDigest(prfAlgorithm);
++++++++
++++++++ // encrypt private key using pbe SHA-1 and AES/DES
++++++++ var dk = forge.pkcs5.pbkdf2(password, salt, count, dkLen, md);
++++++++ var iv = forge.random.getBytesSync(ivLen);
++++++++ var cipher = cipherFn(dk);
++++++++ cipher.start(iv);
++++++++ cipher.update(asn1.toDer(obj));
++++++++ cipher.finish();
++++++++ encryptedData = cipher.output.getBytes();
++++++++
++++++++ // get PBKDF2-params
++++++++ var params = createPbkdf2Params(salt, countBytes, dkLen, prfAlgorithm);
++++++++
++++++++ encryptionAlgorithm = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(oids['pkcs5PBES2']).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // keyDerivationFunc
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(oids['pkcs5PBKDF2']).getBytes()),
++++++++ // PBKDF2-params
++++++++ params
++++++++ ]),
++++++++ // encryptionScheme
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(encOid).getBytes()),
++++++++ // iv
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, iv)
++++++++ ])
++++++++ ])
++++++++ ]);
++++++++ } else if(options.algorithm === '3des') {
++++++++ // Do PKCS12 PBE
++++++++ dkLen = 24;
++++++++
++++++++ var saltBytes = new forge.util.ByteBuffer(salt);
++++++++ var dk = pki.pbe.generatePkcs12Key(password, saltBytes, 1, count, dkLen);
++++++++ var iv = pki.pbe.generatePkcs12Key(password, saltBytes, 2, count, dkLen);
++++++++ var cipher = forge.des.createEncryptionCipher(dk);
++++++++ cipher.start(iv);
++++++++ cipher.update(asn1.toDer(obj));
++++++++ cipher.finish();
++++++++ encryptedData = cipher.output.getBytes();
++++++++
++++++++ encryptionAlgorithm = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(oids['pbeWithSHAAnd3-KeyTripleDES-CBC']).getBytes()),
++++++++ // pkcs-12PbeParams
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // salt
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, salt),
++++++++ // iteration count
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ countBytes.getBytes())
++++++++ ])
++++++++ ]);
++++++++ } else {
++++++++ var error = new Error('Cannot encrypt private key. Unknown encryption algorithm.');
++++++++ error.algorithm = options.algorithm;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // EncryptedPrivateKeyInfo
++++++++ var rval = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // encryptionAlgorithm
++++++++ encryptionAlgorithm,
++++++++ // encryptedData
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, encryptedData)
++++++++ ]);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Decrypts a ASN.1 PrivateKeyInfo object.
++++++++ *
++++++++ * @param obj the ASN.1 EncryptedPrivateKeyInfo object.
++++++++ * @param password the password to decrypt with.
++++++++ *
++++++++ * @return the ASN.1 PrivateKeyInfo on success, null on failure.
++++++++ */
++++++++pki.decryptPrivateKeyInfo = function(obj, password) {
++++++++ var rval = null;
++++++++
++++++++ // get PBE params
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, encryptedPrivateKeyValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read encrypted private key. ' +
++++++++ 'ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get cipher
++++++++ var oid = asn1.derToOid(capture.encryptionOid);
++++++++ var cipher = pki.pbe.getCipher(oid, capture.encryptionParams, password);
++++++++
++++++++ // get encrypted data
++++++++ var encrypted = forge.util.createBuffer(capture.encryptedData);
++++++++
++++++++ cipher.update(encrypted);
++++++++ if(cipher.finish()) {
++++++++ rval = asn1.fromDer(cipher.output);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a EncryptedPrivateKeyInfo to PEM format.
++++++++ *
++++++++ * @param epki the EncryptedPrivateKeyInfo.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted encrypted private key.
++++++++ */
++++++++pki.encryptedPrivateKeyToPem = function(epki, maxline) {
++++++++ // convert to DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'ENCRYPTED PRIVATE KEY',
++++++++ body: asn1.toDer(epki).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PEM-encoded EncryptedPrivateKeyInfo to ASN.1 format. Decryption
++++++++ * is not performed.
++++++++ *
++++++++ * @param pem the EncryptedPrivateKeyInfo in PEM-format.
++++++++ *
++++++++ * @return the ASN.1 EncryptedPrivateKeyInfo.
++++++++ */
++++++++pki.encryptedPrivateKeyFromPem = function(pem) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'ENCRYPTED PRIVATE KEY') {
++++++++ var error = new Error('Could not convert encrypted private key from PEM; ' +
++++++++ 'PEM header type is "ENCRYPTED PRIVATE KEY".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert encrypted private key from PEM; ' +
++++++++ 'PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ return asn1.fromDer(msg.body);
++++++++};
++++++++
++++++++/**
++++++++ * Encrypts an RSA private key. By default, the key will be wrapped in
++++++++ * a PrivateKeyInfo and encrypted to produce a PKCS#8 EncryptedPrivateKeyInfo.
++++++++ * This is the standard, preferred way to encrypt a private key.
++++++++ *
++++++++ * To produce a non-standard PEM-encrypted private key that uses encapsulated
++++++++ * headers to indicate the encryption algorithm (old-style non-PKCS#8 OpenSSL
++++++++ * private key encryption), set the 'legacy' option to true. Note: Using this
++++++++ * option will cause the iteration count to be forced to 1.
++++++++ *
++++++++ * Note: The 'des' algorithm is supported, but it is not considered to be
++++++++ * secure because it only uses a single 56-bit key. If possible, it is highly
++++++++ * recommended that a different algorithm be used.
++++++++ *
++++++++ * @param rsaKey the RSA key to encrypt.
++++++++ * @param password the password to use.
++++++++ * @param options:
++++++++ * algorithm: the encryption algorithm to use
++++++++ * ('aes128', 'aes192', 'aes256', '3des', 'des').
++++++++ * count: the iteration count to use.
++++++++ * saltSize: the salt size to use.
++++++++ * legacy: output an old non-PKCS#8 PEM-encrypted+encapsulated
++++++++ * headers (DEK-Info) private key.
++++++++ *
++++++++ * @return the PEM-encoded ASN.1 EncryptedPrivateKeyInfo.
++++++++ */
++++++++pki.encryptRsaPrivateKey = function(rsaKey, password, options) {
++++++++ // standard PKCS#8
++++++++ options = options || {};
++++++++ if(!options.legacy) {
++++++++ // encrypt PrivateKeyInfo
++++++++ var rval = pki.wrapRsaPrivateKey(pki.privateKeyToAsn1(rsaKey));
++++++++ rval = pki.encryptPrivateKeyInfo(rval, password, options);
++++++++ return pki.encryptedPrivateKeyToPem(rval);
++++++++ }
++++++++
++++++++ // legacy non-PKCS#8
++++++++ var algorithm;
++++++++ var iv;
++++++++ var dkLen;
++++++++ var cipherFn;
++++++++ switch(options.algorithm) {
++++++++ case 'aes128':
++++++++ algorithm = 'AES-128-CBC';
++++++++ dkLen = 16;
++++++++ iv = forge.random.getBytesSync(16);
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case 'aes192':
++++++++ algorithm = 'AES-192-CBC';
++++++++ dkLen = 24;
++++++++ iv = forge.random.getBytesSync(16);
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case 'aes256':
++++++++ algorithm = 'AES-256-CBC';
++++++++ dkLen = 32;
++++++++ iv = forge.random.getBytesSync(16);
++++++++ cipherFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++ case '3des':
++++++++ algorithm = 'DES-EDE3-CBC';
++++++++ dkLen = 24;
++++++++ iv = forge.random.getBytesSync(8);
++++++++ cipherFn = forge.des.createEncryptionCipher;
++++++++ break;
++++++++ case 'des':
++++++++ algorithm = 'DES-CBC';
++++++++ dkLen = 8;
++++++++ iv = forge.random.getBytesSync(8);
++++++++ cipherFn = forge.des.createEncryptionCipher;
++++++++ break;
++++++++ default:
++++++++ var error = new Error('Could not encrypt RSA private key; unsupported ' +
++++++++ 'encryption algorithm "' + options.algorithm + '".');
++++++++ error.algorithm = options.algorithm;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // encrypt private key using OpenSSL legacy key derivation
++++++++ var dk = forge.pbe.opensslDeriveBytes(password, iv.substr(0, 8), dkLen);
++++++++ var cipher = cipherFn(dk);
++++++++ cipher.start(iv);
++++++++ cipher.update(asn1.toDer(pki.privateKeyToAsn1(rsaKey)));
++++++++ cipher.finish();
++++++++
++++++++ var msg = {
++++++++ type: 'RSA PRIVATE KEY',
++++++++ procType: {
++++++++ version: '4',
++++++++ type: 'ENCRYPTED'
++++++++ },
++++++++ dekInfo: {
++++++++ algorithm: algorithm,
++++++++ parameters: forge.util.bytesToHex(iv).toUpperCase()
++++++++ },
++++++++ body: cipher.output.getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg);
++++++++};
++++++++
++++++++/**
++++++++ * Decrypts an RSA private key.
++++++++ *
++++++++ * @param pem the PEM-formatted EncryptedPrivateKeyInfo to decrypt.
++++++++ * @param password the password to use.
++++++++ *
++++++++ * @return the RSA key on success, null on failure.
++++++++ */
++++++++pki.decryptRsaPrivateKey = function(pem, password) {
++++++++ var rval = null;
++++++++
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'ENCRYPTED PRIVATE KEY' &&
++++++++ msg.type !== 'PRIVATE KEY' &&
++++++++ msg.type !== 'RSA PRIVATE KEY') {
++++++++ var error = new Error('Could not convert private key from PEM; PEM header type ' +
++++++++ 'is not "ENCRYPTED PRIVATE KEY", "PRIVATE KEY", or "RSA PRIVATE KEY".');
++++++++ error.headerType = error;
++++++++ throw error;
++++++++ }
++++++++
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ var dkLen;
++++++++ var cipherFn;
++++++++ switch(msg.dekInfo.algorithm) {
++++++++ case 'DES-CBC':
++++++++ dkLen = 8;
++++++++ cipherFn = forge.des.createDecryptionCipher;
++++++++ break;
++++++++ case 'DES-EDE3-CBC':
++++++++ dkLen = 24;
++++++++ cipherFn = forge.des.createDecryptionCipher;
++++++++ break;
++++++++ case 'AES-128-CBC':
++++++++ dkLen = 16;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'AES-192-CBC':
++++++++ dkLen = 24;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'AES-256-CBC':
++++++++ dkLen = 32;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'RC2-40-CBC':
++++++++ dkLen = 5;
++++++++ cipherFn = function(key) {
++++++++ return forge.rc2.createDecryptionCipher(key, 40);
++++++++ };
++++++++ break;
++++++++ case 'RC2-64-CBC':
++++++++ dkLen = 8;
++++++++ cipherFn = function(key) {
++++++++ return forge.rc2.createDecryptionCipher(key, 64);
++++++++ };
++++++++ break;
++++++++ case 'RC2-128-CBC':
++++++++ dkLen = 16;
++++++++ cipherFn = function(key) {
++++++++ return forge.rc2.createDecryptionCipher(key, 128);
++++++++ };
++++++++ break;
++++++++ default:
++++++++ var error = new Error('Could not decrypt private key; unsupported ' +
++++++++ 'encryption algorithm "' + msg.dekInfo.algorithm + '".');
++++++++ error.algorithm = msg.dekInfo.algorithm;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // use OpenSSL legacy key derivation
++++++++ var iv = forge.util.hexToBytes(msg.dekInfo.parameters);
++++++++ var dk = forge.pbe.opensslDeriveBytes(password, iv.substr(0, 8), dkLen);
++++++++ var cipher = cipherFn(dk);
++++++++ cipher.start(iv);
++++++++ cipher.update(forge.util.createBuffer(msg.body));
++++++++ if(cipher.finish()) {
++++++++ rval = cipher.output.getBytes();
++++++++ } else {
++++++++ return rval;
++++++++ }
++++++++ } else {
++++++++ rval = msg.body;
++++++++ }
++++++++
++++++++ if(msg.type === 'ENCRYPTED PRIVATE KEY') {
++++++++ rval = pki.decryptPrivateKeyInfo(asn1.fromDer(rval), password);
++++++++ } else {
++++++++ // decryption already performed above
++++++++ rval = asn1.fromDer(rval);
++++++++ }
++++++++
++++++++ if(rval !== null) {
++++++++ rval = pki.privateKeyFromAsn1(rval);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Derives a PKCS#12 key.
++++++++ *
++++++++ * @param password the password to derive the key material from, null or
++++++++ * undefined for none.
++++++++ * @param salt the salt, as a ByteBuffer, to use.
++++++++ * @param id the PKCS#12 ID byte (1 = key material, 2 = IV, 3 = MAC).
++++++++ * @param iter the iteration count.
++++++++ * @param n the number of bytes to derive from the password.
++++++++ * @param md the message digest to use, defaults to SHA-1.
++++++++ *
++++++++ * @return a ByteBuffer with the bytes derived from the password.
++++++++ */
++++++++pki.pbe.generatePkcs12Key = function(password, salt, id, iter, n, md) {
++++++++ var j, l;
++++++++
++++++++ if(typeof md === 'undefined' || md === null) {
++++++++ if(!('sha1' in forge.md)) {
++++++++ throw new Error('"sha1" hash algorithm unavailable.');
++++++++ }
++++++++ md = forge.md.sha1.create();
++++++++ }
++++++++
++++++++ var u = md.digestLength;
++++++++ var v = md.blockLength;
++++++++ var result = new forge.util.ByteBuffer();
++++++++
++++++++ /* Convert password to Unicode byte buffer + trailing 0-byte. */
++++++++ var passBuf = new forge.util.ByteBuffer();
++++++++ if(password !== null && password !== undefined) {
++++++++ for(l = 0; l < password.length; l++) {
++++++++ passBuf.putInt16(password.charCodeAt(l));
++++++++ }
++++++++ passBuf.putInt16(0);
++++++++ }
++++++++
++++++++ /* Length of salt and password in BYTES. */
++++++++ var p = passBuf.length();
++++++++ var s = salt.length();
++++++++
++++++++ /* 1. Construct a string, D (the "diversifier"), by concatenating
++++++++ v copies of ID. */
++++++++ var D = new forge.util.ByteBuffer();
++++++++ D.fillWithByte(id, v);
++++++++
++++++++ /* 2. Concatenate copies of the salt together to create a string S of length
++++++++ v * ceil(s / v) bytes (the final copy of the salt may be trunacted
++++++++ to create S).
++++++++ Note that if the salt is the empty string, then so is S. */
++++++++ var Slen = v * Math.ceil(s / v);
++++++++ var S = new forge.util.ByteBuffer();
++++++++ for(l = 0; l < Slen; l++) {
++++++++ S.putByte(salt.at(l % s));
++++++++ }
++++++++
++++++++ /* 3. Concatenate copies of the password together to create a string P of
++++++++ length v * ceil(p / v) bytes (the final copy of the password may be
++++++++ truncated to create P).
++++++++ Note that if the password is the empty string, then so is P. */
++++++++ var Plen = v * Math.ceil(p / v);
++++++++ var P = new forge.util.ByteBuffer();
++++++++ for(l = 0; l < Plen; l++) {
++++++++ P.putByte(passBuf.at(l % p));
++++++++ }
++++++++
++++++++ /* 4. Set I=S||P to be the concatenation of S and P. */
++++++++ var I = S;
++++++++ I.putBuffer(P);
++++++++
++++++++ /* 5. Set c=ceil(n / u). */
++++++++ var c = Math.ceil(n / u);
++++++++
++++++++ /* 6. For i=1, 2, ..., c, do the following: */
++++++++ for(var i = 1; i <= c; i++) {
++++++++ /* a) Set Ai=H^r(D||I). (l.e. the rth hash of D||I, H(H(H(...H(D||I)))) */
++++++++ var buf = new forge.util.ByteBuffer();
++++++++ buf.putBytes(D.bytes());
++++++++ buf.putBytes(I.bytes());
++++++++ for(var round = 0; round < iter; round++) {
++++++++ md.start();
++++++++ md.update(buf.getBytes());
++++++++ buf = md.digest();
++++++++ }
++++++++
++++++++ /* b) Concatenate copies of Ai to create a string B of length v bytes (the
++++++++ final copy of Ai may be truncated to create B). */
++++++++ var B = new forge.util.ByteBuffer();
++++++++ for(l = 0; l < v; l++) {
++++++++ B.putByte(buf.at(l % u));
++++++++ }
++++++++
++++++++ /* c) Treating I as a concatenation I0, I1, ..., Ik-1 of v-byte blocks,
++++++++ where k=ceil(s / v) + ceil(p / v), modify I by setting
++++++++ Ij=(Ij+B+1) mod 2v for each j. */
++++++++ var k = Math.ceil(s / v) + Math.ceil(p / v);
++++++++ var Inew = new forge.util.ByteBuffer();
++++++++ for(j = 0; j < k; j++) {
++++++++ var chunk = new forge.util.ByteBuffer(I.getBytes(v));
++++++++ var x = 0x1ff;
++++++++ for(l = B.length() - 1; l >= 0; l--) {
++++++++ x = x >> 8;
++++++++ x += B.at(l) + chunk.at(l);
++++++++ chunk.setAt(l, x & 0xff);
++++++++ }
++++++++ Inew.putBuffer(chunk);
++++++++ }
++++++++ I = Inew;
++++++++
++++++++ /* Add Ai to A. */
++++++++ result.putBuffer(buf);
++++++++ }
++++++++
++++++++ result.truncate(result.length() - n);
++++++++ return result;
++++++++};
++++++++
++++++++/**
++++++++ * Get new Forge cipher object instance.
++++++++ *
++++++++ * @param oid the OID (in string notation).
++++++++ * @param params the ASN.1 params object.
++++++++ * @param password the password to decrypt with.
++++++++ *
++++++++ * @return new cipher object instance.
++++++++ */
++++++++pki.pbe.getCipher = function(oid, params, password) {
++++++++ switch(oid) {
++++++++ case pki.oids['pkcs5PBES2']:
++++++++ return pki.pbe.getCipherForPBES2(oid, params, password);
++++++++
++++++++ case pki.oids['pbeWithSHAAnd3-KeyTripleDES-CBC']:
++++++++ case pki.oids['pbewithSHAAnd40BitRC2-CBC']:
++++++++ return pki.pbe.getCipherForPKCS12PBE(oid, params, password);
++++++++
++++++++ default:
++++++++ var error = new Error('Cannot read encrypted PBE data block. Unsupported OID.');
++++++++ error.oid = oid;
++++++++ error.supportedOids = [
++++++++ 'pkcs5PBES2',
++++++++ 'pbeWithSHAAnd3-KeyTripleDES-CBC',
++++++++ 'pbewithSHAAnd40BitRC2-CBC'
++++++++ ];
++++++++ throw error;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Get new Forge cipher object instance according to PBES2 params block.
++++++++ *
++++++++ * The returned cipher instance is already started using the IV
++++++++ * from PBES2 parameter block.
++++++++ *
++++++++ * @param oid the PKCS#5 PBKDF2 OID (in string notation).
++++++++ * @param params the ASN.1 PBES2-params object.
++++++++ * @param password the password to decrypt with.
++++++++ *
++++++++ * @return new cipher object instance.
++++++++ */
++++++++pki.pbe.getCipherForPBES2 = function(oid, params, password) {
++++++++ // get PBE params
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(params, PBES2AlgorithmsValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read password-based-encryption algorithm ' +
++++++++ 'parameters. ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // check oids
++++++++ oid = asn1.derToOid(capture.kdfOid);
++++++++ if(oid !== pki.oids['pkcs5PBKDF2']) {
++++++++ var error = new Error('Cannot read encrypted private key. ' +
++++++++ 'Unsupported key derivation function OID.');
++++++++ error.oid = oid;
++++++++ error.supportedOids = ['pkcs5PBKDF2'];
++++++++ throw error;
++++++++ }
++++++++ oid = asn1.derToOid(capture.encOid);
++++++++ if(oid !== pki.oids['aes128-CBC'] &&
++++++++ oid !== pki.oids['aes192-CBC'] &&
++++++++ oid !== pki.oids['aes256-CBC'] &&
++++++++ oid !== pki.oids['des-EDE3-CBC'] &&
++++++++ oid !== pki.oids['desCBC']) {
++++++++ var error = new Error('Cannot read encrypted private key. ' +
++++++++ 'Unsupported encryption scheme OID.');
++++++++ error.oid = oid;
++++++++ error.supportedOids = [
++++++++ 'aes128-CBC', 'aes192-CBC', 'aes256-CBC', 'des-EDE3-CBC', 'desCBC'];
++++++++ throw error;
++++++++ }
++++++++
++++++++ // set PBE params
++++++++ var salt = capture.kdfSalt;
++++++++ var count = forge.util.createBuffer(capture.kdfIterationCount);
++++++++ count = count.getInt(count.length() << 3);
++++++++ var dkLen;
++++++++ var cipherFn;
++++++++ switch(pki.oids[oid]) {
++++++++ case 'aes128-CBC':
++++++++ dkLen = 16;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'aes192-CBC':
++++++++ dkLen = 24;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'aes256-CBC':
++++++++ dkLen = 32;
++++++++ cipherFn = forge.aes.createDecryptionCipher;
++++++++ break;
++++++++ case 'des-EDE3-CBC':
++++++++ dkLen = 24;
++++++++ cipherFn = forge.des.createDecryptionCipher;
++++++++ break;
++++++++ case 'desCBC':
++++++++ dkLen = 8;
++++++++ cipherFn = forge.des.createDecryptionCipher;
++++++++ break;
++++++++ }
++++++++
++++++++ // get PRF message digest
++++++++ var md = prfOidToMessageDigest(capture.prfOid);
++++++++
++++++++ // decrypt private key using pbe with chosen PRF and AES/DES
++++++++ var dk = forge.pkcs5.pbkdf2(password, salt, count, dkLen, md);
++++++++ var iv = capture.encIv;
++++++++ var cipher = cipherFn(dk);
++++++++ cipher.start(iv);
++++++++
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Get new Forge cipher object instance for PKCS#12 PBE.
++++++++ *
++++++++ * The returned cipher instance is already started using the key & IV
++++++++ * derived from the provided password and PKCS#12 PBE salt.
++++++++ *
++++++++ * @param oid The PKCS#12 PBE OID (in string notation).
++++++++ * @param params The ASN.1 PKCS#12 PBE-params object.
++++++++ * @param password The password to decrypt with.
++++++++ *
++++++++ * @return the new cipher object instance.
++++++++ */
++++++++pki.pbe.getCipherForPKCS12PBE = function(oid, params, password) {
++++++++ // get PBE params
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(params, pkcs12PbeParamsValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read password-based-encryption algorithm ' +
++++++++ 'parameters. ASN.1 object is not a supported EncryptedPrivateKeyInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var salt = forge.util.createBuffer(capture.salt);
++++++++ var count = forge.util.createBuffer(capture.iterations);
++++++++ count = count.getInt(count.length() << 3);
++++++++
++++++++ var dkLen, dIvLen, cipherFn;
++++++++ switch(oid) {
++++++++ case pki.oids['pbeWithSHAAnd3-KeyTripleDES-CBC']:
++++++++ dkLen = 24;
++++++++ dIvLen = 8;
++++++++ cipherFn = forge.des.startDecrypting;
++++++++ break;
++++++++
++++++++ case pki.oids['pbewithSHAAnd40BitRC2-CBC']:
++++++++ dkLen = 5;
++++++++ dIvLen = 8;
++++++++ cipherFn = function(key, iv) {
++++++++ var cipher = forge.rc2.createDecryptionCipher(key, 40);
++++++++ cipher.start(iv, null);
++++++++ return cipher;
++++++++ };
++++++++ break;
++++++++
++++++++ default:
++++++++ var error = new Error('Cannot read PKCS #12 PBE data block. Unsupported OID.');
++++++++ error.oid = oid;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get PRF message digest
++++++++ var md = prfOidToMessageDigest(capture.prfOid);
++++++++ var key = pki.pbe.generatePkcs12Key(password, salt, 1, count, dkLen, md);
++++++++ md.start();
++++++++ var iv = pki.pbe.generatePkcs12Key(password, salt, 2, count, dIvLen, md);
++++++++
++++++++ return cipherFn(key, iv);
++++++++};
++++++++
++++++++/**
++++++++ * OpenSSL's legacy key derivation function.
++++++++ *
++++++++ * See: http://www.openssl.org/docs/crypto/EVP_BytesToKey.html
++++++++ *
++++++++ * @param password the password to derive the key from.
++++++++ * @param salt the salt to use, null for none.
++++++++ * @param dkLen the number of bytes needed for the derived key.
++++++++ * @param [options] the options to use:
++++++++ * [md] an optional message digest object to use.
++++++++ */
++++++++pki.pbe.opensslDeriveBytes = function(password, salt, dkLen, md) {
++++++++ if(typeof md === 'undefined' || md === null) {
++++++++ if(!('md5' in forge.md)) {
++++++++ throw new Error('"md5" hash algorithm unavailable.');
++++++++ }
++++++++ md = forge.md.md5.create();
++++++++ }
++++++++ if(salt === null) {
++++++++ salt = '';
++++++++ }
++++++++ var digests = [hash(md, password + salt)];
++++++++ for(var length = 16, i = 1; length < dkLen; ++i, length += 16) {
++++++++ digests.push(hash(md, digests[i - 1] + password + salt));
++++++++ }
++++++++ return digests.join('').substr(0, dkLen);
++++++++};
++++++++
++++++++function hash(md, bytes) {
++++++++ return md.start().update(bytes).digest().getBytes();
++++++++}
++++++++
++++++++function prfOidToMessageDigest(prfOid) {
++++++++ // get PRF algorithm, default to SHA-1
++++++++ var prfAlgorithm;
++++++++ if(!prfOid) {
++++++++ prfAlgorithm = 'hmacWithSHA1';
++++++++ } else {
++++++++ prfAlgorithm = pki.oids[asn1.derToOid(prfOid)];
++++++++ if(!prfAlgorithm) {
++++++++ var error = new Error('Unsupported PRF OID.');
++++++++ error.oid = prfOid;
++++++++ error.supported = [
++++++++ 'hmacWithSHA1', 'hmacWithSHA224', 'hmacWithSHA256', 'hmacWithSHA384',
++++++++ 'hmacWithSHA512'];
++++++++ throw error;
++++++++ }
++++++++ }
++++++++ return prfAlgorithmToMessageDigest(prfAlgorithm);
++++++++}
++++++++
++++++++function prfAlgorithmToMessageDigest(prfAlgorithm) {
++++++++ var factory = forge.md;
++++++++ switch(prfAlgorithm) {
++++++++ case 'hmacWithSHA224':
++++++++ factory = forge.md.sha512;
++++++++ case 'hmacWithSHA1':
++++++++ case 'hmacWithSHA256':
++++++++ case 'hmacWithSHA384':
++++++++ case 'hmacWithSHA512':
++++++++ prfAlgorithm = prfAlgorithm.substr(8).toLowerCase();
++++++++ break;
++++++++ default:
++++++++ var error = new Error('Unsupported PRF algorithm.');
++++++++ error.algorithm = prfAlgorithm;
++++++++ error.supported = [
++++++++ 'hmacWithSHA1', 'hmacWithSHA224', 'hmacWithSHA256', 'hmacWithSHA384',
++++++++ 'hmacWithSHA512'];
++++++++ throw error;
++++++++ }
++++++++ if(!factory || !(prfAlgorithm in factory)) {
++++++++ throw new Error('Unknown hash algorithm: ' + prfAlgorithm);
++++++++ }
++++++++ return factory[prfAlgorithm].create();
++++++++}
++++++++
++++++++function createPbkdf2Params(salt, countBytes, dkLen, prfAlgorithm) {
++++++++ var params = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // salt
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, salt),
++++++++ // iteration count
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ countBytes.getBytes())
++++++++ ]);
++++++++ // when PRF algorithm is not SHA-1 default, add key length and PRF algorithm
++++++++ if(prfAlgorithm !== 'hmacWithSHA1') {
++++++++ params.value.push(
++++++++ // key length
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ forge.util.hexToBytes(dkLen.toString(16))),
++++++++ // AlgorithmIdentifier
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids[prfAlgorithm]).getBytes()),
++++++++ // parameters (null)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]));
++++++++ }
++++++++ return params;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Password-Based Key-Derivation Function #2 implementation.
++++++++ *
++++++++ * See RFC 2898 for details.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./hmac');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++var pkcs5 = forge.pkcs5 = forge.pkcs5 || {};
++++++++
++++++++var crypto;
++++++++if(forge.util.isNodejs && !forge.options.usePureJavaScript) {
++++++++ crypto = require('crypto');
++++++++}
++++++++
++++++++/**
++++++++ * Derives a key from a password.
++++++++ *
++++++++ * @param p the password as a binary-encoded string of bytes.
++++++++ * @param s the salt as a binary-encoded string of bytes.
++++++++ * @param c the iteration count, a positive integer.
++++++++ * @param dkLen the intended length, in bytes, of the derived key,
++++++++ * (max: 2^32 - 1) * hash length of the PRF.
++++++++ * @param [md] the message digest (or algorithm identifier as a string) to use
++++++++ * in the PRF, defaults to SHA-1.
++++++++ * @param [callback(err, key)] presence triggers asynchronous version, called
++++++++ * once the operation completes.
++++++++ *
++++++++ * @return the derived key, as a binary-encoded string of bytes, for the
++++++++ * synchronous version (if no callback is specified).
++++++++ */
++++++++module.exports = forge.pbkdf2 = pkcs5.pbkdf2 = function(
++++++++ p, s, c, dkLen, md, callback) {
++++++++ if(typeof md === 'function') {
++++++++ callback = md;
++++++++ md = null;
++++++++ }
++++++++
++++++++ // use native implementation if possible and not disabled, note that
++++++++ // some node versions only support SHA-1, others allow digest to be changed
++++++++ if(forge.util.isNodejs && !forge.options.usePureJavaScript &&
++++++++ crypto.pbkdf2 && (md === null || typeof md !== 'object') &&
++++++++ (crypto.pbkdf2Sync.length > 4 || (!md || md === 'sha1'))) {
++++++++ if(typeof md !== 'string') {
++++++++ // default prf to SHA-1
++++++++ md = 'sha1';
++++++++ }
++++++++ p = Buffer.from(p, 'binary');
++++++++ s = Buffer.from(s, 'binary');
++++++++ if(!callback) {
++++++++ if(crypto.pbkdf2Sync.length === 4) {
++++++++ return crypto.pbkdf2Sync(p, s, c, dkLen).toString('binary');
++++++++ }
++++++++ return crypto.pbkdf2Sync(p, s, c, dkLen, md).toString('binary');
++++++++ }
++++++++ if(crypto.pbkdf2Sync.length === 4) {
++++++++ return crypto.pbkdf2(p, s, c, dkLen, function(err, key) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ callback(null, key.toString('binary'));
++++++++ });
++++++++ }
++++++++ return crypto.pbkdf2(p, s, c, dkLen, md, function(err, key) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ callback(null, key.toString('binary'));
++++++++ });
++++++++ }
++++++++
++++++++ if(typeof md === 'undefined' || md === null) {
++++++++ // default prf to SHA-1
++++++++ md = 'sha1';
++++++++ }
++++++++ if(typeof md === 'string') {
++++++++ if(!(md in forge.md.algorithms)) {
++++++++ throw new Error('Unknown hash algorithm: ' + md);
++++++++ }
++++++++ md = forge.md[md].create();
++++++++ }
++++++++
++++++++ var hLen = md.digestLength;
++++++++
++++++++ /* 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
++++++++ stop. */
++++++++ if(dkLen > (0xFFFFFFFF * hLen)) {
++++++++ var err = new Error('Derived key is too long.');
++++++++ if(callback) {
++++++++ return callback(err);
++++++++ }
++++++++ throw err;
++++++++ }
++++++++
++++++++ /* 2. Let len be the number of hLen-octet blocks in the derived key,
++++++++ rounding up, and let r be the number of octets in the last
++++++++ block:
++++++++
++++++++ len = CEIL(dkLen / hLen),
++++++++ r = dkLen - (len - 1) * hLen. */
++++++++ var len = Math.ceil(dkLen / hLen);
++++++++ var r = dkLen - (len - 1) * hLen;
++++++++
++++++++ /* 3. For each block of the derived key apply the function F defined
++++++++ below to the password P, the salt S, the iteration count c, and
++++++++ the block index to compute the block:
++++++++
++++++++ T_1 = F(P, S, c, 1),
++++++++ T_2 = F(P, S, c, 2),
++++++++ ...
++++++++ T_len = F(P, S, c, len),
++++++++
++++++++ where the function F is defined as the exclusive-or sum of the
++++++++ first c iterates of the underlying pseudorandom function PRF
++++++++ applied to the password P and the concatenation of the salt S
++++++++ and the block index i:
++++++++
++++++++ F(P, S, c, i) = u_1 XOR u_2 XOR ... XOR u_c
++++++++
++++++++ where
++++++++
++++++++ u_1 = PRF(P, S || INT(i)),
++++++++ u_2 = PRF(P, u_1),
++++++++ ...
++++++++ u_c = PRF(P, u_{c-1}).
++++++++
++++++++ Here, INT(i) is a four-octet encoding of the integer i, most
++++++++ significant octet first. */
++++++++ var prf = forge.hmac.create();
++++++++ prf.start(md, p);
++++++++ var dk = '';
++++++++ var xor, u_c, u_c1;
++++++++
++++++++ // sync version
++++++++ if(!callback) {
++++++++ for(var i = 1; i <= len; ++i) {
++++++++ // PRF(P, S || INT(i)) (first iteration)
++++++++ prf.start(null, null);
++++++++ prf.update(s);
++++++++ prf.update(forge.util.int32ToBytes(i));
++++++++ xor = u_c1 = prf.digest().getBytes();
++++++++
++++++++ // PRF(P, u_{c-1}) (other iterations)
++++++++ for(var j = 2; j <= c; ++j) {
++++++++ prf.start(null, null);
++++++++ prf.update(u_c1);
++++++++ u_c = prf.digest().getBytes();
++++++++ // F(p, s, c, i)
++++++++ xor = forge.util.xorBytes(xor, u_c, hLen);
++++++++ u_c1 = u_c;
++++++++ }
++++++++
++++++++ /* 4. Concatenate the blocks and extract the first dkLen octets to
++++++++ produce a derived key DK:
++++++++
++++++++ DK = T_1 || T_2 || ... || T_len<0..r-1> */
++++++++ dk += (i < len) ? xor : xor.substr(0, r);
++++++++ }
++++++++ /* 5. Output the derived key DK. */
++++++++ return dk;
++++++++ }
++++++++
++++++++ // async version
++++++++ var i = 1, j;
++++++++ function outer() {
++++++++ if(i > len) {
++++++++ // done
++++++++ return callback(null, dk);
++++++++ }
++++++++
++++++++ // PRF(P, S || INT(i)) (first iteration)
++++++++ prf.start(null, null);
++++++++ prf.update(s);
++++++++ prf.update(forge.util.int32ToBytes(i));
++++++++ xor = u_c1 = prf.digest().getBytes();
++++++++
++++++++ // PRF(P, u_{c-1}) (other iterations)
++++++++ j = 2;
++++++++ inner();
++++++++ }
++++++++
++++++++ function inner() {
++++++++ if(j <= c) {
++++++++ prf.start(null, null);
++++++++ prf.update(u_c1);
++++++++ u_c = prf.digest().getBytes();
++++++++ // F(p, s, c, i)
++++++++ xor = forge.util.xorBytes(xor, u_c, hLen);
++++++++ u_c1 = u_c;
++++++++ ++j;
++++++++ return forge.util.setImmediate(inner);
++++++++ }
++++++++
++++++++ /* 4. Concatenate the blocks and extract the first dkLen octets to
++++++++ produce a derived key DK:
++++++++
++++++++ DK = T_1 || T_2 || ... || T_len<0..r-1> */
++++++++ dk += (i < len) ? xor : xor.substr(0, r);
++++++++
++++++++ ++i;
++++++++ outer();
++++++++ }
++++++++
++++++++ outer();
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of basic PEM (Privacy Enhanced Mail) algorithms.
++++++++ *
++++++++ * See: RFC 1421.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2013-2014 Digital Bazaar, Inc.
++++++++ *
++++++++ * A Forge PEM object has the following fields:
++++++++ *
++++++++ * type: identifies the type of message (eg: "RSA PRIVATE KEY").
++++++++ *
++++++++ * procType: identifies the type of processing performed on the message,
++++++++ * it has two subfields: version and type, eg: 4,ENCRYPTED.
++++++++ *
++++++++ * contentDomain: identifies the type of content in the message, typically
++++++++ * only uses the value: "RFC822".
++++++++ *
++++++++ * dekInfo: identifies the message encryption algorithm and mode and includes
++++++++ * any parameters for the algorithm, it has two subfields: algorithm and
++++++++ * parameters, eg: DES-CBC,F8143EDE5960C597.
++++++++ *
++++++++ * headers: contains all other PEM encapsulated headers -- where order is
++++++++ * significant (for pairing data like recipient ID + key info).
++++++++ *
++++++++ * body: the binary-encoded body.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++// shortcut for pem API
++++++++var pem = module.exports = forge.pem = forge.pem || {};
++++++++
++++++++/**
++++++++ * Encodes (serializes) the given PEM object.
++++++++ *
++++++++ * @param msg the PEM message object to encode.
++++++++ * @param options the options to use:
++++++++ * maxline the maximum characters per line for the body, (default: 64).
++++++++ *
++++++++ * @return the PEM-formatted string.
++++++++ */
++++++++pem.encode = function(msg, options) {
++++++++ options = options || {};
++++++++ var rval = '-----BEGIN ' + msg.type + '-----\r\n';
++++++++
++++++++ // encode special headers
++++++++ var header;
++++++++ if(msg.procType) {
++++++++ header = {
++++++++ name: 'Proc-Type',
++++++++ values: [String(msg.procType.version), msg.procType.type]
++++++++ };
++++++++ rval += foldHeader(header);
++++++++ }
++++++++ if(msg.contentDomain) {
++++++++ header = {name: 'Content-Domain', values: [msg.contentDomain]};
++++++++ rval += foldHeader(header);
++++++++ }
++++++++ if(msg.dekInfo) {
++++++++ header = {name: 'DEK-Info', values: [msg.dekInfo.algorithm]};
++++++++ if(msg.dekInfo.parameters) {
++++++++ header.values.push(msg.dekInfo.parameters);
++++++++ }
++++++++ rval += foldHeader(header);
++++++++ }
++++++++
++++++++ if(msg.headers) {
++++++++ // encode all other headers
++++++++ for(var i = 0; i < msg.headers.length; ++i) {
++++++++ rval += foldHeader(msg.headers[i]);
++++++++ }
++++++++ }
++++++++
++++++++ // terminate header
++++++++ if(msg.procType) {
++++++++ rval += '\r\n';
++++++++ }
++++++++
++++++++ // add body
++++++++ rval += forge.util.encode64(msg.body, options.maxline || 64) + '\r\n';
++++++++
++++++++ rval += '-----END ' + msg.type + '-----\r\n';
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes (deserializes) all PEM messages found in the given string.
++++++++ *
++++++++ * @param str the PEM-formatted string to decode.
++++++++ *
++++++++ * @return the PEM message objects in an array.
++++++++ */
++++++++pem.decode = function(str) {
++++++++ var rval = [];
++++++++
++++++++ // split string into PEM messages (be lenient w/EOF on BEGIN line)
++++++++ var rMessage = /\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g;
++++++++ var rHeader = /([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/;
++++++++ var rCRLF = /\r?\n/;
++++++++ var match;
++++++++ while(true) {
++++++++ match = rMessage.exec(str);
++++++++ if(!match) {
++++++++ break;
++++++++ }
++++++++
++++++++ // accept "NEW CERTIFICATE REQUEST" as "CERTIFICATE REQUEST"
++++++++ // https://datatracker.ietf.org/doc/html/rfc7468#section-7
++++++++ var type = match[1];
++++++++ if(type === 'NEW CERTIFICATE REQUEST') {
++++++++ type = 'CERTIFICATE REQUEST';
++++++++ }
++++++++
++++++++ var msg = {
++++++++ type: type,
++++++++ procType: null,
++++++++ contentDomain: null,
++++++++ dekInfo: null,
++++++++ headers: [],
++++++++ body: forge.util.decode64(match[3])
++++++++ };
++++++++ rval.push(msg);
++++++++
++++++++ // no headers
++++++++ if(!match[2]) {
++++++++ continue;
++++++++ }
++++++++
++++++++ // parse headers
++++++++ var lines = match[2].split(rCRLF);
++++++++ var li = 0;
++++++++ while(match && li < lines.length) {
++++++++ // get line, trim any rhs whitespace
++++++++ var line = lines[li].replace(/\s+$/, '');
++++++++
++++++++ // RFC2822 unfold any following folded lines
++++++++ for(var nl = li + 1; nl < lines.length; ++nl) {
++++++++ var next = lines[nl];
++++++++ if(!/\s/.test(next[0])) {
++++++++ break;
++++++++ }
++++++++ line += next;
++++++++ li = nl;
++++++++ }
++++++++
++++++++ // parse header
++++++++ match = line.match(rHeader);
++++++++ if(match) {
++++++++ var header = {name: match[1], values: []};
++++++++ var values = match[2].split(',');
++++++++ for(var vi = 0; vi < values.length; ++vi) {
++++++++ header.values.push(ltrim(values[vi]));
++++++++ }
++++++++
++++++++ // Proc-Type must be the first header
++++++++ if(!msg.procType) {
++++++++ if(header.name !== 'Proc-Type') {
++++++++ throw new Error('Invalid PEM formatted message. The first ' +
++++++++ 'encapsulated header must be "Proc-Type".');
++++++++ } else if(header.values.length !== 2) {
++++++++ throw new Error('Invalid PEM formatted message. The "Proc-Type" ' +
++++++++ 'header must have two subfields.');
++++++++ }
++++++++ msg.procType = {version: values[0], type: values[1]};
++++++++ } else if(!msg.contentDomain && header.name === 'Content-Domain') {
++++++++ // special-case Content-Domain
++++++++ msg.contentDomain = values[0] || '';
++++++++ } else if(!msg.dekInfo && header.name === 'DEK-Info') {
++++++++ // special-case DEK-Info
++++++++ if(header.values.length === 0) {
++++++++ throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
++++++++ 'header must have at least one subfield.');
++++++++ }
++++++++ msg.dekInfo = {algorithm: values[0], parameters: values[1] || null};
++++++++ } else {
++++++++ msg.headers.push(header);
++++++++ }
++++++++ }
++++++++
++++++++ ++li;
++++++++ }
++++++++
++++++++ if(msg.procType === 'ENCRYPTED' && !msg.dekInfo) {
++++++++ throw new Error('Invalid PEM formatted message. The "DEK-Info" ' +
++++++++ 'header must be present if "Proc-Type" is "ENCRYPTED".');
++++++++ }
++++++++ }
++++++++
++++++++ if(rval.length === 0) {
++++++++ throw new Error('Invalid PEM formatted message.');
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++function foldHeader(header) {
++++++++ var rval = header.name + ': ';
++++++++
++++++++ // ensure values with CRLF are folded
++++++++ var values = [];
++++++++ var insertSpace = function(match, $1) {
++++++++ return ' ' + $1;
++++++++ };
++++++++ for(var i = 0; i < header.values.length; ++i) {
++++++++ values.push(header.values[i].replace(/^(\S+\r\n)/, insertSpace));
++++++++ }
++++++++ rval += values.join(',') + '\r\n';
++++++++
++++++++ // do folding
++++++++ var length = 0;
++++++++ var candidate = -1;
++++++++ for(var i = 0; i < rval.length; ++i, ++length) {
++++++++ if(length > 65 && candidate !== -1) {
++++++++ var insert = rval[candidate];
++++++++ if(insert === ',') {
++++++++ ++candidate;
++++++++ rval = rval.substr(0, candidate) + '\r\n ' + rval.substr(candidate);
++++++++ } else {
++++++++ rval = rval.substr(0, candidate) +
++++++++ '\r\n' + insert + rval.substr(candidate + 1);
++++++++ }
++++++++ length = (i - candidate - 1);
++++++++ candidate = -1;
++++++++ ++i;
++++++++ } else if(rval[i] === ' ' || rval[i] === '\t' || rval[i] === ',') {
++++++++ candidate = i;
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++function ltrim(str) {
++++++++ return str.replace(/^\s+/, '');
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Partial implementation of PKCS#1 v2.2: RSA-OEAP
++++++++ *
++++++++ * Modified but based on the following MIT and BSD licensed code:
++++++++ *
++++++++ * https://github.com/kjur/jsjws/blob/master/rsa.js:
++++++++ *
++++++++ * The 'jsjws'(JSON Web Signature JavaScript Library) License
++++++++ *
++++++++ * Copyright (c) 2012 Kenji Urushima
++++++++ *
++++++++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++ * of this software and associated documentation files (the "Software"), to deal
++++++++ * in the Software without restriction, including without limitation the rights
++++++++ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++ * copies of the Software, and to permit persons to whom the Software is
++++++++ * furnished to do so, subject to the following conditions:
++++++++ *
++++++++ * The above copyright notice and this permission notice shall be included in
++++++++ * all copies or substantial portions of the Software.
++++++++ *
++++++++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
++++++++ * THE SOFTWARE.
++++++++ *
++++++++ * http://webrsa.cvs.sourceforge.net/viewvc/webrsa/Client/RSAES-OAEP.js?content-type=text%2Fplain:
++++++++ *
++++++++ * RSAES-OAEP.js
++++++++ * $Id: RSAES-OAEP.js,v 1.1.1.1 2003/03/19 15:37:20 ellispritchard Exp $
++++++++ * JavaScript Implementation of PKCS #1 v2.1 RSA CRYPTOGRAPHY STANDARD (RSA Laboratories, June 14, 2002)
++++++++ * Copyright (C) Ellis Pritchard, Guardian Unlimited 2003.
++++++++ * Contact: ellis@nukinetics.com
++++++++ * Distributed under the BSD License.
++++++++ *
++++++++ * Official documentation: http://www.rsa.com/rsalabs/node.asp?id=2125
++++++++ *
++++++++ * @author Evan Jones (http://evanjones.ca/)
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2013-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++require('./random');
++++++++require('./sha1');
++++++++
++++++++// shortcut for PKCS#1 API
++++++++var pkcs1 = module.exports = forge.pkcs1 = forge.pkcs1 || {};
++++++++
++++++++/**
++++++++ * Encode the given RSAES-OAEP message (M) using key, with optional label (L)
++++++++ * and seed.
++++++++ *
++++++++ * This method does not perform RSA encryption, it only encodes the message
++++++++ * using RSAES-OAEP.
++++++++ *
++++++++ * @param key the RSA key to use.
++++++++ * @param message the message to encode.
++++++++ * @param options the options to use:
++++++++ * label an optional label to use.
++++++++ * seed the seed to use.
++++++++ * md the message digest object to use, undefined for SHA-1.
++++++++ * mgf1 optional mgf1 parameters:
++++++++ * md the message digest object to use for MGF1.
++++++++ *
++++++++ * @return the encoded message bytes.
++++++++ */
++++++++pkcs1.encode_rsa_oaep = function(key, message, options) {
++++++++ // parse arguments
++++++++ var label;
++++++++ var seed;
++++++++ var md;
++++++++ var mgf1Md;
++++++++ // legacy args (label, seed, md)
++++++++ if(typeof options === 'string') {
++++++++ label = options;
++++++++ seed = arguments[3] || undefined;
++++++++ md = arguments[4] || undefined;
++++++++ } else if(options) {
++++++++ label = options.label || undefined;
++++++++ seed = options.seed || undefined;
++++++++ md = options.md || undefined;
++++++++ if(options.mgf1 && options.mgf1.md) {
++++++++ mgf1Md = options.mgf1.md;
++++++++ }
++++++++ }
++++++++
++++++++ // default OAEP to SHA-1 message digest
++++++++ if(!md) {
++++++++ md = forge.md.sha1.create();
++++++++ } else {
++++++++ md.start();
++++++++ }
++++++++
++++++++ // default MGF-1 to same as OAEP
++++++++ if(!mgf1Md) {
++++++++ mgf1Md = md;
++++++++ }
++++++++
++++++++ // compute length in bytes and check output
++++++++ var keyLength = Math.ceil(key.n.bitLength() / 8);
++++++++ var maxLength = keyLength - 2 * md.digestLength - 2;
++++++++ if(message.length > maxLength) {
++++++++ var error = new Error('RSAES-OAEP input message length is too long.');
++++++++ error.length = message.length;
++++++++ error.maxLength = maxLength;
++++++++ throw error;
++++++++ }
++++++++
++++++++ if(!label) {
++++++++ label = '';
++++++++ }
++++++++ md.update(label, 'raw');
++++++++ var lHash = md.digest();
++++++++
++++++++ var PS = '';
++++++++ var PS_length = maxLength - message.length;
++++++++ for(var i = 0; i < PS_length; i++) {
++++++++ PS += '\x00';
++++++++ }
++++++++
++++++++ var DB = lHash.getBytes() + PS + '\x01' + message;
++++++++
++++++++ if(!seed) {
++++++++ seed = forge.random.getBytes(md.digestLength);
++++++++ } else if(seed.length !== md.digestLength) {
++++++++ var error = new Error('Invalid RSAES-OAEP seed. The seed length must ' +
++++++++ 'match the digest length.');
++++++++ error.seedLength = seed.length;
++++++++ error.digestLength = md.digestLength;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var dbMask = rsa_mgf1(seed, keyLength - md.digestLength - 1, mgf1Md);
++++++++ var maskedDB = forge.util.xorBytes(DB, dbMask, DB.length);
++++++++
++++++++ var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md);
++++++++ var maskedSeed = forge.util.xorBytes(seed, seedMask, seed.length);
++++++++
++++++++ // return encoded message
++++++++ return '\x00' + maskedSeed + maskedDB;
++++++++};
++++++++
++++++++/**
++++++++ * Decode the given RSAES-OAEP encoded message (EM) using key, with optional
++++++++ * label (L).
++++++++ *
++++++++ * This method does not perform RSA decryption, it only decodes the message
++++++++ * using RSAES-OAEP.
++++++++ *
++++++++ * @param key the RSA key to use.
++++++++ * @param em the encoded message to decode.
++++++++ * @param options the options to use:
++++++++ * label an optional label to use.
++++++++ * md the message digest object to use for OAEP, undefined for SHA-1.
++++++++ * mgf1 optional mgf1 parameters:
++++++++ * md the message digest object to use for MGF1.
++++++++ *
++++++++ * @return the decoded message bytes.
++++++++ */
++++++++pkcs1.decode_rsa_oaep = function(key, em, options) {
++++++++ // parse args
++++++++ var label;
++++++++ var md;
++++++++ var mgf1Md;
++++++++ // legacy args
++++++++ if(typeof options === 'string') {
++++++++ label = options;
++++++++ md = arguments[3] || undefined;
++++++++ } else if(options) {
++++++++ label = options.label || undefined;
++++++++ md = options.md || undefined;
++++++++ if(options.mgf1 && options.mgf1.md) {
++++++++ mgf1Md = options.mgf1.md;
++++++++ }
++++++++ }
++++++++
++++++++ // compute length in bytes
++++++++ var keyLength = Math.ceil(key.n.bitLength() / 8);
++++++++
++++++++ if(em.length !== keyLength) {
++++++++ var error = new Error('RSAES-OAEP encoded message length is invalid.');
++++++++ error.length = em.length;
++++++++ error.expectedLength = keyLength;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // default OAEP to SHA-1 message digest
++++++++ if(md === undefined) {
++++++++ md = forge.md.sha1.create();
++++++++ } else {
++++++++ md.start();
++++++++ }
++++++++
++++++++ // default MGF-1 to same as OAEP
++++++++ if(!mgf1Md) {
++++++++ mgf1Md = md;
++++++++ }
++++++++
++++++++ if(keyLength < 2 * md.digestLength + 2) {
++++++++ throw new Error('RSAES-OAEP key is too short for the hash function.');
++++++++ }
++++++++
++++++++ if(!label) {
++++++++ label = '';
++++++++ }
++++++++ md.update(label, 'raw');
++++++++ var lHash = md.digest().getBytes();
++++++++
++++++++ // split the message into its parts
++++++++ var y = em.charAt(0);
++++++++ var maskedSeed = em.substring(1, md.digestLength + 1);
++++++++ var maskedDB = em.substring(1 + md.digestLength);
++++++++
++++++++ var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md);
++++++++ var seed = forge.util.xorBytes(maskedSeed, seedMask, maskedSeed.length);
++++++++
++++++++ var dbMask = rsa_mgf1(seed, keyLength - md.digestLength - 1, mgf1Md);
++++++++ var db = forge.util.xorBytes(maskedDB, dbMask, maskedDB.length);
++++++++
++++++++ var lHashPrime = db.substring(0, md.digestLength);
++++++++
++++++++ // constant time check that all values match what is expected
++++++++ var error = (y !== '\x00');
++++++++
++++++++ // constant time check lHash vs lHashPrime
++++++++ for(var i = 0; i < md.digestLength; ++i) {
++++++++ error |= (lHash.charAt(i) !== lHashPrime.charAt(i));
++++++++ }
++++++++
++++++++ // "constant time" find the 0x1 byte separating the padding (zeros) from the
++++++++ // message
++++++++ // TODO: It must be possible to do this in a better/smarter way?
++++++++ var in_ps = 1;
++++++++ var index = md.digestLength;
++++++++ for(var j = md.digestLength; j < db.length; j++) {
++++++++ var code = db.charCodeAt(j);
++++++++
++++++++ var is_0 = (code & 0x1) ^ 0x1;
++++++++
++++++++ // non-zero if not 0 or 1 in the ps section
++++++++ var error_mask = in_ps ? 0xfffe : 0x0000;
++++++++ error |= (code & error_mask);
++++++++
++++++++ // latch in_ps to zero after we find 0x1
++++++++ in_ps = in_ps & is_0;
++++++++ index += in_ps;
++++++++ }
++++++++
++++++++ if(error || db.charCodeAt(index) !== 0x1) {
++++++++ throw new Error('Invalid RSAES-OAEP padding.');
++++++++ }
++++++++
++++++++ return db.substring(index + 1);
++++++++};
++++++++
++++++++function rsa_mgf1(seed, maskLength, hash) {
++++++++ // default to SHA-1 message digest
++++++++ if(!hash) {
++++++++ hash = forge.md.sha1.create();
++++++++ }
++++++++ var t = '';
++++++++ var count = Math.ceil(maskLength / hash.digestLength);
++++++++ for(var i = 0; i < count; ++i) {
++++++++ var c = String.fromCharCode(
++++++++ (i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
++++++++ hash.start();
++++++++ hash.update(seed + c);
++++++++ t += hash.digest().getBytes();
++++++++ }
++++++++ return t.substring(0, maskLength);
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of PKCS#12.
++++++++ *
++++++++ * @author Dave Longley
++++++++ * @author Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * The ASN.1 representation of PKCS#12 is as follows
++++++++ * (see ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12-tc1.pdf for details)
++++++++ *
++++++++ * PFX ::= SEQUENCE {
++++++++ * version INTEGER {v3(3)}(v3,...),
++++++++ * authSafe ContentInfo,
++++++++ * macData MacData OPTIONAL
++++++++ * }
++++++++ *
++++++++ * MacData ::= SEQUENCE {
++++++++ * mac DigestInfo,
++++++++ * macSalt OCTET STRING,
++++++++ * iterations INTEGER DEFAULT 1
++++++++ * }
++++++++ * Note: The iterations default is for historical reasons and its use is
++++++++ * deprecated. A higher value, like 1024, is recommended.
++++++++ *
++++++++ * DigestInfo is defined in PKCS#7 as follows:
++++++++ *
++++++++ * DigestInfo ::= SEQUENCE {
++++++++ * digestAlgorithm DigestAlgorithmIdentifier,
++++++++ * digest Digest
++++++++ * }
++++++++ *
++++++++ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ *
++++++++ * The AlgorithmIdentifier contains an Object Identifier (OID) and parameters
++++++++ * for the algorithm, if any. In the case of SHA1 there is none.
++++++++ *
++++++++ * AlgorithmIdentifer ::= SEQUENCE {
++++++++ * algorithm OBJECT IDENTIFIER,
++++++++ * parameters ANY DEFINED BY algorithm OPTIONAL
++++++++ * }
++++++++ *
++++++++ * Digest ::= OCTET STRING
++++++++ *
++++++++ *
++++++++ * ContentInfo ::= SEQUENCE {
++++++++ * contentType ContentType,
++++++++ * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
++++++++ * }
++++++++ *
++++++++ * ContentType ::= OBJECT IDENTIFIER
++++++++ *
++++++++ * AuthenticatedSafe ::= SEQUENCE OF ContentInfo
++++++++ * -- Data if unencrypted
++++++++ * -- EncryptedData if password-encrypted
++++++++ * -- EnvelopedData if public key-encrypted
++++++++ *
++++++++ *
++++++++ * SafeContents ::= SEQUENCE OF SafeBag
++++++++ *
++++++++ * SafeBag ::= SEQUENCE {
++++++++ * bagId BAG-TYPE.&id ({PKCS12BagSet})
++++++++ * bagValue [0] EXPLICIT BAG-TYPE.&Type({PKCS12BagSet}{@bagId}),
++++++++ * bagAttributes SET OF PKCS12Attribute OPTIONAL
++++++++ * }
++++++++ *
++++++++ * PKCS12Attribute ::= SEQUENCE {
++++++++ * attrId ATTRIBUTE.&id ({PKCS12AttrSet}),
++++++++ * attrValues SET OF ATTRIBUTE.&Type ({PKCS12AttrSet}{@attrId})
++++++++ * } -- This type is compatible with the X.500 type 'Attribute'
++++++++ *
++++++++ * PKCS12AttrSet ATTRIBUTE ::= {
++++++++ * friendlyName | -- from PKCS #9
++++++++ * localKeyId, -- from PKCS #9
++++++++ * ... -- Other attributes are allowed
++++++++ * }
++++++++ *
++++++++ * CertBag ::= SEQUENCE {
++++++++ * certId BAG-TYPE.&id ({CertTypes}),
++++++++ * certValue [0] EXPLICIT BAG-TYPE.&Type ({CertTypes}{@certId})
++++++++ * }
++++++++ *
++++++++ * x509Certificate BAG-TYPE ::= {OCTET STRING IDENTIFIED BY {certTypes 1}}
++++++++ * -- DER-encoded X.509 certificate stored in OCTET STRING
++++++++ *
++++++++ * sdsiCertificate BAG-TYPE ::= {IA5String IDENTIFIED BY {certTypes 2}}
++++++++ * -- Base64-encoded SDSI certificate stored in IA5String
++++++++ *
++++++++ * CertTypes BAG-TYPE ::= {
++++++++ * x509Certificate |
++++++++ * sdsiCertificate,
++++++++ * ... -- For future extensions
++++++++ * }
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++require('./hmac');
++++++++require('./oids');
++++++++require('./pkcs7asn1');
++++++++require('./pbe');
++++++++require('./random');
++++++++require('./rsa');
++++++++require('./sha1');
++++++++require('./util');
++++++++require('./x509');
++++++++
++++++++// shortcut for asn.1 & PKI API
++++++++var asn1 = forge.asn1;
++++++++var pki = forge.pki;
++++++++
++++++++// shortcut for PKCS#12 API
++++++++var p12 = module.exports = forge.pkcs12 = forge.pkcs12 || {};
++++++++
++++++++var contentInfoValidator = {
++++++++ name: 'ContentInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE, // a ContentInfo
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'ContentInfo.contentType',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'contentType'
++++++++ }, {
++++++++ name: 'ContentInfo.content',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ constructed: true,
++++++++ captureAsn1: 'content'
++++++++ }]
++++++++};
++++++++
++++++++var pfxValidator = {
++++++++ name: 'PFX',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PFX.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'version'
++++++++ },
++++++++ contentInfoValidator, {
++++++++ name: 'PFX.macData',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ captureAsn1: 'mac',
++++++++ value: [{
++++++++ name: 'PFX.macData.mac',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE, // DigestInfo
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PFX.macData.mac.digestAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE, // DigestAlgorithmIdentifier
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'PFX.macData.mac.digestAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'macAlgorithm'
++++++++ }, {
++++++++ name: 'PFX.macData.mac.digestAlgorithm.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ captureAsn1: 'macAlgorithmParameters'
++++++++ }]
++++++++ }, {
++++++++ name: 'PFX.macData.mac.digest',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'macDigest'
++++++++ }]
++++++++ }, {
++++++++ name: 'PFX.macData.macSalt',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'macSalt'
++++++++ }, {
++++++++ name: 'PFX.macData.iterations',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'macIterations'
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++var safeBagValidator = {
++++++++ name: 'SafeBag',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'SafeBag.bagId',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'bagId'
++++++++ }, {
++++++++ name: 'SafeBag.bagValue',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ constructed: true,
++++++++ captureAsn1: 'bagValue'
++++++++ }, {
++++++++ name: 'SafeBag.bagAttributes',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ capture: 'bagAttributes'
++++++++ }]
++++++++};
++++++++
++++++++var attributeValidator = {
++++++++ name: 'Attribute',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'Attribute.attrId',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'oid'
++++++++ }, {
++++++++ name: 'Attribute.attrValues',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ constructed: true,
++++++++ capture: 'values'
++++++++ }]
++++++++};
++++++++
++++++++var certBagValidator = {
++++++++ name: 'CertBag',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'CertBag.certId',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'certId'
++++++++ }, {
++++++++ name: 'CertBag.certValue',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ constructed: true,
++++++++ /* So far we only support X.509 certificates (which are wrapped in
++++++++ an OCTET STRING, hence hard code that here). */
++++++++ value: [{
++++++++ name: 'CertBag.certValue[0]',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Class.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'cert'
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++/**
++++++++ * Search SafeContents structure for bags with matching attributes.
++++++++ *
++++++++ * The search can optionally be narrowed by a certain bag type.
++++++++ *
++++++++ * @param safeContents the SafeContents structure to search in.
++++++++ * @param attrName the name of the attribute to compare against.
++++++++ * @param attrValue the attribute value to search for.
++++++++ * @param [bagType] bag type to narrow search by.
++++++++ *
++++++++ * @return an array of matching bags.
++++++++ */
++++++++function _getBagsByAttribute(safeContents, attrName, attrValue, bagType) {
++++++++ var result = [];
++++++++
++++++++ for(var i = 0; i < safeContents.length; i++) {
++++++++ for(var j = 0; j < safeContents[i].safeBags.length; j++) {
++++++++ var bag = safeContents[i].safeBags[j];
++++++++ if(bagType !== undefined && bag.type !== bagType) {
++++++++ continue;
++++++++ }
++++++++ // only filter by bag type, no attribute specified
++++++++ if(attrName === null) {
++++++++ result.push(bag);
++++++++ continue;
++++++++ }
++++++++ if(bag.attributes[attrName] !== undefined &&
++++++++ bag.attributes[attrName].indexOf(attrValue) >= 0) {
++++++++ result.push(bag);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return result;
++++++++}
++++++++
++++++++/**
++++++++ * Converts a PKCS#12 PFX in ASN.1 notation into a PFX object.
++++++++ *
++++++++ * @param obj The PKCS#12 PFX in ASN.1 notation.
++++++++ * @param strict true to use strict DER decoding, false not to (default: true).
++++++++ * @param {String} password Password to decrypt with (optional).
++++++++ *
++++++++ * @return PKCS#12 PFX object.
++++++++ */
++++++++p12.pkcs12FromAsn1 = function(obj, strict, password) {
++++++++ // handle args
++++++++ if(typeof strict === 'string') {
++++++++ password = strict;
++++++++ strict = true;
++++++++ } else if(strict === undefined) {
++++++++ strict = true;
++++++++ }
++++++++
++++++++ // validate PFX and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, pfxValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#12 PFX. ' +
++++++++ 'ASN.1 object is not an PKCS#12 PFX.');
++++++++ error.errors = error;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var pfx = {
++++++++ version: capture.version.charCodeAt(0),
++++++++ safeContents: [],
++++++++
++++++++ /**
++++++++ * Gets bags with matching attributes.
++++++++ *
++++++++ * @param filter the attributes to filter by:
++++++++ * [localKeyId] the localKeyId to search for.
++++++++ * [localKeyIdHex] the localKeyId in hex to search for.
++++++++ * [friendlyName] the friendly name to search for.
++++++++ * [bagType] bag type to narrow each attribute search by.
++++++++ *
++++++++ * @return a map of attribute type to an array of matching bags or, if no
++++++++ * attribute was given but a bag type, the map key will be the
++++++++ * bag type.
++++++++ */
++++++++ getBags: function(filter) {
++++++++ var rval = {};
++++++++
++++++++ var localKeyId;
++++++++ if('localKeyId' in filter) {
++++++++ localKeyId = filter.localKeyId;
++++++++ } else if('localKeyIdHex' in filter) {
++++++++ localKeyId = forge.util.hexToBytes(filter.localKeyIdHex);
++++++++ }
++++++++
++++++++ // filter on bagType only
++++++++ if(localKeyId === undefined && !('friendlyName' in filter) &&
++++++++ 'bagType' in filter) {
++++++++ rval[filter.bagType] = _getBagsByAttribute(
++++++++ pfx.safeContents, null, null, filter.bagType);
++++++++ }
++++++++
++++++++ if(localKeyId !== undefined) {
++++++++ rval.localKeyId = _getBagsByAttribute(
++++++++ pfx.safeContents, 'localKeyId',
++++++++ localKeyId, filter.bagType);
++++++++ }
++++++++ if('friendlyName' in filter) {
++++++++ rval.friendlyName = _getBagsByAttribute(
++++++++ pfx.safeContents, 'friendlyName',
++++++++ filter.friendlyName, filter.bagType);
++++++++ }
++++++++
++++++++ return rval;
++++++++ },
++++++++
++++++++ /**
++++++++ * DEPRECATED: use getBags() instead.
++++++++ *
++++++++ * Get bags with matching friendlyName attribute.
++++++++ *
++++++++ * @param friendlyName the friendly name to search for.
++++++++ * @param [bagType] bag type to narrow search by.
++++++++ *
++++++++ * @return an array of bags with matching friendlyName attribute.
++++++++ */
++++++++ getBagsByFriendlyName: function(friendlyName, bagType) {
++++++++ return _getBagsByAttribute(
++++++++ pfx.safeContents, 'friendlyName', friendlyName, bagType);
++++++++ },
++++++++
++++++++ /**
++++++++ * DEPRECATED: use getBags() instead.
++++++++ *
++++++++ * Get bags with matching localKeyId attribute.
++++++++ *
++++++++ * @param localKeyId the localKeyId to search for.
++++++++ * @param [bagType] bag type to narrow search by.
++++++++ *
++++++++ * @return an array of bags with matching localKeyId attribute.
++++++++ */
++++++++ getBagsByLocalKeyId: function(localKeyId, bagType) {
++++++++ return _getBagsByAttribute(
++++++++ pfx.safeContents, 'localKeyId', localKeyId, bagType);
++++++++ }
++++++++ };
++++++++
++++++++ if(capture.version.charCodeAt(0) !== 3) {
++++++++ var error = new Error('PKCS#12 PFX of version other than 3 not supported.');
++++++++ error.version = capture.version.charCodeAt(0);
++++++++ throw error;
++++++++ }
++++++++
++++++++ if(asn1.derToOid(capture.contentType) !== pki.oids.data) {
++++++++ var error = new Error('Only PKCS#12 PFX in password integrity mode supported.');
++++++++ error.oid = asn1.derToOid(capture.contentType);
++++++++ throw error;
++++++++ }
++++++++
++++++++ var data = capture.content.value[0];
++++++++ if(data.tagClass !== asn1.Class.UNIVERSAL ||
++++++++ data.type !== asn1.Type.OCTETSTRING) {
++++++++ throw new Error('PKCS#12 authSafe content data is not an OCTET STRING.');
++++++++ }
++++++++ data = _decodePkcs7Data(data);
++++++++
++++++++ // check for MAC
++++++++ if(capture.mac) {
++++++++ var md = null;
++++++++ var macKeyBytes = 0;
++++++++ var macAlgorithm = asn1.derToOid(capture.macAlgorithm);
++++++++ switch(macAlgorithm) {
++++++++ case pki.oids.sha1:
++++++++ md = forge.md.sha1.create();
++++++++ macKeyBytes = 20;
++++++++ break;
++++++++ case pki.oids.sha256:
++++++++ md = forge.md.sha256.create();
++++++++ macKeyBytes = 32;
++++++++ break;
++++++++ case pki.oids.sha384:
++++++++ md = forge.md.sha384.create();
++++++++ macKeyBytes = 48;
++++++++ break;
++++++++ case pki.oids.sha512:
++++++++ md = forge.md.sha512.create();
++++++++ macKeyBytes = 64;
++++++++ break;
++++++++ case pki.oids.md5:
++++++++ md = forge.md.md5.create();
++++++++ macKeyBytes = 16;
++++++++ break;
++++++++ }
++++++++ if(md === null) {
++++++++ throw new Error('PKCS#12 uses unsupported MAC algorithm: ' + macAlgorithm);
++++++++ }
++++++++
++++++++ // verify MAC (iterations default to 1)
++++++++ var macSalt = new forge.util.ByteBuffer(capture.macSalt);
++++++++ var macIterations = (('macIterations' in capture) ?
++++++++ parseInt(forge.util.bytesToHex(capture.macIterations), 16) : 1);
++++++++ var macKey = p12.generateKey(
++++++++ password, macSalt, 3, macIterations, macKeyBytes, md);
++++++++ var mac = forge.hmac.create();
++++++++ mac.start(md, macKey);
++++++++ mac.update(data.value);
++++++++ var macValue = mac.getMac();
++++++++ if(macValue.getBytes() !== capture.macDigest) {
++++++++ throw new Error('PKCS#12 MAC could not be verified. Invalid password?');
++++++++ }
++++++++ }
++++++++
++++++++ _decodeAuthenticatedSafe(pfx, data.value, strict, password);
++++++++ return pfx;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes PKCS#7 Data. PKCS#7 (RFC 2315) defines "Data" as an OCTET STRING,
++++++++ * but it is sometimes an OCTET STRING that is composed/constructed of chunks,
++++++++ * each its own OCTET STRING. This is BER-encoding vs. DER-encoding. This
++++++++ * function transforms this corner-case into the usual simple,
++++++++ * non-composed/constructed OCTET STRING.
++++++++ *
++++++++ * This function may be moved to ASN.1 at some point to better deal with
++++++++ * more BER-encoding issues, should they arise.
++++++++ *
++++++++ * @param data the ASN.1 Data object to transform.
++++++++ */
++++++++function _decodePkcs7Data(data) {
++++++++ // handle special case of "chunked" data content: an octet string composed
++++++++ // of other octet strings
++++++++ if(data.composed || data.constructed) {
++++++++ var value = forge.util.createBuffer();
++++++++ for(var i = 0; i < data.value.length; ++i) {
++++++++ value.putBytes(data.value[i].value);
++++++++ }
++++++++ data.composed = data.constructed = false;
++++++++ data.value = value.getBytes();
++++++++ }
++++++++ return data;
++++++++}
++++++++
++++++++/**
++++++++ * Decode PKCS#12 AuthenticatedSafe (BER encoded) into PFX object.
++++++++ *
++++++++ * The AuthenticatedSafe is a BER-encoded SEQUENCE OF ContentInfo.
++++++++ *
++++++++ * @param pfx The PKCS#12 PFX object to fill.
++++++++ * @param {String} authSafe BER-encoded AuthenticatedSafe.
++++++++ * @param strict true to use strict DER decoding, false not to.
++++++++ * @param {String} password Password to decrypt with (optional).
++++++++ */
++++++++function _decodeAuthenticatedSafe(pfx, authSafe, strict, password) {
++++++++ authSafe = asn1.fromDer(authSafe, strict); /* actually it's BER encoded */
++++++++
++++++++ if(authSafe.tagClass !== asn1.Class.UNIVERSAL ||
++++++++ authSafe.type !== asn1.Type.SEQUENCE ||
++++++++ authSafe.constructed !== true) {
++++++++ throw new Error('PKCS#12 AuthenticatedSafe expected to be a ' +
++++++++ 'SEQUENCE OF ContentInfo');
++++++++ }
++++++++
++++++++ for(var i = 0; i < authSafe.value.length; i++) {
++++++++ var contentInfo = authSafe.value[i];
++++++++
++++++++ // validate contentInfo and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(contentInfo, contentInfoValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read ContentInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var obj = {
++++++++ encrypted: false
++++++++ };
++++++++ var safeContents = null;
++++++++ var data = capture.content.value[0];
++++++++ switch(asn1.derToOid(capture.contentType)) {
++++++++ case pki.oids.data:
++++++++ if(data.tagClass !== asn1.Class.UNIVERSAL ||
++++++++ data.type !== asn1.Type.OCTETSTRING) {
++++++++ throw new Error('PKCS#12 SafeContents Data is not an OCTET STRING.');
++++++++ }
++++++++ safeContents = _decodePkcs7Data(data).value;
++++++++ break;
++++++++ case pki.oids.encryptedData:
++++++++ safeContents = _decryptSafeContents(data, password);
++++++++ obj.encrypted = true;
++++++++ break;
++++++++ default:
++++++++ var error = new Error('Unsupported PKCS#12 contentType.');
++++++++ error.contentType = asn1.derToOid(capture.contentType);
++++++++ throw error;
++++++++ }
++++++++
++++++++ obj.safeBags = _decodeSafeContents(safeContents, strict, password);
++++++++ pfx.safeContents.push(obj);
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Decrypt PKCS#7 EncryptedData structure.
++++++++ *
++++++++ * @param data ASN.1 encoded EncryptedContentInfo object.
++++++++ * @param password The user-provided password.
++++++++ *
++++++++ * @return The decrypted SafeContents (ASN.1 object).
++++++++ */
++++++++function _decryptSafeContents(data, password) {
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(
++++++++ data, forge.pkcs7.asn1.encryptedDataValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read EncryptedContentInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var oid = asn1.derToOid(capture.contentType);
++++++++ if(oid !== pki.oids.data) {
++++++++ var error = new Error(
++++++++ 'PKCS#12 EncryptedContentInfo ContentType is not Data.');
++++++++ error.oid = oid;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get cipher
++++++++ oid = asn1.derToOid(capture.encAlgorithm);
++++++++ var cipher = pki.pbe.getCipher(oid, capture.encParameter, password);
++++++++
++++++++ // get encrypted data
++++++++ var encryptedContentAsn1 = _decodePkcs7Data(capture.encryptedContentAsn1);
++++++++ var encrypted = forge.util.createBuffer(encryptedContentAsn1.value);
++++++++
++++++++ cipher.update(encrypted);
++++++++ if(!cipher.finish()) {
++++++++ throw new Error('Failed to decrypt PKCS#12 SafeContents.');
++++++++ }
++++++++
++++++++ return cipher.output.getBytes();
++++++++}
++++++++
++++++++/**
++++++++ * Decode PKCS#12 SafeContents (BER-encoded) into array of Bag objects.
++++++++ *
++++++++ * The safeContents is a BER-encoded SEQUENCE OF SafeBag.
++++++++ *
++++++++ * @param {String} safeContents BER-encoded safeContents.
++++++++ * @param strict true to use strict DER decoding, false not to.
++++++++ * @param {String} password Password to decrypt with (optional).
++++++++ *
++++++++ * @return {Array} Array of Bag objects.
++++++++ */
++++++++function _decodeSafeContents(safeContents, strict, password) {
++++++++ // if strict and no safe contents, return empty safes
++++++++ if(!strict && safeContents.length === 0) {
++++++++ return [];
++++++++ }
++++++++
++++++++ // actually it's BER-encoded
++++++++ safeContents = asn1.fromDer(safeContents, strict);
++++++++
++++++++ if(safeContents.tagClass !== asn1.Class.UNIVERSAL ||
++++++++ safeContents.type !== asn1.Type.SEQUENCE ||
++++++++ safeContents.constructed !== true) {
++++++++ throw new Error(
++++++++ 'PKCS#12 SafeContents expected to be a SEQUENCE OF SafeBag.');
++++++++ }
++++++++
++++++++ var res = [];
++++++++ for(var i = 0; i < safeContents.value.length; i++) {
++++++++ var safeBag = safeContents.value[i];
++++++++
++++++++ // validate SafeBag and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(safeBag, safeBagValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read SafeBag.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ /* Create bag object and push to result array. */
++++++++ var bag = {
++++++++ type: asn1.derToOid(capture.bagId),
++++++++ attributes: _decodeBagAttributes(capture.bagAttributes)
++++++++ };
++++++++ res.push(bag);
++++++++
++++++++ var validator, decoder;
++++++++ var bagAsn1 = capture.bagValue.value[0];
++++++++ switch(bag.type) {
++++++++ case pki.oids.pkcs8ShroudedKeyBag:
++++++++ /* bagAsn1 has a EncryptedPrivateKeyInfo, which we need to decrypt.
++++++++ Afterwards we can handle it like a keyBag,
++++++++ which is a PrivateKeyInfo. */
++++++++ bagAsn1 = pki.decryptPrivateKeyInfo(bagAsn1, password);
++++++++ if(bagAsn1 === null) {
++++++++ throw new Error(
++++++++ 'Unable to decrypt PKCS#8 ShroudedKeyBag, wrong password?');
++++++++ }
++++++++
++++++++ /* fall through */
++++++++ case pki.oids.keyBag:
++++++++ /* A PKCS#12 keyBag is a simple PrivateKeyInfo as understood by our
++++++++ PKI module, hence we don't have to do validation/capturing here,
++++++++ just pass what we already got. */
++++++++ try {
++++++++ bag.key = pki.privateKeyFromAsn1(bagAsn1);
++++++++ } catch(e) {
++++++++ // ignore unknown key type, pass asn1 value
++++++++ bag.key = null;
++++++++ bag.asn1 = bagAsn1;
++++++++ }
++++++++ continue; /* Nothing more to do. */
++++++++
++++++++ case pki.oids.certBag:
++++++++ /* A PKCS#12 certBag can wrap both X.509 and sdsi certificates.
++++++++ Therefore put the SafeBag content through another validator to
++++++++ capture the fields. Afterwards check & store the results. */
++++++++ validator = certBagValidator;
++++++++ decoder = function() {
++++++++ if(asn1.derToOid(capture.certId) !== pki.oids.x509Certificate) {
++++++++ var error = new Error(
++++++++ 'Unsupported certificate type, only X.509 supported.');
++++++++ error.oid = asn1.derToOid(capture.certId);
++++++++ throw error;
++++++++ }
++++++++
++++++++ // true=produce cert hash
++++++++ var certAsn1 = asn1.fromDer(capture.cert, strict);
++++++++ try {
++++++++ bag.cert = pki.certificateFromAsn1(certAsn1, true);
++++++++ } catch(e) {
++++++++ // ignore unknown cert type, pass asn1 value
++++++++ bag.cert = null;
++++++++ bag.asn1 = certAsn1;
++++++++ }
++++++++ };
++++++++ break;
++++++++
++++++++ default:
++++++++ var error = new Error('Unsupported PKCS#12 SafeBag type.');
++++++++ error.oid = bag.type;
++++++++ throw error;
++++++++ }
++++++++
++++++++ /* Validate SafeBag value (i.e. CertBag, etc.) and capture data if needed. */
++++++++ if(validator !== undefined &&
++++++++ !asn1.validate(bagAsn1, validator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#12 ' + validator.name);
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ /* Call decoder function from above to store the results. */
++++++++ decoder();
++++++++ }
++++++++
++++++++ return res;
++++++++}
++++++++
++++++++/**
++++++++ * Decode PKCS#12 SET OF PKCS12Attribute into JavaScript object.
++++++++ *
++++++++ * @param attributes SET OF PKCS12Attribute (ASN.1 object).
++++++++ *
++++++++ * @return the decoded attributes.
++++++++ */
++++++++function _decodeBagAttributes(attributes) {
++++++++ var decodedAttrs = {};
++++++++
++++++++ if(attributes !== undefined) {
++++++++ for(var i = 0; i < attributes.length; ++i) {
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(attributes[i], attributeValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#12 BagAttribute.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var oid = asn1.derToOid(capture.oid);
++++++++ if(pki.oids[oid] === undefined) {
++++++++ // unsupported attribute type, ignore.
++++++++ continue;
++++++++ }
++++++++
++++++++ decodedAttrs[pki.oids[oid]] = [];
++++++++ for(var j = 0; j < capture.values.length; ++j) {
++++++++ decodedAttrs[pki.oids[oid]].push(capture.values[j].value);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return decodedAttrs;
++++++++}
++++++++
++++++++/**
++++++++ * Wraps a private key and certificate in a PKCS#12 PFX wrapper. If a
++++++++ * password is provided then the private key will be encrypted.
++++++++ *
++++++++ * An entire certificate chain may also be included. To do this, pass
++++++++ * an array for the "cert" parameter where the first certificate is
++++++++ * the one that is paired with the private key and each subsequent one
++++++++ * verifies the previous one. The certificates may be in PEM format or
++++++++ * have been already parsed by Forge.
++++++++ *
++++++++ * @todo implement password-based-encryption for the whole package
++++++++ *
++++++++ * @param key the private key.
++++++++ * @param cert the certificate (may be an array of certificates in order
++++++++ * to specify a certificate chain).
++++++++ * @param password the password to use, null for none.
++++++++ * @param options:
++++++++ * algorithm the encryption algorithm to use
++++++++ * ('aes128', 'aes192', 'aes256', '3des'), defaults to 'aes128'.
++++++++ * count the iteration count to use.
++++++++ * saltSize the salt size to use.
++++++++ * useMac true to include a MAC, false not to, defaults to true.
++++++++ * localKeyId the local key ID to use, in hex.
++++++++ * friendlyName the friendly name to use.
++++++++ * generateLocalKeyId true to generate a random local key ID,
++++++++ * false not to, defaults to true.
++++++++ *
++++++++ * @return the PKCS#12 PFX ASN.1 object.
++++++++ */
++++++++p12.toPkcs12Asn1 = function(key, cert, password, options) {
++++++++ // set default options
++++++++ options = options || {};
++++++++ options.saltSize = options.saltSize || 8;
++++++++ options.count = options.count || 2048;
++++++++ options.algorithm = options.algorithm || options.encAlgorithm || 'aes128';
++++++++ if(!('useMac' in options)) {
++++++++ options.useMac = true;
++++++++ }
++++++++ if(!('localKeyId' in options)) {
++++++++ options.localKeyId = null;
++++++++ }
++++++++ if(!('generateLocalKeyId' in options)) {
++++++++ options.generateLocalKeyId = true;
++++++++ }
++++++++
++++++++ var localKeyId = options.localKeyId;
++++++++ var bagAttrs;
++++++++ if(localKeyId !== null) {
++++++++ localKeyId = forge.util.hexToBytes(localKeyId);
++++++++ } else if(options.generateLocalKeyId) {
++++++++ // use SHA-1 of paired cert, if available
++++++++ if(cert) {
++++++++ var pairedCert = forge.util.isArray(cert) ? cert[0] : cert;
++++++++ if(typeof pairedCert === 'string') {
++++++++ pairedCert = pki.certificateFromPem(pairedCert);
++++++++ }
++++++++ var sha1 = forge.md.sha1.create();
++++++++ sha1.update(asn1.toDer(pki.certificateToAsn1(pairedCert)).getBytes());
++++++++ localKeyId = sha1.digest().getBytes();
++++++++ } else {
++++++++ // FIXME: consider using SHA-1 of public key (which can be generated
++++++++ // from private key components), see: cert.generateSubjectKeyIdentifier
++++++++ // generate random bytes
++++++++ localKeyId = forge.random.getBytes(20);
++++++++ }
++++++++ }
++++++++
++++++++ var attrs = [];
++++++++ if(localKeyId !== null) {
++++++++ attrs.push(
++++++++ // localKeyID
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // attrId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.localKeyId).getBytes()),
++++++++ // attrValues
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ localKeyId)
++++++++ ])
++++++++ ]));
++++++++ }
++++++++ if('friendlyName' in options) {
++++++++ attrs.push(
++++++++ // friendlyName
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // attrId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.friendlyName).getBytes()),
++++++++ // attrValues
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BMPSTRING, false,
++++++++ options.friendlyName)
++++++++ ])
++++++++ ]));
++++++++ }
++++++++
++++++++ if(attrs.length > 0) {
++++++++ bagAttrs = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, attrs);
++++++++ }
++++++++
++++++++ // collect contents for AuthenticatedSafe
++++++++ var contents = [];
++++++++
++++++++ // create safe bag(s) for certificate chain
++++++++ var chain = [];
++++++++ if(cert !== null) {
++++++++ if(forge.util.isArray(cert)) {
++++++++ chain = cert;
++++++++ } else {
++++++++ chain = [cert];
++++++++ }
++++++++ }
++++++++
++++++++ var certSafeBags = [];
++++++++ for(var i = 0; i < chain.length; ++i) {
++++++++ // convert cert from PEM as necessary
++++++++ cert = chain[i];
++++++++ if(typeof cert === 'string') {
++++++++ cert = pki.certificateFromPem(cert);
++++++++ }
++++++++
++++++++ // SafeBag
++++++++ var certBagAttrs = (i === 0) ? bagAttrs : undefined;
++++++++ var certAsn1 = pki.certificateToAsn1(cert);
++++++++ var certSafeBag =
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // bagId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.certBag).getBytes()),
++++++++ // bagValue
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ // CertBag
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // certId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.x509Certificate).getBytes()),
++++++++ // certValue (x509Certificate)
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ asn1.toDer(certAsn1).getBytes())
++++++++ ])])]),
++++++++ // bagAttributes (OPTIONAL)
++++++++ certBagAttrs
++++++++ ]);
++++++++ certSafeBags.push(certSafeBag);
++++++++ }
++++++++
++++++++ if(certSafeBags.length > 0) {
++++++++ // SafeContents
++++++++ var certSafeContents = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, certSafeBags);
++++++++
++++++++ // ContentInfo
++++++++ var certCI =
++++++++ // PKCS#7 ContentInfo
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // contentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ // OID for the content type is 'data'
++++++++ asn1.oidToDer(pki.oids.data).getBytes()),
++++++++ // content
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ asn1.toDer(certSafeContents).getBytes())
++++++++ ])
++++++++ ]);
++++++++ contents.push(certCI);
++++++++ }
++++++++
++++++++ // create safe contents for private key
++++++++ var keyBag = null;
++++++++ if(key !== null) {
++++++++ // SafeBag
++++++++ var pkAsn1 = pki.wrapRsaPrivateKey(pki.privateKeyToAsn1(key));
++++++++ if(password === null) {
++++++++ // no encryption
++++++++ keyBag = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // bagId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.keyBag).getBytes()),
++++++++ // bagValue
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ // PrivateKeyInfo
++++++++ pkAsn1
++++++++ ]),
++++++++ // bagAttributes (OPTIONAL)
++++++++ bagAttrs
++++++++ ]);
++++++++ } else {
++++++++ // encrypted PrivateKeyInfo
++++++++ keyBag = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // bagId
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.pkcs8ShroudedKeyBag).getBytes()),
++++++++ // bagValue
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ // EncryptedPrivateKeyInfo
++++++++ pki.encryptPrivateKeyInfo(pkAsn1, password, options)
++++++++ ]),
++++++++ // bagAttributes (OPTIONAL)
++++++++ bagAttrs
++++++++ ]);
++++++++ }
++++++++
++++++++ // SafeContents
++++++++ var keySafeContents =
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [keyBag]);
++++++++
++++++++ // ContentInfo
++++++++ var keyCI =
++++++++ // PKCS#7 ContentInfo
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // contentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ // OID for the content type is 'data'
++++++++ asn1.oidToDer(pki.oids.data).getBytes()),
++++++++ // content
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ asn1.toDer(keySafeContents).getBytes())
++++++++ ])
++++++++ ]);
++++++++ contents.push(keyCI);
++++++++ }
++++++++
++++++++ // create AuthenticatedSafe by stringing together the contents
++++++++ var safe = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, contents);
++++++++
++++++++ var macData;
++++++++ if(options.useMac) {
++++++++ // MacData
++++++++ var sha1 = forge.md.sha1.create();
++++++++ var macSalt = new forge.util.ByteBuffer(
++++++++ forge.random.getBytes(options.saltSize));
++++++++ var count = options.count;
++++++++ // 160-bit key
++++++++ var key = p12.generateKey(password, macSalt, 3, count, 20);
++++++++ var mac = forge.hmac.create();
++++++++ mac.start(sha1, key);
++++++++ mac.update(asn1.toDer(safe).getBytes());
++++++++ var macValue = mac.getMac();
++++++++ macData = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // mac DigestInfo
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // digestAlgorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm = SHA-1
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.sha1).getBytes()),
++++++++ // parameters = Null
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]),
++++++++ // digest
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING,
++++++++ false, macValue.getBytes())
++++++++ ]),
++++++++ // macSalt OCTET STRING
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, macSalt.getBytes()),
++++++++ // iterations INTEGER (XXX: Only support count < 65536)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(count).getBytes()
++++++++ )
++++++++ ]);
++++++++ }
++++++++
++++++++ // PFX
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version (3)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(3).getBytes()),
++++++++ // PKCS#7 ContentInfo
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // contentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ // OID for the content type is 'data'
++++++++ asn1.oidToDer(pki.oids.data).getBytes()),
++++++++ // content
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ asn1.toDer(safe).getBytes())
++++++++ ])
++++++++ ]),
++++++++ macData
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Derives a PKCS#12 key.
++++++++ *
++++++++ * @param password the password to derive the key material from, null or
++++++++ * undefined for none.
++++++++ * @param salt the salt, as a ByteBuffer, to use.
++++++++ * @param id the PKCS#12 ID byte (1 = key material, 2 = IV, 3 = MAC).
++++++++ * @param iter the iteration count.
++++++++ * @param n the number of bytes to derive from the password.
++++++++ * @param md the message digest to use, defaults to SHA-1.
++++++++ *
++++++++ * @return a ByteBuffer with the bytes derived from the password.
++++++++ */
++++++++p12.generateKey = forge.pbe.generatePkcs12Key;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of PKCS#7 v1.5.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ * Copyright (c) 2012-2015 Digital Bazaar, Inc.
++++++++ *
++++++++ * Currently this implementation only supports ContentType of EnvelopedData,
++++++++ * EncryptedData, or SignedData at the root level. The top level elements may
++++++++ * contain only a ContentInfo of ContentType Data, i.e. plain data. Further
++++++++ * nesting is not (yet) supported.
++++++++ *
++++++++ * The Forge validators for PKCS #7's ASN.1 structures are available from
++++++++ * a separate file pkcs7asn1.js, since those are referenced from other
++++++++ * PKCS standards like PKCS #12.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./asn1');
++++++++require('./des');
++++++++require('./oids');
++++++++require('./pem');
++++++++require('./pkcs7asn1');
++++++++require('./random');
++++++++require('./util');
++++++++require('./x509');
++++++++
++++++++// shortcut for ASN.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++// shortcut for PKCS#7 API
++++++++var p7 = module.exports = forge.pkcs7 = forge.pkcs7 || {};
++++++++
++++++++/**
++++++++ * Converts a PKCS#7 message from PEM format.
++++++++ *
++++++++ * @param pem the PEM-formatted PKCS#7 message.
++++++++ *
++++++++ * @return the PKCS#7 message.
++++++++ */
++++++++p7.messageFromPem = function(pem) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'PKCS7') {
++++++++ var error = new Error('Could not convert PKCS#7 message from PEM; PEM ' +
++++++++ 'header type is not "PKCS#7".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert PKCS#7 message from PEM; PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ var obj = asn1.fromDer(msg.body);
++++++++
++++++++ return p7.messageFromAsn1(obj);
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#7 message to PEM format.
++++++++ *
++++++++ * @param msg The PKCS#7 message object
++++++++ * @param maxline The maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return The PEM-formatted PKCS#7 message.
++++++++ */
++++++++p7.messageToPem = function(msg, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var pemObj = {
++++++++ type: 'PKCS7',
++++++++ body: asn1.toDer(msg.toAsn1()).getBytes()
++++++++ };
++++++++ return forge.pem.encode(pemObj, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#7 message from an ASN.1 object.
++++++++ *
++++++++ * @param obj the ASN.1 representation of a ContentInfo.
++++++++ *
++++++++ * @return the PKCS#7 message.
++++++++ */
++++++++p7.messageFromAsn1 = function(obj) {
++++++++ // validate root level ContentInfo and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, p7.asn1.contentInfoValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#7 message. ' +
++++++++ 'ASN.1 object is not an PKCS#7 ContentInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var contentType = asn1.derToOid(capture.contentType);
++++++++ var msg;
++++++++
++++++++ switch(contentType) {
++++++++ case forge.pki.oids.envelopedData:
++++++++ msg = p7.createEnvelopedData();
++++++++ break;
++++++++
++++++++ case forge.pki.oids.encryptedData:
++++++++ msg = p7.createEncryptedData();
++++++++ break;
++++++++
++++++++ case forge.pki.oids.signedData:
++++++++ msg = p7.createSignedData();
++++++++ break;
++++++++
++++++++ default:
++++++++ throw new Error('Cannot read PKCS#7 message. ContentType with OID ' +
++++++++ contentType + ' is not (yet) supported.');
++++++++ }
++++++++
++++++++ msg.fromAsn1(capture.content.value[0]);
++++++++ return msg;
++++++++};
++++++++
++++++++p7.createSignedData = function() {
++++++++ var msg = null;
++++++++ msg = {
++++++++ type: forge.pki.oids.signedData,
++++++++ version: 1,
++++++++ certificates: [],
++++++++ crls: [],
++++++++ // TODO: add json-formatted signer stuff here?
++++++++ signers: [],
++++++++ // populated during sign()
++++++++ digestAlgorithmIdentifiers: [],
++++++++ contentInfo: null,
++++++++ signerInfos: [],
++++++++
++++++++ fromAsn1: function(obj) {
++++++++ // validate SignedData content block and capture data.
++++++++ _fromAsn1(msg, obj, p7.asn1.signedDataValidator);
++++++++ msg.certificates = [];
++++++++ msg.crls = [];
++++++++ msg.digestAlgorithmIdentifiers = [];
++++++++ msg.contentInfo = null;
++++++++ msg.signerInfos = [];
++++++++
++++++++ if(msg.rawCapture.certificates) {
++++++++ var certs = msg.rawCapture.certificates.value;
++++++++ for(var i = 0; i < certs.length; ++i) {
++++++++ msg.certificates.push(forge.pki.certificateFromAsn1(certs[i]));
++++++++ }
++++++++ }
++++++++
++++++++ // TODO: parse crls
++++++++ },
++++++++
++++++++ toAsn1: function() {
++++++++ // degenerate case with no content
++++++++ if(!msg.contentInfo) {
++++++++ msg.sign();
++++++++ }
++++++++
++++++++ var certs = [];
++++++++ for(var i = 0; i < msg.certificates.length; ++i) {
++++++++ certs.push(forge.pki.certificateToAsn1(msg.certificates[i]));
++++++++ }
++++++++
++++++++ var crls = [];
++++++++ // TODO: implement CRLs
++++++++
++++++++ // [0] SignedData
++++++++ var signedData = asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Version
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(msg.version).getBytes()),
++++++++ // DigestAlgorithmIdentifiers
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SET, true,
++++++++ msg.digestAlgorithmIdentifiers),
++++++++ // ContentInfo
++++++++ msg.contentInfo
++++++++ ])
++++++++ ]);
++++++++ if(certs.length > 0) {
++++++++ // [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL
++++++++ signedData.value[0].value.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, certs));
++++++++ }
++++++++ if(crls.length > 0) {
++++++++ // [1] IMPLICIT CertificateRevocationLists OPTIONAL
++++++++ signedData.value[0].value.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, crls));
++++++++ }
++++++++ // SignerInfos
++++++++ signedData.value[0].value.push(
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true,
++++++++ msg.signerInfos));
++++++++
++++++++ // ContentInfo
++++++++ return asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // ContentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(msg.type).getBytes()),
++++++++ // [0] SignedData
++++++++ signedData
++++++++ ]);
++++++++ },
++++++++
++++++++ /**
++++++++ * Add (another) entity to list of signers.
++++++++ *
++++++++ * Note: If authenticatedAttributes are provided, then, per RFC 2315,
++++++++ * they must include at least two attributes: content type and
++++++++ * message digest. The message digest attribute value will be
++++++++ * auto-calculated during signing and will be ignored if provided.
++++++++ *
++++++++ * Here's an example of providing these two attributes:
++++++++ *
++++++++ * forge.pkcs7.createSignedData();
++++++++ * p7.addSigner({
++++++++ * issuer: cert.issuer.attributes,
++++++++ * serialNumber: cert.serialNumber,
++++++++ * key: privateKey,
++++++++ * digestAlgorithm: forge.pki.oids.sha1,
++++++++ * authenticatedAttributes: [{
++++++++ * type: forge.pki.oids.contentType,
++++++++ * value: forge.pki.oids.data
++++++++ * }, {
++++++++ * type: forge.pki.oids.messageDigest
++++++++ * }]
++++++++ * });
++++++++ *
++++++++ * TODO: Support [subjectKeyIdentifier] as signer's ID.
++++++++ *
++++++++ * @param signer the signer information:
++++++++ * key the signer's private key.
++++++++ * [certificate] a certificate containing the public key
++++++++ * associated with the signer's private key; use this option as
++++++++ * an alternative to specifying signer.issuer and
++++++++ * signer.serialNumber.
++++++++ * [issuer] the issuer attributes (eg: cert.issuer.attributes).
++++++++ * [serialNumber] the signer's certificate's serial number in
++++++++ * hexadecimal (eg: cert.serialNumber).
++++++++ * [digestAlgorithm] the message digest OID, as a string, to use
++++++++ * (eg: forge.pki.oids.sha1).
++++++++ * [authenticatedAttributes] an optional array of attributes
++++++++ * to also sign along with the content.
++++++++ */
++++++++ addSigner: function(signer) {
++++++++ var issuer = signer.issuer;
++++++++ var serialNumber = signer.serialNumber;
++++++++ if(signer.certificate) {
++++++++ var cert = signer.certificate;
++++++++ if(typeof cert === 'string') {
++++++++ cert = forge.pki.certificateFromPem(cert);
++++++++ }
++++++++ issuer = cert.issuer.attributes;
++++++++ serialNumber = cert.serialNumber;
++++++++ }
++++++++ var key = signer.key;
++++++++ if(!key) {
++++++++ throw new Error(
++++++++ 'Could not add PKCS#7 signer; no private key specified.');
++++++++ }
++++++++ if(typeof key === 'string') {
++++++++ key = forge.pki.privateKeyFromPem(key);
++++++++ }
++++++++
++++++++ // ensure OID known for digest algorithm
++++++++ var digestAlgorithm = signer.digestAlgorithm || forge.pki.oids.sha1;
++++++++ switch(digestAlgorithm) {
++++++++ case forge.pki.oids.sha1:
++++++++ case forge.pki.oids.sha256:
++++++++ case forge.pki.oids.sha384:
++++++++ case forge.pki.oids.sha512:
++++++++ case forge.pki.oids.md5:
++++++++ break;
++++++++ default:
++++++++ throw new Error(
++++++++ 'Could not add PKCS#7 signer; unknown message digest algorithm: ' +
++++++++ digestAlgorithm);
++++++++ }
++++++++
++++++++ // if authenticatedAttributes is present, then the attributes
++++++++ // must contain at least PKCS #9 content-type and message-digest
++++++++ var authenticatedAttributes = signer.authenticatedAttributes || [];
++++++++ if(authenticatedAttributes.length > 0) {
++++++++ var contentType = false;
++++++++ var messageDigest = false;
++++++++ for(var i = 0; i < authenticatedAttributes.length; ++i) {
++++++++ var attr = authenticatedAttributes[i];
++++++++ if(!contentType && attr.type === forge.pki.oids.contentType) {
++++++++ contentType = true;
++++++++ if(messageDigest) {
++++++++ break;
++++++++ }
++++++++ continue;
++++++++ }
++++++++ if(!messageDigest && attr.type === forge.pki.oids.messageDigest) {
++++++++ messageDigest = true;
++++++++ if(contentType) {
++++++++ break;
++++++++ }
++++++++ continue;
++++++++ }
++++++++ }
++++++++
++++++++ if(!contentType || !messageDigest) {
++++++++ throw new Error('Invalid signer.authenticatedAttributes. If ' +
++++++++ 'signer.authenticatedAttributes is specified, then it must ' +
++++++++ 'contain at least two attributes, PKCS #9 content-type and ' +
++++++++ 'PKCS #9 message-digest.');
++++++++ }
++++++++ }
++++++++
++++++++ msg.signers.push({
++++++++ key: key,
++++++++ version: 1,
++++++++ issuer: issuer,
++++++++ serialNumber: serialNumber,
++++++++ digestAlgorithm: digestAlgorithm,
++++++++ signatureAlgorithm: forge.pki.oids.rsaEncryption,
++++++++ signature: null,
++++++++ authenticatedAttributes: authenticatedAttributes,
++++++++ unauthenticatedAttributes: []
++++++++ });
++++++++ },
++++++++
++++++++ /**
++++++++ * Signs the content.
++++++++ * @param options Options to apply when signing:
++++++++ * [detached] boolean. If signing should be done in detached mode. Defaults to false.
++++++++ */
++++++++ sign: function(options) {
++++++++ options = options || {};
++++++++ // auto-generate content info
++++++++ if(typeof msg.content !== 'object' || msg.contentInfo === null) {
++++++++ // use Data ContentInfo
++++++++ msg.contentInfo = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // ContentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(forge.pki.oids.data).getBytes())
++++++++ ]);
++++++++
++++++++ // add actual content, if present
++++++++ if('content' in msg) {
++++++++ var content;
++++++++ if(msg.content instanceof forge.util.ByteBuffer) {
++++++++ content = msg.content.bytes();
++++++++ } else if(typeof msg.content === 'string') {
++++++++ content = forge.util.encodeUtf8(msg.content);
++++++++ }
++++++++
++++++++ if (options.detached) {
++++++++ msg.detachedContent = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, content);
++++++++ } else {
++++++++ msg.contentInfo.value.push(
++++++++ // [0] EXPLICIT content
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ content)
++++++++ ]));
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // no signers, return early (degenerate case for certificate container)
++++++++ if(msg.signers.length === 0) {
++++++++ return;
++++++++ }
++++++++
++++++++ // generate digest algorithm identifiers
++++++++ var mds = addDigestAlgorithmIds();
++++++++
++++++++ // generate signerInfos
++++++++ addSignerInfos(mds);
++++++++ },
++++++++
++++++++ verify: function() {
++++++++ throw new Error('PKCS#7 signature verification not yet implemented.');
++++++++ },
++++++++
++++++++ /**
++++++++ * Add a certificate.
++++++++ *
++++++++ * @param cert the certificate to add.
++++++++ */
++++++++ addCertificate: function(cert) {
++++++++ // convert from PEM
++++++++ if(typeof cert === 'string') {
++++++++ cert = forge.pki.certificateFromPem(cert);
++++++++ }
++++++++ msg.certificates.push(cert);
++++++++ },
++++++++
++++++++ /**
++++++++ * Add a certificate revokation list.
++++++++ *
++++++++ * @param crl the certificate revokation list to add.
++++++++ */
++++++++ addCertificateRevokationList: function(crl) {
++++++++ throw new Error('PKCS#7 CRL support not yet implemented.');
++++++++ }
++++++++ };
++++++++ return msg;
++++++++
++++++++ function addDigestAlgorithmIds() {
++++++++ var mds = {};
++++++++
++++++++ for(var i = 0; i < msg.signers.length; ++i) {
++++++++ var signer = msg.signers[i];
++++++++ var oid = signer.digestAlgorithm;
++++++++ if(!(oid in mds)) {
++++++++ // content digest
++++++++ mds[oid] = forge.md[forge.pki.oids[oid]].create();
++++++++ }
++++++++ if(signer.authenticatedAttributes.length === 0) {
++++++++ // no custom attributes to digest; use content message digest
++++++++ signer.md = mds[oid];
++++++++ } else {
++++++++ // custom attributes to be digested; use own message digest
++++++++ // TODO: optimize to just copy message digest state if that
++++++++ // feature is ever supported with message digests
++++++++ signer.md = forge.md[forge.pki.oids[oid]].create();
++++++++ }
++++++++ }
++++++++
++++++++ // add unique digest algorithm identifiers
++++++++ msg.digestAlgorithmIdentifiers = [];
++++++++ for(var oid in mds) {
++++++++ msg.digestAlgorithmIdentifiers.push(
++++++++ // AlgorithmIdentifier
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(oid).getBytes()),
++++++++ // parameters (null)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]));
++++++++ }
++++++++
++++++++ return mds;
++++++++ }
++++++++
++++++++ function addSignerInfos(mds) {
++++++++ var content;
++++++++
++++++++ if (msg.detachedContent) {
++++++++ // Signature has been made in detached mode.
++++++++ content = msg.detachedContent;
++++++++ } else {
++++++++ // Note: ContentInfo is a SEQUENCE with 2 values, second value is
++++++++ // the content field and is optional for a ContentInfo but required here
++++++++ // since signers are present
++++++++ // get ContentInfo content
++++++++ content = msg.contentInfo.value[1];
++++++++ // skip [0] EXPLICIT content wrapper
++++++++ content = content.value[0];
++++++++ }
++++++++
++++++++ if(!content) {
++++++++ throw new Error(
++++++++ 'Could not sign PKCS#7 message; there is no content to sign.');
++++++++ }
++++++++
++++++++ // get ContentInfo content type
++++++++ var contentType = asn1.derToOid(msg.contentInfo.value[0].value);
++++++++
++++++++ // serialize content
++++++++ var bytes = asn1.toDer(content);
++++++++
++++++++ // skip identifier and length per RFC 2315 9.3
++++++++ // skip identifier (1 byte)
++++++++ bytes.getByte();
++++++++ // read and discard length bytes
++++++++ asn1.getBerValueLength(bytes);
++++++++ bytes = bytes.getBytes();
++++++++
++++++++ // digest content DER value bytes
++++++++ for(var oid in mds) {
++++++++ mds[oid].start().update(bytes);
++++++++ }
++++++++
++++++++ // sign content
++++++++ var signingTime = new Date();
++++++++ for(var i = 0; i < msg.signers.length; ++i) {
++++++++ var signer = msg.signers[i];
++++++++
++++++++ if(signer.authenticatedAttributes.length === 0) {
++++++++ // if ContentInfo content type is not "Data", then
++++++++ // authenticatedAttributes must be present per RFC 2315
++++++++ if(contentType !== forge.pki.oids.data) {
++++++++ throw new Error(
++++++++ 'Invalid signer; authenticatedAttributes must be present ' +
++++++++ 'when the ContentInfo content type is not PKCS#7 Data.');
++++++++ }
++++++++ } else {
++++++++ // process authenticated attributes
++++++++ // [0] IMPLICIT
++++++++ signer.authenticatedAttributesAsn1 = asn1.create(
++++++++ asn1.Class.CONTEXT_SPECIFIC, 0, true, []);
++++++++
++++++++ // per RFC 2315, attributes are to be digested using a SET container
++++++++ // not the above [0] IMPLICIT container
++++++++ var attrsAsn1 = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SET, true, []);
++++++++
++++++++ for(var ai = 0; ai < signer.authenticatedAttributes.length; ++ai) {
++++++++ var attr = signer.authenticatedAttributes[ai];
++++++++ if(attr.type === forge.pki.oids.messageDigest) {
++++++++ // use content message digest as value
++++++++ attr.value = mds[signer.digestAlgorithm].digest();
++++++++ } else if(attr.type === forge.pki.oids.signingTime) {
++++++++ // auto-populate signing time if not already set
++++++++ if(!attr.value) {
++++++++ attr.value = signingTime;
++++++++ }
++++++++ }
++++++++
++++++++ // convert to ASN.1 and push onto Attributes SET (for signing) and
++++++++ // onto authenticatedAttributesAsn1 to complete SignedData ASN.1
++++++++ // TODO: optimize away duplication
++++++++ attrsAsn1.value.push(_attributeToAsn1(attr));
++++++++ signer.authenticatedAttributesAsn1.value.push(_attributeToAsn1(attr));
++++++++ }
++++++++
++++++++ // DER-serialize and digest SET OF attributes only
++++++++ bytes = asn1.toDer(attrsAsn1).getBytes();
++++++++ signer.md.start().update(bytes);
++++++++ }
++++++++
++++++++ // sign digest
++++++++ signer.signature = signer.key.sign(signer.md, 'RSASSA-PKCS1-V1_5');
++++++++ }
++++++++
++++++++ // add signer info
++++++++ msg.signerInfos = _signersToAsn1(msg.signers);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Creates an empty PKCS#7 message of type EncryptedData.
++++++++ *
++++++++ * @return the message.
++++++++ */
++++++++p7.createEncryptedData = function() {
++++++++ var msg = null;
++++++++ msg = {
++++++++ type: forge.pki.oids.encryptedData,
++++++++ version: 0,
++++++++ encryptedContent: {
++++++++ algorithm: forge.pki.oids['aes256-CBC']
++++++++ },
++++++++
++++++++ /**
++++++++ * Reads an EncryptedData content block (in ASN.1 format)
++++++++ *
++++++++ * @param obj The ASN.1 representation of the EncryptedData content block
++++++++ */
++++++++ fromAsn1: function(obj) {
++++++++ // Validate EncryptedData content block and capture data.
++++++++ _fromAsn1(msg, obj, p7.asn1.encryptedDataValidator);
++++++++ },
++++++++
++++++++ /**
++++++++ * Decrypt encrypted content
++++++++ *
++++++++ * @param key The (symmetric) key as a byte buffer
++++++++ */
++++++++ decrypt: function(key) {
++++++++ if(key !== undefined) {
++++++++ msg.encryptedContent.key = key;
++++++++ }
++++++++ _decryptContent(msg);
++++++++ }
++++++++ };
++++++++ return msg;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an empty PKCS#7 message of type EnvelopedData.
++++++++ *
++++++++ * @return the message.
++++++++ */
++++++++p7.createEnvelopedData = function() {
++++++++ var msg = null;
++++++++ msg = {
++++++++ type: forge.pki.oids.envelopedData,
++++++++ version: 0,
++++++++ recipients: [],
++++++++ encryptedContent: {
++++++++ algorithm: forge.pki.oids['aes256-CBC']
++++++++ },
++++++++
++++++++ /**
++++++++ * Reads an EnvelopedData content block (in ASN.1 format)
++++++++ *
++++++++ * @param obj the ASN.1 representation of the EnvelopedData content block.
++++++++ */
++++++++ fromAsn1: function(obj) {
++++++++ // validate EnvelopedData content block and capture data
++++++++ var capture = _fromAsn1(msg, obj, p7.asn1.envelopedDataValidator);
++++++++ msg.recipients = _recipientsFromAsn1(capture.recipientInfos.value);
++++++++ },
++++++++
++++++++ toAsn1: function() {
++++++++ // ContentInfo
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // ContentType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(msg.type).getBytes()),
++++++++ // [0] EnvelopedData
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Version
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(msg.version).getBytes()),
++++++++ // RecipientInfos
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true,
++++++++ _recipientsToAsn1(msg.recipients)),
++++++++ // EncryptedContentInfo
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true,
++++++++ _encryptedContentToAsn1(msg.encryptedContent))
++++++++ ])
++++++++ ])
++++++++ ]);
++++++++ },
++++++++
++++++++ /**
++++++++ * Find recipient by X.509 certificate's issuer.
++++++++ *
++++++++ * @param cert the certificate with the issuer to look for.
++++++++ *
++++++++ * @return the recipient object.
++++++++ */
++++++++ findRecipient: function(cert) {
++++++++ var sAttr = cert.issuer.attributes;
++++++++
++++++++ for(var i = 0; i < msg.recipients.length; ++i) {
++++++++ var r = msg.recipients[i];
++++++++ var rAttr = r.issuer;
++++++++
++++++++ if(r.serialNumber !== cert.serialNumber) {
++++++++ continue;
++++++++ }
++++++++
++++++++ if(rAttr.length !== sAttr.length) {
++++++++ continue;
++++++++ }
++++++++
++++++++ var match = true;
++++++++ for(var j = 0; j < sAttr.length; ++j) {
++++++++ if(rAttr[j].type !== sAttr[j].type ||
++++++++ rAttr[j].value !== sAttr[j].value) {
++++++++ match = false;
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ if(match) {
++++++++ return r;
++++++++ }
++++++++ }
++++++++
++++++++ return null;
++++++++ },
++++++++
++++++++ /**
++++++++ * Decrypt enveloped content
++++++++ *
++++++++ * @param recipient The recipient object related to the private key
++++++++ * @param privKey The (RSA) private key object
++++++++ */
++++++++ decrypt: function(recipient, privKey) {
++++++++ if(msg.encryptedContent.key === undefined && recipient !== undefined &&
++++++++ privKey !== undefined) {
++++++++ switch(recipient.encryptedContent.algorithm) {
++++++++ case forge.pki.oids.rsaEncryption:
++++++++ case forge.pki.oids.desCBC:
++++++++ var key = privKey.decrypt(recipient.encryptedContent.content);
++++++++ msg.encryptedContent.key = forge.util.createBuffer(key);
++++++++ break;
++++++++
++++++++ default:
++++++++ throw new Error('Unsupported asymmetric cipher, ' +
++++++++ 'OID ' + recipient.encryptedContent.algorithm);
++++++++ }
++++++++ }
++++++++
++++++++ _decryptContent(msg);
++++++++ },
++++++++
++++++++ /**
++++++++ * Add (another) entity to list of recipients.
++++++++ *
++++++++ * @param cert The certificate of the entity to add.
++++++++ */
++++++++ addRecipient: function(cert) {
++++++++ msg.recipients.push({
++++++++ version: 0,
++++++++ issuer: cert.issuer.attributes,
++++++++ serialNumber: cert.serialNumber,
++++++++ encryptedContent: {
++++++++ // We simply assume rsaEncryption here, since forge.pki only
++++++++ // supports RSA so far. If the PKI module supports other
++++++++ // ciphers one day, we need to modify this one as well.
++++++++ algorithm: forge.pki.oids.rsaEncryption,
++++++++ key: cert.publicKey
++++++++ }
++++++++ });
++++++++ },
++++++++
++++++++ /**
++++++++ * Encrypt enveloped content.
++++++++ *
++++++++ * This function supports two optional arguments, cipher and key, which
++++++++ * can be used to influence symmetric encryption. Unless cipher is
++++++++ * provided, the cipher specified in encryptedContent.algorithm is used
++++++++ * (defaults to AES-256-CBC). If no key is provided, encryptedContent.key
++++++++ * is (re-)used. If that one's not set, a random key will be generated
++++++++ * automatically.
++++++++ *
++++++++ * @param [key] The key to be used for symmetric encryption.
++++++++ * @param [cipher] The OID of the symmetric cipher to use.
++++++++ */
++++++++ encrypt: function(key, cipher) {
++++++++ // Part 1: Symmetric encryption
++++++++ if(msg.encryptedContent.content === undefined) {
++++++++ cipher = cipher || msg.encryptedContent.algorithm;
++++++++ key = key || msg.encryptedContent.key;
++++++++
++++++++ var keyLen, ivLen, ciphFn;
++++++++ switch(cipher) {
++++++++ case forge.pki.oids['aes128-CBC']:
++++++++ keyLen = 16;
++++++++ ivLen = 16;
++++++++ ciphFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++
++++++++ case forge.pki.oids['aes192-CBC']:
++++++++ keyLen = 24;
++++++++ ivLen = 16;
++++++++ ciphFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++
++++++++ case forge.pki.oids['aes256-CBC']:
++++++++ keyLen = 32;
++++++++ ivLen = 16;
++++++++ ciphFn = forge.aes.createEncryptionCipher;
++++++++ break;
++++++++
++++++++ case forge.pki.oids['des-EDE3-CBC']:
++++++++ keyLen = 24;
++++++++ ivLen = 8;
++++++++ ciphFn = forge.des.createEncryptionCipher;
++++++++ break;
++++++++
++++++++ default:
++++++++ throw new Error('Unsupported symmetric cipher, OID ' + cipher);
++++++++ }
++++++++
++++++++ if(key === undefined) {
++++++++ key = forge.util.createBuffer(forge.random.getBytes(keyLen));
++++++++ } else if(key.length() != keyLen) {
++++++++ throw new Error('Symmetric key has wrong length; ' +
++++++++ 'got ' + key.length() + ' bytes, expected ' + keyLen + '.');
++++++++ }
++++++++
++++++++ // Keep a copy of the key & IV in the object, so the caller can
++++++++ // use it for whatever reason.
++++++++ msg.encryptedContent.algorithm = cipher;
++++++++ msg.encryptedContent.key = key;
++++++++ msg.encryptedContent.parameter = forge.util.createBuffer(
++++++++ forge.random.getBytes(ivLen));
++++++++
++++++++ var ciph = ciphFn(key);
++++++++ ciph.start(msg.encryptedContent.parameter.copy());
++++++++ ciph.update(msg.content);
++++++++
++++++++ // The finish function does PKCS#7 padding by default, therefore
++++++++ // no action required by us.
++++++++ if(!ciph.finish()) {
++++++++ throw new Error('Symmetric encryption failed.');
++++++++ }
++++++++
++++++++ msg.encryptedContent.content = ciph.output;
++++++++ }
++++++++
++++++++ // Part 2: asymmetric encryption for each recipient
++++++++ for(var i = 0; i < msg.recipients.length; ++i) {
++++++++ var recipient = msg.recipients[i];
++++++++
++++++++ // Nothing to do, encryption already done.
++++++++ if(recipient.encryptedContent.content !== undefined) {
++++++++ continue;
++++++++ }
++++++++
++++++++ switch(recipient.encryptedContent.algorithm) {
++++++++ case forge.pki.oids.rsaEncryption:
++++++++ recipient.encryptedContent.content =
++++++++ recipient.encryptedContent.key.encrypt(
++++++++ msg.encryptedContent.key.data);
++++++++ break;
++++++++
++++++++ default:
++++++++ throw new Error('Unsupported asymmetric cipher, OID ' +
++++++++ recipient.encryptedContent.algorithm);
++++++++ }
++++++++ }
++++++++ }
++++++++ };
++++++++ return msg;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a single recipient from an ASN.1 object.
++++++++ *
++++++++ * @param obj the ASN.1 RecipientInfo.
++++++++ *
++++++++ * @return the recipient object.
++++++++ */
++++++++function _recipientFromAsn1(obj) {
++++++++ // validate EnvelopedData content block and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, p7.asn1.recipientInfoValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#7 RecipientInfo. ' +
++++++++ 'ASN.1 object is not an PKCS#7 RecipientInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ return {
++++++++ version: capture.version.charCodeAt(0),
++++++++ issuer: forge.pki.RDNAttributesAsArray(capture.issuer),
++++++++ serialNumber: forge.util.createBuffer(capture.serial).toHex(),
++++++++ encryptedContent: {
++++++++ algorithm: asn1.derToOid(capture.encAlgorithm),
++++++++ parameter: capture.encParameter ? capture.encParameter.value : undefined,
++++++++ content: capture.encKey
++++++++ }
++++++++ };
++++++++}
++++++++
++++++++/**
++++++++ * Converts a single recipient object to an ASN.1 object.
++++++++ *
++++++++ * @param obj the recipient object.
++++++++ *
++++++++ * @return the ASN.1 RecipientInfo.
++++++++ */
++++++++function _recipientToAsn1(obj) {
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Version
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(obj.version).getBytes()),
++++++++ // IssuerAndSerialNumber
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Name
++++++++ forge.pki.distinguishedNameToAsn1({attributes: obj.issuer}),
++++++++ // Serial
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ forge.util.hexToBytes(obj.serialNumber))
++++++++ ]),
++++++++ // KeyEncryptionAlgorithmIdentifier
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(obj.encryptedContent.algorithm).getBytes()),
++++++++ // Parameter, force NULL, only RSA supported for now.
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]),
++++++++ // EncryptedKey
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ obj.encryptedContent.content)
++++++++ ]);
++++++++}
++++++++
++++++++/**
++++++++ * Map a set of RecipientInfo ASN.1 objects to recipient objects.
++++++++ *
++++++++ * @param infos an array of ASN.1 representations RecipientInfo (i.e. SET OF).
++++++++ *
++++++++ * @return an array of recipient objects.
++++++++ */
++++++++function _recipientsFromAsn1(infos) {
++++++++ var ret = [];
++++++++ for(var i = 0; i < infos.length; ++i) {
++++++++ ret.push(_recipientFromAsn1(infos[i]));
++++++++ }
++++++++ return ret;
++++++++}
++++++++
++++++++/**
++++++++ * Map an array of recipient objects to ASN.1 RecipientInfo objects.
++++++++ *
++++++++ * @param recipients an array of recipientInfo objects.
++++++++ *
++++++++ * @return an array of ASN.1 RecipientInfos.
++++++++ */
++++++++function _recipientsToAsn1(recipients) {
++++++++ var ret = [];
++++++++ for(var i = 0; i < recipients.length; ++i) {
++++++++ ret.push(_recipientToAsn1(recipients[i]));
++++++++ }
++++++++ return ret;
++++++++}
++++++++
++++++++/**
++++++++ * Converts a single signer from an ASN.1 object.
++++++++ *
++++++++ * @param obj the ASN.1 representation of a SignerInfo.
++++++++ *
++++++++ * @return the signer object.
++++++++ */
++++++++function _signerFromAsn1(obj) {
++++++++ // validate EnvelopedData content block and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, p7.asn1.signerInfoValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#7 SignerInfo. ' +
++++++++ 'ASN.1 object is not an PKCS#7 SignerInfo.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var rval = {
++++++++ version: capture.version.charCodeAt(0),
++++++++ issuer: forge.pki.RDNAttributesAsArray(capture.issuer),
++++++++ serialNumber: forge.util.createBuffer(capture.serial).toHex(),
++++++++ digestAlgorithm: asn1.derToOid(capture.digestAlgorithm),
++++++++ signatureAlgorithm: asn1.derToOid(capture.signatureAlgorithm),
++++++++ signature: capture.signature,
++++++++ authenticatedAttributes: [],
++++++++ unauthenticatedAttributes: []
++++++++ };
++++++++
++++++++ // TODO: convert attributes
++++++++ var authenticatedAttributes = capture.authenticatedAttributes || [];
++++++++ var unauthenticatedAttributes = capture.unauthenticatedAttributes || [];
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Converts a single signerInfo object to an ASN.1 object.
++++++++ *
++++++++ * @param obj the signerInfo object.
++++++++ *
++++++++ * @return the ASN.1 representation of a SignerInfo.
++++++++ */
++++++++function _signerToAsn1(obj) {
++++++++ // SignerInfo
++++++++ var rval = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(obj.version).getBytes()),
++++++++ // issuerAndSerialNumber
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // name
++++++++ forge.pki.distinguishedNameToAsn1({attributes: obj.issuer}),
++++++++ // serial
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ forge.util.hexToBytes(obj.serialNumber))
++++++++ ]),
++++++++ // digestAlgorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(obj.digestAlgorithm).getBytes()),
++++++++ // parameters (null)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ])
++++++++ ]);
++++++++
++++++++ // authenticatedAttributes (OPTIONAL)
++++++++ if(obj.authenticatedAttributesAsn1) {
++++++++ // add ASN.1 previously generated during signing
++++++++ rval.value.push(obj.authenticatedAttributesAsn1);
++++++++ }
++++++++
++++++++ // digestEncryptionAlgorithm
++++++++ rval.value.push(asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(obj.signatureAlgorithm).getBytes()),
++++++++ // parameters (null)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]));
++++++++
++++++++ // encryptedDigest
++++++++ rval.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, obj.signature));
++++++++
++++++++ // unauthenticatedAttributes (OPTIONAL)
++++++++ if(obj.unauthenticatedAttributes.length > 0) {
++++++++ // [1] IMPLICIT
++++++++ var attrsAsn1 = asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, []);
++++++++ for(var i = 0; i < obj.unauthenticatedAttributes.length; ++i) {
++++++++ var attr = obj.unauthenticatedAttributes[i];
++++++++ attrsAsn1.values.push(_attributeToAsn1(attr));
++++++++ }
++++++++ rval.value.push(attrsAsn1);
++++++++ }
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Map a set of SignerInfo ASN.1 objects to an array of signer objects.
++++++++ *
++++++++ * @param signerInfoAsn1s an array of ASN.1 SignerInfos (i.e. SET OF).
++++++++ *
++++++++ * @return an array of signers objects.
++++++++ */
++++++++function _signersFromAsn1(signerInfoAsn1s) {
++++++++ var ret = [];
++++++++ for(var i = 0; i < signerInfoAsn1s.length; ++i) {
++++++++ ret.push(_signerFromAsn1(signerInfoAsn1s[i]));
++++++++ }
++++++++ return ret;
++++++++}
++++++++
++++++++/**
++++++++ * Map an array of signer objects to ASN.1 objects.
++++++++ *
++++++++ * @param signers an array of signer objects.
++++++++ *
++++++++ * @return an array of ASN.1 SignerInfos.
++++++++ */
++++++++function _signersToAsn1(signers) {
++++++++ var ret = [];
++++++++ for(var i = 0; i < signers.length; ++i) {
++++++++ ret.push(_signerToAsn1(signers[i]));
++++++++ }
++++++++ return ret;
++++++++}
++++++++
++++++++/**
++++++++ * Convert an attribute object to an ASN.1 Attribute.
++++++++ *
++++++++ * @param attr the attribute object.
++++++++ *
++++++++ * @return the ASN.1 Attribute.
++++++++ */
++++++++function _attributeToAsn1(attr) {
++++++++ var value;
++++++++
++++++++ // TODO: generalize to support more attributes
++++++++ if(attr.type === forge.pki.oids.contentType) {
++++++++ value = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(attr.value).getBytes());
++++++++ } else if(attr.type === forge.pki.oids.messageDigest) {
++++++++ value = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ attr.value.bytes());
++++++++ } else if(attr.type === forge.pki.oids.signingTime) {
++++++++ /* Note per RFC 2985: Dates between 1 January 1950 and 31 December 2049
++++++++ (inclusive) MUST be encoded as UTCTime. Any dates with year values
++++++++ before 1950 or after 2049 MUST be encoded as GeneralizedTime. [Further,]
++++++++ UTCTime values MUST be expressed in Greenwich Mean Time (Zulu) and MUST
++++++++ include seconds (i.e., times are YYMMDDHHMMSSZ), even where the
++++++++ number of seconds is zero. Midnight (GMT) must be represented as
++++++++ "YYMMDD000000Z". */
++++++++ // TODO: make these module-level constants
++++++++ var jan_1_1950 = new Date('1950-01-01T00:00:00Z');
++++++++ var jan_1_2050 = new Date('2050-01-01T00:00:00Z');
++++++++ var date = attr.value;
++++++++ if(typeof date === 'string') {
++++++++ // try to parse date
++++++++ var timestamp = Date.parse(date);
++++++++ if(!isNaN(timestamp)) {
++++++++ date = new Date(timestamp);
++++++++ } else if(date.length === 13) {
++++++++ // YYMMDDHHMMSSZ (13 chars for UTCTime)
++++++++ date = asn1.utcTimeToDate(date);
++++++++ } else {
++++++++ // assume generalized time
++++++++ date = asn1.generalizedTimeToDate(date);
++++++++ }
++++++++ }
++++++++
++++++++ if(date >= jan_1_1950 && date < jan_1_2050) {
++++++++ value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.UTCTIME, false,
++++++++ asn1.dateToUtcTime(date));
++++++++ } else {
++++++++ value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.GENERALIZEDTIME, false,
++++++++ asn1.dateToGeneralizedTime(date));
++++++++ }
++++++++ }
++++++++
++++++++ // TODO: expose as common API call
++++++++ // create a RelativeDistinguishedName set
++++++++ // each value in the set is an AttributeTypeAndValue first
++++++++ // containing the type (an OID) and second the value
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // AttributeType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(attr.type).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
++++++++ // AttributeValue
++++++++ value
++++++++ ])
++++++++ ]);
++++++++}
++++++++
++++++++/**
++++++++ * Map messages encrypted content to ASN.1 objects.
++++++++ *
++++++++ * @param ec The encryptedContent object of the message.
++++++++ *
++++++++ * @return ASN.1 representation of the encryptedContent object (SEQUENCE).
++++++++ */
++++++++function _encryptedContentToAsn1(ec) {
++++++++ return [
++++++++ // ContentType, always Data for the moment
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(forge.pki.oids.data).getBytes()),
++++++++ // ContentEncryptionAlgorithmIdentifier
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // Algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(ec.algorithm).getBytes()),
++++++++ // Parameters (IV)
++++++++ !ec.parameter ?
++++++++ undefined :
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ ec.parameter.getBytes())
++++++++ ]),
++++++++ // [0] EncryptedContent
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ ec.content.getBytes())
++++++++ ])
++++++++ ];
++++++++}
++++++++
++++++++/**
++++++++ * Reads the "common part" of an PKCS#7 content block (in ASN.1 format)
++++++++ *
++++++++ * This function reads the "common part" of the PKCS#7 content blocks
++++++++ * EncryptedData and EnvelopedData, i.e. version number and symmetrically
++++++++ * encrypted content block.
++++++++ *
++++++++ * The result of the ASN.1 validate and capture process is returned
++++++++ * to allow the caller to extract further data, e.g. the list of recipients
++++++++ * in case of a EnvelopedData object.
++++++++ *
++++++++ * @param msg the PKCS#7 object to read the data to.
++++++++ * @param obj the ASN.1 representation of the content block.
++++++++ * @param validator the ASN.1 structure validator object to use.
++++++++ *
++++++++ * @return the value map captured by validator object.
++++++++ */
++++++++function _fromAsn1(msg, obj, validator) {
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, validator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#7 message. ' +
++++++++ 'ASN.1 object is not a supported PKCS#7 message.');
++++++++ error.errors = error;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // Check contentType, so far we only support (raw) Data.
++++++++ var contentType = asn1.derToOid(capture.contentType);
++++++++ if(contentType !== forge.pki.oids.data) {
++++++++ throw new Error('Unsupported PKCS#7 message. ' +
++++++++ 'Only wrapped ContentType Data supported.');
++++++++ }
++++++++
++++++++ if(capture.encryptedContent) {
++++++++ var content = '';
++++++++ if(forge.util.isArray(capture.encryptedContent)) {
++++++++ for(var i = 0; i < capture.encryptedContent.length; ++i) {
++++++++ if(capture.encryptedContent[i].type !== asn1.Type.OCTETSTRING) {
++++++++ throw new Error('Malformed PKCS#7 message, expecting encrypted ' +
++++++++ 'content constructed of only OCTET STRING objects.');
++++++++ }
++++++++ content += capture.encryptedContent[i].value;
++++++++ }
++++++++ } else {
++++++++ content = capture.encryptedContent;
++++++++ }
++++++++ msg.encryptedContent = {
++++++++ algorithm: asn1.derToOid(capture.encAlgorithm),
++++++++ parameter: forge.util.createBuffer(capture.encParameter.value),
++++++++ content: forge.util.createBuffer(content)
++++++++ };
++++++++ }
++++++++
++++++++ if(capture.content) {
++++++++ var content = '';
++++++++ if(forge.util.isArray(capture.content)) {
++++++++ for(var i = 0; i < capture.content.length; ++i) {
++++++++ if(capture.content[i].type !== asn1.Type.OCTETSTRING) {
++++++++ throw new Error('Malformed PKCS#7 message, expecting ' +
++++++++ 'content constructed of only OCTET STRING objects.');
++++++++ }
++++++++ content += capture.content[i].value;
++++++++ }
++++++++ } else {
++++++++ content = capture.content;
++++++++ }
++++++++ msg.content = forge.util.createBuffer(content);
++++++++ }
++++++++
++++++++ msg.version = capture.version.charCodeAt(0);
++++++++ msg.rawCapture = capture;
++++++++
++++++++ return capture;
++++++++}
++++++++
++++++++/**
++++++++ * Decrypt the symmetrically encrypted content block of the PKCS#7 message.
++++++++ *
++++++++ * Decryption is skipped in case the PKCS#7 message object already has a
++++++++ * (decrypted) content attribute. The algorithm, key and cipher parameters
++++++++ * (probably the iv) are taken from the encryptedContent attribute of the
++++++++ * message object.
++++++++ *
++++++++ * @param The PKCS#7 message object.
++++++++ */
++++++++function _decryptContent(msg) {
++++++++ if(msg.encryptedContent.key === undefined) {
++++++++ throw new Error('Symmetric key not available.');
++++++++ }
++++++++
++++++++ if(msg.content === undefined) {
++++++++ var ciph;
++++++++
++++++++ switch(msg.encryptedContent.algorithm) {
++++++++ case forge.pki.oids['aes128-CBC']:
++++++++ case forge.pki.oids['aes192-CBC']:
++++++++ case forge.pki.oids['aes256-CBC']:
++++++++ ciph = forge.aes.createDecryptionCipher(msg.encryptedContent.key);
++++++++ break;
++++++++
++++++++ case forge.pki.oids['desCBC']:
++++++++ case forge.pki.oids['des-EDE3-CBC']:
++++++++ ciph = forge.des.createDecryptionCipher(msg.encryptedContent.key);
++++++++ break;
++++++++
++++++++ default:
++++++++ throw new Error('Unsupported symmetric cipher, OID ' +
++++++++ msg.encryptedContent.algorithm);
++++++++ }
++++++++ ciph.start(msg.encryptedContent.parameter);
++++++++ ciph.update(msg.encryptedContent.content);
++++++++
++++++++ if(!ciph.finish()) {
++++++++ throw new Error('Symmetric decryption failed.');
++++++++ }
++++++++
++++++++ msg.content = ciph.output;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of ASN.1 validators for PKCS#7 v1.5.
++++++++ *
++++++++ * @author Dave Longley
++++++++ * @author Stefan Siegl
++++++++ *
++++++++ * Copyright (c) 2012-2015 Digital Bazaar, Inc.
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * The ASN.1 representation of PKCS#7 is as follows
++++++++ * (see RFC #2315 for details, http://www.ietf.org/rfc/rfc2315.txt):
++++++++ *
++++++++ * A PKCS#7 message consists of a ContentInfo on root level, which may
++++++++ * contain any number of further ContentInfo nested into it.
++++++++ *
++++++++ * ContentInfo ::= SEQUENCE {
++++++++ * contentType ContentType,
++++++++ * content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
++++++++ * }
++++++++ *
++++++++ * ContentType ::= OBJECT IDENTIFIER
++++++++ *
++++++++ * EnvelopedData ::= SEQUENCE {
++++++++ * version Version,
++++++++ * recipientInfos RecipientInfos,
++++++++ * encryptedContentInfo EncryptedContentInfo
++++++++ * }
++++++++ *
++++++++ * EncryptedData ::= SEQUENCE {
++++++++ * version Version,
++++++++ * encryptedContentInfo EncryptedContentInfo
++++++++ * }
++++++++ *
++++++++ * id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
++++++++ * us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
++++++++ *
++++++++ * SignedData ::= SEQUENCE {
++++++++ * version INTEGER,
++++++++ * digestAlgorithms DigestAlgorithmIdentifiers,
++++++++ * contentInfo ContentInfo,
++++++++ * certificates [0] IMPLICIT Certificates OPTIONAL,
++++++++ * crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
++++++++ * signerInfos SignerInfos
++++++++ * }
++++++++ *
++++++++ * SignerInfos ::= SET OF SignerInfo
++++++++ *
++++++++ * SignerInfo ::= SEQUENCE {
++++++++ * version Version,
++++++++ * issuerAndSerialNumber IssuerAndSerialNumber,
++++++++ * digestAlgorithm DigestAlgorithmIdentifier,
++++++++ * authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
++++++++ * digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
++++++++ * encryptedDigest EncryptedDigest,
++++++++ * unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
++++++++ * }
++++++++ *
++++++++ * EncryptedDigest ::= OCTET STRING
++++++++ *
++++++++ * Attributes ::= SET OF Attribute
++++++++ *
++++++++ * Attribute ::= SEQUENCE {
++++++++ * attrType OBJECT IDENTIFIER,
++++++++ * attrValues SET OF AttributeValue
++++++++ * }
++++++++ *
++++++++ * AttributeValue ::= ANY
++++++++ *
++++++++ * Version ::= INTEGER
++++++++ *
++++++++ * RecipientInfos ::= SET OF RecipientInfo
++++++++ *
++++++++ * EncryptedContentInfo ::= SEQUENCE {
++++++++ * contentType ContentType,
++++++++ * contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
++++++++ * encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL
++++++++ * }
++++++++ *
++++++++ * ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ *
++++++++ * The AlgorithmIdentifier contains an Object Identifier (OID) and parameters
++++++++ * for the algorithm, if any. In the case of AES and DES3, there is only one,
++++++++ * the IV.
++++++++ *
++++++++ * AlgorithmIdentifer ::= SEQUENCE {
++++++++ * algorithm OBJECT IDENTIFIER,
++++++++ * parameters ANY DEFINED BY algorithm OPTIONAL
++++++++ * }
++++++++ *
++++++++ * EncryptedContent ::= OCTET STRING
++++++++ *
++++++++ * RecipientInfo ::= SEQUENCE {
++++++++ * version Version,
++++++++ * issuerAndSerialNumber IssuerAndSerialNumber,
++++++++ * keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
++++++++ * encryptedKey EncryptedKey
++++++++ * }
++++++++ *
++++++++ * IssuerAndSerialNumber ::= SEQUENCE {
++++++++ * issuer Name,
++++++++ * serialNumber CertificateSerialNumber
++++++++ * }
++++++++ *
++++++++ * CertificateSerialNumber ::= INTEGER
++++++++ *
++++++++ * KeyEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ *
++++++++ * EncryptedKey ::= OCTET STRING
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++require('./util');
++++++++
++++++++// shortcut for ASN.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++// shortcut for PKCS#7 API
++++++++var p7v = module.exports = forge.pkcs7asn1 = forge.pkcs7asn1 || {};
++++++++forge.pkcs7 = forge.pkcs7 || {};
++++++++forge.pkcs7.asn1 = p7v;
++++++++
++++++++var contentInfoValidator = {
++++++++ name: 'ContentInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'ContentInfo.ContentType',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'contentType'
++++++++ }, {
++++++++ name: 'ContentInfo.content',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ captureAsn1: 'content'
++++++++ }]
++++++++};
++++++++p7v.contentInfoValidator = contentInfoValidator;
++++++++
++++++++var encryptedContentInfoValidator = {
++++++++ name: 'EncryptedContentInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'EncryptedContentInfo.contentType',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'contentType'
++++++++ }, {
++++++++ name: 'EncryptedContentInfo.contentEncryptionAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'EncryptedContentInfo.contentEncryptionAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'encAlgorithm'
++++++++ }, {
++++++++ name: 'EncryptedContentInfo.contentEncryptionAlgorithm.parameter',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ captureAsn1: 'encParameter'
++++++++ }]
++++++++ }, {
++++++++ name: 'EncryptedContentInfo.encryptedContent',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ /* The PKCS#7 structure output by OpenSSL somewhat differs from what
++++++++ * other implementations do generate.
++++++++ *
++++++++ * OpenSSL generates a structure like this:
++++++++ * SEQUENCE {
++++++++ * ...
++++++++ * [0]
++++++++ * 26 DA 67 D2 17 9C 45 3C B1 2A A8 59 2F 29 33 38
++++++++ * C3 C3 DF 86 71 74 7A 19 9F 40 D0 29 BE 85 90 45
++++++++ * ...
++++++++ * }
++++++++ *
++++++++ * Whereas other implementations (and this PKCS#7 module) generate:
++++++++ * SEQUENCE {
++++++++ * ...
++++++++ * [0] {
++++++++ * OCTET STRING
++++++++ * 26 DA 67 D2 17 9C 45 3C B1 2A A8 59 2F 29 33 38
++++++++ * C3 C3 DF 86 71 74 7A 19 9F 40 D0 29 BE 85 90 45
++++++++ * ...
++++++++ * }
++++++++ * }
++++++++ *
++++++++ * In order to support both, we just capture the context specific
++++++++ * field here. The OCTET STRING bit is removed below.
++++++++ */
++++++++ capture: 'encryptedContent',
++++++++ captureAsn1: 'encryptedContentAsn1'
++++++++ }]
++++++++};
++++++++
++++++++p7v.envelopedDataValidator = {
++++++++ name: 'EnvelopedData',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'EnvelopedData.Version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'version'
++++++++ }, {
++++++++ name: 'EnvelopedData.RecipientInfos',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ constructed: true,
++++++++ captureAsn1: 'recipientInfos'
++++++++ }].concat(encryptedContentInfoValidator)
++++++++};
++++++++
++++++++p7v.encryptedDataValidator = {
++++++++ name: 'EncryptedData',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'EncryptedData.Version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'version'
++++++++ }].concat(encryptedContentInfoValidator)
++++++++};
++++++++
++++++++var signerValidator = {
++++++++ name: 'SignerInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'SignerInfo.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false
++++++++ }, {
++++++++ name: 'SignerInfo.issuerAndSerialNumber',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'SignerInfo.issuerAndSerialNumber.issuer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'issuer'
++++++++ }, {
++++++++ name: 'SignerInfo.issuerAndSerialNumber.serialNumber',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'serial'
++++++++ }]
++++++++ }, {
++++++++ name: 'SignerInfo.digestAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'SignerInfo.digestAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'digestAlgorithm'
++++++++ }, {
++++++++ name: 'SignerInfo.digestAlgorithm.parameter',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ constructed: false,
++++++++ captureAsn1: 'digestParameter',
++++++++ optional: true
++++++++ }]
++++++++ }, {
++++++++ name: 'SignerInfo.authenticatedAttributes',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ capture: 'authenticatedAttributes'
++++++++ }, {
++++++++ name: 'SignerInfo.digestEncryptionAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ capture: 'signatureAlgorithm'
++++++++ }, {
++++++++ name: 'SignerInfo.encryptedDigest',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'signature'
++++++++ }, {
++++++++ name: 'SignerInfo.unauthenticatedAttributes',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 1,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ capture: 'unauthenticatedAttributes'
++++++++ }]
++++++++};
++++++++
++++++++p7v.signedDataValidator = {
++++++++ name: 'SignedData',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'SignedData.Version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'version'
++++++++ }, {
++++++++ name: 'SignedData.DigestAlgorithms',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ constructed: true,
++++++++ captureAsn1: 'digestAlgorithms'
++++++++ },
++++++++ contentInfoValidator,
++++++++ {
++++++++ name: 'SignedData.Certificates',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ optional: true,
++++++++ captureAsn1: 'certificates'
++++++++ }, {
++++++++ name: 'SignedData.CertificateRevocationLists',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 1,
++++++++ optional: true,
++++++++ captureAsn1: 'crls'
++++++++ }, {
++++++++ name: 'SignedData.SignerInfos',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ capture: 'signerInfos',
++++++++ optional: true,
++++++++ value: [signerValidator]
++++++++ }]
++++++++};
++++++++
++++++++p7v.recipientInfoValidator = {
++++++++ name: 'RecipientInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'RecipientInfo.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'version'
++++++++ }, {
++++++++ name: 'RecipientInfo.issuerAndSerial',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'RecipientInfo.issuerAndSerial.issuer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'issuer'
++++++++ }, {
++++++++ name: 'RecipientInfo.issuerAndSerial.serialNumber',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'serial'
++++++++ }]
++++++++ }, {
++++++++ name: 'RecipientInfo.keyEncryptionAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'RecipientInfo.keyEncryptionAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'encAlgorithm'
++++++++ }, {
++++++++ name: 'RecipientInfo.keyEncryptionAlgorithm.parameter',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ constructed: false,
++++++++ captureAsn1: 'encParameter',
++++++++ optional: true
++++++++ }]
++++++++ }, {
++++++++ name: 'RecipientInfo.encryptedKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'encKey'
++++++++ }]
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of a basic Public Key Infrastructure, including
++++++++ * support for RSA public and private keys.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++require('./oids');
++++++++require('./pbe');
++++++++require('./pem');
++++++++require('./pbkdf2');
++++++++require('./pkcs12');
++++++++require('./pss');
++++++++require('./rsa');
++++++++require('./util');
++++++++require('./x509');
++++++++
++++++++// shortcut for asn.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++/* Public Key Infrastructure (PKI) implementation. */
++++++++var pki = module.exports = forge.pki = forge.pki || {};
++++++++
++++++++/**
++++++++ * NOTE: THIS METHOD IS DEPRECATED. Use pem.decode() instead.
++++++++ *
++++++++ * Converts PEM-formatted data to DER.
++++++++ *
++++++++ * @param pem the PEM-formatted data.
++++++++ *
++++++++ * @return the DER-formatted data.
++++++++ */
++++++++pki.pemToDer = function(pem) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert PEM to DER; PEM is encrypted.');
++++++++ }
++++++++ return forge.util.createBuffer(msg.body);
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RSA private key from PEM format.
++++++++ *
++++++++ * @param pem the PEM-formatted private key.
++++++++ *
++++++++ * @return the private key.
++++++++ */
++++++++pki.privateKeyFromPem = function(pem) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'PRIVATE KEY' && msg.type !== 'RSA PRIVATE KEY') {
++++++++ var error = new Error('Could not convert private key from PEM; PEM ' +
++++++++ 'header type is not "PRIVATE KEY" or "RSA PRIVATE KEY".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert private key from PEM; PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ var obj = asn1.fromDer(msg.body);
++++++++
++++++++ return pki.privateKeyFromAsn1(obj);
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RSA private key to PEM format.
++++++++ *
++++++++ * @param key the private key.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted private key.
++++++++ */
++++++++pki.privateKeyToPem = function(key, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'RSA PRIVATE KEY',
++++++++ body: asn1.toDer(pki.privateKeyToAsn1(key)).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PrivateKeyInfo to PEM format.
++++++++ *
++++++++ * @param pki the PrivateKeyInfo.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted private key.
++++++++ */
++++++++pki.privateKeyInfoToPem = function(pki, maxline) {
++++++++ // convert to DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'PRIVATE KEY',
++++++++ body: asn1.toDer(pki).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Prime number generation API.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++require('./jsbn');
++++++++require('./random');
++++++++
++++++++(function() {
++++++++
++++++++// forge.prime already defined
++++++++if(forge.prime) {
++++++++ module.exports = forge.prime;
++++++++ return;
++++++++}
++++++++
++++++++/* PRIME API */
++++++++var prime = module.exports = forge.prime = forge.prime || {};
++++++++
++++++++var BigInteger = forge.jsbn.BigInteger;
++++++++
++++++++// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
++++++++var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
++++++++var THIRTY = new BigInteger(null);
++++++++THIRTY.fromInt(30);
++++++++var op_or = function(x, y) {return x|y;};
++++++++
++++++++/**
++++++++ * Generates a random probable prime with the given number of bits.
++++++++ *
++++++++ * Alternative algorithms can be specified by name as a string or as an
++++++++ * object with custom options like so:
++++++++ *
++++++++ * {
++++++++ * name: 'PRIMEINC',
++++++++ * options: {
++++++++ * maxBlockTime: <the maximum amount of time to block the main
++++++++ * thread before allowing I/O other JS to run>,
++++++++ * millerRabinTests: <the number of miller-rabin tests to run>,
++++++++ * workerScript: <the worker script URL>,
++++++++ * workers: <the number of web workers (if supported) to use,
++++++++ * -1 to use estimated cores minus one>.
++++++++ * workLoad: the size of the work load, ie: number of possible prime
++++++++ * numbers for each web worker to check per work assignment,
++++++++ * (default: 100).
++++++++ * }
++++++++ * }
++++++++ *
++++++++ * @param bits the number of bits for the prime number.
++++++++ * @param options the options to use.
++++++++ * [algorithm] the algorithm to use (default: 'PRIMEINC').
++++++++ * [prng] a custom crypto-secure pseudo-random number generator to use,
++++++++ * that must define "getBytesSync".
++++++++ *
++++++++ * @return callback(err, num) called once the operation completes.
++++++++ */
++++++++prime.generateProbablePrime = function(bits, options, callback) {
++++++++ if(typeof options === 'function') {
++++++++ callback = options;
++++++++ options = {};
++++++++ }
++++++++ options = options || {};
++++++++
++++++++ // default to PRIMEINC algorithm
++++++++ var algorithm = options.algorithm || 'PRIMEINC';
++++++++ if(typeof algorithm === 'string') {
++++++++ algorithm = {name: algorithm};
++++++++ }
++++++++ algorithm.options = algorithm.options || {};
++++++++
++++++++ // create prng with api that matches BigInteger secure random
++++++++ var prng = options.prng || forge.random;
++++++++ var rng = {
++++++++ // x is an array to fill with bytes
++++++++ nextBytes: function(x) {
++++++++ var b = prng.getBytesSync(x.length);
++++++++ for(var i = 0; i < x.length; ++i) {
++++++++ x[i] = b.charCodeAt(i);
++++++++ }
++++++++ }
++++++++ };
++++++++
++++++++ if(algorithm.name === 'PRIMEINC') {
++++++++ return primeincFindPrime(bits, rng, algorithm.options, callback);
++++++++ }
++++++++
++++++++ throw new Error('Invalid prime generation algorithm: ' + algorithm.name);
++++++++};
++++++++
++++++++function primeincFindPrime(bits, rng, options, callback) {
++++++++ if('workers' in options) {
++++++++ return primeincFindPrimeWithWorkers(bits, rng, options, callback);
++++++++ }
++++++++ return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
++++++++}
++++++++
++++++++function primeincFindPrimeWithoutWorkers(bits, rng, options, callback) {
++++++++ // initialize random number
++++++++ var num = generateRandom(bits, rng);
++++++++
++++++++ /* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
++++++++ number we are given is always aligned at 30k + 1. Each time the number is
++++++++ determined not to be prime we add to get to the next 'i', eg: if the number
++++++++ was at 30k + 1 we add 6. */
++++++++ var deltaIdx = 0;
++++++++
++++++++ // get required number of MR tests
++++++++ var mrTests = getMillerRabinTests(num.bitLength());
++++++++ if('millerRabinTests' in options) {
++++++++ mrTests = options.millerRabinTests;
++++++++ }
++++++++
++++++++ // find prime nearest to 'num' for maxBlockTime ms
++++++++ // 10 ms gives 5ms of leeway for other calculations before dropping
++++++++ // below 60fps (1000/60 == 16.67), but in reality, the number will
++++++++ // likely be higher due to an 'atomic' big int modPow
++++++++ var maxBlockTime = 10;
++++++++ if('maxBlockTime' in options) {
++++++++ maxBlockTime = options.maxBlockTime;
++++++++ }
++++++++
++++++++ _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
++++++++}
++++++++
++++++++function _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback) {
++++++++ var start = +new Date();
++++++++ do {
++++++++ // overflow, regenerate random number
++++++++ if(num.bitLength() > bits) {
++++++++ num = generateRandom(bits, rng);
++++++++ }
++++++++ // do primality test
++++++++ if(num.isProbablePrime(mrTests)) {
++++++++ return callback(null, num);
++++++++ }
++++++++ // get next potential prime
++++++++ num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
++++++++ } while(maxBlockTime < 0 || (+new Date() - start < maxBlockTime));
++++++++
++++++++ // keep trying later
++++++++ forge.util.setImmediate(function() {
++++++++ _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
++++++++ });
++++++++}
++++++++
++++++++// NOTE: This algorithm is indeterminate in nature because workers
++++++++// run in parallel looking at different segments of numbers. Even if this
++++++++// algorithm is run twice with the same input from a predictable RNG, it
++++++++// may produce different outputs.
++++++++function primeincFindPrimeWithWorkers(bits, rng, options, callback) {
++++++++ // web workers unavailable
++++++++ if(typeof Worker === 'undefined') {
++++++++ return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
++++++++ }
++++++++
++++++++ // initialize random number
++++++++ var num = generateRandom(bits, rng);
++++++++
++++++++ // use web workers to generate keys
++++++++ var numWorkers = options.workers;
++++++++ var workLoad = options.workLoad || 100;
++++++++ var range = workLoad * 30 / 8;
++++++++ var workerScript = options.workerScript || 'forge/prime.worker.js';
++++++++ if(numWorkers === -1) {
++++++++ return forge.util.estimateCores(function(err, cores) {
++++++++ if(err) {
++++++++ // default to 2
++++++++ cores = 2;
++++++++ }
++++++++ numWorkers = cores - 1;
++++++++ generate();
++++++++ });
++++++++ }
++++++++ generate();
++++++++
++++++++ function generate() {
++++++++ // require at least 1 worker
++++++++ numWorkers = Math.max(1, numWorkers);
++++++++
++++++++ // TODO: consider optimizing by starting workers outside getPrime() ...
++++++++ // note that in order to clean up they will have to be made internally
++++++++ // asynchronous which may actually be slower
++++++++
++++++++ // start workers immediately
++++++++ var workers = [];
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ // FIXME: fix path or use blob URLs
++++++++ workers[i] = new Worker(workerScript);
++++++++ }
++++++++ var running = numWorkers;
++++++++
++++++++ // listen for requests from workers and assign ranges to find prime
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ workers[i].addEventListener('message', workerMessage);
++++++++ }
++++++++
++++++++ /* Note: The distribution of random numbers is unknown. Therefore, each
++++++++ web worker is continuously allocated a range of numbers to check for a
++++++++ random number until one is found.
++++++++
++++++++ Every 30 numbers will be checked just 8 times, because prime numbers
++++++++ have the form:
++++++++
++++++++ 30k+i, for i < 30 and gcd(30, i)=1 (there are 8 values of i for this)
++++++++
++++++++ Therefore, if we want a web worker to run N checks before asking for
++++++++ a new range of numbers, each range must contain N*30/8 numbers.
++++++++
++++++++ For 100 checks (workLoad), this is a range of 375. */
++++++++
++++++++ var found = false;
++++++++ function workerMessage(e) {
++++++++ // ignore message, prime already found
++++++++ if(found) {
++++++++ return;
++++++++ }
++++++++
++++++++ --running;
++++++++ var data = e.data;
++++++++ if(data.found) {
++++++++ // terminate all workers
++++++++ for(var i = 0; i < workers.length; ++i) {
++++++++ workers[i].terminate();
++++++++ }
++++++++ found = true;
++++++++ return callback(null, new BigInteger(data.prime, 16));
++++++++ }
++++++++
++++++++ // overflow, regenerate random number
++++++++ if(num.bitLength() > bits) {
++++++++ num = generateRandom(bits, rng);
++++++++ }
++++++++
++++++++ // assign new range to check
++++++++ var hex = num.toString(16);
++++++++
++++++++ // start prime search
++++++++ e.target.postMessage({
++++++++ hex: hex,
++++++++ workLoad: workLoad
++++++++ });
++++++++
++++++++ num.dAddOffset(range, 0);
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Generates a random number using the given number of bits and RNG.
++++++++ *
++++++++ * @param bits the number of bits for the number.
++++++++ * @param rng the random number generator to use.
++++++++ *
++++++++ * @return the random number.
++++++++ */
++++++++function generateRandom(bits, rng) {
++++++++ var num = new BigInteger(bits, rng);
++++++++ // force MSB set
++++++++ var bits1 = bits - 1;
++++++++ if(!num.testBit(bits1)) {
++++++++ num.bitwiseTo(BigInteger.ONE.shiftLeft(bits1), op_or, num);
++++++++ }
++++++++ // align number on 30k+1 boundary
++++++++ num.dAddOffset(31 - num.mod(THIRTY).byteValue(), 0);
++++++++ return num;
++++++++}
++++++++
++++++++/**
++++++++ * Returns the required number of Miller-Rabin tests to generate a
++++++++ * prime with an error probability of (1/2)^80.
++++++++ *
++++++++ * See Handbook of Applied Cryptography Chapter 4, Table 4.4.
++++++++ *
++++++++ * @param bits the bit size.
++++++++ *
++++++++ * @return the required number of iterations.
++++++++ */
++++++++function getMillerRabinTests(bits) {
++++++++ if(bits <= 100) return 27;
++++++++ if(bits <= 150) return 18;
++++++++ if(bits <= 200) return 15;
++++++++ if(bits <= 250) return 12;
++++++++ if(bits <= 300) return 9;
++++++++ if(bits <= 350) return 8;
++++++++ if(bits <= 400) return 7;
++++++++ if(bits <= 500) return 6;
++++++++ if(bits <= 600) return 5;
++++++++ if(bits <= 800) return 4;
++++++++ if(bits <= 1250) return 3;
++++++++ return 2;
++++++++}
++++++++
++++++++})();
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * RSA Key Generation Worker.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2013 Digital Bazaar, Inc.
++++++++ */
++++++++// worker is built using CommonJS syntax to include all code in one worker file
++++++++//importScripts('jsbn.js');
++++++++var forge = require('./forge');
++++++++require('./jsbn');
++++++++
++++++++// prime constants
++++++++var LOW_PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
++++++++var LP_LIMIT = (1 << 26) / LOW_PRIMES[LOW_PRIMES.length - 1];
++++++++
++++++++var BigInteger = forge.jsbn.BigInteger;
++++++++var BIG_TWO = new BigInteger(null);
++++++++BIG_TWO.fromInt(2);
++++++++
++++++++self.addEventListener('message', function(e) {
++++++++ var result = findPrime(e.data);
++++++++ self.postMessage(result);
++++++++});
++++++++
++++++++// start receiving ranges to check
++++++++self.postMessage({found: false});
++++++++
++++++++// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
++++++++var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
++++++++
++++++++function findPrime(data) {
++++++++ // TODO: abstract based on data.algorithm (PRIMEINC vs. others)
++++++++
++++++++ // create BigInteger from given random bytes
++++++++ var num = new BigInteger(data.hex, 16);
++++++++
++++++++ /* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
++++++++ number we are given is always aligned at 30k + 1. Each time the number is
++++++++ determined not to be prime we add to get to the next 'i', eg: if the number
++++++++ was at 30k + 1 we add 6. */
++++++++ var deltaIdx = 0;
++++++++
++++++++ // find nearest prime
++++++++ var workLoad = data.workLoad;
++++++++ for(var i = 0; i < workLoad; ++i) {
++++++++ // do primality test
++++++++ if(isProbablePrime(num)) {
++++++++ return {found: true, prime: num.toString(16)};
++++++++ }
++++++++ // get next potential prime
++++++++ num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
++++++++ }
++++++++
++++++++ return {found: false};
++++++++}
++++++++
++++++++function isProbablePrime(n) {
++++++++ // divide by low primes, ignore even checks, etc (n alread aligned properly)
++++++++ var i = 1;
++++++++ while(i < LOW_PRIMES.length) {
++++++++ var m = LOW_PRIMES[i];
++++++++ var j = i + 1;
++++++++ while(j < LOW_PRIMES.length && m < LP_LIMIT) {
++++++++ m *= LOW_PRIMES[j++];
++++++++ }
++++++++ m = n.modInt(m);
++++++++ while(i < j) {
++++++++ if(m % LOW_PRIMES[i++] === 0) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++ }
++++++++ return runMillerRabin(n);
++++++++}
++++++++
++++++++// HAC 4.24, Miller-Rabin
++++++++function runMillerRabin(n) {
++++++++ // n1 = n - 1
++++++++ var n1 = n.subtract(BigInteger.ONE);
++++++++
++++++++ // get s and d such that n1 = 2^s * d
++++++++ var s = n1.getLowestSetBit();
++++++++ if(s <= 0) {
++++++++ return false;
++++++++ }
++++++++ var d = n1.shiftRight(s);
++++++++
++++++++ var k = _getMillerRabinTests(n.bitLength());
++++++++ var prng = getPrng();
++++++++ var a;
++++++++ for(var i = 0; i < k; ++i) {
++++++++ // select witness 'a' at random from between 1 and n - 1
++++++++ do {
++++++++ a = new BigInteger(n.bitLength(), prng);
++++++++ } while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
++++++++
++++++++ /* See if 'a' is a composite witness. */
++++++++
++++++++ // x = a^d mod n
++++++++ var x = a.modPow(d, n);
++++++++
++++++++ // probably prime
++++++++ if(x.compareTo(BigInteger.ONE) === 0 || x.compareTo(n1) === 0) {
++++++++ continue;
++++++++ }
++++++++
++++++++ var j = s;
++++++++ while(--j) {
++++++++ // x = x^2 mod a
++++++++ x = x.modPowInt(2, n);
++++++++
++++++++ // 'n' is composite because no previous x == -1 mod n
++++++++ if(x.compareTo(BigInteger.ONE) === 0) {
++++++++ return false;
++++++++ }
++++++++ // x == -1 mod n, so probably prime
++++++++ if(x.compareTo(n1) === 0) {
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ // 'x' is first_x^(n1/2) and is not +/- 1, so 'n' is not prime
++++++++ if(j === 0) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++
++++++++ return true;
++++++++}
++++++++
++++++++// get pseudo random number generator
++++++++function getPrng() {
++++++++ // create prng with api that matches BigInteger secure random
++++++++ return {
++++++++ // x is an array to fill with bytes
++++++++ nextBytes: function(x) {
++++++++ for(var i = 0; i < x.length; ++i) {
++++++++ x[i] = Math.floor(Math.random() * 0xFF);
++++++++ }
++++++++ }
++++++++ };
++++++++}
++++++++
++++++++/**
++++++++ * Returns the required number of Miller-Rabin tests to generate a
++++++++ * prime with an error probability of (1/2)^80.
++++++++ *
++++++++ * See Handbook of Applied Cryptography Chapter 4, Table 4.4.
++++++++ *
++++++++ * @param bits the bit size.
++++++++ *
++++++++ * @return the required number of iterations.
++++++++ */
++++++++function _getMillerRabinTests(bits) {
++++++++ if(bits <= 100) return 27;
++++++++ if(bits <= 150) return 18;
++++++++ if(bits <= 200) return 15;
++++++++ if(bits <= 250) return 12;
++++++++ if(bits <= 300) return 9;
++++++++ if(bits <= 350) return 8;
++++++++ if(bits <= 400) return 7;
++++++++ if(bits <= 500) return 6;
++++++++ if(bits <= 600) return 5;
++++++++ if(bits <= 800) return 4;
++++++++ if(bits <= 1250) return 3;
++++++++ return 2;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * A javascript implementation of a cryptographically-secure
++++++++ * Pseudo Random Number Generator (PRNG). The Fortuna algorithm is followed
++++++++ * here though the use of SHA-256 is not enforced; when generating an
++++++++ * a PRNG context, the hashing algorithm and block cipher used for
++++++++ * the generator are specified via a plugin.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++var _crypto = null;
++++++++if(forge.util.isNodejs && !forge.options.usePureJavaScript &&
++++++++ !process.versions['node-webkit']) {
++++++++ _crypto = require('crypto');
++++++++}
++++++++
++++++++/* PRNG API */
++++++++var prng = module.exports = forge.prng = forge.prng || {};
++++++++
++++++++/**
++++++++ * Creates a new PRNG context.
++++++++ *
++++++++ * A PRNG plugin must be passed in that will provide:
++++++++ *
++++++++ * 1. A function that initializes the key and seed of a PRNG context. It
++++++++ * will be given a 16 byte key and a 16 byte seed. Any key expansion
++++++++ * or transformation of the seed from a byte string into an array of
++++++++ * integers (or similar) should be performed.
++++++++ * 2. The cryptographic function used by the generator. It takes a key and
++++++++ * a seed.
++++++++ * 3. A seed increment function. It takes the seed and returns seed + 1.
++++++++ * 4. An api to create a message digest.
++++++++ *
++++++++ * For an example, see random.js.
++++++++ *
++++++++ * @param plugin the PRNG plugin to use.
++++++++ */
++++++++prng.create = function(plugin) {
++++++++ var ctx = {
++++++++ plugin: plugin,
++++++++ key: null,
++++++++ seed: null,
++++++++ time: null,
++++++++ // number of reseeds so far
++++++++ reseeds: 0,
++++++++ // amount of data generated so far
++++++++ generated: 0,
++++++++ // no initial key bytes
++++++++ keyBytes: ''
++++++++ };
++++++++
++++++++ // create 32 entropy pools (each is a message digest)
++++++++ var md = plugin.md;
++++++++ var pools = new Array(32);
++++++++ for(var i = 0; i < 32; ++i) {
++++++++ pools[i] = md.create();
++++++++ }
++++++++ ctx.pools = pools;
++++++++
++++++++ // entropy pools are written to cyclically, starting at index 0
++++++++ ctx.pool = 0;
++++++++
++++++++ /**
++++++++ * Generates random bytes. The bytes may be generated synchronously or
++++++++ * asynchronously. Web workers must use the asynchronous interface or
++++++++ * else the behavior is undefined.
++++++++ *
++++++++ * @param count the number of random bytes to generate.
++++++++ * @param [callback(err, bytes)] called once the operation completes.
++++++++ *
++++++++ * @return count random bytes as a string.
++++++++ */
++++++++ ctx.generate = function(count, callback) {
++++++++ // do synchronously
++++++++ if(!callback) {
++++++++ return ctx.generateSync(count);
++++++++ }
++++++++
++++++++ // simple generator using counter-based CBC
++++++++ var cipher = ctx.plugin.cipher;
++++++++ var increment = ctx.plugin.increment;
++++++++ var formatKey = ctx.plugin.formatKey;
++++++++ var formatSeed = ctx.plugin.formatSeed;
++++++++ var b = forge.util.createBuffer();
++++++++
++++++++ // paranoid deviation from Fortuna:
++++++++ // reset key for every request to protect previously
++++++++ // generated random bytes should the key be discovered;
++++++++ // there is no 100ms based reseeding because of this
++++++++ // forced reseed for every `generate` call
++++++++ ctx.key = null;
++++++++
++++++++ generate();
++++++++
++++++++ function generate(err) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++
++++++++ // sufficient bytes generated
++++++++ if(b.length() >= count) {
++++++++ return callback(null, b.getBytes(count));
++++++++ }
++++++++
++++++++ // if amount of data generated is greater than 1 MiB, trigger reseed
++++++++ if(ctx.generated > 0xfffff) {
++++++++ ctx.key = null;
++++++++ }
++++++++
++++++++ if(ctx.key === null) {
++++++++ // prevent stack overflow
++++++++ return forge.util.nextTick(function() {
++++++++ _reseed(generate);
++++++++ });
++++++++ }
++++++++
++++++++ // generate the random bytes
++++++++ var bytes = cipher(ctx.key, ctx.seed);
++++++++ ctx.generated += bytes.length;
++++++++ b.putBytes(bytes);
++++++++
++++++++ // generate bytes for a new key and seed
++++++++ ctx.key = formatKey(cipher(ctx.key, increment(ctx.seed)));
++++++++ ctx.seed = formatSeed(cipher(ctx.key, ctx.seed));
++++++++
++++++++ forge.util.setImmediate(generate);
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Generates random bytes synchronously.
++++++++ *
++++++++ * @param count the number of random bytes to generate.
++++++++ *
++++++++ * @return count random bytes as a string.
++++++++ */
++++++++ ctx.generateSync = function(count) {
++++++++ // simple generator using counter-based CBC
++++++++ var cipher = ctx.plugin.cipher;
++++++++ var increment = ctx.plugin.increment;
++++++++ var formatKey = ctx.plugin.formatKey;
++++++++ var formatSeed = ctx.plugin.formatSeed;
++++++++
++++++++ // paranoid deviation from Fortuna:
++++++++ // reset key for every request to protect previously
++++++++ // generated random bytes should the key be discovered;
++++++++ // there is no 100ms based reseeding because of this
++++++++ // forced reseed for every `generateSync` call
++++++++ ctx.key = null;
++++++++
++++++++ var b = forge.util.createBuffer();
++++++++ while(b.length() < count) {
++++++++ // if amount of data generated is greater than 1 MiB, trigger reseed
++++++++ if(ctx.generated > 0xfffff) {
++++++++ ctx.key = null;
++++++++ }
++++++++
++++++++ if(ctx.key === null) {
++++++++ _reseedSync();
++++++++ }
++++++++
++++++++ // generate the random bytes
++++++++ var bytes = cipher(ctx.key, ctx.seed);
++++++++ ctx.generated += bytes.length;
++++++++ b.putBytes(bytes);
++++++++
++++++++ // generate bytes for a new key and seed
++++++++ ctx.key = formatKey(cipher(ctx.key, increment(ctx.seed)));
++++++++ ctx.seed = formatSeed(cipher(ctx.key, ctx.seed));
++++++++ }
++++++++
++++++++ return b.getBytes(count);
++++++++ };
++++++++
++++++++ /**
++++++++ * Private function that asynchronously reseeds a generator.
++++++++ *
++++++++ * @param callback(err) called once the operation completes.
++++++++ */
++++++++ function _reseed(callback) {
++++++++ if(ctx.pools[0].messageLength >= 32) {
++++++++ _seed();
++++++++ return callback();
++++++++ }
++++++++ // not enough seed data...
++++++++ var needed = (32 - ctx.pools[0].messageLength) << 5;
++++++++ ctx.seedFile(needed, function(err, bytes) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ ctx.collect(bytes);
++++++++ _seed();
++++++++ callback();
++++++++ });
++++++++ }
++++++++
++++++++ /**
++++++++ * Private function that synchronously reseeds a generator.
++++++++ */
++++++++ function _reseedSync() {
++++++++ if(ctx.pools[0].messageLength >= 32) {
++++++++ return _seed();
++++++++ }
++++++++ // not enough seed data...
++++++++ var needed = (32 - ctx.pools[0].messageLength) << 5;
++++++++ ctx.collect(ctx.seedFileSync(needed));
++++++++ _seed();
++++++++ }
++++++++
++++++++ /**
++++++++ * Private function that seeds a generator once enough bytes are available.
++++++++ */
++++++++ function _seed() {
++++++++ // update reseed count
++++++++ ctx.reseeds = (ctx.reseeds === 0xffffffff) ? 0 : ctx.reseeds + 1;
++++++++
++++++++ // goal is to update `key` via:
++++++++ // key = hash(key + s)
++++++++ // where 's' is all collected entropy from selected pools, then...
++++++++
++++++++ // create a plugin-based message digest
++++++++ var md = ctx.plugin.md.create();
++++++++
++++++++ // consume current key bytes
++++++++ md.update(ctx.keyBytes);
++++++++
++++++++ // digest the entropy of pools whose index k meet the
++++++++ // condition 'n mod 2^k == 0' where n is the number of reseeds
++++++++ var _2powK = 1;
++++++++ for(var k = 0; k < 32; ++k) {
++++++++ if(ctx.reseeds % _2powK === 0) {
++++++++ md.update(ctx.pools[k].digest().getBytes());
++++++++ ctx.pools[k].start();
++++++++ }
++++++++ _2powK = _2powK << 1;
++++++++ }
++++++++
++++++++ // get digest for key bytes
++++++++ ctx.keyBytes = md.digest().getBytes();
++++++++
++++++++ // paranoid deviation from Fortuna:
++++++++ // update `seed` via `seed = hash(key)`
++++++++ // instead of initializing to zero once and only
++++++++ // ever incrementing it
++++++++ md.start();
++++++++ md.update(ctx.keyBytes);
++++++++ var seedBytes = md.digest().getBytes();
++++++++
++++++++ // update state
++++++++ ctx.key = ctx.plugin.formatKey(ctx.keyBytes);
++++++++ ctx.seed = ctx.plugin.formatSeed(seedBytes);
++++++++ ctx.generated = 0;
++++++++ }
++++++++
++++++++ /**
++++++++ * The built-in default seedFile. This seedFile is used when entropy
++++++++ * is needed immediately.
++++++++ *
++++++++ * @param needed the number of bytes that are needed.
++++++++ *
++++++++ * @return the random bytes.
++++++++ */
++++++++ function defaultSeedFile(needed) {
++++++++ // use window.crypto.getRandomValues strong source of entropy if available
++++++++ var getRandomValues = null;
++++++++ var globalScope = forge.util.globalScope;
++++++++ var _crypto = globalScope.crypto || globalScope.msCrypto;
++++++++ if(_crypto && _crypto.getRandomValues) {
++++++++ getRandomValues = function(arr) {
++++++++ return _crypto.getRandomValues(arr);
++++++++ };
++++++++ }
++++++++
++++++++ var b = forge.util.createBuffer();
++++++++ if(getRandomValues) {
++++++++ while(b.length() < needed) {
++++++++ // max byte length is 65536 before QuotaExceededError is thrown
++++++++ // http://www.w3.org/TR/WebCryptoAPI/#RandomSource-method-getRandomValues
++++++++ var count = Math.max(1, Math.min(needed - b.length(), 65536) / 4);
++++++++ var entropy = new Uint32Array(Math.floor(count));
++++++++ try {
++++++++ getRandomValues(entropy);
++++++++ for(var i = 0; i < entropy.length; ++i) {
++++++++ b.putInt32(entropy[i]);
++++++++ }
++++++++ } catch(e) {
++++++++ /* only ignore QuotaExceededError */
++++++++ if(!(typeof QuotaExceededError !== 'undefined' &&
++++++++ e instanceof QuotaExceededError)) {
++++++++ throw e;
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // be sad and add some weak random data
++++++++ if(b.length() < needed) {
++++++++ /* Draws from Park-Miller "minimal standard" 31 bit PRNG,
++++++++ implemented with David G. Carta's optimization: with 32 bit math
++++++++ and without division (Public Domain). */
++++++++ var hi, lo, next;
++++++++ var seed = Math.floor(Math.random() * 0x010000);
++++++++ while(b.length() < needed) {
++++++++ lo = 16807 * (seed & 0xFFFF);
++++++++ hi = 16807 * (seed >> 16);
++++++++ lo += (hi & 0x7FFF) << 16;
++++++++ lo += hi >> 15;
++++++++ lo = (lo & 0x7FFFFFFF) + (lo >> 31);
++++++++ seed = lo & 0xFFFFFFFF;
++++++++
++++++++ // consume lower 3 bytes of seed
++++++++ for(var i = 0; i < 3; ++i) {
++++++++ // throw in more pseudo random
++++++++ next = seed >>> (i << 3);
++++++++ next ^= Math.floor(Math.random() * 0x0100);
++++++++ b.putByte(next & 0xFF);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return b.getBytes(needed);
++++++++ }
++++++++ // initialize seed file APIs
++++++++ if(_crypto) {
++++++++ // use nodejs async API
++++++++ ctx.seedFile = function(needed, callback) {
++++++++ _crypto.randomBytes(needed, function(err, bytes) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ callback(null, bytes.toString());
++++++++ });
++++++++ };
++++++++ // use nodejs sync API
++++++++ ctx.seedFileSync = function(needed) {
++++++++ return _crypto.randomBytes(needed).toString();
++++++++ };
++++++++ } else {
++++++++ ctx.seedFile = function(needed, callback) {
++++++++ try {
++++++++ callback(null, defaultSeedFile(needed));
++++++++ } catch(e) {
++++++++ callback(e);
++++++++ }
++++++++ };
++++++++ ctx.seedFileSync = defaultSeedFile;
++++++++ }
++++++++
++++++++ /**
++++++++ * Adds entropy to a prng ctx's accumulator.
++++++++ *
++++++++ * @param bytes the bytes of entropy as a string.
++++++++ */
++++++++ ctx.collect = function(bytes) {
++++++++ // iterate over pools distributing entropy cyclically
++++++++ var count = bytes.length;
++++++++ for(var i = 0; i < count; ++i) {
++++++++ ctx.pools[ctx.pool].update(bytes.substr(i, 1));
++++++++ ctx.pool = (ctx.pool === 31) ? 0 : ctx.pool + 1;
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Collects an integer of n bits.
++++++++ *
++++++++ * @param i the integer entropy.
++++++++ * @param n the number of bits in the integer.
++++++++ */
++++++++ ctx.collectInt = function(i, n) {
++++++++ var bytes = '';
++++++++ for(var x = 0; x < n; x += 8) {
++++++++ bytes += String.fromCharCode((i >> x) & 0xFF);
++++++++ }
++++++++ ctx.collect(bytes);
++++++++ };
++++++++
++++++++ /**
++++++++ * Registers a Web Worker to receive immediate entropy from the main thread.
++++++++ * This method is required until Web Workers can access the native crypto
++++++++ * API. This method should be called twice for each created worker, once in
++++++++ * the main thread, and once in the worker itself.
++++++++ *
++++++++ * @param worker the worker to register.
++++++++ */
++++++++ ctx.registerWorker = function(worker) {
++++++++ // worker receives random bytes
++++++++ if(worker === self) {
++++++++ ctx.seedFile = function(needed, callback) {
++++++++ function listener(e) {
++++++++ var data = e.data;
++++++++ if(data.forge && data.forge.prng) {
++++++++ self.removeEventListener('message', listener);
++++++++ callback(data.forge.prng.err, data.forge.prng.bytes);
++++++++ }
++++++++ }
++++++++ self.addEventListener('message', listener);
++++++++ self.postMessage({forge: {prng: {needed: needed}}});
++++++++ };
++++++++ } else {
++++++++ // main thread sends random bytes upon request
++++++++ var listener = function(e) {
++++++++ var data = e.data;
++++++++ if(data.forge && data.forge.prng) {
++++++++ ctx.seedFile(data.forge.prng.needed, function(err, bytes) {
++++++++ worker.postMessage({forge: {prng: {err: err, bytes: bytes}}});
++++++++ });
++++++++ }
++++++++ };
++++++++ // TODO: do we need to remove the event listener when the worker dies?
++++++++ worker.addEventListener('message', listener);
++++++++ }
++++++++ };
++++++++
++++++++ return ctx;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of PKCS#1 PSS signature padding.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ *
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./random');
++++++++require('./util');
++++++++
++++++++// shortcut for PSS API
++++++++var pss = module.exports = forge.pss = forge.pss || {};
++++++++
++++++++/**
++++++++ * Creates a PSS signature scheme object.
++++++++ *
++++++++ * There are several ways to provide a salt for encoding:
++++++++ *
++++++++ * 1. Specify the saltLength only and the built-in PRNG will generate it.
++++++++ * 2. Specify the saltLength and a custom PRNG with 'getBytesSync' defined that
++++++++ * will be used.
++++++++ * 3. Specify the salt itself as a forge.util.ByteBuffer.
++++++++ *
++++++++ * @param options the options to use:
++++++++ * md the message digest object to use, a forge md instance.
++++++++ * mgf the mask generation function to use, a forge mgf instance.
++++++++ * [saltLength] the length of the salt in octets.
++++++++ * [prng] the pseudo-random number generator to use to produce a salt.
++++++++ * [salt] the salt to use when encoding.
++++++++ *
++++++++ * @return a signature scheme object.
++++++++ */
++++++++pss.create = function(options) {
++++++++ // backwards compatibility w/legacy args: hash, mgf, sLen
++++++++ if(arguments.length === 3) {
++++++++ options = {
++++++++ md: arguments[0],
++++++++ mgf: arguments[1],
++++++++ saltLength: arguments[2]
++++++++ };
++++++++ }
++++++++
++++++++ var hash = options.md;
++++++++ var mgf = options.mgf;
++++++++ var hLen = hash.digestLength;
++++++++
++++++++ var salt_ = options.salt || null;
++++++++ if(typeof salt_ === 'string') {
++++++++ // assume binary-encoded string
++++++++ salt_ = forge.util.createBuffer(salt_);
++++++++ }
++++++++
++++++++ var sLen;
++++++++ if('saltLength' in options) {
++++++++ sLen = options.saltLength;
++++++++ } else if(salt_ !== null) {
++++++++ sLen = salt_.length();
++++++++ } else {
++++++++ throw new Error('Salt length not specified or specific salt not given.');
++++++++ }
++++++++
++++++++ if(salt_ !== null && salt_.length() !== sLen) {
++++++++ throw new Error('Given salt length does not match length of given salt.');
++++++++ }
++++++++
++++++++ var prng = options.prng || forge.random;
++++++++
++++++++ var pssobj = {};
++++++++
++++++++ /**
++++++++ * Encodes a PSS signature.
++++++++ *
++++++++ * This function implements EMSA-PSS-ENCODE as per RFC 3447, section 9.1.1.
++++++++ *
++++++++ * @param md the message digest object with the hash to sign.
++++++++ * @param modsBits the length of the RSA modulus in bits.
++++++++ *
++++++++ * @return the encoded message as a binary-encoded string of length
++++++++ * ceil((modBits - 1) / 8).
++++++++ */
++++++++ pssobj.encode = function(md, modBits) {
++++++++ var i;
++++++++ var emBits = modBits - 1;
++++++++ var emLen = Math.ceil(emBits / 8);
++++++++
++++++++ /* 2. Let mHash = Hash(M), an octet string of length hLen. */
++++++++ var mHash = md.digest().getBytes();
++++++++
++++++++ /* 3. If emLen < hLen + sLen + 2, output "encoding error" and stop. */
++++++++ if(emLen < hLen + sLen + 2) {
++++++++ throw new Error('Message is too long to encrypt.');
++++++++ }
++++++++
++++++++ /* 4. Generate a random octet string salt of length sLen; if sLen = 0,
++++++++ * then salt is the empty string. */
++++++++ var salt;
++++++++ if(salt_ === null) {
++++++++ salt = prng.getBytesSync(sLen);
++++++++ } else {
++++++++ salt = salt_.bytes();
++++++++ }
++++++++
++++++++ /* 5. Let M' = (0x)00 00 00 00 00 00 00 00 || mHash || salt; */
++++++++ var m_ = new forge.util.ByteBuffer();
++++++++ m_.fillWithByte(0, 8);
++++++++ m_.putBytes(mHash);
++++++++ m_.putBytes(salt);
++++++++
++++++++ /* 6. Let H = Hash(M'), an octet string of length hLen. */
++++++++ hash.start();
++++++++ hash.update(m_.getBytes());
++++++++ var h = hash.digest().getBytes();
++++++++
++++++++ /* 7. Generate an octet string PS consisting of emLen - sLen - hLen - 2
++++++++ * zero octets. The length of PS may be 0. */
++++++++ var ps = new forge.util.ByteBuffer();
++++++++ ps.fillWithByte(0, emLen - sLen - hLen - 2);
++++++++
++++++++ /* 8. Let DB = PS || 0x01 || salt; DB is an octet string of length
++++++++ * emLen - hLen - 1. */
++++++++ ps.putByte(0x01);
++++++++ ps.putBytes(salt);
++++++++ var db = ps.getBytes();
++++++++
++++++++ /* 9. Let dbMask = MGF(H, emLen - hLen - 1). */
++++++++ var maskLen = emLen - hLen - 1;
++++++++ var dbMask = mgf.generate(h, maskLen);
++++++++
++++++++ /* 10. Let maskedDB = DB \xor dbMask. */
++++++++ var maskedDB = '';
++++++++ for(i = 0; i < maskLen; i++) {
++++++++ maskedDB += String.fromCharCode(db.charCodeAt(i) ^ dbMask.charCodeAt(i));
++++++++ }
++++++++
++++++++ /* 11. Set the leftmost 8emLen - emBits bits of the leftmost octet in
++++++++ * maskedDB to zero. */
++++++++ var mask = (0xFF00 >> (8 * emLen - emBits)) & 0xFF;
++++++++ maskedDB = String.fromCharCode(maskedDB.charCodeAt(0) & ~mask) +
++++++++ maskedDB.substr(1);
++++++++
++++++++ /* 12. Let EM = maskedDB || H || 0xbc.
++++++++ * 13. Output EM. */
++++++++ return maskedDB + h + String.fromCharCode(0xbc);
++++++++ };
++++++++
++++++++ /**
++++++++ * Verifies a PSS signature.
++++++++ *
++++++++ * This function implements EMSA-PSS-VERIFY as per RFC 3447, section 9.1.2.
++++++++ *
++++++++ * @param mHash the message digest hash, as a binary-encoded string, to
++++++++ * compare against the signature.
++++++++ * @param em the encoded message, as a binary-encoded string
++++++++ * (RSA decryption result).
++++++++ * @param modsBits the length of the RSA modulus in bits.
++++++++ *
++++++++ * @return true if the signature was verified, false if not.
++++++++ */
++++++++ pssobj.verify = function(mHash, em, modBits) {
++++++++ var i;
++++++++ var emBits = modBits - 1;
++++++++ var emLen = Math.ceil(emBits / 8);
++++++++
++++++++ /* c. Convert the message representative m to an encoded message EM
++++++++ * of length emLen = ceil((modBits - 1) / 8) octets, where modBits
++++++++ * is the length in bits of the RSA modulus n */
++++++++ em = em.substr(-emLen);
++++++++
++++++++ /* 3. If emLen < hLen + sLen + 2, output "inconsistent" and stop. */
++++++++ if(emLen < hLen + sLen + 2) {
++++++++ throw new Error('Inconsistent parameters to PSS signature verification.');
++++++++ }
++++++++
++++++++ /* 4. If the rightmost octet of EM does not have hexadecimal value
++++++++ * 0xbc, output "inconsistent" and stop. */
++++++++ if(em.charCodeAt(emLen - 1) !== 0xbc) {
++++++++ throw new Error('Encoded message does not end in 0xBC.');
++++++++ }
++++++++
++++++++ /* 5. Let maskedDB be the leftmost emLen - hLen - 1 octets of EM, and
++++++++ * let H be the next hLen octets. */
++++++++ var maskLen = emLen - hLen - 1;
++++++++ var maskedDB = em.substr(0, maskLen);
++++++++ var h = em.substr(maskLen, hLen);
++++++++
++++++++ /* 6. If the leftmost 8emLen - emBits bits of the leftmost octet in
++++++++ * maskedDB are not all equal to zero, output "inconsistent" and stop. */
++++++++ var mask = (0xFF00 >> (8 * emLen - emBits)) & 0xFF;
++++++++ if((maskedDB.charCodeAt(0) & mask) !== 0) {
++++++++ throw new Error('Bits beyond keysize not zero as expected.');
++++++++ }
++++++++
++++++++ /* 7. Let dbMask = MGF(H, emLen - hLen - 1). */
++++++++ var dbMask = mgf.generate(h, maskLen);
++++++++
++++++++ /* 8. Let DB = maskedDB \xor dbMask. */
++++++++ var db = '';
++++++++ for(i = 0; i < maskLen; i++) {
++++++++ db += String.fromCharCode(maskedDB.charCodeAt(i) ^ dbMask.charCodeAt(i));
++++++++ }
++++++++
++++++++ /* 9. Set the leftmost 8emLen - emBits bits of the leftmost octet
++++++++ * in DB to zero. */
++++++++ db = String.fromCharCode(db.charCodeAt(0) & ~mask) + db.substr(1);
++++++++
++++++++ /* 10. If the emLen - hLen - sLen - 2 leftmost octets of DB are not zero
++++++++ * or if the octet at position emLen - hLen - sLen - 1 (the leftmost
++++++++ * position is "position 1") does not have hexadecimal value 0x01,
++++++++ * output "inconsistent" and stop. */
++++++++ var checkLen = emLen - hLen - sLen - 2;
++++++++ for(i = 0; i < checkLen; i++) {
++++++++ if(db.charCodeAt(i) !== 0x00) {
++++++++ throw new Error('Leftmost octets not zero as expected');
++++++++ }
++++++++ }
++++++++
++++++++ if(db.charCodeAt(checkLen) !== 0x01) {
++++++++ throw new Error('Inconsistent PSS signature, 0x01 marker not found');
++++++++ }
++++++++
++++++++ /* 11. Let salt be the last sLen octets of DB. */
++++++++ var salt = db.substr(-sLen);
++++++++
++++++++ /* 12. Let M' = (0x)00 00 00 00 00 00 00 00 || mHash || salt */
++++++++ var m_ = new forge.util.ByteBuffer();
++++++++ m_.fillWithByte(0, 8);
++++++++ m_.putBytes(mHash);
++++++++ m_.putBytes(salt);
++++++++
++++++++ /* 13. Let H' = Hash(M'), an octet string of length hLen. */
++++++++ hash.start();
++++++++ hash.update(m_.getBytes());
++++++++ var h_ = hash.digest().getBytes();
++++++++
++++++++ /* 14. If H = H', output "consistent." Otherwise, output "inconsistent." */
++++++++ return h === h_;
++++++++ };
++++++++
++++++++ return pssobj;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * An API for getting cryptographically-secure random bytes. The bytes are
++++++++ * generated using the Fortuna algorithm devised by Bruce Schneier and
++++++++ * Niels Ferguson.
++++++++ *
++++++++ * Getting strong random bytes is not yet easy to do in javascript. The only
++++++++ * truish random entropy that can be collected is from the mouse, keyboard, or
++++++++ * from timing with respect to page loads, etc. This generator makes a poor
++++++++ * attempt at providing random bytes when those sources haven't yet provided
++++++++ * enough entropy to initially seed or to reseed the PRNG.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2009-2014 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./sha256');
++++++++require('./prng');
++++++++require('./util');
++++++++
++++++++(function() {
++++++++
++++++++// forge.random already defined
++++++++if(forge.random && forge.random.getBytes) {
++++++++ module.exports = forge.random;
++++++++ return;
++++++++}
++++++++
++++++++(function(jQuery) {
++++++++
++++++++// the default prng plugin, uses AES-128
++++++++var prng_aes = {};
++++++++var _prng_aes_output = new Array(4);
++++++++var _prng_aes_buffer = forge.util.createBuffer();
++++++++prng_aes.formatKey = function(key) {
++++++++ // convert the key into 32-bit integers
++++++++ var tmp = forge.util.createBuffer(key);
++++++++ key = new Array(4);
++++++++ key[0] = tmp.getInt32();
++++++++ key[1] = tmp.getInt32();
++++++++ key[2] = tmp.getInt32();
++++++++ key[3] = tmp.getInt32();
++++++++
++++++++ // return the expanded key
++++++++ return forge.aes._expandKey(key, false);
++++++++};
++++++++prng_aes.formatSeed = function(seed) {
++++++++ // convert seed into 32-bit integers
++++++++ var tmp = forge.util.createBuffer(seed);
++++++++ seed = new Array(4);
++++++++ seed[0] = tmp.getInt32();
++++++++ seed[1] = tmp.getInt32();
++++++++ seed[2] = tmp.getInt32();
++++++++ seed[3] = tmp.getInt32();
++++++++ return seed;
++++++++};
++++++++prng_aes.cipher = function(key, seed) {
++++++++ forge.aes._updateBlock(key, seed, _prng_aes_output, false);
++++++++ _prng_aes_buffer.putInt32(_prng_aes_output[0]);
++++++++ _prng_aes_buffer.putInt32(_prng_aes_output[1]);
++++++++ _prng_aes_buffer.putInt32(_prng_aes_output[2]);
++++++++ _prng_aes_buffer.putInt32(_prng_aes_output[3]);
++++++++ return _prng_aes_buffer.getBytes();
++++++++};
++++++++prng_aes.increment = function(seed) {
++++++++ // FIXME: do we care about carry or signed issues?
++++++++ ++seed[3];
++++++++ return seed;
++++++++};
++++++++prng_aes.md = forge.md.sha256;
++++++++
++++++++/**
++++++++ * Creates a new PRNG.
++++++++ */
++++++++function spawnPrng() {
++++++++ var ctx = forge.prng.create(prng_aes);
++++++++
++++++++ /**
++++++++ * Gets random bytes. If a native secure crypto API is unavailable, this
++++++++ * method tries to make the bytes more unpredictable by drawing from data that
++++++++ * can be collected from the user of the browser, eg: mouse movement.
++++++++ *
++++++++ * If a callback is given, this method will be called asynchronously.
++++++++ *
++++++++ * @param count the number of random bytes to get.
++++++++ * @param [callback(err, bytes)] called once the operation completes.
++++++++ *
++++++++ * @return the random bytes in a string.
++++++++ */
++++++++ ctx.getBytes = function(count, callback) {
++++++++ return ctx.generate(count, callback);
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets random bytes asynchronously. If a native secure crypto API is
++++++++ * unavailable, this method tries to make the bytes more unpredictable by
++++++++ * drawing from data that can be collected from the user of the browser,
++++++++ * eg: mouse movement.
++++++++ *
++++++++ * @param count the number of random bytes to get.
++++++++ *
++++++++ * @return the random bytes in a string.
++++++++ */
++++++++ ctx.getBytesSync = function(count) {
++++++++ return ctx.generate(count);
++++++++ };
++++++++
++++++++ return ctx;
++++++++}
++++++++
++++++++// create default prng context
++++++++var _ctx = spawnPrng();
++++++++
++++++++// add other sources of entropy only if window.crypto.getRandomValues is not
++++++++// available -- otherwise this source will be automatically used by the prng
++++++++var getRandomValues = null;
++++++++var globalScope = forge.util.globalScope;
++++++++var _crypto = globalScope.crypto || globalScope.msCrypto;
++++++++if(_crypto && _crypto.getRandomValues) {
++++++++ getRandomValues = function(arr) {
++++++++ return _crypto.getRandomValues(arr);
++++++++ };
++++++++}
++++++++
++++++++if(forge.options.usePureJavaScript ||
++++++++ (!forge.util.isNodejs && !getRandomValues)) {
++++++++ // if this is a web worker, do not use weak entropy, instead register to
++++++++ // receive strong entropy asynchronously from the main thread
++++++++ if(typeof window === 'undefined' || window.document === undefined) {
++++++++ // FIXME:
++++++++ }
++++++++
++++++++ // get load time entropy
++++++++ _ctx.collectInt(+new Date(), 32);
++++++++
++++++++ // add some entropy from navigator object
++++++++ if(typeof(navigator) !== 'undefined') {
++++++++ var _navBytes = '';
++++++++ for(var key in navigator) {
++++++++ try {
++++++++ if(typeof(navigator[key]) == 'string') {
++++++++ _navBytes += navigator[key];
++++++++ }
++++++++ } catch(e) {
++++++++ /* Some navigator keys might not be accessible, e.g. the geolocation
++++++++ attribute throws an exception if touched in Mozilla chrome://
++++++++ context.
++++++++
++++++++ Silently ignore this and just don't use this as a source of
++++++++ entropy. */
++++++++ }
++++++++ }
++++++++ _ctx.collect(_navBytes);
++++++++ _navBytes = null;
++++++++ }
++++++++
++++++++ // add mouse and keyboard collectors if jquery is available
++++++++ if(jQuery) {
++++++++ // set up mouse entropy capture
++++++++ jQuery().mousemove(function(e) {
++++++++ // add mouse coords
++++++++ _ctx.collectInt(e.clientX, 16);
++++++++ _ctx.collectInt(e.clientY, 16);
++++++++ });
++++++++
++++++++ // set up keyboard entropy capture
++++++++ jQuery().keypress(function(e) {
++++++++ _ctx.collectInt(e.charCode, 8);
++++++++ });
++++++++ }
++++++++}
++++++++
++++++++/* Random API */
++++++++if(!forge.random) {
++++++++ forge.random = _ctx;
++++++++} else {
++++++++ // extend forge.random with _ctx
++++++++ for(var key in _ctx) {
++++++++ forge.random[key] = _ctx[key];
++++++++ }
++++++++}
++++++++
++++++++// expose spawn PRNG
++++++++forge.random.createInstance = spawnPrng;
++++++++
++++++++module.exports = forge.random;
++++++++
++++++++})(typeof(jQuery) !== 'undefined' ? jQuery : null);
++++++++
++++++++})();
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * RC2 implementation.
++++++++ *
++++++++ * @author Stefan Siegl
++++++++ *
++++++++ * Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
++++++++ *
++++++++ * Information on the RC2 cipher is available from RFC #2268,
++++++++ * http://www.ietf.org/rfc/rfc2268.txt
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++var piTable = [
++++++++ 0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
++++++++ 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
++++++++ 0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
++++++++ 0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
++++++++ 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
++++++++ 0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
++++++++ 0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
++++++++ 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
++++++++ 0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
++++++++ 0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
++++++++ 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
++++++++ 0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
++++++++ 0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
++++++++ 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
++++++++ 0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
++++++++ 0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad
++++++++];
++++++++
++++++++var s = [1, 2, 3, 5];
++++++++
++++++++/**
++++++++ * Rotate a word left by given number of bits.
++++++++ *
++++++++ * Bits that are shifted out on the left are put back in on the right
++++++++ * hand side.
++++++++ *
++++++++ * @param word The word to shift left.
++++++++ * @param bits The number of bits to shift by.
++++++++ * @return The rotated word.
++++++++ */
++++++++var rol = function(word, bits) {
++++++++ return ((word << bits) & 0xffff) | ((word & 0xffff) >> (16 - bits));
++++++++};
++++++++
++++++++/**
++++++++ * Rotate a word right by given number of bits.
++++++++ *
++++++++ * Bits that are shifted out on the right are put back in on the left
++++++++ * hand side.
++++++++ *
++++++++ * @param word The word to shift right.
++++++++ * @param bits The number of bits to shift by.
++++++++ * @return The rotated word.
++++++++ */
++++++++var ror = function(word, bits) {
++++++++ return ((word & 0xffff) >> bits) | ((word << (16 - bits)) & 0xffff);
++++++++};
++++++++
++++++++/* RC2 API */
++++++++module.exports = forge.rc2 = forge.rc2 || {};
++++++++
++++++++/**
++++++++ * Perform RC2 key expansion as per RFC #2268, section 2.
++++++++ *
++++++++ * @param key variable-length user key (between 1 and 128 bytes)
++++++++ * @param effKeyBits number of effective key bits (default: 128)
++++++++ * @return the expanded RC2 key (ByteBuffer of 128 bytes)
++++++++ */
++++++++forge.rc2.expandKey = function(key, effKeyBits) {
++++++++ if(typeof key === 'string') {
++++++++ key = forge.util.createBuffer(key);
++++++++ }
++++++++ effKeyBits = effKeyBits || 128;
++++++++
++++++++ /* introduce variables that match the names used in RFC #2268 */
++++++++ var L = key;
++++++++ var T = key.length();
++++++++ var T1 = effKeyBits;
++++++++ var T8 = Math.ceil(T1 / 8);
++++++++ var TM = 0xff >> (T1 & 0x07);
++++++++ var i;
++++++++
++++++++ for(i = T; i < 128; i++) {
++++++++ L.putByte(piTable[(L.at(i - 1) + L.at(i - T)) & 0xff]);
++++++++ }
++++++++
++++++++ L.setAt(128 - T8, piTable[L.at(128 - T8) & TM]);
++++++++
++++++++ for(i = 127 - T8; i >= 0; i--) {
++++++++ L.setAt(i, piTable[L.at(i + 1) ^ L.at(i + T8)]);
++++++++ }
++++++++
++++++++ return L;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a RC2 cipher object.
++++++++ *
++++++++ * @param key the symmetric key to use (as base for key generation).
++++++++ * @param bits the number of effective key bits.
++++++++ * @param encrypt false for decryption, true for encryption.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++var createCipher = function(key, bits, encrypt) {
++++++++ var _finish = false, _input = null, _output = null, _iv = null;
++++++++ var mixRound, mashRound;
++++++++ var i, j, K = [];
++++++++
++++++++ /* Expand key and fill into K[] Array */
++++++++ key = forge.rc2.expandKey(key, bits);
++++++++ for(i = 0; i < 64; i++) {
++++++++ K.push(key.getInt16Le());
++++++++ }
++++++++
++++++++ if(encrypt) {
++++++++ /**
++++++++ * Perform one mixing round "in place".
++++++++ *
++++++++ * @param R Array of four words to perform mixing on.
++++++++ */
++++++++ mixRound = function(R) {
++++++++ for(i = 0; i < 4; i++) {
++++++++ R[i] += K[j] + (R[(i + 3) % 4] & R[(i + 2) % 4]) +
++++++++ ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
++++++++ R[i] = rol(R[i], s[i]);
++++++++ j++;
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Perform one mashing round "in place".
++++++++ *
++++++++ * @param R Array of four words to perform mashing on.
++++++++ */
++++++++ mashRound = function(R) {
++++++++ for(i = 0; i < 4; i++) {
++++++++ R[i] += K[R[(i + 3) % 4] & 63];
++++++++ }
++++++++ };
++++++++ } else {
++++++++ /**
++++++++ * Perform one r-mixing round "in place".
++++++++ *
++++++++ * @param R Array of four words to perform mixing on.
++++++++ */
++++++++ mixRound = function(R) {
++++++++ for(i = 3; i >= 0; i--) {
++++++++ R[i] = ror(R[i], s[i]);
++++++++ R[i] -= K[j] + (R[(i + 3) % 4] & R[(i + 2) % 4]) +
++++++++ ((~R[(i + 3) % 4]) & R[(i + 1) % 4]);
++++++++ j--;
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Perform one r-mashing round "in place".
++++++++ *
++++++++ * @param R Array of four words to perform mashing on.
++++++++ */
++++++++ mashRound = function(R) {
++++++++ for(i = 3; i >= 0; i--) {
++++++++ R[i] -= K[R[(i + 3) % 4] & 63];
++++++++ }
++++++++ };
++++++++ }
++++++++
++++++++ /**
++++++++ * Run the specified cipher execution plan.
++++++++ *
++++++++ * This function takes four words from the input buffer, applies the IV on
++++++++ * it (if requested) and runs the provided execution plan.
++++++++ *
++++++++ * The plan must be put together in form of a array of arrays. Where the
++++++++ * outer one is simply a list of steps to perform and the inner one needs
++++++++ * to have two elements: the first one telling how many rounds to perform,
++++++++ * the second one telling what to do (i.e. the function to call).
++++++++ *
++++++++ * @param {Array} plan The plan to execute.
++++++++ */
++++++++ var runPlan = function(plan) {
++++++++ var R = [];
++++++++
++++++++ /* Get data from input buffer and fill the four words into R */
++++++++ for(i = 0; i < 4; i++) {
++++++++ var val = _input.getInt16Le();
++++++++
++++++++ if(_iv !== null) {
++++++++ if(encrypt) {
++++++++ /* We're encrypting, apply the IV first. */
++++++++ val ^= _iv.getInt16Le();
++++++++ } else {
++++++++ /* We're decryption, keep cipher text for next block. */
++++++++ _iv.putInt16Le(val);
++++++++ }
++++++++ }
++++++++
++++++++ R.push(val & 0xffff);
++++++++ }
++++++++
++++++++ /* Reset global "j" variable as per spec. */
++++++++ j = encrypt ? 0 : 63;
++++++++
++++++++ /* Run execution plan. */
++++++++ for(var ptr = 0; ptr < plan.length; ptr++) {
++++++++ for(var ctr = 0; ctr < plan[ptr][0]; ctr++) {
++++++++ plan[ptr][1](R);
++++++++ }
++++++++ }
++++++++
++++++++ /* Write back result to output buffer. */
++++++++ for(i = 0; i < 4; i++) {
++++++++ if(_iv !== null) {
++++++++ if(encrypt) {
++++++++ /* We're encrypting in CBC-mode, feed back encrypted bytes into
++++++++ IV buffer to carry it forward to next block. */
++++++++ _iv.putInt16Le(R[i]);
++++++++ } else {
++++++++ R[i] ^= _iv.getInt16Le();
++++++++ }
++++++++ }
++++++++
++++++++ _output.putInt16Le(R[i]);
++++++++ }
++++++++ };
++++++++
++++++++ /* Create cipher object */
++++++++ var cipher = null;
++++++++ cipher = {
++++++++ /**
++++++++ * Starts or restarts the encryption or decryption process, whichever
++++++++ * was previously configured.
++++++++ *
++++++++ * To use the cipher in CBC mode, iv may be given either as a string
++++++++ * of bytes, or as a byte buffer. For ECB mode, give null as iv.
++++++++ *
++++++++ * @param iv the initialization vector to use, null for ECB mode.
++++++++ * @param output the output the buffer to write to, null to create one.
++++++++ */
++++++++ start: function(iv, output) {
++++++++ if(iv) {
++++++++ /* CBC mode */
++++++++ if(typeof iv === 'string') {
++++++++ iv = forge.util.createBuffer(iv);
++++++++ }
++++++++ }
++++++++
++++++++ _finish = false;
++++++++ _input = forge.util.createBuffer();
++++++++ _output = output || new forge.util.createBuffer();
++++++++ _iv = iv;
++++++++
++++++++ cipher.output = _output;
++++++++ },
++++++++
++++++++ /**
++++++++ * Updates the next block.
++++++++ *
++++++++ * @param input the buffer to read from.
++++++++ */
++++++++ update: function(input) {
++++++++ if(!_finish) {
++++++++ // not finishing, so fill the input buffer with more input
++++++++ _input.putBuffer(input);
++++++++ }
++++++++
++++++++ while(_input.length() >= 8) {
++++++++ runPlan([
++++++++ [ 5, mixRound ],
++++++++ [ 1, mashRound ],
++++++++ [ 6, mixRound ],
++++++++ [ 1, mashRound ],
++++++++ [ 5, mixRound ]
++++++++ ]);
++++++++ }
++++++++ },
++++++++
++++++++ /**
++++++++ * Finishes encrypting or decrypting.
++++++++ *
++++++++ * @param pad a padding function to use, null for PKCS#7 padding,
++++++++ * signature(blockSize, buffer, decrypt).
++++++++ *
++++++++ * @return true if successful, false on error.
++++++++ */
++++++++ finish: function(pad) {
++++++++ var rval = true;
++++++++
++++++++ if(encrypt) {
++++++++ if(pad) {
++++++++ rval = pad(8, _input, !encrypt);
++++++++ } else {
++++++++ // add PKCS#7 padding to block (each pad byte is the
++++++++ // value of the number of pad bytes)
++++++++ var padding = (_input.length() === 8) ? 8 : (8 - _input.length());
++++++++ _input.fillWithByte(padding, padding);
++++++++ }
++++++++ }
++++++++
++++++++ if(rval) {
++++++++ // do final update
++++++++ _finish = true;
++++++++ cipher.update();
++++++++ }
++++++++
++++++++ if(!encrypt) {
++++++++ // check for error: input data not a multiple of block size
++++++++ rval = (_input.length() === 0);
++++++++ if(rval) {
++++++++ if(pad) {
++++++++ rval = pad(8, _output, !encrypt);
++++++++ } else {
++++++++ // ensure padding byte count is valid
++++++++ var len = _output.length();
++++++++ var count = _output.at(len - 1);
++++++++
++++++++ if(count > len) {
++++++++ rval = false;
++++++++ } else {
++++++++ // trim off padding bytes
++++++++ _output.truncate(count);
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ }
++++++++ };
++++++++
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an RC2 cipher object to encrypt data in ECB or CBC mode using the
++++++++ * given symmetric key. The output will be stored in the 'output' member
++++++++ * of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as a string of bytes or a byte buffer.
++++++++ * The cipher is initialized to use 128 effective key bits.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.rc2.startEncrypting = function(key, iv, output) {
++++++++ var cipher = forge.rc2.createEncryptionCipher(key, 128);
++++++++ cipher.start(iv, output);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an RC2 cipher object to encrypt data in ECB or CBC mode using the
++++++++ * given symmetric key.
++++++++ *
++++++++ * The key may be given as a string of bytes or a byte buffer.
++++++++ *
++++++++ * To start encrypting call start() on the cipher with an iv and optional
++++++++ * output buffer.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.rc2.createEncryptionCipher = function(key, bits) {
++++++++ return createCipher(key, bits, true);
++++++++};
++++++++
++++++++/**
++++++++ * Creates an RC2 cipher object to decrypt data in ECB or CBC mode using the
++++++++ * given symmetric key. The output will be stored in the 'output' member
++++++++ * of the returned cipher.
++++++++ *
++++++++ * The key and iv may be given as a string of bytes or a byte buffer.
++++++++ * The cipher is initialized to use 128 effective key bits.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ * @param iv the initialization vector to use.
++++++++ * @param output the buffer to write to, null to create one.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.rc2.startDecrypting = function(key, iv, output) {
++++++++ var cipher = forge.rc2.createDecryptionCipher(key, 128);
++++++++ cipher.start(iv, output);
++++++++ return cipher;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an RC2 cipher object to decrypt data in ECB or CBC mode using the
++++++++ * given symmetric key.
++++++++ *
++++++++ * The key may be given as a string of bytes or a byte buffer.
++++++++ *
++++++++ * To start decrypting call start() on the cipher with an iv and optional
++++++++ * output buffer.
++++++++ *
++++++++ * @param key the symmetric key to use.
++++++++ *
++++++++ * @return the cipher.
++++++++ */
++++++++forge.rc2.createDecryptionCipher = function(key, bits) {
++++++++ return createCipher(key, bits, false);
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of basic RSA algorithms.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ *
++++++++ * The only algorithm currently supported for PKI is RSA.
++++++++ *
++++++++ * An RSA key is often stored in ASN.1 DER format. The SubjectPublicKeyInfo
++++++++ * ASN.1 structure is composed of an algorithm of type AlgorithmIdentifier
++++++++ * and a subjectPublicKey of type bit string.
++++++++ *
++++++++ * The AlgorithmIdentifier contains an Object Identifier (OID) and parameters
++++++++ * for the algorithm, if any. In the case of RSA, there aren't any.
++++++++ *
++++++++ * SubjectPublicKeyInfo ::= SEQUENCE {
++++++++ * algorithm AlgorithmIdentifier,
++++++++ * subjectPublicKey BIT STRING
++++++++ * }
++++++++ *
++++++++ * AlgorithmIdentifer ::= SEQUENCE {
++++++++ * algorithm OBJECT IDENTIFIER,
++++++++ * parameters ANY DEFINED BY algorithm OPTIONAL
++++++++ * }
++++++++ *
++++++++ * For an RSA public key, the subjectPublicKey is:
++++++++ *
++++++++ * RSAPublicKey ::= SEQUENCE {
++++++++ * modulus INTEGER, -- n
++++++++ * publicExponent INTEGER -- e
++++++++ * }
++++++++ *
++++++++ * PrivateKeyInfo ::= SEQUENCE {
++++++++ * version Version,
++++++++ * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
++++++++ * privateKey PrivateKey,
++++++++ * attributes [0] IMPLICIT Attributes OPTIONAL
++++++++ * }
++++++++ *
++++++++ * Version ::= INTEGER
++++++++ * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ * PrivateKey ::= OCTET STRING
++++++++ * Attributes ::= SET OF Attribute
++++++++ *
++++++++ * An RSA private key as the following structure:
++++++++ *
++++++++ * RSAPrivateKey ::= SEQUENCE {
++++++++ * version Version,
++++++++ * modulus INTEGER, -- n
++++++++ * publicExponent INTEGER, -- e
++++++++ * privateExponent INTEGER, -- d
++++++++ * prime1 INTEGER, -- p
++++++++ * prime2 INTEGER, -- q
++++++++ * exponent1 INTEGER, -- d mod (p-1)
++++++++ * exponent2 INTEGER, -- d mod (q-1)
++++++++ * coefficient INTEGER -- (inverse of q) mod p
++++++++ * }
++++++++ *
++++++++ * Version ::= INTEGER
++++++++ *
++++++++ * The OID for the RSA key algorithm is: 1.2.840.113549.1.1.1
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++require('./jsbn');
++++++++require('./oids');
++++++++require('./pkcs1');
++++++++require('./prime');
++++++++require('./random');
++++++++require('./util');
++++++++
++++++++if(typeof BigInteger === 'undefined') {
++++++++ var BigInteger = forge.jsbn.BigInteger;
++++++++}
++++++++
++++++++var _crypto = forge.util.isNodejs ? require('crypto') : null;
++++++++
++++++++// shortcut for asn.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++// shortcut for util API
++++++++var util = forge.util;
++++++++
++++++++/*
++++++++ * RSA encryption and decryption, see RFC 2313.
++++++++ */
++++++++forge.pki = forge.pki || {};
++++++++module.exports = forge.pki.rsa = forge.rsa = forge.rsa || {};
++++++++var pki = forge.pki;
++++++++
++++++++// for finding primes, which are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
++++++++var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
++++++++
++++++++// validator for a PrivateKeyInfo structure
++++++++var privateKeyValidator = {
++++++++ // PrivateKeyInfo
++++++++ name: 'PrivateKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // Version (INTEGER)
++++++++ name: 'PrivateKeyInfo.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyVersion'
++++++++ }, {
++++++++ // privateKeyAlgorithm
++++++++ name: 'PrivateKeyInfo.privateKeyAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'privateKeyOid'
++++++++ }]
++++++++ }, {
++++++++ // PrivateKey
++++++++ name: 'PrivateKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'privateKey'
++++++++ }]
++++++++};
++++++++
++++++++// validator for an RSA private key
++++++++var rsaPrivateKeyValidator = {
++++++++ // RSAPrivateKey
++++++++ name: 'RSAPrivateKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // Version (INTEGER)
++++++++ name: 'RSAPrivateKey.version',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyVersion'
++++++++ }, {
++++++++ // modulus (n)
++++++++ name: 'RSAPrivateKey.modulus',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyModulus'
++++++++ }, {
++++++++ // publicExponent (e)
++++++++ name: 'RSAPrivateKey.publicExponent',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyPublicExponent'
++++++++ }, {
++++++++ // privateExponent (d)
++++++++ name: 'RSAPrivateKey.privateExponent',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyPrivateExponent'
++++++++ }, {
++++++++ // prime1 (p)
++++++++ name: 'RSAPrivateKey.prime1',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyPrime1'
++++++++ }, {
++++++++ // prime2 (q)
++++++++ name: 'RSAPrivateKey.prime2',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyPrime2'
++++++++ }, {
++++++++ // exponent1 (d mod (p-1))
++++++++ name: 'RSAPrivateKey.exponent1',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyExponent1'
++++++++ }, {
++++++++ // exponent2 (d mod (q-1))
++++++++ name: 'RSAPrivateKey.exponent2',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyExponent2'
++++++++ }, {
++++++++ // coefficient ((inverse of q) mod p)
++++++++ name: 'RSAPrivateKey.coefficient',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'privateKeyCoefficient'
++++++++ }]
++++++++};
++++++++
++++++++// validator for an RSA public key
++++++++var rsaPublicKeyValidator = {
++++++++ // RSAPublicKey
++++++++ name: 'RSAPublicKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // modulus (n)
++++++++ name: 'RSAPublicKey.modulus',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'publicKeyModulus'
++++++++ }, {
++++++++ // publicExponent (e)
++++++++ name: 'RSAPublicKey.exponent',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'publicKeyExponent'
++++++++ }]
++++++++};
++++++++
++++++++// validator for an SubjectPublicKeyInfo structure
++++++++// Note: Currently only works with an RSA public key
++++++++var publicKeyValidator = forge.pki.rsa.publicKeyValidator = {
++++++++ name: 'SubjectPublicKeyInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'subjectPublicKeyInfo',
++++++++ value: [{
++++++++ name: 'SubjectPublicKeyInfo.AlgorithmIdentifier',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'publicKeyOid'
++++++++ }]
++++++++ }, {
++++++++ // subjectPublicKey
++++++++ name: 'SubjectPublicKeyInfo.subjectPublicKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ value: [{
++++++++ // RSAPublicKey
++++++++ name: 'SubjectPublicKeyInfo.subjectPublicKey.RSAPublicKey',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ captureAsn1: 'rsaPublicKey'
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++// validator for a DigestInfo structure
++++++++var digestInfoValidator = {
++++++++ name: 'DigestInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'DigestInfo.DigestAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'DigestInfo.DigestAlgorithm.algorithmIdentifier',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'algorithmIdentifier'
++++++++ }, {
++++++++ // NULL paramters
++++++++ name: 'DigestInfo.DigestAlgorithm.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.NULL,
++++++++ // captured only to check existence for md2 and md5
++++++++ capture: 'parameters',
++++++++ optional: true,
++++++++ constructed: false
++++++++ }]
++++++++ }, {
++++++++ // digest
++++++++ name: 'DigestInfo.digest',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OCTETSTRING,
++++++++ constructed: false,
++++++++ capture: 'digest'
++++++++ }]
++++++++};
++++++++
++++++++/**
++++++++ * Wrap digest in DigestInfo object.
++++++++ *
++++++++ * This function implements EMSA-PKCS1-v1_5-ENCODE as per RFC 3447.
++++++++ *
++++++++ * DigestInfo ::= SEQUENCE {
++++++++ * digestAlgorithm DigestAlgorithmIdentifier,
++++++++ * digest Digest
++++++++ * }
++++++++ *
++++++++ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ * Digest ::= OCTET STRING
++++++++ *
++++++++ * @param md the message digest object with the hash to sign.
++++++++ *
++++++++ * @return the encoded message (ready for RSA encrytion)
++++++++ */
++++++++var emsaPkcs1v15encode = function(md) {
++++++++ // get the oid for the algorithm
++++++++ var oid;
++++++++ if(md.algorithm in pki.oids) {
++++++++ oid = pki.oids[md.algorithm];
++++++++ } else {
++++++++ var error = new Error('Unknown message digest algorithm.');
++++++++ error.algorithm = md.algorithm;
++++++++ throw error;
++++++++ }
++++++++ var oidBytes = asn1.oidToDer(oid).getBytes();
++++++++
++++++++ // create the digest info
++++++++ var digestInfo = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ var digestAlgorithm = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ digestAlgorithm.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OID, false, oidBytes));
++++++++ digestAlgorithm.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.NULL, false, ''));
++++++++ var digest = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING,
++++++++ false, md.digest().getBytes());
++++++++ digestInfo.value.push(digestAlgorithm);
++++++++ digestInfo.value.push(digest);
++++++++
++++++++ // encode digest info
++++++++ return asn1.toDer(digestInfo).getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * Performs x^c mod n (RSA encryption or decryption operation).
++++++++ *
++++++++ * @param x the number to raise and mod.
++++++++ * @param key the key to use.
++++++++ * @param pub true if the key is public, false if private.
++++++++ *
++++++++ * @return the result of x^c mod n.
++++++++ */
++++++++var _modPow = function(x, key, pub) {
++++++++ if(pub) {
++++++++ return x.modPow(key.e, key.n);
++++++++ }
++++++++
++++++++ if(!key.p || !key.q) {
++++++++ // allow calculation without CRT params (slow)
++++++++ return x.modPow(key.d, key.n);
++++++++ }
++++++++
++++++++ // pre-compute dP, dQ, and qInv if necessary
++++++++ if(!key.dP) {
++++++++ key.dP = key.d.mod(key.p.subtract(BigInteger.ONE));
++++++++ }
++++++++ if(!key.dQ) {
++++++++ key.dQ = key.d.mod(key.q.subtract(BigInteger.ONE));
++++++++ }
++++++++ if(!key.qInv) {
++++++++ key.qInv = key.q.modInverse(key.p);
++++++++ }
++++++++
++++++++ /* Chinese remainder theorem (CRT) states:
++++++++
++++++++ Suppose n1, n2, ..., nk are positive integers which are pairwise
++++++++ coprime (n1 and n2 have no common factors other than 1). For any
++++++++ integers x1, x2, ..., xk there exists an integer x solving the
++++++++ system of simultaneous congruences (where ~= means modularly
++++++++ congruent so a ~= b mod n means a mod n = b mod n):
++++++++
++++++++ x ~= x1 mod n1
++++++++ x ~= x2 mod n2
++++++++ ...
++++++++ x ~= xk mod nk
++++++++
++++++++ This system of congruences has a single simultaneous solution x
++++++++ between 0 and n - 1. Furthermore, each xk solution and x itself
++++++++ is congruent modulo the product n = n1*n2*...*nk.
++++++++ So x1 mod n = x2 mod n = xk mod n = x mod n.
++++++++
++++++++ The single simultaneous solution x can be solved with the following
++++++++ equation:
++++++++
++++++++ x = sum(xi*ri*si) mod n where ri = n/ni and si = ri^-1 mod ni.
++++++++
++++++++ Where x is less than n, xi = x mod ni.
++++++++
++++++++ For RSA we are only concerned with k = 2. The modulus n = pq, where
++++++++ p and q are coprime. The RSA decryption algorithm is:
++++++++
++++++++ y = x^d mod n
++++++++
++++++++ Given the above:
++++++++
++++++++ x1 = x^d mod p
++++++++ r1 = n/p = q
++++++++ s1 = q^-1 mod p
++++++++ x2 = x^d mod q
++++++++ r2 = n/q = p
++++++++ s2 = p^-1 mod q
++++++++
++++++++ So y = (x1r1s1 + x2r2s2) mod n
++++++++ = ((x^d mod p)q(q^-1 mod p) + (x^d mod q)p(p^-1 mod q)) mod n
++++++++
++++++++ According to Fermat's Little Theorem, if the modulus P is prime,
++++++++ for any integer A not evenly divisible by P, A^(P-1) ~= 1 mod P.
++++++++ Since A is not divisible by P it follows that if:
++++++++ N ~= M mod (P - 1), then A^N mod P = A^M mod P. Therefore:
++++++++
++++++++ A^N mod P = A^(M mod (P - 1)) mod P. (The latter takes less effort
++++++++ to calculate). In order to calculate x^d mod p more quickly the
++++++++ exponent d mod (p - 1) is stored in the RSA private key (the same
++++++++ is done for x^d mod q). These values are referred to as dP and dQ
++++++++ respectively. Therefore we now have:
++++++++
++++++++ y = ((x^dP mod p)q(q^-1 mod p) + (x^dQ mod q)p(p^-1 mod q)) mod n
++++++++
++++++++ Since we'll be reducing x^dP by modulo p (same for q) we can also
++++++++ reduce x by p (and q respectively) before hand. Therefore, let
++++++++
++++++++ xp = ((x mod p)^dP mod p), and
++++++++ xq = ((x mod q)^dQ mod q), yielding:
++++++++
++++++++ y = (xp*q*(q^-1 mod p) + xq*p*(p^-1 mod q)) mod n
++++++++
++++++++ This can be further reduced to a simple algorithm that only
++++++++ requires 1 inverse (the q inverse is used) to be used and stored.
++++++++ The algorithm is called Garner's algorithm. If qInv is the
++++++++ inverse of q, we simply calculate:
++++++++
++++++++ y = (qInv*(xp - xq) mod p) * q + xq
++++++++
++++++++ However, there are two further complications. First, we need to
++++++++ ensure that xp > xq to prevent signed BigIntegers from being used
++++++++ so we add p until this is true (since we will be mod'ing with
++++++++ p anyway). Then, there is a known timing attack on algorithms
++++++++ using the CRT. To mitigate this risk, "cryptographic blinding"
++++++++ should be used. This requires simply generating a random number r
++++++++ between 0 and n-1 and its inverse and multiplying x by r^e before
++++++++ calculating y and then multiplying y by r^-1 afterwards. Note that
++++++++ r must be coprime with n (gcd(r, n) === 1) in order to have an
++++++++ inverse.
++++++++ */
++++++++
++++++++ // cryptographic blinding
++++++++ var r;
++++++++ do {
++++++++ r = new BigInteger(
++++++++ forge.util.bytesToHex(forge.random.getBytes(key.n.bitLength() / 8)),
++++++++ 16);
++++++++ } while(r.compareTo(key.n) >= 0 || !r.gcd(key.n).equals(BigInteger.ONE));
++++++++ x = x.multiply(r.modPow(key.e, key.n)).mod(key.n);
++++++++
++++++++ // calculate xp and xq
++++++++ var xp = x.mod(key.p).modPow(key.dP, key.p);
++++++++ var xq = x.mod(key.q).modPow(key.dQ, key.q);
++++++++
++++++++ // xp must be larger than xq to avoid signed bit usage
++++++++ while(xp.compareTo(xq) < 0) {
++++++++ xp = xp.add(key.p);
++++++++ }
++++++++
++++++++ // do last step
++++++++ var y = xp.subtract(xq)
++++++++ .multiply(key.qInv).mod(key.p)
++++++++ .multiply(key.q).add(xq);
++++++++
++++++++ // remove effect of random for cryptographic blinding
++++++++ y = y.multiply(r.modInverse(key.n)).mod(key.n);
++++++++
++++++++ return y;
++++++++};
++++++++
++++++++/**
++++++++ * NOTE: THIS METHOD IS DEPRECATED, use 'sign' on a private key object or
++++++++ * 'encrypt' on a public key object instead.
++++++++ *
++++++++ * Performs RSA encryption.
++++++++ *
++++++++ * The parameter bt controls whether to put padding bytes before the
++++++++ * message passed in. Set bt to either true or false to disable padding
++++++++ * completely (in order to handle e.g. EMSA-PSS encoding seperately before),
++++++++ * signaling whether the encryption operation is a public key operation
++++++++ * (i.e. encrypting data) or not, i.e. private key operation (data signing).
++++++++ *
++++++++ * For PKCS#1 v1.5 padding pass in the block type to use, i.e. either 0x01
++++++++ * (for signing) or 0x02 (for encryption). The key operation mode (private
++++++++ * or public) is derived from this flag in that case).
++++++++ *
++++++++ * @param m the message to encrypt as a byte string.
++++++++ * @param key the RSA key to use.
++++++++ * @param bt for PKCS#1 v1.5 padding, the block type to use
++++++++ * (0x01 for private key, 0x02 for public),
++++++++ * to disable padding: true = public key, false = private key.
++++++++ *
++++++++ * @return the encrypted bytes as a string.
++++++++ */
++++++++pki.rsa.encrypt = function(m, key, bt) {
++++++++ var pub = bt;
++++++++ var eb;
++++++++
++++++++ // get the length of the modulus in bytes
++++++++ var k = Math.ceil(key.n.bitLength() / 8);
++++++++
++++++++ if(bt !== false && bt !== true) {
++++++++ // legacy, default to PKCS#1 v1.5 padding
++++++++ pub = (bt === 0x02);
++++++++ eb = _encodePkcs1_v1_5(m, key, bt);
++++++++ } else {
++++++++ eb = forge.util.createBuffer();
++++++++ eb.putBytes(m);
++++++++ }
++++++++
++++++++ // load encryption block as big integer 'x'
++++++++ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
++++++++ var x = new BigInteger(eb.toHex(), 16);
++++++++
++++++++ // do RSA encryption
++++++++ var y = _modPow(x, key, pub);
++++++++
++++++++ // convert y into the encrypted data byte string, if y is shorter in
++++++++ // bytes than k, then prepend zero bytes to fill up ed
++++++++ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
++++++++ var yhex = y.toString(16);
++++++++ var ed = forge.util.createBuffer();
++++++++ var zeros = k - Math.ceil(yhex.length / 2);
++++++++ while(zeros > 0) {
++++++++ ed.putByte(0x00);
++++++++ --zeros;
++++++++ }
++++++++ ed.putBytes(forge.util.hexToBytes(yhex));
++++++++ return ed.getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * NOTE: THIS METHOD IS DEPRECATED, use 'decrypt' on a private key object or
++++++++ * 'verify' on a public key object instead.
++++++++ *
++++++++ * Performs RSA decryption.
++++++++ *
++++++++ * The parameter ml controls whether to apply PKCS#1 v1.5 padding
++++++++ * or not. Set ml = false to disable padding removal completely
++++++++ * (in order to handle e.g. EMSA-PSS later on) and simply pass back
++++++++ * the RSA encryption block.
++++++++ *
++++++++ * @param ed the encrypted data to decrypt in as a byte string.
++++++++ * @param key the RSA key to use.
++++++++ * @param pub true for a public key operation, false for private.
++++++++ * @param ml the message length, if known, false to disable padding.
++++++++ *
++++++++ * @return the decrypted message as a byte string.
++++++++ */
++++++++pki.rsa.decrypt = function(ed, key, pub, ml) {
++++++++ // get the length of the modulus in bytes
++++++++ var k = Math.ceil(key.n.bitLength() / 8);
++++++++
++++++++ // error if the length of the encrypted data ED is not k
++++++++ if(ed.length !== k) {
++++++++ var error = new Error('Encrypted message length is invalid.');
++++++++ error.length = ed.length;
++++++++ error.expected = k;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // convert encrypted data into a big integer
++++++++ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
++++++++ var y = new BigInteger(forge.util.createBuffer(ed).toHex(), 16);
++++++++
++++++++ // y must be less than the modulus or it wasn't the result of
++++++++ // a previous mod operation (encryption) using that modulus
++++++++ if(y.compareTo(key.n) >= 0) {
++++++++ throw new Error('Encrypted message is invalid.');
++++++++ }
++++++++
++++++++ // do RSA decryption
++++++++ var x = _modPow(y, key, pub);
++++++++
++++++++ // create the encryption block, if x is shorter in bytes than k, then
++++++++ // prepend zero bytes to fill up eb
++++++++ // FIXME: hex conversion inefficient, get BigInteger w/byte strings
++++++++ var xhex = x.toString(16);
++++++++ var eb = forge.util.createBuffer();
++++++++ var zeros = k - Math.ceil(xhex.length / 2);
++++++++ while(zeros > 0) {
++++++++ eb.putByte(0x00);
++++++++ --zeros;
++++++++ }
++++++++ eb.putBytes(forge.util.hexToBytes(xhex));
++++++++
++++++++ if(ml !== false) {
++++++++ // legacy, default to PKCS#1 v1.5 padding
++++++++ return _decodePkcs1_v1_5(eb.getBytes(), key, pub);
++++++++ }
++++++++
++++++++ // return message
++++++++ return eb.getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * Creates an RSA key-pair generation state object. It is used to allow
++++++++ * key-generation to be performed in steps. It also allows for a UI to
++++++++ * display progress updates.
++++++++ *
++++++++ * @param bits the size for the private key in bits, defaults to 2048.
++++++++ * @param e the public exponent to use, defaults to 65537 (0x10001).
++++++++ * @param [options] the options to use.
++++++++ * prng a custom crypto-secure pseudo-random number generator to use,
++++++++ * that must define "getBytesSync".
++++++++ * algorithm the algorithm to use (default: 'PRIMEINC').
++++++++ *
++++++++ * @return the state object to use to generate the key-pair.
++++++++ */
++++++++pki.rsa.createKeyPairGenerationState = function(bits, e, options) {
++++++++ // TODO: migrate step-based prime generation code to forge.prime
++++++++
++++++++ // set default bits
++++++++ if(typeof(bits) === 'string') {
++++++++ bits = parseInt(bits, 10);
++++++++ }
++++++++ bits = bits || 2048;
++++++++
++++++++ // create prng with api that matches BigInteger secure random
++++++++ options = options || {};
++++++++ var prng = options.prng || forge.random;
++++++++ var rng = {
++++++++ // x is an array to fill with bytes
++++++++ nextBytes: function(x) {
++++++++ var b = prng.getBytesSync(x.length);
++++++++ for(var i = 0; i < x.length; ++i) {
++++++++ x[i] = b.charCodeAt(i);
++++++++ }
++++++++ }
++++++++ };
++++++++
++++++++ var algorithm = options.algorithm || 'PRIMEINC';
++++++++
++++++++ // create PRIMEINC algorithm state
++++++++ var rval;
++++++++ if(algorithm === 'PRIMEINC') {
++++++++ rval = {
++++++++ algorithm: algorithm,
++++++++ state: 0,
++++++++ bits: bits,
++++++++ rng: rng,
++++++++ eInt: e || 65537,
++++++++ e: new BigInteger(null),
++++++++ p: null,
++++++++ q: null,
++++++++ qBits: bits >> 1,
++++++++ pBits: bits - (bits >> 1),
++++++++ pqState: 0,
++++++++ num: null,
++++++++ keys: null
++++++++ };
++++++++ rval.e.fromInt(rval.eInt);
++++++++ } else {
++++++++ throw new Error('Invalid key generation algorithm: ' + algorithm);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Attempts to runs the key-generation algorithm for at most n seconds
++++++++ * (approximately) using the given state. When key-generation has completed,
++++++++ * the keys will be stored in state.keys.
++++++++ *
++++++++ * To use this function to update a UI while generating a key or to prevent
++++++++ * causing browser lockups/warnings, set "n" to a value other than 0. A
++++++++ * simple pattern for generating a key and showing a progress indicator is:
++++++++ *
++++++++ * var state = pki.rsa.createKeyPairGenerationState(2048);
++++++++ * var step = function() {
++++++++ * // step key-generation, run algorithm for 100 ms, repeat
++++++++ * if(!forge.pki.rsa.stepKeyPairGenerationState(state, 100)) {
++++++++ * setTimeout(step, 1);
++++++++ * } else {
++++++++ * // key-generation complete
++++++++ * // TODO: turn off progress indicator here
++++++++ * // TODO: use the generated key-pair in "state.keys"
++++++++ * }
++++++++ * };
++++++++ * // TODO: turn on progress indicator here
++++++++ * setTimeout(step, 0);
++++++++ *
++++++++ * @param state the state to use.
++++++++ * @param n the maximum number of milliseconds to run the algorithm for, 0
++++++++ * to run the algorithm to completion.
++++++++ *
++++++++ * @return true if the key-generation completed, false if not.
++++++++ */
++++++++pki.rsa.stepKeyPairGenerationState = function(state, n) {
++++++++ // set default algorithm if not set
++++++++ if(!('algorithm' in state)) {
++++++++ state.algorithm = 'PRIMEINC';
++++++++ }
++++++++
++++++++ // TODO: migrate step-based prime generation code to forge.prime
++++++++ // TODO: abstract as PRIMEINC algorithm
++++++++
++++++++ // do key generation (based on Tom Wu's rsa.js, see jsbn.js license)
++++++++ // with some minor optimizations and designed to run in steps
++++++++
++++++++ // local state vars
++++++++ var THIRTY = new BigInteger(null);
++++++++ THIRTY.fromInt(30);
++++++++ var deltaIdx = 0;
++++++++ var op_or = function(x, y) {return x | y;};
++++++++
++++++++ // keep stepping until time limit is reached or done
++++++++ var t1 = +new Date();
++++++++ var t2;
++++++++ var total = 0;
++++++++ while(state.keys === null && (n <= 0 || total < n)) {
++++++++ // generate p or q
++++++++ if(state.state === 0) {
++++++++ /* Note: All primes are of the form:
++++++++
++++++++ 30k+i, for i < 30 and gcd(30, i)=1, where there are 8 values for i
++++++++
++++++++ When we generate a random number, we always align it at 30k + 1. Each
++++++++ time the number is determined not to be prime we add to get to the
++++++++ next 'i', eg: if the number was at 30k + 1 we add 6. */
++++++++ var bits = (state.p === null) ? state.pBits : state.qBits;
++++++++ var bits1 = bits - 1;
++++++++
++++++++ // get a random number
++++++++ if(state.pqState === 0) {
++++++++ state.num = new BigInteger(bits, state.rng);
++++++++ // force MSB set
++++++++ if(!state.num.testBit(bits1)) {
++++++++ state.num.bitwiseTo(
++++++++ BigInteger.ONE.shiftLeft(bits1), op_or, state.num);
++++++++ }
++++++++ // align number on 30k+1 boundary
++++++++ state.num.dAddOffset(31 - state.num.mod(THIRTY).byteValue(), 0);
++++++++ deltaIdx = 0;
++++++++
++++++++ ++state.pqState;
++++++++ } else if(state.pqState === 1) {
++++++++ // try to make the number a prime
++++++++ if(state.num.bitLength() > bits) {
++++++++ // overflow, try again
++++++++ state.pqState = 0;
++++++++ // do primality test
++++++++ } else if(state.num.isProbablePrime(
++++++++ _getMillerRabinTests(state.num.bitLength()))) {
++++++++ ++state.pqState;
++++++++ } else {
++++++++ // get next potential prime
++++++++ state.num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
++++++++ }
++++++++ } else if(state.pqState === 2) {
++++++++ // ensure number is coprime with e
++++++++ state.pqState =
++++++++ (state.num.subtract(BigInteger.ONE).gcd(state.e)
++++++++ .compareTo(BigInteger.ONE) === 0) ? 3 : 0;
++++++++ } else if(state.pqState === 3) {
++++++++ // store p or q
++++++++ state.pqState = 0;
++++++++ if(state.p === null) {
++++++++ state.p = state.num;
++++++++ } else {
++++++++ state.q = state.num;
++++++++ }
++++++++
++++++++ // advance state if both p and q are ready
++++++++ if(state.p !== null && state.q !== null) {
++++++++ ++state.state;
++++++++ }
++++++++ state.num = null;
++++++++ }
++++++++ } else if(state.state === 1) {
++++++++ // ensure p is larger than q (swap them if not)
++++++++ if(state.p.compareTo(state.q) < 0) {
++++++++ state.num = state.p;
++++++++ state.p = state.q;
++++++++ state.q = state.num;
++++++++ }
++++++++ ++state.state;
++++++++ } else if(state.state === 2) {
++++++++ // compute phi: (p - 1)(q - 1) (Euler's totient function)
++++++++ state.p1 = state.p.subtract(BigInteger.ONE);
++++++++ state.q1 = state.q.subtract(BigInteger.ONE);
++++++++ state.phi = state.p1.multiply(state.q1);
++++++++ ++state.state;
++++++++ } else if(state.state === 3) {
++++++++ // ensure e and phi are coprime
++++++++ if(state.phi.gcd(state.e).compareTo(BigInteger.ONE) === 0) {
++++++++ // phi and e are coprime, advance
++++++++ ++state.state;
++++++++ } else {
++++++++ // phi and e aren't coprime, so generate a new p and q
++++++++ state.p = null;
++++++++ state.q = null;
++++++++ state.state = 0;
++++++++ }
++++++++ } else if(state.state === 4) {
++++++++ // create n, ensure n is has the right number of bits
++++++++ state.n = state.p.multiply(state.q);
++++++++
++++++++ // ensure n is right number of bits
++++++++ if(state.n.bitLength() === state.bits) {
++++++++ // success, advance
++++++++ ++state.state;
++++++++ } else {
++++++++ // failed, get new q
++++++++ state.q = null;
++++++++ state.state = 0;
++++++++ }
++++++++ } else if(state.state === 5) {
++++++++ // set keys
++++++++ var d = state.e.modInverse(state.phi);
++++++++ state.keys = {
++++++++ privateKey: pki.rsa.setPrivateKey(
++++++++ state.n, state.e, d, state.p, state.q,
++++++++ d.mod(state.p1), d.mod(state.q1),
++++++++ state.q.modInverse(state.p)),
++++++++ publicKey: pki.rsa.setPublicKey(state.n, state.e)
++++++++ };
++++++++ }
++++++++
++++++++ // update timing
++++++++ t2 = +new Date();
++++++++ total += t2 - t1;
++++++++ t1 = t2;
++++++++ }
++++++++
++++++++ return state.keys !== null;
++++++++};
++++++++
++++++++/**
++++++++ * Generates an RSA public-private key pair in a single call.
++++++++ *
++++++++ * To generate a key-pair in steps (to allow for progress updates and to
++++++++ * prevent blocking or warnings in slow browsers) then use the key-pair
++++++++ * generation state functions.
++++++++ *
++++++++ * To generate a key-pair asynchronously (either through web-workers, if
++++++++ * available, or by breaking up the work on the main thread), pass a
++++++++ * callback function.
++++++++ *
++++++++ * @param [bits] the size for the private key in bits, defaults to 2048.
++++++++ * @param [e] the public exponent to use, defaults to 65537.
++++++++ * @param [options] options for key-pair generation, if given then 'bits'
++++++++ * and 'e' must *not* be given:
++++++++ * bits the size for the private key in bits, (default: 2048).
++++++++ * e the public exponent to use, (default: 65537 (0x10001)).
++++++++ * workerScript the worker script URL.
++++++++ * workers the number of web workers (if supported) to use,
++++++++ * (default: 2).
++++++++ * workLoad the size of the work load, ie: number of possible prime
++++++++ * numbers for each web worker to check per work assignment,
++++++++ * (default: 100).
++++++++ * prng a custom crypto-secure pseudo-random number generator to use,
++++++++ * that must define "getBytesSync". Disables use of native APIs.
++++++++ * algorithm the algorithm to use (default: 'PRIMEINC').
++++++++ * @param [callback(err, keypair)] called once the operation completes.
++++++++ *
++++++++ * @return an object with privateKey and publicKey properties.
++++++++ */
++++++++pki.rsa.generateKeyPair = function(bits, e, options, callback) {
++++++++ // (bits), (options), (callback)
++++++++ if(arguments.length === 1) {
++++++++ if(typeof bits === 'object') {
++++++++ options = bits;
++++++++ bits = undefined;
++++++++ } else if(typeof bits === 'function') {
++++++++ callback = bits;
++++++++ bits = undefined;
++++++++ }
++++++++ } else if(arguments.length === 2) {
++++++++ // (bits, e), (bits, options), (bits, callback), (options, callback)
++++++++ if(typeof bits === 'number') {
++++++++ if(typeof e === 'function') {
++++++++ callback = e;
++++++++ e = undefined;
++++++++ } else if(typeof e !== 'number') {
++++++++ options = e;
++++++++ e = undefined;
++++++++ }
++++++++ } else {
++++++++ options = bits;
++++++++ callback = e;
++++++++ bits = undefined;
++++++++ e = undefined;
++++++++ }
++++++++ } else if(arguments.length === 3) {
++++++++ // (bits, e, options), (bits, e, callback), (bits, options, callback)
++++++++ if(typeof e === 'number') {
++++++++ if(typeof options === 'function') {
++++++++ callback = options;
++++++++ options = undefined;
++++++++ }
++++++++ } else {
++++++++ callback = options;
++++++++ options = e;
++++++++ e = undefined;
++++++++ }
++++++++ }
++++++++ options = options || {};
++++++++ if(bits === undefined) {
++++++++ bits = options.bits || 2048;
++++++++ }
++++++++ if(e === undefined) {
++++++++ e = options.e || 0x10001;
++++++++ }
++++++++
++++++++ // use native code if permitted, available, and parameters are acceptable
++++++++ if(!forge.options.usePureJavaScript && !options.prng &&
++++++++ bits >= 256 && bits <= 16384 && (e === 0x10001 || e === 3)) {
++++++++ if(callback) {
++++++++ // try native async
++++++++ if(_detectNodeCrypto('generateKeyPair')) {
++++++++ return _crypto.generateKeyPair('rsa', {
++++++++ modulusLength: bits,
++++++++ publicExponent: e,
++++++++ publicKeyEncoding: {
++++++++ type: 'spki',
++++++++ format: 'pem'
++++++++ },
++++++++ privateKeyEncoding: {
++++++++ type: 'pkcs8',
++++++++ format: 'pem'
++++++++ }
++++++++ }, function(err, pub, priv) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ callback(null, {
++++++++ privateKey: pki.privateKeyFromPem(priv),
++++++++ publicKey: pki.publicKeyFromPem(pub)
++++++++ });
++++++++ });
++++++++ }
++++++++ if(_detectSubtleCrypto('generateKey') &&
++++++++ _detectSubtleCrypto('exportKey')) {
++++++++ // use standard native generateKey
++++++++ return util.globalScope.crypto.subtle.generateKey({
++++++++ name: 'RSASSA-PKCS1-v1_5',
++++++++ modulusLength: bits,
++++++++ publicExponent: _intToUint8Array(e),
++++++++ hash: {name: 'SHA-256'}
++++++++ }, true /* key can be exported*/, ['sign', 'verify'])
++++++++ .then(function(pair) {
++++++++ return util.globalScope.crypto.subtle.exportKey(
++++++++ 'pkcs8', pair.privateKey);
++++++++ // avoiding catch(function(err) {...}) to support IE <= 8
++++++++ }).then(undefined, function(err) {
++++++++ callback(err);
++++++++ }).then(function(pkcs8) {
++++++++ if(pkcs8) {
++++++++ var privateKey = pki.privateKeyFromAsn1(
++++++++ asn1.fromDer(forge.util.createBuffer(pkcs8)));
++++++++ callback(null, {
++++++++ privateKey: privateKey,
++++++++ publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e)
++++++++ });
++++++++ }
++++++++ });
++++++++ }
++++++++ if(_detectSubtleMsCrypto('generateKey') &&
++++++++ _detectSubtleMsCrypto('exportKey')) {
++++++++ var genOp = util.globalScope.msCrypto.subtle.generateKey({
++++++++ name: 'RSASSA-PKCS1-v1_5',
++++++++ modulusLength: bits,
++++++++ publicExponent: _intToUint8Array(e),
++++++++ hash: {name: 'SHA-256'}
++++++++ }, true /* key can be exported*/, ['sign', 'verify']);
++++++++ genOp.oncomplete = function(e) {
++++++++ var pair = e.target.result;
++++++++ var exportOp = util.globalScope.msCrypto.subtle.exportKey(
++++++++ 'pkcs8', pair.privateKey);
++++++++ exportOp.oncomplete = function(e) {
++++++++ var pkcs8 = e.target.result;
++++++++ var privateKey = pki.privateKeyFromAsn1(
++++++++ asn1.fromDer(forge.util.createBuffer(pkcs8)));
++++++++ callback(null, {
++++++++ privateKey: privateKey,
++++++++ publicKey: pki.setRsaPublicKey(privateKey.n, privateKey.e)
++++++++ });
++++++++ };
++++++++ exportOp.onerror = function(err) {
++++++++ callback(err);
++++++++ };
++++++++ };
++++++++ genOp.onerror = function(err) {
++++++++ callback(err);
++++++++ };
++++++++ return;
++++++++ }
++++++++ } else {
++++++++ // try native sync
++++++++ if(_detectNodeCrypto('generateKeyPairSync')) {
++++++++ var keypair = _crypto.generateKeyPairSync('rsa', {
++++++++ modulusLength: bits,
++++++++ publicExponent: e,
++++++++ publicKeyEncoding: {
++++++++ type: 'spki',
++++++++ format: 'pem'
++++++++ },
++++++++ privateKeyEncoding: {
++++++++ type: 'pkcs8',
++++++++ format: 'pem'
++++++++ }
++++++++ });
++++++++ return {
++++++++ privateKey: pki.privateKeyFromPem(keypair.privateKey),
++++++++ publicKey: pki.publicKeyFromPem(keypair.publicKey)
++++++++ };
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // use JavaScript implementation
++++++++ var state = pki.rsa.createKeyPairGenerationState(bits, e, options);
++++++++ if(!callback) {
++++++++ pki.rsa.stepKeyPairGenerationState(state, 0);
++++++++ return state.keys;
++++++++ }
++++++++ _generateKeyPair(state, options, callback);
++++++++};
++++++++
++++++++/**
++++++++ * Sets an RSA public key from BigIntegers modulus and exponent.
++++++++ *
++++++++ * @param n the modulus.
++++++++ * @param e the exponent.
++++++++ *
++++++++ * @return the public key.
++++++++ */
++++++++pki.setRsaPublicKey = pki.rsa.setPublicKey = function(n, e) {
++++++++ var key = {
++++++++ n: n,
++++++++ e: e
++++++++ };
++++++++
++++++++ /**
++++++++ * Encrypts the given data with this public key. Newer applications
++++++++ * should use the 'RSA-OAEP' decryption scheme, 'RSAES-PKCS1-V1_5' is for
++++++++ * legacy applications.
++++++++ *
++++++++ * @param data the byte string to encrypt.
++++++++ * @param scheme the encryption scheme to use:
++++++++ * 'RSAES-PKCS1-V1_5' (default),
++++++++ * 'RSA-OAEP',
++++++++ * 'RAW', 'NONE', or null to perform raw RSA encryption,
++++++++ * an object with an 'encode' property set to a function
++++++++ * with the signature 'function(data, key)' that returns
++++++++ * a binary-encoded string representing the encoded data.
++++++++ * @param schemeOptions any scheme-specific options.
++++++++ *
++++++++ * @return the encrypted byte string.
++++++++ */
++++++++ key.encrypt = function(data, scheme, schemeOptions) {
++++++++ if(typeof scheme === 'string') {
++++++++ scheme = scheme.toUpperCase();
++++++++ } else if(scheme === undefined) {
++++++++ scheme = 'RSAES-PKCS1-V1_5';
++++++++ }
++++++++
++++++++ if(scheme === 'RSAES-PKCS1-V1_5') {
++++++++ scheme = {
++++++++ encode: function(m, key, pub) {
++++++++ return _encodePkcs1_v1_5(m, key, 0x02).getBytes();
++++++++ }
++++++++ };
++++++++ } else if(scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') {
++++++++ scheme = {
++++++++ encode: function(m, key) {
++++++++ return forge.pkcs1.encode_rsa_oaep(key, m, schemeOptions);
++++++++ }
++++++++ };
++++++++ } else if(['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) {
++++++++ scheme = {encode: function(e) {return e;}};
++++++++ } else if(typeof scheme === 'string') {
++++++++ throw new Error('Unsupported encryption scheme: "' + scheme + '".');
++++++++ }
++++++++
++++++++ // do scheme-based encoding then rsa encryption
++++++++ var e = scheme.encode(data, key, true);
++++++++ return pki.rsa.encrypt(e, key, true);
++++++++ };
++++++++
++++++++ /**
++++++++ * Verifies the given signature against the given digest.
++++++++ *
++++++++ * PKCS#1 supports multiple (currently two) signature schemes:
++++++++ * RSASSA-PKCS1-V1_5 and RSASSA-PSS.
++++++++ *
++++++++ * By default this implementation uses the "old scheme", i.e.
++++++++ * RSASSA-PKCS1-V1_5, in which case once RSA-decrypted, the
++++++++ * signature is an OCTET STRING that holds a DigestInfo.
++++++++ *
++++++++ * DigestInfo ::= SEQUENCE {
++++++++ * digestAlgorithm DigestAlgorithmIdentifier,
++++++++ * digest Digest
++++++++ * }
++++++++ * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
++++++++ * Digest ::= OCTET STRING
++++++++ *
++++++++ * To perform PSS signature verification, provide an instance
++++++++ * of Forge PSS object as the scheme parameter.
++++++++ *
++++++++ * @param digest the message digest hash to compare against the signature,
++++++++ * as a binary-encoded string.
++++++++ * @param signature the signature to verify, as a binary-encoded string.
++++++++ * @param scheme signature verification scheme to use:
++++++++ * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5,
++++++++ * a Forge PSS object for RSASSA-PSS,
++++++++ * 'NONE' or null for none, DigestInfo will not be expected, but
++++++++ * PKCS#1 v1.5 padding will still be used.
++++++++ * @param options optional verify options
++++++++ * _parseAllDigestBytes testing flag to control parsing of all
++++++++ * digest bytes. Unsupported and not for general usage.
++++++++ * (default: true)
++++++++ *
++++++++ * @return true if the signature was verified, false if not.
++++++++ */
++++++++ key.verify = function(digest, signature, scheme, options) {
++++++++ if(typeof scheme === 'string') {
++++++++ scheme = scheme.toUpperCase();
++++++++ } else if(scheme === undefined) {
++++++++ scheme = 'RSASSA-PKCS1-V1_5';
++++++++ }
++++++++ if(options === undefined) {
++++++++ options = {
++++++++ _parseAllDigestBytes: true
++++++++ };
++++++++ }
++++++++ if(!('_parseAllDigestBytes' in options)) {
++++++++ options._parseAllDigestBytes = true;
++++++++ }
++++++++
++++++++ if(scheme === 'RSASSA-PKCS1-V1_5') {
++++++++ scheme = {
++++++++ verify: function(digest, d) {
++++++++ // remove padding
++++++++ d = _decodePkcs1_v1_5(d, key, true);
++++++++ // d is ASN.1 BER-encoded DigestInfo
++++++++ var obj = asn1.fromDer(d, {
++++++++ parseAllBytes: options._parseAllDigestBytes
++++++++ });
++++++++
++++++++ // validate DigestInfo
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, digestInfoValidator, capture, errors)) {
++++++++ var error = new Error(
++++++++ 'ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 ' +
++++++++ 'DigestInfo value.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++ // check hash algorithm identifier
++++++++ // see PKCS1-v1-5DigestAlgorithms in RFC 8017
++++++++ // FIXME: add support to vaidator for strict value choices
++++++++ var oid = asn1.derToOid(capture.algorithmIdentifier);
++++++++ if(!(oid === forge.oids.md2 ||
++++++++ oid === forge.oids.md5 ||
++++++++ oid === forge.oids.sha1 ||
++++++++ oid === forge.oids.sha224 ||
++++++++ oid === forge.oids.sha256 ||
++++++++ oid === forge.oids.sha384 ||
++++++++ oid === forge.oids.sha512 ||
++++++++ oid === forge.oids['sha512-224'] ||
++++++++ oid === forge.oids['sha512-256'])) {
++++++++ var error = new Error(
++++++++ 'Unknown RSASSA-PKCS1-v1_5 DigestAlgorithm identifier.');
++++++++ error.oid = oid;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // special check for md2 and md5 that NULL parameters exist
++++++++ if(oid === forge.oids.md2 || oid === forge.oids.md5) {
++++++++ if(!('parameters' in capture)) {
++++++++ throw new Error(
++++++++ 'ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 ' +
++++++++ 'DigestInfo value. ' +
++++++++ 'Missing algorithm identifer NULL parameters.');
++++++++ }
++++++++ }
++++++++
++++++++ // compare the given digest to the decrypted one
++++++++ return digest === capture.digest;
++++++++ }
++++++++ };
++++++++ } else if(scheme === 'NONE' || scheme === 'NULL' || scheme === null) {
++++++++ scheme = {
++++++++ verify: function(digest, d) {
++++++++ // remove padding
++++++++ d = _decodePkcs1_v1_5(d, key, true);
++++++++ return digest === d;
++++++++ }
++++++++ };
++++++++ }
++++++++
++++++++ // do rsa decryption w/o any decoding, then verify -- which does decoding
++++++++ var d = pki.rsa.decrypt(signature, key, true, false);
++++++++ return scheme.verify(digest, d, key.n.bitLength());
++++++++ };
++++++++
++++++++ return key;
++++++++};
++++++++
++++++++/**
++++++++ * Sets an RSA private key from BigIntegers modulus, exponent, primes,
++++++++ * prime exponents, and modular multiplicative inverse.
++++++++ *
++++++++ * @param n the modulus.
++++++++ * @param e the public exponent.
++++++++ * @param d the private exponent ((inverse of e) mod n).
++++++++ * @param p the first prime.
++++++++ * @param q the second prime.
++++++++ * @param dP exponent1 (d mod (p-1)).
++++++++ * @param dQ exponent2 (d mod (q-1)).
++++++++ * @param qInv ((inverse of q) mod p)
++++++++ *
++++++++ * @return the private key.
++++++++ */
++++++++pki.setRsaPrivateKey = pki.rsa.setPrivateKey = function(
++++++++ n, e, d, p, q, dP, dQ, qInv) {
++++++++ var key = {
++++++++ n: n,
++++++++ e: e,
++++++++ d: d,
++++++++ p: p,
++++++++ q: q,
++++++++ dP: dP,
++++++++ dQ: dQ,
++++++++ qInv: qInv
++++++++ };
++++++++
++++++++ /**
++++++++ * Decrypts the given data with this private key. The decryption scheme
++++++++ * must match the one used to encrypt the data.
++++++++ *
++++++++ * @param data the byte string to decrypt.
++++++++ * @param scheme the decryption scheme to use:
++++++++ * 'RSAES-PKCS1-V1_5' (default),
++++++++ * 'RSA-OAEP',
++++++++ * 'RAW', 'NONE', or null to perform raw RSA decryption.
++++++++ * @param schemeOptions any scheme-specific options.
++++++++ *
++++++++ * @return the decrypted byte string.
++++++++ */
++++++++ key.decrypt = function(data, scheme, schemeOptions) {
++++++++ if(typeof scheme === 'string') {
++++++++ scheme = scheme.toUpperCase();
++++++++ } else if(scheme === undefined) {
++++++++ scheme = 'RSAES-PKCS1-V1_5';
++++++++ }
++++++++
++++++++ // do rsa decryption w/o any decoding
++++++++ var d = pki.rsa.decrypt(data, key, false, false);
++++++++
++++++++ if(scheme === 'RSAES-PKCS1-V1_5') {
++++++++ scheme = {decode: _decodePkcs1_v1_5};
++++++++ } else if(scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') {
++++++++ scheme = {
++++++++ decode: function(d, key) {
++++++++ return forge.pkcs1.decode_rsa_oaep(key, d, schemeOptions);
++++++++ }
++++++++ };
++++++++ } else if(['RAW', 'NONE', 'NULL', null].indexOf(scheme) !== -1) {
++++++++ scheme = {decode: function(d) {return d;}};
++++++++ } else {
++++++++ throw new Error('Unsupported encryption scheme: "' + scheme + '".');
++++++++ }
++++++++
++++++++ // decode according to scheme
++++++++ return scheme.decode(d, key, false);
++++++++ };
++++++++
++++++++ /**
++++++++ * Signs the given digest, producing a signature.
++++++++ *
++++++++ * PKCS#1 supports multiple (currently two) signature schemes:
++++++++ * RSASSA-PKCS1-V1_5 and RSASSA-PSS.
++++++++ *
++++++++ * By default this implementation uses the "old scheme", i.e.
++++++++ * RSASSA-PKCS1-V1_5. In order to generate a PSS signature, provide
++++++++ * an instance of Forge PSS object as the scheme parameter.
++++++++ *
++++++++ * @param md the message digest object with the hash to sign.
++++++++ * @param scheme the signature scheme to use:
++++++++ * 'RSASSA-PKCS1-V1_5' or undefined for RSASSA PKCS#1 v1.5,
++++++++ * a Forge PSS object for RSASSA-PSS,
++++++++ * 'NONE' or null for none, DigestInfo will not be used but
++++++++ * PKCS#1 v1.5 padding will still be used.
++++++++ *
++++++++ * @return the signature as a byte string.
++++++++ */
++++++++ key.sign = function(md, scheme) {
++++++++ /* Note: The internal implementation of RSA operations is being
++++++++ transitioned away from a PKCS#1 v1.5 hard-coded scheme. Some legacy
++++++++ code like the use of an encoding block identifier 'bt' will eventually
++++++++ be removed. */
++++++++
++++++++ // private key operation
++++++++ var bt = false;
++++++++
++++++++ if(typeof scheme === 'string') {
++++++++ scheme = scheme.toUpperCase();
++++++++ }
++++++++
++++++++ if(scheme === undefined || scheme === 'RSASSA-PKCS1-V1_5') {
++++++++ scheme = {encode: emsaPkcs1v15encode};
++++++++ bt = 0x01;
++++++++ } else if(scheme === 'NONE' || scheme === 'NULL' || scheme === null) {
++++++++ scheme = {encode: function() {return md;}};
++++++++ bt = 0x01;
++++++++ }
++++++++
++++++++ // encode and then encrypt
++++++++ var d = scheme.encode(md, key.n.bitLength());
++++++++ return pki.rsa.encrypt(d, key, bt);
++++++++ };
++++++++
++++++++ return key;
++++++++};
++++++++
++++++++/**
++++++++ * Wraps an RSAPrivateKey ASN.1 object in an ASN.1 PrivateKeyInfo object.
++++++++ *
++++++++ * @param rsaKey the ASN.1 RSAPrivateKey.
++++++++ *
++++++++ * @return the ASN.1 PrivateKeyInfo.
++++++++ */
++++++++pki.wrapRsaPrivateKey = function(rsaKey) {
++++++++ // PrivateKeyInfo
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version (0)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(0).getBytes()),
++++++++ // privateKeyAlgorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.rsaEncryption).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]),
++++++++ // PrivateKey
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false,
++++++++ asn1.toDer(rsaKey).getBytes())
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Converts a private key from an ASN.1 object.
++++++++ *
++++++++ * @param obj the ASN.1 representation of a PrivateKeyInfo containing an
++++++++ * RSAPrivateKey or an RSAPrivateKey.
++++++++ *
++++++++ * @return the private key.
++++++++ */
++++++++pki.privateKeyFromAsn1 = function(obj) {
++++++++ // get PrivateKeyInfo
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(asn1.validate(obj, privateKeyValidator, capture, errors)) {
++++++++ obj = asn1.fromDer(forge.util.createBuffer(capture.privateKey));
++++++++ }
++++++++
++++++++ // get RSAPrivateKey
++++++++ capture = {};
++++++++ errors = [];
++++++++ if(!asn1.validate(obj, rsaPrivateKeyValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read private key. ' +
++++++++ 'ASN.1 object does not contain an RSAPrivateKey.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // Note: Version is currently ignored.
++++++++ // capture.privateKeyVersion
++++++++ // FIXME: inefficient, get a BigInteger that uses byte strings
++++++++ var n, e, d, p, q, dP, dQ, qInv;
++++++++ n = forge.util.createBuffer(capture.privateKeyModulus).toHex();
++++++++ e = forge.util.createBuffer(capture.privateKeyPublicExponent).toHex();
++++++++ d = forge.util.createBuffer(capture.privateKeyPrivateExponent).toHex();
++++++++ p = forge.util.createBuffer(capture.privateKeyPrime1).toHex();
++++++++ q = forge.util.createBuffer(capture.privateKeyPrime2).toHex();
++++++++ dP = forge.util.createBuffer(capture.privateKeyExponent1).toHex();
++++++++ dQ = forge.util.createBuffer(capture.privateKeyExponent2).toHex();
++++++++ qInv = forge.util.createBuffer(capture.privateKeyCoefficient).toHex();
++++++++
++++++++ // set private key
++++++++ return pki.setRsaPrivateKey(
++++++++ new BigInteger(n, 16),
++++++++ new BigInteger(e, 16),
++++++++ new BigInteger(d, 16),
++++++++ new BigInteger(p, 16),
++++++++ new BigInteger(q, 16),
++++++++ new BigInteger(dP, 16),
++++++++ new BigInteger(dQ, 16),
++++++++ new BigInteger(qInv, 16));
++++++++};
++++++++
++++++++/**
++++++++ * Converts a private key to an ASN.1 RSAPrivateKey.
++++++++ *
++++++++ * @param key the private key.
++++++++ *
++++++++ * @return the ASN.1 representation of an RSAPrivateKey.
++++++++ */
++++++++pki.privateKeyToAsn1 = pki.privateKeyToRSAPrivateKey = function(key) {
++++++++ // RSAPrivateKey
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version (0 = only 2 primes, 1 multiple primes)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(0).getBytes()),
++++++++ // modulus (n)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.n)),
++++++++ // publicExponent (e)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.e)),
++++++++ // privateExponent (d)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.d)),
++++++++ // privateKeyPrime1 (p)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.p)),
++++++++ // privateKeyPrime2 (q)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.q)),
++++++++ // privateKeyExponent1 (dP)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.dP)),
++++++++ // privateKeyExponent2 (dQ)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.dQ)),
++++++++ // coefficient (qInv)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.qInv))
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Converts a public key from an ASN.1 SubjectPublicKeyInfo or RSAPublicKey.
++++++++ *
++++++++ * @param obj the asn1 representation of a SubjectPublicKeyInfo or RSAPublicKey.
++++++++ *
++++++++ * @return the public key.
++++++++ */
++++++++pki.publicKeyFromAsn1 = function(obj) {
++++++++ // get SubjectPublicKeyInfo
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(asn1.validate(obj, publicKeyValidator, capture, errors)) {
++++++++ // get oid
++++++++ var oid = asn1.derToOid(capture.publicKeyOid);
++++++++ if(oid !== pki.oids.rsaEncryption) {
++++++++ var error = new Error('Cannot read public key. Unknown OID.');
++++++++ error.oid = oid;
++++++++ throw error;
++++++++ }
++++++++ obj = capture.rsaPublicKey;
++++++++ }
++++++++
++++++++ // get RSA params
++++++++ errors = [];
++++++++ if(!asn1.validate(obj, rsaPublicKeyValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read public key. ' +
++++++++ 'ASN.1 object does not contain an RSAPublicKey.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // FIXME: inefficient, get a BigInteger that uses byte strings
++++++++ var n = forge.util.createBuffer(capture.publicKeyModulus).toHex();
++++++++ var e = forge.util.createBuffer(capture.publicKeyExponent).toHex();
++++++++
++++++++ // set public key
++++++++ return pki.setRsaPublicKey(
++++++++ new BigInteger(n, 16),
++++++++ new BigInteger(e, 16));
++++++++};
++++++++
++++++++/**
++++++++ * Converts a public key to an ASN.1 SubjectPublicKeyInfo.
++++++++ *
++++++++ * @param key the public key.
++++++++ *
++++++++ * @return the asn1 representation of a SubjectPublicKeyInfo.
++++++++ */
++++++++pki.publicKeyToAsn1 = pki.publicKeyToSubjectPublicKeyInfo = function(key) {
++++++++ // SubjectPublicKeyInfo
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // AlgorithmIdentifier
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(pki.oids.rsaEncryption).getBytes()),
++++++++ // parameters (null)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ]),
++++++++ // subjectPublicKey
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, [
++++++++ pki.publicKeyToRSAPublicKey(key)
++++++++ ])
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Converts a public key to an ASN.1 RSAPublicKey.
++++++++ *
++++++++ * @param key the public key.
++++++++ *
++++++++ * @return the asn1 representation of a RSAPublicKey.
++++++++ */
++++++++pki.publicKeyToRSAPublicKey = function(key) {
++++++++ // RSAPublicKey
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // modulus (n)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.n)),
++++++++ // publicExponent (e)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ _bnToBytes(key.e))
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Encodes a message using PKCS#1 v1.5 padding.
++++++++ *
++++++++ * @param m the message to encode.
++++++++ * @param key the RSA key to use.
++++++++ * @param bt the block type to use, i.e. either 0x01 (for signing) or 0x02
++++++++ * (for encryption).
++++++++ *
++++++++ * @return the padded byte buffer.
++++++++ */
++++++++function _encodePkcs1_v1_5(m, key, bt) {
++++++++ var eb = forge.util.createBuffer();
++++++++
++++++++ // get the length of the modulus in bytes
++++++++ var k = Math.ceil(key.n.bitLength() / 8);
++++++++
++++++++ /* use PKCS#1 v1.5 padding */
++++++++ if(m.length > (k - 11)) {
++++++++ var error = new Error('Message is too long for PKCS#1 v1.5 padding.');
++++++++ error.length = m.length;
++++++++ error.max = k - 11;
++++++++ throw error;
++++++++ }
++++++++
++++++++ /* A block type BT, a padding string PS, and the data D shall be
++++++++ formatted into an octet string EB, the encryption block:
++++++++
++++++++ EB = 00 || BT || PS || 00 || D
++++++++
++++++++ The block type BT shall be a single octet indicating the structure of
++++++++ the encryption block. For this version of the document it shall have
++++++++ value 00, 01, or 02. For a private-key operation, the block type
++++++++ shall be 00 or 01. For a public-key operation, it shall be 02.
++++++++
++++++++ The padding string PS shall consist of k-3-||D|| octets. For block
++++++++ type 00, the octets shall have value 00; for block type 01, they
++++++++ shall have value FF; and for block type 02, they shall be
++++++++ pseudorandomly generated and nonzero. This makes the length of the
++++++++ encryption block EB equal to k. */
++++++++
++++++++ // build the encryption block
++++++++ eb.putByte(0x00);
++++++++ eb.putByte(bt);
++++++++
++++++++ // create the padding
++++++++ var padNum = k - 3 - m.length;
++++++++ var padByte;
++++++++ // private key op
++++++++ if(bt === 0x00 || bt === 0x01) {
++++++++ padByte = (bt === 0x00) ? 0x00 : 0xFF;
++++++++ for(var i = 0; i < padNum; ++i) {
++++++++ eb.putByte(padByte);
++++++++ }
++++++++ } else {
++++++++ // public key op
++++++++ // pad with random non-zero values
++++++++ while(padNum > 0) {
++++++++ var numZeros = 0;
++++++++ var padBytes = forge.random.getBytes(padNum);
++++++++ for(var i = 0; i < padNum; ++i) {
++++++++ padByte = padBytes.charCodeAt(i);
++++++++ if(padByte === 0) {
++++++++ ++numZeros;
++++++++ } else {
++++++++ eb.putByte(padByte);
++++++++ }
++++++++ }
++++++++ padNum = numZeros;
++++++++ }
++++++++ }
++++++++
++++++++ // zero followed by message
++++++++ eb.putByte(0x00);
++++++++ eb.putBytes(m);
++++++++
++++++++ return eb;
++++++++}
++++++++
++++++++/**
++++++++ * Decodes a message using PKCS#1 v1.5 padding.
++++++++ *
++++++++ * @param em the message to decode.
++++++++ * @param key the RSA key to use.
++++++++ * @param pub true if the key is a public key, false if it is private.
++++++++ * @param ml the message length, if specified.
++++++++ *
++++++++ * @return the decoded bytes.
++++++++ */
++++++++function _decodePkcs1_v1_5(em, key, pub, ml) {
++++++++ // get the length of the modulus in bytes
++++++++ var k = Math.ceil(key.n.bitLength() / 8);
++++++++
++++++++ /* It is an error if any of the following conditions occurs:
++++++++
++++++++ 1. The encryption block EB cannot be parsed unambiguously.
++++++++ 2. The padding string PS consists of fewer than eight octets
++++++++ or is inconsisent with the block type BT.
++++++++ 3. The decryption process is a public-key operation and the block
++++++++ type BT is not 00 or 01, or the decryption process is a
++++++++ private-key operation and the block type is not 02.
++++++++ */
++++++++
++++++++ // parse the encryption block
++++++++ var eb = forge.util.createBuffer(em);
++++++++ var first = eb.getByte();
++++++++ var bt = eb.getByte();
++++++++ if(first !== 0x00 ||
++++++++ (pub && bt !== 0x00 && bt !== 0x01) ||
++++++++ (!pub && bt != 0x02) ||
++++++++ (pub && bt === 0x00 && typeof(ml) === 'undefined')) {
++++++++ throw new Error('Encryption block is invalid.');
++++++++ }
++++++++
++++++++ var padNum = 0;
++++++++ if(bt === 0x00) {
++++++++ // check all padding bytes for 0x00
++++++++ padNum = k - 3 - ml;
++++++++ for(var i = 0; i < padNum; ++i) {
++++++++ if(eb.getByte() !== 0x00) {
++++++++ throw new Error('Encryption block is invalid.');
++++++++ }
++++++++ }
++++++++ } else if(bt === 0x01) {
++++++++ // find the first byte that isn't 0xFF, should be after all padding
++++++++ padNum = 0;
++++++++ while(eb.length() > 1) {
++++++++ if(eb.getByte() !== 0xFF) {
++++++++ --eb.read;
++++++++ break;
++++++++ }
++++++++ ++padNum;
++++++++ }
++++++++ } else if(bt === 0x02) {
++++++++ // look for 0x00 byte
++++++++ padNum = 0;
++++++++ while(eb.length() > 1) {
++++++++ if(eb.getByte() === 0x00) {
++++++++ --eb.read;
++++++++ break;
++++++++ }
++++++++ ++padNum;
++++++++ }
++++++++ }
++++++++
++++++++ // zero must be 0x00 and padNum must be (k - 3 - message length)
++++++++ var zero = eb.getByte();
++++++++ if(zero !== 0x00 || padNum !== (k - 3 - eb.length())) {
++++++++ throw new Error('Encryption block is invalid.');
++++++++ }
++++++++
++++++++ return eb.getBytes();
++++++++}
++++++++
++++++++/**
++++++++ * Runs the key-generation algorithm asynchronously, either in the background
++++++++ * via Web Workers, or using the main thread and setImmediate.
++++++++ *
++++++++ * @param state the key-pair generation state.
++++++++ * @param [options] options for key-pair generation:
++++++++ * workerScript the worker script URL.
++++++++ * workers the number of web workers (if supported) to use,
++++++++ * (default: 2, -1 to use estimated cores minus one).
++++++++ * workLoad the size of the work load, ie: number of possible prime
++++++++ * numbers for each web worker to check per work assignment,
++++++++ * (default: 100).
++++++++ * @param callback(err, keypair) called once the operation completes.
++++++++ */
++++++++function _generateKeyPair(state, options, callback) {
++++++++ if(typeof options === 'function') {
++++++++ callback = options;
++++++++ options = {};
++++++++ }
++++++++ options = options || {};
++++++++
++++++++ var opts = {
++++++++ algorithm: {
++++++++ name: options.algorithm || 'PRIMEINC',
++++++++ options: {
++++++++ workers: options.workers || 2,
++++++++ workLoad: options.workLoad || 100,
++++++++ workerScript: options.workerScript
++++++++ }
++++++++ }
++++++++ };
++++++++ if('prng' in options) {
++++++++ opts.prng = options.prng;
++++++++ }
++++++++
++++++++ generate();
++++++++
++++++++ function generate() {
++++++++ // find p and then q (done in series to simplify)
++++++++ getPrime(state.pBits, function(err, num) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++ state.p = num;
++++++++ if(state.q !== null) {
++++++++ return finish(err, state.q);
++++++++ }
++++++++ getPrime(state.qBits, finish);
++++++++ });
++++++++ }
++++++++
++++++++ function getPrime(bits, callback) {
++++++++ forge.prime.generateProbablePrime(bits, opts, callback);
++++++++ }
++++++++
++++++++ function finish(err, num) {
++++++++ if(err) {
++++++++ return callback(err);
++++++++ }
++++++++
++++++++ // set q
++++++++ state.q = num;
++++++++
++++++++ // ensure p is larger than q (swap them if not)
++++++++ if(state.p.compareTo(state.q) < 0) {
++++++++ var tmp = state.p;
++++++++ state.p = state.q;
++++++++ state.q = tmp;
++++++++ }
++++++++
++++++++ // ensure p is coprime with e
++++++++ if(state.p.subtract(BigInteger.ONE).gcd(state.e)
++++++++ .compareTo(BigInteger.ONE) !== 0) {
++++++++ state.p = null;
++++++++ generate();
++++++++ return;
++++++++ }
++++++++
++++++++ // ensure q is coprime with e
++++++++ if(state.q.subtract(BigInteger.ONE).gcd(state.e)
++++++++ .compareTo(BigInteger.ONE) !== 0) {
++++++++ state.q = null;
++++++++ getPrime(state.qBits, finish);
++++++++ return;
++++++++ }
++++++++
++++++++ // compute phi: (p - 1)(q - 1) (Euler's totient function)
++++++++ state.p1 = state.p.subtract(BigInteger.ONE);
++++++++ state.q1 = state.q.subtract(BigInteger.ONE);
++++++++ state.phi = state.p1.multiply(state.q1);
++++++++
++++++++ // ensure e and phi are coprime
++++++++ if(state.phi.gcd(state.e).compareTo(BigInteger.ONE) !== 0) {
++++++++ // phi and e aren't coprime, so generate a new p and q
++++++++ state.p = state.q = null;
++++++++ generate();
++++++++ return;
++++++++ }
++++++++
++++++++ // create n, ensure n is has the right number of bits
++++++++ state.n = state.p.multiply(state.q);
++++++++ if(state.n.bitLength() !== state.bits) {
++++++++ // failed, get new q
++++++++ state.q = null;
++++++++ getPrime(state.qBits, finish);
++++++++ return;
++++++++ }
++++++++
++++++++ // set keys
++++++++ var d = state.e.modInverse(state.phi);
++++++++ state.keys = {
++++++++ privateKey: pki.rsa.setPrivateKey(
++++++++ state.n, state.e, d, state.p, state.q,
++++++++ d.mod(state.p1), d.mod(state.q1),
++++++++ state.q.modInverse(state.p)),
++++++++ publicKey: pki.rsa.setPublicKey(state.n, state.e)
++++++++ };
++++++++
++++++++ callback(null, state.keys);
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Converts a positive BigInteger into 2's-complement big-endian bytes.
++++++++ *
++++++++ * @param b the big integer to convert.
++++++++ *
++++++++ * @return the bytes.
++++++++ */
++++++++function _bnToBytes(b) {
++++++++ // prepend 0x00 if first byte >= 0x80
++++++++ var hex = b.toString(16);
++++++++ if(hex[0] >= '8') {
++++++++ hex = '00' + hex;
++++++++ }
++++++++ var bytes = forge.util.hexToBytes(hex);
++++++++
++++++++ // ensure integer is minimally-encoded
++++++++ if(bytes.length > 1 &&
++++++++ // leading 0x00 for positive integer
++++++++ ((bytes.charCodeAt(0) === 0 &&
++++++++ (bytes.charCodeAt(1) & 0x80) === 0) ||
++++++++ // leading 0xFF for negative integer
++++++++ (bytes.charCodeAt(0) === 0xFF &&
++++++++ (bytes.charCodeAt(1) & 0x80) === 0x80))) {
++++++++ return bytes.substr(1);
++++++++ }
++++++++ return bytes;
++++++++}
++++++++
++++++++/**
++++++++ * Returns the required number of Miller-Rabin tests to generate a
++++++++ * prime with an error probability of (1/2)^80.
++++++++ *
++++++++ * See Handbook of Applied Cryptography Chapter 4, Table 4.4.
++++++++ *
++++++++ * @param bits the bit size.
++++++++ *
++++++++ * @return the required number of iterations.
++++++++ */
++++++++function _getMillerRabinTests(bits) {
++++++++ if(bits <= 100) return 27;
++++++++ if(bits <= 150) return 18;
++++++++ if(bits <= 200) return 15;
++++++++ if(bits <= 250) return 12;
++++++++ if(bits <= 300) return 9;
++++++++ if(bits <= 350) return 8;
++++++++ if(bits <= 400) return 7;
++++++++ if(bits <= 500) return 6;
++++++++ if(bits <= 600) return 5;
++++++++ if(bits <= 800) return 4;
++++++++ if(bits <= 1250) return 3;
++++++++ return 2;
++++++++}
++++++++
++++++++/**
++++++++ * Performs feature detection on the Node crypto interface.
++++++++ *
++++++++ * @param fn the feature (function) to detect.
++++++++ *
++++++++ * @return true if detected, false if not.
++++++++ */
++++++++function _detectNodeCrypto(fn) {
++++++++ return forge.util.isNodejs && typeof _crypto[fn] === 'function';
++++++++}
++++++++
++++++++/**
++++++++ * Performs feature detection on the SubtleCrypto interface.
++++++++ *
++++++++ * @param fn the feature (function) to detect.
++++++++ *
++++++++ * @return true if detected, false if not.
++++++++ */
++++++++function _detectSubtleCrypto(fn) {
++++++++ return (typeof util.globalScope !== 'undefined' &&
++++++++ typeof util.globalScope.crypto === 'object' &&
++++++++ typeof util.globalScope.crypto.subtle === 'object' &&
++++++++ typeof util.globalScope.crypto.subtle[fn] === 'function');
++++++++}
++++++++
++++++++/**
++++++++ * Performs feature detection on the deprecated Microsoft Internet Explorer
++++++++ * outdated SubtleCrypto interface. This function should only be used after
++++++++ * checking for the modern, standard SubtleCrypto interface.
++++++++ *
++++++++ * @param fn the feature (function) to detect.
++++++++ *
++++++++ * @return true if detected, false if not.
++++++++ */
++++++++function _detectSubtleMsCrypto(fn) {
++++++++ return (typeof util.globalScope !== 'undefined' &&
++++++++ typeof util.globalScope.msCrypto === 'object' &&
++++++++ typeof util.globalScope.msCrypto.subtle === 'object' &&
++++++++ typeof util.globalScope.msCrypto.subtle[fn] === 'function');
++++++++}
++++++++
++++++++function _intToUint8Array(x) {
++++++++ var bytes = forge.util.hexToBytes(x.toString(16));
++++++++ var buffer = new Uint8Array(bytes.length);
++++++++ for(var i = 0; i < bytes.length; ++i) {
++++++++ buffer[i] = bytes.charCodeAt(i);
++++++++ }
++++++++ return buffer;
++++++++}
++++++++
++++++++function _privateKeyFromJwk(jwk) {
++++++++ if(jwk.kty !== 'RSA') {
++++++++ throw new Error(
++++++++ 'Unsupported key algorithm "' + jwk.kty + '"; algorithm must be "RSA".');
++++++++ }
++++++++ return pki.setRsaPrivateKey(
++++++++ _base64ToBigInt(jwk.n),
++++++++ _base64ToBigInt(jwk.e),
++++++++ _base64ToBigInt(jwk.d),
++++++++ _base64ToBigInt(jwk.p),
++++++++ _base64ToBigInt(jwk.q),
++++++++ _base64ToBigInt(jwk.dp),
++++++++ _base64ToBigInt(jwk.dq),
++++++++ _base64ToBigInt(jwk.qi));
++++++++}
++++++++
++++++++function _publicKeyFromJwk(jwk) {
++++++++ if(jwk.kty !== 'RSA') {
++++++++ throw new Error('Key algorithm must be "RSA".');
++++++++ }
++++++++ return pki.setRsaPublicKey(
++++++++ _base64ToBigInt(jwk.n),
++++++++ _base64ToBigInt(jwk.e));
++++++++}
++++++++
++++++++function _base64ToBigInt(b64) {
++++++++ return new BigInteger(forge.util.bytesToHex(forge.util.decode64(b64)), 16);
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Secure Hash Algorithm with 160-bit digest (SHA-1) implementation.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2015 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++var sha1 = module.exports = forge.sha1 = forge.sha1 || {};
++++++++forge.md.sha1 = forge.md.algorithms.sha1 = sha1;
++++++++
++++++++/**
++++++++ * Creates a SHA-1 message digest object.
++++++++ *
++++++++ * @return a message digest object.
++++++++ */
++++++++sha1.create = function() {
++++++++ // do initialization as necessary
++++++++ if(!_initialized) {
++++++++ _init();
++++++++ }
++++++++
++++++++ // SHA-1 state contains five 32-bit integers
++++++++ var _state = null;
++++++++
++++++++ // input buffer
++++++++ var _input = forge.util.createBuffer();
++++++++
++++++++ // used for word storage
++++++++ var _w = new Array(80);
++++++++
++++++++ // message digest object
++++++++ var md = {
++++++++ algorithm: 'sha1',
++++++++ blockLength: 64,
++++++++ digestLength: 20,
++++++++ // 56-bit length of message so far (does not including padding)
++++++++ messageLength: 0,
++++++++ // true message length
++++++++ fullMessageLength: null,
++++++++ // size of message length in bytes
++++++++ messageLengthSize: 8
++++++++ };
++++++++
++++++++ /**
++++++++ * Starts the digest.
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.start = function() {
++++++++ // up to 56-bit message length for convenience
++++++++ md.messageLength = 0;
++++++++
++++++++ // full message length (set md.messageLength64 for backwards-compatibility)
++++++++ md.fullMessageLength = md.messageLength64 = [];
++++++++ var int32s = md.messageLengthSize / 4;
++++++++ for(var i = 0; i < int32s; ++i) {
++++++++ md.fullMessageLength.push(0);
++++++++ }
++++++++ _input = forge.util.createBuffer();
++++++++ _state = {
++++++++ h0: 0x67452301,
++++++++ h1: 0xEFCDAB89,
++++++++ h2: 0x98BADCFE,
++++++++ h3: 0x10325476,
++++++++ h4: 0xC3D2E1F0
++++++++ };
++++++++ return md;
++++++++ };
++++++++ // start digest automatically for first time
++++++++ md.start();
++++++++
++++++++ /**
++++++++ * Updates the digest with the given message input. The given input can
++++++++ * treated as raw input (no encoding will be applied) or an encoding of
++++++++ * 'utf8' maybe given to encode the input using UTF-8.
++++++++ *
++++++++ * @param msg the message input to update with.
++++++++ * @param encoding the encoding to use (default: 'raw', other: 'utf8').
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.update = function(msg, encoding) {
++++++++ if(encoding === 'utf8') {
++++++++ msg = forge.util.encodeUtf8(msg);
++++++++ }
++++++++
++++++++ // update message length
++++++++ var len = msg.length;
++++++++ md.messageLength += len;
++++++++ len = [(len / 0x100000000) >>> 0, len >>> 0];
++++++++ for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
++++++++ md.fullMessageLength[i] += len[1];
++++++++ len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0);
++++++++ md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0;
++++++++ len[0] = ((len[1] / 0x100000000) >>> 0);
++++++++ }
++++++++
++++++++ // add bytes to input buffer
++++++++ _input.putBytes(msg);
++++++++
++++++++ // process bytes
++++++++ _update(_state, _w, _input);
++++++++
++++++++ // compact input buffer every 2K or if empty
++++++++ if(_input.read > 2048 || _input.length() === 0) {
++++++++ _input.compact();
++++++++ }
++++++++
++++++++ return md;
++++++++ };
++++++++
++++++++ /**
++++++++ * Produces the digest.
++++++++ *
++++++++ * @return a byte buffer containing the digest value.
++++++++ */
++++++++ md.digest = function() {
++++++++ /* Note: Here we copy the remaining bytes in the input buffer and
++++++++ add the appropriate SHA-1 padding. Then we do the final update
++++++++ on a copy of the state so that if the user wants to get
++++++++ intermediate digests they can do so. */
++++++++
++++++++ /* Determine the number of bytes that must be added to the message
++++++++ to ensure its length is congruent to 448 mod 512. In other words,
++++++++ the data to be digested must be a multiple of 512 bits (or 128 bytes).
++++++++ This data includes the message, some padding, and the length of the
++++++++ message. Since the length of the message will be encoded as 8 bytes (64
++++++++ bits), that means that the last segment of the data must have 56 bytes
++++++++ (448 bits) of message and padding. Therefore, the length of the message
++++++++ plus the padding must be congruent to 448 mod 512 because
++++++++ 512 - 128 = 448.
++++++++
++++++++ In order to fill up the message length it must be filled with
++++++++ padding that begins with 1 bit followed by all 0 bits. Padding
++++++++ must *always* be present, so if the message length is already
++++++++ congruent to 448 mod 512, then 512 padding bits must be added. */
++++++++
++++++++ var finalBlock = forge.util.createBuffer();
++++++++ finalBlock.putBytes(_input.bytes());
++++++++
++++++++ // compute remaining size to be digested (include message length size)
++++++++ var remaining = (
++++++++ md.fullMessageLength[md.fullMessageLength.length - 1] +
++++++++ md.messageLengthSize);
++++++++
++++++++ // add padding for overflow blockSize - overflow
++++++++ // _padding starts with 1 byte with first bit is set (byte value 128), then
++++++++ // there may be up to (blockSize - 1) other pad bytes
++++++++ var overflow = remaining & (md.blockLength - 1);
++++++++ finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow));
++++++++
++++++++ // serialize message length in bits in big-endian order; since length
++++++++ // is stored in bytes we multiply by 8 and add carry from next int
++++++++ var next, carry;
++++++++ var bits = md.fullMessageLength[0] * 8;
++++++++ for(var i = 0; i < md.fullMessageLength.length - 1; ++i) {
++++++++ next = md.fullMessageLength[i + 1] * 8;
++++++++ carry = (next / 0x100000000) >>> 0;
++++++++ bits += carry;
++++++++ finalBlock.putInt32(bits >>> 0);
++++++++ bits = next >>> 0;
++++++++ }
++++++++ finalBlock.putInt32(bits);
++++++++
++++++++ var s2 = {
++++++++ h0: _state.h0,
++++++++ h1: _state.h1,
++++++++ h2: _state.h2,
++++++++ h3: _state.h3,
++++++++ h4: _state.h4
++++++++ };
++++++++ _update(s2, _w, finalBlock);
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putInt32(s2.h0);
++++++++ rval.putInt32(s2.h1);
++++++++ rval.putInt32(s2.h2);
++++++++ rval.putInt32(s2.h3);
++++++++ rval.putInt32(s2.h4);
++++++++ return rval;
++++++++ };
++++++++
++++++++ return md;
++++++++};
++++++++
++++++++// sha-1 padding bytes not initialized yet
++++++++var _padding = null;
++++++++var _initialized = false;
++++++++
++++++++/**
++++++++ * Initializes the constant tables.
++++++++ */
++++++++function _init() {
++++++++ // create padding
++++++++ _padding = String.fromCharCode(128);
++++++++ _padding += forge.util.fillString(String.fromCharCode(0x00), 64);
++++++++
++++++++ // now initialized
++++++++ _initialized = true;
++++++++}
++++++++
++++++++/**
++++++++ * Updates a SHA-1 state with the given byte buffer.
++++++++ *
++++++++ * @param s the SHA-1 state to update.
++++++++ * @param w the array to use to store words.
++++++++ * @param bytes the byte buffer to update with.
++++++++ */
++++++++function _update(s, w, bytes) {
++++++++ // consume 512 bit (64 byte) chunks
++++++++ var t, a, b, c, d, e, f, i;
++++++++ var len = bytes.length();
++++++++ while(len >= 64) {
++++++++ // the w array will be populated with sixteen 32-bit big-endian words
++++++++ // and then extended into 80 32-bit words according to SHA-1 algorithm
++++++++ // and for 32-79 using Max Locktyukhin's optimization
++++++++
++++++++ // initialize hash value for this chunk
++++++++ a = s.h0;
++++++++ b = s.h1;
++++++++ c = s.h2;
++++++++ d = s.h3;
++++++++ e = s.h4;
++++++++
++++++++ // round 1
++++++++ for(i = 0; i < 16; ++i) {
++++++++ t = bytes.getInt32();
++++++++ w[i] = t;
++++++++ f = d ^ (b & (c ^ d));
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++ for(; i < 20; ++i) {
++++++++ t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]);
++++++++ t = (t << 1) | (t >>> 31);
++++++++ w[i] = t;
++++++++ f = d ^ (b & (c ^ d));
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0x5A827999 + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++ // round 2
++++++++ for(; i < 32; ++i) {
++++++++ t = (w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]);
++++++++ t = (t << 1) | (t >>> 31);
++++++++ w[i] = t;
++++++++ f = b ^ c ^ d;
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++ for(; i < 40; ++i) {
++++++++ t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
++++++++ t = (t << 2) | (t >>> 30);
++++++++ w[i] = t;
++++++++ f = b ^ c ^ d;
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0x6ED9EBA1 + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++ // round 3
++++++++ for(; i < 60; ++i) {
++++++++ t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
++++++++ t = (t << 2) | (t >>> 30);
++++++++ w[i] = t;
++++++++ f = (b & c) | (d & (b ^ c));
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0x8F1BBCDC + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++ // round 4
++++++++ for(; i < 80; ++i) {
++++++++ t = (w[i - 6] ^ w[i - 16] ^ w[i - 28] ^ w[i - 32]);
++++++++ t = (t << 2) | (t >>> 30);
++++++++ w[i] = t;
++++++++ f = b ^ c ^ d;
++++++++ t = ((a << 5) | (a >>> 27)) + f + e + 0xCA62C1D6 + t;
++++++++ e = d;
++++++++ d = c;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ c = ((b << 30) | (b >>> 2)) >>> 0;
++++++++ b = a;
++++++++ a = t;
++++++++ }
++++++++
++++++++ // update hash state
++++++++ s.h0 = (s.h0 + a) | 0;
++++++++ s.h1 = (s.h1 + b) | 0;
++++++++ s.h2 = (s.h2 + c) | 0;
++++++++ s.h3 = (s.h3 + d) | 0;
++++++++ s.h4 = (s.h4 + e) | 0;
++++++++
++++++++ len -= 64;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Secure Hash Algorithm with 256-bit digest (SHA-256) implementation.
++++++++ *
++++++++ * See FIPS 180-2 for details.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2015 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++var sha256 = module.exports = forge.sha256 = forge.sha256 || {};
++++++++forge.md.sha256 = forge.md.algorithms.sha256 = sha256;
++++++++
++++++++/**
++++++++ * Creates a SHA-256 message digest object.
++++++++ *
++++++++ * @return a message digest object.
++++++++ */
++++++++sha256.create = function() {
++++++++ // do initialization as necessary
++++++++ if(!_initialized) {
++++++++ _init();
++++++++ }
++++++++
++++++++ // SHA-256 state contains eight 32-bit integers
++++++++ var _state = null;
++++++++
++++++++ // input buffer
++++++++ var _input = forge.util.createBuffer();
++++++++
++++++++ // used for word storage
++++++++ var _w = new Array(64);
++++++++
++++++++ // message digest object
++++++++ var md = {
++++++++ algorithm: 'sha256',
++++++++ blockLength: 64,
++++++++ digestLength: 32,
++++++++ // 56-bit length of message so far (does not including padding)
++++++++ messageLength: 0,
++++++++ // true message length
++++++++ fullMessageLength: null,
++++++++ // size of message length in bytes
++++++++ messageLengthSize: 8
++++++++ };
++++++++
++++++++ /**
++++++++ * Starts the digest.
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.start = function() {
++++++++ // up to 56-bit message length for convenience
++++++++ md.messageLength = 0;
++++++++
++++++++ // full message length (set md.messageLength64 for backwards-compatibility)
++++++++ md.fullMessageLength = md.messageLength64 = [];
++++++++ var int32s = md.messageLengthSize / 4;
++++++++ for(var i = 0; i < int32s; ++i) {
++++++++ md.fullMessageLength.push(0);
++++++++ }
++++++++ _input = forge.util.createBuffer();
++++++++ _state = {
++++++++ h0: 0x6A09E667,
++++++++ h1: 0xBB67AE85,
++++++++ h2: 0x3C6EF372,
++++++++ h3: 0xA54FF53A,
++++++++ h4: 0x510E527F,
++++++++ h5: 0x9B05688C,
++++++++ h6: 0x1F83D9AB,
++++++++ h7: 0x5BE0CD19
++++++++ };
++++++++ return md;
++++++++ };
++++++++ // start digest automatically for first time
++++++++ md.start();
++++++++
++++++++ /**
++++++++ * Updates the digest with the given message input. The given input can
++++++++ * treated as raw input (no encoding will be applied) or an encoding of
++++++++ * 'utf8' maybe given to encode the input using UTF-8.
++++++++ *
++++++++ * @param msg the message input to update with.
++++++++ * @param encoding the encoding to use (default: 'raw', other: 'utf8').
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.update = function(msg, encoding) {
++++++++ if(encoding === 'utf8') {
++++++++ msg = forge.util.encodeUtf8(msg);
++++++++ }
++++++++
++++++++ // update message length
++++++++ var len = msg.length;
++++++++ md.messageLength += len;
++++++++ len = [(len / 0x100000000) >>> 0, len >>> 0];
++++++++ for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
++++++++ md.fullMessageLength[i] += len[1];
++++++++ len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0);
++++++++ md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0;
++++++++ len[0] = ((len[1] / 0x100000000) >>> 0);
++++++++ }
++++++++
++++++++ // add bytes to input buffer
++++++++ _input.putBytes(msg);
++++++++
++++++++ // process bytes
++++++++ _update(_state, _w, _input);
++++++++
++++++++ // compact input buffer every 2K or if empty
++++++++ if(_input.read > 2048 || _input.length() === 0) {
++++++++ _input.compact();
++++++++ }
++++++++
++++++++ return md;
++++++++ };
++++++++
++++++++ /**
++++++++ * Produces the digest.
++++++++ *
++++++++ * @return a byte buffer containing the digest value.
++++++++ */
++++++++ md.digest = function() {
++++++++ /* Note: Here we copy the remaining bytes in the input buffer and
++++++++ add the appropriate SHA-256 padding. Then we do the final update
++++++++ on a copy of the state so that if the user wants to get
++++++++ intermediate digests they can do so. */
++++++++
++++++++ /* Determine the number of bytes that must be added to the message
++++++++ to ensure its length is congruent to 448 mod 512. In other words,
++++++++ the data to be digested must be a multiple of 512 bits (or 128 bytes).
++++++++ This data includes the message, some padding, and the length of the
++++++++ message. Since the length of the message will be encoded as 8 bytes (64
++++++++ bits), that means that the last segment of the data must have 56 bytes
++++++++ (448 bits) of message and padding. Therefore, the length of the message
++++++++ plus the padding must be congruent to 448 mod 512 because
++++++++ 512 - 128 = 448.
++++++++
++++++++ In order to fill up the message length it must be filled with
++++++++ padding that begins with 1 bit followed by all 0 bits. Padding
++++++++ must *always* be present, so if the message length is already
++++++++ congruent to 448 mod 512, then 512 padding bits must be added. */
++++++++
++++++++ var finalBlock = forge.util.createBuffer();
++++++++ finalBlock.putBytes(_input.bytes());
++++++++
++++++++ // compute remaining size to be digested (include message length size)
++++++++ var remaining = (
++++++++ md.fullMessageLength[md.fullMessageLength.length - 1] +
++++++++ md.messageLengthSize);
++++++++
++++++++ // add padding for overflow blockSize - overflow
++++++++ // _padding starts with 1 byte with first bit is set (byte value 128), then
++++++++ // there may be up to (blockSize - 1) other pad bytes
++++++++ var overflow = remaining & (md.blockLength - 1);
++++++++ finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow));
++++++++
++++++++ // serialize message length in bits in big-endian order; since length
++++++++ // is stored in bytes we multiply by 8 and add carry from next int
++++++++ var next, carry;
++++++++ var bits = md.fullMessageLength[0] * 8;
++++++++ for(var i = 0; i < md.fullMessageLength.length - 1; ++i) {
++++++++ next = md.fullMessageLength[i + 1] * 8;
++++++++ carry = (next / 0x100000000) >>> 0;
++++++++ bits += carry;
++++++++ finalBlock.putInt32(bits >>> 0);
++++++++ bits = next >>> 0;
++++++++ }
++++++++ finalBlock.putInt32(bits);
++++++++
++++++++ var s2 = {
++++++++ h0: _state.h0,
++++++++ h1: _state.h1,
++++++++ h2: _state.h2,
++++++++ h3: _state.h3,
++++++++ h4: _state.h4,
++++++++ h5: _state.h5,
++++++++ h6: _state.h6,
++++++++ h7: _state.h7
++++++++ };
++++++++ _update(s2, _w, finalBlock);
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putInt32(s2.h0);
++++++++ rval.putInt32(s2.h1);
++++++++ rval.putInt32(s2.h2);
++++++++ rval.putInt32(s2.h3);
++++++++ rval.putInt32(s2.h4);
++++++++ rval.putInt32(s2.h5);
++++++++ rval.putInt32(s2.h6);
++++++++ rval.putInt32(s2.h7);
++++++++ return rval;
++++++++ };
++++++++
++++++++ return md;
++++++++};
++++++++
++++++++// sha-256 padding bytes not initialized yet
++++++++var _padding = null;
++++++++var _initialized = false;
++++++++
++++++++// table of constants
++++++++var _k = null;
++++++++
++++++++/**
++++++++ * Initializes the constant tables.
++++++++ */
++++++++function _init() {
++++++++ // create padding
++++++++ _padding = String.fromCharCode(128);
++++++++ _padding += forge.util.fillString(String.fromCharCode(0x00), 64);
++++++++
++++++++ // create K table for SHA-256
++++++++ _k = [
++++++++ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
++++++++ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
++++++++ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
++++++++ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
++++++++ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
++++++++ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
++++++++ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
++++++++ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
++++++++ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
++++++++ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
++++++++ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
++++++++ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
++++++++ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
++++++++ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
++++++++ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
++++++++ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
++++++++
++++++++ // now initialized
++++++++ _initialized = true;
++++++++}
++++++++
++++++++/**
++++++++ * Updates a SHA-256 state with the given byte buffer.
++++++++ *
++++++++ * @param s the SHA-256 state to update.
++++++++ * @param w the array to use to store words.
++++++++ * @param bytes the byte buffer to update with.
++++++++ */
++++++++function _update(s, w, bytes) {
++++++++ // consume 512 bit (64 byte) chunks
++++++++ var t1, t2, s0, s1, ch, maj, i, a, b, c, d, e, f, g, h;
++++++++ var len = bytes.length();
++++++++ while(len >= 64) {
++++++++ // the w array will be populated with sixteen 32-bit big-endian words
++++++++ // and then extended into 64 32-bit words according to SHA-256
++++++++ for(i = 0; i < 16; ++i) {
++++++++ w[i] = bytes.getInt32();
++++++++ }
++++++++ for(; i < 64; ++i) {
++++++++ // XOR word 2 words ago rot right 17, rot right 19, shft right 10
++++++++ t1 = w[i - 2];
++++++++ t1 =
++++++++ ((t1 >>> 17) | (t1 << 15)) ^
++++++++ ((t1 >>> 19) | (t1 << 13)) ^
++++++++ (t1 >>> 10);
++++++++ // XOR word 15 words ago rot right 7, rot right 18, shft right 3
++++++++ t2 = w[i - 15];
++++++++ t2 =
++++++++ ((t2 >>> 7) | (t2 << 25)) ^
++++++++ ((t2 >>> 18) | (t2 << 14)) ^
++++++++ (t2 >>> 3);
++++++++ // sum(t1, word 7 ago, t2, word 16 ago) modulo 2^32
++++++++ w[i] = (t1 + w[i - 7] + t2 + w[i - 16]) | 0;
++++++++ }
++++++++
++++++++ // initialize hash value for this chunk
++++++++ a = s.h0;
++++++++ b = s.h1;
++++++++ c = s.h2;
++++++++ d = s.h3;
++++++++ e = s.h4;
++++++++ f = s.h5;
++++++++ g = s.h6;
++++++++ h = s.h7;
++++++++
++++++++ // round function
++++++++ for(i = 0; i < 64; ++i) {
++++++++ // Sum1(e)
++++++++ s1 =
++++++++ ((e >>> 6) | (e << 26)) ^
++++++++ ((e >>> 11) | (e << 21)) ^
++++++++ ((e >>> 25) | (e << 7));
++++++++ // Ch(e, f, g) (optimized the same way as SHA-1)
++++++++ ch = g ^ (e & (f ^ g));
++++++++ // Sum0(a)
++++++++ s0 =
++++++++ ((a >>> 2) | (a << 30)) ^
++++++++ ((a >>> 13) | (a << 19)) ^
++++++++ ((a >>> 22) | (a << 10));
++++++++ // Maj(a, b, c) (optimized the same way as SHA-1)
++++++++ maj = (a & b) | (c & (a ^ b));
++++++++
++++++++ // main algorithm
++++++++ t1 = h + s1 + ch + _k[i] + w[i];
++++++++ t2 = s0 + maj;
++++++++ h = g;
++++++++ g = f;
++++++++ f = e;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ // can't truncate with `| 0`
++++++++ e = (d + t1) >>> 0;
++++++++ d = c;
++++++++ c = b;
++++++++ b = a;
++++++++ // `>>> 0` necessary to avoid iOS/Safari 10 optimization bug
++++++++ // can't truncate with `| 0`
++++++++ a = (t1 + t2) >>> 0;
++++++++ }
++++++++
++++++++ // update hash state
++++++++ s.h0 = (s.h0 + a) | 0;
++++++++ s.h1 = (s.h1 + b) | 0;
++++++++ s.h2 = (s.h2 + c) | 0;
++++++++ s.h3 = (s.h3 + d) | 0;
++++++++ s.h4 = (s.h4 + e) | 0;
++++++++ s.h5 = (s.h5 + f) | 0;
++++++++ s.h6 = (s.h6 + g) | 0;
++++++++ s.h7 = (s.h7 + h) | 0;
++++++++ len -= 64;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Secure Hash Algorithm with a 1024-bit block size implementation.
++++++++ *
++++++++ * This includes: SHA-512, SHA-384, SHA-512/224, and SHA-512/256. For
++++++++ * SHA-256 (block size 512 bits), see sha256.js.
++++++++ *
++++++++ * See FIPS 180-4 for details.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2014-2015 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./md');
++++++++require('./util');
++++++++
++++++++var sha512 = module.exports = forge.sha512 = forge.sha512 || {};
++++++++
++++++++// SHA-512
++++++++forge.md.sha512 = forge.md.algorithms.sha512 = sha512;
++++++++
++++++++// SHA-384
++++++++var sha384 = forge.sha384 = forge.sha512.sha384 = forge.sha512.sha384 || {};
++++++++sha384.create = function() {
++++++++ return sha512.create('SHA-384');
++++++++};
++++++++forge.md.sha384 = forge.md.algorithms.sha384 = sha384;
++++++++
++++++++// SHA-512/256
++++++++forge.sha512.sha256 = forge.sha512.sha256 || {
++++++++ create: function() {
++++++++ return sha512.create('SHA-512/256');
++++++++ }
++++++++};
++++++++forge.md['sha512/256'] = forge.md.algorithms['sha512/256'] =
++++++++ forge.sha512.sha256;
++++++++
++++++++// SHA-512/224
++++++++forge.sha512.sha224 = forge.sha512.sha224 || {
++++++++ create: function() {
++++++++ return sha512.create('SHA-512/224');
++++++++ }
++++++++};
++++++++forge.md['sha512/224'] = forge.md.algorithms['sha512/224'] =
++++++++ forge.sha512.sha224;
++++++++
++++++++/**
++++++++ * Creates a SHA-2 message digest object.
++++++++ *
++++++++ * @param algorithm the algorithm to use (SHA-512, SHA-384, SHA-512/224,
++++++++ * SHA-512/256).
++++++++ *
++++++++ * @return a message digest object.
++++++++ */
++++++++sha512.create = function(algorithm) {
++++++++ // do initialization as necessary
++++++++ if(!_initialized) {
++++++++ _init();
++++++++ }
++++++++
++++++++ if(typeof algorithm === 'undefined') {
++++++++ algorithm = 'SHA-512';
++++++++ }
++++++++
++++++++ if(!(algorithm in _states)) {
++++++++ throw new Error('Invalid SHA-512 algorithm: ' + algorithm);
++++++++ }
++++++++
++++++++ // SHA-512 state contains eight 64-bit integers (each as two 32-bit ints)
++++++++ var _state = _states[algorithm];
++++++++ var _h = null;
++++++++
++++++++ // input buffer
++++++++ var _input = forge.util.createBuffer();
++++++++
++++++++ // used for 64-bit word storage
++++++++ var _w = new Array(80);
++++++++ for(var wi = 0; wi < 80; ++wi) {
++++++++ _w[wi] = new Array(2);
++++++++ }
++++++++
++++++++ // determine digest length by algorithm name (default)
++++++++ var digestLength = 64;
++++++++ switch(algorithm) {
++++++++ case 'SHA-384':
++++++++ digestLength = 48;
++++++++ break;
++++++++ case 'SHA-512/256':
++++++++ digestLength = 32;
++++++++ break;
++++++++ case 'SHA-512/224':
++++++++ digestLength = 28;
++++++++ break;
++++++++ }
++++++++
++++++++ // message digest object
++++++++ var md = {
++++++++ // SHA-512 => sha512
++++++++ algorithm: algorithm.replace('-', '').toLowerCase(),
++++++++ blockLength: 128,
++++++++ digestLength: digestLength,
++++++++ // 56-bit length of message so far (does not including padding)
++++++++ messageLength: 0,
++++++++ // true message length
++++++++ fullMessageLength: null,
++++++++ // size of message length in bytes
++++++++ messageLengthSize: 16
++++++++ };
++++++++
++++++++ /**
++++++++ * Starts the digest.
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.start = function() {
++++++++ // up to 56-bit message length for convenience
++++++++ md.messageLength = 0;
++++++++
++++++++ // full message length (set md.messageLength128 for backwards-compatibility)
++++++++ md.fullMessageLength = md.messageLength128 = [];
++++++++ var int32s = md.messageLengthSize / 4;
++++++++ for(var i = 0; i < int32s; ++i) {
++++++++ md.fullMessageLength.push(0);
++++++++ }
++++++++ _input = forge.util.createBuffer();
++++++++ _h = new Array(_state.length);
++++++++ for(var i = 0; i < _state.length; ++i) {
++++++++ _h[i] = _state[i].slice(0);
++++++++ }
++++++++ return md;
++++++++ };
++++++++ // start digest automatically for first time
++++++++ md.start();
++++++++
++++++++ /**
++++++++ * Updates the digest with the given message input. The given input can
++++++++ * treated as raw input (no encoding will be applied) or an encoding of
++++++++ * 'utf8' maybe given to encode the input using UTF-8.
++++++++ *
++++++++ * @param msg the message input to update with.
++++++++ * @param encoding the encoding to use (default: 'raw', other: 'utf8').
++++++++ *
++++++++ * @return this digest object.
++++++++ */
++++++++ md.update = function(msg, encoding) {
++++++++ if(encoding === 'utf8') {
++++++++ msg = forge.util.encodeUtf8(msg);
++++++++ }
++++++++
++++++++ // update message length
++++++++ var len = msg.length;
++++++++ md.messageLength += len;
++++++++ len = [(len / 0x100000000) >>> 0, len >>> 0];
++++++++ for(var i = md.fullMessageLength.length - 1; i >= 0; --i) {
++++++++ md.fullMessageLength[i] += len[1];
++++++++ len[1] = len[0] + ((md.fullMessageLength[i] / 0x100000000) >>> 0);
++++++++ md.fullMessageLength[i] = md.fullMessageLength[i] >>> 0;
++++++++ len[0] = ((len[1] / 0x100000000) >>> 0);
++++++++ }
++++++++
++++++++ // add bytes to input buffer
++++++++ _input.putBytes(msg);
++++++++
++++++++ // process bytes
++++++++ _update(_h, _w, _input);
++++++++
++++++++ // compact input buffer every 2K or if empty
++++++++ if(_input.read > 2048 || _input.length() === 0) {
++++++++ _input.compact();
++++++++ }
++++++++
++++++++ return md;
++++++++ };
++++++++
++++++++ /**
++++++++ * Produces the digest.
++++++++ *
++++++++ * @return a byte buffer containing the digest value.
++++++++ */
++++++++ md.digest = function() {
++++++++ /* Note: Here we copy the remaining bytes in the input buffer and
++++++++ add the appropriate SHA-512 padding. Then we do the final update
++++++++ on a copy of the state so that if the user wants to get
++++++++ intermediate digests they can do so. */
++++++++
++++++++ /* Determine the number of bytes that must be added to the message
++++++++ to ensure its length is congruent to 896 mod 1024. In other words,
++++++++ the data to be digested must be a multiple of 1024 bits (or 128 bytes).
++++++++ This data includes the message, some padding, and the length of the
++++++++ message. Since the length of the message will be encoded as 16 bytes (128
++++++++ bits), that means that the last segment of the data must have 112 bytes
++++++++ (896 bits) of message and padding. Therefore, the length of the message
++++++++ plus the padding must be congruent to 896 mod 1024 because
++++++++ 1024 - 128 = 896.
++++++++
++++++++ In order to fill up the message length it must be filled with
++++++++ padding that begins with 1 bit followed by all 0 bits. Padding
++++++++ must *always* be present, so if the message length is already
++++++++ congruent to 896 mod 1024, then 1024 padding bits must be added. */
++++++++
++++++++ var finalBlock = forge.util.createBuffer();
++++++++ finalBlock.putBytes(_input.bytes());
++++++++
++++++++ // compute remaining size to be digested (include message length size)
++++++++ var remaining = (
++++++++ md.fullMessageLength[md.fullMessageLength.length - 1] +
++++++++ md.messageLengthSize);
++++++++
++++++++ // add padding for overflow blockSize - overflow
++++++++ // _padding starts with 1 byte with first bit is set (byte value 128), then
++++++++ // there may be up to (blockSize - 1) other pad bytes
++++++++ var overflow = remaining & (md.blockLength - 1);
++++++++ finalBlock.putBytes(_padding.substr(0, md.blockLength - overflow));
++++++++
++++++++ // serialize message length in bits in big-endian order; since length
++++++++ // is stored in bytes we multiply by 8 and add carry from next int
++++++++ var next, carry;
++++++++ var bits = md.fullMessageLength[0] * 8;
++++++++ for(var i = 0; i < md.fullMessageLength.length - 1; ++i) {
++++++++ next = md.fullMessageLength[i + 1] * 8;
++++++++ carry = (next / 0x100000000) >>> 0;
++++++++ bits += carry;
++++++++ finalBlock.putInt32(bits >>> 0);
++++++++ bits = next >>> 0;
++++++++ }
++++++++ finalBlock.putInt32(bits);
++++++++
++++++++ var h = new Array(_h.length);
++++++++ for(var i = 0; i < _h.length; ++i) {
++++++++ h[i] = _h[i].slice(0);
++++++++ }
++++++++ _update(h, _w, finalBlock);
++++++++ var rval = forge.util.createBuffer();
++++++++ var hlen;
++++++++ if(algorithm === 'SHA-512') {
++++++++ hlen = h.length;
++++++++ } else if(algorithm === 'SHA-384') {
++++++++ hlen = h.length - 2;
++++++++ } else {
++++++++ hlen = h.length - 4;
++++++++ }
++++++++ for(var i = 0; i < hlen; ++i) {
++++++++ rval.putInt32(h[i][0]);
++++++++ if(i !== hlen - 1 || algorithm !== 'SHA-512/224') {
++++++++ rval.putInt32(h[i][1]);
++++++++ }
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ return md;
++++++++};
++++++++
++++++++// sha-512 padding bytes not initialized yet
++++++++var _padding = null;
++++++++var _initialized = false;
++++++++
++++++++// table of constants
++++++++var _k = null;
++++++++
++++++++// initial hash states
++++++++var _states = null;
++++++++
++++++++/**
++++++++ * Initializes the constant tables.
++++++++ */
++++++++function _init() {
++++++++ // create padding
++++++++ _padding = String.fromCharCode(128);
++++++++ _padding += forge.util.fillString(String.fromCharCode(0x00), 128);
++++++++
++++++++ // create K table for SHA-512
++++++++ _k = [
++++++++ [0x428a2f98, 0xd728ae22], [0x71374491, 0x23ef65cd],
++++++++ [0xb5c0fbcf, 0xec4d3b2f], [0xe9b5dba5, 0x8189dbbc],
++++++++ [0x3956c25b, 0xf348b538], [0x59f111f1, 0xb605d019],
++++++++ [0x923f82a4, 0xaf194f9b], [0xab1c5ed5, 0xda6d8118],
++++++++ [0xd807aa98, 0xa3030242], [0x12835b01, 0x45706fbe],
++++++++ [0x243185be, 0x4ee4b28c], [0x550c7dc3, 0xd5ffb4e2],
++++++++ [0x72be5d74, 0xf27b896f], [0x80deb1fe, 0x3b1696b1],
++++++++ [0x9bdc06a7, 0x25c71235], [0xc19bf174, 0xcf692694],
++++++++ [0xe49b69c1, 0x9ef14ad2], [0xefbe4786, 0x384f25e3],
++++++++ [0x0fc19dc6, 0x8b8cd5b5], [0x240ca1cc, 0x77ac9c65],
++++++++ [0x2de92c6f, 0x592b0275], [0x4a7484aa, 0x6ea6e483],
++++++++ [0x5cb0a9dc, 0xbd41fbd4], [0x76f988da, 0x831153b5],
++++++++ [0x983e5152, 0xee66dfab], [0xa831c66d, 0x2db43210],
++++++++ [0xb00327c8, 0x98fb213f], [0xbf597fc7, 0xbeef0ee4],
++++++++ [0xc6e00bf3, 0x3da88fc2], [0xd5a79147, 0x930aa725],
++++++++ [0x06ca6351, 0xe003826f], [0x14292967, 0x0a0e6e70],
++++++++ [0x27b70a85, 0x46d22ffc], [0x2e1b2138, 0x5c26c926],
++++++++ [0x4d2c6dfc, 0x5ac42aed], [0x53380d13, 0x9d95b3df],
++++++++ [0x650a7354, 0x8baf63de], [0x766a0abb, 0x3c77b2a8],
++++++++ [0x81c2c92e, 0x47edaee6], [0x92722c85, 0x1482353b],
++++++++ [0xa2bfe8a1, 0x4cf10364], [0xa81a664b, 0xbc423001],
++++++++ [0xc24b8b70, 0xd0f89791], [0xc76c51a3, 0x0654be30],
++++++++ [0xd192e819, 0xd6ef5218], [0xd6990624, 0x5565a910],
++++++++ [0xf40e3585, 0x5771202a], [0x106aa070, 0x32bbd1b8],
++++++++ [0x19a4c116, 0xb8d2d0c8], [0x1e376c08, 0x5141ab53],
++++++++ [0x2748774c, 0xdf8eeb99], [0x34b0bcb5, 0xe19b48a8],
++++++++ [0x391c0cb3, 0xc5c95a63], [0x4ed8aa4a, 0xe3418acb],
++++++++ [0x5b9cca4f, 0x7763e373], [0x682e6ff3, 0xd6b2b8a3],
++++++++ [0x748f82ee, 0x5defb2fc], [0x78a5636f, 0x43172f60],
++++++++ [0x84c87814, 0xa1f0ab72], [0x8cc70208, 0x1a6439ec],
++++++++ [0x90befffa, 0x23631e28], [0xa4506ceb, 0xde82bde9],
++++++++ [0xbef9a3f7, 0xb2c67915], [0xc67178f2, 0xe372532b],
++++++++ [0xca273ece, 0xea26619c], [0xd186b8c7, 0x21c0c207],
++++++++ [0xeada7dd6, 0xcde0eb1e], [0xf57d4f7f, 0xee6ed178],
++++++++ [0x06f067aa, 0x72176fba], [0x0a637dc5, 0xa2c898a6],
++++++++ [0x113f9804, 0xbef90dae], [0x1b710b35, 0x131c471b],
++++++++ [0x28db77f5, 0x23047d84], [0x32caab7b, 0x40c72493],
++++++++ [0x3c9ebe0a, 0x15c9bebc], [0x431d67c4, 0x9c100d4c],
++++++++ [0x4cc5d4be, 0xcb3e42b6], [0x597f299c, 0xfc657e2a],
++++++++ [0x5fcb6fab, 0x3ad6faec], [0x6c44198c, 0x4a475817]
++++++++ ];
++++++++
++++++++ // initial hash states
++++++++ _states = {};
++++++++ _states['SHA-512'] = [
++++++++ [0x6a09e667, 0xf3bcc908],
++++++++ [0xbb67ae85, 0x84caa73b],
++++++++ [0x3c6ef372, 0xfe94f82b],
++++++++ [0xa54ff53a, 0x5f1d36f1],
++++++++ [0x510e527f, 0xade682d1],
++++++++ [0x9b05688c, 0x2b3e6c1f],
++++++++ [0x1f83d9ab, 0xfb41bd6b],
++++++++ [0x5be0cd19, 0x137e2179]
++++++++ ];
++++++++ _states['SHA-384'] = [
++++++++ [0xcbbb9d5d, 0xc1059ed8],
++++++++ [0x629a292a, 0x367cd507],
++++++++ [0x9159015a, 0x3070dd17],
++++++++ [0x152fecd8, 0xf70e5939],
++++++++ [0x67332667, 0xffc00b31],
++++++++ [0x8eb44a87, 0x68581511],
++++++++ [0xdb0c2e0d, 0x64f98fa7],
++++++++ [0x47b5481d, 0xbefa4fa4]
++++++++ ];
++++++++ _states['SHA-512/256'] = [
++++++++ [0x22312194, 0xFC2BF72C],
++++++++ [0x9F555FA3, 0xC84C64C2],
++++++++ [0x2393B86B, 0x6F53B151],
++++++++ [0x96387719, 0x5940EABD],
++++++++ [0x96283EE2, 0xA88EFFE3],
++++++++ [0xBE5E1E25, 0x53863992],
++++++++ [0x2B0199FC, 0x2C85B8AA],
++++++++ [0x0EB72DDC, 0x81C52CA2]
++++++++ ];
++++++++ _states['SHA-512/224'] = [
++++++++ [0x8C3D37C8, 0x19544DA2],
++++++++ [0x73E19966, 0x89DCD4D6],
++++++++ [0x1DFAB7AE, 0x32FF9C82],
++++++++ [0x679DD514, 0x582F9FCF],
++++++++ [0x0F6D2B69, 0x7BD44DA8],
++++++++ [0x77E36F73, 0x04C48942],
++++++++ [0x3F9D85A8, 0x6A1D36C8],
++++++++ [0x1112E6AD, 0x91D692A1]
++++++++ ];
++++++++
++++++++ // now initialized
++++++++ _initialized = true;
++++++++}
++++++++
++++++++/**
++++++++ * Updates a SHA-512 state with the given byte buffer.
++++++++ *
++++++++ * @param s the SHA-512 state to update.
++++++++ * @param w the array to use to store words.
++++++++ * @param bytes the byte buffer to update with.
++++++++ */
++++++++function _update(s, w, bytes) {
++++++++ // consume 512 bit (128 byte) chunks
++++++++ var t1_hi, t1_lo;
++++++++ var t2_hi, t2_lo;
++++++++ var s0_hi, s0_lo;
++++++++ var s1_hi, s1_lo;
++++++++ var ch_hi, ch_lo;
++++++++ var maj_hi, maj_lo;
++++++++ var a_hi, a_lo;
++++++++ var b_hi, b_lo;
++++++++ var c_hi, c_lo;
++++++++ var d_hi, d_lo;
++++++++ var e_hi, e_lo;
++++++++ var f_hi, f_lo;
++++++++ var g_hi, g_lo;
++++++++ var h_hi, h_lo;
++++++++ var i, hi, lo, w2, w7, w15, w16;
++++++++ var len = bytes.length();
++++++++ while(len >= 128) {
++++++++ // the w array will be populated with sixteen 64-bit big-endian words
++++++++ // and then extended into 64 64-bit words according to SHA-512
++++++++ for(i = 0; i < 16; ++i) {
++++++++ w[i][0] = bytes.getInt32() >>> 0;
++++++++ w[i][1] = bytes.getInt32() >>> 0;
++++++++ }
++++++++ for(; i < 80; ++i) {
++++++++ // for word 2 words ago: ROTR 19(x) ^ ROTR 61(x) ^ SHR 6(x)
++++++++ w2 = w[i - 2];
++++++++ hi = w2[0];
++++++++ lo = w2[1];
++++++++
++++++++ // high bits
++++++++ t1_hi = (
++++++++ ((hi >>> 19) | (lo << 13)) ^ // ROTR 19
++++++++ ((lo >>> 29) | (hi << 3)) ^ // ROTR 61/(swap + ROTR 29)
++++++++ (hi >>> 6)) >>> 0; // SHR 6
++++++++ // low bits
++++++++ t1_lo = (
++++++++ ((hi << 13) | (lo >>> 19)) ^ // ROTR 19
++++++++ ((lo << 3) | (hi >>> 29)) ^ // ROTR 61/(swap + ROTR 29)
++++++++ ((hi << 26) | (lo >>> 6))) >>> 0; // SHR 6
++++++++
++++++++ // for word 15 words ago: ROTR 1(x) ^ ROTR 8(x) ^ SHR 7(x)
++++++++ w15 = w[i - 15];
++++++++ hi = w15[0];
++++++++ lo = w15[1];
++++++++
++++++++ // high bits
++++++++ t2_hi = (
++++++++ ((hi >>> 1) | (lo << 31)) ^ // ROTR 1
++++++++ ((hi >>> 8) | (lo << 24)) ^ // ROTR 8
++++++++ (hi >>> 7)) >>> 0; // SHR 7
++++++++ // low bits
++++++++ t2_lo = (
++++++++ ((hi << 31) | (lo >>> 1)) ^ // ROTR 1
++++++++ ((hi << 24) | (lo >>> 8)) ^ // ROTR 8
++++++++ ((hi << 25) | (lo >>> 7))) >>> 0; // SHR 7
++++++++
++++++++ // sum(t1, word 7 ago, t2, word 16 ago) modulo 2^64 (carry lo overflow)
++++++++ w7 = w[i - 7];
++++++++ w16 = w[i - 16];
++++++++ lo = (t1_lo + w7[1] + t2_lo + w16[1]);
++++++++ w[i][0] = (t1_hi + w7[0] + t2_hi + w16[0] +
++++++++ ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ w[i][1] = lo >>> 0;
++++++++ }
++++++++
++++++++ // initialize hash value for this chunk
++++++++ a_hi = s[0][0];
++++++++ a_lo = s[0][1];
++++++++ b_hi = s[1][0];
++++++++ b_lo = s[1][1];
++++++++ c_hi = s[2][0];
++++++++ c_lo = s[2][1];
++++++++ d_hi = s[3][0];
++++++++ d_lo = s[3][1];
++++++++ e_hi = s[4][0];
++++++++ e_lo = s[4][1];
++++++++ f_hi = s[5][0];
++++++++ f_lo = s[5][1];
++++++++ g_hi = s[6][0];
++++++++ g_lo = s[6][1];
++++++++ h_hi = s[7][0];
++++++++ h_lo = s[7][1];
++++++++
++++++++ // round function
++++++++ for(i = 0; i < 80; ++i) {
++++++++ // Sum1(e) = ROTR 14(e) ^ ROTR 18(e) ^ ROTR 41(e)
++++++++ s1_hi = (
++++++++ ((e_hi >>> 14) | (e_lo << 18)) ^ // ROTR 14
++++++++ ((e_hi >>> 18) | (e_lo << 14)) ^ // ROTR 18
++++++++ ((e_lo >>> 9) | (e_hi << 23))) >>> 0; // ROTR 41/(swap + ROTR 9)
++++++++ s1_lo = (
++++++++ ((e_hi << 18) | (e_lo >>> 14)) ^ // ROTR 14
++++++++ ((e_hi << 14) | (e_lo >>> 18)) ^ // ROTR 18
++++++++ ((e_lo << 23) | (e_hi >>> 9))) >>> 0; // ROTR 41/(swap + ROTR 9)
++++++++
++++++++ // Ch(e, f, g) (optimized the same way as SHA-1)
++++++++ ch_hi = (g_hi ^ (e_hi & (f_hi ^ g_hi))) >>> 0;
++++++++ ch_lo = (g_lo ^ (e_lo & (f_lo ^ g_lo))) >>> 0;
++++++++
++++++++ // Sum0(a) = ROTR 28(a) ^ ROTR 34(a) ^ ROTR 39(a)
++++++++ s0_hi = (
++++++++ ((a_hi >>> 28) | (a_lo << 4)) ^ // ROTR 28
++++++++ ((a_lo >>> 2) | (a_hi << 30)) ^ // ROTR 34/(swap + ROTR 2)
++++++++ ((a_lo >>> 7) | (a_hi << 25))) >>> 0; // ROTR 39/(swap + ROTR 7)
++++++++ s0_lo = (
++++++++ ((a_hi << 4) | (a_lo >>> 28)) ^ // ROTR 28
++++++++ ((a_lo << 30) | (a_hi >>> 2)) ^ // ROTR 34/(swap + ROTR 2)
++++++++ ((a_lo << 25) | (a_hi >>> 7))) >>> 0; // ROTR 39/(swap + ROTR 7)
++++++++
++++++++ // Maj(a, b, c) (optimized the same way as SHA-1)
++++++++ maj_hi = ((a_hi & b_hi) | (c_hi & (a_hi ^ b_hi))) >>> 0;
++++++++ maj_lo = ((a_lo & b_lo) | (c_lo & (a_lo ^ b_lo))) >>> 0;
++++++++
++++++++ // main algorithm
++++++++ // t1 = (h + s1 + ch + _k[i] + _w[i]) modulo 2^64 (carry lo overflow)
++++++++ lo = (h_lo + s1_lo + ch_lo + _k[i][1] + w[i][1]);
++++++++ t1_hi = (h_hi + s1_hi + ch_hi + _k[i][0] + w[i][0] +
++++++++ ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ t1_lo = lo >>> 0;
++++++++
++++++++ // t2 = s0 + maj modulo 2^64 (carry lo overflow)
++++++++ lo = s0_lo + maj_lo;
++++++++ t2_hi = (s0_hi + maj_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ t2_lo = lo >>> 0;
++++++++
++++++++ h_hi = g_hi;
++++++++ h_lo = g_lo;
++++++++
++++++++ g_hi = f_hi;
++++++++ g_lo = f_lo;
++++++++
++++++++ f_hi = e_hi;
++++++++ f_lo = e_lo;
++++++++
++++++++ // e = (d + t1) modulo 2^64 (carry lo overflow)
++++++++ lo = d_lo + t1_lo;
++++++++ e_hi = (d_hi + t1_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ e_lo = lo >>> 0;
++++++++
++++++++ d_hi = c_hi;
++++++++ d_lo = c_lo;
++++++++
++++++++ c_hi = b_hi;
++++++++ c_lo = b_lo;
++++++++
++++++++ b_hi = a_hi;
++++++++ b_lo = a_lo;
++++++++
++++++++ // a = (t1 + t2) modulo 2^64 (carry lo overflow)
++++++++ lo = t1_lo + t2_lo;
++++++++ a_hi = (t1_hi + t2_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ a_lo = lo >>> 0;
++++++++ }
++++++++
++++++++ // update hash state (additional modulo 2^64)
++++++++ lo = s[0][1] + a_lo;
++++++++ s[0][0] = (s[0][0] + a_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[0][1] = lo >>> 0;
++++++++
++++++++ lo = s[1][1] + b_lo;
++++++++ s[1][0] = (s[1][0] + b_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[1][1] = lo >>> 0;
++++++++
++++++++ lo = s[2][1] + c_lo;
++++++++ s[2][0] = (s[2][0] + c_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[2][1] = lo >>> 0;
++++++++
++++++++ lo = s[3][1] + d_lo;
++++++++ s[3][0] = (s[3][0] + d_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[3][1] = lo >>> 0;
++++++++
++++++++ lo = s[4][1] + e_lo;
++++++++ s[4][0] = (s[4][0] + e_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[4][1] = lo >>> 0;
++++++++
++++++++ lo = s[5][1] + f_lo;
++++++++ s[5][0] = (s[5][0] + f_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[5][1] = lo >>> 0;
++++++++
++++++++ lo = s[6][1] + g_lo;
++++++++ s[6][0] = (s[6][0] + g_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[6][1] = lo >>> 0;
++++++++
++++++++ lo = s[7][1] + h_lo;
++++++++ s[7][0] = (s[7][0] + h_hi + ((lo / 0x100000000) >>> 0)) >>> 0;
++++++++ s[7][1] = lo >>> 0;
++++++++
++++++++ len -= 128;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Socket implementation that uses flash SocketPool class as a backend.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./util');
++++++++
++++++++// define net namespace
++++++++var net = module.exports = forge.net = forge.net || {};
++++++++
++++++++// map of flash ID to socket pool
++++++++net.socketPools = {};
++++++++
++++++++/**
++++++++ * Creates a flash socket pool.
++++++++ *
++++++++ * @param options:
++++++++ * flashId: the dom ID for the flash object element.
++++++++ * policyPort: the default policy port for sockets, 0 to use the
++++++++ * flash default.
++++++++ * policyUrl: the default policy file URL for sockets (if provided
++++++++ * used instead of a policy port).
++++++++ * msie: true if the browser is msie, false if not.
++++++++ *
++++++++ * @return the created socket pool.
++++++++ */
++++++++net.createSocketPool = function(options) {
++++++++ // set default
++++++++ options.msie = options.msie || false;
++++++++
++++++++ // initialize the flash interface
++++++++ var spId = options.flashId;
++++++++ var api = document.getElementById(spId);
++++++++ api.init({marshallExceptions: !options.msie});
++++++++
++++++++ // create socket pool entry
++++++++ var sp = {
++++++++ // ID of the socket pool
++++++++ id: spId,
++++++++ // flash interface
++++++++ flashApi: api,
++++++++ // map of socket ID to sockets
++++++++ sockets: {},
++++++++ // default policy port
++++++++ policyPort: options.policyPort || 0,
++++++++ // default policy URL
++++++++ policyUrl: options.policyUrl || null
++++++++ };
++++++++ net.socketPools[spId] = sp;
++++++++
++++++++ // create event handler, subscribe to flash events
++++++++ if(options.msie === true) {
++++++++ sp.handler = function(e) {
++++++++ if(e.id in sp.sockets) {
++++++++ // get handler function
++++++++ var f;
++++++++ switch(e.type) {
++++++++ case 'connect':
++++++++ f = 'connected';
++++++++ break;
++++++++ case 'close':
++++++++ f = 'closed';
++++++++ break;
++++++++ case 'socketData':
++++++++ f = 'data';
++++++++ break;
++++++++ default:
++++++++ f = 'error';
++++++++ break;
++++++++ }
++++++++ /* IE calls javascript on the thread of the external object
++++++++ that triggered the event (in this case flash) ... which will
++++++++ either run concurrently with other javascript or pre-empt any
++++++++ running javascript in the middle of its execution (BAD!) ...
++++++++ calling setTimeout() will schedule the javascript to run on
++++++++ the javascript thread and solve this EVIL problem. */
++++++++ setTimeout(function() {sp.sockets[e.id][f](e);}, 0);
++++++++ }
++++++++ };
++++++++ } else {
++++++++ sp.handler = function(e) {
++++++++ if(e.id in sp.sockets) {
++++++++ // get handler function
++++++++ var f;
++++++++ switch(e.type) {
++++++++ case 'connect':
++++++++ f = 'connected';
++++++++ break;
++++++++ case 'close':
++++++++ f = 'closed';
++++++++ break;
++++++++ case 'socketData':
++++++++ f = 'data';
++++++++ break;
++++++++ default:
++++++++ f = 'error';
++++++++ break;
++++++++ }
++++++++ sp.sockets[e.id][f](e);
++++++++ }
++++++++ };
++++++++ }
++++++++ var handler = 'forge.net.socketPools[\'' + spId + '\'].handler';
++++++++ api.subscribe('connect', handler);
++++++++ api.subscribe('close', handler);
++++++++ api.subscribe('socketData', handler);
++++++++ api.subscribe('ioError', handler);
++++++++ api.subscribe('securityError', handler);
++++++++
++++++++ /**
++++++++ * Destroys a socket pool. The socket pool still needs to be cleaned
++++++++ * up via net.cleanup().
++++++++ */
++++++++ sp.destroy = function() {
++++++++ delete net.socketPools[options.flashId];
++++++++ for(var id in sp.sockets) {
++++++++ sp.sockets[id].destroy();
++++++++ }
++++++++ sp.sockets = {};
++++++++ api.cleanup();
++++++++ };
++++++++
++++++++ /**
++++++++ * Creates a new socket.
++++++++ *
++++++++ * @param options:
++++++++ * connected: function(event) called when the socket connects.
++++++++ * closed: function(event) called when the socket closes.
++++++++ * data: function(event) called when socket data has arrived,
++++++++ * it can be read from the socket using receive().
++++++++ * error: function(event) called when a socket error occurs.
++++++++ */
++++++++ sp.createSocket = function(options) {
++++++++ // default to empty options
++++++++ options = options || {};
++++++++
++++++++ // create flash socket
++++++++ var id = api.create();
++++++++
++++++++ // create javascript socket wrapper
++++++++ var socket = {
++++++++ id: id,
++++++++ // set handlers
++++++++ connected: options.connected || function(e) {},
++++++++ closed: options.closed || function(e) {},
++++++++ data: options.data || function(e) {},
++++++++ error: options.error || function(e) {}
++++++++ };
++++++++
++++++++ /**
++++++++ * Destroys this socket.
++++++++ */
++++++++ socket.destroy = function() {
++++++++ api.destroy(id);
++++++++ delete sp.sockets[id];
++++++++ };
++++++++
++++++++ /**
++++++++ * Connects this socket.
++++++++ *
++++++++ * @param options:
++++++++ * host: the host to connect to.
++++++++ * port: the port to connect to.
++++++++ * policyPort: the policy port to use (if non-default), 0 to
++++++++ * use the flash default.
++++++++ * policyUrl: the policy file URL to use (instead of port).
++++++++ */
++++++++ socket.connect = function(options) {
++++++++ // give precedence to policy URL over policy port
++++++++ // if no policy URL and passed port isn't 0, use default port,
++++++++ // otherwise use 0 for the port
++++++++ var policyUrl = options.policyUrl || null;
++++++++ var policyPort = 0;
++++++++ if(policyUrl === null && options.policyPort !== 0) {
++++++++ policyPort = options.policyPort || sp.policyPort;
++++++++ }
++++++++ api.connect(id, options.host, options.port, policyPort, policyUrl);
++++++++ };
++++++++
++++++++ /**
++++++++ * Closes this socket.
++++++++ */
++++++++ socket.close = function() {
++++++++ api.close(id);
++++++++ socket.closed({
++++++++ id: socket.id,
++++++++ type: 'close',
++++++++ bytesAvailable: 0
++++++++ });
++++++++ };
++++++++
++++++++ /**
++++++++ * Determines if the socket is connected or not.
++++++++ *
++++++++ * @return true if connected, false if not.
++++++++ */
++++++++ socket.isConnected = function() {
++++++++ return api.isConnected(id);
++++++++ };
++++++++
++++++++ /**
++++++++ * Writes bytes to this socket.
++++++++ *
++++++++ * @param bytes the bytes (as a string) to write.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++ socket.send = function(bytes) {
++++++++ return api.send(id, forge.util.encode64(bytes));
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads bytes from this socket (non-blocking). Fewer than the number
++++++++ * of bytes requested may be read if enough bytes are not available.
++++++++ *
++++++++ * This method should be called from the data handler if there are
++++++++ * enough bytes available. To see how many bytes are available, check
++++++++ * the 'bytesAvailable' property on the event in the data handler or
++++++++ * call the bytesAvailable() function on the socket. If the browser is
++++++++ * msie, then the bytesAvailable() function should be used to avoid
++++++++ * race conditions. Otherwise, using the property on the data handler's
++++++++ * event may be quicker.
++++++++ *
++++++++ * @param count the maximum number of bytes to read.
++++++++ *
++++++++ * @return the bytes read (as a string) or null on error.
++++++++ */
++++++++ socket.receive = function(count) {
++++++++ var rval = api.receive(id, count).rval;
++++++++ return (rval === null) ? null : forge.util.decode64(rval);
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets the number of bytes available for receiving on the socket.
++++++++ *
++++++++ * @return the number of bytes available for receiving.
++++++++ */
++++++++ socket.bytesAvailable = function() {
++++++++ return api.getBytesAvailable(id);
++++++++ };
++++++++
++++++++ // store and return socket
++++++++ sp.sockets[id] = socket;
++++++++ return socket;
++++++++ };
++++++++
++++++++ return sp;
++++++++};
++++++++
++++++++/**
++++++++ * Destroys a flash socket pool.
++++++++ *
++++++++ * @param options:
++++++++ * flashId: the dom ID for the flash object element.
++++++++ */
++++++++net.destroySocketPool = function(options) {
++++++++ if(options.flashId in net.socketPools) {
++++++++ var sp = net.socketPools[options.flashId];
++++++++ sp.destroy();
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new socket.
++++++++ *
++++++++ * @param options:
++++++++ * flashId: the dom ID for the flash object element.
++++++++ * connected: function(event) called when the socket connects.
++++++++ * closed: function(event) called when the socket closes.
++++++++ * data: function(event) called when socket data has arrived, it
++++++++ * can be read from the socket using receive().
++++++++ * error: function(event) called when a socket error occurs.
++++++++ *
++++++++ * @return the created socket.
++++++++ */
++++++++net.createSocket = function(options) {
++++++++ var socket = null;
++++++++ if(options.flashId in net.socketPools) {
++++++++ // get related socket pool
++++++++ var sp = net.socketPools[options.flashId];
++++++++ socket = sp.createSocket(options);
++++++++ }
++++++++ return socket;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Functions to output keys in SSH-friendly formats.
++++++++ *
++++++++ * This is part of the Forge project which may be used under the terms of
++++++++ * either the BSD License or the GNU General Public License (GPL) Version 2.
++++++++ *
++++++++ * See: https://github.com/digitalbazaar/forge/blob/cbebca3780658703d925b61b2caffb1d263a6c1d/LICENSE
++++++++ *
++++++++ * @author https://github.com/shellac
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./hmac');
++++++++require('./md5');
++++++++require('./sha1');
++++++++require('./util');
++++++++
++++++++var ssh = module.exports = forge.ssh = forge.ssh || {};
++++++++
++++++++/**
++++++++ * Encodes (and optionally encrypts) a private RSA key as a Putty PPK file.
++++++++ *
++++++++ * @param privateKey the key.
++++++++ * @param passphrase a passphrase to protect the key (falsy for no encryption).
++++++++ * @param comment a comment to include in the key file.
++++++++ *
++++++++ * @return the PPK file as a string.
++++++++ */
++++++++ssh.privateKeyToPutty = function(privateKey, passphrase, comment) {
++++++++ comment = comment || '';
++++++++ passphrase = passphrase || '';
++++++++ var algorithm = 'ssh-rsa';
++++++++ var encryptionAlgorithm = (passphrase === '') ? 'none' : 'aes256-cbc';
++++++++
++++++++ var ppk = 'PuTTY-User-Key-File-2: ' + algorithm + '\r\n';
++++++++ ppk += 'Encryption: ' + encryptionAlgorithm + '\r\n';
++++++++ ppk += 'Comment: ' + comment + '\r\n';
++++++++
++++++++ // public key into buffer for ppk
++++++++ var pubbuffer = forge.util.createBuffer();
++++++++ _addStringToBuffer(pubbuffer, algorithm);
++++++++ _addBigIntegerToBuffer(pubbuffer, privateKey.e);
++++++++ _addBigIntegerToBuffer(pubbuffer, privateKey.n);
++++++++
++++++++ // write public key
++++++++ var pub = forge.util.encode64(pubbuffer.bytes(), 64);
++++++++ var length = Math.floor(pub.length / 66) + 1; // 66 = 64 + \r\n
++++++++ ppk += 'Public-Lines: ' + length + '\r\n';
++++++++ ppk += pub;
++++++++
++++++++ // private key into a buffer
++++++++ var privbuffer = forge.util.createBuffer();
++++++++ _addBigIntegerToBuffer(privbuffer, privateKey.d);
++++++++ _addBigIntegerToBuffer(privbuffer, privateKey.p);
++++++++ _addBigIntegerToBuffer(privbuffer, privateKey.q);
++++++++ _addBigIntegerToBuffer(privbuffer, privateKey.qInv);
++++++++
++++++++ // optionally encrypt the private key
++++++++ var priv;
++++++++ if(!passphrase) {
++++++++ // use the unencrypted buffer
++++++++ priv = forge.util.encode64(privbuffer.bytes(), 64);
++++++++ } else {
++++++++ // encrypt RSA key using passphrase
++++++++ var encLen = privbuffer.length() + 16 - 1;
++++++++ encLen -= encLen % 16;
++++++++
++++++++ // pad private key with sha1-d data -- needs to be a multiple of 16
++++++++ var padding = _sha1(privbuffer.bytes());
++++++++
++++++++ padding.truncate(padding.length() - encLen + privbuffer.length());
++++++++ privbuffer.putBuffer(padding);
++++++++
++++++++ var aeskey = forge.util.createBuffer();
++++++++ aeskey.putBuffer(_sha1('\x00\x00\x00\x00', passphrase));
++++++++ aeskey.putBuffer(_sha1('\x00\x00\x00\x01', passphrase));
++++++++
++++++++ // encrypt some bytes using CBC mode
++++++++ // key is 40 bytes, so truncate *by* 8 bytes
++++++++ var cipher = forge.aes.createEncryptionCipher(aeskey.truncate(8), 'CBC');
++++++++ cipher.start(forge.util.createBuffer().fillWithByte(0, 16));
++++++++ cipher.update(privbuffer.copy());
++++++++ cipher.finish();
++++++++ var encrypted = cipher.output;
++++++++
++++++++ // Note: this appears to differ from Putty -- is forge wrong, or putty?
++++++++ // due to padding we finish as an exact multiple of 16
++++++++ encrypted.truncate(16); // all padding
++++++++
++++++++ priv = forge.util.encode64(encrypted.bytes(), 64);
++++++++ }
++++++++
++++++++ // output private key
++++++++ length = Math.floor(priv.length / 66) + 1; // 64 + \r\n
++++++++ ppk += '\r\nPrivate-Lines: ' + length + '\r\n';
++++++++ ppk += priv;
++++++++
++++++++ // MAC
++++++++ var mackey = _sha1('putty-private-key-file-mac-key', passphrase);
++++++++
++++++++ var macbuffer = forge.util.createBuffer();
++++++++ _addStringToBuffer(macbuffer, algorithm);
++++++++ _addStringToBuffer(macbuffer, encryptionAlgorithm);
++++++++ _addStringToBuffer(macbuffer, comment);
++++++++ macbuffer.putInt32(pubbuffer.length());
++++++++ macbuffer.putBuffer(pubbuffer);
++++++++ macbuffer.putInt32(privbuffer.length());
++++++++ macbuffer.putBuffer(privbuffer);
++++++++
++++++++ var hmac = forge.hmac.create();
++++++++ hmac.start('sha1', mackey);
++++++++ hmac.update(macbuffer.bytes());
++++++++
++++++++ ppk += '\r\nPrivate-MAC: ' + hmac.digest().toHex() + '\r\n';
++++++++
++++++++ return ppk;
++++++++};
++++++++
++++++++/**
++++++++ * Encodes a public RSA key as an OpenSSH file.
++++++++ *
++++++++ * @param key the key.
++++++++ * @param comment a comment.
++++++++ *
++++++++ * @return the public key in OpenSSH format.
++++++++ */
++++++++ssh.publicKeyToOpenSSH = function(key, comment) {
++++++++ var type = 'ssh-rsa';
++++++++ comment = comment || '';
++++++++
++++++++ var buffer = forge.util.createBuffer();
++++++++ _addStringToBuffer(buffer, type);
++++++++ _addBigIntegerToBuffer(buffer, key.e);
++++++++ _addBigIntegerToBuffer(buffer, key.n);
++++++++
++++++++ return type + ' ' + forge.util.encode64(buffer.bytes()) + ' ' + comment;
++++++++};
++++++++
++++++++/**
++++++++ * Encodes a private RSA key as an OpenSSH file.
++++++++ *
++++++++ * @param key the key.
++++++++ * @param passphrase a passphrase to protect the key (falsy for no encryption).
++++++++ *
++++++++ * @return the public key in OpenSSH format.
++++++++ */
++++++++ssh.privateKeyToOpenSSH = function(privateKey, passphrase) {
++++++++ if(!passphrase) {
++++++++ return forge.pki.privateKeyToPem(privateKey);
++++++++ }
++++++++ // OpenSSH private key is just a legacy format, it seems
++++++++ return forge.pki.encryptRsaPrivateKey(privateKey, passphrase,
++++++++ {legacy: true, algorithm: 'aes128'});
++++++++};
++++++++
++++++++/**
++++++++ * Gets the SSH fingerprint for the given public key.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * [md] the message digest object to use (defaults to forge.md.md5).
++++++++ * [encoding] an alternative output encoding, such as 'hex'
++++++++ * (defaults to none, outputs a byte buffer).
++++++++ * [delimiter] the delimiter to use between bytes for 'hex' encoded
++++++++ * output, eg: ':' (defaults to none).
++++++++ *
++++++++ * @return the fingerprint as a byte buffer or other encoding based on options.
++++++++ */
++++++++ssh.getPublicKeyFingerprint = function(key, options) {
++++++++ options = options || {};
++++++++ var md = options.md || forge.md.md5.create();
++++++++
++++++++ var type = 'ssh-rsa';
++++++++ var buffer = forge.util.createBuffer();
++++++++ _addStringToBuffer(buffer, type);
++++++++ _addBigIntegerToBuffer(buffer, key.e);
++++++++ _addBigIntegerToBuffer(buffer, key.n);
++++++++
++++++++ // hash public key bytes
++++++++ md.start();
++++++++ md.update(buffer.getBytes());
++++++++ var digest = md.digest();
++++++++ if(options.encoding === 'hex') {
++++++++ var hex = digest.toHex();
++++++++ if(options.delimiter) {
++++++++ return hex.match(/.{2}/g).join(options.delimiter);
++++++++ }
++++++++ return hex;
++++++++ } else if(options.encoding === 'binary') {
++++++++ return digest.getBytes();
++++++++ } else if(options.encoding) {
++++++++ throw new Error('Unknown encoding "' + options.encoding + '".');
++++++++ }
++++++++ return digest;
++++++++};
++++++++
++++++++/**
++++++++ * Adds len(val) then val to a buffer.
++++++++ *
++++++++ * @param buffer the buffer to add to.
++++++++ * @param val a big integer.
++++++++ */
++++++++function _addBigIntegerToBuffer(buffer, val) {
++++++++ var hexVal = val.toString(16);
++++++++ // ensure 2s complement +ve
++++++++ if(hexVal[0] >= '8') {
++++++++ hexVal = '00' + hexVal;
++++++++ }
++++++++ var bytes = forge.util.hexToBytes(hexVal);
++++++++ buffer.putInt32(bytes.length);
++++++++ buffer.putBytes(bytes);
++++++++}
++++++++
++++++++/**
++++++++ * Adds len(val) then val to a buffer.
++++++++ *
++++++++ * @param buffer the buffer to add to.
++++++++ * @param val a string.
++++++++ */
++++++++function _addStringToBuffer(buffer, val) {
++++++++ buffer.putInt32(val.length);
++++++++ buffer.putString(val);
++++++++}
++++++++
++++++++/**
++++++++ * Hashes the arguments into one value using SHA-1.
++++++++ *
++++++++ * @return the sha1 hash of the provided arguments.
++++++++ */
++++++++function _sha1() {
++++++++ var sha = forge.md.sha1.create();
++++++++ var num = arguments.length;
++++++++ for (var i = 0; i < num; ++i) {
++++++++ sha.update(arguments[i]);
++++++++ }
++++++++ return sha.digest();
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * A Javascript implementation of Transport Layer Security (TLS).
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2009-2014 Digital Bazaar, Inc.
++++++++ *
++++++++ * The TLS Handshake Protocol involves the following steps:
++++++++ *
++++++++ * - Exchange hello messages to agree on algorithms, exchange random values,
++++++++ * and check for session resumption.
++++++++ *
++++++++ * - Exchange the necessary cryptographic parameters to allow the client and
++++++++ * server to agree on a premaster secret.
++++++++ *
++++++++ * - Exchange certificates and cryptographic information to allow the client
++++++++ * and server to authenticate themselves.
++++++++ *
++++++++ * - Generate a master secret from the premaster secret and exchanged random
++++++++ * values.
++++++++ *
++++++++ * - Provide security parameters to the record layer.
++++++++ *
++++++++ * - Allow the client and server to verify that their peer has calculated the
++++++++ * same security parameters and that the handshake occurred without tampering
++++++++ * by an attacker.
++++++++ *
++++++++ * Up to 4 different messages may be sent during a key exchange. The server
++++++++ * certificate, the server key exchange, the client certificate, and the
++++++++ * client key exchange.
++++++++ *
++++++++ * A typical handshake (from the client's perspective).
++++++++ *
++++++++ * 1. Client sends ClientHello.
++++++++ * 2. Client receives ServerHello.
++++++++ * 3. Client receives optional Certificate.
++++++++ * 4. Client receives optional ServerKeyExchange.
++++++++ * 5. Client receives ServerHelloDone.
++++++++ * 6. Client sends optional Certificate.
++++++++ * 7. Client sends ClientKeyExchange.
++++++++ * 8. Client sends optional CertificateVerify.
++++++++ * 9. Client sends ChangeCipherSpec.
++++++++ * 10. Client sends Finished.
++++++++ * 11. Client receives ChangeCipherSpec.
++++++++ * 12. Client receives Finished.
++++++++ * 13. Client sends/receives application data.
++++++++ *
++++++++ * To reuse an existing session:
++++++++ *
++++++++ * 1. Client sends ClientHello with session ID for reuse.
++++++++ * 2. Client receives ServerHello with same session ID if reusing.
++++++++ * 3. Client receives ChangeCipherSpec message if reusing.
++++++++ * 4. Client receives Finished.
++++++++ * 5. Client sends ChangeCipherSpec.
++++++++ * 6. Client sends Finished.
++++++++ *
++++++++ * Note: Client ignores HelloRequest if in the middle of a handshake.
++++++++ *
++++++++ * Record Layer:
++++++++ *
++++++++ * The record layer fragments information blocks into TLSPlaintext records
++++++++ * carrying data in chunks of 2^14 bytes or less. Client message boundaries are
++++++++ * not preserved in the record layer (i.e., multiple client messages of the
++++++++ * same ContentType MAY be coalesced into a single TLSPlaintext record, or a
++++++++ * single message MAY be fragmented across several records).
++++++++ *
++++++++ * struct {
++++++++ * uint8 major;
++++++++ * uint8 minor;
++++++++ * } ProtocolVersion;
++++++++ *
++++++++ * struct {
++++++++ * ContentType type;
++++++++ * ProtocolVersion version;
++++++++ * uint16 length;
++++++++ * opaque fragment[TLSPlaintext.length];
++++++++ * } TLSPlaintext;
++++++++ *
++++++++ * type:
++++++++ * The higher-level protocol used to process the enclosed fragment.
++++++++ *
++++++++ * version:
++++++++ * The version of the protocol being employed. TLS Version 1.2 uses version
++++++++ * {3, 3}. TLS Version 1.0 uses version {3, 1}. Note that a client that
++++++++ * supports multiple versions of TLS may not know what version will be
++++++++ * employed before it receives the ServerHello.
++++++++ *
++++++++ * length:
++++++++ * The length (in bytes) of the following TLSPlaintext.fragment. The length
++++++++ * MUST NOT exceed 2^14 = 16384 bytes.
++++++++ *
++++++++ * fragment:
++++++++ * The application data. This data is transparent and treated as an
++++++++ * independent block to be dealt with by the higher-level protocol specified
++++++++ * by the type field.
++++++++ *
++++++++ * Implementations MUST NOT send zero-length fragments of Handshake, Alert, or
++++++++ * ChangeCipherSpec content types. Zero-length fragments of Application data
++++++++ * MAY be sent as they are potentially useful as a traffic analysis
++++++++ * countermeasure.
++++++++ *
++++++++ * Note: Data of different TLS record layer content types MAY be interleaved.
++++++++ * Application data is generally of lower precedence for transmission than
++++++++ * other content types. However, records MUST be delivered to the network in
++++++++ * the same order as they are protected by the record layer. Recipients MUST
++++++++ * receive and process interleaved application layer traffic during handshakes
++++++++ * subsequent to the first one on a connection.
++++++++ *
++++++++ * struct {
++++++++ * ContentType type; // same as TLSPlaintext.type
++++++++ * ProtocolVersion version;// same as TLSPlaintext.version
++++++++ * uint16 length;
++++++++ * opaque fragment[TLSCompressed.length];
++++++++ * } TLSCompressed;
++++++++ *
++++++++ * length:
++++++++ * The length (in bytes) of the following TLSCompressed.fragment.
++++++++ * The length MUST NOT exceed 2^14 + 1024.
++++++++ *
++++++++ * fragment:
++++++++ * The compressed form of TLSPlaintext.fragment.
++++++++ *
++++++++ * Note: A CompressionMethod.null operation is an identity operation; no fields
++++++++ * are altered. In this implementation, since no compression is supported,
++++++++ * uncompressed records are always the same as compressed records.
++++++++ *
++++++++ * Encryption Information:
++++++++ *
++++++++ * The encryption and MAC functions translate a TLSCompressed structure into a
++++++++ * TLSCiphertext. The decryption functions reverse the process. The MAC of the
++++++++ * record also includes a sequence number so that missing, extra, or repeated
++++++++ * messages are detectable.
++++++++ *
++++++++ * struct {
++++++++ * ContentType type;
++++++++ * ProtocolVersion version;
++++++++ * uint16 length;
++++++++ * select (SecurityParameters.cipher_type) {
++++++++ * case stream: GenericStreamCipher;
++++++++ * case block: GenericBlockCipher;
++++++++ * case aead: GenericAEADCipher;
++++++++ * } fragment;
++++++++ * } TLSCiphertext;
++++++++ *
++++++++ * type:
++++++++ * The type field is identical to TLSCompressed.type.
++++++++ *
++++++++ * version:
++++++++ * The version field is identical to TLSCompressed.version.
++++++++ *
++++++++ * length:
++++++++ * The length (in bytes) of the following TLSCiphertext.fragment.
++++++++ * The length MUST NOT exceed 2^14 + 2048.
++++++++ *
++++++++ * fragment:
++++++++ * The encrypted form of TLSCompressed.fragment, with the MAC.
++++++++ *
++++++++ * Note: Only CBC Block Ciphers are supported by this implementation.
++++++++ *
++++++++ * The TLSCompressed.fragment structures are converted to/from block
++++++++ * TLSCiphertext.fragment structures.
++++++++ *
++++++++ * struct {
++++++++ * opaque IV[SecurityParameters.record_iv_length];
++++++++ * block-ciphered struct {
++++++++ * opaque content[TLSCompressed.length];
++++++++ * opaque MAC[SecurityParameters.mac_length];
++++++++ * uint8 padding[GenericBlockCipher.padding_length];
++++++++ * uint8 padding_length;
++++++++ * };
++++++++ * } GenericBlockCipher;
++++++++ *
++++++++ * The MAC is generated as described in Section 6.2.3.1.
++++++++ *
++++++++ * IV:
++++++++ * The Initialization Vector (IV) SHOULD be chosen at random, and MUST be
++++++++ * unpredictable. Note that in versions of TLS prior to 1.1, there was no
++++++++ * IV field, and the last ciphertext block of the previous record (the "CBC
++++++++ * residue") was used as the IV. This was changed to prevent the attacks
++++++++ * described in [CBCATT]. For block ciphers, the IV length is of length
++++++++ * SecurityParameters.record_iv_length, which is equal to the
++++++++ * SecurityParameters.block_size.
++++++++ *
++++++++ * padding:
++++++++ * Padding that is added to force the length of the plaintext to be an
++++++++ * integral multiple of the block cipher's block length. The padding MAY be
++++++++ * any length up to 255 bytes, as long as it results in the
++++++++ * TLSCiphertext.length being an integral multiple of the block length.
++++++++ * Lengths longer than necessary might be desirable to frustrate attacks on
++++++++ * a protocol that are based on analysis of the lengths of exchanged
++++++++ * messages. Each uint8 in the padding data vector MUST be filled with the
++++++++ * padding length value. The receiver MUST check this padding and MUST use
++++++++ * the bad_record_mac alert to indicate padding errors.
++++++++ *
++++++++ * padding_length:
++++++++ * The padding length MUST be such that the total size of the
++++++++ * GenericBlockCipher structure is a multiple of the cipher's block length.
++++++++ * Legal values range from zero to 255, inclusive. This length specifies the
++++++++ * length of the padding field exclusive of the padding_length field itself.
++++++++ *
++++++++ * The encrypted data length (TLSCiphertext.length) is one more than the sum of
++++++++ * SecurityParameters.block_length, TLSCompressed.length,
++++++++ * SecurityParameters.mac_length, and padding_length.
++++++++ *
++++++++ * Example: If the block length is 8 bytes, the content length
++++++++ * (TLSCompressed.length) is 61 bytes, and the MAC length is 20 bytes, then the
++++++++ * length before padding is 82 bytes (this does not include the IV. Thus, the
++++++++ * padding length modulo 8 must be equal to 6 in order to make the total length
++++++++ * an even multiple of 8 bytes (the block length). The padding length can be
++++++++ * 6, 14, 22, and so on, through 254. If the padding length were the minimum
++++++++ * necessary, 6, the padding would be 6 bytes, each containing the value 6.
++++++++ * Thus, the last 8 octets of the GenericBlockCipher before block encryption
++++++++ * would be xx 06 06 06 06 06 06 06, where xx is the last octet of the MAC.
++++++++ *
++++++++ * Note: With block ciphers in CBC mode (Cipher Block Chaining), it is critical
++++++++ * that the entire plaintext of the record be known before any ciphertext is
++++++++ * transmitted. Otherwise, it is possible for the attacker to mount the attack
++++++++ * described in [CBCATT].
++++++++ *
++++++++ * Implementation note: Canvel et al. [CBCTIME] have demonstrated a timing
++++++++ * attack on CBC padding based on the time required to compute the MAC. In
++++++++ * order to defend against this attack, implementations MUST ensure that
++++++++ * record processing time is essentially the same whether or not the padding
++++++++ * is correct. In general, the best way to do this is to compute the MAC even
++++++++ * if the padding is incorrect, and only then reject the packet. For instance,
++++++++ * if the pad appears to be incorrect, the implementation might assume a
++++++++ * zero-length pad and then compute the MAC. This leaves a small timing
++++++++ * channel, since MAC performance depends, to some extent, on the size of the
++++++++ * data fragment, but it is not believed to be large enough to be exploitable,
++++++++ * due to the large block size of existing MACs and the small size of the
++++++++ * timing signal.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./asn1');
++++++++require('./hmac');
++++++++require('./md5');
++++++++require('./pem');
++++++++require('./pki');
++++++++require('./random');
++++++++require('./sha1');
++++++++require('./util');
++++++++
++++++++/**
++++++++ * Generates pseudo random bytes by mixing the result of two hash functions,
++++++++ * MD5 and SHA-1.
++++++++ *
++++++++ * prf_TLS1(secret, label, seed) =
++++++++ * P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed);
++++++++ *
++++++++ * Each P_hash function functions as follows:
++++++++ *
++++++++ * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
++++++++ * HMAC_hash(secret, A(2) + seed) +
++++++++ * HMAC_hash(secret, A(3) + seed) + ...
++++++++ * A() is defined as:
++++++++ * A(0) = seed
++++++++ * A(i) = HMAC_hash(secret, A(i-1))
++++++++ *
++++++++ * The '+' operator denotes concatenation.
++++++++ *
++++++++ * As many iterations A(N) as are needed are performed to generate enough
++++++++ * pseudo random byte output. If an iteration creates more data than is
++++++++ * necessary, then it is truncated.
++++++++ *
++++++++ * Therefore:
++++++++ * A(1) = HMAC_hash(secret, A(0))
++++++++ * = HMAC_hash(secret, seed)
++++++++ * A(2) = HMAC_hash(secret, A(1))
++++++++ * = HMAC_hash(secret, HMAC_hash(secret, seed))
++++++++ *
++++++++ * Therefore:
++++++++ * P_hash(secret, seed) =
++++++++ * HMAC_hash(secret, HMAC_hash(secret, A(0)) + seed) +
++++++++ * HMAC_hash(secret, HMAC_hash(secret, A(1)) + seed) +
++++++++ * ...
++++++++ *
++++++++ * Therefore:
++++++++ * P_hash(secret, seed) =
++++++++ * HMAC_hash(secret, HMAC_hash(secret, seed) + seed) +
++++++++ * HMAC_hash(secret, HMAC_hash(secret, HMAC_hash(secret, seed)) + seed) +
++++++++ * ...
++++++++ *
++++++++ * @param secret the secret to use.
++++++++ * @param label the label to use.
++++++++ * @param seed the seed value to use.
++++++++ * @param length the number of bytes to generate.
++++++++ *
++++++++ * @return the pseudo random bytes in a byte buffer.
++++++++ */
++++++++var prf_TLS1 = function(secret, label, seed, length) {
++++++++ var rval = forge.util.createBuffer();
++++++++
++++++++ /* For TLS 1.0, the secret is split in half, into two secrets of equal
++++++++ length. If the secret has an odd length then the last byte of the first
++++++++ half will be the same as the first byte of the second. The length of the
++++++++ two secrets is half of the secret rounded up. */
++++++++ var idx = (secret.length >> 1);
++++++++ var slen = idx + (secret.length & 1);
++++++++ var s1 = secret.substr(0, slen);
++++++++ var s2 = secret.substr(idx, slen);
++++++++ var ai = forge.util.createBuffer();
++++++++ var hmac = forge.hmac.create();
++++++++ seed = label + seed;
++++++++
++++++++ // determine the number of iterations that must be performed to generate
++++++++ // enough output bytes, md5 creates 16 byte hashes, sha1 creates 20
++++++++ var md5itr = Math.ceil(length / 16);
++++++++ var sha1itr = Math.ceil(length / 20);
++++++++
++++++++ // do md5 iterations
++++++++ hmac.start('MD5', s1);
++++++++ var md5bytes = forge.util.createBuffer();
++++++++ ai.putBytes(seed);
++++++++ for(var i = 0; i < md5itr; ++i) {
++++++++ // HMAC_hash(secret, A(i-1))
++++++++ hmac.start(null, null);
++++++++ hmac.update(ai.getBytes());
++++++++ ai.putBuffer(hmac.digest());
++++++++
++++++++ // HMAC_hash(secret, A(i) + seed)
++++++++ hmac.start(null, null);
++++++++ hmac.update(ai.bytes() + seed);
++++++++ md5bytes.putBuffer(hmac.digest());
++++++++ }
++++++++
++++++++ // do sha1 iterations
++++++++ hmac.start('SHA1', s2);
++++++++ var sha1bytes = forge.util.createBuffer();
++++++++ ai.clear();
++++++++ ai.putBytes(seed);
++++++++ for(var i = 0; i < sha1itr; ++i) {
++++++++ // HMAC_hash(secret, A(i-1))
++++++++ hmac.start(null, null);
++++++++ hmac.update(ai.getBytes());
++++++++ ai.putBuffer(hmac.digest());
++++++++
++++++++ // HMAC_hash(secret, A(i) + seed)
++++++++ hmac.start(null, null);
++++++++ hmac.update(ai.bytes() + seed);
++++++++ sha1bytes.putBuffer(hmac.digest());
++++++++ }
++++++++
++++++++ // XOR the md5 bytes with the sha1 bytes
++++++++ rval.putBytes(forge.util.xorBytes(
++++++++ md5bytes.getBytes(), sha1bytes.getBytes(), length));
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Generates pseudo random bytes using a SHA256 algorithm. For TLS 1.2.
++++++++ *
++++++++ * @param secret the secret to use.
++++++++ * @param label the label to use.
++++++++ * @param seed the seed value to use.
++++++++ * @param length the number of bytes to generate.
++++++++ *
++++++++ * @return the pseudo random bytes in a byte buffer.
++++++++ */
++++++++var prf_sha256 = function(secret, label, seed, length) {
++++++++ // FIXME: implement me for TLS 1.2
++++++++};
++++++++
++++++++/**
++++++++ * Gets a MAC for a record using the SHA-1 hash algorithm.
++++++++ *
++++++++ * @param key the mac key.
++++++++ * @param state the sequence number (array of two 32-bit integers).
++++++++ * @param record the record.
++++++++ *
++++++++ * @return the sha-1 hash (20 bytes) for the given record.
++++++++ */
++++++++var hmac_sha1 = function(key, seqNum, record) {
++++++++ /* MAC is computed like so:
++++++++ HMAC_hash(
++++++++ key, seqNum +
++++++++ TLSCompressed.type +
++++++++ TLSCompressed.version +
++++++++ TLSCompressed.length +
++++++++ TLSCompressed.fragment)
++++++++ */
++++++++ var hmac = forge.hmac.create();
++++++++ hmac.start('SHA1', key);
++++++++ var b = forge.util.createBuffer();
++++++++ b.putInt32(seqNum[0]);
++++++++ b.putInt32(seqNum[1]);
++++++++ b.putByte(record.type);
++++++++ b.putByte(record.version.major);
++++++++ b.putByte(record.version.minor);
++++++++ b.putInt16(record.length);
++++++++ b.putBytes(record.fragment.bytes());
++++++++ hmac.update(b.getBytes());
++++++++ return hmac.digest().getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * Compresses the TLSPlaintext record into a TLSCompressed record using the
++++++++ * deflate algorithm.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param record the TLSPlaintext record to compress.
++++++++ * @param s the ConnectionState to use.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++var deflate = function(c, record, s) {
++++++++ var rval = false;
++++++++
++++++++ try {
++++++++ var bytes = c.deflate(record.fragment.getBytes());
++++++++ record.fragment = forge.util.createBuffer(bytes);
++++++++ record.length = bytes.length;
++++++++ rval = true;
++++++++ } catch(ex) {
++++++++ // deflate error, fail out
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Decompresses the TLSCompressed record into a TLSPlaintext record using the
++++++++ * deflate algorithm.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param record the TLSCompressed record to decompress.
++++++++ * @param s the ConnectionState to use.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++var inflate = function(c, record, s) {
++++++++ var rval = false;
++++++++
++++++++ try {
++++++++ var bytes = c.inflate(record.fragment.getBytes());
++++++++ record.fragment = forge.util.createBuffer(bytes);
++++++++ record.length = bytes.length;
++++++++ rval = true;
++++++++ } catch(ex) {
++++++++ // inflate error, fail out
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Reads a TLS variable-length vector from a byte buffer.
++++++++ *
++++++++ * Variable-length vectors are defined by specifying a subrange of legal
++++++++ * lengths, inclusively, using the notation <floor..ceiling>. When these are
++++++++ * encoded, the actual length precedes the vector's contents in the byte
++++++++ * stream. The length will be in the form of a number consuming as many bytes
++++++++ * as required to hold the vector's specified maximum (ceiling) length. A
++++++++ * variable-length vector with an actual length field of zero is referred to
++++++++ * as an empty vector.
++++++++ *
++++++++ * @param b the byte buffer.
++++++++ * @param lenBytes the number of bytes required to store the length.
++++++++ *
++++++++ * @return the resulting byte buffer.
++++++++ */
++++++++var readVector = function(b, lenBytes) {
++++++++ var len = 0;
++++++++ switch(lenBytes) {
++++++++ case 1:
++++++++ len = b.getByte();
++++++++ break;
++++++++ case 2:
++++++++ len = b.getInt16();
++++++++ break;
++++++++ case 3:
++++++++ len = b.getInt24();
++++++++ break;
++++++++ case 4:
++++++++ len = b.getInt32();
++++++++ break;
++++++++ }
++++++++
++++++++ // read vector bytes into a new buffer
++++++++ return forge.util.createBuffer(b.getBytes(len));
++++++++};
++++++++
++++++++/**
++++++++ * Writes a TLS variable-length vector to a byte buffer.
++++++++ *
++++++++ * @param b the byte buffer.
++++++++ * @param lenBytes the number of bytes required to store the length.
++++++++ * @param v the byte buffer vector.
++++++++ */
++++++++var writeVector = function(b, lenBytes, v) {
++++++++ // encode length at the start of the vector, where the number of bytes for
++++++++ // the length is the maximum number of bytes it would take to encode the
++++++++ // vector's ceiling
++++++++ b.putInt(v.length(), lenBytes << 3);
++++++++ b.putBuffer(v);
++++++++};
++++++++
++++++++/**
++++++++ * The tls implementation.
++++++++ */
++++++++var tls = {};
++++++++
++++++++/**
++++++++ * Version: TLS 1.2 = 3.3, TLS 1.1 = 3.2, TLS 1.0 = 3.1. Both TLS 1.1 and
++++++++ * TLS 1.2 were still too new (ie: openSSL didn't implement them) at the time
++++++++ * of this implementation so TLS 1.0 was implemented instead.
++++++++ */
++++++++tls.Versions = {
++++++++ TLS_1_0: {major: 3, minor: 1},
++++++++ TLS_1_1: {major: 3, minor: 2},
++++++++ TLS_1_2: {major: 3, minor: 3}
++++++++};
++++++++tls.SupportedVersions = [
++++++++ tls.Versions.TLS_1_1,
++++++++ tls.Versions.TLS_1_0
++++++++];
++++++++tls.Version = tls.SupportedVersions[0];
++++++++
++++++++/**
++++++++ * Maximum fragment size. True maximum is 16384, but we fragment before that
++++++++ * to allow for unusual small increases during compression.
++++++++ */
++++++++tls.MaxFragment = 16384 - 1024;
++++++++
++++++++/**
++++++++ * Whether this entity is considered the "client" or "server".
++++++++ * enum { server, client } ConnectionEnd;
++++++++ */
++++++++tls.ConnectionEnd = {
++++++++ server: 0,
++++++++ client: 1
++++++++};
++++++++
++++++++/**
++++++++ * Pseudo-random function algorithm used to generate keys from the master
++++++++ * secret.
++++++++ * enum { tls_prf_sha256 } PRFAlgorithm;
++++++++ */
++++++++tls.PRFAlgorithm = {
++++++++ tls_prf_sha256: 0
++++++++};
++++++++
++++++++/**
++++++++ * Bulk encryption algorithms.
++++++++ * enum { null, rc4, des3, aes } BulkCipherAlgorithm;
++++++++ */
++++++++tls.BulkCipherAlgorithm = {
++++++++ none: null,
++++++++ rc4: 0,
++++++++ des3: 1,
++++++++ aes: 2
++++++++};
++++++++
++++++++/**
++++++++ * Cipher types.
++++++++ * enum { stream, block, aead } CipherType;
++++++++ */
++++++++tls.CipherType = {
++++++++ stream: 0,
++++++++ block: 1,
++++++++ aead: 2
++++++++};
++++++++
++++++++/**
++++++++ * MAC (Message Authentication Code) algorithms.
++++++++ * enum { null, hmac_md5, hmac_sha1, hmac_sha256,
++++++++ * hmac_sha384, hmac_sha512} MACAlgorithm;
++++++++ */
++++++++tls.MACAlgorithm = {
++++++++ none: null,
++++++++ hmac_md5: 0,
++++++++ hmac_sha1: 1,
++++++++ hmac_sha256: 2,
++++++++ hmac_sha384: 3,
++++++++ hmac_sha512: 4
++++++++};
++++++++
++++++++/**
++++++++ * Compression algorithms.
++++++++ * enum { null(0), deflate(1), (255) } CompressionMethod;
++++++++ */
++++++++tls.CompressionMethod = {
++++++++ none: 0,
++++++++ deflate: 1
++++++++};
++++++++
++++++++/**
++++++++ * TLS record content types.
++++++++ * enum {
++++++++ * change_cipher_spec(20), alert(21), handshake(22),
++++++++ * application_data(23), (255)
++++++++ * } ContentType;
++++++++ */
++++++++tls.ContentType = {
++++++++ change_cipher_spec: 20,
++++++++ alert: 21,
++++++++ handshake: 22,
++++++++ application_data: 23,
++++++++ heartbeat: 24
++++++++};
++++++++
++++++++/**
++++++++ * TLS handshake types.
++++++++ * enum {
++++++++ * hello_request(0), client_hello(1), server_hello(2),
++++++++ * certificate(11), server_key_exchange (12),
++++++++ * certificate_request(13), server_hello_done(14),
++++++++ * certificate_verify(15), client_key_exchange(16),
++++++++ * finished(20), (255)
++++++++ * } HandshakeType;
++++++++ */
++++++++tls.HandshakeType = {
++++++++ hello_request: 0,
++++++++ client_hello: 1,
++++++++ server_hello: 2,
++++++++ certificate: 11,
++++++++ server_key_exchange: 12,
++++++++ certificate_request: 13,
++++++++ server_hello_done: 14,
++++++++ certificate_verify: 15,
++++++++ client_key_exchange: 16,
++++++++ finished: 20
++++++++};
++++++++
++++++++/**
++++++++ * TLS Alert Protocol.
++++++++ *
++++++++ * enum { warning(1), fatal(2), (255) } AlertLevel;
++++++++ *
++++++++ * enum {
++++++++ * close_notify(0),
++++++++ * unexpected_message(10),
++++++++ * bad_record_mac(20),
++++++++ * decryption_failed(21),
++++++++ * record_overflow(22),
++++++++ * decompression_failure(30),
++++++++ * handshake_failure(40),
++++++++ * bad_certificate(42),
++++++++ * unsupported_certificate(43),
++++++++ * certificate_revoked(44),
++++++++ * certificate_expired(45),
++++++++ * certificate_unknown(46),
++++++++ * illegal_parameter(47),
++++++++ * unknown_ca(48),
++++++++ * access_denied(49),
++++++++ * decode_error(50),
++++++++ * decrypt_error(51),
++++++++ * export_restriction(60),
++++++++ * protocol_version(70),
++++++++ * insufficient_security(71),
++++++++ * internal_error(80),
++++++++ * user_canceled(90),
++++++++ * no_renegotiation(100),
++++++++ * (255)
++++++++ * } AlertDescription;
++++++++ *
++++++++ * struct {
++++++++ * AlertLevel level;
++++++++ * AlertDescription description;
++++++++ * } Alert;
++++++++ */
++++++++tls.Alert = {};
++++++++tls.Alert.Level = {
++++++++ warning: 1,
++++++++ fatal: 2
++++++++};
++++++++tls.Alert.Description = {
++++++++ close_notify: 0,
++++++++ unexpected_message: 10,
++++++++ bad_record_mac: 20,
++++++++ decryption_failed: 21,
++++++++ record_overflow: 22,
++++++++ decompression_failure: 30,
++++++++ handshake_failure: 40,
++++++++ bad_certificate: 42,
++++++++ unsupported_certificate: 43,
++++++++ certificate_revoked: 44,
++++++++ certificate_expired: 45,
++++++++ certificate_unknown: 46,
++++++++ illegal_parameter: 47,
++++++++ unknown_ca: 48,
++++++++ access_denied: 49,
++++++++ decode_error: 50,
++++++++ decrypt_error: 51,
++++++++ export_restriction: 60,
++++++++ protocol_version: 70,
++++++++ insufficient_security: 71,
++++++++ internal_error: 80,
++++++++ user_canceled: 90,
++++++++ no_renegotiation: 100
++++++++};
++++++++
++++++++/**
++++++++ * TLS Heartbeat Message types.
++++++++ * enum {
++++++++ * heartbeat_request(1),
++++++++ * heartbeat_response(2),
++++++++ * (255)
++++++++ * } HeartbeatMessageType;
++++++++ */
++++++++tls.HeartbeatMessageType = {
++++++++ heartbeat_request: 1,
++++++++ heartbeat_response: 2
++++++++};
++++++++
++++++++/**
++++++++ * Supported cipher suites.
++++++++ */
++++++++tls.CipherSuites = {};
++++++++
++++++++/**
++++++++ * Gets a supported cipher suite from its 2 byte ID.
++++++++ *
++++++++ * @param twoBytes two bytes in a string.
++++++++ *
++++++++ * @return the matching supported cipher suite or null.
++++++++ */
++++++++tls.getCipherSuite = function(twoBytes) {
++++++++ var rval = null;
++++++++ for(var key in tls.CipherSuites) {
++++++++ var cs = tls.CipherSuites[key];
++++++++ if(cs.id[0] === twoBytes.charCodeAt(0) &&
++++++++ cs.id[1] === twoBytes.charCodeAt(1)) {
++++++++ rval = cs;
++++++++ break;
++++++++ }
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Called when an unexpected record is encountered.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleUnexpected = function(c, record) {
++++++++ // if connection is client and closed, ignore unexpected messages
++++++++ var ignore = (!c.open && c.entity === tls.ConnectionEnd.client);
++++++++ if(!ignore) {
++++++++ c.error(c, {
++++++++ message: 'Unexpected message. Received TLS record out of order.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.unexpected_message
++++++++ }
++++++++ });
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a HelloRequest record.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleHelloRequest = function(c, record, length) {
++++++++ // ignore renegotiation requests from the server during a handshake, but
++++++++ // if handshaking, send a warning alert that renegotation is denied
++++++++ if(!c.handshaking && c.handshakes > 0) {
++++++++ // send alert warning
++++++++ tls.queue(c, tls.createAlert(c, {
++++++++ level: tls.Alert.Level.warning,
++++++++ description: tls.Alert.Description.no_renegotiation
++++++++ }));
++++++++ tls.flush(c);
++++++++ }
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Parses a hello message from a ClientHello or ServerHello record.
++++++++ *
++++++++ * @param record the record to parse.
++++++++ *
++++++++ * @return the parsed message.
++++++++ */
++++++++tls.parseHelloMessage = function(c, record, length) {
++++++++ var msg = null;
++++++++
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++
++++++++ // minimum of 38 bytes in message
++++++++ if(length < 38) {
++++++++ c.error(c, {
++++++++ message: client ?
++++++++ 'Invalid ServerHello message. Message too short.' :
++++++++ 'Invalid ClientHello message. Message too short.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ } else {
++++++++ // use 'remaining' to calculate # of remaining bytes in the message
++++++++ var b = record.fragment;
++++++++ var remaining = b.length();
++++++++ msg = {
++++++++ version: {
++++++++ major: b.getByte(),
++++++++ minor: b.getByte()
++++++++ },
++++++++ random: forge.util.createBuffer(b.getBytes(32)),
++++++++ session_id: readVector(b, 1),
++++++++ extensions: []
++++++++ };
++++++++ if(client) {
++++++++ msg.cipher_suite = b.getBytes(2);
++++++++ msg.compression_method = b.getByte();
++++++++ } else {
++++++++ msg.cipher_suites = readVector(b, 2);
++++++++ msg.compression_methods = readVector(b, 1);
++++++++ }
++++++++
++++++++ // read extensions if there are any bytes left in the message
++++++++ remaining = length - (remaining - b.length());
++++++++ if(remaining > 0) {
++++++++ // parse extensions
++++++++ var exts = readVector(b, 2);
++++++++ while(exts.length() > 0) {
++++++++ msg.extensions.push({
++++++++ type: [exts.getByte(), exts.getByte()],
++++++++ data: readVector(exts, 2)
++++++++ });
++++++++ }
++++++++
++++++++ // TODO: make extension support modular
++++++++ if(!client) {
++++++++ for(var i = 0; i < msg.extensions.length; ++i) {
++++++++ var ext = msg.extensions[i];
++++++++
++++++++ // support SNI extension
++++++++ if(ext.type[0] === 0x00 && ext.type[1] === 0x00) {
++++++++ // get server name list
++++++++ var snl = readVector(ext.data, 2);
++++++++ while(snl.length() > 0) {
++++++++ // read server name type
++++++++ var snType = snl.getByte();
++++++++
++++++++ // only HostName type (0x00) is known, break out if
++++++++ // another type is detected
++++++++ if(snType !== 0x00) {
++++++++ break;
++++++++ }
++++++++
++++++++ // add host name to server name list
++++++++ c.session.extensions.server_name.serverNameList.push(
++++++++ readVector(snl, 2).getBytes());
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // version already set, do not allow version change
++++++++ if(c.session.version) {
++++++++ if(msg.version.major !== c.session.version.major ||
++++++++ msg.version.minor !== c.session.version.minor) {
++++++++ return c.error(c, {
++++++++ message: 'TLS version change is disallowed during renegotiation.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.protocol_version
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++
++++++++ // get the chosen (ServerHello) cipher suite
++++++++ if(client) {
++++++++ // FIXME: should be checking configured acceptable cipher suites
++++++++ c.session.cipherSuite = tls.getCipherSuite(msg.cipher_suite);
++++++++ } else {
++++++++ // get a supported preferred (ClientHello) cipher suite
++++++++ // choose the first supported cipher suite
++++++++ var tmp = forge.util.createBuffer(msg.cipher_suites.bytes());
++++++++ while(tmp.length() > 0) {
++++++++ // FIXME: should be checking configured acceptable suites
++++++++ // cipher suites take up 2 bytes
++++++++ c.session.cipherSuite = tls.getCipherSuite(tmp.getBytes(2));
++++++++ if(c.session.cipherSuite !== null) {
++++++++ break;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // cipher suite not supported
++++++++ if(c.session.cipherSuite === null) {
++++++++ return c.error(c, {
++++++++ message: 'No cipher suites in common.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.handshake_failure
++++++++ },
++++++++ cipherSuite: forge.util.bytesToHex(msg.cipher_suite)
++++++++ });
++++++++ }
++++++++
++++++++ // TODO: handle compression methods
++++++++ if(client) {
++++++++ c.session.compressionMethod = msg.compression_method;
++++++++ } else {
++++++++ // no compression
++++++++ c.session.compressionMethod = tls.CompressionMethod.none;
++++++++ }
++++++++ }
++++++++
++++++++ return msg;
++++++++};
++++++++
++++++++/**
++++++++ * Creates security parameters for the given connection based on the given
++++++++ * hello message.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param msg the hello message.
++++++++ */
++++++++tls.createSecurityParameters = function(c, msg) {
++++++++ /* Note: security params are from TLS 1.2, some values like prf_algorithm
++++++++ are ignored for TLS 1.0/1.1 and the builtin as specified in the spec is
++++++++ used. */
++++++++
++++++++ // TODO: handle other options from server when more supported
++++++++
++++++++ // get client and server randoms
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ var msgRandom = msg.random.bytes();
++++++++ var cRandom = client ? c.session.sp.client_random : msgRandom;
++++++++ var sRandom = client ? msgRandom : tls.createRandom().getBytes();
++++++++
++++++++ // create new security parameters
++++++++ c.session.sp = {
++++++++ entity: c.entity,
++++++++ prf_algorithm: tls.PRFAlgorithm.tls_prf_sha256,
++++++++ bulk_cipher_algorithm: null,
++++++++ cipher_type: null,
++++++++ enc_key_length: null,
++++++++ block_length: null,
++++++++ fixed_iv_length: null,
++++++++ record_iv_length: null,
++++++++ mac_algorithm: null,
++++++++ mac_length: null,
++++++++ mac_key_length: null,
++++++++ compression_algorithm: c.session.compressionMethod,
++++++++ pre_master_secret: null,
++++++++ master_secret: null,
++++++++ client_random: cRandom,
++++++++ server_random: sRandom
++++++++ };
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a ServerHello record.
++++++++ *
++++++++ * When a ServerHello message will be sent:
++++++++ * The server will send this message in response to a client hello message
++++++++ * when it was able to find an acceptable set of algorithms. If it cannot
++++++++ * find such a match, it will respond with a handshake failure alert.
++++++++ *
++++++++ * uint24 length;
++++++++ * struct {
++++++++ * ProtocolVersion server_version;
++++++++ * Random random;
++++++++ * SessionID session_id;
++++++++ * CipherSuite cipher_suite;
++++++++ * CompressionMethod compression_method;
++++++++ * select(extensions_present) {
++++++++ * case false:
++++++++ * struct {};
++++++++ * case true:
++++++++ * Extension extensions<0..2^16-1>;
++++++++ * };
++++++++ * } ServerHello;
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleServerHello = function(c, record, length) {
++++++++ var msg = tls.parseHelloMessage(c, record, length);
++++++++ if(c.fail) {
++++++++ return;
++++++++ }
++++++++
++++++++ // ensure server version is compatible
++++++++ if(msg.version.minor <= c.version.minor) {
++++++++ c.version.minor = msg.version.minor;
++++++++ } else {
++++++++ return c.error(c, {
++++++++ message: 'Incompatible TLS version.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.protocol_version
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // indicate session version has been set
++++++++ c.session.version = c.version;
++++++++
++++++++ // get the session ID from the message
++++++++ var sessionId = msg.session_id.bytes();
++++++++
++++++++ // if the session ID is not blank and matches the cached one, resume
++++++++ // the session
++++++++ if(sessionId.length > 0 && sessionId === c.session.id) {
++++++++ // resuming session, expect a ChangeCipherSpec next
++++++++ c.expect = SCC;
++++++++ c.session.resuming = true;
++++++++
++++++++ // get new server random
++++++++ c.session.sp.server_random = msg.random.bytes();
++++++++ } else {
++++++++ // not resuming, expect a server Certificate message next
++++++++ c.expect = SCE;
++++++++ c.session.resuming = false;
++++++++
++++++++ // create new security parameters
++++++++ tls.createSecurityParameters(c, msg);
++++++++ }
++++++++
++++++++ // set new session ID
++++++++ c.session.id = sessionId;
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a server receives a ClientHello record.
++++++++ *
++++++++ * When a ClientHello message will be sent:
++++++++ * When a client first connects to a server it is required to send the
++++++++ * client hello as its first message. The client can also send a client
++++++++ * hello in response to a hello request or on its own initiative in order
++++++++ * to renegotiate the security parameters in an existing connection.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleClientHello = function(c, record, length) {
++++++++ var msg = tls.parseHelloMessage(c, record, length);
++++++++ if(c.fail) {
++++++++ return;
++++++++ }
++++++++
++++++++ // get the session ID from the message
++++++++ var sessionId = msg.session_id.bytes();
++++++++
++++++++ // see if the given session ID is in the cache
++++++++ var session = null;
++++++++ if(c.sessionCache) {
++++++++ session = c.sessionCache.getSession(sessionId);
++++++++ if(session === null) {
++++++++ // session ID not found
++++++++ sessionId = '';
++++++++ } else if(session.version.major !== msg.version.major ||
++++++++ session.version.minor > msg.version.minor) {
++++++++ // if session version is incompatible with client version, do not resume
++++++++ session = null;
++++++++ sessionId = '';
++++++++ }
++++++++ }
++++++++
++++++++ // no session found to resume, generate a new session ID
++++++++ if(sessionId.length === 0) {
++++++++ sessionId = forge.random.getBytes(32);
++++++++ }
++++++++
++++++++ // update session
++++++++ c.session.id = sessionId;
++++++++ c.session.clientHelloVersion = msg.version;
++++++++ c.session.sp = {};
++++++++ if(session) {
++++++++ // use version and security parameters from resumed session
++++++++ c.version = c.session.version = session.version;
++++++++ c.session.sp = session.sp;
++++++++ } else {
++++++++ // use highest compatible minor version
++++++++ var version;
++++++++ for(var i = 1; i < tls.SupportedVersions.length; ++i) {
++++++++ version = tls.SupportedVersions[i];
++++++++ if(version.minor <= msg.version.minor) {
++++++++ break;
++++++++ }
++++++++ }
++++++++ c.version = {major: version.major, minor: version.minor};
++++++++ c.session.version = c.version;
++++++++ }
++++++++
++++++++ // if a session is set, resume it
++++++++ if(session !== null) {
++++++++ // resuming session, expect a ChangeCipherSpec next
++++++++ c.expect = CCC;
++++++++ c.session.resuming = true;
++++++++
++++++++ // get new client random
++++++++ c.session.sp.client_random = msg.random.bytes();
++++++++ } else {
++++++++ // not resuming, expect a Certificate or ClientKeyExchange
++++++++ c.expect = (c.verifyClient !== false) ? CCE : CKE;
++++++++ c.session.resuming = false;
++++++++
++++++++ // create new security parameters
++++++++ tls.createSecurityParameters(c, msg);
++++++++ }
++++++++
++++++++ // connection now open
++++++++ c.open = true;
++++++++
++++++++ // queue server hello
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createServerHello(c)
++++++++ }));
++++++++
++++++++ if(c.session.resuming) {
++++++++ // queue change cipher spec message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.change_cipher_spec,
++++++++ data: tls.createChangeCipherSpec()
++++++++ }));
++++++++
++++++++ // create pending state
++++++++ c.state.pending = tls.createConnectionState(c);
++++++++
++++++++ // change current write state to pending write state
++++++++ c.state.current.write = c.state.pending.write;
++++++++
++++++++ // queue finished
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createFinished(c)
++++++++ }));
++++++++ } else {
++++++++ // queue server certificate
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createCertificate(c)
++++++++ }));
++++++++
++++++++ if(!c.fail) {
++++++++ // queue server key exchange
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createServerKeyExchange(c)
++++++++ }));
++++++++
++++++++ // request client certificate if set
++++++++ if(c.verifyClient !== false) {
++++++++ // queue certificate request
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createCertificateRequest(c)
++++++++ }));
++++++++ }
++++++++
++++++++ // queue server hello done
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createServerHelloDone(c)
++++++++ }));
++++++++ }
++++++++ }
++++++++
++++++++ // send records
++++++++ tls.flush(c);
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a Certificate record.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * The server must send a certificate whenever the agreed-upon key exchange
++++++++ * method is not an anonymous one. This message will always immediately
++++++++ * follow the server hello message.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * The certificate type must be appropriate for the selected cipher suite's
++++++++ * key exchange algorithm, and is generally an X.509v3 certificate. It must
++++++++ * contain a key which matches the key exchange method, as follows. Unless
++++++++ * otherwise specified, the signing algorithm for the certificate must be
++++++++ * the same as the algorithm for the certificate key. Unless otherwise
++++++++ * specified, the public key may be of any length.
++++++++ *
++++++++ * opaque ASN.1Cert<1..2^24-1>;
++++++++ * struct {
++++++++ * ASN.1Cert certificate_list<1..2^24-1>;
++++++++ * } Certificate;
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleCertificate = function(c, record, length) {
++++++++ // minimum of 3 bytes in message
++++++++ if(length < 3) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid Certificate message. Message too short.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ var b = record.fragment;
++++++++ var msg = {
++++++++ certificate_list: readVector(b, 3)
++++++++ };
++++++++
++++++++ /* The sender's certificate will be first in the list (chain), each
++++++++ subsequent one that follows will certify the previous one, but root
++++++++ certificates (self-signed) that specify the certificate authority may
++++++++ be omitted under the assumption that clients must already possess it. */
++++++++ var cert, asn1;
++++++++ var certs = [];
++++++++ try {
++++++++ while(msg.certificate_list.length() > 0) {
++++++++ // each entry in msg.certificate_list is a vector with 3 len bytes
++++++++ cert = readVector(msg.certificate_list, 3);
++++++++ asn1 = forge.asn1.fromDer(cert);
++++++++ cert = forge.pki.certificateFromAsn1(asn1, true);
++++++++ certs.push(cert);
++++++++ }
++++++++ } catch(ex) {
++++++++ return c.error(c, {
++++++++ message: 'Could not parse certificate list.',
++++++++ cause: ex,
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.bad_certificate
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // ensure at least 1 certificate was provided if in client-mode
++++++++ // or if verifyClient was set to true to require a certificate
++++++++ // (as opposed to 'optional')
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ if((client || c.verifyClient === true) && certs.length === 0) {
++++++++ // error, no certificate
++++++++ c.error(c, {
++++++++ message: client ?
++++++++ 'No server certificate provided.' :
++++++++ 'No client certificate provided.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ } else if(certs.length === 0) {
++++++++ // no certs to verify
++++++++ // expect a ServerKeyExchange or ClientKeyExchange message next
++++++++ c.expect = client ? SKE : CKE;
++++++++ } else {
++++++++ // save certificate in session
++++++++ if(client) {
++++++++ c.session.serverCertificate = certs[0];
++++++++ } else {
++++++++ c.session.clientCertificate = certs[0];
++++++++ }
++++++++
++++++++ if(tls.verifyCertificateChain(c, certs)) {
++++++++ // expect a ServerKeyExchange or ClientKeyExchange message next
++++++++ c.expect = client ? SKE : CKE;
++++++++ }
++++++++ }
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a ServerKeyExchange record.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * This message will be sent immediately after the server certificate
++++++++ * message (or the server hello message, if this is an anonymous
++++++++ * negotiation).
++++++++ *
++++++++ * The server key exchange message is sent by the server only when the
++++++++ * server certificate message (if sent) does not contain enough data to
++++++++ * allow the client to exchange a premaster secret.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * This message conveys cryptographic information to allow the client to
++++++++ * communicate the premaster secret: either an RSA public key to encrypt
++++++++ * the premaster secret with, or a Diffie-Hellman public key with which the
++++++++ * client can complete a key exchange (with the result being the premaster
++++++++ * secret.)
++++++++ *
++++++++ * enum {
++++++++ * dhe_dss, dhe_rsa, dh_anon, rsa, dh_dss, dh_rsa
++++++++ * } KeyExchangeAlgorithm;
++++++++ *
++++++++ * struct {
++++++++ * opaque dh_p<1..2^16-1>;
++++++++ * opaque dh_g<1..2^16-1>;
++++++++ * opaque dh_Ys<1..2^16-1>;
++++++++ * } ServerDHParams;
++++++++ *
++++++++ * struct {
++++++++ * select(KeyExchangeAlgorithm) {
++++++++ * case dh_anon:
++++++++ * ServerDHParams params;
++++++++ * case dhe_dss:
++++++++ * case dhe_rsa:
++++++++ * ServerDHParams params;
++++++++ * digitally-signed struct {
++++++++ * opaque client_random[32];
++++++++ * opaque server_random[32];
++++++++ * ServerDHParams params;
++++++++ * } signed_params;
++++++++ * case rsa:
++++++++ * case dh_dss:
++++++++ * case dh_rsa:
++++++++ * struct {};
++++++++ * };
++++++++ * } ServerKeyExchange;
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleServerKeyExchange = function(c, record, length) {
++++++++ // this implementation only supports RSA, no Diffie-Hellman support
++++++++ // so any length > 0 is invalid
++++++++ if(length > 0) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid key parameters. Only RSA is supported.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.unsupported_certificate
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // expect an optional CertificateRequest message next
++++++++ c.expect = SCR;
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a ClientKeyExchange record.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleClientKeyExchange = function(c, record, length) {
++++++++ // this implementation only supports RSA, no Diffie-Hellman support
++++++++ // so any length < 48 is invalid
++++++++ if(length < 48) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid key parameters. Only RSA is supported.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.unsupported_certificate
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ var b = record.fragment;
++++++++ var msg = {
++++++++ enc_pre_master_secret: readVector(b, 2).getBytes()
++++++++ };
++++++++
++++++++ // do rsa decryption
++++++++ var privateKey = null;
++++++++ if(c.getPrivateKey) {
++++++++ try {
++++++++ privateKey = c.getPrivateKey(c, c.session.serverCertificate);
++++++++ privateKey = forge.pki.privateKeyFromPem(privateKey);
++++++++ } catch(ex) {
++++++++ c.error(c, {
++++++++ message: 'Could not get private key.',
++++++++ cause: ex,
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++
++++++++ if(privateKey === null) {
++++++++ return c.error(c, {
++++++++ message: 'No private key set.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ try {
++++++++ // decrypt 48-byte pre-master secret
++++++++ var sp = c.session.sp;
++++++++ sp.pre_master_secret = privateKey.decrypt(msg.enc_pre_master_secret);
++++++++
++++++++ // ensure client hello version matches first 2 bytes
++++++++ var version = c.session.clientHelloVersion;
++++++++ if(version.major !== sp.pre_master_secret.charCodeAt(0) ||
++++++++ version.minor !== sp.pre_master_secret.charCodeAt(1)) {
++++++++ // error, do not send alert (see BLEI attack below)
++++++++ throw new Error('TLS version rollback attack detected.');
++++++++ }
++++++++ } catch(ex) {
++++++++ /* Note: Daniel Bleichenbacher [BLEI] can be used to attack a
++++++++ TLS server which is using PKCS#1 encoded RSA, so instead of
++++++++ failing here, we generate 48 random bytes and use that as
++++++++ the pre-master secret. */
++++++++ sp.pre_master_secret = forge.random.getBytes(48);
++++++++ }
++++++++
++++++++ // expect a CertificateVerify message if a Certificate was received that
++++++++ // does not have fixed Diffie-Hellman params, otherwise expect
++++++++ // ChangeCipherSpec
++++++++ c.expect = CCC;
++++++++ if(c.session.clientCertificate !== null) {
++++++++ // only RSA support, so expect CertificateVerify
++++++++ // TODO: support Diffie-Hellman
++++++++ c.expect = CCV;
++++++++ }
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a CertificateRequest record.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * A non-anonymous server can optionally request a certificate from the
++++++++ * client, if appropriate for the selected cipher suite. This message, if
++++++++ * sent, will immediately follow the Server Key Exchange message (if it is
++++++++ * sent; otherwise, the Server Certificate message).
++++++++ *
++++++++ * enum {
++++++++ * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
++++++++ * rsa_ephemeral_dh_RESERVED(5), dss_ephemeral_dh_RESERVED(6),
++++++++ * fortezza_dms_RESERVED(20), (255)
++++++++ * } ClientCertificateType;
++++++++ *
++++++++ * opaque DistinguishedName<1..2^16-1>;
++++++++ *
++++++++ * struct {
++++++++ * ClientCertificateType certificate_types<1..2^8-1>;
++++++++ * SignatureAndHashAlgorithm supported_signature_algorithms<2^16-1>;
++++++++ * DistinguishedName certificate_authorities<0..2^16-1>;
++++++++ * } CertificateRequest;
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleCertificateRequest = function(c, record, length) {
++++++++ // minimum of 3 bytes in message
++++++++ if(length < 3) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid CertificateRequest. Message too short.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // TODO: TLS 1.2+ has different format including
++++++++ // SignatureAndHashAlgorithm after cert types
++++++++ var b = record.fragment;
++++++++ var msg = {
++++++++ certificate_types: readVector(b, 1),
++++++++ certificate_authorities: readVector(b, 2)
++++++++ };
++++++++
++++++++ // save certificate request in session
++++++++ c.session.certificateRequest = msg;
++++++++
++++++++ // expect a ServerHelloDone message next
++++++++ c.expect = SHD;
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a server receives a CertificateVerify record.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleCertificateVerify = function(c, record, length) {
++++++++ if(length < 2) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid CertificateVerify. Message too short.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // rewind to get full bytes for message so it can be manually
++++++++ // digested below (special case for CertificateVerify messages because
++++++++ // they must be digested *after* handling as opposed to all others)
++++++++ var b = record.fragment;
++++++++ b.read -= 4;
++++++++ var msgBytes = b.bytes();
++++++++ b.read += 4;
++++++++
++++++++ var msg = {
++++++++ signature: readVector(b, 2).getBytes()
++++++++ };
++++++++
++++++++ // TODO: add support for DSA
++++++++
++++++++ // generate data to verify
++++++++ var verify = forge.util.createBuffer();
++++++++ verify.putBuffer(c.session.md5.digest());
++++++++ verify.putBuffer(c.session.sha1.digest());
++++++++ verify = verify.getBytes();
++++++++
++++++++ try {
++++++++ var cert = c.session.clientCertificate;
++++++++ /*b = forge.pki.rsa.decrypt(
++++++++ msg.signature, cert.publicKey, true, verify.length);
++++++++ if(b !== verify) {*/
++++++++ if(!cert.publicKey.verify(verify, msg.signature, 'NONE')) {
++++++++ throw new Error('CertificateVerify signature does not match.');
++++++++ }
++++++++
++++++++ // digest message now that it has been handled
++++++++ c.session.md5.update(msgBytes);
++++++++ c.session.sha1.update(msgBytes);
++++++++ } catch(ex) {
++++++++ return c.error(c, {
++++++++ message: 'Bad signature in CertificateVerify.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.handshake_failure
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // expect ChangeCipherSpec
++++++++ c.expect = CCC;
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a client receives a ServerHelloDone record.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * The server hello done message is sent by the server to indicate the end
++++++++ * of the server hello and associated messages. After sending this message
++++++++ * the server will wait for a client response.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * This message means that the server is done sending messages to support
++++++++ * the key exchange, and the client can proceed with its phase of the key
++++++++ * exchange.
++++++++ *
++++++++ * Upon receipt of the server hello done message the client should verify
++++++++ * that the server provided a valid certificate if required and check that
++++++++ * the server hello parameters are acceptable.
++++++++ *
++++++++ * struct {} ServerHelloDone;
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleServerHelloDone = function(c, record, length) {
++++++++ // len must be 0 bytes
++++++++ if(length > 0) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid ServerHelloDone message. Invalid length.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.record_overflow
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ if(c.serverCertificate === null) {
++++++++ // no server certificate was provided
++++++++ var error = {
++++++++ message: 'No server certificate provided. Not enough security.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.insufficient_security
++++++++ }
++++++++ };
++++++++
++++++++ // call application callback
++++++++ var depth = 0;
++++++++ var ret = c.verify(c, error.alert.description, depth, []);
++++++++ if(ret !== true) {
++++++++ // check for custom alert info
++++++++ if(ret || ret === 0) {
++++++++ // set custom message and alert description
++++++++ if(typeof ret === 'object' && !forge.util.isArray(ret)) {
++++++++ if(ret.message) {
++++++++ error.message = ret.message;
++++++++ }
++++++++ if(ret.alert) {
++++++++ error.alert.description = ret.alert;
++++++++ }
++++++++ } else if(typeof ret === 'number') {
++++++++ // set custom alert description
++++++++ error.alert.description = ret;
++++++++ }
++++++++ }
++++++++
++++++++ // send error
++++++++ return c.error(c, error);
++++++++ }
++++++++ }
++++++++
++++++++ // create client certificate message if requested
++++++++ if(c.session.certificateRequest !== null) {
++++++++ record = tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createCertificate(c)
++++++++ });
++++++++ tls.queue(c, record);
++++++++ }
++++++++
++++++++ // create client key exchange message
++++++++ record = tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createClientKeyExchange(c)
++++++++ });
++++++++ tls.queue(c, record);
++++++++
++++++++ // expect no messages until the following callback has been called
++++++++ c.expect = SER;
++++++++
++++++++ // create callback to handle client signature (for client-certs)
++++++++ var callback = function(c, signature) {
++++++++ if(c.session.certificateRequest !== null &&
++++++++ c.session.clientCertificate !== null) {
++++++++ // create certificate verify message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createCertificateVerify(c, signature)
++++++++ }));
++++++++ }
++++++++
++++++++ // create change cipher spec message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.change_cipher_spec,
++++++++ data: tls.createChangeCipherSpec()
++++++++ }));
++++++++
++++++++ // create pending state
++++++++ c.state.pending = tls.createConnectionState(c);
++++++++
++++++++ // change current write state to pending write state
++++++++ c.state.current.write = c.state.pending.write;
++++++++
++++++++ // create finished message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createFinished(c)
++++++++ }));
++++++++
++++++++ // expect a server ChangeCipherSpec message next
++++++++ c.expect = SCC;
++++++++
++++++++ // send records
++++++++ tls.flush(c);
++++++++
++++++++ // continue
++++++++ c.process();
++++++++ };
++++++++
++++++++ // if there is no certificate request or no client certificate, do
++++++++ // callback immediately
++++++++ if(c.session.certificateRequest === null ||
++++++++ c.session.clientCertificate === null) {
++++++++ return callback(c, null);
++++++++ }
++++++++
++++++++ // otherwise get the client signature
++++++++ tls.getClientSignature(c, callback);
++++++++};
++++++++
++++++++/**
++++++++ * Called when a ChangeCipherSpec record is received.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleChangeCipherSpec = function(c, record) {
++++++++ if(record.fragment.getByte() !== 0x01) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid ChangeCipherSpec message received.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.illegal_parameter
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // create pending state if:
++++++++ // 1. Resuming session in client mode OR
++++++++ // 2. NOT resuming session in server mode
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ if((c.session.resuming && client) || (!c.session.resuming && !client)) {
++++++++ c.state.pending = tls.createConnectionState(c);
++++++++ }
++++++++
++++++++ // change current read state to pending read state
++++++++ c.state.current.read = c.state.pending.read;
++++++++
++++++++ // clear pending state if:
++++++++ // 1. NOT resuming session in client mode OR
++++++++ // 2. resuming a session in server mode
++++++++ if((!c.session.resuming && client) || (c.session.resuming && !client)) {
++++++++ c.state.pending = null;
++++++++ }
++++++++
++++++++ // expect a Finished record next
++++++++ c.expect = client ? SFI : CFI;
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a Finished record is received.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * A finished message is always sent immediately after a change
++++++++ * cipher spec message to verify that the key exchange and
++++++++ * authentication processes were successful. It is essential that a
++++++++ * change cipher spec message be received between the other
++++++++ * handshake messages and the Finished message.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * The finished message is the first protected with the just-
++++++++ * negotiated algorithms, keys, and secrets. Recipients of finished
++++++++ * messages must verify that the contents are correct. Once a side
++++++++ * has sent its Finished message and received and validated the
++++++++ * Finished message from its peer, it may begin to send and receive
++++++++ * application data over the connection.
++++++++ *
++++++++ * struct {
++++++++ * opaque verify_data[verify_data_length];
++++++++ * } Finished;
++++++++ *
++++++++ * verify_data
++++++++ * PRF(master_secret, finished_label, Hash(handshake_messages))
++++++++ * [0..verify_data_length-1];
++++++++ *
++++++++ * finished_label
++++++++ * For Finished messages sent by the client, the string
++++++++ * "client finished". For Finished messages sent by the server, the
++++++++ * string "server finished".
++++++++ *
++++++++ * verify_data_length depends on the cipher suite. If it is not specified
++++++++ * by the cipher suite, then it is 12. Versions of TLS < 1.2 always used
++++++++ * 12 bytes.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ * @param length the length of the handshake message.
++++++++ */
++++++++tls.handleFinished = function(c, record, length) {
++++++++ // rewind to get full bytes for message so it can be manually
++++++++ // digested below (special case for Finished messages because they
++++++++ // must be digested *after* handling as opposed to all others)
++++++++ var b = record.fragment;
++++++++ b.read -= 4;
++++++++ var msgBytes = b.bytes();
++++++++ b.read += 4;
++++++++
++++++++ // message contains only verify_data
++++++++ var vd = record.fragment.getBytes();
++++++++
++++++++ // ensure verify data is correct
++++++++ b = forge.util.createBuffer();
++++++++ b.putBuffer(c.session.md5.digest());
++++++++ b.putBuffer(c.session.sha1.digest());
++++++++
++++++++ // set label based on entity type
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ var label = client ? 'server finished' : 'client finished';
++++++++
++++++++ // TODO: determine prf function and verify length for TLS 1.2
++++++++ var sp = c.session.sp;
++++++++ var vdl = 12;
++++++++ var prf = prf_TLS1;
++++++++ b = prf(sp.master_secret, label, b.getBytes(), vdl);
++++++++ if(b.getBytes() !== vd) {
++++++++ return c.error(c, {
++++++++ message: 'Invalid verify_data in Finished message.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.decrypt_error
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ // digest finished message now that it has been handled
++++++++ c.session.md5.update(msgBytes);
++++++++ c.session.sha1.update(msgBytes);
++++++++
++++++++ // resuming session as client or NOT resuming session as server
++++++++ if((c.session.resuming && client) || (!c.session.resuming && !client)) {
++++++++ // create change cipher spec message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.change_cipher_spec,
++++++++ data: tls.createChangeCipherSpec()
++++++++ }));
++++++++
++++++++ // change current write state to pending write state, clear pending
++++++++ c.state.current.write = c.state.pending.write;
++++++++ c.state.pending = null;
++++++++
++++++++ // create finished message
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createFinished(c)
++++++++ }));
++++++++ }
++++++++
++++++++ // expect application data next
++++++++ c.expect = client ? SAD : CAD;
++++++++
++++++++ // handshake complete
++++++++ c.handshaking = false;
++++++++ ++c.handshakes;
++++++++
++++++++ // save access to peer certificate
++++++++ c.peerCertificate = client ?
++++++++ c.session.serverCertificate : c.session.clientCertificate;
++++++++
++++++++ // send records
++++++++ tls.flush(c);
++++++++
++++++++ // now connected
++++++++ c.isConnected = true;
++++++++ c.connected(c);
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when an Alert record is received.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleAlert = function(c, record) {
++++++++ // read alert
++++++++ var b = record.fragment;
++++++++ var alert = {
++++++++ level: b.getByte(),
++++++++ description: b.getByte()
++++++++ };
++++++++
++++++++ // TODO: consider using a table?
++++++++ // get appropriate message
++++++++ var msg;
++++++++ switch(alert.description) {
++++++++ case tls.Alert.Description.close_notify:
++++++++ msg = 'Connection closed.';
++++++++ break;
++++++++ case tls.Alert.Description.unexpected_message:
++++++++ msg = 'Unexpected message.';
++++++++ break;
++++++++ case tls.Alert.Description.bad_record_mac:
++++++++ msg = 'Bad record MAC.';
++++++++ break;
++++++++ case tls.Alert.Description.decryption_failed:
++++++++ msg = 'Decryption failed.';
++++++++ break;
++++++++ case tls.Alert.Description.record_overflow:
++++++++ msg = 'Record overflow.';
++++++++ break;
++++++++ case tls.Alert.Description.decompression_failure:
++++++++ msg = 'Decompression failed.';
++++++++ break;
++++++++ case tls.Alert.Description.handshake_failure:
++++++++ msg = 'Handshake failure.';
++++++++ break;
++++++++ case tls.Alert.Description.bad_certificate:
++++++++ msg = 'Bad certificate.';
++++++++ break;
++++++++ case tls.Alert.Description.unsupported_certificate:
++++++++ msg = 'Unsupported certificate.';
++++++++ break;
++++++++ case tls.Alert.Description.certificate_revoked:
++++++++ msg = 'Certificate revoked.';
++++++++ break;
++++++++ case tls.Alert.Description.certificate_expired:
++++++++ msg = 'Certificate expired.';
++++++++ break;
++++++++ case tls.Alert.Description.certificate_unknown:
++++++++ msg = 'Certificate unknown.';
++++++++ break;
++++++++ case tls.Alert.Description.illegal_parameter:
++++++++ msg = 'Illegal parameter.';
++++++++ break;
++++++++ case tls.Alert.Description.unknown_ca:
++++++++ msg = 'Unknown certificate authority.';
++++++++ break;
++++++++ case tls.Alert.Description.access_denied:
++++++++ msg = 'Access denied.';
++++++++ break;
++++++++ case tls.Alert.Description.decode_error:
++++++++ msg = 'Decode error.';
++++++++ break;
++++++++ case tls.Alert.Description.decrypt_error:
++++++++ msg = 'Decrypt error.';
++++++++ break;
++++++++ case tls.Alert.Description.export_restriction:
++++++++ msg = 'Export restriction.';
++++++++ break;
++++++++ case tls.Alert.Description.protocol_version:
++++++++ msg = 'Unsupported protocol version.';
++++++++ break;
++++++++ case tls.Alert.Description.insufficient_security:
++++++++ msg = 'Insufficient security.';
++++++++ break;
++++++++ case tls.Alert.Description.internal_error:
++++++++ msg = 'Internal error.';
++++++++ break;
++++++++ case tls.Alert.Description.user_canceled:
++++++++ msg = 'User canceled.';
++++++++ break;
++++++++ case tls.Alert.Description.no_renegotiation:
++++++++ msg = 'Renegotiation not supported.';
++++++++ break;
++++++++ default:
++++++++ msg = 'Unknown error.';
++++++++ break;
++++++++ }
++++++++
++++++++ // close connection on close_notify, not an error
++++++++ if(alert.description === tls.Alert.Description.close_notify) {
++++++++ return c.close();
++++++++ }
++++++++
++++++++ // call error handler
++++++++ c.error(c, {
++++++++ message: msg,
++++++++ send: false,
++++++++ // origin is the opposite end
++++++++ origin: (c.entity === tls.ConnectionEnd.client) ? 'server' : 'client',
++++++++ alert: alert
++++++++ });
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a Handshake record is received.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleHandshake = function(c, record) {
++++++++ // get the handshake type and message length
++++++++ var b = record.fragment;
++++++++ var type = b.getByte();
++++++++ var length = b.getInt24();
++++++++
++++++++ // see if the record fragment doesn't yet contain the full message
++++++++ if(length > b.length()) {
++++++++ // cache the record, clear its fragment, and reset the buffer read
++++++++ // pointer before the type and length were read
++++++++ c.fragmented = record;
++++++++ record.fragment = forge.util.createBuffer();
++++++++ b.read -= 4;
++++++++
++++++++ // continue
++++++++ return c.process();
++++++++ }
++++++++
++++++++ // full message now available, clear cache, reset read pointer to
++++++++ // before type and length
++++++++ c.fragmented = null;
++++++++ b.read -= 4;
++++++++
++++++++ // save the handshake bytes for digestion after handler is found
++++++++ // (include type and length of handshake msg)
++++++++ var bytes = b.bytes(length + 4);
++++++++
++++++++ // restore read pointer
++++++++ b.read += 4;
++++++++
++++++++ // handle expected message
++++++++ if(type in hsTable[c.entity][c.expect]) {
++++++++ // initialize server session
++++++++ if(c.entity === tls.ConnectionEnd.server && !c.open && !c.fail) {
++++++++ c.handshaking = true;
++++++++ c.session = {
++++++++ version: null,
++++++++ extensions: {
++++++++ server_name: {
++++++++ serverNameList: []
++++++++ }
++++++++ },
++++++++ cipherSuite: null,
++++++++ compressionMethod: null,
++++++++ serverCertificate: null,
++++++++ clientCertificate: null,
++++++++ md5: forge.md.md5.create(),
++++++++ sha1: forge.md.sha1.create()
++++++++ };
++++++++ }
++++++++
++++++++ /* Update handshake messages digest. Finished and CertificateVerify
++++++++ messages are not digested here. They can't be digested as part of
++++++++ the verify_data that they contain. These messages are manually
++++++++ digested in their handlers. HelloRequest messages are simply never
++++++++ included in the handshake message digest according to spec. */
++++++++ if(type !== tls.HandshakeType.hello_request &&
++++++++ type !== tls.HandshakeType.certificate_verify &&
++++++++ type !== tls.HandshakeType.finished) {
++++++++ c.session.md5.update(bytes);
++++++++ c.session.sha1.update(bytes);
++++++++ }
++++++++
++++++++ // handle specific handshake type record
++++++++ hsTable[c.entity][c.expect][type](c, record, length);
++++++++ } else {
++++++++ // unexpected record
++++++++ tls.handleUnexpected(c, record);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Called when an ApplicationData record is received.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleApplicationData = function(c, record) {
++++++++ // buffer data, notify that its ready
++++++++ c.data.putBuffer(record.fragment);
++++++++ c.dataReady(c);
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * Called when a Heartbeat record is received.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record.
++++++++ */
++++++++tls.handleHeartbeat = function(c, record) {
++++++++ // get the heartbeat type and payload
++++++++ var b = record.fragment;
++++++++ var type = b.getByte();
++++++++ var length = b.getInt16();
++++++++ var payload = b.getBytes(length);
++++++++
++++++++ if(type === tls.HeartbeatMessageType.heartbeat_request) {
++++++++ // discard request during handshake or if length is too large
++++++++ if(c.handshaking || length > payload.length) {
++++++++ // continue
++++++++ return c.process();
++++++++ }
++++++++ // retransmit payload
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.heartbeat,
++++++++ data: tls.createHeartbeat(
++++++++ tls.HeartbeatMessageType.heartbeat_response, payload)
++++++++ }));
++++++++ tls.flush(c);
++++++++ } else if(type === tls.HeartbeatMessageType.heartbeat_response) {
++++++++ // check payload against expected payload, discard heartbeat if no match
++++++++ if(payload !== c.expectedHeartbeatPayload) {
++++++++ // continue
++++++++ return c.process();
++++++++ }
++++++++
++++++++ // notify that a valid heartbeat was received
++++++++ if(c.heartbeatReceived) {
++++++++ c.heartbeatReceived(c, forge.util.createBuffer(payload));
++++++++ }
++++++++ }
++++++++
++++++++ // continue
++++++++ c.process();
++++++++};
++++++++
++++++++/**
++++++++ * The transistional state tables for receiving TLS records. It maps the
++++++++ * current TLS engine state and a received record to a function to handle the
++++++++ * record and update the state.
++++++++ *
++++++++ * For instance, if the current state is SHE, then the TLS engine is expecting
++++++++ * a ServerHello record. Once a record is received, the handler function is
++++++++ * looked up using the state SHE and the record's content type.
++++++++ *
++++++++ * The resulting function will either be an error handler or a record handler.
++++++++ * The function will take whatever action is appropriate and update the state
++++++++ * for the next record.
++++++++ *
++++++++ * The states are all based on possible server record types. Note that the
++++++++ * client will never specifically expect to receive a HelloRequest or an alert
++++++++ * from the server so there is no state that reflects this. These messages may
++++++++ * occur at any time.
++++++++ *
++++++++ * There are two tables for mapping states because there is a second tier of
++++++++ * types for handshake messages. Once a record with a content type of handshake
++++++++ * is received, the handshake record handler will look up the handshake type in
++++++++ * the secondary map to get its appropriate handler.
++++++++ *
++++++++ * Valid message orders are as follows:
++++++++ *
++++++++ * =======================FULL HANDSHAKE======================
++++++++ * Client Server
++++++++ *
++++++++ * ClientHello -------->
++++++++ * ServerHello
++++++++ * Certificate*
++++++++ * ServerKeyExchange*
++++++++ * CertificateRequest*
++++++++ * <-------- ServerHelloDone
++++++++ * Certificate*
++++++++ * ClientKeyExchange
++++++++ * CertificateVerify*
++++++++ * [ChangeCipherSpec]
++++++++ * Finished -------->
++++++++ * [ChangeCipherSpec]
++++++++ * <-------- Finished
++++++++ * Application Data <-------> Application Data
++++++++ *
++++++++ * =====================SESSION RESUMPTION=====================
++++++++ * Client Server
++++++++ *
++++++++ * ClientHello -------->
++++++++ * ServerHello
++++++++ * [ChangeCipherSpec]
++++++++ * <-------- Finished
++++++++ * [ChangeCipherSpec]
++++++++ * Finished -------->
++++++++ * Application Data <-------> Application Data
++++++++ */
++++++++// client expect states (indicate which records are expected to be received)
++++++++var SHE = 0; // rcv server hello
++++++++var SCE = 1; // rcv server certificate
++++++++var SKE = 2; // rcv server key exchange
++++++++var SCR = 3; // rcv certificate request
++++++++var SHD = 4; // rcv server hello done
++++++++var SCC = 5; // rcv change cipher spec
++++++++var SFI = 6; // rcv finished
++++++++var SAD = 7; // rcv application data
++++++++var SER = 8; // not expecting any messages at this point
++++++++
++++++++// server expect states
++++++++var CHE = 0; // rcv client hello
++++++++var CCE = 1; // rcv client certificate
++++++++var CKE = 2; // rcv client key exchange
++++++++var CCV = 3; // rcv certificate verify
++++++++var CCC = 4; // rcv change cipher spec
++++++++var CFI = 5; // rcv finished
++++++++var CAD = 6; // rcv application data
++++++++var CER = 7; // not expecting any messages at this point
++++++++
++++++++// map client current expect state and content type to function
++++++++var __ = tls.handleUnexpected;
++++++++var R0 = tls.handleChangeCipherSpec;
++++++++var R1 = tls.handleAlert;
++++++++var R2 = tls.handleHandshake;
++++++++var R3 = tls.handleApplicationData;
++++++++var R4 = tls.handleHeartbeat;
++++++++var ctTable = [];
++++++++ctTable[tls.ConnectionEnd.client] = [
++++++++// CC,AL,HS,AD,HB
++++++++/*SHE*/[__,R1,R2,__,R4],
++++++++/*SCE*/[__,R1,R2,__,R4],
++++++++/*SKE*/[__,R1,R2,__,R4],
++++++++/*SCR*/[__,R1,R2,__,R4],
++++++++/*SHD*/[__,R1,R2,__,R4],
++++++++/*SCC*/[R0,R1,__,__,R4],
++++++++/*SFI*/[__,R1,R2,__,R4],
++++++++/*SAD*/[__,R1,R2,R3,R4],
++++++++/*SER*/[__,R1,R2,__,R4]
++++++++];
++++++++
++++++++// map server current expect state and content type to function
++++++++ctTable[tls.ConnectionEnd.server] = [
++++++++// CC,AL,HS,AD
++++++++/*CHE*/[__,R1,R2,__,R4],
++++++++/*CCE*/[__,R1,R2,__,R4],
++++++++/*CKE*/[__,R1,R2,__,R4],
++++++++/*CCV*/[__,R1,R2,__,R4],
++++++++/*CCC*/[R0,R1,__,__,R4],
++++++++/*CFI*/[__,R1,R2,__,R4],
++++++++/*CAD*/[__,R1,R2,R3,R4],
++++++++/*CER*/[__,R1,R2,__,R4]
++++++++];
++++++++
++++++++// map client current expect state and handshake type to function
++++++++var H0 = tls.handleHelloRequest;
++++++++var H1 = tls.handleServerHello;
++++++++var H2 = tls.handleCertificate;
++++++++var H3 = tls.handleServerKeyExchange;
++++++++var H4 = tls.handleCertificateRequest;
++++++++var H5 = tls.handleServerHelloDone;
++++++++var H6 = tls.handleFinished;
++++++++var hsTable = [];
++++++++hsTable[tls.ConnectionEnd.client] = [
++++++++// HR,01,SH,03,04,05,06,07,08,09,10,SC,SK,CR,HD,15,CK,17,18,19,FI
++++++++/*SHE*/[__,__,H1,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*SCE*/[H0,__,__,__,__,__,__,__,__,__,__,H2,H3,H4,H5,__,__,__,__,__,__],
++++++++/*SKE*/[H0,__,__,__,__,__,__,__,__,__,__,__,H3,H4,H5,__,__,__,__,__,__],
++++++++/*SCR*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,H4,H5,__,__,__,__,__,__],
++++++++/*SHD*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,__,H5,__,__,__,__,__,__],
++++++++/*SCC*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*SFI*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,H6],
++++++++/*SAD*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*SER*/[H0,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__]
++++++++];
++++++++
++++++++// map server current expect state and handshake type to function
++++++++// Note: CAD[CH] does not map to FB because renegotation is prohibited
++++++++var H7 = tls.handleClientHello;
++++++++var H8 = tls.handleClientKeyExchange;
++++++++var H9 = tls.handleCertificateVerify;
++++++++hsTable[tls.ConnectionEnd.server] = [
++++++++// 01,CH,02,03,04,05,06,07,08,09,10,CC,12,13,14,CV,CK,17,18,19,FI
++++++++/*CHE*/[__,H7,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*CCE*/[__,__,__,__,__,__,__,__,__,__,__,H2,__,__,__,__,__,__,__,__,__],
++++++++/*CKE*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,H8,__,__,__,__],
++++++++/*CCV*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,H9,__,__,__,__,__],
++++++++/*CCC*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*CFI*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,H6],
++++++++/*CAD*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__],
++++++++/*CER*/[__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__]
++++++++];
++++++++
++++++++/**
++++++++ * Generates the master_secret and keys using the given security parameters.
++++++++ *
++++++++ * The security parameters for a TLS connection state are defined as such:
++++++++ *
++++++++ * struct {
++++++++ * ConnectionEnd entity;
++++++++ * PRFAlgorithm prf_algorithm;
++++++++ * BulkCipherAlgorithm bulk_cipher_algorithm;
++++++++ * CipherType cipher_type;
++++++++ * uint8 enc_key_length;
++++++++ * uint8 block_length;
++++++++ * uint8 fixed_iv_length;
++++++++ * uint8 record_iv_length;
++++++++ * MACAlgorithm mac_algorithm;
++++++++ * uint8 mac_length;
++++++++ * uint8 mac_key_length;
++++++++ * CompressionMethod compression_algorithm;
++++++++ * opaque master_secret[48];
++++++++ * opaque client_random[32];
++++++++ * opaque server_random[32];
++++++++ * } SecurityParameters;
++++++++ *
++++++++ * Note that this definition is from TLS 1.2. In TLS 1.0 some of these
++++++++ * parameters are ignored because, for instance, the PRFAlgorithm is a
++++++++ * builtin-fixed algorithm combining iterations of MD5 and SHA-1 in TLS 1.0.
++++++++ *
++++++++ * The Record Protocol requires an algorithm to generate keys required by the
++++++++ * current connection state.
++++++++ *
++++++++ * The master secret is expanded into a sequence of secure bytes, which is then
++++++++ * split to a client write MAC key, a server write MAC key, a client write
++++++++ * encryption key, and a server write encryption key. In TLS 1.0 a client write
++++++++ * IV and server write IV are also generated. Each of these is generated from
++++++++ * the byte sequence in that order. Unused values are empty. In TLS 1.2, some
++++++++ * AEAD ciphers may additionally require a client write IV and a server write
++++++++ * IV (see Section 6.2.3.3).
++++++++ *
++++++++ * When keys, MAC keys, and IVs are generated, the master secret is used as an
++++++++ * entropy source.
++++++++ *
++++++++ * To generate the key material, compute:
++++++++ *
++++++++ * master_secret = PRF(pre_master_secret, "master secret",
++++++++ * ClientHello.random + ServerHello.random)
++++++++ *
++++++++ * key_block = PRF(SecurityParameters.master_secret,
++++++++ * "key expansion",
++++++++ * SecurityParameters.server_random +
++++++++ * SecurityParameters.client_random);
++++++++ *
++++++++ * until enough output has been generated. Then, the key_block is
++++++++ * partitioned as follows:
++++++++ *
++++++++ * client_write_MAC_key[SecurityParameters.mac_key_length]
++++++++ * server_write_MAC_key[SecurityParameters.mac_key_length]
++++++++ * client_write_key[SecurityParameters.enc_key_length]
++++++++ * server_write_key[SecurityParameters.enc_key_length]
++++++++ * client_write_IV[SecurityParameters.fixed_iv_length]
++++++++ * server_write_IV[SecurityParameters.fixed_iv_length]
++++++++ *
++++++++ * In TLS 1.2, the client_write_IV and server_write_IV are only generated for
++++++++ * implicit nonce techniques as described in Section 3.2.1 of [AEAD]. This
++++++++ * implementation uses TLS 1.0 so IVs are generated.
++++++++ *
++++++++ * Implementation note: The currently defined cipher suite which requires the
++++++++ * most material is AES_256_CBC_SHA256. It requires 2 x 32 byte keys and 2 x 32
++++++++ * byte MAC keys, for a total 128 bytes of key material. In TLS 1.0 it also
++++++++ * requires 2 x 16 byte IVs, so it actually takes 160 bytes of key material.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param sp the security parameters to use.
++++++++ *
++++++++ * @return the security keys.
++++++++ */
++++++++tls.generateKeys = function(c, sp) {
++++++++ // TLS_RSA_WITH_AES_128_CBC_SHA (required to be compliant with TLS 1.2) &
++++++++ // TLS_RSA_WITH_AES_256_CBC_SHA are the only cipher suites implemented
++++++++ // at present
++++++++
++++++++ // TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA is required to be compliant with
++++++++ // TLS 1.0 but we don't care right now because AES is better and we have
++++++++ // an implementation for it
++++++++
++++++++ // TODO: TLS 1.2 implementation
++++++++ /*
++++++++ // determine the PRF
++++++++ var prf;
++++++++ switch(sp.prf_algorithm) {
++++++++ case tls.PRFAlgorithm.tls_prf_sha256:
++++++++ prf = prf_sha256;
++++++++ break;
++++++++ default:
++++++++ // should never happen
++++++++ throw new Error('Invalid PRF');
++++++++ }
++++++++ */
++++++++
++++++++ // TLS 1.0/1.1 implementation
++++++++ var prf = prf_TLS1;
++++++++
++++++++ // concatenate server and client random
++++++++ var random = sp.client_random + sp.server_random;
++++++++
++++++++ // only create master secret if session is new
++++++++ if(!c.session.resuming) {
++++++++ // create master secret, clean up pre-master secret
++++++++ sp.master_secret = prf(
++++++++ sp.pre_master_secret, 'master secret', random, 48).bytes();
++++++++ sp.pre_master_secret = null;
++++++++ }
++++++++
++++++++ // generate the amount of key material needed
++++++++ random = sp.server_random + sp.client_random;
++++++++ var length = 2 * sp.mac_key_length + 2 * sp.enc_key_length;
++++++++
++++++++ // include IV for TLS/1.0
++++++++ var tls10 = (c.version.major === tls.Versions.TLS_1_0.major &&
++++++++ c.version.minor === tls.Versions.TLS_1_0.minor);
++++++++ if(tls10) {
++++++++ length += 2 * sp.fixed_iv_length;
++++++++ }
++++++++ var km = prf(sp.master_secret, 'key expansion', random, length);
++++++++
++++++++ // split the key material into the MAC and encryption keys
++++++++ var rval = {
++++++++ client_write_MAC_key: km.getBytes(sp.mac_key_length),
++++++++ server_write_MAC_key: km.getBytes(sp.mac_key_length),
++++++++ client_write_key: km.getBytes(sp.enc_key_length),
++++++++ server_write_key: km.getBytes(sp.enc_key_length)
++++++++ };
++++++++
++++++++ // include TLS 1.0 IVs
++++++++ if(tls10) {
++++++++ rval.client_write_IV = km.getBytes(sp.fixed_iv_length);
++++++++ rval.server_write_IV = km.getBytes(sp.fixed_iv_length);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new initialized TLS connection state. A connection state has
++++++++ * a read mode and a write mode.
++++++++ *
++++++++ * compression state:
++++++++ * The current state of the compression algorithm.
++++++++ *
++++++++ * cipher state:
++++++++ * The current state of the encryption algorithm. This will consist of the
++++++++ * scheduled key for that connection. For stream ciphers, this will also
++++++++ * contain whatever state information is necessary to allow the stream to
++++++++ * continue to encrypt or decrypt data.
++++++++ *
++++++++ * MAC key:
++++++++ * The MAC key for the connection.
++++++++ *
++++++++ * sequence number:
++++++++ * Each connection state contains a sequence number, which is maintained
++++++++ * separately for read and write states. The sequence number MUST be set to
++++++++ * zero whenever a connection state is made the active state. Sequence
++++++++ * numbers are of type uint64 and may not exceed 2^64-1. Sequence numbers do
++++++++ * not wrap. If a TLS implementation would need to wrap a sequence number,
++++++++ * it must renegotiate instead. A sequence number is incremented after each
++++++++ * record: specifically, the first record transmitted under a particular
++++++++ * connection state MUST use sequence number 0.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the new initialized TLS connection state.
++++++++ */
++++++++tls.createConnectionState = function(c) {
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++
++++++++ var createMode = function() {
++++++++ var mode = {
++++++++ // two 32-bit numbers, first is most significant
++++++++ sequenceNumber: [0, 0],
++++++++ macKey: null,
++++++++ macLength: 0,
++++++++ macFunction: null,
++++++++ cipherState: null,
++++++++ cipherFunction: function(record) {return true;},
++++++++ compressionState: null,
++++++++ compressFunction: function(record) {return true;},
++++++++ updateSequenceNumber: function() {
++++++++ if(mode.sequenceNumber[1] === 0xFFFFFFFF) {
++++++++ mode.sequenceNumber[1] = 0;
++++++++ ++mode.sequenceNumber[0];
++++++++ } else {
++++++++ ++mode.sequenceNumber[1];
++++++++ }
++++++++ }
++++++++ };
++++++++ return mode;
++++++++ };
++++++++ var state = {
++++++++ read: createMode(),
++++++++ write: createMode()
++++++++ };
++++++++
++++++++ // update function in read mode will decrypt then decompress a record
++++++++ state.read.update = function(c, record) {
++++++++ if(!state.read.cipherFunction(record, state.read)) {
++++++++ c.error(c, {
++++++++ message: 'Could not decrypt record or bad MAC.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ // doesn't matter if decryption failed or MAC was
++++++++ // invalid, return the same error so as not to reveal
++++++++ // which one occurred
++++++++ description: tls.Alert.Description.bad_record_mac
++++++++ }
++++++++ });
++++++++ } else if(!state.read.compressFunction(c, record, state.read)) {
++++++++ c.error(c, {
++++++++ message: 'Could not decompress record.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.decompression_failure
++++++++ }
++++++++ });
++++++++ }
++++++++ return !c.fail;
++++++++ };
++++++++
++++++++ // update function in write mode will compress then encrypt a record
++++++++ state.write.update = function(c, record) {
++++++++ if(!state.write.compressFunction(c, record, state.write)) {
++++++++ // error, but do not send alert since it would require
++++++++ // compression as well
++++++++ c.error(c, {
++++++++ message: 'Could not compress record.',
++++++++ send: false,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ } else if(!state.write.cipherFunction(record, state.write)) {
++++++++ // error, but do not send alert since it would require
++++++++ // encryption as well
++++++++ c.error(c, {
++++++++ message: 'Could not encrypt record.',
++++++++ send: false,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ }
++++++++ return !c.fail;
++++++++ };
++++++++
++++++++ // handle security parameters
++++++++ if(c.session) {
++++++++ var sp = c.session.sp;
++++++++ c.session.cipherSuite.initSecurityParameters(sp);
++++++++
++++++++ // generate keys
++++++++ sp.keys = tls.generateKeys(c, sp);
++++++++ state.read.macKey = client ?
++++++++ sp.keys.server_write_MAC_key : sp.keys.client_write_MAC_key;
++++++++ state.write.macKey = client ?
++++++++ sp.keys.client_write_MAC_key : sp.keys.server_write_MAC_key;
++++++++
++++++++ // cipher suite setup
++++++++ c.session.cipherSuite.initConnectionState(state, c, sp);
++++++++
++++++++ // compression setup
++++++++ switch(sp.compression_algorithm) {
++++++++ case tls.CompressionMethod.none:
++++++++ break;
++++++++ case tls.CompressionMethod.deflate:
++++++++ state.read.compressFunction = inflate;
++++++++ state.write.compressFunction = deflate;
++++++++ break;
++++++++ default:
++++++++ throw new Error('Unsupported compression algorithm.');
++++++++ }
++++++++ }
++++++++
++++++++ return state;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a Random structure.
++++++++ *
++++++++ * struct {
++++++++ * uint32 gmt_unix_time;
++++++++ * opaque random_bytes[28];
++++++++ * } Random;
++++++++ *
++++++++ * gmt_unix_time:
++++++++ * The current time and date in standard UNIX 32-bit format (seconds since
++++++++ * the midnight starting Jan 1, 1970, UTC, ignoring leap seconds) according
++++++++ * to the sender's internal clock. Clocks are not required to be set
++++++++ * correctly by the basic TLS protocol; higher-level or application
++++++++ * protocols may define additional requirements. Note that, for historical
++++++++ * reasons, the data element is named using GMT, the predecessor of the
++++++++ * current worldwide time base, UTC.
++++++++ * random_bytes:
++++++++ * 28 bytes generated by a secure random number generator.
++++++++ *
++++++++ * @return the Random structure as a byte array.
++++++++ */
++++++++tls.createRandom = function() {
++++++++ // get UTC milliseconds
++++++++ var d = new Date();
++++++++ var utc = +d + d.getTimezoneOffset() * 60000;
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putInt32(utc);
++++++++ rval.putBytes(forge.random.getBytes(28));
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a TLS record with the given type and data.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param options:
++++++++ * type: the record type.
++++++++ * data: the plain text data in a byte buffer.
++++++++ *
++++++++ * @return the created record.
++++++++ */
++++++++tls.createRecord = function(c, options) {
++++++++ if(!options.data) {
++++++++ return null;
++++++++ }
++++++++ var record = {
++++++++ type: options.type,
++++++++ version: {
++++++++ major: c.version.major,
++++++++ minor: c.version.minor
++++++++ },
++++++++ length: options.data.length(),
++++++++ fragment: options.data
++++++++ };
++++++++ return record;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a TLS alert record.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param alert:
++++++++ * level: the TLS alert level.
++++++++ * description: the TLS alert description.
++++++++ *
++++++++ * @return the created alert record.
++++++++ */
++++++++tls.createAlert = function(c, alert) {
++++++++ var b = forge.util.createBuffer();
++++++++ b.putByte(alert.level);
++++++++ b.putByte(alert.description);
++++++++ return tls.createRecord(c, {
++++++++ type: tls.ContentType.alert,
++++++++ data: b
++++++++ });
++++++++};
++++++++
++++++++/* The structure of a TLS handshake message.
++++++++ *
++++++++ * struct {
++++++++ * HandshakeType msg_type; // handshake type
++++++++ * uint24 length; // bytes in message
++++++++ * select(HandshakeType) {
++++++++ * case hello_request: HelloRequest;
++++++++ * case client_hello: ClientHello;
++++++++ * case server_hello: ServerHello;
++++++++ * case certificate: Certificate;
++++++++ * case server_key_exchange: ServerKeyExchange;
++++++++ * case certificate_request: CertificateRequest;
++++++++ * case server_hello_done: ServerHelloDone;
++++++++ * case certificate_verify: CertificateVerify;
++++++++ * case client_key_exchange: ClientKeyExchange;
++++++++ * case finished: Finished;
++++++++ * } body;
++++++++ * } Handshake;
++++++++ */
++++++++
++++++++/**
++++++++ * Creates a ClientHello message.
++++++++ *
++++++++ * opaque SessionID<0..32>;
++++++++ * enum { null(0), deflate(1), (255) } CompressionMethod;
++++++++ * uint8 CipherSuite[2];
++++++++ *
++++++++ * struct {
++++++++ * ProtocolVersion client_version;
++++++++ * Random random;
++++++++ * SessionID session_id;
++++++++ * CipherSuite cipher_suites<2..2^16-2>;
++++++++ * CompressionMethod compression_methods<1..2^8-1>;
++++++++ * select(extensions_present) {
++++++++ * case false:
++++++++ * struct {};
++++++++ * case true:
++++++++ * Extension extensions<0..2^16-1>;
++++++++ * };
++++++++ * } ClientHello;
++++++++ *
++++++++ * The extension format for extended client hellos and server hellos is:
++++++++ *
++++++++ * struct {
++++++++ * ExtensionType extension_type;
++++++++ * opaque extension_data<0..2^16-1>;
++++++++ * } Extension;
++++++++ *
++++++++ * Here:
++++++++ *
++++++++ * - "extension_type" identifies the particular extension type.
++++++++ * - "extension_data" contains information specific to the particular
++++++++ * extension type.
++++++++ *
++++++++ * The extension types defined in this document are:
++++++++ *
++++++++ * enum {
++++++++ * server_name(0), max_fragment_length(1),
++++++++ * client_certificate_url(2), trusted_ca_keys(3),
++++++++ * truncated_hmac(4), status_request(5), (65535)
++++++++ * } ExtensionType;
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the ClientHello byte buffer.
++++++++ */
++++++++tls.createClientHello = function(c) {
++++++++ // save hello version
++++++++ c.session.clientHelloVersion = {
++++++++ major: c.version.major,
++++++++ minor: c.version.minor
++++++++ };
++++++++
++++++++ // create supported cipher suites
++++++++ var cipherSuites = forge.util.createBuffer();
++++++++ for(var i = 0; i < c.cipherSuites.length; ++i) {
++++++++ var cs = c.cipherSuites[i];
++++++++ cipherSuites.putByte(cs.id[0]);
++++++++ cipherSuites.putByte(cs.id[1]);
++++++++ }
++++++++ var cSuites = cipherSuites.length();
++++++++
++++++++ // create supported compression methods, null always supported, but
++++++++ // also support deflate if connection has inflate and deflate methods
++++++++ var compressionMethods = forge.util.createBuffer();
++++++++ compressionMethods.putByte(tls.CompressionMethod.none);
++++++++ // FIXME: deflate support disabled until issues with raw deflate data
++++++++ // without zlib headers are resolved
++++++++ /*
++++++++ if(c.inflate !== null && c.deflate !== null) {
++++++++ compressionMethods.putByte(tls.CompressionMethod.deflate);
++++++++ }
++++++++ */
++++++++ var cMethods = compressionMethods.length();
++++++++
++++++++ // create TLS SNI (server name indication) extension if virtual host
++++++++ // has been specified, see RFC 3546
++++++++ var extensions = forge.util.createBuffer();
++++++++ if(c.virtualHost) {
++++++++ // create extension struct
++++++++ var ext = forge.util.createBuffer();
++++++++ ext.putByte(0x00); // type server_name (ExtensionType is 2 bytes)
++++++++ ext.putByte(0x00);
++++++++
++++++++ /* In order to provide the server name, clients MAY include an
++++++++ * extension of type "server_name" in the (extended) client hello.
++++++++ * The "extension_data" field of this extension SHALL contain
++++++++ * "ServerNameList" where:
++++++++ *
++++++++ * struct {
++++++++ * NameType name_type;
++++++++ * select(name_type) {
++++++++ * case host_name: HostName;
++++++++ * } name;
++++++++ * } ServerName;
++++++++ *
++++++++ * enum {
++++++++ * host_name(0), (255)
++++++++ * } NameType;
++++++++ *
++++++++ * opaque HostName<1..2^16-1>;
++++++++ *
++++++++ * struct {
++++++++ * ServerName server_name_list<1..2^16-1>
++++++++ * } ServerNameList;
++++++++ */
++++++++ var serverName = forge.util.createBuffer();
++++++++ serverName.putByte(0x00); // type host_name
++++++++ writeVector(serverName, 2, forge.util.createBuffer(c.virtualHost));
++++++++
++++++++ // ServerNameList is in extension_data
++++++++ var snList = forge.util.createBuffer();
++++++++ writeVector(snList, 2, serverName);
++++++++ writeVector(ext, 2, snList);
++++++++ extensions.putBuffer(ext);
++++++++ }
++++++++ var extLength = extensions.length();
++++++++ if(extLength > 0) {
++++++++ // add extension vector length
++++++++ extLength += 2;
++++++++ }
++++++++
++++++++ // determine length of the handshake message
++++++++ // cipher suites and compression methods size will need to be
++++++++ // updated if more get added to the list
++++++++ var sessionId = c.session.id;
++++++++ var length =
++++++++ sessionId.length + 1 + // session ID vector
++++++++ 2 + // version (major + minor)
++++++++ 4 + 28 + // random time and random bytes
++++++++ 2 + cSuites + // cipher suites vector
++++++++ 1 + cMethods + // compression methods vector
++++++++ extLength; // extensions vector
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.client_hello);
++++++++ rval.putInt24(length); // handshake length
++++++++ rval.putByte(c.version.major); // major version
++++++++ rval.putByte(c.version.minor); // minor version
++++++++ rval.putBytes(c.session.sp.client_random); // random time + bytes
++++++++ writeVector(rval, 1, forge.util.createBuffer(sessionId));
++++++++ writeVector(rval, 2, cipherSuites);
++++++++ writeVector(rval, 1, compressionMethods);
++++++++ if(extLength > 0) {
++++++++ writeVector(rval, 2, extensions);
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a ServerHello message.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the ServerHello byte buffer.
++++++++ */
++++++++tls.createServerHello = function(c) {
++++++++ // determine length of the handshake message
++++++++ var sessionId = c.session.id;
++++++++ var length =
++++++++ sessionId.length + 1 + // session ID vector
++++++++ 2 + // version (major + minor)
++++++++ 4 + 28 + // random time and random bytes
++++++++ 2 + // chosen cipher suite
++++++++ 1; // chosen compression method
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.server_hello);
++++++++ rval.putInt24(length); // handshake length
++++++++ rval.putByte(c.version.major); // major version
++++++++ rval.putByte(c.version.minor); // minor version
++++++++ rval.putBytes(c.session.sp.server_random); // random time + bytes
++++++++ writeVector(rval, 1, forge.util.createBuffer(sessionId));
++++++++ rval.putByte(c.session.cipherSuite.id[0]);
++++++++ rval.putByte(c.session.cipherSuite.id[1]);
++++++++ rval.putByte(c.session.compressionMethod);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a Certificate message.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * This is the first message the client can send after receiving a server
++++++++ * hello done message and the first message the server can send after
++++++++ * sending a ServerHello. This client message is only sent if the server
++++++++ * requests a certificate. If no suitable certificate is available, the
++++++++ * client should send a certificate message containing no certificates. If
++++++++ * client authentication is required by the server for the handshake to
++++++++ * continue, it may respond with a fatal handshake failure alert.
++++++++ *
++++++++ * opaque ASN.1Cert<1..2^24-1>;
++++++++ *
++++++++ * struct {
++++++++ * ASN.1Cert certificate_list<0..2^24-1>;
++++++++ * } Certificate;
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the Certificate byte buffer.
++++++++ */
++++++++tls.createCertificate = function(c) {
++++++++ // TODO: check certificate request to ensure types are supported
++++++++
++++++++ // get a certificate (a certificate as a PEM string)
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ var cert = null;
++++++++ if(c.getCertificate) {
++++++++ var hint;
++++++++ if(client) {
++++++++ hint = c.session.certificateRequest;
++++++++ } else {
++++++++ hint = c.session.extensions.server_name.serverNameList;
++++++++ }
++++++++ cert = c.getCertificate(c, hint);
++++++++ }
++++++++
++++++++ // buffer to hold certificate list
++++++++ var certList = forge.util.createBuffer();
++++++++ if(cert !== null) {
++++++++ try {
++++++++ // normalize cert to a chain of certificates
++++++++ if(!forge.util.isArray(cert)) {
++++++++ cert = [cert];
++++++++ }
++++++++ var asn1 = null;
++++++++ for(var i = 0; i < cert.length; ++i) {
++++++++ var msg = forge.pem.decode(cert[i])[0];
++++++++ if(msg.type !== 'CERTIFICATE' &&
++++++++ msg.type !== 'X509 CERTIFICATE' &&
++++++++ msg.type !== 'TRUSTED CERTIFICATE') {
++++++++ var error = new Error('Could not convert certificate from PEM; PEM ' +
++++++++ 'header type is not "CERTIFICATE", "X509 CERTIFICATE", or ' +
++++++++ '"TRUSTED CERTIFICATE".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert certificate from PEM; PEM is encrypted.');
++++++++ }
++++++++
++++++++ var der = forge.util.createBuffer(msg.body);
++++++++ if(asn1 === null) {
++++++++ asn1 = forge.asn1.fromDer(der.bytes(), false);
++++++++ }
++++++++
++++++++ // certificate entry is itself a vector with 3 length bytes
++++++++ var certBuffer = forge.util.createBuffer();
++++++++ writeVector(certBuffer, 3, der);
++++++++
++++++++ // add cert vector to cert list vector
++++++++ certList.putBuffer(certBuffer);
++++++++ }
++++++++
++++++++ // save certificate
++++++++ cert = forge.pki.certificateFromAsn1(asn1);
++++++++ if(client) {
++++++++ c.session.clientCertificate = cert;
++++++++ } else {
++++++++ c.session.serverCertificate = cert;
++++++++ }
++++++++ } catch(ex) {
++++++++ return c.error(c, {
++++++++ message: 'Could not send certificate list.',
++++++++ cause: ex,
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.bad_certificate
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++
++++++++ // determine length of the handshake message
++++++++ var length = 3 + certList.length(); // cert list vector
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.certificate);
++++++++ rval.putInt24(length);
++++++++ writeVector(rval, 3, certList);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a ClientKeyExchange message.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * This message is always sent by the client. It will immediately follow the
++++++++ * client certificate message, if it is sent. Otherwise it will be the first
++++++++ * message sent by the client after it receives the server hello done
++++++++ * message.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * With this message, the premaster secret is set, either though direct
++++++++ * transmission of the RSA-encrypted secret, or by the transmission of
++++++++ * Diffie-Hellman parameters which will allow each side to agree upon the
++++++++ * same premaster secret. When the key exchange method is DH_RSA or DH_DSS,
++++++++ * client certification has been requested, and the client was able to
++++++++ * respond with a certificate which contained a Diffie-Hellman public key
++++++++ * whose parameters (group and generator) matched those specified by the
++++++++ * server in its certificate, this message will not contain any data.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * If RSA is being used for key agreement and authentication, the client
++++++++ * generates a 48-byte premaster secret, encrypts it using the public key
++++++++ * from the server's certificate or the temporary RSA key provided in a
++++++++ * server key exchange message, and sends the result in an encrypted
++++++++ * premaster secret message. This structure is a variant of the client
++++++++ * key exchange message, not a message in itself.
++++++++ *
++++++++ * struct {
++++++++ * select(KeyExchangeAlgorithm) {
++++++++ * case rsa: EncryptedPreMasterSecret;
++++++++ * case diffie_hellman: ClientDiffieHellmanPublic;
++++++++ * } exchange_keys;
++++++++ * } ClientKeyExchange;
++++++++ *
++++++++ * struct {
++++++++ * ProtocolVersion client_version;
++++++++ * opaque random[46];
++++++++ * } PreMasterSecret;
++++++++ *
++++++++ * struct {
++++++++ * public-key-encrypted PreMasterSecret pre_master_secret;
++++++++ * } EncryptedPreMasterSecret;
++++++++ *
++++++++ * A public-key-encrypted element is encoded as a vector <0..2^16-1>.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the ClientKeyExchange byte buffer.
++++++++ */
++++++++tls.createClientKeyExchange = function(c) {
++++++++ // create buffer to encrypt
++++++++ var b = forge.util.createBuffer();
++++++++
++++++++ // add highest client-supported protocol to help server avoid version
++++++++ // rollback attacks
++++++++ b.putByte(c.session.clientHelloVersion.major);
++++++++ b.putByte(c.session.clientHelloVersion.minor);
++++++++
++++++++ // generate and add 46 random bytes
++++++++ b.putBytes(forge.random.getBytes(46));
++++++++
++++++++ // save pre-master secret
++++++++ var sp = c.session.sp;
++++++++ sp.pre_master_secret = b.getBytes();
++++++++
++++++++ // RSA-encrypt the pre-master secret
++++++++ var key = c.session.serverCertificate.publicKey;
++++++++ b = key.encrypt(sp.pre_master_secret);
++++++++
++++++++ /* Note: The encrypted pre-master secret will be stored in a
++++++++ public-key-encrypted opaque vector that has the length prefixed using
++++++++ 2 bytes, so include those 2 bytes in the handshake message length. This
++++++++ is done as a minor optimization instead of calling writeVector(). */
++++++++
++++++++ // determine length of the handshake message
++++++++ var length = b.length + 2;
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.client_key_exchange);
++++++++ rval.putInt24(length);
++++++++ // add vector length bytes
++++++++ rval.putInt16(b.length);
++++++++ rval.putBytes(b);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a ServerKeyExchange message.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the ServerKeyExchange byte buffer.
++++++++ */
++++++++tls.createServerKeyExchange = function(c) {
++++++++ // this implementation only supports RSA, no Diffie-Hellman support,
++++++++ // so this record is empty
++++++++
++++++++ // determine length of the handshake message
++++++++ var length = 0;
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ if(length > 0) {
++++++++ rval.putByte(tls.HandshakeType.server_key_exchange);
++++++++ rval.putInt24(length);
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the signed data used to verify a client-side certificate. See
++++++++ * tls.createCertificateVerify() for details.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param callback the callback to call once the signed data is ready.
++++++++ */
++++++++tls.getClientSignature = function(c, callback) {
++++++++ // generate data to RSA encrypt
++++++++ var b = forge.util.createBuffer();
++++++++ b.putBuffer(c.session.md5.digest());
++++++++ b.putBuffer(c.session.sha1.digest());
++++++++ b = b.getBytes();
++++++++
++++++++ // create default signing function as necessary
++++++++ c.getSignature = c.getSignature || function(c, b, callback) {
++++++++ // do rsa encryption, call callback
++++++++ var privateKey = null;
++++++++ if(c.getPrivateKey) {
++++++++ try {
++++++++ privateKey = c.getPrivateKey(c, c.session.clientCertificate);
++++++++ privateKey = forge.pki.privateKeyFromPem(privateKey);
++++++++ } catch(ex) {
++++++++ c.error(c, {
++++++++ message: 'Could not get private key.',
++++++++ cause: ex,
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++ if(privateKey === null) {
++++++++ c.error(c, {
++++++++ message: 'No private key set.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.internal_error
++++++++ }
++++++++ });
++++++++ } else {
++++++++ b = privateKey.sign(b, null);
++++++++ }
++++++++ callback(c, b);
++++++++ };
++++++++
++++++++ // get client signature
++++++++ c.getSignature(c, b, callback);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a CertificateVerify message.
++++++++ *
++++++++ * Meaning of this message:
++++++++ * This structure conveys the client's Diffie-Hellman public value
++++++++ * (Yc) if it was not already included in the client's certificate.
++++++++ * The encoding used for Yc is determined by the enumerated
++++++++ * PublicValueEncoding. This structure is a variant of the client
++++++++ * key exchange message, not a message in itself.
++++++++ *
++++++++ * When this message will be sent:
++++++++ * This message is used to provide explicit verification of a client
++++++++ * certificate. This message is only sent following a client
++++++++ * certificate that has signing capability (i.e. all certificates
++++++++ * except those containing fixed Diffie-Hellman parameters). When
++++++++ * sent, it will immediately follow the client key exchange message.
++++++++ *
++++++++ * struct {
++++++++ * Signature signature;
++++++++ * } CertificateVerify;
++++++++ *
++++++++ * CertificateVerify.signature.md5_hash
++++++++ * MD5(handshake_messages);
++++++++ *
++++++++ * Certificate.signature.sha_hash
++++++++ * SHA(handshake_messages);
++++++++ *
++++++++ * Here handshake_messages refers to all handshake messages sent or
++++++++ * received starting at client hello up to but not including this
++++++++ * message, including the type and length fields of the handshake
++++++++ * messages.
++++++++ *
++++++++ * select(SignatureAlgorithm) {
++++++++ * case anonymous: struct { };
++++++++ * case rsa:
++++++++ * digitally-signed struct {
++++++++ * opaque md5_hash[16];
++++++++ * opaque sha_hash[20];
++++++++ * };
++++++++ * case dsa:
++++++++ * digitally-signed struct {
++++++++ * opaque sha_hash[20];
++++++++ * };
++++++++ * } Signature;
++++++++ *
++++++++ * In digital signing, one-way hash functions are used as input for a
++++++++ * signing algorithm. A digitally-signed element is encoded as an opaque
++++++++ * vector <0..2^16-1>, where the length is specified by the signing
++++++++ * algorithm and key.
++++++++ *
++++++++ * In RSA signing, a 36-byte structure of two hashes (one SHA and one
++++++++ * MD5) is signed (encrypted with the private key). It is encoded with
++++++++ * PKCS #1 block type 0 or type 1 as described in [PKCS1].
++++++++ *
++++++++ * In DSS, the 20 bytes of the SHA hash are run directly through the
++++++++ * Digital Signing Algorithm with no additional hashing.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param signature the signature to include in the message.
++++++++ *
++++++++ * @return the CertificateVerify byte buffer.
++++++++ */
++++++++tls.createCertificateVerify = function(c, signature) {
++++++++ /* Note: The signature will be stored in a "digitally-signed" opaque
++++++++ vector that has the length prefixed using 2 bytes, so include those
++++++++ 2 bytes in the handshake message length. This is done as a minor
++++++++ optimization instead of calling writeVector(). */
++++++++
++++++++ // determine length of the handshake message
++++++++ var length = signature.length + 2;
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.certificate_verify);
++++++++ rval.putInt24(length);
++++++++ // add vector length bytes
++++++++ rval.putInt16(signature.length);
++++++++ rval.putBytes(signature);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a CertificateRequest message.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the CertificateRequest byte buffer.
++++++++ */
++++++++tls.createCertificateRequest = function(c) {
++++++++ // TODO: support other certificate types
++++++++ var certTypes = forge.util.createBuffer();
++++++++
++++++++ // common RSA certificate type
++++++++ certTypes.putByte(0x01);
++++++++
++++++++ // add distinguished names from CA store
++++++++ var cAs = forge.util.createBuffer();
++++++++ for(var key in c.caStore.certs) {
++++++++ var cert = c.caStore.certs[key];
++++++++ var dn = forge.pki.distinguishedNameToAsn1(cert.subject);
++++++++ var byteBuffer = forge.asn1.toDer(dn);
++++++++ cAs.putInt16(byteBuffer.length());
++++++++ cAs.putBuffer(byteBuffer);
++++++++ }
++++++++
++++++++ // TODO: TLS 1.2+ has a different format
++++++++
++++++++ // determine length of the handshake message
++++++++ var length =
++++++++ 1 + certTypes.length() +
++++++++ 2 + cAs.length();
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.certificate_request);
++++++++ rval.putInt24(length);
++++++++ writeVector(rval, 1, certTypes);
++++++++ writeVector(rval, 2, cAs);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a ServerHelloDone message.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the ServerHelloDone byte buffer.
++++++++ */
++++++++tls.createServerHelloDone = function(c) {
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.server_hello_done);
++++++++ rval.putInt24(0);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a ChangeCipherSpec message.
++++++++ *
++++++++ * The change cipher spec protocol exists to signal transitions in
++++++++ * ciphering strategies. The protocol consists of a single message,
++++++++ * which is encrypted and compressed under the current (not the pending)
++++++++ * connection state. The message consists of a single byte of value 1.
++++++++ *
++++++++ * struct {
++++++++ * enum { change_cipher_spec(1), (255) } type;
++++++++ * } ChangeCipherSpec;
++++++++ *
++++++++ * @return the ChangeCipherSpec byte buffer.
++++++++ */
++++++++tls.createChangeCipherSpec = function() {
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(0x01);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a Finished message.
++++++++ *
++++++++ * struct {
++++++++ * opaque verify_data[12];
++++++++ * } Finished;
++++++++ *
++++++++ * verify_data
++++++++ * PRF(master_secret, finished_label, MD5(handshake_messages) +
++++++++ * SHA-1(handshake_messages)) [0..11];
++++++++ *
++++++++ * finished_label
++++++++ * For Finished messages sent by the client, the string "client
++++++++ * finished". For Finished messages sent by the server, the
++++++++ * string "server finished".
++++++++ *
++++++++ * handshake_messages
++++++++ * All of the data from all handshake messages up to but not
++++++++ * including this message. This is only data visible at the
++++++++ * handshake layer and does not include record layer headers.
++++++++ * This is the concatenation of all the Handshake structures as
++++++++ * defined in 7.4 exchanged thus far.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return the Finished byte buffer.
++++++++ */
++++++++tls.createFinished = function(c) {
++++++++ // generate verify_data
++++++++ var b = forge.util.createBuffer();
++++++++ b.putBuffer(c.session.md5.digest());
++++++++ b.putBuffer(c.session.sha1.digest());
++++++++
++++++++ // TODO: determine prf function and verify length for TLS 1.2
++++++++ var client = (c.entity === tls.ConnectionEnd.client);
++++++++ var sp = c.session.sp;
++++++++ var vdl = 12;
++++++++ var prf = prf_TLS1;
++++++++ var label = client ? 'client finished' : 'server finished';
++++++++ b = prf(sp.master_secret, label, b.getBytes(), vdl);
++++++++
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(tls.HandshakeType.finished);
++++++++ rval.putInt24(b.length());
++++++++ rval.putBuffer(b);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a HeartbeatMessage (See RFC 6520).
++++++++ *
++++++++ * struct {
++++++++ * HeartbeatMessageType type;
++++++++ * uint16 payload_length;
++++++++ * opaque payload[HeartbeatMessage.payload_length];
++++++++ * opaque padding[padding_length];
++++++++ * } HeartbeatMessage;
++++++++ *
++++++++ * The total length of a HeartbeatMessage MUST NOT exceed 2^14 or
++++++++ * max_fragment_length when negotiated as defined in [RFC6066].
++++++++ *
++++++++ * type: The message type, either heartbeat_request or heartbeat_response.
++++++++ *
++++++++ * payload_length: The length of the payload.
++++++++ *
++++++++ * payload: The payload consists of arbitrary content.
++++++++ *
++++++++ * padding: The padding is random content that MUST be ignored by the
++++++++ * receiver. The length of a HeartbeatMessage is TLSPlaintext.length
++++++++ * for TLS and DTLSPlaintext.length for DTLS. Furthermore, the
++++++++ * length of the type field is 1 byte, and the length of the
++++++++ * payload_length is 2. Therefore, the padding_length is
++++++++ * TLSPlaintext.length - payload_length - 3 for TLS and
++++++++ * DTLSPlaintext.length - payload_length - 3 for DTLS. The
++++++++ * padding_length MUST be at least 16.
++++++++ *
++++++++ * The sender of a HeartbeatMessage MUST use a random padding of at
++++++++ * least 16 bytes. The padding of a received HeartbeatMessage message
++++++++ * MUST be ignored.
++++++++ *
++++++++ * If the payload_length of a received HeartbeatMessage is too large,
++++++++ * the received HeartbeatMessage MUST be discarded silently.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param type the tls.HeartbeatMessageType.
++++++++ * @param payload the heartbeat data to send as the payload.
++++++++ * @param [payloadLength] the payload length to use, defaults to the
++++++++ * actual payload length.
++++++++ *
++++++++ * @return the HeartbeatRequest byte buffer.
++++++++ */
++++++++tls.createHeartbeat = function(type, payload, payloadLength) {
++++++++ if(typeof payloadLength === 'undefined') {
++++++++ payloadLength = payload.length;
++++++++ }
++++++++ // build record fragment
++++++++ var rval = forge.util.createBuffer();
++++++++ rval.putByte(type); // heartbeat message type
++++++++ rval.putInt16(payloadLength); // payload length
++++++++ rval.putBytes(payload); // payload
++++++++ // padding
++++++++ var plaintextLength = rval.length();
++++++++ var paddingLength = Math.max(16, plaintextLength - payloadLength - 3);
++++++++ rval.putBytes(forge.random.getBytes(paddingLength));
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Fragments, compresses, encrypts, and queues a record for delivery.
++++++++ *
++++++++ * @param c the connection.
++++++++ * @param record the record to queue.
++++++++ */
++++++++tls.queue = function(c, record) {
++++++++ // error during record creation
++++++++ if(!record) {
++++++++ return;
++++++++ }
++++++++
++++++++ if(record.fragment.length() === 0) {
++++++++ if(record.type === tls.ContentType.handshake ||
++++++++ record.type === tls.ContentType.alert ||
++++++++ record.type === tls.ContentType.change_cipher_spec) {
++++++++ // Empty handshake, alert of change cipher spec messages are not allowed per the TLS specification and should not be sent.
++++++++ return;
++++++++ }
++++++++ }
++++++++
++++++++ // if the record is a handshake record, update handshake hashes
++++++++ if(record.type === tls.ContentType.handshake) {
++++++++ var bytes = record.fragment.bytes();
++++++++ c.session.md5.update(bytes);
++++++++ c.session.sha1.update(bytes);
++++++++ bytes = null;
++++++++ }
++++++++
++++++++ // handle record fragmentation
++++++++ var records;
++++++++ if(record.fragment.length() <= tls.MaxFragment) {
++++++++ records = [record];
++++++++ } else {
++++++++ // fragment data as long as it is too long
++++++++ records = [];
++++++++ var data = record.fragment.bytes();
++++++++ while(data.length > tls.MaxFragment) {
++++++++ records.push(tls.createRecord(c, {
++++++++ type: record.type,
++++++++ data: forge.util.createBuffer(data.slice(0, tls.MaxFragment))
++++++++ }));
++++++++ data = data.slice(tls.MaxFragment);
++++++++ }
++++++++ // add last record
++++++++ if(data.length > 0) {
++++++++ records.push(tls.createRecord(c, {
++++++++ type: record.type,
++++++++ data: forge.util.createBuffer(data)
++++++++ }));
++++++++ }
++++++++ }
++++++++
++++++++ // compress and encrypt all fragmented records
++++++++ for(var i = 0; i < records.length && !c.fail; ++i) {
++++++++ // update the record using current write state
++++++++ var rec = records[i];
++++++++ var s = c.state.current.write;
++++++++ if(s.update(c, rec)) {
++++++++ // store record
++++++++ c.records.push(rec);
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Flushes all queued records to the output buffer and calls the
++++++++ * tlsDataReady() handler on the given connection.
++++++++ *
++++++++ * @param c the connection.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++tls.flush = function(c) {
++++++++ for(var i = 0; i < c.records.length; ++i) {
++++++++ var record = c.records[i];
++++++++
++++++++ // add record header and fragment
++++++++ c.tlsData.putByte(record.type);
++++++++ c.tlsData.putByte(record.version.major);
++++++++ c.tlsData.putByte(record.version.minor);
++++++++ c.tlsData.putInt16(record.fragment.length());
++++++++ c.tlsData.putBuffer(c.records[i].fragment);
++++++++ }
++++++++ c.records = [];
++++++++ return c.tlsDataReady(c);
++++++++};
++++++++
++++++++/**
++++++++ * Maps a pki.certificateError to a tls.Alert.Description.
++++++++ *
++++++++ * @param error the error to map.
++++++++ *
++++++++ * @return the alert description.
++++++++ */
++++++++var _certErrorToAlertDesc = function(error) {
++++++++ switch(error) {
++++++++ case true:
++++++++ return true;
++++++++ case forge.pki.certificateError.bad_certificate:
++++++++ return tls.Alert.Description.bad_certificate;
++++++++ case forge.pki.certificateError.unsupported_certificate:
++++++++ return tls.Alert.Description.unsupported_certificate;
++++++++ case forge.pki.certificateError.certificate_revoked:
++++++++ return tls.Alert.Description.certificate_revoked;
++++++++ case forge.pki.certificateError.certificate_expired:
++++++++ return tls.Alert.Description.certificate_expired;
++++++++ case forge.pki.certificateError.certificate_unknown:
++++++++ return tls.Alert.Description.certificate_unknown;
++++++++ case forge.pki.certificateError.unknown_ca:
++++++++ return tls.Alert.Description.unknown_ca;
++++++++ default:
++++++++ return tls.Alert.Description.bad_certificate;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Maps a tls.Alert.Description to a pki.certificateError.
++++++++ *
++++++++ * @param desc the alert description.
++++++++ *
++++++++ * @return the certificate error.
++++++++ */
++++++++var _alertDescToCertError = function(desc) {
++++++++ switch(desc) {
++++++++ case true:
++++++++ return true;
++++++++ case tls.Alert.Description.bad_certificate:
++++++++ return forge.pki.certificateError.bad_certificate;
++++++++ case tls.Alert.Description.unsupported_certificate:
++++++++ return forge.pki.certificateError.unsupported_certificate;
++++++++ case tls.Alert.Description.certificate_revoked:
++++++++ return forge.pki.certificateError.certificate_revoked;
++++++++ case tls.Alert.Description.certificate_expired:
++++++++ return forge.pki.certificateError.certificate_expired;
++++++++ case tls.Alert.Description.certificate_unknown:
++++++++ return forge.pki.certificateError.certificate_unknown;
++++++++ case tls.Alert.Description.unknown_ca:
++++++++ return forge.pki.certificateError.unknown_ca;
++++++++ default:
++++++++ return forge.pki.certificateError.bad_certificate;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Verifies a certificate chain against the given connection's
++++++++ * Certificate Authority store.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param chain the certificate chain to verify, with the root or highest
++++++++ * authority at the end.
++++++++ *
++++++++ * @return true if successful, false if not.
++++++++ */
++++++++tls.verifyCertificateChain = function(c, chain) {
++++++++ try {
++++++++ // Make a copy of c.verifyOptions so that we can modify options.verify
++++++++ // without modifying c.verifyOptions.
++++++++ var options = {};
++++++++ for (var key in c.verifyOptions) {
++++++++ options[key] = c.verifyOptions[key];
++++++++ }
++++++++
++++++++ options.verify = function(vfd, depth, chain) {
++++++++ // convert pki.certificateError to tls alert description
++++++++ var desc = _certErrorToAlertDesc(vfd);
++++++++
++++++++ // call application callback
++++++++ var ret = c.verify(c, vfd, depth, chain);
++++++++ if(ret !== true) {
++++++++ if(typeof ret === 'object' && !forge.util.isArray(ret)) {
++++++++ // throw custom error
++++++++ var error = new Error('The application rejected the certificate.');
++++++++ error.send = true;
++++++++ error.alert = {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.bad_certificate
++++++++ };
++++++++ if(ret.message) {
++++++++ error.message = ret.message;
++++++++ }
++++++++ if(ret.alert) {
++++++++ error.alert.description = ret.alert;
++++++++ }
++++++++ throw error;
++++++++ }
++++++++
++++++++ // convert tls alert description to pki.certificateError
++++++++ if(ret !== vfd) {
++++++++ ret = _alertDescToCertError(ret);
++++++++ }
++++++++ }
++++++++
++++++++ return ret;
++++++++ };
++++++++
++++++++ // verify chain
++++++++ forge.pki.verifyCertificateChain(c.caStore, chain, options);
++++++++ } catch(ex) {
++++++++ // build tls error if not already customized
++++++++ var err = ex;
++++++++ if(typeof err !== 'object' || forge.util.isArray(err)) {
++++++++ err = {
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: _certErrorToAlertDesc(ex)
++++++++ }
++++++++ };
++++++++ }
++++++++ if(!('send' in err)) {
++++++++ err.send = true;
++++++++ }
++++++++ if(!('alert' in err)) {
++++++++ err.alert = {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: _certErrorToAlertDesc(err.error)
++++++++ };
++++++++ }
++++++++
++++++++ // send error
++++++++ c.error(c, err);
++++++++ }
++++++++
++++++++ return !c.fail;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new TLS session cache.
++++++++ *
++++++++ * @param cache optional map of session ID to cached session.
++++++++ * @param capacity the maximum size for the cache (default: 100).
++++++++ *
++++++++ * @return the new TLS session cache.
++++++++ */
++++++++tls.createSessionCache = function(cache, capacity) {
++++++++ var rval = null;
++++++++
++++++++ // assume input is already a session cache object
++++++++ if(cache && cache.getSession && cache.setSession && cache.order) {
++++++++ rval = cache;
++++++++ } else {
++++++++ // create cache
++++++++ rval = {};
++++++++ rval.cache = cache || {};
++++++++ rval.capacity = Math.max(capacity || 100, 1);
++++++++ rval.order = [];
++++++++
++++++++ // store order for sessions, delete session overflow
++++++++ for(var key in cache) {
++++++++ if(rval.order.length <= capacity) {
++++++++ rval.order.push(key);
++++++++ } else {
++++++++ delete cache[key];
++++++++ }
++++++++ }
++++++++
++++++++ // get a session from a session ID (or get any session)
++++++++ rval.getSession = function(sessionId) {
++++++++ var session = null;
++++++++ var key = null;
++++++++
++++++++ // if session ID provided, use it
++++++++ if(sessionId) {
++++++++ key = forge.util.bytesToHex(sessionId);
++++++++ } else if(rval.order.length > 0) {
++++++++ // get first session from cache
++++++++ key = rval.order[0];
++++++++ }
++++++++
++++++++ if(key !== null && key in rval.cache) {
++++++++ // get cached session and remove from cache
++++++++ session = rval.cache[key];
++++++++ delete rval.cache[key];
++++++++ for(var i in rval.order) {
++++++++ if(rval.order[i] === key) {
++++++++ rval.order.splice(i, 1);
++++++++ break;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return session;
++++++++ };
++++++++
++++++++ // set a session in the cache
++++++++ rval.setSession = function(sessionId, session) {
++++++++ // remove session from cache if at capacity
++++++++ if(rval.order.length === rval.capacity) {
++++++++ var key = rval.order.shift();
++++++++ delete rval.cache[key];
++++++++ }
++++++++ // add session to cache
++++++++ var key = forge.util.bytesToHex(sessionId);
++++++++ rval.order.push(key);
++++++++ rval.cache[key] = session;
++++++++ };
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new TLS connection.
++++++++ *
++++++++ * See public createConnection() docs for more details.
++++++++ *
++++++++ * @param options the options for this connection.
++++++++ *
++++++++ * @return the new TLS connection.
++++++++ */
++++++++tls.createConnection = function(options) {
++++++++ var caStore = null;
++++++++ if(options.caStore) {
++++++++ // if CA store is an array, convert it to a CA store object
++++++++ if(forge.util.isArray(options.caStore)) {
++++++++ caStore = forge.pki.createCaStore(options.caStore);
++++++++ } else {
++++++++ caStore = options.caStore;
++++++++ }
++++++++ } else {
++++++++ // create empty CA store
++++++++ caStore = forge.pki.createCaStore();
++++++++ }
++++++++
++++++++ // setup default cipher suites
++++++++ var cipherSuites = options.cipherSuites || null;
++++++++ if(cipherSuites === null) {
++++++++ cipherSuites = [];
++++++++ for(var key in tls.CipherSuites) {
++++++++ cipherSuites.push(tls.CipherSuites[key]);
++++++++ }
++++++++ }
++++++++
++++++++ // set default entity
++++++++ var entity = (options.server || false) ?
++++++++ tls.ConnectionEnd.server : tls.ConnectionEnd.client;
++++++++
++++++++ // create session cache if requested
++++++++ var sessionCache = options.sessionCache ?
++++++++ tls.createSessionCache(options.sessionCache) : null;
++++++++
++++++++ // create TLS connection
++++++++ var c = {
++++++++ version: {major: tls.Version.major, minor: tls.Version.minor},
++++++++ entity: entity,
++++++++ sessionId: options.sessionId,
++++++++ caStore: caStore,
++++++++ sessionCache: sessionCache,
++++++++ cipherSuites: cipherSuites,
++++++++ connected: options.connected,
++++++++ virtualHost: options.virtualHost || null,
++++++++ verifyClient: options.verifyClient || false,
++++++++ verify: options.verify || function(cn, vfd, dpth, cts) {return vfd;},
++++++++ verifyOptions: options.verifyOptions || {},
++++++++ getCertificate: options.getCertificate || null,
++++++++ getPrivateKey: options.getPrivateKey || null,
++++++++ getSignature: options.getSignature || null,
++++++++ input: forge.util.createBuffer(),
++++++++ tlsData: forge.util.createBuffer(),
++++++++ data: forge.util.createBuffer(),
++++++++ tlsDataReady: options.tlsDataReady,
++++++++ dataReady: options.dataReady,
++++++++ heartbeatReceived: options.heartbeatReceived,
++++++++ closed: options.closed,
++++++++ error: function(c, ex) {
++++++++ // set origin if not set
++++++++ ex.origin = ex.origin ||
++++++++ ((c.entity === tls.ConnectionEnd.client) ? 'client' : 'server');
++++++++
++++++++ // send TLS alert
++++++++ if(ex.send) {
++++++++ tls.queue(c, tls.createAlert(c, ex.alert));
++++++++ tls.flush(c);
++++++++ }
++++++++
++++++++ // error is fatal by default
++++++++ var fatal = (ex.fatal !== false);
++++++++ if(fatal) {
++++++++ // set fail flag
++++++++ c.fail = true;
++++++++ }
++++++++
++++++++ // call error handler first
++++++++ options.error(c, ex);
++++++++
++++++++ if(fatal) {
++++++++ // fatal error, close connection, do not clear fail
++++++++ c.close(false);
++++++++ }
++++++++ },
++++++++ deflate: options.deflate || null,
++++++++ inflate: options.inflate || null
++++++++ };
++++++++
++++++++ /**
++++++++ * Resets a closed TLS connection for reuse. Called in c.close().
++++++++ *
++++++++ * @param clearFail true to clear the fail flag (default: true).
++++++++ */
++++++++ c.reset = function(clearFail) {
++++++++ c.version = {major: tls.Version.major, minor: tls.Version.minor};
++++++++ c.record = null;
++++++++ c.session = null;
++++++++ c.peerCertificate = null;
++++++++ c.state = {
++++++++ pending: null,
++++++++ current: null
++++++++ };
++++++++ c.expect = (c.entity === tls.ConnectionEnd.client) ? SHE : CHE;
++++++++ c.fragmented = null;
++++++++ c.records = [];
++++++++ c.open = false;
++++++++ c.handshakes = 0;
++++++++ c.handshaking = false;
++++++++ c.isConnected = false;
++++++++ c.fail = !(clearFail || typeof(clearFail) === 'undefined');
++++++++ c.input.clear();
++++++++ c.tlsData.clear();
++++++++ c.data.clear();
++++++++ c.state.current = tls.createConnectionState(c);
++++++++ };
++++++++
++++++++ // do initial reset of connection
++++++++ c.reset();
++++++++
++++++++ /**
++++++++ * Updates the current TLS engine state based on the given record.
++++++++ *
++++++++ * @param c the TLS connection.
++++++++ * @param record the TLS record to act on.
++++++++ */
++++++++ var _update = function(c, record) {
++++++++ // get record handler (align type in table by subtracting lowest)
++++++++ var aligned = record.type - tls.ContentType.change_cipher_spec;
++++++++ var handlers = ctTable[c.entity][c.expect];
++++++++ if(aligned in handlers) {
++++++++ handlers[aligned](c, record);
++++++++ } else {
++++++++ // unexpected record
++++++++ tls.handleUnexpected(c, record);
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads the record header and initializes the next record on the given
++++++++ * connection.
++++++++ *
++++++++ * @param c the TLS connection with the next record.
++++++++ *
++++++++ * @return 0 if the input data could be processed, otherwise the
++++++++ * number of bytes required for data to be processed.
++++++++ */
++++++++ var _readRecordHeader = function(c) {
++++++++ var rval = 0;
++++++++
++++++++ // get input buffer and its length
++++++++ var b = c.input;
++++++++ var len = b.length();
++++++++
++++++++ // need at least 5 bytes to initialize a record
++++++++ if(len < 5) {
++++++++ rval = 5 - len;
++++++++ } else {
++++++++ // enough bytes for header
++++++++ // initialize record
++++++++ c.record = {
++++++++ type: b.getByte(),
++++++++ version: {
++++++++ major: b.getByte(),
++++++++ minor: b.getByte()
++++++++ },
++++++++ length: b.getInt16(),
++++++++ fragment: forge.util.createBuffer(),
++++++++ ready: false
++++++++ };
++++++++
++++++++ // check record version
++++++++ var compatibleVersion = (c.record.version.major === c.version.major);
++++++++ if(compatibleVersion && c.session && c.session.version) {
++++++++ // session version already set, require same minor version
++++++++ compatibleVersion = (c.record.version.minor === c.version.minor);
++++++++ }
++++++++ if(!compatibleVersion) {
++++++++ c.error(c, {
++++++++ message: 'Incompatible TLS version.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description: tls.Alert.Description.protocol_version
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads the next record's contents and appends its message to any
++++++++ * previously fragmented message.
++++++++ *
++++++++ * @param c the TLS connection with the next record.
++++++++ *
++++++++ * @return 0 if the input data could be processed, otherwise the
++++++++ * number of bytes required for data to be processed.
++++++++ */
++++++++ var _readRecord = function(c) {
++++++++ var rval = 0;
++++++++
++++++++ // ensure there is enough input data to get the entire record
++++++++ var b = c.input;
++++++++ var len = b.length();
++++++++ if(len < c.record.length) {
++++++++ // not enough data yet, return how much is required
++++++++ rval = c.record.length - len;
++++++++ } else {
++++++++ // there is enough data to parse the pending record
++++++++ // fill record fragment and compact input buffer
++++++++ c.record.fragment.putBytes(b.getBytes(c.record.length));
++++++++ b.compact();
++++++++
++++++++ // update record using current read state
++++++++ var s = c.state.current.read;
++++++++ if(s.update(c, c.record)) {
++++++++ // see if there is a previously fragmented message that the
++++++++ // new record's message fragment should be appended to
++++++++ if(c.fragmented !== null) {
++++++++ // if the record type matches a previously fragmented
++++++++ // record, append the record fragment to it
++++++++ if(c.fragmented.type === c.record.type) {
++++++++ // concatenate record fragments
++++++++ c.fragmented.fragment.putBuffer(c.record.fragment);
++++++++ c.record = c.fragmented;
++++++++ } else {
++++++++ // error, invalid fragmented record
++++++++ c.error(c, {
++++++++ message: 'Invalid fragmented record.',
++++++++ send: true,
++++++++ alert: {
++++++++ level: tls.Alert.Level.fatal,
++++++++ description:
++++++++ tls.Alert.Description.unexpected_message
++++++++ }
++++++++ });
++++++++ }
++++++++ }
++++++++
++++++++ // record is now ready
++++++++ c.record.ready = true;
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Performs a handshake using the TLS Handshake Protocol, as a client.
++++++++ *
++++++++ * This method should only be called if the connection is in client mode.
++++++++ *
++++++++ * @param sessionId the session ID to use, null to start a new one.
++++++++ */
++++++++ c.handshake = function(sessionId) {
++++++++ // error to call this in non-client mode
++++++++ if(c.entity !== tls.ConnectionEnd.client) {
++++++++ // not fatal error
++++++++ c.error(c, {
++++++++ message: 'Cannot initiate handshake as a server.',
++++++++ fatal: false
++++++++ });
++++++++ } else if(c.handshaking) {
++++++++ // handshake is already in progress, fail but not fatal error
++++++++ c.error(c, {
++++++++ message: 'Handshake already in progress.',
++++++++ fatal: false
++++++++ });
++++++++ } else {
++++++++ // clear fail flag on reuse
++++++++ if(c.fail && !c.open && c.handshakes === 0) {
++++++++ c.fail = false;
++++++++ }
++++++++
++++++++ // now handshaking
++++++++ c.handshaking = true;
++++++++
++++++++ // default to blank (new session)
++++++++ sessionId = sessionId || '';
++++++++
++++++++ // if a session ID was specified, try to find it in the cache
++++++++ var session = null;
++++++++ if(sessionId.length > 0) {
++++++++ if(c.sessionCache) {
++++++++ session = c.sessionCache.getSession(sessionId);
++++++++ }
++++++++
++++++++ // matching session not found in cache, clear session ID
++++++++ if(session === null) {
++++++++ sessionId = '';
++++++++ }
++++++++ }
++++++++
++++++++ // no session given, grab a session from the cache, if available
++++++++ if(sessionId.length === 0 && c.sessionCache) {
++++++++ session = c.sessionCache.getSession();
++++++++ if(session !== null) {
++++++++ sessionId = session.id;
++++++++ }
++++++++ }
++++++++
++++++++ // set up session
++++++++ c.session = {
++++++++ id: sessionId,
++++++++ version: null,
++++++++ cipherSuite: null,
++++++++ compressionMethod: null,
++++++++ serverCertificate: null,
++++++++ certificateRequest: null,
++++++++ clientCertificate: null,
++++++++ sp: {},
++++++++ md5: forge.md.md5.create(),
++++++++ sha1: forge.md.sha1.create()
++++++++ };
++++++++
++++++++ // use existing session information
++++++++ if(session) {
++++++++ // only update version on connection, session version not yet set
++++++++ c.version = session.version;
++++++++ c.session.sp = session.sp;
++++++++ }
++++++++
++++++++ // generate new client random
++++++++ c.session.sp.client_random = tls.createRandom().getBytes();
++++++++
++++++++ // connection now open
++++++++ c.open = true;
++++++++
++++++++ // send hello
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.handshake,
++++++++ data: tls.createClientHello(c)
++++++++ }));
++++++++ tls.flush(c);
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Called when TLS protocol data has been received from somewhere and should
++++++++ * be processed by the TLS engine.
++++++++ *
++++++++ * @param data the TLS protocol data, as a string, to process.
++++++++ *
++++++++ * @return 0 if the data could be processed, otherwise the number of bytes
++++++++ * required for data to be processed.
++++++++ */
++++++++ c.process = function(data) {
++++++++ var rval = 0;
++++++++
++++++++ // buffer input data
++++++++ if(data) {
++++++++ c.input.putBytes(data);
++++++++ }
++++++++
++++++++ // process next record if no failure, process will be called after
++++++++ // each record is handled (since handling can be asynchronous)
++++++++ if(!c.fail) {
++++++++ // reset record if ready and now empty
++++++++ if(c.record !== null &&
++++++++ c.record.ready && c.record.fragment.isEmpty()) {
++++++++ c.record = null;
++++++++ }
++++++++
++++++++ // if there is no pending record, try to read record header
++++++++ if(c.record === null) {
++++++++ rval = _readRecordHeader(c);
++++++++ }
++++++++
++++++++ // read the next record (if record not yet ready)
++++++++ if(!c.fail && c.record !== null && !c.record.ready) {
++++++++ rval = _readRecord(c);
++++++++ }
++++++++
++++++++ // record ready to be handled, update engine state
++++++++ if(!c.fail && c.record !== null && c.record.ready) {
++++++++ _update(c, c.record);
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Requests that application data be packaged into a TLS record. The
++++++++ * tlsDataReady handler will be called when the TLS record(s) have been
++++++++ * prepared.
++++++++ *
++++++++ * @param data the application data, as a raw 'binary' encoded string, to
++++++++ * be sent; to send utf-16/utf-8 string data, use the return value
++++++++ * of util.encodeUtf8(str).
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++ c.prepare = function(data) {
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.application_data,
++++++++ data: forge.util.createBuffer(data)
++++++++ }));
++++++++ return tls.flush(c);
++++++++ };
++++++++
++++++++ /**
++++++++ * Requests that a heartbeat request be packaged into a TLS record for
++++++++ * transmission. The tlsDataReady handler will be called when TLS record(s)
++++++++ * have been prepared.
++++++++ *
++++++++ * When a heartbeat response has been received, the heartbeatReceived
++++++++ * handler will be called with the matching payload. This handler can
++++++++ * be used to clear a retransmission timer, etc.
++++++++ *
++++++++ * @param payload the heartbeat data to send as the payload in the message.
++++++++ * @param [payloadLength] the payload length to use, defaults to the
++++++++ * actual payload length.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++ c.prepareHeartbeatRequest = function(payload, payloadLength) {
++++++++ if(payload instanceof forge.util.ByteBuffer) {
++++++++ payload = payload.bytes();
++++++++ }
++++++++ if(typeof payloadLength === 'undefined') {
++++++++ payloadLength = payload.length;
++++++++ }
++++++++ c.expectedHeartbeatPayload = payload;
++++++++ tls.queue(c, tls.createRecord(c, {
++++++++ type: tls.ContentType.heartbeat,
++++++++ data: tls.createHeartbeat(
++++++++ tls.HeartbeatMessageType.heartbeat_request, payload, payloadLength)
++++++++ }));
++++++++ return tls.flush(c);
++++++++ };
++++++++
++++++++ /**
++++++++ * Closes the connection (sends a close_notify alert).
++++++++ *
++++++++ * @param clearFail true to clear the fail flag (default: true).
++++++++ */
++++++++ c.close = function(clearFail) {
++++++++ // save session if connection didn't fail
++++++++ if(!c.fail && c.sessionCache && c.session) {
++++++++ // only need to preserve session ID, version, and security params
++++++++ var session = {
++++++++ id: c.session.id,
++++++++ version: c.session.version,
++++++++ sp: c.session.sp
++++++++ };
++++++++ session.sp.keys = null;
++++++++ c.sessionCache.setSession(session.id, session);
++++++++ }
++++++++
++++++++ if(c.open) {
++++++++ // connection no longer open, clear input
++++++++ c.open = false;
++++++++ c.input.clear();
++++++++
++++++++ // if connected or handshaking, send an alert
++++++++ if(c.isConnected || c.handshaking) {
++++++++ c.isConnected = c.handshaking = false;
++++++++
++++++++ // send close_notify alert
++++++++ tls.queue(c, tls.createAlert(c, {
++++++++ level: tls.Alert.Level.warning,
++++++++ description: tls.Alert.Description.close_notify
++++++++ }));
++++++++ tls.flush(c);
++++++++ }
++++++++
++++++++ // call handler
++++++++ c.closed(c);
++++++++ }
++++++++
++++++++ // reset TLS connection, do not clear fail flag
++++++++ c.reset(clearFail);
++++++++ };
++++++++
++++++++ return c;
++++++++};
++++++++
++++++++/* TLS API */
++++++++module.exports = forge.tls = forge.tls || {};
++++++++
++++++++// expose non-functions
++++++++for(var key in tls) {
++++++++ if(typeof tls[key] !== 'function') {
++++++++ forge.tls[key] = tls[key];
++++++++ }
++++++++}
++++++++
++++++++// expose prf_tls1 for testing
++++++++forge.tls.prf_tls1 = prf_TLS1;
++++++++
++++++++// expose sha1 hmac method
++++++++forge.tls.hmac_sha1 = hmac_sha1;
++++++++
++++++++// expose session cache creation
++++++++forge.tls.createSessionCache = tls.createSessionCache;
++++++++
++++++++/**
++++++++ * Creates a new TLS connection. This does not make any assumptions about the
++++++++ * transport layer that TLS is working on top of, ie: it does not assume there
++++++++ * is a TCP/IP connection or establish one. A TLS connection is totally
++++++++ * abstracted away from the layer is runs on top of, it merely establishes a
++++++++ * secure channel between a client" and a "server".
++++++++ *
++++++++ * A TLS connection contains 4 connection states: pending read and write, and
++++++++ * current read and write.
++++++++ *
++++++++ * At initialization, the current read and write states will be null. Only once
++++++++ * the security parameters have been set and the keys have been generated can
++++++++ * the pending states be converted into current states. Current states will be
++++++++ * updated for each record processed.
++++++++ *
++++++++ * A custom certificate verify callback may be provided to check information
++++++++ * like the common name on the server's certificate. It will be called for
++++++++ * every certificate in the chain. It has the following signature:
++++++++ *
++++++++ * variable func(c, certs, index, preVerify)
++++++++ * Where:
++++++++ * c The TLS connection
++++++++ * verified Set to true if certificate was verified, otherwise the alert
++++++++ * tls.Alert.Description for why the certificate failed.
++++++++ * depth The current index in the chain, where 0 is the server's cert.
++++++++ * certs The certificate chain, *NOTE* if the server was anonymous then
++++++++ * the chain will be empty.
++++++++ *
++++++++ * The function returns true on success and on failure either the appropriate
++++++++ * tls.Alert.Description or an object with 'alert' set to the appropriate
++++++++ * tls.Alert.Description and 'message' set to a custom error message. If true
++++++++ * is not returned then the connection will abort using, in order of
++++++++ * availability, first the returned alert description, second the preVerify
++++++++ * alert description, and lastly the default 'bad_certificate'.
++++++++ *
++++++++ * There are three callbacks that can be used to make use of client-side
++++++++ * certificates where each takes the TLS connection as the first parameter:
++++++++ *
++++++++ * getCertificate(conn, hint)
++++++++ * The second parameter is a hint as to which certificate should be
++++++++ * returned. If the connection entity is a client, then the hint will be
++++++++ * the CertificateRequest message from the server that is part of the
++++++++ * TLS protocol. If the connection entity is a server, then it will be
++++++++ * the servername list provided via an SNI extension the ClientHello, if
++++++++ * one was provided (empty array if not). The hint can be examined to
++++++++ * determine which certificate to use (advanced). Most implementations
++++++++ * will just return a certificate. The return value must be a
++++++++ * PEM-formatted certificate or an array of PEM-formatted certificates
++++++++ * that constitute a certificate chain, with the first in the array/chain
++++++++ * being the client's certificate.
++++++++ * getPrivateKey(conn, certificate)
++++++++ * The second parameter is an forge.pki X.509 certificate object that
++++++++ * is associated with the requested private key. The return value must
++++++++ * be a PEM-formatted private key.
++++++++ * getSignature(conn, bytes, callback)
++++++++ * This callback can be used instead of getPrivateKey if the private key
++++++++ * is not directly accessible in javascript or should not be. For
++++++++ * instance, a secure external web service could provide the signature
++++++++ * in exchange for appropriate credentials. The second parameter is a
++++++++ * string of bytes to be signed that are part of the TLS protocol. These
++++++++ * bytes are used to verify that the private key for the previously
++++++++ * provided client-side certificate is accessible to the client. The
++++++++ * callback is a function that takes 2 parameters, the TLS connection
++++++++ * and the RSA encrypted (signed) bytes as a string. This callback must
++++++++ * be called once the signature is ready.
++++++++ *
++++++++ * @param options the options for this connection:
++++++++ * server: true if the connection is server-side, false for client.
++++++++ * sessionId: a session ID to reuse, null for a new connection.
++++++++ * caStore: an array of certificates to trust.
++++++++ * sessionCache: a session cache to use.
++++++++ * cipherSuites: an optional array of cipher suites to use,
++++++++ * see tls.CipherSuites.
++++++++ * connected: function(conn) called when the first handshake completes.
++++++++ * virtualHost: the virtual server name to use in a TLS SNI extension.
++++++++ * verifyClient: true to require a client certificate in server mode,
++++++++ * 'optional' to request one, false not to (default: false).
++++++++ * verify: a handler used to custom verify certificates in the chain.
++++++++ * verifyOptions: an object with options for the certificate chain validation.
++++++++ * See documentation of pki.verifyCertificateChain for possible options.
++++++++ * verifyOptions.verify is ignored. If you wish to specify a verify handler
++++++++ * use the verify key.
++++++++ * getCertificate: an optional callback used to get a certificate or
++++++++ * a chain of certificates (as an array).
++++++++ * getPrivateKey: an optional callback used to get a private key.
++++++++ * getSignature: an optional callback used to get a signature.
++++++++ * tlsDataReady: function(conn) called when TLS protocol data has been
++++++++ * prepared and is ready to be used (typically sent over a socket
++++++++ * connection to its destination), read from conn.tlsData buffer.
++++++++ * dataReady: function(conn) called when application data has
++++++++ * been parsed from a TLS record and should be consumed by the
++++++++ * application, read from conn.data buffer.
++++++++ * closed: function(conn) called when the connection has been closed.
++++++++ * error: function(conn, error) called when there was an error.
++++++++ * deflate: function(inBytes) if provided, will deflate TLS records using
++++++++ * the deflate algorithm if the server supports it.
++++++++ * inflate: function(inBytes) if provided, will inflate TLS records using
++++++++ * the deflate algorithm if the server supports it.
++++++++ *
++++++++ * @return the new TLS connection.
++++++++ */
++++++++forge.tls.createConnection = tls.createConnection;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Socket wrapping functions for TLS.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2009-2012 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./tls');
++++++++
++++++++/**
++++++++ * Wraps a forge.net socket with a TLS layer.
++++++++ *
++++++++ * @param options:
++++++++ * sessionId: a session ID to reuse, null for a new connection if no session
++++++++ * cache is provided or it is empty.
++++++++ * caStore: an array of certificates to trust.
++++++++ * sessionCache: a session cache to use.
++++++++ * cipherSuites: an optional array of cipher suites to use, see
++++++++ * tls.CipherSuites.
++++++++ * socket: the socket to wrap.
++++++++ * virtualHost: the virtual server name to use in a TLS SNI extension.
++++++++ * verify: a handler used to custom verify certificates in the chain.
++++++++ * getCertificate: an optional callback used to get a certificate.
++++++++ * getPrivateKey: an optional callback used to get a private key.
++++++++ * getSignature: an optional callback used to get a signature.
++++++++ * deflate: function(inBytes) if provided, will deflate TLS records using
++++++++ * the deflate algorithm if the server supports it.
++++++++ * inflate: function(inBytes) if provided, will inflate TLS records using
++++++++ * the deflate algorithm if the server supports it.
++++++++ *
++++++++ * @return the TLS-wrapped socket.
++++++++ */
++++++++forge.tls.wrapSocket = function(options) {
++++++++ // get raw socket
++++++++ var socket = options.socket;
++++++++
++++++++ // create TLS socket
++++++++ var tlsSocket = {
++++++++ id: socket.id,
++++++++ // set handlers
++++++++ connected: socket.connected || function(e) {},
++++++++ closed: socket.closed || function(e) {},
++++++++ data: socket.data || function(e) {},
++++++++ error: socket.error || function(e) {}
++++++++ };
++++++++
++++++++ // create TLS connection
++++++++ var c = forge.tls.createConnection({
++++++++ server: false,
++++++++ sessionId: options.sessionId || null,
++++++++ caStore: options.caStore || [],
++++++++ sessionCache: options.sessionCache || null,
++++++++ cipherSuites: options.cipherSuites || null,
++++++++ virtualHost: options.virtualHost,
++++++++ verify: options.verify,
++++++++ getCertificate: options.getCertificate,
++++++++ getPrivateKey: options.getPrivateKey,
++++++++ getSignature: options.getSignature,
++++++++ deflate: options.deflate,
++++++++ inflate: options.inflate,
++++++++ connected: function(c) {
++++++++ // first handshake complete, call handler
++++++++ if(c.handshakes === 1) {
++++++++ tlsSocket.connected({
++++++++ id: socket.id,
++++++++ type: 'connect',
++++++++ bytesAvailable: c.data.length()
++++++++ });
++++++++ }
++++++++ },
++++++++ tlsDataReady: function(c) {
++++++++ // send TLS data over socket
++++++++ return socket.send(c.tlsData.getBytes());
++++++++ },
++++++++ dataReady: function(c) {
++++++++ // indicate application data is ready
++++++++ tlsSocket.data({
++++++++ id: socket.id,
++++++++ type: 'socketData',
++++++++ bytesAvailable: c.data.length()
++++++++ });
++++++++ },
++++++++ closed: function(c) {
++++++++ // close socket
++++++++ socket.close();
++++++++ },
++++++++ error: function(c, e) {
++++++++ // send error, close socket
++++++++ tlsSocket.error({
++++++++ id: socket.id,
++++++++ type: 'tlsError',
++++++++ message: e.message,
++++++++ bytesAvailable: 0,
++++++++ error: e
++++++++ });
++++++++ socket.close();
++++++++ }
++++++++ });
++++++++
++++++++ // handle doing handshake after connecting
++++++++ socket.connected = function(e) {
++++++++ c.handshake(options.sessionId);
++++++++ };
++++++++
++++++++ // handle closing TLS connection
++++++++ socket.closed = function(e) {
++++++++ if(c.open && c.handshaking) {
++++++++ // error
++++++++ tlsSocket.error({
++++++++ id: socket.id,
++++++++ type: 'ioError',
++++++++ message: 'Connection closed during handshake.',
++++++++ bytesAvailable: 0
++++++++ });
++++++++ }
++++++++ c.close();
++++++++
++++++++ // call socket handler
++++++++ tlsSocket.closed({
++++++++ id: socket.id,
++++++++ type: 'close',
++++++++ bytesAvailable: 0
++++++++ });
++++++++ };
++++++++
++++++++ // handle error on socket
++++++++ socket.error = function(e) {
++++++++ // error
++++++++ tlsSocket.error({
++++++++ id: socket.id,
++++++++ type: e.type,
++++++++ message: e.message,
++++++++ bytesAvailable: 0
++++++++ });
++++++++ c.close();
++++++++ };
++++++++
++++++++ // handle receiving raw TLS data from socket
++++++++ var _requiredBytes = 0;
++++++++ socket.data = function(e) {
++++++++ // drop data if connection not open
++++++++ if(!c.open) {
++++++++ socket.receive(e.bytesAvailable);
++++++++ } else {
++++++++ // only receive if there are enough bytes available to
++++++++ // process a record
++++++++ if(e.bytesAvailable >= _requiredBytes) {
++++++++ var count = Math.max(e.bytesAvailable, _requiredBytes);
++++++++ var data = socket.receive(count);
++++++++ if(data !== null) {
++++++++ _requiredBytes = c.process(data);
++++++++ }
++++++++ }
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Destroys this socket.
++++++++ */
++++++++ tlsSocket.destroy = function() {
++++++++ socket.destroy();
++++++++ };
++++++++
++++++++ /**
++++++++ * Sets this socket's TLS session cache. This should be called before
++++++++ * the socket is connected or after it is closed.
++++++++ *
++++++++ * The cache is an object mapping session IDs to internal opaque state.
++++++++ * An application might need to change the cache used by a particular
++++++++ * tlsSocket between connections if it accesses multiple TLS hosts.
++++++++ *
++++++++ * @param cache the session cache to use.
++++++++ */
++++++++ tlsSocket.setSessionCache = function(cache) {
++++++++ c.sessionCache = tls.createSessionCache(cache);
++++++++ };
++++++++
++++++++ /**
++++++++ * Connects this socket.
++++++++ *
++++++++ * @param options:
++++++++ * host: the host to connect to.
++++++++ * port: the port to connect to.
++++++++ * policyPort: the policy port to use (if non-default), 0 to
++++++++ * use the flash default.
++++++++ * policyUrl: the policy file URL to use (instead of port).
++++++++ */
++++++++ tlsSocket.connect = function(options) {
++++++++ socket.connect(options);
++++++++ };
++++++++
++++++++ /**
++++++++ * Closes this socket.
++++++++ */
++++++++ tlsSocket.close = function() {
++++++++ c.close();
++++++++ };
++++++++
++++++++ /**
++++++++ * Determines if the socket is connected or not.
++++++++ *
++++++++ * @return true if connected, false if not.
++++++++ */
++++++++ tlsSocket.isConnected = function() {
++++++++ return c.isConnected && socket.isConnected();
++++++++ };
++++++++
++++++++ /**
++++++++ * Writes bytes to this socket.
++++++++ *
++++++++ * @param bytes the bytes (as a string) to write.
++++++++ *
++++++++ * @return true on success, false on failure.
++++++++ */
++++++++ tlsSocket.send = function(bytes) {
++++++++ return c.prepare(bytes);
++++++++ };
++++++++
++++++++ /**
++++++++ * Reads bytes from this socket (non-blocking). Fewer than the number of
++++++++ * bytes requested may be read if enough bytes are not available.
++++++++ *
++++++++ * This method should be called from the data handler if there are enough
++++++++ * bytes available. To see how many bytes are available, check the
++++++++ * 'bytesAvailable' property on the event in the data handler or call the
++++++++ * bytesAvailable() function on the socket. If the browser is msie, then the
++++++++ * bytesAvailable() function should be used to avoid race conditions.
++++++++ * Otherwise, using the property on the data handler's event may be quicker.
++++++++ *
++++++++ * @param count the maximum number of bytes to read.
++++++++ *
++++++++ * @return the bytes read (as a string) or null on error.
++++++++ */
++++++++ tlsSocket.receive = function(count) {
++++++++ return c.data.getBytes(count);
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets the number of bytes available for receiving on the socket.
++++++++ *
++++++++ * @return the number of bytes available for receiving.
++++++++ */
++++++++ tlsSocket.bytesAvailable = function() {
++++++++ return c.data.length();
++++++++ };
++++++++
++++++++ return tlsSocket;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Utility functions for web applications.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2018 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++var baseN = require('./baseN');
++++++++
++++++++/* Utilities API */
++++++++var util = module.exports = forge.util = forge.util || {};
++++++++
++++++++// define setImmediate and nextTick
++++++++(function() {
++++++++ // use native nextTick (unless we're in webpack)
++++++++ // webpack (or better node-libs-browser polyfill) sets process.browser.
++++++++ // this way we can detect webpack properly
++++++++ if(typeof process !== 'undefined' && process.nextTick && !process.browser) {
++++++++ util.nextTick = process.nextTick;
++++++++ if(typeof setImmediate === 'function') {
++++++++ util.setImmediate = setImmediate;
++++++++ } else {
++++++++ // polyfill setImmediate with nextTick, older versions of node
++++++++ // (those w/o setImmediate) won't totally starve IO
++++++++ util.setImmediate = util.nextTick;
++++++++ }
++++++++ return;
++++++++ }
++++++++
++++++++ // polyfill nextTick with native setImmediate
++++++++ if(typeof setImmediate === 'function') {
++++++++ util.setImmediate = function() { return setImmediate.apply(undefined, arguments); };
++++++++ util.nextTick = function(callback) {
++++++++ return setImmediate(callback);
++++++++ };
++++++++ return;
++++++++ }
++++++++
++++++++ /* Note: A polyfill upgrade pattern is used here to allow combining
++++++++ polyfills. For example, MutationObserver is fast, but blocks UI updates,
++++++++ so it needs to allow UI updates periodically, so it falls back on
++++++++ postMessage or setTimeout. */
++++++++
++++++++ // polyfill with setTimeout
++++++++ util.setImmediate = function(callback) {
++++++++ setTimeout(callback, 0);
++++++++ };
++++++++
++++++++ // upgrade polyfill to use postMessage
++++++++ if(typeof window !== 'undefined' &&
++++++++ typeof window.postMessage === 'function') {
++++++++ var msg = 'forge.setImmediate';
++++++++ var callbacks = [];
++++++++ util.setImmediate = function(callback) {
++++++++ callbacks.push(callback);
++++++++ // only send message when one hasn't been sent in
++++++++ // the current turn of the event loop
++++++++ if(callbacks.length === 1) {
++++++++ window.postMessage(msg, '*');
++++++++ }
++++++++ };
++++++++ function handler(event) {
++++++++ if(event.source === window && event.data === msg) {
++++++++ event.stopPropagation();
++++++++ var copy = callbacks.slice();
++++++++ callbacks.length = 0;
++++++++ copy.forEach(function(callback) {
++++++++ callback();
++++++++ });
++++++++ }
++++++++ }
++++++++ window.addEventListener('message', handler, true);
++++++++ }
++++++++
++++++++ // upgrade polyfill to use MutationObserver
++++++++ if(typeof MutationObserver !== 'undefined') {
++++++++ // polyfill with MutationObserver
++++++++ var now = Date.now();
++++++++ var attr = true;
++++++++ var div = document.createElement('div');
++++++++ var callbacks = [];
++++++++ new MutationObserver(function() {
++++++++ var copy = callbacks.slice();
++++++++ callbacks.length = 0;
++++++++ copy.forEach(function(callback) {
++++++++ callback();
++++++++ });
++++++++ }).observe(div, {attributes: true});
++++++++ var oldSetImmediate = util.setImmediate;
++++++++ util.setImmediate = function(callback) {
++++++++ if(Date.now() - now > 15) {
++++++++ now = Date.now();
++++++++ oldSetImmediate(callback);
++++++++ } else {
++++++++ callbacks.push(callback);
++++++++ // only trigger observer when it hasn't been triggered in
++++++++ // the current turn of the event loop
++++++++ if(callbacks.length === 1) {
++++++++ div.setAttribute('a', attr = !attr);
++++++++ }
++++++++ }
++++++++ };
++++++++ }
++++++++
++++++++ util.nextTick = util.setImmediate;
++++++++})();
++++++++
++++++++// check if running under Node.js
++++++++util.isNodejs =
++++++++ typeof process !== 'undefined' && process.versions && process.versions.node;
++++++++
++++++++
++++++++// 'self' will also work in Web Workers (instance of WorkerGlobalScope) while
++++++++// it will point to `window` in the main thread.
++++++++// To remain compatible with older browsers, we fall back to 'window' if 'self'
++++++++// is not available.
++++++++util.globalScope = (function() {
++++++++ if(util.isNodejs) {
++++++++ return global;
++++++++ }
++++++++
++++++++ return typeof self === 'undefined' ? window : self;
++++++++})();
++++++++
++++++++// define isArray
++++++++util.isArray = Array.isArray || function(x) {
++++++++ return Object.prototype.toString.call(x) === '[object Array]';
++++++++};
++++++++
++++++++// define isArrayBuffer
++++++++util.isArrayBuffer = function(x) {
++++++++ return typeof ArrayBuffer !== 'undefined' && x instanceof ArrayBuffer;
++++++++};
++++++++
++++++++// define isArrayBufferView
++++++++util.isArrayBufferView = function(x) {
++++++++ return x && util.isArrayBuffer(x.buffer) && x.byteLength !== undefined;
++++++++};
++++++++
++++++++/**
++++++++ * Ensure a bits param is 8, 16, 24, or 32. Used to validate input for
++++++++ * algorithms where bit manipulation, JavaScript limitations, and/or algorithm
++++++++ * design only allow for byte operations of a limited size.
++++++++ *
++++++++ * @param n number of bits.
++++++++ *
++++++++ * Throw Error if n invalid.
++++++++ */
++++++++function _checkBitsParam(n) {
++++++++ if(!(n === 8 || n === 16 || n === 24 || n === 32)) {
++++++++ throw new Error('Only 8, 16, 24, or 32 bits supported: ' + n);
++++++++ }
++++++++}
++++++++
++++++++// TODO: set ByteBuffer to best available backing
++++++++util.ByteBuffer = ByteStringBuffer;
++++++++
++++++++/** Buffer w/BinaryString backing */
++++++++
++++++++/**
++++++++ * Constructor for a binary string backed byte buffer.
++++++++ *
++++++++ * @param [b] the bytes to wrap (either encoded as string, one byte per
++++++++ * character, or as an ArrayBuffer or Typed Array).
++++++++ */
++++++++function ByteStringBuffer(b) {
++++++++ // TODO: update to match DataBuffer API
++++++++
++++++++ // the data in this buffer
++++++++ this.data = '';
++++++++ // the pointer for reading from this buffer
++++++++ this.read = 0;
++++++++
++++++++ if(typeof b === 'string') {
++++++++ this.data = b;
++++++++ } else if(util.isArrayBuffer(b) || util.isArrayBufferView(b)) {
++++++++ if(typeof Buffer !== 'undefined' && b instanceof Buffer) {
++++++++ this.data = b.toString('binary');
++++++++ } else {
++++++++ // convert native buffer to forge buffer
++++++++ // FIXME: support native buffers internally instead
++++++++ var arr = new Uint8Array(b);
++++++++ try {
++++++++ this.data = String.fromCharCode.apply(null, arr);
++++++++ } catch(e) {
++++++++ for(var i = 0; i < arr.length; ++i) {
++++++++ this.putByte(arr[i]);
++++++++ }
++++++++ }
++++++++ }
++++++++ } else if(b instanceof ByteStringBuffer ||
++++++++ (typeof b === 'object' && typeof b.data === 'string' &&
++++++++ typeof b.read === 'number')) {
++++++++ // copy existing buffer
++++++++ this.data = b.data;
++++++++ this.read = b.read;
++++++++ }
++++++++
++++++++ // used for v8 optimization
++++++++ this._constructedStringLength = 0;
++++++++}
++++++++util.ByteStringBuffer = ByteStringBuffer;
++++++++
++++++++/* Note: This is an optimization for V8-based browsers. When V8 concatenates
++++++++ a string, the strings are only joined logically using a "cons string" or
++++++++ "constructed/concatenated string". These containers keep references to one
++++++++ another and can result in very large memory usage. For example, if a 2MB
++++++++ string is constructed by concatenating 4 bytes together at a time, the
++++++++ memory usage will be ~44MB; so ~22x increase. The strings are only joined
++++++++ together when an operation requiring their joining takes place, such as
++++++++ substr(). This function is called when adding data to this buffer to ensure
++++++++ these types of strings are periodically joined to reduce the memory
++++++++ footprint. */
++++++++var _MAX_CONSTRUCTED_STRING_LENGTH = 4096;
++++++++util.ByteStringBuffer.prototype._optimizeConstructedString = function(x) {
++++++++ this._constructedStringLength += x;
++++++++ if(this._constructedStringLength > _MAX_CONSTRUCTED_STRING_LENGTH) {
++++++++ // this substr() should cause the constructed string to join
++++++++ this.data.substr(0, 1);
++++++++ this._constructedStringLength = 0;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Gets the number of bytes in this buffer.
++++++++ *
++++++++ * @return the number of bytes in this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.length = function() {
++++++++ return this.data.length - this.read;
++++++++};
++++++++
++++++++/**
++++++++ * Gets whether or not this buffer is empty.
++++++++ *
++++++++ * @return true if this buffer is empty, false if not.
++++++++ */
++++++++util.ByteStringBuffer.prototype.isEmpty = function() {
++++++++ return this.length() <= 0;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte in this buffer.
++++++++ *
++++++++ * @param b the byte to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putByte = function(b) {
++++++++ return this.putBytes(String.fromCharCode(b));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte in this buffer N times.
++++++++ *
++++++++ * @param b the byte to put.
++++++++ * @param n the number of bytes of value b to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.fillWithByte = function(b, n) {
++++++++ b = String.fromCharCode(b);
++++++++ var d = this.data;
++++++++ while(n > 0) {
++++++++ if(n & 1) {
++++++++ d += b;
++++++++ }
++++++++ n >>>= 1;
++++++++ if(n > 0) {
++++++++ b += b;
++++++++ }
++++++++ }
++++++++ this.data = d;
++++++++ this._optimizeConstructedString(n);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts bytes in this buffer.
++++++++ *
++++++++ * @param bytes the bytes (as a binary encoded string) to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putBytes = function(bytes) {
++++++++ this.data += bytes;
++++++++ this._optimizeConstructedString(bytes.length);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a UTF-16 encoded string into this buffer.
++++++++ *
++++++++ * @param str the string to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putString = function(str) {
++++++++ return this.putBytes(util.encodeUtf8(str));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 16-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 16-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt16 = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 24-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 24-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt24 = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i >> 16 & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 32-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 32-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt32 = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i >> 24 & 0xFF) +
++++++++ String.fromCharCode(i >> 16 & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 16-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 16-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt16Le = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 24-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 24-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt24Le = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i >> 16 & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 32-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 32-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt32Le = function(i) {
++++++++ return this.putBytes(
++++++++ String.fromCharCode(i & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i >> 16 & 0xFF) +
++++++++ String.fromCharCode(i >> 24 & 0xFF));
++++++++};
++++++++
++++++++/**
++++++++ * Puts an n-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the n-bit integer.
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putInt = function(i, n) {
++++++++ _checkBitsParam(n);
++++++++ var bytes = '';
++++++++ do {
++++++++ n -= 8;
++++++++ bytes += String.fromCharCode((i >> n) & 0xFF);
++++++++ } while(n > 0);
++++++++ return this.putBytes(bytes);
++++++++};
++++++++
++++++++/**
++++++++ * Puts a signed n-bit integer in this buffer in big-endian order. Two's
++++++++ * complement representation is used.
++++++++ *
++++++++ * @param i the n-bit integer.
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putSignedInt = function(i, n) {
++++++++ // putInt checks n
++++++++ if(i < 0) {
++++++++ i += 2 << (n - 1);
++++++++ }
++++++++ return this.putInt(i, n);
++++++++};
++++++++
++++++++/**
++++++++ * Puts the given buffer into this buffer.
++++++++ *
++++++++ * @param buffer the buffer to put into this one.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.putBuffer = function(buffer) {
++++++++ return this.putBytes(buffer.getBytes());
++++++++};
++++++++
++++++++/**
++++++++ * Gets a byte from this buffer and advances the read pointer by 1.
++++++++ *
++++++++ * @return the byte.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getByte = function() {
++++++++ return this.data.charCodeAt(this.read++);
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint16 from this buffer in big-endian order and advances the read
++++++++ * pointer by 2.
++++++++ *
++++++++ * @return the uint16.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt16 = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) << 8 ^
++++++++ this.data.charCodeAt(this.read + 1));
++++++++ this.read += 2;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint24 from this buffer in big-endian order and advances the read
++++++++ * pointer by 3.
++++++++ *
++++++++ * @return the uint24.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt24 = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) << 16 ^
++++++++ this.data.charCodeAt(this.read + 1) << 8 ^
++++++++ this.data.charCodeAt(this.read + 2));
++++++++ this.read += 3;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint32 from this buffer in big-endian order and advances the read
++++++++ * pointer by 4.
++++++++ *
++++++++ * @return the word.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt32 = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) << 24 ^
++++++++ this.data.charCodeAt(this.read + 1) << 16 ^
++++++++ this.data.charCodeAt(this.read + 2) << 8 ^
++++++++ this.data.charCodeAt(this.read + 3));
++++++++ this.read += 4;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint16 from this buffer in little-endian order and advances the read
++++++++ * pointer by 2.
++++++++ *
++++++++ * @return the uint16.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt16Le = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) ^
++++++++ this.data.charCodeAt(this.read + 1) << 8);
++++++++ this.read += 2;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint24 from this buffer in little-endian order and advances the read
++++++++ * pointer by 3.
++++++++ *
++++++++ * @return the uint24.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt24Le = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) ^
++++++++ this.data.charCodeAt(this.read + 1) << 8 ^
++++++++ this.data.charCodeAt(this.read + 2) << 16);
++++++++ this.read += 3;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint32 from this buffer in little-endian order and advances the read
++++++++ * pointer by 4.
++++++++ *
++++++++ * @return the word.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt32Le = function() {
++++++++ var rval = (
++++++++ this.data.charCodeAt(this.read) ^
++++++++ this.data.charCodeAt(this.read + 1) << 8 ^
++++++++ this.data.charCodeAt(this.read + 2) << 16 ^
++++++++ this.data.charCodeAt(this.read + 3) << 24);
++++++++ this.read += 4;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets an n-bit integer from this buffer in big-endian order and advances the
++++++++ * read pointer by ceil(n/8).
++++++++ *
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return the integer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getInt = function(n) {
++++++++ _checkBitsParam(n);
++++++++ var rval = 0;
++++++++ do {
++++++++ // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits.
++++++++ rval = (rval << 8) + this.data.charCodeAt(this.read++);
++++++++ n -= 8;
++++++++ } while(n > 0);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a signed n-bit integer from this buffer in big-endian order, using
++++++++ * two's complement, and advances the read pointer by n/8.
++++++++ *
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return the integer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getSignedInt = function(n) {
++++++++ // getInt checks n
++++++++ var x = this.getInt(n);
++++++++ var max = 2 << (n - 2);
++++++++ if(x >= max) {
++++++++ x -= max << 1;
++++++++ }
++++++++ return x;
++++++++};
++++++++
++++++++/**
++++++++ * Reads bytes out as a binary encoded string and clears them from the
++++++++ * buffer. Note that the resulting string is binary encoded (in node.js this
++++++++ * encoding is referred to as `binary`, it is *not* `utf8`).
++++++++ *
++++++++ * @param count the number of bytes to read, undefined or null for all.
++++++++ *
++++++++ * @return a binary encoded string of bytes.
++++++++ */
++++++++util.ByteStringBuffer.prototype.getBytes = function(count) {
++++++++ var rval;
++++++++ if(count) {
++++++++ // read count bytes
++++++++ count = Math.min(this.length(), count);
++++++++ rval = this.data.slice(this.read, this.read + count);
++++++++ this.read += count;
++++++++ } else if(count === 0) {
++++++++ rval = '';
++++++++ } else {
++++++++ // read all bytes, optimize to only copy when needed
++++++++ rval = (this.read === 0) ? this.data : this.data.slice(this.read);
++++++++ this.clear();
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a binary encoded string of the bytes from this buffer without
++++++++ * modifying the read pointer.
++++++++ *
++++++++ * @param count the number of bytes to get, omit to get all.
++++++++ *
++++++++ * @return a string full of binary encoded characters.
++++++++ */
++++++++util.ByteStringBuffer.prototype.bytes = function(count) {
++++++++ return (typeof(count) === 'undefined' ?
++++++++ this.data.slice(this.read) :
++++++++ this.data.slice(this.read, this.read + count));
++++++++};
++++++++
++++++++/**
++++++++ * Gets a byte at the given index without modifying the read pointer.
++++++++ *
++++++++ * @param i the byte index.
++++++++ *
++++++++ * @return the byte.
++++++++ */
++++++++util.ByteStringBuffer.prototype.at = function(i) {
++++++++ return this.data.charCodeAt(this.read + i);
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte at the given index without modifying the read pointer.
++++++++ *
++++++++ * @param i the byte index.
++++++++ * @param b the byte to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.setAt = function(i, b) {
++++++++ this.data = this.data.substr(0, this.read + i) +
++++++++ String.fromCharCode(b) +
++++++++ this.data.substr(this.read + i + 1);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the last byte without modifying the read pointer.
++++++++ *
++++++++ * @return the last byte.
++++++++ */
++++++++util.ByteStringBuffer.prototype.last = function() {
++++++++ return this.data.charCodeAt(this.data.length - 1);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a copy of this buffer.
++++++++ *
++++++++ * @return the copy.
++++++++ */
++++++++util.ByteStringBuffer.prototype.copy = function() {
++++++++ var c = util.createBuffer(this.data);
++++++++ c.read = this.read;
++++++++ return c;
++++++++};
++++++++
++++++++/**
++++++++ * Compacts this buffer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.compact = function() {
++++++++ if(this.read > 0) {
++++++++ this.data = this.data.slice(this.read);
++++++++ this.read = 0;
++++++++ }
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Clears this buffer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.clear = function() {
++++++++ this.data = '';
++++++++ this.read = 0;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Shortens this buffer by triming bytes off of the end of this buffer.
++++++++ *
++++++++ * @param count the number of bytes to trim off.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.ByteStringBuffer.prototype.truncate = function(count) {
++++++++ var len = Math.max(0, this.length() - count);
++++++++ this.data = this.data.substr(this.read, len);
++++++++ this.read = 0;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Converts this buffer to a hexadecimal string.
++++++++ *
++++++++ * @return a hexadecimal string.
++++++++ */
++++++++util.ByteStringBuffer.prototype.toHex = function() {
++++++++ var rval = '';
++++++++ for(var i = this.read; i < this.data.length; ++i) {
++++++++ var b = this.data.charCodeAt(i);
++++++++ if(b < 16) {
++++++++ rval += '0';
++++++++ }
++++++++ rval += b.toString(16);
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts this buffer to a UTF-16 string (standard JavaScript string).
++++++++ *
++++++++ * @return a UTF-16 string.
++++++++ */
++++++++util.ByteStringBuffer.prototype.toString = function() {
++++++++ return util.decodeUtf8(this.bytes());
++++++++};
++++++++
++++++++/** End Buffer w/BinaryString backing */
++++++++
++++++++/** Buffer w/UInt8Array backing */
++++++++
++++++++/**
++++++++ * FIXME: Experimental. Do not use yet.
++++++++ *
++++++++ * Constructor for an ArrayBuffer-backed byte buffer.
++++++++ *
++++++++ * The buffer may be constructed from a string, an ArrayBuffer, DataView, or a
++++++++ * TypedArray.
++++++++ *
++++++++ * If a string is given, its encoding should be provided as an option,
++++++++ * otherwise it will default to 'binary'. A 'binary' string is encoded such
++++++++ * that each character is one byte in length and size.
++++++++ *
++++++++ * If an ArrayBuffer, DataView, or TypedArray is given, it will be used
++++++++ * *directly* without any copying. Note that, if a write to the buffer requires
++++++++ * more space, the buffer will allocate a new backing ArrayBuffer to
++++++++ * accommodate. The starting read and write offsets for the buffer may be
++++++++ * given as options.
++++++++ *
++++++++ * @param [b] the initial bytes for this buffer.
++++++++ * @param options the options to use:
++++++++ * [readOffset] the starting read offset to use (default: 0).
++++++++ * [writeOffset] the starting write offset to use (default: the
++++++++ * length of the first parameter).
++++++++ * [growSize] the minimum amount, in bytes, to grow the buffer by to
++++++++ * accommodate writes (default: 1024).
++++++++ * [encoding] the encoding ('binary', 'utf8', 'utf16', 'hex') for the
++++++++ * first parameter, if it is a string (default: 'binary').
++++++++ */
++++++++function DataBuffer(b, options) {
++++++++ // default options
++++++++ options = options || {};
++++++++
++++++++ // pointers for read from/write to buffer
++++++++ this.read = options.readOffset || 0;
++++++++ this.growSize = options.growSize || 1024;
++++++++
++++++++ var isArrayBuffer = util.isArrayBuffer(b);
++++++++ var isArrayBufferView = util.isArrayBufferView(b);
++++++++ if(isArrayBuffer || isArrayBufferView) {
++++++++ // use ArrayBuffer directly
++++++++ if(isArrayBuffer) {
++++++++ this.data = new DataView(b);
++++++++ } else {
++++++++ // TODO: adjust read/write offset based on the type of view
++++++++ // or specify that this must be done in the options ... that the
++++++++ // offsets are byte-based
++++++++ this.data = new DataView(b.buffer, b.byteOffset, b.byteLength);
++++++++ }
++++++++ this.write = ('writeOffset' in options ?
++++++++ options.writeOffset : this.data.byteLength);
++++++++ return;
++++++++ }
++++++++
++++++++ // initialize to empty array buffer and add any given bytes using putBytes
++++++++ this.data = new DataView(new ArrayBuffer(0));
++++++++ this.write = 0;
++++++++
++++++++ if(b !== null && b !== undefined) {
++++++++ this.putBytes(b);
++++++++ }
++++++++
++++++++ if('writeOffset' in options) {
++++++++ this.write = options.writeOffset;
++++++++ }
++++++++}
++++++++util.DataBuffer = DataBuffer;
++++++++
++++++++/**
++++++++ * Gets the number of bytes in this buffer.
++++++++ *
++++++++ * @return the number of bytes in this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.length = function() {
++++++++ return this.write - this.read;
++++++++};
++++++++
++++++++/**
++++++++ * Gets whether or not this buffer is empty.
++++++++ *
++++++++ * @return true if this buffer is empty, false if not.
++++++++ */
++++++++util.DataBuffer.prototype.isEmpty = function() {
++++++++ return this.length() <= 0;
++++++++};
++++++++
++++++++/**
++++++++ * Ensures this buffer has enough empty space to accommodate the given number
++++++++ * of bytes. An optional parameter may be given that indicates a minimum
++++++++ * amount to grow the buffer if necessary. If the parameter is not given,
++++++++ * the buffer will be grown by some previously-specified default amount
++++++++ * or heuristic.
++++++++ *
++++++++ * @param amount the number of bytes to accommodate.
++++++++ * @param [growSize] the minimum amount, in bytes, to grow the buffer by if
++++++++ * necessary.
++++++++ */
++++++++util.DataBuffer.prototype.accommodate = function(amount, growSize) {
++++++++ if(this.length() >= amount) {
++++++++ return this;
++++++++ }
++++++++ growSize = Math.max(growSize || this.growSize, amount);
++++++++
++++++++ // grow buffer
++++++++ var src = new Uint8Array(
++++++++ this.data.buffer, this.data.byteOffset, this.data.byteLength);
++++++++ var dst = new Uint8Array(this.length() + growSize);
++++++++ dst.set(src);
++++++++ this.data = new DataView(dst.buffer);
++++++++
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte in this buffer.
++++++++ *
++++++++ * @param b the byte to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putByte = function(b) {
++++++++ this.accommodate(1);
++++++++ this.data.setUint8(this.write++, b);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte in this buffer N times.
++++++++ *
++++++++ * @param b the byte to put.
++++++++ * @param n the number of bytes of value b to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.fillWithByte = function(b, n) {
++++++++ this.accommodate(n);
++++++++ for(var i = 0; i < n; ++i) {
++++++++ this.data.setUint8(b);
++++++++ }
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts bytes in this buffer. The bytes may be given as a string, an
++++++++ * ArrayBuffer, a DataView, or a TypedArray.
++++++++ *
++++++++ * @param bytes the bytes to put.
++++++++ * @param [encoding] the encoding for the first parameter ('binary', 'utf8',
++++++++ * 'utf16', 'hex'), if it is a string (default: 'binary').
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putBytes = function(bytes, encoding) {
++++++++ if(util.isArrayBufferView(bytes)) {
++++++++ var src = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
++++++++ var len = src.byteLength - src.byteOffset;
++++++++ this.accommodate(len);
++++++++ var dst = new Uint8Array(this.data.buffer, this.write);
++++++++ dst.set(src);
++++++++ this.write += len;
++++++++ return this;
++++++++ }
++++++++
++++++++ if(util.isArrayBuffer(bytes)) {
++++++++ var src = new Uint8Array(bytes);
++++++++ this.accommodate(src.byteLength);
++++++++ var dst = new Uint8Array(this.data.buffer);
++++++++ dst.set(src, this.write);
++++++++ this.write += src.byteLength;
++++++++ return this;
++++++++ }
++++++++
++++++++ // bytes is a util.DataBuffer or equivalent
++++++++ if(bytes instanceof util.DataBuffer ||
++++++++ (typeof bytes === 'object' &&
++++++++ typeof bytes.read === 'number' && typeof bytes.write === 'number' &&
++++++++ util.isArrayBufferView(bytes.data))) {
++++++++ var src = new Uint8Array(bytes.data.byteLength, bytes.read, bytes.length());
++++++++ this.accommodate(src.byteLength);
++++++++ var dst = new Uint8Array(bytes.data.byteLength, this.write);
++++++++ dst.set(src);
++++++++ this.write += src.byteLength;
++++++++ return this;
++++++++ }
++++++++
++++++++ if(bytes instanceof util.ByteStringBuffer) {
++++++++ // copy binary string and process as the same as a string parameter below
++++++++ bytes = bytes.data;
++++++++ encoding = 'binary';
++++++++ }
++++++++
++++++++ // string conversion
++++++++ encoding = encoding || 'binary';
++++++++ if(typeof bytes === 'string') {
++++++++ var view;
++++++++
++++++++ // decode from string
++++++++ if(encoding === 'hex') {
++++++++ this.accommodate(Math.ceil(bytes.length / 2));
++++++++ view = new Uint8Array(this.data.buffer, this.write);
++++++++ this.write += util.binary.hex.decode(bytes, view, this.write);
++++++++ return this;
++++++++ }
++++++++ if(encoding === 'base64') {
++++++++ this.accommodate(Math.ceil(bytes.length / 4) * 3);
++++++++ view = new Uint8Array(this.data.buffer, this.write);
++++++++ this.write += util.binary.base64.decode(bytes, view, this.write);
++++++++ return this;
++++++++ }
++++++++
++++++++ // encode text as UTF-8 bytes
++++++++ if(encoding === 'utf8') {
++++++++ // encode as UTF-8 then decode string as raw binary
++++++++ bytes = util.encodeUtf8(bytes);
++++++++ encoding = 'binary';
++++++++ }
++++++++
++++++++ // decode string as raw binary
++++++++ if(encoding === 'binary' || encoding === 'raw') {
++++++++ // one byte per character
++++++++ this.accommodate(bytes.length);
++++++++ view = new Uint8Array(this.data.buffer, this.write);
++++++++ this.write += util.binary.raw.decode(view);
++++++++ return this;
++++++++ }
++++++++
++++++++ // encode text as UTF-16 bytes
++++++++ if(encoding === 'utf16') {
++++++++ // two bytes per character
++++++++ this.accommodate(bytes.length * 2);
++++++++ view = new Uint16Array(this.data.buffer, this.write);
++++++++ this.write += util.text.utf16.encode(view);
++++++++ return this;
++++++++ }
++++++++
++++++++ throw new Error('Invalid encoding: ' + encoding);
++++++++ }
++++++++
++++++++ throw Error('Invalid parameter: ' + bytes);
++++++++};
++++++++
++++++++/**
++++++++ * Puts the given buffer into this buffer.
++++++++ *
++++++++ * @param buffer the buffer to put into this one.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putBuffer = function(buffer) {
++++++++ this.putBytes(buffer);
++++++++ buffer.clear();
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a string into this buffer.
++++++++ *
++++++++ * @param str the string to put.
++++++++ * @param [encoding] the encoding for the string (default: 'utf16').
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putString = function(str) {
++++++++ return this.putBytes(str, 'utf16');
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 16-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 16-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt16 = function(i) {
++++++++ this.accommodate(2);
++++++++ this.data.setInt16(this.write, i);
++++++++ this.write += 2;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 24-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 24-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt24 = function(i) {
++++++++ this.accommodate(3);
++++++++ this.data.setInt16(this.write, i >> 8 & 0xFFFF);
++++++++ this.data.setInt8(this.write, i >> 16 & 0xFF);
++++++++ this.write += 3;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 32-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the 32-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt32 = function(i) {
++++++++ this.accommodate(4);
++++++++ this.data.setInt32(this.write, i);
++++++++ this.write += 4;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 16-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 16-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt16Le = function(i) {
++++++++ this.accommodate(2);
++++++++ this.data.setInt16(this.write, i, true);
++++++++ this.write += 2;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 24-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 24-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt24Le = function(i) {
++++++++ this.accommodate(3);
++++++++ this.data.setInt8(this.write, i >> 16 & 0xFF);
++++++++ this.data.setInt16(this.write, i >> 8 & 0xFFFF, true);
++++++++ this.write += 3;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a 32-bit integer in this buffer in little-endian order.
++++++++ *
++++++++ * @param i the 32-bit integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt32Le = function(i) {
++++++++ this.accommodate(4);
++++++++ this.data.setInt32(this.write, i, true);
++++++++ this.write += 4;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts an n-bit integer in this buffer in big-endian order.
++++++++ *
++++++++ * @param i the n-bit integer.
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putInt = function(i, n) {
++++++++ _checkBitsParam(n);
++++++++ this.accommodate(n / 8);
++++++++ do {
++++++++ n -= 8;
++++++++ this.data.setInt8(this.write++, (i >> n) & 0xFF);
++++++++ } while(n > 0);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Puts a signed n-bit integer in this buffer in big-endian order. Two's
++++++++ * complement representation is used.
++++++++ *
++++++++ * @param i the n-bit integer.
++++++++ * @param n the number of bits in the integer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.putSignedInt = function(i, n) {
++++++++ _checkBitsParam(n);
++++++++ this.accommodate(n / 8);
++++++++ if(i < 0) {
++++++++ i += 2 << (n - 1);
++++++++ }
++++++++ return this.putInt(i, n);
++++++++};
++++++++
++++++++/**
++++++++ * Gets a byte from this buffer and advances the read pointer by 1.
++++++++ *
++++++++ * @return the byte.
++++++++ */
++++++++util.DataBuffer.prototype.getByte = function() {
++++++++ return this.data.getInt8(this.read++);
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint16 from this buffer in big-endian order and advances the read
++++++++ * pointer by 2.
++++++++ *
++++++++ * @return the uint16.
++++++++ */
++++++++util.DataBuffer.prototype.getInt16 = function() {
++++++++ var rval = this.data.getInt16(this.read);
++++++++ this.read += 2;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint24 from this buffer in big-endian order and advances the read
++++++++ * pointer by 3.
++++++++ *
++++++++ * @return the uint24.
++++++++ */
++++++++util.DataBuffer.prototype.getInt24 = function() {
++++++++ var rval = (
++++++++ this.data.getInt16(this.read) << 8 ^
++++++++ this.data.getInt8(this.read + 2));
++++++++ this.read += 3;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint32 from this buffer in big-endian order and advances the read
++++++++ * pointer by 4.
++++++++ *
++++++++ * @return the word.
++++++++ */
++++++++util.DataBuffer.prototype.getInt32 = function() {
++++++++ var rval = this.data.getInt32(this.read);
++++++++ this.read += 4;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint16 from this buffer in little-endian order and advances the read
++++++++ * pointer by 2.
++++++++ *
++++++++ * @return the uint16.
++++++++ */
++++++++util.DataBuffer.prototype.getInt16Le = function() {
++++++++ var rval = this.data.getInt16(this.read, true);
++++++++ this.read += 2;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint24 from this buffer in little-endian order and advances the read
++++++++ * pointer by 3.
++++++++ *
++++++++ * @return the uint24.
++++++++ */
++++++++util.DataBuffer.prototype.getInt24Le = function() {
++++++++ var rval = (
++++++++ this.data.getInt8(this.read) ^
++++++++ this.data.getInt16(this.read + 1, true) << 8);
++++++++ this.read += 3;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a uint32 from this buffer in little-endian order and advances the read
++++++++ * pointer by 4.
++++++++ *
++++++++ * @return the word.
++++++++ */
++++++++util.DataBuffer.prototype.getInt32Le = function() {
++++++++ var rval = this.data.getInt32(this.read, true);
++++++++ this.read += 4;
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets an n-bit integer from this buffer in big-endian order and advances the
++++++++ * read pointer by n/8.
++++++++ *
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return the integer.
++++++++ */
++++++++util.DataBuffer.prototype.getInt = function(n) {
++++++++ _checkBitsParam(n);
++++++++ var rval = 0;
++++++++ do {
++++++++ // TODO: Use (rval * 0x100) if adding support for 33 to 53 bits.
++++++++ rval = (rval << 8) + this.data.getInt8(this.read++);
++++++++ n -= 8;
++++++++ } while(n > 0);
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a signed n-bit integer from this buffer in big-endian order, using
++++++++ * two's complement, and advances the read pointer by n/8.
++++++++ *
++++++++ * @param n the number of bits in the integer (8, 16, 24, or 32).
++++++++ *
++++++++ * @return the integer.
++++++++ */
++++++++util.DataBuffer.prototype.getSignedInt = function(n) {
++++++++ // getInt checks n
++++++++ var x = this.getInt(n);
++++++++ var max = 2 << (n - 2);
++++++++ if(x >= max) {
++++++++ x -= max << 1;
++++++++ }
++++++++ return x;
++++++++};
++++++++
++++++++/**
++++++++ * Reads bytes out as a binary encoded string and clears them from the
++++++++ * buffer.
++++++++ *
++++++++ * @param count the number of bytes to read, undefined or null for all.
++++++++ *
++++++++ * @return a binary encoded string of bytes.
++++++++ */
++++++++util.DataBuffer.prototype.getBytes = function(count) {
++++++++ // TODO: deprecate this method, it is poorly named and
++++++++ // this.toString('binary') replaces it
++++++++ // add a toTypedArray()/toArrayBuffer() function
++++++++ var rval;
++++++++ if(count) {
++++++++ // read count bytes
++++++++ count = Math.min(this.length(), count);
++++++++ rval = this.data.slice(this.read, this.read + count);
++++++++ this.read += count;
++++++++ } else if(count === 0) {
++++++++ rval = '';
++++++++ } else {
++++++++ // read all bytes, optimize to only copy when needed
++++++++ rval = (this.read === 0) ? this.data : this.data.slice(this.read);
++++++++ this.clear();
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets a binary encoded string of the bytes from this buffer without
++++++++ * modifying the read pointer.
++++++++ *
++++++++ * @param count the number of bytes to get, omit to get all.
++++++++ *
++++++++ * @return a string full of binary encoded characters.
++++++++ */
++++++++util.DataBuffer.prototype.bytes = function(count) {
++++++++ // TODO: deprecate this method, it is poorly named, add "getString()"
++++++++ return (typeof(count) === 'undefined' ?
++++++++ this.data.slice(this.read) :
++++++++ this.data.slice(this.read, this.read + count));
++++++++};
++++++++
++++++++/**
++++++++ * Gets a byte at the given index without modifying the read pointer.
++++++++ *
++++++++ * @param i the byte index.
++++++++ *
++++++++ * @return the byte.
++++++++ */
++++++++util.DataBuffer.prototype.at = function(i) {
++++++++ return this.data.getUint8(this.read + i);
++++++++};
++++++++
++++++++/**
++++++++ * Puts a byte at the given index without modifying the read pointer.
++++++++ *
++++++++ * @param i the byte index.
++++++++ * @param b the byte to put.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.setAt = function(i, b) {
++++++++ this.data.setUint8(i, b);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the last byte without modifying the read pointer.
++++++++ *
++++++++ * @return the last byte.
++++++++ */
++++++++util.DataBuffer.prototype.last = function() {
++++++++ return this.data.getUint8(this.write - 1);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a copy of this buffer.
++++++++ *
++++++++ * @return the copy.
++++++++ */
++++++++util.DataBuffer.prototype.copy = function() {
++++++++ return new util.DataBuffer(this);
++++++++};
++++++++
++++++++/**
++++++++ * Compacts this buffer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.compact = function() {
++++++++ if(this.read > 0) {
++++++++ var src = new Uint8Array(this.data.buffer, this.read);
++++++++ var dst = new Uint8Array(src.byteLength);
++++++++ dst.set(src);
++++++++ this.data = new DataView(dst);
++++++++ this.write -= this.read;
++++++++ this.read = 0;
++++++++ }
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Clears this buffer.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.clear = function() {
++++++++ this.data = new DataView(new ArrayBuffer(0));
++++++++ this.read = this.write = 0;
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Shortens this buffer by triming bytes off of the end of this buffer.
++++++++ *
++++++++ * @param count the number of bytes to trim off.
++++++++ *
++++++++ * @return this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.truncate = function(count) {
++++++++ this.write = Math.max(0, this.length() - count);
++++++++ this.read = Math.min(this.read, this.write);
++++++++ return this;
++++++++};
++++++++
++++++++/**
++++++++ * Converts this buffer to a hexadecimal string.
++++++++ *
++++++++ * @return a hexadecimal string.
++++++++ */
++++++++util.DataBuffer.prototype.toHex = function() {
++++++++ var rval = '';
++++++++ for(var i = this.read; i < this.data.byteLength; ++i) {
++++++++ var b = this.data.getUint8(i);
++++++++ if(b < 16) {
++++++++ rval += '0';
++++++++ }
++++++++ rval += b.toString(16);
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts this buffer to a string, using the given encoding. If no
++++++++ * encoding is given, 'utf8' (UTF-8) is used.
++++++++ *
++++++++ * @param [encoding] the encoding to use: 'binary', 'utf8', 'utf16', 'hex',
++++++++ * 'base64' (default: 'utf8').
++++++++ *
++++++++ * @return a string representation of the bytes in this buffer.
++++++++ */
++++++++util.DataBuffer.prototype.toString = function(encoding) {
++++++++ var view = new Uint8Array(this.data, this.read, this.length());
++++++++ encoding = encoding || 'utf8';
++++++++
++++++++ // encode to string
++++++++ if(encoding === 'binary' || encoding === 'raw') {
++++++++ return util.binary.raw.encode(view);
++++++++ }
++++++++ if(encoding === 'hex') {
++++++++ return util.binary.hex.encode(view);
++++++++ }
++++++++ if(encoding === 'base64') {
++++++++ return util.binary.base64.encode(view);
++++++++ }
++++++++
++++++++ // decode to text
++++++++ if(encoding === 'utf8') {
++++++++ return util.text.utf8.decode(view);
++++++++ }
++++++++ if(encoding === 'utf16') {
++++++++ return util.text.utf16.decode(view);
++++++++ }
++++++++
++++++++ throw new Error('Invalid encoding: ' + encoding);
++++++++};
++++++++
++++++++/** End Buffer w/UInt8Array backing */
++++++++
++++++++/**
++++++++ * Creates a buffer that stores bytes. A value may be given to populate the
++++++++ * buffer with data. This value can either be string of encoded bytes or a
++++++++ * regular string of characters. When passing a string of binary encoded
++++++++ * bytes, the encoding `raw` should be given. This is also the default. When
++++++++ * passing a string of characters, the encoding `utf8` should be given.
++++++++ *
++++++++ * @param [input] a string with encoded bytes to store in the buffer.
++++++++ * @param [encoding] (default: 'raw', other: 'utf8').
++++++++ */
++++++++util.createBuffer = function(input, encoding) {
++++++++ // TODO: deprecate, use new ByteBuffer() instead
++++++++ encoding = encoding || 'raw';
++++++++ if(input !== undefined && encoding === 'utf8') {
++++++++ input = util.encodeUtf8(input);
++++++++ }
++++++++ return new util.ByteBuffer(input);
++++++++};
++++++++
++++++++/**
++++++++ * Fills a string with a particular value. If you want the string to be a byte
++++++++ * string, pass in String.fromCharCode(theByte).
++++++++ *
++++++++ * @param c the character to fill the string with, use String.fromCharCode
++++++++ * to fill the string with a byte value.
++++++++ * @param n the number of characters of value c to fill with.
++++++++ *
++++++++ * @return the filled string.
++++++++ */
++++++++util.fillString = function(c, n) {
++++++++ var s = '';
++++++++ while(n > 0) {
++++++++ if(n & 1) {
++++++++ s += c;
++++++++ }
++++++++ n >>>= 1;
++++++++ if(n > 0) {
++++++++ c += c;
++++++++ }
++++++++ }
++++++++ return s;
++++++++};
++++++++
++++++++/**
++++++++ * Performs a per byte XOR between two byte strings and returns the result as a
++++++++ * string of bytes.
++++++++ *
++++++++ * @param s1 first string of bytes.
++++++++ * @param s2 second string of bytes.
++++++++ * @param n the number of bytes to XOR.
++++++++ *
++++++++ * @return the XOR'd result.
++++++++ */
++++++++util.xorBytes = function(s1, s2, n) {
++++++++ var s3 = '';
++++++++ var b = '';
++++++++ var t = '';
++++++++ var i = 0;
++++++++ var c = 0;
++++++++ for(; n > 0; --n, ++i) {
++++++++ b = s1.charCodeAt(i) ^ s2.charCodeAt(i);
++++++++ if(c >= 10) {
++++++++ s3 += t;
++++++++ t = '';
++++++++ c = 0;
++++++++ }
++++++++ t += String.fromCharCode(b);
++++++++ ++c;
++++++++ }
++++++++ s3 += t;
++++++++ return s3;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a hex string into a 'binary' encoded string of bytes.
++++++++ *
++++++++ * @param hex the hexadecimal string to convert.
++++++++ *
++++++++ * @return the binary-encoded string of bytes.
++++++++ */
++++++++util.hexToBytes = function(hex) {
++++++++ // TODO: deprecate: "Deprecated. Use util.binary.hex.decode instead."
++++++++ var rval = '';
++++++++ var i = 0;
++++++++ if(hex.length & 1 == 1) {
++++++++ // odd number of characters, convert first character alone
++++++++ i = 1;
++++++++ rval += String.fromCharCode(parseInt(hex[0], 16));
++++++++ }
++++++++ // convert 2 characters (1 byte) at a time
++++++++ for(; i < hex.length; i += 2) {
++++++++ rval += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
++++++++ }
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a 'binary' encoded string of bytes to hex.
++++++++ *
++++++++ * @param bytes the byte string to convert.
++++++++ *
++++++++ * @return the string of hexadecimal characters.
++++++++ */
++++++++util.bytesToHex = function(bytes) {
++++++++ // TODO: deprecate: "Deprecated. Use util.binary.hex.encode instead."
++++++++ return util.createBuffer(bytes).toHex();
++++++++};
++++++++
++++++++/**
++++++++ * Converts an 32-bit integer to 4-big-endian byte string.
++++++++ *
++++++++ * @param i the integer.
++++++++ *
++++++++ * @return the byte string.
++++++++ */
++++++++util.int32ToBytes = function(i) {
++++++++ return (
++++++++ String.fromCharCode(i >> 24 & 0xFF) +
++++++++ String.fromCharCode(i >> 16 & 0xFF) +
++++++++ String.fromCharCode(i >> 8 & 0xFF) +
++++++++ String.fromCharCode(i & 0xFF));
++++++++};
++++++++
++++++++// base64 characters, reverse mapping
++++++++var _base64 =
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
++++++++var _base64Idx = [
++++++++/*43 -43 = 0*/
++++++++/*'+', 1, 2, 3,'/' */
++++++++ 62, -1, -1, -1, 63,
++++++++
++++++++/*'0','1','2','3','4','5','6','7','8','9' */
++++++++ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
++++++++
++++++++/*15, 16, 17,'=', 19, 20, 21 */
++++++++ -1, -1, -1, 64, -1, -1, -1,
++++++++
++++++++/*65 - 43 = 22*/
++++++++/*'A','B','C','D','E','F','G','H','I','J','K','L','M', */
++++++++ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
++++++++
++++++++/*'N','O','P','Q','R','S','T','U','V','W','X','Y','Z' */
++++++++ 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
++++++++
++++++++/*91 - 43 = 48 */
++++++++/*48, 49, 50, 51, 52, 53 */
++++++++ -1, -1, -1, -1, -1, -1,
++++++++
++++++++/*97 - 43 = 54*/
++++++++/*'a','b','c','d','e','f','g','h','i','j','k','l','m' */
++++++++ 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
++++++++
++++++++/*'n','o','p','q','r','s','t','u','v','w','x','y','z' */
++++++++ 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
++++++++];
++++++++
++++++++// base58 characters (Bitcoin alphabet)
++++++++var _base58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
++++++++
++++++++/**
++++++++ * Base64 encodes a 'binary' encoded string of bytes.
++++++++ *
++++++++ * @param input the binary encoded string of bytes to base64-encode.
++++++++ * @param maxline the maximum number of encoded characters per line to use,
++++++++ * defaults to none.
++++++++ *
++++++++ * @return the base64-encoded output.
++++++++ */
++++++++util.encode64 = function(input, maxline) {
++++++++ // TODO: deprecate: "Deprecated. Use util.binary.base64.encode instead."
++++++++ var line = '';
++++++++ var output = '';
++++++++ var chr1, chr2, chr3;
++++++++ var i = 0;
++++++++ while(i < input.length) {
++++++++ chr1 = input.charCodeAt(i++);
++++++++ chr2 = input.charCodeAt(i++);
++++++++ chr3 = input.charCodeAt(i++);
++++++++
++++++++ // encode 4 character group
++++++++ line += _base64.charAt(chr1 >> 2);
++++++++ line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4));
++++++++ if(isNaN(chr2)) {
++++++++ line += '==';
++++++++ } else {
++++++++ line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6));
++++++++ line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
++++++++ }
++++++++
++++++++ if(maxline && line.length > maxline) {
++++++++ output += line.substr(0, maxline) + '\r\n';
++++++++ line = line.substr(maxline);
++++++++ }
++++++++ }
++++++++ output += line;
++++++++ return output;
++++++++};
++++++++
++++++++/**
++++++++ * Base64 decodes a string into a 'binary' encoded string of bytes.
++++++++ *
++++++++ * @param input the base64-encoded input.
++++++++ *
++++++++ * @return the binary encoded string.
++++++++ */
++++++++util.decode64 = function(input) {
++++++++ // TODO: deprecate: "Deprecated. Use util.binary.base64.decode instead."
++++++++
++++++++ // remove all non-base64 characters
++++++++ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
++++++++
++++++++ var output = '';
++++++++ var enc1, enc2, enc3, enc4;
++++++++ var i = 0;
++++++++
++++++++ while(i < input.length) {
++++++++ enc1 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc2 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc3 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc4 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++
++++++++ output += String.fromCharCode((enc1 << 2) | (enc2 >> 4));
++++++++ if(enc3 !== 64) {
++++++++ // decoded at least 2 bytes
++++++++ output += String.fromCharCode(((enc2 & 15) << 4) | (enc3 >> 2));
++++++++ if(enc4 !== 64) {
++++++++ // decoded 3 bytes
++++++++ output += String.fromCharCode(((enc3 & 3) << 6) | enc4);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return output;
++++++++};
++++++++
++++++++/**
++++++++ * Encodes the given string of characters (a standard JavaScript
++++++++ * string) as a binary encoded string where the bytes represent
++++++++ * a UTF-8 encoded string of characters. Non-ASCII characters will be
++++++++ * encoded as multiple bytes according to UTF-8.
++++++++ *
++++++++ * @param str a standard string of characters to encode.
++++++++ *
++++++++ * @return the binary encoded string.
++++++++ */
++++++++util.encodeUtf8 = function(str) {
++++++++ return unescape(encodeURIComponent(str));
++++++++};
++++++++
++++++++/**
++++++++ * Decodes a binary encoded string that contains bytes that
++++++++ * represent a UTF-8 encoded string of characters -- into a
++++++++ * string of characters (a standard JavaScript string).
++++++++ *
++++++++ * @param str the binary encoded string to decode.
++++++++ *
++++++++ * @return the resulting standard string of characters.
++++++++ */
++++++++util.decodeUtf8 = function(str) {
++++++++ return decodeURIComponent(escape(str));
++++++++};
++++++++
++++++++// binary encoding/decoding tools
++++++++// FIXME: Experimental. Do not use yet.
++++++++util.binary = {
++++++++ raw: {},
++++++++ hex: {},
++++++++ base64: {},
++++++++ base58: {},
++++++++ baseN : {
++++++++ encode: baseN.encode,
++++++++ decode: baseN.decode
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Encodes a Uint8Array as a binary-encoded string. This encoding uses
++++++++ * a value between 0 and 255 for each character.
++++++++ *
++++++++ * @param bytes the Uint8Array to encode.
++++++++ *
++++++++ * @return the binary-encoded string.
++++++++ */
++++++++util.binary.raw.encode = function(bytes) {
++++++++ return String.fromCharCode.apply(null, bytes);
++++++++};
++++++++
++++++++/**
++++++++ * Decodes a binary-encoded string to a Uint8Array. This encoding uses
++++++++ * a value between 0 and 255 for each character.
++++++++ *
++++++++ * @param str the binary-encoded string to decode.
++++++++ * @param [output] an optional Uint8Array to write the output to; if it
++++++++ * is too small, an exception will be thrown.
++++++++ * @param [offset] the start offset for writing to the output (default: 0).
++++++++ *
++++++++ * @return the Uint8Array or the number of bytes written if output was given.
++++++++ */
++++++++util.binary.raw.decode = function(str, output, offset) {
++++++++ var out = output;
++++++++ if(!out) {
++++++++ out = new Uint8Array(str.length);
++++++++ }
++++++++ offset = offset || 0;
++++++++ var j = offset;
++++++++ for(var i = 0; i < str.length; ++i) {
++++++++ out[j++] = str.charCodeAt(i);
++++++++ }
++++++++ return output ? (j - offset) : out;
++++++++};
++++++++
++++++++/**
++++++++ * Encodes a 'binary' string, ArrayBuffer, DataView, TypedArray, or
++++++++ * ByteBuffer as a string of hexadecimal characters.
++++++++ *
++++++++ * @param bytes the bytes to convert.
++++++++ *
++++++++ * @return the string of hexadecimal characters.
++++++++ */
++++++++util.binary.hex.encode = util.bytesToHex;
++++++++
++++++++/**
++++++++ * Decodes a hex-encoded string to a Uint8Array.
++++++++ *
++++++++ * @param hex the hexadecimal string to convert.
++++++++ * @param [output] an optional Uint8Array to write the output to; if it
++++++++ * is too small, an exception will be thrown.
++++++++ * @param [offset] the start offset for writing to the output (default: 0).
++++++++ *
++++++++ * @return the Uint8Array or the number of bytes written if output was given.
++++++++ */
++++++++util.binary.hex.decode = function(hex, output, offset) {
++++++++ var out = output;
++++++++ if(!out) {
++++++++ out = new Uint8Array(Math.ceil(hex.length / 2));
++++++++ }
++++++++ offset = offset || 0;
++++++++ var i = 0, j = offset;
++++++++ if(hex.length & 1) {
++++++++ // odd number of characters, convert first character alone
++++++++ i = 1;
++++++++ out[j++] = parseInt(hex[0], 16);
++++++++ }
++++++++ // convert 2 characters (1 byte) at a time
++++++++ for(; i < hex.length; i += 2) {
++++++++ out[j++] = parseInt(hex.substr(i, 2), 16);
++++++++ }
++++++++ return output ? (j - offset) : out;
++++++++};
++++++++
++++++++/**
++++++++ * Base64-encodes a Uint8Array.
++++++++ *
++++++++ * @param input the Uint8Array to encode.
++++++++ * @param maxline the maximum number of encoded characters per line to use,
++++++++ * defaults to none.
++++++++ *
++++++++ * @return the base64-encoded output string.
++++++++ */
++++++++util.binary.base64.encode = function(input, maxline) {
++++++++ var line = '';
++++++++ var output = '';
++++++++ var chr1, chr2, chr3;
++++++++ var i = 0;
++++++++ while(i < input.byteLength) {
++++++++ chr1 = input[i++];
++++++++ chr2 = input[i++];
++++++++ chr3 = input[i++];
++++++++
++++++++ // encode 4 character group
++++++++ line += _base64.charAt(chr1 >> 2);
++++++++ line += _base64.charAt(((chr1 & 3) << 4) | (chr2 >> 4));
++++++++ if(isNaN(chr2)) {
++++++++ line += '==';
++++++++ } else {
++++++++ line += _base64.charAt(((chr2 & 15) << 2) | (chr3 >> 6));
++++++++ line += isNaN(chr3) ? '=' : _base64.charAt(chr3 & 63);
++++++++ }
++++++++
++++++++ if(maxline && line.length > maxline) {
++++++++ output += line.substr(0, maxline) + '\r\n';
++++++++ line = line.substr(maxline);
++++++++ }
++++++++ }
++++++++ output += line;
++++++++ return output;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes a base64-encoded string to a Uint8Array.
++++++++ *
++++++++ * @param input the base64-encoded input string.
++++++++ * @param [output] an optional Uint8Array to write the output to; if it
++++++++ * is too small, an exception will be thrown.
++++++++ * @param [offset] the start offset for writing to the output (default: 0).
++++++++ *
++++++++ * @return the Uint8Array or the number of bytes written if output was given.
++++++++ */
++++++++util.binary.base64.decode = function(input, output, offset) {
++++++++ var out = output;
++++++++ if(!out) {
++++++++ out = new Uint8Array(Math.ceil(input.length / 4) * 3);
++++++++ }
++++++++
++++++++ // remove all non-base64 characters
++++++++ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
++++++++
++++++++ offset = offset || 0;
++++++++ var enc1, enc2, enc3, enc4;
++++++++ var i = 0, j = offset;
++++++++
++++++++ while(i < input.length) {
++++++++ enc1 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc2 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc3 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++ enc4 = _base64Idx[input.charCodeAt(i++) - 43];
++++++++
++++++++ out[j++] = (enc1 << 2) | (enc2 >> 4);
++++++++ if(enc3 !== 64) {
++++++++ // decoded at least 2 bytes
++++++++ out[j++] = ((enc2 & 15) << 4) | (enc3 >> 2);
++++++++ if(enc4 !== 64) {
++++++++ // decoded 3 bytes
++++++++ out[j++] = ((enc3 & 3) << 6) | enc4;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // make sure result is the exact decoded length
++++++++ return output ? (j - offset) : out.subarray(0, j);
++++++++};
++++++++
++++++++// add support for base58 encoding/decoding with Bitcoin alphabet
++++++++util.binary.base58.encode = function(input, maxline) {
++++++++ return util.binary.baseN.encode(input, _base58, maxline);
++++++++};
++++++++util.binary.base58.decode = function(input, maxline) {
++++++++ return util.binary.baseN.decode(input, _base58, maxline);
++++++++};
++++++++
++++++++// text encoding/decoding tools
++++++++// FIXME: Experimental. Do not use yet.
++++++++util.text = {
++++++++ utf8: {},
++++++++ utf16: {}
++++++++};
++++++++
++++++++/**
++++++++ * Encodes the given string as UTF-8 in a Uint8Array.
++++++++ *
++++++++ * @param str the string to encode.
++++++++ * @param [output] an optional Uint8Array to write the output to; if it
++++++++ * is too small, an exception will be thrown.
++++++++ * @param [offset] the start offset for writing to the output (default: 0).
++++++++ *
++++++++ * @return the Uint8Array or the number of bytes written if output was given.
++++++++ */
++++++++util.text.utf8.encode = function(str, output, offset) {
++++++++ str = util.encodeUtf8(str);
++++++++ var out = output;
++++++++ if(!out) {
++++++++ out = new Uint8Array(str.length);
++++++++ }
++++++++ offset = offset || 0;
++++++++ var j = offset;
++++++++ for(var i = 0; i < str.length; ++i) {
++++++++ out[j++] = str.charCodeAt(i);
++++++++ }
++++++++ return output ? (j - offset) : out;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes the UTF-8 contents from a Uint8Array.
++++++++ *
++++++++ * @param bytes the Uint8Array to decode.
++++++++ *
++++++++ * @return the resulting string.
++++++++ */
++++++++util.text.utf8.decode = function(bytes) {
++++++++ return util.decodeUtf8(String.fromCharCode.apply(null, bytes));
++++++++};
++++++++
++++++++/**
++++++++ * Encodes the given string as UTF-16 in a Uint8Array.
++++++++ *
++++++++ * @param str the string to encode.
++++++++ * @param [output] an optional Uint8Array to write the output to; if it
++++++++ * is too small, an exception will be thrown.
++++++++ * @param [offset] the start offset for writing to the output (default: 0).
++++++++ *
++++++++ * @return the Uint8Array or the number of bytes written if output was given.
++++++++ */
++++++++util.text.utf16.encode = function(str, output, offset) {
++++++++ var out = output;
++++++++ if(!out) {
++++++++ out = new Uint8Array(str.length * 2);
++++++++ }
++++++++ var view = new Uint16Array(out.buffer);
++++++++ offset = offset || 0;
++++++++ var j = offset;
++++++++ var k = offset;
++++++++ for(var i = 0; i < str.length; ++i) {
++++++++ view[k++] = str.charCodeAt(i);
++++++++ j += 2;
++++++++ }
++++++++ return output ? (j - offset) : out;
++++++++};
++++++++
++++++++/**
++++++++ * Decodes the UTF-16 contents from a Uint8Array.
++++++++ *
++++++++ * @param bytes the Uint8Array to decode.
++++++++ *
++++++++ * @return the resulting string.
++++++++ */
++++++++util.text.utf16.decode = function(bytes) {
++++++++ return String.fromCharCode.apply(null, new Uint16Array(bytes.buffer));
++++++++};
++++++++
++++++++/**
++++++++ * Deflates the given data using a flash interface.
++++++++ *
++++++++ * @param api the flash interface.
++++++++ * @param bytes the data.
++++++++ * @param raw true to return only raw deflate data, false to include zlib
++++++++ * header and trailer.
++++++++ *
++++++++ * @return the deflated data as a string.
++++++++ */
++++++++util.deflate = function(api, bytes, raw) {
++++++++ bytes = util.decode64(api.deflate(util.encode64(bytes)).rval);
++++++++
++++++++ // strip zlib header and trailer if necessary
++++++++ if(raw) {
++++++++ // zlib header is 2 bytes (CMF,FLG) where FLG indicates that
++++++++ // there is a 4-byte DICT (alder-32) block before the data if
++++++++ // its 5th bit is set
++++++++ var start = 2;
++++++++ var flg = bytes.charCodeAt(1);
++++++++ if(flg & 0x20) {
++++++++ start = 6;
++++++++ }
++++++++ // zlib trailer is 4 bytes of adler-32
++++++++ bytes = bytes.substring(start, bytes.length - 4);
++++++++ }
++++++++
++++++++ return bytes;
++++++++};
++++++++
++++++++/**
++++++++ * Inflates the given data using a flash interface.
++++++++ *
++++++++ * @param api the flash interface.
++++++++ * @param bytes the data.
++++++++ * @param raw true if the incoming data has no zlib header or trailer and is
++++++++ * raw DEFLATE data.
++++++++ *
++++++++ * @return the inflated data as a string, null on error.
++++++++ */
++++++++util.inflate = function(api, bytes, raw) {
++++++++ // TODO: add zlib header and trailer if necessary/possible
++++++++ var rval = api.inflate(util.encode64(bytes)).rval;
++++++++ return (rval === null) ? null : util.decode64(rval);
++++++++};
++++++++
++++++++/**
++++++++ * Sets a storage object.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ * @param obj the storage object, null to remove.
++++++++ */
++++++++var _setStorageObject = function(api, id, obj) {
++++++++ if(!api) {
++++++++ throw new Error('WebStorage not available.');
++++++++ }
++++++++
++++++++ var rval;
++++++++ if(obj === null) {
++++++++ rval = api.removeItem(id);
++++++++ } else {
++++++++ // json-encode and base64-encode object
++++++++ obj = util.encode64(JSON.stringify(obj));
++++++++ rval = api.setItem(id, obj);
++++++++ }
++++++++
++++++++ // handle potential flash error
++++++++ if(typeof(rval) !== 'undefined' && rval.rval !== true) {
++++++++ var error = new Error(rval.error.message);
++++++++ error.id = rval.error.id;
++++++++ error.name = rval.error.name;
++++++++ throw error;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Gets a storage object.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ *
++++++++ * @return the storage object entry or null if none exists.
++++++++ */
++++++++var _getStorageObject = function(api, id) {
++++++++ if(!api) {
++++++++ throw new Error('WebStorage not available.');
++++++++ }
++++++++
++++++++ // get the existing entry
++++++++ var rval = api.getItem(id);
++++++++
++++++++ /* Note: We check api.init because we can't do (api == localStorage)
++++++++ on IE because of "Class doesn't support Automation" exception. Only
++++++++ the flash api has an init method so this works too, but we need a
++++++++ better solution in the future. */
++++++++
++++++++ // flash returns item wrapped in an object, handle special case
++++++++ if(api.init) {
++++++++ if(rval.rval === null) {
++++++++ if(rval.error) {
++++++++ var error = new Error(rval.error.message);
++++++++ error.id = rval.error.id;
++++++++ error.name = rval.error.name;
++++++++ throw error;
++++++++ }
++++++++ // no error, but also no item
++++++++ rval = null;
++++++++ } else {
++++++++ rval = rval.rval;
++++++++ }
++++++++ }
++++++++
++++++++ // handle decoding
++++++++ if(rval !== null) {
++++++++ // base64-decode and json-decode data
++++++++ rval = JSON.parse(util.decode64(rval));
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Stores an item in local storage.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ * @param data the data for the item (any javascript object/primitive).
++++++++ */
++++++++var _setItem = function(api, id, key, data) {
++++++++ // get storage object
++++++++ var obj = _getStorageObject(api, id);
++++++++ if(obj === null) {
++++++++ // create a new storage object
++++++++ obj = {};
++++++++ }
++++++++ // update key
++++++++ obj[key] = data;
++++++++
++++++++ // set storage object
++++++++ _setStorageObject(api, id, obj);
++++++++};
++++++++
++++++++/**
++++++++ * Gets an item from local storage.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ *
++++++++ * @return the item.
++++++++ */
++++++++var _getItem = function(api, id, key) {
++++++++ // get storage object
++++++++ var rval = _getStorageObject(api, id);
++++++++ if(rval !== null) {
++++++++ // return data at key
++++++++ rval = (key in rval) ? rval[key] : null;
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Removes an item from local storage.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ */
++++++++var _removeItem = function(api, id, key) {
++++++++ // get storage object
++++++++ var obj = _getStorageObject(api, id);
++++++++ if(obj !== null && key in obj) {
++++++++ // remove key
++++++++ delete obj[key];
++++++++
++++++++ // see if entry has no keys remaining
++++++++ var empty = true;
++++++++ for(var prop in obj) {
++++++++ empty = false;
++++++++ break;
++++++++ }
++++++++ if(empty) {
++++++++ // remove entry entirely if no keys are left
++++++++ obj = null;
++++++++ }
++++++++
++++++++ // set storage object
++++++++ _setStorageObject(api, id, obj);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Clears the local disk storage identified by the given ID.
++++++++ *
++++++++ * @param api the storage interface.
++++++++ * @param id the storage ID to use.
++++++++ */
++++++++var _clearItems = function(api, id) {
++++++++ _setStorageObject(api, id, null);
++++++++};
++++++++
++++++++/**
++++++++ * Calls a storage function.
++++++++ *
++++++++ * @param func the function to call.
++++++++ * @param args the arguments for the function.
++++++++ * @param location the location argument.
++++++++ *
++++++++ * @return the return value from the function.
++++++++ */
++++++++var _callStorageFunction = function(func, args, location) {
++++++++ var rval = null;
++++++++
++++++++ // default storage types
++++++++ if(typeof(location) === 'undefined') {
++++++++ location = ['web', 'flash'];
++++++++ }
++++++++
++++++++ // apply storage types in order of preference
++++++++ var type;
++++++++ var done = false;
++++++++ var exception = null;
++++++++ for(var idx in location) {
++++++++ type = location[idx];
++++++++ try {
++++++++ if(type === 'flash' || type === 'both') {
++++++++ if(args[0] === null) {
++++++++ throw new Error('Flash local storage not available.');
++++++++ }
++++++++ rval = func.apply(this, args);
++++++++ done = (type === 'flash');
++++++++ }
++++++++ if(type === 'web' || type === 'both') {
++++++++ args[0] = localStorage;
++++++++ rval = func.apply(this, args);
++++++++ done = true;
++++++++ }
++++++++ } catch(ex) {
++++++++ exception = ex;
++++++++ }
++++++++ if(done) {
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ if(!done) {
++++++++ throw exception;
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Stores an item on local disk.
++++++++ *
++++++++ * The available types of local storage include 'flash', 'web', and 'both'.
++++++++ *
++++++++ * The type 'flash' refers to flash local storage (SharedObject). In order
++++++++ * to use flash local storage, the 'api' parameter must be valid. The type
++++++++ * 'web' refers to WebStorage, if supported by the browser. The type 'both'
++++++++ * refers to storing using both 'flash' and 'web', not just one or the
++++++++ * other.
++++++++ *
++++++++ * The location array should list the storage types to use in order of
++++++++ * preference:
++++++++ *
++++++++ * ['flash']: flash only storage
++++++++ * ['web']: web only storage
++++++++ * ['both']: try to store in both
++++++++ * ['flash','web']: store in flash first, but if not available, 'web'
++++++++ * ['web','flash']: store in web first, but if not available, 'flash'
++++++++ *
++++++++ * The location array defaults to: ['web', 'flash']
++++++++ *
++++++++ * @param api the flash interface, null to use only WebStorage.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ * @param data the data for the item (any javascript object/primitive).
++++++++ * @param location an array with the preferred types of storage to use.
++++++++ */
++++++++util.setItem = function(api, id, key, data, location) {
++++++++ _callStorageFunction(_setItem, arguments, location);
++++++++};
++++++++
++++++++/**
++++++++ * Gets an item on local disk.
++++++++ *
++++++++ * Set setItem() for details on storage types.
++++++++ *
++++++++ * @param api the flash interface, null to use only WebStorage.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ * @param location an array with the preferred types of storage to use.
++++++++ *
++++++++ * @return the item.
++++++++ */
++++++++util.getItem = function(api, id, key, location) {
++++++++ return _callStorageFunction(_getItem, arguments, location);
++++++++};
++++++++
++++++++/**
++++++++ * Removes an item on local disk.
++++++++ *
++++++++ * Set setItem() for details on storage types.
++++++++ *
++++++++ * @param api the flash interface.
++++++++ * @param id the storage ID to use.
++++++++ * @param key the key for the item.
++++++++ * @param location an array with the preferred types of storage to use.
++++++++ */
++++++++util.removeItem = function(api, id, key, location) {
++++++++ _callStorageFunction(_removeItem, arguments, location);
++++++++};
++++++++
++++++++/**
++++++++ * Clears the local disk storage identified by the given ID.
++++++++ *
++++++++ * Set setItem() for details on storage types.
++++++++ *
++++++++ * @param api the flash interface if flash is available.
++++++++ * @param id the storage ID to use.
++++++++ * @param location an array with the preferred types of storage to use.
++++++++ */
++++++++util.clearItems = function(api, id, location) {
++++++++ _callStorageFunction(_clearItems, arguments, location);
++++++++};
++++++++
++++++++/**
++++++++ * Check if an object is empty.
++++++++ *
++++++++ * Taken from:
++++++++ * http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json/679937#679937
++++++++ *
++++++++ * @param object the object to check.
++++++++ */
++++++++util.isEmpty = function(obj) {
++++++++ for(var prop in obj) {
++++++++ if(obj.hasOwnProperty(prop)) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++ return true;
++++++++};
++++++++
++++++++/**
++++++++ * Format with simple printf-style interpolation.
++++++++ *
++++++++ * %%: literal '%'
++++++++ * %s,%o: convert next argument into a string.
++++++++ *
++++++++ * @param format the string to format.
++++++++ * @param ... arguments to interpolate into the format string.
++++++++ */
++++++++util.format = function(format) {
++++++++ var re = /%./g;
++++++++ // current match
++++++++ var match;
++++++++ // current part
++++++++ var part;
++++++++ // current arg index
++++++++ var argi = 0;
++++++++ // collected parts to recombine later
++++++++ var parts = [];
++++++++ // last index found
++++++++ var last = 0;
++++++++ // loop while matches remain
++++++++ while((match = re.exec(format))) {
++++++++ part = format.substring(last, re.lastIndex - 2);
++++++++ // don't add empty strings (ie, parts between %s%s)
++++++++ if(part.length > 0) {
++++++++ parts.push(part);
++++++++ }
++++++++ last = re.lastIndex;
++++++++ // switch on % code
++++++++ var code = match[0][1];
++++++++ switch(code) {
++++++++ case 's':
++++++++ case 'o':
++++++++ // check if enough arguments were given
++++++++ if(argi < arguments.length) {
++++++++ parts.push(arguments[argi++ + 1]);
++++++++ } else {
++++++++ parts.push('<?>');
++++++++ }
++++++++ break;
++++++++ // FIXME: do proper formating for numbers, etc
++++++++ //case 'f':
++++++++ //case 'd':
++++++++ case '%':
++++++++ parts.push('%');
++++++++ break;
++++++++ default:
++++++++ parts.push('<%' + code + '?>');
++++++++ }
++++++++ }
++++++++ // add trailing part of format string
++++++++ parts.push(format.substring(last));
++++++++ return parts.join('');
++++++++};
++++++++
++++++++/**
++++++++ * Formats a number.
++++++++ *
++++++++ * http://snipplr.com/view/5945/javascript-numberformat--ported-from-php/
++++++++ */
++++++++util.formatNumber = function(number, decimals, dec_point, thousands_sep) {
++++++++ // http://kevin.vanzonneveld.net
++++++++ // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
++++++++ // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
++++++++ // + bugfix by: Michael White (http://crestidg.com)
++++++++ // + bugfix by: Benjamin Lupton
++++++++ // + bugfix by: Allan Jensen (http://www.winternet.no)
++++++++ // + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
++++++++ // * example 1: number_format(1234.5678, 2, '.', '');
++++++++ // * returns 1: 1234.57
++++++++
++++++++ var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
++++++++ var d = dec_point === undefined ? ',' : dec_point;
++++++++ var t = thousands_sep === undefined ?
++++++++ '.' : thousands_sep, s = n < 0 ? '-' : '';
++++++++ var i = parseInt((n = Math.abs(+n || 0).toFixed(c)), 10) + '';
++++++++ var j = (i.length > 3) ? i.length % 3 : 0;
++++++++ return s + (j ? i.substr(0, j) + t : '') +
++++++++ i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) +
++++++++ (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
++++++++};
++++++++
++++++++/**
++++++++ * Formats a byte size.
++++++++ *
++++++++ * http://snipplr.com/view/5949/format-humanize-file-byte-size-presentation-in-javascript/
++++++++ */
++++++++util.formatSize = function(size) {
++++++++ if(size >= 1073741824) {
++++++++ size = util.formatNumber(size / 1073741824, 2, '.', '') + ' GiB';
++++++++ } else if(size >= 1048576) {
++++++++ size = util.formatNumber(size / 1048576, 2, '.', '') + ' MiB';
++++++++ } else if(size >= 1024) {
++++++++ size = util.formatNumber(size / 1024, 0) + ' KiB';
++++++++ } else {
++++++++ size = util.formatNumber(size, 0) + ' bytes';
++++++++ }
++++++++ return size;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an IPv4 or IPv6 string representation into bytes (in network order).
++++++++ *
++++++++ * @param ip the IPv4 or IPv6 address to convert.
++++++++ *
++++++++ * @return the 4-byte IPv6 or 16-byte IPv6 address or null if the address can't
++++++++ * be parsed.
++++++++ */
++++++++util.bytesFromIP = function(ip) {
++++++++ if(ip.indexOf('.') !== -1) {
++++++++ return util.bytesFromIPv4(ip);
++++++++ }
++++++++ if(ip.indexOf(':') !== -1) {
++++++++ return util.bytesFromIPv6(ip);
++++++++ }
++++++++ return null;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an IPv4 string representation into bytes (in network order).
++++++++ *
++++++++ * @param ip the IPv4 address to convert.
++++++++ *
++++++++ * @return the 4-byte address or null if the address can't be parsed.
++++++++ */
++++++++util.bytesFromIPv4 = function(ip) {
++++++++ ip = ip.split('.');
++++++++ if(ip.length !== 4) {
++++++++ return null;
++++++++ }
++++++++ var b = util.createBuffer();
++++++++ for(var i = 0; i < ip.length; ++i) {
++++++++ var num = parseInt(ip[i], 10);
++++++++ if(isNaN(num)) {
++++++++ return null;
++++++++ }
++++++++ b.putByte(num);
++++++++ }
++++++++ return b.getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * Converts an IPv6 string representation into bytes (in network order).
++++++++ *
++++++++ * @param ip the IPv6 address to convert.
++++++++ *
++++++++ * @return the 16-byte address or null if the address can't be parsed.
++++++++ */
++++++++util.bytesFromIPv6 = function(ip) {
++++++++ var blanks = 0;
++++++++ ip = ip.split(':').filter(function(e) {
++++++++ if(e.length === 0) ++blanks;
++++++++ return true;
++++++++ });
++++++++ var zeros = (8 - ip.length + blanks) * 2;
++++++++ var b = util.createBuffer();
++++++++ for(var i = 0; i < 8; ++i) {
++++++++ if(!ip[i] || ip[i].length === 0) {
++++++++ b.fillWithByte(0, zeros);
++++++++ zeros = 0;
++++++++ continue;
++++++++ }
++++++++ var bytes = util.hexToBytes(ip[i]);
++++++++ if(bytes.length < 2) {
++++++++ b.putByte(0);
++++++++ }
++++++++ b.putBytes(bytes);
++++++++ }
++++++++ return b.getBytes();
++++++++};
++++++++
++++++++/**
++++++++ * Converts 4-bytes into an IPv4 string representation or 16-bytes into
++++++++ * an IPv6 string representation. The bytes must be in network order.
++++++++ *
++++++++ * @param bytes the bytes to convert.
++++++++ *
++++++++ * @return the IPv4 or IPv6 string representation if 4 or 16 bytes,
++++++++ * respectively, are given, otherwise null.
++++++++ */
++++++++util.bytesToIP = function(bytes) {
++++++++ if(bytes.length === 4) {
++++++++ return util.bytesToIPv4(bytes);
++++++++ }
++++++++ if(bytes.length === 16) {
++++++++ return util.bytesToIPv6(bytes);
++++++++ }
++++++++ return null;
++++++++};
++++++++
++++++++/**
++++++++ * Converts 4-bytes into an IPv4 string representation. The bytes must be
++++++++ * in network order.
++++++++ *
++++++++ * @param bytes the bytes to convert.
++++++++ *
++++++++ * @return the IPv4 string representation or null for an invalid # of bytes.
++++++++ */
++++++++util.bytesToIPv4 = function(bytes) {
++++++++ if(bytes.length !== 4) {
++++++++ return null;
++++++++ }
++++++++ var ip = [];
++++++++ for(var i = 0; i < bytes.length; ++i) {
++++++++ ip.push(bytes.charCodeAt(i));
++++++++ }
++++++++ return ip.join('.');
++++++++};
++++++++
++++++++/**
++++++++ * Converts 16-bytes into an IPv16 string representation. The bytes must be
++++++++ * in network order.
++++++++ *
++++++++ * @param bytes the bytes to convert.
++++++++ *
++++++++ * @return the IPv16 string representation or null for an invalid # of bytes.
++++++++ */
++++++++util.bytesToIPv6 = function(bytes) {
++++++++ if(bytes.length !== 16) {
++++++++ return null;
++++++++ }
++++++++ var ip = [];
++++++++ var zeroGroups = [];
++++++++ var zeroMaxGroup = 0;
++++++++ for(var i = 0; i < bytes.length; i += 2) {
++++++++ var hex = util.bytesToHex(bytes[i] + bytes[i + 1]);
++++++++ // canonicalize zero representation
++++++++ while(hex[0] === '0' && hex !== '0') {
++++++++ hex = hex.substr(1);
++++++++ }
++++++++ if(hex === '0') {
++++++++ var last = zeroGroups[zeroGroups.length - 1];
++++++++ var idx = ip.length;
++++++++ if(!last || idx !== last.end + 1) {
++++++++ zeroGroups.push({start: idx, end: idx});
++++++++ } else {
++++++++ last.end = idx;
++++++++ if((last.end - last.start) >
++++++++ (zeroGroups[zeroMaxGroup].end - zeroGroups[zeroMaxGroup].start)) {
++++++++ zeroMaxGroup = zeroGroups.length - 1;
++++++++ }
++++++++ }
++++++++ }
++++++++ ip.push(hex);
++++++++ }
++++++++ if(zeroGroups.length > 0) {
++++++++ var group = zeroGroups[zeroMaxGroup];
++++++++ // only shorten group of length > 0
++++++++ if(group.end - group.start > 0) {
++++++++ ip.splice(group.start, group.end - group.start + 1, '');
++++++++ if(group.start === 0) {
++++++++ ip.unshift('');
++++++++ }
++++++++ if(group.end === 7) {
++++++++ ip.push('');
++++++++ }
++++++++ }
++++++++ }
++++++++ return ip.join(':');
++++++++};
++++++++
++++++++/**
++++++++ * Estimates the number of processes that can be run concurrently. If
++++++++ * creating Web Workers, keep in mind that the main JavaScript process needs
++++++++ * its own core.
++++++++ *
++++++++ * @param options the options to use:
++++++++ * update true to force an update (not use the cached value).
++++++++ * @param callback(err, max) called once the operation completes.
++++++++ */
++++++++util.estimateCores = function(options, callback) {
++++++++ if(typeof options === 'function') {
++++++++ callback = options;
++++++++ options = {};
++++++++ }
++++++++ options = options || {};
++++++++ if('cores' in util && !options.update) {
++++++++ return callback(null, util.cores);
++++++++ }
++++++++ if(typeof navigator !== 'undefined' &&
++++++++ 'hardwareConcurrency' in navigator &&
++++++++ navigator.hardwareConcurrency > 0) {
++++++++ util.cores = navigator.hardwareConcurrency;
++++++++ return callback(null, util.cores);
++++++++ }
++++++++ if(typeof Worker === 'undefined') {
++++++++ // workers not available
++++++++ util.cores = 1;
++++++++ return callback(null, util.cores);
++++++++ }
++++++++ if(typeof Blob === 'undefined') {
++++++++ // can't estimate, default to 2
++++++++ util.cores = 2;
++++++++ return callback(null, util.cores);
++++++++ }
++++++++
++++++++ // create worker concurrency estimation code as blob
++++++++ var blobUrl = URL.createObjectURL(new Blob(['(',
++++++++ function() {
++++++++ self.addEventListener('message', function(e) {
++++++++ // run worker for 4 ms
++++++++ var st = Date.now();
++++++++ var et = st + 4;
++++++++ while(Date.now() < et);
++++++++ self.postMessage({st: st, et: et});
++++++++ });
++++++++ }.toString(),
++++++++ ')()'], {type: 'application/javascript'}));
++++++++
++++++++ // take 5 samples using 16 workers
++++++++ sample([], 5, 16);
++++++++
++++++++ function sample(max, samples, numWorkers) {
++++++++ if(samples === 0) {
++++++++ // get overlap average
++++++++ var avg = Math.floor(max.reduce(function(avg, x) {
++++++++ return avg + x;
++++++++ }, 0) / max.length);
++++++++ util.cores = Math.max(1, avg);
++++++++ URL.revokeObjectURL(blobUrl);
++++++++ return callback(null, util.cores);
++++++++ }
++++++++ map(numWorkers, function(err, results) {
++++++++ max.push(reduce(numWorkers, results));
++++++++ sample(max, samples - 1, numWorkers);
++++++++ });
++++++++ }
++++++++
++++++++ function map(numWorkers, callback) {
++++++++ var workers = [];
++++++++ var results = [];
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ var worker = new Worker(blobUrl);
++++++++ worker.addEventListener('message', function(e) {
++++++++ results.push(e.data);
++++++++ if(results.length === numWorkers) {
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ workers[i].terminate();
++++++++ }
++++++++ callback(null, results);
++++++++ }
++++++++ });
++++++++ workers.push(worker);
++++++++ }
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ workers[i].postMessage(i);
++++++++ }
++++++++ }
++++++++
++++++++ function reduce(numWorkers, results) {
++++++++ // find overlapping time windows
++++++++ var overlaps = [];
++++++++ for(var n = 0; n < numWorkers; ++n) {
++++++++ var r1 = results[n];
++++++++ var overlap = overlaps[n] = [];
++++++++ for(var i = 0; i < numWorkers; ++i) {
++++++++ if(n === i) {
++++++++ continue;
++++++++ }
++++++++ var r2 = results[i];
++++++++ if((r1.st > r2.st && r1.st < r2.et) ||
++++++++ (r2.st > r1.st && r2.st < r1.et)) {
++++++++ overlap.push(i);
++++++++ }
++++++++ }
++++++++ }
++++++++ // get maximum overlaps ... don't include overlapping worker itself
++++++++ // as the main JS process was also being scheduled during the work and
++++++++ // would have to be subtracted from the estimate anyway
++++++++ return overlaps.reduce(function(max, overlap) {
++++++++ return Math.max(max, overlap.length);
++++++++ }, 0);
++++++++ }
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * Javascript implementation of X.509 and related components (such as
++++++++ * Certification Signing Requests) of a Public Key Infrastructure.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
++++++++ *
++++++++ * The ASN.1 representation of an X.509v3 certificate is as follows
++++++++ * (see RFC 2459):
++++++++ *
++++++++ * Certificate ::= SEQUENCE {
++++++++ * tbsCertificate TBSCertificate,
++++++++ * signatureAlgorithm AlgorithmIdentifier,
++++++++ * signatureValue BIT STRING
++++++++ * }
++++++++ *
++++++++ * TBSCertificate ::= SEQUENCE {
++++++++ * version [0] EXPLICIT Version DEFAULT v1,
++++++++ * serialNumber CertificateSerialNumber,
++++++++ * signature AlgorithmIdentifier,
++++++++ * issuer Name,
++++++++ * validity Validity,
++++++++ * subject Name,
++++++++ * subjectPublicKeyInfo SubjectPublicKeyInfo,
++++++++ * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
++++++++ * -- If present, version shall be v2 or v3
++++++++ * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
++++++++ * -- If present, version shall be v2 or v3
++++++++ * extensions [3] EXPLICIT Extensions OPTIONAL
++++++++ * -- If present, version shall be v3
++++++++ * }
++++++++ *
++++++++ * Version ::= INTEGER { v1(0), v2(1), v3(2) }
++++++++ *
++++++++ * CertificateSerialNumber ::= INTEGER
++++++++ *
++++++++ * Name ::= CHOICE {
++++++++ * // only one possible choice for now
++++++++ * RDNSequence
++++++++ * }
++++++++ *
++++++++ * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
++++++++ *
++++++++ * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
++++++++ *
++++++++ * AttributeTypeAndValue ::= SEQUENCE {
++++++++ * type AttributeType,
++++++++ * value AttributeValue
++++++++ * }
++++++++ * AttributeType ::= OBJECT IDENTIFIER
++++++++ * AttributeValue ::= ANY DEFINED BY AttributeType
++++++++ *
++++++++ * Validity ::= SEQUENCE {
++++++++ * notBefore Time,
++++++++ * notAfter Time
++++++++ * }
++++++++ *
++++++++ * Time ::= CHOICE {
++++++++ * utcTime UTCTime,
++++++++ * generalTime GeneralizedTime
++++++++ * }
++++++++ *
++++++++ * UniqueIdentifier ::= BIT STRING
++++++++ *
++++++++ * SubjectPublicKeyInfo ::= SEQUENCE {
++++++++ * algorithm AlgorithmIdentifier,
++++++++ * subjectPublicKey BIT STRING
++++++++ * }
++++++++ *
++++++++ * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
++++++++ *
++++++++ * Extension ::= SEQUENCE {
++++++++ * extnID OBJECT IDENTIFIER,
++++++++ * critical BOOLEAN DEFAULT FALSE,
++++++++ * extnValue OCTET STRING
++++++++ * }
++++++++ *
++++++++ * The only key algorithm currently supported for PKI is RSA.
++++++++ *
++++++++ * RSASSA-PSS signatures are described in RFC 3447 and RFC 4055.
++++++++ *
++++++++ * PKCS#10 v1.7 describes certificate signing requests:
++++++++ *
++++++++ * CertificationRequestInfo:
++++++++ *
++++++++ * CertificationRequestInfo ::= SEQUENCE {
++++++++ * version INTEGER { v1(0) } (v1,...),
++++++++ * subject Name,
++++++++ * subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
++++++++ * attributes [0] Attributes{{ CRIAttributes }}
++++++++ * }
++++++++ *
++++++++ * Attributes { ATTRIBUTE:IOSet } ::= SET OF Attribute{{ IOSet }}
++++++++ *
++++++++ * CRIAttributes ATTRIBUTE ::= {
++++++++ * ... -- add any locally defined attributes here -- }
++++++++ *
++++++++ * Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
++++++++ * type ATTRIBUTE.&id({IOSet}),
++++++++ * values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type})
++++++++ * }
++++++++ *
++++++++ * CertificationRequest ::= SEQUENCE {
++++++++ * certificationRequestInfo CertificationRequestInfo,
++++++++ * signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
++++++++ * signature BIT STRING
++++++++ * }
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./aes');
++++++++require('./asn1');
++++++++require('./des');
++++++++require('./md');
++++++++require('./mgf');
++++++++require('./oids');
++++++++require('./pem');
++++++++require('./pss');
++++++++require('./rsa');
++++++++require('./util');
++++++++
++++++++// shortcut for asn.1 API
++++++++var asn1 = forge.asn1;
++++++++
++++++++/* Public Key Infrastructure (PKI) implementation. */
++++++++var pki = module.exports = forge.pki = forge.pki || {};
++++++++var oids = pki.oids;
++++++++
++++++++// short name OID mappings
++++++++var _shortNames = {};
++++++++_shortNames['CN'] = oids['commonName'];
++++++++_shortNames['commonName'] = 'CN';
++++++++_shortNames['C'] = oids['countryName'];
++++++++_shortNames['countryName'] = 'C';
++++++++_shortNames['L'] = oids['localityName'];
++++++++_shortNames['localityName'] = 'L';
++++++++_shortNames['ST'] = oids['stateOrProvinceName'];
++++++++_shortNames['stateOrProvinceName'] = 'ST';
++++++++_shortNames['O'] = oids['organizationName'];
++++++++_shortNames['organizationName'] = 'O';
++++++++_shortNames['OU'] = oids['organizationalUnitName'];
++++++++_shortNames['organizationalUnitName'] = 'OU';
++++++++_shortNames['E'] = oids['emailAddress'];
++++++++_shortNames['emailAddress'] = 'E';
++++++++
++++++++// validator for an SubjectPublicKeyInfo structure
++++++++// Note: Currently only works with an RSA public key
++++++++var publicKeyValidator = forge.pki.rsa.publicKeyValidator;
++++++++
++++++++// validator for an X.509v3 certificate
++++++++var x509CertificateValidator = {
++++++++ name: 'Certificate',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'tbsCertificate',
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate.version',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate.version.integer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'certVersion'
++++++++ }]
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.serialNumber',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'certSerialNumber'
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.signature',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate.signature.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'certinfoSignatureOid'
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.signature.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ optional: true,
++++++++ captureAsn1: 'certinfoSignatureParams'
++++++++ }]
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.issuer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'certIssuer'
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.validity',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ // Note: UTC and generalized times may both appear so the capture
++++++++ // names are based on their detected order, the names used below
++++++++ // are only for the common case, which validity time really means
++++++++ // "notBefore" and which means "notAfter" will be determined by order
++++++++ value: [{
++++++++ // notBefore (Time) (UTC time case)
++++++++ name: 'Certificate.TBSCertificate.validity.notBefore (utc)',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.UTCTIME,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'certValidity1UTCTime'
++++++++ }, {
++++++++ // notBefore (Time) (generalized time case)
++++++++ name: 'Certificate.TBSCertificate.validity.notBefore (generalized)',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.GENERALIZEDTIME,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'certValidity2GeneralizedTime'
++++++++ }, {
++++++++ // notAfter (Time) (only UTC time is supported)
++++++++ name: 'Certificate.TBSCertificate.validity.notAfter (utc)',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.UTCTIME,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'certValidity3UTCTime'
++++++++ }, {
++++++++ // notAfter (Time) (only UTC time is supported)
++++++++ name: 'Certificate.TBSCertificate.validity.notAfter (generalized)',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.GENERALIZEDTIME,
++++++++ constructed: false,
++++++++ optional: true,
++++++++ capture: 'certValidity4GeneralizedTime'
++++++++ }]
++++++++ }, {
++++++++ // Name (subject) (RDNSequence)
++++++++ name: 'Certificate.TBSCertificate.subject',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'certSubject'
++++++++ },
++++++++ // SubjectPublicKeyInfo
++++++++ publicKeyValidator,
++++++++ {
++++++++ // issuerUniqueID (optional)
++++++++ name: 'Certificate.TBSCertificate.issuerUniqueID',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 1,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate.issuerUniqueID.id',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ // TODO: support arbitrary bit length ids
++++++++ captureBitStringValue: 'certIssuerUniqueId'
++++++++ }]
++++++++ }, {
++++++++ // subjectUniqueID (optional)
++++++++ name: 'Certificate.TBSCertificate.subjectUniqueID',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 2,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'Certificate.TBSCertificate.subjectUniqueID.id',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ // TODO: support arbitrary bit length ids
++++++++ captureBitStringValue: 'certSubjectUniqueId'
++++++++ }]
++++++++ }, {
++++++++ // Extensions (optional)
++++++++ name: 'Certificate.TBSCertificate.extensions',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 3,
++++++++ constructed: true,
++++++++ captureAsn1: 'certExtensions',
++++++++ optional: true
++++++++ }]
++++++++ }, {
++++++++ // AlgorithmIdentifier (signature algorithm)
++++++++ name: 'Certificate.signatureAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // algorithm
++++++++ name: 'Certificate.signatureAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'certSignatureOid'
++++++++ }, {
++++++++ name: 'Certificate.TBSCertificate.signature.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ optional: true,
++++++++ captureAsn1: 'certSignatureParams'
++++++++ }]
++++++++ }, {
++++++++ // SignatureValue
++++++++ name: 'Certificate.signatureValue',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ captureBitStringValue: 'certSignature'
++++++++ }]
++++++++};
++++++++
++++++++var rsassaPssParameterValidator = {
++++++++ name: 'rsapss',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'rsapss.hashAlgorithm',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'rsapss.hashAlgorithm.AlgorithmIdentifier',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Class.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'rsapss.hashAlgorithm.AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'hashOid'
++++++++ /* parameter block omitted, for SHA1 NULL anyhow. */
++++++++ }]
++++++++ }]
++++++++ }, {
++++++++ name: 'rsapss.maskGenAlgorithm',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 1,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'rsapss.maskGenAlgorithm.AlgorithmIdentifier',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Class.SEQUENCE,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'rsapss.maskGenAlgorithm.AlgorithmIdentifier.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'maskGenOid'
++++++++ }, {
++++++++ name: 'rsapss.maskGenAlgorithm.AlgorithmIdentifier.params',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'rsapss.maskGenAlgorithm.AlgorithmIdentifier.params.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'maskGenHashOid'
++++++++ /* parameter block omitted, for SHA1 NULL anyhow. */
++++++++ }]
++++++++ }]
++++++++ }]
++++++++ }, {
++++++++ name: 'rsapss.saltLength',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 2,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'rsapss.saltLength.saltLength',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Class.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'saltLength'
++++++++ }]
++++++++ }, {
++++++++ name: 'rsapss.trailerField',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 3,
++++++++ optional: true,
++++++++ value: [{
++++++++ name: 'rsapss.trailer.trailer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Class.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'trailer'
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++// validator for a CertificationRequestInfo structure
++++++++var certificationRequestInfoValidator = {
++++++++ name: 'CertificationRequestInfo',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'certificationRequestInfo',
++++++++ value: [{
++++++++ name: 'CertificationRequestInfo.integer',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.INTEGER,
++++++++ constructed: false,
++++++++ capture: 'certificationRequestInfoVersion'
++++++++ }, {
++++++++ // Name (subject) (RDNSequence)
++++++++ name: 'CertificationRequestInfo.subject',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'certificationRequestInfoSubject'
++++++++ },
++++++++ // SubjectPublicKeyInfo
++++++++ publicKeyValidator,
++++++++ {
++++++++ name: 'CertificationRequestInfo.attributes',
++++++++ tagClass: asn1.Class.CONTEXT_SPECIFIC,
++++++++ type: 0,
++++++++ constructed: true,
++++++++ optional: true,
++++++++ capture: 'certificationRequestInfoAttributes',
++++++++ value: [{
++++++++ name: 'CertificationRequestInfo.attributes',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ name: 'CertificationRequestInfo.attributes.type',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false
++++++++ }, {
++++++++ name: 'CertificationRequestInfo.attributes.value',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SET,
++++++++ constructed: true
++++++++ }]
++++++++ }]
++++++++ }]
++++++++};
++++++++
++++++++// validator for a CertificationRequest structure
++++++++var certificationRequestValidator = {
++++++++ name: 'CertificationRequest',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ captureAsn1: 'csr',
++++++++ value: [
++++++++ certificationRequestInfoValidator, {
++++++++ // AlgorithmIdentifier (signature algorithm)
++++++++ name: 'CertificationRequest.signatureAlgorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.SEQUENCE,
++++++++ constructed: true,
++++++++ value: [{
++++++++ // algorithm
++++++++ name: 'CertificationRequest.signatureAlgorithm.algorithm',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.OID,
++++++++ constructed: false,
++++++++ capture: 'csrSignatureOid'
++++++++ }, {
++++++++ name: 'CertificationRequest.signatureAlgorithm.parameters',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ optional: true,
++++++++ captureAsn1: 'csrSignatureParams'
++++++++ }]
++++++++ }, {
++++++++ // signature
++++++++ name: 'CertificationRequest.signature',
++++++++ tagClass: asn1.Class.UNIVERSAL,
++++++++ type: asn1.Type.BITSTRING,
++++++++ constructed: false,
++++++++ captureBitStringValue: 'csrSignature'
++++++++ }
++++++++ ]
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RDNSequence of ASN.1 DER-encoded RelativeDistinguishedName
++++++++ * sets into an array with objects that have type and value properties.
++++++++ *
++++++++ * @param rdn the RDNSequence to convert.
++++++++ * @param md a message digest to append type and value to if provided.
++++++++ */
++++++++pki.RDNAttributesAsArray = function(rdn, md) {
++++++++ var rval = [];
++++++++
++++++++ // each value in 'rdn' in is a SET of RelativeDistinguishedName
++++++++ var set, attr, obj;
++++++++ for(var si = 0; si < rdn.value.length; ++si) {
++++++++ // get the RelativeDistinguishedName set
++++++++ set = rdn.value[si];
++++++++
++++++++ // each value in the SET is an AttributeTypeAndValue sequence
++++++++ // containing first a type (an OID) and second a value (defined by
++++++++ // the OID)
++++++++ for(var i = 0; i < set.value.length; ++i) {
++++++++ obj = {};
++++++++ attr = set.value[i];
++++++++ obj.type = asn1.derToOid(attr.value[0].value);
++++++++ obj.value = attr.value[1].value;
++++++++ obj.valueTagClass = attr.value[1].type;
++++++++ // if the OID is known, get its name and short name
++++++++ if(obj.type in oids) {
++++++++ obj.name = oids[obj.type];
++++++++ if(obj.name in _shortNames) {
++++++++ obj.shortName = _shortNames[obj.name];
++++++++ }
++++++++ }
++++++++ if(md) {
++++++++ md.update(obj.type);
++++++++ md.update(obj.value);
++++++++ }
++++++++ rval.push(obj);
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts ASN.1 CRIAttributes into an array with objects that have type and
++++++++ * value properties.
++++++++ *
++++++++ * @param attributes the CRIAttributes to convert.
++++++++ */
++++++++pki.CRIAttributesAsArray = function(attributes) {
++++++++ var rval = [];
++++++++
++++++++ // each value in 'attributes' in is a SEQUENCE with an OID and a SET
++++++++ for(var si = 0; si < attributes.length; ++si) {
++++++++ // get the attribute sequence
++++++++ var seq = attributes[si];
++++++++
++++++++ // each value in the SEQUENCE containing first a type (an OID) and
++++++++ // second a set of values (defined by the OID)
++++++++ var type = asn1.derToOid(seq.value[0].value);
++++++++ var values = seq.value[1].value;
++++++++ for(var vi = 0; vi < values.length; ++vi) {
++++++++ var obj = {};
++++++++ obj.type = type;
++++++++ obj.value = values[vi].value;
++++++++ obj.valueTagClass = values[vi].type;
++++++++ // if the OID is known, get its name and short name
++++++++ if(obj.type in oids) {
++++++++ obj.name = oids[obj.type];
++++++++ if(obj.name in _shortNames) {
++++++++ obj.shortName = _shortNames[obj.name];
++++++++ }
++++++++ }
++++++++ // parse extensions
++++++++ if(obj.type === oids.extensionRequest) {
++++++++ obj.extensions = [];
++++++++ for(var ei = 0; ei < obj.value.length; ++ei) {
++++++++ obj.extensions.push(pki.certificateExtensionFromAsn1(obj.value[ei]));
++++++++ }
++++++++ }
++++++++ rval.push(obj);
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Gets an issuer or subject attribute from its name, type, or short name.
++++++++ *
++++++++ * @param obj the issuer or subject object.
++++++++ * @param options a short name string or an object with:
++++++++ * shortName the short name for the attribute.
++++++++ * name the name for the attribute.
++++++++ * type the type for the attribute.
++++++++ *
++++++++ * @return the attribute.
++++++++ */
++++++++function _getAttribute(obj, options) {
++++++++ if(typeof options === 'string') {
++++++++ options = {shortName: options};
++++++++ }
++++++++
++++++++ var rval = null;
++++++++ var attr;
++++++++ for(var i = 0; rval === null && i < obj.attributes.length; ++i) {
++++++++ attr = obj.attributes[i];
++++++++ if(options.type && options.type === attr.type) {
++++++++ rval = attr;
++++++++ } else if(options.name && options.name === attr.name) {
++++++++ rval = attr;
++++++++ } else if(options.shortName && options.shortName === attr.shortName) {
++++++++ rval = attr;
++++++++ }
++++++++ }
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Converts signature parameters from ASN.1 structure.
++++++++ *
++++++++ * Currently only RSASSA-PSS supported. The PKCS#1 v1.5 signature scheme had
++++++++ * no parameters.
++++++++ *
++++++++ * RSASSA-PSS-params ::= SEQUENCE {
++++++++ * hashAlgorithm [0] HashAlgorithm DEFAULT
++++++++ * sha1Identifier,
++++++++ * maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT
++++++++ * mgf1SHA1Identifier,
++++++++ * saltLength [2] INTEGER DEFAULT 20,
++++++++ * trailerField [3] INTEGER DEFAULT 1
++++++++ * }
++++++++ *
++++++++ * HashAlgorithm ::= AlgorithmIdentifier
++++++++ *
++++++++ * MaskGenAlgorithm ::= AlgorithmIdentifier
++++++++ *
++++++++ * AlgorithmIdentifer ::= SEQUENCE {
++++++++ * algorithm OBJECT IDENTIFIER,
++++++++ * parameters ANY DEFINED BY algorithm OPTIONAL
++++++++ * }
++++++++ *
++++++++ * @param oid The OID specifying the signature algorithm
++++++++ * @param obj The ASN.1 structure holding the parameters
++++++++ * @param fillDefaults Whether to use return default values where omitted
++++++++ * @return signature parameter object
++++++++ */
++++++++var _readSignatureParameters = function(oid, obj, fillDefaults) {
++++++++ var params = {};
++++++++
++++++++ if(oid !== oids['RSASSA-PSS']) {
++++++++ return params;
++++++++ }
++++++++
++++++++ if(fillDefaults) {
++++++++ params = {
++++++++ hash: {
++++++++ algorithmOid: oids['sha1']
++++++++ },
++++++++ mgf: {
++++++++ algorithmOid: oids['mgf1'],
++++++++ hash: {
++++++++ algorithmOid: oids['sha1']
++++++++ }
++++++++ },
++++++++ saltLength: 20
++++++++ };
++++++++ }
++++++++
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, rsassaPssParameterValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read RSASSA-PSS parameter block.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ if(capture.hashOid !== undefined) {
++++++++ params.hash = params.hash || {};
++++++++ params.hash.algorithmOid = asn1.derToOid(capture.hashOid);
++++++++ }
++++++++
++++++++ if(capture.maskGenOid !== undefined) {
++++++++ params.mgf = params.mgf || {};
++++++++ params.mgf.algorithmOid = asn1.derToOid(capture.maskGenOid);
++++++++ params.mgf.hash = params.mgf.hash || {};
++++++++ params.mgf.hash.algorithmOid = asn1.derToOid(capture.maskGenHashOid);
++++++++ }
++++++++
++++++++ if(capture.saltLength !== undefined) {
++++++++ params.saltLength = capture.saltLength.charCodeAt(0);
++++++++ }
++++++++
++++++++ return params;
++++++++};
++++++++
++++++++/**
++++++++ * Create signature digest for OID.
++++++++ *
++++++++ * @param options
++++++++ * signatureOid: the OID specifying the signature algorithm.
++++++++ * type: a human readable type for error messages
++++++++ * @return a created md instance. throws if unknown oid.
++++++++ */
++++++++var _createSignatureDigest = function(options) {
++++++++ switch(oids[options.signatureOid]) {
++++++++ case 'sha1WithRSAEncryption':
++++++++ // deprecated alias
++++++++ case 'sha1WithRSASignature':
++++++++ return forge.md.sha1.create();
++++++++ case 'md5WithRSAEncryption':
++++++++ return forge.md.md5.create();
++++++++ case 'sha256WithRSAEncryption':
++++++++ return forge.md.sha256.create();
++++++++ case 'sha384WithRSAEncryption':
++++++++ return forge.md.sha384.create();
++++++++ case 'sha512WithRSAEncryption':
++++++++ return forge.md.sha512.create();
++++++++ case 'RSASSA-PSS':
++++++++ return forge.md.sha256.create();
++++++++ default:
++++++++ var error = new Error(
++++++++ 'Could not compute ' + options.type + ' digest. ' +
++++++++ 'Unknown signature OID.');
++++++++ error.signatureOid = options.signatureOid;
++++++++ throw error;
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Verify signature on certificate or CSR.
++++++++ *
++++++++ * @param options:
++++++++ * certificate the certificate or CSR to verify.
++++++++ * md the signature digest.
++++++++ * signature the signature
++++++++ * @return a created md instance. throws if unknown oid.
++++++++ */
++++++++var _verifySignature = function(options) {
++++++++ var cert = options.certificate;
++++++++ var scheme;
++++++++
++++++++ switch(cert.signatureOid) {
++++++++ case oids.sha1WithRSAEncryption:
++++++++ // deprecated alias
++++++++ case oids.sha1WithRSASignature:
++++++++ /* use PKCS#1 v1.5 padding scheme */
++++++++ break;
++++++++ case oids['RSASSA-PSS']:
++++++++ var hash, mgf;
++++++++
++++++++ /* initialize mgf */
++++++++ hash = oids[cert.signatureParameters.mgf.hash.algorithmOid];
++++++++ if(hash === undefined || forge.md[hash] === undefined) {
++++++++ var error = new Error('Unsupported MGF hash function.');
++++++++ error.oid = cert.signatureParameters.mgf.hash.algorithmOid;
++++++++ error.name = hash;
++++++++ throw error;
++++++++ }
++++++++
++++++++ mgf = oids[cert.signatureParameters.mgf.algorithmOid];
++++++++ if(mgf === undefined || forge.mgf[mgf] === undefined) {
++++++++ var error = new Error('Unsupported MGF function.');
++++++++ error.oid = cert.signatureParameters.mgf.algorithmOid;
++++++++ error.name = mgf;
++++++++ throw error;
++++++++ }
++++++++
++++++++ mgf = forge.mgf[mgf].create(forge.md[hash].create());
++++++++
++++++++ /* initialize hash function */
++++++++ hash = oids[cert.signatureParameters.hash.algorithmOid];
++++++++ if(hash === undefined || forge.md[hash] === undefined) {
++++++++ var error = new Error('Unsupported RSASSA-PSS hash function.');
++++++++ error.oid = cert.signatureParameters.hash.algorithmOid;
++++++++ error.name = hash;
++++++++ throw error;
++++++++ }
++++++++
++++++++ scheme = forge.pss.create(
++++++++ forge.md[hash].create(), mgf, cert.signatureParameters.saltLength
++++++++ );
++++++++ break;
++++++++ }
++++++++
++++++++ // verify signature on cert using public key
++++++++ return cert.publicKey.verify(
++++++++ options.md.digest().getBytes(), options.signature, scheme
++++++++ );
++++++++};
++++++++
++++++++/**
++++++++ * Converts an X.509 certificate from PEM format.
++++++++ *
++++++++ * Note: If the certificate is to be verified then compute hash should
++++++++ * be set to true. This will scan the TBSCertificate part of the ASN.1
++++++++ * object while it is converted so it doesn't need to be converted back
++++++++ * to ASN.1-DER-encoding later.
++++++++ *
++++++++ * @param pem the PEM-formatted certificate.
++++++++ * @param computeHash true to compute the hash for verification.
++++++++ * @param strict true to be strict when checking ASN.1 value lengths, false to
++++++++ * allow truncated values (default: true).
++++++++ *
++++++++ * @return the certificate.
++++++++ */
++++++++pki.certificateFromPem = function(pem, computeHash, strict) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'CERTIFICATE' &&
++++++++ msg.type !== 'X509 CERTIFICATE' &&
++++++++ msg.type !== 'TRUSTED CERTIFICATE') {
++++++++ var error = new Error(
++++++++ 'Could not convert certificate from PEM; PEM header type ' +
++++++++ 'is not "CERTIFICATE", "X509 CERTIFICATE", or "TRUSTED CERTIFICATE".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error(
++++++++ 'Could not convert certificate from PEM; PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ var obj = asn1.fromDer(msg.body, strict);
++++++++
++++++++ return pki.certificateFromAsn1(obj, computeHash);
++++++++};
++++++++
++++++++/**
++++++++ * Converts an X.509 certificate to PEM format.
++++++++ *
++++++++ * @param cert the certificate.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted certificate.
++++++++ */
++++++++pki.certificateToPem = function(cert, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'CERTIFICATE',
++++++++ body: asn1.toDer(pki.certificateToAsn1(cert)).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RSA public key from PEM format.
++++++++ *
++++++++ * @param pem the PEM-formatted public key.
++++++++ *
++++++++ * @return the public key.
++++++++ */
++++++++pki.publicKeyFromPem = function(pem) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'PUBLIC KEY' && msg.type !== 'RSA PUBLIC KEY') {
++++++++ var error = new Error('Could not convert public key from PEM; PEM header ' +
++++++++ 'type is not "PUBLIC KEY" or "RSA PUBLIC KEY".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert public key from PEM; PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ var obj = asn1.fromDer(msg.body);
++++++++
++++++++ return pki.publicKeyFromAsn1(obj);
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RSA public key to PEM format (using a SubjectPublicKeyInfo).
++++++++ *
++++++++ * @param key the public key.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted public key.
++++++++ */
++++++++pki.publicKeyToPem = function(key, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'PUBLIC KEY',
++++++++ body: asn1.toDer(pki.publicKeyToAsn1(key)).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Converts an RSA public key to PEM format (using an RSAPublicKey).
++++++++ *
++++++++ * @param key the public key.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted public key.
++++++++ */
++++++++pki.publicKeyToRSAPublicKeyPem = function(key, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'RSA PUBLIC KEY',
++++++++ body: asn1.toDer(pki.publicKeyToRSAPublicKey(key)).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Gets a fingerprint for the given public key.
++++++++ *
++++++++ * @param options the options to use.
++++++++ * [md] the message digest object to use (defaults to forge.md.sha1).
++++++++ * [type] the type of fingerprint, such as 'RSAPublicKey',
++++++++ * 'SubjectPublicKeyInfo' (defaults to 'RSAPublicKey').
++++++++ * [encoding] an alternative output encoding, such as 'hex'
++++++++ * (defaults to none, outputs a byte buffer).
++++++++ * [delimiter] the delimiter to use between bytes for 'hex' encoded
++++++++ * output, eg: ':' (defaults to none).
++++++++ *
++++++++ * @return the fingerprint as a byte buffer or other encoding based on options.
++++++++ */
++++++++pki.getPublicKeyFingerprint = function(key, options) {
++++++++ options = options || {};
++++++++ var md = options.md || forge.md.sha1.create();
++++++++ var type = options.type || 'RSAPublicKey';
++++++++
++++++++ var bytes;
++++++++ switch(type) {
++++++++ case 'RSAPublicKey':
++++++++ bytes = asn1.toDer(pki.publicKeyToRSAPublicKey(key)).getBytes();
++++++++ break;
++++++++ case 'SubjectPublicKeyInfo':
++++++++ bytes = asn1.toDer(pki.publicKeyToAsn1(key)).getBytes();
++++++++ break;
++++++++ default:
++++++++ throw new Error('Unknown fingerprint type "' + options.type + '".');
++++++++ }
++++++++
++++++++ // hash public key bytes
++++++++ md.start();
++++++++ md.update(bytes);
++++++++ var digest = md.digest();
++++++++ if(options.encoding === 'hex') {
++++++++ var hex = digest.toHex();
++++++++ if(options.delimiter) {
++++++++ return hex.match(/.{2}/g).join(options.delimiter);
++++++++ }
++++++++ return hex;
++++++++ } else if(options.encoding === 'binary') {
++++++++ return digest.getBytes();
++++++++ } else if(options.encoding) {
++++++++ throw new Error('Unknown encoding "' + options.encoding + '".');
++++++++ }
++++++++ return digest;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#10 certification request (CSR) from PEM format.
++++++++ *
++++++++ * Note: If the certification request is to be verified then compute hash
++++++++ * should be set to true. This will scan the CertificationRequestInfo part of
++++++++ * the ASN.1 object while it is converted so it doesn't need to be converted
++++++++ * back to ASN.1-DER-encoding later.
++++++++ *
++++++++ * @param pem the PEM-formatted certificate.
++++++++ * @param computeHash true to compute the hash for verification.
++++++++ * @param strict true to be strict when checking ASN.1 value lengths, false to
++++++++ * allow truncated values (default: true).
++++++++ *
++++++++ * @return the certification request (CSR).
++++++++ */
++++++++pki.certificationRequestFromPem = function(pem, computeHash, strict) {
++++++++ var msg = forge.pem.decode(pem)[0];
++++++++
++++++++ if(msg.type !== 'CERTIFICATE REQUEST') {
++++++++ var error = new Error('Could not convert certification request from PEM; ' +
++++++++ 'PEM header type is not "CERTIFICATE REQUEST".');
++++++++ error.headerType = msg.type;
++++++++ throw error;
++++++++ }
++++++++ if(msg.procType && msg.procType.type === 'ENCRYPTED') {
++++++++ throw new Error('Could not convert certification request from PEM; ' +
++++++++ 'PEM is encrypted.');
++++++++ }
++++++++
++++++++ // convert DER to ASN.1 object
++++++++ var obj = asn1.fromDer(msg.body, strict);
++++++++
++++++++ return pki.certificationRequestFromAsn1(obj, computeHash);
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#10 certification request (CSR) to PEM format.
++++++++ *
++++++++ * @param csr the certification request.
++++++++ * @param maxline the maximum characters per line, defaults to 64.
++++++++ *
++++++++ * @return the PEM-formatted certification request.
++++++++ */
++++++++pki.certificationRequestToPem = function(csr, maxline) {
++++++++ // convert to ASN.1, then DER, then PEM-encode
++++++++ var msg = {
++++++++ type: 'CERTIFICATE REQUEST',
++++++++ body: asn1.toDer(pki.certificationRequestToAsn1(csr)).getBytes()
++++++++ };
++++++++ return forge.pem.encode(msg, {maxline: maxline});
++++++++};
++++++++
++++++++/**
++++++++ * Creates an empty X.509v3 RSA certificate.
++++++++ *
++++++++ * @return the certificate.
++++++++ */
++++++++pki.createCertificate = function() {
++++++++ var cert = {};
++++++++ cert.version = 0x02;
++++++++ cert.serialNumber = '00';
++++++++ cert.signatureOid = null;
++++++++ cert.signature = null;
++++++++ cert.siginfo = {};
++++++++ cert.siginfo.algorithmOid = null;
++++++++ cert.validity = {};
++++++++ cert.validity.notBefore = new Date();
++++++++ cert.validity.notAfter = new Date();
++++++++
++++++++ cert.issuer = {};
++++++++ cert.issuer.getField = function(sn) {
++++++++ return _getAttribute(cert.issuer, sn);
++++++++ };
++++++++ cert.issuer.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ cert.issuer.attributes.push(attr);
++++++++ };
++++++++ cert.issuer.attributes = [];
++++++++ cert.issuer.hash = null;
++++++++
++++++++ cert.subject = {};
++++++++ cert.subject.getField = function(sn) {
++++++++ return _getAttribute(cert.subject, sn);
++++++++ };
++++++++ cert.subject.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ cert.subject.attributes.push(attr);
++++++++ };
++++++++ cert.subject.attributes = [];
++++++++ cert.subject.hash = null;
++++++++
++++++++ cert.extensions = [];
++++++++ cert.publicKey = null;
++++++++ cert.md = null;
++++++++
++++++++ /**
++++++++ * Sets the subject of this certificate.
++++++++ *
++++++++ * @param attrs the array of subject attributes to use.
++++++++ * @param uniqueId an optional a unique ID to use.
++++++++ */
++++++++ cert.setSubject = function(attrs, uniqueId) {
++++++++ // set new attributes, clear hash
++++++++ _fillMissingFields(attrs);
++++++++ cert.subject.attributes = attrs;
++++++++ delete cert.subject.uniqueId;
++++++++ if(uniqueId) {
++++++++ // TODO: support arbitrary bit length ids
++++++++ cert.subject.uniqueId = uniqueId;
++++++++ }
++++++++ cert.subject.hash = null;
++++++++ };
++++++++
++++++++ /**
++++++++ * Sets the issuer of this certificate.
++++++++ *
++++++++ * @param attrs the array of issuer attributes to use.
++++++++ * @param uniqueId an optional a unique ID to use.
++++++++ */
++++++++ cert.setIssuer = function(attrs, uniqueId) {
++++++++ // set new attributes, clear hash
++++++++ _fillMissingFields(attrs);
++++++++ cert.issuer.attributes = attrs;
++++++++ delete cert.issuer.uniqueId;
++++++++ if(uniqueId) {
++++++++ // TODO: support arbitrary bit length ids
++++++++ cert.issuer.uniqueId = uniqueId;
++++++++ }
++++++++ cert.issuer.hash = null;
++++++++ };
++++++++
++++++++ /**
++++++++ * Sets the extensions of this certificate.
++++++++ *
++++++++ * @param exts the array of extensions to use.
++++++++ */
++++++++ cert.setExtensions = function(exts) {
++++++++ for(var i = 0; i < exts.length; ++i) {
++++++++ _fillMissingExtensionFields(exts[i], {cert: cert});
++++++++ }
++++++++ // set new extensions
++++++++ cert.extensions = exts;
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets an extension by its name or id.
++++++++ *
++++++++ * @param options the name to use or an object with:
++++++++ * name the name to use.
++++++++ * id the id to use.
++++++++ *
++++++++ * @return the extension or null if not found.
++++++++ */
++++++++ cert.getExtension = function(options) {
++++++++ if(typeof options === 'string') {
++++++++ options = {name: options};
++++++++ }
++++++++
++++++++ var rval = null;
++++++++ var ext;
++++++++ for(var i = 0; rval === null && i < cert.extensions.length; ++i) {
++++++++ ext = cert.extensions[i];
++++++++ if(options.id && ext.id === options.id) {
++++++++ rval = ext;
++++++++ } else if(options.name && ext.name === options.name) {
++++++++ rval = ext;
++++++++ }
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Signs this certificate using the given private key.
++++++++ *
++++++++ * @param key the private key to sign with.
++++++++ * @param md the message digest object to use (defaults to forge.md.sha1).
++++++++ */
++++++++ cert.sign = function(key, md) {
++++++++ // TODO: get signature OID from private key
++++++++ cert.md = md || forge.md.sha1.create();
++++++++ var algorithmOid = oids[cert.md.algorithm + 'WithRSAEncryption'];
++++++++ if(!algorithmOid) {
++++++++ var error = new Error('Could not compute certificate digest. ' +
++++++++ 'Unknown message digest algorithm OID.');
++++++++ error.algorithm = cert.md.algorithm;
++++++++ throw error;
++++++++ }
++++++++ cert.signatureOid = cert.siginfo.algorithmOid = algorithmOid;
++++++++
++++++++ // get TBSCertificate, convert to DER
++++++++ cert.tbsCertificate = pki.getTBSCertificate(cert);
++++++++ var bytes = asn1.toDer(cert.tbsCertificate);
++++++++
++++++++ // digest and sign
++++++++ cert.md.update(bytes.getBytes());
++++++++ cert.signature = key.sign(cert.md);
++++++++ };
++++++++
++++++++ /**
++++++++ * Attempts verify the signature on the passed certificate using this
++++++++ * certificate's public key.
++++++++ *
++++++++ * @param child the certificate to verify.
++++++++ *
++++++++ * @return true if verified, false if not.
++++++++ */
++++++++ cert.verify = function(child) {
++++++++ var rval = false;
++++++++
++++++++ if(!cert.issued(child)) {
++++++++ var issuer = child.issuer;
++++++++ var subject = cert.subject;
++++++++ var error = new Error(
++++++++ 'The parent certificate did not issue the given child ' +
++++++++ 'certificate; the child certificate\'s issuer does not match the ' +
++++++++ 'parent\'s subject.');
++++++++ error.expectedIssuer = subject.attributes;
++++++++ error.actualIssuer = issuer.attributes;
++++++++ throw error;
++++++++ }
++++++++
++++++++ var md = child.md;
++++++++ if(md === null) {
++++++++ // create digest for OID signature types
++++++++ md = _createSignatureDigest({
++++++++ signatureOid: child.signatureOid,
++++++++ type: 'certificate'
++++++++ });
++++++++
++++++++ // produce DER formatted TBSCertificate and digest it
++++++++ var tbsCertificate = child.tbsCertificate || pki.getTBSCertificate(child);
++++++++ var bytes = asn1.toDer(tbsCertificate);
++++++++ md.update(bytes.getBytes());
++++++++ }
++++++++
++++++++ if(md !== null) {
++++++++ rval = _verifySignature({
++++++++ certificate: cert, md: md, signature: child.signature
++++++++ });
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Returns true if this certificate's issuer matches the passed
++++++++ * certificate's subject. Note that no signature check is performed.
++++++++ *
++++++++ * @param parent the certificate to check.
++++++++ *
++++++++ * @return true if this certificate's issuer matches the passed certificate's
++++++++ * subject.
++++++++ */
++++++++ cert.isIssuer = function(parent) {
++++++++ var rval = false;
++++++++
++++++++ var i = cert.issuer;
++++++++ var s = parent.subject;
++++++++
++++++++ // compare hashes if present
++++++++ if(i.hash && s.hash) {
++++++++ rval = (i.hash === s.hash);
++++++++ } else if(i.attributes.length === s.attributes.length) {
++++++++ // all attributes are the same so issuer matches subject
++++++++ rval = true;
++++++++ var iattr, sattr;
++++++++ for(var n = 0; rval && n < i.attributes.length; ++n) {
++++++++ iattr = i.attributes[n];
++++++++ sattr = s.attributes[n];
++++++++ if(iattr.type !== sattr.type || iattr.value !== sattr.value) {
++++++++ // attribute mismatch
++++++++ rval = false;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Returns true if this certificate's subject matches the issuer of the
++++++++ * given certificate). Note that not signature check is performed.
++++++++ *
++++++++ * @param child the certificate to check.
++++++++ *
++++++++ * @return true if this certificate's subject matches the passed
++++++++ * certificate's issuer.
++++++++ */
++++++++ cert.issued = function(child) {
++++++++ return child.isIssuer(cert);
++++++++ };
++++++++
++++++++ /**
++++++++ * Generates the subjectKeyIdentifier for this certificate as byte buffer.
++++++++ *
++++++++ * @return the subjectKeyIdentifier for this certificate as byte buffer.
++++++++ */
++++++++ cert.generateSubjectKeyIdentifier = function() {
++++++++ /* See: 4.2.1.2 section of the the RFC3280, keyIdentifier is either:
++++++++
++++++++ (1) The keyIdentifier is composed of the 160-bit SHA-1 hash of the
++++++++ value of the BIT STRING subjectPublicKey (excluding the tag,
++++++++ length, and number of unused bits).
++++++++
++++++++ (2) The keyIdentifier is composed of a four bit type field with
++++++++ the value 0100 followed by the least significant 60 bits of the
++++++++ SHA-1 hash of the value of the BIT STRING subjectPublicKey
++++++++ (excluding the tag, length, and number of unused bit string bits).
++++++++ */
++++++++
++++++++ // skipping the tag, length, and number of unused bits is the same
++++++++ // as just using the RSAPublicKey (for RSA keys, which are the
++++++++ // only ones supported)
++++++++ return pki.getPublicKeyFingerprint(cert.publicKey, {type: 'RSAPublicKey'});
++++++++ };
++++++++
++++++++ /**
++++++++ * Verifies the subjectKeyIdentifier extension value for this certificate
++++++++ * against its public key. If no extension is found, false will be
++++++++ * returned.
++++++++ *
++++++++ * @return true if verified, false if not.
++++++++ */
++++++++ cert.verifySubjectKeyIdentifier = function() {
++++++++ var oid = oids['subjectKeyIdentifier'];
++++++++ for(var i = 0; i < cert.extensions.length; ++i) {
++++++++ var ext = cert.extensions[i];
++++++++ if(ext.id === oid) {
++++++++ var ski = cert.generateSubjectKeyIdentifier().getBytes();
++++++++ return (forge.util.hexToBytes(ext.subjectKeyIdentifier) === ski);
++++++++ }
++++++++ }
++++++++ return false;
++++++++ };
++++++++
++++++++ return cert;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an X.509v3 RSA certificate from an ASN.1 object.
++++++++ *
++++++++ * Note: If the certificate is to be verified then compute hash should
++++++++ * be set to true. There is currently no implementation for converting
++++++++ * a certificate back to ASN.1 so the TBSCertificate part of the ASN.1
++++++++ * object needs to be scanned before the cert object is created.
++++++++ *
++++++++ * @param obj the asn1 representation of an X.509v3 RSA certificate.
++++++++ * @param computeHash true to compute the hash for verification.
++++++++ *
++++++++ * @return the certificate.
++++++++ */
++++++++pki.certificateFromAsn1 = function(obj, computeHash) {
++++++++ // validate certificate and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, x509CertificateValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read X.509 certificate. ' +
++++++++ 'ASN.1 object is not an X509v3 Certificate.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get oid
++++++++ var oid = asn1.derToOid(capture.publicKeyOid);
++++++++ if(oid !== pki.oids.rsaEncryption) {
++++++++ throw new Error('Cannot read public key. OID is not RSA.');
++++++++ }
++++++++
++++++++ // create certificate
++++++++ var cert = pki.createCertificate();
++++++++ cert.version = capture.certVersion ?
++++++++ capture.certVersion.charCodeAt(0) : 0;
++++++++ var serial = forge.util.createBuffer(capture.certSerialNumber);
++++++++ cert.serialNumber = serial.toHex();
++++++++ cert.signatureOid = forge.asn1.derToOid(capture.certSignatureOid);
++++++++ cert.signatureParameters = _readSignatureParameters(
++++++++ cert.signatureOid, capture.certSignatureParams, true);
++++++++ cert.siginfo.algorithmOid = forge.asn1.derToOid(capture.certinfoSignatureOid);
++++++++ cert.siginfo.parameters = _readSignatureParameters(cert.siginfo.algorithmOid,
++++++++ capture.certinfoSignatureParams, false);
++++++++ cert.signature = capture.certSignature;
++++++++
++++++++ var validity = [];
++++++++ if(capture.certValidity1UTCTime !== undefined) {
++++++++ validity.push(asn1.utcTimeToDate(capture.certValidity1UTCTime));
++++++++ }
++++++++ if(capture.certValidity2GeneralizedTime !== undefined) {
++++++++ validity.push(asn1.generalizedTimeToDate(
++++++++ capture.certValidity2GeneralizedTime));
++++++++ }
++++++++ if(capture.certValidity3UTCTime !== undefined) {
++++++++ validity.push(asn1.utcTimeToDate(capture.certValidity3UTCTime));
++++++++ }
++++++++ if(capture.certValidity4GeneralizedTime !== undefined) {
++++++++ validity.push(asn1.generalizedTimeToDate(
++++++++ capture.certValidity4GeneralizedTime));
++++++++ }
++++++++ if(validity.length > 2) {
++++++++ throw new Error('Cannot read notBefore/notAfter validity times; more ' +
++++++++ 'than two times were provided in the certificate.');
++++++++ }
++++++++ if(validity.length < 2) {
++++++++ throw new Error('Cannot read notBefore/notAfter validity times; they ' +
++++++++ 'were not provided as either UTCTime or GeneralizedTime.');
++++++++ }
++++++++ cert.validity.notBefore = validity[0];
++++++++ cert.validity.notAfter = validity[1];
++++++++
++++++++ // keep TBSCertificate to preserve signature when exporting
++++++++ cert.tbsCertificate = capture.tbsCertificate;
++++++++
++++++++ if(computeHash) {
++++++++ // create digest for OID signature type
++++++++ cert.md = _createSignatureDigest({
++++++++ signatureOid: cert.signatureOid,
++++++++ type: 'certificate'
++++++++ });
++++++++
++++++++ // produce DER formatted TBSCertificate and digest it
++++++++ var bytes = asn1.toDer(cert.tbsCertificate);
++++++++ cert.md.update(bytes.getBytes());
++++++++ }
++++++++
++++++++ // handle issuer, build issuer message digest
++++++++ var imd = forge.md.sha1.create();
++++++++ var ibytes = asn1.toDer(capture.certIssuer);
++++++++ imd.update(ibytes.getBytes());
++++++++ cert.issuer.getField = function(sn) {
++++++++ return _getAttribute(cert.issuer, sn);
++++++++ };
++++++++ cert.issuer.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ cert.issuer.attributes.push(attr);
++++++++ };
++++++++ cert.issuer.attributes = pki.RDNAttributesAsArray(capture.certIssuer);
++++++++ if(capture.certIssuerUniqueId) {
++++++++ cert.issuer.uniqueId = capture.certIssuerUniqueId;
++++++++ }
++++++++ cert.issuer.hash = imd.digest().toHex();
++++++++
++++++++ // handle subject, build subject message digest
++++++++ var smd = forge.md.sha1.create();
++++++++ var sbytes = asn1.toDer(capture.certSubject);
++++++++ smd.update(sbytes.getBytes());
++++++++ cert.subject.getField = function(sn) {
++++++++ return _getAttribute(cert.subject, sn);
++++++++ };
++++++++ cert.subject.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ cert.subject.attributes.push(attr);
++++++++ };
++++++++ cert.subject.attributes = pki.RDNAttributesAsArray(capture.certSubject);
++++++++ if(capture.certSubjectUniqueId) {
++++++++ cert.subject.uniqueId = capture.certSubjectUniqueId;
++++++++ }
++++++++ cert.subject.hash = smd.digest().toHex();
++++++++
++++++++ // handle extensions
++++++++ if(capture.certExtensions) {
++++++++ cert.extensions = pki.certificateExtensionsFromAsn1(capture.certExtensions);
++++++++ } else {
++++++++ cert.extensions = [];
++++++++ }
++++++++
++++++++ // convert RSA public key from ASN.1
++++++++ cert.publicKey = pki.publicKeyFromAsn1(capture.subjectPublicKeyInfo);
++++++++
++++++++ return cert;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an ASN.1 extensions object (with extension sequences as its
++++++++ * values) into an array of extension objects with types and values.
++++++++ *
++++++++ * Supported extensions:
++++++++ *
++++++++ * id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
++++++++ * KeyUsage ::= BIT STRING {
++++++++ * digitalSignature (0),
++++++++ * nonRepudiation (1),
++++++++ * keyEncipherment (2),
++++++++ * dataEncipherment (3),
++++++++ * keyAgreement (4),
++++++++ * keyCertSign (5),
++++++++ * cRLSign (6),
++++++++ * encipherOnly (7),
++++++++ * decipherOnly (8)
++++++++ * }
++++++++ *
++++++++ * id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
++++++++ * BasicConstraints ::= SEQUENCE {
++++++++ * cA BOOLEAN DEFAULT FALSE,
++++++++ * pathLenConstraint INTEGER (0..MAX) OPTIONAL
++++++++ * }
++++++++ *
++++++++ * subjectAltName EXTENSION ::= {
++++++++ * SYNTAX GeneralNames
++++++++ * IDENTIFIED BY id-ce-subjectAltName
++++++++ * }
++++++++ *
++++++++ * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
++++++++ *
++++++++ * GeneralName ::= CHOICE {
++++++++ * otherName [0] INSTANCE OF OTHER-NAME,
++++++++ * rfc822Name [1] IA5String,
++++++++ * dNSName [2] IA5String,
++++++++ * x400Address [3] ORAddress,
++++++++ * directoryName [4] Name,
++++++++ * ediPartyName [5] EDIPartyName,
++++++++ * uniformResourceIdentifier [6] IA5String,
++++++++ * IPAddress [7] OCTET STRING,
++++++++ * registeredID [8] OBJECT IDENTIFIER
++++++++ * }
++++++++ *
++++++++ * OTHER-NAME ::= TYPE-IDENTIFIER
++++++++ *
++++++++ * EDIPartyName ::= SEQUENCE {
++++++++ * nameAssigner [0] DirectoryString {ub-name} OPTIONAL,
++++++++ * partyName [1] DirectoryString {ub-name}
++++++++ * }
++++++++ *
++++++++ * @param exts the extensions ASN.1 with extension sequences to parse.
++++++++ *
++++++++ * @return the array.
++++++++ */
++++++++pki.certificateExtensionsFromAsn1 = function(exts) {
++++++++ var rval = [];
++++++++ for(var i = 0; i < exts.value.length; ++i) {
++++++++ // get extension sequence
++++++++ var extseq = exts.value[i];
++++++++ for(var ei = 0; ei < extseq.value.length; ++ei) {
++++++++ rval.push(pki.certificateExtensionFromAsn1(extseq.value[ei]));
++++++++ }
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Parses a single certificate extension from ASN.1.
++++++++ *
++++++++ * @param ext the extension in ASN.1 format.
++++++++ *
++++++++ * @return the parsed extension as an object.
++++++++ */
++++++++pki.certificateExtensionFromAsn1 = function(ext) {
++++++++ // an extension has:
++++++++ // [0] extnID OBJECT IDENTIFIER
++++++++ // [1] critical BOOLEAN DEFAULT FALSE
++++++++ // [2] extnValue OCTET STRING
++++++++ var e = {};
++++++++ e.id = asn1.derToOid(ext.value[0].value);
++++++++ e.critical = false;
++++++++ if(ext.value[1].type === asn1.Type.BOOLEAN) {
++++++++ e.critical = (ext.value[1].value.charCodeAt(0) !== 0x00);
++++++++ e.value = ext.value[2].value;
++++++++ } else {
++++++++ e.value = ext.value[1].value;
++++++++ }
++++++++ // if the oid is known, get its name
++++++++ if(e.id in oids) {
++++++++ e.name = oids[e.id];
++++++++
++++++++ // handle key usage
++++++++ if(e.name === 'keyUsage') {
++++++++ // get value as BIT STRING
++++++++ var ev = asn1.fromDer(e.value);
++++++++ var b2 = 0x00;
++++++++ var b3 = 0x00;
++++++++ if(ev.value.length > 1) {
++++++++ // skip first byte, just indicates unused bits which
++++++++ // will be padded with 0s anyway
++++++++ // get bytes with flag bits
++++++++ b2 = ev.value.charCodeAt(1);
++++++++ b3 = ev.value.length > 2 ? ev.value.charCodeAt(2) : 0;
++++++++ }
++++++++ // set flags
++++++++ e.digitalSignature = (b2 & 0x80) === 0x80;
++++++++ e.nonRepudiation = (b2 & 0x40) === 0x40;
++++++++ e.keyEncipherment = (b2 & 0x20) === 0x20;
++++++++ e.dataEncipherment = (b2 & 0x10) === 0x10;
++++++++ e.keyAgreement = (b2 & 0x08) === 0x08;
++++++++ e.keyCertSign = (b2 & 0x04) === 0x04;
++++++++ e.cRLSign = (b2 & 0x02) === 0x02;
++++++++ e.encipherOnly = (b2 & 0x01) === 0x01;
++++++++ e.decipherOnly = (b3 & 0x80) === 0x80;
++++++++ } else if(e.name === 'basicConstraints') {
++++++++ // handle basic constraints
++++++++ // get value as SEQUENCE
++++++++ var ev = asn1.fromDer(e.value);
++++++++ // get cA BOOLEAN flag (defaults to false)
++++++++ if(ev.value.length > 0 && ev.value[0].type === asn1.Type.BOOLEAN) {
++++++++ e.cA = (ev.value[0].value.charCodeAt(0) !== 0x00);
++++++++ } else {
++++++++ e.cA = false;
++++++++ }
++++++++ // get path length constraint
++++++++ var value = null;
++++++++ if(ev.value.length > 0 && ev.value[0].type === asn1.Type.INTEGER) {
++++++++ value = ev.value[0].value;
++++++++ } else if(ev.value.length > 1) {
++++++++ value = ev.value[1].value;
++++++++ }
++++++++ if(value !== null) {
++++++++ e.pathLenConstraint = asn1.derToInteger(value);
++++++++ }
++++++++ } else if(e.name === 'extKeyUsage') {
++++++++ // handle extKeyUsage
++++++++ // value is a SEQUENCE of OIDs
++++++++ var ev = asn1.fromDer(e.value);
++++++++ for(var vi = 0; vi < ev.value.length; ++vi) {
++++++++ var oid = asn1.derToOid(ev.value[vi].value);
++++++++ if(oid in oids) {
++++++++ e[oids[oid]] = true;
++++++++ } else {
++++++++ e[oid] = true;
++++++++ }
++++++++ }
++++++++ } else if(e.name === 'nsCertType') {
++++++++ // handle nsCertType
++++++++ // get value as BIT STRING
++++++++ var ev = asn1.fromDer(e.value);
++++++++ var b2 = 0x00;
++++++++ if(ev.value.length > 1) {
++++++++ // skip first byte, just indicates unused bits which
++++++++ // will be padded with 0s anyway
++++++++ // get bytes with flag bits
++++++++ b2 = ev.value.charCodeAt(1);
++++++++ }
++++++++ // set flags
++++++++ e.client = (b2 & 0x80) === 0x80;
++++++++ e.server = (b2 & 0x40) === 0x40;
++++++++ e.email = (b2 & 0x20) === 0x20;
++++++++ e.objsign = (b2 & 0x10) === 0x10;
++++++++ e.reserved = (b2 & 0x08) === 0x08;
++++++++ e.sslCA = (b2 & 0x04) === 0x04;
++++++++ e.emailCA = (b2 & 0x02) === 0x02;
++++++++ e.objCA = (b2 & 0x01) === 0x01;
++++++++ } else if(
++++++++ e.name === 'subjectAltName' ||
++++++++ e.name === 'issuerAltName') {
++++++++ // handle subjectAltName/issuerAltName
++++++++ e.altNames = [];
++++++++
++++++++ // ev is a SYNTAX SEQUENCE
++++++++ var gn;
++++++++ var ev = asn1.fromDer(e.value);
++++++++ for(var n = 0; n < ev.value.length; ++n) {
++++++++ // get GeneralName
++++++++ gn = ev.value[n];
++++++++
++++++++ var altName = {
++++++++ type: gn.type,
++++++++ value: gn.value
++++++++ };
++++++++ e.altNames.push(altName);
++++++++
++++++++ // Note: Support for types 1,2,6,7,8
++++++++ switch(gn.type) {
++++++++ // rfc822Name
++++++++ case 1:
++++++++ // dNSName
++++++++ case 2:
++++++++ // uniformResourceIdentifier (URI)
++++++++ case 6:
++++++++ break;
++++++++ // IPAddress
++++++++ case 7:
++++++++ // convert to IPv4/IPv6 string representation
++++++++ altName.ip = forge.util.bytesToIP(gn.value);
++++++++ break;
++++++++ // registeredID
++++++++ case 8:
++++++++ altName.oid = asn1.derToOid(gn.value);
++++++++ break;
++++++++ default:
++++++++ // unsupported
++++++++ }
++++++++ }
++++++++ } else if(e.name === 'subjectKeyIdentifier') {
++++++++ // value is an OCTETSTRING w/the hash of the key-type specific
++++++++ // public key structure (eg: RSAPublicKey)
++++++++ var ev = asn1.fromDer(e.value);
++++++++ e.subjectKeyIdentifier = forge.util.bytesToHex(ev.value);
++++++++ }
++++++++ }
++++++++ return e;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#10 certification request (CSR) from an ASN.1 object.
++++++++ *
++++++++ * Note: If the certification request is to be verified then compute hash
++++++++ * should be set to true. There is currently no implementation for converting
++++++++ * a certificate back to ASN.1 so the CertificationRequestInfo part of the
++++++++ * ASN.1 object needs to be scanned before the csr object is created.
++++++++ *
++++++++ * @param obj the asn1 representation of a PKCS#10 certification request (CSR).
++++++++ * @param computeHash true to compute the hash for verification.
++++++++ *
++++++++ * @return the certification request (CSR).
++++++++ */
++++++++pki.certificationRequestFromAsn1 = function(obj, computeHash) {
++++++++ // validate certification request and capture data
++++++++ var capture = {};
++++++++ var errors = [];
++++++++ if(!asn1.validate(obj, certificationRequestValidator, capture, errors)) {
++++++++ var error = new Error('Cannot read PKCS#10 certificate request. ' +
++++++++ 'ASN.1 object is not a PKCS#10 CertificationRequest.');
++++++++ error.errors = errors;
++++++++ throw error;
++++++++ }
++++++++
++++++++ // get oid
++++++++ var oid = asn1.derToOid(capture.publicKeyOid);
++++++++ if(oid !== pki.oids.rsaEncryption) {
++++++++ throw new Error('Cannot read public key. OID is not RSA.');
++++++++ }
++++++++
++++++++ // create certification request
++++++++ var csr = pki.createCertificationRequest();
++++++++ csr.version = capture.csrVersion ? capture.csrVersion.charCodeAt(0) : 0;
++++++++ csr.signatureOid = forge.asn1.derToOid(capture.csrSignatureOid);
++++++++ csr.signatureParameters = _readSignatureParameters(
++++++++ csr.signatureOid, capture.csrSignatureParams, true);
++++++++ csr.siginfo.algorithmOid = forge.asn1.derToOid(capture.csrSignatureOid);
++++++++ csr.siginfo.parameters = _readSignatureParameters(
++++++++ csr.siginfo.algorithmOid, capture.csrSignatureParams, false);
++++++++ csr.signature = capture.csrSignature;
++++++++
++++++++ // keep CertificationRequestInfo to preserve signature when exporting
++++++++ csr.certificationRequestInfo = capture.certificationRequestInfo;
++++++++
++++++++ if(computeHash) {
++++++++ // create digest for OID signature type
++++++++ csr.md = _createSignatureDigest({
++++++++ signatureOid: csr.signatureOid,
++++++++ type: 'certification request'
++++++++ });
++++++++
++++++++ // produce DER formatted CertificationRequestInfo and digest it
++++++++ var bytes = asn1.toDer(csr.certificationRequestInfo);
++++++++ csr.md.update(bytes.getBytes());
++++++++ }
++++++++
++++++++ // handle subject, build subject message digest
++++++++ var smd = forge.md.sha1.create();
++++++++ csr.subject.getField = function(sn) {
++++++++ return _getAttribute(csr.subject, sn);
++++++++ };
++++++++ csr.subject.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ csr.subject.attributes.push(attr);
++++++++ };
++++++++ csr.subject.attributes = pki.RDNAttributesAsArray(
++++++++ capture.certificationRequestInfoSubject, smd);
++++++++ csr.subject.hash = smd.digest().toHex();
++++++++
++++++++ // convert RSA public key from ASN.1
++++++++ csr.publicKey = pki.publicKeyFromAsn1(capture.subjectPublicKeyInfo);
++++++++
++++++++ // convert attributes from ASN.1
++++++++ csr.getAttribute = function(sn) {
++++++++ return _getAttribute(csr, sn);
++++++++ };
++++++++ csr.addAttribute = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ csr.attributes.push(attr);
++++++++ };
++++++++ csr.attributes = pki.CRIAttributesAsArray(
++++++++ capture.certificationRequestInfoAttributes || []);
++++++++
++++++++ return csr;
++++++++};
++++++++
++++++++/**
++++++++ * Creates an empty certification request (a CSR or certificate signing
++++++++ * request). Once created, its public key and attributes can be set and then
++++++++ * it can be signed.
++++++++ *
++++++++ * @return the empty certification request.
++++++++ */
++++++++pki.createCertificationRequest = function() {
++++++++ var csr = {};
++++++++ csr.version = 0x00;
++++++++ csr.signatureOid = null;
++++++++ csr.signature = null;
++++++++ csr.siginfo = {};
++++++++ csr.siginfo.algorithmOid = null;
++++++++
++++++++ csr.subject = {};
++++++++ csr.subject.getField = function(sn) {
++++++++ return _getAttribute(csr.subject, sn);
++++++++ };
++++++++ csr.subject.addField = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ csr.subject.attributes.push(attr);
++++++++ };
++++++++ csr.subject.attributes = [];
++++++++ csr.subject.hash = null;
++++++++
++++++++ csr.publicKey = null;
++++++++ csr.attributes = [];
++++++++ csr.getAttribute = function(sn) {
++++++++ return _getAttribute(csr, sn);
++++++++ };
++++++++ csr.addAttribute = function(attr) {
++++++++ _fillMissingFields([attr]);
++++++++ csr.attributes.push(attr);
++++++++ };
++++++++ csr.md = null;
++++++++
++++++++ /**
++++++++ * Sets the subject of this certification request.
++++++++ *
++++++++ * @param attrs the array of subject attributes to use.
++++++++ */
++++++++ csr.setSubject = function(attrs) {
++++++++ // set new attributes
++++++++ _fillMissingFields(attrs);
++++++++ csr.subject.attributes = attrs;
++++++++ csr.subject.hash = null;
++++++++ };
++++++++
++++++++ /**
++++++++ * Sets the attributes of this certification request.
++++++++ *
++++++++ * @param attrs the array of attributes to use.
++++++++ */
++++++++ csr.setAttributes = function(attrs) {
++++++++ // set new attributes
++++++++ _fillMissingFields(attrs);
++++++++ csr.attributes = attrs;
++++++++ };
++++++++
++++++++ /**
++++++++ * Signs this certification request using the given private key.
++++++++ *
++++++++ * @param key the private key to sign with.
++++++++ * @param md the message digest object to use (defaults to forge.md.sha1).
++++++++ */
++++++++ csr.sign = function(key, md) {
++++++++ // TODO: get signature OID from private key
++++++++ csr.md = md || forge.md.sha1.create();
++++++++ var algorithmOid = oids[csr.md.algorithm + 'WithRSAEncryption'];
++++++++ if(!algorithmOid) {
++++++++ var error = new Error('Could not compute certification request digest. ' +
++++++++ 'Unknown message digest algorithm OID.');
++++++++ error.algorithm = csr.md.algorithm;
++++++++ throw error;
++++++++ }
++++++++ csr.signatureOid = csr.siginfo.algorithmOid = algorithmOid;
++++++++
++++++++ // get CertificationRequestInfo, convert to DER
++++++++ csr.certificationRequestInfo = pki.getCertificationRequestInfo(csr);
++++++++ var bytes = asn1.toDer(csr.certificationRequestInfo);
++++++++
++++++++ // digest and sign
++++++++ csr.md.update(bytes.getBytes());
++++++++ csr.signature = key.sign(csr.md);
++++++++ };
++++++++
++++++++ /**
++++++++ * Attempts verify the signature on the passed certification request using
++++++++ * its public key.
++++++++ *
++++++++ * A CSR that has been exported to a file in PEM format can be verified using
++++++++ * OpenSSL using this command:
++++++++ *
++++++++ * openssl req -in <the-csr-pem-file> -verify -noout -text
++++++++ *
++++++++ * @return true if verified, false if not.
++++++++ */
++++++++ csr.verify = function() {
++++++++ var rval = false;
++++++++
++++++++ var md = csr.md;
++++++++ if(md === null) {
++++++++ md = _createSignatureDigest({
++++++++ signatureOid: csr.signatureOid,
++++++++ type: 'certification request'
++++++++ });
++++++++
++++++++ // produce DER formatted CertificationRequestInfo and digest it
++++++++ var cri = csr.certificationRequestInfo ||
++++++++ pki.getCertificationRequestInfo(csr);
++++++++ var bytes = asn1.toDer(cri);
++++++++ md.update(bytes.getBytes());
++++++++ }
++++++++
++++++++ if(md !== null) {
++++++++ rval = _verifySignature({
++++++++ certificate: csr, md: md, signature: csr.signature
++++++++ });
++++++++ }
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ return csr;
++++++++};
++++++++
++++++++/**
++++++++ * Converts an X.509 subject or issuer to an ASN.1 RDNSequence.
++++++++ *
++++++++ * @param obj the subject or issuer (distinguished name).
++++++++ *
++++++++ * @return the ASN.1 RDNSequence.
++++++++ */
++++++++function _dnToAsn1(obj) {
++++++++ // create an empty RDNSequence
++++++++ var rval = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++
++++++++ // iterate over attributes
++++++++ var attr, set;
++++++++ var attrs = obj.attributes;
++++++++ for(var i = 0; i < attrs.length; ++i) {
++++++++ attr = attrs[i];
++++++++ var value = attr.value;
++++++++
++++++++ // reuse tag class for attribute value if available
++++++++ var valueTagClass = asn1.Type.PRINTABLESTRING;
++++++++ if('valueTagClass' in attr) {
++++++++ valueTagClass = attr.valueTagClass;
++++++++
++++++++ if(valueTagClass === asn1.Type.UTF8) {
++++++++ value = forge.util.encodeUtf8(value);
++++++++ }
++++++++ // FIXME: handle more encodings
++++++++ }
++++++++
++++++++ // create a RelativeDistinguishedName set
++++++++ // each value in the set is an AttributeTypeAndValue first
++++++++ // containing the type (an OID) and second the value
++++++++ set = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // AttributeType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(attr.type).getBytes()),
++++++++ // AttributeValue
++++++++ asn1.create(asn1.Class.UNIVERSAL, valueTagClass, false, value)
++++++++ ])
++++++++ ]);
++++++++ rval.value.push(set);
++++++++ }
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Gets all printable attributes (typically of an issuer or subject) in a
++++++++ * simplified JSON format for display.
++++++++ *
++++++++ * @param attrs the attributes.
++++++++ *
++++++++ * @return the JSON for display.
++++++++ */
++++++++function _getAttributesAsJson(attrs) {
++++++++ var rval = {};
++++++++ for(var i = 0; i < attrs.length; ++i) {
++++++++ var attr = attrs[i];
++++++++ if(attr.shortName && (
++++++++ attr.valueTagClass === asn1.Type.UTF8 ||
++++++++ attr.valueTagClass === asn1.Type.PRINTABLESTRING ||
++++++++ attr.valueTagClass === asn1.Type.IA5STRING)) {
++++++++ var value = attr.value;
++++++++ if(attr.valueTagClass === asn1.Type.UTF8) {
++++++++ value = forge.util.encodeUtf8(attr.value);
++++++++ }
++++++++ if(!(attr.shortName in rval)) {
++++++++ rval[attr.shortName] = value;
++++++++ } else if(forge.util.isArray(rval[attr.shortName])) {
++++++++ rval[attr.shortName].push(value);
++++++++ } else {
++++++++ rval[attr.shortName] = [rval[attr.shortName], value];
++++++++ }
++++++++ }
++++++++ }
++++++++ return rval;
++++++++}
++++++++
++++++++/**
++++++++ * Fills in missing fields in attributes.
++++++++ *
++++++++ * @param attrs the attributes to fill missing fields in.
++++++++ */
++++++++function _fillMissingFields(attrs) {
++++++++ var attr;
++++++++ for(var i = 0; i < attrs.length; ++i) {
++++++++ attr = attrs[i];
++++++++
++++++++ // populate missing name
++++++++ if(typeof attr.name === 'undefined') {
++++++++ if(attr.type && attr.type in pki.oids) {
++++++++ attr.name = pki.oids[attr.type];
++++++++ } else if(attr.shortName && attr.shortName in _shortNames) {
++++++++ attr.name = pki.oids[_shortNames[attr.shortName]];
++++++++ }
++++++++ }
++++++++
++++++++ // populate missing type (OID)
++++++++ if(typeof attr.type === 'undefined') {
++++++++ if(attr.name && attr.name in pki.oids) {
++++++++ attr.type = pki.oids[attr.name];
++++++++ } else {
++++++++ var error = new Error('Attribute type not specified.');
++++++++ error.attribute = attr;
++++++++ throw error;
++++++++ }
++++++++ }
++++++++
++++++++ // populate missing shortname
++++++++ if(typeof attr.shortName === 'undefined') {
++++++++ if(attr.name && attr.name in _shortNames) {
++++++++ attr.shortName = _shortNames[attr.name];
++++++++ }
++++++++ }
++++++++
++++++++ // convert extensions to value
++++++++ if(attr.type === oids.extensionRequest) {
++++++++ attr.valueConstructed = true;
++++++++ attr.valueTagClass = asn1.Type.SEQUENCE;
++++++++ if(!attr.value && attr.extensions) {
++++++++ attr.value = [];
++++++++ for(var ei = 0; ei < attr.extensions.length; ++ei) {
++++++++ attr.value.push(pki.certificateExtensionToAsn1(
++++++++ _fillMissingExtensionFields(attr.extensions[ei])));
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ if(typeof attr.value === 'undefined') {
++++++++ var error = new Error('Attribute value not specified.');
++++++++ error.attribute = attr;
++++++++ throw error;
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Fills in missing fields in certificate extensions.
++++++++ *
++++++++ * @param e the extension.
++++++++ * @param [options] the options to use.
++++++++ * [cert] the certificate the extensions are for.
++++++++ *
++++++++ * @return the extension.
++++++++ */
++++++++function _fillMissingExtensionFields(e, options) {
++++++++ options = options || {};
++++++++
++++++++ // populate missing name
++++++++ if(typeof e.name === 'undefined') {
++++++++ if(e.id && e.id in pki.oids) {
++++++++ e.name = pki.oids[e.id];
++++++++ }
++++++++ }
++++++++
++++++++ // populate missing id
++++++++ if(typeof e.id === 'undefined') {
++++++++ if(e.name && e.name in pki.oids) {
++++++++ e.id = pki.oids[e.name];
++++++++ } else {
++++++++ var error = new Error('Extension ID not specified.');
++++++++ error.extension = e;
++++++++ throw error;
++++++++ }
++++++++ }
++++++++
++++++++ if(typeof e.value !== 'undefined') {
++++++++ return e;
++++++++ }
++++++++
++++++++ // handle missing value:
++++++++
++++++++ // value is a BIT STRING
++++++++ if(e.name === 'keyUsage') {
++++++++ // build flags
++++++++ var unused = 0;
++++++++ var b2 = 0x00;
++++++++ var b3 = 0x00;
++++++++ if(e.digitalSignature) {
++++++++ b2 |= 0x80;
++++++++ unused = 7;
++++++++ }
++++++++ if(e.nonRepudiation) {
++++++++ b2 |= 0x40;
++++++++ unused = 6;
++++++++ }
++++++++ if(e.keyEncipherment) {
++++++++ b2 |= 0x20;
++++++++ unused = 5;
++++++++ }
++++++++ if(e.dataEncipherment) {
++++++++ b2 |= 0x10;
++++++++ unused = 4;
++++++++ }
++++++++ if(e.keyAgreement) {
++++++++ b2 |= 0x08;
++++++++ unused = 3;
++++++++ }
++++++++ if(e.keyCertSign) {
++++++++ b2 |= 0x04;
++++++++ unused = 2;
++++++++ }
++++++++ if(e.cRLSign) {
++++++++ b2 |= 0x02;
++++++++ unused = 1;
++++++++ }
++++++++ if(e.encipherOnly) {
++++++++ b2 |= 0x01;
++++++++ unused = 0;
++++++++ }
++++++++ if(e.decipherOnly) {
++++++++ b3 |= 0x80;
++++++++ unused = 7;
++++++++ }
++++++++
++++++++ // create bit string
++++++++ var value = String.fromCharCode(unused);
++++++++ if(b3 !== 0) {
++++++++ value += String.fromCharCode(b2) + String.fromCharCode(b3);
++++++++ } else if(b2 !== 0) {
++++++++ value += String.fromCharCode(b2);
++++++++ }
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, value);
++++++++ } else if(e.name === 'basicConstraints') {
++++++++ // basicConstraints is a SEQUENCE
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ // cA BOOLEAN flag defaults to false
++++++++ if(e.cA) {
++++++++ e.value.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.BOOLEAN, false,
++++++++ String.fromCharCode(0xFF)));
++++++++ }
++++++++ if('pathLenConstraint' in e) {
++++++++ e.value.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(e.pathLenConstraint).getBytes()));
++++++++ }
++++++++ } else if(e.name === 'extKeyUsage') {
++++++++ // extKeyUsage is a SEQUENCE of OIDs
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ var seq = e.value.value;
++++++++ for(var key in e) {
++++++++ if(e[key] !== true) {
++++++++ continue;
++++++++ }
++++++++ // key is name in OID map
++++++++ if(key in oids) {
++++++++ seq.push(asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID,
++++++++ false, asn1.oidToDer(oids[key]).getBytes()));
++++++++ } else if(key.indexOf('.') !== -1) {
++++++++ // assume key is an OID
++++++++ seq.push(asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID,
++++++++ false, asn1.oidToDer(key).getBytes()));
++++++++ }
++++++++ }
++++++++ } else if(e.name === 'nsCertType') {
++++++++ // nsCertType is a BIT STRING
++++++++ // build flags
++++++++ var unused = 0;
++++++++ var b2 = 0x00;
++++++++
++++++++ if(e.client) {
++++++++ b2 |= 0x80;
++++++++ unused = 7;
++++++++ }
++++++++ if(e.server) {
++++++++ b2 |= 0x40;
++++++++ unused = 6;
++++++++ }
++++++++ if(e.email) {
++++++++ b2 |= 0x20;
++++++++ unused = 5;
++++++++ }
++++++++ if(e.objsign) {
++++++++ b2 |= 0x10;
++++++++ unused = 4;
++++++++ }
++++++++ if(e.reserved) {
++++++++ b2 |= 0x08;
++++++++ unused = 3;
++++++++ }
++++++++ if(e.sslCA) {
++++++++ b2 |= 0x04;
++++++++ unused = 2;
++++++++ }
++++++++ if(e.emailCA) {
++++++++ b2 |= 0x02;
++++++++ unused = 1;
++++++++ }
++++++++ if(e.objCA) {
++++++++ b2 |= 0x01;
++++++++ unused = 0;
++++++++ }
++++++++
++++++++ // create bit string
++++++++ var value = String.fromCharCode(unused);
++++++++ if(b2 !== 0) {
++++++++ value += String.fromCharCode(b2);
++++++++ }
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false, value);
++++++++ } else if(e.name === 'subjectAltName' || e.name === 'issuerAltName') {
++++++++ // SYNTAX SEQUENCE
++++++++ e.value = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++
++++++++ var altName;
++++++++ for(var n = 0; n < e.altNames.length; ++n) {
++++++++ altName = e.altNames[n];
++++++++ var value = altName.value;
++++++++ // handle IP
++++++++ if(altName.type === 7 && altName.ip) {
++++++++ value = forge.util.bytesFromIP(altName.ip);
++++++++ if(value === null) {
++++++++ var error = new Error(
++++++++ 'Extension "ip" value is not a valid IPv4 or IPv6 address.');
++++++++ error.extension = e;
++++++++ throw error;
++++++++ }
++++++++ } else if(altName.type === 8) {
++++++++ // handle OID
++++++++ if(altName.oid) {
++++++++ value = asn1.oidToDer(asn1.oidToDer(altName.oid));
++++++++ } else {
++++++++ // deprecated ... convert value to OID
++++++++ value = asn1.oidToDer(value);
++++++++ }
++++++++ }
++++++++ e.value.value.push(asn1.create(
++++++++ asn1.Class.CONTEXT_SPECIFIC, altName.type, false,
++++++++ value));
++++++++ }
++++++++ } else if(e.name === 'nsComment' && options.cert) {
++++++++ // sanity check value is ASCII (req'd) and not too big
++++++++ if(!(/^[\x00-\x7F]*$/.test(e.comment)) ||
++++++++ (e.comment.length < 1) || (e.comment.length > 128)) {
++++++++ throw new Error('Invalid "nsComment" content.');
++++++++ }
++++++++ // IA5STRING opaque comment
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.IA5STRING, false, e.comment);
++++++++ } else if(e.name === 'subjectKeyIdentifier' && options.cert) {
++++++++ var ski = options.cert.generateSubjectKeyIdentifier();
++++++++ e.subjectKeyIdentifier = ski.toHex();
++++++++ // OCTETSTRING w/digest
++++++++ e.value = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, ski.getBytes());
++++++++ } else if(e.name === 'authorityKeyIdentifier' && options.cert) {
++++++++ // SYNTAX SEQUENCE
++++++++ e.value = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ var seq = e.value.value;
++++++++
++++++++ if(e.keyIdentifier) {
++++++++ var keyIdentifier = (e.keyIdentifier === true ?
++++++++ options.cert.generateSubjectKeyIdentifier().getBytes() :
++++++++ e.keyIdentifier);
++++++++ seq.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, false, keyIdentifier));
++++++++ }
++++++++
++++++++ if(e.authorityCertIssuer) {
++++++++ var authorityCertIssuer = [
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 4, true, [
++++++++ _dnToAsn1(e.authorityCertIssuer === true ?
++++++++ options.cert.issuer : e.authorityCertIssuer)
++++++++ ])
++++++++ ];
++++++++ seq.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, authorityCertIssuer));
++++++++ }
++++++++
++++++++ if(e.serialNumber) {
++++++++ var serialNumber = forge.util.hexToBytes(e.serialNumber === true ?
++++++++ options.cert.serialNumber : e.serialNumber);
++++++++ seq.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 2, false, serialNumber));
++++++++ }
++++++++ } else if(e.name === 'cRLDistributionPoints') {
++++++++ e.value = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ var seq = e.value.value;
++++++++
++++++++ // Create sub SEQUENCE of DistributionPointName
++++++++ var subSeq = asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++
++++++++ // Create fullName CHOICE
++++++++ var fullNameGeneralNames = asn1.create(
++++++++ asn1.Class.CONTEXT_SPECIFIC, 0, true, []);
++++++++ var altName;
++++++++ for(var n = 0; n < e.altNames.length; ++n) {
++++++++ altName = e.altNames[n];
++++++++ var value = altName.value;
++++++++ // handle IP
++++++++ if(altName.type === 7 && altName.ip) {
++++++++ value = forge.util.bytesFromIP(altName.ip);
++++++++ if(value === null) {
++++++++ var error = new Error(
++++++++ 'Extension "ip" value is not a valid IPv4 or IPv6 address.');
++++++++ error.extension = e;
++++++++ throw error;
++++++++ }
++++++++ } else if(altName.type === 8) {
++++++++ // handle OID
++++++++ if(altName.oid) {
++++++++ value = asn1.oidToDer(asn1.oidToDer(altName.oid));
++++++++ } else {
++++++++ // deprecated ... convert value to OID
++++++++ value = asn1.oidToDer(value);
++++++++ }
++++++++ }
++++++++ fullNameGeneralNames.value.push(asn1.create(
++++++++ asn1.Class.CONTEXT_SPECIFIC, altName.type, false,
++++++++ value));
++++++++ }
++++++++
++++++++ // Add to the parent SEQUENCE
++++++++ subSeq.value.push(asn1.create(
++++++++ asn1.Class.CONTEXT_SPECIFIC, 0, true, [fullNameGeneralNames]));
++++++++ seq.push(subSeq);
++++++++ }
++++++++
++++++++ // ensure value has been defined by now
++++++++ if(typeof e.value === 'undefined') {
++++++++ var error = new Error('Extension value not specified.');
++++++++ error.extension = e;
++++++++ throw error;
++++++++ }
++++++++
++++++++ return e;
++++++++}
++++++++
++++++++/**
++++++++ * Convert signature parameters object to ASN.1
++++++++ *
++++++++ * @param {String} oid Signature algorithm OID
++++++++ * @param params The signature parametrs object
++++++++ * @return ASN.1 object representing signature parameters
++++++++ */
++++++++function _signatureParametersToAsn1(oid, params) {
++++++++ switch(oid) {
++++++++ case oids['RSASSA-PSS']:
++++++++ var parts = [];
++++++++
++++++++ if(params.hash.algorithmOid !== undefined) {
++++++++ parts.push(asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(params.hash.algorithmOid).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ])
++++++++ ]));
++++++++ }
++++++++
++++++++ if(params.mgf.algorithmOid !== undefined) {
++++++++ parts.push(asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(params.mgf.algorithmOid).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(params.mgf.hash.algorithmOid).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '')
++++++++ ])
++++++++ ])
++++++++ ]));
++++++++ }
++++++++
++++++++ if(params.saltLength !== undefined) {
++++++++ parts.push(asn1.create(asn1.Class.CONTEXT_SPECIFIC, 2, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(params.saltLength).getBytes())
++++++++ ]));
++++++++ }
++++++++
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, parts);
++++++++
++++++++ default:
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.NULL, false, '');
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Converts a certification request's attributes to an ASN.1 set of
++++++++ * CRIAttributes.
++++++++ *
++++++++ * @param csr certification request.
++++++++ *
++++++++ * @return the ASN.1 set of CRIAttributes.
++++++++ */
++++++++function _CRIAttributesToAsn1(csr) {
++++++++ // create an empty context-specific container
++++++++ var rval = asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, []);
++++++++
++++++++ // no attributes, return empty container
++++++++ if(csr.attributes.length === 0) {
++++++++ return rval;
++++++++ }
++++++++
++++++++ // each attribute has a sequence with a type and a set of values
++++++++ var attrs = csr.attributes;
++++++++ for(var i = 0; i < attrs.length; ++i) {
++++++++ var attr = attrs[i];
++++++++ var value = attr.value;
++++++++
++++++++ // reuse tag class for attribute value if available
++++++++ var valueTagClass = asn1.Type.UTF8;
++++++++ if('valueTagClass' in attr) {
++++++++ valueTagClass = attr.valueTagClass;
++++++++ }
++++++++ if(valueTagClass === asn1.Type.UTF8) {
++++++++ value = forge.util.encodeUtf8(value);
++++++++ }
++++++++ var valueConstructed = false;
++++++++ if('valueConstructed' in attr) {
++++++++ valueConstructed = attr.valueConstructed;
++++++++ }
++++++++ // FIXME: handle more encodings
++++++++
++++++++ // create a RelativeDistinguishedName set
++++++++ // each value in the set is an AttributeTypeAndValue first
++++++++ // containing the type (an OID) and second the value
++++++++ var seq = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // AttributeType
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(attr.type).getBytes()),
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SET, true, [
++++++++ // AttributeValue
++++++++ asn1.create(
++++++++ asn1.Class.UNIVERSAL, valueTagClass, valueConstructed, value)
++++++++ ])
++++++++ ]);
++++++++ rval.value.push(seq);
++++++++ }
++++++++
++++++++ return rval;
++++++++}
++++++++
++++++++var jan_1_1950 = new Date('1950-01-01T00:00:00Z');
++++++++var jan_1_2050 = new Date('2050-01-01T00:00:00Z');
++++++++
++++++++/**
++++++++ * Converts a Date object to ASN.1
++++++++ * Handles the different format before and after 1st January 2050
++++++++ *
++++++++ * @param date date object.
++++++++ *
++++++++ * @return the ASN.1 object representing the date.
++++++++ */
++++++++function _dateToAsn1(date) {
++++++++ if(date >= jan_1_1950 && date < jan_1_2050) {
++++++++ return asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.UTCTIME, false,
++++++++ asn1.dateToUtcTime(date));
++++++++ } else {
++++++++ return asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.GENERALIZEDTIME, false,
++++++++ asn1.dateToGeneralizedTime(date));
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ * Gets the ASN.1 TBSCertificate part of an X.509v3 certificate.
++++++++ *
++++++++ * @param cert the certificate.
++++++++ *
++++++++ * @return the asn1 TBSCertificate.
++++++++ */
++++++++pki.getTBSCertificate = function(cert) {
++++++++ // TBSCertificate
++++++++ var notBefore = _dateToAsn1(cert.validity.notBefore);
++++++++ var notAfter = _dateToAsn1(cert.validity.notAfter);
++++++++ var tbs = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 0, true, [
++++++++ // integer
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(cert.version).getBytes())
++++++++ ]),
++++++++ // serialNumber
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ forge.util.hexToBytes(cert.serialNumber)),
++++++++ // signature
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(cert.siginfo.algorithmOid).getBytes()),
++++++++ // parameters
++++++++ _signatureParametersToAsn1(
++++++++ cert.siginfo.algorithmOid, cert.siginfo.parameters)
++++++++ ]),
++++++++ // issuer
++++++++ _dnToAsn1(cert.issuer),
++++++++ // validity
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ notBefore,
++++++++ notAfter
++++++++ ]),
++++++++ // subject
++++++++ _dnToAsn1(cert.subject),
++++++++ // SubjectPublicKeyInfo
++++++++ pki.publicKeyToAsn1(cert.publicKey)
++++++++ ]);
++++++++
++++++++ if(cert.issuer.uniqueId) {
++++++++ // issuerUniqueID (optional)
++++++++ tbs.value.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 1, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false,
++++++++ // TODO: support arbitrary bit length ids
++++++++ String.fromCharCode(0x00) +
++++++++ cert.issuer.uniqueId
++++++++ )
++++++++ ])
++++++++ );
++++++++ }
++++++++ if(cert.subject.uniqueId) {
++++++++ // subjectUniqueID (optional)
++++++++ tbs.value.push(
++++++++ asn1.create(asn1.Class.CONTEXT_SPECIFIC, 2, true, [
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false,
++++++++ // TODO: support arbitrary bit length ids
++++++++ String.fromCharCode(0x00) +
++++++++ cert.subject.uniqueId
++++++++ )
++++++++ ])
++++++++ );
++++++++ }
++++++++
++++++++ if(cert.extensions.length > 0) {
++++++++ // extensions (optional)
++++++++ tbs.value.push(pki.certificateExtensionsToAsn1(cert.extensions));
++++++++ }
++++++++
++++++++ return tbs;
++++++++};
++++++++
++++++++/**
++++++++ * Gets the ASN.1 CertificationRequestInfo part of a
++++++++ * PKCS#10 CertificationRequest.
++++++++ *
++++++++ * @param csr the certification request.
++++++++ *
++++++++ * @return the asn1 CertificationRequestInfo.
++++++++ */
++++++++pki.getCertificationRequestInfo = function(csr) {
++++++++ // CertificationRequestInfo
++++++++ var cri = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // version
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.INTEGER, false,
++++++++ asn1.integerToDer(csr.version).getBytes()),
++++++++ // subject
++++++++ _dnToAsn1(csr.subject),
++++++++ // SubjectPublicKeyInfo
++++++++ pki.publicKeyToAsn1(csr.publicKey),
++++++++ // attributes
++++++++ _CRIAttributesToAsn1(csr)
++++++++ ]);
++++++++
++++++++ return cri;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a DistinguishedName (subject or issuer) to an ASN.1 object.
++++++++ *
++++++++ * @param dn the DistinguishedName.
++++++++ *
++++++++ * @return the asn1 representation of a DistinguishedName.
++++++++ */
++++++++pki.distinguishedNameToAsn1 = function(dn) {
++++++++ return _dnToAsn1(dn);
++++++++};
++++++++
++++++++/**
++++++++ * Converts an X.509v3 RSA certificate to an ASN.1 object.
++++++++ *
++++++++ * @param cert the certificate.
++++++++ *
++++++++ * @return the asn1 representation of an X.509v3 RSA certificate.
++++++++ */
++++++++pki.certificateToAsn1 = function(cert) {
++++++++ // prefer cached TBSCertificate over generating one
++++++++ var tbsCertificate = cert.tbsCertificate || pki.getTBSCertificate(cert);
++++++++
++++++++ // Certificate
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // TBSCertificate
++++++++ tbsCertificate,
++++++++ // AlgorithmIdentifier (signature algorithm)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(cert.signatureOid).getBytes()),
++++++++ // parameters
++++++++ _signatureParametersToAsn1(cert.signatureOid, cert.signatureParameters)
++++++++ ]),
++++++++ // SignatureValue
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false,
++++++++ String.fromCharCode(0x00) + cert.signature)
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Converts X.509v3 certificate extensions to ASN.1.
++++++++ *
++++++++ * @param exts the extensions to convert.
++++++++ *
++++++++ * @return the extensions in ASN.1 format.
++++++++ */
++++++++pki.certificateExtensionsToAsn1 = function(exts) {
++++++++ // create top-level extension container
++++++++ var rval = asn1.create(asn1.Class.CONTEXT_SPECIFIC, 3, true, []);
++++++++
++++++++ // create extension sequence (stores a sequence for each extension)
++++++++ var seq = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++ rval.value.push(seq);
++++++++
++++++++ for(var i = 0; i < exts.length; ++i) {
++++++++ seq.value.push(pki.certificateExtensionToAsn1(exts[i]));
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a single certificate extension to ASN.1.
++++++++ *
++++++++ * @param ext the extension to convert.
++++++++ *
++++++++ * @return the extension in ASN.1 format.
++++++++ */
++++++++pki.certificateExtensionToAsn1 = function(ext) {
++++++++ // create a sequence for each extension
++++++++ var extseq = asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, []);
++++++++
++++++++ // extnID (OID)
++++++++ extseq.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(ext.id).getBytes()));
++++++++
++++++++ // critical defaults to false
++++++++ if(ext.critical) {
++++++++ // critical BOOLEAN DEFAULT FALSE
++++++++ extseq.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.BOOLEAN, false,
++++++++ String.fromCharCode(0xFF)));
++++++++ }
++++++++
++++++++ var value = ext.value;
++++++++ if(typeof ext.value !== 'string') {
++++++++ // value is asn.1
++++++++ value = asn1.toDer(value).getBytes();
++++++++ }
++++++++
++++++++ // extnValue (OCTET STRING)
++++++++ extseq.value.push(asn1.create(
++++++++ asn1.Class.UNIVERSAL, asn1.Type.OCTETSTRING, false, value));
++++++++
++++++++ return extseq;
++++++++};
++++++++
++++++++/**
++++++++ * Converts a PKCS#10 certification request to an ASN.1 object.
++++++++ *
++++++++ * @param csr the certification request.
++++++++ *
++++++++ * @return the asn1 representation of a certification request.
++++++++ */
++++++++pki.certificationRequestToAsn1 = function(csr) {
++++++++ // prefer cached CertificationRequestInfo over generating one
++++++++ var cri = csr.certificationRequestInfo ||
++++++++ pki.getCertificationRequestInfo(csr);
++++++++
++++++++ // Certificate
++++++++ return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // CertificationRequestInfo
++++++++ cri,
++++++++ // AlgorithmIdentifier (signature algorithm)
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
++++++++ // algorithm
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.OID, false,
++++++++ asn1.oidToDer(csr.signatureOid).getBytes()),
++++++++ // parameters
++++++++ _signatureParametersToAsn1(csr.signatureOid, csr.signatureParameters)
++++++++ ]),
++++++++ // signature
++++++++ asn1.create(asn1.Class.UNIVERSAL, asn1.Type.BITSTRING, false,
++++++++ String.fromCharCode(0x00) + csr.signature)
++++++++ ]);
++++++++};
++++++++
++++++++/**
++++++++ * Creates a CA store.
++++++++ *
++++++++ * @param certs an optional array of certificate objects or PEM-formatted
++++++++ * certificate strings to add to the CA store.
++++++++ *
++++++++ * @return the CA store.
++++++++ */
++++++++pki.createCaStore = function(certs) {
++++++++ // create CA store
++++++++ var caStore = {
++++++++ // stored certificates
++++++++ certs: {}
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets the certificate that issued the passed certificate or its
++++++++ * 'parent'.
++++++++ *
++++++++ * @param cert the certificate to get the parent for.
++++++++ *
++++++++ * @return the parent certificate or null if none was found.
++++++++ */
++++++++ caStore.getIssuer = function(cert) {
++++++++ var rval = getBySubject(cert.issuer);
++++++++
++++++++ // see if there are multiple matches
++++++++ /*if(forge.util.isArray(rval)) {
++++++++ // TODO: resolve multiple matches by checking
++++++++ // authorityKey/subjectKey/issuerUniqueID/other identifiers, etc.
++++++++ // FIXME: or alternatively do authority key mapping
++++++++ // if possible (X.509v1 certs can't work?)
++++++++ throw new Error('Resolving multiple issuer matches not implemented yet.');
++++++++ }*/
++++++++
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Adds a trusted certificate to the store.
++++++++ *
++++++++ * @param cert the certificate to add as a trusted certificate (either a
++++++++ * pki.certificate object or a PEM-formatted certificate).
++++++++ */
++++++++ caStore.addCertificate = function(cert) {
++++++++ // convert from pem if necessary
++++++++ if(typeof cert === 'string') {
++++++++ cert = forge.pki.certificateFromPem(cert);
++++++++ }
++++++++
++++++++ ensureSubjectHasHash(cert.subject);
++++++++
++++++++ if(!caStore.hasCertificate(cert)) { // avoid duplicate certificates in store
++++++++ if(cert.subject.hash in caStore.certs) {
++++++++ // subject hash already exists, append to array
++++++++ var tmp = caStore.certs[cert.subject.hash];
++++++++ if(!forge.util.isArray(tmp)) {
++++++++ tmp = [tmp];
++++++++ }
++++++++ tmp.push(cert);
++++++++ caStore.certs[cert.subject.hash] = tmp;
++++++++ } else {
++++++++ caStore.certs[cert.subject.hash] = cert;
++++++++ }
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Checks to see if the given certificate is in the store.
++++++++ *
++++++++ * @param cert the certificate to check (either a pki.certificate or a
++++++++ * PEM-formatted certificate).
++++++++ *
++++++++ * @return true if the certificate is in the store, false if not.
++++++++ */
++++++++ caStore.hasCertificate = function(cert) {
++++++++ // convert from pem if necessary
++++++++ if(typeof cert === 'string') {
++++++++ cert = forge.pki.certificateFromPem(cert);
++++++++ }
++++++++
++++++++ var match = getBySubject(cert.subject);
++++++++ if(!match) {
++++++++ return false;
++++++++ }
++++++++ if(!forge.util.isArray(match)) {
++++++++ match = [match];
++++++++ }
++++++++ // compare DER-encoding of certificates
++++++++ var der1 = asn1.toDer(pki.certificateToAsn1(cert)).getBytes();
++++++++ for(var i = 0; i < match.length; ++i) {
++++++++ var der2 = asn1.toDer(pki.certificateToAsn1(match[i])).getBytes();
++++++++ if(der1 === der2) {
++++++++ return true;
++++++++ }
++++++++ }
++++++++ return false;
++++++++ };
++++++++
++++++++ /**
++++++++ * Lists all of the certificates kept in the store.
++++++++ *
++++++++ * @return an array of all of the pki.certificate objects in the store.
++++++++ */
++++++++ caStore.listAllCertificates = function() {
++++++++ var certList = [];
++++++++
++++++++ for(var hash in caStore.certs) {
++++++++ if(caStore.certs.hasOwnProperty(hash)) {
++++++++ var value = caStore.certs[hash];
++++++++ if(!forge.util.isArray(value)) {
++++++++ certList.push(value);
++++++++ } else {
++++++++ for(var i = 0; i < value.length; ++i) {
++++++++ certList.push(value[i]);
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ return certList;
++++++++ };
++++++++
++++++++ /**
++++++++ * Removes a certificate from the store.
++++++++ *
++++++++ * @param cert the certificate to remove (either a pki.certificate or a
++++++++ * PEM-formatted certificate).
++++++++ *
++++++++ * @return the certificate that was removed or null if the certificate
++++++++ * wasn't in store.
++++++++ */
++++++++ caStore.removeCertificate = function(cert) {
++++++++ var result;
++++++++
++++++++ // convert from pem if necessary
++++++++ if(typeof cert === 'string') {
++++++++ cert = forge.pki.certificateFromPem(cert);
++++++++ }
++++++++ ensureSubjectHasHash(cert.subject);
++++++++ if(!caStore.hasCertificate(cert)) {
++++++++ return null;
++++++++ }
++++++++
++++++++ var match = getBySubject(cert.subject);
++++++++
++++++++ if(!forge.util.isArray(match)) {
++++++++ result = caStore.certs[cert.subject.hash];
++++++++ delete caStore.certs[cert.subject.hash];
++++++++ return result;
++++++++ }
++++++++
++++++++ // compare DER-encoding of certificates
++++++++ var der1 = asn1.toDer(pki.certificateToAsn1(cert)).getBytes();
++++++++ for(var i = 0; i < match.length; ++i) {
++++++++ var der2 = asn1.toDer(pki.certificateToAsn1(match[i])).getBytes();
++++++++ if(der1 === der2) {
++++++++ result = match[i];
++++++++ match.splice(i, 1);
++++++++ }
++++++++ }
++++++++ if(match.length === 0) {
++++++++ delete caStore.certs[cert.subject.hash];
++++++++ }
++++++++
++++++++ return result;
++++++++ };
++++++++
++++++++ function getBySubject(subject) {
++++++++ ensureSubjectHasHash(subject);
++++++++ return caStore.certs[subject.hash] || null;
++++++++ }
++++++++
++++++++ function ensureSubjectHasHash(subject) {
++++++++ // produce subject hash if it doesn't exist
++++++++ if(!subject.hash) {
++++++++ var md = forge.md.sha1.create();
++++++++ subject.attributes = pki.RDNAttributesAsArray(_dnToAsn1(subject), md);
++++++++ subject.hash = md.digest().toHex();
++++++++ }
++++++++ }
++++++++
++++++++ // auto-add passed in certs
++++++++ if(certs) {
++++++++ // parse PEM-formatted certificates as necessary
++++++++ for(var i = 0; i < certs.length; ++i) {
++++++++ var cert = certs[i];
++++++++ caStore.addCertificate(cert);
++++++++ }
++++++++ }
++++++++
++++++++ return caStore;
++++++++};
++++++++
++++++++/**
++++++++ * Certificate verification errors, based on TLS.
++++++++ */
++++++++pki.certificateError = {
++++++++ bad_certificate: 'forge.pki.BadCertificate',
++++++++ unsupported_certificate: 'forge.pki.UnsupportedCertificate',
++++++++ certificate_revoked: 'forge.pki.CertificateRevoked',
++++++++ certificate_expired: 'forge.pki.CertificateExpired',
++++++++ certificate_unknown: 'forge.pki.CertificateUnknown',
++++++++ unknown_ca: 'forge.pki.UnknownCertificateAuthority'
++++++++};
++++++++
++++++++/**
++++++++ * Verifies a certificate chain against the given Certificate Authority store
++++++++ * with an optional custom verify callback.
++++++++ *
++++++++ * @param caStore a certificate store to verify against.
++++++++ * @param chain the certificate chain to verify, with the root or highest
++++++++ * authority at the end (an array of certificates).
++++++++ * @param options a callback to be called for every certificate in the chain or
++++++++ * an object with:
++++++++ * verify a callback to be called for every certificate in the
++++++++ * chain
++++++++ * validityCheckDate the date against which the certificate
++++++++ * validity period should be checked. Pass null to not check
++++++++ * the validity period. By default, the current date is used.
++++++++ *
++++++++ * The verify callback has the following signature:
++++++++ *
++++++++ * verified - Set to true if certificate was verified, otherwise the
++++++++ * pki.certificateError for why the certificate failed.
++++++++ * depth - The current index in the chain, where 0 is the end point's cert.
++++++++ * certs - The certificate chain, *NOTE* an empty chain indicates an anonymous
++++++++ * end point.
++++++++ *
++++++++ * The function returns true on success and on failure either the appropriate
++++++++ * pki.certificateError or an object with 'error' set to the appropriate
++++++++ * pki.certificateError and 'message' set to a custom error message.
++++++++ *
++++++++ * @return true if successful, error thrown if not.
++++++++ */
++++++++pki.verifyCertificateChain = function(caStore, chain, options) {
++++++++ /* From: RFC3280 - Internet X.509 Public Key Infrastructure Certificate
++++++++ Section 6: Certification Path Validation
++++++++ See inline parentheticals related to this particular implementation.
++++++++
++++++++ The primary goal of path validation is to verify the binding between
++++++++ a subject distinguished name or a subject alternative name and subject
++++++++ public key, as represented in the end entity certificate, based on the
++++++++ public key of the trust anchor. This requires obtaining a sequence of
++++++++ certificates that support that binding. That sequence should be provided
++++++++ in the passed 'chain'. The trust anchor should be in the given CA
++++++++ store. The 'end entity' certificate is the certificate provided by the
++++++++ end point (typically a server) and is the first in the chain.
++++++++
++++++++ To meet this goal, the path validation process verifies, among other
++++++++ things, that a prospective certification path (a sequence of n
++++++++ certificates or a 'chain') satisfies the following conditions:
++++++++
++++++++ (a) for all x in {1, ..., n-1}, the subject of certificate x is
++++++++ the issuer of certificate x+1;
++++++++
++++++++ (b) certificate 1 is issued by the trust anchor;
++++++++
++++++++ (c) certificate n is the certificate to be validated; and
++++++++
++++++++ (d) for all x in {1, ..., n}, the certificate was valid at the
++++++++ time in question.
++++++++
++++++++ Note that here 'n' is index 0 in the chain and 1 is the last certificate
++++++++ in the chain and it must be signed by a certificate in the connection's
++++++++ CA store.
++++++++
++++++++ The path validation process also determines the set of certificate
++++++++ policies that are valid for this path, based on the certificate policies
++++++++ extension, policy mapping extension, policy constraints extension, and
++++++++ inhibit any-policy extension.
++++++++
++++++++ Note: Policy mapping extension not supported (Not Required).
++++++++
++++++++ Note: If the certificate has an unsupported critical extension, then it
++++++++ must be rejected.
++++++++
++++++++ Note: A certificate is self-issued if the DNs that appear in the subject
++++++++ and issuer fields are identical and are not empty.
++++++++
++++++++ The path validation algorithm assumes the following seven inputs are
++++++++ provided to the path processing logic. What this specific implementation
++++++++ will use is provided parenthetically:
++++++++
++++++++ (a) a prospective certification path of length n (the 'chain')
++++++++ (b) the current date/time: ('now').
++++++++ (c) user-initial-policy-set: A set of certificate policy identifiers
++++++++ naming the policies that are acceptable to the certificate user.
++++++++ The user-initial-policy-set contains the special value any-policy
++++++++ if the user is not concerned about certificate policy
++++++++ (Not implemented. Any policy is accepted).
++++++++ (d) trust anchor information, describing a CA that serves as a trust
++++++++ anchor for the certification path. The trust anchor information
++++++++ includes:
++++++++
++++++++ (1) the trusted issuer name,
++++++++ (2) the trusted public key algorithm,
++++++++ (3) the trusted public key, and
++++++++ (4) optionally, the trusted public key parameters associated
++++++++ with the public key.
++++++++
++++++++ (Trust anchors are provided via certificates in the CA store).
++++++++
++++++++ The trust anchor information may be provided to the path processing
++++++++ procedure in the form of a self-signed certificate. The trusted anchor
++++++++ information is trusted because it was delivered to the path processing
++++++++ procedure by some trustworthy out-of-band procedure. If the trusted
++++++++ public key algorithm requires parameters, then the parameters are
++++++++ provided along with the trusted public key (No parameters used in this
++++++++ implementation).
++++++++
++++++++ (e) initial-policy-mapping-inhibit, which indicates if policy mapping is
++++++++ allowed in the certification path.
++++++++ (Not implemented, no policy checking)
++++++++
++++++++ (f) initial-explicit-policy, which indicates if the path must be valid
++++++++ for at least one of the certificate policies in the user-initial-
++++++++ policy-set.
++++++++ (Not implemented, no policy checking)
++++++++
++++++++ (g) initial-any-policy-inhibit, which indicates whether the
++++++++ anyPolicy OID should be processed if it is included in a
++++++++ certificate.
++++++++ (Not implemented, so any policy is valid provided that it is
++++++++ not marked as critical) */
++++++++
++++++++ /* Basic Path Processing:
++++++++
++++++++ For each certificate in the 'chain', the following is checked:
++++++++
++++++++ 1. The certificate validity period includes the current time.
++++++++ 2. The certificate was signed by its parent (where the parent is either
++++++++ the next in the chain or from the CA store). Allow processing to
++++++++ continue to the next step if no parent is found but the certificate is
++++++++ in the CA store.
++++++++ 3. TODO: The certificate has not been revoked.
++++++++ 4. The certificate issuer name matches the parent's subject name.
++++++++ 5. TODO: If the certificate is self-issued and not the final certificate
++++++++ in the chain, skip this step, otherwise verify that the subject name
++++++++ is within one of the permitted subtrees of X.500 distinguished names
++++++++ and that each of the alternative names in the subjectAltName extension
++++++++ (critical or non-critical) is within one of the permitted subtrees for
++++++++ that name type.
++++++++ 6. TODO: If the certificate is self-issued and not the final certificate
++++++++ in the chain, skip this step, otherwise verify that the subject name
++++++++ is not within one of the excluded subtrees for X.500 distinguished
++++++++ names and none of the subjectAltName extension names are excluded for
++++++++ that name type.
++++++++ 7. The other steps in the algorithm for basic path processing involve
++++++++ handling the policy extension which is not presently supported in this
++++++++ implementation. Instead, if a critical policy extension is found, the
++++++++ certificate is rejected as not supported.
++++++++ 8. If the certificate is not the first or if its the only certificate in
++++++++ the chain (having no parent from the CA store or is self-signed) and it
++++++++ has a critical key usage extension, verify that the keyCertSign bit is
++++++++ set. If the key usage extension exists, verify that the basic
++++++++ constraints extension exists. If the basic constraints extension exists,
++++++++ verify that the cA flag is set. If pathLenConstraint is set, ensure that
++++++++ the number of certificates that precede in the chain (come earlier
++++++++ in the chain as implemented below), excluding the very first in the
++++++++ chain (typically the end-entity one), isn't greater than the
++++++++ pathLenConstraint. This constraint limits the number of intermediate
++++++++ CAs that may appear below a CA before only end-entity certificates
++++++++ may be issued. */
++++++++
++++++++ // if a verify callback is passed as the third parameter, package it within
++++++++ // the options object. This is to support a legacy function signature that
++++++++ // expected the verify callback as the third parameter.
++++++++ if(typeof options === 'function') {
++++++++ options = {verify: options};
++++++++ }
++++++++ options = options || {};
++++++++
++++++++ // copy cert chain references to another array to protect against changes
++++++++ // in verify callback
++++++++ chain = chain.slice(0);
++++++++ var certs = chain.slice(0);
++++++++
++++++++ var validityCheckDate = options.validityCheckDate;
++++++++ // if no validityCheckDate is specified, default to the current date. Make
++++++++ // sure to maintain the value null because it indicates that the validity
++++++++ // period should not be checked.
++++++++ if(typeof validityCheckDate === 'undefined') {
++++++++ validityCheckDate = new Date();
++++++++ }
++++++++
++++++++ // verify each cert in the chain using its parent, where the parent
++++++++ // is either the next in the chain or from the CA store
++++++++ var first = true;
++++++++ var error = null;
++++++++ var depth = 0;
++++++++ do {
++++++++ var cert = chain.shift();
++++++++ var parent = null;
++++++++ var selfSigned = false;
++++++++
++++++++ if(validityCheckDate) {
++++++++ // 1. check valid time
++++++++ if(validityCheckDate < cert.validity.notBefore ||
++++++++ validityCheckDate > cert.validity.notAfter) {
++++++++ error = {
++++++++ message: 'Certificate is not valid yet or has expired.',
++++++++ error: pki.certificateError.certificate_expired,
++++++++ notBefore: cert.validity.notBefore,
++++++++ notAfter: cert.validity.notAfter,
++++++++ // TODO: we might want to reconsider renaming 'now' to
++++++++ // 'validityCheckDate' should this API be changed in the future.
++++++++ now: validityCheckDate
++++++++ };
++++++++ }
++++++++ }
++++++++
++++++++ // 2. verify with parent from chain or CA store
++++++++ if(error === null) {
++++++++ parent = chain[0] || caStore.getIssuer(cert);
++++++++ if(parent === null) {
++++++++ // check for self-signed cert
++++++++ if(cert.isIssuer(cert)) {
++++++++ selfSigned = true;
++++++++ parent = cert;
++++++++ }
++++++++ }
++++++++
++++++++ if(parent) {
++++++++ // FIXME: current CA store implementation might have multiple
++++++++ // certificates where the issuer can't be determined from the
++++++++ // certificate (happens rarely with, eg: old certificates) so normalize
++++++++ // by always putting parents into an array
++++++++ // TODO: there's may be an extreme degenerate case currently uncovered
++++++++ // where an old intermediate certificate seems to have a matching parent
++++++++ // but none of the parents actually verify ... but the intermediate
++++++++ // is in the CA and it should pass this check; needs investigation
++++++++ var parents = parent;
++++++++ if(!forge.util.isArray(parents)) {
++++++++ parents = [parents];
++++++++ }
++++++++
++++++++ // try to verify with each possible parent (typically only one)
++++++++ var verified = false;
++++++++ while(!verified && parents.length > 0) {
++++++++ parent = parents.shift();
++++++++ try {
++++++++ verified = parent.verify(cert);
++++++++ } catch(ex) {
++++++++ // failure to verify, don't care why, try next one
++++++++ }
++++++++ }
++++++++
++++++++ if(!verified) {
++++++++ error = {
++++++++ message: 'Certificate signature is invalid.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++ }
++++++++
++++++++ if(error === null && (!parent || selfSigned) &&
++++++++ !caStore.hasCertificate(cert)) {
++++++++ // no parent issuer and certificate itself is not trusted
++++++++ error = {
++++++++ message: 'Certificate is not trusted.',
++++++++ error: pki.certificateError.unknown_ca
++++++++ };
++++++++ }
++++++++ }
++++++++
++++++++ // TODO: 3. check revoked
++++++++
++++++++ // 4. check for matching issuer/subject
++++++++ if(error === null && parent && !cert.isIssuer(parent)) {
++++++++ // parent is not issuer
++++++++ error = {
++++++++ message: 'Certificate issuer is invalid.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++
++++++++ // 5. TODO: check names with permitted names tree
++++++++
++++++++ // 6. TODO: check names against excluded names tree
++++++++
++++++++ // 7. check for unsupported critical extensions
++++++++ if(error === null) {
++++++++ // supported extensions
++++++++ var se = {
++++++++ keyUsage: true,
++++++++ basicConstraints: true
++++++++ };
++++++++ for(var i = 0; error === null && i < cert.extensions.length; ++i) {
++++++++ var ext = cert.extensions[i];
++++++++ if(ext.critical && !(ext.name in se)) {
++++++++ error = {
++++++++ message:
++++++++ 'Certificate has an unsupported critical extension.',
++++++++ error: pki.certificateError.unsupported_certificate
++++++++ };
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // 8. check for CA if cert is not first or is the only certificate
++++++++ // remaining in chain with no parent or is self-signed
++++++++ if(error === null &&
++++++++ (!first || (chain.length === 0 && (!parent || selfSigned)))) {
++++++++ // first check keyUsage extension and then basic constraints
++++++++ var bcExt = cert.getExtension('basicConstraints');
++++++++ var keyUsageExt = cert.getExtension('keyUsage');
++++++++ if(keyUsageExt !== null) {
++++++++ // keyCertSign must be true and there must be a basic
++++++++ // constraints extension
++++++++ if(!keyUsageExt.keyCertSign || bcExt === null) {
++++++++ // bad certificate
++++++++ error = {
++++++++ message:
++++++++ 'Certificate keyUsage or basicConstraints conflict ' +
++++++++ 'or indicate that the certificate is not a CA. ' +
++++++++ 'If the certificate is the only one in the chain or ' +
++++++++ 'isn\'t the first then the certificate must be a ' +
++++++++ 'valid CA.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++ }
++++++++ // basic constraints cA flag must be set
++++++++ if(error === null && bcExt !== null && !bcExt.cA) {
++++++++ // bad certificate
++++++++ error = {
++++++++ message:
++++++++ 'Certificate basicConstraints indicates the certificate ' +
++++++++ 'is not a CA.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++ // if error is not null and keyUsage is available, then we know it
++++++++ // has keyCertSign and there is a basic constraints extension too,
++++++++ // which means we can check pathLenConstraint (if it exists)
++++++++ if(error === null && keyUsageExt !== null &&
++++++++ 'pathLenConstraint' in bcExt) {
++++++++ // pathLen is the maximum # of intermediate CA certs that can be
++++++++ // found between the current certificate and the end-entity (depth 0)
++++++++ // certificate; this number does not include the end-entity (depth 0,
++++++++ // last in the chain) even if it happens to be a CA certificate itself
++++++++ var pathLen = depth - 1;
++++++++ if(pathLen > bcExt.pathLenConstraint) {
++++++++ // pathLenConstraint violated, bad certificate
++++++++ error = {
++++++++ message:
++++++++ 'Certificate basicConstraints pathLenConstraint violated.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // call application callback
++++++++ var vfd = (error === null) ? true : error.error;
++++++++ var ret = options.verify ? options.verify(vfd, depth, certs) : vfd;
++++++++ if(ret === true) {
++++++++ // clear any set error
++++++++ error = null;
++++++++ } else {
++++++++ // if passed basic tests, set default message and alert
++++++++ if(vfd === true) {
++++++++ error = {
++++++++ message: 'The application rejected the certificate.',
++++++++ error: pki.certificateError.bad_certificate
++++++++ };
++++++++ }
++++++++
++++++++ // check for custom error info
++++++++ if(ret || ret === 0) {
++++++++ // set custom message and error
++++++++ if(typeof ret === 'object' && !forge.util.isArray(ret)) {
++++++++ if(ret.message) {
++++++++ error.message = ret.message;
++++++++ }
++++++++ if(ret.error) {
++++++++ error.error = ret.error;
++++++++ }
++++++++ } else if(typeof ret === 'string') {
++++++++ // set custom error
++++++++ error.error = ret;
++++++++ }
++++++++ }
++++++++
++++++++ // throw error
++++++++ throw error;
++++++++ }
++++++++
++++++++ // no longer first cert in chain
++++++++ first = false;
++++++++ ++depth;
++++++++ } while(chain.length > 0);
++++++++
++++++++ return true;
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * XmlHttpRequest implementation that uses TLS and flash SocketPool.
++++++++ *
++++++++ * @author Dave Longley
++++++++ *
++++++++ * Copyright (c) 2010-2013 Digital Bazaar, Inc.
++++++++ */
++++++++var forge = require('./forge');
++++++++require('./socket');
++++++++require('./http');
++++++++
++++++++/* XHR API */
++++++++var xhrApi = module.exports = forge.xhr = forge.xhr || {};
++++++++
++++++++(function($) {
++++++++
++++++++// logging category
++++++++var cat = 'forge.xhr';
++++++++
++++++++/*
++++++++XMLHttpRequest interface definition from:
++++++++http://www.w3.org/TR/XMLHttpRequest
++++++++
++++++++interface XMLHttpRequest {
++++++++ // event handler
++++++++ attribute EventListener onreadystatechange;
++++++++
++++++++ // state
++++++++ const unsigned short UNSENT = 0;
++++++++ const unsigned short OPENED = 1;
++++++++ const unsigned short HEADERS_RECEIVED = 2;
++++++++ const unsigned short LOADING = 3;
++++++++ const unsigned short DONE = 4;
++++++++ readonly attribute unsigned short readyState;
++++++++
++++++++ // request
++++++++ void open(in DOMString method, in DOMString url);
++++++++ void open(in DOMString method, in DOMString url, in boolean async);
++++++++ void open(in DOMString method, in DOMString url,
++++++++ in boolean async, in DOMString user);
++++++++ void open(in DOMString method, in DOMString url,
++++++++ in boolean async, in DOMString user, in DOMString password);
++++++++ void setRequestHeader(in DOMString header, in DOMString value);
++++++++ void send();
++++++++ void send(in DOMString data);
++++++++ void send(in Document data);
++++++++ void abort();
++++++++
++++++++ // response
++++++++ DOMString getAllResponseHeaders();
++++++++ DOMString getResponseHeader(in DOMString header);
++++++++ readonly attribute DOMString responseText;
++++++++ readonly attribute Document responseXML;
++++++++ readonly attribute unsigned short status;
++++++++ readonly attribute DOMString statusText;
++++++++};
++++++++*/
++++++++
++++++++// readyStates
++++++++var UNSENT = 0;
++++++++var OPENED = 1;
++++++++var HEADERS_RECEIVED = 2;
++++++++var LOADING = 3;
++++++++var DONE = 4;
++++++++
++++++++// exceptions
++++++++var INVALID_STATE_ERR = 11;
++++++++var SYNTAX_ERR = 12;
++++++++var SECURITY_ERR = 18;
++++++++var NETWORK_ERR = 19;
++++++++var ABORT_ERR = 20;
++++++++
++++++++// private flash socket pool vars
++++++++var _sp = null;
++++++++var _policyPort = 0;
++++++++var _policyUrl = null;
++++++++
++++++++// default client (used if no special URL provided when creating an XHR)
++++++++var _client = null;
++++++++
++++++++// all clients including the default, key'd by full base url
++++++++// (multiple cross-domain http clients are permitted so there may be more
++++++++// than one client in this map)
++++++++// TODO: provide optional clean up API for non-default clients
++++++++var _clients = {};
++++++++
++++++++// the default maximum number of concurrents connections per client
++++++++var _maxConnections = 10;
++++++++
++++++++var net = forge.net;
++++++++var http = forge.http;
++++++++
++++++++/**
++++++++ * Initializes flash XHR support.
++++++++ *
++++++++ * @param options:
++++++++ * url: the default base URL to connect to if xhr URLs are relative,
++++++++ * ie: https://myserver.com.
++++++++ * flashId: the dom ID of the flash SocketPool.
++++++++ * policyPort: the port that provides the server's flash policy, 0 to use
++++++++ * the flash default.
++++++++ * policyUrl: the policy file URL to use instead of a policy port.
++++++++ * msie: true if browser is internet explorer, false if not.
++++++++ * connections: the maximum number of concurrent connections.
++++++++ * caCerts: a list of PEM-formatted certificates to trust.
++++++++ * cipherSuites: an optional array of cipher suites to use,
++++++++ * see forge.tls.CipherSuites.
++++++++ * verify: optional TLS certificate verify callback to use (see forge.tls
++++++++ * for details).
++++++++ * getCertificate: an optional callback used to get a client-side
++++++++ * certificate (see forge.tls for details).
++++++++ * getPrivateKey: an optional callback used to get a client-side private
++++++++ * key (see forge.tls for details).
++++++++ * getSignature: an optional callback used to get a client-side signature
++++++++ * (see forge.tls for details).
++++++++ * persistCookies: true to use persistent cookies via flash local storage,
++++++++ * false to only keep cookies in javascript.
++++++++ * primeTlsSockets: true to immediately connect TLS sockets on their
++++++++ * creation so that they will cache TLS sessions for reuse.
++++++++ */
++++++++xhrApi.init = function(options) {
++++++++ forge.log.debug(cat, 'initializing', options);
++++++++
++++++++ // update default policy port and max connections
++++++++ _policyPort = options.policyPort || _policyPort;
++++++++ _policyUrl = options.policyUrl || _policyUrl;
++++++++ _maxConnections = options.connections || _maxConnections;
++++++++
++++++++ // create the flash socket pool
++++++++ _sp = net.createSocketPool({
++++++++ flashId: options.flashId,
++++++++ policyPort: _policyPort,
++++++++ policyUrl: _policyUrl,
++++++++ msie: options.msie || false
++++++++ });
++++++++
++++++++ // create default http client
++++++++ _client = http.createClient({
++++++++ url: options.url || (
++++++++ window.location.protocol + '//' + window.location.host),
++++++++ socketPool: _sp,
++++++++ policyPort: _policyPort,
++++++++ policyUrl: _policyUrl,
++++++++ connections: options.connections || _maxConnections,
++++++++ caCerts: options.caCerts,
++++++++ cipherSuites: options.cipherSuites,
++++++++ persistCookies: options.persistCookies || true,
++++++++ primeTlsSockets: options.primeTlsSockets || false,
++++++++ verify: options.verify,
++++++++ getCertificate: options.getCertificate,
++++++++ getPrivateKey: options.getPrivateKey,
++++++++ getSignature: options.getSignature
++++++++ });
++++++++ _clients[_client.url.origin] = _client;
++++++++
++++++++ forge.log.debug(cat, 'ready');
++++++++};
++++++++
++++++++/**
++++++++ * Called to clean up the clients and socket pool.
++++++++ */
++++++++xhrApi.cleanup = function() {
++++++++ // destroy all clients
++++++++ for(var key in _clients) {
++++++++ _clients[key].destroy();
++++++++ }
++++++++ _clients = {};
++++++++ _client = null;
++++++++
++++++++ // destroy socket pool
++++++++ _sp.destroy();
++++++++ _sp = null;
++++++++};
++++++++
++++++++/**
++++++++ * Sets a cookie.
++++++++ *
++++++++ * @param cookie the cookie with parameters:
++++++++ * name: the name of the cookie.
++++++++ * value: the value of the cookie.
++++++++ * comment: an optional comment string.
++++++++ * maxAge: the age of the cookie in seconds relative to created time.
++++++++ * secure: true if the cookie must be sent over a secure protocol.
++++++++ * httpOnly: true to restrict access to the cookie from javascript
++++++++ * (inaffective since the cookies are stored in javascript).
++++++++ * path: the path for the cookie.
++++++++ * domain: optional domain the cookie belongs to (must start with dot).
++++++++ * version: optional version of the cookie.
++++++++ * created: creation time, in UTC seconds, of the cookie.
++++++++ */
++++++++xhrApi.setCookie = function(cookie) {
++++++++ // default cookie expiration to never
++++++++ cookie.maxAge = cookie.maxAge || -1;
++++++++
++++++++ // if the cookie's domain is set, use the appropriate client
++++++++ if(cookie.domain) {
++++++++ // add the cookies to the applicable domains
++++++++ for(var key in _clients) {
++++++++ var client = _clients[key];
++++++++ if(http.withinCookieDomain(client.url, cookie) &&
++++++++ client.secure === cookie.secure) {
++++++++ client.setCookie(cookie);
++++++++ }
++++++++ }
++++++++ } else {
++++++++ // use the default domain
++++++++ // FIXME: should a null domain cookie be added to all clients? should
++++++++ // this be an option?
++++++++ _client.setCookie(cookie);
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Gets a cookie.
++++++++ *
++++++++ * @param name the name of the cookie.
++++++++ * @param path an optional path for the cookie (if there are multiple cookies
++++++++ * with the same name but different paths).
++++++++ * @param domain an optional domain for the cookie (if not using the default
++++++++ * domain).
++++++++ *
++++++++ * @return the cookie, cookies (if multiple matches), or null if not found.
++++++++ */
++++++++xhrApi.getCookie = function(name, path, domain) {
++++++++ var rval = null;
++++++++
++++++++ if(domain) {
++++++++ // get the cookies from the applicable domains
++++++++ for(var key in _clients) {
++++++++ var client = _clients[key];
++++++++ if(http.withinCookieDomain(client.url, domain)) {
++++++++ var cookie = client.getCookie(name, path);
++++++++ if(cookie !== null) {
++++++++ if(rval === null) {
++++++++ rval = cookie;
++++++++ } else if(!forge.util.isArray(rval)) {
++++++++ rval = [rval, cookie];
++++++++ } else {
++++++++ rval.push(cookie);
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ } else {
++++++++ // get cookie from default domain
++++++++ rval = _client.getCookie(name, path);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Removes a cookie.
++++++++ *
++++++++ * @param name the name of the cookie.
++++++++ * @param path an optional path for the cookie (if there are multiple cookies
++++++++ * with the same name but different paths).
++++++++ * @param domain an optional domain for the cookie (if not using the default
++++++++ * domain).
++++++++ *
++++++++ * @return true if a cookie was removed, false if not.
++++++++ */
++++++++xhrApi.removeCookie = function(name, path, domain) {
++++++++ var rval = false;
++++++++
++++++++ if(domain) {
++++++++ // remove the cookies from the applicable domains
++++++++ for(var key in _clients) {
++++++++ var client = _clients[key];
++++++++ if(http.withinCookieDomain(client.url, domain)) {
++++++++ if(client.removeCookie(name, path)) {
++++++++ rval = true;
++++++++ }
++++++++ }
++++++++ }
++++++++ } else {
++++++++ // remove cookie from default domain
++++++++ rval = _client.removeCookie(name, path);
++++++++ }
++++++++
++++++++ return rval;
++++++++};
++++++++
++++++++/**
++++++++ * Creates a new XmlHttpRequest. By default the base URL, flash policy port,
++++++++ * etc, will be used. However, an XHR can be created to point at another
++++++++ * cross-domain URL.
++++++++ *
++++++++ * @param options:
++++++++ * logWarningOnError: If true and an HTTP error status code is received then
++++++++ * log a warning, otherwise log a verbose message.
++++++++ * verbose: If true be very verbose in the output including the response
++++++++ * event and response body, otherwise only include status, timing, and
++++++++ * data size.
++++++++ * logError: a multi-var log function for warnings that takes the log
++++++++ * category as the first var.
++++++++ * logWarning: a multi-var log function for warnings that takes the log
++++++++ * category as the first var.
++++++++ * logDebug: a multi-var log function for warnings that takes the log
++++++++ * category as the first var.
++++++++ * logVerbose: a multi-var log function for warnings that takes the log
++++++++ * category as the first var.
++++++++ * url: the default base URL to connect to if xhr URLs are relative,
++++++++ * eg: https://myserver.com, and note that the following options will be
++++++++ * ignored if the URL is absent or the same as the default base URL.
++++++++ * policyPort: the port that provides the server's flash policy, 0 to use
++++++++ * the flash default.
++++++++ * policyUrl: the policy file URL to use instead of a policy port.
++++++++ * connections: the maximum number of concurrent connections.
++++++++ * caCerts: a list of PEM-formatted certificates to trust.
++++++++ * cipherSuites: an optional array of cipher suites to use, see
++++++++ * forge.tls.CipherSuites.
++++++++ * verify: optional TLS certificate verify callback to use (see forge.tls
++++++++ * for details).
++++++++ * getCertificate: an optional callback used to get a client-side
++++++++ * certificate.
++++++++ * getPrivateKey: an optional callback used to get a client-side private key.
++++++++ * getSignature: an optional callback used to get a client-side signature.
++++++++ * persistCookies: true to use persistent cookies via flash local storage,
++++++++ * false to only keep cookies in javascript.
++++++++ * primeTlsSockets: true to immediately connect TLS sockets on their
++++++++ * creation so that they will cache TLS sessions for reuse.
++++++++ *
++++++++ * @return the XmlHttpRequest.
++++++++ */
++++++++xhrApi.create = function(options) {
++++++++ // set option defaults
++++++++ options = $.extend({
++++++++ logWarningOnError: true,
++++++++ verbose: false,
++++++++ logError: function() {},
++++++++ logWarning: function() {},
++++++++ logDebug: function() {},
++++++++ logVerbose: function() {},
++++++++ url: null
++++++++ }, options || {});
++++++++
++++++++ // private xhr state
++++++++ var _state = {
++++++++ // the http client to use
++++++++ client: null,
++++++++ // request storage
++++++++ request: null,
++++++++ // response storage
++++++++ response: null,
++++++++ // asynchronous, true if doing asynchronous communication
++++++++ asynchronous: true,
++++++++ // sendFlag, true if send has been called
++++++++ sendFlag: false,
++++++++ // errorFlag, true if a network error occurred
++++++++ errorFlag: false
++++++++ };
++++++++
++++++++ // private log functions
++++++++ var _log = {
++++++++ error: options.logError || forge.log.error,
++++++++ warning: options.logWarning || forge.log.warning,
++++++++ debug: options.logDebug || forge.log.debug,
++++++++ verbose: options.logVerbose || forge.log.verbose
++++++++ };
++++++++
++++++++ // create public xhr interface
++++++++ var xhr = {
++++++++ // an EventListener
++++++++ onreadystatechange: null,
++++++++ // readonly, the current readyState
++++++++ readyState: UNSENT,
++++++++ // a string with the response entity-body
++++++++ responseText: '',
++++++++ // a Document for response entity-bodies that are XML
++++++++ responseXML: null,
++++++++ // readonly, returns the HTTP status code (i.e. 404)
++++++++ status: 0,
++++++++ // readonly, returns the HTTP status message (i.e. 'Not Found')
++++++++ statusText: ''
++++++++ };
++++++++
++++++++ // determine which http client to use
++++++++ if(options.url === null) {
++++++++ // use default
++++++++ _state.client = _client;
++++++++ } else {
++++++++ var url;
++++++++ try {
++++++++ url = new URL(options.url);
++++++++ } catch(e) {
++++++++ var error = new Error('Invalid url.');
++++++++ error.details = {
++++++++ url: options.url
++++++++ };
++++++++ }
++++++++
++++++++ // find client
++++++++ if(url.origin in _clients) {
++++++++ // client found
++++++++ _state.client = _clients[url.origin];
++++++++ } else {
++++++++ // create client
++++++++ _state.client = http.createClient({
++++++++ url: options.url,
++++++++ socketPool: _sp,
++++++++ policyPort: options.policyPort || _policyPort,
++++++++ policyUrl: options.policyUrl || _policyUrl,
++++++++ connections: options.connections || _maxConnections,
++++++++ caCerts: options.caCerts,
++++++++ cipherSuites: options.cipherSuites,
++++++++ persistCookies: options.persistCookies || true,
++++++++ primeTlsSockets: options.primeTlsSockets || false,
++++++++ verify: options.verify,
++++++++ getCertificate: options.getCertificate,
++++++++ getPrivateKey: options.getPrivateKey,
++++++++ getSignature: options.getSignature
++++++++ });
++++++++ _clients[url.origin] = _state.client;
++++++++ }
++++++++ }
++++++++
++++++++ /**
++++++++ * Opens the request. This method will create the HTTP request to send.
++++++++ *
++++++++ * @param method the HTTP method (i.e. 'GET').
++++++++ * @param url the relative url (the HTTP request path).
++++++++ * @param async always true, ignored.
++++++++ * @param user always null, ignored.
++++++++ * @param password always null, ignored.
++++++++ */
++++++++ xhr.open = function(method, url, async, user, password) {
++++++++ // 1. validate Document if one is associated
++++++++ // TODO: not implemented (not used yet)
++++++++
++++++++ // 2. validate method token
++++++++ // 3. change method to uppercase if it matches a known
++++++++ // method (here we just require it to be uppercase, and
++++++++ // we do not allow the standard methods)
++++++++ // 4. disallow CONNECT, TRACE, or TRACK with a security error
++++++++ switch(method) {
++++++++ case 'DELETE':
++++++++ case 'GET':
++++++++ case 'HEAD':
++++++++ case 'OPTIONS':
++++++++ case 'PATCH':
++++++++ case 'POST':
++++++++ case 'PUT':
++++++++ // valid method
++++++++ break;
++++++++ case 'CONNECT':
++++++++ case 'TRACE':
++++++++ case 'TRACK':
++++++++ throw new Error('CONNECT, TRACE and TRACK methods are disallowed');
++++++++ default:
++++++++ throw new Error('Invalid method: ' + method);
++++++++ }
++++++++
++++++++ // TODO: other validation steps in algorithm are not implemented
++++++++
++++++++ // 19. set send flag to false
++++++++ // set response body to null
++++++++ // empty list of request headers
++++++++ // set request method to given method
++++++++ // set request URL
++++++++ // set username, password
++++++++ // set asychronous flag
++++++++ _state.sendFlag = false;
++++++++ xhr.responseText = '';
++++++++ xhr.responseXML = null;
++++++++
++++++++ // custom: reset status and statusText
++++++++ xhr.status = 0;
++++++++ xhr.statusText = '';
++++++++
++++++++ // create the HTTP request
++++++++ _state.request = http.createRequest({
++++++++ method: method,
++++++++ path: url
++++++++ });
++++++++
++++++++ // 20. set state to OPENED
++++++++ xhr.readyState = OPENED;
++++++++
++++++++ // 21. dispatch onreadystatechange
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Adds an HTTP header field to the request.
++++++++ *
++++++++ * @param header the name of the header field.
++++++++ * @param value the value of the header field.
++++++++ */
++++++++ xhr.setRequestHeader = function(header, value) {
++++++++ // 1. if state is not OPENED or send flag is true, raise exception
++++++++ if(xhr.readyState != OPENED || _state.sendFlag) {
++++++++ throw new Error('XHR not open or sending');
++++++++ }
++++++++
++++++++ // TODO: other validation steps in spec aren't implemented
++++++++
++++++++ // set header
++++++++ _state.request.setField(header, value);
++++++++ };
++++++++
++++++++ /**
++++++++ * Sends the request and any associated data.
++++++++ *
++++++++ * @param data a string or Document object to send, null to send no data.
++++++++ */
++++++++ xhr.send = function(data) {
++++++++ // 1. if state is not OPENED or 2. send flag is true, raise
++++++++ // an invalid state exception
++++++++ if(xhr.readyState != OPENED || _state.sendFlag) {
++++++++ throw new Error('XHR not open or sending');
++++++++ }
++++++++
++++++++ // 3. ignore data if method is GET or HEAD
++++++++ if(data &&
++++++++ _state.request.method !== 'GET' &&
++++++++ _state.request.method !== 'HEAD') {
++++++++ // handle non-IE case
++++++++ if(typeof(XMLSerializer) !== 'undefined') {
++++++++ if(data instanceof Document) {
++++++++ var xs = new XMLSerializer();
++++++++ _state.request.body = xs.serializeToString(data);
++++++++ } else {
++++++++ _state.request.body = data;
++++++++ }
++++++++ } else {
++++++++ // poorly implemented IE case
++++++++ if(typeof(data.xml) !== 'undefined') {
++++++++ _state.request.body = data.xml;
++++++++ } else {
++++++++ _state.request.body = data;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // 4. release storage mutex (not used)
++++++++
++++++++ // 5. set error flag to false
++++++++ _state.errorFlag = false;
++++++++
++++++++ // 6. if asynchronous is true (must be in this implementation)
++++++++
++++++++ // 6.1 set send flag to true
++++++++ _state.sendFlag = true;
++++++++
++++++++ // 6.2 dispatch onreadystatechange
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++
++++++++ // create send options
++++++++ var options = {};
++++++++ options.request = _state.request;
++++++++ options.headerReady = function(e) {
++++++++ // make cookies available for ease of use/iteration
++++++++ xhr.cookies = _state.client.cookies;
++++++++
++++++++ // TODO: update document.cookie with any cookies where the
++++++++ // script's domain matches
++++++++
++++++++ // headers received
++++++++ xhr.readyState = HEADERS_RECEIVED;
++++++++ xhr.status = e.response.code;
++++++++ xhr.statusText = e.response.message;
++++++++ _state.response = e.response;
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++ if(!_state.response.aborted) {
++++++++ // now loading body
++++++++ xhr.readyState = LOADING;
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++ }
++++++++ };
++++++++ options.bodyReady = function(e) {
++++++++ xhr.readyState = DONE;
++++++++ var ct = e.response.getField('Content-Type');
++++++++ // Note: this null/undefined check is done outside because IE
++++++++ // dies otherwise on a "'null' is null" error
++++++++ if(ct) {
++++++++ if(ct.indexOf('text/xml') === 0 ||
++++++++ ct.indexOf('application/xml') === 0 ||
++++++++ ct.indexOf('+xml') !== -1) {
++++++++ try {
++++++++ var doc = new ActiveXObject('MicrosoftXMLDOM');
++++++++ doc.async = false;
++++++++ doc.loadXML(e.response.body);
++++++++ xhr.responseXML = doc;
++++++++ } catch(ex) {
++++++++ var parser = new DOMParser();
++++++++ xhr.responseXML = parser.parseFromString(ex.body, 'text/xml');
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ var length = 0;
++++++++ if(e.response.body !== null) {
++++++++ xhr.responseText = e.response.body;
++++++++ length = e.response.body.length;
++++++++ }
++++++++ // build logging output
++++++++ var req = _state.request;
++++++++ var output =
++++++++ req.method + ' ' + req.path + ' ' +
++++++++ xhr.status + ' ' + xhr.statusText + ' ' +
++++++++ length + 'B ' +
++++++++ (e.request.connectTime + e.request.time + e.response.time) +
++++++++ 'ms';
++++++++ var lFunc;
++++++++ if(options.verbose) {
++++++++ lFunc = (xhr.status >= 400 && options.logWarningOnError) ?
++++++++ _log.warning : _log.verbose;
++++++++ lFunc(cat, output,
++++++++ e, e.response.body ? '\n' + e.response.body : '\nNo content');
++++++++ } else {
++++++++ lFunc = (xhr.status >= 400 && options.logWarningOnError) ?
++++++++ _log.warning : _log.debug;
++++++++ lFunc(cat, output);
++++++++ }
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++ };
++++++++ options.error = function(e) {
++++++++ var req = _state.request;
++++++++ _log.error(cat, req.method + ' ' + req.path, e);
++++++++
++++++++ // 1. set response body to null
++++++++ xhr.responseText = '';
++++++++ xhr.responseXML = null;
++++++++
++++++++ // 2. set error flag to true (and reset status)
++++++++ _state.errorFlag = true;
++++++++ xhr.status = 0;
++++++++ xhr.statusText = '';
++++++++
++++++++ // 3. set state to done
++++++++ xhr.readyState = DONE;
++++++++
++++++++ // 4. asyc flag is always true, so dispatch onreadystatechange
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++ };
++++++++
++++++++ // 7. send request
++++++++ _state.client.send(options);
++++++++ };
++++++++
++++++++ /**
++++++++ * Aborts the request.
++++++++ */
++++++++ xhr.abort = function() {
++++++++ // 1. abort send
++++++++ // 2. stop network activity
++++++++ _state.request.abort();
++++++++
++++++++ // 3. set response to null
++++++++ xhr.responseText = '';
++++++++ xhr.responseXML = null;
++++++++
++++++++ // 4. set error flag to true (and reset status)
++++++++ _state.errorFlag = true;
++++++++ xhr.status = 0;
++++++++ xhr.statusText = '';
++++++++
++++++++ // 5. clear user headers
++++++++ _state.request = null;
++++++++ _state.response = null;
++++++++
++++++++ // 6. if state is DONE or UNSENT, or if OPENED and send flag is false
++++++++ if(xhr.readyState === DONE || xhr.readyState === UNSENT ||
++++++++ (xhr.readyState === OPENED && !_state.sendFlag)) {
++++++++ // 7. set ready state to unsent
++++++++ xhr.readyState = UNSENT;
++++++++ } else {
++++++++ // 6.1 set state to DONE
++++++++ xhr.readyState = DONE;
++++++++
++++++++ // 6.2 set send flag to false
++++++++ _state.sendFlag = false;
++++++++
++++++++ // 6.3 dispatch onreadystatechange
++++++++ if(xhr.onreadystatechange) {
++++++++ xhr.onreadystatechange();
++++++++ }
++++++++
++++++++ // 7. set state to UNSENT
++++++++ xhr.readyState = UNSENT;
++++++++ }
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets all response headers as a string.
++++++++ *
++++++++ * @return the HTTP-encoded response header fields.
++++++++ */
++++++++ xhr.getAllResponseHeaders = function() {
++++++++ var rval = '';
++++++++ if(_state.response !== null) {
++++++++ var fields = _state.response.fields;
++++++++ $.each(fields, function(name, array) {
++++++++ $.each(array, function(i, value) {
++++++++ rval += name + ': ' + value + '\r\n';
++++++++ });
++++++++ });
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ /**
++++++++ * Gets a single header field value or, if there are multiple
++++++++ * fields with the same name, a comma-separated list of header
++++++++ * values.
++++++++ *
++++++++ * @return the header field value(s) or null.
++++++++ */
++++++++ xhr.getResponseHeader = function(header) {
++++++++ var rval = null;
++++++++ if(_state.response !== null) {
++++++++ if(header in _state.response.fields) {
++++++++ rval = _state.response.fields[header];
++++++++ if(forge.util.isArray(rval)) {
++++++++ rval = rval.join();
++++++++ }
++++++++ }
++++++++ }
++++++++ return rval;
++++++++ };
++++++++
++++++++ return xhr;
++++++++};
++++++++
++++++++})(jQuery);
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "node-forge",
++++++++ "version": "1.3.1",
++++++++ "description": "JavaScript implementations of network transports, cryptography, ciphers, PKI, message digests, and various utilities.",
++++++++ "homepage": "https://github.com/digitalbazaar/forge",
++++++++ "author": {
++++++++ "name": "Digital Bazaar, Inc.",
++++++++ "email": "support@digitalbazaar.com",
++++++++ "url": "http://digitalbazaar.com/"
++++++++ },
++++++++ "contributors": [
++++++++ "Dave Longley <dlongley@digitalbazaar.com>",
++++++++ "David I. Lehn <dlehn@digitalbazaar.com>",
++++++++ "Stefan Siegl <stesie@brokenpipe.de>",
++++++++ "Christoph Dorn <christoph@christophdorn.com>"
++++++++ ],
++++++++ "devDependencies": {
++++++++ "browserify": "^16.5.2",
++++++++ "commander": "^2.20.0",
++++++++ "cross-env": "^5.2.1",
++++++++ "eslint": "^7.27.0",
++++++++ "eslint-config-digitalbazaar": "^2.8.0",
++++++++ "express": "^4.16.2",
++++++++ "karma": "^4.4.1",
++++++++ "karma-browserify": "^7.0.0",
++++++++ "karma-chrome-launcher": "^3.1.0",
++++++++ "karma-edge-launcher": "^0.4.2",
++++++++ "karma-firefox-launcher": "^1.3.0",
++++++++ "karma-ie-launcher": "^1.0.0",
++++++++ "karma-mocha": "^1.3.0",
++++++++ "karma-mocha-reporter": "^2.2.5",
++++++++ "karma-safari-launcher": "^1.0.0",
++++++++ "karma-sauce-launcher": "^2.0.2",
++++++++ "karma-sourcemap-loader": "^0.3.8",
++++++++ "karma-tap-reporter": "0.0.6",
++++++++ "karma-webpack": "^4.0.2",
++++++++ "mocha": "^5.2.0",
++++++++ "mocha-lcov-reporter": "^1.2.0",
++++++++ "nodejs-websocket": "^1.7.1",
++++++++ "nyc": "^15.1.0",
++++++++ "opts": "^1.2.7",
++++++++ "webpack": "^4.44.1",
++++++++ "webpack-cli": "^3.3.12",
++++++++ "worker-loader": "^2.0.0"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "https://github.com/digitalbazaar/forge"
++++++++ },
++++++++ "bugs": {
++++++++ "url": "https://github.com/digitalbazaar/forge/issues",
++++++++ "email": "support@digitalbazaar.com"
++++++++ },
++++++++ "license": "(BSD-3-Clause OR GPL-2.0)",
++++++++ "main": "lib/index.js",
++++++++ "files": [
++++++++ "lib/*.js",
++++++++ "flash/swf/*.swf",
++++++++ "dist/*.min.js",
++++++++ "dist/*.min.js.map"
++++++++ ],
++++++++ "engines": {
++++++++ "node": ">= 6.13.0"
++++++++ },
++++++++ "keywords": [
++++++++ "aes",
++++++++ "asn",
++++++++ "asn.1",
++++++++ "cbc",
++++++++ "crypto",
++++++++ "cryptography",
++++++++ "csr",
++++++++ "des",
++++++++ "gcm",
++++++++ "hmac",
++++++++ "http",
++++++++ "https",
++++++++ "md5",
++++++++ "network",
++++++++ "pkcs",
++++++++ "pki",
++++++++ "prng",
++++++++ "rc2",
++++++++ "rsa",
++++++++ "sha1",
++++++++ "sha256",
++++++++ "sha384",
++++++++ "sha512",
++++++++ "ssh",
++++++++ "tls",
++++++++ "x.509",
++++++++ "x509"
++++++++ ],
++++++++ "scripts": {
++++++++ "prepublish": "npm run build",
++++++++ "build": "webpack",
++++++++ "test-build": "webpack --config webpack-tests.config.js",
++++++++ "test": "npm run test-node",
++++++++ "test-node": "cross-env NODE_ENV=test mocha -t 30000 -R ${REPORTER:-spec} tests/unit/index.js",
++++++++ "test-karma": "karma start",
++++++++ "test-karma-sauce": "karma start karma-sauce.conf",
++++++++ "test-server": "node tests/server.js",
++++++++ "test-server-ws": "node tests/websockets/server-ws.js",
++++++++ "test-server-webid": "node tests/websockets/server-webid.js",
++++++++ "coverage": "rm -rf coverage && nyc --reporter=lcov --reporter=text-summary npm test",
++++++++ "coverage-ci": "rm -rf coverage && nyc --reporter=lcovonly npm test",
++++++++ "coverage-report": "nyc report",
++++++++ "lint": "eslint *.js lib/*.js tests/*.js tests/**/*.js examples/*.js flash/*.js"
++++++++ },
++++++++ "nyc": {
++++++++ "exclude": [
++++++++ "tests"
++++++++ ]
++++++++ },
++++++++ "jspm": {
++++++++ "format": "amd"
++++++++ },
++++++++ "browser": {
++++++++ "buffer": false,
++++++++ "crypto": false,
++++++++ "process": false
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++MIT License
++++++++
++++++++Copyright (c) 2013 José F. Romaniello
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to deal
++++++++in the Software without restriction, including without limitation the rights
++++++++to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
++++++++copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in all
++++++++copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
++++++++OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
++++++++SOFTWARE.
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { pki } from 'node-forge'
++++++++
++++++++declare interface SelfsignedOptions {
++++++++ /**
++++++++ * The number of days before expiration
++++++++ *
++++++++ * @default 365 */
++++++++ days?: number
++++++++ /**
++++++++ * the size for the private key in bits
++++++++ * @default 1024
++++++++ */
++++++++ keySize?: number
++++++++ /**
++++++++ * additional extensions for the certificate
++++++++ */
++++++++ extensions?: any[];
++++++++ /**
++++++++ * The signature algorithm sha256 or sha1
++++++++ * @default "sha1"
++++++++ */
++++++++ algorithm?: string
++++++++ /**
++++++++ * include PKCS#7 as part of the output
++++++++ * @default false
++++++++ */
++++++++ pkcs7?: boolean
++++++++ /**
++++++++ * generate client cert signed by the original key
++++++++ * @default false
++++++++ */
++++++++ clientCertificate?: undefined
++++++++ /**
++++++++ * client certificate's common name
++++++++ * @default "John Doe jdoe123"
++++++++ */
++++++++ clientCertificateCN?: string
++++++++}
++++++++
++++++++declare interface GenerateResult {
++++++++ private: string
++++++++ public: string
++++++++ cert: string
++++++++ fingerprint: string
++++++++}
++++++++
++++++++declare function generate(
++++++++ attrs?: pki.CertificateField[],
++++++++ opts?: SelfsignedOptions
++++++++): GenerateResult
++++++++
++++++++declare function generate(
++++++++ attrs?: pki.CertificateField[],
++++++++ opts?: SelfsignedOptions,
++++++++ /** Optional callback, if not provided the generation is synchronous */
++++++++ done?: (err: undefined | Error, result: GenerateResult) => any
++++++++): void
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++var forge = require('node-forge');
++++++++
++++++++// a hexString is considered negative if it's most significant bit is 1
++++++++// because serial numbers use ones' complement notation
++++++++// this RFC in section 4.1.2.2 requires serial numbers to be positive
++++++++// http://www.ietf.org/rfc/rfc5280.txt
++++++++function toPositiveHex(hexString){
++++++++ var mostSiginficativeHexAsInt = parseInt(hexString[0], 16);
++++++++ if (mostSiginficativeHexAsInt < 8){
++++++++ return hexString;
++++++++ }
++++++++
++++++++ mostSiginficativeHexAsInt -= 8;
++++++++ return mostSiginficativeHexAsInt.toString() + hexString.substring(1);
++++++++}
++++++++
++++++++function getAlgorithm(key) {
++++++++ switch (key) {
++++++++ case 'sha256':
++++++++ return forge.md.sha256.create();
++++++++ default:
++++++++ return forge.md.sha1.create();
++++++++ }
++++++++}
++++++++
++++++++/**
++++++++ *
++++++++ * @param {forge.pki.CertificateField[]} attrs Attributes used for subject and issuer.
++++++++ * @param {object} options
++++++++ * @param {number} [options.days=365] the number of days before expiration
++++++++ * @param {number} [options.keySize=1024] the size for the private key in bits
++++++++ * @param {object} [options.extensions] additional extensions for the certificate
++++++++ * @param {string} [options.algorithm="sha1"] The signature algorithm sha256 or sha1
++++++++ * @param {boolean} [options.pkcs7=false] include PKCS#7 as part of the output
++++++++ * @param {boolean} [options.clientCertificate=false] generate client cert signed by the original key
++++++++ * @param {string} [options.clientCertificateCN="John Doe jdoe123"] client certificate's common name
++++++++ * @param {function} [done] Optional callback, if not provided the generation is synchronous
++++++++ * @returns
++++++++ */
++++++++exports.generate = function generate(attrs, options, done) {
++++++++ if (typeof attrs === 'function') {
++++++++ done = attrs;
++++++++ attrs = undefined;
++++++++ } else if (typeof options === 'function') {
++++++++ done = options;
++++++++ options = {};
++++++++ }
++++++++
++++++++ options = options || {};
++++++++
++++++++ var generatePem = function (keyPair) {
++++++++ var cert = forge.pki.createCertificate();
++++++++
++++++++ cert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9))); // the serial number can be decimal or hex (if preceded by 0x)
++++++++
++++++++ cert.validity.notBefore = new Date();
++++++++ cert.validity.notAfter = new Date();
++++++++ cert.validity.notAfter.setDate(cert.validity.notBefore.getDate() + (options.days || 365));
++++++++
++++++++ attrs = attrs || [{
++++++++ name: 'commonName',
++++++++ value: 'example.org'
++++++++ }, {
++++++++ name: 'countryName',
++++++++ value: 'US'
++++++++ }, {
++++++++ shortName: 'ST',
++++++++ value: 'Virginia'
++++++++ }, {
++++++++ name: 'localityName',
++++++++ value: 'Blacksburg'
++++++++ }, {
++++++++ name: 'organizationName',
++++++++ value: 'Test'
++++++++ }, {
++++++++ shortName: 'OU',
++++++++ value: 'Test'
++++++++ }];
++++++++
++++++++ cert.setSubject(attrs);
++++++++ cert.setIssuer(attrs);
++++++++
++++++++ cert.publicKey = keyPair.publicKey;
++++++++
++++++++ cert.setExtensions(options.extensions || [{
++++++++ name: 'basicConstraints',
++++++++ cA: true
++++++++ }, {
++++++++ name: 'keyUsage',
++++++++ keyCertSign: true,
++++++++ digitalSignature: true,
++++++++ nonRepudiation: true,
++++++++ keyEncipherment: true,
++++++++ dataEncipherment: true
++++++++ }, {
++++++++ name: 'subjectAltName',
++++++++ altNames: [{
++++++++ type: 6, // URI
++++++++ value: 'http://example.org/webid#me'
++++++++ }]
++++++++ }]);
++++++++
++++++++ cert.sign(keyPair.privateKey, getAlgorithm(options && options.algorithm));
++++++++
++++++++ const fingerprint = forge.md.sha1
++++++++ .create()
++++++++ .update(forge.asn1.toDer(forge.pki.certificateToAsn1(cert)).getBytes())
++++++++ .digest()
++++++++ .toHex()
++++++++ .match(/.{2}/g)
++++++++ .join(':');
++++++++
++++++++ var pem = {
++++++++ private: forge.pki.privateKeyToPem(keyPair.privateKey),
++++++++ public: forge.pki.publicKeyToPem(keyPair.publicKey),
++++++++ cert: forge.pki.certificateToPem(cert),
++++++++ fingerprint: fingerprint,
++++++++ };
++++++++
++++++++ if (options && options.pkcs7) {
++++++++ var p7 = forge.pkcs7.createSignedData();
++++++++ p7.addCertificate(cert);
++++++++ pem.pkcs7 = forge.pkcs7.messageToPem(p7);
++++++++ }
++++++++
++++++++ if (options && options.clientCertificate) {
++++++++ var clientkeys = forge.pki.rsa.generateKeyPair(1024);
++++++++ var clientcert = forge.pki.createCertificate();
++++++++ clientcert.serialNumber = toPositiveHex(forge.util.bytesToHex(forge.random.getBytesSync(9)));
++++++++ clientcert.validity.notBefore = new Date();
++++++++ clientcert.validity.notAfter = new Date();
++++++++ clientcert.validity.notAfter.setFullYear(clientcert.validity.notBefore.getFullYear() + 1);
++++++++
++++++++ var clientAttrs = JSON.parse(JSON.stringify(attrs));
++++++++
++++++++ for(var i = 0; i < clientAttrs.length; i++) {
++++++++ if(clientAttrs[i].name === 'commonName') {
++++++++ if( options.clientCertificateCN )
++++++++ clientAttrs[i] = { name: 'commonName', value: options.clientCertificateCN };
++++++++ else
++++++++ clientAttrs[i] = { name: 'commonName', value: 'John Doe jdoe123' };
++++++++ }
++++++++ }
++++++++
++++++++ clientcert.setSubject(clientAttrs);
++++++++
++++++++ // Set the issuer to the parent key
++++++++ clientcert.setIssuer(attrs);
++++++++
++++++++ clientcert.publicKey = clientkeys.publicKey;
++++++++
++++++++ // Sign client cert with root cert
++++++++ clientcert.sign(keyPair.privateKey);
++++++++
++++++++ pem.clientprivate = forge.pki.privateKeyToPem(clientkeys.privateKey);
++++++++ pem.clientpublic = forge.pki.publicKeyToPem(clientkeys.publicKey);
++++++++ pem.clientcert = forge.pki.certificateToPem(clientcert);
++++++++
++++++++ if (options.pkcs7) {
++++++++ var clientp7 = forge.pkcs7.createSignedData();
++++++++ clientp7.addCertificate(clientcert);
++++++++ pem.clientpkcs7 = forge.pkcs7.messageToPem(clientp7);
++++++++ }
++++++++ }
++++++++
++++++++ var caStore = forge.pki.createCaStore();
++++++++ caStore.addCertificate(cert);
++++++++
++++++++ try {
++++++++ forge.pki.verifyCertificateChain(caStore, [cert],
++++++++ function (vfd, depth, chain) {
++++++++ if (vfd !== true) {
++++++++ throw new Error('Certificate could not be verified.');
++++++++ }
++++++++ return true;
++++++++ });
++++++++ }
++++++++ catch(ex) {
++++++++ throw new Error(ex);
++++++++ }
++++++++
++++++++ return pem;
++++++++ };
++++++++
++++++++ var keySize = options.keySize || 1024;
++++++++
++++++++ if (done) { // async scenario
++++++++ return forge.pki.rsa.generateKeyPair({ bits: keySize }, function (err, keyPair) {
++++++++ if (err) { return done(err); }
++++++++
++++++++ try {
++++++++ return done(null, generatePem(keyPair));
++++++++ } catch (ex) {
++++++++ return done(ex);
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ var keyPair = options.keyPair ? {
++++++++ privateKey: forge.pki.privateKeyFromPem(options.keyPair.privateKey),
++++++++ publicKey: forge.pki.publicKeyFromPem(options.keyPair.publicKey)
++++++++ } : forge.pki.rsa.generateKeyPair(keySize);
++++++++
++++++++ return generatePem(keyPair);
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "selfsigned",
++++++++ "version": "2.1.1",
++++++++ "description": "Generate self signed certificates private and public keys",
++++++++ "main": "index.js",
++++++++ "types": "index.d.ts",
++++++++ "scripts": {
++++++++ "test": "mocha -t 5000"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git://github.com/jfromaniello/selfsigned.git"
++++++++ },
++++++++ "keywords": [
++++++++ "openssl",
++++++++ "self",
++++++++ "signed",
++++++++ "certificates"
++++++++ ],
++++++++ "author": "José F. Romaniello <jfromaniello@gmail.com> (http://joseoncode.com)",
++++++++ "contributors": [
++++++++ {
++++++++ "name": "Paolo Fragomeni",
++++++++ "email": "paolo@async.ly",
++++++++ "url": "http://async.ly"
++++++++ },
++++++++ {
++++++++ "name": "Charles Bushong",
++++++++ "email": "bushong1@gmail.com",
++++++++ "url": "http://github.com/bushong1"
++++++++ }
++++++++ ],
++++++++ "license": "MIT",
++++++++ "dependencies": {
++++++++ "node-forge": "^1"
++++++++ },
++++++++ "devDependencies": {
++++++++ "chai": "^4.3.4",
++++++++ "mocha": "^9.1.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++---
++++++++Archive: GitHub
++++++++Bug-Database: https://github.com/nodejs/undici/issues
++++++++Bug-Submit: https://github.com/nodejs/undici/issues/new
++++++++Changelog: https://github.com/nodejs/undici/tags
++++++++Repository: https://github.com/nodejs/undici.git
++++++++Repository-Browse: https://github.com/nodejs/undici
++++++++Security-Contact: https://github.com/nodejs/undici/tree/HEAD/SECURITY.md
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++version=4
++++++++opts=\
++++++++dversionmangle=auto,\
++++++++repacksuffix=+dfsg1,\
++++++++filenamemangle=s/.*?(@ANY_VERSION@@ARCHIVE_EXT@)/undici-$1/ \
++++++++ https://github.com/nodejs/undici/tags .*?@ANY_VERSION@@ARCHIVE_EXT@ group
++++++++
++++++++opts=\
++++++++component=llhttp,\
++++++++mode=git,\
++++++++pretty=describe,\
++++++++uversionmangle=s/^v//;s/(?:[-]|\.[a-z]).*$//,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(@ANY_VERSION@@ARCHIVE_EXT@)/node-llhttp-$1/ \
++++++++ https://github.com/nodejs/llhttp HEAD checksum
++++++++
++++++++opts=\
++++++++component=llparse,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(@ANY_VERSION@@ARCHIVE_EXT@)/node-llparse-$1/ \
++++++++ https://github.com/nodejs/llparse/tags .*?@ANY_VERSION@@ARCHIVE_EXT@ checksum
++++++++
++++++++opts=\
++++++++component=llparse-frontend,\
++++++++ctype=nodejs,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(\d[\d\.-]*@ARCHIVE_EXT@)/node-llparse-frontend-$1/ \
++++++++ https://github.com/indutny/llparse-frontend/tags .*/archive.*/v?([\d\.]+).tar.gz checksum
++++++++
++++++++opts=\
++++++++component=llparse-builder,\
++++++++ctype=nodejs,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(\d[\d\.-]*@ARCHIVE_EXT@)/node-llparse-builder-$1/ \
++++++++ https://github.com/indutny/llparse-builder/tags .*/archive.*/v?([\d\.]+).tar.gz checksum
++++++++
++++++++opts=\
++++++++component=binary-search,\
++++++++ctype=nodejs,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(\d[\d\.-]*@ARCHIVE_EXT@)/node-binary-search-$1/ \
++++++++ https://github.com/darkskyapp/binary-search/tags .*/archive.*/v?([\d\.]+).tar.gz checksum
++++++++
++++++++opts=\
++++++++component=fastify-busboy,\
++++++++ctype=nodejs,\
++++++++dversionmangle=auto,\
++++++++filenamemangle=s/.*?(\d[\d\.-]*@ARCHIVE_EXT@)/node-busboy-$1/ \
++++++++ https://github.com/fastify/busboy/tags .*/archive.*/v?([\d\.]+).tar.gz checksum
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++module.exports = {
++++++++ ignorePatterns: [
++++++++ 'bench',
++++++++ 'deps/encoding'
++++++++ ],
++++++++ extends: [
++++++++ 'standard',
++++++++ 'eslint:recommended',
++++++++ 'plugin:n/recommended'
++++++++ ],
++++++++ rules: {
++++++++ 'no-unused-vars': [1, { vars: 'all', args: 'none' }],
++++++++ 'n/no-missing-require': 1,
++++++++ 'no-constant-condition': 'off',
++++++++ 'no-var': 'off',
++++++++ 'no-redeclare': 1,
++++++++ 'no-fallthrough': 1,
++++++++ 'no-control-regex': 1,
++++++++ 'no-empty': 'off',
++++++++ 'prefer-const': 'off'
++++++++ },
++++++++ env: {
++++++++ node: true,
++++++++ mocha: true,
++++++++ es6: true
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++* text=false
++++++++*.header -crlf
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++version: 2
++++++++updates:
++++++++ - package-ecosystem: "github-actions"
++++++++ directory: "/"
++++++++ schedule:
++++++++ interval: "monthly"
++++++++ open-pull-requests-limit: 10
++++++++
++++++++ - package-ecosystem: "npm"
++++++++ directory: "/"
++++++++ schedule:
++++++++ interval: "weekly"
++++++++ open-pull-requests-limit: 10
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++name: CI
++++++++
++++++++on:
++++++++ push:
++++++++ branches:
++++++++ - main
++++++++ - master
++++++++ - next
++++++++ - 'v*'
++++++++ paths-ignore:
++++++++ - 'docs/**'
++++++++ - '*.md'
++++++++ pull_request:
++++++++ paths-ignore:
++++++++ - 'docs/**'
++++++++ - '*.md'
++++++++
++++++++jobs:
++++++++ test:
++++++++ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
++++++++ with:
++++++++ license-check: true
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++---
++++++++
++++++++name: coverage
++++++++
++++++++on:
++++++++ push:
++++++++ branches:
++++++++ - master
++++++++
++++++++jobs:
++++++++ build:
++++++++ runs-on: ubuntu-latest
++++++++ name: coverage
++++++++
++++++++ strategy:
++++++++ matrix:
++++++++ node-version: [16.x]
++++++++
++++++++ steps:
++++++++ - name: Checkout Repository
++++++++ uses: actions/checkout@v4
++++++++ with:
++++++++ fetch-depth: 1
++++++++
++++++++ - name: Setup Node ${{ matrix.node-version }}
++++++++ uses: actions/setup-node@v4
++++++++ with:
++++++++ always-auth: false
++++++++ node-version: ${{ matrix.node-version }}
++++++++
++++++++ - name: Run npm install
++++++++ run: npm install
++++++++
++++++++ - name: Run Tests
++++++++ run: npm run test:coverage
++++++++
++++++++ - name: Generate LCOV
++++++++ run: npm run coveralls
++++++++
++++++++ - name: Update Coveralls
++++++++ uses: coverallsapp/github-action@master
++++++++ with:
++++++++ github-token: ${{ secrets.GITHUB_TOKEN }}
++++++++ if: success()
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++---
++++++++
++++++++name: Linting and Types
++++++++
++++++++on:
++++++++ pull_request:
++++++++ branches:
++++++++ - master
++++++++
++++++++jobs:
++++++++ build:
++++++++ runs-on: ubuntu-latest
++++++++ name: Linting and Types
++++++++
++++++++ strategy:
++++++++ matrix:
++++++++ node-version: [16.x]
++++++++
++++++++ steps:
++++++++ - name: Checkout Repository
++++++++ uses: actions/checkout@v4
++++++++ with:
++++++++ fetch-depth: 1
++++++++
++++++++ - name: Setup Node ${{ matrix.node-version }}
++++++++ uses: actions/setup-node@v4
++++++++ with:
++++++++ always-auth: false
++++++++ node-version: ${{ matrix.node-version }}
++++++++
++++++++ - name: Run npm install
++++++++ run: npm install
++++++++
++++++++ - name: Run lint:everything
++++++++ run: npm run lint:everything
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# Logs
++++++++logs
++++++++*.log
++++++++npm-debug.log*
++++++++yarn-debug.log*
++++++++yarn-error.log*
++++++++lerna-debug.log*
++++++++.pnpm-debug.log*
++++++++
++++++++# Diagnostic reports (https://nodejs.org/api/report.html)
++++++++report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
++++++++
++++++++# Runtime data
++++++++pids
++++++++*.pid
++++++++*.seed
++++++++*.pid.lock
++++++++
++++++++# Directory for instrumented libs generated by jscoverage/JSCover
++++++++lib-cov
++++++++
++++++++# Coverage directory used by tools like istanbul
++++++++coverage
++++++++*.lcov
++++++++
++++++++# nyc test coverage
++++++++.nyc_output
++++++++
++++++++# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
++++++++.grunt
++++++++
++++++++# Bower dependency directory (https://bower.io/)
++++++++bower_components
++++++++
++++++++# node-waf configuration
++++++++.lock-wscript
++++++++
++++++++# Compiled binary addons (https://nodejs.org/api/addons.html)
++++++++build/Release
++++++++
++++++++# Dependency directories
++++++++node_modules/
++++++++jspm_packages/
++++++++
++++++++# Snowpack dependency directory (https://snowpack.dev/)
++++++++web_modules/
++++++++
++++++++# TypeScript cache
++++++++*.tsbuildinfo
++++++++
++++++++# Optional npm cache directory
++++++++.npm
++++++++
++++++++# Optional eslint cache
++++++++.eslintcache
++++++++
++++++++# Optional stylelint cache
++++++++.stylelintcache
++++++++
++++++++# Microbundle cache
++++++++.rpt2_cache/
++++++++.rts2_cache_cjs/
++++++++.rts2_cache_es/
++++++++.rts2_cache_umd/
++++++++
++++++++# Optional REPL history
++++++++.node_repl_history
++++++++
++++++++# Output of 'npm pack'
++++++++*.tgz
++++++++
++++++++# Yarn Integrity file
++++++++.yarn-integrity
++++++++
++++++++# dotenv environment variable files
++++++++.env
++++++++.env.development.local
++++++++.env.test.local
++++++++.env.production.local
++++++++.env.local
++++++++
++++++++# parcel-bundler cache (https://parceljs.org/)
++++++++.cache
++++++++.parcel-cache
++++++++
++++++++# Next.js build output
++++++++.next
++++++++out
++++++++
++++++++# Nuxt.js build / generate output
++++++++.nuxt
++++++++dist
++++++++
++++++++# Gatsby files
++++++++.cache/
++++++++# Comment in the public line in if your project uses Gatsby and not Next.js
++++++++# https://nextjs.org/blog/next-9-1#public-directory-support
++++++++# public
++++++++
++++++++# vuepress build output
++++++++.vuepress/dist
++++++++
++++++++# vuepress v2.x temp and cache directory
++++++++.temp
++++++++.cache
++++++++
++++++++# Docusaurus cache and generated files
++++++++.docusaurus
++++++++
++++++++# Serverless directories
++++++++.serverless/
++++++++
++++++++# FuseBox cache
++++++++.fusebox/
++++++++
++++++++# DynamoDB Local files
++++++++.dynamodb/
++++++++
++++++++# TernJS port file
++++++++.tern-port
++++++++
++++++++# Stores VSCode versions used for testing VSCode extensions
++++++++.vscode-test
++++++++
++++++++# yarn v2
++++++++.yarn/cache
++++++++.yarn/unplugged
++++++++.yarn/build-state.yml
++++++++.yarn/install-state.gz
++++++++.pnp.*
++++++++
++++++++# Vim swap files
++++++++*.swp
++++++++
++++++++# macOS files
++++++++.DS_Store
++++++++
++++++++# Clinic
++++++++.clinic
++++++++
++++++++# lock files
++++++++bun.lockb
++++++++package-lock.json
++++++++pnpm-lock.yaml
++++++++yarn.lock
++++++++
++++++++# editor files
++++++++.vscode
++++++++.idea
++++++++
++++++++#tap files
++++++++.tap/
++++++++
++++++++/benchmarks/node_modules/
++++++++/benchmarks/package-lock.json
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++files:
++++++++ - test/**/*.test.js
++++++++
++++++++coverage: false
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# Changelog
++++++++
++++++++Major changes since the last busboy release (0.3.1):
++++++++
++++++++# 1.1.0 - 09 June, 2022
++++++++
++++++++* Fix potential ReDOS-Attack-Vector in Headerparser (#72)
++++++++* Improve array parse performances (#69)
++++++++* Export Dicer library (#90)
++++++++
++++++++# 1.0.0 - 04 December, 2021
++++++++
++++++++* Prevent malformed headers from crashing the web server (#34)
++++++++* Prevent empty parts from hanging the process (#55)
++++++++* Use non-deprecated Buffer creation (#8, #10)
++++++++* Include TypeScript types in the package itself (#13)
++++++++* Make `busboy` importable both as ESM and as CJS module (#61)
++++++++* Improve performance (#21, #32, #36)
++++++++* Set `autoDestroy` to `false` by default in order to avoid regressions when upgrading from Node.js 12 to Node.js 14 (#9)
++++++++* Add option `isPartAFile`, to make the file-detection configurable (#53)
++++++++* Add property `bytesRead` on FileStreams (#51)
++++++++* Add and expose headerSize limit (#64)
++++++++* Throw an error on non-number limit (#7)
++++++++* Use the native TextDecoder and the package `text-decoding` for fallback if Node.js does not support the requested encoding (#50)
++++++++* Integrate `dicer` dependency into `busboy` itself (#14)
++++++++* Convert tests to Mocha (#11, #12, #22, #23)
++++++++* Implement better benchmarks (#40, #54)
++++++++* Use JavaScript Standard style (#44, #45)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Copyright Brian White. All rights reserved.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to
++++++++deal in the Software without restriction, including without limitation the
++++++++rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++++++++sell copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in
++++++++all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++++++++FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++++++++IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# busboy
++++++++
++++++++<div align="center">
++++++++
++++++++[](https://github.com/fastify/busboy/actions)
++++++++[](https://coveralls.io/r/fastify/busboy?branch=master)
++++++++[](https://standardjs.com/)
++++++++[](https://github.com/fastify/.github/blob/main/SECURITY.md)
++++++++
++++++++</div>
++++++++
++++++++<div align="center">
++++++++
++++++++[](https://www.npmjs.com/package/@fastify/busboy)
++++++++[](https://www.npmjs.com/package/@fastify/busboy)
++++++++
++++++++</div>
++++++++
++++++++Description
++++++++===========
++++++++
++++++++A Node.js module for parsing incoming HTML form data.
++++++++
++++++++This is an officially supported fork by [fastify](https://github.com/fastify/) organization of the amazing library [originally created](https://github.com/mscdex/busboy) by Brian White,
++++++++aimed at addressing long-standing issues with it.
++++++++
++++++++Benchmark (Mean time for 500 Kb payload, 2000 cycles, 1000 cycle warmup):
++++++++
++++++++| Library | Version | Mean time in nanoseconds (less is better) |
++++++++|-----------------------|---------|-------------------------------------------|
++++++++| busboy | 0.3.1 | `340114` |
++++++++| @fastify/busboy | 1.0.0 | `270984` |
++++++++
++++++++[Changelog](https://github.com/fastify/busboy/blob/master/CHANGELOG.md) since busboy 0.31.
++++++++
++++++++Requirements
++++++++============
++++++++
++++++++* [Node.js](http://nodejs.org/) 10+
++++++++
++++++++
++++++++Install
++++++++=======
++++++++
++++++++ npm i @fastify/busboy
++++++++
++++++++
++++++++Examples
++++++++========
++++++++
++++++++* Parsing (multipart) with default options:
++++++++
++++++++```javascript
++++++++const http = require('node:http');
++++++++const { inspect } = require('node:util');
++++++++const Busboy = require('busboy');
++++++++
++++++++http.createServer((req, res) => {
++++++++ if (req.method === 'POST') {
++++++++ const busboy = new Busboy({ headers: req.headers });
++++++++ busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ console.log(`File [${fieldname}]: filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype}`);
++++++++ file.on('data', data => {
++++++++ console.log(`File [${fieldname}] got ${data.length} bytes`);
++++++++ });
++++++++ file.on('end', () => {
++++++++ console.log(`File [${fieldname}] Finished`);
++++++++ });
++++++++ });
++++++++ busboy.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ console.log(`Field [${fieldname}]: value: ${inspect(val)}`);
++++++++ });
++++++++ busboy.on('finish', () => {
++++++++ console.log('Done parsing form!');
++++++++ res.writeHead(303, { Connection: 'close', Location: '/' });
++++++++ res.end();
++++++++ });
++++++++ req.pipe(busboy);
++++++++ } else if (req.method === 'GET') {
++++++++ res.writeHead(200, { Connection: 'close' });
++++++++ res.end(`<html><head></head><body>
++++++++ <form method="POST" enctype="multipart/form-data">
++++++++ <input type="text" name="textfield"><br>
++++++++ <input type="file" name="filefield"><br>
++++++++ <input type="submit">
++++++++ </form>
++++++++ </body></html>`);
++++++++ }
++++++++}).listen(8000, () => {
++++++++ console.log('Listening for requests');
++++++++});
++++++++
++++++++// Example output, using http://nodejs.org/images/ryan-speaker.jpg as the file:
++++++++//
++++++++// Listening for requests
++++++++// File [filefield]: filename: ryan-speaker.jpg, encoding: binary
++++++++// File [filefield] got 11971 bytes
++++++++// Field [textfield]: value: 'testing! :-)'
++++++++// File [filefield] Finished
++++++++// Done parsing form!
++++++++```
++++++++
++++++++* Save all incoming files to disk:
++++++++
++++++++```javascript
++++++++const http = require('node:http');
++++++++const path = require('node:path');
++++++++const os = require('node:os');
++++++++const fs = require('node:fs');
++++++++
++++++++const Busboy = require('busboy');
++++++++
++++++++http.createServer(function(req, res) {
++++++++ if (req.method === 'POST') {
++++++++ const busboy = new Busboy({ headers: req.headers });
++++++++ busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
++++++++ var saveTo = path.join(os.tmpdir(), path.basename(fieldname));
++++++++ file.pipe(fs.createWriteStream(saveTo));
++++++++ });
++++++++ busboy.on('finish', function() {
++++++++ res.writeHead(200, { 'Connection': 'close' });
++++++++ res.end("That's all folks!");
++++++++ });
++++++++ return req.pipe(busboy);
++++++++ }
++++++++ res.writeHead(404);
++++++++ res.end();
++++++++}).listen(8000, function() {
++++++++ console.log('Listening for requests');
++++++++});
++++++++```
++++++++
++++++++* Parsing (urlencoded) with default options:
++++++++
++++++++```javascript
++++++++const http = require('node:http');
++++++++const { inspect } = require('node:util');
++++++++
++++++++const Busboy = require('busboy');
++++++++
++++++++http.createServer(function(req, res) {
++++++++ if (req.method === 'POST') {
++++++++ const busboy = new Busboy({ headers: req.headers });
++++++++ busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
++++++++ console.log('File [' + fieldname + ']: filename: ' + filename);
++++++++ file.on('data', function(data) {
++++++++ console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
++++++++ });
++++++++ file.on('end', function() {
++++++++ console.log('File [' + fieldname + '] Finished');
++++++++ });
++++++++ });
++++++++ busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
++++++++ console.log('Field [' + fieldname + ']: value: ' + inspect(val));
++++++++ });
++++++++ busboy.on('finish', function() {
++++++++ console.log('Done parsing form!');
++++++++ res.writeHead(303, { Connection: 'close', Location: '/' });
++++++++ res.end();
++++++++ });
++++++++ req.pipe(busboy);
++++++++ } else if (req.method === 'GET') {
++++++++ res.writeHead(200, { Connection: 'close' });
++++++++ res.end('<html><head></head><body>\
++++++++ <form method="POST">\
++++++++ <input type="text" name="textfield"><br />\
++++++++ <select name="selectfield">\
++++++++ <option value="1">1</option>\
++++++++ <option value="10">10</option>\
++++++++ <option value="100">100</option>\
++++++++ <option value="9001">9001</option>\
++++++++ </select><br />\
++++++++ <input type="checkbox" name="checkfield">Node.js rules!<br />\
++++++++ <input type="submit">\
++++++++ </form>\
++++++++ </body></html>');
++++++++ }
++++++++}).listen(8000, function() {
++++++++ console.log('Listening for requests');
++++++++});
++++++++
++++++++// Example output:
++++++++//
++++++++// Listening for requests
++++++++// Field [textfield]: value: 'testing! :-)'
++++++++// Field [selectfield]: value: '9001'
++++++++// Field [checkfield]: value: 'on'
++++++++// Done parsing form!
++++++++```
++++++++
++++++++
++++++++API
++++++++===
++++++++
++++++++_Busboy_ is a _Writable_ stream
++++++++
++++++++Busboy (special) events
++++++++-----------------------
++++++++
++++++++* **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
++++++++ * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
++++++++ * If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
++++++++ * The property `bytesRead` informs about the number of bytes that have been read so far.
++++++++
++++++++* **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.
++++++++
++++++++* **partsLimit**() - Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
++++++++
++++++++* **filesLimit**() - Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
++++++++
++++++++* **fieldsLimit**() - Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
++++++++
++++++++
++++++++Busboy methods
++++++++--------------
++++++++
++++++++* **(constructor)**(< _object_ >config) - Creates and returns a new Busboy instance.
++++++++
++++++++ * The constructor takes the following valid `config` settings:
++++++++
++++++++ * **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers.
++++++++
++++++++ * **autoDestroy** - _boolean_ - Whether this stream should automatically call .destroy() on itself after ending. (Default: false).
++++++++
++++++++ * **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default).
++++++++
++++++++ * **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default).
++++++++
++++++++ * **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8').
++++++++
++++++++ * **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false).
++++++++
++++++++ * **isPartAFile** - __function__ - Use this function to override the default file detection functionality. It has following parameters:
++++++++
++++++++ * fieldName - __string__ The name of the field.
++++++++
++++++++ * contentType - __string__ The content-type of the part, e.g. `text/plain`, `image/jpeg`, `application/octet-stream`
++++++++
++++++++ * fileName - __string__ The name of a file supplied by the part.
++++++++
++++++++ (Default: `(fieldName, contentType, fileName) => (contentType === 'application/octet-stream' || fileName !== undefined)`)
++++++++
++++++++ * **limits** - _object_ - Various limits on incoming data. Valid properties are:
++++++++
++++++++ * **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes).
++++++++
++++++++ * **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1 MiB, which is 1024 x 1024 bytes).
++++++++
++++++++ * **fields** - _integer_ - Max number of non-file fields (Default: Infinity).
++++++++
++++++++ * **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity).
++++++++
++++++++ * **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity).
++++++++
++++++++ * **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity).
++++++++
++++++++ * **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000
++++++++
++++++++ * **headerSize** - _integer_ - For multipart forms, the max size of a multipart header **Default:** 81920.
++++++++
++++++++ * The constructor can throw errors:
++++++++
++++++++ * **Busboy expected an options-Object.** - Busboy expected an Object as first parameters.
++++++++
++++++++ * **Busboy expected an options-Object with headers-attribute.** - The first parameter is lacking of a headers-attribute.
++++++++
++++++++ * **Limit $limit is not a valid number** - Busboy expected the desired limit to be of type number. Busboy throws this Error to prevent a potential security issue by falling silently back to the Busboy-defaults. Potential source for this Error can be the direct use of environment variables without transforming them to the type number.
++++++++
++++++++ * **Unsupported Content-Type.** - The `Content-Type` isn't one Busboy can parse.
++++++++
++++++++ * **Missing Content-Type-header.** - The provided headers don't include `Content-Type` at all.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('busboy');
++++++++const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
++++++++
++++++++ for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
++++++++ const boundary = '-----------------------------168072824752491622650073',
++++++++ busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ }),
++++++++ buffer = createMultipartBufferForEncodingBench(boundary, 100, 'iso-8859-1'),
++++++++ mb = buffer.length / 1048576;
++++++++
++++++++ let processedData = 0;
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ file.resume()
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ })
++++++++
++++++++ const start = +new Date();
++++++++ const result = busboy.write(buffer, () => { });
++++++++ busboy.end();
++++++++ const duration = +new Date - start;
++++++++ const mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++ console.log(mbPerSec + ' mb/sec');
++++++++ }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('busboy');
++++++++const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
++++++++
++++++++ for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
++++++++ const boundary = '-----------------------------168072824752491622650073',
++++++++ busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ }),
++++++++ buffer = createMultipartBufferForEncodingBench(boundary, 100, 'utf-8'),
++++++++ mb = buffer.length / 1048576;
++++++++
++++++++ let processedData = 0;
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ file.resume()
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ })
++++++++
++++++++ const start = +new Date();
++++++++ const result = busboy.write(buffer, () => { });
++++++++ busboy.end();
++++++++ const duration = +new Date - start;
++++++++ const mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++ console.log(mbPerSec + ' mb/sec');
++++++++ }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++function createMultipartBufferForEncodingBench(boundary, amount, charset) {
++++++++ const filename = charset === 'utf-8' ? 'utf-8\'\'%c2%a3%20and%20%e2%82%ac%20rates' : `${charset}\'en\'%A3%20rates`;
++++++++ const head = '--' + boundary + '\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + 'content-type: text/plain;charset=' + charset + '; filename*=' + filename + '\r\n'
++++++++ + '\r\n', tail = '\r\n--' + boundary + '--\r\n', buffer = Buffer.concat([Buffer.from(head), Buffer.from(`
++++++++Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pretium leo ex, vitae dignissim felis viverra non. Praesent id quam ac elit tincidunt porttitor sed eget magna. Vivamus nibh ipsum, malesuada in eros sit amet, rutrum mattis leo. Ut nunc justo, ornare a finibus in, consectetur euismod sapien. Praesent facilisis, odio consectetur facilisis varius, tellus justo tristique sapien, non porttitor eros massa quis nibh. Nam blandit orci ac efficitur cursus. Nunc non mollis sapien, sit amet pretium odio. Nam vestibulum lectus ac orci egestas aliquet. Duis nec nibh quis augue consequat vulputate a a dui.
++++++++
++++++++Aenean nec laoreet dolor, commodo aliquam leo. Quisque at placerat sem. In scelerisque cursus dolor, ac aliquam metus malesuada in. Vestibulum lacinia dolor purus, at convallis ipsum iaculis id. Integer bibendum sem neque, at bibendum enim lobortis eu. Cras pretium arcu eget congue cursus. Curabitur blandit ultricies mollis. Sed lacinia quis felis ut fringilla.
++++++++
++++++++Nulla vitae lobortis metus. Morbi gravida risus tortor, in pulvinar massa lobortis vitae. Etiam vitae massa libero. Sed id tincidunt elit. Quisque congue felis vel aliquam varius. Sed a massa vitae lectus vehicula lacinia vitae ac justo. In commodo sodales nisi finibus vulputate. Suspendisse viverra, est eget fringilla gravida, nulla justo vulputate lorem, at eleifend nisi urna a eros. Sed sit amet ipsum vehicula, venenatis urna ac, interdum felis.
++++++++
++++++++Cras semper mi magna, nec iaculis neque rhoncus at. In sit amet odio sed libero fringilla commodo. Sed hendrerit pulvinar turpis sed porta. Pellentesque consequat scelerisque sapien nec iaculis. Aenean sed nunc a purus laoreet efficitur id eu orci. Mauris tincidunt auctor congue. Aliquam nisi ligula, facilisis a molestie sed, luctus vitae mauris. Mauris at facilisis elit. Maecenas sodales pretium nisi in sodales. Cras nec blandit enim. Praesent in lacus et nibh varius suscipit in sit amet nibh.
++++++++
++++++++Nam hendrerit justo eu lectus molestie, sit amet fringilla ipsum semper. Maecenas sit amet nunc elementum, interdum nunc eu, euismod ipsum. Vestibulum ut mauris sapien. Praesent nec felis ex. Fusce vel leo lobortis, mattis sem a, ullamcorper dolor. Aliquam erat volutpat. Fusce feugiat odio ut feugiat volutpat. Vestibulum magna ante, tempor in volutpat ut, gravida vitae justo. Praesent vitae eleifend eros. Integer feugiat molestie dolor, et pretium enim accumsan sit amet. Sed quis suscipit dui. Integer gravida dolor elit, sit amet fringilla odio commodo at. Quisque ut eleifend risus. Nunc mollis velit quis lectus laoreet pellentesque.\r\n\r\n`)]);
++++++++
++++++++ const buffers = new Array(amount).fill(buffer);
++++++++ buffers.push(Buffer.from(tail));
++++++++ return Buffer.concat(buffers);
++++++++}
++++++++exports.createMultipartBufferForEncodingBench = createMultipartBufferForEncodingBench;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Dicer = require('../../deps/dicer/lib/Dicer')
++++++++
++++++++function createMultipartBuffer(boundary, size) {
++++++++ const head =
++++++++ '--' + boundary + '\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + '\r\n'
++++++++ , tail = '\r\n--' + boundary + '--\r\n'
++++++++ , buffer = Buffer.allocUnsafe(size);
++++++++
++++++++ buffer.write(head, 0, 'ascii');
++++++++ buffer.write(tail, buffer.length - tail.length, 'ascii');
++++++++ return buffer;
++++++++}
++++++++
++++++++for (var i = 0, il = 10; i < il; i++) { // eslint-disable-line no-var
++++++++ const boundary = '-----------------------------168072824752491622650073',
++++++++ d = new Dicer({ boundary: boundary }),
++++++++ mb = 100,
++++++++ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
++++++++ callbacks =
++++++++ {
++++++++ partBegin: -1,
++++++++ partEnd: -1,
++++++++ headerField: -1,
++++++++ headerValue: -1,
++++++++ partData: -1,
++++++++ end: -1,
++++++++ };
++++++++
++++++++
++++++++ d.on('part', function (p) {
++++++++ callbacks.partBegin++;
++++++++ p.on('header', function (header) {
++++++++ /*for (var h in header)
++++++++ console.log('Part header: k: ' + inspect(h) + ', v: ' + inspect(header[h]));*/
++++++++ });
++++++++ p.on('data', function (data) {
++++++++ callbacks.partData++;
++++++++ //console.log('Part data: ' + inspect(data.toString()));
++++++++ });
++++++++ p.on('end', function () {
++++++++ //console.log('End of part\n');
++++++++ callbacks.partEnd++;
++++++++ });
++++++++ });
++++++++ d.on('end', function () {
++++++++ //console.log('End of parts');
++++++++ callbacks.end++;
++++++++ });
++++++++
++++++++ const start = +new Date();
++++++++ d.write(buffer);
++++++++ const duration = +new Date - start;
++++++++ const mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++
++++++++ console.log(mbPerSec + ' mb/sec');
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++require('../node_modules/formidable/test/common');
++++++++var multipartParser = require('../node_modules/formidable/lib/multipart_parser'),
++++++++ MultipartParser = multipartParser.MultipartParser,
++++++++ parser = new MultipartParser(),
++++++++ boundary = '-----------------------------168072824752491622650073',
++++++++ mb = 100,
++++++++ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
++++++++ callbacks =
++++++++ { partBegin: -1,
++++++++ partEnd: -1,
++++++++ headerField: -1,
++++++++ headerValue: -1,
++++++++ partData: -1,
++++++++ end: -1,
++++++++ };
++++++++
++++++++
++++++++parser.initWithBoundary(boundary);
++++++++parser.onHeaderField = function() {
++++++++ callbacks.headerField++;
++++++++};
++++++++
++++++++parser.onHeaderValue = function() {
++++++++ callbacks.headerValue++;
++++++++};
++++++++
++++++++parser.onPartBegin = function() {
++++++++ callbacks.partBegin++;
++++++++};
++++++++
++++++++parser.onPartData = function() {
++++++++ callbacks.partData++;
++++++++};
++++++++
++++++++parser.onPartEnd = function() {
++++++++ callbacks.partEnd++;
++++++++};
++++++++
++++++++parser.onEnd = function() {
++++++++ callbacks.end++;
++++++++};
++++++++
++++++++var start = +new Date(),
++++++++ nparsed = parser.write(buffer),
++++++++ duration = +new Date - start,
++++++++ mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++
++++++++console.log(mbPerSec+' mb/sec');
++++++++
++++++++//assert.equal(nparsed, buffer.length);
++++++++
++++++++function createMultipartBuffer(boundary, size) {
++++++++ var head =
++++++++ '--'+boundary+'\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + '\r\n'
++++++++ , tail = '\r\n--'+boundary+'--\r\n'
++++++++ , buffer = Buffer.allocUnsafe(size);
++++++++
++++++++ buffer.write(head, 'ascii', 0);
++++++++ buffer.write(tail, 'ascii', buffer.length - tail.length);
++++++++ return buffer;
++++++++}
++++++++
++++++++process.on('exit', function() {
++++++++ /*for (var k in callbacks) {
++++++++ assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
++++++++ }*/
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++var multipartser = require('multipartser'),
++++++++ boundary = '-----------------------------168072824752491622650073',
++++++++ parser = multipartser(),
++++++++ mb = 100,
++++++++ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
++++++++ callbacks =
++++++++ { partBegin: -1,
++++++++ partEnd: -1,
++++++++ headerField: -1,
++++++++ headerValue: -1,
++++++++ partData: -1,
++++++++ end: -1,
++++++++ };
++++++++
++++++++parser.boundary( boundary );
++++++++
++++++++parser.on( 'part', function ( part ) {
++++++++});
++++++++
++++++++parser.on( 'end', function () {
++++++++ //console.log( 'completed parsing' );
++++++++});
++++++++
++++++++parser.on( 'error', function ( error ) {
++++++++ console.error( error );
++++++++});
++++++++
++++++++var start = +new Date(),
++++++++ nparsed = parser.data(buffer),
++++++++ nend = parser.end(),
++++++++ duration = +new Date - start,
++++++++ mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++
++++++++console.log(mbPerSec+' mb/sec');
++++++++
++++++++//assert.equal(nparsed, buffer.length);
++++++++
++++++++function createMultipartBuffer(boundary, size) {
++++++++ var head =
++++++++ '--'+boundary+'\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + '\r\n'
++++++++ , tail = '\r\n--'+boundary+'--\r\n'
++++++++ , buffer = Buffer.allocUnsafe(size);
++++++++
++++++++ buffer.write(head, 'ascii', 0);
++++++++ buffer.write(tail, 'ascii', buffer.length - tail.length);
++++++++ return buffer;
++++++++}
++++++++
++++++++process.on('exit', function() {
++++++++ /*for (var k in callbacks) {
++++++++ assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
++++++++ }*/
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++var assert = require('node:assert'),
++++++++ Form = require('multiparty').Form,
++++++++ boundary = '-----------------------------168072824752491622650073',
++++++++ mb = 100,
++++++++ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
++++++++ callbacks =
++++++++ { partBegin: -1,
++++++++ partEnd: -1,
++++++++ headerField: -1,
++++++++ headerValue: -1,
++++++++ partData: -1,
++++++++ end: -1,
++++++++ };
++++++++
++++++++var form = new Form({ boundary: boundary });
++++++++
++++++++hijack('onParseHeaderField', function() {
++++++++ callbacks.headerField++;
++++++++});
++++++++
++++++++hijack('onParseHeaderValue', function() {
++++++++ callbacks.headerValue++;
++++++++});
++++++++
++++++++hijack('onParsePartBegin', function() {
++++++++ callbacks.partBegin++;
++++++++});
++++++++
++++++++hijack('onParsePartData', function() {
++++++++ callbacks.partData++;
++++++++});
++++++++
++++++++hijack('onParsePartEnd', function() {
++++++++ callbacks.partEnd++;
++++++++});
++++++++
++++++++form.on('finish', function() {
++++++++ callbacks.end++;
++++++++});
++++++++
++++++++var start = new Date();
++++++++form.write(buffer, function(err) {
++++++++ var duration = new Date() - start;
++++++++ assert.ifError(err);
++++++++ var mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++ console.log(mbPerSec+' mb/sec');
++++++++});
++++++++
++++++++//assert.equal(nparsed, buffer.length);
++++++++
++++++++function createMultipartBuffer(boundary, size) {
++++++++ var head =
++++++++ '--'+boundary+'\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + '\r\n'
++++++++ , tail = '\r\n--'+boundary+'--\r\n'
++++++++ , buffer = Buffer.allocUnsafe(size);
++++++++
++++++++ buffer.write(head, 'ascii', 0);
++++++++ buffer.write(tail, 'ascii', buffer.length - tail.length);
++++++++ return buffer;
++++++++}
++++++++
++++++++process.on('exit', function() {
++++++++ /*for (var k in callbacks) {
++++++++ assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
++++++++ }*/
++++++++});
++++++++
++++++++function hijack(name, fn) {
++++++++ var oldFn = form[name];
++++++++ form[name] = function() {
++++++++ fn();
++++++++ return oldFn.apply(this, arguments);
++++++++ };
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++// A special, edited version of the multipart parser from parted is needed here
++++++++// because otherwise it attempts to do some things above and beyond just parsing
++++++++// -- like saving to disk and whatnot
++++++++
++++++++var assert = require('node:assert');
++++++++var Parser = require('./parted-multipart'),
++++++++ boundary = '-----------------------------168072824752491622650073',
++++++++ parser = new Parser('boundary=' + boundary),
++++++++ mb = 100,
++++++++ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
++++++++ callbacks =
++++++++ { partBegin: -1,
++++++++ partEnd: -1,
++++++++ headerField: -1,
++++++++ headerValue: -1,
++++++++ partData: -1,
++++++++ end: -1,
++++++++ };
++++++++
++++++++
++++++++parser.on('header', function() {
++++++++ //callbacks.headerField++;
++++++++});
++++++++
++++++++parser.on('data', function() {
++++++++ //callbacks.partBegin++;
++++++++});
++++++++
++++++++parser.on('part', function() {
++++++++
++++++++});
++++++++
++++++++parser.on('end', function() {
++++++++ //callbacks.end++;
++++++++});
++++++++
++++++++var start = +new Date(),
++++++++ nparsed = parser.write(buffer),
++++++++ duration = +new Date - start,
++++++++ mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++
++++++++console.log(mbPerSec+' mb/sec');
++++++++
++++++++//assert.equal(nparsed, buffer.length);
++++++++
++++++++function createMultipartBuffer(boundary, size) {
++++++++ var head =
++++++++ '--'+boundary+'\r\n'
++++++++ + 'content-disposition: form-data; name="field1"\r\n'
++++++++ + '\r\n'
++++++++ , tail = '\r\n--'+boundary+'--\r\n'
++++++++ , buffer = Buffer.allocUnsafe(size);
++++++++
++++++++ buffer.write(head, 'ascii', 0);
++++++++ buffer.write(tail, 'ascii', buffer.length - tail.length);
++++++++ return buffer;
++++++++}
++++++++
++++++++process.on('exit', function() {
++++++++ /*for (var k in callbacks) {
++++++++ assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
++++++++ }*/
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++/**
++++++++ * Parted (https://github.com/chjj/parted)
++++++++ * A streaming multipart state parser.
++++++++ * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
++++++++ */
++++++++
++++++++var fs = require('node:fs')
++++++++ , path = require('node:path')
++++++++ , EventEmitter = require('node:events').EventEmitter
++++++++ , StringDecoder = require('node:string_decoder').StringDecoder
++++++++ , set = require('qs').set
++++++++ , each = Array.prototype.forEach;
++++++++
++++++++/**
++++++++ * Character Constants
++++++++ */
++++++++
++++++++var DASH = '-'.charCodeAt(0)
++++++++ , CR = '\r'.charCodeAt(0)
++++++++ , LF = '\n'.charCodeAt(0)
++++++++ , COLON = ':'.charCodeAt(0)
++++++++ , SPACE = ' '.charCodeAt(0);
++++++++
++++++++/**
++++++++ * Parser
++++++++ */
++++++++
++++++++var Parser = function(type, options) {
++++++++ if (!(this instanceof Parser)) {
++++++++ return new Parser(type, options);
++++++++ }
++++++++
++++++++ EventEmitter.call(this);
++++++++
++++++++ this.writable = true;
++++++++ this.readable = true;
++++++++
++++++++ this.options = options || {};
++++++++
++++++++ var key = grab(type, 'boundary');
++++++++ if (!key) {
++++++++ return this._error('No boundary key found.');
++++++++ }
++++++++
++++++++ this.key = Buffer.allocUnsafe('\r\n--' + key);
++++++++
++++++++ this._key = {};
++++++++ each.call(this.key, function(ch) {
++++++++ this._key[ch] = true;
++++++++ }, this);
++++++++
++++++++ this.state = 'start';
++++++++ this.pending = 0;
++++++++ this.written = 0;
++++++++ this.writtenDisk = 0;
++++++++ this.buff = Buffer.allocUnsafe(200);
++++++++
++++++++ this.preamble = true;
++++++++ this.epilogue = false;
++++++++
++++++++ this._reset();
++++++++};
++++++++
++++++++Parser.prototype.__proto__ = EventEmitter.prototype;
++++++++
++++++++/**
++++++++ * Parsing
++++++++ */
++++++++
++++++++Parser.prototype.write = function(data) {
++++++++ if (!this.writable
++++++++ || this.epilogue) return;
++++++++
++++++++ try {
++++++++ this._parse(data);
++++++++ } catch (e) {
++++++++ this._error(e);
++++++++ }
++++++++
++++++++ return true;
++++++++};
++++++++
++++++++Parser.prototype.end = function(data) {
++++++++ if (!this.writable) return;
++++++++
++++++++ if (data) this.write(data);
++++++++
++++++++ if (!this.epilogue) {
++++++++ return this._error('Message underflow.');
++++++++ }
++++++++
++++++++ return true;
++++++++};
++++++++
++++++++Parser.prototype._parse = function(data) {
++++++++ var i = 0
++++++++ , len = data.length
++++++++ , buff = this.buff
++++++++ , key = this.key
++++++++ , ch
++++++++ , val
++++++++ , j;
++++++++
++++++++ for (; i < len; i++) {
++++++++ if (this.pos >= 200) {
++++++++ return this._error('Potential buffer overflow.');
++++++++ }
++++++++
++++++++ ch = data[i];
++++++++
++++++++ switch (this.state) {
++++++++ case 'start':
++++++++ switch (ch) {
++++++++ case DASH:
++++++++ this.pos = 3;
++++++++ this.state = 'key';
++++++++ break;
++++++++ default:
++++++++ break;
++++++++ }
++++++++ break;
++++++++ case 'key':
++++++++ if (this.pos === key.length) {
++++++++ this.state = 'key_end';
++++++++ i--;
++++++++ } else if (ch !== key[this.pos]) {
++++++++ if (this.preamble) {
++++++++ this.state = 'start';
++++++++ i--;
++++++++ } else {
++++++++ this.state = 'body';
++++++++ val = this.pos - i;
++++++++ if (val > 0) {
++++++++ this._write(key.slice(0, val));
++++++++ }
++++++++ i--;
++++++++ }
++++++++ } else {
++++++++ this.pos++;
++++++++ }
++++++++ break;
++++++++ case 'key_end':
++++++++ switch (ch) {
++++++++ case CR:
++++++++ this.state = 'key_line_end';
++++++++ break;
++++++++ case DASH:
++++++++ this.state = 'key_dash_end';
++++++++ break;
++++++++ default:
++++++++ return this._error('Expected CR or DASH.');
++++++++ }
++++++++ break;
++++++++ case 'key_line_end':
++++++++ switch (ch) {
++++++++ case LF:
++++++++ if (this.preamble) {
++++++++ this.preamble = false;
++++++++ } else {
++++++++ this._finish();
++++++++ }
++++++++ this.state = 'header_name';
++++++++ this.pos = 0;
++++++++ break;
++++++++ default:
++++++++ return this._error('Expected CR.');
++++++++ }
++++++++ break;
++++++++ case 'key_dash_end':
++++++++ switch (ch) {
++++++++ case DASH:
++++++++ this.epilogue = true;
++++++++ this._finish();
++++++++ return;
++++++++ default:
++++++++ return this._error('Expected DASH.');
++++++++ }
++++++++ case 'header_name':
++++++++ switch (ch) {
++++++++ case COLON:
++++++++ this.header = buff.toString('ascii', 0, this.pos);
++++++++ this.pos = 0;
++++++++ this.state = 'header_val';
++++++++ break;
++++++++ default:
++++++++ buff[this.pos++] = ch | 32;
++++++++ break;
++++++++ }
++++++++ break;
++++++++ case 'header_val':
++++++++ switch (ch) {
++++++++ case CR:
++++++++ this.state = 'header_val_end';
++++++++ break;
++++++++ case SPACE:
++++++++ if (this.pos === 0) {
++++++++ break;
++++++++ }
++++++++ // FALL-THROUGH
++++++++ default:
++++++++ buff[this.pos++] = ch;
++++++++ break;
++++++++ }
++++++++ break;
++++++++ case 'header_val_end':
++++++++ switch (ch) {
++++++++ case LF:
++++++++ val = buff.toString('ascii', 0, this.pos);
++++++++ this._header(this.header, val);
++++++++ this.pos = 0;
++++++++ this.state = 'header_end';
++++++++ break;
++++++++ default:
++++++++ return this._error('Expected LF.');
++++++++ }
++++++++ break;
++++++++ case 'header_end':
++++++++ switch (ch) {
++++++++ case CR:
++++++++ this.state = 'head_end';
++++++++ break;
++++++++ default:
++++++++ this.state = 'header_name';
++++++++ i--;
++++++++ break;
++++++++ }
++++++++ break;
++++++++ case 'head_end':
++++++++ switch (ch) {
++++++++ case LF:
++++++++ this.state = 'body';
++++++++ i++;
++++++++ if (i >= len) return;
++++++++ data = data.slice(i);
++++++++ i = -1;
++++++++ len = data.length;
++++++++ break;
++++++++ default:
++++++++ return this._error('Expected LF.');
++++++++ }
++++++++ break;
++++++++ case 'body':
++++++++ switch (ch) {
++++++++ case CR:
++++++++ if (i > 0) {
++++++++ this._write(data.slice(0, i));
++++++++ }
++++++++ this.pos = 1;
++++++++ this.state = 'key';
++++++++ data = data.slice(i);
++++++++ i = 0;
++++++++ len = data.length;
++++++++ break;
++++++++ default:
++++++++ // boyer-moore-like algorithm
++++++++ // at felixge's suggestion
++++++++ while ((j = i + key.length - 1) < len) {
++++++++ if (this._key[data[j]]) break;
++++++++ i = j;
++++++++ }
++++++++ break;
++++++++ }
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ if (this.state === 'body') {
++++++++ this._write(data);
++++++++ }
++++++++};
++++++++
++++++++Parser.prototype._header = function(name, val) {
++++++++ /*if (name === 'content-disposition') {
++++++++ this.field = grab(val, 'name');
++++++++ this.file = grab(val, 'filename');
++++++++
++++++++ if (this.file) {
++++++++ this.data = stream(this.file, this.options.path);
++++++++ } else {
++++++++ this.decode = new StringDecoder('utf8');
++++++++ this.data = '';
++++++++ }
++++++++ }*/
++++++++
++++++++ return this.emit('header', name, val);
++++++++};
++++++++
++++++++Parser.prototype._write = function(data) {
++++++++ /*if (this.data == null) {
++++++++ return this._error('No disposition.');
++++++++ }
++++++++
++++++++ if (this.file) {
++++++++ this.data.write(data);
++++++++ this.writtenDisk += data.length;
++++++++ } else {
++++++++ this.data += this.decode.write(data);
++++++++ this.written += data.length;
++++++++ }*/
++++++++
++++++++ this.emit('data', data);
++++++++};
++++++++
++++++++Parser.prototype._reset = function() {
++++++++ this.pos = 0;
++++++++ this.decode = null;
++++++++ this.field = null;
++++++++ this.data = null;
++++++++ this.file = null;
++++++++ this.header = null;
++++++++};
++++++++
++++++++Parser.prototype._error = function(err) {
++++++++ this.destroy();
++++++++ this.emit('error', typeof err === 'string'
++++++++ ? new Error(err)
++++++++ : err);
++++++++};
++++++++
++++++++Parser.prototype.destroy = function(err) {
++++++++ this.writable = false;
++++++++ this.readable = false;
++++++++ this._reset();
++++++++};
++++++++
++++++++Parser.prototype._finish = function() {
++++++++ var self = this
++++++++ , field = this.field
++++++++ , data = this.data
++++++++ , file = this.file
++++++++ , part;
++++++++
++++++++ this.pending++;
++++++++
++++++++ this._reset();
++++++++
++++++++ if (data && data.path) {
++++++++ part = data.path;
++++++++ data.end(next);
++++++++ } else {
++++++++ part = data;
++++++++ next();
++++++++ }
++++++++
++++++++ function next() {
++++++++ if (!self.readable) return;
++++++++
++++++++ self.pending--;
++++++++
++++++++ self.emit('part', field, part);
++++++++
++++++++ if (data && data.path) {
++++++++ self.emit('file', field, part, file);
++++++++ }
++++++++
++++++++ if (self.epilogue && !self.pending) {
++++++++ self.emit('end');
++++++++ self.destroy();
++++++++ }
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Uploads
++++++++ */
++++++++
++++++++Parser.root = process.platform === 'win32'
++++++++ ? 'C:/Temp'
++++++++ : '/tmp';
++++++++
++++++++/**
++++++++ * Middleware
++++++++ */
++++++++
++++++++Parser.middleware = function(options) {
++++++++ options = options || {};
++++++++ return function(req, res, next) {
++++++++ if (options.ensureBody) {
++++++++ req.body = {};
++++++++ }
++++++++
++++++++ if (req.method === 'GET'
++++++++ || req.method === 'HEAD'
++++++++ || req._multipart) return next();
++++++++
++++++++ req._multipart = true;
++++++++
++++++++ var type = req.headers['content-type'];
++++++++
++++++++ if (type) type = type.split(';', 1)[0].trim().toLowerCase();
++++++++
++++++++ if (type === 'multipart/form-data') {
++++++++ Parser.handle(req, res, next, options);
++++++++ } else {
++++++++ next();
++++++++ }
++++++++ };
++++++++};
++++++++
++++++++/**
++++++++ * Handler
++++++++ */
++++++++
++++++++Parser.handle = function(req, res, next, options) {
++++++++ var parser = new Parser(req.headers['content-type'], options)
++++++++ , diskLimit = options.diskLimit
++++++++ , limit = options.limit
++++++++ , parts = {}
++++++++ , files = {};
++++++++
++++++++ parser.on('error', function(err) {
++++++++ req.destroy();
++++++++ next(err);
++++++++ });
++++++++
++++++++ parser.on('part', function(field, part) {
++++++++ set(parts, field, part);
++++++++ });
++++++++
++++++++ parser.on('file', function(field, path, name) {
++++++++ set(files, field, {
++++++++ path: path,
++++++++ name: name,
++++++++ toString: function() {
++++++++ return path;
++++++++ }
++++++++ });
++++++++ });
++++++++
++++++++ parser.on('data', function() {
++++++++ if (this.writtenDisk > diskLimit || this.written > limit) {
++++++++ this.emit('error', new Error('Overflow.'));
++++++++ this.destroy();
++++++++ }
++++++++ });
++++++++
++++++++ parser.on('end', next);
++++++++
++++++++ req.body = parts;
++++++++ req.files = files;
++++++++ req.pipe(parser);
++++++++};
++++++++
++++++++/**
++++++++ * Helpers
++++++++ */
++++++++
++++++++var isWindows = process.platform === 'win32';
++++++++
++++++++var stream = function(name, dir) {
++++++++ var ext = path.extname(name) || ''
++++++++ , name = path.basename(name, ext) || ''
++++++++ , dir = dir || Parser.root
++++++++ , tag;
++++++++
++++++++ tag = Math.random().toString(36).substring(2);
++++++++
++++++++ name = name.substring(0, 200) + '.' + tag;
++++++++ name = path.join(dir, name) + ext.substring(0, 6);
++++++++ name = name.replace(/\0/g, '');
++++++++
++++++++ if (isWindows) {
++++++++ name = name.replace(/[:*<>|"?]/g, '');
++++++++ }
++++++++
++++++++ return fs.createWriteStream(name);
++++++++};
++++++++
++++++++var grab = function(str, name) {
++++++++ if (!str) return;
++++++++
++++++++ var rx = new RegExp('\\b' + name + '\\s*=\\s*("[^"]+"|\'[^\']+\'|[^;,]+)', 'i')
++++++++ , cap = rx.exec(str);
++++++++
++++++++ if (cap) {
++++++++ return cap[1].trim().replace(/^['"]|['"]$/g, '');
++++++++ }
++++++++};
++++++++
++++++++/**
++++++++ * Expose
++++++++ */
++++++++
++++++++module.exports = Parser;
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('../lib/main');
++++++++const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
++++++++
++++++++ for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
++++++++ const boundary = '-----------------------------168072824752491622650073',
++++++++ busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ }),
++++++++ buffer = createMultipartBufferForEncodingBench(boundary, 100, 'iso-8859-1'),
++++++++ mb = buffer.length / 1048576;
++++++++
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ file.resume()
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ })
++++++++
++++++++ const start = +new Date();
++++++++ busboy.write(buffer, () => { });
++++++++ busboy.end();
++++++++ const duration = +new Date - start;
++++++++ const mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++ console.log(mbPerSec + ' mb/sec');
++++++++ }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('../lib/main');
++++++++const { createMultipartBufferForEncodingBench } = require("./createMultipartBufferForEncodingBench");
++++++++
++++++++ for (var i = 0, il = 10000; i < il; i++) { // eslint-disable-line no-var
++++++++ const boundary = '-----------------------------168072824752491622650073',
++++++++ busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ }),
++++++++ buffer = createMultipartBufferForEncodingBench(boundary, 100, 'utf-8'),
++++++++ mb = buffer.length / 1048576;
++++++++
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ file.resume()
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ })
++++++++
++++++++ const start = +new Date();
++++++++ busboy.write(buffer, () => { });
++++++++ busboy.end();
++++++++ const duration = +new Date - start;
++++++++ const mbPerSec = (mb / (duration / 1000)).toFixed(2);
++++++++ console.log(mbPerSec + ' mb/sec');
++++++++ }
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const parseParams = require('../lib/utils/parseParams')
++++++++const { Bench } = require('tinybench');
++++++++const bench = new Bench();
++++++++
++++++++const simple = 'video/ogg'
++++++++const complex = "'text/plain; filename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates'"
++++++++
++++++++bench
++++++++ .add(simple, function () { parseParams(simple) })
++++++++ .add(complex, function () { parseParams(complex) })
++++++++ .run()
++++++++ .then((tasks) => {
++++++++ const errors = tasks.map(t => t.result?.error).filter((t) => t)
++++++++ if (errors.length) {
++++++++ errors.map((e) => console.error(e))
++++++++ } else {
++++++++ console.table(bench.table())
++++++++ }
++++++++ })
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "runtimeVersion": "12.22.7, V8 7.8.279.23-node.56",
++++++++ "benchmarkName": "Busboy comparison",
++++++++ "benchmarkEntryName": "busboy",
++++++++ "benchmarkCycles": 10,
++++++++ "benchmarkCycleSamples": 50,
++++++++ "warmupCycles": 10,
++++++++ "meanTimeNs": 1945927.3472222222,
++++++++ "meanTimeMs": 1.9459273472222223
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "runtimeVersion": "16.13.0, V8 9.4.146.19-node.13",
++++++++ "benchmarkName": "Busboy comparison",
++++++++ "benchmarkEntryName": "busboy",
++++++++ "benchmarkCycles": 2000,
++++++++ "benchmarkCycleSamples": 50,
++++++++ "warmupCycles": 1000,
++++++++ "meanTimeNs": 340114.0411908194,
++++++++ "meanTimeMs": 0.3401140411908194
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "runtimeVersion": "16.13.0, V8 9.4.146.19-node.13",
++++++++ "benchmarkName": "Busboy comparison",
++++++++ "benchmarkEntryName": "fastify-busboy",
++++++++ "benchmarkCycles": 2000,
++++++++ "benchmarkCycleSamples": 50,
++++++++ "warmupCycles": 1000,
++++++++ "meanTimeNs": 270984.48082281026,
++++++++ "meanTimeMs": 0.27098448082281024
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('busboy')
++++++++const { buffer, boundary } = require('../data')
++++++++
++++++++function process () {
++++++++ const busboy = Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ })
++++++++ let processedData = ''
++++++++
++++++++ return new Promise((resolve, reject) => {
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ // console.log('read file')
++++++++ file.on('data', (data) => {
++++++++ processedData += data.toString()
++++++++ // console.log(`File [${filename}] got ${data.length} bytes`);
++++++++ })
++++++++ file.on('end', (fieldname) => {
++++++++ // console.log(`File [${fieldname}] Finished`);
++++++++ })
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ reject(err)
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ resolve(processedData)
++++++++ })
++++++++ busboy.write(buffer, () => { })
++++++++
++++++++ busboy.end()
++++++++ })
++++++++}
++++++++
++++++++module.exports = {
++++++++ process
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('../../../lib/main')
++++++++const { buffer, boundary } = require('../data')
++++++++
++++++++function process () {
++++++++ const busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + boundary
++++++++ }
++++++++ })
++++++++
++++++++ let processedData = ''
++++++++
++++++++ return new Promise((resolve, reject) => {
++++++++ busboy.on('file', (field, file, filename, encoding, mimetype) => {
++++++++ // console.log('read file')
++++++++ file.on('data', (data) => {
++++++++ processedData += data.toString()
++++++++ // console.log(`File [${filename}] got ${data.length} bytes`);
++++++++ })
++++++++ file.on('end', (fieldname) => {
++++++++ // console.log(`File [${fieldname}] Finished`);
++++++++ })
++++++++ })
++++++++
++++++++ busboy.on('error', function (err) {
++++++++ reject(err)
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ resolve(processedData)
++++++++ })
++++++++ busboy.write(buffer, () => { })
++++++++
++++++++ busboy.end()
++++++++ })
++++++++}
++++++++
++++++++module.exports = {
++++++++ process
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'\r
++++++++\r
++++++++const boundary = '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k'\r
++++++++const randomContent = Buffer.from(makeString(1024 * 500), 'utf8')\r
++++++++const buffer = createMultipartBuffer(boundary)\r
++++++++\r
++++++++function makeString (length) {\r
++++++++ let result = ''\r
++++++++ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'\r
++++++++ const charactersLength = characters.length\r
++++++++ for (var i = 0; i < length; i++) { // eslint-disable-line no-var\r
++++++++ result += characters.charAt(Math.floor(Math.random() *\r
++++++++ charactersLength))\r
++++++++ }\r
++++++++ return result\r
++++++++}\r
++++++++\r
++++++++function createMultipartBuffer (boundary) {\r
++++++++ const payload = [\r
++++++++ '--' + boundary,\r
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',\r
++++++++ 'Content-Type: application/octet-stream',\r
++++++++ '',\r
++++++++ randomContent,\r
++++++++ '--' + boundary + '--'\r
++++++++ ].join('\r\n')\r
++++++++ return Buffer.from(payload, 'ascii')\r
++++++++}\r
++++++++\r
++++++++module.exports = {\r
++++++++ boundary,\r
++++++++ buffer,\r
++++++++ randomContent\r
++++++++}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { process: processBusboy } = require('./contestants/busboy')
++++++++const { process: processFastify } = require('./contestants/fastify-busboy')
++++++++const { getCommonBuilder } = require('../common/commonBuilder')
++++++++const { validateAccuracy } = require('./validator')
++++++++const { resolveContestant } = require('../common/contestantResolver')
++++++++const { outputResults } = require('../common/resultUtils')
++++++++
++++++++const contestants = {
++++++++ busboy: measureBusboy,
++++++++ fastify: measureFastify
++++++++}
++++++++
++++++++async function measureBusboy () {
++++++++ const benchmark = getCommonBuilder()
++++++++ .benchmarkName('Busboy comparison')
++++++++ .benchmarkEntryName('busboy')
++++++++ .asyncFunctionUnderTest(processBusboy)
++++++++ .build()
++++++++ const benchmarkResults = await benchmark.executeAsync()
++++++++ outputResults(benchmark, benchmarkResults)
++++++++}
++++++++
++++++++async function measureFastify () {
++++++++ const benchmark = getCommonBuilder()
++++++++ .benchmarkName('Busboy comparison')
++++++++ .benchmarkEntryName('fastify-busboy')
++++++++ .asyncFunctionUnderTest(processFastify)
++++++++ .build()
++++++++ const benchmarkResults = await benchmark.executeAsync()
++++++++ outputResults(benchmark, benchmarkResults)
++++++++}
++++++++
++++++++function execute () {
++++++++ return validateAccuracy(processBusboy())
++++++++ .then(() => {
++++++++ return validateAccuracy(processFastify())
++++++++ })
++++++++ .then(() => {
++++++++ const contestant = resolveContestant(contestants)
++++++++ return contestant()
++++++++ }).then(() => {
++++++++ console.log('all done')
++++++++ }).catch((err) => {
++++++++ console.error(`Something went wrong: ${err.message}`)
++++++++ })
++++++++}
++++++++
++++++++execute()
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++rem Make sure to run this in Admin account\r
++++++++rem\r
++++++++call npm run install-node\r
++++++++timeout /t 2\r
++++++++call nvm use 17.2.0\r
++++++++timeout /t 2\r
++++++++call npm run benchmark-all\r
++++++++call nvm use 16.13.1\r
++++++++timeout /t 2\r
++++++++call npm run benchmark-all\r
++++++++call nvm use 14.18.2\r
++++++++timeout /t 2\r
++++++++call npm run benchmark-all\r
++++++++call nvm use 12.22.7\r
++++++++timeout /t 2\r
++++++++call npm run benchmark-all\r
++++++++call npm run combine-results\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'\r
++++++++\r
++++++++const { validateEqual } = require('validation-utils')\r
++++++++const { randomContent } = require('./data')\r
++++++++\r
++++++++const EXPECTED_RESULT = randomContent.toString()\r
++++++++\r
++++++++async function validateAccuracy (actualResultPromise) {\r
++++++++ const result = await actualResultPromise\r
++++++++ validateEqual(result, EXPECTED_RESULT)\r
++++++++}\r
++++++++\r
++++++++module.exports = {\r
++++++++ validateAccuracy\r
++++++++}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { validateNotNil } = require('validation-utils')
++++++++const { BenchmarkBuilder } = require('photofinish')
++++++++const getopts = require('getopts')
++++++++
++++++++const options = getopts(process.argv.slice(1), {
++++++++ alias: {
++++++++ preset: 'p'
++++++++ },
++++++++ default: {}
++++++++})
++++++++
++++++++const PRESET = {
++++++++ LOW: (builder) => {
++++++++ return builder
++++++++ .warmupCycles(1000)
++++++++ .benchmarkCycles(1000)
++++++++ },
++++++++
++++++++ MEDIUM: (builder) => {
++++++++ return builder
++++++++ .warmupCycles(1000)
++++++++ .benchmarkCycles(2000)
++++++++ },
++++++++
++++++++ HIGH: (builder) => {
++++++++ return builder
++++++++ .warmupCycles(1000)
++++++++ .benchmarkCycles(10000)
++++++++ }
++++++++}
++++++++
++++++++function getCommonBuilder () {
++++++++ const presetId = options.preset || 'MEDIUM'
++++++++ const preset = validateNotNil(PRESET[presetId.toUpperCase()], `Unknown preset: ${presetId}`)
++++++++
++++++++ const builder = new BenchmarkBuilder()
++++++++ preset(builder)
++++++++ return builder
++++++++ .benchmarkCycleSamples(50)
++++++++}
++++++++
++++++++module.exports = {
++++++++ getCommonBuilder
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'\r
++++++++\r
++++++++const getopts = require('getopts')\r
++++++++\r
++++++++const options = getopts(process.argv.slice(1), {\r
++++++++ alias: {\r
++++++++ contestant: 'c'\r
++++++++ },\r
++++++++ default: {}\r
++++++++})\r
++++++++\r
++++++++function resolveContestant (contestants) {\r
++++++++ const contestantId = options.contestant\r
++++++++ const contestant = Number.isFinite(contestantId)\r
++++++++ ? Object.values(contestants)[contestantId]\r
++++++++ : contestants[contestantId]\r
++++++++\r
++++++++ if (!contestant) {\r
++++++++ throw new Error(`Unknown contestant ${contestantId}`)\r
++++++++ }\r
++++++++ return contestant\r
++++++++}\r
++++++++\r
++++++++module.exports = {\r
++++++++ resolveContestant\r
++++++++}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'\r
++++++++\r
++++++++const { getCommonBuilder } = require('./commonBuilder')\r
++++++++const { outputResults } = require('./resultUtils')\r
++++++++\r
++++++++function getMeasureFn (constestandId, fn) {\r
++++++++ return () => {\r
++++++++ const benchmark = getCommonBuilder()\r
++++++++ .benchmarkEntryName(constestandId)\r
++++++++ .functionUnderTest(fn).build()\r
++++++++ const benchmarkResults = benchmark.execute()\r
++++++++ outputResults(benchmark, benchmarkResults)\r
++++++++ }\r
++++++++}\r
++++++++\r
++++++++module.exports = {\r
++++++++ getMeasureFn\r
++++++++}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { exportResults } = require('photofinish')
++++++++
++++++++function outputResults (benchmark, benchmarkResults) {
++++++++ console.log(
++++++++ `Mean time for ${
++++++++ benchmark.benchmarkEntryName
++++++++ } is ${benchmarkResults.meanTime.getTimeInNanoSeconds()} nanoseconds`
++++++++ )
++++++++
++++++++ exportResults(benchmarkResults, { exportPath: '_results' })
++++++++}
++++++++
++++++++module.exports = {
++++++++ outputResults
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const fs = require('node:fs')
++++++++const path = require('node:path')
++++++++const getopts = require('getopts')
++++++++const systemInformation = require('systeminformation')
++++++++const { loadResults } = require('photofinish')
++++++++
++++++++const options = getopts(process.argv.slice(1), {
++++++++ alias: {
++++++++ resultsDir: 'r',
++++++++ precision: 'p'
++++++++ },
++++++++ default: {}
++++++++})
++++++++
++++++++const { generateTable } = require('photofinish')
++++++++
++++++++async function getSpecs () {
++++++++ const cpuInfo = await systemInformation.cpu()
++++++++
++++++++ return {
++++++++ cpu: {
++++++++ brand: cpuInfo.brand,
++++++++ speed: `${cpuInfo.speed} GHz`
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++async function saveTable () {
++++++++ const baseResultsDir = options.resultsDir
++++++++ const benchmarkResults = await loadResults(baseResultsDir)
++++++++
++++++++ const table = generateTable(benchmarkResults, {
++++++++ precision: options.precision,
++++++++ sortBy: [
++++++++ { field: 'meanTimeNs', order: 'asc' }
++++++++ ]
++++++++ })
++++++++
++++++++ const specs = await getSpecs()
++++++++
++++++++ console.log(specs)
++++++++ console.log(table)
++++++++
++++++++ const targetFilePath = path.resolve(baseResultsDir, 'results.md')
++++++++ fs.writeFileSync(
++++++++ targetFilePath,
++++++++ `${table}` +
++++++++ `\n\n**Specs**: ${specs.cpu.brand} (${specs.cpu.speed})`
++++++++ )
++++++++}
++++++++
++++++++saveTable()
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "busboy-benchmarks",
++++++++ "version": "1.0.0",
++++++++ "license": "MIT",
++++++++ "dependencies": {
++++++++ "getopts": "^2.3.0",
++++++++ "photofinish": "^1.8.0",
++++++++ "systeminformation": "^5.9.15",
++++++++ "tslib": "^2.3.1",
++++++++ "validation-utils": "^7.0.0"
++++++++ },
++++++++ "scripts": {
++++++++ "install-node": "nvm install 17.2.0 && nvm install 16.13.1 && nvm install 14.18.2 && nvm install 12.22.7",
++++++++ "benchmark-busboy": "node busboy/executioner.js -c 0",
++++++++ "benchmark-fastify": "node busboy/executioner.js -c 1",
++++++++ "benchmark-all": "npm run benchmark-busboy -- -p high && npm run benchmark-fastify -- -p high",
++++++++ "benchmark-all-medium": "npm run benchmark-busboy -- -p medium && npm run benchmark-fastify -- -p medium",
++++++++ "benchmark-all-low": "npm run benchmark-busboy -- -p low && npm run benchmark-fastify -- -p low",
++++++++ "combine-results": "node common/resultsCombinator.js -r _results -p 6"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Copyright Brian White. All rights reserved.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++of this software and associated documentation files (the "Software"), to
++++++++deal in the Software without restriction, including without limitation the
++++++++rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++++++++sell copies of the Software, and to permit persons to whom the Software is
++++++++furnished to do so, subject to the following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included in
++++++++all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++++++++FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++++++++IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const WritableStream = require('node:stream').Writable
++++++++const inherits = require('node:util').inherits
++++++++
++++++++const StreamSearch = require('../../streamsearch/sbmh')
++++++++
++++++++const PartStream = require('./PartStream')
++++++++const HeaderParser = require('./HeaderParser')
++++++++
++++++++const DASH = 45
++++++++const B_ONEDASH = Buffer.from('-')
++++++++const B_CRLF = Buffer.from('\r\n')
++++++++const EMPTY_FN = function () {}
++++++++
++++++++function Dicer (cfg) {
++++++++ if (!(this instanceof Dicer)) { return new Dicer(cfg) }
++++++++ WritableStream.call(this, cfg)
++++++++
++++++++ if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string')) { throw new TypeError('Boundary required') }
++++++++
++++++++ if (typeof cfg.boundary === 'string') { this.setBoundary(cfg.boundary) } else { this._bparser = undefined }
++++++++
++++++++ this._headerFirst = cfg.headerFirst
++++++++
++++++++ this._dashes = 0
++++++++ this._parts = 0
++++++++ this._finished = false
++++++++ this._realFinish = false
++++++++ this._isPreamble = true
++++++++ this._justMatched = false
++++++++ this._firstWrite = true
++++++++ this._inHeader = true
++++++++ this._part = undefined
++++++++ this._cb = undefined
++++++++ this._ignoreData = false
++++++++ this._partOpts = { highWaterMark: cfg.partHwm }
++++++++ this._pause = false
++++++++
++++++++ const self = this
++++++++ this._hparser = new HeaderParser(cfg)
++++++++ this._hparser.on('header', function (header) {
++++++++ self._inHeader = false
++++++++ self._part.emit('header', header)
++++++++ })
++++++++}
++++++++inherits(Dicer, WritableStream)
++++++++
++++++++Dicer.prototype.emit = function (ev) {
++++++++ if (ev === 'finish' && !this._realFinish) {
++++++++ if (!this._finished) {
++++++++ const self = this
++++++++ process.nextTick(function () {
++++++++ self.emit('error', new Error('Unexpected end of multipart data'))
++++++++ if (self._part && !self._ignoreData) {
++++++++ const type = (self._isPreamble ? 'Preamble' : 'Part')
++++++++ self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'))
++++++++ self._part.push(null)
++++++++ process.nextTick(function () {
++++++++ self._realFinish = true
++++++++ self.emit('finish')
++++++++ self._realFinish = false
++++++++ })
++++++++ return
++++++++ }
++++++++ self._realFinish = true
++++++++ self.emit('finish')
++++++++ self._realFinish = false
++++++++ })
++++++++ }
++++++++ } else { WritableStream.prototype.emit.apply(this, arguments) }
++++++++}
++++++++
++++++++Dicer.prototype._write = function (data, encoding, cb) {
++++++++ // ignore unexpected data (e.g. extra trailer data after finished)
++++++++ if (!this._hparser && !this._bparser) { return cb() }
++++++++
++++++++ if (this._headerFirst && this._isPreamble) {
++++++++ if (!this._part) {
++++++++ this._part = new PartStream(this._partOpts)
++++++++ if (this.listenerCount('preamble') !== 0) { this.emit('preamble', this._part) } else { this._ignore() }
++++++++ }
++++++++ const r = this._hparser.push(data)
++++++++ if (!this._inHeader && r !== undefined && r < data.length) { data = data.slice(r) } else { return cb() }
++++++++ }
++++++++
++++++++ // allows for "easier" testing
++++++++ if (this._firstWrite) {
++++++++ this._bparser.push(B_CRLF)
++++++++ this._firstWrite = false
++++++++ }
++++++++
++++++++ this._bparser.push(data)
++++++++
++++++++ if (this._pause) { this._cb = cb } else { cb() }
++++++++}
++++++++
++++++++Dicer.prototype.reset = function () {
++++++++ this._part = undefined
++++++++ this._bparser = undefined
++++++++ this._hparser = undefined
++++++++}
++++++++
++++++++Dicer.prototype.setBoundary = function (boundary) {
++++++++ const self = this
++++++++ this._bparser = new StreamSearch('\r\n--' + boundary)
++++++++ this._bparser.on('info', function (isMatch, data, start, end) {
++++++++ self._oninfo(isMatch, data, start, end)
++++++++ })
++++++++}
++++++++
++++++++Dicer.prototype._ignore = function () {
++++++++ if (this._part && !this._ignoreData) {
++++++++ this._ignoreData = true
++++++++ this._part.on('error', EMPTY_FN)
++++++++ // we must perform some kind of read on the stream even though we are
++++++++ // ignoring the data, otherwise node's Readable stream will not emit 'end'
++++++++ // after pushing null to the stream
++++++++ this._part.resume()
++++++++ }
++++++++}
++++++++
++++++++Dicer.prototype._oninfo = function (isMatch, data, start, end) {
++++++++ let buf; const self = this; let i = 0; let r; let shouldWriteMore = true
++++++++
++++++++ if (!this._part && this._justMatched && data) {
++++++++ while (this._dashes < 2 && (start + i) < end) {
++++++++ if (data[start + i] === DASH) {
++++++++ ++i
++++++++ ++this._dashes
++++++++ } else {
++++++++ if (this._dashes) { buf = B_ONEDASH }
++++++++ this._dashes = 0
++++++++ break
++++++++ }
++++++++ }
++++++++ if (this._dashes === 2) {
++++++++ if ((start + i) < end && this.listenerCount('trailer') !== 0) { this.emit('trailer', data.slice(start + i, end)) }
++++++++ this.reset()
++++++++ this._finished = true
++++++++ // no more parts will be added
++++++++ if (self._parts === 0) {
++++++++ self._realFinish = true
++++++++ self.emit('finish')
++++++++ self._realFinish = false
++++++++ }
++++++++ }
++++++++ if (this._dashes) { return }
++++++++ }
++++++++ if (this._justMatched) { this._justMatched = false }
++++++++ if (!this._part) {
++++++++ this._part = new PartStream(this._partOpts)
++++++++ this._part._read = function (n) {
++++++++ self._unpause()
++++++++ }
++++++++ if (this._isPreamble && this.listenerCount('preamble') !== 0) {
++++++++ this.emit('preamble', this._part)
++++++++ } else if (this._isPreamble !== true && this.listenerCount('part') !== 0) {
++++++++ this.emit('part', this._part)
++++++++ } else {
++++++++ this._ignore()
++++++++ }
++++++++ if (!this._isPreamble) { this._inHeader = true }
++++++++ }
++++++++ if (data && start < end && !this._ignoreData) {
++++++++ if (this._isPreamble || !this._inHeader) {
++++++++ if (buf) { shouldWriteMore = this._part.push(buf) }
++++++++ shouldWriteMore = this._part.push(data.slice(start, end))
++++++++ if (!shouldWriteMore) { this._pause = true }
++++++++ } else if (!this._isPreamble && this._inHeader) {
++++++++ if (buf) { this._hparser.push(buf) }
++++++++ r = this._hparser.push(data.slice(start, end))
++++++++ if (!this._inHeader && r !== undefined && r < end) { this._oninfo(false, data, start + r, end) }
++++++++ }
++++++++ }
++++++++ if (isMatch) {
++++++++ this._hparser.reset()
++++++++ if (this._isPreamble) { this._isPreamble = false } else {
++++++++ if (start !== end) {
++++++++ ++this._parts
++++++++ this._part.on('end', function () {
++++++++ if (--self._parts === 0) {
++++++++ if (self._finished) {
++++++++ self._realFinish = true
++++++++ self.emit('finish')
++++++++ self._realFinish = false
++++++++ } else {
++++++++ self._unpause()
++++++++ }
++++++++ }
++++++++ })
++++++++ }
++++++++ }
++++++++ this._part.push(null)
++++++++ this._part = undefined
++++++++ this._ignoreData = false
++++++++ this._justMatched = true
++++++++ this._dashes = 0
++++++++ }
++++++++}
++++++++
++++++++Dicer.prototype._unpause = function () {
++++++++ if (!this._pause) { return }
++++++++
++++++++ this._pause = false
++++++++ if (this._cb) {
++++++++ const cb = this._cb
++++++++ this._cb = undefined
++++++++ cb()
++++++++ }
++++++++}
++++++++
++++++++module.exports = Dicer
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const EventEmitter = require('node:events').EventEmitter
++++++++const inherits = require('node:util').inherits
++++++++const getLimit = require('../../../lib/utils/getLimit')
++++++++
++++++++const StreamSearch = require('../../streamsearch/sbmh')
++++++++
++++++++const B_DCRLF = Buffer.from('\r\n\r\n')
++++++++const RE_CRLF = /\r\n/g
++++++++const RE_HDR = /^([^:]+):[ \t]?([\x00-\xFF]+)?$/ // eslint-disable-line no-control-regex
++++++++
++++++++function HeaderParser (cfg) {
++++++++ EventEmitter.call(this)
++++++++
++++++++ cfg = cfg || {}
++++++++ const self = this
++++++++ this.nread = 0
++++++++ this.maxed = false
++++++++ this.npairs = 0
++++++++ this.maxHeaderPairs = getLimit(cfg, 'maxHeaderPairs', 2000)
++++++++ this.maxHeaderSize = getLimit(cfg, 'maxHeaderSize', 80 * 1024)
++++++++ this.buffer = ''
++++++++ this.header = {}
++++++++ this.finished = false
++++++++ this.ss = new StreamSearch(B_DCRLF)
++++++++ this.ss.on('info', function (isMatch, data, start, end) {
++++++++ if (data && !self.maxed) {
++++++++ if (self.nread + end - start >= self.maxHeaderSize) {
++++++++ end = self.maxHeaderSize - self.nread + start
++++++++ self.nread = self.maxHeaderSize
++++++++ self.maxed = true
++++++++ } else { self.nread += (end - start) }
++++++++
++++++++ self.buffer += data.toString('binary', start, end)
++++++++ }
++++++++ if (isMatch) { self._finish() }
++++++++ })
++++++++}
++++++++inherits(HeaderParser, EventEmitter)
++++++++
++++++++HeaderParser.prototype.push = function (data) {
++++++++ const r = this.ss.push(data)
++++++++ if (this.finished) { return r }
++++++++}
++++++++
++++++++HeaderParser.prototype.reset = function () {
++++++++ this.finished = false
++++++++ this.buffer = ''
++++++++ this.header = {}
++++++++ this.ss.reset()
++++++++}
++++++++
++++++++HeaderParser.prototype._finish = function () {
++++++++ if (this.buffer) { this._parseHeader() }
++++++++ this.ss.matches = this.ss.maxMatches
++++++++ const header = this.header
++++++++ this.header = {}
++++++++ this.buffer = ''
++++++++ this.finished = true
++++++++ this.nread = this.npairs = 0
++++++++ this.maxed = false
++++++++ this.emit('header', header)
++++++++}
++++++++
++++++++HeaderParser.prototype._parseHeader = function () {
++++++++ if (this.npairs === this.maxHeaderPairs) { return }
++++++++
++++++++ const lines = this.buffer.split(RE_CRLF)
++++++++ const len = lines.length
++++++++ let m, h
++++++++
++++++++ for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
++++++++ if (lines[i].length === 0) { continue }
++++++++ if (lines[i][0] === '\t' || lines[i][0] === ' ') {
++++++++ // folded header content
++++++++ // RFC2822 says to just remove the CRLF and not the whitespace following
++++++++ // it, so we follow the RFC and include the leading whitespace ...
++++++++ if (h) {
++++++++ this.header[h][this.header[h].length - 1] += lines[i]
++++++++ continue
++++++++ }
++++++++ }
++++++++
++++++++ const posColon = lines[i].indexOf(':')
++++++++ if (
++++++++ posColon === -1 ||
++++++++ posColon === 0
++++++++ ) {
++++++++ return
++++++++ }
++++++++ m = RE_HDR.exec(lines[i])
++++++++ h = m[1].toLowerCase()
++++++++ this.header[h] = this.header[h] || []
++++++++ this.header[h].push((m[2] || ''))
++++++++ if (++this.npairs === this.maxHeaderPairs) { break }
++++++++ }
++++++++}
++++++++
++++++++module.exports = HeaderParser
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const inherits = require('node:util').inherits
++++++++const ReadableStream = require('node:stream').Readable
++++++++
++++++++function PartStream (opts) {
++++++++ ReadableStream.call(this, opts)
++++++++}
++++++++inherits(PartStream, ReadableStream)
++++++++
++++++++PartStream.prototype._read = function (n) {}
++++++++
++++++++module.exports = PartStream
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++// Type definitions for dicer 0.2
++++++++// Project: https://github.com/mscdex/dicer
++++++++// Definitions by: BendingBender <https://github.com/BendingBender>
++++++++// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
++++++++// TypeScript Version: 2.2
++++++++/// <reference types="node" />
++++++++
++++++++import stream = require("stream");
++++++++
++++++++// tslint:disable:unified-signatures
++++++++
++++++++/**
++++++++ * A very fast streaming multipart parser for node.js.
++++++++ * Dicer is a WritableStream
++++++++ *
++++++++ * Dicer (special) events:
++++++++ * - on('finish', ()) - Emitted when all parts have been parsed and the Dicer instance has been ended.
++++++++ * - on('part', (stream: PartStream)) - Emitted when a new part has been found.
++++++++ * - on('preamble', (stream: PartStream)) - Emitted for preamble if you should happen to need it (can usually be ignored).
++++++++ * - on('trailer', (data: Buffer)) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too).
++++++++ */
++++++++export class Dicer extends stream.Writable {
++++++++ /**
++++++++ * Creates and returns a new Dicer instance with the following valid config settings:
++++++++ *
++++++++ * @param config The configuration to use
++++++++ */
++++++++ constructor(config: Dicer.Config);
++++++++ /**
++++++++ * Sets the boundary to use for parsing and performs some initialization needed for parsing.
++++++++ * You should only need to use this if you set headerFirst to true in the constructor and are parsing the boundary from the preamble header.
++++++++ *
++++++++ * @param boundary The boundary to use
++++++++ */
++++++++ setBoundary(boundary: string): void;
++++++++ addListener(event: "finish", listener: () => void): this;
++++++++ addListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ addListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ addListener(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ addListener(event: "close", listener: () => void): this;
++++++++ addListener(event: "drain", listener: () => void): this;
++++++++ addListener(event: "error", listener: (err: Error) => void): this;
++++++++ addListener(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ addListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ addListener(event: string, listener: (...args: any[]) => void): this;
++++++++ on(event: "finish", listener: () => void): this;
++++++++ on(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ on(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ on(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ on(event: "close", listener: () => void): this;
++++++++ on(event: "drain", listener: () => void): this;
++++++++ on(event: "error", listener: (err: Error) => void): this;
++++++++ on(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ on(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ on(event: string, listener: (...args: any[]) => void): this;
++++++++ once(event: "finish", listener: () => void): this;
++++++++ once(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ once(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ once(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ once(event: "close", listener: () => void): this;
++++++++ once(event: "drain", listener: () => void): this;
++++++++ once(event: "error", listener: (err: Error) => void): this;
++++++++ once(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ once(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ once(event: string, listener: (...args: any[]) => void): this;
++++++++ prependListener(event: "finish", listener: () => void): this;
++++++++ prependListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ prependListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ prependListener(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ prependListener(event: "close", listener: () => void): this;
++++++++ prependListener(event: "drain", listener: () => void): this;
++++++++ prependListener(event: "error", listener: (err: Error) => void): this;
++++++++ prependListener(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ prependListener(event: string, listener: (...args: any[]) => void): this;
++++++++ prependOnceListener(event: "finish", listener: () => void): this;
++++++++ prependOnceListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ prependOnceListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ prependOnceListener(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ prependOnceListener(event: "close", listener: () => void): this;
++++++++ prependOnceListener(event: "drain", listener: () => void): this;
++++++++ prependOnceListener(event: "error", listener: (err: Error) => void): this;
++++++++ prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ prependOnceListener(event: string, listener: (...args: any[]) => void): this;
++++++++ removeListener(event: "finish", listener: () => void): this;
++++++++ removeListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
++++++++ removeListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
++++++++ removeListener(event: "trailer", listener: (data: Buffer) => void): this;
++++++++ removeListener(event: "close", listener: () => void): this;
++++++++ removeListener(event: "drain", listener: () => void): this;
++++++++ removeListener(event: "error", listener: (err: Error) => void): this;
++++++++ removeListener(event: "pipe", listener: (src: stream.Readable) => void): this;
++++++++ removeListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
++++++++ removeListener(event: string, listener: (...args: any[]) => void): this;
++++++++}
++++++++
++++++++declare namespace Dicer {
++++++++ interface Config {
++++++++ /**
++++++++ * This is the boundary used to detect the beginning of a new part.
++++++++ */
++++++++ boundary?: string | undefined;
++++++++ /**
++++++++ * If true, preamble header parsing will be performed first.
++++++++ */
++++++++ headerFirst?: boolean | undefined;
++++++++ /**
++++++++ * The maximum number of header key=>value pairs to parse Default: 2000 (same as node's http).
++++++++ */
++++++++ maxHeaderPairs?: number | undefined;
++++++++ }
++++++++
++++++++ /**
++++++++ * PartStream is a _ReadableStream_
++++++++ *
++++++++ * PartStream (special) events:
++++++++ * - on('header', (header: object)) - An object containing the header for this particular part. Each property value is an array of one or more string values.
++++++++ */
++++++++ interface PartStream extends stream.Readable {
++++++++ addListener(event: "header", listener: (header: object) => void): this;
++++++++ addListener(event: "close", listener: () => void): this;
++++++++ addListener(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ addListener(event: "end", listener: () => void): this;
++++++++ addListener(event: "readable", listener: () => void): this;
++++++++ addListener(event: "error", listener: (err: Error) => void): this;
++++++++ addListener(event: string, listener: (...args: any[]) => void): this;
++++++++ on(event: "header", listener: (header: object) => void): this;
++++++++ on(event: "close", listener: () => void): this;
++++++++ on(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ on(event: "end", listener: () => void): this;
++++++++ on(event: "readable", listener: () => void): this;
++++++++ on(event: "error", listener: (err: Error) => void): this;
++++++++ on(event: string, listener: (...args: any[]) => void): this;
++++++++ once(event: "header", listener: (header: object) => void): this;
++++++++ once(event: "close", listener: () => void): this;
++++++++ once(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ once(event: "end", listener: () => void): this;
++++++++ once(event: "readable", listener: () => void): this;
++++++++ once(event: "error", listener: (err: Error) => void): this;
++++++++ once(event: string, listener: (...args: any[]) => void): this;
++++++++ prependListener(event: "header", listener: (header: object) => void): this;
++++++++ prependListener(event: "close", listener: () => void): this;
++++++++ prependListener(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ prependListener(event: "end", listener: () => void): this;
++++++++ prependListener(event: "readable", listener: () => void): this;
++++++++ prependListener(event: "error", listener: (err: Error) => void): this;
++++++++ prependListener(event: string, listener: (...args: any[]) => void): this;
++++++++ prependOnceListener(event: "header", listener: (header: object) => void): this;
++++++++ prependOnceListener(event: "close", listener: () => void): this;
++++++++ prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ prependOnceListener(event: "end", listener: () => void): this;
++++++++ prependOnceListener(event: "readable", listener: () => void): this;
++++++++ prependOnceListener(event: "error", listener: (err: Error) => void): this;
++++++++ prependOnceListener(event: string, listener: (...args: any[]) => void): this;
++++++++ removeListener(event: "header", listener: (header: object) => void): this;
++++++++ removeListener(event: "close", listener: () => void): this;
++++++++ removeListener(event: "data", listener: (chunk: Buffer | string) => void): this;
++++++++ removeListener(event: "end", listener: () => void): this;
++++++++ removeListener(event: "readable", listener: () => void): this;
++++++++ removeListener(event: "error", listener: (err: Error) => void): this;
++++++++ removeListener(event: string, listener: (...args: any[]) => void): this;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++/**
++++++++ * Copyright Brian White. All rights reserved.
++++++++ *
++++++++ * @see https://github.com/mscdex/streamsearch
++++++++ *
++++++++ * Permission is hereby granted, free of charge, to any person obtaining a copy
++++++++ * of this software and associated documentation files (the "Software"), to
++++++++ * deal in the Software without restriction, including without limitation the
++++++++ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
++++++++ * sell copies of the Software, and to permit persons to whom the Software is
++++++++ * furnished to do so, subject to the following conditions:
++++++++ *
++++++++ * The above copyright notice and this permission notice shall be included in
++++++++ * all copies or substantial portions of the Software.
++++++++ *
++++++++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
++++++++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
++++++++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
++++++++ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
++++++++ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
++++++++ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
++++++++ * IN THE SOFTWARE.
++++++++ *
++++++++ * Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
++++++++ * by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
++++++++ */
++++++++const EventEmitter = require('node:events').EventEmitter
++++++++const inherits = require('node:util').inherits
++++++++
++++++++function SBMH (needle) {
++++++++ if (typeof needle === 'string') {
++++++++ needle = Buffer.from(needle)
++++++++ }
++++++++
++++++++ if (!Buffer.isBuffer(needle)) {
++++++++ throw new TypeError('The needle has to be a String or a Buffer.')
++++++++ }
++++++++
++++++++ const needleLength = needle.length
++++++++
++++++++ if (needleLength === 0) {
++++++++ throw new Error('The needle cannot be an empty String/Buffer.')
++++++++ }
++++++++
++++++++ if (needleLength > 256) {
++++++++ throw new Error('The needle cannot have a length bigger than 256.')
++++++++ }
++++++++
++++++++ this.maxMatches = Infinity
++++++++ this.matches = 0
++++++++
++++++++ this._occ = new Array(256)
++++++++ .fill(needleLength) // Initialize occurrence table.
++++++++ this._lookbehind_size = 0
++++++++ this._needle = needle
++++++++ this._bufpos = 0
++++++++
++++++++ this._lookbehind = Buffer.alloc(needleLength)
++++++++
++++++++ // Populate occurrence table with analysis of the needle,
++++++++ // ignoring last letter.
++++++++ for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
++++++++ this._occ[needle[i]] = needleLength - 1 - i
++++++++ }
++++++++}
++++++++inherits(SBMH, EventEmitter)
++++++++
++++++++SBMH.prototype.reset = function () {
++++++++ this._lookbehind_size = 0
++++++++ this.matches = 0
++++++++ this._bufpos = 0
++++++++}
++++++++
++++++++SBMH.prototype.push = function (chunk, pos) {
++++++++ if (!Buffer.isBuffer(chunk)) {
++++++++ chunk = Buffer.from(chunk, 'binary')
++++++++ }
++++++++ const chlen = chunk.length
++++++++ this._bufpos = pos || 0
++++++++ let r
++++++++ while (r !== chlen && this.matches < this.maxMatches) { r = this._sbmh_feed(chunk) }
++++++++ return r
++++++++}
++++++++
++++++++SBMH.prototype._sbmh_feed = function (data) {
++++++++ const len = data.length
++++++++ const needle = this._needle
++++++++ const needleLength = needle.length
++++++++ const lastNeedleChar = needle[needleLength - 1]
++++++++
++++++++ // Positive: points to a position in `data`
++++++++ // pos == 3 points to data[3]
++++++++ // Negative: points to a position in the lookbehind buffer
++++++++ // pos == -2 points to lookbehind[lookbehind_size - 2]
++++++++ let pos = -this._lookbehind_size
++++++++ let ch
++++++++
++++++++ if (pos < 0) {
++++++++ // Lookbehind buffer is not empty. Perform Boyer-Moore-Horspool
++++++++ // search with character lookup code that considers both the
++++++++ // lookbehind buffer and the current round's haystack data.
++++++++ //
++++++++ // Loop until
++++++++ // there is a match.
++++++++ // or until
++++++++ // we've moved past the position that requires the
++++++++ // lookbehind buffer. In this case we switch to the
++++++++ // optimized loop.
++++++++ // or until
++++++++ // the character to look at lies outside the haystack.
++++++++ while (pos < 0 && pos <= len - needleLength) {
++++++++ ch = this._sbmh_lookup_char(data, pos + needleLength - 1)
++++++++
++++++++ if (
++++++++ ch === lastNeedleChar &&
++++++++ this._sbmh_memcmp(data, pos, needleLength - 1)
++++++++ ) {
++++++++ this._lookbehind_size = 0
++++++++ ++this.matches
++++++++ this.emit('info', true)
++++++++
++++++++ return (this._bufpos = pos + needleLength)
++++++++ }
++++++++ pos += this._occ[ch]
++++++++ }
++++++++
++++++++ // No match.
++++++++
++++++++ if (pos < 0) {
++++++++ // There's too few data for Boyer-Moore-Horspool to run,
++++++++ // so let's use a different algorithm to skip as much as
++++++++ // we can.
++++++++ // Forward pos until
++++++++ // the trailing part of lookbehind + data
++++++++ // looks like the beginning of the needle
++++++++ // or until
++++++++ // pos == 0
++++++++ while (pos < 0 && !this._sbmh_memcmp(data, pos, len - pos)) { ++pos }
++++++++ }
++++++++
++++++++ if (pos >= 0) {
++++++++ // Discard lookbehind buffer.
++++++++ this.emit('info', false, this._lookbehind, 0, this._lookbehind_size)
++++++++ this._lookbehind_size = 0
++++++++ } else {
++++++++ // Cut off part of the lookbehind buffer that has
++++++++ // been processed and append the entire haystack
++++++++ // into it.
++++++++ const bytesToCutOff = this._lookbehind_size + pos
++++++++ if (bytesToCutOff > 0) {
++++++++ // The cut off data is guaranteed not to contain the needle.
++++++++ this.emit('info', false, this._lookbehind, 0, bytesToCutOff)
++++++++ }
++++++++
++++++++ this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff,
++++++++ this._lookbehind_size - bytesToCutOff)
++++++++ this._lookbehind_size -= bytesToCutOff
++++++++
++++++++ data.copy(this._lookbehind, this._lookbehind_size)
++++++++ this._lookbehind_size += len
++++++++
++++++++ this._bufpos = len
++++++++ return len
++++++++ }
++++++++ }
++++++++
++++++++ pos += (pos >= 0) * this._bufpos
++++++++
++++++++ // Lookbehind buffer is now empty. We only need to check if the
++++++++ // needle is in the haystack.
++++++++ if (data.indexOf(needle, pos) !== -1) {
++++++++ pos = data.indexOf(needle, pos)
++++++++ ++this.matches
++++++++ if (pos > 0) { this.emit('info', true, data, this._bufpos, pos) } else { this.emit('info', true) }
++++++++
++++++++ return (this._bufpos = pos + needleLength)
++++++++ } else {
++++++++ pos = len - needleLength
++++++++ }
++++++++
++++++++ // There was no match. If there's trailing haystack data that we cannot
++++++++ // match yet using the Boyer-Moore-Horspool algorithm (because the trailing
++++++++ // data is less than the needle size) then match using a modified
++++++++ // algorithm that starts matching from the beginning instead of the end.
++++++++ // Whatever trailing data is left after running this algorithm is added to
++++++++ // the lookbehind buffer.
++++++++ while (
++++++++ pos < len &&
++++++++ (
++++++++ data[pos] !== needle[0] ||
++++++++ (
++++++++ (Buffer.compare(
++++++++ data.subarray(pos, pos + len - pos),
++++++++ needle.subarray(0, len - pos)
++++++++ ) !== 0)
++++++++ )
++++++++ )
++++++++ ) {
++++++++ ++pos
++++++++ }
++++++++ if (pos < len) {
++++++++ data.copy(this._lookbehind, 0, pos, pos + (len - pos))
++++++++ this._lookbehind_size = len - pos
++++++++ }
++++++++
++++++++ // Everything until pos is guaranteed not to contain needle data.
++++++++ if (pos > 0) { this.emit('info', false, data, this._bufpos, pos < len ? pos : len) }
++++++++
++++++++ this._bufpos = len
++++++++ return len
++++++++}
++++++++
++++++++SBMH.prototype._sbmh_lookup_char = function (data, pos) {
++++++++ return (pos < 0)
++++++++ ? this._lookbehind[this._lookbehind_size + pos]
++++++++ : data[pos]
++++++++}
++++++++
++++++++SBMH.prototype._sbmh_memcmp = function (data, pos, len) {
++++++++ for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
++++++++ if (this._sbmh_lookup_char(data, pos + i) !== this._needle[i]) { return false }
++++++++ }
++++++++ return true
++++++++}
++++++++
++++++++module.exports = SBMH
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++// Definitions by: Jacob Baskin <https://github.com/jacobbaskin>
++++++++// BendingBender <https://github.com/BendingBender>
++++++++// Igor Savin <https://github.com/kibertoad>
++++++++
++++++++/// <reference types="node" />
++++++++
++++++++import * as http from 'http';
++++++++import { Readable, Writable } from 'stream';
++++++++export { Dicer } from "../deps/dicer/lib/dicer";
++++++++
++++++++export const Busboy: BusboyConstructor;
++++++++export default Busboy;
++++++++
++++++++export interface BusboyConfig {
++++++++ /**
++++++++ * These are the HTTP headers of the incoming request, which are used by individual parsers.
++++++++ */
++++++++ headers: BusboyHeaders;
++++++++ /**
++++++++ * `highWaterMark` to use for this Busboy instance.
++++++++ * @default WritableStream default.
++++++++ */
++++++++ highWaterMark?: number | undefined;
++++++++ /**
++++++++ * highWaterMark to use for file streams.
++++++++ * @default ReadableStream default.
++++++++ */
++++++++ fileHwm?: number | undefined;
++++++++ /**
++++++++ * Default character set to use when one isn't defined.
++++++++ * @default 'utf8'
++++++++ */
++++++++ defCharset?: string | undefined;
++++++++ /**
++++++++ * Detect if a Part is a file.
++++++++ *
++++++++ * By default a file is detected if contentType
++++++++ * is application/octet-stream or fileName is not
++++++++ * undefined.
++++++++ *
++++++++ * Modify this to handle e.g. Blobs.
++++++++ */
++++++++ isPartAFile?: (fieldName: string | undefined, contentType: string | undefined, fileName: string | undefined) => boolean;
++++++++ /**
++++++++ * If paths in the multipart 'filename' field shall be preserved.
++++++++ * @default false
++++++++ */
++++++++ preservePath?: boolean | undefined;
++++++++ /**
++++++++ * Various limits on incoming data.
++++++++ */
++++++++ limits?:
++++++++ | {
++++++++ /**
++++++++ * Max field name size (in bytes)
++++++++ * @default 100 bytes
++++++++ */
++++++++ fieldNameSize?: number | undefined;
++++++++ /**
++++++++ * Max field value size (in bytes)
++++++++ * @default 1MB
++++++++ */
++++++++ fieldSize?: number | undefined;
++++++++ /**
++++++++ * Max number of non-file fields
++++++++ * @default Infinity
++++++++ */
++++++++ fields?: number | undefined;
++++++++ /**
++++++++ * For multipart forms, the max file size (in bytes)
++++++++ * @default Infinity
++++++++ */
++++++++ fileSize?: number | undefined;
++++++++ /**
++++++++ * For multipart forms, the max number of file fields
++++++++ * @default Infinity
++++++++ */
++++++++ files?: number | undefined;
++++++++ /**
++++++++ * For multipart forms, the max number of parts (fields + files)
++++++++ * @default Infinity
++++++++ */
++++++++ parts?: number | undefined;
++++++++ /**
++++++++ * For multipart forms, the max number of header key=>value pairs to parse
++++++++ * @default 2000
++++++++ */
++++++++ headerPairs?: number | undefined;
++++++++
++++++++ /**
++++++++ * For multipart forms, the max size of a header part
++++++++ * @default 81920
++++++++ */
++++++++ headerSize?: number | undefined;
++++++++ }
++++++++ | undefined;
++++++++}
++++++++
++++++++export type BusboyHeaders = { 'content-type': string } & http.IncomingHttpHeaders;
++++++++
++++++++export interface BusboyFileStream extends
++++++++ Readable {
++++++++
++++++++ truncated: boolean;
++++++++
++++++++ /**
++++++++ * The number of bytes that have been read so far.
++++++++ */
++++++++ bytesRead: number;
++++++++}
++++++++
++++++++export interface Busboy extends Writable {
++++++++ addListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ addListener(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ on<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ on(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ once<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ once(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ removeListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ off<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ off(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ prependListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++
++++++++ prependOnceListener<Event extends keyof BusboyEvents>(event: Event, listener: BusboyEvents[Event]): this;
++++++++
++++++++ prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
++++++++}
++++++++
++++++++export interface BusboyEvents {
++++++++ /**
++++++++ * Emitted for each new file form field found.
++++++++ *
++++++++ * * Note: if you listen for this event, you should always handle the `stream` no matter if you care about the
++++++++ * file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents),
++++++++ * otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any**
++++++++ * incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically
++++++++ * and safely discarded (these discarded files do still count towards `files` and `parts` limits).
++++++++ * * If a configured file size limit was reached, `stream` will both have a boolean property `truncated`
++++++++ * (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
++++++++ *
++++++++ * @param listener.transferEncoding Contains the 'Content-Transfer-Encoding' value for the file stream.
++++++++ * @param listener.mimeType Contains the 'Content-Type' value for the file stream.
++++++++ */
++++++++ file: (
++++++++ fieldname: string,
++++++++ stream: BusboyFileStream,
++++++++ filename: string,
++++++++ transferEncoding: string,
++++++++ mimeType: string,
++++++++ ) => void;
++++++++ /**
++++++++ * Emitted for each new non-file field found.
++++++++ */
++++++++ field: (
++++++++ fieldname: string,
++++++++ value: string,
++++++++ fieldnameTruncated: boolean,
++++++++ valueTruncated: boolean,
++++++++ transferEncoding: string,
++++++++ mimeType: string,
++++++++ ) => void;
++++++++ finish: () => void;
++++++++ /**
++++++++ * Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
++++++++ */
++++++++ partsLimit: () => void;
++++++++ /**
++++++++ * Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
++++++++ */
++++++++ filesLimit: () => void;
++++++++ /**
++++++++ * Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
++++++++ */
++++++++ fieldsLimit: () => void;
++++++++ error: (error: unknown) => void;
++++++++}
++++++++
++++++++export interface BusboyConstructor {
++++++++ (options: BusboyConfig): Busboy;
++++++++
++++++++ new(options: BusboyConfig): Busboy;
++++++++}
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const WritableStream = require('node:stream').Writable
++++++++const { inherits } = require('node:util')
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++
++++++++const MultipartParser = require('./types/multipart')
++++++++const UrlencodedParser = require('./types/urlencoded')
++++++++const parseParams = require('./utils/parseParams')
++++++++
++++++++function Busboy (opts) {
++++++++ if (!(this instanceof Busboy)) { return new Busboy(opts) }
++++++++
++++++++ if (typeof opts !== 'object') {
++++++++ throw new TypeError('Busboy expected an options-Object.')
++++++++ }
++++++++ if (typeof opts.headers !== 'object') {
++++++++ throw new TypeError('Busboy expected an options-Object with headers-attribute.')
++++++++ }
++++++++ if (typeof opts.headers['content-type'] !== 'string') {
++++++++ throw new TypeError('Missing Content-Type-header.')
++++++++ }
++++++++
++++++++ const {
++++++++ headers,
++++++++ ...streamOptions
++++++++ } = opts
++++++++
++++++++ this.opts = {
++++++++ autoDestroy: false,
++++++++ ...streamOptions
++++++++ }
++++++++ WritableStream.call(this, this.opts)
++++++++
++++++++ this._done = false
++++++++ this._parser = this.getParserByHeaders(headers)
++++++++ this._finished = false
++++++++}
++++++++inherits(Busboy, WritableStream)
++++++++
++++++++Busboy.prototype.emit = function (ev) {
++++++++ if (ev === 'finish') {
++++++++ if (!this._done) {
++++++++ this._parser?.end()
++++++++ return
++++++++ } else if (this._finished) {
++++++++ return
++++++++ }
++++++++ this._finished = true
++++++++ }
++++++++ WritableStream.prototype.emit.apply(this, arguments)
++++++++}
++++++++
++++++++Busboy.prototype.getParserByHeaders = function (headers) {
++++++++ const parsed = parseParams(headers['content-type'])
++++++++
++++++++ const cfg = {
++++++++ defCharset: this.opts.defCharset,
++++++++ fileHwm: this.opts.fileHwm,
++++++++ headers,
++++++++ highWaterMark: this.opts.highWaterMark,
++++++++ isPartAFile: this.opts.isPartAFile,
++++++++ limits: this.opts.limits,
++++++++ parsedConType: parsed,
++++++++ preservePath: this.opts.preservePath
++++++++ }
++++++++
++++++++ if (MultipartParser.detect.test(parsed[0])) {
++++++++ return new MultipartParser(this, cfg)
++++++++ }
++++++++ if (UrlencodedParser.detect.test(parsed[0])) {
++++++++ return new UrlencodedParser(this, cfg)
++++++++ }
++++++++ throw new Error('Unsupported Content-Type.')
++++++++}
++++++++
++++++++Busboy.prototype._write = function (chunk, encoding, cb) {
++++++++ this._parser.write(chunk, cb)
++++++++}
++++++++
++++++++module.exports = Busboy
++++++++module.exports.default = Busboy
++++++++module.exports.Busboy = Busboy
++++++++
++++++++module.exports.Dicer = Dicer
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++// TODO:
++++++++// * support 1 nested multipart level
++++++++// (see second multipart example here:
++++++++// http://www.w3.org/TR/html401/interact/forms.html#didx-multipartform-data)
++++++++// * support limits.fieldNameSize
++++++++// -- this will require modifications to utils.parseParams
++++++++
++++++++const { Readable } = require('node:stream')
++++++++const { inherits } = require('node:util')
++++++++
++++++++const Dicer = require('../../deps/dicer/lib/Dicer')
++++++++
++++++++const parseParams = require('../utils/parseParams')
++++++++const decodeText = require('../utils/decodeText')
++++++++const basename = require('../utils/basename')
++++++++const getLimit = require('../utils/getLimit')
++++++++
++++++++const RE_BOUNDARY = /^boundary$/i
++++++++const RE_FIELD = /^form-data$/i
++++++++const RE_CHARSET = /^charset$/i
++++++++const RE_FILENAME = /^filename$/i
++++++++const RE_NAME = /^name$/i
++++++++
++++++++Multipart.detect = /^multipart\/form-data/i
++++++++function Multipart (boy, cfg) {
++++++++ let i
++++++++ let len
++++++++ const self = this
++++++++ let boundary
++++++++ const limits = cfg.limits
++++++++ const isPartAFile = cfg.isPartAFile || ((fieldName, contentType, fileName) => (contentType === 'application/octet-stream' || fileName !== undefined))
++++++++ const parsedConType = cfg.parsedConType || []
++++++++ const defCharset = cfg.defCharset || 'utf8'
++++++++ const preservePath = cfg.preservePath
++++++++ const fileOpts = { highWaterMark: cfg.fileHwm }
++++++++
++++++++ for (i = 0, len = parsedConType.length; i < len; ++i) {
++++++++ if (Array.isArray(parsedConType[i]) &&
++++++++ RE_BOUNDARY.test(parsedConType[i][0])) {
++++++++ boundary = parsedConType[i][1]
++++++++ break
++++++++ }
++++++++ }
++++++++
++++++++ function checkFinished () {
++++++++ if (nends === 0 && finished && !boy._done) {
++++++++ finished = false
++++++++ self.end()
++++++++ }
++++++++ }
++++++++
++++++++ if (typeof boundary !== 'string') { throw new Error('Multipart: Boundary not found') }
++++++++
++++++++ const fieldSizeLimit = getLimit(limits, 'fieldSize', 1 * 1024 * 1024)
++++++++ const fileSizeLimit = getLimit(limits, 'fileSize', Infinity)
++++++++ const filesLimit = getLimit(limits, 'files', Infinity)
++++++++ const fieldsLimit = getLimit(limits, 'fields', Infinity)
++++++++ const partsLimit = getLimit(limits, 'parts', Infinity)
++++++++ const headerPairsLimit = getLimit(limits, 'headerPairs', 2000)
++++++++ const headerSizeLimit = getLimit(limits, 'headerSize', 80 * 1024)
++++++++
++++++++ let nfiles = 0
++++++++ let nfields = 0
++++++++ let nends = 0
++++++++ let curFile
++++++++ let curField
++++++++ let finished = false
++++++++
++++++++ this._needDrain = false
++++++++ this._pause = false
++++++++ this._cb = undefined
++++++++ this._nparts = 0
++++++++ this._boy = boy
++++++++
++++++++ const parserCfg = {
++++++++ boundary,
++++++++ maxHeaderPairs: headerPairsLimit,
++++++++ maxHeaderSize: headerSizeLimit,
++++++++ partHwm: fileOpts.highWaterMark,
++++++++ highWaterMark: cfg.highWaterMark
++++++++ }
++++++++
++++++++ this.parser = new Dicer(parserCfg)
++++++++ this.parser.on('drain', function () {
++++++++ self._needDrain = false
++++++++ if (self._cb && !self._pause) {
++++++++ const cb = self._cb
++++++++ self._cb = undefined
++++++++ cb()
++++++++ }
++++++++ }).on('part', function onPart (part) {
++++++++ if (++self._nparts > partsLimit) {
++++++++ self.parser.removeListener('part', onPart)
++++++++ self.parser.on('part', skipPart)
++++++++ boy.hitPartsLimit = true
++++++++ boy.emit('partsLimit')
++++++++ return skipPart(part)
++++++++ }
++++++++
++++++++ // hack because streams2 _always_ doesn't emit 'end' until nextTick, so let
++++++++ // us emit 'end' early since we know the part has ended if we are already
++++++++ // seeing the next part
++++++++ if (curField) {
++++++++ const field = curField
++++++++ field.emit('end')
++++++++ field.removeAllListeners('end')
++++++++ }
++++++++
++++++++ part.on('header', function (header) {
++++++++ let contype
++++++++ let fieldname
++++++++ let parsed
++++++++ let charset
++++++++ let encoding
++++++++ let filename
++++++++ let nsize = 0
++++++++
++++++++ if (header['content-type']) {
++++++++ parsed = parseParams(header['content-type'][0])
++++++++ if (parsed[0]) {
++++++++ contype = parsed[0].toLowerCase()
++++++++ for (i = 0, len = parsed.length; i < len; ++i) {
++++++++ if (RE_CHARSET.test(parsed[i][0])) {
++++++++ charset = parsed[i][1].toLowerCase()
++++++++ break
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ if (contype === undefined) { contype = 'text/plain' }
++++++++ if (charset === undefined) { charset = defCharset }
++++++++
++++++++ if (header['content-disposition']) {
++++++++ parsed = parseParams(header['content-disposition'][0])
++++++++ if (!RE_FIELD.test(parsed[0])) { return skipPart(part) }
++++++++ for (i = 0, len = parsed.length; i < len; ++i) {
++++++++ if (RE_NAME.test(parsed[i][0])) {
++++++++ fieldname = parsed[i][1]
++++++++ } else if (RE_FILENAME.test(parsed[i][0])) {
++++++++ filename = parsed[i][1]
++++++++ if (!preservePath) { filename = basename(filename) }
++++++++ }
++++++++ }
++++++++ } else { return skipPart(part) }
++++++++
++++++++ if (header['content-transfer-encoding']) { encoding = header['content-transfer-encoding'][0].toLowerCase() } else { encoding = '7bit' }
++++++++
++++++++ let onData,
++++++++ onEnd
++++++++
++++++++ if (isPartAFile(fieldname, contype, filename)) {
++++++++ // file/binary field
++++++++ if (nfiles === filesLimit) {
++++++++ if (!boy.hitFilesLimit) {
++++++++ boy.hitFilesLimit = true
++++++++ boy.emit('filesLimit')
++++++++ }
++++++++ return skipPart(part)
++++++++ }
++++++++
++++++++ ++nfiles
++++++++
++++++++ if (boy.listenerCount('file') === 0) {
++++++++ self.parser._ignore()
++++++++ return
++++++++ }
++++++++
++++++++ ++nends
++++++++ const file = new FileStream(fileOpts)
++++++++ curFile = file
++++++++ file.on('end', function () {
++++++++ --nends
++++++++ self._pause = false
++++++++ checkFinished()
++++++++ if (self._cb && !self._needDrain) {
++++++++ const cb = self._cb
++++++++ self._cb = undefined
++++++++ cb()
++++++++ }
++++++++ })
++++++++ file._read = function (n) {
++++++++ if (!self._pause) { return }
++++++++ self._pause = false
++++++++ if (self._cb && !self._needDrain) {
++++++++ const cb = self._cb
++++++++ self._cb = undefined
++++++++ cb()
++++++++ }
++++++++ }
++++++++ boy.emit('file', fieldname, file, filename, encoding, contype)
++++++++
++++++++ onData = function (data) {
++++++++ if ((nsize += data.length) > fileSizeLimit) {
++++++++ const extralen = fileSizeLimit - nsize + data.length
++++++++ if (extralen > 0) { file.push(data.slice(0, extralen)) }
++++++++ file.truncated = true
++++++++ file.bytesRead = fileSizeLimit
++++++++ part.removeAllListeners('data')
++++++++ file.emit('limit')
++++++++ return
++++++++ } else if (!file.push(data)) { self._pause = true }
++++++++
++++++++ file.bytesRead = nsize
++++++++ }
++++++++
++++++++ onEnd = function () {
++++++++ curFile = undefined
++++++++ file.push(null)
++++++++ }
++++++++ } else {
++++++++ // non-file field
++++++++ if (nfields === fieldsLimit) {
++++++++ if (!boy.hitFieldsLimit) {
++++++++ boy.hitFieldsLimit = true
++++++++ boy.emit('fieldsLimit')
++++++++ }
++++++++ return skipPart(part)
++++++++ }
++++++++
++++++++ ++nfields
++++++++ ++nends
++++++++ let buffer = ''
++++++++ let truncated = false
++++++++ curField = part
++++++++
++++++++ onData = function (data) {
++++++++ if ((nsize += data.length) > fieldSizeLimit) {
++++++++ const extralen = (fieldSizeLimit - (nsize - data.length))
++++++++ buffer += data.toString('binary', 0, extralen)
++++++++ truncated = true
++++++++ part.removeAllListeners('data')
++++++++ } else { buffer += data.toString('binary') }
++++++++ }
++++++++
++++++++ onEnd = function () {
++++++++ curField = undefined
++++++++ if (buffer.length) { buffer = decodeText(buffer, 'binary', charset) }
++++++++ boy.emit('field', fieldname, buffer, false, truncated, encoding, contype)
++++++++ --nends
++++++++ checkFinished()
++++++++ }
++++++++ }
++++++++
++++++++ /* As of node@2efe4ab761666 (v0.10.29+/v0.11.14+), busboy had become
++++++++ broken. Streams2/streams3 is a huge black box of confusion, but
++++++++ somehow overriding the sync state seems to fix things again (and still
++++++++ seems to work for previous node versions).
++++++++ */
++++++++ part._readableState.sync = false
++++++++
++++++++ part.on('data', onData)
++++++++ part.on('end', onEnd)
++++++++ }).on('error', function (err) {
++++++++ if (curFile) { curFile.emit('error', err) }
++++++++ })
++++++++ }).on('error', function (err) {
++++++++ boy.emit('error', err)
++++++++ }).on('finish', function () {
++++++++ finished = true
++++++++ checkFinished()
++++++++ })
++++++++}
++++++++
++++++++Multipart.prototype.write = function (chunk, cb) {
++++++++ const r = this.parser.write(chunk)
++++++++ if (r && !this._pause) {
++++++++ cb()
++++++++ } else {
++++++++ this._needDrain = !r
++++++++ this._cb = cb
++++++++ }
++++++++}
++++++++
++++++++Multipart.prototype.end = function () {
++++++++ const self = this
++++++++
++++++++ if (self.parser.writable) {
++++++++ self.parser.end()
++++++++ } else if (!self._boy._done) {
++++++++ process.nextTick(function () {
++++++++ self._boy._done = true
++++++++ self._boy.emit('finish')
++++++++ })
++++++++ }
++++++++}
++++++++
++++++++function skipPart (part) {
++++++++ part.resume()
++++++++}
++++++++
++++++++function FileStream (opts) {
++++++++ Readable.call(this, opts)
++++++++
++++++++ this.bytesRead = 0
++++++++
++++++++ this.truncated = false
++++++++}
++++++++
++++++++inherits(FileStream, Readable)
++++++++
++++++++FileStream.prototype._read = function (n) {}
++++++++
++++++++module.exports = Multipart
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Decoder = require('../utils/Decoder')
++++++++const decodeText = require('../utils/decodeText')
++++++++const getLimit = require('../utils/getLimit')
++++++++
++++++++const RE_CHARSET = /^charset$/i
++++++++
++++++++UrlEncoded.detect = /^application\/x-www-form-urlencoded/i
++++++++function UrlEncoded (boy, cfg) {
++++++++ const limits = cfg.limits
++++++++ const parsedConType = cfg.parsedConType
++++++++ this.boy = boy
++++++++
++++++++ this.fieldSizeLimit = getLimit(limits, 'fieldSize', 1 * 1024 * 1024)
++++++++ this.fieldNameSizeLimit = getLimit(limits, 'fieldNameSize', 100)
++++++++ this.fieldsLimit = getLimit(limits, 'fields', Infinity)
++++++++
++++++++ let charset
++++++++ for (var i = 0, len = parsedConType.length; i < len; ++i) { // eslint-disable-line no-var
++++++++ if (Array.isArray(parsedConType[i]) &&
++++++++ RE_CHARSET.test(parsedConType[i][0])) {
++++++++ charset = parsedConType[i][1].toLowerCase()
++++++++ break
++++++++ }
++++++++ }
++++++++
++++++++ if (charset === undefined) { charset = cfg.defCharset || 'utf8' }
++++++++
++++++++ this.decoder = new Decoder()
++++++++ this.charset = charset
++++++++ this._fields = 0
++++++++ this._state = 'key'
++++++++ this._checkingBytes = true
++++++++ this._bytesKey = 0
++++++++ this._bytesVal = 0
++++++++ this._key = ''
++++++++ this._val = ''
++++++++ this._keyTrunc = false
++++++++ this._valTrunc = false
++++++++ this._hitLimit = false
++++++++}
++++++++
++++++++UrlEncoded.prototype.write = function (data, cb) {
++++++++ if (this._fields === this.fieldsLimit) {
++++++++ if (!this.boy.hitFieldsLimit) {
++++++++ this.boy.hitFieldsLimit = true
++++++++ this.boy.emit('fieldsLimit')
++++++++ }
++++++++ return cb()
++++++++ }
++++++++
++++++++ let idxeq; let idxamp; let i; let p = 0; const len = data.length
++++++++
++++++++ while (p < len) {
++++++++ if (this._state === 'key') {
++++++++ idxeq = idxamp = undefined
++++++++ for (i = p; i < len; ++i) {
++++++++ if (!this._checkingBytes) { ++p }
++++++++ if (data[i] === 0x3D/* = */) {
++++++++ idxeq = i
++++++++ break
++++++++ } else if (data[i] === 0x26/* & */) {
++++++++ idxamp = i
++++++++ break
++++++++ }
++++++++ if (this._checkingBytes && this._bytesKey === this.fieldNameSizeLimit) {
++++++++ this._hitLimit = true
++++++++ break
++++++++ } else if (this._checkingBytes) { ++this._bytesKey }
++++++++ }
++++++++
++++++++ if (idxeq !== undefined) {
++++++++ // key with assignment
++++++++ if (idxeq > p) { this._key += this.decoder.write(data.toString('binary', p, idxeq)) }
++++++++ this._state = 'val'
++++++++
++++++++ this._hitLimit = false
++++++++ this._checkingBytes = true
++++++++ this._val = ''
++++++++ this._bytesVal = 0
++++++++ this._valTrunc = false
++++++++ this.decoder.reset()
++++++++
++++++++ p = idxeq + 1
++++++++ } else if (idxamp !== undefined) {
++++++++ // key with no assignment
++++++++ ++this._fields
++++++++ let key; const keyTrunc = this._keyTrunc
++++++++ if (idxamp > p) { key = (this._key += this.decoder.write(data.toString('binary', p, idxamp))) } else { key = this._key }
++++++++
++++++++ this._hitLimit = false
++++++++ this._checkingBytes = true
++++++++ this._key = ''
++++++++ this._bytesKey = 0
++++++++ this._keyTrunc = false
++++++++ this.decoder.reset()
++++++++
++++++++ if (key.length) {
++++++++ this.boy.emit('field', decodeText(key, 'binary', this.charset),
++++++++ '',
++++++++ keyTrunc,
++++++++ false)
++++++++ }
++++++++
++++++++ p = idxamp + 1
++++++++ if (this._fields === this.fieldsLimit) { return cb() }
++++++++ } else if (this._hitLimit) {
++++++++ // we may not have hit the actual limit if there are encoded bytes...
++++++++ if (i > p) { this._key += this.decoder.write(data.toString('binary', p, i)) }
++++++++ p = i
++++++++ if ((this._bytesKey = this._key.length) === this.fieldNameSizeLimit) {
++++++++ // yep, we actually did hit the limit
++++++++ this._checkingBytes = false
++++++++ this._keyTrunc = true
++++++++ }
++++++++ } else {
++++++++ if (p < len) { this._key += this.decoder.write(data.toString('binary', p)) }
++++++++ p = len
++++++++ }
++++++++ } else {
++++++++ idxamp = undefined
++++++++ for (i = p; i < len; ++i) {
++++++++ if (!this._checkingBytes) { ++p }
++++++++ if (data[i] === 0x26/* & */) {
++++++++ idxamp = i
++++++++ break
++++++++ }
++++++++ if (this._checkingBytes && this._bytesVal === this.fieldSizeLimit) {
++++++++ this._hitLimit = true
++++++++ break
++++++++ } else if (this._checkingBytes) { ++this._bytesVal }
++++++++ }
++++++++
++++++++ if (idxamp !== undefined) {
++++++++ ++this._fields
++++++++ if (idxamp > p) { this._val += this.decoder.write(data.toString('binary', p, idxamp)) }
++++++++ this.boy.emit('field', decodeText(this._key, 'binary', this.charset),
++++++++ decodeText(this._val, 'binary', this.charset),
++++++++ this._keyTrunc,
++++++++ this._valTrunc)
++++++++ this._state = 'key'
++++++++
++++++++ this._hitLimit = false
++++++++ this._checkingBytes = true
++++++++ this._key = ''
++++++++ this._bytesKey = 0
++++++++ this._keyTrunc = false
++++++++ this.decoder.reset()
++++++++
++++++++ p = idxamp + 1
++++++++ if (this._fields === this.fieldsLimit) { return cb() }
++++++++ } else if (this._hitLimit) {
++++++++ // we may not have hit the actual limit if there are encoded bytes...
++++++++ if (i > p) { this._val += this.decoder.write(data.toString('binary', p, i)) }
++++++++ p = i
++++++++ if ((this._val === '' && this.fieldSizeLimit === 0) ||
++++++++ (this._bytesVal = this._val.length) === this.fieldSizeLimit) {
++++++++ // yep, we actually did hit the limit
++++++++ this._checkingBytes = false
++++++++ this._valTrunc = true
++++++++ }
++++++++ } else {
++++++++ if (p < len) { this._val += this.decoder.write(data.toString('binary', p)) }
++++++++ p = len
++++++++ }
++++++++ }
++++++++ }
++++++++ cb()
++++++++}
++++++++
++++++++UrlEncoded.prototype.end = function () {
++++++++ if (this.boy._done) { return }
++++++++
++++++++ if (this._state === 'key' && this._key.length > 0) {
++++++++ this.boy.emit('field', decodeText(this._key, 'binary', this.charset),
++++++++ '',
++++++++ this._keyTrunc,
++++++++ false)
++++++++ } else if (this._state === 'val') {
++++++++ this.boy.emit('field', decodeText(this._key, 'binary', this.charset),
++++++++ decodeText(this._val, 'binary', this.charset),
++++++++ this._keyTrunc,
++++++++ this._valTrunc)
++++++++ }
++++++++ this.boy._done = true
++++++++ this.boy.emit('finish')
++++++++}
++++++++
++++++++module.exports = UrlEncoded
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const RE_PLUS = /\+/g
++++++++
++++++++const HEX = [
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
++++++++ 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
++++++++ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
++++++++]
++++++++
++++++++function Decoder () {
++++++++ this.buffer = undefined
++++++++}
++++++++Decoder.prototype.write = function (str) {
++++++++ // Replace '+' with ' ' before decoding
++++++++ str = str.replace(RE_PLUS, ' ')
++++++++ let res = ''
++++++++ let i = 0; let p = 0; const len = str.length
++++++++ for (; i < len; ++i) {
++++++++ if (this.buffer !== undefined) {
++++++++ if (!HEX[str.charCodeAt(i)]) {
++++++++ res += '%' + this.buffer
++++++++ this.buffer = undefined
++++++++ --i // retry character
++++++++ } else {
++++++++ this.buffer += str[i]
++++++++ ++p
++++++++ if (this.buffer.length === 2) {
++++++++ res += String.fromCharCode(parseInt(this.buffer, 16))
++++++++ this.buffer = undefined
++++++++ }
++++++++ }
++++++++ } else if (str[i] === '%') {
++++++++ if (i > p) {
++++++++ res += str.substring(p, i)
++++++++ p = i
++++++++ }
++++++++ this.buffer = ''
++++++++ ++p
++++++++ }
++++++++ }
++++++++ if (p < len && this.buffer === undefined) { res += str.substring(p) }
++++++++ return res
++++++++}
++++++++Decoder.prototype.reset = function () {
++++++++ this.buffer = undefined
++++++++}
++++++++
++++++++module.exports = Decoder
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++module.exports = function basename (path) {
++++++++ if (typeof path !== 'string') { return '' }
++++++++ for (var i = path.length - 1; i >= 0; --i) { // eslint-disable-line no-var
++++++++ switch (path.charCodeAt(i)) {
++++++++ case 0x2F: // '/'
++++++++ case 0x5C: // '\'
++++++++ path = path.slice(i + 1)
++++++++ return (path === '..' || path === '.' ? '' : path)
++++++++ }
++++++++ }
++++++++ return (path === '..' || path === '.' ? '' : path)
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++// Node has always utf-8
++++++++const utf8Decoder = new TextDecoder('utf-8')
++++++++const textDecoders = new Map([
++++++++ ['utf-8', utf8Decoder],
++++++++ ['utf8', utf8Decoder]
++++++++])
++++++++
++++++++function getDecoder (charset) {
++++++++ let lc
++++++++ while (true) {
++++++++ switch (charset) {
++++++++ case 'utf-8':
++++++++ case 'utf8':
++++++++ return decoders.utf8
++++++++ case 'latin1':
++++++++ case 'ascii': // TODO: Make these a separate, strict decoder?
++++++++ case 'us-ascii':
++++++++ case 'iso-8859-1':
++++++++ case 'iso8859-1':
++++++++ case 'iso88591':
++++++++ case 'iso_8859-1':
++++++++ case 'windows-1252':
++++++++ case 'iso_8859-1:1987':
++++++++ case 'cp1252':
++++++++ case 'x-cp1252':
++++++++ return decoders.latin1
++++++++ case 'utf16le':
++++++++ case 'utf-16le':
++++++++ case 'ucs2':
++++++++ case 'ucs-2':
++++++++ return decoders.utf16le
++++++++ case 'base64':
++++++++ return decoders.base64
++++++++ default:
++++++++ if (lc === undefined) {
++++++++ lc = true
++++++++ charset = charset.toLowerCase()
++++++++ continue
++++++++ }
++++++++ return decoders.other.bind(charset)
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++const decoders = {
++++++++ utf8: (data, sourceEncoding) => {
++++++++ if (data.length === 0) {
++++++++ return ''
++++++++ }
++++++++ if (typeof data === 'string') {
++++++++ data = Buffer.from(data, sourceEncoding)
++++++++ }
++++++++ return data.utf8Slice(0, data.length)
++++++++ },
++++++++
++++++++ latin1: (data, sourceEncoding) => {
++++++++ if (data.length === 0) {
++++++++ return ''
++++++++ }
++++++++ if (typeof data === 'string') {
++++++++ return data
++++++++ }
++++++++ return data.latin1Slice(0, data.length)
++++++++ },
++++++++
++++++++ utf16le: (data, sourceEncoding) => {
++++++++ if (data.length === 0) {
++++++++ return ''
++++++++ }
++++++++ if (typeof data === 'string') {
++++++++ data = Buffer.from(data, sourceEncoding)
++++++++ }
++++++++ return data.ucs2Slice(0, data.length)
++++++++ },
++++++++
++++++++ base64: (data, sourceEncoding) => {
++++++++ if (data.length === 0) {
++++++++ return ''
++++++++ }
++++++++ if (typeof data === 'string') {
++++++++ data = Buffer.from(data, sourceEncoding)
++++++++ }
++++++++ return data.base64Slice(0, data.length)
++++++++ },
++++++++
++++++++ other: (data, sourceEncoding) => {
++++++++ if (data.length === 0) {
++++++++ return ''
++++++++ }
++++++++ if (typeof data === 'string') {
++++++++ data = Buffer.from(data, sourceEncoding)
++++++++ }
++++++++
++++++++ if (textDecoders.has(this.toString())) {
++++++++ try {
++++++++ return textDecoders.get(this).decode(data)
++++++++ } catch {}
++++++++ }
++++++++ return typeof data === 'string'
++++++++ ? data
++++++++ : data.toString()
++++++++ }
++++++++}
++++++++
++++++++function decodeText (text, sourceEncoding, destEncoding) {
++++++++ if (text) {
++++++++ return getDecoder(destEncoding)(text, sourceEncoding)
++++++++ }
++++++++ return text
++++++++}
++++++++
++++++++module.exports = decodeText
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++module.exports = function getLimit (limits, name, defaultLimit) {
++++++++ if (
++++++++ !limits ||
++++++++ limits[name] === undefined ||
++++++++ limits[name] === null
++++++++ ) { return defaultLimit }
++++++++
++++++++ if (
++++++++ typeof limits[name] !== 'number' ||
++++++++ isNaN(limits[name])
++++++++ ) { throw new TypeError('Limit ' + name + ' is not a valid number') }
++++++++
++++++++ return limits[name]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/* eslint-disable object-property-newline */
++++++++'use strict'
++++++++
++++++++const decodeText = require('./decodeText')
++++++++
++++++++const RE_ENCODED = /%[a-fA-F0-9][a-fA-F0-9]/g
++++++++
++++++++const EncodedLookup = {
++++++++ '%00': '\x00', '%01': '\x01', '%02': '\x02', '%03': '\x03', '%04': '\x04',
++++++++ '%05': '\x05', '%06': '\x06', '%07': '\x07', '%08': '\x08', '%09': '\x09',
++++++++ '%0a': '\x0a', '%0A': '\x0a', '%0b': '\x0b', '%0B': '\x0b', '%0c': '\x0c',
++++++++ '%0C': '\x0c', '%0d': '\x0d', '%0D': '\x0d', '%0e': '\x0e', '%0E': '\x0e',
++++++++ '%0f': '\x0f', '%0F': '\x0f', '%10': '\x10', '%11': '\x11', '%12': '\x12',
++++++++ '%13': '\x13', '%14': '\x14', '%15': '\x15', '%16': '\x16', '%17': '\x17',
++++++++ '%18': '\x18', '%19': '\x19', '%1a': '\x1a', '%1A': '\x1a', '%1b': '\x1b',
++++++++ '%1B': '\x1b', '%1c': '\x1c', '%1C': '\x1c', '%1d': '\x1d', '%1D': '\x1d',
++++++++ '%1e': '\x1e', '%1E': '\x1e', '%1f': '\x1f', '%1F': '\x1f', '%20': '\x20',
++++++++ '%21': '\x21', '%22': '\x22', '%23': '\x23', '%24': '\x24', '%25': '\x25',
++++++++ '%26': '\x26', '%27': '\x27', '%28': '\x28', '%29': '\x29', '%2a': '\x2a',
++++++++ '%2A': '\x2a', '%2b': '\x2b', '%2B': '\x2b', '%2c': '\x2c', '%2C': '\x2c',
++++++++ '%2d': '\x2d', '%2D': '\x2d', '%2e': '\x2e', '%2E': '\x2e', '%2f': '\x2f',
++++++++ '%2F': '\x2f', '%30': '\x30', '%31': '\x31', '%32': '\x32', '%33': '\x33',
++++++++ '%34': '\x34', '%35': '\x35', '%36': '\x36', '%37': '\x37', '%38': '\x38',
++++++++ '%39': '\x39', '%3a': '\x3a', '%3A': '\x3a', '%3b': '\x3b', '%3B': '\x3b',
++++++++ '%3c': '\x3c', '%3C': '\x3c', '%3d': '\x3d', '%3D': '\x3d', '%3e': '\x3e',
++++++++ '%3E': '\x3e', '%3f': '\x3f', '%3F': '\x3f', '%40': '\x40', '%41': '\x41',
++++++++ '%42': '\x42', '%43': '\x43', '%44': '\x44', '%45': '\x45', '%46': '\x46',
++++++++ '%47': '\x47', '%48': '\x48', '%49': '\x49', '%4a': '\x4a', '%4A': '\x4a',
++++++++ '%4b': '\x4b', '%4B': '\x4b', '%4c': '\x4c', '%4C': '\x4c', '%4d': '\x4d',
++++++++ '%4D': '\x4d', '%4e': '\x4e', '%4E': '\x4e', '%4f': '\x4f', '%4F': '\x4f',
++++++++ '%50': '\x50', '%51': '\x51', '%52': '\x52', '%53': '\x53', '%54': '\x54',
++++++++ '%55': '\x55', '%56': '\x56', '%57': '\x57', '%58': '\x58', '%59': '\x59',
++++++++ '%5a': '\x5a', '%5A': '\x5a', '%5b': '\x5b', '%5B': '\x5b', '%5c': '\x5c',
++++++++ '%5C': '\x5c', '%5d': '\x5d', '%5D': '\x5d', '%5e': '\x5e', '%5E': '\x5e',
++++++++ '%5f': '\x5f', '%5F': '\x5f', '%60': '\x60', '%61': '\x61', '%62': '\x62',
++++++++ '%63': '\x63', '%64': '\x64', '%65': '\x65', '%66': '\x66', '%67': '\x67',
++++++++ '%68': '\x68', '%69': '\x69', '%6a': '\x6a', '%6A': '\x6a', '%6b': '\x6b',
++++++++ '%6B': '\x6b', '%6c': '\x6c', '%6C': '\x6c', '%6d': '\x6d', '%6D': '\x6d',
++++++++ '%6e': '\x6e', '%6E': '\x6e', '%6f': '\x6f', '%6F': '\x6f', '%70': '\x70',
++++++++ '%71': '\x71', '%72': '\x72', '%73': '\x73', '%74': '\x74', '%75': '\x75',
++++++++ '%76': '\x76', '%77': '\x77', '%78': '\x78', '%79': '\x79', '%7a': '\x7a',
++++++++ '%7A': '\x7a', '%7b': '\x7b', '%7B': '\x7b', '%7c': '\x7c', '%7C': '\x7c',
++++++++ '%7d': '\x7d', '%7D': '\x7d', '%7e': '\x7e', '%7E': '\x7e', '%7f': '\x7f',
++++++++ '%7F': '\x7f', '%80': '\x80', '%81': '\x81', '%82': '\x82', '%83': '\x83',
++++++++ '%84': '\x84', '%85': '\x85', '%86': '\x86', '%87': '\x87', '%88': '\x88',
++++++++ '%89': '\x89', '%8a': '\x8a', '%8A': '\x8a', '%8b': '\x8b', '%8B': '\x8b',
++++++++ '%8c': '\x8c', '%8C': '\x8c', '%8d': '\x8d', '%8D': '\x8d', '%8e': '\x8e',
++++++++ '%8E': '\x8e', '%8f': '\x8f', '%8F': '\x8f', '%90': '\x90', '%91': '\x91',
++++++++ '%92': '\x92', '%93': '\x93', '%94': '\x94', '%95': '\x95', '%96': '\x96',
++++++++ '%97': '\x97', '%98': '\x98', '%99': '\x99', '%9a': '\x9a', '%9A': '\x9a',
++++++++ '%9b': '\x9b', '%9B': '\x9b', '%9c': '\x9c', '%9C': '\x9c', '%9d': '\x9d',
++++++++ '%9D': '\x9d', '%9e': '\x9e', '%9E': '\x9e', '%9f': '\x9f', '%9F': '\x9f',
++++++++ '%a0': '\xa0', '%A0': '\xa0', '%a1': '\xa1', '%A1': '\xa1', '%a2': '\xa2',
++++++++ '%A2': '\xa2', '%a3': '\xa3', '%A3': '\xa3', '%a4': '\xa4', '%A4': '\xa4',
++++++++ '%a5': '\xa5', '%A5': '\xa5', '%a6': '\xa6', '%A6': '\xa6', '%a7': '\xa7',
++++++++ '%A7': '\xa7', '%a8': '\xa8', '%A8': '\xa8', '%a9': '\xa9', '%A9': '\xa9',
++++++++ '%aa': '\xaa', '%Aa': '\xaa', '%aA': '\xaa', '%AA': '\xaa', '%ab': '\xab',
++++++++ '%Ab': '\xab', '%aB': '\xab', '%AB': '\xab', '%ac': '\xac', '%Ac': '\xac',
++++++++ '%aC': '\xac', '%AC': '\xac', '%ad': '\xad', '%Ad': '\xad', '%aD': '\xad',
++++++++ '%AD': '\xad', '%ae': '\xae', '%Ae': '\xae', '%aE': '\xae', '%AE': '\xae',
++++++++ '%af': '\xaf', '%Af': '\xaf', '%aF': '\xaf', '%AF': '\xaf', '%b0': '\xb0',
++++++++ '%B0': '\xb0', '%b1': '\xb1', '%B1': '\xb1', '%b2': '\xb2', '%B2': '\xb2',
++++++++ '%b3': '\xb3', '%B3': '\xb3', '%b4': '\xb4', '%B4': '\xb4', '%b5': '\xb5',
++++++++ '%B5': '\xb5', '%b6': '\xb6', '%B6': '\xb6', '%b7': '\xb7', '%B7': '\xb7',
++++++++ '%b8': '\xb8', '%B8': '\xb8', '%b9': '\xb9', '%B9': '\xb9', '%ba': '\xba',
++++++++ '%Ba': '\xba', '%bA': '\xba', '%BA': '\xba', '%bb': '\xbb', '%Bb': '\xbb',
++++++++ '%bB': '\xbb', '%BB': '\xbb', '%bc': '\xbc', '%Bc': '\xbc', '%bC': '\xbc',
++++++++ '%BC': '\xbc', '%bd': '\xbd', '%Bd': '\xbd', '%bD': '\xbd', '%BD': '\xbd',
++++++++ '%be': '\xbe', '%Be': '\xbe', '%bE': '\xbe', '%BE': '\xbe', '%bf': '\xbf',
++++++++ '%Bf': '\xbf', '%bF': '\xbf', '%BF': '\xbf', '%c0': '\xc0', '%C0': '\xc0',
++++++++ '%c1': '\xc1', '%C1': '\xc1', '%c2': '\xc2', '%C2': '\xc2', '%c3': '\xc3',
++++++++ '%C3': '\xc3', '%c4': '\xc4', '%C4': '\xc4', '%c5': '\xc5', '%C5': '\xc5',
++++++++ '%c6': '\xc6', '%C6': '\xc6', '%c7': '\xc7', '%C7': '\xc7', '%c8': '\xc8',
++++++++ '%C8': '\xc8', '%c9': '\xc9', '%C9': '\xc9', '%ca': '\xca', '%Ca': '\xca',
++++++++ '%cA': '\xca', '%CA': '\xca', '%cb': '\xcb', '%Cb': '\xcb', '%cB': '\xcb',
++++++++ '%CB': '\xcb', '%cc': '\xcc', '%Cc': '\xcc', '%cC': '\xcc', '%CC': '\xcc',
++++++++ '%cd': '\xcd', '%Cd': '\xcd', '%cD': '\xcd', '%CD': '\xcd', '%ce': '\xce',
++++++++ '%Ce': '\xce', '%cE': '\xce', '%CE': '\xce', '%cf': '\xcf', '%Cf': '\xcf',
++++++++ '%cF': '\xcf', '%CF': '\xcf', '%d0': '\xd0', '%D0': '\xd0', '%d1': '\xd1',
++++++++ '%D1': '\xd1', '%d2': '\xd2', '%D2': '\xd2', '%d3': '\xd3', '%D3': '\xd3',
++++++++ '%d4': '\xd4', '%D4': '\xd4', '%d5': '\xd5', '%D5': '\xd5', '%d6': '\xd6',
++++++++ '%D6': '\xd6', '%d7': '\xd7', '%D7': '\xd7', '%d8': '\xd8', '%D8': '\xd8',
++++++++ '%d9': '\xd9', '%D9': '\xd9', '%da': '\xda', '%Da': '\xda', '%dA': '\xda',
++++++++ '%DA': '\xda', '%db': '\xdb', '%Db': '\xdb', '%dB': '\xdb', '%DB': '\xdb',
++++++++ '%dc': '\xdc', '%Dc': '\xdc', '%dC': '\xdc', '%DC': '\xdc', '%dd': '\xdd',
++++++++ '%Dd': '\xdd', '%dD': '\xdd', '%DD': '\xdd', '%de': '\xde', '%De': '\xde',
++++++++ '%dE': '\xde', '%DE': '\xde', '%df': '\xdf', '%Df': '\xdf', '%dF': '\xdf',
++++++++ '%DF': '\xdf', '%e0': '\xe0', '%E0': '\xe0', '%e1': '\xe1', '%E1': '\xe1',
++++++++ '%e2': '\xe2', '%E2': '\xe2', '%e3': '\xe3', '%E3': '\xe3', '%e4': '\xe4',
++++++++ '%E4': '\xe4', '%e5': '\xe5', '%E5': '\xe5', '%e6': '\xe6', '%E6': '\xe6',
++++++++ '%e7': '\xe7', '%E7': '\xe7', '%e8': '\xe8', '%E8': '\xe8', '%e9': '\xe9',
++++++++ '%E9': '\xe9', '%ea': '\xea', '%Ea': '\xea', '%eA': '\xea', '%EA': '\xea',
++++++++ '%eb': '\xeb', '%Eb': '\xeb', '%eB': '\xeb', '%EB': '\xeb', '%ec': '\xec',
++++++++ '%Ec': '\xec', '%eC': '\xec', '%EC': '\xec', '%ed': '\xed', '%Ed': '\xed',
++++++++ '%eD': '\xed', '%ED': '\xed', '%ee': '\xee', '%Ee': '\xee', '%eE': '\xee',
++++++++ '%EE': '\xee', '%ef': '\xef', '%Ef': '\xef', '%eF': '\xef', '%EF': '\xef',
++++++++ '%f0': '\xf0', '%F0': '\xf0', '%f1': '\xf1', '%F1': '\xf1', '%f2': '\xf2',
++++++++ '%F2': '\xf2', '%f3': '\xf3', '%F3': '\xf3', '%f4': '\xf4', '%F4': '\xf4',
++++++++ '%f5': '\xf5', '%F5': '\xf5', '%f6': '\xf6', '%F6': '\xf6', '%f7': '\xf7',
++++++++ '%F7': '\xf7', '%f8': '\xf8', '%F8': '\xf8', '%f9': '\xf9', '%F9': '\xf9',
++++++++ '%fa': '\xfa', '%Fa': '\xfa', '%fA': '\xfa', '%FA': '\xfa', '%fb': '\xfb',
++++++++ '%Fb': '\xfb', '%fB': '\xfb', '%FB': '\xfb', '%fc': '\xfc', '%Fc': '\xfc',
++++++++ '%fC': '\xfc', '%FC': '\xfc', '%fd': '\xfd', '%Fd': '\xfd', '%fD': '\xfd',
++++++++ '%FD': '\xfd', '%fe': '\xfe', '%Fe': '\xfe', '%fE': '\xfe', '%FE': '\xfe',
++++++++ '%ff': '\xff', '%Ff': '\xff', '%fF': '\xff', '%FF': '\xff'
++++++++}
++++++++
++++++++function encodedReplacer (match) {
++++++++ return EncodedLookup[match]
++++++++}
++++++++
++++++++const STATE_KEY = 0
++++++++const STATE_VALUE = 1
++++++++const STATE_CHARSET = 2
++++++++const STATE_LANG = 3
++++++++
++++++++function parseParams (str) {
++++++++ const res = []
++++++++ let state = STATE_KEY
++++++++ let charset = ''
++++++++ let inquote = false
++++++++ let escaping = false
++++++++ let p = 0
++++++++ let tmp = ''
++++++++ const len = str.length
++++++++
++++++++ for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
++++++++ const char = str[i]
++++++++ if (char === '\\' && inquote) {
++++++++ if (escaping) { escaping = false } else {
++++++++ escaping = true
++++++++ continue
++++++++ }
++++++++ } else if (char === '"') {
++++++++ if (!escaping) {
++++++++ if (inquote) {
++++++++ inquote = false
++++++++ state = STATE_KEY
++++++++ } else { inquote = true }
++++++++ continue
++++++++ } else { escaping = false }
++++++++ } else {
++++++++ if (escaping && inquote) { tmp += '\\' }
++++++++ escaping = false
++++++++ if ((state === STATE_CHARSET || state === STATE_LANG) && char === "'") {
++++++++ if (state === STATE_CHARSET) {
++++++++ state = STATE_LANG
++++++++ charset = tmp.substring(1)
++++++++ } else { state = STATE_VALUE }
++++++++ tmp = ''
++++++++ continue
++++++++ } else if (state === STATE_KEY &&
++++++++ (char === '*' || char === '=') &&
++++++++ res.length) {
++++++++ state = char === '*'
++++++++ ? STATE_CHARSET
++++++++ : STATE_VALUE
++++++++ res[p] = [tmp, undefined]
++++++++ tmp = ''
++++++++ continue
++++++++ } else if (!inquote && char === ';') {
++++++++ state = STATE_KEY
++++++++ if (charset) {
++++++++ if (tmp.length) {
++++++++ tmp = decodeText(tmp.replace(RE_ENCODED, encodedReplacer),
++++++++ 'binary',
++++++++ charset)
++++++++ }
++++++++ charset = ''
++++++++ } else if (tmp.length) {
++++++++ tmp = decodeText(tmp, 'binary', 'utf8')
++++++++ }
++++++++ if (res[p] === undefined) { res[p] = tmp } else { res[p][1] = tmp }
++++++++ tmp = ''
++++++++ ++p
++++++++ continue
++++++++ } else if (!inquote && (char === ' ' || char === '\t')) { continue }
++++++++ }
++++++++ tmp += char
++++++++ }
++++++++ if (charset && tmp.length) {
++++++++ tmp = decodeText(tmp.replace(RE_ENCODED, encodedReplacer),
++++++++ 'binary',
++++++++ charset)
++++++++ } else if (tmp) {
++++++++ tmp = decodeText(tmp, 'binary', 'utf8')
++++++++ }
++++++++
++++++++ if (res[p] === undefined) {
++++++++ if (tmp) { res[p] = tmp }
++++++++ } else { res[p][1] = tmp }
++++++++
++++++++ return res
++++++++}
++++++++
++++++++module.exports = parseParams
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "@fastify/busboy",
++++++++ "version": "2.1.1",
++++++++ "private": false,
++++++++ "author": "Brian White <mscdex@mscdex.net>",
++++++++ "contributors": [
++++++++ {
++++++++ "name": "Igor Savin",
++++++++ "email": "kibertoad@gmail.com",
++++++++ "url": "https://github.com/kibertoad"
++++++++ },
++++++++ {
++++++++ "name": "Aras Abbasi",
++++++++ "email": "aras.abbasi@gmail.com",
++++++++ "url": "https://github.com/uzlopak"
++++++++ }
++++++++ ],
++++++++ "description": "A streaming parser for HTML form data for node.js",
++++++++ "main": "lib/main",
++++++++ "type": "commonjs",
++++++++ "types": "lib/main.d.ts",
++++++++ "scripts": {
++++++++ "bench:busboy": "cd benchmarks && npm install && npm run benchmark-fastify",
++++++++ "bench:dicer": "node bench/dicer/dicer-bench-multipart-parser.js",
++++++++ "coveralls": "nyc report --reporter=lcov",
++++++++ "lint": "npm run lint:standard",
++++++++ "lint:everything": "npm run lint && npm run test:types",
++++++++ "lint:fix": "standard --fix",
++++++++ "lint:standard": "standard --verbose | snazzy",
++++++++ "test:mocha": "tap",
++++++++ "test:types": "tsd",
++++++++ "test:coverage": "nyc npm run test",
++++++++ "test": "npm run test:mocha"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=14"
++++++++ },
++++++++ "devDependencies": {
++++++++ "@types/node": "^20.1.0",
++++++++ "busboy": "^1.0.0",
++++++++ "photofinish": "^1.8.0",
++++++++ "snazzy": "^9.0.0",
++++++++ "standard": "^17.0.0",
++++++++ "tap": "^16.3.8",
++++++++ "tinybench": "^2.5.1",
++++++++ "tsd": "^0.30.0",
++++++++ "typescript": "^5.0.2"
++++++++ },
++++++++ "keywords": [
++++++++ "uploads",
++++++++ "forms",
++++++++ "multipart",
++++++++ "form-data"
++++++++ ],
++++++++ "license": "MIT",
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+https://github.com/fastify/busboy.git"
++++++++ },
++++++++ "tsd": {
++++++++ "directory": "test/types",
++++++++ "compilerOptions": {
++++++++ "esModuleInterop": false,
++++++++ "module": "commonjs",
++++++++ "target": "ES2017"
++++++++ }
++++++++ },
++++++++ "standard": {
++++++++ "globals": [
++++++++ "describe",
++++++++ "it"
++++++++ ],
++++++++ "ignore": [
++++++++ "bench"
++++++++ ]
++++++++ },
++++++++ "files": [
++++++++ "README.md",
++++++++ "LICENSE",
++++++++ "lib/*",
++++++++ "deps/encoding/*",
++++++++ "deps/dicer/lib",
++++++++ "deps/streamsearch/",
++++++++ "deps/dicer/LICENSE"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('../lib/main')
++++++++const { test } = require('tap')
++++++++
++++++++test('busboy-constructor - should throw an Error if no options are provided', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Busboy(), new Error('Busboy expected an options-Object.'))
++++++++})
++++++++
++++++++test('busboy-constructor - should throw an Error if options does not contain headers', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Busboy({}), new Error('Busboy expected an options-Object with headers-attribute.'))
++++++++})
++++++++
++++++++test('busboy-constructor - if busboy is called without new-operator, still creates a busboy instance', t => {
++++++++ t.plan(1)
++++++++
++++++++ const busboyInstance = Busboy({ headers: { 'content-type': 'application/x-www-form-urlencoded' } })
++++++++ t.type(busboyInstance, Busboy)
++++++++})
++++++++
++++++++test('busboy-constructor - should throw an Error if content-type is not set', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Busboy({ headers: {} }), new Error('Missing Content-Type-header.'))
++++++++})
++++++++
++++++++test('busboy-constructor - should throw an Error if content-type is unsupported', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Busboy({ headers: { 'content-type': 'unsupported' } }), new Error('Unsupported Content-Type.'))
++++++++})
++++++++
++++++++test('busboy-constructor - should not throw an Error if content-type is urlencoded', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.doesNotThrow(() => new Busboy({ headers: { 'content-type': 'application/x-www-form-urlencoded' } }))
++++++++})
++++++++
++++++++test('busboy-constructor - if busboy is called without stream options autoDestroy is set to false', t => {
++++++++ t.plan(1)
++++++++
++++++++ const busboyInstance = Busboy({ headers: { 'content-type': 'application/x-www-form-urlencoded' } })
++++++++ t.equal(busboyInstance._writableState.autoDestroy, false)
++++++++})
++++++++
++++++++test('busboy-constructor - if busboy is called with invalid value for stream option highWaterMark we should throw', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => Busboy({ highWaterMark: 'not_allowed_value_for_highWaterMark', headers: { 'content-type': 'application/x-www-form-urlencoded' } }), new Error('not_allowed_value_for_highWaterMark'))
++++++++})
++++++++
++++++++test('busboy-constructor - if busboy is called with stream options and autoDestroy:true, autoDestroy should be set to true', t => {
++++++++ t.plan(1)
++++++++
++++++++ const busboyInstance = Busboy({ autoDestroy: true, headers: { 'content-type': 'application/x-www-form-urlencoded' } })
++++++++ t.equal(busboyInstance._writableState.autoDestroy, true)
++++++++})
++++++++
++++++++test('busboy-constructor - busboy should be initialized with private attribute _done set as false', t => {
++++++++ t.plan(1)
++++++++
++++++++ const busboyInstance = Busboy({ headers: { 'content-type': 'application/x-www-form-urlencoded' } })
++++++++ t.equal(busboyInstance._done, false)
++++++++})
++++++++
++++++++test('busboy-constructor - busboy should be initialized with private attribute _finished set as false', t => {
++++++++ t.plan(1)
++++++++
++++++++ const busboyInstance = Busboy({ headers: { 'content-type': 'application/x-www-form-urlencoded' } })
++++++++ t.equal(busboyInstance._finished, false)
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const decodeText = require('../lib/utils/decodeText')
++++++++
++++++++test('decodeText', t => {
++++++++ const testCases = [
++++++++ { description: 'UTF-8 encoding', text: Buffer.from('Hello, World!', 'utf8'), initialCharset: 'utf8', outputCharset: 'utf8', expected: 'Hello, World!' },
++++++++ { description: 'UTF-8 encoding empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'utf8', expected: '' },
++++++++ { description: 'Latin1 encoding', text: Buffer.from('Hello, World!', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: 'Hello, World!' },
++++++++ { description: 'Latin1 encoding empty', text: Buffer.from('', 'latin1'), initialCharset: 'latin1', outputCharset: 'latin1', expected: '' },
++++++++ { description: 'UTF-16LE encoding', text: Buffer.from('Hello, World!', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' },
++++++++ { description: 'UTF-16LE encoding with special char', text: Buffer.from('Hello, World! 🌍🚀', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World! 🌍🚀' },
++++++++ { description: 'UTF-8 to UTF-16LE encoding', text: 'Hello, World!', initialCharset: 'utf16le', outputCharset: 'utf16le', expected: 'Hello, World!' },
++++++++ { description: 'UTF-16LE encoding empty', text: Buffer.from('', 'utf16le'), initialCharset: 'utf16le', outputCharset: 'utf16le', expected: '' },
++++++++ { description: 'Base64 encoding', text: Buffer.from('Hello, World!').toString('base64'), initialCharset: 'base64', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' },
++++++++ { description: 'UTF-8 to Base64 encoding', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' },
++++++++ { description: 'Base64 encoding empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'base64', expected: '' },
++++++++ { description: 'UTF8 to base64', text: Buffer.from('Hello, World!', 'utf-8'), initialCharset: 'utf8', outputCharset: 'base64', expected: 'SGVsbG8sIFdvcmxkIQ==' },
++++++++ { description: 'Unknown', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'other', expected: 'Hello, World!' },
++++++++ { description: 'Unknown empty', text: Buffer.from('', 'utf8'), initialCharset: 'utf8', outputCharset: 'other', expected: '' },
++++++++ { description: 'Unknown', text: 'Hello, World!', initialCharset: 'utf8', outputCharset: 'utf-8', expected: 'Hello, World!' }
++++++++ ]
++++++++
++++++++ t.plan(testCases.length)
++++++++
++++++++ testCases.forEach((c, index) => {
++++++++ t.test(c.description, t => {
++++++++ t.plan(1)
++++++++ t.equal(decodeText(c.text, c.initialCharset, c.outputCharset), c.expected, `Test case ${index + 1}`)
++++++++ })
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const Decoder = require('../lib/utils/Decoder')
++++++++
++++++++test('Decoder', t => {
++++++++ const tests =
++++++++ [
++++++++ {
++++++++ source: ['Hello world'],
++++++++ expected: 'Hello world',
++++++++ what: 'No encoded bytes'
++++++++ },
++++++++ {
++++++++ source: ['Hello%20world'],
++++++++ expected: 'Hello world',
++++++++ what: 'One full encoded byte'
++++++++ },
++++++++ {
++++++++ source: ['Hello%20world%21'],
++++++++ expected: 'Hello world!',
++++++++ what: 'Two full encoded bytes'
++++++++ },
++++++++ {
++++++++ source: ['Hello%', '20world'],
++++++++ expected: 'Hello world',
++++++++ what: 'One full encoded byte split #1'
++++++++ },
++++++++ {
++++++++ source: ['Hello%2', '0world'],
++++++++ expected: 'Hello world',
++++++++ what: 'One full encoded byte split #2'
++++++++ },
++++++++ {
++++++++ source: ['Hello%20', 'world'],
++++++++ expected: 'Hello world',
++++++++ what: 'One full encoded byte (concat)'
++++++++ },
++++++++ {
++++++++ source: ['Hello%2Qworld'],
++++++++ expected: 'Hello%2Qworld',
++++++++ what: 'Malformed encoded byte #1'
++++++++ },
++++++++ {
++++++++ source: ['Hello%world'],
++++++++ expected: 'Hello%world',
++++++++ what: 'Malformed encoded byte #2'
++++++++ },
++++++++ {
++++++++ source: ['Hello+world'],
++++++++ expected: 'Hello world',
++++++++ what: 'Plus to space'
++++++++ },
++++++++ {
++++++++ source: ['Hello+world%21'],
++++++++ expected: 'Hello world!',
++++++++ what: 'Plus and encoded byte'
++++++++ },
++++++++ {
++++++++ source: ['5%2B5%3D10'],
++++++++ expected: '5+5=10',
++++++++ what: 'Encoded plus'
++++++++ },
++++++++ {
++++++++ source: ['5+%2B+5+%3D+10'],
++++++++ expected: '5 + 5 = 10',
++++++++ what: 'Spaces and encoded plus'
++++++++ }
++++++++ ]
++++++++ t.plan(tests.length + 1)
++++++++
++++++++ tests.forEach((v) => {
++++++++ t.test(v.what, t => {
++++++++ t.plan(1)
++++++++
++++++++ const dec = new Decoder()
++++++++ let result = ''
++++++++ v.source.forEach(function (s) {
++++++++ result += dec.write(s)
++++++++ })
++++++++ const msg = 'Decoded string mismatch.\n' +
++++++++ 'Saw: ' + result + '\n' +
++++++++ 'Expected: ' + v.expected
++++++++ t.strictSame(result, v.expected, msg)
++++++++ })
++++++++ })
++++++++
++++++++ t.test('reset sets internal buffer to undefined', t => {
++++++++ t.plan(2)
++++++++
++++++++ const dec = new Decoder()
++++++++ dec.write('Hello+world%2')
++++++++
++++++++ t.notSame(dec.buffer, undefined)
++++++++ dec.reset()
++++++++ t.equal(dec.buffer, undefined)
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++
++++++++test('dicer-constructor', t => {
++++++++ t.plan(2)
++++++++
++++++++ t.test('should throw an Error when no options parameter is supplied to Dicer', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Dicer(), new Error('Boundary required'))
++++++++ })
++++++++
++++++++ t.test('without new operator a new dicer instance will be initialized', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.type(Dicer({
++++++++ boundary: '----boundary'
++++++++ }), Dicer)
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++const { test } = require('tap')
++++++++
++++++++test('dicer-endfinish', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.test('should properly handle finish', t => {
++++++++ t.plan(4)
++++++++
++++++++ const CRLF = '\r\n'
++++++++ const boundary = 'boundary'
++++++++
++++++++ const writeSep = '--' + boundary
++++++++
++++++++ const writePart = [
++++++++ writeSep,
++++++++ 'Content-Type: text/plain',
++++++++ 'Content-Length: 0'
++++++++ ].join(CRLF) +
++++++++ CRLF + CRLF +
++++++++ 'some data' + CRLF
++++++++
++++++++ const writeEnd = '--' + CRLF
++++++++
++++++++ let firedEnd = false
++++++++ let firedFinish = false
++++++++
++++++++ const dicer = new Dicer({ boundary })
++++++++ dicer.on('part', partListener)
++++++++ dicer.on('finish', finishListener)
++++++++ dicer.write(writePart + writeSep)
++++++++
++++++++ function partListener (partReadStream) {
++++++++ partReadStream.on('data', function () { })
++++++++ partReadStream.on('end', partEndListener)
++++++++ }
++++++++ function partEndListener () {
++++++++ firedEnd = true
++++++++ setImmediate(afterEnd)
++++++++ }
++++++++ function afterEnd () {
++++++++ dicer.end(writeEnd)
++++++++ setImmediate(afterWrite)
++++++++ }
++++++++ function finishListener () {
++++++++ t.ok(firedEnd, 'end before finishing')
++++++++ firedFinish = true
++++++++ test2()
++++++++ }
++++++++ function afterWrite () {
++++++++ t.ok(firedFinish, 'Failed to finish')
++++++++ }
++++++++
++++++++ let isPausePush = true
++++++++
++++++++ let firedPauseCallback = false
++++++++ let firedPauseFinish = false
++++++++
++++++++ let dicer2 = null
++++++++
++++++++ function test2 () {
++++++++ dicer2 = new Dicer({ boundary })
++++++++ dicer2.on('part', pausePartListener)
++++++++ dicer2.on('finish', pauseFinish)
++++++++ dicer2.write(writePart + writeSep, 'utf8', pausePartCallback)
++++++++ setImmediate(pauseAfterWrite)
++++++++ }
++++++++ function pausePartListener (partReadStream) {
++++++++ partReadStream.on('data', function () { })
++++++++ partReadStream.on('end', function () { })
++++++++ const realPush = partReadStream.push
++++++++ partReadStream.push = function fakePush () {
++++++++ realPush.apply(partReadStream, arguments)
++++++++ if (!isPausePush) { return true }
++++++++ isPausePush = false
++++++++ return false
++++++++ }
++++++++ }
++++++++ function pauseAfterWrite () {
++++++++ dicer2.end(writeEnd)
++++++++ setImmediate(pauseAfterEnd)
++++++++ }
++++++++ function pauseAfterEnd () {
++++++++ t.ok(firedPauseCallback, 'Called callback after pause')
++++++++ t.ok(firedPauseFinish, 'Finish after pause')
++++++++ }
++++++++ function pauseFinish () {
++++++++ firedPauseFinish = true
++++++++ }
++++++++ function pausePartCallback () {
++++++++ firedPauseCallback = true
++++++++ }
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const { Dicer } = require('../lib/main')
++++++++
++++++++test('dicer-export', t => {
++++++++ t.plan(2)
++++++++
++++++++ t.test('without new operator a new dicer instance will be initialized', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.type(Dicer({
++++++++ boundary: '----boundary'
++++++++ }), Dicer)
++++++++ })
++++++++
++++++++ t.test('with new operator a new dicer instance will be initialized', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.type(new Dicer({
++++++++ boundary: '----boundary'
++++++++ }), Dicer)
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const HeaderParser = require('../deps/dicer/lib/HeaderParser')
++++++++
++++++++test('dicer-headerparser', t => {
++++++++ const DCRLF = '\r\n\r\n'
++++++++ const MAXED_BUFFER = Buffer.allocUnsafe(128 * 1024)
++++++++ MAXED_BUFFER.fill(0x41) // 'A'
++++++++
++++++++ const tests = [
++++++++ {
++++++++ source: DCRLF,
++++++++ expected: {},
++++++++ what: 'No header'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\t text/plain',
++++++++ 'Content-Length:0'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'], 'content-length': ['0'] },
++++++++ what: 'Value spacing'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\t text/plain',
++++++++ 'Content-Length:0'
++++++++ ].join('\r\n') + DCRLF,
++++++++ cfg: {
++++++++ maxHeaderPairs: 0
++++++++ },
++++++++ expected: {},
++++++++ what: 'should enforce maxHeaderPairs of 0'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\t text/plain',
++++++++ 'Content-Length:0'
++++++++ ].join('\r\n') + DCRLF,
++++++++ cfg: {
++++++++ maxHeaderPairs: 1
++++++++ },
++++++++ expected: { 'content-type': [' text/plain'] },
++++++++ what: 'should enforce maxHeaderPairs of 1'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: {},
++++++++ cfg: {
++++++++ maxHeaderSize: 0
++++++++ },
++++++++ what: 'should enforce maxHeaderSize of 0'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plai'] },
++++++++ cfg: {
++++++++ maxHeaderSize: 25
++++++++ },
++++++++ what: 'should enforce maxHeaderSize of 25'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'] },
++++++++ cfg: {
++++++++ maxHeaderSize: 31
++++++++ },
++++++++ what: 'should enforce maxHeaderSize of 31 and ignore the second header'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'], foo: [''] },
++++++++ cfg: {
++++++++ maxHeaderSize: 32
++++++++ },
++++++++ what: 'should enforce maxHeaderSize of 32 and only add key of second header'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'], foo: ['\r'] },
++++++++ cfg: {
++++++++ maxHeaderSize: 33
++++++++ },
++++++++ what: 'should enforce maxHeaderSize of 32 and get only first character of second pair'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ ' : '
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain : '] },
++++++++ what: 'should not break if invalid header pair (colon exists but empty key and value) is provided'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'FoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobaz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'] },
++++++++ what: 'should not break if invalid header pair (no distinctive colon) is provided'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ ':FoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobaz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'] },
++++++++ what: 'should not break if invalid header pair (no key) is provided'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\t text/plain',
++++++++ 'Content-Length:0'
++++++++ ].join('\r\n') + DCRLF,
++++++++ cfg: {
++++++++ maxHeaderPairs: 2
++++++++ },
++++++++ expected: { 'content-type': [' text/plain'], 'content-length': ['0'] },
++++++++ what: 'should enforce maxHeaderPairs of 2'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:\r\n text/plain',
++++++++ 'Foo:\r\n bar\r\n baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [' text/plain'], foo: [' bar baz'] },
++++++++ what: 'Folded values'
++++++++ },
++++++++ {
++++++++ source: [
++++++++ 'Foo: bar',
++++++++ 'Foo: baz'
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { foo: ['bar', 'baz'] },
++++++++ what: 'Folded values'
++++++++ },
++++++++ {
++++++++ source: ['Content-Type:',
++++++++ 'Foo: '
++++++++ ].join('\r\n') + DCRLF,
++++++++ expected: { 'content-type': [''], foo: [''] },
++++++++ what: 'Empty values'
++++++++ },
++++++++ {
++++++++ source: MAXED_BUFFER.toString('ascii') + DCRLF,
++++++++ expected: {},
++++++++ what: 'Max header size (single chunk)'
++++++++ },
++++++++ {
++++++++ source: ['ABCDEFGHIJ', MAXED_BUFFER.toString('ascii'), DCRLF],
++++++++ expected: {},
++++++++ what: 'Max header size (multiple chunks #1)'
++++++++ },
++++++++ {
++++++++ source: [MAXED_BUFFER.toString('ascii'), MAXED_BUFFER.toString('ascii'), DCRLF],
++++++++ expected: {},
++++++++ what: 'Max header size (multiple chunk #2)'
++++++++ }
++++++++ ]
++++++++
++++++++ t.plan(tests.length)
++++++++
++++++++ tests.forEach(function (v) {
++++++++ t.test(v.what, t => {
++++++++ t.plan(4)
++++++++
++++++++ const cfg = {
++++++++ ...v.cfg
++++++++ }
++++++++
++++++++ const parser = Object.keys(cfg).length ? new HeaderParser(cfg) : new HeaderParser()
++++++++ let fired = false
++++++++
++++++++ parser.on('header', function (header) {
++++++++ t.ok(!fired, `${v.what}: Header event fired more than once`)
++++++++ fired = true
++++++++ t.strictSame(header,
++++++++ v.expected,
++++++++ `${v.what}: Parsed result mismatch`)
++++++++ })
++++++++ if (!Array.isArray(v.source)) { v.source = [v.source] }
++++++++ v.source.forEach(function (s) {
++++++++ parser.push(s)
++++++++ })
++++++++ t.ok(fired, `${v.what}: Did not receive header from parser`)
++++++++ t.pass()
++++++++ })
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++
++++++++test('dicer-malformed-header', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.test('should gracefully handle headers with leading whitespace', t => {
++++++++ t.plan(3)
++++++++ const d = new Dicer({ boundary: '----WebKitFormBoundaryoo6vortfDzBsDiro' })
++++++++
++++++++ d.on('part', function (p) {
++++++++ p.on('header', function (header) {
++++++++ t.hasProp(header, ' content-disposition')
++++++++ t.strictSame(header[' content-disposition'], ['form-data; name="bildbeschreibung"'])
++++++++ })
++++++++ p.on('data', function (data) {
++++++++ })
++++++++ p.on('end', function () {
++++++++ })
++++++++ })
++++++++ d.on('finish', function () {
++++++++ t.pass()
++++++++ })
++++++++
++++++++ d.write(Buffer.from('------WebKitFormBoundaryoo6vortfDzBsDiro\r\n Content-Disposition: form-data; name="bildbeschreibung"\r\n\r\n\r\n------WebKitFormBoundaryoo6vortfDzBsDiro--'))
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++const fs = require('fs')
++++++++const path = require('path')
++++++++
++++++++const FIXTURES_ROOT = path.join(__dirname, 'fixtures/')
++++++++
++++++++test('dicer-multipart-extra-trailer', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.test('Extra trailer data pushed after finished', t => {
++++++++ t.plan(5)
++++++++ const fixtureBase = FIXTURES_ROOT + 'many'
++++++++ let n = 0
++++++++ const buffer = Buffer.allocUnsafe(16)
++++++++ const state = { parts: [] }
++++++++
++++++++ const fd = fs.openSync(fixtureBase + '/original', 'r')
++++++++
++++++++ const dicer = new Dicer({ boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' })
++++++++ let error
++++++++ let finishes = 0
++++++++ let trailerEmitted = false
++++++++
++++++++ dicer.on('part', function (p) {
++++++++ const part = {
++++++++ body: undefined,
++++++++ bodylen: 0,
++++++++ error: undefined,
++++++++ header: undefined
++++++++ }
++++++++
++++++++ p.on('header', function (h) {
++++++++ part.header = h
++++++++ }).on('data', function (data) {
++++++++ // make a copy because we are using readSync which re-uses a buffer ...
++++++++ const copy = Buffer.allocUnsafe(data.length)
++++++++ data.copy(copy)
++++++++ data = copy
++++++++ if (!part.body) { part.body = [data] } else { part.body.push(data) }
++++++++ part.bodylen += data.length
++++++++ }).on('error', function (err) {
++++++++ part.error = err
++++++++ t.fail()
++++++++ }).on('end', function () {
++++++++ if (part.body) { part.body = Buffer.concat(part.body, part.bodylen) }
++++++++ state.parts.push(part)
++++++++ })
++++++++ }).on('error', function (err) {
++++++++ error = err
++++++++ }).on('trailer', function (data) {
++++++++ trailerEmitted = true
++++++++ t.equal(data.toString(), 'Extra', 'trailer should contain the extra data')
++++++++ }).on('finish', function () {
++++++++ t.ok(finishes++ === 0, makeMsg('Extra trailer data pushed after finished', 'finish emitted multiple times'))
++++++++ t.ok(trailerEmitted, makeMsg('Extra trailer data pushed after finished', 'should have emitted trailer'))
++++++++
++++++++ t.ok(error === undefined, makeMsg('Extra trailer data pushed after finished', 'Unexpected error'))
++++++++
++++++++ t.pass()
++++++++ })
++++++++
++++++++ while (true) {
++++++++ n = fs.readSync(fd, buffer, 0, buffer.length, null)
++++++++ if (n === 0) {
++++++++ setTimeout(function () {
++++++++ dicer.write('\r\n\r\n\r\n')
++++++++ dicer.end()
++++++++ }, 50)
++++++++ break
++++++++ }
++++++++ dicer.write(n === buffer.length ? buffer : buffer.slice(0, n))
++++++++ }
++++++++ fs.closeSync(fd)
++++++++ })
++++++++})
++++++++
++++++++function makeMsg (what, msg) {
++++++++ return what + ': ' + msg
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++const { test } = require('tap')
++++++++const fs = require('fs')
++++++++const path = require('path')
++++++++
++++++++const FIXTURES_ROOT = path.join(__dirname, 'fixtures/')
++++++++
++++++++test('dicer-multipart-nolisteners', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.test('No preamble or part listeners', t => {
++++++++ t.plan(3)
++++++++ const fixtureBase = path.resolve(FIXTURES_ROOT, 'many')
++++++++ let n = 0
++++++++ const buffer = Buffer.allocUnsafe(16)
++++++++
++++++++ const fd = fs.openSync(fixtureBase + '/original', 'r')
++++++++
++++++++ const dicer = new Dicer({ boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' })
++++++++ let error
++++++++ let finishes = 0
++++++++
++++++++ dicer.on('error', function (err) {
++++++++ error = err
++++++++ }).on('finish', function () {
++++++++ t.ok(finishes++ === 0, 'finish emitted multiple times')
++++++++
++++++++ t.ok(error === undefined, `Unexpected error: ${error}`)
++++++++ t.pass()
++++++++ })
++++++++
++++++++ while (true) {
++++++++ n = fs.readSync(fd, buffer, 0, buffer.length, null)
++++++++ if (n === 0) {
++++++++ dicer.end()
++++++++ break
++++++++ }
++++++++ dicer.write(n === buffer.length ? buffer : buffer.slice(0, n))
++++++++ }
++++++++ fs.closeSync(fd)
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Dicer = require('../deps/dicer/lib/Dicer')
++++++++const assert = require('node:assert')
++++++++const fs = require('node:fs')
++++++++const path = require('node:path')
++++++++const inspect = require('node:util').inspect
++++++++const { test } = require('tap')
++++++++
++++++++const FIXTURES_ROOT = path.join(__dirname, 'fixtures/')
++++++++
++++++++test('dicer-multipart', t => {
++++++++ const tests =
++++++++ [
++++++++ {
++++++++ source: 'nested',
++++++++ opts: { boundary: 'AaB03x' },
++++++++ chsize: 32,
++++++++ nparts: 2,
++++++++ what: 'One nested multipart'
++++++++ },
++++++++ {
++++++++ source: 'many',
++++++++ opts: { boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' },
++++++++ chsize: 16,
++++++++ nparts: 7,
++++++++ what: 'Many parts'
++++++++ },
++++++++ {
++++++++ source: 'many-wrongboundary',
++++++++ opts: { boundary: 'LOLOLOL' },
++++++++ chsize: 8,
++++++++ nparts: 0,
++++++++ dicerError: true,
++++++++ what: 'Many parts, wrong boundary'
++++++++ },
++++++++ {
++++++++ source: 'many-noend',
++++++++ opts: { boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' },
++++++++ chsize: 16,
++++++++ nparts: 7,
++++++++ npartErrors: 1,
++++++++ dicerError: true,
++++++++ what: 'Many parts, end boundary missing, 1 file open'
++++++++ },
++++++++ {
++++++++ source: 'nested-full',
++++++++ opts: { boundary: 'AaB03x', headerFirst: true },
++++++++ chsize: 32,
++++++++ nparts: 2,
++++++++ what: 'One nested multipart with preceding header'
++++++++ },
++++++++ {
++++++++ source: 'nested-full',
++++++++ opts: { headerFirst: true },
++++++++ chsize: 32,
++++++++ nparts: 2,
++++++++ setBoundary: 'AaB03x',
++++++++ what: 'One nested multipart with preceding header, using setBoundary'
++++++++ }
++++++++ ]
++++++++
++++++++ t.plan(tests.length)
++++++++
++++++++ tests.forEach(function (v) {
++++++++ t.test(v.what, t => {
++++++++ t.plan(1)
++++++++ const fixtureBase = FIXTURES_ROOT + v.source
++++++++ const state = { parts: [], preamble: undefined }
++++++++
++++++++ const dicer = new Dicer(v.opts)
++++++++ let error
++++++++ let partErrors = 0
++++++++ let finishes = 0
++++++++
++++++++ dicer.on('preamble', function (p) {
++++++++ const preamble = {
++++++++ body: undefined,
++++++++ bodylen: 0,
++++++++ error: undefined,
++++++++ header: undefined
++++++++ }
++++++++
++++++++ p.on('header', function (h) {
++++++++ preamble.header = h
++++++++ if (v.setBoundary) { dicer.setBoundary(v.setBoundary) }
++++++++ }).on('data', function (data) {
++++++++ // make a copy because we are using readSync which re-uses a buffer ...
++++++++ const copy = Buffer.allocUnsafe(data.length)
++++++++ data.copy(copy)
++++++++ data = copy
++++++++ if (!preamble.body) { preamble.body = [data] } else { preamble.body.push(data) }
++++++++ preamble.bodylen += data.length
++++++++ }).on('error', function (err) {
++++++++ preamble.error = err
++++++++ }).on('end', function () {
++++++++ if (preamble.body) { preamble.body = Buffer.concat(preamble.body, preamble.bodylen) }
++++++++ if (preamble.body || preamble.header) { state.preamble = preamble }
++++++++ })
++++++++ })
++++++++ dicer.on('part', function (p) {
++++++++ const part = {
++++++++ body: undefined,
++++++++ bodylen: 0,
++++++++ error: undefined,
++++++++ header: undefined
++++++++ }
++++++++
++++++++ p.on('header', function (h) {
++++++++ part.header = h
++++++++ }).on('data', function (data) {
++++++++ if (!part.body) { part.body = [data] } else { part.body.push(data) }
++++++++ part.bodylen += data.length
++++++++ }).on('error', function (err) {
++++++++ part.error = err
++++++++ ++partErrors
++++++++ }).on('end', function () {
++++++++ if (part.body) { part.body = Buffer.concat(part.body, part.bodylen) }
++++++++ state.parts.push(part)
++++++++ })
++++++++ }).on('error', function (err) {
++++++++ error = err
++++++++ }).on('finish', function () {
++++++++ assert(finishes++ === 0, makeMsg(v.what, 'finish emitted multiple times'))
++++++++
++++++++ if (v.dicerError) { assert(error !== undefined, makeMsg(v.what, 'Expected error')) } else { assert(error === undefined, makeMsg(v.what, 'Unexpected error: ' + error)) }
++++++++
++++++++ let preamble
++++++++ if (fs.existsSync(fixtureBase + '/preamble')) {
++++++++ const prebody = fs.readFileSync(fixtureBase + '/preamble')
++++++++ if (prebody.length) {
++++++++ preamble = {
++++++++ body: prebody,
++++++++ bodylen: prebody.length,
++++++++ error: undefined,
++++++++ header: undefined
++++++++ }
++++++++ }
++++++++ }
++++++++ if (fs.existsSync(fixtureBase + '/preamble.header')) {
++++++++ const prehead = JSON.parse(fs.readFileSync(fixtureBase +
++++++++ '/preamble.header', 'binary'))
++++++++ if (!preamble) {
++++++++ preamble = {
++++++++ body: undefined,
++++++++ bodylen: 0,
++++++++ error: undefined,
++++++++ header: prehead
++++++++ }
++++++++ } else { preamble.header = prehead }
++++++++ }
++++++++ if (fs.existsSync(fixtureBase + '/preamble.error')) {
++++++++ const err = new Error(fs.readFileSync(fixtureBase +
++++++++ '/preamble.error', 'binary'))
++++++++ if (!preamble) {
++++++++ preamble = {
++++++++ body: undefined,
++++++++ bodylen: 0,
++++++++ error: err,
++++++++ header: undefined
++++++++ }
++++++++ } else { preamble.error = err }
++++++++ }
++++++++
++++++++ assert.deepEqual(state.preamble,
++++++++ preamble,
++++++++ makeMsg(v.what,
++++++++ 'Preamble mismatch:\nActual:' +
++++++++ inspect(state.preamble) +
++++++++ '\nExpected: ' +
++++++++ inspect(preamble)))
++++++++
++++++++ assert.equal(state.parts.length,
++++++++ v.nparts,
++++++++ makeMsg(v.what,
++++++++ 'Part count mismatch:\nActual: ' +
++++++++ state.parts.length +
++++++++ '\nExpected: ' +
++++++++ v.nparts))
++++++++
++++++++ if (!v.npartErrors) { v.npartErrors = 0 }
++++++++ assert.equal(partErrors,
++++++++ v.npartErrors,
++++++++ makeMsg(v.what,
++++++++ 'Part errors mismatch:\nActual: ' +
++++++++ partErrors +
++++++++ '\nExpected: ' +
++++++++ v.npartErrors))
++++++++
++++++++ for (let i = 0, header, body; i < v.nparts; ++i) {
++++++++ if (fs.existsSync(fixtureBase + '/part' + (i + 1))) {
++++++++ body = fs.readFileSync(fixtureBase + '/part' + (i + 1))
++++++++ if (body.length === 0) { body = undefined }
++++++++ } else { body = undefined }
++++++++ assert.deepEqual(state.parts[i].body,
++++++++ body,
++++++++ makeMsg(v.what,
++++++++ 'Part #' + (i + 1) + ' body mismatch'))
++++++++ if (fs.existsSync(fixtureBase + '/part' + (i + 1) + '.header')) {
++++++++ header = fs.readFileSync(fixtureBase +
++++++++ '/part' + (i + 1) + '.header', 'binary')
++++++++ header = JSON.parse(header)
++++++++ } else { header = undefined }
++++++++ assert.deepEqual(state.parts[i].header,
++++++++ header,
++++++++ makeMsg(v.what,
++++++++ 'Part #' + (i + 1) +
++++++++ ' parsed header mismatch:\nActual: ' +
++++++++ inspect(state.parts[i].header) +
++++++++ '\nExpected: ' +
++++++++ inspect(header)))
++++++++ }
++++++++ t.pass()
++++++++ })
++++++++
++++++++ fs.createReadStream(fixtureBase + '/original').pipe(dicer)
++++++++ })
++++++++ })
++++++++})
++++++++
++++++++function makeMsg (what, msg) {
++++++++ return what + ': ' + msg
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="_method"\r
++++++++\r
++++++++put\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[blog]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[public_email]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[interests]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[bio]"\r
++++++++\r
++++++++hello\r
++++++++\r
++++++++"quote"\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="commit"\r
++++++++\r
++++++++Save\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="media"; filename=""\r
++++++++Content-Type: application/octet-stream\r
++++++++\r
++++++++\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++put
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"_method\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[blog]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[public_email]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[interests]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++hello\r
++++++++\r
++++++++"quote"
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[bio]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Save
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"commit\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"media\"; filename=\"\""],\r
++++++++ "content-type": ["application/octet-stream"]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="_method"\r
++++++++\r
++++++++put\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[blog]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[public_email]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[interests]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[bio]"\r
++++++++\r
++++++++hello\r
++++++++\r
++++++++"quote"\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="media"; filename=""\r
++++++++Content-Type: application/octet-stream\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="commit"\r
++++++++\r
++++++++Save\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="_method"\r
++++++++\r
++++++++put\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[blog]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[public_email]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[interests]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[bio]"\r
++++++++\r
++++++++hello\r
++++++++\r
++++++++"quote"\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="media"; filename=""\r
++++++++Content-Type: application/octet-stream\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="commit"\r
++++++++\r
++++++++Save\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Preamble terminated early due to unexpected end of multipart data
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="_method"\r
++++++++\r
++++++++put\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[blog]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[public_email]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[interests]"\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="profile[bio]"\r
++++++++\r
++++++++hello\r
++++++++\r
++++++++"quote"\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="media"; filename=""\r
++++++++Content-Type: application/octet-stream\r
++++++++\r
++++++++\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR\r
++++++++Content-Disposition: form-data; name="commit"\r
++++++++\r
++++++++Save\r
++++++++------WebKitFormBoundaryWLHCs9qmcJJoyjKR--Extra
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++put
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"_method\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[blog]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[public_email]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[interests]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++hello\r
++++++++\r
++++++++"quote"
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"profile[bio]\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"media\"; filename=\"\""],\r
++++++++ "content-type": ["application/octet-stream"]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Save
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"commit\""]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++User-Agent: foo bar baz\r
++++++++Content-Type: multipart/form-data; boundary=AaB03x\r
++++++++\r
++++++++--AaB03x\r
++++++++Content-Disposition: form-data; name="foo"\r
++++++++\r
++++++++bar\r
++++++++--AaB03x\r
++++++++Content-Disposition: form-data; name="files"\r
++++++++Content-Type: multipart/mixed, boundary=BbC04y\r
++++++++\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="file.txt"\r
++++++++Content-Type: text/plain\r
++++++++\r
++++++++contents\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="flowers.jpg"\r
++++++++Content-Type: image/jpeg\r
++++++++Content-Transfer-Encoding: binary\r
++++++++\r
++++++++contents\r
++++++++--BbC04y--\r
++++++++--AaB03x--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++bar
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"foo\""]}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="file.txt"\r
++++++++Content-Type: text/plain\r
++++++++\r
++++++++contents\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="flowers.jpg"\r
++++++++Content-Type: image/jpeg\r
++++++++Content-Transfer-Encoding: binary\r
++++++++\r
++++++++contents\r
++++++++--BbC04y--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"files\""], \r
++++++++ "content-type": ["multipart/mixed, boundary=BbC04y"]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"user-agent": ["foo bar baz"],\r
++++++++ "content-type": ["multipart/form-data; boundary=AaB03x"]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++--AaB03x\r
++++++++Content-Disposition: form-data; name="foo"\r
++++++++\r
++++++++bar\r
++++++++--AaB03x\r
++++++++Content-Disposition: form-data; name="files"\r
++++++++Content-Type: multipart/mixed, boundary=BbC04y\r
++++++++\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="file.txt"\r
++++++++Content-Type: text/plain\r
++++++++\r
++++++++contents\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="flowers.jpg"\r
++++++++Content-Type: image/jpeg\r
++++++++Content-Transfer-Encoding: binary\r
++++++++\r
++++++++contents\r
++++++++--BbC04y--\r
++++++++--AaB03x--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++bar
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"foo\""]}\r
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="file.txt"\r
++++++++Content-Type: text/plain\r
++++++++\r
++++++++contents\r
++++++++--BbC04y\r
++++++++Content-Disposition: attachment; filename="flowers.jpg"\r
++++++++Content-Type: image/jpeg\r
++++++++Content-Transfer-Encoding: binary\r
++++++++\r
++++++++contents\r
++++++++--BbC04y--
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{"content-disposition": ["form-data; name=\"files\""], \r
++++++++ "content-type": ["multipart/mixed, boundary=BbC04y"]}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const getLimit = require('../lib/utils/getLimit')
++++++++const { test } = require('tap')
++++++++
++++++++test('Get limit', t => {
++++++++ t.plan(2)
++++++++
++++++++ t.test('Correctly resolves limits', t => {
++++++++ t.plan(8)
++++++++ t.strictSame(getLimit(undefined, 'fieldSize', 1), 1)
++++++++ t.strictSame(getLimit(undefined, 'fileSize', Infinity), Infinity)
++++++++
++++++++ t.strictSame(getLimit({}, 'fieldSize', 1), 1)
++++++++ t.strictSame(getLimit({}, 'fileSize', Infinity), Infinity)
++++++++ t.strictSame(getLimit({ fieldSize: null }, 'fieldSize', 1), 1)
++++++++ t.strictSame(getLimit({ fileSize: null }, 'fileSize', Infinity), Infinity)
++++++++
++++++++ t.strictSame(getLimit({ fieldSize: 0 }, 'fieldSize', 1), 0)
++++++++ t.strictSame(getLimit({ fileSize: 2 }, 'fileSize', 1), 2)
++++++++ })
++++++++
++++++++ t.test('Throws an error on incorrect limits', t => {
++++++++ t.plan(2)
++++++++
++++++++ t.throws(function () {
++++++++ getLimit({ fieldSize: '1' }, 'fieldSize', 1)
++++++++ }, new Error('Limit fieldSize is not a valid number'))
++++++++
++++++++ t.throws(function () {
++++++++ getLimit({ fieldSize: NaN }, 'fieldSize', 1)
++++++++ }, new Error('Limit fieldSize is not a valid number'))
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { inspect } = require('util')
++++++++const { test } = require('tap')
++++++++
++++++++const Busboy = require('..')
++++++++
++++++++const BOUNDARY = 'u2KxIV5yF1y+xUspOQCCZopaVgeV6Jxihv35XQJmuTx8X3sh'
++++++++
++++++++function formDataSection (key, value) {
++++++++ return Buffer.from('\r\n--' + BOUNDARY +
++++++++ '\r\nContent-Disposition: form-data; name="' +
++++++++ key + '"\r\n\r\n' + value)
++++++++}
++++++++function formDataFile (key, filename, contentType) {
++++++++ return Buffer.concat([
++++++++ Buffer.from('\r\n--' + BOUNDARY + '\r\n'),
++++++++ Buffer.from('Content-Disposition: form-data; name="' +
++++++++ key + '"; filename="' + filename + '"\r\n'),
++++++++ Buffer.from('Content-Type: ' + contentType + '\r\n\r\n'),
++++++++ Buffer.allocUnsafe(100000)
++++++++ ])
++++++++}
++++++++
++++++++test('multipart-stream-pause - processes stream correctly', t => {
++++++++ t.plan(6)
++++++++ const reqChunks = [
++++++++ Buffer.concat([
++++++++ formDataFile('file', 'file.bin', 'application/octet-stream'),
++++++++ formDataSection('foo', 'foo value')
++++++++ ]),
++++++++ formDataSection('bar', 'bar value'),
++++++++ Buffer.from('\r\n--' + BOUNDARY + '--\r\n')
++++++++ ]
++++++++ const busboy = new Busboy({
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + BOUNDARY
++++++++ }
++++++++ })
++++++++ let finishes = 0
++++++++ const results = []
++++++++ const expected = [
++++++++ ['file', 'file', 'file.bin', '7bit', 'application/octet-stream'],
++++++++ ['field', 'foo', 'foo value', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'bar', 'bar value', false, false, '7bit', 'text/plain']
++++++++ ]
++++++++
++++++++ busboy.on('field', function (key, val, keyTrunc, valTrunc, encoding, contype) {
++++++++ results.push(['field', key, val, keyTrunc, valTrunc, encoding, contype])
++++++++ })
++++++++ busboy.on('file', function (fieldname, stream, filename, encoding, mimeType) {
++++++++ results.push(['file', fieldname, filename, encoding, mimeType])
++++++++ // Simulate a pipe where the destination is pausing (perhaps due to waiting
++++++++ // for file system write to finish)
++++++++ setTimeout(function () {
++++++++ stream.resume()
++++++++ }, 10)
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ t.ok(finishes++ === 0, 'finish emitted multiple times')
++++++++ t.strictSame(results.length,
++++++++ expected.length,
++++++++ 'Parsed result count mismatch. Saw ' +
++++++++ results.length +
++++++++ '. Expected: ' + expected.length)
++++++++
++++++++ results.forEach(function (result, i) {
++++++++ t.strictSame(result,
++++++++ expected[i],
++++++++ 'Result mismatch:\nParsed: ' + inspect(result) +
++++++++ '\nExpected: ' + inspect(expected[i]))
++++++++ })
++++++++ t.pass()
++++++++ }).on('error', function (err) {
++++++++ t.error(err)
++++++++ })
++++++++
++++++++ reqChunks.forEach(function (buf) {
++++++++ busboy.write(buf)
++++++++ })
++++++++ busboy.end()
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { inspect } = require('node:util')
++++++++const { test } = require('tap')
++++++++const parseParams = require('../lib/utils/parseParams')
++++++++
++++++++test('parse-params', t => {
++++++++ const tests = [
++++++++ {
++++++++ source: 'video/ogg',
++++++++ expected: ['video/ogg'],
++++++++ what: 'No parameters'
++++++++ },
++++++++ {
++++++++ source: 'video/ogg;',
++++++++ expected: ['video/ogg'],
++++++++ what: 'No parameters (with separator)'
++++++++ },
++++++++ {
++++++++ source: 'video/ogg; ',
++++++++ expected: ['video/ogg'],
++++++++ what: 'No parameters (with separator followed by whitespace)'
++++++++ },
++++++++ {
++++++++ source: ';video/ogg',
++++++++ expected: ['', 'video/ogg'],
++++++++ what: 'Empty parameter'
++++++++ },
++++++++ {
++++++++ source: 'video/*',
++++++++ expected: ['video/*'],
++++++++ what: 'Subtype with asterisk'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; encoding=utf8',
++++++++ expected: ['text/plain', ['encoding', 'utf8']],
++++++++ what: 'Unquoted'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; encoding=',
++++++++ expected: ['text/plain', ['encoding', '']],
++++++++ what: 'Unquoted empty string'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; encoding="utf8"',
++++++++ expected: ['text/plain', ['encoding', 'utf8']],
++++++++ what: 'Quoted'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; greeting="hello \\"world\\""',
++++++++ expected: ['text/plain', ['greeting', 'hello "world"']],
++++++++ what: 'Quotes within quoted'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; encoding=""',
++++++++ expected: ['text/plain', ['encoding', '']],
++++++++ what: 'Quoted empty string'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; encoding="utf8";\t foo=bar;test',
++++++++ expected: ['text/plain', ['encoding', 'utf8'], ['foo', 'bar'], 'test'],
++++++++ what: 'Multiple params with various spacing'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates",
++++++++ expected: ['text/plain', ['filename', '£ rates']],
++++++++ what: 'Extended parameter (RFC 5987) with language'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
++++++++ expected: ['text/plain', ['filename', '£ and € rates']],
++++++++ what: 'Extended parameter (RFC 5987) without language'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename*=utf-8''%E6%B5%8B%E8%AF%95%E6%96%87%E6%A1%A3",
++++++++ expected: ['text/plain', ['filename', '测试文档']],
++++++++ what: 'Extended parameter (RFC 5987) without language #2'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename*=utf-8''%c2%a3%20and%20%e2%82%ac%20rates",
++++++++ expected: ['text/plain', ['filename', '£ rates'], ['altfilename', '£ and € rates']],
++++++++ what: 'Multiple extended parameters (RFC 5987) with mixed charsets'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename*=iso-8859-1'en'%A3%20rates; altfilename=\"foobarbaz\"",
++++++++ expected: ['text/plain', ['filename', '£ rates'], ['altfilename', 'foobarbaz']],
++++++++ what: 'Mixed regular and extended parameters (RFC 5987)'
++++++++ },
++++++++ {
++++++++ source: "text/plain; filename=\"foobarbaz\"; altfilename*=iso-8859-1'en'%A3%20rates",
++++++++ expected: ['text/plain', ['filename', 'foobarbaz'], ['altfilename', '£ rates']],
++++++++ what: 'Mixed regular and extended parameters (RFC 5987) #2'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; filename="C:\\folder\\test.png"',
++++++++ expected: ['text/plain', ['filename', 'C:\\folder\\test.png']],
++++++++ what: 'Unescaped backslashes should be considered backslashes'
++++++++ },
++++++++ {
++++++++ source: 'text/plain; filename="John \\"Magic\\" Smith.png"',
++++++++ expected: ['text/plain', ['filename', 'John "Magic" Smith.png']],
++++++++ what: 'Escaped double-quotes should be considered double-quotes'
++++++++ },
++++++++ {
++++++++ source: 'multipart/form-data; charset=utf-8; boundary=0xKhTmLbOuNdArY',
++++++++ expected: ['multipart/form-data', ['charset', 'utf-8'], ['boundary', '0xKhTmLbOuNdArY']],
++++++++ what: 'Multiple non-quoted parameters'
++++++++ }
++++++++ ]
++++++++
++++++++ t.plan(tests.length)
++++++++
++++++++ tests.forEach((v) => {
++++++++ t.test(v.what, t => {
++++++++ t.plan(1)
++++++++
++++++++ const result = parseParams(v.source)
++++++++ t.strictSame(
++++++++ result,
++++++++ v.expected,
++++++++ `parsed parameters match.\nSaw: ${inspect(result)}\nExpected: ${inspect(v.expected)}`)
++++++++ })
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { test } = require('tap')
++++++++const Streamsearch = require('../deps/streamsearch/sbmh')
++++++++
++++++++test('streamsearch', t => {
++++++++ t.plan(17)
++++++++
++++++++ t.test('should throw an error if the needle is not a String or Buffer', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Streamsearch(2), new Error('The needle has to be a String or a Buffer.'))
++++++++ })
++++++++ t.test('should throw an error if the needle is an empty String', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Streamsearch(''), new Error('The needle cannot be an empty String/Buffer.'))
++++++++ })
++++++++ t.test('should throw an error if the needle is an empty Buffer', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Streamsearch(Buffer.from('')), new Error('The needle cannot be an empty String/Buffer.'))
++++++++ })
++++++++ t.test('should throw an error if the needle is bigger than 256 characters', t => {
++++++++ t.plan(1)
++++++++
++++++++ t.throws(() => new Streamsearch(Buffer.from(Array(257).fill('a').join(''))), new Error('The needle cannot have a length bigger than 256.'))
++++++++ })
++++++++
++++++++ t.test('should process a Buffer without a needle', t => {
++++++++ t.plan(5)
++++++++ const expected = [
++++++++ [false, Buffer.from('bar hello'), 0, 9]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 1) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should cast a string without a needle', t => {
++++++++ t.plan(5)
++++++++
++++++++ const expected = [
++++++++ [false, Buffer.from('bar hello'), 0, 9]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ 'bar hello'
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 1) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should process a chunk with a needle at the beginning', t => {
++++++++ t.plan(9)
++++++++
++++++++ const expected = [
++++++++ [true, undefined, undefined, undefined],
++++++++ [false, Buffer.from('\r\nbar hello'), 2, 11]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('\r\nbar hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 2) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should process a chunk with a needle in the middle', t => {
++++++++ t.plan(9)
++++++++ const expected = [
++++++++ [true, Buffer.from('bar\r\n hello'), 0, 3],
++++++++ [false, Buffer.from('bar\r\n hello'), 5, 11]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r\n hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 2) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should process a chunk with a needle at the end', t => {
++++++++ t.plan(5)
++++++++ const expected = [
++++++++ [true, Buffer.from('bar hello\r\n'), 0, 9]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar hello\r\n')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 1) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should process a chunk with multiple needle at the end', t => {
++++++++ t.plan(9)
++++++++ const expected = [
++++++++ [true, Buffer.from('bar hello\r\n\r\n'), 0, 9],
++++++++ [true, Buffer.from('bar hello\r\n\r\n'), 11, 11]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar hello\r\n\r\n')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 2) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ })
++++++++
++++++++ t.test('should process two chunks without a needle', t => {
++++++++ t.plan(9)
++++++++ const expected = [
++++++++ [false, Buffer.from('bar'), 0, 3],
++++++++ [false, Buffer.from('hello'), 0, 5]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar'),
++++++++ Buffer.from('hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 2) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ })
++++++++
++++++++ t.test('should process two chunks with an overflowing needle', t => {
++++++++ t.plan(13)
++++++++ const expected = [
++++++++ [false, Buffer.from('bar\r'), 0, 3],
++++++++ [true, undefined, undefined, undefined],
++++++++ [false, Buffer.from('\nhello'), 1, 6]
++++++++ ]
++++++++ const needle = '\r\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r'),
++++++++ Buffer.from('\nhello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 3) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ })
++++++++
++++++++ t.test('should process two chunks with a potentially overflowing needle', t => {
++++++++ t.plan(13)
++++++++
++++++++ const expected = [
++++++++ [false, Buffer.from('bar\r'), 0, 3],
++++++++ [false, Buffer.from('\r\0\0'), 0, 1],
++++++++ [false, Buffer.from('\n\r\nhello'), 0, 8]
++++++++ ]
++++++++ const needle = '\r\n\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r'),
++++++++ Buffer.from('\n\r\nhello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 3) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ })
++++++++
++++++++ t.test('should process three chunks with a overflowing needle', t => {
++++++++ t.plan(13)
++++++++
++++++++ const expected = [
++++++++ [false, Buffer.from('bar\r'), 0, 3],
++++++++ [true, undefined, undefined, undefined],
++++++++ [false, Buffer.from('\nhello'), 1, 6]
++++++++ ]
++++++++ const needle = '\r\n\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r'),
++++++++ Buffer.from('\n'),
++++++++ Buffer.from('\nhello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 3) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ s.push(chunks[2])
++++++++ })
++++++++
++++++++ t.test('should process four chunks with a overflowing needle', t => {
++++++++ t.plan(13)
++++++++
++++++++ const expected = [
++++++++ [false, Buffer.from('bar\r'), 0, 3],
++++++++ [true, undefined, undefined, undefined],
++++++++ [false, Buffer.from('hello'), 0, 5]
++++++++ ]
++++++++ const needle = '\r\n\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r'),
++++++++ Buffer.from('\n'),
++++++++ Buffer.from('\n'),
++++++++ Buffer.from('hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 3) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ s.push(chunks[2])
++++++++ s.push(chunks[3])
++++++++ })
++++++++
++++++++ t.test('should process four chunks with a potentially overflowing needle', t => {
++++++++ t.plan(17)
++++++++
++++++++ const expected = [
++++++++ [false, Buffer.from('bar\r'), 0, 3],
++++++++ [false, Buffer.from('\r\n\0'), 0, 2],
++++++++ [false, Buffer.from('\r\n\0'), 0, 1],
++++++++ [false, Buffer.from('hello'), 0, 5]
++++++++ ]
++++++++ const needle = '\r\n\n'
++++++++ const s = new Streamsearch(needle)
++++++++ const chunks = [
++++++++ Buffer.from('bar\r'),
++++++++ Buffer.from('\n'),
++++++++ Buffer.from('\r'),
++++++++ Buffer.from('hello')
++++++++ ]
++++++++ let i = 0
++++++++ s.on('info', (isMatched, data, start, end) => {
++++++++ t.strictSame(isMatched, expected[i][0])
++++++++ t.strictSame(data, expected[i][1])
++++++++ t.strictSame(start, expected[i][2])
++++++++ t.strictSame(end, expected[i][3])
++++++++ i++
++++++++ if (i >= 4) {
++++++++ t.pass()
++++++++ }
++++++++ })
++++++++
++++++++ s.push(chunks[0])
++++++++ s.push(chunks[1])
++++++++ s.push(chunks[2])
++++++++ s.push(chunks[3])
++++++++ })
++++++++
++++++++ t.test('should reset the internal values if .reset() is called', t => {
++++++++ t.plan(9)
++++++++
++++++++ const s = new Streamsearch('test')
++++++++
++++++++ t.strictSame(s._lookbehind_size, 0)
++++++++ t.strictSame(s.matches, 0)
++++++++ t.strictSame(s._bufpos, 0)
++++++++
++++++++ s._lookbehind_size = 1
++++++++ s._bufpos = 1
++++++++ s.matches = 1
++++++++
++++++++ t.strictSame(s._lookbehind_size, 1)
++++++++ t.strictSame(s.matches, 1)
++++++++ t.strictSame(s._bufpos, 1)
++++++++
++++++++ s.reset()
++++++++
++++++++ t.strictSame(s._lookbehind_size, 0)
++++++++ t.strictSame(s.matches, 0)
++++++++ t.strictSame(s._bufpos, 0)
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const Busboy = require('..')
++++++++
++++++++const { test } = require('tap')
++++++++const { inspect } = require('util')
++++++++
++++++++const EMPTY_FN = function () {
++++++++}
++++++++
++++++++const tests = [
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_1"',
++++++++ '',
++++++++ 'super beta file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_1"; filename="1k_b.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'file_name_1', 'super beta file', false, false, '7bit', 'text/plain'],
++++++++ ['file', 'upload_file_0', 1023, 0, '1k_a.dat', '7bit', 'application/octet-stream'],
++++++++ ['file', 'upload_file_1', 1023, 0, '1k_b.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Fields and files',
++++++++ plan: 11
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: form-data; name="cont"',
++++++++ '',
++++++++ 'some random content',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: form-data; name="pass"',
++++++++ '',
++++++++ 'some random pass',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: form-data; name="bit"',
++++++++ '',
++++++++ '2',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [
++++++++ ['field', 'cont', 'some random content', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'pass', 'some random pass', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'bit', '2', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'Fields only',
++++++++ plan: 6
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ''
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [],
++++++++ shouldError: 'Unexpected end of multipart data',
++++++++ what: 'No fields and no files',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ fileSize: 13,
++++++++ fieldSize: 5
++++++++ },
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super', false, true, '7bit', 'text/plain'],
++++++++ ['file', 'upload_file_0', 13, 2, '1k_a.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Fields and files (limits)',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ fields: 0
++++++++ },
++++++++ events: ['file'],
++++++++ expected: [
++++++++ ['file', 'upload_file_0', 26, 0, '1k_a.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'should not emit fieldsLimit if no field was sent',
++++++++ plan: 6
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ fields: 0
++++++++ },
++++++++ events: ['file', 'fieldsLimit'],
++++++++ expected: [
++++++++ ['file', 'upload_file_0', 26, 0, '1k_a.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'should respect fields limit of 0',
++++++++ plan: 6
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_1"',
++++++++ '',
++++++++ 'super beta file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ fields: 1
++++++++ },
++++++++ events: ['field', 'file', 'fieldsLimit'],
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain'],
++++++++ ['file', 'upload_file_0', 26, 0, '1k_a.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'should respect fields limit of 7',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ files: 0
++++++++ },
++++++++ events: ['field'],
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'should not emit filesLimit if no file was sent',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ files: 0
++++++++ },
++++++++ events: ['field', 'filesLimit'],
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'should respect fields limit of 0',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_b"; filename="1k_b.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ limits: {
++++++++ files: 1
++++++++ },
++++++++ events: ['field', 'file', 'filesLimit'],
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain'],
++++++++ ['file', 'upload_file_0', 26, 0, '1k_a.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'should respect fields limit of 1',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_0"',
++++++++ '',
++++++++ 'super alpha file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file_name_1"',
++++++++ '',
++++++++ 'super beta file',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_1"; filename="1k_b.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['field', 'file_name_0', 'super alpha file', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'file_name_1', 'super beta file', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ events: ['field'],
++++++++ what: 'Fields and (ignored) files',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="/tmp/1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_1"; filename="C:\\files\\1k_b.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_2"; filename="relative/1k_c.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['file', 'upload_file_0', 26, 0, '1k_a.dat', '7bit', 'application/octet-stream'],
++++++++ ['file', 'upload_file_1', 26, 0, '1k_b.dat', '7bit', 'application/octet-stream'],
++++++++ ['file', 'upload_file_2', 26, 0, '1k_c.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Files with filenames containing paths',
++++++++ plan: 12
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="/absolute/1k_a.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_1"; filename="C:\\absolute\\1k_b.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_2"; filename="relative/1k_c.dat"',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ preservePath: true,
++++++++ expected: [
++++++++ ['file', 'upload_file_0', 26, 0, '/absolute/1k_a.dat', '7bit', 'application/octet-stream'],
++++++++ ['file', 'upload_file_1', 26, 0, 'C:\\absolute\\1k_b.dat', '7bit', 'application/octet-stream'],
++++++++ ['file', 'upload_file_2', 26, 0, 'relative/1k_c.dat', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Paths to be preserved through the preservePath option',
++++++++ plan: 12
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: form-data; name="cont"',
++++++++ 'Content-Type: ',
++++++++ '',
++++++++ 'some random content',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: ',
++++++++ '',
++++++++ 'some random pass',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [
++++++++ ['field', 'cont', 'some random content', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'Empty content-type and empty content-disposition',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ config: {
++++++++ isPartAFile: (fieldName) => (fieldName !== 'upload_file_0')
++++++++ },
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="blob"',
++++++++ 'Content-Type: application/json',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['field', 'upload_file_0', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', false, false, '7bit', 'application/json']
++++++++ ],
++++++++ what: 'Blob uploads should be handled as fields if isPartAFile is provided.',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ config: {
++++++++ isPartAFile: (fieldName) => (fieldName !== 'upload_file_0')
++++++++ },
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="blob"',
++++++++ 'Content-Type: application/json',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file"; filename*=utf-8\'\'n%C3%A4me.txt',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['field', 'upload_file_0', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', false, false, '7bit', 'application/json'],
++++++++ ['file', 'file', 26, 0, 'näme.txt', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Blob uploads should be handled as fields if isPartAFile is provided. Other parts should be files.',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ config: {
++++++++ isPartAFile: (fieldName) => (fieldName === 'upload_file_0')
++++++++ },
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="upload_file_0"; filename="blob"',
++++++++ 'Content-Type: application/json',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file"; filename*=utf-8\'\'n%C3%A4me.txt',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['file', 'upload_file_0', 26, 0, 'blob', '7bit', 'application/json'],
++++++++ ['field', 'file', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', false, false, '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Blob uploads sould be handled as files if corresponding isPartAFile is provided. Other parts should be fields.',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="file"; filename*=utf-8\'\'n%C3%A4me.txt',
++++++++ 'Content-Type: application/octet-stream',
++++++++ '',
++++++++ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['file', 'file', 26, 0, 'näme.txt', '7bit', 'application/octet-stream']
++++++++ ],
++++++++ what: 'Unicode filenames',
++++++++ plan: 6
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['--asdasdasdasd\r\n',
++++++++ 'Content-Type: text/plain\r\n',
++++++++ 'Content-Disposition: form-data; name="foo"\r\n',
++++++++ '\r\n',
++++++++ 'asd\r\n',
++++++++ '--asdasdasdasd--'
++++++++ ].join(':)')
++++++++ ],
++++++++ boundary: 'asdasdasdasd',
++++++++ expected: [],
++++++++ shouldError: 'Unexpected end of multipart data',
++++++++ what: 'Stopped mid-header',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['------WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ 'Content-Disposition: form-data; name="cont"',
++++++++ 'Content-Type: application/json',
++++++++ '',
++++++++ '{}',
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [
++++++++ ['field', 'cont', '{}', false, false, '7bit', 'application/json']
++++++++ ],
++++++++ what: 'content-type for fields',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: [
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [],
++++++++ what: 'empty form',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ['-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="field1"',
++++++++ 'content-type: text/plain; charset=utf-8',
++++++++ '',
++++++++ 'Aufklärung ist der Ausgang des Menschen aus seiner selbstverschuldeten Unmündigkeit.',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ 'Content-Disposition: form-data; name="field2"',
++++++++ 'content-type: text/plain; charset=iso-8859-1',
++++++++ '',
++++++++ 'sapere aude!',
++++++++ '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
++++++++ ].join('\r\n')
++++++++ ],
++++++++ boundary: '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
++++++++ expected: [
++++++++ ['field', 'field1', 'Aufklärung ist der Ausgang des Menschen aus seiner selbstverschuldeten Unmündigkeit.', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'field2', 'sapere aude!', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'Fields and files',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: [[
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="regsubmit"',
++++++++ '',
++++++++ 'yes',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="referer"',
++++++++ '',
++++++++ 'http://domainExample/./',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="activationauth"',
++++++++ '',
++++++++ '',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="seccodemodid"',
++++++++ '',
++++++++ 'member::register',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7--'].join('\r\n')
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ expected: [
++++++++ ['field', 'regsubmit', 'yes', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'referer', 'http://domainExample/./', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'activationauth', '', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'seccodemodid', 'member::register', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'one empty part should get ignored',
++++++++ plan: 7
++++++++ },
++++++++ {
++++++++ source: [
++++++++ ' ------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhY',
++++++++ expected: [],
++++++++ shouldError: 'Unexpected end of multipart data',
++++++++ what: 'empty form with preceding whitespace',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: [
++++++++ '------WebKitFormBoundaryTB2MiQ36fnSJlrhY--\r\n'
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryTB2MiQ36fnSJlrhYY',
++++++++ expected: [],
++++++++ shouldError: 'Unexpected end of multipart data',
++++++++ what: 'empty form with wrong boundary (extra Y)',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: [[
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="regsubmit"',
++++++++ '',
++++++++ 'yes',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="referer"',
++++++++ '',
++++++++ 'http://domainExample/./',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="activationauth"',
++++++++ '',
++++++++ '',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ 'Content-Disposition: form-data; name="seccodemodid"',
++++++++ '',
++++++++ 'member::register',
++++++++ '------WebKitFormBoundaryzca7IDMnT6QwqBp7--'].join('\r\n')
++++++++ ],
++++++++ boundary: '----WebKitFormBoundaryzca7IDMnT6QwqBp7',
++++++++ expected: [
++++++++ ['field', 'regsubmit', 'yes', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'referer', 'http://domainExample/./', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'activationauth', '', false, false, '7bit', 'text/plain'],
++++++++ ['field', 'seccodemodid', 'member::register', false, false, '7bit', 'text/plain']
++++++++ ],
++++++++ what: 'multiple empty parts should get ignored',
++++++++ plan: 7
++++++++ }
++++++++]
++++++++
++++++++tests.forEach((v) => {
++++++++ test(v.what, t => {
++++++++ t.plan(v.plan)
++++++++ const busboy = new Busboy({
++++++++ ...v.config,
++++++++ limits: v.limits,
++++++++ preservePath: v.preservePath,
++++++++ headers: {
++++++++ 'content-type': 'multipart/form-data; boundary=' + v.boundary
++++++++ }
++++++++ })
++++++++ let finishes = 0
++++++++ const results = []
++++++++
++++++++ if (v.events === undefined || v.events.indexOf('field') > -1) {
++++++++ busboy.on('field', function (key, val, keyTrunc, valTrunc, encoding, contype) {
++++++++ results.push(['field', key, val, keyTrunc, valTrunc, encoding, contype])
++++++++ })
++++++++ }
++++++++ if (v.events === undefined || v.events.indexOf('file') > -1) {
++++++++ busboy.on('file', function (fieldname, stream, filename, encoding, mimeType) {
++++++++ let nb = 0
++++++++ const info = ['file',
++++++++ fieldname,
++++++++ nb,
++++++++ 0,
++++++++ filename,
++++++++ encoding,
++++++++ mimeType]
++++++++ results.push(info)
++++++++ stream.on('data', function (d) {
++++++++ nb += d.length
++++++++ }).on('limit', function () {
++++++++ ++info[3]
++++++++ }).on('end', function () {
++++++++ info[2] = nb
++++++++ t.ok(typeof (stream.bytesRead) === 'number', 'file.bytesRead is missing')
++++++++ t.ok(stream.bytesRead === nb, 'file.bytesRead is not equal to filesize')
++++++++ if (stream.truncated) { ++info[3] }
++++++++ })
++++++++ })
++++++++ }
++++++++ busboy.on('finish', function () {
++++++++ t.ok(finishes++ === 0, 'finish emitted multiple times')
++++++++ t.equal(results.length,
++++++++ v.expected.length,
++++++++ 'Parsed result count mismatch. Saw ' +
++++++++ results.length +
++++++++ '. Expected: ' + v.expected.length)
++++++++
++++++++ results.forEach(function (result, i) {
++++++++ t.strictSame(result,
++++++++ v.expected[i],
++++++++ 'Result mismatch:\nParsed: ' + inspect(result) +
++++++++ '\nExpected: ' + inspect(v.expected[i])
++++++++ )
++++++++ })
++++++++ t.pass()
++++++++ }).on('error', function (err) {
++++++++ if (!v.shouldError || v.shouldError !== err.message) { t.error(err) }
++++++++ })
++++++++
++++++++ v.source.forEach(function (s) {
++++++++ busboy.write(Buffer.from(s, 'utf8'), EMPTY_FN)
++++++++ })
++++++++ busboy.end()
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++'use strict'
++++++++
++++++++const { inspect } = require('util')
++++++++const Busboy = require('..')
++++++++const { test } = require('tap')
++++++++
++++++++const EMPTY_FN = function () {
++++++++}
++++++++
++++++++const tests = [
++++++++ {
++++++++ source: ['foo'],
++++++++ expected: [['foo', '', false, false]],
++++++++ what: 'Unassigned value',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo=bar'],
++++++++ expected: [['foo', 'bar', false, false]],
++++++++ what: 'Assigned value',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo&bar=baz'],
++++++++ expected: [['foo', '', false, false],
++++++++ ['bar', 'baz', false, false]],
++++++++ what: 'Unassigned and assigned value',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz'],
++++++++ expected: [['foo', 'bar', false, false],
++++++++ ['baz', '', false, false]],
++++++++ what: 'Assigned and unassigned value',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['foo', 'bar', false, false],
++++++++ ['baz', 'bla', false, false]],
++++++++ what: 'Two assigned values',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo&bar'],
++++++++ expected: [['foo', '', false, false],
++++++++ ['bar', '', false, false]],
++++++++ what: 'Two unassigned values',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo&bar&'],
++++++++ expected: [['foo', '', false, false],
++++++++ ['bar', '', false, false]],
++++++++ what: 'Two unassigned values and ampersand',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar+baz%2Bquux'],
++++++++ expected: [['foo', 'bar baz+quux', false, false]],
++++++++ what: 'Assigned value with (plus) space',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo=bar%20baz%21'],
++++++++ expected: [['foo', 'bar baz!', false, false]],
++++++++ what: 'Assigned value with encoded bytes',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo%20bar=baz%20bla%21'],
++++++++ expected: [['foo bar', 'baz bla!', false, false]],
++++++++ what: 'Assigned value with encoded bytes #2',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo=bar%20baz%21&num=1000'],
++++++++ expected: [['foo', 'bar baz!', false, false],
++++++++ ['num', '1000', false, false]],
++++++++ what: 'Two assigned values, one with encoded bytes',
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [],
++++++++ what: 'Limits: zero fields',
++++++++ limits: { fields: 0 },
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['foo', 'bar', false, false]],
++++++++ what: 'Limits: one field',
++++++++ limits: { fields: 1 },
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['foo', 'bar', false, false],
++++++++ ['baz', 'bla', false, false]],
++++++++ what: 'Limits: field part lengths match limits',
++++++++ limits: { fieldNameSize: 3, fieldSize: 3 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['fo', 'bar', true, false],
++++++++ ['ba', 'bla', true, false]],
++++++++ what: 'Limits: truncated field name',
++++++++ limits: { fieldNameSize: 2 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['foo', 'ba', false, true],
++++++++ ['baz', 'bl', false, true]],
++++++++ what: 'Limits: truncated field value',
++++++++ limits: { fieldSize: 2 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['fo', 'ba', true, true],
++++++++ ['ba', 'bl', true, true]],
++++++++ what: 'Limits: truncated field name and value',
++++++++ limits: { fieldNameSize: 2, fieldSize: 2 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['fo', '', true, true],
++++++++ ['ba', '', true, true]],
++++++++ what: 'Limits: truncated field name and zero value limit',
++++++++ limits: { fieldNameSize: 2, fieldSize: 0 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['foo=bar&baz=bla'],
++++++++ expected: [['', '', true, true],
++++++++ ['', '', true, true]],
++++++++ what: 'Limits: truncated zero field name and zero value limit',
++++++++ limits: { fieldNameSize: 0, fieldSize: 0 },
++++++++ plan: 5
++++++++ },
++++++++ {
++++++++ source: ['&'],
++++++++ expected: [],
++++++++ what: 'Ampersand',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: ['&&&&&'],
++++++++ expected: [],
++++++++ what: 'Many ampersands',
++++++++ plan: 3
++++++++ },
++++++++ {
++++++++ source: ['='],
++++++++ expected: [['', '', false, false]],
++++++++ what: 'Assigned value, empty name and value',
++++++++ plan: 4
++++++++ },
++++++++ {
++++++++ source: [''],
++++++++ expected: [],
++++++++ what: 'Nothing',
++++++++ plan: 3
++++++++ }
++++++++]
++++++++
++++++++tests.forEach((v) => {
++++++++ test(v.what, t => {
++++++++ t.plan(v.plan || 20)
++++++++ const busboy = new Busboy({
++++++++ limits: v.limits,
++++++++ headers: {
++++++++ 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
++++++++ }
++++++++ })
++++++++ let finishes = 0
++++++++ const results = []
++++++++
++++++++ busboy.on('field', function (key, val, keyTrunc, valTrunc) {
++++++++ results.push([key, val, keyTrunc, valTrunc])
++++++++ })
++++++++ busboy.on('file', function () {
++++++++ throw new Error('Unexpected file')
++++++++ })
++++++++ busboy.on('finish', function () {
++++++++ t.ok(finishes++ === 0, 'finish emitted multiple times')
++++++++ t.equal(results.length, v.expected.length)
++++++++
++++++++ let i = 0
++++++++ results.forEach(function (result) {
++++++++ t.strictSame(result,
++++++++ v.expected[i],
++++++++ 'Result mismatch:\nParsed: ' + inspect(result) +
++++++++ '\nExpected: ' + inspect(v.expected[i])
++++++++ )
++++++++ ++i
++++++++ })
++++++++ t.pass()
++++++++ })
++++++++
++++++++ v.source.forEach(function (s) {
++++++++ busboy.write(Buffer.from(s, 'utf8'), EMPTY_FN)
++++++++ })
++++++++ busboy.end()
++++++++ })
++++++++})
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Dicer } from "../../lib/main";
++++++++import * as fs from "fs";
++++++++import * as stream from "stream";
++++++++
++++++++function testDicerSyntax() {
++++++++ const opts: Dicer.Config = {
++++++++ boundary: "testing",
++++++++ };
++++++++ const dicer = new Dicer(opts);
++++++++ const opts2: Dicer.Config = {
++++++++ headerFirst: true,
++++++++ maxHeaderPairs: 1,
++++++++ };
++++++++ const opts3: Dicer.Config = {
++++++++ boundary: "more-testing",
++++++++ headerFirst: false,
++++++++ maxHeaderPairs: 8,
++++++++ };
++++++++ dicer.setBoundary("new-testing-boundary");
++++++++ dicer.on("part", handleDicerPartStream);
++++++++ dicer.on("finish", () => {
++++++++ console.log("dicer parsing finished");
++++++++ });
++++++++ dicer.on("preamble", part => {
++++++++ console.log("dicer preamble to new part");
++++++++ });
++++++++ dicer.on("trailer", data => {
++++++++ console.log(`dicer trailing data found: ${data.length} bytes`);
++++++++ });
++++++++ dicer.on("close", () => {
++++++++ console.log("dicer close");
++++++++ });
++++++++ dicer.on("drain", () => {
++++++++ console.log("dicer drain");
++++++++ });
++++++++ dicer.on("error", err => {
++++++++ console.error(`dicer error: ${err.message || JSON.stringify(err)}`);
++++++++ });
++++++++ dicer.on("finish", () => {
++++++++ console.log("dicer finish");
++++++++ });
++++++++ dicer.on("pipe", (src: stream.Readable) => {
++++++++ console.log("dicer pipe");
++++++++ });
++++++++ dicer.on("unpipe", (src: stream.Readable) => {
++++++++ console.log("dicer unpipe");
++++++++ });
++++++++ const inputFileStream = fs.createReadStream("in-test-file.txt");
++++++++ inputFileStream.pipe(dicer);
++++++++}
++++++++/**
++++++++ * Handle a part found by a Dicer parser
++++++++ *
++++++++ * @param part Part found
++++++++ */
++++++++function handleDicerPartStream(part: Dicer.PartStream) {
++++++++ console.log("dicer part found");
++++++++ const outputFileStream = fs.createWriteStream("out-test-file.txt");
++++++++ part.on("readable", () => {
++++++++ console.log("part readable");
++++++++ });
++++++++ part.on("header", header => {
++++++++ console.log(`part header found:\n${JSON.stringify(header)}`);
++++++++ });
++++++++ part.on("data", () => {
++++++++ console.log("part data");
++++++++ });
++++++++ part.on("finish", () => {
++++++++ console.log("part finished");
++++++++ });
++++++++ part.on("error", err => {
++++++++ console.error(`part error: ${err.message || JSON.stringify(err)}`);
++++++++ });
++++++++ part.on("end", () => {
++++++++ console.log("part ended");
++++++++ });
++++++++ part.on("close", () => {
++++++++ console.log("part closed");
++++++++ });
++++++++ part.pipe(outputFileStream);
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import BusboyDefault, { BusboyConstructor, BusboyConfig, BusboyHeaders, Busboy, BusboyEvents, BusboyFileStream } from '../..';
++++++++import {expectError, expectType} from "tsd";
++++++++import BusboyESM from "../..";
++++++++
++++++++// test type exports
++++++++type Constructor = BusboyConstructor;
++++++++type Config = BusboyConfig;
++++++++type Headers = BusboyHeaders;
++++++++type Events = BusboyEvents;
++++++++type BB = Busboy;
++++++++
++++++++expectType<Busboy>(new BusboyESM({ headers: { 'content-type': 'foo' } }));
++++++++expectType<Busboy>(new Busboy({ headers: { 'content-type': 'foo' } }));
++++++++
++++++++expectError(new BusboyDefault({}));
++++++++const busboy = BusboyDefault({ headers: { 'content-type': 'foo' } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, highWaterMark: 1000 }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, fileHwm: 1000 }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, defCharset: 'utf8' }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, preservePath: true }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { fieldNameSize: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { fieldSize: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { fields: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { fileSize: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { files: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { parts: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { headerPairs: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, limits: { headerSize: 200 } }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, isPartAFile: (fieldName, contentType, fileName) => fieldName === 'my-special-field' || fileName !== 'not-so-special.txt' }); // $ExpectType Busboy
++++++++new BusboyDefault({ headers: { 'content-type': 'foo' }, isPartAFile: (fieldName, contentType, fileName) => fileName !== undefined }); // $ExpectType Busboy
++++++++
++++++++busboy.addListener('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname)
++++++++ expectType<BusboyFileStream>(file);
++++++++ expectType<string>(filename);
++++++++ expectType<string>(encoding);
++++++++ expectType<string>(mimetype);
++++++++});
++++++++busboy.addListener('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.addListener('partsLimit', () => {});
++++++++busboy.addListener('filesLimit', () => {});
++++++++busboy.addListener('fieldsLimit', () => {});
++++++++busboy.addListener('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.addListener('finish', () => {});
++++++++// test fallback
++++++++busboy.on('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.on(Symbol('foo'), foo => {
++++++++ expectType<any>(foo);
++++++++});
++++++++
++++++++busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.on('partsLimit', () => {});
++++++++busboy.on('filesLimit', () => {});
++++++++busboy.on('fieldsLimit', () => {});
++++++++busboy.on('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.on('finish', () => {});
++++++++// test fallback
++++++++busboy.on('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.on(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++
++++++++busboy.once('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.once('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.once('partsLimit', () => {});
++++++++busboy.once('filesLimit', () => {});
++++++++busboy.once('fieldsLimit', () => {});
++++++++busboy.once('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.once('finish', () => {});
++++++++// test fallback
++++++++busboy.once('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.once(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++
++++++++busboy.removeListener('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.removeListener('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.removeListener('partsLimit', () => {});
++++++++busboy.removeListener('filesLimit', () => {});
++++++++busboy.removeListener('fieldsLimit', () => {});
++++++++busboy.removeListener('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.removeListener('finish', () => {});
++++++++// test fallback
++++++++busboy.removeListener('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.removeListener(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++
++++++++busboy.off('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.off('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.off('partsLimit', () => {});
++++++++busboy.off('filesLimit', () => {});
++++++++busboy.off('fieldsLimit', () => {});
++++++++busboy.off('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.off('finish', () => {});
++++++++// test fallback
++++++++busboy.off('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.off(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++
++++++++busboy.prependListener('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.prependListener('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.prependListener('partsLimit', () => {});
++++++++busboy.prependListener('filesLimit', () => {});
++++++++busboy.prependListener('fieldsLimit', () => {});
++++++++busboy.prependListener('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.prependListener('finish', () => {});
++++++++// test fallback
++++++++busboy.prependListener('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.prependListener(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++
++++++++busboy.prependOnceListener('file', (fieldname, file, filename, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<BusboyFileStream> (file);
++++++++ expectType<string> (filename);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.prependOnceListener('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
++++++++ expectType<string> (fieldname);
++++++++ expectType<string> (val);
++++++++ expectType<boolean> (fieldnameTruncated);
++++++++ expectType<boolean> (valTruncated);
++++++++ expectType<string> (encoding);
++++++++ expectType<string> (mimetype);
++++++++});
++++++++busboy.prependOnceListener('partsLimit', () => {});
++++++++busboy.prependOnceListener('filesLimit', () => {});
++++++++busboy.prependOnceListener('fieldsLimit', () => {});
++++++++busboy.prependOnceListener('error', e => {
++++++++ expectType<unknown> (e);
++++++++});
++++++++busboy.prependOnceListener('finish', () => {});
++++++++// test fallback
++++++++busboy.prependOnceListener('foo', foo => {
++++++++ expectType<any> (foo);
++++++++});
++++++++busboy.prependOnceListener(Symbol('foo'), foo => {
++++++++ expectType<any> (foo);
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "compilerOptions": {
++++++++ "outDir": "dist",
++++++++ "module": "commonjs",
++++++++ "target": "es2015",
++++++++ "sourceMap": false,
++++++++ "declaration": true,
++++++++ "declarationMap": false,
++++++++ "types": ["node"],
++++++++ "strict": true,
++++++++ "moduleResolution": "node",
++++++++ "noUnusedLocals": false,
++++++++ "noUnusedParameters": false,
++++++++ "noFallthroughCasesInSwitch": true,
++++++++ "noImplicitReturns": true,
++++++++ "noImplicitAny": true,
++++++++ "noImplicitThis": true,
++++++++ "strictNullChecks": true,
++++++++ "importHelpers": true,
++++++++ "baseUrl": ".",
++++++++ "allowSyntheticDefaultImports": true,
++++++++ "esModuleInterop": true,
++++++++ "forceConsistentCasingInFileNames": true
++++++++ },
++++++++ "exclude": [
++++++++ "node_modules",
++++++++ "test",
++++++++ "dist"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++*
++++++++!package.json
++++++++!package-lock.json
++++++++!tsconfig.json
++++++++!tsconfig.base.json
++++++++!bin
++++++++!src
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++module.exports = {
++++++++ 'env': {
++++++++ 'browser': false,
++++++++ 'commonjs': true,
++++++++ 'es6': true,
++++++++ 'node': true
++++++++ },
++++++++ 'extends': 'eslint:recommended',
++++++++ 'rules': {
++++++++ 'max-len': [ 2, {
++++++++ 'code': 80,
++++++++ 'ignoreComments': true
++++++++ } ],
++++++++ 'indent': [
++++++++ 'error',
++++++++ 2
++++++++ ],
++++++++ 'linebreak-style': [
++++++++ 'error',
++++++++ 'unix'
++++++++ ],
++++++++ 'quotes': [
++++++++ 'error',
++++++++ 'single'
++++++++ ],
++++++++ 'semi': [
++++++++ 'error',
++++++++ 'always'
++++++++ ]
++++++++ }
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++version: 2
++++++++updates:
++++++++ - package-ecosystem: github-actions
++++++++ directory: /
++++++++ schedule:
++++++++ interval: daily
++++++++
++++++++ - package-ecosystem: docker
++++++++ directory: /
++++++++ schedule:
++++++++ interval: daily
++++++++
++++++++ - package-ecosystem: npm
++++++++ directory: /
++++++++ schedule:
++++++++ interval: daily
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++name: Aiohttp
++++++++# If you don't understand the reason for a test failure, ping @Dreamsorcerer or open an issue in aio-libs/aiohttp.
++++++++
++++++++on:
++++++++ push:
++++++++ branches:
++++++++ - 'main'
++++++++ pull_request:
++++++++ branches:
++++++++ - 'main'
++++++++
++++++++permissions:
++++++++ contents: read
++++++++
++++++++jobs:
++++++++ test:
++++++++ permissions:
++++++++ contents: read # to fetch code (actions/checkout)
++++++++
++++++++ name: Aiohttp regression tests
++++++++ runs-on: ubuntu-latest
++++++++ steps:
++++++++ - name: Checkout aiohttp
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ repository: aio-libs/aiohttp
++++++++ - name: Checkout llhttp
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ path: vendor/llhttp
++++++++ - name: Restore node_modules cache
++++++++ uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
++++++++ with:
++++++++ path: vendor/llhttp/.npm
++++++++ key: ubuntu-latest-node-${{ hashFiles('vendor/llhttp/**/package-lock.json') }}
++++++++ restore-keys: ubuntu-latest-node-
++++++++ - name: Install llhttp dependencies
++++++++ run: npm ci --ignore-scripts
++++++++ working-directory: vendor/llhttp
++++++++ - name: Build llhttp
++++++++ run: make
++++++++ working-directory: vendor/llhttp
++++++++ - name: Setup Python
++++++++ uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5.0.0
++++++++ with:
++++++++ python-version: 3.x
++++++++ cache: 'pip'
++++++++ cache-dependency-path: 'requirements/*.txt'
++++++++ - name: Provision the dev env
++++++++ run: >-
++++++++ PATH="${HOME}/.local/bin:${PATH}"
++++++++ make .develop
++++++++ - name: Run tests
++++++++ env:
++++++++ COLOR: yes
++++++++ run: >-
++++++++ PATH="${HOME}/.local/bin:${PATH}"
++++++++ pytest tests/test_http_parser.py tests/test_web_functional.py
++++++++ - name: Run dev_mode tests
++++++++ env:
++++++++ COLOR: yes
++++++++ run: >-
++++++++ PATH="${HOME}/.local/bin:${PATH}"
++++++++ python -X dev -m pytest -m dev_mode tests/test_http_parser.py tests/test_web_functional.py
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++name: CI
++++++++
++++++++on: [push, pull_request]
++++++++
++++++++env:
++++++++ CI: true
++++++++
++++++++permissions:
++++++++ contents: read
++++++++
++++++++jobs:
++++++++ build:
++++++++ name: Build libllhttp.a
++++++++ runs-on: ${{ matrix.os }}
++++++++ strategy:
++++++++ matrix:
++++++++ os:
++++++++ - macos-latest
++++++++ - ubuntu-latest
++++++++ - windows-latest
++++++++ steps:
++++++++ - name: Install clang for Windows
++++++++ if: runner.os == 'Windows'
++++++++ run: |
++++++++ iwr -useb get.scoop.sh -outfile 'install.ps1'
++++++++ .\install.ps1 -RunAsAdmin
++++++++ scoop install llvm --global
++++++++
++++++++ # Scoop modifies the PATH so we make the modified PATH global.
++++++++ echo $env:PATH >> $env:GITHUB_PATH
++++++++
++++++++ - name: Fetch code
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ fetch-depth: 1
++++++++
++++++++ # Skip macOS & Windows, cache there is slower
++++++++ - name: Restore node_modules cache for Linux
++++++++ uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
++++++++ if: runner.os == 'Linux'
++++++++ with:
++++++++ path: ~/.npm
++++++++ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
++++++++ restore-keys: |
++++++++ ${{ runner.os }}-node-
++++++++
++++++++ - name: Install dependencies
++++++++ run: npm ci --ignore-scripts
++++++++
++++++++ - name: Build libllhttp.a
++++++++ shell: bash
++++++++ run: |
++++++++ make build/libllhttp.a
++++++++
++++++++ test:
++++++++ name: Run tests
++++++++ runs-on: ${{ matrix.os }}
++++++++ strategy:
++++++++ matrix:
++++++++ os:
++++++++ - macos-latest
++++++++ - ubuntu-latest
++++++++ - windows-latest
++++++++ steps:
++++++++ - name: Install clang for Windows
++++++++ if: runner.os == 'Windows'
++++++++ run: |
++++++++ iwr -useb get.scoop.sh -outfile 'install.ps1'
++++++++ .\install.ps1 -RunAsAdmin
++++++++ scoop install llvm --global
++++++++
++++++++ # Scoop modifies the PATH so we make the modified PATH global.
++++++++ echo $env:PATH >> $env:GITHUB_PATH
++++++++
++++++++ - name: Fetch code
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ fetch-depth: 1
++++++++
++++++++ # Skip macOS & Windows, cache there is slower
++++++++ - name: Restore node_modules cache for Linux
++++++++ uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
++++++++ if: runner.os == 'Linux'
++++++++ with:
++++++++ path: ~/.npm
++++++++ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
++++++++ restore-keys: |
++++++++ ${{ runner.os }}-node-
++++++++
++++++++ - name: Install dependencies
++++++++ run: npm ci --ignore-scripts
++++++++
++++++++ # Custom script, because progress looks not good in CI
++++++++ - name: Run tests
++++++++ run: npm run test
++++++++
++++++++ lint:
++++++++ name: Run ESLint
++++++++ runs-on: ubuntu-latest
++++++++ steps:
++++++++ - name: Fetch code
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ fetch-depth: 1
++++++++
++++++++ - name: Restore node_modules cache
++++++++ uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319 # v4.0.1
++++++++ with:
++++++++ path: ~/.npm
++++++++ key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
++++++++ restore-keys: |
++++++++ ${{ runner.os }}-node-
++++++++
++++++++ - name: Install dependencies
++++++++ run: npm ci --ignore-scripts
++++++++
++++++++ - name: Run lint command
++++++++ run: npm run lint
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# For most projects, this workflow file will not need changing; you simply need
++++++++# to commit it to your repository.
++++++++#
++++++++# You may wish to alter this file to override the set of languages analyzed,
++++++++# or to provide custom queries or build logic.
++++++++#
++++++++# ******** NOTE ********
++++++++# We have attempted to detect the languages in your repository. Please check
++++++++# the `language` matrix defined below to confirm you have the correct set of
++++++++# supported CodeQL languages.
++++++++#
++++++++name: "CodeQL"
++++++++
++++++++on:
++++++++ push:
++++++++ branches: ["main"]
++++++++ pull_request:
++++++++ # The branches below must be a subset of the branches above
++++++++ branches: ["main"]
++++++++ schedule:
++++++++ - cron: "0 0 * * 1"
++++++++
++++++++permissions:
++++++++ contents: read
++++++++
++++++++jobs:
++++++++ analyze:
++++++++ name: Analyze
++++++++ runs-on: ubuntu-latest
++++++++ permissions:
++++++++ actions: read
++++++++ contents: read
++++++++ security-events: write
++++++++
++++++++ strategy:
++++++++ fail-fast: false
++++++++ matrix:
++++++++ language: ["javascript", "typescript"]
++++++++ # CodeQL supports [ $supported-codeql-languages ]
++++++++ # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
++++++++
++++++++ steps:
++++++++ - name: Checkout repository
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++
++++++++ # Initializes the CodeQL tools for scanning.
++++++++ - name: Initialize CodeQL
++++++++ uses: github/codeql-action/init@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
++++++++ with:
++++++++ languages: ${{ matrix.language }}
++++++++ # If you wish to specify custom queries, you can do so here or in a config file.
++++++++ # By default, queries listed here will override any specified in a config file.
++++++++ # Prefix the list here with "+" to use these queries and those in the config file.
++++++++
++++++++ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
++++++++ # If this step fails, then you should remove it and run the build manually (see below)
++++++++ - name: Autobuild
++++++++ uses: github/codeql-action/autobuild@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
++++++++
++++++++ # ℹ️ Command-line programs to run using the OS shell.
++++++++ # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
++++++++
++++++++ # If the Autobuild fails above, remove it and uncomment the following three lines.
++++++++ # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
++++++++
++++++++ # - run: |
++++++++ # echo "Run, Build Application using script"
++++++++ # ./location_of_script_within_repo/buildscript.sh
++++++++
++++++++ - name: Perform CodeQL Analysis
++++++++ uses: github/codeql-action/analyze@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
++++++++ with:
++++++++ category: "/language:${{matrix.language}}"
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# This workflow uses actions that are not certified by GitHub. They are provided
++++++++# by a third-party and are governed by separate terms of service, privacy
++++++++# policy, and support documentation.
++++++++
++++++++name: Scorecard supply-chain security
++++++++on:
++++++++ # For Branch-Protection check. Only the default branch is supported. See
++++++++ # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection
++++++++ branch_protection_rule:
++++++++ # To guarantee Maintained check is occasionally updated. See
++++++++ # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained
++++++++ schedule:
++++++++ - cron: '20 7 * * 2'
++++++++ push:
++++++++ branches: ["main"]
++++++++
++++++++# Declare default permissions as read only.
++++++++permissions: read-all
++++++++
++++++++jobs:
++++++++ analysis:
++++++++ name: Scorecard analysis
++++++++ runs-on: ubuntu-latest
++++++++ permissions:
++++++++ # Needed to upload the results to code-scanning dashboard.
++++++++ security-events: write
++++++++ # Needed to publish results and get a badge (see publish_results below).
++++++++ id-token: write
++++++++ contents: read
++++++++ actions: read
++++++++
++++++++ steps:
++++++++ - name: "Checkout code"
++++++++ uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
++++++++ with:
++++++++ persist-credentials: false
++++++++
++++++++ - name: "Run analysis"
++++++++ uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
++++++++ with:
++++++++ results_file: results.sarif
++++++++ results_format: sarif
++++++++ # (Optional) "write" PAT token. Uncomment the `repo_token` line below if:
++++++++ # - you want to enable the Branch-Protection check on a *public* repository, or
++++++++ # - you are installing Scorecards on a *private* repository
++++++++ # To create the PAT, follow the steps in https://github.com/ossf/scorecard-action#authentication-with-pat.
++++++++ # repo_token: ${{ secrets.SCORECARD_TOKEN }}
++++++++
++++++++ # Public repositories:
++++++++ # - Publish results to OpenSSF REST API for easy access by consumers
++++++++ # - Allows the repository to include the Scorecard badge.
++++++++ # - See https://github.com/ossf/scorecard-action#publishing-results.
++++++++ # For private repositories:
++++++++ # - `publish_results` will always be set to `false`, regardless
++++++++ # of the value entered here.
++++++++ publish_results: true
++++++++
++++++++ # Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
++++++++ # format to the repository Actions tab.
++++++++ - name: "Upload artifact"
++++++++ uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
++++++++ with:
++++++++ name: SARIF file
++++++++ path: results.sarif
++++++++ retention-days: 5
++++++++
++++++++ # Upload the results to GitHub's code scanning dashboard.
++++++++ - name: "Upload to code-scanning"
++++++++ uses: github/codeql-action/upload-sarif@3ab4101902695724f9365a384f86c1074d94e18c # v3.24.7
++++++++ with:
++++++++ sarif_file: results.sarif
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node_modules/
++++++++npm-debug.log
++++++++test/tmp/
++++++++lib/
++++++++build/
++++++++release/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++package-lock=true
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++cmake_minimum_required(VERSION 3.5.1)
++++++++cmake_policy(SET CMP0069 NEW)
++++++++
++++++++project(llhttp VERSION _RELEASE_)
++++++++include(GNUInstallDirs)
++++++++
++++++++set(CMAKE_C_STANDARD 99)
++++++++
++++++++# By default build in relwithdebinfo type, supports both lowercase and uppercase
++++++++if(NOT CMAKE_CONFIGURATION_TYPES)
++++++++ set(allowableBuildTypes DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
++++++++ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowableBuildTypes}")
++++++++ if(NOT CMAKE_BUILD_TYPE)
++++++++ set(CMAKE_BUILD_TYPE RELWITHDEBINFO CACHE STRING "" FORCE)
++++++++ else()
++++++++ string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
++++++++ if(NOT CMAKE_BUILD_TYPE IN_LIST allowableBuildTypes)
++++++++ message(FATAL_ERROR "Invalid build type: ${CMAKE_BUILD_TYPE}")
++++++++ endif()
++++++++ endif()
++++++++endif()
++++++++
++++++++#
++++++++# Options
++++++++#
++++++++# Generic option
++++++++option(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so)" ON)
++++++++option(BUILD_STATIC_LIBS "Build static libraries (.lib/.a)" OFF)
++++++++
++++++++# Source code
++++++++set(LLHTTP_SOURCES
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/src/llhttp.c
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/src/http.c
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/src/api.c
++++++++)
++++++++
++++++++set(LLHTTP_HEADERS
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/include/llhttp.h
++++++++)
++++++++
++++++++configure_file(
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc.in
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc
++++++++ @ONLY
++++++++)
++++++++
++++++++function(config_library target)
++++++++ target_sources(${target} PRIVATE ${LLHTTP_SOURCES} ${LLHTTP_HEADERS})
++++++++
++++++++ target_include_directories(${target} PUBLIC
++++++++ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
++++++++ $<INSTALL_INTERFACE:include>
++++++++ )
++++++++
++++++++ set_target_properties(${target} PROPERTIES
++++++++ OUTPUT_NAME llhttp
++++++++ VERSION ${PROJECT_VERSION}
++++++++ SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
++++++++ PUBLIC_HEADER ${LLHTTP_HEADERS}
++++++++ )
++++++++
++++++++ install(TARGETS ${target}
++++++++ EXPORT llhttp
++++++++ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
++++++++ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
++++++++ PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
++++++++ )
++++++++
++++++++ install(FILES
++++++++ ${CMAKE_CURRENT_SOURCE_DIR}/libllhttp.pc
++++++++ DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
++++++++ )
++++++++
++++++++ # This is required to work with FetchContent
++++++++ install(EXPORT llhttp
++++++++ FILE llhttp-config.cmake
++++++++ NAMESPACE llhttp::
++++++++ DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/llhttp
++++++++ )
++++++++endfunction(config_library target)
++++++++
++++++++if(BUILD_SHARED_LIBS)
++++++++ add_library(llhttp_shared SHARED
++++++++ ${llhttp_src}
++++++++ )
++++++++ add_library(llhttp::llhttp ALIAS llhttp_shared)
++++++++ config_library(llhttp_shared)
++++++++endif()
++++++++
++++++++if(BUILD_STATIC_LIBS)
++++++++ add_library(llhttp_static STATIC
++++++++ ${llhttp_src}
++++++++ )
++++++++ if(BUILD_SHARED_LIBS)
++++++++ add_library(llhttp::llhttp ALIAS llhttp_shared)
++++++++ else()
++++++++ add_library(llhttp::llhttp ALIAS llhttp_static)
++++++++ endif()
++++++++ config_library(llhttp_static)
++++++++endif()
++++++++
++++++++# On windows with Visual Studio, add a debug postfix so that release
++++++++# and debug libraries can coexist.
++++++++if(MSVC)
++++++++ set(CMAKE_DEBUG_POSTFIX "d")
++++++++endif()
++++++++
++++++++# Print project configure summary
++++++++message(STATUS "")
++++++++message(STATUS "")
++++++++message(STATUS "Project configure summary:")
++++++++message(STATUS "")
++++++++message(STATUS " CMake build type .................: ${CMAKE_BUILD_TYPE}")
++++++++message(STATUS " Install prefix ...................: ${CMAKE_INSTALL_PREFIX}")
++++++++message(STATUS " Build shared library .............: ${BUILD_SHARED_LIBS}")
++++++++message(STATUS " Build static library .............: ${BUILD_STATIC_LIBS}")
++++++++message(STATUS "")
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llhttp.org
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# Code of Conduct
++++++++
++++++++* [Node.js Code of Conduct](https://github.com/nodejs/admin/blob/main/CODE_OF_CONDUCT.md)
++++++++* [Node.js Moderation Policy](https://github.com/nodejs/admin/blob/main/Moderation-Policy.md)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++FROM node:21-alpine@sha256:7bfef1d72befbb72b0894a3e4503edbdc0441058b4d091325143338cbf54cff8
++++++++ARG UID=1000
++++++++ARG GID=1000
++++++++
++++++++RUN apk add -U clang lld wasi-sdk && mkdir /home/node/llhttp
++++++++
++++++++WORKDIR /home/node/llhttp
++++++++
++++++++COPY . .
++++++++
++++++++RUN npm ci
++++++++
++++++++USER node
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2018.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++CLANG ?= clang
++++++++CFLAGS ?=
++++++++OS ?=
++++++++
++++++++CFLAGS += -Os -g3 -Wall -Wextra -Wno-unused-parameter
++++++++ifneq ($(OS),Windows_NT)
++++++++ # NOTE: clang on windows does not support fPIC
++++++++ CFLAGS += -fPIC
++++++++endif
++++++++
++++++++INCLUDES += -Ibuild/
++++++++
++++++++INSTALL ?= install
++++++++PREFIX ?= /usr/local
++++++++LIBDIR = $(PREFIX)/lib
++++++++INCLUDEDIR = $(PREFIX)/include
++++++++
++++++++all: build/libllhttp.a build/libllhttp.so
++++++++
++++++++clean:
++++++++ rm -rf release/
++++++++ rm -rf build/
++++++++
++++++++build/libllhttp.so: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++ $(CLANG) -shared $^ -o $@
++++++++
++++++++build/libllhttp.a: build/c/llhttp.o build/native/api.o \
++++++++ build/native/http.o
++++++++ $(AR) rcs $@ build/c/llhttp.o build/native/api.o build/native/http.o
++++++++
++++++++build/c/llhttp.o: build/c/llhttp.c
++++++++ $(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@
++++++++
++++++++build/native/%.o: src/native/%.c build/llhttp.h src/native/api.h \
++++++++ build/native
++++++++ $(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@
++++++++
++++++++build/llhttp.h: generate
++++++++build/c/llhttp.c: generate
++++++++
++++++++build/native:
++++++++ mkdir -p build/native
++++++++
++++++++release: clean generate
++++++++ @echo "${RELEASE}" | grep -q -E ".+" || { echo "Please make sure the RELEASE argument is set."; exit 1; }
++++++++ rm -rf release
++++++++ mkdir -p release/src
++++++++ mkdir -p release/include
++++++++ cp -rf build/llhttp.h release/include/
++++++++ cp -rf build/c/llhttp.c release/src/
++++++++ cp -rf src/native/*.c release/src/
++++++++ cp -rf src/llhttp.gyp release/
++++++++ cp -rf src/common.gypi release/
++++++++ sed s/_RELEASE_/$(RELEASE)/ CMakeLists.txt > release/CMakeLists.txt
++++++++ cp -rf libllhttp.pc.in release/
++++++++ cp -rf README.md release/
++++++++ cp -rf LICENSE-MIT release/
++++++++
++++++++github-release:
++++++++ @echo "${RELEASE_V}" | grep -q -E "^v" || { echo "Please make sure version starts with \"v\"."; exit 1; }
++++++++ gh release create -d --generate-notes ${RELEASE_V}
++++++++ @sleep 5
++++++++ gh release view ${RELEASE_V} -t "{{.body}}" --json body > RELEASE_NOTES
++++++++ gh release delete ${RELEASE_V} -y
++++++++ gh release create -F RELEASE_NOTES -d --title ${RELEASE_V} --target release release/${RELEASE_V}
++++++++ @sleep 5
++++++++ rm -rf RELEASE_NOTES
++++++++ open $$(gh release view release/${RELEASE_V} --json url -t "{{.url}}")
++++++++
++++++++postversion: release
++++++++ git fetch origin
++++++++ git push
++++++++ git checkout release --
++++++++ cp -rf release/* ./
++++++++ rm -rf release
++++++++ git add include src *.gyp *.gypi CMakeLists.txt README.md LICENSE-MIT libllhttp.pc.in
++++++++ git commit -a -m "release: $(RELEASE)"
++++++++ git tag "release/v$(RELEASE)"
++++++++ git push && git push --tags
++++++++ git checkout main
++++++++
++++++++generate:
++++++++ npx ts-node bin/generate.ts
++++++++
++++++++install: build/libllhttp.a build/libllhttp.so
++++++++ $(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)
++++++++ $(INSTALL) -d $(DESTDIR)$(LIBDIR)
++++++++ $(INSTALL) -C build/llhttp.h $(DESTDIR)$(INCLUDEDIR)/llhttp.h
++++++++ $(INSTALL) -C build/libllhttp.a $(DESTDIR)$(LIBDIR)/libllhttp.a
++++++++ $(INSTALL) build/libllhttp.so $(DESTDIR)$(LIBDIR)/libllhttp.so
++++++++
++++++++.PHONY: all generate clean release postversion github-release
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# llhttp
++++++++[](https://github.com/nodejs/llhttp/actions?query=workflow%3ACI)
++++++++
++++++++Port of [http_parser][0] to [llparse][1].
++++++++
++++++++## Why?
++++++++
++++++++Let's face it, [http_parser][0] is practically unmaintainable. Even
++++++++introduction of a single new method results in a significant code churn.
++++++++
++++++++This project aims to:
++++++++
++++++++* Make it maintainable
++++++++* Verifiable
++++++++* Improving benchmarks where possible
++++++++
++++++++More details in [Fedor Indutny's talk at JSConf EU 2019](https://youtu.be/x3k_5Mi66sY)
++++++++
++++++++## How?
++++++++
++++++++Over time, different approaches for improving [http_parser][0]'s code base
++++++++were tried. However, all of them failed due to resulting significant performance
++++++++degradation.
++++++++
++++++++This project is a port of [http_parser][0] to TypeScript. [llparse][1] is used
++++++++to generate the output C source file, which could be compiled and
++++++++linked with the embedder's program (like [Node.js][7]).
++++++++
++++++++## Performance
++++++++
++++++++So far llhttp outperforms http_parser:
++++++++
++++++++| | input size | bandwidth | reqs/sec | time |
++++++++|:----------------|-----------:|-------------:|-----------:|--------:|
++++++++| **llhttp** | 8192.00 mb | 1777.24 mb/s | 3583799.39 req/sec | 4.61 s |
++++++++| **http_parser** | 8192.00 mb | 694.66 mb/s | 1406180.33 req/sec | 11.79 s |
++++++++
++++++++llhttp is faster by approximately **156%**.
++++++++
++++++++## Maintenance
++++++++
++++++++llhttp project has about 1400 lines of TypeScript code describing the parser
++++++++itself and around 450 lines of C code and headers providing the helper methods.
++++++++The whole [http_parser][0] is implemented in approximately 2500 lines of C, and
++++++++436 lines of headers.
++++++++
++++++++All optimizations and multi-character matching in llhttp are generated
++++++++automatically, and thus doesn't add any extra maintenance cost. On the contrary,
++++++++most of http_parser's code is hand-optimized and unrolled. Instead describing
++++++++"how" it should parse the HTTP requests/responses, a maintainer should
++++++++implement the new features in [http_parser][0] cautiously, considering
++++++++possible performance degradation and manually optimizing the new code.
++++++++
++++++++## Verification
++++++++
++++++++The state machine graph is encoded explicitly in llhttp. The [llparse][1]
++++++++automatically checks the graph for absence of loops and correct reporting of the
++++++++input ranges (spans) like header names and values. In the future, additional
++++++++checks could be performed to get even stricter verification of the llhttp.
++++++++
++++++++## Usage
++++++++
++++++++```C
++++++++#include "stdio.h"
++++++++#include "llhttp.h"
++++++++#include "string.h"
++++++++
++++++++int handle_on_message_complete(llhttp_t* parser) {
++++++++ fprintf(stdout, "Message completed!\n");
++++++++ return 0;
++++++++}
++++++++
++++++++int main() {
++++++++ llhttp_t parser;
++++++++ llhttp_settings_t settings;
++++++++
++++++++ /*Initialize user callbacks and settings */
++++++++ llhttp_settings_init(&settings);
++++++++
++++++++ /*Set user callback */
++++++++ settings.on_message_complete = handle_on_message_complete;
++++++++
++++++++ /*Initialize the parser in HTTP_BOTH mode, meaning that it will select between
++++++++ *HTTP_REQUEST and HTTP_RESPONSE parsing automatically while reading the first
++++++++ *input.
++++++++ */
++++++++ llhttp_init(&parser, HTTP_BOTH, &settings);
++++++++
++++++++ /*Parse request! */
++++++++ const char* request = "GET / HTTP/1.1\r\n\r\n";
++++++++ int request_len = strlen(request);
++++++++
++++++++ enum llhttp_errno err = llhttp_execute(&parser, request, request_len);
++++++++ if (err == HPE_OK) {
++++++++ fprintf(stdout, "Successfully parsed!\n");
++++++++ } else {
++++++++ fprintf(stderr, "Parse error: %s %s\n", llhttp_errno_name(err), parser.reason);
++++++++ }
++++++++}
++++++++```
++++++++For more information on API usage, please refer to [src/native/api.h](https://github.com/nodejs/llhttp/blob/main/src/native/api.h).
++++++++
++++++++## API
++++++++
++++++++### llhttp_settings_t
++++++++
++++++++The settings object contains a list of callbacks that the parser will invoke.
++++++++
++++++++The following callbacks can return `0` (proceed normally), `-1` (error) or `HPE_PAUSED` (pause the parser):
++++++++
++++++++* `on_message_begin`: Invoked when a new request/response starts.
++++++++* `on_message_complete`: Invoked when a request/response has been completedly parsed.
++++++++* `on_url_complete`: Invoked after the URL has been parsed.
++++++++* `on_method_complete`: Invoked after the HTTP method has been parsed.
++++++++* `on_version_complete`: Invoked after the HTTP version has been parsed.
++++++++* `on_status_complete`: Invoked after the status code has been parsed.
++++++++* `on_header_field_complete`: Invoked after a header name has been parsed.
++++++++* `on_header_value_complete`: Invoked after a header value has been parsed.
++++++++* `on_chunk_header`: Invoked after a new chunk is started. The current chunk length is stored in `parser->content_length`.
++++++++* `on_chunk_extension_name_complete`: Invoked after a chunk extension name is started.
++++++++* `on_chunk_extension_value_complete`: Invoked after a chunk extension value is started.
++++++++* `on_chunk_complete`: Invoked after a new chunk is received.
++++++++* `on_reset`: Invoked after `on_message_complete` and before `on_message_begin` when a new message
++++++++ is received on the same parser. This is not invoked for the first message of the parser.
++++++++
++++++++The following callbacks can return `0` (proceed normally), `-1` (error) or `HPE_USER` (error from the callback):
++++++++
++++++++* `on_url`: Invoked when another character of the URL is received.
++++++++* `on_status`: Invoked when another character of the status is received.
++++++++* `on_method`: Invoked when another character of the method is received.
++++++++ When parser is created with `HTTP_BOTH` and the input is a response, this also invoked for the sequence `HTTP/`
++++++++ of the first message.
++++++++* `on_version`: Invoked when another character of the version is received.
++++++++* `on_header_field`: Invoked when another character of a header name is received.
++++++++* `on_header_value`: Invoked when another character of a header value is received.
++++++++* `on_chunk_extension_name`: Invoked when another character of a chunk extension name is received.
++++++++* `on_chunk_extension_value`: Invoked when another character of a extension value is received.
++++++++
++++++++The callback `on_headers_complete`, invoked when headers are completed, can return:
++++++++
++++++++* `0`: Proceed normally.
++++++++* `1`: Assume that request/response has no body, and proceed to parsing the next message.
++++++++* `2`: Assume absence of body (as above) and make `llhttp_execute()` return `HPE_PAUSED_UPGRADE`.
++++++++* `-1`: Error
++++++++* `HPE_PAUSED`: Pause the parser.
++++++++
++++++++### `void llhttp_init(llhttp_t* parser, llhttp_type_t type, const llhttp_settings_t* settings)`
++++++++
++++++++Initialize the parser with specific type and user settings.
++++++++
++++++++### `uint8_t llhttp_get_type(llhttp_t* parser)`
++++++++
++++++++Returns the type of the parser.
++++++++
++++++++### `uint8_t llhttp_get_http_major(llhttp_t* parser)`
++++++++
++++++++Returns the major version of the HTTP protocol of the current request/response.
++++++++
++++++++### `uint8_t llhttp_get_http_minor(llhttp_t* parser)`
++++++++
++++++++Returns the minor version of the HTTP protocol of the current request/response.
++++++++
++++++++### `uint8_t llhttp_get_method(llhttp_t* parser)`
++++++++
++++++++Returns the method of the current request.
++++++++
++++++++### `int llhttp_get_status_code(llhttp_t* parser)`
++++++++
++++++++Returns the method of the current response.
++++++++
++++++++### `uint8_t llhttp_get_upgrade(llhttp_t* parser)`
++++++++
++++++++Returns `1` if request includes the `Connection: upgrade` header.
++++++++
++++++++### `void llhttp_reset(llhttp_t* parser)`
++++++++
++++++++Reset an already initialized parser back to the start state, preserving the
++++++++existing parser type, callback settings, user data, and lenient flags.
++++++++
++++++++### `void llhttp_settings_init(llhttp_settings_t* settings)`
++++++++
++++++++Initialize the settings object.
++++++++
++++++++### `llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len)`
++++++++
++++++++Parse full or partial request/response, invoking user callbacks along the way.
++++++++
++++++++If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing interrupts,
++++++++and such errno is returned from `llhttp_execute()`. If `HPE_PAUSED` was used as a errno,
++++++++the execution can be resumed with `llhttp_resume()` call.
++++++++
++++++++In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE` is returned
++++++++after fully parsing the request/response. If the user wishes to continue parsing,
++++++++they need to invoke `llhttp_resume_after_upgrade()`.
++++++++
++++++++**if this function ever returns a non-pause type error, it will continue to return
++++++++the same error upon each successive call up until `llhttp_init()` is called.**
++++++++
++++++++### `llhttp_errno_t llhttp_finish(llhttp_t* parser)`
++++++++
++++++++This method should be called when the other side has no further bytes to
++++++++send (e.g. shutdown of readable side of the TCP connection.)
++++++++
++++++++Requests without `Content-Length` and other messages might require treating
++++++++all incoming bytes as the part of the body, up to the last byte of the
++++++++connection.
++++++++
++++++++This method will invoke `on_message_complete()` callback if the
++++++++request was terminated safely. Otherwise a error code would be returned.
++++++++
++++++++
++++++++### `int llhttp_message_needs_eof(const llhttp_t* parser)`
++++++++
++++++++Returns `1` if the incoming message is parsed until the last byte, and has to be completed by calling `llhttp_finish()` on EOF.
++++++++
++++++++### `int llhttp_should_keep_alive(const llhttp_t* parser)`
++++++++
++++++++Returns `1` if there might be any other messages following the last that was
++++++++successfully parsed.
++++++++
++++++++### `void llhttp_pause(llhttp_t* parser)`
++++++++
++++++++Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
++++++++appropriate error reason.
++++++++
++++++++**Do not call this from user callbacks! User callbacks must return
++++++++`HPE_PAUSED` if pausing is required.**
++++++++
++++++++### `void llhttp_resume(llhttp_t* parser)`
++++++++
++++++++Might be called to resume the execution after the pause in user's callback.
++++++++
++++++++See `llhttp_execute()` above for details.
++++++++
++++++++**Call this only if `llhttp_execute()` returns `HPE_PAUSED`.**
++++++++
++++++++### `void llhttp_resume_after_upgrade(llhttp_t* parser)`
++++++++
++++++++Might be called to resume the execution after the pause in user's callback.
++++++++See `llhttp_execute()` above for details.
++++++++
++++++++**Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`**
++++++++
++++++++### `llhttp_errno_t llhttp_get_errno(const llhttp_t* parser)`
++++++++
++++++++Returns the latest error.
++++++++
++++++++### `const char* llhttp_get_error_reason(const llhttp_t* parser)`
++++++++
++++++++Returns the verbal explanation of the latest returned error.
++++++++
++++++++**User callback should set error reason when returning the error. See
++++++++`llhttp_set_error_reason()` for details.**
++++++++
++++++++### `void llhttp_set_error_reason(llhttp_t* parser, const char* reason)`
++++++++
++++++++Assign verbal description to the returned error. Must be called in user
++++++++callbacks right before returning the errno.
++++++++
++++++++**`HPE_USER` error code might be useful in user callbacks.**
++++++++
++++++++### `const char* llhttp_get_error_pos(const llhttp_t* parser)`
++++++++
++++++++Returns the pointer to the last parsed byte before the returned error. The
++++++++pointer is relative to the `data` argument of `llhttp_execute()`.
++++++++
++++++++**This method might be useful for counting the number of parsed bytes.**
++++++++
++++++++### `const char* llhttp_errno_name(llhttp_errno_t err)`
++++++++
++++++++Returns textual name of error code.
++++++++
++++++++### `const char* llhttp_method_name(llhttp_method_t method)`
++++++++
++++++++Returns textual name of HTTP method.
++++++++
++++++++### `const char* llhttp_status_name(llhttp_status_t status)`
++++++++
++++++++Returns textual name of HTTP status.
++++++++
++++++++### `void llhttp_set_lenient_headers(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient header value parsing (disabled by default).
++++++++Lenient parsing disables header value token checks, extending llhttp's
++++++++protocol support to highly non-compliant clients/server.
++++++++
++++++++No `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
++++++++lenient parsing is "on".
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of conflicting `Transfer-Encoding` and
++++++++`Content-Length` headers (disabled by default).
++++++++
++++++++Normally `llhttp` would error when `Transfer-Encoding` is present in
++++++++conjunction with `Content-Length`.
++++++++
++++++++This error is important to prevent HTTP request smuggling, but may be less desirable
++++++++for small number of cases involving legacy servers.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of `Connection: close` and HTTP/1.0
++++++++requests responses.
++++++++
++++++++Normally `llhttp` would error the HTTP request/response
++++++++after the request/response with `Connection: close` and `Content-Length`.
++++++++
++++++++This is important to prevent cache poisoning attacks,
++++++++but might interact badly with outdated and insecure clients.
++++++++
++++++++With this flag the extra request/response will be parsed normally.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to poisoning attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of `Transfer-Encoding` header.
++++++++
++++++++Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
++++++++and another value after it (either in a single header or in multiple
++++++++headers whose value are internally joined using `, `).
++++++++
++++++++This is mandated by the spec to reliably determine request body size and thus
++++++++avoid request smuggling.
++++++++
++++++++With this flag the extra value will be parsed normally.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_version(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of HTTP version.
++++++++
++++++++Normally `llhttp` would error when the HTTP version in the request or status line
++++++++is not `0.9`, `1.0`, `1.1` or `2.0`.
++++++++With this flag the extra value will be parsed normally.
++++++++
++++++++**Enabling this flag can pose a security issue since you will allow unsupported HTTP versions. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of additional data received after a message ends
++++++++and keep-alive is disabled.
++++++++
++++++++Normally `llhttp` would error when additional unexpected data is received if the message
++++++++contains the `Connection` header with `close` value.
++++++++With this flag the extra data will discarded without throwing an error.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to poisoning attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of incomplete CRLF sequences.
++++++++
++++++++Normally `llhttp` would error when a CR is not followed by LF when terminating the
++++++++request line, the status line, the headers or a chunk header.
++++++++With this flag only a CR is required to terminate such sections.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of line separators.
++++++++
++++++++Normally `llhttp` would error when a LF is not preceded by CR when terminating the
++++++++request line, the status line, the headers, a chunk header or a chunk data.
++++++++With this flag only a LF is required to terminate such sections.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of chunks not separated via CRLF.
++++++++
++++++++Normally `llhttp` would error when after a chunk data a CRLF is missing before
++++++++starting a new chunk.
++++++++With this flag the new chunk can start immediately after the previous one.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++### `void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled)`
++++++++
++++++++Enables/disables lenient handling of spaces after chunk size.
++++++++
++++++++Normally `llhttp` would error when after a chunk size is followed by one or more spaces are present instead of a CRLF or `;`.
++++++++With this flag this check is disabled.
++++++++
++++++++**Enabling this flag can pose a security issue since you will be exposed to request smuggling attacks. USE WITH CAUTION!**
++++++++
++++++++## Build Instructions
++++++++
++++++++Make sure you have [Node.js](https://nodejs.org/), npm and npx installed. Then under project directory run:
++++++++
++++++++```sh
++++++++npm ci
++++++++make
++++++++```
++++++++
++++++++---
++++++++
++++++++### Bindings to other languages
++++++++
++++++++* Lua: [MunifTanjim/llhttp.lua][11]
++++++++* Python: [pallas/pyllhttp][8]
++++++++* Ruby: [metabahn/llhttp][9]
++++++++* Rust: [JackLiar/rust-llhttp][10]
++++++++
++++++++### Using with CMake
++++++++
++++++++If you want to use this library in a CMake project as a shared library, you can use the snippet below.
++++++++
++++++++```
++++++++FetchContent_Declare(llhttp
++++++++ URL "https://github.com/nodejs/llhttp/archive/refs/tags/release/v8.1.0.tar.gz")
++++++++
++++++++FetchContent_MakeAvailable(llhttp)
++++++++
++++++++# Link with the llhttp_shared target
++++++++target_link_libraries(${EXAMPLE_PROJECT_NAME} ${PROJECT_LIBRARIES} llhttp_shared ${PROJECT_NAME})
++++++++```
++++++++
++++++++If you want to use this library in a CMake project as a static library, you can set some cache variables first.
++++++++
++++++++```
++++++++FetchContent_Declare(llhttp
++++++++ URL "https://github.com/nodejs/llhttp/archive/refs/tags/release/v8.1.0.tar.gz")
++++++++
++++++++set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "")
++++++++set(BUILD_STATIC_LIBS ON CACHE INTERNAL "")
++++++++FetchContent_MakeAvailable(llhttp)
++++++++
++++++++# Link with the llhttp_static target
++++++++target_link_libraries(${EXAMPLE_PROJECT_NAME} ${PROJECT_LIBRARIES} llhttp_static ${PROJECT_NAME})
++++++++```
++++++++
++++++++_Note that using the git repo directly (e.g., via a git repo url and tag) will not work with FetchContent_Declare because [CMakeLists.txt](./CMakeLists.txt) requires string replacements (e.g., `_RELEASE_`) before it will build._
++++++++
++++++++## Building on Windows
++++++++
++++++++### Installation
++++++++
++++++++* `choco install git`
++++++++* `choco install node`
++++++++* `choco install llvm` (or install the `C++ Clang tools for Windows` optional package from the Visual Studio 2019 installer)
++++++++* `choco install make` (or if you have MinGW, it comes bundled)
++++++++
++++++++1. Ensure that `Clang` and `make` are in your system path.
++++++++2. Using Git Bash, clone the repo to your preferred location.
++++++++3. Cd into the cloned directory and run `npm ci`
++++++++5. Run `make`
++++++++6. Your `repo/build` directory should now have `libllhttp.a` and `libllhttp.so` static and dynamic libraries.
++++++++7. When building your executable, you can link to these libraries. Make sure to set the build folder as an include path when building so you can reference the declarations in `repo/build/llhttp.h`.
++++++++
++++++++### A simple example on linking with the library:
++++++++
++++++++Assuming you have an executable `main.cpp` in your current working directory, you would run: `clang++ -Os -g3 -Wall -Wextra -Wno-unused-parameter -I/path/to/llhttp/build main.cpp /path/to/llhttp/build/libllhttp.a -o main.exe`.
++++++++
++++++++If you are getting `unresolved external symbol` linker errors you are likely attempting to build `llhttp.c` without linking it with object files from `api.c` and `http.c`.
++++++++
++++++++#### LICENSE
++++++++
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2018.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
++++++++
++++++++[0]: https://github.com/nodejs/http-parser
++++++++[1]: https://github.com/nodejs/llparse
++++++++[2]: https://en.wikipedia.org/wiki/Register_allocation#Spilling
++++++++[3]: https://en.wikipedia.org/wiki/Tail_call
++++++++[4]: https://llvm.org/docs/LangRef.html
++++++++[5]: https://llvm.org/docs/LangRef.html#call-instruction
++++++++[6]: https://clang.llvm.org/
++++++++[7]: https://github.com/nodejs/node
++++++++[8]: https://github.com/pallas/pyllhttp
++++++++[9]: https://github.com/metabahn/llhttp
++++++++[10]: https://github.com/JackLiar/rust-llhttp
++++++++[11]: https://github.com/MunifTanjim/llhttp.lua
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++theme: jekyll-theme-midnight
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from "assert";
++++++++import { spawnSync } from "child_process";
++++++++import { existsSync } from "fs";
++++++++import { resolve } from "path";
++++++++
++++++++function request(tpl: TemplateStringsArray): string {
++++++++ return tpl.raw[0].replace(/^\s+/gm, '').replace(/\n/gm, '').replace(/\\r/gm, '\r').replace(/\\n/gm, '\n')
++++++++}
++++++++
++++++++const urlExecutable = resolve(__dirname, "../test/tmp/url-url-c");
++++++++const httpExecutable = resolve(__dirname, "../test/tmp/http-request-c");
++++++++
++++++++const httpRequests: Record<string, string> = {
++++++++ "seanmonstar/httparse": request`
++++++++ GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n
++++++++ Host: www.kittyhell.com\r\n
++++++++ User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n
++++++++ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
++++++++ Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n
++++++++ Accept-Encoding: gzip,deflate\r\n
++++++++ Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n
++++++++ Keep-Alive: 115\r\n
++++++++ Connection: keep-alive\r\n
++++++++ Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n\r\n
++++++++ `,
++++++++ "nodejs/http-parser": request`
++++++++ POST /joyent/http-parser HTTP/1.1\r\n
++++++++ Host: github.com\r\n
++++++++ DNT: 1\r\n
++++++++ Accept-Encoding: gzip, deflate, sdch\r\n
++++++++ Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n
++++++++ User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)
++++++++ AppleWebKit/537.36 (KHTML, like Gecko)
++++++++ Chrome/39.0.2171.65 Safari/537.36\r\n
++++++++ Accept: text/html,application/xhtml+xml,application/xml;q=0.9,
++++++++ image/webp,*/*;q=0.8\r\n
++++++++ Referer: https://github.com/joyent/http-parser\r\n
++++++++ Connection: keep-alive\r\n
++++++++ Transfer-Encoding: chunked\r\n
++++++++ Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n
++++++++ `
++++++++}
++++++++const urlRequest = "http://example.com/path/to/file?query=value#fragment";
++++++++
++++++++if (!existsSync(urlExecutable) || !existsSync(urlExecutable)) {
++++++++ console.error(
++++++++ "\x1b[31m\x1b[1mPlease run npm test in order to create required executables."
++++++++ );
++++++++ process.exit(1);
++++++++}
++++++++
++++++++if (process.argv[2] === "loop") {
++++++++ const reqName = process.argv[3];
++++++++ const request = httpRequests[reqName]!;
++++++++
++++++++ assert(request, `Unknown request name: "${reqName}"`);
++++++++ spawnSync(httpExecutable, ["loop", request], { stdio: "inherit" });
++++++++ process.exit(0);
++++++++}
++++++++
++++++++if (!process.argv[2] || process.argv[2] === "url") {
++++++++ console.log("url (C)");
++++++++ spawnSync(urlExecutable, ["bench", urlRequest], { stdio: "inherit" });
++++++++}
++++++++
++++++++if (!process.argv[2] || process.argv[2] === "http") {
++++++++ for (const [name, request] of Object.entries(httpRequests)) {
++++++++ console.log('http: "%s" (C)', name);
++++++++ spawnSync(httpExecutable, ["bench", request], { stdio: "inherit" });
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { execSync } from 'child_process';
++++++++import { copyFileSync, mkdirSync } from 'fs';
++++++++import { join, resolve } from 'path';
++++++++
++++++++let platform = process.env.WASM_PLATFORM ?? '';
++++++++const WASM_OUT = resolve(__dirname, '../build/wasm');
++++++++const WASM_SRC = resolve(__dirname, '../');
++++++++
++++++++if (!platform && process.argv[2]) {
++++++++ platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim();
++++++++}
++++++++
++++++++if (process.argv[2] === '--prebuild') {
++++++++ const cmd = `docker build --platform=${platform.toString().trim()} -t llhttp_wasm_builder .`;
++++++++
++++++++ // eslint-disable-next-line no-console
++++++++ console.log(`> ${cmd}\n\n`);
++++++++ execSync(cmd, { stdio: 'inherit' });
++++++++
++++++++ process.exit(0);
++++++++}
++++++++
++++++++if (process.argv[2] === '--setup') {
++++++++ try {
++++++++ mkdirSync(join(WASM_SRC, 'build'));
++++++++ process.exit(0);
++++++++ } catch (error: unknown) {
++++++++ if (isErrorWithCode(error) && error.code !== 'EEXIST') {
++++++++ throw error;
++++++++ }
++++++++ process.exit(0);
++++++++ }
++++++++}
++++++++
++++++++if (process.argv[2] === '--docker') {
++++++++ let cmd = `docker run --rm -it --platform=${platform.toString().trim()}`;
++++++++ // Try to avoid root permission problems on compiled assets
++++++++ // when running on linux.
++++++++ // It will work flawessly if uid === gid === 1000
++++++++ // there will be some warnings otherwise.
++++++++ if (process.platform === 'linux') {
++++++++ cmd += ` --user ${process.getuid!()}:${process.getegid!()}`;
++++++++ }
++++++++ cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`;
++++++++
++++++++ // eslint-disable-next-line no-console
++++++++ console.log(`> ${cmd}\n\n`);
++++++++ execSync(cmd, { cwd: WASM_SRC, stdio: 'inherit' });
++++++++ process.exit(0);
++++++++}
++++++++
++++++++try {
++++++++ mkdirSync(WASM_OUT);
++++++++} catch (error: unknown) {
++++++++ if (isErrorWithCode(error) && error.code !== 'EEXIST') {
++++++++ throw error;
++++++++ }
++++++++}
++++++++
++++++++// Build ts
++++++++execSync('npm run build', { cwd: WASM_SRC, stdio: 'inherit' });
++++++++
++++++++// Build wasm binary
++++++++execSync(
++++++++ `clang \
++++++++ --sysroot=/usr/share/wasi-sysroot \
++++++++ -target wasm32-unknown-wasi \
++++++++ -Ofast \
++++++++ -fno-exceptions \
++++++++ -fvisibility=hidden \
++++++++ -mexec-model=reactor \
++++++++ -Wl,-error-limit=0 \
++++++++ -Wl,-O3 \
++++++++ -Wl,--lto-O3 \
++++++++ -Wl,--strip-all \
++++++++ -Wl,--allow-undefined \
++++++++ -Wl,--export-dynamic \
++++++++ -Wl,--export-table \
++++++++ -Wl,--export=malloc \
++++++++ -Wl,--export=free \
++++++++ -Wl,--no-entry \
++++++++ ${join(WASM_SRC, 'build', 'c')}/*.c \
++++++++ ${join(WASM_SRC, 'src', 'native')}/*.c \
++++++++ -I${join(WASM_SRC, 'build')} \
++++++++ -o ${join(WASM_OUT, 'llhttp.wasm')}`,
++++++++ { stdio: 'inherit' },
++++++++);
++++++++
++++++++// Copy constants for `.js` and `.ts` users.
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js'), join(WASM_OUT, 'constants.js'));
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js.map'), join(WASM_OUT, 'constants.js.map'));
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.d.ts'), join(WASM_OUT, 'constants.d.ts'));
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js'), join(WASM_OUT, 'utils.js'));
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js.map'), join(WASM_OUT, 'utils.js.map'));
++++++++copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.d.ts'), join(WASM_OUT, 'utils.d.ts'));
++++++++
++++++++function isErrorWithCode(error: unknown): error is Error & { code: string } {
++++++++ return typeof error === 'object' && error !== null && 'code' in error;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#!/usr/bin/env -S npx ts-node
++++++++
++++++++import { mkdirSync, readFileSync, writeFileSync } from 'fs';
++++++++import { LLParse } from 'llparse';
++++++++import { dirname, resolve } from 'path';
++++++++import { CHeaders, HTTP } from '../src/llhttp';
++++++++
++++++++// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
++++++++// eslint-disable-next-line max-len
++++++++const semverRE = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
++++++++
++++++++const C_FILE = resolve(__dirname, '../build/c/llhttp.c');
++++++++const HEADER_FILE = resolve(__dirname, '../build/llhttp.h');
++++++++
++++++++const pkg = JSON.parse(
++++++++ readFileSync(resolve(__dirname, '..', 'package.json')).toString(),
++++++++);
++++++++
++++++++const version = pkg.version.match(semverRE)?.groups;
++++++++const llparse = new LLParse('llhttp__internal');
++++++++
++++++++const cHeaders = new CHeaders();
++++++++const nativeHeaders = readFileSync(resolve(__dirname, '../src/native/api.h'));
++++++++const generated = llparse.build(new HTTP(llparse).build().entry, {
++++++++ c: {
++++++++ header: 'llhttp',
++++++++ },
++++++++ debug: process.env.LLPARSE_DEBUG ? 'llhttp__debug' : undefined,
++++++++ headerGuard: 'INCLUDE_LLHTTP_ITSELF_H_',
++++++++});
++++++++
++++++++const headers = `
++++++++#ifndef INCLUDE_LLHTTP_H_
++++++++#define INCLUDE_LLHTTP_H_
++++++++
++++++++#define LLHTTP_VERSION_MAJOR ${version.major}
++++++++#define LLHTTP_VERSION_MINOR ${version.minor}
++++++++#define LLHTTP_VERSION_PATCH ${version.patch}
++++++++
++++++++${generated.header}
++++++++
++++++++${cHeaders.build()}
++++++++
++++++++${nativeHeaders}
++++++++
++++++++#endif /* INCLUDE_LLHTTP_H_ */
++++++++`;
++++++++
++++++++mkdirSync(dirname(C_FILE), { recursive: true });
++++++++writeFileSync(HEADER_FILE, headers);
++++++++writeFileSync(C_FILE, generated.c);
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# How to release a new version of llhttp
++++++++
++++++++## What does releasing involves?
++++++++
++++++++These are the required steps to release a new version of llhttp:
++++++++
++++++++1. Increase the version number.
++++++++2. Build it locally.
++++++++3. Create a new build and push it to GitHub.
++++++++4. Create a new release on GitHub release.
++++++++
++++++++> Do not try to execute the commands in the Makefile manually. This is really error-prone!
++++++++
++++++++## Which commands to run?
++++++++
++++++++First of all, make sure you have [GitHub CLI](https://cli.github.com) installed and configured. While this is not strictly necessary, it will make your life way easier.
++++++++
++++++++As a preliminary check, lint the code, run the build command and execute the test suite locally:
++++++++
++++++++```
++++++++npm run lint
++++++++npm run build
++++++++npm run test
++++++++```
++++++++
++++++++If all goes good, you are ready to go!
++++++++
++++++++To release a new version of llhttp, first increase the version using `npm` and make sure it also execute the `postversion` script. Unless you have some very specific setup, this should happen automatically, which means the following command will suffice:
++++++++
++++++++```
++++++++npm version [major|minor|patch]
++++++++```
++++++++
++++++++The command will increase the version and then will create a new release branch on GitHub.
++++++++
++++++++> Even thought there is a package on NPM, it is not updated anymore. NEVER RUN `npm publish`!
++++++++
++++++++It's now time to create the release on GitHub. If you DON'T have GitHub CLI available, skip to the next section, otherwise run the following command:
++++++++
++++++++```
++++++++npm run github-release
++++++++```
++++++++
++++++++This command will create a draft release on GitHub and then show it in your browser so you can review and publish it.
++++++++
++++++++Congratulation, you are all set!
++++++++
++++++++## Create a GitHub release without GitHub CLI
++++++++
++++++++> From now on, `$VERSION` will be the new version you are trying to create, including the leading letter, for instance `v6.0.9`.
++++++++
++++++++If you don't want to or can't use GitHub CLI, you can still create the release on GitHub following this procedure.
++++++++
++++++++1. Go on GitHub and start creating a new release which targets tag `$VERSION`. Generate the notes using the `Generate release notes` button.
++++++++
++++++++2. At the bottom of the generated notes, make sure the previous and current version in the notes are correct.
++++++++
++++++++ The last line should be something like this: `**Full Changelog**: https://github.com/nodejs/llhttp/compare/v6.0.8...v6.0.9`
++++++++
++++++++ In this case it says we are creating release `v6.0.9` and we are showing the changes between `v6.0.8` and `v6.0.9`.
++++++++
++++++++3. Change the target of the release to point to tag `release/$VERSION`.
++++++++
++++++++4. Review and then publish the release.
++++++++
++++++++Congratulation, you are all set!
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "extends": [
++++++++ "eslint:recommended",
++++++++ "plugin:@typescript-eslint/recommended",
++++++++ "plugin:@stylistic/recommended-extends"
++++++++ ],
++++++++ "rules": {
++++++++ "max-len": [
++++++++ "error",
++++++++ {
++++++++ "code": 120
++++++++ }
++++++++ ],
++++++++ "no-console": "error",
++++++++ "@typescript-eslint/no-unused-vars": "error",
++++++++ "@stylistic/array-bracket-spacing": ["error", "always"],
++++++++ "@stylistic/operator-linebreak": ["error", "after"],
++++++++ "@stylistic/brace-style": ["error", "1tbs", { "allowSingleLine": true }],
++++++++ "@stylistic/member-delimiter-style": ["error", {
++++++++ "overrides": {
++++++++ "interface": {
++++++++ "multiline": {
++++++++ "delimiter": "semi",
++++++++ "requireLast": true
++++++++ }
++++++++ }
++++++++ }
++++++++ }],
++++++++ "indent": "off",
++++++++ "@stylistic/indent": ["error", 2, {
++++++++ "SwitchCase": 1,
++++++++ "FunctionDeclaration": { "parameters": "first" },
++++++++ "FunctionExpression": { "parameters": "first" }
++++++++ }],
++++++++ "semi": "off",
++++++++ "@stylistic/semi": ["error", "always"]
++++++++ },
++++++++ "parser": "@typescript-eslint/parser",
++++++++ "parserOptions": {
++++++++ "project": [
++++++++ "tsconfig.eslint.json"
++++++++ ],
++++++++ "sourceType": "module",
++++++++ "ecmaFeatures": {
++++++++ "jsx": true,
++++++++ "modules": true,
++++++++ "experimentalObjectRestSpread": true
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++/**
++++++++ * A minimal Parser that mimicks a small fraction of the Node.js parser
++++++++ * API.
++++++++ * To run:
++++++++ * - `npm run build-wasm`
++++++++ * - `npx ts-node examples/wasm.ts`
++++++++ */
++++++++import { readFileSync } from 'fs';
++++++++import { resolve } from 'path';
++++++++import * as constants from '../build/wasm/constants';
++++++++
++++++++const bin = readFileSync(resolve(__dirname, '../build/wasm/llhttp.wasm'));
++++++++const mod = new WebAssembly.Module(bin);
++++++++
++++++++const REQUEST = constants.TYPE.REQUEST;
++++++++const RESPONSE = constants.TYPE.RESPONSE;
++++++++const kOnMessageBegin = 0;
++++++++const kOnHeaders = 1;
++++++++const kOnHeadersComplete = 2;
++++++++const kOnBody = 3;
++++++++const kOnMessageComplete = 4;
++++++++const kOnExecute = 5;
++++++++
++++++++const kPtr = Symbol('kPtr');
++++++++const kUrl = Symbol('kUrl');
++++++++const kStatusMessage = Symbol('kStatusMessage');
++++++++const kHeadersFields = Symbol('kHeadersFields');
++++++++const kHeadersValues = Symbol('kHeadersValues');
++++++++const kBody = Symbol('kBody');
++++++++const kReset = Symbol('kReset');
++++++++const kCheckErr = Symbol('kCheckErr');
++++++++
++++++++const cstr = (ptr: number, len: number): string =>
++++++++ Buffer.from(memory.buffer, ptr, len).toString();
++++++++
++++++++const wasm_on_message_begin = (p: number) => {
++++++++ const i = instMap.get(p);
++++++++ i[kReset]();
++++++++ return i[kOnMessageBegin]();
++++++++};
++++++++
++++++++const wasm_on_url = (p: number, at: number, length: number) => {
++++++++ instMap.get(p)[kUrl] = cstr(at, length);
++++++++ return 0;
++++++++};
++++++++
++++++++const wasm_on_status = (p: number, at: number, length: number) => {
++++++++ instMap.get(p)[kStatusMessage] = cstr(at, length);
++++++++ return 0;
++++++++};
++++++++
++++++++const wasm_on_header_field = (p: number, at: number, length: number) => {
++++++++ const i= instMap.get(p)
++++++++ i[kHeadersFields].push(cstr(at, length));
++++++++ return 0;
++++++++};
++++++++
++++++++const wasm_on_header_value = (p: number, at: number, length: number) => {
++++++++ const i = instMap.get(p);
++++++++ i[kHeadersValues].push(cstr(at, length));
++++++++ return 0;
++++++++};
++++++++
++++++++const wasm_on_headers_complete = (p: number) => {
++++++++ const i = instMap.get(p);
++++++++ const type = get_type(p);
++++++++ const versionMajor = get_version_major(p);
++++++++ const versionMinor = get_version_minor(p);
++++++++ const rawHeaders = [];
++++++++ let method;
++++++++ let url;
++++++++ let statusCode;
++++++++ let statusMessage;
++++++++ const upgrade = get_upgrade(p);
++++++++ const shouldKeepAlive = should_keep_alive(p);
++++++++
++++++++ for (let c = 0; c < i[kHeadersFields].length; c++) {
++++++++ rawHeaders.push(i[kHeadersFields][c], i[kHeadersValues][c])
++++++++ }
++++++++
++++++++ if (type === HTTPParser.REQUEST) {
++++++++ method = constants.METHODS[get_method(p)];
++++++++ url = i[kUrl];
++++++++ } else if (type === HTTPParser.RESPONSE) {
++++++++ statusCode = get_status_code(p);
++++++++ statusMessage = i[kStatusMessage];
++++++++ }
++++++++ return i[kOnHeadersComplete](versionMajor, versionMinor, rawHeaders, method,
++++++++url, statusCode, statusMessage, upgrade, shouldKeepAlive);
++++++++};
++++++++
++++++++const wasm_on_body = (p: number, at: number, length: number) => {
++++++++ const i = instMap.get(p);
++++++++ const body = Buffer.from(memory.buffer, at, length);
++++++++ return i[kOnBody](body);
++++++++};
++++++++
++++++++const wasm_on_message_complete = (p: number) => {
++++++++ return instMap.get(p)[kOnMessageComplete]();
++++++++};
++++++++
++++++++const instMap = new Map();
++++++++
++++++++const inst = new WebAssembly.Instance(mod, {
++++++++ env: {
++++++++ wasm_on_message_begin,
++++++++ wasm_on_url,
++++++++ wasm_on_status,
++++++++ wasm_on_header_field,
++++++++ wasm_on_header_value,
++++++++ wasm_on_headers_complete,
++++++++ wasm_on_body,
++++++++ wasm_on_message_complete,
++++++++ },
++++++++});
++++++++
++++++++const memory = inst.exports.memory as any;
++++++++const alloc = inst.exports.llhttp_alloc as CallableFunction;
++++++++const malloc = inst.exports.malloc as CallableFunction;
++++++++const execute = inst.exports.llhttp_execute as CallableFunction;
++++++++const get_type = inst.exports.llhttp_get_type as CallableFunction;
++++++++const get_upgrade = inst.exports.llhttp_get_upgrade as CallableFunction;
++++++++const should_keep_alive = inst.exports.llhttp_should_keep_alive as CallableFunction;
++++++++const get_method = inst.exports.llhttp_get_method as CallableFunction;
++++++++const get_status_code = inst.exports.llhttp_get_status_code as CallableFunction;
++++++++const get_version_minor = inst.exports.llhttp_get_http_minor as CallableFunction;
++++++++const get_version_major = inst.exports.llhttp_get_http_major as CallableFunction;
++++++++const get_error_reason = inst.exports.llhttp_get_error_reason as CallableFunction;
++++++++const free = inst.exports.free as CallableFunction;
++++++++const initialize = inst.exports._initialize as CallableFunction;
++++++++
++++++++initialize(); // wasi reactor
++++++++
++++++++class HTTPParser {
++++++++ static REQUEST = REQUEST;
++++++++ static RESPONSE = RESPONSE;
++++++++ static kOnMessageBegin = kOnMessageBegin;
++++++++ static kOnHeaders = kOnHeaders;
++++++++ static kOnHeadersComplete = kOnHeadersComplete;
++++++++ static kOnBody = kOnBody;
++++++++ static kOnMessageComplete = kOnMessageComplete;
++++++++ static kOnExecute = kOnExecute;
++++++++
++++++++ [kPtr]: number;
++++++++ [kUrl]: string;
++++++++ [kStatusMessage]: null|string;
++++++++ [kHeadersFields]: []|[string];
++++++++ [kHeadersValues]: []|[string];
++++++++ [kBody]: null|Buffer;
++++++++
++++++++ constructor(type: constants.TYPE) {
++++++++ this[kPtr] = alloc(constants.TYPE[type]);
++++++++ instMap.set(this[kPtr], this);
++++++++
++++++++ this[kUrl] = '';
++++++++ this[kStatusMessage] = null;
++++++++ this[kHeadersFields] = [];
++++++++ this[kHeadersValues] = [];
++++++++ this[kBody] = null;
++++++++ }
++++++++
++++++++ [kReset]() {
++++++++ this[kUrl] = '';
++++++++ this[kStatusMessage] = null;
++++++++ this[kHeadersFields] = [];
++++++++ this[kHeadersValues] = [];
++++++++ this[kBody] = null;
++++++++ }
++++++++
++++++++ [kOnMessageBegin]() {
++++++++ return 0;
++++++++ }
++++++++
++++++++ [kOnHeaders](rawHeaders: [string]) {}
++++++++
++++++++ [kOnHeadersComplete](versionMajor: number, versionMinor: number, rawHeaders: [string], method: string,
++++++++ url: string, statusCode: number, statusMessage: string, upgrade: boolean, shouldKeepAlive: boolean) {
++++++++ return 0;
++++++++ }
++++++++
++++++++ [kOnBody](body: Buffer) {
++++++++ this[kBody] = body;
++++++++ return 0;
++++++++ }
++++++++
++++++++ [kOnMessageComplete]() {
++++++++ return 0;
++++++++ }
++++++++
++++++++ destroy() {
++++++++ instMap.delete(this[kPtr]);
++++++++ free(this[kPtr]);
++++++++ }
++++++++
++++++++ execute(data: Buffer) {
++++++++ const ptr = malloc(data.byteLength);
++++++++ const u8 = new Uint8Array(memory.buffer);
++++++++ u8.set(data, ptr);
++++++++ const ret = execute(this[kPtr], ptr, data.length);
++++++++ free(ptr);
++++++++ this[kCheckErr](ret);
++++++++ return ret;
++++++++ }
++++++++
++++++++ [kCheckErr](n: number) {
++++++++ if (n === constants.ERROR.OK) {
++++++++ return;
++++++++ }
++++++++ const ptr = get_error_reason(this[kPtr]);
++++++++ const u8 = new Uint8Array(memory.buffer);
++++++++ const len = u8.indexOf(0, ptr) - ptr;
++++++++ throw new Error(cstr(ptr, len));
++++++++ }
++++++++}
++++++++
++++++++
++++++++{
++++++++ const p = new HTTPParser(HTTPParser.REQUEST);
++++++++
++++++++ p.execute(Buffer.from([
++++++++ 'POST /owo HTTP/1.1',
++++++++ 'X: Y',
++++++++ 'Content-Length: 9',
++++++++ '',
++++++++ 'uh, meow?',
++++++++ '',
++++++++ ].join('\r\n')));
++++++++
++++++++ console.log(p);
++++++++
++++++++ p.destroy();
++++++++}
++++++++
++++++++{
++++++++ const p = new HTTPParser(HTTPParser.RESPONSE);
++++++++
++++++++ p.execute(Buffer.from([
++++++++ 'HTTP/1.1 200 OK',
++++++++ 'X: Y',
++++++++ 'Content-Length: 9',
++++++++ '',
++++++++ 'uh, meow?'
++++++++ ].join('\r\n')));
++++++++
++++++++ console.log(p);
++++++++
++++++++ p.destroy();
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++prefix=@CMAKE_INSTALL_PREFIX@
++++++++exec_prefix=@CMAKE_INSTALL_PREFIX@
++++++++libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@
++++++++includedir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@
++++++++
++++++++Name: libllhttp
++++++++Description: Node.js llhttp Library
++++++++Version: @PROJECT_VERSION@
++++++++Libs: -L${libdir} -lllhttp
++++++++Cflags: -I${includedir}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llhttp",
++++++++ "version": "9.2.1",
++++++++ "lockfileVersion": 3,
++++++++ "requires": true,
++++++++ "packages": {
++++++++ "": {
++++++++ "name": "llhttp",
++++++++ "version": "9.2.1",
++++++++ "license": "MIT",
++++++++ "dependencies": {
++++++++ "llparse": "^7.1.1"
++++++++ },
++++++++ "devDependencies": {
++++++++ "@stylistic/eslint-plugin": "^1.5.4",
++++++++ "@types/node": "^20.11.10",
++++++++ "@typescript-eslint/eslint-plugin": "^7.0.0",
++++++++ "@typescript-eslint/parser": "^6.20.0",
++++++++ "eslint": "^8.56.0",
++++++++ "llparse-dot": "^1.0.1",
++++++++ "llparse-test-fixture": "^5.0.1",
++++++++ "mdgator": "^1.1.2",
++++++++ "ts-node": "^10.9.2",
++++++++ "typescript": "^5.3.3"
++++++++ }
++++++++ },
++++++++ "node_modules/@aashutoshrathi/word-wrap": {
++++++++ "version": "1.2.6",
++++++++ "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz",
++++++++ "integrity": "sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@cspotcode/source-map-support": {
++++++++ "version": "0.8.1",
++++++++ "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz",
++++++++ "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@jridgewell/trace-mapping": "0.3.9"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=12"
++++++++ }
++++++++ },
++++++++ "node_modules/@eslint-community/eslint-utils": {
++++++++ "version": "4.4.0",
++++++++ "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.4.0.tgz",
++++++++ "integrity": "sha512-1/sA4dwrzBAyeUoQ6oxahHKmrZvsnLCg4RfxW3ZFGGmQkSNQPFNLV9CUEFQP1x9EYXHTo5p6xdhZM1Ne9p/AfA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "eslint-visitor-keys": "^3.3.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^6.0.0 || ^7.0.0 || >=8.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@eslint-community/regexpp": {
++++++++ "version": "4.10.0",
++++++++ "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.10.0.tgz",
++++++++ "integrity": "sha512-Cu96Sd2By9mCNTx2iyKOmq10v22jUVQv0lQnlGNy16oE9589yE+QADPbrMGCkA51cKZSg3Pu/aTJVTGfL/qjUA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^12.0.0 || ^14.0.0 || >=16.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@eslint/eslintrc": {
++++++++ "version": "2.1.4",
++++++++ "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-2.1.4.tgz",
++++++++ "integrity": "sha512-269Z39MS6wVJtsoUl10L60WdkhJVdPG24Q4eZTH3nnF6lpvSShEK3wQjDX9JRWAUPvPh7COouPpU9IrqaZFvtQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "ajv": "^6.12.4",
++++++++ "debug": "^4.3.2",
++++++++ "espree": "^9.6.0",
++++++++ "globals": "^13.19.0",
++++++++ "ignore": "^5.2.0",
++++++++ "import-fresh": "^3.2.1",
++++++++ "js-yaml": "^4.1.0",
++++++++ "minimatch": "^3.1.2",
++++++++ "strip-json-comments": "^3.1.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://opencollective.com/eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@eslint/eslintrc/node_modules/argparse": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
++++++++ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@eslint/eslintrc/node_modules/js-yaml": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
++++++++ "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "argparse": "^2.0.1"
++++++++ },
++++++++ "bin": {
++++++++ "js-yaml": "bin/js-yaml.js"
++++++++ }
++++++++ },
++++++++ "node_modules/@eslint/js": {
++++++++ "version": "8.57.0",
++++++++ "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.57.0.tgz",
++++++++ "integrity": "sha512-Ys+3g2TaW7gADOJzPt83SJtCDhMjndcDMFVQ/Tj9iA1BfJzFKD9mAUXT3OenpuPHbI6P/myECxRJrofUsDx/5g==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@humanwhocodes/config-array": {
++++++++ "version": "0.11.14",
++++++++ "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz",
++++++++ "integrity": "sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@humanwhocodes/object-schema": "^2.0.2",
++++++++ "debug": "^4.3.1",
++++++++ "minimatch": "^3.0.5"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@humanwhocodes/module-importer": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/@humanwhocodes/module-importer/-/module-importer-1.0.1.tgz",
++++++++ "integrity": "sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=12.22"
++++++++ },
++++++++ "funding": {
++++++++ "type": "github",
++++++++ "url": "https://github.com/sponsors/nzakas"
++++++++ }
++++++++ },
++++++++ "node_modules/@humanwhocodes/object-schema": {
++++++++ "version": "2.0.2",
++++++++ "resolved": "https://registry.npmjs.org/@humanwhocodes/object-schema/-/object-schema-2.0.2.tgz",
++++++++ "integrity": "sha512-6EwiSjwWYP7pTckG6I5eyFANjPhmPjUX9JRLUSfNPC7FX7zK9gyZAfUEaECL6ALTpGX5AjnBq3C9XmVWPitNpw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@jridgewell/resolve-uri": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz",
++++++++ "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@jridgewell/sourcemap-codec": {
++++++++ "version": "1.4.15",
++++++++ "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz",
++++++++ "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@jridgewell/trace-mapping": {
++++++++ "version": "0.3.9",
++++++++ "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz",
++++++++ "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@jridgewell/resolve-uri": "^3.0.3",
++++++++ "@jridgewell/sourcemap-codec": "^1.4.10"
++++++++ }
++++++++ },
++++++++ "node_modules/@nodelib/fs.scandir": {
++++++++ "version": "2.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
++++++++ "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@nodelib/fs.stat": "2.0.5",
++++++++ "run-parallel": "^1.1.9"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/@nodelib/fs.stat": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz",
++++++++ "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/@nodelib/fs.walk": {
++++++++ "version": "1.2.8",
++++++++ "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz",
++++++++ "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@nodelib/fs.scandir": "2.1.5",
++++++++ "fastq": "^1.6.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin": {
++++++++ "version": "1.6.3",
++++++++ "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin/-/eslint-plugin-1.6.3.tgz",
++++++++ "integrity": "sha512-WDa4FjhImp7YcztRaMG09svhKYYhi2Hc4p9ltQRSqyB4fsUUFm+GKzStqqH7xfjHnxacMJaOnaMGRTUqIIZDLA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@stylistic/eslint-plugin-js": "1.6.3",
++++++++ "@stylistic/eslint-plugin-jsx": "1.6.3",
++++++++ "@stylistic/eslint-plugin-plus": "1.6.3",
++++++++ "@stylistic/eslint-plugin-ts": "1.6.3",
++++++++ "@types/eslint": "^8.56.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": ">=8.40.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin-js": {
++++++++ "version": "1.6.3",
++++++++ "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin-js/-/eslint-plugin-js-1.6.3.tgz",
++++++++ "integrity": "sha512-ckdz51oHxD2FaxgY2piJWJVJiwgp8Uu96s+as2yB3RMwavn3nHBrpliVukXY9S/DmMicPRB2+H8nBk23GDG+qA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@types/eslint": "^8.56.2",
++++++++ "acorn": "^8.11.3",
++++++++ "escape-string-regexp": "^4.0.0",
++++++++ "eslint-visitor-keys": "^3.4.3",
++++++++ "espree": "^9.6.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": ">=8.40.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin-jsx": {
++++++++ "version": "1.6.3",
++++++++ "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin-jsx/-/eslint-plugin-jsx-1.6.3.tgz",
++++++++ "integrity": "sha512-SRysCIg59Zvn3dJPqHziiHwuni4NNj1et5stAmivmyQ3Cdp2ULCB7tGxCF1OxpkwRlZQue3ZgdiM7EXfJKaf9w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@stylistic/eslint-plugin-js": "^1.6.3",
++++++++ "@types/eslint": "^8.56.2",
++++++++ "estraverse": "^5.3.0",
++++++++ "picomatch": "^4.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": ">=8.40.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin-jsx/node_modules/picomatch": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.1.tgz",
++++++++ "integrity": "sha512-xUXwsxNjwTQ8K3GnT4pCJm+xq3RUPQbmkYJTP5aFIfNIvbcc/4MUxgBaaRSZJ6yGJZiGSyYlM6MzwTsRk8SYCg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=12"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/jonschlinkert"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin-plus": {
++++++++ "version": "1.6.3",
++++++++ "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin-plus/-/eslint-plugin-plus-1.6.3.tgz",
++++++++ "integrity": "sha512-TuwQOdyVGycDPw5XeF7W4f3ZonAVzOAzORSaD2yGAJ0fRAbJ+l/v3CkKzIAqBBwWkc+c2aRMsWtLP2+viBnmlQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@types/eslint": "^8.56.2",
++++++++ "@typescript-eslint/utils": "^6.21.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "*"
++++++++ }
++++++++ },
++++++++ "node_modules/@stylistic/eslint-plugin-ts": {
++++++++ "version": "1.6.3",
++++++++ "resolved": "https://registry.npmjs.org/@stylistic/eslint-plugin-ts/-/eslint-plugin-ts-1.6.3.tgz",
++++++++ "integrity": "sha512-v5GwZsPLblWM9uAIdaSi31Sed3XBWlTFQJ3b5upEmj6QsKYivA5nmIYutwqqL133QdVWjmC86pINlx2Muq3uNQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@stylistic/eslint-plugin-js": "1.6.3",
++++++++ "@types/eslint": "^8.56.2",
++++++++ "@typescript-eslint/utils": "^6.21.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": ">=8.40.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@tsconfig/node10": {
++++++++ "version": "1.0.9",
++++++++ "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz",
++++++++ "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@tsconfig/node12": {
++++++++ "version": "1.0.11",
++++++++ "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz",
++++++++ "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@tsconfig/node14": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz",
++++++++ "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@tsconfig/node16": {
++++++++ "version": "1.0.4",
++++++++ "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz",
++++++++ "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ=="
++++++++ },
++++++++ "node_modules/@types/eslint": {
++++++++ "version": "8.56.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.5.tgz",
++++++++ "integrity": "sha512-u5/YPJHo1tvkSF2CE0USEkxon82Z5DBy2xR+qfyYNszpX9qcs4sT6uq2kBbj4BXY1+DBGDPnrhMZV3pKWGNukw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@types/estree": "*",
++++++++ "@types/json-schema": "*"
++++++++ }
++++++++ },
++++++++ "node_modules/@types/estree": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz",
++++++++ "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@types/json-schema": {
++++++++ "version": "7.0.15",
++++++++ "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz",
++++++++ "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@types/markdown-it": {
++++++++ "version": "0.0.4",
++++++++ "resolved": "https://registry.npmjs.org/@types/markdown-it/-/markdown-it-0.0.4.tgz",
++++++++ "integrity": "sha512-FWR7QB7EqBRq1s9BMk0ccOSOuRLfVEWYpHQYpFPaXtCoqN6dJx2ttdsdQbUxLLnAlKpYeVjveGGhQ3583TTa7g==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@types/node": {
++++++++ "version": "20.11.27",
++++++++ "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.27.tgz",
++++++++ "integrity": "sha512-qyUZfMnCg1KEz57r7pzFtSGt49f6RPkPBis3Vo4PbS7roQEDn22hiHzl/Lo1q4i4hDEgBJmBF/NTNg2XR0HbFg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "undici-types": "~5.26.4"
++++++++ }
++++++++ },
++++++++ "node_modules/@types/semver": {
++++++++ "version": "7.5.6",
++++++++ "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.6.tgz",
++++++++ "integrity": "sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/@typescript-eslint/eslint-plugin": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.0.0.tgz",
++++++++ "integrity": "sha512-M72SJ0DkcQVmmsbqlzc6EJgb/3Oz2Wdm6AyESB4YkGgCxP8u5jt5jn4/OBMPK3HLOxcttZq5xbBBU7e2By4SZQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@eslint-community/regexpp": "^4.5.1",
++++++++ "@typescript-eslint/scope-manager": "7.0.0",
++++++++ "@typescript-eslint/type-utils": "7.0.0",
++++++++ "@typescript-eslint/utils": "7.0.0",
++++++++ "@typescript-eslint/visitor-keys": "7.0.0",
++++++++ "debug": "^4.3.4",
++++++++ "graphemer": "^1.4.0",
++++++++ "ignore": "^5.2.4",
++++++++ "natural-compare": "^1.4.0",
++++++++ "semver": "^7.5.4",
++++++++ "ts-api-utils": "^1.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "@typescript-eslint/parser": "^6.0.0 || ^6.0.0-alpha",
++++++++ "eslint": "^8.56.0"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/eslint-plugin/node_modules/@typescript-eslint/utils": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-7.0.0.tgz",
++++++++ "integrity": "sha512-kuPZcPAdGcDBAyqDn/JVeJVhySvpkxzfXjJq1X1BFSTYo1TTuo4iyb937u457q4K0In84p6u2VHQGaFnv7VYqg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@eslint-community/eslint-utils": "^4.4.0",
++++++++ "@types/json-schema": "^7.0.12",
++++++++ "@types/semver": "^7.5.0",
++++++++ "@typescript-eslint/scope-manager": "7.0.0",
++++++++ "@typescript-eslint/types": "7.0.0",
++++++++ "@typescript-eslint/typescript-estree": "7.0.0",
++++++++ "semver": "^7.5.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^8.56.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-6.21.0.tgz",
++++++++ "integrity": "sha512-tbsV1jPne5CkFQCgPBcDOt30ItF7aJoZL997JSF7MhGQqOeT3svWRYxiqlfA5RUdlHN6Fi+EI9bxqbdyAUZjYQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/scope-manager": "6.21.0",
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/typescript-estree": "6.21.0",
++++++++ "@typescript-eslint/visitor-keys": "6.21.0",
++++++++ "debug": "^4.3.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^7.0.0 || ^8.0.0"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/scope-manager": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.21.0.tgz",
++++++++ "integrity": "sha512-OwLUIWZJry80O99zvqXVEioyniJMa+d2GrqpUTqi5/v5D5rOrppJVBPa0yKCblcigC0/aYAzxxqQ1B+DS2RYsg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/visitor-keys": "6.21.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/types": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.21.0.tgz",
++++++++ "integrity": "sha512-1kFmZ1rOm5epu9NZEZm1kckCDGj5UJEf7P1kliH4LKu/RkwpsfqqGmY2OOcUs18lSlQBKLDYBOGxRVtrMN5lpg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/typescript-estree": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.21.0.tgz",
++++++++ "integrity": "sha512-6npJTkZcO+y2/kr+z0hc4HwNfrrP4kNYh57ek7yCNlrBjWQ1Y0OS7jiZTkgumrvkX5HkEKXFZkkdFNkaW2wmUQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/visitor-keys": "6.21.0",
++++++++ "debug": "^4.3.4",
++++++++ "globby": "^11.1.0",
++++++++ "is-glob": "^4.0.3",
++++++++ "minimatch": "9.0.3",
++++++++ "semver": "^7.5.4",
++++++++ "ts-api-utils": "^1.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/visitor-keys": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.21.0.tgz",
++++++++ "integrity": "sha512-JJtkDduxLi9bivAB+cYOVMtbkqdPOhZ+ZI5LC47MIRrDV4Yn2o+ZnW10Nkmr28xRpSpdJ6Sm42Hjf2+REYXm0A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "eslint-visitor-keys": "^3.4.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/brace-expansion": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
++++++++ "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "balanced-match": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/parser/node_modules/minimatch": {
++++++++ "version": "9.0.3",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz",
++++++++ "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "brace-expansion": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=16 || 14 >=14.17"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/isaacs"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/scope-manager": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-7.0.0.tgz",
++++++++ "integrity": "sha512-IxTStwhNDPO07CCrYuAqjuJ3Xf5MrMaNgbAZPxFXAUpAtwqFxiuItxUaVtP/SJQeCdJjwDGh9/lMOluAndkKeg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "7.0.0",
++++++++ "@typescript-eslint/visitor-keys": "7.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/type-utils": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-7.0.0.tgz",
++++++++ "integrity": "sha512-FIM8HPxj1P2G7qfrpiXvbHeHypgo2mFpFGoh5I73ZlqmJOsloSa1x0ZyXCer43++P1doxCgNqIOLqmZR6SOT8g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/typescript-estree": "7.0.0",
++++++++ "@typescript-eslint/utils": "7.0.0",
++++++++ "debug": "^4.3.4",
++++++++ "ts-api-utils": "^1.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^8.56.0"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/type-utils/node_modules/@typescript-eslint/utils": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-7.0.0.tgz",
++++++++ "integrity": "sha512-kuPZcPAdGcDBAyqDn/JVeJVhySvpkxzfXjJq1X1BFSTYo1TTuo4iyb937u457q4K0In84p6u2VHQGaFnv7VYqg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@eslint-community/eslint-utils": "^4.4.0",
++++++++ "@types/json-schema": "^7.0.12",
++++++++ "@types/semver": "^7.5.0",
++++++++ "@typescript-eslint/scope-manager": "7.0.0",
++++++++ "@typescript-eslint/types": "7.0.0",
++++++++ "@typescript-eslint/typescript-estree": "7.0.0",
++++++++ "semver": "^7.5.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^8.56.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/types": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-7.0.0.tgz",
++++++++ "integrity": "sha512-9ZIJDqagK1TTs4W9IyeB2sH/s1fFhN9958ycW8NRTg1vXGzzH5PQNzq6KbsbVGMT+oyyfa17DfchHDidcmf5cg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/typescript-estree": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-7.0.0.tgz",
++++++++ "integrity": "sha512-JzsOzhJJm74aQ3c9um/aDryHgSHfaX8SHFIu9x4Gpik/+qxLvxUylhTsO9abcNu39JIdhY2LgYrFxTii3IajLA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "7.0.0",
++++++++ "@typescript-eslint/visitor-keys": "7.0.0",
++++++++ "debug": "^4.3.4",
++++++++ "globby": "^11.1.0",
++++++++ "is-glob": "^4.0.3",
++++++++ "minimatch": "9.0.3",
++++++++ "semver": "^7.5.4",
++++++++ "ts-api-utils": "^1.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/typescript-estree/node_modules/brace-expansion": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
++++++++ "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "balanced-match": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/typescript-estree/node_modules/minimatch": {
++++++++ "version": "9.0.3",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz",
++++++++ "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "brace-expansion": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=16 || 14 >=14.17"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/isaacs"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.21.0.tgz",
++++++++ "integrity": "sha512-NfWVaC8HP9T8cbKQxHcsJBY5YE1O33+jpMwN45qzWWaPDZgLIbo12toGMWnmhvCpd3sIxkpDw3Wv1B3dYrbDQQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@eslint-community/eslint-utils": "^4.4.0",
++++++++ "@types/json-schema": "^7.0.12",
++++++++ "@types/semver": "^7.5.0",
++++++++ "@typescript-eslint/scope-manager": "6.21.0",
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/typescript-estree": "6.21.0",
++++++++ "semver": "^7.5.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "eslint": "^7.0.0 || ^8.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/scope-manager": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.21.0.tgz",
++++++++ "integrity": "sha512-OwLUIWZJry80O99zvqXVEioyniJMa+d2GrqpUTqi5/v5D5rOrppJVBPa0yKCblcigC0/aYAzxxqQ1B+DS2RYsg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/visitor-keys": "6.21.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/types": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.21.0.tgz",
++++++++ "integrity": "sha512-1kFmZ1rOm5epu9NZEZm1kckCDGj5UJEf7P1kliH4LKu/RkwpsfqqGmY2OOcUs18lSlQBKLDYBOGxRVtrMN5lpg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/typescript-estree": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.21.0.tgz",
++++++++ "integrity": "sha512-6npJTkZcO+y2/kr+z0hc4HwNfrrP4kNYh57ek7yCNlrBjWQ1Y0OS7jiZTkgumrvkX5HkEKXFZkkdFNkaW2wmUQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "@typescript-eslint/visitor-keys": "6.21.0",
++++++++ "debug": "^4.3.4",
++++++++ "globby": "^11.1.0",
++++++++ "is-glob": "^4.0.3",
++++++++ "minimatch": "9.0.3",
++++++++ "semver": "^7.5.4",
++++++++ "ts-api-utils": "^1.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "typescript": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/@typescript-eslint/visitor-keys": {
++++++++ "version": "6.21.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.21.0.tgz",
++++++++ "integrity": "sha512-JJtkDduxLi9bivAB+cYOVMtbkqdPOhZ+ZI5LC47MIRrDV4Yn2o+ZnW10Nkmr28xRpSpdJ6Sm42Hjf2+REYXm0A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "6.21.0",
++++++++ "eslint-visitor-keys": "^3.4.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/brace-expansion": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
++++++++ "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "balanced-match": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/utils/node_modules/minimatch": {
++++++++ "version": "9.0.3",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz",
++++++++ "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "brace-expansion": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=16 || 14 >=14.17"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/isaacs"
++++++++ }
++++++++ },
++++++++ "node_modules/@typescript-eslint/visitor-keys": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-7.0.0.tgz",
++++++++ "integrity": "sha512-JZP0uw59PRHp7sHQl3aF/lFgwOW2rgNVnXUksj1d932PMita9wFBd3621vHQRDvHwPsSY9FMAAHVc8gTvLYY4w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@typescript-eslint/types": "7.0.0",
++++++++ "eslint-visitor-keys": "^3.4.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^16.0.0 || >=18.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "type": "opencollective",
++++++++ "url": "https://opencollective.com/typescript-eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/@ungap/structured-clone": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz",
++++++++ "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/acorn": {
++++++++ "version": "8.11.3",
++++++++ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz",
++++++++ "integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==",
++++++++ "dev": true,
++++++++ "bin": {
++++++++ "acorn": "bin/acorn"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=0.4.0"
++++++++ }
++++++++ },
++++++++ "node_modules/acorn-jsx": {
++++++++ "version": "5.3.2",
++++++++ "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz",
++++++++ "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==",
++++++++ "dev": true,
++++++++ "peerDependencies": {
++++++++ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/acorn-walk": {
++++++++ "version": "8.3.2",
++++++++ "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.3.2.tgz",
++++++++ "integrity": "sha512-cjkyv4OtNCIeqhHrfS81QWXoCBPExR/J62oyEqepVw8WaQeSqpW2uhuLPh1m9eWhDuOo/jUXVTlifvesOWp/4A==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.4.0"
++++++++ }
++++++++ },
++++++++ "node_modules/ajv": {
++++++++ "version": "6.12.6",
++++++++ "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
++++++++ "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "fast-deep-equal": "^3.1.1",
++++++++ "fast-json-stable-stringify": "^2.0.0",
++++++++ "json-schema-traverse": "^0.4.1",
++++++++ "uri-js": "^4.2.2"
++++++++ },
++++++++ "funding": {
++++++++ "type": "github",
++++++++ "url": "https://github.com/sponsors/epoberezkin"
++++++++ }
++++++++ },
++++++++ "node_modules/ansi-regex": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz",
++++++++ "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/arg": {
++++++++ "version": "4.1.3",
++++++++ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
++++++++ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/argparse": {
++++++++ "version": "1.0.10",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
++++++++ "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "sprintf-js": "~1.0.2"
++++++++ }
++++++++ },
++++++++ "node_modules/array-union": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz",
++++++++ "integrity": "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/balanced-match": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
++++++++ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/binary-search": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/binary-search/-/binary-search-1.3.6.tgz",
++++++++ "integrity": "sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA=="
++++++++ },
++++++++ "node_modules/brace-expansion": {
++++++++ "version": "1.1.11",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
++++++++ "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "balanced-match": "^1.0.0",
++++++++ "concat-map": "0.0.1"
++++++++ }
++++++++ },
++++++++ "node_modules/braces": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
++++++++ "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "fill-range": "^7.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/callsites": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",
++++++++ "integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/camelcase": {
++++++++ "version": "5.3.1",
++++++++ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
++++++++ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/cliui": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz",
++++++++ "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "string-width": "^4.2.0",
++++++++ "strip-ansi": "^6.0.0",
++++++++ "wrap-ansi": "^6.2.0"
++++++++ }
++++++++ },
++++++++ "node_modules/concat-map": {
++++++++ "version": "0.0.1",
++++++++ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
++++++++ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/create-require": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz",
++++++++ "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/cross-spawn": {
++++++++ "version": "7.0.3",
++++++++ "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
++++++++ "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "path-key": "^3.1.0",
++++++++ "shebang-command": "^2.0.0",
++++++++ "which": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/debug": {
++++++++ "version": "4.3.4",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
++++++++ "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
++++++++ "dependencies": {
++++++++ "ms": "2.1.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6.0"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "supports-color": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/decamelize": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
++++++++ "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/deep-is": {
++++++++ "version": "0.1.4",
++++++++ "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz",
++++++++ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.3.1"
++++++++ }
++++++++ },
++++++++ "node_modules/dir-glob": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz",
++++++++ "integrity": "sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "path-type": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/doctrine": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz",
++++++++ "integrity": "sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "esutils": "^2.0.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/emoji-regex": {
++++++++ "version": "8.0.0",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
++++++++ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/entities": {
++++++++ "version": "1.1.2",
++++++++ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz",
++++++++ "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/escape-string-regexp": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
++++++++ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint": {
++++++++ "version": "8.57.0",
++++++++ "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.57.0.tgz",
++++++++ "integrity": "sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@eslint-community/eslint-utils": "^4.2.0",
++++++++ "@eslint-community/regexpp": "^4.6.1",
++++++++ "@eslint/eslintrc": "^2.1.4",
++++++++ "@eslint/js": "8.57.0",
++++++++ "@humanwhocodes/config-array": "^0.11.14",
++++++++ "@humanwhocodes/module-importer": "^1.0.1",
++++++++ "@nodelib/fs.walk": "^1.2.8",
++++++++ "@ungap/structured-clone": "^1.2.0",
++++++++ "ajv": "^6.12.4",
++++++++ "chalk": "^4.0.0",
++++++++ "cross-spawn": "^7.0.2",
++++++++ "debug": "^4.3.2",
++++++++ "doctrine": "^3.0.0",
++++++++ "escape-string-regexp": "^4.0.0",
++++++++ "eslint-scope": "^7.2.2",
++++++++ "eslint-visitor-keys": "^3.4.3",
++++++++ "espree": "^9.6.1",
++++++++ "esquery": "^1.4.2",
++++++++ "esutils": "^2.0.2",
++++++++ "fast-deep-equal": "^3.1.3",
++++++++ "file-entry-cache": "^6.0.1",
++++++++ "find-up": "^5.0.0",
++++++++ "glob-parent": "^6.0.2",
++++++++ "globals": "^13.19.0",
++++++++ "graphemer": "^1.4.0",
++++++++ "ignore": "^5.2.0",
++++++++ "imurmurhash": "^0.1.4",
++++++++ "is-glob": "^4.0.0",
++++++++ "is-path-inside": "^3.0.3",
++++++++ "js-yaml": "^4.1.0",
++++++++ "json-stable-stringify-without-jsonify": "^1.0.1",
++++++++ "levn": "^0.4.1",
++++++++ "lodash.merge": "^4.6.2",
++++++++ "minimatch": "^3.1.2",
++++++++ "natural-compare": "^1.4.0",
++++++++ "optionator": "^0.9.3",
++++++++ "strip-ansi": "^6.0.1",
++++++++ "text-table": "^0.2.0"
++++++++ },
++++++++ "bin": {
++++++++ "eslint": "bin/eslint.js"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://opencollective.com/eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint-scope": {
++++++++ "version": "7.2.2",
++++++++ "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.2.2.tgz",
++++++++ "integrity": "sha512-dOt21O7lTMhDM+X9mB4GX+DZrZtCUJPL/wlcTqxyrx5IvO0IYtILdtrQGQp+8n5S0gwSVmOf9NQrjMOgfQZlIg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "esrecurse": "^4.3.0",
++++++++ "estraverse": "^5.2.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://opencollective.com/eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint-visitor-keys": {
++++++++ "version": "3.4.3",
++++++++ "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz",
++++++++ "integrity": "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://opencollective.com/eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/ansi-styles": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
++++++++ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "color-convert": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/chalk/ansi-styles?sponsor=1"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/argparse": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
++++++++ "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/eslint/node_modules/chalk": {
++++++++ "version": "4.1.2",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
++++++++ "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "ansi-styles": "^4.1.0",
++++++++ "supports-color": "^7.1.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/chalk/chalk?sponsor=1"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "color-name": "~1.1.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=7.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/eslint/node_modules/glob-parent": {
++++++++ "version": "6.0.2",
++++++++ "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
++++++++ "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "is-glob": "^4.0.3"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10.13.0"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/has-flag": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
++++++++ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/js-yaml": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
++++++++ "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "argparse": "^2.0.1"
++++++++ },
++++++++ "bin": {
++++++++ "js-yaml": "bin/js-yaml.js"
++++++++ }
++++++++ },
++++++++ "node_modules/eslint/node_modules/supports-color": {
++++++++ "version": "7.2.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
++++++++ "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "has-flag": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/esm": {
++++++++ "version": "3.2.25",
++++++++ "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz",
++++++++ "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/espree": {
++++++++ "version": "9.6.1",
++++++++ "resolved": "https://registry.npmjs.org/espree/-/espree-9.6.1.tgz",
++++++++ "integrity": "sha512-oruZaFkjorTpF32kDSI5/75ViwGeZginGGy2NoOSg3Q9bnwlnmDm4HLnkl0RE3n+njDXR037aY1+x58Z/zFdwQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "acorn": "^8.9.0",
++++++++ "acorn-jsx": "^5.3.2",
++++++++ "eslint-visitor-keys": "^3.4.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://opencollective.com/eslint"
++++++++ }
++++++++ },
++++++++ "node_modules/esquery": {
++++++++ "version": "1.5.0",
++++++++ "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.5.0.tgz",
++++++++ "integrity": "sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "estraverse": "^5.1.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=0.10"
++++++++ }
++++++++ },
++++++++ "node_modules/esrecurse": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.3.0.tgz",
++++++++ "integrity": "sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "estraverse": "^5.2.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=4.0"
++++++++ }
++++++++ },
++++++++ "node_modules/estraverse": {
++++++++ "version": "5.3.0",
++++++++ "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.3.0.tgz",
++++++++ "integrity": "sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=4.0"
++++++++ }
++++++++ },
++++++++ "node_modules/esutils": {
++++++++ "version": "2.0.3",
++++++++ "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz",
++++++++ "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/fast-deep-equal": {
++++++++ "version": "3.1.3",
++++++++ "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
++++++++ "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/fast-glob": {
++++++++ "version": "3.3.2",
++++++++ "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.2.tgz",
++++++++ "integrity": "sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@nodelib/fs.stat": "^2.0.2",
++++++++ "@nodelib/fs.walk": "^1.2.3",
++++++++ "glob-parent": "^5.1.2",
++++++++ "merge2": "^1.3.0",
++++++++ "micromatch": "^4.0.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8.6.0"
++++++++ }
++++++++ },
++++++++ "node_modules/fast-json-stable-stringify": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
++++++++ "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/fast-levenshtein": {
++++++++ "version": "2.0.6",
++++++++ "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
++++++++ "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/fastq": {
++++++++ "version": "1.17.0",
++++++++ "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.0.tgz",
++++++++ "integrity": "sha512-zGygtijUMT7jnk3h26kUms3BkSDp4IfIKjmnqI2tvx6nuBfiF1UqOxbnLfzdv+apBy+53oaImsKtMw/xYbW+1w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "reusify": "^1.0.4"
++++++++ }
++++++++ },
++++++++ "node_modules/file-entry-cache": {
++++++++ "version": "6.0.1",
++++++++ "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
++++++++ "integrity": "sha512-7Gps/XWymbLk2QLYK4NzpMOrYjMhdIxXuIvy2QBsLE6ljuodKvdkWs/cpyJJ3CVIVpH0Oi1Hvg1ovbMzLdFBBg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "flat-cache": "^3.0.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^10.12.0 || >=12.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/fill-range": {
++++++++ "version": "7.0.1",
++++++++ "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
++++++++ "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "to-regex-range": "^5.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/find-up": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz",
++++++++ "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "locate-path": "^6.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/flat-cache": {
++++++++ "version": "3.2.0",
++++++++ "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.2.0.tgz",
++++++++ "integrity": "sha512-CYcENa+FtcUKLmhhqyctpclsq7QF38pKjZHsGNiSQF5r4FtoKDWabFDl3hzaEQMvT1LHEysw5twgLvpYYb4vbw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "flatted": "^3.2.9",
++++++++ "keyv": "^4.5.3",
++++++++ "rimraf": "^3.0.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": "^10.12.0 || >=12.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/flatted": {
++++++++ "version": "3.2.9",
++++++++ "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz",
++++++++ "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/fs.realpath": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
++++++++ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/get-caller-file": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
++++++++ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": "6.* || 8.* || >= 10.*"
++++++++ }
++++++++ },
++++++++ "node_modules/glob": {
++++++++ "version": "7.2.0",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz",
++++++++ "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": "*"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/isaacs"
++++++++ }
++++++++ },
++++++++ "node_modules/glob-parent": {
++++++++ "version": "5.1.2",
++++++++ "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
++++++++ "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "is-glob": "^4.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 6"
++++++++ }
++++++++ },
++++++++ "node_modules/globals": {
++++++++ "version": "13.24.0",
++++++++ "resolved": "https://registry.npmjs.org/globals/-/globals-13.24.0.tgz",
++++++++ "integrity": "sha512-AhO5QUcj8llrbG09iWhPU2B204J1xnPeL8kQmVorSsy+Sjj1sk8gIyh6cUocGmH4L0UuhAJy+hJMRA4mgA4mFQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "type-fest": "^0.20.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/globby": {
++++++++ "version": "11.1.0",
++++++++ "resolved": "https://registry.npmjs.org/globby/-/globby-11.1.0.tgz",
++++++++ "integrity": "sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "array-union": "^2.1.0",
++++++++ "dir-glob": "^3.0.1",
++++++++ "fast-glob": "^3.2.9",
++++++++ "ignore": "^5.2.0",
++++++++ "merge2": "^1.4.1",
++++++++ "slash": "^3.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/graphemer": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz",
++++++++ "integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/ignore": {
++++++++ "version": "5.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.0.tgz",
++++++++ "integrity": "sha512-g7dmpshy+gD7mh88OC9NwSGTKoc3kyLAZQRU1mt53Aw/vnvfXnbC+F/7F7QoYVKbV+KNvJx8wArewKy1vXMtlg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">= 4"
++++++++ }
++++++++ },
++++++++ "node_modules/import-fresh": {
++++++++ "version": "3.3.0",
++++++++ "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz",
++++++++ "integrity": "sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "parent-module": "^1.0.0",
++++++++ "resolve-from": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/imurmurhash": {
++++++++ "version": "0.1.4",
++++++++ "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz",
++++++++ "integrity": "sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.8.19"
++++++++ }
++++++++ },
++++++++ "node_modules/inflight": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
++++++++ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "once": "^1.3.0",
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "node_modules/inherits": {
++++++++ "version": "2.0.4",
++++++++ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
++++++++ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/is-extglob": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
++++++++ "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/is-fullwidth-code-point": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
++++++++ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/is-glob": {
++++++++ "version": "4.0.3",
++++++++ "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
++++++++ "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "is-extglob": "^2.1.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/is-number": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
++++++++ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.12.0"
++++++++ }
++++++++ },
++++++++ "node_modules/is-path-inside": {
++++++++ "version": "3.0.3",
++++++++ "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz",
++++++++ "integrity": "sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/isexe": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
++++++++ "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/json-buffer": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz",
++++++++ "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/json-schema-traverse": {
++++++++ "version": "0.4.1",
++++++++ "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
++++++++ "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/json-stable-stringify-without-jsonify": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz",
++++++++ "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/keyv": {
++++++++ "version": "4.5.4",
++++++++ "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
++++++++ "integrity": "sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "json-buffer": "3.0.1"
++++++++ }
++++++++ },
++++++++ "node_modules/levn": {
++++++++ "version": "0.4.1",
++++++++ "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz",
++++++++ "integrity": "sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "prelude-ls": "^1.2.1",
++++++++ "type-check": "~0.4.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 0.8.0"
++++++++ }
++++++++ },
++++++++ "node_modules/linkify-it": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-2.2.0.tgz",
++++++++ "integrity": "sha512-GnAl/knGn+i1U/wjBz3akz2stz+HrHLsxMwHQGofCDfPvlf+gDKN58UtfmUquTY4/MXeE2x7k19KQmeoZi94Iw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "uc.micro": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse": {
++++++++ "version": "7.1.1",
++++++++ "resolved": "https://registry.npmjs.org/llparse/-/llparse-7.1.1.tgz",
++++++++ "integrity": "sha512-lBxN5O6sKq6KSOaRFIGczoVpO/U/37mHhjJioQbPuiXdfZmwzP1zC3txV9xx778TRNFENzeCM0Uoo+mE1rfJOA==",
++++++++ "dependencies": {
++++++++ "debug": "^4.2.0",
++++++++ "llparse-frontend": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse-builder": {
++++++++ "version": "1.5.2",
++++++++ "resolved": "https://registry.npmjs.org/llparse-builder/-/llparse-builder-1.5.2.tgz",
++++++++ "integrity": "sha512-i862UNC3YUEdlfK/NUCJxlKjtWjgAI9AJXDRgjcfRHfwFt4Sf8eFPTRsc91/2R9MBZ0kyFdfhi8SVhMsZf1gNQ==",
++++++++ "dependencies": {
++++++++ "@types/debug": "4.1.5 ",
++++++++ "binary-search": "^1.3.6",
++++++++ "debug": "^4.2.0"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse-dot": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/llparse-dot/-/llparse-dot-1.0.1.tgz",
++++++++ "integrity": "sha512-3e271C2LuDWBzhxaCUDzjpufamoEBuTYQz83QyMixI/i99BntCEk6ngHWOhhDb0XdtNNh6qAfRmXyjgNP+Nxpw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "llparse-builder": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse-frontend": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/llparse-frontend/-/llparse-frontend-3.0.0.tgz",
++++++++ "integrity": "sha512-G/o0Po2C+G5OtP8MJeQDjDf5qwDxcO7K6x4r6jqGsJwxk7yblbJnRqpmye7G/lZ8dD0Hv5neY4/KB5BhDmEc9Q==",
++++++++ "dependencies": {
++++++++ "debug": "^3.2.6",
++++++++ "llparse-builder": "^1.5.2"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse-frontend/node_modules/debug": {
++++++++ "version": "3.2.7",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
++++++++ "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
++++++++ "dependencies": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "node_modules/llparse-test-fixture": {
++++++++ "version": "5.0.2",
++++++++ "resolved": "https://registry.npmjs.org/llparse-test-fixture/-/llparse-test-fixture-5.0.2.tgz",
++++++++ "integrity": "sha512-61KI5J/b5uyRktD0y1EezleEW6UfaxhHkn1adLKNVemRZzklE+SpLakr251qo04kb9jN/ytk8lllgK+yFOj4cQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "esm": "^3.2.25",
++++++++ "llparse": "^7.0.0",
++++++++ "yargs": "^15.4.1"
++++++++ }
++++++++ },
++++++++ "node_modules/locate-path": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",
++++++++ "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "p-locate": "^5.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/lodash.merge": {
++++++++ "version": "4.6.2",
++++++++ "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
++++++++ "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/lru-cache": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
++++++++ "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "yallist": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ }
++++++++ },
++++++++ "node_modules/make-error": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
++++++++ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/markdown-it": {
++++++++ "version": "8.4.2",
++++++++ "resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-8.4.2.tgz",
++++++++ "integrity": "sha512-GcRz3AWTqSUphY3vsUqQSFMbgR38a4Lh3GWlHRh/7MRwz8mcu9n2IO7HOh+bXHrR9kOPDl5RNCaEsrneb+xhHQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "argparse": "^1.0.7",
++++++++ "entities": "~1.1.1",
++++++++ "linkify-it": "^2.0.0",
++++++++ "mdurl": "^1.0.1",
++++++++ "uc.micro": "^1.0.5"
++++++++ },
++++++++ "bin": {
++++++++ "markdown-it": "bin/markdown-it.js"
++++++++ }
++++++++ },
++++++++ "node_modules/mdgator": {
++++++++ "version": "1.1.2",
++++++++ "resolved": "https://registry.npmjs.org/mdgator/-/mdgator-1.1.2.tgz",
++++++++ "integrity": "sha512-S2GvsLIznUQ2McXfpe6BCD+IqhnRuHcBO7krqnvnsHgDpjjO1mLhr0vZtVa5ca4WZET037g3G+94DznpicKkOA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@types/markdown-it": "0.0.4",
++++++++ "markdown-it": "^8.4.1"
++++++++ }
++++++++ },
++++++++ "node_modules/mdurl": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/mdurl/-/mdurl-1.0.1.tgz",
++++++++ "integrity": "sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/merge2": {
++++++++ "version": "1.4.1",
++++++++ "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
++++++++ "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/micromatch": {
++++++++ "version": "4.0.5",
++++++++ "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz",
++++++++ "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "braces": "^3.0.2",
++++++++ "picomatch": "^2.3.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8.6"
++++++++ }
++++++++ },
++++++++ "node_modules/minimatch": {
++++++++ "version": "3.1.2",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
++++++++ "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "brace-expansion": "^1.1.7"
++++++++ },
++++++++ "engines": {
++++++++ "node": "*"
++++++++ }
++++++++ },
++++++++ "node_modules/ms": {
++++++++ "version": "2.1.2",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
++++++++ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
++++++++ },
++++++++ "node_modules/natural-compare": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
++++++++ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/once": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
++++++++ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "node_modules/optionator": {
++++++++ "version": "0.9.3",
++++++++ "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz",
++++++++ "integrity": "sha512-JjCoypp+jKn1ttEFExxhetCKeJt9zhAgAve5FXHixTvFDW/5aEktX9bufBKLRRMdU7bNtpLfcGu94B3cdEJgjg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@aashutoshrathi/word-wrap": "^1.2.3",
++++++++ "deep-is": "^0.1.3",
++++++++ "fast-levenshtein": "^2.0.6",
++++++++ "levn": "^0.4.1",
++++++++ "prelude-ls": "^1.2.1",
++++++++ "type-check": "^0.4.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 0.8.0"
++++++++ }
++++++++ },
++++++++ "node_modules/p-limit": {
++++++++ "version": "2.3.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
++++++++ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "p-try": "^2.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/p-locate": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz",
++++++++ "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "p-limit": "^3.0.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/p-locate/node_modules/p-limit": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz",
++++++++ "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "yocto-queue": "^0.1.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/p-try": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
++++++++ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/parent-module": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
++++++++ "integrity": "sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "callsites": "^3.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/path-exists": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
++++++++ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/path-is-absolute": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
++++++++ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/path-key": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
++++++++ "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/path-type": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz",
++++++++ "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/picomatch": {
++++++++ "version": "2.3.1",
++++++++ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
++++++++ "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8.6"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/jonschlinkert"
++++++++ }
++++++++ },
++++++++ "node_modules/prelude-ls": {
++++++++ "version": "1.2.1",
++++++++ "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz",
++++++++ "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">= 0.8.0"
++++++++ }
++++++++ },
++++++++ "node_modules/punycode": {
++++++++ "version": "2.3.1",
++++++++ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
++++++++ "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/queue-microtask": {
++++++++ "version": "1.2.3",
++++++++ "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
++++++++ "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==",
++++++++ "dev": true,
++++++++ "funding": [
++++++++ {
++++++++ "type": "github",
++++++++ "url": "https://github.com/sponsors/feross"
++++++++ },
++++++++ {
++++++++ "type": "patreon",
++++++++ "url": "https://www.patreon.com/feross"
++++++++ },
++++++++ {
++++++++ "type": "consulting",
++++++++ "url": "https://feross.org/support"
++++++++ }
++++++++ ]
++++++++ },
++++++++ "node_modules/require-directory": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
++++++++ "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/require-main-filename": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
++++++++ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/resolve-from": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz",
++++++++ "integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=4"
++++++++ }
++++++++ },
++++++++ "node_modules/reusify": {
++++++++ "version": "1.0.4",
++++++++ "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz",
++++++++ "integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "iojs": ">=1.0.0",
++++++++ "node": ">=0.10.0"
++++++++ }
++++++++ },
++++++++ "node_modules/rimraf": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
++++++++ "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "glob": "^7.1.3"
++++++++ },
++++++++ "bin": {
++++++++ "rimraf": "bin.js"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/isaacs"
++++++++ }
++++++++ },
++++++++ "node_modules/run-parallel": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
++++++++ "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==",
++++++++ "dev": true,
++++++++ "funding": [
++++++++ {
++++++++ "type": "github",
++++++++ "url": "https://github.com/sponsors/feross"
++++++++ },
++++++++ {
++++++++ "type": "patreon",
++++++++ "url": "https://www.patreon.com/feross"
++++++++ },
++++++++ {
++++++++ "type": "consulting",
++++++++ "url": "https://feross.org/support"
++++++++ }
++++++++ ],
++++++++ "dependencies": {
++++++++ "queue-microtask": "^1.2.2"
++++++++ }
++++++++ },
++++++++ "node_modules/semver": {
++++++++ "version": "7.5.4",
++++++++ "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz",
++++++++ "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "lru-cache": "^6.0.0"
++++++++ },
++++++++ "bin": {
++++++++ "semver": "bin/semver.js"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ }
++++++++ },
++++++++ "node_modules/set-blocking": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
++++++++ "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/shebang-command": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
++++++++ "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "shebang-regex": "^3.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/shebang-regex": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
++++++++ "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/slash": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/slash/-/slash-3.0.0.tgz",
++++++++ "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/sprintf-js": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
++++++++ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/string-width": {
++++++++ "version": "4.2.3",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
++++++++ "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "emoji-regex": "^8.0.0",
++++++++ "is-fullwidth-code-point": "^3.0.0",
++++++++ "strip-ansi": "^6.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/strip-ansi": {
++++++++ "version": "6.0.1",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz",
++++++++ "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "ansi-regex": "^5.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/strip-json-comments": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz",
++++++++ "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/text-table": {
++++++++ "version": "0.2.0",
++++++++ "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz",
++++++++ "integrity": "sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/to-regex-range": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
++++++++ "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "is-number": "^7.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8.0"
++++++++ }
++++++++ },
++++++++ "node_modules/ts-api-utils": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-1.0.3.tgz",
++++++++ "integrity": "sha512-wNMeqtMz5NtwpT/UZGY5alT+VoKdSsOOP/kqHFcUW1P/VRhH2wJ48+DN2WwUliNbQ976ETwDL0Ifd2VVvgonvg==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=16.13.0"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "typescript": ">=4.2.0"
++++++++ }
++++++++ },
++++++++ "node_modules/ts-node": {
++++++++ "version": "10.9.2",
++++++++ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.2.tgz",
++++++++ "integrity": "sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "@cspotcode/source-map-support": "^0.8.0",
++++++++ "@tsconfig/node10": "^1.0.7",
++++++++ "@tsconfig/node12": "^1.0.7",
++++++++ "@tsconfig/node14": "^1.0.0",
++++++++ "@tsconfig/node16": "^1.0.2",
++++++++ "acorn": "^8.4.1",
++++++++ "acorn-walk": "^8.1.1",
++++++++ "arg": "^4.1.0",
++++++++ "create-require": "^1.1.0",
++++++++ "diff": "^4.0.1",
++++++++ "make-error": "^1.1.1",
++++++++ "v8-compile-cache-lib": "^3.0.1",
++++++++ "yn": "3.1.1"
++++++++ },
++++++++ "bin": {
++++++++ "ts-node": "dist/bin.js",
++++++++ "ts-node-cwd": "dist/bin-cwd.js",
++++++++ "ts-node-esm": "dist/bin-esm.js",
++++++++ "ts-node-script": "dist/bin-script.js",
++++++++ "ts-node-transpile-only": "dist/bin-transpile.js",
++++++++ "ts-script": "dist/bin-script-deprecated.js"
++++++++ },
++++++++ "peerDependencies": {
++++++++ "@swc/core": ">=1.2.50",
++++++++ "@swc/wasm": ">=1.2.50",
++++++++ "@types/node": "*",
++++++++ "typescript": ">=2.7"
++++++++ },
++++++++ "peerDependenciesMeta": {
++++++++ "@swc/core": {
++++++++ "optional": true
++++++++ },
++++++++ "@swc/wasm": {
++++++++ "optional": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "node_modules/type-check": {
++++++++ "version": "0.4.0",
++++++++ "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
++++++++ "integrity": "sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "prelude-ls": "^1.2.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 0.8.0"
++++++++ }
++++++++ },
++++++++ "node_modules/type-fest": {
++++++++ "version": "0.20.2",
++++++++ "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz",
++++++++ "integrity": "sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ },
++++++++ "node_modules/typescript": {
++++++++ "version": "5.4.2",
++++++++ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz",
++++++++ "integrity": "sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==",
++++++++ "dev": true,
++++++++ "bin": {
++++++++ "tsc": "bin/tsc",
++++++++ "tsserver": "bin/tsserver"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=14.17"
++++++++ }
++++++++ },
++++++++ "node_modules/uc.micro": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz",
++++++++ "integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/undici-types": {
++++++++ "version": "5.26.5",
++++++++ "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
++++++++ "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/uri-js": {
++++++++ "version": "4.4.1",
++++++++ "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
++++++++ "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "punycode": "^2.1.0"
++++++++ }
++++++++ },
++++++++ "node_modules/v8-compile-cache-lib": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
++++++++ "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/which": {
++++++++ "version": "2.0.2",
++++++++ "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
++++++++ "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "isexe": "^2.0.0"
++++++++ },
++++++++ "bin": {
++++++++ "node-which": "bin/node-which"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">= 8"
++++++++ }
++++++++ },
++++++++ "node_modules/which-module": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",
++++++++ "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/wrap-ansi": {
++++++++ "version": "6.2.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz",
++++++++ "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "ansi-styles": "^4.0.0",
++++++++ "string-width": "^4.1.0",
++++++++ "strip-ansi": "^6.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/wrap-ansi/node_modules/ansi-styles": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
++++++++ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "color-convert": "^2.0.1"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/chalk/ansi-styles?sponsor=1"
++++++++ }
++++++++ },
++++++++ "node_modules/wrap-ansi/node_modules/color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "color-name": "~1.1.4"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=7.0.0"
++++++++ }
++++++++ },
++++++++ "node_modules/wrap-ansi/node_modules/color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/wrappy": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
++++++++ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/y18n": {
++++++++ "version": "4.0.3",
++++++++ "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz",
++++++++ "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/yallist": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
++++++++ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
++++++++ "dev": true
++++++++ },
++++++++ "node_modules/yargs": {
++++++++ "version": "15.4.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz",
++++++++ "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "cliui": "^6.0.0",
++++++++ "decamelize": "^1.2.0",
++++++++ "find-up": "^4.1.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^4.2.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^18.1.2"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/yargs/node_modules/find-up": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
++++++++ "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "locate-path": "^5.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/yargs/node_modules/locate-path": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz",
++++++++ "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "p-locate": "^4.1.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/yargs/node_modules/p-locate": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
++++++++ "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "p-limit": "^2.2.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=8"
++++++++ }
++++++++ },
++++++++ "node_modules/yargs/node_modules/yargs-parser": {
++++++++ "version": "18.1.3",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz",
++++++++ "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==",
++++++++ "dev": true,
++++++++ "dependencies": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ },
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/yn": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
++++++++ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=6"
++++++++ }
++++++++ },
++++++++ "node_modules/yocto-queue": {
++++++++ "version": "0.1.0",
++++++++ "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
++++++++ "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==",
++++++++ "dev": true,
++++++++ "engines": {
++++++++ "node": ">=10"
++++++++ },
++++++++ "funding": {
++++++++ "url": "https://github.com/sponsors/sindresorhus"
++++++++ }
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llhttp",
++++++++ "version": "9.2.1",
++++++++ "description": "HTTP parser in LLVM IR",
++++++++ "main": "lib/llhttp.js",
++++++++ "types": "lib/llhttp.d.ts",
++++++++ "files": [
++++++++ "lib",
++++++++ "src"
++++++++ ],
++++++++ "scripts": {
++++++++ "bench": "ts-node bench/",
++++++++ "build": "ts-node bin/generate.ts",
++++++++ "build-ts": "tsc",
++++++++ "prebuild-wasm": "npm run wasm -- --prebuild && npm run wasm -- --setup",
++++++++ "build-wasm": "npm run wasm -- --docker",
++++++++ "wasm": "ts-node bin/build_wasm.ts",
++++++++ "clean": "rm -rf lib && rm -rf test/tmp",
++++++++ "prepare": "npm run clean && npm run build-ts",
++++++++ "test": "node -r ts-node/register/type-check ./test/md-test.ts",
++++++++ "lint": "eslint -c eslint.json bin/*.ts src/*.ts src/**/*.ts test/*.ts test/**/*.ts",
++++++++ "lint-fix": "eslint --fix -c eslint.json bin/*.ts src/*.ts src/**/*.ts test/*.ts test/**/*.ts",
++++++++ "postversion": "RELEASE=`node -e \"process.stdout.write(require('./package').version)\"` make -B postversion",
++++++++ "github-release": "RELEASE_V=`node -e \"process.stdout.write('v' + require('./package').version)\"` make github-release"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+ssh://git@github.com/nodejs/llhttp.git"
++++++++ },
++++++++ "keywords": [
++++++++ "http",
++++++++ "llvm",
++++++++ "ir",
++++++++ "llparse"
++++++++ ],
++++++++ "author": "Fedor Indutny <fedor@indutny.com> (http://darksi.de/)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/nodejs/llhttp/issues"
++++++++ },
++++++++ "homepage": "https://github.com/nodejs/llhttp#readme",
++++++++ "devDependencies": {
++++++++ "@stylistic/eslint-plugin": "^1.5.4",
++++++++ "@types/node": "^20.11.10",
++++++++ "@typescript-eslint/eslint-plugin": "^7.0.0",
++++++++ "@typescript-eslint/parser": "^6.20.0",
++++++++ "eslint": "^8.56.0",
++++++++ "llparse-dot": "^1.0.1",
++++++++ "llparse-test-fixture": "^5.0.1",
++++++++ "mdgator": "^1.1.2",
++++++++ "ts-node": "^10.9.2",
++++++++ "typescript": "^5.3.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "llparse": "^7.1.1"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ 'target_defaults': {
++++++++ 'default_configuration': 'Debug',
++++++++ 'configurations': {
++++++++ # TODO: hoist these out and put them somewhere common, because
++++++++ # RuntimeLibrary MUST MATCH across the entire project
++++++++ 'Debug': {
++++++++ 'defines': [ 'DEBUG', '_DEBUG' ],
++++++++ 'cflags': [ '-Wall', '-Wextra', '-O0', '-g', '-ftrapv' ],
++++++++ 'msvs_settings': {
++++++++ 'VCCLCompilerTool': {
++++++++ 'RuntimeLibrary': 1, # static debug
++++++++ },
++++++++ },
++++++++ },
++++++++ 'Release': {
++++++++ 'defines': [ 'NDEBUG' ],
++++++++ 'cflags': [ '-Wall', '-Wextra', '-O3' ],
++++++++ 'msvs_settings': {
++++++++ 'VCCLCompilerTool': {
++++++++ 'RuntimeLibrary': 0, # static release
++++++++ },
++++++++ },
++++++++ }
++++++++ },
++++++++ 'msvs_settings': {
++++++++ 'VCCLCompilerTool': {
++++++++ # Compile as C++. llhttp.c is actually C99, but C++ is
++++++++ # close enough in this case.
++++++++ 'CompileAs': 2,
++++++++ },
++++++++ 'VCLibrarianTool': {
++++++++ },
++++++++ 'VCLinkerTool': {
++++++++ 'GenerateDebugInformation': 'true',
++++++++ },
++++++++ },
++++++++ 'conditions': [
++++++++ ['OS == "win"', {
++++++++ 'defines': [
++++++++ 'WIN32'
++++++++ ],
++++++++ }]
++++++++ ],
++++++++ },
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ 'variables': {
++++++++ 'llhttp_sources': [
++++++++ 'src/llhttp.c',
++++++++ 'src/api.c',
++++++++ 'src/http.c',
++++++++ ]
++++++++ },
++++++++ 'targets': [
++++++++ {
++++++++ 'target_name': 'llhttp',
++++++++ 'type': 'static_library',
++++++++ 'include_dirs': [ '.', 'include' ],
++++++++ 'direct_dependent_settings': {
++++++++ 'include_dirs': [ 'include' ],
++++++++ },
++++++++ 'sources': [
++++++++ '<@(llhttp_sources)',
++++++++ ],
++++++++ },
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as constants from './llhttp/constants';
++++++++
++++++++export { constants };
++++++++
++++++++export { HTTP } from './llhttp/http';
++++++++export { URL } from './llhttp/url';
++++++++export { CHeaders } from './llhttp/c-headers';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as constants from './constants';
++++++++import { enumToMap } from './utils';
++++++++
++++++++type Encoding = 'none' | 'hex';
++++++++
++++++++export class CHeaders {
++++++++ public build(): string {
++++++++ let res = '';
++++++++
++++++++ res += '#ifndef LLLLHTTP_C_HEADERS_\n';
++++++++ res += '#define LLLLHTTP_C_HEADERS_\n';
++++++++
++++++++ res += '#ifdef __cplusplus\n';
++++++++ res += 'extern "C" {\n';
++++++++ res += '#endif\n';
++++++++
++++++++ res += '\n';
++++++++
++++++++ const errorMap = enumToMap(constants.ERROR);
++++++++ const methodMap = enumToMap(constants.METHODS);
++++++++ const httpMethodMap = enumToMap(constants.METHODS, constants.METHODS_HTTP, [
++++++++ constants.METHODS.PRI,
++++++++ ]);
++++++++ const rtspMethodMap = enumToMap(constants.METHODS, constants.METHODS_RTSP);
++++++++ const statusMap = enumToMap(constants.STATUSES, constants.STATUSES_HTTP);
++++++++
++++++++ res += this.buildEnum('llhttp_errno', 'HPE', errorMap);
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_flags', 'F', enumToMap(constants.FLAGS),
++++++++ 'hex');
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_lenient_flags', 'LENIENT',
++++++++ enumToMap(constants.LENIENT_FLAGS), 'hex');
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_type', 'HTTP',
++++++++ enumToMap(constants.TYPE));
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_finish', 'HTTP_FINISH',
++++++++ enumToMap(constants.FINISH));
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_method', 'HTTP', methodMap);
++++++++ res += '\n';
++++++++ res += this.buildEnum('llhttp_status', 'HTTP_STATUS', statusMap);
++++++++
++++++++ res += '\n';
++++++++
++++++++ res += this.buildMap('HTTP_ERRNO', errorMap);
++++++++ res += '\n';
++++++++ res += this.buildMap('HTTP_METHOD', httpMethodMap);
++++++++ res += '\n';
++++++++ res += this.buildMap('RTSP_METHOD', rtspMethodMap);
++++++++ res += '\n';
++++++++ res += this.buildMap('HTTP_ALL_METHOD', methodMap);
++++++++ res += '\n';
++++++++ res += this.buildMap('HTTP_STATUS', statusMap);
++++++++
++++++++ res += '\n';
++++++++
++++++++ res += '#ifdef __cplusplus\n';
++++++++ res += '} /* extern "C" */\n';
++++++++ res += '#endif\n';
++++++++ res += '#endif /* LLLLHTTP_C_HEADERS_ */\n';
++++++++
++++++++ return res;
++++++++ }
++++++++
++++++++ private buildEnum(name: string, prefix: string, map: constants.IntDict,
++++++++ encoding: Encoding = 'none'): string {
++++++++ let res = '';
++++++++
++++++++ res += `enum ${name} {\n`;
++++++++ const keys = Object.keys(map);
++++++++ const keysLength = keys.length;
++++++++ for (let i = 0; i < keysLength; i++) {
++++++++ const key = keys[i];
++++++++ const isLast = i === keysLength - 1;
++++++++
++++++++ let value: number | string = map[key];
++++++++
++++++++ if (encoding === 'hex') {
++++++++ value = `0x${value.toString(16)}`;
++++++++ }
++++++++
++++++++ res += ` ${prefix}_${key.replace(/-/g, '')} = ${value}`;
++++++++ if (!isLast) {
++++++++ res += ',\n';
++++++++ }
++++++++ }
++++++++ res += '\n};\n';
++++++++ res += `typedef enum ${name} ${name}_t;\n`;
++++++++
++++++++ return res;
++++++++ }
++++++++
++++++++ private buildMap(name: string, map: constants.IntDict): string {
++++++++ let res = '';
++++++++
++++++++ res += `#define ${name}_MAP(XX) \\\n`;
++++++++ for (const [ key, value ] of Object.entries(map)) {
++++++++ res += ` XX(${value!}, ${key.replace(/-/g, '')}, ${key}) \\\n`;
++++++++ }
++++++++ res += '\n';
++++++++
++++++++ return res;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { enumToMap } from './utils';
++++++++
++++++++export type IntDict = Record<string, number>;
++++++++
++++++++// Emums
++++++++
++++++++export const ERROR: IntDict = {
++++++++ OK: 0,
++++++++ INTERNAL: 1,
++++++++ STRICT: 2,
++++++++ CR_EXPECTED: 25,
++++++++ LF_EXPECTED: 3,
++++++++ UNEXPECTED_CONTENT_LENGTH: 4,
++++++++ UNEXPECTED_SPACE: 30,
++++++++ CLOSED_CONNECTION: 5,
++++++++ INVALID_METHOD: 6,
++++++++ INVALID_URL: 7,
++++++++ INVALID_CONSTANT: 8,
++++++++ INVALID_VERSION: 9,
++++++++ INVALID_HEADER_TOKEN: 10,
++++++++ INVALID_CONTENT_LENGTH: 11,
++++++++ INVALID_CHUNK_SIZE: 12,
++++++++ INVALID_STATUS: 13,
++++++++ INVALID_EOF_STATE: 14,
++++++++ INVALID_TRANSFER_ENCODING: 15,
++++++++
++++++++ CB_MESSAGE_BEGIN: 16,
++++++++ CB_HEADERS_COMPLETE: 17,
++++++++ CB_MESSAGE_COMPLETE: 18,
++++++++ CB_CHUNK_HEADER: 19,
++++++++ CB_CHUNK_COMPLETE: 20,
++++++++
++++++++ PAUSED: 21,
++++++++ PAUSED_UPGRADE: 22,
++++++++ PAUSED_H2_UPGRADE: 23,
++++++++
++++++++ USER: 24,
++++++++
++++++++ CB_URL_COMPLETE: 26,
++++++++ CB_STATUS_COMPLETE: 27,
++++++++ CB_METHOD_COMPLETE: 32,
++++++++ CB_VERSION_COMPLETE: 33,
++++++++ CB_HEADER_FIELD_COMPLETE: 28,
++++++++ CB_HEADER_VALUE_COMPLETE: 29,
++++++++ CB_CHUNK_EXTENSION_NAME_COMPLETE: 34,
++++++++ CB_CHUNK_EXTENSION_VALUE_COMPLETE: 35,
++++++++ CB_RESET: 31,
++++++++};
++++++++
++++++++export const TYPE: IntDict = {
++++++++ BOTH: 0, // default
++++++++ REQUEST: 1,
++++++++ RESPONSE: 2,
++++++++};
++++++++
++++++++export const FLAGS: IntDict = {
++++++++ CONNECTION_KEEP_ALIVE: 1 << 0,
++++++++ CONNECTION_CLOSE: 1 << 1,
++++++++ CONNECTION_UPGRADE: 1 << 2,
++++++++ CHUNKED: 1 << 3,
++++++++ UPGRADE: 1 << 4,
++++++++ CONTENT_LENGTH: 1 << 5,
++++++++ SKIPBODY: 1 << 6,
++++++++ TRAILING: 1 << 7,
++++++++ // 1 << 8 is unused
++++++++ TRANSFER_ENCODING: 1 << 9,
++++++++};
++++++++
++++++++export const LENIENT_FLAGS: IntDict = {
++++++++ HEADERS: 1 << 0,
++++++++ CHUNKED_LENGTH: 1 << 1,
++++++++ KEEP_ALIVE: 1 << 2,
++++++++ TRANSFER_ENCODING: 1 << 3,
++++++++ VERSION: 1 << 4,
++++++++ DATA_AFTER_CLOSE: 1 << 5,
++++++++ OPTIONAL_LF_AFTER_CR: 1 << 6,
++++++++ OPTIONAL_CRLF_AFTER_CHUNK: 1 << 7,
++++++++ OPTIONAL_CR_BEFORE_LF: 1 << 8,
++++++++ SPACES_AFTER_CHUNK_SIZE: 1 << 9,
++++++++};
++++++++
++++++++export const METHODS: IntDict = {
++++++++ 'DELETE': 0,
++++++++ 'GET': 1,
++++++++ 'HEAD': 2,
++++++++ 'POST': 3,
++++++++ 'PUT': 4,
++++++++ /* pathological */
++++++++ 'CONNECT': 5,
++++++++ 'OPTIONS': 6,
++++++++ 'TRACE': 7,
++++++++ /* WebDAV */
++++++++ 'COPY': 8,
++++++++ 'LOCK': 9,
++++++++ 'MKCOL': 10,
++++++++ 'MOVE': 11,
++++++++ 'PROPFIND': 12,
++++++++ 'PROPPATCH': 13,
++++++++ 'SEARCH': 14,
++++++++ 'UNLOCK': 15,
++++++++ 'BIND': 16,
++++++++ 'REBIND': 17,
++++++++ 'UNBIND': 18,
++++++++ 'ACL': 19,
++++++++ /* subversion */
++++++++ 'REPORT': 20,
++++++++ 'MKACTIVITY': 21,
++++++++ 'CHECKOUT': 22,
++++++++ 'MERGE': 23,
++++++++ /* upnp */
++++++++ 'M-SEARCH': 24,
++++++++ 'NOTIFY': 25,
++++++++ 'SUBSCRIBE': 26,
++++++++ 'UNSUBSCRIBE': 27,
++++++++ /* RFC-5789 */
++++++++ 'PATCH': 28,
++++++++ 'PURGE': 29,
++++++++ /* CalDAV */
++++++++ 'MKCALENDAR': 30,
++++++++ /* RFC-2068, section 19.6.1.2 */
++++++++ 'LINK': 31,
++++++++ 'UNLINK': 32,
++++++++ /* icecast */
++++++++ 'SOURCE': 33,
++++++++ /* RFC-7540, section 11.6 */
++++++++ 'PRI': 34,
++++++++ /* RFC-2326 RTSP */
++++++++ 'DESCRIBE': 35,
++++++++ 'ANNOUNCE': 36,
++++++++ 'SETUP': 37,
++++++++ 'PLAY': 38,
++++++++ 'PAUSE': 39,
++++++++ 'TEARDOWN': 40,
++++++++ 'GET_PARAMETER': 41,
++++++++ 'SET_PARAMETER': 42,
++++++++ 'REDIRECT': 43,
++++++++ 'RECORD': 44,
++++++++ /* RAOP */
++++++++ 'FLUSH': 45,
++++++++ /* DRAFT https://www.ietf.org/archive/id/draft-ietf-httpbis-safe-method-w-body-02.html */
++++++++ 'QUERY': 46,
++++++++};
++++++++
++++++++export const STATUSES: IntDict = {
++++++++ CONTINUE: 100,
++++++++ SWITCHING_PROTOCOLS: 101,
++++++++ PROCESSING: 102,
++++++++ EARLY_HINTS: 103,
++++++++ RESPONSE_IS_STALE: 110, // Unofficial
++++++++ REVALIDATION_FAILED: 111, // Unofficial
++++++++ DISCONNECTED_OPERATION: 112, // Unofficial
++++++++ HEURISTIC_EXPIRATION: 113, // Unofficial
++++++++ MISCELLANEOUS_WARNING: 199, // Unofficial
++++++++ OK: 200,
++++++++ CREATED: 201,
++++++++ ACCEPTED: 202,
++++++++ NON_AUTHORITATIVE_INFORMATION: 203,
++++++++ NO_CONTENT: 204,
++++++++ RESET_CONTENT: 205,
++++++++ PARTIAL_CONTENT: 206,
++++++++ MULTI_STATUS: 207,
++++++++ ALREADY_REPORTED: 208,
++++++++ TRANSFORMATION_APPLIED: 214, // Unofficial
++++++++ IM_USED: 226,
++++++++ MISCELLANEOUS_PERSISTENT_WARNING: 299, // Unofficial
++++++++ MULTIPLE_CHOICES: 300,
++++++++ MOVED_PERMANENTLY: 301,
++++++++ FOUND: 302,
++++++++ SEE_OTHER: 303,
++++++++ NOT_MODIFIED: 304,
++++++++ USE_PROXY: 305,
++++++++ SWITCH_PROXY: 306, // No longer used
++++++++ TEMPORARY_REDIRECT: 307,
++++++++ PERMANENT_REDIRECT: 308,
++++++++ BAD_REQUEST: 400,
++++++++ UNAUTHORIZED: 401,
++++++++ PAYMENT_REQUIRED: 402,
++++++++ FORBIDDEN: 403,
++++++++ NOT_FOUND: 404,
++++++++ METHOD_NOT_ALLOWED: 405,
++++++++ NOT_ACCEPTABLE: 406,
++++++++ PROXY_AUTHENTICATION_REQUIRED: 407,
++++++++ REQUEST_TIMEOUT: 408,
++++++++ CONFLICT: 409,
++++++++ GONE: 410,
++++++++ LENGTH_REQUIRED: 411,
++++++++ PRECONDITION_FAILED: 412,
++++++++ PAYLOAD_TOO_LARGE: 413,
++++++++ URI_TOO_LONG: 414,
++++++++ UNSUPPORTED_MEDIA_TYPE: 415,
++++++++ RANGE_NOT_SATISFIABLE: 416,
++++++++ EXPECTATION_FAILED: 417,
++++++++ IM_A_TEAPOT: 418,
++++++++ PAGE_EXPIRED: 419, // Unofficial
++++++++ ENHANCE_YOUR_CALM: 420, // Unofficial
++++++++ MISDIRECTED_REQUEST: 421,
++++++++ UNPROCESSABLE_ENTITY: 422,
++++++++ LOCKED: 423,
++++++++ FAILED_DEPENDENCY: 424,
++++++++ TOO_EARLY: 425,
++++++++ UPGRADE_REQUIRED: 426,
++++++++ PRECONDITION_REQUIRED: 428,
++++++++ TOO_MANY_REQUESTS: 429,
++++++++ REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL: 430, // Unofficial
++++++++ REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
++++++++ LOGIN_TIMEOUT: 440, // Unofficial
++++++++ NO_RESPONSE: 444, // Unofficial
++++++++ RETRY_WITH: 449, // Unofficial
++++++++ BLOCKED_BY_PARENTAL_CONTROL: 450, // Unofficial
++++++++ UNAVAILABLE_FOR_LEGAL_REASONS: 451,
++++++++ CLIENT_CLOSED_LOAD_BALANCED_REQUEST: 460, // Unofficial
++++++++ INVALID_X_FORWARDED_FOR: 463, // Unofficial
++++++++ REQUEST_HEADER_TOO_LARGE: 494, // Unofficial
++++++++ SSL_CERTIFICATE_ERROR: 495, // Unofficial
++++++++ SSL_CERTIFICATE_REQUIRED: 496, // Unofficial
++++++++ HTTP_REQUEST_SENT_TO_HTTPS_PORT: 497, // Unofficial
++++++++ INVALID_TOKEN: 498, // Unofficial
++++++++ CLIENT_CLOSED_REQUEST: 499, // Unofficial
++++++++ INTERNAL_SERVER_ERROR: 500,
++++++++ NOT_IMPLEMENTED: 501,
++++++++ BAD_GATEWAY: 502,
++++++++ SERVICE_UNAVAILABLE: 503,
++++++++ GATEWAY_TIMEOUT: 504,
++++++++ HTTP_VERSION_NOT_SUPPORTED: 505,
++++++++ VARIANT_ALSO_NEGOTIATES: 506,
++++++++ INSUFFICIENT_STORAGE: 507,
++++++++ LOOP_DETECTED: 508,
++++++++ BANDWIDTH_LIMIT_EXCEEDED: 509,
++++++++ NOT_EXTENDED: 510,
++++++++ NETWORK_AUTHENTICATION_REQUIRED: 511,
++++++++ WEB_SERVER_UNKNOWN_ERROR: 520, // Unofficial
++++++++ WEB_SERVER_IS_DOWN: 521, // Unofficial
++++++++ CONNECTION_TIMEOUT: 522, // Unofficial
++++++++ ORIGIN_IS_UNREACHABLE: 523, // Unofficial
++++++++ TIMEOUT_OCCURED: 524, // Unofficial
++++++++ SSL_HANDSHAKE_FAILED: 525, // Unofficial
++++++++ INVALID_SSL_CERTIFICATE: 526, // Unofficial
++++++++ RAILGUN_ERROR: 527, // Unofficial
++++++++ SITE_IS_OVERLOADED: 529, // Unofficial
++++++++ SITE_IS_FROZEN: 530, // Unofficial
++++++++ IDENTITY_PROVIDER_AUTHENTICATION_ERROR: 561, // Unofficial
++++++++ NETWORK_READ_TIMEOUT: 598, // Unofficial
++++++++ NETWORK_CONNECT_TIMEOUT: 599, // Unofficial
++++++++};
++++++++
++++++++export const FINISH: IntDict = {
++++++++ SAFE: 0,
++++++++ SAFE_WITH_CB: 1,
++++++++ UNSAFE: 2,
++++++++};
++++++++
++++++++export const HEADER_STATE: IntDict = {
++++++++ GENERAL: 0,
++++++++ CONNECTION: 1,
++++++++ CONTENT_LENGTH: 2,
++++++++ TRANSFER_ENCODING: 3,
++++++++ UPGRADE: 4,
++++++++ CONNECTION_KEEP_ALIVE: 5,
++++++++ CONNECTION_CLOSE: 6,
++++++++ CONNECTION_UPGRADE: 7,
++++++++ TRANSFER_ENCODING_CHUNKED: 8,
++++++++};
++++++++
++++++++// C headers
++++++++export const METHODS_HTTP = [
++++++++ METHODS.DELETE,
++++++++ METHODS.GET,
++++++++ METHODS.HEAD,
++++++++ METHODS.POST,
++++++++ METHODS.PUT,
++++++++ METHODS.CONNECT,
++++++++ METHODS.OPTIONS,
++++++++ METHODS.TRACE,
++++++++ METHODS.COPY,
++++++++ METHODS.LOCK,
++++++++ METHODS.MKCOL,
++++++++ METHODS.MOVE,
++++++++ METHODS.PROPFIND,
++++++++ METHODS.PROPPATCH,
++++++++ METHODS.SEARCH,
++++++++ METHODS.UNLOCK,
++++++++ METHODS.BIND,
++++++++ METHODS.REBIND,
++++++++ METHODS.UNBIND,
++++++++ METHODS.ACL,
++++++++ METHODS.REPORT,
++++++++ METHODS.MKACTIVITY,
++++++++ METHODS.CHECKOUT,
++++++++ METHODS.MERGE,
++++++++ METHODS['M-SEARCH'],
++++++++ METHODS.NOTIFY,
++++++++ METHODS.SUBSCRIBE,
++++++++ METHODS.UNSUBSCRIBE,
++++++++ METHODS.PATCH,
++++++++ METHODS.PURGE,
++++++++ METHODS.MKCALENDAR,
++++++++ METHODS.LINK,
++++++++ METHODS.UNLINK,
++++++++ METHODS.PRI,
++++++++
++++++++ // TODO(indutny): should we allow it with HTTP?
++++++++ METHODS.SOURCE,
++++++++ METHODS.QUERY,
++++++++];
++++++++
++++++++export const METHODS_ICE = [
++++++++ METHODS.SOURCE,
++++++++];
++++++++
++++++++export const METHODS_RTSP = [
++++++++ METHODS.OPTIONS,
++++++++ METHODS.DESCRIBE,
++++++++ METHODS.ANNOUNCE,
++++++++ METHODS.SETUP,
++++++++ METHODS.PLAY,
++++++++ METHODS.PAUSE,
++++++++ METHODS.TEARDOWN,
++++++++ METHODS.GET_PARAMETER,
++++++++ METHODS.SET_PARAMETER,
++++++++ METHODS.REDIRECT,
++++++++ METHODS.RECORD,
++++++++ METHODS.FLUSH,
++++++++
++++++++ // For AirPlay
++++++++ METHODS.GET,
++++++++ METHODS.POST,
++++++++];
++++++++
++++++++export const METHOD_MAP = enumToMap(METHODS);
++++++++
++++++++export const H_METHOD_MAP = Object.fromEntries(
++++++++ Object.entries(METHODS).filter(([ k ]) => k.startsWith('H'))
++++++++);
++++++++
++++++++export const STATUSES_HTTP = [
++++++++ STATUSES.CONTINUE,
++++++++ STATUSES.SWITCHING_PROTOCOLS,
++++++++ STATUSES.PROCESSING,
++++++++ STATUSES.EARLY_HINTS,
++++++++ STATUSES.RESPONSE_IS_STALE,
++++++++ STATUSES.REVALIDATION_FAILED,
++++++++ STATUSES.DISCONNECTED_OPERATION,
++++++++ STATUSES.HEURISTIC_EXPIRATION,
++++++++ STATUSES.MISCELLANEOUS_WARNING,
++++++++ STATUSES.OK,
++++++++ STATUSES.CREATED,
++++++++ STATUSES.ACCEPTED,
++++++++ STATUSES.NON_AUTHORITATIVE_INFORMATION,
++++++++ STATUSES.NO_CONTENT,
++++++++ STATUSES.RESET_CONTENT,
++++++++ STATUSES.PARTIAL_CONTENT,
++++++++ STATUSES.MULTI_STATUS,
++++++++ STATUSES.ALREADY_REPORTED,
++++++++ STATUSES.TRANSFORMATION_APPLIED,
++++++++ STATUSES.IM_USED,
++++++++ STATUSES.MISCELLANEOUS_PERSISTENT_WARNING,
++++++++ STATUSES.MULTIPLE_CHOICES,
++++++++ STATUSES.MOVED_PERMANENTLY,
++++++++ STATUSES.FOUND,
++++++++ STATUSES.SEE_OTHER,
++++++++ STATUSES.NOT_MODIFIED,
++++++++ STATUSES.USE_PROXY,
++++++++ STATUSES.SWITCH_PROXY,
++++++++ STATUSES.TEMPORARY_REDIRECT,
++++++++ STATUSES.PERMANENT_REDIRECT,
++++++++ STATUSES.BAD_REQUEST,
++++++++ STATUSES.UNAUTHORIZED,
++++++++ STATUSES.PAYMENT_REQUIRED,
++++++++ STATUSES.FORBIDDEN,
++++++++ STATUSES.NOT_FOUND,
++++++++ STATUSES.METHOD_NOT_ALLOWED,
++++++++ STATUSES.NOT_ACCEPTABLE,
++++++++ STATUSES.PROXY_AUTHENTICATION_REQUIRED,
++++++++ STATUSES.REQUEST_TIMEOUT,
++++++++ STATUSES.CONFLICT,
++++++++ STATUSES.GONE,
++++++++ STATUSES.LENGTH_REQUIRED,
++++++++ STATUSES.PRECONDITION_FAILED,
++++++++ STATUSES.PAYLOAD_TOO_LARGE,
++++++++ STATUSES.URI_TOO_LONG,
++++++++ STATUSES.UNSUPPORTED_MEDIA_TYPE,
++++++++ STATUSES.RANGE_NOT_SATISFIABLE,
++++++++ STATUSES.EXPECTATION_FAILED,
++++++++ STATUSES.IM_A_TEAPOT,
++++++++ STATUSES.PAGE_EXPIRED,
++++++++ STATUSES.ENHANCE_YOUR_CALM,
++++++++ STATUSES.MISDIRECTED_REQUEST,
++++++++ STATUSES.UNPROCESSABLE_ENTITY,
++++++++ STATUSES.LOCKED,
++++++++ STATUSES.FAILED_DEPENDENCY,
++++++++ STATUSES.TOO_EARLY,
++++++++ STATUSES.UPGRADE_REQUIRED,
++++++++ STATUSES.PRECONDITION_REQUIRED,
++++++++ STATUSES.TOO_MANY_REQUESTS,
++++++++ STATUSES.REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL,
++++++++ STATUSES.REQUEST_HEADER_FIELDS_TOO_LARGE,
++++++++ STATUSES.LOGIN_TIMEOUT,
++++++++ STATUSES.NO_RESPONSE,
++++++++ STATUSES.RETRY_WITH,
++++++++ STATUSES.BLOCKED_BY_PARENTAL_CONTROL,
++++++++ STATUSES.UNAVAILABLE_FOR_LEGAL_REASONS,
++++++++ STATUSES.CLIENT_CLOSED_LOAD_BALANCED_REQUEST,
++++++++ STATUSES.INVALID_X_FORWARDED_FOR,
++++++++ STATUSES.REQUEST_HEADER_TOO_LARGE,
++++++++ STATUSES.SSL_CERTIFICATE_ERROR,
++++++++ STATUSES.SSL_CERTIFICATE_REQUIRED,
++++++++ STATUSES.HTTP_REQUEST_SENT_TO_HTTPS_PORT,
++++++++ STATUSES.INVALID_TOKEN,
++++++++ STATUSES.CLIENT_CLOSED_REQUEST,
++++++++ STATUSES.INTERNAL_SERVER_ERROR,
++++++++ STATUSES.NOT_IMPLEMENTED,
++++++++ STATUSES.BAD_GATEWAY,
++++++++ STATUSES.SERVICE_UNAVAILABLE,
++++++++ STATUSES.GATEWAY_TIMEOUT,
++++++++ STATUSES.HTTP_VERSION_NOT_SUPPORTED,
++++++++ STATUSES.VARIANT_ALSO_NEGOTIATES,
++++++++ STATUSES.INSUFFICIENT_STORAGE,
++++++++ STATUSES.LOOP_DETECTED,
++++++++ STATUSES.BANDWIDTH_LIMIT_EXCEEDED,
++++++++ STATUSES.NOT_EXTENDED,
++++++++ STATUSES.NETWORK_AUTHENTICATION_REQUIRED,
++++++++ STATUSES.WEB_SERVER_UNKNOWN_ERROR,
++++++++ STATUSES.WEB_SERVER_IS_DOWN,
++++++++ STATUSES.CONNECTION_TIMEOUT,
++++++++ STATUSES.ORIGIN_IS_UNREACHABLE,
++++++++ STATUSES.TIMEOUT_OCCURED,
++++++++ STATUSES.SSL_HANDSHAKE_FAILED,
++++++++ STATUSES.INVALID_SSL_CERTIFICATE,
++++++++ STATUSES.RAILGUN_ERROR,
++++++++ STATUSES.SITE_IS_OVERLOADED,
++++++++ STATUSES.SITE_IS_FROZEN,
++++++++ STATUSES.IDENTITY_PROVIDER_AUTHENTICATION_ERROR,
++++++++ STATUSES.NETWORK_READ_TIMEOUT,
++++++++ STATUSES.NETWORK_CONNECT_TIMEOUT,
++++++++];
++++++++
++++++++// Internal
++++++++
++++++++export type CharList = Array<string | number>;
++++++++
++++++++export const ALPHA: CharList = [];
++++++++
++++++++for (let i = 'A'.charCodeAt(0); i <= 'Z'.charCodeAt(0); i++) {
++++++++ // Upper case
++++++++ ALPHA.push(String.fromCharCode(i));
++++++++
++++++++ // Lower case
++++++++ ALPHA.push(String.fromCharCode(i + 0x20));
++++++++}
++++++++
++++++++export const NUM_MAP = {
++++++++ 0: 0, 1: 1, 2: 2, 3: 3, 4: 4,
++++++++ 5: 5, 6: 6, 7: 7, 8: 8, 9: 9,
++++++++};
++++++++
++++++++export const HEX_MAP = {
++++++++ 0: 0, 1: 1, 2: 2, 3: 3, 4: 4,
++++++++ 5: 5, 6: 6, 7: 7, 8: 8, 9: 9,
++++++++ A: 0XA, B: 0XB, C: 0XC, D: 0XD, E: 0XE, F: 0XF,
++++++++ a: 0xa, b: 0xb, c: 0xc, d: 0xd, e: 0xe, f: 0xf,
++++++++};
++++++++
++++++++export const NUM: CharList = [
++++++++ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
++++++++];
++++++++
++++++++export const ALPHANUM: CharList = ALPHA.concat(NUM);
++++++++export const MARK: CharList = [ '-', '_', '.', '!', '~', '*', '\'', '(', ')' ];
++++++++export const USERINFO_CHARS: CharList = ALPHANUM
++++++++ .concat(MARK)
++++++++ .concat([ '%', ';', ':', '&', '=', '+', '$', ',' ]);
++++++++
++++++++// TODO(indutny): use RFC
++++++++export const URL_CHAR: CharList = ([
++++++++ '!', '"', '$', '%', '&', '\'',
++++++++ '(', ')', '*', '+', ',', '-', '.', '/',
++++++++ ':', ';', '<', '=', '>',
++++++++ '@', '[', '\\', ']', '^', '_',
++++++++ '`',
++++++++ '{', '|', '}', '~',
++++++++] as CharList).concat(ALPHANUM);
++++++++
++++++++export const HEX: CharList = NUM.concat(
++++++++ [ 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F' ]);
++++++++
++++++++/* Tokens as defined by rfc 2616. Also lowercases them.
++++++++ * token = 1*<any CHAR except CTLs or separators>
++++++++ * separators = "(" | ")" | "<" | ">" | "@"
++++++++ * | "," | ";" | ":" | "\" | <">
++++++++ * | "/" | "[" | "]" | "?" | "="
++++++++ * | "{" | "}" | SP | HT
++++++++ */
++++++++export const TOKEN: CharList = ([
++++++++ '!', '#', '$', '%', '&', '\'',
++++++++ '*', '+', '-', '.',
++++++++ '^', '_', '`',
++++++++ '|', '~',
++++++++] as CharList).concat(ALPHANUM);
++++++++
++++++++/*
++++++++ * Verify that a char is a valid visible (printable) US-ASCII
++++++++ * character or %x80-FF
++++++++ */
++++++++export const HEADER_CHARS: CharList = [ '\t' ];
++++++++for (let i = 32; i <= 255; i++) {
++++++++ if (i !== 127) {
++++++++ HEADER_CHARS.push(i);
++++++++ }
++++++++}
++++++++
++++++++// ',' = \x44
++++++++export const CONNECTION_TOKEN_CHARS: CharList =
++++++++ HEADER_CHARS.filter((c: string | number) => c !== 44);
++++++++
++++++++export const QUOTED_STRING: CharList = [ '\t', ' ' ];
++++++++for (let i = 0x21; i <= 0xff; i++) {
++++++++ if (i !== 0x22 && i !== 0x5c) { // All characters in ASCII except \ and "
++++++++ QUOTED_STRING.push(i);
++++++++ }
++++++++}
++++++++
++++++++export const HTAB_SP_VCHAR_OBS_TEXT: CharList = [ '\t', ' ' ];
++++++++
++++++++// VCHAR: https://tools.ietf.org/html/rfc5234#appendix-B.1
++++++++for (let i = 0x21; i <= 0x7E; i++) {
++++++++ HTAB_SP_VCHAR_OBS_TEXT.push(i);
++++++++}
++++++++// OBS_TEXT: https://datatracker.ietf.org/doc/html/rfc9110#name-collected-abnf
++++++++for (let i = 0x80; i <= 0xff; i++) {
++++++++ HTAB_SP_VCHAR_OBS_TEXT.push(i);
++++++++}
++++++++
++++++++export const MAJOR = NUM_MAP;
++++++++export const MINOR = MAJOR;
++++++++
++++++++export const SPECIAL_HEADERS = {
++++++++ 'connection': HEADER_STATE.CONNECTION,
++++++++ 'content-length': HEADER_STATE.CONTENT_LENGTH,
++++++++ 'proxy-connection': HEADER_STATE.CONNECTION,
++++++++ 'transfer-encoding': HEADER_STATE.TRANSFER_ENCODING,
++++++++ 'upgrade': HEADER_STATE.UPGRADE,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { LLParse, source } from 'llparse';
++++++++
++++++++import Match = source.node.Match;
++++++++import Node = source.node.Node;
++++++++
++++++++import {
++++++++ CharList,
++++++++ CONNECTION_TOKEN_CHARS, ERROR, FINISH, FLAGS, H_METHOD_MAP, HEADER_CHARS,
++++++++ HEADER_STATE, HEX_MAP, HTAB_SP_VCHAR_OBS_TEXT,
++++++++ LENIENT_FLAGS,
++++++++ MAJOR, METHOD_MAP, METHODS, METHODS_HTTP, METHODS_ICE, METHODS_RTSP,
++++++++ MINOR, NUM_MAP, QUOTED_STRING, SPECIAL_HEADERS,
++++++++ TOKEN, TYPE,
++++++++} from './constants';
++++++++import { URL } from './url';
++++++++
++++++++const NODES: ReadonlyArray<string> = [
++++++++ 'start',
++++++++ 'after_start',
++++++++ 'start_req',
++++++++ 'after_start_req',
++++++++ 'start_res',
++++++++ 'start_req_or_res',
++++++++
++++++++ 'req_or_res_method',
++++++++
++++++++ 'res_http_major',
++++++++ 'res_http_dot',
++++++++ 'res_http_minor',
++++++++ 'res_http_end',
++++++++ 'res_after_version',
++++++++ 'res_status_code_digit_1',
++++++++ 'res_status_code_digit_2',
++++++++ 'res_status_code_digit_3',
++++++++ 'res_status_code_otherwise',
++++++++ 'res_status_start',
++++++++ 'res_status',
++++++++ 'res_line_almost_done',
++++++++
++++++++ 'req_first_space_before_url',
++++++++ 'req_spaces_before_url',
++++++++ 'req_http_start',
++++++++ 'req_http_version',
++++++++ 'req_http_major',
++++++++ 'req_http_dot',
++++++++ 'req_http_minor',
++++++++ 'req_http_end',
++++++++ 'req_http_complete',
++++++++ 'req_http_complete_crlf',
++++++++
++++++++ 'req_pri_upgrade',
++++++++
++++++++ 'headers_start',
++++++++ 'header_field_start',
++++++++ 'header_field',
++++++++ 'header_field_colon',
++++++++ 'header_field_colon_discard_ws',
++++++++ 'header_field_general',
++++++++ 'header_field_general_otherwise',
++++++++ 'header_value_discard_ws',
++++++++ 'header_value_discard_ws_almost_done',
++++++++ 'header_value_discard_lws',
++++++++ 'header_value_start',
++++++++ 'header_value',
++++++++ 'header_value_otherwise',
++++++++ 'header_value_lenient',
++++++++ 'header_value_lenient_failed',
++++++++ 'header_value_lws',
++++++++ 'header_value_te_chunked',
++++++++ 'header_value_te_chunked_last',
++++++++ 'header_value_te_token',
++++++++ 'header_value_te_token_ows',
++++++++ 'header_value_content_length_once',
++++++++ 'header_value_content_length',
++++++++ 'header_value_content_length_ws',
++++++++ 'header_value_connection',
++++++++ 'header_value_connection_ws',
++++++++ 'header_value_connection_token',
++++++++ 'header_value_almost_done',
++++++++
++++++++ 'headers_almost_done',
++++++++ 'headers_done',
++++++++
++++++++ 'chunk_size_start',
++++++++ 'chunk_size_digit',
++++++++ 'chunk_size',
++++++++ 'chunk_size_otherwise',
++++++++ 'chunk_size_almost_done',
++++++++ 'chunk_size_almost_done_lf',
++++++++ 'chunk_extensions',
++++++++ 'chunk_extension_name',
++++++++ 'chunk_extension_value',
++++++++ 'chunk_extension_quoted_value',
++++++++ 'chunk_extension_quoted_value_quoted_pair',
++++++++ 'chunk_extension_quoted_value_done',
++++++++ 'chunk_data',
++++++++ 'chunk_data_almost_done',
++++++++ 'chunk_complete',
++++++++ 'body_identity',
++++++++ 'body_identity_eof',
++++++++
++++++++ 'message_done',
++++++++
++++++++ 'eof',
++++++++ 'cleanup',
++++++++ 'closed',
++++++++ 'restart',
++++++++];
++++++++
++++++++interface ISpanMap {
++++++++ readonly status: source.Span;
++++++++ readonly method: source.Span;
++++++++ readonly version: source.Span;
++++++++ readonly headerField: source.Span;
++++++++ readonly headerValue: source.Span;
++++++++ readonly chunkExtensionName: source.Span;
++++++++ readonly chunkExtensionValue: source.Span;
++++++++ readonly body: source.Span;
++++++++}
++++++++
++++++++interface ICallbackMap {
++++++++ readonly onMessageBegin: source.code.Code;
++++++++ readonly onUrlComplete: source.code.Code;
++++++++ readonly onMethodComplete: source.code.Code;
++++++++ readonly onVersionComplete: source.code.Code;
++++++++ readonly onStatusComplete: source.code.Code;
++++++++ readonly beforeHeadersComplete: source.code.Code;
++++++++ readonly onHeaderFieldComplete: source.code.Code;
++++++++ readonly onHeaderValueComplete: source.code.Code;
++++++++ readonly onHeadersComplete: source.code.Code;
++++++++ readonly afterHeadersComplete: source.code.Code;
++++++++ readonly onChunkHeader: source.code.Code;
++++++++ readonly onChunkExtensionName: source.code.Code;
++++++++ readonly onChunkExtensionValue: source.code.Code;
++++++++ readonly onChunkComplete: source.code.Code;
++++++++ readonly onMessageComplete: source.code.Code;
++++++++ readonly afterMessageComplete: source.code.Code;
++++++++ readonly onReset: source.code.Code;
++++++++}
++++++++
++++++++interface IMulTargets {
++++++++ readonly overflow: string | Node;
++++++++ readonly success: string | Node;
++++++++}
++++++++
++++++++interface IMulOptions {
++++++++ readonly base: number;
++++++++ readonly max?: number;
++++++++ readonly signed: boolean;
++++++++}
++++++++
++++++++interface IIsEqualTargets {
++++++++ readonly equal: string | Node;
++++++++ readonly notEqual: string | Node;
++++++++}
++++++++
++++++++export interface IHTTPResult {
++++++++ readonly entry: Node;
++++++++}
++++++++
++++++++export class HTTP {
++++++++ private readonly url: URL;
++++++++ private readonly TOKEN: CharList;
++++++++ private readonly span: ISpanMap;
++++++++ private readonly callback: ICallbackMap;
++++++++ private readonly nodes: Map<string, Match> = new Map();
++++++++
++++++++ constructor(private readonly llparse: LLParse) {
++++++++ const p = llparse;
++++++++
++++++++ this.url = new URL(p);
++++++++ this.TOKEN = TOKEN;
++++++++
++++++++ this.span = {
++++++++ body: p.span(p.code.span('llhttp__on_body')),
++++++++ chunkExtensionName: p.span(p.code.span('llhttp__on_chunk_extension_name')),
++++++++ chunkExtensionValue: p.span(p.code.span('llhttp__on_chunk_extension_value')),
++++++++ headerField: p.span(p.code.span('llhttp__on_header_field')),
++++++++ headerValue: p.span(p.code.span('llhttp__on_header_value')),
++++++++ method: p.span(p.code.span('llhttp__on_method')),
++++++++ status: p.span(p.code.span('llhttp__on_status')),
++++++++ version: p.span(p.code.span('llhttp__on_version')),
++++++++ };
++++++++
++++++++ /* tslint:disable:object-literal-sort-keys */
++++++++ this.callback = {
++++++++ // User callbacks
++++++++ onUrlComplete: p.code.match('llhttp__on_url_complete'),
++++++++ onStatusComplete: p.code.match('llhttp__on_status_complete'),
++++++++ onMethodComplete: p.code.match('llhttp__on_method_complete'),
++++++++ onVersionComplete: p.code.match('llhttp__on_version_complete'),
++++++++ onHeaderFieldComplete: p.code.match('llhttp__on_header_field_complete'),
++++++++ onHeaderValueComplete: p.code.match('llhttp__on_header_value_complete'),
++++++++ onHeadersComplete: p.code.match('llhttp__on_headers_complete'),
++++++++ onMessageBegin: p.code.match('llhttp__on_message_begin'),
++++++++ onMessageComplete: p.code.match('llhttp__on_message_complete'),
++++++++ onChunkHeader: p.code.match('llhttp__on_chunk_header'),
++++++++ onChunkExtensionName: p.code.match('llhttp__on_chunk_extension_name_complete'),
++++++++ onChunkExtensionValue: p.code.match('llhttp__on_chunk_extension_value_complete'),
++++++++ onChunkComplete: p.code.match('llhttp__on_chunk_complete'),
++++++++ onReset: p.code.match('llhttp__on_reset'),
++++++++
++++++++ // Internal callbacks `src/http.c`
++++++++ beforeHeadersComplete:
++++++++ p.code.match('llhttp__before_headers_complete'),
++++++++ afterHeadersComplete: p.code.match('llhttp__after_headers_complete'),
++++++++ afterMessageComplete: p.code.match('llhttp__after_message_complete'),
++++++++ };
++++++++ /* tslint:enable:object-literal-sort-keys */
++++++++
++++++++ for (const name of NODES) {
++++++++ this.nodes.set(name, p.node(name) as Match);
++++++++ }
++++++++ }
++++++++
++++++++ public build(): IHTTPResult {
++++++++ const p = this.llparse;
++++++++
++++++++ p.property('i64', 'content_length');
++++++++ p.property('i8', 'type');
++++++++ p.property('i8', 'method');
++++++++ p.property('i8', 'http_major');
++++++++ p.property('i8', 'http_minor');
++++++++ p.property('i8', 'header_state');
++++++++ p.property('i16', 'lenient_flags');
++++++++ p.property('i8', 'upgrade');
++++++++ p.property('i8', 'finish');
++++++++ p.property('i16', 'flags');
++++++++ p.property('i16', 'status_code');
++++++++ p.property('i8', 'initial_message_completed');
++++++++
++++++++ // Verify defaults
++++++++ assert.strictEqual(FINISH.SAFE, 0);
++++++++ assert.strictEqual(TYPE.BOTH, 0);
++++++++
++++++++ // Shared settings (to be used in C wrapper)
++++++++ p.property('ptr', 'settings');
++++++++
++++++++ this.buildLine();
++++++++ this.buildHeaders();
++++++++
++++++++ return {
++++++++ entry: this.node('start'),
++++++++ };
++++++++ }
++++++++
++++++++ private buildLine(): void {
++++++++ const p = this.llparse;
++++++++ const span = this.span;
++++++++ const n = (name: string): Match => this.node<Match>(name);
++++++++
++++++++ const url = this.url.build();
++++++++
++++++++ const switchType = this.load('type', {
++++++++ [TYPE.REQUEST]: n('start_req'),
++++++++ [TYPE.RESPONSE]: n('start_res'),
++++++++ }, n('start_req_or_res'));
++++++++
++++++++ n('start')
++++++++ .match([ '\r', '\n' ], n('start'))
++++++++ .otherwise(
++++++++ this.load('initial_message_completed', {
++++++++ 1: this.invokePausable('on_reset', ERROR.CB_RESET, n('after_start')),
++++++++ }, n('after_start')),
++++++++ );
++++++++
++++++++ n('after_start').otherwise(
++++++++ this.update(
++++++++ 'finish',
++++++++ FINISH.UNSAFE,
++++++++ this.invokePausable('on_message_begin', ERROR.CB_MESSAGE_BEGIN, switchType),
++++++++ ),
++++++++ );
++++++++
++++++++ n('start_req_or_res')
++++++++ .peek('H', this.span.method.start(n('req_or_res_method')))
++++++++ .otherwise(this.update('type', TYPE.REQUEST, 'start_req'));
++++++++
++++++++ n('req_or_res_method')
++++++++ .select(H_METHOD_MAP, this.store('method',
++++++++ this.update('type', TYPE.REQUEST, this.span.method.end(
++++++++ this.invokePausable('on_method_complete', ERROR.CB_METHOD_COMPLETE, n('req_first_space_before_url')),
++++++++ )),
++++++++ ))
++++++++ .match('HTTP/', this.span.method.end(this.update('type', TYPE.RESPONSE,
++++++++ this.span.version.start(n('res_http_major')))))
++++++++ .otherwise(p.error(ERROR.INVALID_CONSTANT, 'Invalid word encountered'));
++++++++
++++++++ const checkVersion = (destination: string): Node => {
++++++++ const node = n(destination);
++++++++ const errorNode = this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Invalid HTTP version'));
++++++++
++++++++ return this.testLenientFlags(LENIENT_FLAGS.VERSION,
++++++++ {
++++++++ 1: node,
++++++++ },
++++++++ this.load('http_major', {
++++++++ 0: this.load('http_minor', {
++++++++ 9: node,
++++++++ }, errorNode),
++++++++ 1: this.load('http_minor', {
++++++++ 0: node,
++++++++ 1: node,
++++++++ }, errorNode),
++++++++ 2: this.load('http_minor', {
++++++++ 0: node,
++++++++ }, errorNode),
++++++++ }, errorNode),
++++++++ );
++++++++ };
++++++++
++++++++ const checkIfAllowLFWithoutCR = (success: Node, failure: Node) => {
++++++++ return this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_CR_BEFORE_LF, { 1: success }, failure);
++++++++ };
++++++++
++++++++ // Response
++++++++ n('start_res')
++++++++ .match('HTTP/', span.version.start(n('res_http_major')))
++++++++ .otherwise(p.error(ERROR.INVALID_CONSTANT, 'Expected HTTP/'));
++++++++
++++++++ n('res_http_major')
++++++++ .select(MAJOR, this.store('http_major', 'res_http_dot'))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Invalid major version')));
++++++++
++++++++ n('res_http_dot')
++++++++ .match('.', n('res_http_minor'))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Expected dot')));
++++++++
++++++++ n('res_http_minor')
++++++++ .select(MINOR, this.store('http_minor', checkVersion('res_http_end')))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Invalid minor version')));
++++++++
++++++++ n('res_http_end')
++++++++ .otherwise(this.span.version.end(
++++++++ this.invokePausable('on_version_complete', ERROR.CB_VERSION_COMPLETE, 'res_after_version'),
++++++++ ));
++++++++
++++++++ n('res_after_version')
++++++++ .match(' ', this.update('status_code', 0, 'res_status_code_digit_1'))
++++++++ .otherwise(p.error(ERROR.INVALID_VERSION,
++++++++ 'Expected space after version'));
++++++++
++++++++ n('res_status_code_digit_1')
++++++++ .select(NUM_MAP, this.mulAdd('status_code', {
++++++++ overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
++++++++ success: 'res_status_code_digit_2',
++++++++ }))
++++++++ .otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));
++++++++
++++++++ n('res_status_code_digit_2')
++++++++ .select(NUM_MAP, this.mulAdd('status_code', {
++++++++ overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
++++++++ success: 'res_status_code_digit_3',
++++++++ }))
++++++++ .otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));
++++++++
++++++++ n('res_status_code_digit_3')
++++++++ .select(NUM_MAP, this.mulAdd('status_code', {
++++++++ overflow: p.error(ERROR.INVALID_STATUS, 'Invalid status code'),
++++++++ success: 'res_status_code_otherwise',
++++++++ }))
++++++++ .otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid status code'));
++++++++
++++++++ const onStatusComplete = this.invokePausable(
++++++++ 'on_status_complete', ERROR.CB_STATUS_COMPLETE, n('headers_start'),
++++++++ );
++++++++
++++++++ n('res_status_code_otherwise')
++++++++ .match(' ', n('res_status_start'))
++++++++ .match('\r', n('res_line_almost_done'))
++++++++ .match(
++++++++ '\n',
++++++++ checkIfAllowLFWithoutCR(
++++++++ onStatusComplete,
++++++++ p.error(ERROR.INVALID_STATUS, 'Invalid response status'),
++++++++ ),
++++++++ )
++++++++ .otherwise(p.error(ERROR.INVALID_STATUS, 'Invalid response status'));
++++++++
++++++++ n('res_status_start')
++++++++ .otherwise(span.status.start(n('res_status')));
++++++++
++++++++ n('res_status')
++++++++ .peek('\r', span.status.end().skipTo(n('res_line_almost_done')))
++++++++ .peek(
++++++++ '\n',
++++++++ span.status.end().skipTo(
++++++++ checkIfAllowLFWithoutCR(
++++++++ onStatusComplete,
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after response line'),
++++++++ ),
++++++++ ),
++++++++ )
++++++++ .skipTo(n('res_status'));
++++++++
++++++++ n('res_line_almost_done')
++++++++ .match([ '\r', '\n' ], onStatusComplete)
++++++++ .otherwise(this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_LF_AFTER_CR, {
++++++++ 1: onStatusComplete,
++++++++ }, p.error(ERROR.STRICT, 'Expected LF after CR')));
++++++++
++++++++ // Request
++++++++ n('start_req').otherwise(this.span.method.start(n('after_start_req')));
++++++++
++++++++ n('after_start_req')
++++++++ .select(METHOD_MAP, this.store('method', this.span.method.end(
++++++++ this.invokePausable('on_method_complete', ERROR.CB_METHOD_COMPLETE, n('req_first_space_before_url'),
++++++++ ))))
++++++++ .otherwise(p.error(ERROR.INVALID_METHOD, 'Invalid method encountered'));
++++++++
++++++++ n('req_first_space_before_url')
++++++++ .match(' ', n('req_spaces_before_url'))
++++++++ .otherwise(p.error(ERROR.INVALID_METHOD, 'Expected space after method'));
++++++++
++++++++ n('req_spaces_before_url')
++++++++ .match(' ', n('req_spaces_before_url'))
++++++++ .otherwise(this.isEqual('method', METHODS.CONNECT, {
++++++++ equal: url.entry.connect,
++++++++ notEqual: url.entry.normal,
++++++++ }));
++++++++
++++++++ const onUrlCompleteHTTP = this.invokePausable(
++++++++ 'on_url_complete', ERROR.CB_URL_COMPLETE, n('req_http_start'),
++++++++ );
++++++++
++++++++ url.exit.toHTTP
++++++++ .otherwise(onUrlCompleteHTTP);
++++++++
++++++++ const onUrlCompleteHTTP09 = this.invokePausable(
++++++++ 'on_url_complete', ERROR.CB_URL_COMPLETE, n('headers_start'),
++++++++ );
++++++++
++++++++ url.exit.toHTTP09
++++++++ .otherwise(
++++++++ this.update('http_major', 0,
++++++++ this.update('http_minor', 9, onUrlCompleteHTTP09)),
++++++++ );
++++++++
++++++++ const checkMethod = (methods: number[], error: string): Node => {
++++++++ const success = n('req_http_version');
++++++++ const failure = p.error(ERROR.INVALID_CONSTANT, error);
++++++++
++++++++ const map: { [key: number]: Node } = {};
++++++++ for (const method of methods) {
++++++++ map[method] = success;
++++++++ }
++++++++
++++++++ return this.load('method', map, failure);
++++++++ };
++++++++
++++++++ n('req_http_start')
++++++++ .match('HTTP/', checkMethod(METHODS_HTTP,
++++++++ 'Invalid method for HTTP/x.x request'))
++++++++ .match('RTSP/', checkMethod(METHODS_RTSP,
++++++++ 'Invalid method for RTSP/x.x request'))
++++++++ .match('ICE/', checkMethod(METHODS_ICE,
++++++++ 'Expected SOURCE method for ICE/x.x request'))
++++++++ .match(' ', n('req_http_start'))
++++++++ .otherwise(p.error(ERROR.INVALID_CONSTANT, 'Expected HTTP/'));
++++++++
++++++++ n('req_http_version').otherwise(span.version.start(n('req_http_major')));
++++++++
++++++++ n('req_http_major')
++++++++ .select(MAJOR, this.store('http_major', 'req_http_dot'))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Invalid major version')));
++++++++
++++++++ n('req_http_dot')
++++++++ .match('.', n('req_http_minor'))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Expected dot')));
++++++++
++++++++ n('req_http_minor')
++++++++ .select(MINOR, this.store('http_minor', checkVersion('req_http_end')))
++++++++ .otherwise(this.span.version.end(p.error(ERROR.INVALID_VERSION, 'Invalid minor version')));
++++++++
++++++++ n('req_http_end').otherwise(
++++++++ span.version.end(
++++++++ this.invokePausable(
++++++++ 'on_version_complete',
++++++++ ERROR.CB_VERSION_COMPLETE,
++++++++ this.load('method', {
++++++++ [METHODS.PRI]: n('req_pri_upgrade'),
++++++++ }, n('req_http_complete')),
++++++++ ),
++++++++ ),
++++++++ );
++++++++
++++++++ n('req_http_complete')
++++++++ .match('\r', n('req_http_complete_crlf'))
++++++++ .match(
++++++++ '\n',
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('req_http_complete_crlf'),
++++++++ p.error(ERROR.INVALID_VERSION, 'Expected CRLF after version'),
++++++++ ),
++++++++ )
++++++++ .otherwise(p.error(ERROR.INVALID_VERSION, 'Expected CRLF after version'));
++++++++
++++++++ n('req_http_complete_crlf')
++++++++ .match('\n', n('headers_start'))
++++++++ .otherwise(this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_LF_AFTER_CR, {
++++++++ 1: n('headers_start'),
++++++++ }, p.error(ERROR.STRICT, 'Expected CRLF after version')));
++++++++
++++++++ n('req_pri_upgrade')
++++++++ .match('\r\n\r\nSM\r\n\r\n',
++++++++ p.error(ERROR.PAUSED_H2_UPGRADE, 'Pause on PRI/Upgrade'))
++++++++ .otherwise(
++++++++ p.error(ERROR.INVALID_VERSION, 'Expected HTTP/2 Connection Preface'));
++++++++ }
++++++++
++++++++ private buildHeaders(): void {
++++++++ this.buildHeaderField();
++++++++ this.buildHeaderValue();
++++++++ }
++++++++
++++++++ private buildHeaderField(): void {
++++++++ const p = this.llparse;
++++++++ const span = this.span;
++++++++ const n = (name: string): Match => this.node<Match>(name);
++++++++
++++++++ const onInvalidHeaderFieldChar =
++++++++ p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header field char');
++++++++
++++++++ n('headers_start')
++++++++ .match(' ',
++++++++ this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: n('header_field_start'),
++++++++ }, p.error(ERROR.UNEXPECTED_SPACE, 'Unexpected space after start line')),
++++++++ )
++++++++ .otherwise(n('header_field_start'));
++++++++
++++++++ n('header_field_start')
++++++++ .match('\r', n('headers_almost_done'))
++++++++ .match('\n',
++++++++ this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_CR_BEFORE_LF, {
++++++++ 1: this.testFlags(FLAGS.TRAILING, {
++++++++ 1: this.invokePausable('on_chunk_complete',
++++++++ ERROR.CB_CHUNK_COMPLETE, 'message_done'),
++++++++ }).otherwise(this.headersCompleted()),
++++++++ }, onInvalidHeaderFieldChar),
++++++++ )
++++++++ .peek(':', p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header token'))
++++++++ .otherwise(span.headerField.start(n('header_field')));
++++++++
++++++++ n('header_field')
++++++++ .transform(p.transform.toLower())
++++++++ // Match headers that need special treatment
++++++++ .select(SPECIAL_HEADERS, this.store('header_state', 'header_field_colon'))
++++++++ .otherwise(this.resetHeaderState('header_field_general'));
++++++++
++++++++ /* https://www.rfc-editor.org/rfc/rfc7230.html#section-3.3.3, paragraph 3.
++++++++ *
++++++++ * If a message is received with both a Transfer-Encoding and a
++++++++ * Content-Length header field, the Transfer-Encoding overrides the
++++++++ * Content-Length. Such a message might indicate an attempt to
++++++++ * perform request smuggling (Section 9.5) or response splitting
++++++++ * (Section 9.4) and **ought to be handled as an error**. A sender MUST
++++++++ * remove the received Content-Length field prior to forwarding such
++++++++ * a message downstream.
++++++++ *
++++++++ * Since llhttp 9, we go for the stricter approach and treat this as an error.
++++++++ */
++++++++ const checkInvalidTransferEncoding = (otherwise: Node) => {
++++++++ return this.testFlags(FLAGS.CONTENT_LENGTH, {
++++++++ 1: this.testLenientFlags(LENIENT_FLAGS.CHUNKED_LENGTH, {
++++++++ 0: p.error(ERROR.INVALID_TRANSFER_ENCODING, 'Transfer-Encoding can\'t be present with Content-Length'),
++++++++ }).otherwise(otherwise),
++++++++ }).otherwise(otherwise);
++++++++ };
++++++++
++++++++ const checkInvalidContentLength = (otherwise: Node) => {
++++++++ return this.testFlags(FLAGS.TRANSFER_ENCODING, {
++++++++ 1: this.testLenientFlags(LENIENT_FLAGS.CHUNKED_LENGTH, {
++++++++ 0: p.error(ERROR.INVALID_CONTENT_LENGTH, 'Content-Length can\'t be present with Transfer-Encoding'),
++++++++ }).otherwise(otherwise),
++++++++ }).otherwise(otherwise);
++++++++ };
++++++++
++++++++ const onHeaderFieldComplete = this.invokePausable(
++++++++ 'on_header_field_complete', ERROR.CB_HEADER_FIELD_COMPLETE,
++++++++ this.load('header_state', {
++++++++ [HEADER_STATE.TRANSFER_ENCODING]: checkInvalidTransferEncoding(n('header_value_discard_ws')),
++++++++ [HEADER_STATE.CONTENT_LENGTH]: checkInvalidContentLength(n('header_value_discard_ws')),
++++++++ }, 'header_value_discard_ws'),
++++++++ );
++++++++
++++++++ const checkLenientFlagsOnColon =
++++++++ this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: n('header_field_colon_discard_ws'),
++++++++ }, span.headerField.end().skipTo(onInvalidHeaderFieldChar));
++++++++
++++++++ n('header_field_colon')
++++++++ // https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.4
++++++++ // Whitespace character is not allowed between the header field-name
++++++++ // and colon. If the next token matches whitespace then throw an error.
++++++++ //
++++++++ // Add a check for the lenient flag. If the lenient flag is set, the
++++++++ // whitespace token is allowed to support legacy code not following
++++++++ // http specs.
++++++++ .peek(' ', checkLenientFlagsOnColon)
++++++++ .peek(':', span.headerField.end().skipTo(onHeaderFieldComplete))
++++++++ // Fallback to general header, there're additional characters:
++++++++ // `Connection-Duration` instead of `Connection` and so on.
++++++++ .otherwise(this.resetHeaderState('header_field_general'));
++++++++
++++++++ n('header_field_colon_discard_ws')
++++++++ .match(' ', n('header_field_colon_discard_ws'))
++++++++ .otherwise(n('header_field_colon'));
++++++++
++++++++ n('header_field_general')
++++++++ .match(this.TOKEN, n('header_field_general'))
++++++++ .otherwise(n('header_field_general_otherwise'));
++++++++
++++++++ // Just a performance optimization, split the node so that the fast case
++++++++ // remains in `header_field_general`
++++++++ n('header_field_general_otherwise')
++++++++ .peek(':', span.headerField.end().skipTo(onHeaderFieldComplete))
++++++++ .otherwise(p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header token'));
++++++++ }
++++++++
++++++++ private buildHeaderValue(): void {
++++++++ const p = this.llparse;
++++++++ const span = this.span;
++++++++ const callback = this.callback;
++++++++ const n = (name: string): Match => this.node<Match>(name);
++++++++
++++++++ const fallback = this.resetHeaderState('header_value');
++++++++
++++++++ n('header_value_discard_ws')
++++++++ .match([ ' ', '\t' ], n('header_value_discard_ws'))
++++++++ .match('\r', n('header_value_discard_ws_almost_done'))
++++++++ .match('\n', this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_CR_BEFORE_LF, {
++++++++ 1: n('header_value_discard_lws'),
++++++++ }, p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header value char')))
++++++++ .otherwise(span.headerValue.start(n('header_value_start')));
++++++++
++++++++ n('header_value_discard_ws_almost_done')
++++++++ .match('\n', n('header_value_discard_lws'))
++++++++ .otherwise(
++++++++ this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: n('header_value_discard_lws'),
++++++++ }, p.error(ERROR.STRICT, 'Expected LF after CR')),
++++++++ );
++++++++
++++++++ const onHeaderValueComplete = this.invokePausable(
++++++++ 'on_header_value_complete', ERROR.CB_HEADER_VALUE_COMPLETE, n('header_field_start'),
++++++++ );
++++++++
++++++++ const emptyContentLengthError = p.error(
++++++++ ERROR.INVALID_CONTENT_LENGTH, 'Empty Content-Length');
++++++++ const checkContentLengthEmptiness = this.load('header_state', {
++++++++ [HEADER_STATE.CONTENT_LENGTH]: emptyContentLengthError,
++++++++ }, this.setHeaderFlags(
++++++++ this.emptySpan(span.headerValue, onHeaderValueComplete)));
++++++++
++++++++ n('header_value_discard_lws')
++++++++ .match([ ' ', '\t' ], this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: n('header_value_discard_ws'),
++++++++ }, p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header value char')))
++++++++ .otherwise(checkContentLengthEmptiness);
++++++++
++++++++ // Multiple `Transfer-Encoding` headers should be treated as one, but with
++++++++ // values separate by a comma.
++++++++ //
++++++++ // See: https://tools.ietf.org/html/rfc7230#section-3.2.2
++++++++ const toTransferEncoding = this.unsetFlag(
++++++++ FLAGS.CHUNKED,
++++++++ 'header_value_te_chunked');
++++++++
++++++++ // Once chunked has been selected, no other encoding is possible in requests
++++++++ // https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1
++++++++ const forbidAfterChunkedInRequest = (otherwise: Node) => {
++++++++ return this.load('type', {
++++++++ [TYPE.REQUEST]: this.testLenientFlags(LENIENT_FLAGS.TRANSFER_ENCODING, {
++++++++ 0: span.headerValue.end().skipTo(
++++++++ p.error(ERROR.INVALID_TRANSFER_ENCODING, 'Invalid `Transfer-Encoding` header value'),
++++++++ ),
++++++++ }).otherwise(otherwise),
++++++++ }, otherwise);
++++++++ };
++++++++
++++++++ n('header_value_start')
++++++++ .otherwise(this.load('header_state', {
++++++++ [HEADER_STATE.UPGRADE]: this.setFlag(FLAGS.UPGRADE, fallback),
++++++++ [HEADER_STATE.TRANSFER_ENCODING]: this.testFlags(
++++++++ FLAGS.CHUNKED,
++++++++ {
++++++++ 1: forbidAfterChunkedInRequest(this.setFlag(FLAGS.TRANSFER_ENCODING, toTransferEncoding)),
++++++++ },
++++++++ this.setFlag(FLAGS.TRANSFER_ENCODING, toTransferEncoding)),
++++++++ [HEADER_STATE.CONTENT_LENGTH]: n('header_value_content_length_once'),
++++++++ [HEADER_STATE.CONNECTION]: n('header_value_connection'),
++++++++ }, 'header_value'));
++++++++
++++++++ //
++++++++ // Transfer-Encoding
++++++++ //
++++++++
++++++++ n('header_value_te_chunked')
++++++++ .transform(p.transform.toLowerUnsafe())
++++++++ .match(
++++++++ 'chunked',
++++++++ n('header_value_te_chunked_last'),
++++++++ )
++++++++ .otherwise(n('header_value_te_token'));
++++++++
++++++++ n('header_value_te_chunked_last')
++++++++ .match(' ', n('header_value_te_chunked_last'))
++++++++ .peek([ '\r', '\n' ], this.update('header_state',
++++++++ HEADER_STATE.TRANSFER_ENCODING_CHUNKED,
++++++++ 'header_value_otherwise'))
++++++++ .peek(',', forbidAfterChunkedInRequest(n('header_value_te_chunked')))
++++++++ .otherwise(n('header_value_te_token'));
++++++++
++++++++ n('header_value_te_token')
++++++++ .match(',', n('header_value_te_token_ows'))
++++++++ .match(CONNECTION_TOKEN_CHARS, n('header_value_te_token'))
++++++++ .otherwise(fallback);
++++++++
++++++++ n('header_value_te_token_ows')
++++++++ .match([ ' ', '\t' ], n('header_value_te_token_ows'))
++++++++ .otherwise(n('header_value_te_chunked'));
++++++++
++++++++ //
++++++++ // Content-Length
++++++++ //
++++++++
++++++++ const invalidContentLength = (reason: string): Node => {
++++++++ // End span for easier testing
++++++++ // TODO(indutny): minimize code size
++++++++ return span.headerValue.end()
++++++++ .otherwise(p.error(ERROR.INVALID_CONTENT_LENGTH, reason));
++++++++ };
++++++++
++++++++ n('header_value_content_length_once')
++++++++ .otherwise(this.testFlags(FLAGS.CONTENT_LENGTH, {
++++++++ 0: n('header_value_content_length'),
++++++++ }, p.error(ERROR.UNEXPECTED_CONTENT_LENGTH, 'Duplicate Content-Length')));
++++++++
++++++++ n('header_value_content_length')
++++++++ .select(NUM_MAP, this.mulAdd('content_length', {
++++++++ overflow: invalidContentLength('Content-Length overflow'),
++++++++ success: 'header_value_content_length',
++++++++ }))
++++++++ .otherwise(n('header_value_content_length_ws'));
++++++++
++++++++ n('header_value_content_length_ws')
++++++++ .match(' ', n('header_value_content_length_ws'))
++++++++ .peek([ '\r', '\n' ],
++++++++ this.setFlag(FLAGS.CONTENT_LENGTH, 'header_value_otherwise'))
++++++++ .otherwise(invalidContentLength('Invalid character in Content-Length'));
++++++++
++++++++ //
++++++++ // Connection
++++++++ //
++++++++
++++++++ n('header_value_connection')
++++++++ .transform(p.transform.toLower())
++++++++ // TODO(indutny): extra node for token back-edge?
++++++++ // Skip lws
++++++++ .match([ ' ', '\t' ], n('header_value_connection'))
++++++++ .match(
++++++++ 'close',
++++++++ this.update('header_state', HEADER_STATE.CONNECTION_CLOSE,
++++++++ 'header_value_connection_ws'),
++++++++ )
++++++++ .match(
++++++++ 'upgrade',
++++++++ this.update('header_state', HEADER_STATE.CONNECTION_UPGRADE,
++++++++ 'header_value_connection_ws'),
++++++++ )
++++++++ .match(
++++++++ 'keep-alive',
++++++++ this.update('header_state', HEADER_STATE.CONNECTION_KEEP_ALIVE,
++++++++ 'header_value_connection_ws'),
++++++++ )
++++++++ .otherwise(n('header_value_connection_token'));
++++++++
++++++++ n('header_value_connection_ws')
++++++++ .match(',', this.setHeaderFlags('header_value_connection'))
++++++++ .match(' ', n('header_value_connection_ws'))
++++++++ .peek([ '\r', '\n' ], n('header_value_otherwise'))
++++++++ .otherwise(this.resetHeaderState('header_value_connection_token'));
++++++++
++++++++ n('header_value_connection_token')
++++++++ .match(',', n('header_value_connection'))
++++++++ .match(CONNECTION_TOKEN_CHARS,
++++++++ n('header_value_connection_token'))
++++++++ .otherwise(n('header_value_otherwise'));
++++++++
++++++++ // Split for performance reasons
++++++++ n('header_value')
++++++++ .match(HEADER_CHARS, n('header_value'))
++++++++ .otherwise(n('header_value_otherwise'));
++++++++
++++++++ const checkIfAllowLFWithoutCR = (success: Node, failure: Node) => {
++++++++ return this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_CR_BEFORE_LF, { 1: success }, failure);
++++++++ };
++++++++
++++++++ const checkLenient = this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: n('header_value_lenient'),
++++++++ }, span.headerValue.end(p.error(ERROR.INVALID_HEADER_TOKEN, 'Invalid header value char')));
++++++++
++++++++ n('header_value_otherwise')
++++++++ .peek('\r', span.headerValue.end().skipTo(n('header_value_almost_done')))
++++++++ .peek(
++++++++ '\n',
++++++++ span.headerValue.end(
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('header_value_almost_done'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after header value'),
++++++++ ),
++++++++ ),
++++++++ )
++++++++ .otherwise(checkLenient);
++++++++
++++++++ n('header_value_lenient')
++++++++ .peek('\r', span.headerValue.end().skipTo(n('header_value_almost_done')))
++++++++ .peek('\n', span.headerValue.end(n('header_value_almost_done')))
++++++++ .skipTo(n('header_value_lenient'));
++++++++
++++++++ n('header_value_almost_done')
++++++++ .match('\n', n('header_value_lws'))
++++++++ .otherwise(p.error(ERROR.LF_EXPECTED,
++++++++ 'Missing expected LF after header value'));
++++++++
++++++++ n('header_value_lws')
++++++++ .peek(
++++++++ [ ' ', '\t' ],
++++++++ this.testLenientFlags(LENIENT_FLAGS.HEADERS, {
++++++++ 1: this.load('header_state', {
++++++++ [HEADER_STATE.TRANSFER_ENCODING_CHUNKED]:
++++++++ this.resetHeaderState(span.headerValue.start(n('header_value_start'))),
++++++++ }, span.headerValue.start(n('header_value_start'))),
++++++++ }, p.error(ERROR.INVALID_HEADER_TOKEN, 'Unexpected whitespace after header value')))
++++++++ .otherwise(this.setHeaderFlags(onHeaderValueComplete));
++++++++
++++++++ const checkTrailing = this.testFlags(FLAGS.TRAILING, {
++++++++ 1: this.invokePausable('on_chunk_complete',
++++++++ ERROR.CB_CHUNK_COMPLETE, 'message_done'),
++++++++ }).otherwise(this.headersCompleted());
++++++++
++++++++ n('headers_almost_done')
++++++++ .match('\n', checkTrailing)
++++++++ .otherwise(
++++++++ this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_LF_AFTER_CR, {
++++++++ 1: checkTrailing,
++++++++ }, p.error(ERROR.STRICT, 'Expected LF after headers')));
++++++++
++++++++ const upgradePause = p.pause(ERROR.PAUSED_UPGRADE,
++++++++ 'Pause on CONNECT/Upgrade');
++++++++
++++++++ const afterHeadersComplete = p.invoke(callback.afterHeadersComplete, {
++++++++ 1: this.invokePausable('on_message_complete',
++++++++ ERROR.CB_MESSAGE_COMPLETE, upgradePause),
++++++++ 2: n('chunk_size_start'),
++++++++ 3: n('body_identity'),
++++++++ 4: n('body_identity_eof'),
++++++++
++++++++ // non-chunked `Transfer-Encoding` for request, see `src/native/http.c`
++++++++ 5: p.error(ERROR.INVALID_TRANSFER_ENCODING,
++++++++ 'Request has invalid `Transfer-Encoding`'),
++++++++ });
++++++++
++++++++ n('headers_done')
++++++++ .otherwise(afterHeadersComplete);
++++++++
++++++++ upgradePause
++++++++ .otherwise(n('cleanup'));
++++++++
++++++++ afterHeadersComplete
++++++++ .otherwise(this.invokePausable('on_message_complete',
++++++++ ERROR.CB_MESSAGE_COMPLETE, 'cleanup'));
++++++++
++++++++ n('body_identity')
++++++++ .otherwise(span.body.start()
++++++++ .otherwise(p.consume('content_length').otherwise(
++++++++ span.body.end(n('message_done')))));
++++++++
++++++++ n('body_identity_eof')
++++++++ .otherwise(
++++++++ this.update('finish', FINISH.SAFE_WITH_CB, span.body.start(n('eof'))));
++++++++
++++++++ // Just read everything until EOF
++++++++ n('eof')
++++++++ .skipTo(n('eof'));
++++++++
++++++++ n('chunk_size_start')
++++++++ .otherwise(this.update('content_length', 0, 'chunk_size_digit'));
++++++++
++++++++ const addContentLength = this.mulAdd('content_length', {
++++++++ overflow: p.error(ERROR.INVALID_CHUNK_SIZE, 'Chunk size overflow'),
++++++++ success: 'chunk_size',
++++++++ }, { signed: false, base: 0x10 });
++++++++
++++++++ n('chunk_size_digit')
++++++++ .select(HEX_MAP, addContentLength)
++++++++ .otherwise(p.error(ERROR.INVALID_CHUNK_SIZE,
++++++++ 'Invalid character in chunk size'));
++++++++
++++++++ n('chunk_size')
++++++++ .select(HEX_MAP, addContentLength)
++++++++ .otherwise(n('chunk_size_otherwise'));
++++++++
++++++++ n('chunk_size_otherwise')
++++++++ .match(
++++++++ [ ' ', '\t' ],
++++++++ this.testLenientFlags(
++++++++ LENIENT_FLAGS.SPACES_AFTER_CHUNK_SIZE,
++++++++ {
++++++++ 1: n('chunk_size_otherwise'),
++++++++ },
++++++++ p.error(ERROR.INVALID_CHUNK_SIZE, 'Invalid character in chunk size'),
++++++++ ),
++++++++ )
++++++++ .match('\r', n('chunk_size_almost_done'))
++++++++ .match(
++++++++ '\n',
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('chunk_size_almost_done'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after chunk size'),
++++++++ ),
++++++++ )
++++++++ .match(';', n('chunk_extensions'))
++++++++ .otherwise(p.error(ERROR.INVALID_CHUNK_SIZE,
++++++++ 'Invalid character in chunk size'));
++++++++
++++++++ const onChunkExtensionNameCompleted = (destination: Node) => {
++++++++ return this.invokePausable(
++++++++ 'on_chunk_extension_name', ERROR.CB_CHUNK_EXTENSION_NAME_COMPLETE, destination);
++++++++ };
++++++++
++++++++ const onChunkExtensionValueCompleted = (destination: Node) => {
++++++++ return this.invokePausable(
++++++++ 'on_chunk_extension_value', ERROR.CB_CHUNK_EXTENSION_VALUE_COMPLETE, destination);
++++++++ };
++++++++
++++++++ n('chunk_extensions')
++++++++ .match(' ', p.error(ERROR.STRICT, 'Invalid character in chunk extensions'))
++++++++ .match('\r', p.error(ERROR.STRICT, 'Invalid character in chunk extensions'))
++++++++ .otherwise(this.span.chunkExtensionName.start(n('chunk_extension_name')));
++++++++
++++++++ n('chunk_extension_name')
++++++++ .match(TOKEN, n('chunk_extension_name'))
++++++++ .peek('=', this.span.chunkExtensionName.end().skipTo(
++++++++ this.span.chunkExtensionValue.start(
++++++++ onChunkExtensionNameCompleted(n('chunk_extension_value')),
++++++++ ),
++++++++ ))
++++++++ .peek(';', this.span.chunkExtensionName.end().skipTo(
++++++++ onChunkExtensionNameCompleted(n('chunk_extensions')),
++++++++ ))
++++++++ .peek('\r', this.span.chunkExtensionName.end().skipTo(
++++++++ onChunkExtensionNameCompleted(n('chunk_size_almost_done')),
++++++++ ))
++++++++ .peek('\n', this.span.chunkExtensionName.end(
++++++++ onChunkExtensionNameCompleted(
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('chunk_size_almost_done'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after chunk extension name'),
++++++++ ),
++++++++ ),
++++++++ ))
++++++++ .otherwise(this.span.chunkExtensionName.end().skipTo(
++++++++ p.error(ERROR.STRICT, 'Invalid character in chunk extensions name'),
++++++++ ));
++++++++
++++++++ n('chunk_extension_value')
++++++++ .match('"', n('chunk_extension_quoted_value'))
++++++++ .match(TOKEN, n('chunk_extension_value'))
++++++++ .peek(';', this.span.chunkExtensionValue.end().skipTo(
++++++++ onChunkExtensionValueCompleted(n('chunk_extensions')),
++++++++ ))
++++++++ .peek('\r', this.span.chunkExtensionValue.end().skipTo(
++++++++ onChunkExtensionValueCompleted(n('chunk_size_almost_done')),
++++++++ ))
++++++++ .peek('\n', this.span.chunkExtensionValue.end(
++++++++ onChunkExtensionValueCompleted(
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('chunk_size_almost_done'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after chunk extension value'),
++++++++ ),
++++++++ ),
++++++++ ))
++++++++ .otherwise(this.span.chunkExtensionValue.end().skipTo(
++++++++ p.error(ERROR.STRICT, 'Invalid character in chunk extensions value'),
++++++++ ));
++++++++
++++++++ n('chunk_extension_quoted_value')
++++++++ .match(QUOTED_STRING, n('chunk_extension_quoted_value'))
++++++++ .match('"', this.span.chunkExtensionValue.end(
++++++++ onChunkExtensionValueCompleted(n('chunk_extension_quoted_value_done')),
++++++++ ))
++++++++ .match('\\', n('chunk_extension_quoted_value_quoted_pair'))
++++++++ .otherwise(this.span.chunkExtensionValue.end().skipTo(
++++++++ p.error(ERROR.STRICT, 'Invalid character in chunk extensions quoted value'),
++++++++ ));
++++++++
++++++++ n('chunk_extension_quoted_value_quoted_pair')
++++++++ .match(HTAB_SP_VCHAR_OBS_TEXT, n('chunk_extension_quoted_value'))
++++++++ .otherwise(this.span.chunkExtensionValue.end().skipTo(
++++++++ p.error(ERROR.STRICT, 'Invalid quoted-pair in chunk extensions quoted value'),
++++++++ ));
++++++++
++++++++ n('chunk_extension_quoted_value_done')
++++++++ .match(';', n('chunk_extensions'))
++++++++ .match('\r', n('chunk_size_almost_done'))
++++++++ .peek(
++++++++ '\n',
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('chunk_size_almost_done'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after chunk extension value'),
++++++++ ),
++++++++ )
++++++++ .otherwise(p.error(ERROR.STRICT,
++++++++ 'Invalid character in chunk extensions quote value'));
++++++++
++++++++ n('chunk_size_almost_done')
++++++++ .match('\n', n('chunk_size_almost_done_lf'))
++++++++ .otherwise(
++++++++ this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_LF_AFTER_CR, {
++++++++ 1: n('chunk_size_almost_done_lf'),
++++++++ }).otherwise(p.error(ERROR.STRICT, 'Expected LF after chunk size')),
++++++++ );
++++++++
++++++++ const toChunk = this.isEqual('content_length', 0, {
++++++++ equal: this.setFlag(FLAGS.TRAILING, 'header_field_start'),
++++++++ notEqual: 'chunk_data',
++++++++ });
++++++++
++++++++ n('chunk_size_almost_done_lf')
++++++++ .otherwise(this.invokePausable('on_chunk_header',
++++++++ ERROR.CB_CHUNK_HEADER, toChunk));
++++++++
++++++++ n('chunk_data')
++++++++ .otherwise(span.body.start()
++++++++ .otherwise(p.consume('content_length').otherwise(
++++++++ span.body.end(n('chunk_data_almost_done')))));
++++++++
++++++++ n('chunk_data_almost_done')
++++++++ .match('\r\n', n('chunk_complete'))
++++++++ .match(
++++++++ '\n',
++++++++ checkIfAllowLFWithoutCR(
++++++++ n('chunk_complete'),
++++++++ p.error(ERROR.CR_EXPECTED, 'Missing expected CR after chunk data'),
++++++++ ),
++++++++ )
++++++++ .otherwise(
++++++++ this.testLenientFlags(LENIENT_FLAGS.OPTIONAL_CRLF_AFTER_CHUNK, {
++++++++ 1: n('chunk_complete'),
++++++++ }).otherwise(p.error(ERROR.STRICT, 'Expected LF after chunk data')),
++++++++ );
++++++++
++++++++ n('chunk_complete')
++++++++ .otherwise(this.invokePausable('on_chunk_complete',
++++++++ ERROR.CB_CHUNK_COMPLETE, 'chunk_size_start'));
++++++++
++++++++ const upgradeAfterDone = this.isEqual('upgrade', 1, {
++++++++ // Exit, the rest of the message is in a different protocol.
++++++++ equal: upgradePause,
++++++++
++++++++ // Restart
++++++++ notEqual: 'cleanup',
++++++++ });
++++++++
++++++++ n('message_done')
++++++++ .otherwise(this.invokePausable('on_message_complete',
++++++++ ERROR.CB_MESSAGE_COMPLETE, upgradeAfterDone));
++++++++
++++++++ const lenientClose = this.testLenientFlags(LENIENT_FLAGS.KEEP_ALIVE, {
++++++++ 1: n('restart'),
++++++++ }, n('closed'));
++++++++
++++++++ // Check if we'd like to keep-alive
++++++++ n('cleanup')
++++++++ .otherwise(p.invoke(callback.afterMessageComplete, {
++++++++ 1: this.update('content_length', 0, n('restart')),
++++++++ }, this.update('finish', FINISH.SAFE, lenientClose)));
++++++++
++++++++ const lenientDiscardAfterClose = this.testLenientFlags(LENIENT_FLAGS.DATA_AFTER_CLOSE, {
++++++++ 1: n('closed'),
++++++++ }, p.error(ERROR.CLOSED_CONNECTION, 'Data after `Connection: close`'));
++++++++
++++++++ n('closed')
++++++++ .match([ '\r', '\n' ], n('closed'))
++++++++ .skipTo(lenientDiscardAfterClose);
++++++++
++++++++ n('restart')
++++++++ .otherwise(
++++++++ this.update('initial_message_completed', 1, this.update('finish', FINISH.SAFE, n('start')),
++++++++ ));
++++++++ }
++++++++
++++++++ private headersCompleted(): Node {
++++++++ const p = this.llparse;
++++++++ const callback = this.callback;
++++++++ const n = (name: string): Match => this.node<Match>(name);
++++++++
++++++++ // Set `upgrade` if needed
++++++++ const beforeHeadersComplete = p.invoke(callback.beforeHeadersComplete);
++++++++
++++++++ /* Here we call the headers_complete callback. This is somewhat
++++++++ * different than other callbacks because if the user returns 1, we
++++++++ * will interpret that as saying that this message has no body. This
++++++++ * is needed for the annoying case of receiving a response to a HEAD
++++++++ * request.
++++++++ *
++++++++ * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so
++++++++ * we have to simulate it by handling a change in errno below.
++++++++ */
++++++++ const onHeadersComplete = p.invoke(callback.onHeadersComplete, {
++++++++ 0: n('headers_done'),
++++++++ 1: this.setFlag(FLAGS.SKIPBODY, 'headers_done'),
++++++++ 2: this.update('upgrade', 1,
++++++++ this.setFlag(FLAGS.SKIPBODY, 'headers_done')),
++++++++ [ERROR.PAUSED]: this.pause('Paused by on_headers_complete',
++++++++ 'headers_done'),
++++++++ }, p.error(ERROR.CB_HEADERS_COMPLETE, 'User callback error'));
++++++++
++++++++ beforeHeadersComplete.otherwise(onHeadersComplete);
++++++++
++++++++ return beforeHeadersComplete;
++++++++ }
++++++++
++++++++ private node<T extends Node>(name: string | T): T {
++++++++ if (name instanceof Node) {
++++++++ return name;
++++++++ }
++++++++
++++++++ assert(this.nodes.has(name), `Unknown node with name "${name}"`);
++++++++ return this.nodes.get(name) as unknown as T;
++++++++ }
++++++++
++++++++ private load(field: string, map: { [key: number]: Node },
++++++++ next?: string | Node): Node {
++++++++ const p = this.llparse;
++++++++
++++++++ const res = p.invoke(p.code.load(field), map);
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private store(field: string, next?: string | Node): Node {
++++++++ const p = this.llparse;
++++++++
++++++++ const res = p.invoke(p.code.store(field));
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private update(field: string, value: number, next?: string | Node): Node {
++++++++ const p = this.llparse;
++++++++
++++++++ const res = p.invoke(p.code.update(field, value));
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private resetHeaderState(next: string | Node): Node {
++++++++ return this.update('header_state', HEADER_STATE.GENERAL, next);
++++++++ }
++++++++
++++++++ private emptySpan(span: source.Span, next: string | Node): Node {
++++++++ return span.start(span.end(this.node(next)));
++++++++ }
++++++++
++++++++ private unsetFlag(flag: number, next: string | Node): Node {
++++++++ const p = this.llparse;
++++++++ return p.invoke(p.code.and('flags', ~flag), this.node(next));
++++++++ }
++++++++
++++++++ private setFlag(flag: number, next: string | Node): Node {
++++++++ const p = this.llparse;
++++++++ return p.invoke(p.code.or('flags', flag), this.node(next));
++++++++ }
++++++++
++++++++ private testFlags(flag: number, map: { [key: number]: Node },
++++++++ next?: string | Node): Node {
++++++++ const p = this.llparse;
++++++++ const res = p.invoke(p.code.test('flags', flag), map);
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private testLenientFlags(flag: number, map: { [key: number]: Node },
++++++++ next?: string | Node): Node {
++++++++ const p = this.llparse;
++++++++ const res = p.invoke(p.code.test('lenient_flags', flag), map);
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private setHeaderFlags(next: string | Node): Node {
++++++++ const HS = HEADER_STATE;
++++++++ const F = FLAGS;
++++++++
++++++++ const toConnection =
++++++++ this.update('header_state', HEADER_STATE.CONNECTION, next);
++++++++
++++++++ return this.load('header_state', {
++++++++ [HS.CONNECTION_KEEP_ALIVE]:
++++++++ this.setFlag(F.CONNECTION_KEEP_ALIVE, toConnection),
++++++++ [HS.CONNECTION_CLOSE]: this.setFlag(F.CONNECTION_CLOSE, toConnection),
++++++++ [HS.CONNECTION_UPGRADE]: this.setFlag(F.CONNECTION_UPGRADE, toConnection),
++++++++ [HS.TRANSFER_ENCODING_CHUNKED]: this.setFlag(F.CHUNKED, next),
++++++++ }, this.node(next));
++++++++ }
++++++++
++++++++ private mulAdd(field: string, targets: IMulTargets,
++++++++ options: IMulOptions = { base: 10, signed: false }): Node {
++++++++ const p = this.llparse;
++++++++
++++++++ return p.invoke(p.code.mulAdd(field, options), {
++++++++ 1: this.node(targets.overflow),
++++++++ }, this.node(targets.success));
++++++++ }
++++++++
++++++++ private isEqual(field: string, value: number, map: IIsEqualTargets) {
++++++++ const p = this.llparse;
++++++++ return p.invoke(p.code.isEqual(field, value), {
++++++++ 0: this.node(map.notEqual),
++++++++ }, this.node(map.equal));
++++++++ }
++++++++
++++++++ private pause(msg: string, next?: string | Node) {
++++++++ const p = this.llparse;
++++++++ const res = p.pause(ERROR.PAUSED, msg);
++++++++ if (next !== undefined) {
++++++++ res.otherwise(this.node(next));
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private invokePausable(name: string, errorCode: number, next: string | Node): Node {
++++++++ let cb;
++++++++
++++++++ switch (name) {
++++++++ case 'on_message_begin':
++++++++ cb = this.callback.onMessageBegin;
++++++++ break;
++++++++ case 'on_url_complete':
++++++++ cb = this.callback.onUrlComplete;
++++++++ break;
++++++++ case 'on_status_complete':
++++++++ cb = this.callback.onStatusComplete;
++++++++ break;
++++++++ case 'on_method_complete':
++++++++ cb = this.callback.onMethodComplete;
++++++++ break;
++++++++ case 'on_version_complete':
++++++++ cb = this.callback.onVersionComplete;
++++++++ break;
++++++++ case 'on_header_field_complete':
++++++++ cb = this.callback.onHeaderFieldComplete;
++++++++ break;
++++++++ case 'on_header_value_complete':
++++++++ cb = this.callback.onHeaderValueComplete;
++++++++ break;
++++++++ case 'on_message_complete':
++++++++ cb = this.callback.onMessageComplete;
++++++++ break;
++++++++ case 'on_chunk_header':
++++++++ cb = this.callback.onChunkHeader;
++++++++ break;
++++++++ case 'on_chunk_extension_name':
++++++++ cb = this.callback.onChunkExtensionName;
++++++++ break;
++++++++ case 'on_chunk_extension_value':
++++++++ cb = this.callback.onChunkExtensionValue;
++++++++ break;
++++++++ case 'on_chunk_complete':
++++++++ cb = this.callback.onChunkComplete;
++++++++ break;
++++++++ case 'on_reset':
++++++++ cb = this.callback.onReset;
++++++++ break;
++++++++ default:
++++++++ throw new Error('Unknown callback: ' + name);
++++++++ }
++++++++
++++++++ const p = this.llparse;
++++++++ return p.invoke(cb, {
++++++++ 0: this.node(next),
++++++++ [ERROR.PAUSED]: this.pause(`${name} pause`, next),
++++++++ }, p.error(errorCode, `\`${name}\` callback error`));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { LLParse, source } from 'llparse';
++++++++
++++++++import Match = source.node.Match;
++++++++import Node = source.node.Node;
++++++++
++++++++import {
++++++++ ALPHA,
++++++++ CharList,
++++++++ ERROR,
++++++++ URL_CHAR,
++++++++ USERINFO_CHARS,
++++++++} from './constants';
++++++++
++++++++type SpanName = 'schema' | 'host' | 'path' | 'query' | 'fragment' | 'url';
++++++++
++++++++export interface IURLResult {
++++++++ readonly entry: {
++++++++ readonly normal: Node;
++++++++ readonly connect: Node;
++++++++ };
++++++++ readonly exit: {
++++++++ readonly toHTTP: Node;
++++++++ readonly toHTTP09: Node;
++++++++ };
++++++++}
++++++++
++++++++type SpanTable = Map<SpanName, source.Span>;
++++++++
++++++++export class URL {
++++++++ private readonly spanTable: SpanTable = new Map();
++++++++ private readonly errorInvalid: Node;
++++++++ private readonly URL_CHAR: CharList;
++++++++
++++++++ constructor(private readonly llparse: LLParse, separateSpans: boolean = false) {
++++++++ const p = this.llparse;
++++++++
++++++++ this.errorInvalid = p.error(ERROR.INVALID_URL, 'Invalid characters in url');
++++++++
++++++++ this.URL_CHAR = URL_CHAR;
++++++++
++++++++ const table = this.spanTable;
++++++++ if (separateSpans) {
++++++++ table.set('schema', p.span(p.code.span('llhttp__on_url_schema')));
++++++++ table.set('host', p.span(p.code.span('llhttp__on_url_host')));
++++++++ table.set('path', p.span(p.code.span('llhttp__on_url_path')));
++++++++ table.set('query', p.span(p.code.span('llhttp__on_url_query')));
++++++++ table.set('fragment',
++++++++ p.span(p.code.span('llhttp__on_url_fragment')));
++++++++ } else {
++++++++ table.set('url', p.span(p.code.span('llhttp__on_url')));
++++++++ }
++++++++ }
++++++++
++++++++ public build(): IURLResult {
++++++++ const p = this.llparse;
++++++++
++++++++ const entry = {
++++++++ connect: this.node('entry_connect'),
++++++++ normal: this.node('entry_normal'),
++++++++ };
++++++++
++++++++ const start = this.node('start');
++++++++ const path = this.node('path');
++++++++ const queryOrFragment = this.node('query_or_fragment');
++++++++ const schema = this.node('schema');
++++++++ const schemaDelim = this.node('schema_delim');
++++++++ const server = this.node('server');
++++++++ const queryStart = this.node('query_start');
++++++++ const query = this.node('query');
++++++++ const fragment = this.node('fragment');
++++++++ const serverWithAt = this.node('server_with_at');
++++++++
++++++++ entry.normal
++++++++ .otherwise(this.spanStart('url', start));
++++++++
++++++++ entry.connect
++++++++ .otherwise(this.spanStart('url', this.spanStart('host', server)));
++++++++
++++++++ start
++++++++ .peek([ '/', '*' ], this.spanStart('path').skipTo(path))
++++++++ .peek(ALPHA, this.spanStart('schema', schema))
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Unexpected start char in url'));
++++++++
++++++++ schema
++++++++ .match(ALPHA, schema)
++++++++ .peek(':', this.spanEnd('schema').skipTo(schemaDelim))
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Unexpected char in url schema'));
++++++++
++++++++ schemaDelim
++++++++ .match('//', this.spanStart('host', server))
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Unexpected char in url schema'));
++++++++
++++++++ for (const node of [ server, serverWithAt ]) {
++++++++ node
++++++++ .peek('/', this.spanEnd('host', this.spanStart('path').skipTo(path)))
++++++++ .match('?', this.spanEnd('host', this.spanStart('query', query)))
++++++++ .match(USERINFO_CHARS, server)
++++++++ .match([ '[', ']' ], server)
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Unexpected char in url server'));
++++++++
++++++++ if (node !== serverWithAt) {
++++++++ node.match('@', serverWithAt);
++++++++ }
++++++++ }
++++++++
++++++++ serverWithAt
++++++++ .match('@', p.error(ERROR.INVALID_URL, 'Double @ in url'));
++++++++
++++++++ path
++++++++ .match(this.URL_CHAR, path)
++++++++ .otherwise(this.spanEnd('path', queryOrFragment));
++++++++
++++++++ // Performance optimization, split `path` so that the fast case remains
++++++++ // there
++++++++ queryOrFragment
++++++++ .match('?', this.spanStart('query', query))
++++++++ .match('#', this.spanStart('fragment', fragment))
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Invalid char in url path'));
++++++++
++++++++ query
++++++++ .match(this.URL_CHAR, query)
++++++++ // Allow extra '?' in query string
++++++++ .match('?', query)
++++++++ .peek('#', this.spanEnd('query')
++++++++ .skipTo(this.spanStart('fragment', fragment)))
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Invalid char in url query'));
++++++++
++++++++ fragment
++++++++ .match(this.URL_CHAR, fragment)
++++++++ .match([ '?', '#' ], fragment)
++++++++ .otherwise(
++++++++ p.error(ERROR.INVALID_URL, 'Invalid char in url fragment start'));
++++++++
++++++++ for (const node of [ start, schema, schemaDelim ]) {
++++++++ /* No whitespace allowed here */
++++++++ node.match([ ' ', '\r', '\n' ], this.errorInvalid);
++++++++ }
++++++++
++++++++ // Adaptors
++++++++ const toHTTP = this.node('to_http');
++++++++ const toHTTP09 = this.node('to_http_09');
++++++++
++++++++ const skipToHTTP = this.node('skip_to_http')
++++++++ .skipTo(toHTTP);
++++++++
++++++++ const skipToHTTP09 = this.node('skip_to_http09')
++++++++ .skipTo(toHTTP09);
++++++++
++++++++ const skipCRLF = this.node('skip_lf_to_http09')
++++++++ .match('\r\n', toHTTP09)
++++++++ .otherwise(p.error(ERROR.INVALID_URL, 'Expected CRLF'));
++++++++
++++++++ for (const node of [ server, serverWithAt, queryOrFragment, queryStart, query, fragment ]) {
++++++++ let spanName: SpanName | undefined;
++++++++
++++++++ if (node === server || node === serverWithAt) {
++++++++ spanName = 'host';
++++++++ } else if (node === queryStart || node === query) {
++++++++ spanName = 'query';
++++++++ } else if (node === fragment) {
++++++++ spanName = 'fragment';
++++++++ }
++++++++
++++++++ const endTo = (target: Node): Node => {
++++++++ let res: Node = this.spanEnd('url', target);
++++++++ if (spanName !== undefined) {
++++++++ res = this.spanEnd(spanName, res);
++++++++ }
++++++++ return res;
++++++++ };
++++++++
++++++++ node.peek(' ', endTo(skipToHTTP));
++++++++
++++++++ node.peek('\r', endTo(skipCRLF));
++++++++ node.peek('\n', endTo(skipToHTTP09));
++++++++ }
++++++++
++++++++ return {
++++++++ entry,
++++++++ exit: {
++++++++ toHTTP,
++++++++ toHTTP09,
++++++++ },
++++++++ };
++++++++ }
++++++++
++++++++ private spanStart(name: SpanName, otherwise?: Node): Node {
++++++++ let res: Node;
++++++++ if (this.spanTable.has(name)) {
++++++++ res = this.spanTable.get(name)!.start();
++++++++ } else {
++++++++ res = this.llparse.node('span_start_stub_' + name);
++++++++ }
++++++++ if (otherwise !== undefined) {
++++++++ res.otherwise(otherwise);
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private spanEnd(name: SpanName, otherwise?: Node): Node {
++++++++ let res: Node;
++++++++ if (this.spanTable.has(name)) {
++++++++ res = this.spanTable.get(name)!.end();
++++++++ } else {
++++++++ res = this.llparse.node('span_end_stub_' + name);
++++++++ }
++++++++ if (otherwise !== undefined) {
++++++++ res.otherwise(otherwise);
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ private node(name: string): Match {
++++++++ const res = this.llparse.node('url_' + name);
++++++++
++++++++ res.match([ '\t', '\f' ], this.errorInvalid);
++++++++
++++++++ return res;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { IntDict } from './constants';
++++++++
++++++++export function enumToMap(
++++++++ obj: IntDict,
++++++++ filter: ReadonlyArray<number> = [],
++++++++ exceptions: ReadonlyArray<number> = [],
++++++++): IntDict {
++++++++ const emptyFilter = (filter?.length ?? 0) === 0;
++++++++ const emptyExceptions = (exceptions?.length ?? 0) === 0;
++++++++
++++++++ return Object.fromEntries(Object.entries(obj).filter(([ , value ]) => {
++++++++ return (
++++++++ typeof value === 'number' &&
++++++++ (emptyFilter || filter.includes(value)) &&
++++++++ (emptyExceptions || !exceptions.includes(value))
++++++++ );
++++++++ }));
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include <stdlib.h>
++++++++#include <stdio.h>
++++++++#include <string.h>
++++++++
++++++++#include "llhttp.h"
++++++++
++++++++#define CALLBACK_MAYBE(PARSER, NAME) \
++++++++ do { \
++++++++ const llhttp_settings_t* settings; \
++++++++ settings = (const llhttp_settings_t*) (PARSER)->settings; \
++++++++ if (settings == NULL || settings->NAME == NULL) { \
++++++++ err = 0; \
++++++++ break; \
++++++++ } \
++++++++ err = settings->NAME((PARSER)); \
++++++++ } while (0)
++++++++
++++++++#define SPAN_CALLBACK_MAYBE(PARSER, NAME, START, LEN) \
++++++++ do { \
++++++++ const llhttp_settings_t* settings; \
++++++++ settings = (const llhttp_settings_t*) (PARSER)->settings; \
++++++++ if (settings == NULL || settings->NAME == NULL) { \
++++++++ err = 0; \
++++++++ break; \
++++++++ } \
++++++++ err = settings->NAME((PARSER), (START), (LEN)); \
++++++++ if (err == -1) { \
++++++++ err = HPE_USER; \
++++++++ llhttp_set_error_reason((PARSER), "Span callback error in " #NAME); \
++++++++ } \
++++++++ } while (0)
++++++++
++++++++void llhttp_init(llhttp_t* parser, llhttp_type_t type,
++++++++ const llhttp_settings_t* settings) {
++++++++ llhttp__internal_init(parser);
++++++++
++++++++ parser->type = type;
++++++++ parser->settings = (void*) settings;
++++++++}
++++++++
++++++++
++++++++#if defined(__wasm__)
++++++++
++++++++extern int wasm_on_message_begin(llhttp_t * p);
++++++++extern int wasm_on_url(llhttp_t* p, const char* at, size_t length);
++++++++extern int wasm_on_status(llhttp_t* p, const char* at, size_t length);
++++++++extern int wasm_on_header_field(llhttp_t* p, const char* at, size_t length);
++++++++extern int wasm_on_header_value(llhttp_t* p, const char* at, size_t length);
++++++++extern int wasm_on_headers_complete(llhttp_t * p, int status_code,
++++++++ uint8_t upgrade, int should_keep_alive);
++++++++extern int wasm_on_body(llhttp_t* p, const char* at, size_t length);
++++++++extern int wasm_on_message_complete(llhttp_t * p);
++++++++
++++++++static int wasm_on_headers_complete_wrap(llhttp_t* p) {
++++++++ return wasm_on_headers_complete(p, p->status_code, p->upgrade,
++++++++ llhttp_should_keep_alive(p));
++++++++}
++++++++
++++++++const llhttp_settings_t wasm_settings = {
++++++++ wasm_on_message_begin,
++++++++ wasm_on_url,
++++++++ wasm_on_status,
++++++++ NULL,
++++++++ NULL,
++++++++ wasm_on_header_field,
++++++++ wasm_on_header_value,
++++++++ NULL,
++++++++ NULL,
++++++++ wasm_on_headers_complete_wrap,
++++++++ wasm_on_body,
++++++++ wasm_on_message_complete,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++ NULL,
++++++++};
++++++++
++++++++
++++++++llhttp_t* llhttp_alloc(llhttp_type_t type) {
++++++++ llhttp_t* parser = malloc(sizeof(llhttp_t));
++++++++ llhttp_init(parser, type, &wasm_settings);
++++++++ return parser;
++++++++}
++++++++
++++++++void llhttp_free(llhttp_t* parser) {
++++++++ free(parser);
++++++++}
++++++++
++++++++#endif // defined(__wasm__)
++++++++
++++++++/* Some getters required to get stuff from the parser */
++++++++
++++++++uint8_t llhttp_get_type(llhttp_t* parser) {
++++++++ return parser->type;
++++++++}
++++++++
++++++++uint8_t llhttp_get_http_major(llhttp_t* parser) {
++++++++ return parser->http_major;
++++++++}
++++++++
++++++++uint8_t llhttp_get_http_minor(llhttp_t* parser) {
++++++++ return parser->http_minor;
++++++++}
++++++++
++++++++uint8_t llhttp_get_method(llhttp_t* parser) {
++++++++ return parser->method;
++++++++}
++++++++
++++++++int llhttp_get_status_code(llhttp_t* parser) {
++++++++ return parser->status_code;
++++++++}
++++++++
++++++++uint8_t llhttp_get_upgrade(llhttp_t* parser) {
++++++++ return parser->upgrade;
++++++++}
++++++++
++++++++
++++++++void llhttp_reset(llhttp_t* parser) {
++++++++ llhttp_type_t type = parser->type;
++++++++ const llhttp_settings_t* settings = parser->settings;
++++++++ void* data = parser->data;
++++++++ uint16_t lenient_flags = parser->lenient_flags;
++++++++
++++++++ llhttp__internal_init(parser);
++++++++
++++++++ parser->type = type;
++++++++ parser->settings = (void*) settings;
++++++++ parser->data = data;
++++++++ parser->lenient_flags = lenient_flags;
++++++++}
++++++++
++++++++
++++++++llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) {
++++++++ return llhttp__internal_execute(parser, data, data + len);
++++++++}
++++++++
++++++++
++++++++void llhttp_settings_init(llhttp_settings_t* settings) {
++++++++ memset(settings, 0, sizeof(*settings));
++++++++}
++++++++
++++++++
++++++++llhttp_errno_t llhttp_finish(llhttp_t* parser) {
++++++++ int err;
++++++++
++++++++ /* We're in an error state. Don't bother doing anything. */
++++++++ if (parser->error != 0) {
++++++++ return 0;
++++++++ }
++++++++
++++++++ switch (parser->finish) {
++++++++ case HTTP_FINISH_SAFE_WITH_CB:
++++++++ CALLBACK_MAYBE(parser, on_message_complete);
++++++++ if (err != HPE_OK) return err;
++++++++
++++++++ /* FALLTHROUGH */
++++++++ case HTTP_FINISH_SAFE:
++++++++ return HPE_OK;
++++++++ case HTTP_FINISH_UNSAFE:
++++++++ parser->reason = "Invalid EOF state";
++++++++ return HPE_INVALID_EOF_STATE;
++++++++ default:
++++++++ abort();
++++++++ }
++++++++}
++++++++
++++++++
++++++++void llhttp_pause(llhttp_t* parser) {
++++++++ if (parser->error != HPE_OK) {
++++++++ return;
++++++++ }
++++++++
++++++++ parser->error = HPE_PAUSED;
++++++++ parser->reason = "Paused";
++++++++}
++++++++
++++++++
++++++++void llhttp_resume(llhttp_t* parser) {
++++++++ if (parser->error != HPE_PAUSED) {
++++++++ return;
++++++++ }
++++++++
++++++++ parser->error = 0;
++++++++}
++++++++
++++++++
++++++++void llhttp_resume_after_upgrade(llhttp_t* parser) {
++++++++ if (parser->error != HPE_PAUSED_UPGRADE) {
++++++++ return;
++++++++ }
++++++++
++++++++ parser->error = 0;
++++++++}
++++++++
++++++++
++++++++llhttp_errno_t llhttp_get_errno(const llhttp_t* parser) {
++++++++ return parser->error;
++++++++}
++++++++
++++++++
++++++++const char* llhttp_get_error_reason(const llhttp_t* parser) {
++++++++ return parser->reason;
++++++++}
++++++++
++++++++
++++++++void llhttp_set_error_reason(llhttp_t* parser, const char* reason) {
++++++++ parser->reason = reason;
++++++++}
++++++++
++++++++
++++++++const char* llhttp_get_error_pos(const llhttp_t* parser) {
++++++++ return parser->error_pos;
++++++++}
++++++++
++++++++
++++++++const char* llhttp_errno_name(llhttp_errno_t err) {
++++++++#define HTTP_ERRNO_GEN(CODE, NAME, _) case HPE_##NAME: return "HPE_" #NAME;
++++++++ switch (err) {
++++++++ HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
++++++++ default: abort();
++++++++ }
++++++++#undef HTTP_ERRNO_GEN
++++++++}
++++++++
++++++++
++++++++const char* llhttp_method_name(llhttp_method_t method) {
++++++++#define HTTP_METHOD_GEN(NUM, NAME, STRING) case HTTP_##NAME: return #STRING;
++++++++ switch (method) {
++++++++ HTTP_ALL_METHOD_MAP(HTTP_METHOD_GEN)
++++++++ default: abort();
++++++++ }
++++++++#undef HTTP_METHOD_GEN
++++++++}
++++++++
++++++++const char* llhttp_status_name(llhttp_status_t status) {
++++++++#define HTTP_STATUS_GEN(NUM, NAME, STRING) case HTTP_STATUS_##NAME: return #STRING;
++++++++ switch (status) {
++++++++ HTTP_STATUS_MAP(HTTP_STATUS_GEN)
++++++++ default: abort();
++++++++ }
++++++++#undef HTTP_STATUS_GEN
++++++++}
++++++++
++++++++
++++++++void llhttp_set_lenient_headers(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_HEADERS;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_HEADERS;
++++++++ }
++++++++}
++++++++
++++++++
++++++++void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_CHUNKED_LENGTH;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_CHUNKED_LENGTH;
++++++++ }
++++++++}
++++++++
++++++++
++++++++void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_KEEP_ALIVE;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_KEEP_ALIVE;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_TRANSFER_ENCODING;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_TRANSFER_ENCODING;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_version(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_VERSION;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_VERSION;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_DATA_AFTER_CLOSE;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_DATA_AFTER_CLOSE;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_OPTIONAL_LF_AFTER_CR;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_OPTIONAL_LF_AFTER_CR;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_OPTIONAL_CR_BEFORE_LF;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_OPTIONAL_CR_BEFORE_LF;
++++++++ }
++++++++}
++++++++
++++++++void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled) {
++++++++ if (enabled) {
++++++++ parser->lenient_flags |= LENIENT_SPACES_AFTER_CHUNK_SIZE;
++++++++ } else {
++++++++ parser->lenient_flags &= ~LENIENT_SPACES_AFTER_CHUNK_SIZE;
++++++++ }
++++++++}
++++++++
++++++++/* Callbacks */
++++++++
++++++++
++++++++int llhttp__on_message_begin(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_message_begin);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_url, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_url_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_status(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_status, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_status_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_status_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_method(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_method, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_method_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_method_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_version(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_version, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_version_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_version_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_field(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_header_field, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_field_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_header_field_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_value(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_header_value, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_value_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_header_value_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_headers_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_headers_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_message_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_message_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_body(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_body, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_header(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_chunk_header);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_name(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_chunk_extension_name, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_name_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_chunk_extension_name_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_value(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ SPAN_CALLBACK_MAYBE(s, on_chunk_extension_value, p, endp - p);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_value_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_chunk_extension_value_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_complete(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_chunk_complete);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++int llhttp__on_reset(llhttp_t* s, const char* p, const char* endp) {
++++++++ int err;
++++++++ CALLBACK_MAYBE(s, on_reset);
++++++++ return err;
++++++++}
++++++++
++++++++
++++++++/* Private */
++++++++
++++++++
++++++++void llhttp__debug(llhttp_t* s, const char* p, const char* endp,
++++++++ const char* msg) {
++++++++ if (p == endp) {
++++++++ fprintf(stderr, "p=%p type=%d flags=%02x next=null debug=%s\n", s, s->type,
++++++++ s->flags, msg);
++++++++ } else {
++++++++ fprintf(stderr, "p=%p type=%d flags=%02x next=%02x debug=%s\n", s,
++++++++ s->type, s->flags, *p, msg);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#ifndef INCLUDE_LLHTTP_API_H_
++++++++#define INCLUDE_LLHTTP_API_H_
++++++++#ifdef __cplusplus
++++++++extern "C" {
++++++++#endif
++++++++#include <stddef.h>
++++++++
++++++++#if defined(__wasm__)
++++++++#define LLHTTP_EXPORT __attribute__((visibility("default")))
++++++++#elif defined(_WIN32)
++++++++#define LLHTTP_EXPORT __declspec(dllexport)
++++++++#else
++++++++#define LLHTTP_EXPORT
++++++++#endif
++++++++
++++++++typedef llhttp__internal_t llhttp_t;
++++++++typedef struct llhttp_settings_s llhttp_settings_t;
++++++++
++++++++typedef int (*llhttp_data_cb)(llhttp_t*, const char *at, size_t length);
++++++++typedef int (*llhttp_cb)(llhttp_t*);
++++++++
++++++++struct llhttp_settings_s {
++++++++ /* Possible return values 0, -1, `HPE_PAUSED` */
++++++++ llhttp_cb on_message_begin;
++++++++
++++++++ /* Possible return values 0, -1, HPE_USER */
++++++++ llhttp_data_cb on_url;
++++++++ llhttp_data_cb on_status;
++++++++ llhttp_data_cb on_method;
++++++++ llhttp_data_cb on_version;
++++++++ llhttp_data_cb on_header_field;
++++++++ llhttp_data_cb on_header_value;
++++++++ llhttp_data_cb on_chunk_extension_name;
++++++++ llhttp_data_cb on_chunk_extension_value;
++++++++
++++++++ /* Possible return values:
++++++++ * 0 - Proceed normally
++++++++ * 1 - Assume that request/response has no body, and proceed to parsing the
++++++++ * next message
++++++++ * 2 - Assume absence of body (as above) and make `llhttp_execute()` return
++++++++ * `HPE_PAUSED_UPGRADE`
++++++++ * -1 - Error
++++++++ * `HPE_PAUSED`
++++++++ */
++++++++ llhttp_cb on_headers_complete;
++++++++
++++++++ /* Possible return values 0, -1, HPE_USER */
++++++++ llhttp_data_cb on_body;
++++++++
++++++++ /* Possible return values 0, -1, `HPE_PAUSED` */
++++++++ llhttp_cb on_message_complete;
++++++++ llhttp_cb on_url_complete;
++++++++ llhttp_cb on_status_complete;
++++++++ llhttp_cb on_method_complete;
++++++++ llhttp_cb on_version_complete;
++++++++ llhttp_cb on_header_field_complete;
++++++++ llhttp_cb on_header_value_complete;
++++++++ llhttp_cb on_chunk_extension_name_complete;
++++++++ llhttp_cb on_chunk_extension_value_complete;
++++++++
++++++++ /* When on_chunk_header is called, the current chunk length is stored
++++++++ * in parser->content_length.
++++++++ * Possible return values 0, -1, `HPE_PAUSED`
++++++++ */
++++++++ llhttp_cb on_chunk_header;
++++++++ llhttp_cb on_chunk_complete;
++++++++ llhttp_cb on_reset;
++++++++};
++++++++
++++++++/* Initialize the parser with specific type and user settings.
++++++++ *
++++++++ * NOTE: lifetime of `settings` has to be at least the same as the lifetime of
++++++++ * the `parser` here. In practice, `settings` has to be either a static
++++++++ * variable or be allocated with `malloc`, `new`, etc.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_init(llhttp_t* parser, llhttp_type_t type,
++++++++ const llhttp_settings_t* settings);
++++++++
++++++++LLHTTP_EXPORT
++++++++llhttp_t* llhttp_alloc(llhttp_type_t type);
++++++++
++++++++LLHTTP_EXPORT
++++++++void llhttp_free(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++uint8_t llhttp_get_type(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++uint8_t llhttp_get_http_major(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++uint8_t llhttp_get_http_minor(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++uint8_t llhttp_get_method(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++int llhttp_get_status_code(llhttp_t* parser);
++++++++
++++++++LLHTTP_EXPORT
++++++++uint8_t llhttp_get_upgrade(llhttp_t* parser);
++++++++
++++++++/* Reset an already initialized parser back to the start state, preserving the
++++++++ * existing parser type, callback settings, user data, and lenient flags.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_reset(llhttp_t* parser);
++++++++
++++++++/* Initialize the settings object */
++++++++LLHTTP_EXPORT
++++++++void llhttp_settings_init(llhttp_settings_t* settings);
++++++++
++++++++/* Parse full or partial request/response, invoking user callbacks along the
++++++++ * way.
++++++++ *
++++++++ * If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing
++++++++ * interrupts, and such errno is returned from `llhttp_execute()`. If
++++++++ * `HPE_PAUSED` was used as a errno, the execution can be resumed with
++++++++ * `llhttp_resume()` call.
++++++++ *
++++++++ * In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE`
++++++++ * is returned after fully parsing the request/response. If the user wishes to
++++++++ * continue parsing, they need to invoke `llhttp_resume_after_upgrade()`.
++++++++ *
++++++++ * NOTE: if this function ever returns a non-pause type error, it will continue
++++++++ * to return the same error upon each successive call up until `llhttp_init()`
++++++++ * is called.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
++++++++
++++++++/* This method should be called when the other side has no further bytes to
++++++++ * send (e.g. shutdown of readable side of the TCP connection.)
++++++++ *
++++++++ * Requests without `Content-Length` and other messages might require treating
++++++++ * all incoming bytes as the part of the body, up to the last byte of the
++++++++ * connection. This method will invoke `on_message_complete()` callback if the
++++++++ * request was terminated safely. Otherwise a error code would be returned.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++llhttp_errno_t llhttp_finish(llhttp_t* parser);
++++++++
++++++++/* Returns `1` if the incoming message is parsed until the last byte, and has
++++++++ * to be completed by calling `llhttp_finish()` on EOF
++++++++ */
++++++++LLHTTP_EXPORT
++++++++int llhttp_message_needs_eof(const llhttp_t* parser);
++++++++
++++++++/* Returns `1` if there might be any other messages following the last that was
++++++++ * successfully parsed.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++int llhttp_should_keep_alive(const llhttp_t* parser);
++++++++
++++++++/* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
++++++++ * appropriate error reason.
++++++++ *
++++++++ * Important: do not call this from user callbacks! User callbacks must return
++++++++ * `HPE_PAUSED` if pausing is required.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_pause(llhttp_t* parser);
++++++++
++++++++/* Might be called to resume the execution after the pause in user's callback.
++++++++ * See `llhttp_execute()` above for details.
++++++++ *
++++++++ * Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_resume(llhttp_t* parser);
++++++++
++++++++/* Might be called to resume the execution after the pause in user's callback.
++++++++ * See `llhttp_execute()` above for details.
++++++++ *
++++++++ * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_resume_after_upgrade(llhttp_t* parser);
++++++++
++++++++/* Returns the latest return error */
++++++++LLHTTP_EXPORT
++++++++llhttp_errno_t llhttp_get_errno(const llhttp_t* parser);
++++++++
++++++++/* Returns the verbal explanation of the latest returned error.
++++++++ *
++++++++ * Note: User callback should set error reason when returning the error. See
++++++++ * `llhttp_set_error_reason()` for details.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++const char* llhttp_get_error_reason(const llhttp_t* parser);
++++++++
++++++++/* Assign verbal description to the returned error. Must be called in user
++++++++ * callbacks right before returning the errno.
++++++++ *
++++++++ * Note: `HPE_USER` error code might be useful in user callbacks.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_error_reason(llhttp_t* parser, const char* reason);
++++++++
++++++++/* Returns the pointer to the last parsed byte before the returned error. The
++++++++ * pointer is relative to the `data` argument of `llhttp_execute()`.
++++++++ *
++++++++ * Note: this method might be useful for counting the number of parsed bytes.
++++++++ */
++++++++LLHTTP_EXPORT
++++++++const char* llhttp_get_error_pos(const llhttp_t* parser);
++++++++
++++++++/* Returns textual name of error code */
++++++++LLHTTP_EXPORT
++++++++const char* llhttp_errno_name(llhttp_errno_t err);
++++++++
++++++++/* Returns textual name of HTTP method */
++++++++LLHTTP_EXPORT
++++++++const char* llhttp_method_name(llhttp_method_t method);
++++++++
++++++++/* Returns textual name of HTTP status */
++++++++LLHTTP_EXPORT
++++++++const char* llhttp_status_name(llhttp_status_t status);
++++++++
++++++++/* Enables/disables lenient header value parsing (disabled by default).
++++++++ *
++++++++ * Lenient parsing disables header value token checks, extending llhttp's
++++++++ * protocol support to highly non-compliant clients/server. No
++++++++ * `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
++++++++ * lenient parsing is "on".
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
++++++++
++++++++
++++++++/* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
++++++++ * `Content-Length` headers (disabled by default).
++++++++ *
++++++++ * Normally `llhttp` would error when `Transfer-Encoding` is present in
++++++++ * conjunction with `Content-Length`. This error is important to prevent HTTP
++++++++ * request smuggling, but may be less desirable for small number of cases
++++++++ * involving legacy servers.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
++++++++
++++++++
++++++++/* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
++++++++ * requests responses.
++++++++ *
++++++++ * Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
++++++++ * the HTTP request/response after the request/response with `Connection: close`
++++++++ * and `Content-Length`. This is important to prevent cache poisoning attacks,
++++++++ * but might interact badly with outdated and insecure clients. With this flag
++++++++ * the extra request/response will be parsed normally.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * poisoning attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of `Transfer-Encoding` header.
++++++++ *
++++++++ * Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
++++++++ * and another value after it (either in a single header or in multiple
++++++++ * headers whose value are internally joined using `, `).
++++++++ * This is mandated by the spec to reliably determine request body size and thus
++++++++ * avoid request smuggling.
++++++++ * With this flag the extra value will be parsed normally.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of HTTP version.
++++++++ *
++++++++ * Normally `llhttp` would error when the HTTP version in the request or status line
++++++++ * is not `0.9`, `1.0`, `1.1` or `2.0`.
++++++++ * With this flag the invalid value will be parsed normally.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will allow unsupported
++++++++ * HTTP versions. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_version(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of additional data received after a message ends
++++++++ * and keep-alive is disabled.
++++++++ *
++++++++ * Normally `llhttp` would error when additional unexpected data is received if the message
++++++++ * contains the `Connection` header with `close` value.
++++++++ * With this flag the extra data will discarded without throwing an error.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * poisoning attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of incomplete CRLF sequences.
++++++++ *
++++++++ * Normally `llhttp` would error when a CR is not followed by LF when terminating the
++++++++ * request line, the status line, the headers or a chunk header.
++++++++ * With this flag only a CR is required to terminate such sections.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled);
++++++++
++++++++/*
++++++++ * Enables/disables lenient handling of line separators.
++++++++ *
++++++++ * Normally `llhttp` would error when a LF is not preceded by CR when terminating the
++++++++ * request line, the status line, the headers, a chunk header or a chunk data.
++++++++ * With this flag only a LF is required to terminate such sections.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_optional_cr_before_lf(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of chunks not separated via CRLF.
++++++++ *
++++++++ * Normally `llhttp` would error when after a chunk data a CRLF is missing before
++++++++ * starting a new chunk.
++++++++ * With this flag the new chunk can start immediately after the previous one.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled);
++++++++
++++++++/* Enables/disables lenient handling of spaces after chunk size.
++++++++ *
++++++++ * Normally `llhttp` would error when after a chunk size is followed by one or more
++++++++ * spaces are present instead of a CRLF or `;`.
++++++++ * With this flag this check is disabled.
++++++++ *
++++++++ * **Enabling this flag can pose a security issue since you will be exposed to
++++++++ * request smuggling attacks. USE WITH CAUTION!**
++++++++ */
++++++++LLHTTP_EXPORT
++++++++void llhttp_set_lenient_spaces_after_chunk_size(llhttp_t* parser, int enabled);
++++++++
++++++++#ifdef __cplusplus
++++++++} /* extern "C" */
++++++++#endif
++++++++#endif /* INCLUDE_LLHTTP_API_H_ */
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include <stdio.h>
++++++++#ifndef LLHTTP__TEST
++++++++# include "llhttp.h"
++++++++#else
++++++++# define llhttp_t llparse_t
++++++++#endif /* */
++++++++
++++++++int llhttp_message_needs_eof(const llhttp_t* parser);
++++++++int llhttp_should_keep_alive(const llhttp_t* parser);
++++++++
++++++++int llhttp__before_headers_complete(llhttp_t* parser, const char* p,
++++++++ const char* endp) {
++++++++ /* Set this here so that on_headers_complete() callbacks can see it */
++++++++ if ((parser->flags & F_UPGRADE) &&
++++++++ (parser->flags & F_CONNECTION_UPGRADE)) {
++++++++ /* For responses, "Upgrade: foo" and "Connection: upgrade" are
++++++++ * mandatory only when it is a 101 Switching Protocols response,
++++++++ * otherwise it is purely informational, to announce support.
++++++++ */
++++++++ parser->upgrade =
++++++++ (parser->type == HTTP_REQUEST || parser->status_code == 101);
++++++++ } else {
++++++++ parser->upgrade = (parser->method == HTTP_CONNECT);
++++++++ }
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++/* Return values:
++++++++ * 0 - No body, `restart`, message_complete
++++++++ * 1 - CONNECT request, `restart`, message_complete, and pause
++++++++ * 2 - chunk_size_start
++++++++ * 3 - body_identity
++++++++ * 4 - body_identity_eof
++++++++ * 5 - invalid transfer-encoding for request
++++++++ */
++++++++int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
++++++++ const char* endp) {
++++++++ int hasBody;
++++++++
++++++++ hasBody = parser->flags & F_CHUNKED || parser->content_length > 0;
++++++++ if (
++++++++ (parser->upgrade && (parser->method == HTTP_CONNECT ||
++++++++ (parser->flags & F_SKIPBODY) || !hasBody)) ||
++++++++ /* See RFC 2616 section 4.4 - 1xx e.g. Continue */
++++++++ (parser->type == HTTP_RESPONSE && parser->status_code == 101)
++++++++ ) {
++++++++ /* Exit, the rest of the message is in a different protocol. */
++++++++ return 1;
++++++++ }
++++++++
++++++++ if (parser->type == HTTP_RESPONSE && parser->status_code == 100) {
++++++++ /* No body, restart as the message is complete */
++++++++ return 0;
++++++++ }
++++++++
++++++++ /* See RFC 2616 section 4.4 */
++++++++ if (
++++++++ parser->flags & F_SKIPBODY || /* response to a HEAD request */
++++++++ (
++++++++ parser->type == HTTP_RESPONSE && (
++++++++ parser->status_code == 102 || /* Processing */
++++++++ parser->status_code == 103 || /* Early Hints */
++++++++ parser->status_code == 204 || /* No Content */
++++++++ parser->status_code == 304 /* Not Modified */
++++++++ )
++++++++ )
++++++++ ) {
++++++++ return 0;
++++++++ } else if (parser->flags & F_CHUNKED) {
++++++++ /* chunked encoding - ignore Content-Length header, prepare for a chunk */
++++++++ return 2;
++++++++ } else if (parser->flags & F_TRANSFER_ENCODING) {
++++++++ if (parser->type == HTTP_REQUEST &&
++++++++ (parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 &&
++++++++ (parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) {
++++++++ /* RFC 7230 3.3.3 */
++++++++
++++++++ /* If a Transfer-Encoding header field
++++++++ * is present in a request and the chunked transfer coding is not
++++++++ * the final encoding, the message body length cannot be determined
++++++++ * reliably; the server MUST respond with the 400 (Bad Request)
++++++++ * status code and then close the connection.
++++++++ */
++++++++ return 5;
++++++++ } else {
++++++++ /* RFC 7230 3.3.3 */
++++++++
++++++++ /* If a Transfer-Encoding header field is present in a response and
++++++++ * the chunked transfer coding is not the final encoding, the
++++++++ * message body length is determined by reading the connection until
++++++++ * it is closed by the server.
++++++++ */
++++++++ return 4;
++++++++ }
++++++++ } else {
++++++++ if (!(parser->flags & F_CONTENT_LENGTH)) {
++++++++ if (!llhttp_message_needs_eof(parser)) {
++++++++ /* Assume content-length 0 - read the next */
++++++++ return 0;
++++++++ } else {
++++++++ /* Read body until EOF */
++++++++ return 4;
++++++++ }
++++++++ } else if (parser->content_length == 0) {
++++++++ /* Content-Length header given but zero: Content-Length: 0\r\n */
++++++++ return 0;
++++++++ } else {
++++++++ /* Content-Length header given and non-zero */
++++++++ return 3;
++++++++ }
++++++++ }
++++++++}
++++++++
++++++++
++++++++int llhttp__after_message_complete(llhttp_t* parser, const char* p,
++++++++ const char* endp) {
++++++++ int should_keep_alive;
++++++++
++++++++ should_keep_alive = llhttp_should_keep_alive(parser);
++++++++ parser->finish = HTTP_FINISH_SAFE;
++++++++ parser->flags = 0;
++++++++
++++++++ /* NOTE: this is ignored in loose parsing mode */
++++++++ return should_keep_alive;
++++++++}
++++++++
++++++++
++++++++int llhttp_message_needs_eof(const llhttp_t* parser) {
++++++++ if (parser->type == HTTP_REQUEST) {
++++++++ return 0;
++++++++ }
++++++++
++++++++ /* See RFC 2616 section 4.4 */
++++++++ if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
++++++++ parser->status_code == 204 || /* No Content */
++++++++ parser->status_code == 304 || /* Not Modified */
++++++++ (parser->flags & F_SKIPBODY)) { /* response to a HEAD request */
++++++++ return 0;
++++++++ }
++++++++
++++++++ /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */
++++++++ if ((parser->flags & F_TRANSFER_ENCODING) &&
++++++++ (parser->flags & F_CHUNKED) == 0) {
++++++++ return 1;
++++++++ }
++++++++
++++++++ if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
++++++++ return 0;
++++++++ }
++++++++
++++++++ return 1;
++++++++}
++++++++
++++++++
++++++++int llhttp_should_keep_alive(const llhttp_t* parser) {
++++++++ if (parser->http_major > 0 && parser->http_minor > 0) {
++++++++ /* HTTP/1.1 */
++++++++ if (parser->flags & F_CONNECTION_CLOSE) {
++++++++ return 0;
++++++++ }
++++++++ } else {
++++++++ /* HTTP/1.0 or earlier */
++++++++ if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
++++++++ return 0;
++++++++ }
++++++++ }
++++++++
++++++++ return !llhttp_message_needs_eof(parser);
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include <stdlib.h>
++++++++
++++++++#include "fixture.h"
++++++++
++++++++int llhttp__on_url(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "url complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_URL_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_schema(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url.schema", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_host(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url.host", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_path(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url.path", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_query(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url.query", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_url_fragment(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("url.fragment", p, endp);
++++++++}
++++++++
++++++++
++++++++#ifdef LLHTTP__TEST_HTTP
++++++++
++++++++void llhttp__test_init_request(llparse_t* s) {
++++++++ s->type = HTTP_REQUEST;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_response(llparse_t* s) {
++++++++ s->type = HTTP_RESPONSE;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_request_lenient_all(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |=
++++++++ LENIENT_HEADERS | LENIENT_CHUNKED_LENGTH | LENIENT_KEEP_ALIVE |
++++++++ LENIENT_TRANSFER_ENCODING | LENIENT_VERSION | LENIENT_DATA_AFTER_CLOSE |
++++++++ LENIENT_OPTIONAL_LF_AFTER_CR | LENIENT_OPTIONAL_CR_BEFORE_LF |
++++++++ LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_response_lenient_all(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |=
++++++++ LENIENT_HEADERS | LENIENT_CHUNKED_LENGTH | LENIENT_KEEP_ALIVE |
++++++++ LENIENT_TRANSFER_ENCODING | LENIENT_VERSION | LENIENT_DATA_AFTER_CLOSE |
++++++++ LENIENT_OPTIONAL_LF_AFTER_CR | LENIENT_OPTIONAL_CR_BEFORE_LF |
++++++++ LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_request_lenient_headers(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_HEADERS;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_request_lenient_chunked_length(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_CHUNKED_LENGTH;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_request_lenient_keep_alive(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_KEEP_ALIVE;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_transfer_encoding(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_TRANSFER_ENCODING;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_request_lenient_version(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_VERSION;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_response_lenient_keep_alive(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_KEEP_ALIVE;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_version(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_VERSION;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_init_response_lenient_headers(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_HEADERS;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_data_after_close(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_DATA_AFTER_CLOSE;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_data_after_close(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_DATA_AFTER_CLOSE;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_optional_lf_after_cr(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_LF_AFTER_CR;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_optional_lf_after_cr(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_LF_AFTER_CR;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_optional_cr_before_lf(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_CR_BEFORE_LF;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_optional_cr_before_lf(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_CR_BEFORE_LF;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_optional_crlf_after_chunk(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_optional_crlf_after_chunk(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_OPTIONAL_CRLF_AFTER_CHUNK;
++++++++}
++++++++
++++++++void llhttp__test_init_request_lenient_spaces_after_chunk_size(llparse_t* s) {
++++++++ llhttp__test_init_request(s);
++++++++ s->lenient_flags |= LENIENT_SPACES_AFTER_CHUNK_SIZE;
++++++++}
++++++++
++++++++void llhttp__test_init_response_lenient_spaces_after_chunk_size(llparse_t* s) {
++++++++ llhttp__test_init_response(s);
++++++++ s->lenient_flags |= LENIENT_SPACES_AFTER_CHUNK_SIZE;
++++++++}
++++++++
++++++++
++++++++void llhttp__test_finish(llparse_t* s) {
++++++++ llparse__print(NULL, NULL, "finish=%d", s->finish);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_message_begin(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "message begin");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_MESSAGE_BEGIN
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_message_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "message complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_MESSAGE_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_status(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("status", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_status_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "status complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_STATUS_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_method(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench || s->type != HTTP_REQUEST)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("method", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_method_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "method complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_METHOD_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_version(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("version", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_version_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "version complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_VERSION_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++int llhttp__on_header_field(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("header_field", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_field_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "header_field complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_HEADER_FIELD_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_value(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("header_value", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_header_value_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "header_value complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_HEADER_VALUE_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_headers_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ if (s->type == HTTP_REQUEST) {
++++++++ llparse__print(p, endp,
++++++++ "headers complete method=%d v=%d/%d flags=%x content_length=%llu",
++++++++ s->method, s->http_major, s->http_minor, s->flags, s->content_length);
++++++++ } else if (s->type == HTTP_RESPONSE) {
++++++++ llparse__print(p, endp,
++++++++ "headers complete status=%d v=%d/%d flags=%x content_length=%llu",
++++++++ s->status_code, s->http_major, s->http_minor, s->flags,
++++++++ s->content_length);
++++++++ } else {
++++++++ llparse__print(p, endp, "invalid headers complete");
++++++++ }
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_HEADERS_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #elif defined(LLHTTP__TEST_SKIP_BODY)
++++++++ llparse__print(p, endp, "skip body");
++++++++ return 1;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_body(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("body", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_header(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "chunk header len=%d", (int) s->content_length);
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_CHUNK_HEADER
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_name(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("chunk_extension_name", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_name_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "chunk_extension_name complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_CHUNK_EXTENSION_NAME
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_value(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ return llparse__print_span("chunk_extension_value", p, endp);
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_extension_value_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "chunk_extension_value complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_CHUNK_EXTENSION_VALUE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++
++++++++int llhttp__on_chunk_complete(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "chunk complete");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_CHUNK_COMPLETE
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++int llhttp__on_reset(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++
++++++++ llparse__print(p, endp, "reset");
++++++++
++++++++ #ifdef LLHTTP__TEST_PAUSE_ON_RESET
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++ #else
++++++++ return 0;
++++++++ #endif
++++++++}
++++++++
++++++++#endif /* LLHTTP__TEST_HTTP */
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as fs from 'fs';
++++++++import { ICompilerResult, LLParse } from 'llparse';
++++++++import { Dot } from 'llparse-dot';
++++++++import {
++++++++ Fixture, FixtureResult, IFixtureBuildOptions,
++++++++} from 'llparse-test-fixture';
++++++++import * as path from 'path';
++++++++
++++++++import * as llhttp from '../../src/llhttp';
++++++++
++++++++export type Node = Parameters<LLParse['build']>['0'];
++++++++
++++++++export { FixtureResult };
++++++++
++++++++export type TestType = 'request' | 'response' | 'request-finish' | 'response-finish' |
++++++++ 'request-lenient-all' | 'response-lenient-all' |
++++++++ 'request-lenient-headers' | 'response-lenient-headers' |
++++++++ 'request-lenient-chunked-length' | 'request-lenient-transfer-encoding' |
++++++++ 'request-lenient-keep-alive' | 'response-lenient-keep-alive' |
++++++++ 'request-lenient-version' | 'response-lenient-version' |
++++++++ 'request-lenient-data-after-close' | 'response-lenient-data-after-close' |
++++++++ 'request-lenient-optional-lf-after-cr' | 'response-lenient-optional-lf-after-cr' |
++++++++ 'request-lenient-optional-cr-before-lf' | 'response-lenient-optional-cr-before-lf' |
++++++++ 'request-lenient-optional-crlf-after-chunk' | 'response-lenient-optional-crlf-after-chunk' |
++++++++ 'request-lenient-spaces-after-chunk-size' | 'response-lenient-spaces-after-chunk-size' |
++++++++ 'none' | 'url';
++++++++
++++++++export const allowedTypes: TestType[] = [
++++++++ 'request',
++++++++ 'response',
++++++++ 'request-finish',
++++++++ 'response-finish',
++++++++ 'request-lenient-all',
++++++++ 'response-lenient-all',
++++++++ 'request-lenient-headers',
++++++++ 'response-lenient-headers',
++++++++ 'request-lenient-keep-alive',
++++++++ 'response-lenient-keep-alive',
++++++++ 'request-lenient-chunked-length',
++++++++ 'request-lenient-transfer-encoding',
++++++++ 'request-lenient-version',
++++++++ 'response-lenient-version',
++++++++ 'request-lenient-data-after-close',
++++++++ 'response-lenient-data-after-close',
++++++++ 'request-lenient-optional-lf-after-cr',
++++++++ 'response-lenient-optional-lf-after-cr',
++++++++ 'request-lenient-optional-cr-before-lf',
++++++++ 'response-lenient-optional-cr-before-lf',
++++++++ 'request-lenient-optional-crlf-after-chunk',
++++++++ 'response-lenient-optional-crlf-after-chunk',
++++++++ 'request-lenient-spaces-after-chunk-size',
++++++++ 'response-lenient-spaces-after-chunk-size',
++++++++];
++++++++
++++++++const BUILD_DIR = path.join(__dirname, '..', 'tmp');
++++++++const CHEADERS_FILE = path.join(BUILD_DIR, 'cheaders.h');
++++++++
++++++++const cheaders = new llhttp.CHeaders().build();
++++++++try {
++++++++ fs.mkdirSync(BUILD_DIR);
++++++++} catch (e) {
++++++++ // no-op
++++++++}
++++++++fs.writeFileSync(CHEADERS_FILE, cheaders);
++++++++
++++++++const fixtures = new Fixture({
++++++++ buildDir: path.join(__dirname, '..', 'tmp'),
++++++++ extra: [
++++++++ '-msse4.2',
++++++++ '-DLLHTTP__TEST',
++++++++ '-DLLPARSE__ERROR_PAUSE=' + llhttp.constants.ERROR.PAUSED,
++++++++ '-include', CHEADERS_FILE,
++++++++ path.join(__dirname, 'extra.c'),
++++++++ ],
++++++++ maxParallel: process.env.LLPARSE_DEBUG ? 1 : undefined,
++++++++});
++++++++
++++++++const cache: Map<Node, ICompilerResult> = new Map();
++++++++
++++++++export async function build(
++++++++ llparse: LLParse, node: Node, outFile: string,
++++++++ options: IFixtureBuildOptions = {},
++++++++ ty: TestType = 'none'): Promise<FixtureResult> {
++++++++ const dot = new Dot();
++++++++ fs.writeFileSync(path.join(BUILD_DIR, outFile + '.dot'),
++++++++ dot.build(node));
++++++++
++++++++ let artifacts: ICompilerResult;
++++++++ if (cache.has(node)) {
++++++++ artifacts = cache.get(node)!;
++++++++ } else {
++++++++ artifacts = llparse.build(node, {
++++++++ c: { header: outFile },
++++++++ debug: process.env.LLPARSE_DEBUG ? 'llparse__debug' : undefined,
++++++++ });
++++++++ cache.set(node, artifacts);
++++++++ }
++++++++
++++++++ const extra = options.extra === undefined ? [] : options.extra.slice();
++++++++
++++++++ if (allowedTypes.includes(ty)) {
++++++++ extra.push(
++++++++ `-DLLPARSE__TEST_INIT=llhttp__test_init_${ty.replace(/-/g, '_')}`);
++++++++ }
++++++++
++++++++ if (ty === 'request-finish' || ty === 'response-finish') {
++++++++ if (ty === 'request-finish') {
++++++++ extra.push('-DLLPARSE__TEST_INIT=llhttp__test_init_request');
++++++++ } else {
++++++++ extra.push('-DLLPARSE__TEST_INIT=llhttp__test_init_response');
++++++++ }
++++++++ extra.push('-DLLPARSE__TEST_FINISH=llhttp__test_finish');
++++++++ }
++++++++
++++++++ return await fixtures.build(artifacts, outFile, Object.assign(options, {
++++++++ extra,
++++++++ }));
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include "llhttp.h"
++++++++#include <stdio.h>
++++++++#include <stdlib.h>
++++++++#include <string.h>
++++++++
++++++++int handle_on_message_complete(llhttp_t *arg) { return 0; }
++++++++
++++++++int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
++++++++ llhttp_t parser;
++++++++ llhttp_settings_t settings;
++++++++ llhttp_type_t http_type;
++++++++
++++++++ /* We need four bytes to determine variable parameters */
++++++++ if (size < 4) {
++++++++ return 0;
++++++++ }
++++++++
++++++++ int headers = (data[0] & 0x01) == 1;
++++++++ int chunked_length = (data[1] & 0x01) == 1;
++++++++ int keep_alive = (data[2] & 0x01) == 1;
++++++++ if (data[0] % 3 == 0) {
++++++++ http_type = HTTP_BOTH;
++++++++ } else if (data[0] % 3 == 1) {
++++++++ http_type = HTTP_REQUEST;
++++++++ } else {
++++++++ http_type = HTTP_RESPONSE;
++++++++ }
++++++++ data += 4;
++++++++ size -= 4;
++++++++
++++++++ /* Initialize user callbacks and settings */
++++++++ llhttp_settings_init(&settings);
++++++++
++++++++ /* Set user callback */
++++++++ settings.on_message_complete = handle_on_message_complete;
++++++++
++++++++ llhttp_init(&parser, http_type, &settings);
++++++++ llhttp_set_lenient_headers(&parser, headers);
++++++++ llhttp_set_lenient_chunked_length(&parser, chunked_length);
++++++++ llhttp_set_lenient_keep_alive(&parser, keep_alive);
++++++++
++++++++ llhttp_execute(&parser, data, size);
++++++++
++++++++ return 0;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'node:assert';
++++++++import { describe, test } from 'node:test';
++++++++import * as fs from 'fs';
++++++++import { LLParse } from 'llparse';
++++++++import { Group, MDGator, Metadata, Test } from 'mdgator';
++++++++import * as path from 'path';
++++++++import * as vm from 'vm';
++++++++
++++++++import * as llhttp from '../src/llhttp';
++++++++import { allowedTypes, build, FixtureResult, Node, TestType } from './fixtures';
++++++++
++++++++//
++++++++// Cache nodes/llparse instances ahead of time
++++++++// (different types of tests will re-use them)
++++++++//
++++++++
++++++++const modeCache = new Map<string, FixtureResult>();
++++++++
++++++++function buildNode() {
++++++++ const p = new LLParse();
++++++++ const instance = new llhttp.HTTP(p);
++++++++
++++++++ return { llparse: p, entry: instance.build().entry };
++++++++}
++++++++
++++++++function buildURL() {
++++++++ const p = new LLParse();
++++++++ const instance = new llhttp.URL(p, true);
++++++++
++++++++ const node = instance.build();
++++++++
++++++++ // Loop
++++++++ node.exit.toHTTP.otherwise(node.entry.normal);
++++++++ node.exit.toHTTP09.otherwise(node.entry.normal);
++++++++
++++++++ return { llparse: p, entry: node.entry.normal };
++++++++}
++++++++
++++++++//
++++++++// Build binaries using cached nodes/llparse
++++++++//
++++++++
++++++++async function buildMode(ty: TestType, meta: Metadata): Promise<FixtureResult> {
++++++++ const cacheKey = `${ty}:${JSON.stringify(meta || {})}`;
++++++++ let entry = modeCache.get(cacheKey);
++++++++
++++++++ if (entry) {
++++++++ return entry;
++++++++ }
++++++++
++++++++ let node: { llparse: LLParse; entry: Node };
++++++++ let prefix: string;
++++++++ let extra: string[];
++++++++ if (ty === 'url') {
++++++++ node = buildURL();
++++++++ prefix = 'url';
++++++++ extra = [];
++++++++ } else {
++++++++ node = buildNode();
++++++++ prefix = 'http';
++++++++ extra = [
++++++++ '-DLLHTTP__TEST_HTTP',
++++++++ path.join(__dirname, '..', 'src', 'native', 'http.c'),
++++++++ ];
++++++++ }
++++++++
++++++++ if (meta.pause) {
++++++++ extra.push(`-DLLHTTP__TEST_PAUSE_${meta.pause.toUpperCase()}=1`);
++++++++ }
++++++++
++++++++ if (meta.skipBody) {
++++++++ extra.push('-DLLHTTP__TEST_SKIP_BODY=1');
++++++++ }
++++++++
++++++++ entry = await build(node.llparse, node.entry, `${prefix}-${ty}`, {
++++++++ extra,
++++++++ }, ty);
++++++++
++++++++ modeCache.set(cacheKey, entry);
++++++++ return entry;
++++++++}
++++++++
++++++++//
++++++++// Run test suite
++++++++//
++++++++
++++++++function run(name: string): void {
++++++++ const md = new MDGator();
++++++++
++++++++ const raw = fs.readFileSync(path.join(__dirname, name + '.md')).toString();
++++++++ const groups = md.parse(raw);
++++++++
++++++++ function runSingleTest(ty: TestType, meta: Metadata,
++++++++ input: string,
++++++++ expected: ReadonlyArray<string | RegExp>): void {
++++++++ test(`should pass for type="${ty}"`, { timeout: 60000 }, async () => {
++++++++ const binary = await buildMode(ty, meta);
++++++++ await binary.check(input, expected, {
++++++++ noScan: meta.noScan === true,
++++++++ });
++++++++ });
++++++++ }
++++++++
++++++++ function runTest(test: Test) {
++++++++ describe(test.name + ` at ${name}.md:${test.line + 1}`, () => {
++++++++ let types: TestType[] = [];
++++++++
++++++++ const isURL = test.values.has('url');
++++++++ const inputKey = isURL ? 'url' : 'http';
++++++++
++++++++ assert(test.values.has(inputKey),
++++++++ `Missing "${inputKey}" code in md file`);
++++++++ assert.strictEqual(test.values.get(inputKey)!.length, 1,
++++++++ `Expected just one "${inputKey}" input`);
++++++++
++++++++ let meta: Metadata;
++++++++ if (test.meta.has(inputKey)) {
++++++++ meta = test.meta.get(inputKey)![0]!;
++++++++ } else {
++++++++ assert(isURL, 'Missing required http metadata');
++++++++ meta = {};
++++++++ }
++++++++
++++++++ if (isURL) {
++++++++ types = [ 'url' ];
++++++++ } else {
++++++++ assert(Object.prototype.hasOwnProperty.call(meta, 'type'), 'Missing required `type` metadata');
++++++++
++++++++ if (meta.type) {
++++++++ if (!allowedTypes.includes(meta.type)) {
++++++++ throw new Error(`Invalid value of \`type\` metadata: "${meta.type}"`);
++++++++ }
++++++++
++++++++ types.push(meta.type);
++++++++ }
++++++++ }
++++++++
++++++++ assert(test.values.has('log'), 'Missing `log` code in md file');
++++++++
++++++++ assert.strictEqual(test.values.get('log')!.length, 1,
++++++++ 'Expected just one output');
++++++++
++++++++ let input: string = test.values.get(inputKey)![0];
++++++++ let expected: string = test.values.get('log')![0];
++++++++
++++++++ // Remove trailing newline
++++++++ input = input.replace(/\n$/, '');
++++++++
++++++++ // Remove escaped newlines
++++++++ input = input.replace(/\\(\r\n|\r|\n)/g, '');
++++++++
++++++++ // Normalize all newlines
++++++++ input = input.replace(/\r\n|\r|\n/g, '\r\n');
++++++++
++++++++ // Replace escaped CRLF, tabs, form-feed
++++++++ input = input.replace(/\\r/g, '\r');
++++++++ input = input.replace(/\\n/g, '\n');
++++++++ input = input.replace(/\\t/g, '\t');
++++++++ input = input.replace(/\\f/g, '\f');
++++++++ input = input.replace(/\\x([0-9a-fA-F]+)/g, (all, hex) => {
++++++++ return String.fromCharCode(parseInt(hex, 16));
++++++++ });
++++++++
++++++++ // Useful in token tests
++++++++ input = input.replace(/\\([0-7]{1,3})/g, (_, digits) => {
++++++++ return String.fromCharCode(parseInt(digits, 8));
++++++++ });
++++++++
++++++++ // Evaluate inline JavaScript
++++++++ input = input.replace(/\$\{(.+?)\}/g, (_, code) => {
++++++++ return vm.runInNewContext(code) + '';
++++++++ });
++++++++
++++++++ // Escape first symbol `\r` or `\n`, `|`, `&` for Windows
++++++++ if (process.platform === 'win32') {
++++++++ const firstByte = Buffer.from(input)[0];
++++++++ if (firstByte === 0x0a || firstByte === 0x0d) {
++++++++ input = '\\' + input;
++++++++ }
++++++++
++++++++ input = input.replace(/\|/g, '^|');
++++++++ input = input.replace(/&/g, '^&');
++++++++ }
++++++++
++++++++ // Replace escaped tabs/form-feed in expected too
++++++++ expected = expected.replace(/\\t/g, '\t');
++++++++ expected = expected.replace(/\\f/g, '\f');
++++++++
++++++++ // Split
++++++++ const expectedLines = expected.split(/\n/g).slice(0, -1);
++++++++
++++++++ const fullExpected = expectedLines.map((line) => {
++++++++ if (line.startsWith('/')) {
++++++++ return new RegExp(line.trim().slice(1, -1));
++++++++ } else {
++++++++ return line;
++++++++ }
++++++++ });
++++++++
++++++++ for (const ty of types) {
++++++++ if (meta.skip === true || (process.env.ONLY === 'true' && !meta.only)) {
++++++++ continue;
++++++++ }
++++++++
++++++++ runSingleTest(ty, meta, input, fullExpected);
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ function runGroup(group: Group) {
++++++++ describe(group.name + ` at ${name}.md:${group.line + 1}`, function () {
++++++++ for (const child of group.children) {
++++++++ runGroup(child);
++++++++ }
++++++++
++++++++ for (const test of group.tests) {
++++++++ runTest(test);
++++++++ }
++++++++ });
++++++++ }
++++++++
++++++++ for (const group of groups) {
++++++++ runGroup(group);
++++++++ }
++++++++}
++++++++
++++++++run('request/sample');
++++++++run('request/lenient-headers');
++++++++run('request/lenient-version');
++++++++run('request/method');
++++++++run('request/uri');
++++++++run('request/connection');
++++++++run('request/content-length');
++++++++run('request/transfer-encoding');
++++++++run('request/invalid');
++++++++run('request/finish');
++++++++run('request/pausing');
++++++++run('request/pipelining');
++++++++
++++++++run('response/sample');
++++++++run('response/connection');
++++++++run('response/content-length');
++++++++run('response/transfer-encoding');
++++++++run('response/invalid');
++++++++run('response/finish');
++++++++run('response/lenient-version');
++++++++run('response/pausing');
++++++++run('response/pipelining');
++++++++
++++++++run('url');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Connection header
++++++++=================
++++++++
++++++++## `keep-alive`
++++++++
++++++++### Setting flag
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: keep-alive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=10 span[header_value]="keep-alive"
++++++++off=43 header_value complete
++++++++off=45 headers complete method=4 v=1/1 flags=1 content_length=0
++++++++off=45 message complete
++++++++```
++++++++
++++++++### Restarting when keep-alive is explicitly
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: keep-alive
++++++++
++++++++PUT /url HTTP/1.1
++++++++Connection: keep-alive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=10 span[header_value]="keep-alive"
++++++++off=43 header_value complete
++++++++off=45 headers complete method=4 v=1/1 flags=1 content_length=0
++++++++off=45 message complete
++++++++off=45 reset
++++++++off=45 message begin
++++++++off=45 len=3 span[method]="PUT"
++++++++off=48 method complete
++++++++off=49 len=4 span[url]="/url"
++++++++off=54 url complete
++++++++off=59 len=3 span[version]="1.1"
++++++++off=62 version complete
++++++++off=64 len=10 span[header_field]="Connection"
++++++++off=75 header_field complete
++++++++off=76 len=10 span[header_value]="keep-alive"
++++++++off=88 header_value complete
++++++++off=90 headers complete method=4 v=1/1 flags=1 content_length=0
++++++++off=90 message complete
++++++++```
++++++++
++++++++### No restart when keep-alive is off (1.0)
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++```http
++++++++PUT /url HTTP/1.0
++++++++
++++++++PUT /url HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.0"
++++++++off=17 version complete
++++++++off=21 headers complete method=4 v=1/0 flags=0 content_length=0
++++++++off=21 message complete
++++++++off=22 error code=5 reason="Data after `Connection: close`"
++++++++```
++++++++
++++++++### Resetting flags when keep-alive is off (1.0, lenient)
++++++++
++++++++Even though we allow restarts in loose mode, the flags should be still set to
++++++++`0` upon restart.
++++++++
++++++++<!-- meta={"type": "request-lenient-keep-alive"} -->
++++++++```http
++++++++PUT /url HTTP/1.0
++++++++Content-Length: 0
++++++++
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.0"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="0"
++++++++off=38 header_value complete
++++++++off=40 headers complete method=4 v=1/0 flags=20 content_length=0
++++++++off=40 message complete
++++++++off=40 reset
++++++++off=40 message begin
++++++++off=40 len=3 span[method]="PUT"
++++++++off=43 method complete
++++++++off=44 len=4 span[url]="/url"
++++++++off=49 url complete
++++++++off=54 len=3 span[version]="1.1"
++++++++off=57 version complete
++++++++off=59 len=17 span[header_field]="Transfer-Encoding"
++++++++off=77 header_field complete
++++++++off=78 len=7 span[header_value]="chunked"
++++++++off=87 header_value complete
++++++++off=89 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++```
++++++++
++++++++### CRLF between requests, implicit `keep-alive`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: www.example.com
++++++++Content-Type: application/x-www-form-urlencoded
++++++++Content-Length: 4
++++++++
++++++++q=42
++++++++
++++++++GET / HTTP/1.1
++++++++```
++++++++_Note the trailing CRLF above_
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=15 span[header_value]="www.example.com"
++++++++off=40 header_value complete
++++++++off=40 len=12 span[header_field]="Content-Type"
++++++++off=53 header_field complete
++++++++off=54 len=33 span[header_value]="application/x-www-form-urlencoded"
++++++++off=89 header_value complete
++++++++off=89 len=14 span[header_field]="Content-Length"
++++++++off=104 header_field complete
++++++++off=105 len=1 span[header_value]="4"
++++++++off=108 header_value complete
++++++++off=110 headers complete method=3 v=1/1 flags=20 content_length=4
++++++++off=110 len=4 span[body]="q=42"
++++++++off=114 message complete
++++++++off=118 reset
++++++++off=118 message begin
++++++++off=118 len=3 span[method]="GET"
++++++++off=121 method complete
++++++++off=122 len=1 span[url]="/"
++++++++off=124 url complete
++++++++off=129 len=3 span[version]="1.1"
++++++++off=132 version complete
++++++++```
++++++++
++++++++### Not treating `\r` as `-`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: keep\ralive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=4 span[header_value]="keep"
++++++++off=36 error code=3 reason="Missing expected LF after header value"
++++++++```
++++++++
++++++++## `close`
++++++++
++++++++### Setting flag on `close`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: close
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=5 span[header_value]="close"
++++++++off=38 header_value complete
++++++++off=40 headers complete method=4 v=1/1 flags=2 content_length=0
++++++++off=40 message complete
++++++++```
++++++++
++++++++### CRLF between requests, explicit `close`
++++++++
++++++++`close` means closed connection
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: www.example.com
++++++++Content-Type: application/x-www-form-urlencoded
++++++++Content-Length: 4
++++++++Connection: close
++++++++
++++++++q=42
++++++++
++++++++GET / HTTP/1.1
++++++++```
++++++++_Note the trailing CRLF above_
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=15 span[header_value]="www.example.com"
++++++++off=40 header_value complete
++++++++off=40 len=12 span[header_field]="Content-Type"
++++++++off=53 header_field complete
++++++++off=54 len=33 span[header_value]="application/x-www-form-urlencoded"
++++++++off=89 header_value complete
++++++++off=89 len=14 span[header_field]="Content-Length"
++++++++off=104 header_field complete
++++++++off=105 len=1 span[header_value]="4"
++++++++off=108 header_value complete
++++++++off=108 len=10 span[header_field]="Connection"
++++++++off=119 header_field complete
++++++++off=120 len=5 span[header_value]="close"
++++++++off=127 header_value complete
++++++++off=129 headers complete method=3 v=1/1 flags=22 content_length=4
++++++++off=129 len=4 span[body]="q=42"
++++++++off=133 message complete
++++++++off=138 error code=5 reason="Data after `Connection: close`"
++++++++```
++++++++
++++++++### CRLF between requests, explicit `close` (lenient)
++++++++
++++++++Loose mode is more lenient, and allows further requests.
++++++++
++++++++<!-- meta={"type": "request-lenient-keep-alive"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: www.example.com
++++++++Content-Type: application/x-www-form-urlencoded
++++++++Content-Length: 4
++++++++Connection: close
++++++++
++++++++q=42
++++++++
++++++++GET / HTTP/1.1
++++++++```
++++++++_Note the trailing CRLF above_
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=15 span[header_value]="www.example.com"
++++++++off=40 header_value complete
++++++++off=40 len=12 span[header_field]="Content-Type"
++++++++off=53 header_field complete
++++++++off=54 len=33 span[header_value]="application/x-www-form-urlencoded"
++++++++off=89 header_value complete
++++++++off=89 len=14 span[header_field]="Content-Length"
++++++++off=104 header_field complete
++++++++off=105 len=1 span[header_value]="4"
++++++++off=108 header_value complete
++++++++off=108 len=10 span[header_field]="Connection"
++++++++off=119 header_field complete
++++++++off=120 len=5 span[header_value]="close"
++++++++off=127 header_value complete
++++++++off=129 headers complete method=3 v=1/1 flags=22 content_length=4
++++++++off=129 len=4 span[body]="q=42"
++++++++off=133 message complete
++++++++off=137 reset
++++++++off=137 message begin
++++++++off=137 len=3 span[method]="GET"
++++++++off=140 method complete
++++++++off=141 len=1 span[url]="/"
++++++++off=143 url complete
++++++++off=148 len=3 span[version]="1.1"
++++++++off=151 version complete
++++++++```
++++++++
++++++++## Parsing multiple tokens
++++++++
++++++++### Sample
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: close, token, upgrade, token, keep-alive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=40 span[header_value]="close, token, upgrade, token, keep-alive"
++++++++off=73 header_value complete
++++++++off=75 headers complete method=4 v=1/1 flags=7 content_length=0
++++++++off=75 message complete
++++++++```
++++++++
++++++++### Multiple tokens with folding
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET /demo HTTP/1.1
++++++++Host: example.com
++++++++Connection: Something,
++++++++ Upgrade, ,Keep-Alive
++++++++Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
++++++++Sec-WebSocket-Protocol: sample
++++++++Upgrade: WebSocket
++++++++Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
++++++++Origin: http://example.com
++++++++
++++++++Hot diggity dogg
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/demo"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=4 span[header_field]="Host"
++++++++off=25 header_field complete
++++++++off=26 len=11 span[header_value]="example.com"
++++++++off=39 header_value complete
++++++++off=39 len=10 span[header_field]="Connection"
++++++++off=50 header_field complete
++++++++off=51 len=10 span[header_value]="Something,"
++++++++off=63 len=21 span[header_value]=" Upgrade, ,Keep-Alive"
++++++++off=86 header_value complete
++++++++off=86 len=18 span[header_field]="Sec-WebSocket-Key2"
++++++++off=105 header_field complete
++++++++off=106 len=18 span[header_value]="12998 5 Y3 1 .P00"
++++++++off=126 header_value complete
++++++++off=126 len=22 span[header_field]="Sec-WebSocket-Protocol"
++++++++off=149 header_field complete
++++++++off=150 len=6 span[header_value]="sample"
++++++++off=158 header_value complete
++++++++off=158 len=7 span[header_field]="Upgrade"
++++++++off=166 header_field complete
++++++++off=167 len=9 span[header_value]="WebSocket"
++++++++off=178 header_value complete
++++++++off=178 len=18 span[header_field]="Sec-WebSocket-Key1"
++++++++off=197 header_field complete
++++++++off=198 len=20 span[header_value]="4 @1 46546xW%0l 1 5"
++++++++off=220 header_value complete
++++++++off=220 len=6 span[header_field]="Origin"
++++++++off=227 header_field complete
++++++++off=228 len=18 span[header_value]="http://example.com"
++++++++off=248 header_value complete
++++++++off=250 headers complete method=1 v=1/1 flags=15 content_length=0
++++++++off=250 message complete
++++++++off=250 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Multiple tokens with folding and LWS
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /demo HTTP/1.1
++++++++Connection: keep-alive, upgrade
++++++++Upgrade: WebSocket
++++++++
++++++++Hot diggity dogg
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/demo"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=10 span[header_field]="Connection"
++++++++off=31 header_field complete
++++++++off=32 len=19 span[header_value]="keep-alive, upgrade"
++++++++off=53 header_value complete
++++++++off=53 len=7 span[header_field]="Upgrade"
++++++++off=61 header_field complete
++++++++off=62 len=9 span[header_value]="WebSocket"
++++++++off=73 header_value complete
++++++++off=75 headers complete method=1 v=1/1 flags=15 content_length=0
++++++++off=75 message complete
++++++++off=75 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Multiple tokens with folding, LWS, and CRLF
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET /demo HTTP/1.1
++++++++Connection: keep-alive, \r\n upgrade
++++++++Upgrade: WebSocket
++++++++
++++++++Hot diggity dogg
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/demo"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=10 span[header_field]="Connection"
++++++++off=31 header_field complete
++++++++off=32 len=12 span[header_value]="keep-alive, "
++++++++off=46 len=8 span[header_value]=" upgrade"
++++++++off=56 header_value complete
++++++++off=56 len=7 span[header_field]="Upgrade"
++++++++off=64 header_field complete
++++++++off=65 len=9 span[header_value]="WebSocket"
++++++++off=76 header_value complete
++++++++off=78 headers complete method=1 v=1/1 flags=15 content_length=0
++++++++off=78 message complete
++++++++off=78 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Invalid whitespace token with `Connection` header field
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection : upgrade
++++++++Content-Length: 4
++++++++Upgrade: ws
++++++++
++++++++abcdefgh
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 error code=10 reason="Invalid header field char"
++++++++```
++++++++
++++++++### Invalid whitespace token with `Connection` header field (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection : upgrade
++++++++Content-Length: 4
++++++++Upgrade: ws
++++++++
++++++++abcdefgh
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=11 span[header_field]="Connection "
++++++++off=31 header_field complete
++++++++off=32 len=7 span[header_value]="upgrade"
++++++++off=41 header_value complete
++++++++off=41 len=14 span[header_field]="Content-Length"
++++++++off=56 header_field complete
++++++++off=57 len=1 span[header_value]="4"
++++++++off=60 header_value complete
++++++++off=60 len=7 span[header_field]="Upgrade"
++++++++off=68 header_field complete
++++++++off=69 len=2 span[header_value]="ws"
++++++++off=73 header_value complete
++++++++off=75 headers complete method=4 v=1/1 flags=34 content_length=4
++++++++off=75 len=4 span[body]="abcd"
++++++++off=79 message complete
++++++++off=79 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++## `upgrade`
++++++++
++++++++### Setting a flag and pausing
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: upgrade
++++++++Upgrade: ws
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=7 span[header_value]="upgrade"
++++++++off=40 header_value complete
++++++++off=40 len=7 span[header_field]="Upgrade"
++++++++off=48 header_field complete
++++++++off=49 len=2 span[header_value]="ws"
++++++++off=53 header_value complete
++++++++off=55 headers complete method=4 v=1/1 flags=14 content_length=0
++++++++off=55 message complete
++++++++off=55 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Emitting part of body and pausing
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: upgrade
++++++++Content-Length: 4
++++++++Upgrade: ws
++++++++
++++++++abcdefgh
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=7 span[header_value]="upgrade"
++++++++off=40 header_value complete
++++++++off=40 len=14 span[header_field]="Content-Length"
++++++++off=55 header_field complete
++++++++off=56 len=1 span[header_value]="4"
++++++++off=59 header_value complete
++++++++off=59 len=7 span[header_field]="Upgrade"
++++++++off=67 header_field complete
++++++++off=68 len=2 span[header_value]="ws"
++++++++off=72 header_value complete
++++++++off=74 headers complete method=4 v=1/1 flags=34 content_length=4
++++++++off=74 len=4 span[body]="abcd"
++++++++off=78 message complete
++++++++off=78 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Upgrade GET request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /demo HTTP/1.1
++++++++Host: example.com
++++++++Connection: Upgrade
++++++++Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
++++++++Sec-WebSocket-Protocol: sample
++++++++Upgrade: WebSocket
++++++++Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
++++++++Origin: http://example.com
++++++++
++++++++Hot diggity dogg
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/demo"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=4 span[header_field]="Host"
++++++++off=25 header_field complete
++++++++off=26 len=11 span[header_value]="example.com"
++++++++off=39 header_value complete
++++++++off=39 len=10 span[header_field]="Connection"
++++++++off=50 header_field complete
++++++++off=51 len=7 span[header_value]="Upgrade"
++++++++off=60 header_value complete
++++++++off=60 len=18 span[header_field]="Sec-WebSocket-Key2"
++++++++off=79 header_field complete
++++++++off=80 len=18 span[header_value]="12998 5 Y3 1 .P00"
++++++++off=100 header_value complete
++++++++off=100 len=22 span[header_field]="Sec-WebSocket-Protocol"
++++++++off=123 header_field complete
++++++++off=124 len=6 span[header_value]="sample"
++++++++off=132 header_value complete
++++++++off=132 len=7 span[header_field]="Upgrade"
++++++++off=140 header_field complete
++++++++off=141 len=9 span[header_value]="WebSocket"
++++++++off=152 header_value complete
++++++++off=152 len=18 span[header_field]="Sec-WebSocket-Key1"
++++++++off=171 header_field complete
++++++++off=172 len=20 span[header_value]="4 @1 46546xW%0l 1 5"
++++++++off=194 header_value complete
++++++++off=194 len=6 span[header_field]="Origin"
++++++++off=201 header_field complete
++++++++off=202 len=18 span[header_value]="http://example.com"
++++++++off=222 header_value complete
++++++++off=224 headers complete method=1 v=1/1 flags=14 content_length=0
++++++++off=224 message complete
++++++++off=224 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### Upgrade POST request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /demo HTTP/1.1
++++++++Host: example.com
++++++++Connection: Upgrade
++++++++Upgrade: HTTP/2.0
++++++++Content-Length: 15
++++++++
++++++++sweet post body\
++++++++Hot diggity dogg
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=5 span[url]="/demo"
++++++++off=11 url complete
++++++++off=16 len=3 span[version]="1.1"
++++++++off=19 version complete
++++++++off=21 len=4 span[header_field]="Host"
++++++++off=26 header_field complete
++++++++off=27 len=11 span[header_value]="example.com"
++++++++off=40 header_value complete
++++++++off=40 len=10 span[header_field]="Connection"
++++++++off=51 header_field complete
++++++++off=52 len=7 span[header_value]="Upgrade"
++++++++off=61 header_value complete
++++++++off=61 len=7 span[header_field]="Upgrade"
++++++++off=69 header_field complete
++++++++off=70 len=8 span[header_value]="HTTP/2.0"
++++++++off=80 header_value complete
++++++++off=80 len=14 span[header_field]="Content-Length"
++++++++off=95 header_field complete
++++++++off=96 len=2 span[header_value]="15"
++++++++off=100 header_value complete
++++++++off=102 headers complete method=3 v=1/1 flags=34 content_length=15
++++++++off=102 len=15 span[body]="sweet post body"
++++++++off=117 message complete
++++++++off=117 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Content-Length header
++++++++=====================
++++++++
++++++++## `Content-Length` with zeroes
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 003
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=3 span[header_value]="003"
++++++++off=40 header_value complete
++++++++off=42 headers complete method=4 v=1/1 flags=20 content_length=3
++++++++off=42 len=3 span[body]="abc"
++++++++off=45 message complete
++++++++```
++++++++
++++++++## `Content-Length` with follow-up headers
++++++++
++++++++The way the parser works is that special headers (like `Content-Length`) first
++++++++set `header_state` to appropriate value, and then apply custom parsing using
++++++++that value. For `Content-Length`, in particular, the `header_state` is used for
++++++++setting the flag too.
++++++++
++++++++Make sure that `header_state` is reset to `0`, so that the flag won't be
++++++++attempted to set twice (and error).
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 003
++++++++Ohai: world
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=3 span[header_value]="003"
++++++++off=40 header_value complete
++++++++off=40 len=4 span[header_field]="Ohai"
++++++++off=45 header_field complete
++++++++off=46 len=5 span[header_value]="world"
++++++++off=53 header_value complete
++++++++off=55 headers complete method=4 v=1/1 flags=20 content_length=3
++++++++off=55 len=3 span[body]="abc"
++++++++off=58 message complete
++++++++```
++++++++
++++++++## Error on `Content-Length` overflow
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 1000000000000000000000
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=21 span[header_value]="100000000000000000000"
++++++++off=56 error code=11 reason="Content-Length overflow"
++++++++```
++++++++
++++++++## Error on duplicate `Content-Length`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 1
++++++++Content-Length: 2
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="1"
++++++++off=38 header_value complete
++++++++off=38 len=14 span[header_field]="Content-Length"
++++++++off=53 header_field complete
++++++++off=54 error code=4 reason="Duplicate Content-Length"
++++++++```
++++++++
++++++++## Error on simultaneous `Content-Length` and `Transfer-Encoding: identity`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 1
++++++++Transfer-Encoding: identity
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="1"
++++++++off=38 header_value complete
++++++++off=38 len=17 span[header_field]="Transfer-Encoding"
++++++++off=56 header_field complete
++++++++off=56 error code=15 reason="Transfer-Encoding can't be present with Content-Length"
++++++++```
++++++++
++++++++## Invalid whitespace token with `Content-Length` header field
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: upgrade
++++++++Content-Length : 4
++++++++Upgrade: ws
++++++++
++++++++abcdefgh
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=7 span[header_value]="upgrade"
++++++++off=40 header_value complete
++++++++off=40 len=14 span[header_field]="Content-Length"
++++++++off=55 error code=10 reason="Invalid header field char"
++++++++```
++++++++
++++++++## Invalid whitespace token with `Content-Length` header field (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Connection: upgrade
++++++++Content-Length : 4
++++++++Upgrade: ws
++++++++
++++++++abcdefgh
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=10 span[header_field]="Connection"
++++++++off=30 header_field complete
++++++++off=31 len=7 span[header_value]="upgrade"
++++++++off=40 header_value complete
++++++++off=40 len=15 span[header_field]="Content-Length "
++++++++off=56 header_field complete
++++++++off=57 len=1 span[header_value]="4"
++++++++off=60 header_value complete
++++++++off=60 len=7 span[header_field]="Upgrade"
++++++++off=68 header_field complete
++++++++off=69 len=2 span[header_value]="ws"
++++++++off=73 header_value complete
++++++++off=75 headers complete method=4 v=1/1 flags=34 content_length=4
++++++++off=75 len=4 span[body]="abcd"
++++++++off=79 message complete
++++++++off=79 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++## No error on simultaneous `Content-Length` and `Transfer-Encoding: identity` (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-chunked-length"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 1
++++++++Transfer-Encoding: identity
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="1"
++++++++off=38 header_value complete
++++++++off=38 len=17 span[header_field]="Transfer-Encoding"
++++++++off=56 header_field complete
++++++++off=57 len=8 span[header_value]="identity"
++++++++off=67 header_value complete
++++++++off=69 headers complete method=4 v=1/1 flags=220 content_length=1
++++++++```
++++++++
++++++++## Funky `Content-Length` with body
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /get_funky_content_length_body_hello HTTP/1.0
++++++++conTENT-Length: 5
++++++++
++++++++HELLO
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=36 span[url]="/get_funky_content_length_body_hello"
++++++++off=41 url complete
++++++++off=46 len=3 span[version]="1.0"
++++++++off=49 version complete
++++++++off=51 len=14 span[header_field]="conTENT-Length"
++++++++off=66 header_field complete
++++++++off=67 len=1 span[header_value]="5"
++++++++off=70 header_value complete
++++++++off=72 headers complete method=1 v=1/0 flags=20 content_length=5
++++++++off=72 len=5 span[body]="HELLO"
++++++++off=77 message complete
++++++++```
++++++++
++++++++## Spaces in `Content-Length` (surrounding)
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 42
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=34 len=3 span[header_value]="42 "
++++++++off=39 header_value complete
++++++++off=41 headers complete method=3 v=1/1 flags=20 content_length=42
++++++++```
++++++++
++++++++### Spaces in `Content-Length` #2
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 4 2
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=2 span[header_value]="4 "
++++++++off=35 error code=11 reason="Invalid character in Content-Length"
++++++++```
++++++++
++++++++### Spaces in `Content-Length` #3
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 13 37
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=3 span[header_value]="13 "
++++++++off=36 error code=11 reason="Invalid character in Content-Length"
++++++++```
++++++++
++++++++### Empty `Content-Length`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length:
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=34 error code=11 reason="Empty Content-Length"
++++++++```
++++++++
++++++++## `Content-Length` with CR instead of dash
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content\rLength: 003
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=26 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++## Content-Length reset when no body is received
++++++++
++++++++<!-- meta={"type": "request", "skipBody": true} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 123
++++++++
++++++++POST /url HTTP/1.1
++++++++Content-Length: 456
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=3 span[header_value]="123"
++++++++off=40 header_value complete
++++++++off=42 headers complete method=4 v=1/1 flags=20 content_length=123
++++++++off=42 skip body
++++++++off=42 message complete
++++++++off=42 reset
++++++++off=42 message begin
++++++++off=42 len=4 span[method]="POST"
++++++++off=46 method complete
++++++++off=47 len=4 span[url]="/url"
++++++++off=52 url complete
++++++++off=57 len=3 span[version]="1.1"
++++++++off=60 version complete
++++++++off=62 len=14 span[header_field]="Content-Length"
++++++++off=77 header_field complete
++++++++off=78 len=3 span[header_value]="456"
++++++++off=83 header_value complete
++++++++off=85 headers complete method=3 v=1/1 flags=20 content_length=456
++++++++off=85 skip body
++++++++off=85 message complete
++++++++```
++++++++
++++++++## Missing CRLF-CRLF before body
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 3
++++++++\rabc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="3"
++++++++off=38 header_value complete
++++++++off=39 error code=2 reason="Expected LF after headers"
++++++++```
++++++++
++++++++## Missing CRLF-CRLF before body (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-optional-lf-after-cr" } -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Content-Length: 3
++++++++\rabc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=14 span[header_field]="Content-Length"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="3"
++++++++off=38 header_value complete
++++++++off=39 headers complete method=4 v=1/1 flags=20 content_length=3
++++++++off=39 len=3 span[body]="abc"
++++++++off=42 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Finish
++++++++======
++++++++
++++++++Those tests check the return codes and the behavior of `llhttp_finish()` C API.
++++++++
++++++++## It should be safe to finish after GET request
++++++++
++++++++<!-- meta={"type": "request-finish"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=18 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=18 message complete
++++++++off=NULL finish=0
++++++++```
++++++++
++++++++## It should be unsafe to finish after incomplete PUT request
++++++++
++++++++<!-- meta={"type": "request-finish"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Content-Length: 100
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=14 span[header_field]="Content-Length"
++++++++off=31 header_field complete
++++++++off=32 len=3 span[header_value]="100"
++++++++off=NULL finish=2
++++++++```
++++++++
++++++++## It should be unsafe to finish inside of the header
++++++++
++++++++<!-- meta={"type": "request-finish"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Content-Leng
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=12 span[header_field]="Content-Leng"
++++++++off=NULL finish=2
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Invalid requests
++++++++================
++++++++
++++++++### ICE protocol and GET method
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /music/sweet/music ICE/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=18 span[url]="/music/sweet/music"
++++++++off=23 url complete
++++++++off=27 error code=8 reason="Expected SOURCE method for ICE/x.x request"
++++++++```
++++++++
++++++++### ICE protocol, but not really
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /music/sweet/music IHTTP/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=18 span[url]="/music/sweet/music"
++++++++off=23 url complete
++++++++off=24 error code=8 reason="Expected HTTP/"
++++++++```
++++++++
++++++++### RTSP protocol and PUT method
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /music/sweet/music RTSP/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=18 span[url]="/music/sweet/music"
++++++++off=23 url complete
++++++++off=28 error code=8 reason="Invalid method for RTSP/x.x request"
++++++++```
++++++++
++++++++### HTTP protocol and ANNOUNCE method
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++ANNOUNCE /music/sweet/music HTTP/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=8 span[method]="ANNOUNCE"
++++++++off=8 method complete
++++++++off=9 len=18 span[url]="/music/sweet/music"
++++++++off=28 url complete
++++++++off=33 error code=8 reason="Invalid method for HTTP/x.x request"
++++++++```
++++++++
++++++++### Headers separated by CR
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Foo: 1\rBar: 2
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=3 span[header_field]="Foo"
++++++++off=20 header_field complete
++++++++off=21 len=1 span[header_value]="1"
++++++++off=23 error code=3 reason="Missing expected LF after header value"
++++++++```
++++++++
++++++++### Headers separated by LF
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: localhost:5000
++++++++x:x\nTransfer-Encoding: chunked
++++++++
++++++++1
++++++++A
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=14 span[header_value]="localhost:5000"
++++++++off=39 header_value complete
++++++++off=39 len=1 span[header_field]="x"
++++++++off=41 header_field complete
++++++++off=41 len=1 span[header_value]="x"
++++++++off=42 error code=25 reason="Missing expected CR after header value"
++++++++```
++++++++
++++++++### Headers separated by dummy characters
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Connection: close
++++++++Host: a
++++++++\rZGET /evil: HTTP/1.1
++++++++Host: a
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=10 span[header_field]="Connection"
++++++++off=27 header_field complete
++++++++off=28 len=5 span[header_value]="close"
++++++++off=35 header_value complete
++++++++off=35 len=4 span[header_field]="Host"
++++++++off=40 header_field complete
++++++++off=41 len=1 span[header_value]="a"
++++++++off=44 header_value complete
++++++++off=45 error code=2 reason="Expected LF after headers"
++++++++```
++++++++
++++++++
++++++++### Headers separated by dummy characters (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-optional-lf-after-cr"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Connection: close
++++++++Host: a
++++++++\rZGET /evil: HTTP/1.1
++++++++Host: a
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=10 span[header_field]="Connection"
++++++++off=27 header_field complete
++++++++off=28 len=5 span[header_value]="close"
++++++++off=35 header_value complete
++++++++off=35 len=4 span[header_field]="Host"
++++++++off=40 header_field complete
++++++++off=41 len=1 span[header_value]="a"
++++++++off=44 header_value complete
++++++++off=45 headers complete method=1 v=1/1 flags=2 content_length=0
++++++++off=45 message complete
++++++++off=46 error code=5 reason="Data after `Connection: close`"
++++++++```
++++++++
++++++++### Empty headers separated by CR
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Connection: Close
++++++++Host: localhost:5000
++++++++x:\rTransfer-Encoding: chunked
++++++++
++++++++1
++++++++A
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=5 span[header_value]="Close"
++++++++off=36 header_value complete
++++++++off=36 len=4 span[header_field]="Host"
++++++++off=41 header_field complete
++++++++off=42 len=14 span[header_value]="localhost:5000"
++++++++off=58 header_value complete
++++++++off=58 len=1 span[header_field]="x"
++++++++off=60 header_field complete
++++++++off=61 error code=2 reason="Expected LF after CR"
++++++++```
++++++++
++++++++### Empty headers separated by LF
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: localhost:5000
++++++++x:\nTransfer-Encoding: chunked
++++++++
++++++++1
++++++++A
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=14 span[header_value]="localhost:5000"
++++++++off=39 header_value complete
++++++++off=39 len=1 span[header_field]="x"
++++++++off=41 header_field complete
++++++++off=42 error code=10 reason="Invalid header value char"
++++++++```
++++++++
++++++++### Invalid header token #1
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Fo@: Failure
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=18 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Invalid header token #2
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Foo\01\test: Bar
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=19 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Invalid header token #3
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++: Bar
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Invalid method
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++MKCOLA / HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=5 span[method]="MKCOL"
++++++++off=5 method complete
++++++++off=5 error code=6 reason="Expected space after method"
++++++++```
++++++++
++++++++### Illegal header field name line folding
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++name
++++++++ : value
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=20 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Corrupted Connection header
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: www.example.com
++++++++Connection\r\033\065\325eep-Alive
++++++++Accept-Encoding: gzip
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=15 span[header_value]="www.example.com"
++++++++off=39 header_value complete
++++++++off=49 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Corrupted header name
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: www.example.com
++++++++X-Some-Header\r\033\065\325eep-Alive
++++++++Accept-Encoding: gzip
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=15 span[header_value]="www.example.com"
++++++++off=39 header_value complete
++++++++off=52 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++### Missing CR between headers
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: localhost
++++++++Dummy: x\nContent-Length: 23
++++++++
++++++++GET / HTTP/1.1
++++++++Dummy: GET /admin HTTP/1.1
++++++++Host: localhost
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=9 span[header_value]="localhost"
++++++++off=33 header_value complete
++++++++off=33 len=5 span[header_field]="Dummy"
++++++++off=39 header_field complete
++++++++off=40 len=1 span[header_value]="x"
++++++++off=41 error code=25 reason="Missing expected CR after header value"
++++++++```
++++++++
++++++++### Invalid HTTP version
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/5.6
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="5.6"
++++++++off=14 error code=9 reason="Invalid HTTP version"
++++++++```
++++++++
++++++++## Invalid space after start line
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++ Host: foo
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=17 error code=30 reason="Unexpected space after start line"
++++++++```
++++++++
++++++++
++++++++### Only LFs present
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1\n\
++++++++Transfer-Encoding: chunked\n\
++++++++Trailer: Baz
++++++++Foo: abc\n\
++++++++Bar: def\n\
++++++++\n\
++++++++1\n\
++++++++A\n\
++++++++1;abc\n\
++++++++B\n\
++++++++1;def=ghi\n\
++++++++C\n\
++++++++1;jkl="mno"\n\
++++++++D\n\
++++++++0\n\
++++++++\n\
++++++++Baz: ghi\n\
++++++++\n\
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=16 error code=9 reason="Expected CRLF after version"
++++++++```
++++++++
++++++++### Only LFs present (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-all"} -->
++++++++```http
++++++++POST / HTTP/1.1\n\
++++++++Transfer-Encoding: chunked\n\
++++++++Trailer: Baz
++++++++Foo: abc\n\
++++++++Bar: def\n\
++++++++\n\
++++++++1\n\
++++++++A\n\
++++++++1;abc\n\
++++++++B\n\
++++++++1;def=ghi\n\
++++++++C\n\
++++++++1;jkl="mno"\n\
++++++++D\n\
++++++++0\n\
++++++++\n\
++++++++Baz: ghi\n\
++++++++\n
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=16 len=17 span[header_field]="Transfer-Encoding"
++++++++off=34 header_field complete
++++++++off=35 len=7 span[header_value]="chunked"
++++++++off=43 header_value complete
++++++++off=43 len=7 span[header_field]="Trailer"
++++++++off=51 header_field complete
++++++++off=52 len=3 span[header_value]="Baz"
++++++++off=57 header_value complete
++++++++off=57 len=3 span[header_field]="Foo"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="abc"
++++++++off=66 header_value complete
++++++++off=66 len=3 span[header_field]="Bar"
++++++++off=70 header_field complete
++++++++off=71 len=3 span[header_value]="def"
++++++++off=75 header_value complete
++++++++off=76 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=78 chunk header len=1
++++++++off=78 len=1 span[body]="A"
++++++++off=80 chunk complete
++++++++off=82 len=3 span[chunk_extension_name]="abc"
++++++++off=85 chunk_extension_name complete
++++++++off=86 chunk header len=1
++++++++off=86 len=1 span[body]="B"
++++++++off=88 chunk complete
++++++++off=90 len=3 span[chunk_extension_name]="def"
++++++++off=94 chunk_extension_name complete
++++++++off=94 len=3 span[chunk_extension_value]="ghi"
++++++++off=97 chunk_extension_value complete
++++++++off=98 chunk header len=1
++++++++off=98 len=1 span[body]="C"
++++++++off=100 chunk complete
++++++++off=102 len=3 span[chunk_extension_name]="jkl"
++++++++off=106 chunk_extension_name complete
++++++++off=106 len=5 span[chunk_extension_value]=""mno""
++++++++off=111 chunk_extension_value complete
++++++++off=112 chunk header len=1
++++++++off=112 len=1 span[body]="D"
++++++++off=114 chunk complete
++++++++off=117 chunk header len=0
++++++++off=117 len=3 span[header_field]="Baz"
++++++++off=121 header_field complete
++++++++off=122 len=3 span[header_value]="ghi"
++++++++off=126 header_value complete
++++++++off=127 chunk complete
++++++++off=127 message complete
++++++++```
++++++++
++++++++### Spaces before headers
++++++++
++++++++<!-- meta={ "type": "request" } -->
++++++++
++++++++```http
++++++++POST /hello HTTP/1.1
++++++++Host: localhost
++++++++Foo: bar
++++++++ Content-Length: 38
++++++++
++++++++GET /bye HTTP/1.1
++++++++Host: localhost
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=6 span[url]="/hello"
++++++++off=12 url complete
++++++++off=17 len=3 span[version]="1.1"
++++++++off=20 version complete
++++++++off=22 len=4 span[header_field]="Host"
++++++++off=27 header_field complete
++++++++off=28 len=9 span[header_value]="localhost"
++++++++off=39 header_value complete
++++++++off=39 len=3 span[header_field]="Foo"
++++++++off=43 header_field complete
++++++++off=44 len=3 span[header_value]="bar"
++++++++off=49 error code=10 reason="Unexpected whitespace after header value"
++++++++```
++++++++
++++++++### Spaces before headers (lenient)
++++++++
++++++++<!-- meta={ "type": "request-lenient-headers" } -->
++++++++
++++++++```http
++++++++POST /hello HTTP/1.1
++++++++Host: localhost
++++++++Foo: bar
++++++++ Content-Length: 38
++++++++
++++++++GET /bye HTTP/1.1
++++++++Host: localhost
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=6 span[url]="/hello"
++++++++off=12 url complete
++++++++off=17 len=3 span[version]="1.1"
++++++++off=20 version complete
++++++++off=22 len=4 span[header_field]="Host"
++++++++off=27 header_field complete
++++++++off=28 len=9 span[header_value]="localhost"
++++++++off=39 header_value complete
++++++++off=39 len=3 span[header_field]="Foo"
++++++++off=43 header_field complete
++++++++off=44 len=3 span[header_value]="bar"
++++++++off=49 len=19 span[header_value]=" Content-Length: 38"
++++++++off=70 header_value complete
++++++++off=72 headers complete method=3 v=1/1 flags=0 content_length=0
++++++++off=72 message complete
++++++++off=72 reset
++++++++off=72 message begin
++++++++off=72 len=3 span[method]="GET"
++++++++off=75 method complete
++++++++off=76 len=4 span[url]="/bye"
++++++++off=81 url complete
++++++++off=86 len=3 span[version]="1.1"
++++++++off=89 version complete
++++++++off=91 len=4 span[header_field]="Host"
++++++++off=96 header_field complete
++++++++off=97 len=9 span[header_value]="localhost"
++++++++off=108 header_value complete
++++++++off=110 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=110 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Lenient header value parsing
++++++++============================
++++++++
++++++++Parsing with header value token checks off.
++++++++
++++++++## Header value (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET /url HTTP/1.1
++++++++Header1: \f
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=7 span[header_field]="Header1"
++++++++off=27 header_field complete
++++++++off=28 len=1 span[header_value]="\f"
++++++++off=31 header_value complete
++++++++off=33 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=33 message complete
++++++++```
++++++++
++++++++## Second request header value (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET /url HTTP/1.1
++++++++Header1: Okay
++++++++
++++++++
++++++++GET /url HTTP/1.1
++++++++Header1: \f
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=7 span[header_field]="Header1"
++++++++off=27 header_field complete
++++++++off=28 len=4 span[header_value]="Okay"
++++++++off=34 header_value complete
++++++++off=36 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=36 message complete
++++++++off=38 reset
++++++++off=38 message begin
++++++++off=38 len=3 span[method]="GET"
++++++++off=41 method complete
++++++++off=42 len=4 span[url]="/url"
++++++++off=47 url complete
++++++++off=52 len=3 span[version]="1.1"
++++++++off=55 version complete
++++++++off=57 len=7 span[header_field]="Header1"
++++++++off=65 header_field complete
++++++++off=66 len=1 span[header_value]="\f"
++++++++off=69 header_value complete
++++++++off=71 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=71 message complete
++++++++```
++++++++
++++++++## Header value
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /url HTTP/1.1
++++++++Header1: \f
++++++++
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=7 span[header_field]="Header1"
++++++++off=27 header_field complete
++++++++off=28 len=0 span[header_value]=""
++++++++off=28 error code=10 reason="Invalid header value char"
++++++++```
++++++++
++++++++### Empty headers separated by CR (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Connection: Close
++++++++Host: localhost:5000
++++++++x:\rTransfer-Encoding: chunked
++++++++
++++++++1
++++++++A
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=5 span[header_value]="Close"
++++++++off=36 header_value complete
++++++++off=36 len=4 span[header_field]="Host"
++++++++off=41 header_field complete
++++++++off=42 len=14 span[header_value]="localhost:5000"
++++++++off=58 header_value complete
++++++++off=58 len=1 span[header_field]="x"
++++++++off=60 header_field complete
++++++++off=61 len=0 span[header_value]=""
++++++++off=61 header_value complete
++++++++off=61 len=17 span[header_field]="Transfer-Encoding"
++++++++off=79 header_field complete
++++++++off=80 len=7 span[header_value]="chunked"
++++++++off=89 header_value complete
++++++++off=91 headers complete method=3 v=1/1 flags=20a content_length=0
++++++++off=94 chunk header len=1
++++++++off=94 len=1 span[body]="A"
++++++++off=97 chunk complete
++++++++off=100 chunk header len=0
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Lenient HTTP version parsing
++++++++============================
++++++++
++++++++### Invalid HTTP version (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-version"} -->
++++++++```http
++++++++GET / HTTP/5.6
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="5.6"
++++++++off=14 version complete
++++++++off=18 headers complete method=1 v=5/6 flags=0 content_length=0
++++++++off=18 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Methods
++++++++=======
++++++++
++++++++### REPORT request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++REPORT /test HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=6 span[method]="REPORT"
++++++++off=6 method complete
++++++++off=7 len=5 span[url]="/test"
++++++++off=13 url complete
++++++++off=18 len=3 span[version]="1.1"
++++++++off=21 version complete
++++++++off=25 headers complete method=20 v=1/1 flags=0 content_length=0
++++++++off=25 message complete
++++++++```
++++++++
++++++++### CONNECT request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++CONNECT 0-home0.netscape.com:443 HTTP/1.0
++++++++User-agent: Mozilla/1.1N
++++++++Proxy-authorization: basic aGVsbG86d29ybGQ=
++++++++
++++++++some data
++++++++and yet even more data
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="CONNECT"
++++++++off=7 method complete
++++++++off=8 len=24 span[url]="0-home0.netscape.com:443"
++++++++off=33 url complete
++++++++off=38 len=3 span[version]="1.0"
++++++++off=41 version complete
++++++++off=43 len=10 span[header_field]="User-agent"
++++++++off=54 header_field complete
++++++++off=55 len=12 span[header_value]="Mozilla/1.1N"
++++++++off=69 header_value complete
++++++++off=69 len=19 span[header_field]="Proxy-authorization"
++++++++off=89 header_field complete
++++++++off=90 len=22 span[header_value]="basic aGVsbG86d29ybGQ="
++++++++off=114 header_value complete
++++++++off=116 headers complete method=5 v=1/0 flags=0 content_length=0
++++++++off=116 message complete
++++++++off=116 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### CONNECT request with CAPS
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0
++++++++User-agent: Mozilla/1.1N
++++++++Proxy-authorization: basic aGVsbG86d29ybGQ=
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="CONNECT"
++++++++off=7 method complete
++++++++off=8 len=22 span[url]="HOME0.NETSCAPE.COM:443"
++++++++off=31 url complete
++++++++off=36 len=3 span[version]="1.0"
++++++++off=39 version complete
++++++++off=41 len=10 span[header_field]="User-agent"
++++++++off=52 header_field complete
++++++++off=53 len=12 span[header_value]="Mozilla/1.1N"
++++++++off=67 header_value complete
++++++++off=67 len=19 span[header_field]="Proxy-authorization"
++++++++off=87 header_field complete
++++++++off=88 len=22 span[header_value]="basic aGVsbG86d29ybGQ="
++++++++off=112 header_value complete
++++++++off=114 headers complete method=5 v=1/0 flags=0 content_length=0
++++++++off=114 message complete
++++++++off=114 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### CONNECT with body
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++CONNECT foo.bar.com:443 HTTP/1.0
++++++++User-agent: Mozilla/1.1N
++++++++Proxy-authorization: basic aGVsbG86d29ybGQ=
++++++++Content-Length: 10
++++++++
++++++++blarfcicle"
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="CONNECT"
++++++++off=7 method complete
++++++++off=8 len=15 span[url]="foo.bar.com:443"
++++++++off=24 url complete
++++++++off=29 len=3 span[version]="1.0"
++++++++off=32 version complete
++++++++off=34 len=10 span[header_field]="User-agent"
++++++++off=45 header_field complete
++++++++off=46 len=12 span[header_value]="Mozilla/1.1N"
++++++++off=60 header_value complete
++++++++off=60 len=19 span[header_field]="Proxy-authorization"
++++++++off=80 header_field complete
++++++++off=81 len=22 span[header_value]="basic aGVsbG86d29ybGQ="
++++++++off=105 header_value complete
++++++++off=105 len=14 span[header_field]="Content-Length"
++++++++off=120 header_field complete
++++++++off=121 len=2 span[header_value]="10"
++++++++off=125 header_value complete
++++++++off=127 headers complete method=5 v=1/0 flags=20 content_length=10
++++++++off=127 message complete
++++++++off=127 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++### M-SEARCH request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++M-SEARCH * HTTP/1.1
++++++++HOST: 239.255.255.250:1900
++++++++MAN: "ssdp:discover"
++++++++ST: "ssdp:all"
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=8 span[method]="M-SEARCH"
++++++++off=8 method complete
++++++++off=9 len=1 span[url]="*"
++++++++off=11 url complete
++++++++off=16 len=3 span[version]="1.1"
++++++++off=19 version complete
++++++++off=21 len=4 span[header_field]="HOST"
++++++++off=26 header_field complete
++++++++off=27 len=20 span[header_value]="239.255.255.250:1900"
++++++++off=49 header_value complete
++++++++off=49 len=3 span[header_field]="MAN"
++++++++off=53 header_field complete
++++++++off=54 len=15 span[header_value]=""ssdp:discover""
++++++++off=71 header_value complete
++++++++off=71 len=2 span[header_field]="ST"
++++++++off=74 header_field complete
++++++++off=75 len=10 span[header_value]=""ssdp:all""
++++++++off=87 header_value complete
++++++++off=89 headers complete method=24 v=1/1 flags=0 content_length=0
++++++++off=89 message complete
++++++++```
++++++++
++++++++### PATCH request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PATCH /file.txt HTTP/1.1
++++++++Host: www.example.com
++++++++Content-Type: application/example
++++++++If-Match: "e0023aa4e"
++++++++Content-Length: 10
++++++++
++++++++cccccccccc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=5 span[method]="PATCH"
++++++++off=5 method complete
++++++++off=6 len=9 span[url]="/file.txt"
++++++++off=16 url complete
++++++++off=21 len=3 span[version]="1.1"
++++++++off=24 version complete
++++++++off=26 len=4 span[header_field]="Host"
++++++++off=31 header_field complete
++++++++off=32 len=15 span[header_value]="www.example.com"
++++++++off=49 header_value complete
++++++++off=49 len=12 span[header_field]="Content-Type"
++++++++off=62 header_field complete
++++++++off=63 len=19 span[header_value]="application/example"
++++++++off=84 header_value complete
++++++++off=84 len=8 span[header_field]="If-Match"
++++++++off=93 header_field complete
++++++++off=94 len=11 span[header_value]=""e0023aa4e""
++++++++off=107 header_value complete
++++++++off=107 len=14 span[header_field]="Content-Length"
++++++++off=122 header_field complete
++++++++off=123 len=2 span[header_value]="10"
++++++++off=127 header_value complete
++++++++off=129 headers complete method=28 v=1/1 flags=20 content_length=10
++++++++off=129 len=10 span[body]="cccccccccc"
++++++++off=139 message complete
++++++++```
++++++++
++++++++### PURGE request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PURGE /file.txt HTTP/1.1
++++++++Host: www.example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=5 span[method]="PURGE"
++++++++off=5 method complete
++++++++off=6 len=9 span[url]="/file.txt"
++++++++off=16 url complete
++++++++off=21 len=3 span[version]="1.1"
++++++++off=24 version complete
++++++++off=26 len=4 span[header_field]="Host"
++++++++off=31 header_field complete
++++++++off=32 len=15 span[header_value]="www.example.com"
++++++++off=49 header_value complete
++++++++off=51 headers complete method=29 v=1/1 flags=0 content_length=0
++++++++off=51 message complete
++++++++```
++++++++
++++++++### SEARCH request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++SEARCH / HTTP/1.1
++++++++Host: www.example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=6 span[method]="SEARCH"
++++++++off=6 method complete
++++++++off=7 len=1 span[url]="/"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=4 span[header_field]="Host"
++++++++off=24 header_field complete
++++++++off=25 len=15 span[header_value]="www.example.com"
++++++++off=42 header_value complete
++++++++off=44 headers complete method=14 v=1/1 flags=0 content_length=0
++++++++off=44 message complete
++++++++```
++++++++
++++++++### LINK request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++LINK /images/my_dog.jpg HTTP/1.1
++++++++Host: example.com
++++++++Link: <http://example.com/profiles/joe>; rel="tag"
++++++++Link: <http://example.com/profiles/sally>; rel="tag"
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="LINK"
++++++++off=4 method complete
++++++++off=5 len=18 span[url]="/images/my_dog.jpg"
++++++++off=24 url complete
++++++++off=29 len=3 span[version]="1.1"
++++++++off=32 version complete
++++++++off=34 len=4 span[header_field]="Host"
++++++++off=39 header_field complete
++++++++off=40 len=11 span[header_value]="example.com"
++++++++off=53 header_value complete
++++++++off=53 len=4 span[header_field]="Link"
++++++++off=58 header_field complete
++++++++off=59 len=44 span[header_value]="<http://example.com/profiles/joe>; rel="tag""
++++++++off=105 header_value complete
++++++++off=105 len=4 span[header_field]="Link"
++++++++off=110 header_field complete
++++++++off=111 len=46 span[header_value]="<http://example.com/profiles/sally>; rel="tag""
++++++++off=159 header_value complete
++++++++off=161 headers complete method=31 v=1/1 flags=0 content_length=0
++++++++off=161 message complete
++++++++```
++++++++
++++++++### LINK request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++UNLINK /images/my_dog.jpg HTTP/1.1
++++++++Host: example.com
++++++++Link: <http://example.com/profiles/sally>; rel="tag"
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=6 span[method]="UNLINK"
++++++++off=6 method complete
++++++++off=7 len=18 span[url]="/images/my_dog.jpg"
++++++++off=26 url complete
++++++++off=31 len=3 span[version]="1.1"
++++++++off=34 version complete
++++++++off=36 len=4 span[header_field]="Host"
++++++++off=41 header_field complete
++++++++off=42 len=11 span[header_value]="example.com"
++++++++off=55 header_value complete
++++++++off=55 len=4 span[header_field]="Link"
++++++++off=60 header_field complete
++++++++off=61 len=46 span[header_value]="<http://example.com/profiles/sally>; rel="tag""
++++++++off=109 header_value complete
++++++++off=111 headers complete method=32 v=1/1 flags=0 content_length=0
++++++++off=111 message complete
++++++++```
++++++++
++++++++### SOURCE request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++SOURCE /music/sweet/music HTTP/1.1
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=6 span[method]="SOURCE"
++++++++off=6 method complete
++++++++off=7 len=18 span[url]="/music/sweet/music"
++++++++off=26 url complete
++++++++off=31 len=3 span[version]="1.1"
++++++++off=34 version complete
++++++++off=36 len=4 span[header_field]="Host"
++++++++off=41 header_field complete
++++++++off=42 len=11 span[header_value]="example.com"
++++++++off=55 header_value complete
++++++++off=57 headers complete method=33 v=1/1 flags=0 content_length=0
++++++++off=57 message complete
++++++++```
++++++++
++++++++### SOURCE request with ICE
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++SOURCE /music/sweet/music ICE/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=6 span[method]="SOURCE"
++++++++off=6 method complete
++++++++off=7 len=18 span[url]="/music/sweet/music"
++++++++off=26 url complete
++++++++off=30 len=3 span[version]="1.0"
++++++++off=33 version complete
++++++++off=35 len=4 span[header_field]="Host"
++++++++off=40 header_field complete
++++++++off=41 len=11 span[header_value]="example.com"
++++++++off=54 header_value complete
++++++++off=56 headers complete method=33 v=1/0 flags=0 content_length=0
++++++++off=56 message complete
++++++++```
++++++++
++++++++### OPTIONS request with RTSP
++++++++
++++++++NOTE: `OPTIONS` is a valid HTTP metho too.
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++OPTIONS /music/sweet/music RTSP/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="OPTIONS"
++++++++off=7 method complete
++++++++off=8 len=18 span[url]="/music/sweet/music"
++++++++off=27 url complete
++++++++off=32 len=3 span[version]="1.0"
++++++++off=35 version complete
++++++++off=37 len=4 span[header_field]="Host"
++++++++off=42 header_field complete
++++++++off=43 len=11 span[header_value]="example.com"
++++++++off=56 header_value complete
++++++++off=58 headers complete method=6 v=1/0 flags=0 content_length=0
++++++++off=58 message complete
++++++++```
++++++++
++++++++### ANNOUNCE request with RTSP
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++ANNOUNCE /music/sweet/music RTSP/1.0
++++++++Host: example.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=8 span[method]="ANNOUNCE"
++++++++off=8 method complete
++++++++off=9 len=18 span[url]="/music/sweet/music"
++++++++off=28 url complete
++++++++off=33 len=3 span[version]="1.0"
++++++++off=36 version complete
++++++++off=38 len=4 span[header_field]="Host"
++++++++off=43 header_field complete
++++++++off=44 len=11 span[header_value]="example.com"
++++++++off=57 header_value complete
++++++++off=59 headers complete method=36 v=1/0 flags=0 content_length=0
++++++++off=59 message complete
++++++++```
++++++++
++++++++### PRI request HTTP2
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PRI * HTTP/1.1
++++++++
++++++++SM
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PRI"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="*"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=24 error code=23 reason="Pause on PRI/Upgrade"
++++++++```
++++++++
++++++++### QUERY request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++QUERY /contacts HTTP/1.1
++++++++Host: example.org
++++++++Content-Type: example/query
++++++++Accept: text/csv
++++++++Content-Length: 41
++++++++
++++++++select surname, givenname, email limit 10
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=5 span[method]="QUERY"
++++++++off=5 method complete
++++++++off=6 len=9 span[url]="/contacts"
++++++++off=16 url complete
++++++++off=21 len=3 span[version]="1.1"
++++++++off=24 version complete
++++++++off=26 len=4 span[header_field]="Host"
++++++++off=31 header_field complete
++++++++off=32 len=11 span[header_value]="example.org"
++++++++off=45 header_value complete
++++++++off=45 len=12 span[header_field]="Content-Type"
++++++++off=58 header_field complete
++++++++off=59 len=13 span[header_value]="example/query"
++++++++off=74 header_value complete
++++++++off=74 len=6 span[header_field]="Accept"
++++++++off=81 header_field complete
++++++++off=82 len=8 span[header_value]="text/csv"
++++++++off=92 header_value complete
++++++++off=92 len=14 span[header_field]="Content-Length"
++++++++off=107 header_field complete
++++++++off=108 len=2 span[header_value]="41"
++++++++off=112 header_value complete
++++++++off=114 headers complete method=46 v=1/1 flags=20 content_length=41
++++++++off=114 len=41 span[body]="select surname, givenname, email limit 10"
++++++++off=155 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Pausing
++++++++=======
++++++++
++++++++### on_message_begin
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_message_begin"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 pause
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_message_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_message_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++off=41 pause
++++++++```
++++++++
++++++++### on_method_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_method_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=4 pause
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_url_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_url_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=7 pause
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_version_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_version_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=15 pause
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_header_field_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_header_field_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=32 pause
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_header_value_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_header_value_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=36 pause
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_headers_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_headers_complete"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=38 pause
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_chunk_header
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_chunk_header"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=17 span[header_field]="Transfer-Encoding"
++++++++off=34 header_field complete
++++++++off=35 len=7 span[header_value]="chunked"
++++++++off=44 header_value complete
++++++++off=46 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=49 chunk header len=10
++++++++off=49 pause
++++++++off=49 len=10 span[body]="0123456789"
++++++++off=61 chunk complete
++++++++off=64 chunk header len=0
++++++++off=64 pause
++++++++off=66 chunk complete
++++++++off=66 message complete
++++++++```
++++++++
++++++++### on_chunk_extension_name
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_chunk_extension_name"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a;foo=bar
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=17 span[header_field]="Transfer-Encoding"
++++++++off=34 header_field complete
++++++++off=35 len=7 span[header_value]="chunked"
++++++++off=44 header_value complete
++++++++off=46 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=48 len=3 span[chunk_extension_name]="foo"
++++++++off=52 chunk_extension_name complete
++++++++off=52 pause
++++++++off=52 len=3 span[chunk_extension_value]="bar"
++++++++off=56 chunk_extension_value complete
++++++++off=57 chunk header len=10
++++++++off=57 len=10 span[body]="0123456789"
++++++++off=69 chunk complete
++++++++off=72 chunk header len=0
++++++++off=74 chunk complete
++++++++off=74 message complete
++++++++```
++++++++
++++++++### on_chunk_extension_value
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_chunk_extension_value"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a;foo=bar
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=17 span[header_field]="Transfer-Encoding"
++++++++off=34 header_field complete
++++++++off=35 len=7 span[header_value]="chunked"
++++++++off=44 header_value complete
++++++++off=46 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=48 len=3 span[chunk_extension_name]="foo"
++++++++off=52 chunk_extension_name complete
++++++++off=52 len=3 span[chunk_extension_value]="bar"
++++++++off=56 chunk_extension_value complete
++++++++off=56 pause
++++++++off=57 chunk header len=10
++++++++off=57 len=10 span[body]="0123456789"
++++++++off=69 chunk complete
++++++++off=72 chunk header len=0
++++++++off=74 chunk complete
++++++++off=74 message complete
++++++++```
++++++++
++++++++
++++++++### on_chunk_complete
++++++++
++++++++<!-- meta={"type": "request", "pause": "on_chunk_complete"} -->
++++++++```http
++++++++PUT / HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=17 span[header_field]="Transfer-Encoding"
++++++++off=34 header_field complete
++++++++off=35 len=7 span[header_value]="chunked"
++++++++off=44 header_value complete
++++++++off=46 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=49 chunk header len=10
++++++++off=49 len=10 span[body]="0123456789"
++++++++off=61 chunk complete
++++++++off=61 pause
++++++++off=64 chunk header len=0
++++++++off=66 chunk complete
++++++++off=66 pause
++++++++off=66 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Pipelining
++++++++==========
++++++++
++++++++## Should parse multiple events
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /aaa HTTP/1.1
++++++++Content-Length: 3
++++++++
++++++++AAA
++++++++PUT /bbb HTTP/1.1
++++++++Content-Length: 4
++++++++
++++++++BBBB
++++++++PATCH /ccc HTTP/1.1
++++++++Content-Length: 5
++++++++
++++++++CCCC
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=4 span[url]="/aaa"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=14 span[header_field]="Content-Length"
++++++++off=35 header_field complete
++++++++off=36 len=1 span[header_value]="3"
++++++++off=39 header_value complete
++++++++off=41 headers complete method=3 v=1/1 flags=20 content_length=3
++++++++off=41 len=3 span[body]="AAA"
++++++++off=44 message complete
++++++++off=46 reset
++++++++off=46 message begin
++++++++off=46 len=3 span[method]="PUT"
++++++++off=49 method complete
++++++++off=50 len=4 span[url]="/bbb"
++++++++off=55 url complete
++++++++off=60 len=3 span[version]="1.1"
++++++++off=63 version complete
++++++++off=65 len=14 span[header_field]="Content-Length"
++++++++off=80 header_field complete
++++++++off=81 len=1 span[header_value]="4"
++++++++off=84 header_value complete
++++++++off=86 headers complete method=4 v=1/1 flags=20 content_length=4
++++++++off=86 len=4 span[body]="BBBB"
++++++++off=90 message complete
++++++++off=92 reset
++++++++off=92 message begin
++++++++off=92 len=5 span[method]="PATCH"
++++++++off=97 method complete
++++++++off=98 len=4 span[url]="/ccc"
++++++++off=103 url complete
++++++++off=108 len=3 span[version]="1.1"
++++++++off=111 version complete
++++++++off=113 len=14 span[header_field]="Content-Length"
++++++++off=128 header_field complete
++++++++off=129 len=1 span[header_value]="5"
++++++++off=132 header_value complete
++++++++off=134 headers complete method=28 v=1/1 flags=20 content_length=5
++++++++off=134 len=4 span[body]="CCCC"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Sample requests
++++++++===============
++++++++
++++++++Lots of sample requests, most ported from [http_parser][0] test suite.
++++++++
++++++++## Simple request
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++OPTIONS /url HTTP/1.1
++++++++Header1: Value1
++++++++Header2:\t Value2
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="OPTIONS"
++++++++off=7 method complete
++++++++off=8 len=4 span[url]="/url"
++++++++off=13 url complete
++++++++off=18 len=3 span[version]="1.1"
++++++++off=21 version complete
++++++++off=23 len=7 span[header_field]="Header1"
++++++++off=31 header_field complete
++++++++off=32 len=6 span[header_value]="Value1"
++++++++off=40 header_value complete
++++++++off=40 len=7 span[header_field]="Header2"
++++++++off=48 header_field complete
++++++++off=50 len=6 span[header_value]="Value2"
++++++++off=58 header_value complete
++++++++off=60 headers complete method=6 v=1/1 flags=0 content_length=0
++++++++off=60 message complete
++++++++```
++++++++
++++++++## Request with method starting with `H`
++++++++
++++++++There's a optimization in `start_req_or_res` that passes execution to
++++++++`start_req` when the first character is not `H` (because response must start
++++++++with `HTTP/`). However, there're still methods like `HEAD` that should get
++++++++to `start_req`. Verify that it still works after optimization.
++++++++
++++++++<!-- meta={"type": "request", "noScan": true } -->
++++++++```http
++++++++HEAD /url HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="HEAD"
++++++++off=4 method complete
++++++++off=5 len=4 span[url]="/url"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=22 headers complete method=2 v=1/1 flags=0 content_length=0
++++++++off=22 message complete
++++++++```
++++++++
++++++++## curl GET
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /test HTTP/1.1
++++++++User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1
++++++++Host: 0.0.0.0=5000
++++++++Accept: */*
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/test"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.1"
++++++++off=18 version complete
++++++++off=20 len=10 span[header_field]="User-Agent"
++++++++off=31 header_field complete
++++++++off=32 len=85 span[header_value]="curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1"
++++++++off=119 header_value complete
++++++++off=119 len=4 span[header_field]="Host"
++++++++off=124 header_field complete
++++++++off=125 len=12 span[header_value]="0.0.0.0=5000"
++++++++off=139 header_value complete
++++++++off=139 len=6 span[header_field]="Accept"
++++++++off=146 header_field complete
++++++++off=147 len=3 span[header_value]="*/*"
++++++++off=152 header_value complete
++++++++off=154 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=154 message complete
++++++++```
++++++++
++++++++## Firefox GET
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /favicon.ico HTTP/1.1
++++++++Host: 0.0.0.0=5000
++++++++User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0
++++++++Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
++++++++Accept-Language: en-us,en;q=0.5
++++++++Accept-Encoding: gzip,deflate
++++++++Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
++++++++Keep-Alive: 300
++++++++Connection: keep-alive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=12 span[url]="/favicon.ico"
++++++++off=17 url complete
++++++++off=22 len=3 span[version]="1.1"
++++++++off=25 version complete
++++++++off=27 len=4 span[header_field]="Host"
++++++++off=32 header_field complete
++++++++off=33 len=12 span[header_value]="0.0.0.0=5000"
++++++++off=47 header_value complete
++++++++off=47 len=10 span[header_field]="User-Agent"
++++++++off=58 header_field complete
++++++++off=59 len=76 span[header_value]="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0"
++++++++off=137 header_value complete
++++++++off=137 len=6 span[header_field]="Accept"
++++++++off=144 header_field complete
++++++++off=145 len=63 span[header_value]="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
++++++++off=210 header_value complete
++++++++off=210 len=15 span[header_field]="Accept-Language"
++++++++off=226 header_field complete
++++++++off=227 len=14 span[header_value]="en-us,en;q=0.5"
++++++++off=243 header_value complete
++++++++off=243 len=15 span[header_field]="Accept-Encoding"
++++++++off=259 header_field complete
++++++++off=260 len=12 span[header_value]="gzip,deflate"
++++++++off=274 header_value complete
++++++++off=274 len=14 span[header_field]="Accept-Charset"
++++++++off=289 header_field complete
++++++++off=290 len=30 span[header_value]="ISO-8859-1,utf-8;q=0.7,*;q=0.7"
++++++++off=322 header_value complete
++++++++off=322 len=10 span[header_field]="Keep-Alive"
++++++++off=333 header_field complete
++++++++off=334 len=3 span[header_value]="300"
++++++++off=339 header_value complete
++++++++off=339 len=10 span[header_field]="Connection"
++++++++off=350 header_field complete
++++++++off=351 len=10 span[header_value]="keep-alive"
++++++++off=363 header_value complete
++++++++off=365 headers complete method=1 v=1/1 flags=1 content_length=0
++++++++off=365 message complete
++++++++```
++++++++
++++++++## DUMBPACK
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /dumbpack HTTP/1.1
++++++++aaaaaaaaaaaaa:++++++++++
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=9 span[url]="/dumbpack"
++++++++off=14 url complete
++++++++off=19 len=3 span[version]="1.1"
++++++++off=22 version complete
++++++++off=24 len=13 span[header_field]="aaaaaaaaaaaaa"
++++++++off=38 header_field complete
++++++++off=38 len=10 span[header_value]="++++++++++"
++++++++off=50 header_value complete
++++++++off=52 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=52 message complete
++++++++```
++++++++
++++++++## No headers and no body
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /get_no_headers_no_body/world HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=29 span[url]="/get_no_headers_no_body/world"
++++++++off=34 url complete
++++++++off=39 len=3 span[version]="1.1"
++++++++off=42 version complete
++++++++off=46 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=46 message complete
++++++++```
++++++++
++++++++## One header and no body
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /get_one_header_no_body HTTP/1.1
++++++++Accept: */*
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=23 span[url]="/get_one_header_no_body"
++++++++off=28 url complete
++++++++off=33 len=3 span[version]="1.1"
++++++++off=36 version complete
++++++++off=38 len=6 span[header_field]="Accept"
++++++++off=45 header_field complete
++++++++off=46 len=3 span[header_value]="*/*"
++++++++off=51 header_value complete
++++++++off=53 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=53 message complete
++++++++```
++++++++
++++++++## Apache bench GET
++++++++
++++++++The server receiving this request SHOULD NOT wait for EOF to know that
++++++++`Content-Length == 0`.
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /test HTTP/1.0
++++++++Host: 0.0.0.0:5000
++++++++User-Agent: ApacheBench/2.3
++++++++Accept: */*
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=5 span[url]="/test"
++++++++off=10 url complete
++++++++off=15 len=3 span[version]="1.0"
++++++++off=18 version complete
++++++++off=20 len=4 span[header_field]="Host"
++++++++off=25 header_field complete
++++++++off=26 len=12 span[header_value]="0.0.0.0:5000"
++++++++off=40 header_value complete
++++++++off=40 len=10 span[header_field]="User-Agent"
++++++++off=51 header_field complete
++++++++off=52 len=15 span[header_value]="ApacheBench/2.3"
++++++++off=69 header_value complete
++++++++off=69 len=6 span[header_field]="Accept"
++++++++off=76 header_field complete
++++++++off=77 len=3 span[header_value]="*/*"
++++++++off=82 header_value complete
++++++++off=84 headers complete method=1 v=1/0 flags=0 content_length=0
++++++++off=84 message complete
++++++++```
++++++++
++++++++## Prefix newline
++++++++
++++++++Some clients, especially after a POST in a keep-alive connection,
++++++++will send an extra CRLF before the next request.
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++\r\nGET /test HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=2 message begin
++++++++off=2 len=3 span[method]="GET"
++++++++off=5 method complete
++++++++off=6 len=5 span[url]="/test"
++++++++off=12 url complete
++++++++off=17 len=3 span[version]="1.1"
++++++++off=20 version complete
++++++++off=24 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=24 message complete
++++++++```
++++++++
++++++++## No HTTP version
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=9 headers complete method=1 v=0/9 flags=0 content_length=0
++++++++off=9 message complete
++++++++```
++++++++
++++++++## Line folding in header value with CRLF
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Line1: abc
++++++++\tdef
++++++++ ghi
++++++++\t\tjkl
++++++++ mno
++++++++\t \tqrs
++++++++Line2: \t line2\t
++++++++Line3:
++++++++ line3
++++++++Line4:
++++++++
++++++++Connection:
++++++++ close
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=5 span[header_field]="Line1"
++++++++off=22 header_field complete
++++++++off=25 len=3 span[header_value]="abc"
++++++++off=30 len=4 span[header_value]="\tdef"
++++++++off=36 len=4 span[header_value]=" ghi"
++++++++off=42 len=5 span[header_value]="\t\tjkl"
++++++++off=49 len=6 span[header_value]=" mno "
++++++++off=57 len=6 span[header_value]="\t \tqrs"
++++++++off=65 header_value complete
++++++++off=65 len=5 span[header_field]="Line2"
++++++++off=71 header_field complete
++++++++off=74 len=6 span[header_value]="line2\t"
++++++++off=82 header_value complete
++++++++off=82 len=5 span[header_field]="Line3"
++++++++off=88 header_field complete
++++++++off=91 len=5 span[header_value]="line3"
++++++++off=98 header_value complete
++++++++off=98 len=5 span[header_field]="Line4"
++++++++off=104 header_field complete
++++++++off=110 len=0 span[header_value]=""
++++++++off=110 header_value complete
++++++++off=110 len=10 span[header_field]="Connection"
++++++++off=121 header_field complete
++++++++off=124 len=5 span[header_value]="close"
++++++++off=131 header_value complete
++++++++off=133 headers complete method=1 v=1/1 flags=2 content_length=0
++++++++off=133 message complete
++++++++```
++++++++
++++++++## Line folding in header value with LF
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Line1: abc\n\
++++++++\tdef\n\
++++++++ ghi\n\
++++++++\t\tjkl\n\
++++++++ mno \n\
++++++++\t \tqrs\n\
++++++++Line2: \t line2\t\n\
++++++++Line3:\n\
++++++++ line3\n\
++++++++Line4: \n\
++++++++ \n\
++++++++Connection:\n\
++++++++ close\n\
++++++++\n
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=5 span[header_field]="Line1"
++++++++off=22 header_field complete
++++++++off=25 len=3 span[header_value]="abc"
++++++++off=28 error code=25 reason="Missing expected CR after header value"
++++++++```
++++++++
++++++++## No LF after CR
++++++++
++++++++<!-- meta={"type":"request"} -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1\rLine: 1
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=15 error code=2 reason="Expected CRLF after version"
++++++++```
++++++++
++++++++## No LF after CR (lenient)
++++++++
++++++++<!-- meta={"type":"request-lenient-optional-lf-after-cr"} -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1\rLine: 1
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=15 len=4 span[header_field]="Line"
++++++++off=20 header_field complete
++++++++off=21 len=1 span[header_value]="1"
++++++++```
++++++++
++++++++## Request starting with CRLF
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++\r\nGET /url HTTP/1.1
++++++++Header1: Value1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=2 message begin
++++++++off=2 len=3 span[method]="GET"
++++++++off=5 method complete
++++++++off=6 len=4 span[url]="/url"
++++++++off=11 url complete
++++++++off=16 len=3 span[version]="1.1"
++++++++off=19 version complete
++++++++off=21 len=7 span[header_field]="Header1"
++++++++off=29 header_field complete
++++++++off=30 len=6 span[header_value]="Value1"
++++++++off=38 header_value complete
++++++++off=40 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=40 message complete
++++++++```
++++++++
++++++++## Extended Characters
++++++++
++++++++See nodejs/test/parallel/test-http-headers-obstext.js
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++Test: Düsseldorf
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Test"
++++++++off=21 header_field complete
++++++++off=22 len=11 span[header_value]="Düsseldorf"
++++++++off=35 header_value complete
++++++++off=37 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=37 message complete
++++++++```
++++++++
++++++++## 255 ASCII in header value
++++++++
++++++++Note: `Buffer.from([ 0xff ]).toString('latin1') === 'ÿ'`.
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++OPTIONS /url HTTP/1.1
++++++++Header1: Value1
++++++++Header2: \xffValue2
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="OPTIONS"
++++++++off=7 method complete
++++++++off=8 len=4 span[url]="/url"
++++++++off=13 url complete
++++++++off=18 len=3 span[version]="1.1"
++++++++off=21 version complete
++++++++off=23 len=7 span[header_field]="Header1"
++++++++off=31 header_field complete
++++++++off=32 len=6 span[header_value]="Value1"
++++++++off=40 header_value complete
++++++++off=40 len=7 span[header_field]="Header2"
++++++++off=48 header_field complete
++++++++off=49 len=8 span[header_value]="ÿValue2"
++++++++off=59 header_value complete
++++++++off=61 headers complete method=6 v=1/1 flags=0 content_length=0
++++++++off=61 message complete
++++++++```
++++++++
++++++++## X-SSL-Nonsense
++++++++
++++++++See nodejs/test/parallel/test-http-headers-obstext.js
++++++++
++++++++<!-- meta={"type": "request-lenient-headers"} -->
++++++++```http
++++++++GET / HTTP/1.1
++++++++X-SSL-Nonsense: -----BEGIN CERTIFICATE-----
++++++++\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx
++++++++\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT
++++++++\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu
++++++++\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV
++++++++\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV
++++++++\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB
++++++++\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF
++++++++\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR
++++++++\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL
++++++++\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP
++++++++\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR
++++++++\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG
++++++++\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs
++++++++\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD
++++++++\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj
++++++++\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj
++++++++\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG
++++++++\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE
++++++++\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO
++++++++\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1
++++++++\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0
++++++++\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD
++++++++\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv
++++++++\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3
++++++++\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8
++++++++\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk
++++++++\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK
++++++++\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu
++++++++\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3
++++++++\tRA==
++++++++\t-----END CERTIFICATE-----
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=14 span[header_field]="X-SSL-Nonsense"
++++++++off=31 header_field complete
++++++++off=34 len=27 span[header_value]="-----BEGIN CERTIFICATE-----"
++++++++off=63 len=65 span[header_value]="\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx"
++++++++off=130 len=65 span[header_value]="\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT"
++++++++off=197 len=65 span[header_value]="\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu"
++++++++off=264 len=65 span[header_value]="\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV"
++++++++off=331 len=65 span[header_value]="\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV"
++++++++off=398 len=65 span[header_value]="\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB"
++++++++off=465 len=65 span[header_value]="\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF"
++++++++off=532 len=65 span[header_value]="\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR"
++++++++off=599 len=65 span[header_value]="\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL"
++++++++off=666 len=65 span[header_value]="\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP"
++++++++off=733 len=65 span[header_value]="\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR"
++++++++off=800 len=65 span[header_value]="\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG"
++++++++off=867 len=66 span[header_value]="\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgHTTPAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs"
++++++++off=935 len=65 span[header_value]="\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD"
++++++++off=1002 len=65 span[header_value]="\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj"
++++++++off=1069 len=65 span[header_value]="\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj"
++++++++off=1136 len=65 span[header_value]="\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG"
++++++++off=1203 len=65 span[header_value]="\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE"
++++++++off=1270 len=65 span[header_value]="\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO"
++++++++off=1337 len=65 span[header_value]="\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1"
++++++++off=1404 len=75 span[header_value]="\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0"
++++++++off=1481 len=65 span[header_value]="\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD"
++++++++off=1548 len=55 span[header_value]="\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv"
++++++++off=1605 len=65 span[header_value]="\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3"
++++++++off=1672 len=65 span[header_value]="\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8"
++++++++off=1739 len=65 span[header_value]="\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk"
++++++++off=1806 len=65 span[header_value]="\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK"
++++++++off=1873 len=65 span[header_value]="\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu"
++++++++off=1940 len=65 span[header_value]="\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3"
++++++++off=2007 len=5 span[header_value]="\tRA=="
++++++++off=2014 len=26 span[header_value]="\t-----END CERTIFICATE-----"
++++++++off=2042 header_value complete
++++++++off=2044 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=2044 message complete
++++++++```
++++++++
++++++++[0]: https://github.com/nodejs/http-parser
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Transfer-Encoding header
++++++++========================
++++++++
++++++++## `chunked`
++++++++
++++++++### Parsing and setting flag
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++```
++++++++
++++++++### Parse chunks with lowercase size
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=52 chunk header len=10
++++++++off=52 len=10 span[body]="0123456789"
++++++++off=64 chunk complete
++++++++off=67 chunk header len=0
++++++++off=69 chunk complete
++++++++off=69 message complete
++++++++```
++++++++
++++++++### Parse chunks with uppercase size
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++A
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=52 chunk header len=10
++++++++off=52 len=10 span[body]="0123456789"
++++++++off=64 chunk complete
++++++++off=67 chunk header len=0
++++++++off=69 chunk complete
++++++++off=69 message complete
++++++++```
++++++++
++++++++### POST with `Transfer-Encoding: chunked`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /post_chunked_all_your_base HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++1e
++++++++all your base are belong to us
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=27 span[url]="/post_chunked_all_your_base"
++++++++off=33 url complete
++++++++off=38 len=3 span[version]="1.1"
++++++++off=41 version complete
++++++++off=43 len=17 span[header_field]="Transfer-Encoding"
++++++++off=61 header_field complete
++++++++off=62 len=7 span[header_value]="chunked"
++++++++off=71 header_value complete
++++++++off=73 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=77 chunk header len=30
++++++++off=77 len=30 span[body]="all your base are belong to us"
++++++++off=109 chunk complete
++++++++off=112 chunk header len=0
++++++++off=114 chunk complete
++++++++off=114 message complete
++++++++```
++++++++
++++++++### Two chunks and triple zero prefixed end chunk
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /two_chunks_mult_zero_end HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++hello
++++++++6
++++++++ world
++++++++000
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=25 span[url]="/two_chunks_mult_zero_end"
++++++++off=31 url complete
++++++++off=36 len=3 span[version]="1.1"
++++++++off=39 version complete
++++++++off=41 len=17 span[header_field]="Transfer-Encoding"
++++++++off=59 header_field complete
++++++++off=60 len=7 span[header_value]="chunked"
++++++++off=69 header_value complete
++++++++off=71 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=74 chunk header len=5
++++++++off=74 len=5 span[body]="hello"
++++++++off=81 chunk complete
++++++++off=84 chunk header len=6
++++++++off=84 len=6 span[body]=" world"
++++++++off=92 chunk complete
++++++++off=97 chunk header len=0
++++++++off=99 chunk complete
++++++++off=99 message complete
++++++++```
++++++++
++++++++### Trailing headers
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_trailing_headers HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++hello
++++++++6
++++++++ world
++++++++0
++++++++Vary: *
++++++++Content-Type: text/plain
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=27 span[url]="/chunked_w_trailing_headers"
++++++++off=33 url complete
++++++++off=38 len=3 span[version]="1.1"
++++++++off=41 version complete
++++++++off=43 len=17 span[header_field]="Transfer-Encoding"
++++++++off=61 header_field complete
++++++++off=62 len=7 span[header_value]="chunked"
++++++++off=71 header_value complete
++++++++off=73 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=76 chunk header len=5
++++++++off=76 len=5 span[body]="hello"
++++++++off=83 chunk complete
++++++++off=86 chunk header len=6
++++++++off=86 len=6 span[body]=" world"
++++++++off=94 chunk complete
++++++++off=97 chunk header len=0
++++++++off=97 len=4 span[header_field]="Vary"
++++++++off=102 header_field complete
++++++++off=103 len=1 span[header_value]="*"
++++++++off=106 header_value complete
++++++++off=106 len=12 span[header_field]="Content-Type"
++++++++off=119 header_field complete
++++++++off=120 len=10 span[header_value]="text/plain"
++++++++off=132 header_value complete
++++++++off=134 chunk complete
++++++++off=134 message complete
++++++++```
++++++++
++++++++### Chunk extensions
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_unicorns_after_length HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++5;ilovew3;somuchlove=aretheseparametersfor;another=withvalue
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=32 span[url]="/chunked_w_unicorns_after_length"
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=48 len=17 span[header_field]="Transfer-Encoding"
++++++++off=66 header_field complete
++++++++off=67 len=7 span[header_value]="chunked"
++++++++off=76 header_value complete
++++++++off=78 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=80 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=88 chunk_extension_name complete
++++++++off=88 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=99 chunk_extension_name complete
++++++++off=99 len=21 span[chunk_extension_value]="aretheseparametersfor"
++++++++off=121 chunk_extension_value complete
++++++++off=121 len=7 span[chunk_extension_name]="another"
++++++++off=129 chunk_extension_name complete
++++++++off=129 len=9 span[chunk_extension_value]="withvalue"
++++++++off=139 chunk_extension_value complete
++++++++off=140 chunk header len=5
++++++++off=140 len=5 span[body]="hello"
++++++++off=147 chunk complete
++++++++off=149 len=8 span[chunk_extension_name]="blahblah"
++++++++off=158 chunk_extension_name complete
++++++++off=158 len=4 span[chunk_extension_name]="blah"
++++++++off=163 chunk_extension_name complete
++++++++off=164 chunk header len=6
++++++++off=164 len=6 span[body]=" world"
++++++++off=172 chunk complete
++++++++off=175 chunk header len=0
++++++++```
++++++++
++++++++### No semicolon before chunk extensions
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_unicorns_after_length HTTP/1.1
++++++++Host: localhost
++++++++Transfer-encoding: chunked
++++++++
++++++++2 erfrferferf
++++++++aa
++++++++0 rrrr
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=32 span[url]="/chunked_w_unicorns_after_length"
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=48 len=4 span[header_field]="Host"
++++++++off=53 header_field complete
++++++++off=54 len=9 span[header_value]="localhost"
++++++++off=65 header_value complete
++++++++off=65 len=17 span[header_field]="Transfer-encoding"
++++++++off=83 header_field complete
++++++++off=84 len=7 span[header_value]="chunked"
++++++++off=93 header_value complete
++++++++off=95 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=97 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++### No extension after semicolon
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_unicorns_after_length HTTP/1.1
++++++++Host: localhost
++++++++Transfer-encoding: chunked
++++++++
++++++++2;
++++++++aa
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=32 span[url]="/chunked_w_unicorns_after_length"
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=48 len=4 span[header_field]="Host"
++++++++off=53 header_field complete
++++++++off=54 len=9 span[header_value]="localhost"
++++++++off=65 header_value complete
++++++++off=65 len=17 span[header_field]="Transfer-encoding"
++++++++off=83 header_field complete
++++++++off=84 len=7 span[header_value]="chunked"
++++++++off=93 header_value complete
++++++++off=95 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=98 error code=2 reason="Invalid character in chunk extensions"
++++++++```
++++++++
++++++++
++++++++### Chunk extensions quoting
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_unicorns_after_length HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++5;ilovew3="I \"love\"; \\extensions\\";somuchlove="aretheseparametersfor";blah;foo=bar
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=32 span[url]="/chunked_w_unicorns_after_length"
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=48 len=17 span[header_field]="Transfer-Encoding"
++++++++off=66 header_field complete
++++++++off=67 len=7 span[header_value]="chunked"
++++++++off=76 header_value complete
++++++++off=78 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=80 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=88 chunk_extension_name complete
++++++++off=88 len=28 span[chunk_extension_value]=""I \"love\"; \\extensions\\""
++++++++off=116 chunk_extension_value complete
++++++++off=117 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=128 chunk_extension_name complete
++++++++off=128 len=23 span[chunk_extension_value]=""aretheseparametersfor""
++++++++off=151 chunk_extension_value complete
++++++++off=152 len=4 span[chunk_extension_name]="blah"
++++++++off=157 chunk_extension_name complete
++++++++off=157 len=3 span[chunk_extension_name]="foo"
++++++++off=161 chunk_extension_name complete
++++++++off=161 len=3 span[chunk_extension_value]="bar"
++++++++off=165 chunk_extension_value complete
++++++++off=166 chunk header len=5
++++++++off=166 len=5 span[body]="hello"
++++++++off=173 chunk complete
++++++++off=175 len=8 span[chunk_extension_name]="blahblah"
++++++++off=184 chunk_extension_name complete
++++++++off=184 len=4 span[chunk_extension_name]="blah"
++++++++off=189 chunk_extension_name complete
++++++++off=190 chunk header len=6
++++++++off=190 len=6 span[body]=" world"
++++++++off=198 chunk complete
++++++++off=201 chunk header len=0
++++++++```
++++++++
++++++++
++++++++### Unbalanced chunk extensions quoting
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /chunked_w_unicorns_after_length HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++5;ilovew3="abc";somuchlove="def; ghi
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=32 span[url]="/chunked_w_unicorns_after_length"
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=48 len=17 span[header_field]="Transfer-Encoding"
++++++++off=66 header_field complete
++++++++off=67 len=7 span[header_value]="chunked"
++++++++off=76 header_value complete
++++++++off=78 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=80 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=88 chunk_extension_name complete
++++++++off=88 len=5 span[chunk_extension_value]=""abc""
++++++++off=93 chunk_extension_value complete
++++++++off=94 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=105 chunk_extension_name complete
++++++++off=105 len=9 span[chunk_extension_value]=""def; ghi"
++++++++off=115 error code=2 reason="Invalid character in chunk extensions quoted value"
++++++++```
++++++++
++++++++## Ignoring `pigeons`
++++++++
++++++++Requests cannot have invalid `Transfer-Encoding`. It is impossible to determine
++++++++their body size. Not erroring would make HTTP smuggling attacks possible.
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: pigeons
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="pigeons"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=200 content_length=0
++++++++off=49 error code=15 reason="Request has invalid `Transfer-Encoding`"
++++++++```
++++++++
++++++++## POST with `Transfer-Encoding` and `Content-Length`
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: identity
++++++++Content-Length: 5
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=8 span[header_value]="identity"
++++++++off=96 header_value complete
++++++++off=96 len=14 span[header_field]="Content-Length"
++++++++off=111 header_field complete
++++++++off=111 error code=11 reason="Content-Length can't be present with Transfer-Encoding"
++++++++```
++++++++
++++++++## POST with `Transfer-Encoding` and `Content-Length` (lenient)
++++++++
++++++++TODO(indutny): should we allow it even in lenient mode? (Consider disabling
++++++++this).
++++++++
++++++++NOTE: `Content-Length` is ignored when `Transfer-Encoding` is present. Messages
++++++++(in lenient mode) are read until EOF.
++++++++
++++++++<!-- meta={"type": "request-lenient-chunked-length"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: identity
++++++++Content-Length: 1
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=8 span[header_value]="identity"
++++++++off=96 header_value complete
++++++++off=96 len=14 span[header_field]="Content-Length"
++++++++off=111 header_field complete
++++++++off=112 len=1 span[header_value]="1"
++++++++off=115 header_value complete
++++++++off=117 headers complete method=3 v=1/1 flags=220 content_length=1
++++++++off=117 len=5 span[body]="World"
++++++++```
++++++++
++++++++## POST with empty `Transfer-Encoding` and `Content-Length` (lenient)
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST / HTTP/1.1
++++++++Host: foo
++++++++Content-Length: 10
++++++++Transfer-Encoding:
++++++++Transfer-Encoding:
++++++++Transfer-Encoding:
++++++++
++++++++2
++++++++AA
++++++++0
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=1 span[url]="/"
++++++++off=7 url complete
++++++++off=12 len=3 span[version]="1.1"
++++++++off=15 version complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=3 span[header_value]="foo"
++++++++off=28 header_value complete
++++++++off=28 len=14 span[header_field]="Content-Length"
++++++++off=43 header_field complete
++++++++off=44 len=2 span[header_value]="10"
++++++++off=48 header_value complete
++++++++off=48 len=17 span[header_field]="Transfer-Encoding"
++++++++off=66 header_field complete
++++++++off=66 error code=15 reason="Transfer-Encoding can't be present with Content-Length"
++++++++```
++++++++
++++++++## POST with `chunked` before other transfer coding names
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: chunked, deflate
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=7 span[header_value]="chunked"
++++++++off=94 error code=15 reason="Invalid `Transfer-Encoding` header value"
++++++++```
++++++++
++++++++## POST with `chunked` and duplicate transfer-encoding
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: chunked
++++++++Transfer-Encoding: deflate
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=7 span[header_value]="chunked"
++++++++off=95 header_value complete
++++++++off=95 len=17 span[header_field]="Transfer-Encoding"
++++++++off=113 header_field complete
++++++++off=114 len=0 span[header_value]=""
++++++++off=115 error code=15 reason="Invalid `Transfer-Encoding` header value"
++++++++```
++++++++
++++++++## POST with `chunked` before other transfer-coding (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-transfer-encoding"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: chunked, deflate
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=16 span[header_value]="chunked, deflate"
++++++++off=104 header_value complete
++++++++off=106 headers complete method=3 v=1/1 flags=200 content_length=0
++++++++off=106 len=5 span[body]="World"
++++++++```
++++++++
++++++++## POST with `chunked` and duplicate transfer-encoding (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-transfer-encoding"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: chunked
++++++++Transfer-Encoding: deflate
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=7 span[header_value]="chunked"
++++++++off=95 header_value complete
++++++++off=95 len=17 span[header_field]="Transfer-Encoding"
++++++++off=113 header_field complete
++++++++off=114 len=7 span[header_value]="deflate"
++++++++off=123 header_value complete
++++++++off=125 headers complete method=3 v=1/1 flags=200 content_length=0
++++++++off=125 len=5 span[body]="World"
++++++++```
++++++++
++++++++## POST with `chunked` as last transfer-encoding
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: deflate, chunked
++++++++
++++++++5
++++++++World
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=16 span[header_value]="deflate, chunked"
++++++++off=104 header_value complete
++++++++off=106 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=109 chunk header len=5
++++++++off=109 len=5 span[body]="World"
++++++++off=116 chunk complete
++++++++off=119 chunk header len=0
++++++++off=121 chunk complete
++++++++off=121 message complete
++++++++```
++++++++
++++++++## POST with `chunked` as last transfer-encoding (multiple headers)
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: deflate
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++World
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=7 span[header_value]="deflate"
++++++++off=95 header_value complete
++++++++off=95 len=17 span[header_field]="Transfer-Encoding"
++++++++off=113 header_field complete
++++++++off=114 len=7 span[header_value]="chunked"
++++++++off=123 header_value complete
++++++++off=125 headers complete method=3 v=1/1 flags=208 content_length=0
++++++++off=128 chunk header len=5
++++++++off=128 len=5 span[body]="World"
++++++++off=135 chunk complete
++++++++off=138 chunk header len=0
++++++++off=140 chunk complete
++++++++off=140 message complete
++++++++```
++++++++
++++++++## POST with `chunkedchunked` as transfer-encoding
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++POST /post_identity_body_world?q=search#hey HTTP/1.1
++++++++Accept: */*
++++++++Transfer-Encoding: chunkedchunked
++++++++
++++++++5
++++++++World
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=4 span[method]="POST"
++++++++off=4 method complete
++++++++off=5 len=38 span[url]="/post_identity_body_world?q=search#hey"
++++++++off=44 url complete
++++++++off=49 len=3 span[version]="1.1"
++++++++off=52 version complete
++++++++off=54 len=6 span[header_field]="Accept"
++++++++off=61 header_field complete
++++++++off=62 len=3 span[header_value]="*/*"
++++++++off=67 header_value complete
++++++++off=67 len=17 span[header_field]="Transfer-Encoding"
++++++++off=85 header_field complete
++++++++off=86 len=14 span[header_value]="chunkedchunked"
++++++++off=102 header_value complete
++++++++off=104 headers complete method=3 v=1/1 flags=200 content_length=0
++++++++off=104 error code=15 reason="Request has invalid `Transfer-Encoding`"
++++++++```
++++++++
++++++++## Missing last-chunk
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++3
++++++++foo
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=52 chunk header len=3
++++++++off=52 len=3 span[body]="foo"
++++++++off=57 chunk complete
++++++++off=57 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++## Validate chunk parameters
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++3 \n \r\n\
++++++++foo
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=51 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++## Invalid OBS fold after chunked value
++++++++
++++++++<!-- meta={"type": "request-lenient-headers" } -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++ abc
++++++++
++++++++5
++++++++World
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 len=5 span[header_value]=" abc"
++++++++off=54 header_value complete
++++++++off=56 headers complete method=4 v=1/1 flags=200 content_length=0
++++++++off=56 error code=15 reason="Request has invalid `Transfer-Encoding`"
++++++++```
++++++++
++++++++### Chunk header not terminated by CRLF
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Connection: close
++++++++Transfer-Encoding: chunked
++++++++
++++++++5\r\r;ABCD
++++++++34
++++++++E
++++++++0
++++++++
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Content-Length: 5
++++++++
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=1 span[header_value]="a"
++++++++off=25 header_value complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=6 span[header_value]="close "
++++++++off=45 header_value complete
++++++++off=45 len=17 span[header_field]="Transfer-Encoding"
++++++++off=63 header_field complete
++++++++off=64 len=8 span[header_value]="chunked "
++++++++off=74 header_value complete
++++++++off=76 headers complete method=1 v=1/1 flags=20a content_length=0
++++++++off=78 error code=2 reason="Expected LF after chunk size"
++++++++```
++++++++
++++++++### Chunk header not terminated by CRLF (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-optional-lf-after-cr" } -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Connection: close
++++++++Transfer-Encoding: chunked
++++++++
++++++++6\r\r;ABCD
++++++++33
++++++++E
++++++++0
++++++++
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Content-Length: 5
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=1 span[header_value]="a"
++++++++off=25 header_value complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=6 span[header_value]="close "
++++++++off=45 header_value complete
++++++++off=45 len=17 span[header_field]="Transfer-Encoding"
++++++++off=63 header_field complete
++++++++off=64 len=8 span[header_value]="chunked "
++++++++off=74 header_value complete
++++++++off=76 headers complete method=1 v=1/1 flags=20a content_length=0
++++++++off=78 chunk header len=6
++++++++off=78 len=1 span[body]=cr
++++++++off=79 len=5 span[body]=";ABCD"
++++++++off=86 chunk complete
++++++++off=90 chunk header len=51
++++++++off=90 len=1 span[body]="E"
++++++++off=91 len=1 span[body]=cr
++++++++off=92 len=1 span[body]=lf
++++++++off=93 len=1 span[body]="0"
++++++++off=94 len=1 span[body]=cr
++++++++off=95 len=1 span[body]=lf
++++++++off=96 len=1 span[body]=cr
++++++++off=97 len=1 span[body]=lf
++++++++off=98 len=15 span[body]="GET / HTTP/1.1 "
++++++++off=113 len=1 span[body]=cr
++++++++off=114 len=1 span[body]=lf
++++++++off=115 len=7 span[body]="Host: a"
++++++++off=122 len=1 span[body]=cr
++++++++off=123 len=1 span[body]=lf
++++++++off=124 len=17 span[body]="Content-Length: 5"
++++++++off=143 chunk complete
++++++++off=146 chunk header len=0
++++++++off=148 chunk complete
++++++++off=148 message complete
++++++++```
++++++++
++++++++### Chunk data not terminated by CRLF
++++++++
++++++++<!-- meta={"type": "request" } -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Connection: close
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++ABCDE0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=1 span[header_value]="a"
++++++++off=25 header_value complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=6 span[header_value]="close "
++++++++off=45 header_value complete
++++++++off=45 len=17 span[header_field]="Transfer-Encoding"
++++++++off=63 header_field complete
++++++++off=64 len=8 span[header_value]="chunked "
++++++++off=74 header_value complete
++++++++off=76 headers complete method=1 v=1/1 flags=20a content_length=0
++++++++off=79 chunk header len=5
++++++++off=79 len=5 span[body]="ABCDE"
++++++++off=84 error code=2 reason="Expected LF after chunk data"
++++++++```
++++++++
++++++++### Chunk data not terminated by CRLF (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-optional-crlf-after-chunk" } -->
++++++++
++++++++```http
++++++++GET / HTTP/1.1
++++++++Host: a
++++++++Connection: close
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++ABCDE0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=1 span[url]="/"
++++++++off=6 url complete
++++++++off=11 len=3 span[version]="1.1"
++++++++off=14 version complete
++++++++off=16 len=4 span[header_field]="Host"
++++++++off=21 header_field complete
++++++++off=22 len=1 span[header_value]="a"
++++++++off=25 header_value complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=6 span[header_value]="close "
++++++++off=45 header_value complete
++++++++off=45 len=17 span[header_field]="Transfer-Encoding"
++++++++off=63 header_field complete
++++++++off=64 len=8 span[header_value]="chunked "
++++++++off=74 header_value complete
++++++++off=76 headers complete method=1 v=1/1 flags=20a content_length=0
++++++++off=79 chunk header len=5
++++++++off=79 len=5 span[body]="ABCDE"
++++++++off=84 chunk complete
++++++++off=87 chunk header len=0
++++++++```
++++++++
++++++++## Space after chunk header
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a \r\n0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=51 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++## Space after chunk header (lenient)
++++++++
++++++++<!-- meta={"type": "request-lenient-spaces-after-chunk-size"} -->
++++++++```http
++++++++PUT /url HTTP/1.1
++++++++Transfer-Encoding: chunked
++++++++
++++++++a \r\n0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="PUT"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/url"
++++++++off=9 url complete
++++++++off=14 len=3 span[version]="1.1"
++++++++off=17 version complete
++++++++off=19 len=17 span[header_field]="Transfer-Encoding"
++++++++off=37 header_field complete
++++++++off=38 len=7 span[header_value]="chunked"
++++++++off=47 header_value complete
++++++++off=49 headers complete method=4 v=1/1 flags=208 content_length=0
++++++++off=53 chunk header len=10
++++++++off=53 len=10 span[body]="0123456789"
++++++++off=65 chunk complete
++++++++off=68 chunk header len=0
++++++++off=70 chunk complete
++++++++off=70 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++URI
++++++++===
++++++++
++++++++## Quotes in URI
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /with_"lovely"_quotes?foo=\"bar\" HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=33 span[url]="/with_"lovely"_quotes?foo=\"bar\""
++++++++off=38 url complete
++++++++off=43 len=3 span[version]="1.1"
++++++++off=46 version complete
++++++++off=50 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=50 message complete
++++++++```
++++++++
++++++++## Query URL with question mark
++++++++
++++++++Some clients include `?` characters in query strings.
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /test.cgi?foo=bar?baz HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=21 span[url]="/test.cgi?foo=bar?baz"
++++++++off=26 url complete
++++++++off=31 len=3 span[version]="1.1"
++++++++off=34 version complete
++++++++off=38 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=38 message complete
++++++++```
++++++++
++++++++## Host terminated by a query string
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET http://hypnotoad.org?hail=all HTTP/1.1\r\n
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=29 span[url]="http://hypnotoad.org?hail=all"
++++++++off=34 url complete
++++++++off=39 len=3 span[version]="1.1"
++++++++off=42 version complete
++++++++off=46 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=46 message complete
++++++++```
++++++++
++++++++## `host:port` terminated by a query string
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET http://hypnotoad.org:1234?hail=all HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=34 span[url]="http://hypnotoad.org:1234?hail=all"
++++++++off=39 url complete
++++++++off=44 len=3 span[version]="1.1"
++++++++off=47 version complete
++++++++off=51 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=51 message complete
++++++++```
++++++++
++++++++## Query URL with vertical bar character
++++++++
++++++++It should be allowed to have vertical bar symbol in URI: `|`.
++++++++
++++++++See: https://github.com/nodejs/node/issues/27584
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /test.cgi?query=| HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=17 span[url]="/test.cgi?query=|"
++++++++off=22 url complete
++++++++off=27 len=3 span[version]="1.1"
++++++++off=30 version complete
++++++++off=34 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=34 message complete
++++++++```
++++++++
++++++++## `host:port` terminated by a space
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET http://hypnotoad.org:1234 HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=25 span[url]="http://hypnotoad.org:1234"
++++++++off=30 url complete
++++++++off=35 len=3 span[version]="1.1"
++++++++off=38 version complete
++++++++off=42 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=42 message complete
++++++++```
++++++++
++++++++## Disallow UTF-8 in URI path in strict mode
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET /δ¶/δt/pope?q=1#narf HTTP/1.1
++++++++Host: github.com
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=5 error code=7 reason="Invalid char in url path"
++++++++```
++++++++
++++++++## Fragment in URI
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=40 span[url]="/forums/1/topics/2375?page=1#posts-17408"
++++++++off=45 url complete
++++++++off=50 len=3 span[version]="1.1"
++++++++off=53 version complete
++++++++off=57 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=57 message complete
++++++++```
++++++++
++++++++## Underscore in hostname
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++CONNECT home_0.netscape.com:443 HTTP/1.0
++++++++User-agent: Mozilla/1.1N
++++++++Proxy-authorization: basic aGVsbG86d29ybGQ=
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=7 span[method]="CONNECT"
++++++++off=7 method complete
++++++++off=8 len=23 span[url]="home_0.netscape.com:443"
++++++++off=32 url complete
++++++++off=37 len=3 span[version]="1.0"
++++++++off=40 version complete
++++++++off=42 len=10 span[header_field]="User-agent"
++++++++off=53 header_field complete
++++++++off=54 len=12 span[header_value]="Mozilla/1.1N"
++++++++off=68 header_value complete
++++++++off=68 len=19 span[header_field]="Proxy-authorization"
++++++++off=88 header_field complete
++++++++off=89 len=22 span[header_value]="basic aGVsbG86d29ybGQ="
++++++++off=113 header_value complete
++++++++off=115 headers complete method=5 v=1/0 flags=0 content_length=0
++++++++off=115 message complete
++++++++off=115 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++## `host:port` and basic auth
++++++++
++++++++<!-- meta={"type": "request"} -->
++++++++```http
++++++++GET http://a%12:b!&*$@hypnotoad.org:1234/toto HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=41 span[url]="http://a%12:b!&*$@hypnotoad.org:1234/toto"
++++++++off=46 url complete
++++++++off=51 len=3 span[version]="1.1"
++++++++off=54 version complete
++++++++off=58 headers complete method=1 v=1/1 flags=0 content_length=0
++++++++off=58 message complete
++++++++```
++++++++
++++++++## Space in URI
++++++++
++++++++<!-- meta={"type": "request", "noScan": true} -->
++++++++```http
++++++++GET /foo bar/ HTTP/1.1
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 len=3 span[method]="GET"
++++++++off=3 method complete
++++++++off=4 len=4 span[url]="/foo"
++++++++off=9 url complete
++++++++off=9 error code=8 reason="Expected HTTP/"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Connection header
++++++++=================
++++++++
++++++++## Proxy-Connection
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Type: text/html; charset=UTF-8
++++++++Content-Length: 11
++++++++Proxy-Connection: close
++++++++Date: Thu, 31 Dec 2009 20:55:48 +0000
++++++++
++++++++hello world
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=12 span[header_field]="Content-Type"
++++++++off=30 header_field complete
++++++++off=31 len=24 span[header_value]="text/html; charset=UTF-8"
++++++++off=57 header_value complete
++++++++off=57 len=14 span[header_field]="Content-Length"
++++++++off=72 header_field complete
++++++++off=73 len=2 span[header_value]="11"
++++++++off=77 header_value complete
++++++++off=77 len=16 span[header_field]="Proxy-Connection"
++++++++off=94 header_field complete
++++++++off=95 len=5 span[header_value]="close"
++++++++off=102 header_value complete
++++++++off=102 len=4 span[header_field]="Date"
++++++++off=107 header_field complete
++++++++off=108 len=31 span[header_value]="Thu, 31 Dec 2009 20:55:48 +0000"
++++++++off=141 header_value complete
++++++++off=143 headers complete status=200 v=1/1 flags=22 content_length=11
++++++++off=143 len=11 span[body]="hello world"
++++++++off=154 message complete
++++++++```
++++++++
++++++++## HTTP/1.0 with keep-alive and EOF-terminated 200 status
++++++++
++++++++There is no `Content-Length` in this response, so even though the
++++++++`keep-alive` is on - it should read until EOF.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.0 200 OK
++++++++Connection: keep-alive
++++++++
++++++++HTTP/1.0 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.0"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=10 span[header_value]="keep-alive"
++++++++off=41 header_value complete
++++++++off=43 headers complete status=200 v=1/0 flags=1 content_length=0
++++++++off=43 len=15 span[body]="HTTP/1.0 200 OK"
++++++++```
++++++++
++++++++## HTTP/1.0 with keep-alive and 204 status
++++++++
++++++++Responses with `204` status cannot have a body.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.0 204 No content
++++++++Connection: keep-alive
++++++++
++++++++HTTP/1.0 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.0"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=10 span[header_value]="keep-alive"
++++++++off=49 header_value complete
++++++++off=51 headers complete status=204 v=1/0 flags=1 content_length=0
++++++++off=51 message complete
++++++++off=51 reset
++++++++off=51 message begin
++++++++off=56 len=3 span[version]="1.0"
++++++++off=59 version complete
++++++++off=64 len=2 span[status]="OK"
++++++++```
++++++++
++++++++## HTTP/1.1 with EOF-terminated 200 status
++++++++
++++++++There is no `Content-Length` in this response, so even though the
++++++++`keep-alive` is on (implicitly in HTTP 1.1) - it should read until EOF.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++
++++++++HTTP/1.1 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=19 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++off=19 len=15 span[body]="HTTP/1.1 200 OK"
++++++++```
++++++++
++++++++## HTTP/1.1 with 204 status
++++++++
++++++++Responses with `204` status cannot have a body.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 204 No content
++++++++
++++++++HTTP/1.1 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=27 headers complete status=204 v=1/1 flags=0 content_length=0
++++++++off=27 message complete
++++++++off=27 reset
++++++++off=27 message begin
++++++++off=32 len=3 span[version]="1.1"
++++++++off=35 version complete
++++++++off=40 len=2 span[status]="OK"
++++++++```
++++++++
++++++++## HTTP/1.1 with keep-alive disabled and 204 status
++++++++
++++++++<!-- meta={"type": "response" } -->
++++++++```http
++++++++HTTP/1.1 204 No content
++++++++Connection: close
++++++++
++++++++HTTP/1.1 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=5 span[header_value]="close"
++++++++off=44 header_value complete
++++++++off=46 headers complete status=204 v=1/1 flags=2 content_length=0
++++++++off=46 message complete
++++++++off=47 error code=5 reason="Data after `Connection: close`"
++++++++```
++++++++
++++++++## HTTP/1.1 with keep-alive disabled, content-length (lenient)
++++++++
++++++++Parser should discard extra request in lenient mode.
++++++++
++++++++<!-- meta={"type": "response-lenient-data-after-close" } -->
++++++++```http
++++++++HTTP/1.1 200 No content
++++++++Content-Length: 5
++++++++Connection: close
++++++++
++++++++2ad731e3-4dcd-4f70-b871-0ad284b29ffc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=25 len=14 span[header_field]="Content-Length"
++++++++off=40 header_field complete
++++++++off=41 len=1 span[header_value]="5"
++++++++off=44 header_value complete
++++++++off=44 len=10 span[header_field]="Connection"
++++++++off=55 header_field complete
++++++++off=56 len=5 span[header_value]="close"
++++++++off=63 header_value complete
++++++++off=65 headers complete status=200 v=1/1 flags=22 content_length=5
++++++++off=65 len=5 span[body]="2ad73"
++++++++off=70 message complete
++++++++```
++++++++
++++++++## HTTP/1.1 with keep-alive disabled, content-length
++++++++
++++++++Parser should discard extra request in strict mode.
++++++++
++++++++<!-- meta={"type": "response" } -->
++++++++```http
++++++++HTTP/1.1 200 No content
++++++++Content-Length: 5
++++++++Connection: close
++++++++
++++++++2ad731e3-4dcd-4f70-b871-0ad284b29ffc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=25 len=14 span[header_field]="Content-Length"
++++++++off=40 header_field complete
++++++++off=41 len=1 span[header_value]="5"
++++++++off=44 header_value complete
++++++++off=44 len=10 span[header_field]="Connection"
++++++++off=55 header_field complete
++++++++off=56 len=5 span[header_value]="close"
++++++++off=63 header_value complete
++++++++off=65 headers complete status=200 v=1/1 flags=22 content_length=5
++++++++off=65 len=5 span[body]="2ad73"
++++++++off=70 message complete
++++++++off=71 error code=5 reason="Data after `Connection: close`"
++++++++```
++++++++
++++++++## HTTP/1.1 with keep-alive disabled and 204 status (lenient)
++++++++
++++++++<!-- meta={"type": "response-lenient-keep-alive"} -->
++++++++```http
++++++++HTTP/1.1 204 No content
++++++++Connection: close
++++++++
++++++++HTTP/1.1 200 OK
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=10 span[status]="No content"
++++++++off=25 status complete
++++++++off=25 len=10 span[header_field]="Connection"
++++++++off=36 header_field complete
++++++++off=37 len=5 span[header_value]="close"
++++++++off=44 header_value complete
++++++++off=46 headers complete status=204 v=1/1 flags=2 content_length=0
++++++++off=46 message complete
++++++++off=46 reset
++++++++off=46 message begin
++++++++off=51 len=3 span[version]="1.1"
++++++++off=54 version complete
++++++++off=59 len=2 span[status]="OK"
++++++++```
++++++++
++++++++## HTTP 101 response with Upgrade and Content-Length header
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 101 Switching Protocols
++++++++Connection: upgrade
++++++++Upgrade: h2c
++++++++Content-Length: 4
++++++++
++++++++body\
++++++++proto
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=19 span[status]="Switching Protocols"
++++++++off=34 status complete
++++++++off=34 len=10 span[header_field]="Connection"
++++++++off=45 header_field complete
++++++++off=46 len=7 span[header_value]="upgrade"
++++++++off=55 header_value complete
++++++++off=55 len=7 span[header_field]="Upgrade"
++++++++off=63 header_field complete
++++++++off=64 len=3 span[header_value]="h2c"
++++++++off=69 header_value complete
++++++++off=69 len=14 span[header_field]="Content-Length"
++++++++off=84 header_field complete
++++++++off=85 len=1 span[header_value]="4"
++++++++off=88 header_value complete
++++++++off=90 headers complete status=101 v=1/1 flags=34 content_length=4
++++++++off=90 message complete
++++++++off=90 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++## HTTP 101 response with Upgrade and Transfer-Encoding header
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 101 Switching Protocols
++++++++Connection: upgrade
++++++++Upgrade: h2c
++++++++Transfer-Encoding: chunked
++++++++
++++++++2
++++++++bo
++++++++2
++++++++dy
++++++++0
++++++++
++++++++proto
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=19 span[status]="Switching Protocols"
++++++++off=34 status complete
++++++++off=34 len=10 span[header_field]="Connection"
++++++++off=45 header_field complete
++++++++off=46 len=7 span[header_value]="upgrade"
++++++++off=55 header_value complete
++++++++off=55 len=7 span[header_field]="Upgrade"
++++++++off=63 header_field complete
++++++++off=64 len=3 span[header_value]="h2c"
++++++++off=69 header_value complete
++++++++off=69 len=17 span[header_field]="Transfer-Encoding"
++++++++off=87 header_field complete
++++++++off=88 len=7 span[header_value]="chunked"
++++++++off=97 header_value complete
++++++++off=99 headers complete status=101 v=1/1 flags=21c content_length=0
++++++++off=99 message complete
++++++++off=99 error code=22 reason="Pause on CONNECT/Upgrade"
++++++++```
++++++++
++++++++## HTTP 200 response with Upgrade header
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Connection: upgrade
++++++++Upgrade: h2c
++++++++
++++++++body
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=7 span[header_value]="upgrade"
++++++++off=38 header_value complete
++++++++off=38 len=7 span[header_field]="Upgrade"
++++++++off=46 header_field complete
++++++++off=47 len=3 span[header_value]="h2c"
++++++++off=52 header_value complete
++++++++off=54 headers complete status=200 v=1/1 flags=14 content_length=0
++++++++off=54 len=4 span[body]="body"
++++++++```
++++++++
++++++++## HTTP 200 response with Upgrade header and Content-Length
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Connection: upgrade
++++++++Upgrade: h2c
++++++++Content-Length: 4
++++++++
++++++++body
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=7 span[header_value]="upgrade"
++++++++off=38 header_value complete
++++++++off=38 len=7 span[header_field]="Upgrade"
++++++++off=46 header_field complete
++++++++off=47 len=3 span[header_value]="h2c"
++++++++off=52 header_value complete
++++++++off=52 len=14 span[header_field]="Content-Length"
++++++++off=67 header_field complete
++++++++off=68 len=1 span[header_value]="4"
++++++++off=71 header_value complete
++++++++off=73 headers complete status=200 v=1/1 flags=34 content_length=4
++++++++off=73 len=4 span[body]="body"
++++++++off=77 message complete
++++++++```
++++++++
++++++++## HTTP 200 response with Upgrade header and Transfer-Encoding
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Connection: upgrade
++++++++Upgrade: h2c
++++++++Transfer-Encoding: chunked
++++++++
++++++++2
++++++++bo
++++++++2
++++++++dy
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=10 span[header_field]="Connection"
++++++++off=28 header_field complete
++++++++off=29 len=7 span[header_value]="upgrade"
++++++++off=38 header_value complete
++++++++off=38 len=7 span[header_field]="Upgrade"
++++++++off=46 header_field complete
++++++++off=47 len=3 span[header_value]="h2c"
++++++++off=52 header_value complete
++++++++off=52 len=17 span[header_field]="Transfer-Encoding"
++++++++off=70 header_field complete
++++++++off=71 len=7 span[header_value]="chunked"
++++++++off=80 header_value complete
++++++++off=82 headers complete status=200 v=1/1 flags=21c content_length=0
++++++++off=85 chunk header len=2
++++++++off=85 len=2 span[body]="bo"
++++++++off=89 chunk complete
++++++++off=92 chunk header len=2
++++++++off=92 len=2 span[body]="dy"
++++++++off=96 chunk complete
++++++++off=99 chunk header len=0
++++++++off=101 chunk complete
++++++++off=101 message complete
++++++++```
++++++++
++++++++## HTTP 304 with Content-Length
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 304 Not Modified
++++++++Content-Length: 10
++++++++
++++++++
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 5
++++++++
++++++++hello
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=12 span[status]="Not Modified"
++++++++off=27 status complete
++++++++off=27 len=14 span[header_field]="Content-Length"
++++++++off=42 header_field complete
++++++++off=43 len=2 span[header_value]="10"
++++++++off=47 header_value complete
++++++++off=49 headers complete status=304 v=1/1 flags=20 content_length=10
++++++++off=49 message complete
++++++++off=51 reset
++++++++off=51 message begin
++++++++off=56 len=3 span[version]="1.1"
++++++++off=59 version complete
++++++++off=64 len=2 span[status]="OK"
++++++++off=68 status complete
++++++++off=68 len=14 span[header_field]="Content-Length"
++++++++off=83 header_field complete
++++++++off=84 len=1 span[header_value]="5"
++++++++off=87 header_value complete
++++++++off=89 headers complete status=200 v=1/1 flags=20 content_length=5
++++++++off=89 len=5 span[body]="hello"
++++++++off=94 message complete
++++++++```
++++++++
++++++++## HTTP 304 with Transfer-Encoding
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 304 Not Modified
++++++++Transfer-Encoding: chunked
++++++++
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++
++++++++5
++++++++hello
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=12 span[status]="Not Modified"
++++++++off=27 status complete
++++++++off=27 len=17 span[header_field]="Transfer-Encoding"
++++++++off=45 header_field complete
++++++++off=46 len=7 span[header_value]="chunked"
++++++++off=55 header_value complete
++++++++off=57 headers complete status=304 v=1/1 flags=208 content_length=0
++++++++off=57 message complete
++++++++off=57 reset
++++++++off=57 message begin
++++++++off=62 len=3 span[version]="1.1"
++++++++off=65 version complete
++++++++off=70 len=2 span[status]="OK"
++++++++off=74 status complete
++++++++off=74 len=17 span[header_field]="Transfer-Encoding"
++++++++off=92 header_field complete
++++++++off=93 len=7 span[header_value]="chunked"
++++++++off=102 header_value complete
++++++++off=104 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=107 chunk header len=5
++++++++off=107 len=5 span[body]="hello"
++++++++off=114 chunk complete
++++++++off=117 chunk header len=0
++++++++```
++++++++
++++++++## HTTP 100 first, then 400
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 100 Continue
++++++++
++++++++
++++++++HTTP/1.1 404 Not Found
++++++++Content-Type: text/plain; charset=utf-8
++++++++Content-Length: 14
++++++++Date: Fri, 15 Sep 2023 19:47:23 GMT
++++++++Server: Python/3.10 aiohttp/4.0.0a2.dev0
++++++++
++++++++404: Not Found
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=8 span[status]="Continue"
++++++++off=23 status complete
++++++++off=25 headers complete status=100 v=1/1 flags=0 content_length=0
++++++++off=25 message complete
++++++++off=27 reset
++++++++off=27 message begin
++++++++off=32 len=3 span[version]="1.1"
++++++++off=35 version complete
++++++++off=40 len=9 span[status]="Not Found"
++++++++off=51 status complete
++++++++off=51 len=12 span[header_field]="Content-Type"
++++++++off=64 header_field complete
++++++++off=65 len=25 span[header_value]="text/plain; charset=utf-8"
++++++++off=92 header_value complete
++++++++off=92 len=14 span[header_field]="Content-Length"
++++++++off=107 header_field complete
++++++++off=108 len=2 span[header_value]="14"
++++++++off=112 header_value complete
++++++++off=112 len=4 span[header_field]="Date"
++++++++off=117 header_field complete
++++++++off=118 len=29 span[header_value]="Fri, 15 Sep 2023 19:47:23 GMT"
++++++++off=149 header_value complete
++++++++off=149 len=6 span[header_field]="Server"
++++++++off=156 header_field complete
++++++++off=157 len=32 span[header_value]="Python/3.10 aiohttp/4.0.0a2.dev0"
++++++++off=191 header_value complete
++++++++off=193 headers complete status=404 v=1/1 flags=20 content_length=14
++++++++off=193 len=14 span[body]="404: Not Found"
++++++++off=207 message complete
++++++++```
++++++++
++++++++## HTTP 103 first, then 200
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 103 Early Hints
++++++++Link: </styles.css>; rel=preload; as=style
++++++++
++++++++HTTP/1.1 200 OK
++++++++Date: Wed, 13 Sep 2023 11:09:41 GMT
++++++++Connection: keep-alive
++++++++Keep-Alive: timeout=5
++++++++Content-Length: 17
++++++++
++++++++response content
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=11 span[status]="Early Hints"
++++++++off=26 status complete
++++++++off=26 len=4 span[header_field]="Link"
++++++++off=31 header_field complete
++++++++off=32 len=36 span[header_value]="</styles.css>; rel=preload; as=style"
++++++++off=70 header_value complete
++++++++off=72 headers complete status=103 v=1/1 flags=0 content_length=0
++++++++off=72 message complete
++++++++off=72 reset
++++++++off=72 message begin
++++++++off=77 len=3 span[version]="1.1"
++++++++off=80 version complete
++++++++off=85 len=2 span[status]="OK"
++++++++off=89 status complete
++++++++off=89 len=4 span[header_field]="Date"
++++++++off=94 header_field complete
++++++++off=95 len=29 span[header_value]="Wed, 13 Sep 2023 11:09:41 GMT"
++++++++off=126 header_value complete
++++++++off=126 len=10 span[header_field]="Connection"
++++++++off=137 header_field complete
++++++++off=138 len=10 span[header_value]="keep-alive"
++++++++off=150 header_value complete
++++++++off=150 len=10 span[header_field]="Keep-Alive"
++++++++off=161 header_field complete
++++++++off=162 len=9 span[header_value]="timeout=5"
++++++++off=173 header_value complete
++++++++off=173 len=14 span[header_field]="Content-Length"
++++++++off=188 header_field complete
++++++++off=189 len=2 span[header_value]="17"
++++++++off=193 header_value complete
++++++++off=195 headers complete status=200 v=1/1 flags=21 content_length=17
++++++++off=195 len=16 span[body]="response content"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Content-Length header
++++++++=====================
++++++++
++++++++## Response without `Content-Length`, but with body
++++++++
++++++++The client should wait for the server's EOF. That is, when
++++++++`Content-Length` is not specified, and `Connection: close`, the end of body is
++++++++specified by the EOF.
++++++++
++++++++_(Compare with APACHEBENCH_GET)_
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Date: Tue, 04 Aug 2009 07:59:32 GMT
++++++++Server: Apache
++++++++X-Powered-By: Servlet/2.5 JSP/2.1
++++++++Content-Type: text/xml; charset=utf-8
++++++++Connection: close
++++++++
++++++++<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
++++++++<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\
++++++++ <SOAP-ENV:Body>\n\
++++++++ <SOAP-ENV:Fault>\n\
++++++++ <faultcode>SOAP-ENV:Client</faultcode>\n\
++++++++ <faultstring>Client Error</faultstring>\n\
++++++++ </SOAP-ENV:Fault>\n\
++++++++ </SOAP-ENV:Body>\n\
++++++++</SOAP-ENV:Envelope>
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Date"
++++++++off=22 header_field complete
++++++++off=23 len=29 span[header_value]="Tue, 04 Aug 2009 07:59:32 GMT"
++++++++off=54 header_value complete
++++++++off=54 len=6 span[header_field]="Server"
++++++++off=61 header_field complete
++++++++off=62 len=6 span[header_value]="Apache"
++++++++off=70 header_value complete
++++++++off=70 len=12 span[header_field]="X-Powered-By"
++++++++off=83 header_field complete
++++++++off=84 len=19 span[header_value]="Servlet/2.5 JSP/2.1"
++++++++off=105 header_value complete
++++++++off=105 len=12 span[header_field]="Content-Type"
++++++++off=118 header_field complete
++++++++off=119 len=23 span[header_value]="text/xml; charset=utf-8"
++++++++off=144 header_value complete
++++++++off=144 len=10 span[header_field]="Connection"
++++++++off=155 header_field complete
++++++++off=156 len=5 span[header_value]="close"
++++++++off=163 header_value complete
++++++++off=165 headers complete status=200 v=1/1 flags=2 content_length=0
++++++++off=165 len=42 span[body]="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
++++++++off=207 len=1 span[body]=lf
++++++++off=208 len=80 span[body]="<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
++++++++off=288 len=1 span[body]=lf
++++++++off=289 len=17 span[body]=" <SOAP-ENV:Body>"
++++++++off=306 len=1 span[body]=lf
++++++++off=307 len=20 span[body]=" <SOAP-ENV:Fault>"
++++++++off=327 len=1 span[body]=lf
++++++++off=328 len=45 span[body]=" <faultcode>SOAP-ENV:Client</faultcode>"
++++++++off=373 len=1 span[body]=lf
++++++++off=374 len=46 span[body]=" <faultstring>Client Error</faultstring>"
++++++++off=420 len=1 span[body]=lf
++++++++off=421 len=21 span[body]=" </SOAP-ENV:Fault>"
++++++++off=442 len=1 span[body]=lf
++++++++off=443 len=18 span[body]=" </SOAP-ENV:Body>"
++++++++off=461 len=1 span[body]=lf
++++++++off=462 len=20 span[body]="</SOAP-ENV:Envelope>"
++++++++```
++++++++
++++++++## Content-Length-X
++++++++
++++++++The header that starts with `Content-Length*` should not be treated as
++++++++`Content-Length`.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length-X: 0
++++++++Transfer-Encoding: chunked
++++++++
++++++++2
++++++++OK
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=16 span[header_field]="Content-Length-X"
++++++++off=34 header_field complete
++++++++off=35 len=1 span[header_value]="0"
++++++++off=38 header_value complete
++++++++off=38 len=17 span[header_field]="Transfer-Encoding"
++++++++off=56 header_field complete
++++++++off=57 len=7 span[header_value]="chunked"
++++++++off=66 header_value complete
++++++++off=68 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=71 chunk header len=2
++++++++off=71 len=2 span[body]="OK"
++++++++off=75 chunk complete
++++++++off=78 chunk header len=0
++++++++off=80 chunk complete
++++++++off=80 message complete
++++++++```
++++++++
++++++++## Content-Length reset when no body is received
++++++++
++++++++<!-- meta={"type": "response", "skipBody": true} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 123
++++++++
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 456
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=3 span[header_value]="123"
++++++++off=38 header_value complete
++++++++off=40 headers complete status=200 v=1/1 flags=20 content_length=123
++++++++off=40 skip body
++++++++off=40 message complete
++++++++off=40 reset
++++++++off=40 message begin
++++++++off=45 len=3 span[version]="1.1"
++++++++off=48 version complete
++++++++off=53 len=2 span[status]="OK"
++++++++off=57 status complete
++++++++off=57 len=14 span[header_field]="Content-Length"
++++++++off=72 header_field complete
++++++++off=73 len=3 span[header_value]="456"
++++++++off=78 header_value complete
++++++++off=80 headers complete status=200 v=1/1 flags=20 content_length=456
++++++++off=80 skip body
++++++++off=80 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Finish
++++++++======
++++++++
++++++++Those tests check the return codes and the behavior of `llhttp_finish()` C API.
++++++++
++++++++## It should be safe to finish with cb after empty response
++++++++
++++++++<!-- meta={"type": "response-finish"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=19 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++off=NULL finish=1
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Invalid responses
++++++++=================
++++++++
++++++++### Incomplete HTTP protocol
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTP/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=2 error code=8 reason="Expected HTTP/"
++++++++```
++++++++
++++++++### Extra digit in HTTP major version
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/01.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=1 span[version]="0"
++++++++off=6 error code=9 reason="Expected dot"
++++++++```
++++++++
++++++++### Extra digit in HTTP major version #2
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/11.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=1 span[version]="1"
++++++++off=6 error code=9 reason="Expected dot"
++++++++```
++++++++
++++++++### Extra digit in HTTP minor version
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.01 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.0"
++++++++off=8 version complete
++++++++off=8 error code=9 reason="Expected space after version"
++++++++```
++++++++-->
++++++++
++++++++### Tab after HTTP version
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1\t200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=8 error code=9 reason="Expected space after version"
++++++++```
++++++++
++++++++### CR before response and tab after HTTP version
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++\rHTTP/1.1\t200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=1 message begin
++++++++off=6 len=3 span[version]="1.1"
++++++++off=9 version complete
++++++++off=9 error code=9 reason="Expected space after version"
++++++++```
++++++++
++++++++### Headers separated by CR
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Foo: 1\rBar: 2
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=3 span[header_field]="Foo"
++++++++off=21 header_field complete
++++++++off=22 len=1 span[header_value]="1"
++++++++off=24 error code=3 reason="Missing expected LF after header value"
++++++++```
++++++++
++++++++### Invalid HTTP version
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/5.6 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="5.6"
++++++++off=8 error code=9 reason="Invalid HTTP version"
++++++++```
++++++++
++++++++## Invalid space after start line
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++ Host: foo
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=18 error code=30 reason="Unexpected space after start line"
++++++++```
++++++++
++++++++### Extra space between HTTP version and status code
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=9 error code=13 reason="Invalid status code"
++++++++```
++++++++
++++++++### Extra space between status code and reason
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=3 span[status]=" OK"
++++++++off=18 status complete
++++++++off=20 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++```
++++++++
++++++++### One-digit status code
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 2 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=10 error code=13 reason="Invalid status code"
++++++++```
++++++++
++++++++### Only LFs present and no body
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\nContent-Length: 0\n\n
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 error code=25 reason="Missing expected CR after response line"
++++++++```
++++++++
++++++++### Only LFs present and no body (lenient)
++++++++
++++++++<!-- meta={"type": "response-lenient-all"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\nContent-Length: 0\n\n
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 status complete
++++++++off=16 len=14 span[header_field]="Content-Length"
++++++++off=31 header_field complete
++++++++off=32 len=1 span[header_value]="0"
++++++++off=34 header_value complete
++++++++off=35 headers complete status=200 v=1/1 flags=20 content_length=0
++++++++off=35 message complete
++++++++```
++++++++
++++++++### Only LFs present
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\n\
++++++++Foo: abc\n\
++++++++Bar: def\n\
++++++++\n\
++++++++BODY\n\
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 error code=25 reason="Missing expected CR after response line"
++++++++```
++++++++
++++++++### Only LFs present (lenient)
++++++++
++++++++<!-- meta={"type": "response-lenient-all"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\n\
++++++++Foo: abc\n\
++++++++Bar: def\n\
++++++++\n\
++++++++BODY\n\
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 status complete
++++++++off=16 len=3 span[header_field]="Foo"
++++++++off=20 header_field complete
++++++++off=21 len=3 span[header_value]="abc"
++++++++off=25 header_value complete
++++++++off=25 len=3 span[header_field]="Bar"
++++++++off=29 header_field complete
++++++++off=30 len=3 span[header_value]="def"
++++++++off=34 header_value complete
++++++++off=35 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++off=35 len=4 span[body]="BODY"
++++++++off=39 len=1 span[body]=lf
++++++++off=40 len=1 span[body]="\"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Lenient HTTP version parsing
++++++++============================
++++++++
++++++++### Invalid HTTP version (lenient)
++++++++
++++++++<!-- meta={"type": "response-lenient-version"} -->
++++++++```http
++++++++HTTP/5.6 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="5.6"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=19 headers complete status=200 v=5/6 flags=0 content_length=0
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Pausing
++++++++=======
++++++++
++++++++### on_message_begin
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_message_begin"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=0 pause
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_message_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_message_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++off=41 pause
++++++++```
++++++++
++++++++### on_version_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_version_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=8 pause
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_status_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_status_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 pause
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_header_field_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_header_field_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=32 pause
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_header_value_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_header_value_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=36 pause
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_headers_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_headers_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++abc
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 pause
++++++++off=38 len=3 span[body]="abc"
++++++++off=41 message complete
++++++++```
++++++++
++++++++### on_chunk_header
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_chunk_header"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++
++++++++a
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=17 span[header_field]="Transfer-Encoding"
++++++++off=35 header_field complete
++++++++off=36 len=7 span[header_value]="chunked"
++++++++off=45 header_value complete
++++++++off=47 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=50 chunk header len=10
++++++++off=50 pause
++++++++off=50 len=10 span[body]="0123456789"
++++++++off=62 chunk complete
++++++++off=65 chunk header len=0
++++++++off=65 pause
++++++++off=67 chunk complete
++++++++off=67 message complete
++++++++```
++++++++
++++++++### on_chunk_extension_name
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_chunk_extension_name"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++
++++++++a;foo=bar
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=17 span[header_field]="Transfer-Encoding"
++++++++off=35 header_field complete
++++++++off=36 len=7 span[header_value]="chunked"
++++++++off=45 header_value complete
++++++++off=47 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=49 len=3 span[chunk_extension_name]="foo"
++++++++off=53 chunk_extension_name complete
++++++++off=53 pause
++++++++off=53 len=3 span[chunk_extension_value]="bar"
++++++++off=57 chunk_extension_value complete
++++++++off=58 chunk header len=10
++++++++off=58 len=10 span[body]="0123456789"
++++++++off=70 chunk complete
++++++++off=73 chunk header len=0
++++++++off=75 chunk complete
++++++++off=75 message complete
++++++++```
++++++++
++++++++### on_chunk_extension_value
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_chunk_extension_value"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++
++++++++a;foo=bar
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=17 span[header_field]="Transfer-Encoding"
++++++++off=35 header_field complete
++++++++off=36 len=7 span[header_value]="chunked"
++++++++off=45 header_value complete
++++++++off=47 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=49 len=3 span[chunk_extension_name]="foo"
++++++++off=53 chunk_extension_name complete
++++++++off=53 len=3 span[chunk_extension_value]="bar"
++++++++off=57 chunk_extension_value complete
++++++++off=57 pause
++++++++off=58 chunk header len=10
++++++++off=58 len=10 span[body]="0123456789"
++++++++off=70 chunk complete
++++++++off=73 chunk header len=0
++++++++off=75 chunk complete
++++++++off=75 message complete
++++++++```
++++++++
++++++++### on_chunk_complete
++++++++
++++++++<!-- meta={"type": "response", "pause": "on_chunk_complete"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++
++++++++a
++++++++0123456789
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=17 span[header_field]="Transfer-Encoding"
++++++++off=35 header_field complete
++++++++off=36 len=7 span[header_value]="chunked"
++++++++off=45 header_value complete
++++++++off=47 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=50 chunk header len=10
++++++++off=50 len=10 span[body]="0123456789"
++++++++off=62 chunk complete
++++++++off=62 pause
++++++++off=65 chunk header len=0
++++++++off=67 chunk complete
++++++++off=67 pause
++++++++off=67 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Pipelining
++++++++==========
++++++++
++++++++## Should parse multiple events
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Length: 3
++++++++
++++++++AAA
++++++++HTTP/1.1 201 Created
++++++++Content-Length: 4
++++++++
++++++++BBBB
++++++++HTTP/1.1 202 Accepted
++++++++Content-Length: 5
++++++++
++++++++CCCC
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=14 span[header_field]="Content-Length"
++++++++off=32 header_field complete
++++++++off=33 len=1 span[header_value]="3"
++++++++off=36 header_value complete
++++++++off=38 headers complete status=200 v=1/1 flags=20 content_length=3
++++++++off=38 len=3 span[body]="AAA"
++++++++off=41 message complete
++++++++off=43 reset
++++++++off=43 message begin
++++++++off=48 len=3 span[version]="1.1"
++++++++off=51 version complete
++++++++off=56 len=7 span[status]="Created"
++++++++off=65 status complete
++++++++off=65 len=14 span[header_field]="Content-Length"
++++++++off=80 header_field complete
++++++++off=81 len=1 span[header_value]="4"
++++++++off=84 header_value complete
++++++++off=86 headers complete status=201 v=1/1 flags=20 content_length=4
++++++++off=86 len=4 span[body]="BBBB"
++++++++off=90 message complete
++++++++off=92 reset
++++++++off=92 message begin
++++++++off=97 len=3 span[version]="1.1"
++++++++off=100 version complete
++++++++off=105 len=8 span[status]="Accepted"
++++++++off=115 status complete
++++++++off=115 len=14 span[header_field]="Content-Length"
++++++++off=130 header_field complete
++++++++off=131 len=1 span[header_value]="5"
++++++++off=134 header_value complete
++++++++off=136 headers complete status=202 v=1/1 flags=20 content_length=5
++++++++off=136 len=4 span[body]="CCCC"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Sample responses
++++++++================
++++++++
++++++++## Simple response
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Header1: Value1
++++++++Header2:\t Value2
++++++++Content-Length: 0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=7 span[header_field]="Header1"
++++++++off=25 header_field complete
++++++++off=26 len=6 span[header_value]="Value1"
++++++++off=34 header_value complete
++++++++off=34 len=7 span[header_field]="Header2"
++++++++off=42 header_field complete
++++++++off=44 len=6 span[header_value]="Value2"
++++++++off=52 header_value complete
++++++++off=52 len=14 span[header_field]="Content-Length"
++++++++off=67 header_field complete
++++++++off=68 len=1 span[header_value]="0"
++++++++off=71 header_value complete
++++++++off=73 headers complete status=200 v=1/1 flags=20 content_length=0
++++++++off=73 message complete
++++++++```
++++++++
++++++++## Error on invalid response start
++++++++
++++++++Every response must start with `HTTP/`.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTPER/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=4 error code=8 reason="Expected HTTP/"
++++++++```
++++++++
++++++++## Empty body should not trigger spurious span callbacks
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=19 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++```
++++++++
++++++++## Google 301
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 301 Moved Permanently
++++++++Location: http://www.google.com/
++++++++Content-Type: text/html; charset=UTF-8
++++++++Date: Sun, 26 Apr 2009 11:11:49 GMT
++++++++Expires: Tue, 26 May 2009 11:11:49 GMT
++++++++X-$PrototypeBI-Version: 1.6.0.3
++++++++Cache-Control: public, max-age=2592000
++++++++Server: gws
++++++++Content-Length: 219
++++++++
++++++++<HTML><HEAD><meta http-equiv=content-type content=text/html;charset=utf-8>\n\
++++++++<TITLE>301 Moved</TITLE></HEAD><BODY>\n\
++++++++<H1>301 Moved</H1>\n\
++++++++The document has moved\n\
++++++++<A HREF="http://www.google.com/">here</A>.
++++++++</BODY></HTML>
++++++++```
++++++++_(Note the `$` char in header field)_
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=17 span[status]="Moved Permanently"
++++++++off=32 status complete
++++++++off=32 len=8 span[header_field]="Location"
++++++++off=41 header_field complete
++++++++off=42 len=22 span[header_value]="http://www.google.com/"
++++++++off=66 header_value complete
++++++++off=66 len=12 span[header_field]="Content-Type"
++++++++off=79 header_field complete
++++++++off=80 len=24 span[header_value]="text/html; charset=UTF-8"
++++++++off=106 header_value complete
++++++++off=106 len=4 span[header_field]="Date"
++++++++off=111 header_field complete
++++++++off=112 len=29 span[header_value]="Sun, 26 Apr 2009 11:11:49 GMT"
++++++++off=143 header_value complete
++++++++off=143 len=7 span[header_field]="Expires"
++++++++off=151 header_field complete
++++++++off=152 len=29 span[header_value]="Tue, 26 May 2009 11:11:49 GMT"
++++++++off=183 header_value complete
++++++++off=183 len=22 span[header_field]="X-$PrototypeBI-Version"
++++++++off=206 header_field complete
++++++++off=207 len=7 span[header_value]="1.6.0.3"
++++++++off=216 header_value complete
++++++++off=216 len=13 span[header_field]="Cache-Control"
++++++++off=230 header_field complete
++++++++off=231 len=23 span[header_value]="public, max-age=2592000"
++++++++off=256 header_value complete
++++++++off=256 len=6 span[header_field]="Server"
++++++++off=263 header_field complete
++++++++off=264 len=3 span[header_value]="gws"
++++++++off=269 header_value complete
++++++++off=269 len=14 span[header_field]="Content-Length"
++++++++off=284 header_field complete
++++++++off=286 len=5 span[header_value]="219 "
++++++++off=293 header_value complete
++++++++off=295 headers complete status=301 v=1/1 flags=20 content_length=219
++++++++off=295 len=74 span[body]="<HTML><HEAD><meta http-equiv=content-type content=text/html;charset=utf-8>"
++++++++off=369 len=1 span[body]=lf
++++++++off=370 len=37 span[body]="<TITLE>301 Moved</TITLE></HEAD><BODY>"
++++++++off=407 len=1 span[body]=lf
++++++++off=408 len=18 span[body]="<H1>301 Moved</H1>"
++++++++off=426 len=1 span[body]=lf
++++++++off=427 len=22 span[body]="The document has moved"
++++++++off=449 len=1 span[body]=lf
++++++++off=450 len=42 span[body]="<A HREF="http://www.google.com/">here</A>."
++++++++off=492 len=1 span[body]=cr
++++++++off=493 len=1 span[body]=lf
++++++++off=494 len=14 span[body]="</BODY></HTML>"
++++++++```
++++++++
++++++++## amazon.com
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 301 MovedPermanently
++++++++Date: Wed, 15 May 2013 17:06:33 GMT
++++++++Server: Server
++++++++x-amz-id-1: 0GPHKXSJQ826RK7GZEB2
++++++++p3p: policyref="http://www.amazon.com/w3c/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC "
++++++++x-amz-id-2: STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD
++++++++Location: http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846
++++++++Vary: Accept-Encoding,User-Agent
++++++++Content-Type: text/html; charset=ISO-8859-1
++++++++Transfer-Encoding: chunked
++++++++
++++++++1
++++++++\n
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=16 span[status]="MovedPermanently"
++++++++off=31 status complete
++++++++off=31 len=4 span[header_field]="Date"
++++++++off=36 header_field complete
++++++++off=37 len=29 span[header_value]="Wed, 15 May 2013 17:06:33 GMT"
++++++++off=68 header_value complete
++++++++off=68 len=6 span[header_field]="Server"
++++++++off=75 header_field complete
++++++++off=76 len=6 span[header_value]="Server"
++++++++off=84 header_value complete
++++++++off=84 len=10 span[header_field]="x-amz-id-1"
++++++++off=95 header_field complete
++++++++off=96 len=20 span[header_value]="0GPHKXSJQ826RK7GZEB2"
++++++++off=118 header_value complete
++++++++off=118 len=3 span[header_field]="p3p"
++++++++off=122 header_field complete
++++++++off=123 len=178 span[header_value]="policyref="http://www.amazon.com/w3c/p3p.xml",CP="CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC ""
++++++++off=303 header_value complete
++++++++off=303 len=10 span[header_field]="x-amz-id-2"
++++++++off=314 header_field complete
++++++++off=315 len=64 span[header_value]="STN69VZxIFSz9YJLbz1GDbxpbjG6Qjmmq5E3DxRhOUw+Et0p4hr7c/Q8qNcx4oAD"
++++++++off=381 header_value complete
++++++++off=381 len=8 span[header_field]="Location"
++++++++off=390 header_field complete
++++++++off=391 len=214 span[header_value]="http://www.amazon.com/Dan-Brown/e/B000AP9DSU/ref=s9_pop_gw_al1?_encoding=UTF8&refinementId=618073011&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=0SHYY5BZXN3KR20BNFAY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846"
++++++++off=607 header_value complete
++++++++off=607 len=4 span[header_field]="Vary"
++++++++off=612 header_field complete
++++++++off=613 len=26 span[header_value]="Accept-Encoding,User-Agent"
++++++++off=641 header_value complete
++++++++off=641 len=12 span[header_field]="Content-Type"
++++++++off=654 header_field complete
++++++++off=655 len=29 span[header_value]="text/html; charset=ISO-8859-1"
++++++++off=686 header_value complete
++++++++off=686 len=17 span[header_field]="Transfer-Encoding"
++++++++off=704 header_field complete
++++++++off=705 len=7 span[header_value]="chunked"
++++++++off=714 header_value complete
++++++++off=716 headers complete status=301 v=1/1 flags=208 content_length=0
++++++++off=719 chunk header len=1
++++++++off=719 len=1 span[body]=lf
++++++++off=722 chunk complete
++++++++off=725 chunk header len=0
++++++++off=727 chunk complete
++++++++off=727 message complete
++++++++```
++++++++
++++++++## No headers and no body
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 404 Not Found
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=9 span[status]="Not Found"
++++++++off=24 status complete
++++++++off=26 headers complete status=404 v=1/1 flags=0 content_length=0
++++++++```
++++++++
++++++++## No reason phrase
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 301
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=14 status complete
++++++++off=16 headers complete status=301 v=1/1 flags=0 content_length=0
++++++++```
++++++++
++++++++## Empty reason phrase after space
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 \r\n\
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=0 span[status]=""
++++++++off=15 status complete
++++++++off=17 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++```
++++++++
++++++++## No carriage ret
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\n\
++++++++Content-Type: text/html; charset=utf-8\n\
++++++++Connection: close\n\
++++++++\n\
++++++++these headers are from http://news.ycombinator.com/
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 error code=25 reason="Missing expected CR after response line"
++++++++```
++++++++
++++++++## No carriage ret (lenient)
++++++++
++++++++<!-- meta={"type": "response-lenient-optional-cr-before-lf"} -->
++++++++```http
++++++++HTTP/1.1 200 OK\n\
++++++++Content-Type: text/html; charset=utf-8\n\
++++++++Connection: close\n\
++++++++\n\
++++++++these headers are from http://news.ycombinator.com/
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=16 status complete
++++++++off=16 len=12 span[header_field]="Content-Type"
++++++++off=29 header_field complete
++++++++off=30 len=24 span[header_value]="text/html; charset=utf-8"
++++++++off=55 header_value complete
++++++++off=55 len=10 span[header_field]="Connection"
++++++++off=66 header_field complete
++++++++off=67 len=5 span[header_value]="close"
++++++++off=73 header_value complete
++++++++off=74 headers complete status=200 v=1/1 flags=2 content_length=0
++++++++off=74 len=51 span[body]="these headers are from http://news.ycombinator.com/"
++++++++```
++++++++
++++++++## Underscore in header key
++++++++
++++++++Shown by: `curl -o /dev/null -v "http://ad.doubleclick.net/pfadx/DARTSHELLCONFIGXML;dcmt=text/xml;"`
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Server: DCLK-AdSvr
++++++++Content-Type: text/xml
++++++++Content-Length: 0
++++++++DCLK_imp: v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=6 span[header_field]="Server"
++++++++off=24 header_field complete
++++++++off=25 len=10 span[header_value]="DCLK-AdSvr"
++++++++off=37 header_value complete
++++++++off=37 len=12 span[header_field]="Content-Type"
++++++++off=50 header_field complete
++++++++off=51 len=8 span[header_value]="text/xml"
++++++++off=61 header_value complete
++++++++off=61 len=14 span[header_field]="Content-Length"
++++++++off=76 header_field complete
++++++++off=77 len=1 span[header_value]="0"
++++++++off=80 header_value complete
++++++++off=80 len=8 span[header_field]="DCLK_imp"
++++++++off=89 header_field complete
++++++++off=90 len=81 span[header_value]="v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o"
++++++++off=173 header_value complete
++++++++off=175 headers complete status=200 v=1/1 flags=20 content_length=0
++++++++off=175 message complete
++++++++```
++++++++
++++++++## bonjourmadame.fr
++++++++
++++++++The client should not merge two headers fields when the first one doesn't
++++++++have a value.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.0 301 Moved Permanently
++++++++Date: Thu, 03 Jun 2010 09:56:32 GMT
++++++++Server: Apache/2.2.3 (Red Hat)
++++++++Cache-Control: public
++++++++Pragma: \r\n\
++++++++Location: http://www.bonjourmadame.fr/
++++++++Vary: Accept-Encoding
++++++++Content-Length: 0
++++++++Content-Type: text/html; charset=UTF-8
++++++++Connection: keep-alive
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.0"
++++++++off=8 version complete
++++++++off=13 len=17 span[status]="Moved Permanently"
++++++++off=32 status complete
++++++++off=32 len=4 span[header_field]="Date"
++++++++off=37 header_field complete
++++++++off=38 len=29 span[header_value]="Thu, 03 Jun 2010 09:56:32 GMT"
++++++++off=69 header_value complete
++++++++off=69 len=6 span[header_field]="Server"
++++++++off=76 header_field complete
++++++++off=77 len=22 span[header_value]="Apache/2.2.3 (Red Hat)"
++++++++off=101 header_value complete
++++++++off=101 len=13 span[header_field]="Cache-Control"
++++++++off=115 header_field complete
++++++++off=116 len=6 span[header_value]="public"
++++++++off=124 header_value complete
++++++++off=124 len=6 span[header_field]="Pragma"
++++++++off=131 header_field complete
++++++++off=134 len=0 span[header_value]=""
++++++++off=134 header_value complete
++++++++off=134 len=8 span[header_field]="Location"
++++++++off=143 header_field complete
++++++++off=144 len=28 span[header_value]="http://www.bonjourmadame.fr/"
++++++++off=174 header_value complete
++++++++off=174 len=4 span[header_field]="Vary"
++++++++off=179 header_field complete
++++++++off=180 len=15 span[header_value]="Accept-Encoding"
++++++++off=197 header_value complete
++++++++off=197 len=14 span[header_field]="Content-Length"
++++++++off=212 header_field complete
++++++++off=213 len=1 span[header_value]="0"
++++++++off=216 header_value complete
++++++++off=216 len=12 span[header_field]="Content-Type"
++++++++off=229 header_field complete
++++++++off=230 len=24 span[header_value]="text/html; charset=UTF-8"
++++++++off=256 header_value complete
++++++++off=256 len=10 span[header_field]="Connection"
++++++++off=267 header_field complete
++++++++off=268 len=10 span[header_value]="keep-alive"
++++++++off=280 header_value complete
++++++++off=282 headers complete status=301 v=1/0 flags=21 content_length=0
++++++++off=282 message complete
++++++++```
++++++++
++++++++## Spaces in header value
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Date: Tue, 28 Sep 2010 01:14:13 GMT
++++++++Server: Apache
++++++++Cache-Control: no-cache, must-revalidate
++++++++Expires: Mon, 26 Jul 1997 05:00:00 GMT
++++++++.et-Cookie: PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com
++++++++Vary: Accept-Encoding
++++++++_eep-Alive: timeout=45
++++++++_onnection: Keep-Alive
++++++++Transfer-Encoding: chunked
++++++++Content-Type: text/html
++++++++Connection: close
++++++++
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Date"
++++++++off=22 header_field complete
++++++++off=23 len=29 span[header_value]="Tue, 28 Sep 2010 01:14:13 GMT"
++++++++off=54 header_value complete
++++++++off=54 len=6 span[header_field]="Server"
++++++++off=61 header_field complete
++++++++off=62 len=6 span[header_value]="Apache"
++++++++off=70 header_value complete
++++++++off=70 len=13 span[header_field]="Cache-Control"
++++++++off=84 header_field complete
++++++++off=85 len=25 span[header_value]="no-cache, must-revalidate"
++++++++off=112 header_value complete
++++++++off=112 len=7 span[header_field]="Expires"
++++++++off=120 header_field complete
++++++++off=121 len=29 span[header_value]="Mon, 26 Jul 1997 05:00:00 GMT"
++++++++off=152 header_value complete
++++++++off=152 len=10 span[header_field]=".et-Cookie"
++++++++off=163 header_field complete
++++++++off=164 len=54 span[header_value]="PlaxoCS=1274804622353690521; path=/; domain=.plaxo.com"
++++++++off=220 header_value complete
++++++++off=220 len=4 span[header_field]="Vary"
++++++++off=225 header_field complete
++++++++off=226 len=15 span[header_value]="Accept-Encoding"
++++++++off=243 header_value complete
++++++++off=243 len=10 span[header_field]="_eep-Alive"
++++++++off=254 header_field complete
++++++++off=255 len=10 span[header_value]="timeout=45"
++++++++off=267 header_value complete
++++++++off=267 len=10 span[header_field]="_onnection"
++++++++off=278 header_field complete
++++++++off=279 len=10 span[header_value]="Keep-Alive"
++++++++off=291 header_value complete
++++++++off=291 len=17 span[header_field]="Transfer-Encoding"
++++++++off=309 header_field complete
++++++++off=310 len=7 span[header_value]="chunked"
++++++++off=319 header_value complete
++++++++off=319 len=12 span[header_field]="Content-Type"
++++++++off=332 header_field complete
++++++++off=333 len=9 span[header_value]="text/html"
++++++++off=344 header_value complete
++++++++off=344 len=10 span[header_field]="Connection"
++++++++off=355 header_field complete
++++++++off=356 len=5 span[header_value]="close"
++++++++off=363 header_value complete
++++++++off=365 headers complete status=200 v=1/1 flags=20a content_length=0
++++++++off=368 chunk header len=0
++++++++off=370 chunk complete
++++++++off=370 message complete
++++++++```
++++++++
++++++++## Spaces in header name
++++++++
++++++++<!-- meta={"type": "response", "noScan": true} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Server: Microsoft-IIS/6.0
++++++++X-Powered-By: ASP.NET
++++++++en-US Content-Type: text/xml
++++++++Content-Type: text/xml
++++++++Content-Length: 16
++++++++Date: Fri, 23 Jul 2010 18:45:38 GMT
++++++++Connection: keep-alive
++++++++
++++++++<xml>hello</xml>
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=6 span[header_field]="Server"
++++++++off=24 header_field complete
++++++++off=25 len=17 span[header_value]="Microsoft-IIS/6.0"
++++++++off=44 header_value complete
++++++++off=44 len=12 span[header_field]="X-Powered-By"
++++++++off=57 header_field complete
++++++++off=58 len=7 span[header_value]="ASP.NET"
++++++++off=67 header_value complete
++++++++off=72 error code=10 reason="Invalid header token"
++++++++```
++++++++
++++++++## Non ASCII in status line
++++++++
++++++++<!-- meta={"type": "response", "noScan": true} -->
++++++++```http
++++++++HTTP/1.1 500 Oriëntatieprobleem
++++++++Date: Fri, 5 Nov 2010 23:07:12 GMT+2
++++++++Content-Length: 0
++++++++Connection: close
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=19 span[status]="Oriëntatieprobleem"
++++++++off=34 status complete
++++++++off=34 len=4 span[header_field]="Date"
++++++++off=39 header_field complete
++++++++off=40 len=30 span[header_value]="Fri, 5 Nov 2010 23:07:12 GMT+2"
++++++++off=72 header_value complete
++++++++off=72 len=14 span[header_field]="Content-Length"
++++++++off=87 header_field complete
++++++++off=88 len=1 span[header_value]="0"
++++++++off=91 header_value complete
++++++++off=91 len=10 span[header_field]="Connection"
++++++++off=102 header_field complete
++++++++off=103 len=5 span[header_value]="close"
++++++++off=110 header_value complete
++++++++off=112 headers complete status=500 v=1/1 flags=22 content_length=0
++++++++off=112 message complete
++++++++```
++++++++
++++++++## HTTP version 0.9
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/0.9 200 OK
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="0.9"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=19 headers complete status=200 v=0/9 flags=0 content_length=0
++++++++```
++++++++
++++++++## No Content-Length, no Transfer-Encoding
++++++++
++++++++The client should wait for the server's EOF. That is, when neither
++++++++content-length nor transfer-encoding is specified, the end of body
++++++++is specified by the EOF.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Type: text/plain
++++++++
++++++++hello world
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=12 span[header_field]="Content-Type"
++++++++off=30 header_field complete
++++++++off=31 len=10 span[header_value]="text/plain"
++++++++off=43 header_value complete
++++++++off=45 headers complete status=200 v=1/1 flags=0 content_length=0
++++++++off=45 len=11 span[body]="hello world"
++++++++```
++++++++
++++++++## Response starting with CRLF
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++\r\nHTTP/1.1 200 OK
++++++++Header1: Value1
++++++++Header2:\t Value2
++++++++Content-Length: 0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=2 message begin
++++++++off=7 len=3 span[version]="1.1"
++++++++off=10 version complete
++++++++off=15 len=2 span[status]="OK"
++++++++off=19 status complete
++++++++off=19 len=7 span[header_field]="Header1"
++++++++off=27 header_field complete
++++++++off=28 len=6 span[header_value]="Value1"
++++++++off=36 header_value complete
++++++++off=36 len=7 span[header_field]="Header2"
++++++++off=44 header_field complete
++++++++off=46 len=6 span[header_value]="Value2"
++++++++off=54 header_value complete
++++++++off=54 len=14 span[header_field]="Content-Length"
++++++++off=69 header_field complete
++++++++off=70 len=1 span[header_value]="0"
++++++++off=73 header_value complete
++++++++off=75 headers complete status=200 v=1/1 flags=20 content_length=0
++++++++off=75 message complete
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++Transfer-Encoding header
++++++++========================
++++++++
++++++++## Trailing space on chunked body
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Content-Type: text/plain
++++++++Transfer-Encoding: chunked
++++++++
++++++++25 \r\n\
++++++++This is the data in the first chunk
++++++++
++++++++1C
++++++++and this is the second one
++++++++
++++++++0 \r\n\
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=12 span[header_field]="Content-Type"
++++++++off=30 header_field complete
++++++++off=31 len=10 span[header_value]="text/plain"
++++++++off=43 header_value complete
++++++++off=43 len=17 span[header_field]="Transfer-Encoding"
++++++++off=61 header_field complete
++++++++off=62 len=7 span[header_value]="chunked"
++++++++off=71 header_value complete
++++++++off=73 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=76 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++## `chunked` before other transfer-encoding
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Accept: */*
++++++++Transfer-Encoding: chunked, deflate
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=6 span[header_field]="Accept"
++++++++off=24 header_field complete
++++++++off=25 len=3 span[header_value]="*/*"
++++++++off=30 header_value complete
++++++++off=30 len=17 span[header_field]="Transfer-Encoding"
++++++++off=48 header_field complete
++++++++off=49 len=16 span[header_value]="chunked, deflate"
++++++++off=67 header_value complete
++++++++off=69 headers complete status=200 v=1/1 flags=200 content_length=0
++++++++off=69 len=5 span[body]="World"
++++++++```
++++++++
++++++++## multiple transfer-encoding where chunked is not the last one
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Accept: */*
++++++++Transfer-Encoding: chunked
++++++++Transfer-Encoding: identity
++++++++
++++++++World
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=6 span[header_field]="Accept"
++++++++off=24 header_field complete
++++++++off=25 len=3 span[header_value]="*/*"
++++++++off=30 header_value complete
++++++++off=30 len=17 span[header_field]="Transfer-Encoding"
++++++++off=48 header_field complete
++++++++off=49 len=7 span[header_value]="chunked"
++++++++off=58 header_value complete
++++++++off=58 len=17 span[header_field]="Transfer-Encoding"
++++++++off=76 header_field complete
++++++++off=77 len=8 span[header_value]="identity"
++++++++off=87 header_value complete
++++++++off=89 headers complete status=200 v=1/1 flags=200 content_length=0
++++++++off=89 len=5 span[body]="World"
++++++++```
++++++++
++++++++## `chunkedchunked` transfer-encoding does not enable chunked enconding
++++++++
++++++++This check that the word `chunked` repeat more than once (with or without spaces) does not mistakenly enables chunked encoding.
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Accept: */*
++++++++Transfer-Encoding: chunkedchunked
++++++++
++++++++2
++++++++OK
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=6 span[header_field]="Accept"
++++++++off=24 header_field complete
++++++++off=25 len=3 span[header_value]="*/*"
++++++++off=30 header_value complete
++++++++off=30 len=17 span[header_field]="Transfer-Encoding"
++++++++off=48 header_field complete
++++++++off=49 len=14 span[header_value]="chunkedchunked"
++++++++off=65 header_value complete
++++++++off=67 headers complete status=200 v=1/1 flags=200 content_length=0
++++++++off=67 len=1 span[body]="2"
++++++++off=68 len=1 span[body]=cr
++++++++off=69 len=1 span[body]=lf
++++++++off=70 len=2 span[body]="OK"
++++++++off=72 len=1 span[body]=cr
++++++++off=73 len=1 span[body]=lf
++++++++off=74 len=1 span[body]="0"
++++++++off=75 len=1 span[body]=cr
++++++++off=76 len=1 span[body]=lf
++++++++off=77 len=1 span[body]=cr
++++++++off=78 len=1 span[body]=lf
++++++++```
++++++++
++++++++## Chunk extensions
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Host: localhost
++++++++Transfer-encoding: chunked
++++++++
++++++++5;ilovew3;somuchlove=aretheseparametersfor
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=9 span[header_value]="localhost"
++++++++off=34 header_value complete
++++++++off=34 len=17 span[header_field]="Transfer-encoding"
++++++++off=52 header_field complete
++++++++off=53 len=7 span[header_value]="chunked"
++++++++off=62 header_value complete
++++++++off=64 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=66 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=74 chunk_extension_name complete
++++++++off=74 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=85 chunk_extension_name complete
++++++++off=85 len=21 span[chunk_extension_value]="aretheseparametersfor"
++++++++off=107 chunk_extension_value complete
++++++++off=108 chunk header len=5
++++++++off=108 len=5 span[body]="hello"
++++++++off=115 chunk complete
++++++++off=117 len=8 span[chunk_extension_name]="blahblah"
++++++++off=126 chunk_extension_name complete
++++++++off=126 len=4 span[chunk_extension_name]="blah"
++++++++off=131 chunk_extension_name complete
++++++++off=132 chunk header len=6
++++++++off=132 len=6 span[body]=" world"
++++++++off=140 chunk complete
++++++++off=143 chunk header len=0
++++++++off=145 chunk complete
++++++++off=145 message complete
++++++++```
++++++++
++++++++## No semicolon before chunk extensions
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Host: localhost
++++++++Transfer-encoding: chunked
++++++++
++++++++2 erfrferferf
++++++++aa
++++++++0 rrrr
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=9 span[header_value]="localhost"
++++++++off=34 header_value complete
++++++++off=34 len=17 span[header_field]="Transfer-encoding"
++++++++off=52 header_field complete
++++++++off=53 len=7 span[header_value]="chunked"
++++++++off=62 header_value complete
++++++++off=64 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=66 error code=12 reason="Invalid character in chunk size"
++++++++```
++++++++
++++++++
++++++++## No extension after semicolon
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Host: localhost
++++++++Transfer-encoding: chunked
++++++++
++++++++2;
++++++++aa
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=9 span[header_value]="localhost"
++++++++off=34 header_value complete
++++++++off=34 len=17 span[header_field]="Transfer-encoding"
++++++++off=52 header_field complete
++++++++off=53 len=7 span[header_value]="chunked"
++++++++off=62 header_value complete
++++++++off=64 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=67 error code=2 reason="Invalid character in chunk extensions"
++++++++```
++++++++
++++++++
++++++++## Chunk extensions quoting
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Host: localhost
++++++++Transfer-Encoding: chunked
++++++++
++++++++5;ilovew3="I love; extensions";somuchlove="aretheseparametersfor";blah;foo=bar
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=9 span[header_value]="localhost"
++++++++off=34 header_value complete
++++++++off=34 len=17 span[header_field]="Transfer-Encoding"
++++++++off=52 header_field complete
++++++++off=53 len=7 span[header_value]="chunked"
++++++++off=62 header_value complete
++++++++off=64 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=66 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=74 chunk_extension_name complete
++++++++off=74 len=20 span[chunk_extension_value]=""I love; extensions""
++++++++off=94 chunk_extension_value complete
++++++++off=95 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=106 chunk_extension_name complete
++++++++off=106 len=23 span[chunk_extension_value]=""aretheseparametersfor""
++++++++off=129 chunk_extension_value complete
++++++++off=130 len=4 span[chunk_extension_name]="blah"
++++++++off=135 chunk_extension_name complete
++++++++off=135 len=3 span[chunk_extension_name]="foo"
++++++++off=139 chunk_extension_name complete
++++++++off=139 len=3 span[chunk_extension_value]="bar"
++++++++off=143 chunk_extension_value complete
++++++++off=144 chunk header len=5
++++++++off=144 len=5 span[body]="hello"
++++++++off=151 chunk complete
++++++++off=153 len=8 span[chunk_extension_name]="blahblah"
++++++++off=162 chunk_extension_name complete
++++++++off=162 len=4 span[chunk_extension_name]="blah"
++++++++off=167 chunk_extension_name complete
++++++++off=168 chunk header len=6
++++++++off=168 len=6 span[body]=" world"
++++++++off=176 chunk complete
++++++++off=179 chunk header len=0
++++++++```
++++++++
++++++++
++++++++## Unbalanced chunk extensions quoting
++++++++
++++++++<!-- meta={"type": "response"} -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Host: localhost
++++++++Transfer-Encoding: chunked
++++++++
++++++++5;ilovew3="abc";somuchlove="def; ghi
++++++++hello
++++++++6;blahblah;blah
++++++++ world
++++++++0
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=4 span[header_field]="Host"
++++++++off=22 header_field complete
++++++++off=23 len=9 span[header_value]="localhost"
++++++++off=34 header_value complete
++++++++off=34 len=17 span[header_field]="Transfer-Encoding"
++++++++off=52 header_field complete
++++++++off=53 len=7 span[header_value]="chunked"
++++++++off=62 header_value complete
++++++++off=64 headers complete status=200 v=1/1 flags=208 content_length=0
++++++++off=66 len=7 span[chunk_extension_name]="ilovew3"
++++++++off=74 chunk_extension_name complete
++++++++off=74 len=5 span[chunk_extension_value]=""abc""
++++++++off=79 chunk_extension_value complete
++++++++off=80 len=10 span[chunk_extension_name]="somuchlove"
++++++++off=91 chunk_extension_name complete
++++++++off=91 len=9 span[chunk_extension_value]=""def; ghi"
++++++++off=101 error code=2 reason="Invalid character in chunk extensions quoted value"
++++++++```
++++++++
++++++++
++++++++## Invalid OBS fold after chunked value
++++++++
++++++++<!-- meta={"type": "response-lenient-headers" } -->
++++++++```http
++++++++HTTP/1.1 200 OK
++++++++Transfer-Encoding: chunked
++++++++ abc
++++++++
++++++++5
++++++++World
++++++++0
++++++++
++++++++
++++++++```
++++++++
++++++++```log
++++++++off=0 message begin
++++++++off=5 len=3 span[version]="1.1"
++++++++off=8 version complete
++++++++off=13 len=2 span[status]="OK"
++++++++off=17 status complete
++++++++off=17 len=17 span[header_field]="Transfer-Encoding"
++++++++off=35 header_field complete
++++++++off=36 len=7 span[header_value]="chunked"
++++++++off=45 len=5 span[header_value]=" abc"
++++++++off=52 header_value complete
++++++++off=54 headers complete status=200 v=1/1 flags=200 content_length=0
++++++++off=54 len=1 span[body]="5"
++++++++off=55 len=1 span[body]=cr
++++++++off=56 len=1 span[body]=lf
++++++++off=57 len=5 span[body]="World"
++++++++off=62 len=1 span[body]=cr
++++++++off=63 len=1 span[body]=lf
++++++++off=64 len=1 span[body]="0"
++++++++off=65 len=1 span[body]=cr
++++++++off=66 len=1 span[body]=lf
++++++++off=67 len=1 span[body]=cr
++++++++off=68 len=1 span[body]=lf
++++++++```
++++++++
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# URL tests
++++++++
++++++++## Absolute URL
++++++++
++++++++```url
++++++++http://example.com/path?query=value#schema
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=11 span[url.host]="example.com"
++++++++off=18 len=5 span[url.path]="/path"
++++++++off=24 len=11 span[url.query]="query=value"
++++++++off=36 len=6 span[url.fragment]="schema"
++++++++```
++++++++
++++++++## Relative URL
++++++++
++++++++```url
++++++++/path?query=value#schema
++++++++```
++++++++
++++++++```log
++++++++off=0 len=5 span[url.path]="/path"
++++++++off=6 len=11 span[url.query]="query=value"
++++++++off=18 len=6 span[url.fragment]="schema"
++++++++```
++++++++
++++++++## Failing on broken schema
++++++++
++++++++<!-- meta={"noScan": true} -->
++++++++```url
++++++++schema:/path?query=value#schema
++++++++```
++++++++
++++++++```log
++++++++off=0 len=6 span[url.schema]="schema"
++++++++off=8 error code=7 reason="Unexpected char in url schema"
++++++++```
++++++++
++++++++## Proxy request
++++++++
++++++++```url
++++++++http://hostname/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=8 span[url.host]="hostname"
++++++++off=15 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Proxy request with port
++++++++
++++++++```url
++++++++http://hostname:444/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=12 span[url.host]="hostname:444"
++++++++off=19 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Proxy IPv6 request
++++++++
++++++++```url
++++++++http://[1:2::3:4]/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=10 span[url.host]="[1:2::3:4]"
++++++++off=17 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Proxy IPv6 request with port
++++++++
++++++++```url
++++++++http://[1:2::3:4]:67/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=13 span[url.host]="[1:2::3:4]:67"
++++++++off=20 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## IPv4 in IPv6 address
++++++++
++++++++```url
++++++++http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=39 span[url.host]="[2001:0000:0000:0000:0000:0000:1.9.1.1]"
++++++++off=46 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Extra `?` in query string
++++++++
++++++++```url
++++++++http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,\
++++++++fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,\
++++++++fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=10 span[url.host]="a.tbcdn.cn"
++++++++off=17 len=12 span[url.path]="/p/fp/2010c/"
++++++++off=30 len=187 span[url.query]="?fp-header-min.css,fp-base-min.css,fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css"
++++++++```
++++++++
++++++++## URL encoded space
++++++++
++++++++```url
++++++++/toto.html?toto=a%20b
++++++++```
++++++++
++++++++```log
++++++++off=0 len=10 span[url.path]="/toto.html"
++++++++off=11 len=10 span[url.query]="toto=a%20b"
++++++++```
++++++++
++++++++## URL fragment
++++++++
++++++++```url
++++++++/toto.html#titi
++++++++```
++++++++
++++++++```log
++++++++off=0 len=10 span[url.path]="/toto.html"
++++++++off=11 len=4 span[url.fragment]="titi"
++++++++```
++++++++
++++++++## Complex URL fragment
++++++++
++++++++```url
++++++++http://www.webmasterworld.com/r.cgi?f=21&d=8405&url=\
++++++++http://www.example.com/index.html?foo=bar&hello=world#midpage
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=22 span[url.host]="www.webmasterworld.com"
++++++++off=29 len=6 span[url.path]="/r.cgi"
++++++++off=36 len=69 span[url.query]="f=21&d=8405&url=http://www.example.com/index.html?foo=bar&hello=world"
++++++++off=106 len=7 span[url.fragment]="midpage"
++++++++```
++++++++
++++++++## Complex URL from node.js url parser doc
++++++++
++++++++```url
++++++++http://host.com:8080/p/a/t/h?query=string#hash
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=13 span[url.host]="host.com:8080"
++++++++off=20 len=8 span[url.path]="/p/a/t/h"
++++++++off=29 len=12 span[url.query]="query=string"
++++++++off=42 len=4 span[url.fragment]="hash"
++++++++```
++++++++
++++++++## Complex URL with basic auth from node.js url parser doc
++++++++
++++++++```url
++++++++http://a:b@host.com:8080/p/a/t/h?query=string#hash
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=17 span[url.host]="a:b@host.com:8080"
++++++++off=24 len=8 span[url.path]="/p/a/t/h"
++++++++off=33 len=12 span[url.query]="query=string"
++++++++off=46 len=4 span[url.fragment]="hash"
++++++++```
++++++++
++++++++## Double `@`
++++++++
++++++++<!-- meta={"noScan": true} -->
++++++++```url
++++++++http://a:b@@hostname:443/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=12 error code=7 reason="Double @ in url"
++++++++```
++++++++
++++++++## Proxy basic auth with url encoded space
++++++++
++++++++```url
++++++++http://a%20:b@host.com/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=15 span[url.host]="a%20:b@host.com"
++++++++off=22 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Proxy basic auth with unreserved chars
++++++++
++++++++```url
++++++++http://a!;-_!=+$@host.com/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=18 span[url.host]="a!;-_!=+$@host.com"
++++++++off=25 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## IPv6 address with Zone ID
++++++++
++++++++```url
++++++++http://[fe80::a%25eth0]/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=16 span[url.host]="[fe80::a%25eth0]"
++++++++off=23 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## IPv6 address with Zone ID, but `%` is not percent-encoded
++++++++
++++++++```url
++++++++http://[fe80::a%eth0]/
++++++++```
++++++++
++++++++```log
++++++++off=0 len=4 span[url.schema]="http"
++++++++off=7 len=14 span[url.host]="[fe80::a%eth0]"
++++++++off=21 len=1 span[url.path]="/"
++++++++```
++++++++
++++++++## Disallow tab in URL
++++++++
++++++++<!-- meta={ "noScan": true} -->
++++++++```url
++++++++/foo\tbar/
++++++++```
++++++++
++++++++```log
++++++++off=5 error code=7 reason="Invalid characters in url"
++++++++```
++++++++
++++++++## Disallow form-feed in URL
++++++++
++++++++<!-- meta={ "noScan": true} -->
++++++++```url
++++++++/foo\fbar/
++++++++```
++++++++
++++++++```log
++++++++off=5 error code=7 reason="Invalid characters in url"
++++++++```
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "compilerOptions": {
++++++++ "strict": true,
++++++++ "target": "es2017",
++++++++ "module": "commonjs",
++++++++ "moduleResolution": "node",
++++++++ "outDir": "./lib",
++++++++ "declaration": true,
++++++++ "pretty": true,
++++++++ "sourceMap": true
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "extends": "./tsconfig.base.json",
++++++++ "include": [
++++++++ "**/*.ts"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "extends": "./tsconfig.base.json",
++++++++ "include": [
++++++++ "src/**/*.ts"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node_modules/
++++++++npm-debug.log
++++++++lib/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++sudo: false
++++++++language: node_js
++++++++node_js:
++++++++ - "stable"
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# llparse-builder
++++++++[](http://travis-ci.org/indutny/llparse-builder)
++++++++[](https://badge.fury.io/js/llparse-builder)
++++++++
++++++++See [llparse][0].
++++++++
++++++++#### LICENSE
++++++++
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2018.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
++++++++
++++++++[0]: https://github.com/indutny/llparse
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse-builder",
++++++++ "version": "1.5.2",
++++++++ "lockfileVersion": 1,
++++++++ "requires": true,
++++++++ "dependencies": {
++++++++ "@babel/code-frame": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.4.tgz",
++++++++ "integrity": "sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/highlight": "^7.10.4"
++++++++ }
++++++++ },
++++++++ "@babel/helper-validator-identifier": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.4.tgz",
++++++++ "integrity": "sha512-3U9y+43hz7ZM+rzG24Qe2mufW5KhvFg/NhnNph+i9mgCtdTCtMJuI1TMkrIUiK7Ix4PYlRF9I5dhqaLYA/ADXw==",
++++++++ "dev": true
++++++++ },
++++++++ "@babel/highlight": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.10.4.tgz",
++++++++ "integrity": "sha512-i6rgnR/YgPEQzZZnbTHHuZdlE8qyoBNalD6F+q4vAFlcMEcqmkoG+mPqJYJCo63qPf74+Y1UZsl3l6f7/RIkmA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/helper-validator-identifier": "^7.10.4",
++++++++ "chalk": "^2.0.0",
++++++++ "js-tokens": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ=="
++++++++ },
++++++++ "@types/mocha": {
++++++++ "version": "8.0.3",
++++++++ "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.3.tgz",
++++++++ "integrity": "sha512-vyxR57nv8NfcU0GZu8EUXZLTbCMupIUwy95LJ6lllN+JRPG25CwMHoB1q5xKh8YKhQnHYRAn4yW2yuHbf/5xgg==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/node": {
++++++++ "version": "14.11.8",
++++++++ "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz",
++++++++ "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-colors": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz",
++++++++ "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-regex": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
++++++++ "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-styles": {
++++++++ "version": "3.2.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
++++++++ "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^1.9.0"
++++++++ }
++++++++ },
++++++++ "anymatch": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz",
++++++++ "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "normalize-path": "^3.0.0",
++++++++ "picomatch": "^2.0.4"
++++++++ }
++++++++ },
++++++++ "arg": {
++++++++ "version": "4.1.3",
++++++++ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
++++++++ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
++++++++ "dev": true
++++++++ },
++++++++ "argparse": {
++++++++ "version": "1.0.10",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
++++++++ "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "sprintf-js": "~1.0.2"
++++++++ }
++++++++ },
++++++++ "array.prototype.map": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/array.prototype.map/-/array.prototype.map-1.0.2.tgz",
++++++++ "integrity": "sha512-Az3OYxgsa1g7xDYp86l0nnN4bcmuEITGe1rbdEBVkrqkzMgDcbdQ2R7r41pNzti+4NMces3H8gMmuioZUilLgw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "es-array-method-boxes-properly": "^1.0.0",
++++++++ "is-string": "^1.0.4"
++++++++ }
++++++++ },
++++++++ "balanced-match": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
++++++++ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
++++++++ "dev": true
++++++++ },
++++++++ "binary-extensions": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
++++++++ "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==",
++++++++ "dev": true
++++++++ },
++++++++ "binary-search": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/binary-search/-/binary-search-1.3.6.tgz",
++++++++ "integrity": "sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA=="
++++++++ },
++++++++ "brace-expansion": {
++++++++ "version": "1.1.11",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
++++++++ "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "balanced-match": "^1.0.0",
++++++++ "concat-map": "0.0.1"
++++++++ }
++++++++ },
++++++++ "braces": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
++++++++ "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fill-range": "^7.0.1"
++++++++ }
++++++++ },
++++++++ "browser-stdout": {
++++++++ "version": "1.3.1",
++++++++ "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
++++++++ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
++++++++ "dev": true
++++++++ },
++++++++ "buffer-from": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
++++++++ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
++++++++ "dev": true
++++++++ },
++++++++ "builtin-modules": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
++++++++ "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
++++++++ "dev": true
++++++++ },
++++++++ "camelcase": {
++++++++ "version": "5.3.1",
++++++++ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
++++++++ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
++++++++ "dev": true
++++++++ },
++++++++ "chalk": {
++++++++ "version": "2.3.2",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.3.2.tgz",
++++++++ "integrity": "sha512-ZM4j2/ld/YZDc3Ma8PgN7gyAk+kHMMMyzLNryCPGhWrsfAuDVeuid5bpRFTDgMH9JBK2lA4dyyAkkZYF/WcqDQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.1",
++++++++ "escape-string-regexp": "^1.0.5",
++++++++ "supports-color": "^5.3.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "has-flag": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
++++++++ "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "5.3.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.3.0.tgz",
++++++++ "integrity": "sha512-0aP01LLIskjKs3lq52EC0aGBAJhLq7B2Rd8HC/DR/PtNNpcLilNmHC12O+hu0usQpo7wtHNRqtrhBwtDb0+dNg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^3.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "chokidar": {
++++++++ "version": "3.4.2",
++++++++ "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.4.2.tgz",
++++++++ "integrity": "sha512-IZHaDeBeI+sZJRX7lGcXsdzgvZqKv6sECqsbErJA4mHWfpRrD8B97kSFN4cQz6nGBGiuFia1MKR4d6c1o8Cv7A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "anymatch": "~3.1.1",
++++++++ "braces": "~3.0.2",
++++++++ "fsevents": "~2.1.2",
++++++++ "glob-parent": "~5.1.0",
++++++++ "is-binary-path": "~2.1.0",
++++++++ "is-glob": "~4.0.1",
++++++++ "normalize-path": "~3.0.0",
++++++++ "readdirp": "~3.4.0"
++++++++ }
++++++++ },
++++++++ "cliui": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz",
++++++++ "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^3.1.0",
++++++++ "strip-ansi": "^5.2.0",
++++++++ "wrap-ansi": "^5.1.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "1.9.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.1.tgz",
++++++++ "integrity": "sha512-mjGanIiwQJskCC18rPR6OmrZ6fm2Lc7PeGFYwCmy5J34wC6F1PzdGL6xeMfmgicfYcNLGuVFA3WzXtIDCQSZxQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "^1.1.1"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
++++++++ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
++++++++ "dev": true
++++++++ },
++++++++ "commander": {
++++++++ "version": "2.15.1",
++++++++ "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
++++++++ "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
++++++++ "dev": true
++++++++ },
++++++++ "concat-map": {
++++++++ "version": "0.0.1",
++++++++ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
++++++++ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
++++++++ "dev": true
++++++++ },
++++++++ "debug": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz",
++++++++ "integrity": "sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==",
++++++++ "requires": {
++++++++ "ms": "2.1.2"
++++++++ }
++++++++ },
++++++++ "decamelize": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
++++++++ "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
++++++++ "dev": true
++++++++ },
++++++++ "define-properties": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
++++++++ "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
++++++++ "requires": {
++++++++ "object-keys": "^1.0.12"
++++++++ }
++++++++ },
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "7.0.3",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
++++++++ "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-abstract": {
++++++++ "version": "1.17.7",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.7.tgz",
++++++++ "integrity": "sha512-VBl/gnfcJ7OercKA9MVaegWsBHFjV492syMudcnQZvt/Dw8ezpcOHYZXa/J96O8vx+g4x65YKhxOwDUh63aS5g==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.1.tgz",
++++++++ "integrity": "sha512-VT/cxmx5yaoHSOTSyrCygIDFco+RsibY2NM0a4RdEeY/4KgqezwFtK1yr3U67xYhqJSlASm2pKhLVzPj2lr4bA==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.18.0-next.0",
++++++++ "has-symbols": "^1.0.1",
++++++++ "object-keys": "^1.1.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "es-array-method-boxes-properly": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz",
++++++++ "integrity": "sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-get-iterator": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/es-get-iterator/-/es-get-iterator-1.1.0.tgz",
++++++++ "integrity": "sha512-UfrmHuWQlNMTs35e1ypnvikg6jCz3SK8v8ImvmDsh36fCVUR1MqoFDiyn0/k52C8NqO3YsO8Oe0azeesNuqSsQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-abstract": "^1.17.4",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-arguments": "^1.0.4",
++++++++ "is-map": "^2.0.1",
++++++++ "is-set": "^2.0.1",
++++++++ "is-string": "^1.0.5",
++++++++ "isarray": "^2.0.5"
++++++++ }
++++++++ },
++++++++ "es-to-primitive": {
++++++++ "version": "1.2.1",
++++++++ "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
++++++++ "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==",
++++++++ "requires": {
++++++++ "is-callable": "^1.1.4",
++++++++ "is-date-object": "^1.0.1",
++++++++ "is-symbol": "^1.0.2"
++++++++ }
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
++++++++ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
++++++++ "dev": true
++++++++ },
++++++++ "esprima": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
++++++++ "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
++++++++ "dev": true
++++++++ },
++++++++ "fill-range": {
++++++++ "version": "7.0.1",
++++++++ "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
++++++++ "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "to-regex-range": "^5.0.1"
++++++++ }
++++++++ },
++++++++ "find-up": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz",
++++++++ "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^6.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "flat": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz",
++++++++ "integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-buffer": "~2.0.3"
++++++++ }
++++++++ },
++++++++ "fs.realpath": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
++++++++ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
++++++++ "dev": true
++++++++ },
++++++++ "fsevents": {
++++++++ "version": "2.1.3",
++++++++ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz",
++++++++ "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==",
++++++++ "dev": true,
++++++++ "optional": true
++++++++ },
++++++++ "function-bind": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
++++++++ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
++++++++ },
++++++++ "get-caller-file": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
++++++++ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.2",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
++++++++ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "glob-parent": {
++++++++ "version": "5.1.1",
++++++++ "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.1.tgz",
++++++++ "integrity": "sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-glob": "^4.0.1"
++++++++ }
++++++++ },
++++++++ "growl": {
++++++++ "version": "1.10.5",
++++++++ "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz",
++++++++ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==",
++++++++ "dev": true
++++++++ },
++++++++ "has": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
++++++++ "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
++++++++ "requires": {
++++++++ "function-bind": "^1.1.1"
++++++++ }
++++++++ },
++++++++ "has-flag": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
++++++++ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
++++++++ "dev": true
++++++++ },
++++++++ "has-symbols": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",
++++++++ "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg=="
++++++++ },
++++++++ "he": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
++++++++ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
++++++++ "dev": true
++++++++ },
++++++++ "inflight": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
++++++++ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "once": "^1.3.0",
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "inherits": {
++++++++ "version": "2.0.3",
++++++++ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
++++++++ "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
++++++++ "dev": true
++++++++ },
++++++++ "is-arguments": {
++++++++ "version": "1.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz",
++++++++ "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-binary-path": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
++++++++ "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "binary-extensions": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "is-buffer": {
++++++++ "version": "2.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz",
++++++++ "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==",
++++++++ "dev": true
++++++++ },
++++++++ "is-callable": {
++++++++ "version": "1.2.2",
++++++++ "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.2.tgz",
++++++++ "integrity": "sha512-dnMqspv5nU3LoewK2N/y7KLtxtakvTuaCsU9FU50/QDmdbHNy/4/JuRtMHqRU22o3q+W89YQndQEeCVwK+3qrA=="
++++++++ },
++++++++ "is-date-object": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz",
++++++++ "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g=="
++++++++ },
++++++++ "is-extglob": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
++++++++ "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=",
++++++++ "dev": true
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
++++++++ "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=",
++++++++ "dev": true
++++++++ },
++++++++ "is-glob": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz",
++++++++ "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-extglob": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "is-map": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-map/-/is-map-2.0.1.tgz",
++++++++ "integrity": "sha512-T/S49scO8plUiAOA2DBTBG3JHpn1yiw0kRp6dgiZ0v2/6twi5eiB0rHtHFH9ZIrvlWc6+4O+m4zg5+Z833aXgw==",
++++++++ "dev": true
++++++++ },
++++++++ "is-negative-zero": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.0.tgz",
++++++++ "integrity": "sha1-lVOxIbD6wohp2p7UWeIMdUN4hGE="
++++++++ },
++++++++ "is-number": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
++++++++ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
++++++++ "dev": true
++++++++ },
++++++++ "is-plain-obj": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
++++++++ "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
++++++++ "dev": true
++++++++ },
++++++++ "is-regex": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.1.tgz",
++++++++ "integrity": "sha512-1+QkEcxiLlB7VEyFtyBg94e08OAsvq7FUBgApTq/w2ymCLyKJgDPsybBENVtA7XCQEgEXxKPonG+mvYRxh/LIg==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "is-set": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-set/-/is-set-2.0.1.tgz",
++++++++ "integrity": "sha512-eJEzOtVyenDs1TMzSQ3kU3K+E0GUS9sno+F0OBT97xsgcJsF9nXMBtkT9/kut5JEpM7oL7X/0qxR17K3mcwIAA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-string": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/is-string/-/is-string-1.0.5.tgz",
++++++++ "integrity": "sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==",
++++++++ "dev": true
++++++++ },
++++++++ "is-symbol": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz",
++++++++ "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "isarray": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz",
++++++++ "integrity": "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==",
++++++++ "dev": true
++++++++ },
++++++++ "isexe": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
++++++++ "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-iterator": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/iterate-iterator/-/iterate-iterator-1.0.1.tgz",
++++++++ "integrity": "sha512-3Q6tudGN05kbkDQDI4CqjaBf4qf85w6W6GnuZDtUVYwKgtC1q8yxYX7CZed7N+tLzQqS6roujWvszf13T+n9aw==",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-value": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/iterate-value/-/iterate-value-1.0.2.tgz",
++++++++ "integrity": "sha512-A6fMAio4D2ot2r/TYzr4yUWrmwNdsN5xL7+HUiyACE4DXm+q8HtPcnFTp+NnW3k4N05tZ7FVYFFb2CR13NxyHQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-get-iterator": "^1.0.2",
++++++++ "iterate-iterator": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "js-tokens": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
++++++++ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
++++++++ "dev": true
++++++++ },
++++++++ "js-yaml": {
++++++++ "version": "3.14.0",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz",
++++++++ "integrity": "sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "argparse": "^1.0.7",
++++++++ "esprima": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",
++++++++ "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^5.0.0"
++++++++ }
++++++++ },
++++++++ "log-symbols": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz",
++++++++ "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "chalk": "^4.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-styles": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
++++++++ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^2.0.1"
++++++++ }
++++++++ },
++++++++ "chalk": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz",
++++++++ "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^4.1.0",
++++++++ "supports-color": "^7.1.0"
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "~1.1.4"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "make-error": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
++++++++ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
++++++++ "dev": true
++++++++ },
++++++++ "minimatch": {
++++++++ "version": "3.0.4",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
++++++++ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "brace-expansion": "^1.1.7"
++++++++ }
++++++++ },
++++++++ "mkdirp": {
++++++++ "version": "0.5.5",
++++++++ "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
++++++++ "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "minimist": "^1.2.5"
++++++++ },
++++++++ "dependencies": {
++++++++ "minimist": {
++++++++ "version": "1.2.5",
++++++++ "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
++++++++ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "mocha": {
++++++++ "version": "8.1.3",
++++++++ "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.1.3.tgz",
++++++++ "integrity": "sha512-ZbaYib4hT4PpF4bdSO2DohooKXIn4lDeiYqB+vTmCdr6l2woW0b6H3pf5x4sM5nwQMru9RvjjHYWVGltR50ZBw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-colors": "4.1.1",
++++++++ "browser-stdout": "1.3.1",
++++++++ "chokidar": "3.4.2",
++++++++ "debug": "4.1.1",
++++++++ "diff": "4.0.2",
++++++++ "escape-string-regexp": "4.0.0",
++++++++ "find-up": "5.0.0",
++++++++ "glob": "7.1.6",
++++++++ "growl": "1.10.5",
++++++++ "he": "1.2.0",
++++++++ "js-yaml": "3.14.0",
++++++++ "log-symbols": "4.0.0",
++++++++ "minimatch": "3.0.4",
++++++++ "ms": "2.1.2",
++++++++ "object.assign": "4.1.0",
++++++++ "promise.allsettled": "1.0.2",
++++++++ "serialize-javascript": "4.0.0",
++++++++ "strip-json-comments": "3.0.1",
++++++++ "supports-color": "7.1.0",
++++++++ "which": "2.0.2",
++++++++ "wide-align": "1.1.3",
++++++++ "workerpool": "6.0.0",
++++++++ "yargs": "13.3.2",
++++++++ "yargs-parser": "13.1.2",
++++++++ "yargs-unparser": "1.6.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "debug": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
++++++++ "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
++++++++ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.6",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
++++++++ "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "ms": {
++++++++ "version": "2.1.2",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
++++++++ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
++++++++ },
++++++++ "normalize-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
++++++++ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
++++++++ "dev": true
++++++++ },
++++++++ "object-inspect": {
++++++++ "version": "1.8.0",
++++++++ "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.8.0.tgz",
++++++++ "integrity": "sha512-jLdtEOB112fORuypAyl/50VRVIBIdVQOSUUGQHzJ4xBSbit81zRarz7GThkEFZy1RceYrWYcPcBFPQwHyAc1gA=="
++++++++ },
++++++++ "object-keys": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
++++++++ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz",
++++++++ "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.2",
++++++++ "function-bind": "^1.1.1",
++++++++ "has-symbols": "^1.0.0",
++++++++ "object-keys": "^1.0.11"
++++++++ }
++++++++ },
++++++++ "once": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
++++++++ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.0.2.tgz",
++++++++ "integrity": "sha512-iwqZSOoWIW+Ew4kAGUlN16J4M7OB3ysMLSZtnhmqx7njIHFPlxWBX8xo3lVTyFVq6mI/lL9qt2IsN1sHwaxJkg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz",
++++++++ "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^3.0.2"
++++++++ }
++++++++ },
++++++++ "p-try": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
++++++++ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
++++++++ "dev": true
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
++++++++ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
++++++++ "dev": true
++++++++ },
++++++++ "path-is-absolute": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
++++++++ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
++++++++ "dev": true
++++++++ },
++++++++ "path-parse": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
++++++++ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==",
++++++++ "dev": true
++++++++ },
++++++++ "picomatch": {
++++++++ "version": "2.2.2",
++++++++ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz",
++++++++ "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==",
++++++++ "dev": true
++++++++ },
++++++++ "promise.allsettled": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/promise.allsettled/-/promise.allsettled-1.0.2.tgz",
++++++++ "integrity": "sha512-UpcYW5S1RaNKT6pd+s9jp9K9rlQge1UXKskec0j6Mmuq7UJCvlS2J2/s/yuPN8ehftf9HXMxWlKiPbGGUzpoRg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "array.prototype.map": "^1.0.1",
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "iterate-value": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "randombytes": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
++++++++ "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "safe-buffer": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "readdirp": {
++++++++ "version": "3.4.0",
++++++++ "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz",
++++++++ "integrity": "sha512-0xe001vZBnJEK+uKcj8qOhyAKPzIT+gStxWr3LCB0DwcXR5NZJ3IaC+yGnHCYzB/S7ov3m3EEbZI2zeNvX+hGQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "picomatch": "^2.2.1"
++++++++ }
++++++++ },
++++++++ "require-directory": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
++++++++ "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=",
++++++++ "dev": true
++++++++ },
++++++++ "require-main-filename": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
++++++++ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
++++++++ "dev": true
++++++++ },
++++++++ "resolve": {
++++++++ "version": "1.17.0",
++++++++ "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",
++++++++ "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "path-parse": "^1.0.6"
++++++++ }
++++++++ },
++++++++ "safe-buffer": {
++++++++ "version": "5.2.1",
++++++++ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
++++++++ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
++++++++ "dev": true
++++++++ },
++++++++ "semver": {
++++++++ "version": "5.7.1",
++++++++ "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
++++++++ "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
++++++++ "dev": true
++++++++ },
++++++++ "serialize-javascript": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-4.0.0.tgz",
++++++++ "integrity": "sha512-GaNA54380uFefWghODBWEGisLZFj00nS5ACs6yHa9nLqlLpVLO8ChDGeKRjZnV4Nh4n0Qi7nhYZD/9fCPzEqkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "randombytes": "^2.1.0"
++++++++ }
++++++++ },
++++++++ "set-blocking": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
++++++++ "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
++++++++ "dev": true
++++++++ },
++++++++ "source-map": {
++++++++ "version": "0.6.1",
++++++++ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
++++++++ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
++++++++ "dev": true
++++++++ },
++++++++ "source-map-support": {
++++++++ "version": "0.5.19",
++++++++ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz",
++++++++ "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "buffer-from": "^1.0.0",
++++++++ "source-map": "^0.6.0"
++++++++ }
++++++++ },
++++++++ "sprintf-js": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
++++++++ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
++++++++ "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimend": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz",
++++++++ "integrity": "sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimstart": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz",
++++++++ "integrity": "sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
++++++++ "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "strip-json-comments": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.0.1.tgz",
++++++++ "integrity": "sha512-VTyMAUfdm047mwKl+u79WIdrZxtFtn+nBxHeb844XBQ9uMNTuTHdx2hc5RiAJYqwTj3wc/xe5HLSdJSkJ+WfZw==",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "7.1.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz",
++++++++ "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "to-regex-range": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
++++++++ "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-number": "^7.0.0"
++++++++ }
++++++++ },
++++++++ "ts-node": {
++++++++ "version": "9.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.0.0.tgz",
++++++++ "integrity": "sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "arg": "^4.1.0",
++++++++ "diff": "^4.0.1",
++++++++ "make-error": "^1.1.1",
++++++++ "source-map-support": "^0.5.17",
++++++++ "yn": "3.1.1"
++++++++ }
++++++++ },
++++++++ "tslib": {
++++++++ "version": "1.13.0",
++++++++ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz",
++++++++ "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==",
++++++++ "dev": true
++++++++ },
++++++++ "tslint": {
++++++++ "version": "5.20.1",
++++++++ "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.20.1.tgz",
++++++++ "integrity": "sha512-EcMxhzCFt8k+/UP5r8waCf/lzmeSyVlqxqMEDQE7rWYiQky8KpIBz1JAoYXfROHrPZ1XXd43q8yQnULOLiBRQg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/code-frame": "^7.0.0",
++++++++ "builtin-modules": "^1.1.1",
++++++++ "chalk": "^2.3.0",
++++++++ "commander": "^2.12.1",
++++++++ "diff": "^4.0.1",
++++++++ "glob": "^7.1.1",
++++++++ "js-yaml": "^3.13.1",
++++++++ "minimatch": "^3.0.4",
++++++++ "mkdirp": "^0.5.1",
++++++++ "resolve": "^1.3.2",
++++++++ "semver": "^5.3.0",
++++++++ "tslib": "^1.8.0",
++++++++ "tsutils": "^2.29.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "tsutils": {
++++++++ "version": "2.29.0",
++++++++ "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz",
++++++++ "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "tslib": "^1.8.1"
++++++++ }
++++++++ },
++++++++ "typescript": {
++++++++ "version": "4.0.3",
++++++++ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz",
++++++++ "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==",
++++++++ "dev": true
++++++++ },
++++++++ "which": {
++++++++ "version": "2.0.2",
++++++++ "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
++++++++ "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "isexe": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "which-module": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",
++++++++ "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=",
++++++++ "dev": true
++++++++ },
++++++++ "wide-align": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz",
++++++++ "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^1.0.2 || 2"
++++++++ }
++++++++ },
++++++++ "workerpool": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.0.0.tgz",
++++++++ "integrity": "sha512-fU2OcNA/GVAJLLyKUoHkAgIhKb0JoCpSjLC/G2vYKxUjVmQwGbRVeoPJ1a8U4pnVofz4AQV5Y/NEw8oKqxEBtA==",
++++++++ "dev": true
++++++++ },
++++++++ "wrap-ansi": {
++++++++ "version": "5.1.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz",
++++++++ "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.0",
++++++++ "string-width": "^3.0.0",
++++++++ "strip-ansi": "^5.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "wrappy": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
++++++++ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
++++++++ "dev": true
++++++++ },
++++++++ "y18n": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz",
++++++++ "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==",
++++++++ "dev": true
++++++++ },
++++++++ "yargs": {
++++++++ "version": "13.3.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.3.2.tgz",
++++++++ "integrity": "sha512-AX3Zw5iPruN5ie6xGRIDgqkT+ZhnRlZMLMHAs8tg7nRruy2Nb+i5o9bwghAogtM08q1dpr2LVoS8KSTMYpWXUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^13.1.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "2.3.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
++++++++ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "13.1.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.2.tgz",
++++++++ "integrity": "sha512-3lbsNRf/j+A4QuSZfDRA7HRSfWrzO0YjqTJd5kjAq37Zep1CEgaYmrH9Q3GwPiB9cHyd1Y1UwggGhJGoxipbzg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ },
++++++++ "yargs-unparser": {
++++++++ "version": "1.6.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-1.6.1.tgz",
++++++++ "integrity": "sha512-qZV14lK9MWsGCmcr7u5oXGH0dbGqZAIxTDrWXZDo5zUr6b6iUmelNKO6x6R1dQT24AH3LgRxJpr8meWy2unolA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.3.1",
++++++++ "decamelize": "^1.2.0",
++++++++ "flat": "^4.1.0",
++++++++ "is-plain-obj": "^1.1.0",
++++++++ "yargs": "^14.2.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "2.3.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
++++++++ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ },
++++++++ "yargs": {
++++++++ "version": "14.2.3",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-14.2.3.tgz",
++++++++ "integrity": "sha512-ZbotRWhF+lkjijC/VhmOT9wSgyBQ7+zr13+YLkhfsSiTriYsMzkTUFP18pFhWwBeMa5gUc1MzbhrO6/VB7c9Xg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "decamelize": "^1.2.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^15.0.1"
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "15.0.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz",
++++++++ "integrity": "sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yn": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
++++++++ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse-builder",
++++++++ "version": "1.5.2",
++++++++ "description": "Build graph for consumption in LLParse",
++++++++ "main": "lib/builder.js",
++++++++ "types": "lib/builder.d.ts",
++++++++ "files": [
++++++++ "lib",
++++++++ "src"
++++++++ ],
++++++++ "scripts": {
++++++++ "build": "tsc",
++++++++ "clean": "rm -rf lib",
++++++++ "prepare": "npm run clean && npm run build",
++++++++ "lint": "tslint -c tslint.json src/*.ts src/**/*.ts src/**/**/*.ts test/*.ts",
++++++++ "mocha": "mocha -r ts-node/register/type-check --reporter spec test/*-test.ts",
++++++++ "test": "npm run mocha && npm run lint"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+ssh://git@github.com/indutny/llparse-builder.git"
++++++++ },
++++++++ "keywords": [
++++++++ "llparse",
++++++++ "builder",
++++++++ "llvm",
++++++++ "bitcode"
++++++++ ],
++++++++ "author": "Fedor Indutny <fedor@indutny.com> (http://darksi.de/)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/indutny/llparse-builder/issues"
++++++++ },
++++++++ "homepage": "https://github.com/indutny/llparse-builder#readme",
++++++++ "devDependencies": {
++++++++ "@types/mocha": "^8.0.3",
++++++++ "@types/node": "^14.11.8",
++++++++ "mocha": "^8.1.3",
++++++++ "ts-node": "^9.0.0",
++++++++ "tslint": "^5.20.1",
++++++++ "typescript": "^4.0.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "@types/debug": "4.1.5 ",
++++++++ "binary-search": "^1.3.6",
++++++++ "debug": "^4.2.0"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as code from './code';
++++++++import * as node from './node';
++++++++import { Property, PropertyType } from './property';
++++++++import { Span } from './span';
++++++++import * as transform from './transform';
++++++++
++++++++export { code, node, transform, Property, PropertyType, Span };
++++++++export { Edge } from './edge';
++++++++export { LoopChecker } from './loop-checker';
++++++++export { ISpanAllocatorResult, SpanAllocator } from './span-allocator';
++++++++export { Reachability } from './reachability';
++++++++
++++++++/**
++++++++ * Construct parsing graph for later use in `llparse`.
++++++++ */
++++++++export class Builder {
++++++++ /**
++++++++ * API for creating external callbacks and intrinsic operations.
++++++++ */
++++++++ public readonly code: code.Creator = new code.Creator();
++++++++
++++++++ /**
++++++++ * API for creating character transforms for use in nodes created with
++++++++ * `builder.node()`
++++++++ */
++++++++ public readonly transform: transform.Creator = new transform.Creator();
++++++++
++++++++ private readonly privProperties: Map<string, Property> = new Map();
++++++++
++++++++ // Various nodes
++++++++
++++++++ /**
++++++++ * Create regular node for matching characters and sequences.
++++++++ *
++++++++ * @param name Node name
++++++++ */
++++++++ public node(name: string): node.Match {
++++++++ return new node.Match(name);
++++++++ }
++++++++
++++++++ /**
++++++++ * Create terminal error node. Returns error code to user, and sets reason
++++++++ * in the parser's state object.
++++++++ *
++++++++ * This node does not consume any bytes upon execution.
++++++++ *
++++++++ * @param errorCode Integer error code
++++++++ * @param reason Error description
++++++++ */
++++++++ public error(errorCode: number, reason: string): node.Error {
++++++++ return new node.Error(errorCode, reason);
++++++++ }
++++++++
++++++++ /**
++++++++ * Create invoke node that calls either external user callback or an
++++++++ * intrinsic operation.
++++++++ *
++++++++ * This node does not consume any bytes upon execution.
++++++++ *
++++++++ * NOTE: When `.invoke()` is a target of `node().select()` - callback must
++++++++ * have signature that accepts `.select()`'s value, otherwise it must be of
++++++++ * the signature that takes no such value.
++++++++ *
++++++++ * @param fn Code instance to invoke
++++++++ * @param map Object with integer keys and `Node` values. Describes
++++++++ * nodes that are visited upon receiving particular
++++++++ * return integer value
++++++++ * @param otherwise Convenience `Node` argument. Effect is the same as calling
++++++++ * `p.invoke(...).otherwise(node)`
++++++++ */
++++++++ public invoke(fn: code.Code, map?: node.IInvokeMap | node.Node,
++++++++ otherwise?: node.Node): node.Invoke {
++++++++ let res: node.Invoke;
++++++++
++++++++ // `.invoke(name)`
++++++++ if (map === undefined) {
++++++++ res = new node.Invoke(fn, {});
++++++++ // `.invoke(name, otherwise)`
++++++++ } else if (map instanceof node.Node) {
++++++++ res = new node.Invoke(fn, {});
++++++++ otherwise = map;
++++++++ } else {
++++++++ res = new node.Invoke(fn, map as node.IInvokeMap);
++++++++ }
++++++++
++++++++ if (otherwise !== undefined) {
++++++++ res.otherwise(otherwise);
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ /**
++++++++ * Create node that consumes number of bytes specified by value of the
++++++++ * state's property with name in `field` argument.
++++++++ *
++++++++ * @param field Property name to use
++++++++ */
++++++++ public consume(field: string): node.Consume {
++++++++ return new node.Consume(field);
++++++++ }
++++++++
++++++++ /**
++++++++ * Create non-terminal node that returns `errorCode` as error number to
++++++++ * user, but still allows feeding more data to the parser.
++++++++ *
++++++++ * This node does not consume any bytes upon execution.
++++++++ *
++++++++ * @param errorCode Integer error code
++++++++ * @param reason Error description
++++++++ */
++++++++ public pause(errorCode: number, reason: string): node.Pause {
++++++++ return new node.Pause(errorCode, reason);
++++++++ }
++++++++
++++++++ // Span
++++++++
++++++++ /**
++++++++ * Create Span with given `callback`.
++++++++ *
++++++++ * @param callback External span callback, must be result of
++++++++ * `.code.span(...)`
++++++++ */
++++++++ public span(callback: code.Span): Span {
++++++++ return new Span(callback);
++++++++ }
++++++++
++++++++ // Custom property API
++++++++
++++++++ /**
++++++++ * Allocate space for property in parser's state.
++++++++ */
++++++++ public property(ty: PropertyType, name: string): void {
++++++++ if (this.privProperties.has(name)) {
++++++++ throw new Error(`Duplicate property with a name: "${name}"`);
++++++++ }
++++++++
++++++++ const prop = new Property(ty, name);
++++++++ this.privProperties.set(name, prop);
++++++++ }
++++++++
++++++++ /**
++++++++ * Return list of all allocated properties in parser's state.
++++++++ */
++++++++ public get properties(): ReadonlyArray<Property> {
++++++++ return Array.from(this.privProperties.values());
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class And extends FieldValue {
++++++++ constructor(field: string, value: number) {
++++++++ super('match', 'and', field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export type Signature = 'match' | 'value';
++++++++
++++++++/**
++++++++ * Base code class.
++++++++ */
++++++++export abstract class Code {
++++++++ /**
++++++++ * @param signature Code signature to be used. `match` means that code takes
++++++++ * no input value (from `.select()`), otherwise it must be
++++++++ * `value`
++++++++ * @param name External function or intrinsic name.
++++++++ */
++++++++ constructor(public readonly signature: Signature,
++++++++ public readonly name: string) {
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as code from './';
++++++++
++++++++/**
++++++++ * API for creating external callbacks and intrinsic operations.
++++++++ */
++++++++export class Creator {
++++++++ // Callbacks to external C functions
++++++++
++++++++ /**
++++++++ * Create an external callback that **has no** `value` argument.
++++++++ *
++++++++ * This callback can be used in all `Invoke` nodes except those that are
++++++++ * targets of `.select()` method.
++++++++ *
++++++++ * C signature of callback must be:
++++++++ *
++++++++ * ```c
++++++++ * int name(llparse_t* state, const char* p, const char* endp)
++++++++ * ```
++++++++ *
++++++++ * Where `llparse_t` is parser state's type name.
++++++++ *
++++++++ * @param name External function name.
++++++++ */
++++++++ public match(name: string): code.Match {
++++++++ return new code.Match(name);
++++++++ }
++++++++
++++++++ /**
++++++++ * Create an external callback that **has** `value` argument.
++++++++ *
++++++++ * This callback can be used only in `Invoke` nodes that are targets of
++++++++ * `.select()` method.
++++++++ *
++++++++ * C signature of callback must be:
++++++++ *
++++++++ * ```c
++++++++ * int name(llparse_t* state, const char* p, const char* endp, int value)
++++++++ * ```
++++++++ *
++++++++ * Where `llparse_t` is parser state's type name.
++++++++ *
++++++++ * @param name External function name.
++++++++ */
++++++++ public value(name: string): code.Value {
++++++++ return new code.Value(name);
++++++++ }
++++++++
++++++++ /**
++++++++ * Create an external span callback.
++++++++ *
++++++++ * This callback can be used only in `Span` constructor.
++++++++ *
++++++++ * C signature of callback must be:
++++++++ *
++++++++ * ```c
++++++++ * int name(llparse_t* state, const char* p, const char* endp)
++++++++ * ```
++++++++ *
++++++++ * NOTE: non-zero return value is treated as resumable error.
++++++++ *
++++++++ * @param name External function name.
++++++++ */
++++++++ public span(name: string): code.Span {
++++++++ return new code.Span(name);
++++++++ }
++++++++
++++++++ // Helpers
++++++++
++++++++ /**
++++++++ * Intrinsic operation. Stores `value` from `.select()` node into the state's
++++++++ * property with the name specified by `field`, returns zero.
++++++++ *
++++++++ * state[field] = value;
++++++++ * return 0;
++++++++ *
++++++++ * @param field Property name
++++++++ */
++++++++ public store(field: string): code.Store {
++++++++ return new code.Store(field);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation. Loads and returns state's property with the name
++++++++ * specified by `field`.
++++++++ *
++++++++ * The value of the property is either truncated or zero-extended to fit into
++++++++ * 32-bit unsigned integer.
++++++++ *
++++++++ * return state[field];
++++++++ *
++++++++ * @param field Property name.
++++++++ */
++++++++ public load(field: string): code.Load {
++++++++ return new code.Load(field);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation. Takes `value` from `.select()`, state's property
++++++++ * with the name `field` and does:
++++++++ *
++++++++ * field = state[field];
++++++++ * field *= options.base;
++++++++ * field += value;
++++++++ * state[field] = field;
++++++++ * return 0; // or 1 on overflow
++++++++ *
++++++++ * Return values are:
++++++++ *
++++++++ * - 0 - success
++++++++ * - 1 - overflow
++++++++ *
++++++++ * @param field Property name
++++++++ * @param options See `code.MulAdd` documentation.
++++++++ */
++++++++ public mulAdd(field: string, options: code.IMulAddOptions): code.MulAdd {
++++++++ return new code.MulAdd(field, options);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation. Puts `value` integer into the state's property with
++++++++ * the name specified by `field`.
++++++++ *
++++++++ * state[field] = value;
++++++++ * return 0;
++++++++ *
++++++++ * @param field Property name
++++++++ * @param value Integer value to be stored into the property.
++++++++ */
++++++++ public update(field: string, value: number): code.Update {
++++++++ return new code.Update(field, value);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation. Returns 1 if the integer `value` is equal to the
++++++++ * state's property with the name specified by `field`.
++++++++ *
++++++++ * return state[field] === value ? 1 : 0;
++++++++ *
++++++++ * @param field Property name
++++++++ * @param value Integer value to be checked against.
++++++++ */
++++++++ public isEqual(field: string, value: number): code.IsEqual {
++++++++ return new code.IsEqual(field, value);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation.
++++++++ *
++++++++ * state[field] &= value
++++++++ * return 0;
++++++++ *
++++++++ * @param field Property name
++++++++ * @param value Integer value
++++++++ */
++++++++ public and(field: string, value: number): code.And {
++++++++ return new code.And(field, value);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation.
++++++++ *
++++++++ * state[field] |= value
++++++++ * return 0;
++++++++ *
++++++++ * @param field Property name
++++++++ * @param value Integer value
++++++++ */
++++++++ public or(field: string, value: number): code.Or {
++++++++ return new code.Or(field, value);
++++++++ }
++++++++
++++++++ /**
++++++++ * Intrinsic operation.
++++++++ *
++++++++ * return (state[field] & value) == value ? 1 : 0;
++++++++ *
++++++++ * @param field Property name
++++++++ * @param value Integer value
++++++++ */
++++++++ public test(field: string, value: number): code.Test {
++++++++ return new code.Test(field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Signature } from './base';
++++++++import { Field } from './field';
++++++++
++++++++export abstract class FieldValue extends Field {
++++++++ constructor(signature: Signature, name: string, field: string,
++++++++ public readonly value: number) {
++++++++ super(signature, name, field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Code, Signature } from './base';
++++++++
++++++++export abstract class Field extends Code {
++++++++ constructor(signature: Signature, name: string,
++++++++ public readonly field: string) {
++++++++ super(signature, name + '_' + field);
++++++++ assert(!/^_/.test(field), 'Can\'t access internal field from user code');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export { Code } from './base';
++++++++export { Creator } from './creator';
++++++++export { Field } from './field';
++++++++export { FieldValue } from './field-value';
++++++++export { IsEqual } from './is-equal';
++++++++export { Load } from './load';
++++++++export { Match } from './match';
++++++++export { IMulAddOptions, MulAdd } from './mul-add';
++++++++export { Or } from './or';
++++++++export { And } from './and';
++++++++export { Span } from './span';
++++++++export { Store } from './store';
++++++++export { Test } from './test';
++++++++export { Update } from './update';
++++++++export { Value } from './value';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class IsEqual extends FieldValue {
++++++++ constructor(field: string, value: number) {
++++++++ super('match', 'is_equal', field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Field } from './field';
++++++++
++++++++export class Load extends Field {
++++++++ constructor(field: string) {
++++++++ super('match', 'load', field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Code } from './base';
++++++++
++++++++export class Match extends Code {
++++++++ constructor(name: string) {
++++++++ super('match', name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Field } from './field';
++++++++
++++++++/**
++++++++ * Options for `code.mulAdd()`.
++++++++ */
++++++++export interface IMulAddOptions {
++++++++ /** Value to multiply the property with in the first step */
++++++++ readonly base: number;
++++++++
++++++++ /**
++++++++ * Maximum value of the property. If at any point of computation the
++++++++ * intermediate result exceeds it - `mulAdd` returns 1 (overflow).
++++++++ */
++++++++ readonly max?: number;
++++++++
++++++++ /**
++++++++ * If `true` - all arithmetics perfomed by `mulAdd` will be signed.
++++++++ *
++++++++ * Default value: `false`
++++++++ */
++++++++ readonly signed?: boolean;
++++++++}
++++++++
++++++++export class MulAdd extends Field {
++++++++ constructor(field: string, public readonly options: IMulAddOptions) {
++++++++ super('value', 'mul_add', field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Or extends FieldValue {
++++++++ constructor(field: string, value: number) {
++++++++ super('match', 'or', field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Match } from './match';
++++++++
++++++++export class Span extends Match {
++++++++ // no-op
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Field } from './field';
++++++++
++++++++export class Store extends Field {
++++++++ constructor(field: string) {
++++++++ super('value', 'store', field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Test extends FieldValue {
++++++++ constructor(field: string, value: number) {
++++++++ super('match', 'test', field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Update extends FieldValue {
++++++++ constructor(field: string, value: number) {
++++++++ super('match', 'update', field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Code } from './base';
++++++++
++++++++export class Value extends Code {
++++++++ constructor(name: string) {
++++++++ super('value', name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Buffer } from 'buffer';
++++++++import { Invoke, Node } from './node';
++++++++
++++++++/**
++++++++ * This class represents an edge in the parser graph.
++++++++ */
++++++++export class Edge {
++++++++ /**
++++++++ * Comparator for `.sort()` function.
++++++++ */
++++++++ public static compare(a: Edge, b: Edge): number {
++++++++ if (typeof a.key === 'number') {
++++++++ return a.key - (b.key as number);
++++++++ }
++++++++ return a.key!.compare(b.key as Buffer);
++++++++ }
++++++++
++++++++ /**
++++++++ * @param node Edge target
++++++++ * @param noAdvance If `true` - the parent should not consume bytes before
++++++++ * moving to the target `node`
++++++++ * @param key `Buffer` for `node.Match`, `number` for `node.Invoke`,
++++++++ * `undefined` for edges created with `.otherwise()`
++++++++ * @param value `.select()` value associated with the edge
++++++++ */
++++++++ constructor(public readonly node: Node,
++++++++ public readonly noAdvance: boolean,
++++++++ public readonly key: Buffer | number | undefined,
++++++++ public readonly value: number | undefined) {
++++++++ if (node instanceof Invoke) {
++++++++ if (value === undefined) {
++++++++ assert.strictEqual(node.code.signature, 'match',
++++++++ 'Invalid Invoke\'s code signature');
++++++++ } else {
++++++++ assert.strictEqual(node.code.signature, 'value',
++++++++ 'Invalid Invoke\'s code signature');
++++++++ }
++++++++ } else {
++++++++ assert.strictEqual(value, undefined,
++++++++ 'Attempting to pass value to non-Invoke node');
++++++++ }
++++++++
++++++++ if (Buffer.isBuffer(key)) {
++++++++ assert(key.length > 0, 'Invalid edge buffer length');
++++++++
++++++++ if (noAdvance) {
++++++++ assert.strictEqual(key.length, 1,
++++++++ 'Only 1-char keys are allowed in `noAdvance` edges');
++++++++ }
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as debugAPI from 'debug';
++++++++
++++++++import { Node } from '../node';
++++++++import { Reachability } from '../reachability';
++++++++import { Lattice } from './lattice';
++++++++
++++++++const debug = debugAPI('llparse-builder:loop-checker');
++++++++
++++++++const EMPTY_VALUE = new Lattice('empty');
++++++++const ANY_VALUE = new Lattice('any');
++++++++
++++++++/**
++++++++ * This class implements a loop checker pass. The goal of this pass is to verify
++++++++ * that the graph doesn't contain infinite loops.
++++++++ */
++++++++export class LoopChecker {
++++++++ private readonly lattice: Map<Node, Lattice> = new Map();
++++++++
++++++++ // Just a cache of terminated keys
++++++++ private readonly terminatedCache: Map<Node, Lattice> = new Map();
++++++++
++++++++ /**
++++++++ * Run loop checker pass on a graph starting from `root`.
++++++++ *
++++++++ * Throws on failure.
++++++++ *
++++++++ * @param root Graph root node
++++++++ */
++++++++ public check(root: Node): void {
++++++++ const r = new Reachability();
++++++++
++++++++ const nodes = r.build(root);
++++++++
++++++++ for (const node of nodes) {
++++++++ debug('checking loops starting from %j', node.name);
++++++++
++++++++ // Set initial lattice value for all nodes
++++++++ this.clear(nodes);
++++++++
++++++++ // Mark root as reachable with any value
++++++++ this.lattice.set(node, ANY_VALUE);
++++++++
++++++++ // Raise lattice values
++++++++ let changed: Set<Node> = new Set([ root ]);
++++++++ while (changed.size !== 0) {
++++++++ if (debug.enabled) {
++++++++ debug('changed %j', Array.from(changed).map((other) => other.name));
++++++++ }
++++++++
++++++++ const next: Set<Node> = new Set();
++++++++ for (const changedNode of changed) {
++++++++ this.propagate(changedNode, next);
++++++++ }
++++++++ changed = next;
++++++++ }
++++++++
++++++++ debug('lattice stabilized');
++++++++
++++++++ // Visit nodes and walk through reachable edges to detect loops
++++++++ this.visit(node, []);
++++++++ }
++++++++ }
++++++++
++++++++ private clear(nodes: ReadonlyArray<Node>): void {
++++++++ for (const node of nodes) {
++++++++ this.lattice.set(node, EMPTY_VALUE);
++++++++ }
++++++++ }
++++++++
++++++++ private propagate(node: Node, changed: Set<Node>): void {
++++++++ let value: Lattice = this.lattice.get(node)!;
++++++++ debug('propagate(%j), initial value %j', node.name, value);
++++++++
++++++++ // Terminate values that are consumed by `match`/`select`
++++++++ const terminated = this.terminate(node, value, changed);
++++++++ if (!terminated.isEqual(EMPTY_VALUE)) {
++++++++ debug('node %j terminates %j', node.name, terminated);
++++++++ value = value.subtract(terminated);
++++++++ if (value.isEqual(EMPTY_VALUE)) {
++++++++ return;
++++++++ }
++++++++ }
++++++++
++++++++ const keysByTarget: Map<Node, Lattice> = new Map();
++++++++ // Propagate value through `.peek()`/`.otherwise()` edges
++++++++ for (const edge of node.getAllEdges()) {
++++++++ if (!edge.noAdvance) {
++++++++ continue;
++++++++ }
++++++++
++++++++ let targetValue: Lattice;
++++++++ if (keysByTarget.has(edge.node)) {
++++++++ targetValue = keysByTarget.get(edge.node)!;
++++++++ } else {
++++++++ targetValue = this.lattice.get(edge.node)!;
++++++++ }
++++++++
++++++++ // `otherwise` or `Invoke`'s edges
++++++++ if (edge.key === undefined || typeof edge.key === 'number') {
++++++++ targetValue = targetValue.union(value);
++++++++ } else {
++++++++ // `.peek()`
++++++++ const edgeValue = new Lattice([ edge.key[0] ]).intersect(value);
++++++++ if (edgeValue.isEqual(EMPTY_VALUE)) {
++++++++ continue;
++++++++ }
++++++++
++++++++ targetValue = targetValue.union(edgeValue);
++++++++ }
++++++++
++++++++ keysByTarget.set(edge.node, targetValue);
++++++++ }
++++++++
++++++++ for (const [ child, childValue ] of keysByTarget) {
++++++++ debug('node %j propagates %j to %j', node.name, childValue,
++++++++ child.name);
++++++++ this.update(child, childValue, changed);
++++++++ }
++++++++ }
++++++++
++++++++ private update(node: Node, newValue: Lattice, changed: Set<Node>): boolean {
++++++++ const value = this.lattice.get(node)!;
++++++++ if (newValue.isEqual(value)) {
++++++++ return false;
++++++++ }
++++++++
++++++++ this.lattice.set(node, newValue);
++++++++ changed.add(node);
++++++++ return true;
++++++++ }
++++++++
++++++++ private terminate(node: Node, value: Lattice, changed: Set<Node>): Lattice {
++++++++ if (this.terminatedCache.has(node)) {
++++++++ return this.terminatedCache.get(node)!;
++++++++ }
++++++++
++++++++ const terminated: number[] = [];
++++++++ for (const edge of node.getAllEdges()) {
++++++++ if (edge.noAdvance) {
++++++++ continue;
++++++++ }
++++++++
++++++++ // Ignore `otherwise` and `Invoke`'s edges
++++++++ if (edge.key === undefined || typeof edge.key === 'number') {
++++++++ continue;
++++++++ }
++++++++
++++++++ terminated.push(edge.key[0]);
++++++++ }
++++++++
++++++++ const result = new Lattice(terminated);
++++++++ this.terminatedCache.set(node, result);
++++++++ return result;
++++++++ }
++++++++
++++++++ private visit(node: Node, path: ReadonlyArray<Node>): void {
++++++++ let value = this.lattice.get(node)!;
++++++++ debug('enter %j, value is %j', node.name, value);
++++++++
++++++++ const terminated = this.terminatedCache.has(node) ?
++++++++ this.terminatedCache.get(node)! : EMPTY_VALUE;
++++++++ if (!terminated.isEqual(EMPTY_VALUE)) {
++++++++ debug('subtract terminated %j', terminated);
++++++++ value = value.subtract(terminated);
++++++++ if (value.isEqual(EMPTY_VALUE)) {
++++++++ debug('terminated everything');
++++++++ return;
++++++++ }
++++++++ }
++++++++
++++++++ for (const edge of node.getAllEdges()) {
++++++++ if (!edge.noAdvance) {
++++++++ continue;
++++++++ }
++++++++
++++++++ let edgeValue = value;
++++++++
++++++++ // `otherwise` or `Invoke`'s edges
++++++++ if (edge.key === undefined || typeof edge.key === 'number') {
++++++++ // nothing to do
++++++++ // `.peek()`
++++++++ } else {
++++++++ edgeValue = edgeValue.intersect(new Lattice([ edge.key[0] ]));
++++++++ }
++++++++
++++++++ // Ignore unreachable edges
++++++++ if (edgeValue.isEqual(EMPTY_VALUE)) {
++++++++ continue;
++++++++ }
++++++++ if (path.indexOf(edge.node) !== -1) {
++++++++ if (path.length === 0) {
++++++++ throw new Error(
++++++++ `Detected loop in "${edge.node.name}" through "${edge.node.name}"`);
++++++++ }
++++++++ throw new Error(
++++++++ `Detected loop in "${edge.node.name}" through chain ` +
++++++++ `${path.map((parent) => '"' + parent.name + '"').join(' -> ')}`);
++++++++ }
++++++++ this.visit(edge.node, path.concat(edge.node));
++++++++ }
++++++++
++++++++ debug('leave %j', node.name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++const MAX_VALUE = 256;
++++++++const WORD_SIZE = 32;
++++++++const SIZE = (MAX_VALUE / WORD_SIZE) | 0;
++++++++const WORD_FILL = -1 | 0;
++++++++
++++++++assert.strictEqual(MAX_VALUE % WORD_SIZE, 0);
++++++++
++++++++export type LatticeValue = 'empty' | ReadonlyArray<number> | 'any';
++++++++
++++++++/**
++++++++ * A fixed-size bitfield, really
++++++++ */
++++++++export class Lattice {
++++++++ protected readonly words: number[];
++++++++
++++++++ constructor(value: LatticeValue) {
++++++++ this.words = new Array(SIZE).fill(value === 'any' ? WORD_FILL : 0);
++++++++
++++++++ if (Array.isArray(value)) {
++++++++ for (const single of value) {
++++++++ this.add(single);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ public check(bit: number): boolean {
++++++++ assert(0 <= bit && bit < MAX_VALUE, 'Invalid bit');
++++++++ const index = (bit / WORD_SIZE) | 0;
++++++++ const off = bit % WORD_SIZE;
++++++++ return (this.words[index] & (1 << off)) !== 0;
++++++++ }
++++++++
++++++++ public union(other: Lattice): Lattice {
++++++++ const result = new Lattice('empty');
++++++++
++++++++ for (let i = 0; i < SIZE; i++) {
++++++++ result.words[i] = this.words[i] | other.words[i];
++++++++ }
++++++++
++++++++ return result;
++++++++ }
++++++++
++++++++ public intersect(other: Lattice): Lattice {
++++++++ const result = new Lattice('empty');
++++++++
++++++++ for (let i = 0; i < SIZE; i++) {
++++++++ result.words[i] = this.words[i] & other.words[i];
++++++++ }
++++++++
++++++++ return result;
++++++++ }
++++++++
++++++++ public subtract(other: Lattice): Lattice {
++++++++ const result = new Lattice('empty');
++++++++
++++++++ for (let i = 0; i < SIZE; i++) {
++++++++ result.words[i] = this.words[i] & (~other.words[i]);
++++++++ }
++++++++
++++++++ return result;
++++++++ }
++++++++
++++++++ public isEqual(other: Lattice): boolean {
++++++++ if (this === other) {
++++++++ return true;
++++++++ }
++++++++
++++++++ for (let i = 0; i < SIZE; i++) {
++++++++ if (this.words[i] !== other.words[i]) {
++++++++ return false;
++++++++ }
++++++++ }
++++++++ return true;
++++++++ }
++++++++
++++++++ public *[Symbol.iterator](): Iterator<number> {
++++++++ // TODO(indutny): improve speed if needed
++++++++ for (let i = 0; i < MAX_VALUE; i++) {
++++++++ if (this.check(i)) {
++++++++ yield i;
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ public toJSON(): any {
++++++++ let isEmpty = true;
++++++++ let isFull = true;
++++++++ for (let i = 0; i < SIZE; i++) {
++++++++ if (this.words[i] !== 0) {
++++++++ isEmpty = false;
++++++++ }
++++++++ if (this.words[i] !== WORD_FILL) {
++++++++ isFull = false;
++++++++ }
++++++++ }
++++++++ if (isEmpty) {
++++++++ return 'empty';
++++++++ }
++++++++ if (isFull) {
++++++++ return 'any';
++++++++ }
++++++++ return Array.from(this);
++++++++ }
++++++++
++++++++ // Private
++++++++
++++++++ private add(bit: number): void {
++++++++ assert(0 <= bit && bit < MAX_VALUE, 'Invalid bit');
++++++++ const index = (bit / WORD_SIZE) | 0;
++++++++ const off = bit % WORD_SIZE;
++++++++ this.words[index] |= 1 << off;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import binarySearch = require('binary-search');
++++++++import { Edge } from '../edge';
++++++++
++++++++/**
++++++++ * Base class for all graph nodes.
++++++++ */
++++++++export abstract class Node {
++++++++ private otherwiseEdge: Edge | undefined;
++++++++ private privEdges: Edge[] = [];
++++++++
++++++++ /**
++++++++ * @param name Node name
++++++++ */
++++++++ constructor(public readonly name: string) {
++++++++ // no-op
++++++++ }
++++++++
++++++++ /**
++++++++ * Create an otherwise edge to node `node`.
++++++++ *
++++++++ * This edge is executed when no other edges match current input. No
++++++++ * characters are consumed upon transition.
++++++++ *
++++++++ * NOTE: At most one otherwise (skipping or not) edge can be set, most nodes
++++++++ * except `Error` require it.
++++++++ *
++++++++ * @param node Target node
++++++++ */
++++++++ public otherwise(node: Node): this {
++++++++ if (this.otherwiseEdge !== undefined) {
++++++++ throw new Error('Node already has `otherwise` or `skipTo`');
++++++++ }
++++++++
++++++++ this.otherwiseEdge = new Edge(node, true, undefined, undefined);
++++++++ return this;
++++++++ }
++++++++
++++++++ /**
++++++++ * Create a skipping otherwise edge to node `node`.
++++++++ *
++++++++ * This edge is executed when no other edges match current input. Single
++++++++ * character is consumed upon transition.
++++++++ *
++++++++ * NOTE: At most one otherwise (skipping or not) edge can be set, most nodes
++++++++ * except `Error` require it.
++++++++ *
++++++++ * @param node Target node
++++++++ */
++++++++ public skipTo(node: Node): this {
++++++++ if (this.otherwiseEdge !== undefined) {
++++++++ throw new Error('Node already has `otherwise` or `skipTo`');
++++++++ }
++++++++
++++++++ this.otherwiseEdge = new Edge(node, false, undefined, undefined);
++++++++ return this;
++++++++ }
++++++++
++++++++ // Limited public use
++++++++
++++++++ /** Get otherwise edge. */
++++++++ public getOtherwiseEdge(): Edge | undefined {
++++++++ return this.otherwiseEdge;
++++++++ }
++++++++
++++++++ /** Get list of all non-otherwise edges. */
++++++++ public getEdges(): ReadonlyArray<Edge> {
++++++++ return this.privEdges;
++++++++ }
++++++++
++++++++ /** Get list of all edges (including otherwise, if present). */
++++++++ public getAllEdges(): ReadonlyArray<Edge> {
++++++++ const res = this.privEdges;
++++++++ if (this.otherwiseEdge === undefined) {
++++++++ return res;
++++++++ } else {
++++++++ return res.concat(this.otherwiseEdge);
++++++++ }
++++++++ }
++++++++
++++++++ /** Get iterator through all non-otherwise edges. */
++++++++ public *[Symbol.iterator](): Iterator<Edge> {
++++++++ yield* this.privEdges;
++++++++ }
++++++++
++++++++ // Internal
++++++++
++++++++ protected addEdge(edge: Edge): void {
++++++++ assert.notStrictEqual(edge.key, undefined);
++++++++
++++++++ const index = binarySearch(this.privEdges, edge, Edge.compare);
++++++++ assert(index < 0, 'Attempting to create duplicate edge');
++++++++
++++++++ this.privEdges.splice(-1 - index, 0, edge);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * This node consumes number of characters specified by state's property with
++++++++ * name `field` from the input, and forwards execution to `otherwise` node.
++++++++ */
++++++++export class Consume extends Node {
++++++++ /**
++++++++ * @param field State's property name
++++++++ */
++++++++ constructor(public readonly field: string) {
++++++++ super('consume_' + field);
++++++++
++++++++ if (/^_/.test(field)) {
++++++++ throw new Error(`Can't use internal field in \`consume()\`: "${field}"`);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * This node terminates the execution with an error
++++++++ */
++++++++class NodeError extends Node {
++++++++ /**
++++++++ * @param code Error code to return to user
++++++++ * @param reason Error description to store in parser's state
++++++++ */
++++++++ constructor(public readonly code: number, public readonly reason: string) {
++++++++ super('error');
++++++++ assert.strictEqual(code, code | 0, 'code must be integer');
++++++++ }
++++++++
++++++++ /** `.otherwise()` is not supported on this type of node */
++++++++ public otherwise(node: Node): this { throw new Error('Not supported'); }
++++++++
++++++++ /** `.skipTo()` is not supported on this type of node */
++++++++ public skipTo(node: Node): this { throw new Error('Not supported'); }
++++++++}
++++++++
++++++++export { NodeError as Error };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export { Node } from './base';
++++++++export { Consume } from './consume';
++++++++export { Error } from './error';
++++++++export { Invoke, IInvokeMap } from './invoke';
++++++++export { Match } from './match';
++++++++export { Pause } from './pause';
++++++++export { SpanStart } from './span-start';
++++++++export { SpanEnd } from './span-end';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Code } from '../code';
++++++++import { Edge } from '../edge';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * Map of return codes of the callback. Each key is a return code,
++++++++ * value is the target node that must be executed upon getting such return code.
++++++++ */
++++++++export interface IInvokeMap {
++++++++ readonly [key: number]: Node;
++++++++}
++++++++
++++++++/**
++++++++ * This node invokes either external callback or intrinsic code and passes the
++++++++ * execution to either a target from a `map` (if the return code matches one of
++++++++ * registered in it), or to `otherwise` node.
++++++++ */
++++++++export class Invoke extends Node {
++++++++ /**
++++++++ * @param code External callback or intrinsic code. Can be created with
++++++++ * `builder.code.*()` methods.
++++++++ * @param map Map from callback return codes to target nodes
++++++++ */
++++++++ constructor(public readonly code: Code, map: IInvokeMap) {
++++++++ super('invoke_' + code.name);
++++++++
++++++++ Object.keys(map).forEach((mapKey) => {
++++++++ const numKey: number = parseInt(mapKey, 10);
++++++++ const targetNode = map[numKey]!;
++++++++
++++++++ assert.strictEqual(numKey, numKey | 0,
++++++++ 'Invoke\'s map keys must be integers');
++++++++
++++++++ this.addEdge(new Edge(targetNode, true, numKey, undefined));
++++++++ });
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++
++++++++import { Edge } from '../edge';
++++++++import { Transform } from '../transform';
++++++++import { toBuffer } from '../utils';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * Character/sequence to match.
++++++++ *
++++++++ * May have following types:
++++++++ *
++++++++ * * `number` - for single character
++++++++ * * `string` - for printable character sequence
++++++++ * * `Buffer` - for raw byte sequence
++++++++ */
++++++++export type MatchSingleValue = string | number | Buffer;
++++++++
++++++++/**
++++++++ * Convenience type for passing several characters/sequences to match methods.
++++++++ */
++++++++export type MatchValue = MatchSingleValue | ReadonlyArray<MatchSingleValue>;
++++++++
++++++++/**
++++++++ * A map from characters/sequences to `.select()`'s values. Used for specifying
++++++++ * the value to be passed to `.select()'`s targets.
++++++++ */
++++++++export interface IMatchSelect {
++++++++ readonly [key: string]: number;
++++++++}
++++++++
++++++++/**
++++++++ * This node matches characters/sequences and forwards the execution according
++++++++ * to matched character with optional attached value (See `.select()`).
++++++++ */
++++++++export class Match extends Node {
++++++++ private transformFn: Transform | undefined;
++++++++
++++++++ /**
++++++++ * Set character transformation function.
++++++++ *
++++++++ * @param transform Transformation to apply. Can be created with
++++++++ * `builder.transform.*()` methods.
++++++++ */
++++++++ public transform(transformFn: Transform): this {
++++++++ this.transformFn = transformFn;
++++++++ return this;
++++++++ }
++++++++
++++++++ /**
++++++++ * Match sequence/character and forward execution to `next` on success,
++++++++ * consuming matched bytes of the input.
++++++++ *
++++++++ * No value is attached on such execution forwarding, and the target node
++++++++ * **must not** be an `Invoke` node with a callback expecting the value.
++++++++ *
++++++++ * @param value Sequence/character to be matched
++++++++ * @param next Target node to be executed on success.
++++++++ */
++++++++ public match(value: MatchValue, next: Node): this {
++++++++ if (Array.isArray(value)) {
++++++++ for (const subvalue of value) {
++++++++ this.match(subvalue, next);
++++++++ }
++++++++ return this;
++++++++ }
++++++++
++++++++ const buffer = toBuffer(value as MatchSingleValue);
++++++++ const edge = new Edge(next, false, buffer, undefined);
++++++++ this.addEdge(edge);
++++++++ return this;
++++++++ }
++++++++
++++++++ /**
++++++++ * Match character and forward execution to `next` on success
++++++++ * without consuming one byte of the input.
++++++++ *
++++++++ * No value is attached on such execution forwarding, and the target node
++++++++ * **must not** be an `Invoke` with a callback expecting the value.
++++++++ *
++++++++ * @param value Character to be matched
++++++++ * @param next Target node to be executed on success.
++++++++ */
++++++++ public peek(value: MatchValue, next: Node): this {
++++++++ if (Array.isArray(value)) {
++++++++ for (const subvalue of value) {
++++++++ this.peek(subvalue, next);
++++++++ }
++++++++ return this;
++++++++ }
++++++++
++++++++ const buffer = toBuffer(value as MatchSingleValue);
++++++++ assert.strictEqual(buffer.length, 1,
++++++++ '`.peek()` accepts only single character keys');
++++++++
++++++++ const edge = new Edge(next, true, buffer, undefined);
++++++++ this.addEdge(edge);
++++++++ return this;
++++++++ }
++++++++
++++++++ /**
++++++++ * Match character/sequence and forward execution to `next` on success
++++++++ * consumed matched bytes of the input.
++++++++ *
++++++++ * Value is attached on such execution forwarding, and the target node
++++++++ * **must** be an `Invoke` with a callback expecting the value.
++++++++ *
++++++++ * Possible signatures:
++++++++ *
++++++++ * * `.select(key, value [, next ])`
++++++++ * * `.select({ key: value } [, next])`
++++++++ *
++++++++ * @param keyOrMap Either a sequence to match, or a map from sequences to
++++++++ * values
++++++++ * @param valueOrNext Either an integer value to be forwarded to the target
++++++++ * node, or an otherwise node
++++++++ * @param next Convenience param. Same as calling `.otherwise(...)`
++++++++ */
++++++++ public select(keyOrMap: MatchSingleValue | IMatchSelect,
++++++++ valueOrNext?: number | Node, next?: Node): this {
++++++++ // .select({ key: value, ... }, next)
++++++++ if (typeof keyOrMap === 'object') {
++++++++ assert(valueOrNext instanceof Node,
++++++++ 'Invalid `next` argument of `.select()`');
++++++++ assert.strictEqual(next, undefined,
++++++++ 'Invalid argument count of `.select()`');
++++++++
++++++++ const map: IMatchSelect = keyOrMap as IMatchSelect;
++++++++ next = valueOrNext as Node | undefined;
++++++++
++++++++ Object.keys(map).forEach((mapKey) => {
++++++++ const numKey: number = mapKey as any;
++++++++
++++++++ this.select(numKey, map[numKey]!, next);
++++++++ });
++++++++ return this;
++++++++ }
++++++++
++++++++ // .select(key, value, next)
++++++++ assert.strictEqual(typeof valueOrNext, 'number',
++++++++ 'Invalid `value` argument of `.select()`');
++++++++ assert.notStrictEqual(next, undefined,
++++++++ 'Invalid `next` argument of `.select()`');
++++++++
++++++++ const key = toBuffer(keyOrMap as MatchSingleValue);
++++++++ const value = valueOrNext as number;
++++++++
++++++++ const edge = new Edge(next!, false, key, value);
++++++++ this.addEdge(edge);
++++++++ return this;
++++++++ }
++++++++
++++++++ // Limited public use
++++++++
++++++++ /**
++++++++ * Get tranformation function
++++++++ */
++++++++ public getTransform(): Transform | undefined {
++++++++ return this.transformFn;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * This returns the specified error code, but makes the resumption to
++++++++ * `otherwise` target possible.
++++++++ */
++++++++export class Pause extends Node {
++++++++ /**
++++++++ * @param code Error code to return
++++++++ * @param reason Error description
++++++++ */
++++++++ constructor(public readonly code: number, public readonly reason: string) {
++++++++ super('pause');
++++++++ assert.strictEqual(code, code | 0, 'code must be integer');
++++++++ }
++++++++
++++++++ /**
++++++++ * `.skipTo()` is not supported on this type of node, please use
++++++++ * `.otherwise()`
++++++++ */
++++++++ public skipTo(node: Node): this {
++++++++ throw new Error('Not supported, please use `pause.otherwise()`');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Span } from '../span';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * Indicates span end.
++++++++ *
++++++++ * A callback will be invoked with all input data since the most recent of:
++++++++ *
++++++++ * * Span start invocation
++++++++ * * Parser execution
++++++++ */
++++++++export class SpanEnd extends Node {
++++++++ /**
++++++++ * @param span Span instance
++++++++ */
++++++++ constructor(public readonly span: Span) {
++++++++ super(`span_end_${span.callback.name}`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Span } from '../span';
++++++++import { Node } from './base';
++++++++
++++++++/**
++++++++ * Indicates span start.
++++++++ *
++++++++ * See `SpanEnd` for details on callback invocation.
++++++++ */
++++++++export class SpanStart extends Node {
++++++++ /**
++++++++ * @param span Span instance
++++++++ */
++++++++ constructor(public readonly span: Span) {
++++++++ super(`span_start_${span.callback.name}`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export type PropertyType = 'i8' | 'i16' | 'i32' | 'i64' | 'ptr';
++++++++
++++++++/**
++++++++ * Class describing allocated property in parser's state
++++++++ */
++++++++export class Property {
++++++++ constructor(public readonly ty: PropertyType, public readonly name: string) {
++++++++ if (/^_/.test(name)) {
++++++++ throw new Error(`Can't use internal property name: "${name}"`);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Node } from './node';
++++++++
++++++++/**
++++++++ * This class finds all reachable nodes
++++++++ */
++++++++export class Reachability {
++++++++ /**
++++++++ * Build and return list of reachable nodes.
++++++++ */
++++++++ public build(root: Node): ReadonlyArray<Node> {
++++++++ const res = new Set();
++++++++ const queue = [ root ];
++++++++ while (queue.length !== 0) {
++++++++ const node = queue.pop()!;
++++++++ if (res.has(node)) {
++++++++ continue;
++++++++ }
++++++++ res.add(node);
++++++++
++++++++ for (const edge of node) {
++++++++ queue.push(edge.node);
++++++++ }
++++++++
++++++++ const otherwise = node.getOtherwiseEdge();
++++++++ if (otherwise !== undefined) {
++++++++ queue.push(otherwise.node);
++++++++ }
++++++++ }
++++++++ return Array.from(res) as ReadonlyArray<Node>;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as debugAPI from 'debug';
++++++++
++++++++import { Node, SpanEnd, SpanStart } from './node';
++++++++import { Reachability } from './reachability';
++++++++import { Span } from './span';
++++++++
++++++++const debug = debugAPI('llparse-builder:span-allocator');
++++++++
++++++++type SpanSet = Set<Span>;
++++++++
++++++++interface ISpanActiveInfo {
++++++++ readonly active: Map<Node, SpanSet>;
++++++++ readonly spans: ReadonlyArray<Span>;
++++++++}
++++++++
++++++++type SpanOverlap = Map<Span, SpanSet>;
++++++++
++++++++export interface ISpanAllocatorResult {
++++++++ readonly colors: ReadonlyMap<Span, number>;
++++++++ readonly concurrency: ReadonlyArray<ReadonlyArray<Span> >;
++++++++ readonly max: number;
++++++++}
++++++++
++++++++function id(node: SpanStart | SpanEnd): Span {
++++++++ return node.span;
++++++++}
++++++++
++++++++export class SpanAllocator {
++++++++ public allocate(root: Node): ISpanAllocatorResult {
++++++++ const r = new Reachability();
++++++++ const nodes = r.build(root);
++++++++ const info = this.computeActive(nodes);
++++++++ this.check(info);
++++++++ const overlap = this.computeOverlap(info);
++++++++ return this.color(info.spans, overlap);
++++++++ }
++++++++
++++++++ private computeActive(nodes: ReadonlyArray<Node>): ISpanActiveInfo {
++++++++ const activeMap: Map<Node, SpanSet> = new Map();
++++++++ nodes.forEach((node) => activeMap.set(node, new Set()));
++++++++
++++++++ const queue: Set<Node> = new Set(nodes);
++++++++ const spans: SpanSet = new Set();
++++++++ for (const node of queue) {
++++++++ queue.delete(node);
++++++++
++++++++ const active = activeMap.get(node)!;
++++++++
++++++++ if (node instanceof SpanStart) {
++++++++ const span = id(node);
++++++++ spans.add(span);
++++++++ active.add(span);
++++++++ }
++++++++
++++++++ active.forEach((span) => {
++++++++ // Don't propagate span past the spanEnd
++++++++ if (node instanceof SpanEnd && span === id(node)) {
++++++++ return;
++++++++ }
++++++++
++++++++ node.getAllEdges().forEach((edge) => {
++++++++ const edgeNode = edge.node;
++++++++
++++++++ // Disallow loops
++++++++ if (edgeNode instanceof SpanStart) {
++++++++ assert.notStrictEqual(id(edgeNode), span,
++++++++ `Detected loop in span "${span.callback.name}", started ` +
++++++++ `at "${node.name}"`);
++++++++ }
++++++++
++++++++ const edgeActive = activeMap.get(edgeNode)!;
++++++++ if (edgeActive.has(span)) {
++++++++ return;
++++++++ }
++++++++
++++++++ edgeActive.add(span);
++++++++ queue.add(edgeNode);
++++++++ });
++++++++ });
++++++++ }
++++++++
++++++++ return { active: activeMap, spans: Array.from(spans) };
++++++++ }
++++++++
++++++++ private check(info: ISpanActiveInfo): void {
++++++++ debug('check start');
++++++++ for (const [ node, spans ] of info.active) {
++++++++ for (const edge of node.getAllEdges()) {
++++++++ if (edge.node instanceof SpanStart) {
++++++++ continue;
++++++++ }
++++++++
++++++++ // Skip terminal nodes
++++++++ if (edge.node.getAllEdges().length === 0) {
++++++++ continue;
++++++++ }
++++++++
++++++++ debug('checking edge from %j to %j', node.name, edge.node.name);
++++++++
++++++++ const edgeSpans = info.active.get(edge.node)!;
++++++++ for (const subSpan of edgeSpans) {
++++++++ assert(spans.has(subSpan),
++++++++ `Unmatched span end for "${subSpan.callback.name}" ` +
++++++++ `at "${edge.node.name}", coming from "${node.name}"`);
++++++++ }
++++++++
++++++++ if (edge.node instanceof SpanEnd) {
++++++++ const span = id(edge.node);
++++++++ assert(spans.has(span),
++++++++ `Unmatched span end for "${span.callback.name}"`);
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ private computeOverlap(info: ISpanActiveInfo): SpanOverlap {
++++++++ const active = info.active;
++++++++ const overlap: SpanOverlap = new Map();
++++++++
++++++++ info.spans.forEach((span) => overlap.set(span, new Set()));
++++++++
++++++++ active.forEach((spans) => {
++++++++ spans.forEach((one) => {
++++++++ const set = overlap.get(one)!;
++++++++ spans.forEach((other) => {
++++++++ if (other !== one) {
++++++++ set.add(other);
++++++++ }
++++++++ });
++++++++ });
++++++++ });
++++++++
++++++++ return overlap;
++++++++ }
++++++++
++++++++ private color(spans: ReadonlyArray<Span>, overlapMap: SpanOverlap)
++++++++ : ISpanAllocatorResult {
++++++++ let max = -1;
++++++++ const colors: Map<Span, number> = new Map();
++++++++
++++++++ const allocate = (span: Span): number => {
++++++++ if (colors.has(span)) {
++++++++ return colors.get(span)!;
++++++++ }
++++++++
++++++++ const overlap = overlapMap.get(span)!;
++++++++
++++++++ // See which colors are already used
++++++++ const used: Set<number> = new Set();
++++++++ for (const subSpan of overlap) {
++++++++ if (colors.has(subSpan)) {
++++++++ used.add(colors.get(subSpan)!);
++++++++ }
++++++++ }
++++++++
++++++++ // Find minimum available color
++++++++ let i;
++++++++ for (i = 0; used.has(i); i++) {
++++++++ // no-op
++++++++ }
++++++++
++++++++ max = Math.max(max, i);
++++++++ colors.set(span, i);
++++++++
++++++++ return i;
++++++++ };
++++++++
++++++++ const map: Map<Span, number> = new Map();
++++++++
++++++++ spans.forEach((span) => map.set(span, allocate(span)));
++++++++
++++++++ const concurrency: Span[][] = new Array(max + 1);
++++++++ for (let i = 0; i < concurrency.length; i++) {
++++++++ concurrency[i] = [];
++++++++ }
++++++++
++++++++ spans.forEach((span) => concurrency[allocate(span)].push(span));
++++++++
++++++++ return { colors: map, concurrency, max };
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Span as SpanCallback } from './code';
++++++++import { Node, SpanEnd, SpanStart } from './node';
++++++++
++++++++/**
++++++++ * Spans are used for notifying parser user about matched data. Each byte after
++++++++ * span start will be sent to the span callback until span end is called.
++++++++ */
++++++++export class Span {
++++++++ private readonly startCache: Map<Node, SpanStart> = new Map();
++++++++ private readonly endCache: Map<Node, SpanEnd> = new Map();
++++++++
++++++++ /**
++++++++ * @param callback External callback, must be `code.span(...)` result.
++++++++ */
++++++++ constructor(public readonly callback: SpanCallback) {
++++++++ }
++++++++
++++++++ /**
++++++++ * Create `SpanStart` that indicates the start of the span.
++++++++ *
++++++++ * @param otherwise Optional convenience value. Same as calling
++++++++ * `span.start().otherwise(...)`
++++++++ */
++++++++ public start(otherwise?: Node) {
++++++++ if (otherwise !== undefined && this.startCache.has(otherwise)) {
++++++++ return this.startCache.get(otherwise)!;
++++++++ }
++++++++
++++++++ const res = new SpanStart(this);
++++++++ if (otherwise !== undefined) {
++++++++ res.otherwise(otherwise);
++++++++ this.startCache.set(otherwise, res);
++++++++ }
++++++++ return res;
++++++++ }
++++++++
++++++++ /**
++++++++ * Create `SpanEnd` that indicates the end of the span.
++++++++ *
++++++++ * @param otherwise Optional convenience value. Same as calling
++++++++ * `span.end().otherwise(...)`
++++++++ */
++++++++ public end(otherwise?: Node) {
++++++++ if (otherwise !== undefined && this.endCache.has(otherwise)) {
++++++++ return this.endCache.get(otherwise)!;
++++++++ }
++++++++
++++++++ const res = new SpanEnd(this);
++++++++ if (otherwise !== undefined) {
++++++++ res.otherwise(otherwise);
++++++++ this.endCache.set(otherwise, res);
++++++++ }
++++++++ return res;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export type TransformName = 'to_lower_unsafe' | 'to_lower';
++++++++
++++++++/**
++++++++ * Character transformation.
++++++++ */
++++++++export abstract class Transform {
++++++++ /**
++++++++ * @param name Transform name
++++++++ */
++++++++ constructor(public readonly name: TransformName) {
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++import { ToLower } from './to-lower';
++++++++import { ToLowerUnsafe } from './to-lower-unsafe';
++++++++
++++++++/**
++++++++ * API for creating character transformations.
++++++++ *
++++++++ * The results of methods of this class can be used as an argument to:
++++++++ * `p.node().transform(...)`.
++++++++ */
++++++++export class Creator {
++++++++ /**
++++++++ * Unsafe transform to lowercase.
++++++++ *
++++++++ * The operation of this transformation is equivalent to:
++++++++ * `String.fromCharCode(input.charCodeAt(0) | 0x20)`.
++++++++ */
++++++++ public toLowerUnsafe(): Transform {
++++++++ return new ToLowerUnsafe();
++++++++ }
++++++++
++++++++ /**
++++++++ * Safe transform to lowercase.
++++++++ */
++++++++ public toLower(): Transform {
++++++++ return new ToLower();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export { Transform } from './base';
++++++++export { Creator } from './creator';
++++++++export { ToLowerUnsafe } from './to-lower-unsafe';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLowerUnsafe extends Transform {
++++++++ constructor() {
++++++++ super('to_lower_unsafe');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLower extends Transform {
++++++++ constructor() {
++++++++ super('to_lower');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++
++++++++/**
++++++++ * Internal
++++++++ */
++++++++export function toBuffer(value: number | string | Buffer): Buffer {
++++++++ let res: Buffer;
++++++++ if (Buffer.isBuffer(value)) {
++++++++ res = value;
++++++++ } else if (typeof value === 'string') {
++++++++ res = Buffer.from(value);
++++++++ } else {
++++++++ assert(0 <= value && value <= 0xff, 'Invalid byte value');
++++++++ res = Buffer.from([ value ]);
++++++++ }
++++++++ assert(res.length >= 1, 'Invalid key length');
++++++++ return res;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Builder } from '../src/builder';
++++++++
++++++++describe('LLParse/Builder', () => {
++++++++ let b: Builder;
++++++++ beforeEach(() => {
++++++++ b = new Builder();
++++++++ });
++++++++
++++++++ it('should build primitive graph', () => {
++++++++ const start = b.node('start');
++++++++ const end = b.node('end');
++++++++
++++++++ start
++++++++ .peek('e', end)
++++++++ .match('a', start)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ end
++++++++ .skipTo(start);
++++++++
++++++++ const edges = start.getEdges();
++++++++ assert.strictEqual(edges.length, 2);
++++++++
++++++++ assert(!edges[0].noAdvance);
++++++++ assert.strictEqual(edges[0].node, start);
++++++++
++++++++ assert(edges[1].noAdvance);
++++++++ assert.strictEqual(edges[1].node, end);
++++++++ });
++++++++
++++++++ it('should disallow duplicate edges', () => {
++++++++ const start = b.node('start');
++++++++
++++++++ start.peek('e', start);
++++++++
++++++++ assert.throws(() => {
++++++++ start.peek('e', start);
++++++++ }, /duplicate edge/);
++++++++ });
++++++++
++++++++ it('should disallow select to non-invoke', () => {
++++++++ const start = b.node('start');
++++++++
++++++++ assert.throws(() => {
++++++++ start.select('a', 1, start);
++++++++ }, /value to non-Invoke/);
++++++++ });
++++++++
++++++++ it('should disallow select to match-invoke', () => {
++++++++ const start = b.node('start');
++++++++ const invoke = b.invoke(b.code.match('something'));
++++++++
++++++++ assert.throws(() => {
++++++++ start.select('a', 1, invoke);
++++++++ }, /Invalid.*code signature/);
++++++++ });
++++++++
++++++++ it('should disallow peek to value-invoke', () => {
++++++++ const start = b.node('start');
++++++++ const invoke = b.invoke(b.code.value('something'));
++++++++
++++++++ assert.throws(() => {
++++++++ start.peek('a', invoke);
++++++++ }, /Invalid.*code signature/);
++++++++ });
++++++++
++++++++ it('should allow select to value-invoke', () => {
++++++++ const start = b.node('start');
++++++++ const invoke = b.invoke(b.code.value('something'));
++++++++
++++++++ assert.doesNotThrow(() => {
++++++++ start.select('a', 1, invoke);
++++++++ });
++++++++ });
++++++++
++++++++ it('should create edges for Invoke', () => {
++++++++ const start = b.node('start');
++++++++ const invoke = b.invoke(b.code.value('something'), {
++++++++ '-1': start,
++++++++ '1': start,
++++++++ '10': start,
++++++++ });
++++++++
++++++++ const edges = invoke.getEdges();
++++++++ const keys = edges.map((edge) => edge.key!);
++++++++ assert.deepStrictEqual(keys, [
++++++++ -1,
++++++++ 1,
++++++++ 10,
++++++++ ]);
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Builder, LoopChecker } from '../src/builder';
++++++++
++++++++describe('LLParse/LoopChecker', () => {
++++++++ let b: Builder;
++++++++ let lc: LoopChecker;
++++++++ beforeEach(() => {
++++++++ b = new Builder();
++++++++ lc = new LoopChecker();
++++++++ });
++++++++
++++++++ it('should detect shallow loops', () => {
++++++++ const start = b.node('start');
++++++++
++++++++ start
++++++++ .otherwise(start);
++++++++
++++++++ assert.throws(() => {
++++++++ lc.check(start);
++++++++ }, /Detected loop in "start".*"start"/);
++++++++ });
++++++++
++++++++ it('should detect loops', () => {
++++++++ const start = b.node('start');
++++++++ const a = b.node('a');
++++++++ const invoke = b.invoke(b.code.match('nop'), {
++++++++ 0: start,
++++++++ }, b.error(1, 'error'));
++++++++
++++++++ start
++++++++ .peek('a', a)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ a.otherwise(invoke);
++++++++
++++++++ assert.throws(() => {
++++++++ lc.check(start);
++++++++ }, /Detected loop in "a".*"a" -> "invoke_nop"/);
++++++++ });
++++++++
++++++++ it('should detect seemingly unreachable keys', () => {
++++++++ const start = b.node('start');
++++++++ const loop = b.node('loop');
++++++++
++++++++ start
++++++++ .peek('a', loop)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ loop
++++++++ .match('a', loop)
++++++++ .otherwise(loop);
++++++++
++++++++ assert.throws(() => {
++++++++ lc.check(start);
++++++++ }, /Detected loop in "loop" through.*"loop"/);
++++++++ });
++++++++
++++++++ it('should ignore loops through `peek` to `match`', () => {
++++++++ const start = b.node('start');
++++++++ const a = b.node('a');
++++++++ const invoke = b.invoke(b.code.match('nop'), {
++++++++ 0: start,
++++++++ }, b.error(1, 'error'));
++++++++
++++++++ start
++++++++ .peek('a', a)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ a
++++++++ .match('abc', invoke)
++++++++ .otherwise(start);
++++++++
++++++++ assert.doesNotThrow(() => lc.check(start));
++++++++ });
++++++++
++++++++ it('should ignore irrelevant `peek`s', () => {
++++++++ const start = b.node('start');
++++++++ const a = b.node('a');
++++++++
++++++++ start
++++++++ .peek('a', a)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ a
++++++++ .peek('b', start)
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ assert.doesNotThrow(() => lc.check(start));
++++++++ });
++++++++
++++++++ it('should ignore loops with multi `peek`/`match`', () => {
++++++++ const start = b.node('start');
++++++++ const another = b.node('another');
++++++++
++++++++ const NUM: ReadonlyArray<string> = [
++++++++ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
++++++++ ];
++++++++
++++++++ const ALPHA: ReadonlyArray<string> = [
++++++++ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
++++++++ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
++++++++ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
++++++++ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
++++++++ ];
++++++++
++++++++ start
++++++++ .match(ALPHA, start)
++++++++ .peek(NUM, another)
++++++++ .skipTo(start);
++++++++
++++++++ another
++++++++ .match(NUM, another)
++++++++ .otherwise(start);
++++++++
++++++++ assert.doesNotThrow(() => lc.check(start));
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Builder, SpanAllocator } from '../src/builder';
++++++++
++++++++describe('LLParse/LoopChecker', () => {
++++++++ let b: Builder;
++++++++ let sa: SpanAllocator;
++++++++ beforeEach(() => {
++++++++ b = new Builder();
++++++++ sa = new SpanAllocator();
++++++++ });
++++++++
++++++++ it('should allocate single span', () => {
++++++++ const span = b.span(b.code.span('span'));
++++++++ const start = b.node('start');
++++++++ const body = b.node('body');
++++++++
++++++++ start
++++++++ .otherwise(span.start(body));
++++++++
++++++++ body
++++++++ .skipTo(span.end(start));
++++++++
++++++++ const res = sa.allocate(start);
++++++++
++++++++ assert.strictEqual(res.max, 0);
++++++++
++++++++ assert.strictEqual(res.concurrency.length, 1);
++++++++ assert.ok(res.concurrency[0].includes(span));
++++++++
++++++++ assert.strictEqual(res.colors.size, 1);
++++++++ assert.strictEqual(res.colors.get(span), 0);
++++++++ });
++++++++
++++++++ it('should allocate overlapping spans', () => {
++++++++ const span1 = b.span(b.code.span('span1'));
++++++++ const span2 = b.span(b.code.span('span2'));
++++++++
++++++++ const start = b.node('start');
++++++++ const body1 = b.node('body1');
++++++++ const body2 = b.node('body2');
++++++++
++++++++ start
++++++++ .otherwise(span1.start(body1));
++++++++
++++++++ body1
++++++++ .otherwise(span2.start(body2));
++++++++
++++++++ body2
++++++++ .skipTo(span2.end(span1.end(start)));
++++++++
++++++++ const res = sa.allocate(start);
++++++++
++++++++ assert.strictEqual(res.max, 1);
++++++++
++++++++ assert.strictEqual(res.concurrency.length, 2);
++++++++ assert.ok(res.concurrency[0].includes(span1));
++++++++ assert.ok(res.concurrency[1].includes(span2));
++++++++
++++++++ assert.strictEqual(res.colors.size, 2);
++++++++ assert.strictEqual(res.colors.get(span1), 0);
++++++++ assert.strictEqual(res.colors.get(span2), 1);
++++++++ });
++++++++
++++++++ it('should allocate non-overlapping spans', () => {
++++++++ const span1 = b.span(b.code.span('span1'));
++++++++ const span2 = b.span(b.code.span('span2'));
++++++++
++++++++ const start = b.node('start');
++++++++ const body1 = b.node('body1');
++++++++ const body2 = b.node('body2');
++++++++
++++++++ start
++++++++ .match('a', span1.start(body1))
++++++++ .otherwise(span2.start(body2));
++++++++
++++++++ body1
++++++++ .skipTo(span1.end(start));
++++++++
++++++++ body2
++++++++ .skipTo(span2.end(start));
++++++++
++++++++ const res = sa.allocate(start);
++++++++
++++++++ assert.strictEqual(res.max, 0);
++++++++
++++++++ assert.strictEqual(res.concurrency.length, 1);
++++++++ assert.ok(res.concurrency[0].includes(span1));
++++++++ assert.ok(res.concurrency[0].includes(span2));
++++++++
++++++++ assert.strictEqual(res.colors.size, 2);
++++++++ assert.strictEqual(res.colors.get(span1), 0);
++++++++ assert.strictEqual(res.colors.get(span2), 0);
++++++++ });
++++++++
++++++++ it('should throw on loops', () => {
++++++++ const span = b.span(b.code.span('span_name'));
++++++++
++++++++ const start = b.node('start');
++++++++
++++++++ start
++++++++ .otherwise(span.start(start));
++++++++
++++++++ assert.throws(() => {
++++++++ sa.allocate(start);
++++++++ }, /loop.*span_name/);
++++++++ });
++++++++
++++++++ it('should throw on unmatched ends', () => {
++++++++ const start = b.node('start');
++++++++ const span = b.span(b.code.span('on_data'));
++++++++
++++++++ start.otherwise(span.end().skipTo(start));
++++++++
++++++++ assert.throws(() => sa.allocate(start), /unmatched.*on_data/i);
++++++++ });
++++++++
++++++++ it('should throw on branched unmatched ends', () => {
++++++++ const start = b.node('start');
++++++++ const end = b.node('end');
++++++++ const span = b.span(b.code.span('on_data'));
++++++++
++++++++ start
++++++++ .match('a', end)
++++++++ .match('b', span.start(end))
++++++++ .otherwise(b.error(1, 'error'));
++++++++
++++++++ end
++++++++ .otherwise(span.end(start));
++++++++
++++++++ assert.throws(() => sa.allocate(start), /unmatched.*on_data/i);
++++++++ });
++++++++
++++++++ it('should propagate through the Invoke map', () => {
++++++++ const start = b.node('start');
++++++++ const span = b.span(b.code.span('llparse__on_data'));
++++++++
++++++++ b.property('i8', 'custom');
++++++++
++++++++ start.otherwise(b.invoke(b.code.load('custom'), {
++++++++ 0: span.end().skipTo(start),
++++++++ }, span.end().skipTo(start)));
++++++++
++++++++ assert.doesNotThrow(() => sa.allocate(span.start(start)));
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "compilerOptions": {
++++++++ "strict": true,
++++++++ "target": "es2017",
++++++++ "module": "commonjs",
++++++++ "moduleResolution": "node",
++++++++ "outDir": "./lib",
++++++++ "declaration": true,
++++++++ "pretty": true,
++++++++ "sourceMap": true
++++++++ },
++++++++ "include": [
++++++++ "src/**/*.ts"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "defaultSeverity": "error",
++++++++ "extends": [
++++++++ "tslint:recommended"
++++++++ ],
++++++++ "jsRules": {},
++++++++ "rules": {
++++++++ "no-bitwise": null,
++++++++ "quotemark": [
++++++++ true, "single", "avoid-escape", "avoid-template"
++++++++ ]
++++++++ },
++++++++ "rulesDirectory": []
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node_modules/
++++++++lib/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++sudo: false
++++++++language: node_js
++++++++node_js:
++++++++ - "stable"
++++++++script:
++++++++ npm test
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# llparse-frontend
++++++++[](http://travis-ci.org/indutny/llparse-frontend)
++++++++[](https://badge.fury.io/js/llparse-frontend)
++++++++
++++++++WIP
++++++++
++++++++#### LICENSE
++++++++
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2018.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse-frontend",
++++++++ "version": "3.0.0",
++++++++ "lockfileVersion": 1,
++++++++ "requires": true,
++++++++ "dependencies": {
++++++++ "@babel/code-frame": {
++++++++ "version": "7.10.3",
++++++++ "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.3.tgz",
++++++++ "integrity": "sha512-fDx9eNW0qz0WkUeqL6tXEXzVlPh6Y5aCDEZesl0xBGA8ndRukX91Uk44ZqnkECp01NAZUdCAl+aiQNGi0k88Eg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/highlight": "^7.10.3"
++++++++ }
++++++++ },
++++++++ "@babel/helper-validator-identifier": {
++++++++ "version": "7.10.3",
++++++++ "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.3.tgz",
++++++++ "integrity": "sha512-bU8JvtlYpJSBPuj1VUmKpFGaDZuLxASky3LhaKj3bmpSTY6VWooSM8msk+Z0CZoErFye2tlABF6yDkT3FOPAXw==",
++++++++ "dev": true
++++++++ },
++++++++ "@babel/highlight": {
++++++++ "version": "7.10.3",
++++++++ "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.10.3.tgz",
++++++++ "integrity": "sha512-Ih9B/u7AtgEnySE2L2F0Xm0GaM729XqqLfHkalTsbjXGyqmf/6M0Cu0WpvqueUlW+xk88BHw9Nkpj49naU+vWw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/helper-validator-identifier": "^7.10.3",
++++++++ "chalk": "^2.0.0",
++++++++ "js-tokens": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/mocha": {
++++++++ "version": "8.0.3",
++++++++ "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.3.tgz",
++++++++ "integrity": "sha512-vyxR57nv8NfcU0GZu8EUXZLTbCMupIUwy95LJ6lllN+JRPG25CwMHoB1q5xKh8YKhQnHYRAn4yW2yuHbf/5xgg==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/node": {
++++++++ "version": "14.11.8",
++++++++ "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz",
++++++++ "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-colors": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz",
++++++++ "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-regex": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
++++++++ "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-styles": {
++++++++ "version": "3.2.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
++++++++ "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^1.9.0"
++++++++ }
++++++++ },
++++++++ "anymatch": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz",
++++++++ "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "normalize-path": "^3.0.0",
++++++++ "picomatch": "^2.0.4"
++++++++ }
++++++++ },
++++++++ "arg": {
++++++++ "version": "4.1.3",
++++++++ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
++++++++ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
++++++++ "dev": true
++++++++ },
++++++++ "argparse": {
++++++++ "version": "1.0.10",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
++++++++ "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "sprintf-js": "~1.0.2"
++++++++ }
++++++++ },
++++++++ "array.prototype.map": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/array.prototype.map/-/array.prototype.map-1.0.2.tgz",
++++++++ "integrity": "sha512-Az3OYxgsa1g7xDYp86l0nnN4bcmuEITGe1rbdEBVkrqkzMgDcbdQ2R7r41pNzti+4NMces3H8gMmuioZUilLgw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "es-array-method-boxes-properly": "^1.0.0",
++++++++ "is-string": "^1.0.4"
++++++++ }
++++++++ },
++++++++ "balanced-match": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
++++++++ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
++++++++ "dev": true
++++++++ },
++++++++ "binary-extensions": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
++++++++ "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==",
++++++++ "dev": true
++++++++ },
++++++++ "binary-search": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/binary-search/-/binary-search-1.3.6.tgz",
++++++++ "integrity": "sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA=="
++++++++ },
++++++++ "brace-expansion": {
++++++++ "version": "1.1.11",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
++++++++ "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "balanced-match": "^1.0.0",
++++++++ "concat-map": "0.0.1"
++++++++ }
++++++++ },
++++++++ "braces": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
++++++++ "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fill-range": "^7.0.1"
++++++++ }
++++++++ },
++++++++ "browser-stdout": {
++++++++ "version": "1.3.1",
++++++++ "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
++++++++ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
++++++++ "dev": true
++++++++ },
++++++++ "buffer-from": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
++++++++ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
++++++++ "dev": true
++++++++ },
++++++++ "builtin-modules": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
++++++++ "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
++++++++ "dev": true
++++++++ },
++++++++ "camelcase": {
++++++++ "version": "5.3.1",
++++++++ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
++++++++ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
++++++++ "dev": true
++++++++ },
++++++++ "chalk": {
++++++++ "version": "2.4.2",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
++++++++ "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.1",
++++++++ "escape-string-regexp": "^1.0.5",
++++++++ "supports-color": "^5.3.0"
++++++++ }
++++++++ },
++++++++ "chokidar": {
++++++++ "version": "3.4.2",
++++++++ "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.4.2.tgz",
++++++++ "integrity": "sha512-IZHaDeBeI+sZJRX7lGcXsdzgvZqKv6sECqsbErJA4mHWfpRrD8B97kSFN4cQz6nGBGiuFia1MKR4d6c1o8Cv7A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "anymatch": "~3.1.1",
++++++++ "braces": "~3.0.2",
++++++++ "fsevents": "~2.1.2",
++++++++ "glob-parent": "~5.1.0",
++++++++ "is-binary-path": "~2.1.0",
++++++++ "is-glob": "~4.0.1",
++++++++ "normalize-path": "~3.0.0",
++++++++ "readdirp": "~3.4.0"
++++++++ }
++++++++ },
++++++++ "cliui": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz",
++++++++ "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^3.1.0",
++++++++ "strip-ansi": "^5.2.0",
++++++++ "wrap-ansi": "^5.1.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "1.9.3",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz",
++++++++ "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "1.1.3"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
++++++++ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
++++++++ "dev": true
++++++++ },
++++++++ "commander": {
++++++++ "version": "2.15.1",
++++++++ "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
++++++++ "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
++++++++ "dev": true
++++++++ },
++++++++ "concat-map": {
++++++++ "version": "0.0.1",
++++++++ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
++++++++ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
++++++++ "dev": true
++++++++ },
++++++++ "debug": {
++++++++ "version": "3.2.6",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
++++++++ "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "decamelize": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
++++++++ "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
++++++++ "dev": true
++++++++ },
++++++++ "define-properties": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
++++++++ "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
++++++++ "requires": {
++++++++ "object-keys": "^1.0.12"
++++++++ }
++++++++ },
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "7.0.3",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
++++++++ "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-abstract": {
++++++++ "version": "1.17.7",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.7.tgz",
++++++++ "integrity": "sha512-VBl/gnfcJ7OercKA9MVaegWsBHFjV492syMudcnQZvt/Dw8ezpcOHYZXa/J96O8vx+g4x65YKhxOwDUh63aS5g==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.1.tgz",
++++++++ "integrity": "sha512-VT/cxmx5yaoHSOTSyrCygIDFco+RsibY2NM0a4RdEeY/4KgqezwFtK1yr3U67xYhqJSlASm2pKhLVzPj2lr4bA==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.18.0-next.0",
++++++++ "has-symbols": "^1.0.1",
++++++++ "object-keys": "^1.1.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "es-array-method-boxes-properly": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz",
++++++++ "integrity": "sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-get-iterator": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/es-get-iterator/-/es-get-iterator-1.1.0.tgz",
++++++++ "integrity": "sha512-UfrmHuWQlNMTs35e1ypnvikg6jCz3SK8v8ImvmDsh36fCVUR1MqoFDiyn0/k52C8NqO3YsO8Oe0azeesNuqSsQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-abstract": "^1.17.4",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-arguments": "^1.0.4",
++++++++ "is-map": "^2.0.1",
++++++++ "is-set": "^2.0.1",
++++++++ "is-string": "^1.0.5",
++++++++ "isarray": "^2.0.5"
++++++++ }
++++++++ },
++++++++ "es-to-primitive": {
++++++++ "version": "1.2.1",
++++++++ "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
++++++++ "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==",
++++++++ "requires": {
++++++++ "is-callable": "^1.1.4",
++++++++ "is-date-object": "^1.0.1",
++++++++ "is-symbol": "^1.0.2"
++++++++ }
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
++++++++ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
++++++++ "dev": true
++++++++ },
++++++++ "esprima": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
++++++++ "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
++++++++ "dev": true
++++++++ },
++++++++ "fill-range": {
++++++++ "version": "7.0.1",
++++++++ "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
++++++++ "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "to-regex-range": "^5.0.1"
++++++++ }
++++++++ },
++++++++ "find-up": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz",
++++++++ "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^6.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "flat": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz",
++++++++ "integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-buffer": "~2.0.3"
++++++++ }
++++++++ },
++++++++ "fs.realpath": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
++++++++ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
++++++++ "dev": true
++++++++ },
++++++++ "fsevents": {
++++++++ "version": "2.1.3",
++++++++ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz",
++++++++ "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==",
++++++++ "dev": true,
++++++++ "optional": true
++++++++ },
++++++++ "function-bind": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
++++++++ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
++++++++ },
++++++++ "get-caller-file": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
++++++++ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.2",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
++++++++ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "glob-parent": {
++++++++ "version": "5.1.1",
++++++++ "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.1.tgz",
++++++++ "integrity": "sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-glob": "^4.0.1"
++++++++ }
++++++++ },
++++++++ "growl": {
++++++++ "version": "1.10.5",
++++++++ "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz",
++++++++ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==",
++++++++ "dev": true
++++++++ },
++++++++ "has": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
++++++++ "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
++++++++ "requires": {
++++++++ "function-bind": "^1.1.1"
++++++++ }
++++++++ },
++++++++ "has-flag": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
++++++++ "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
++++++++ "dev": true
++++++++ },
++++++++ "has-symbols": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",
++++++++ "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg=="
++++++++ },
++++++++ "he": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
++++++++ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
++++++++ "dev": true
++++++++ },
++++++++ "inflight": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
++++++++ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "once": "^1.3.0",
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "inherits": {
++++++++ "version": "2.0.3",
++++++++ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
++++++++ "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
++++++++ "dev": true
++++++++ },
++++++++ "is-arguments": {
++++++++ "version": "1.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz",
++++++++ "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-binary-path": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
++++++++ "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "binary-extensions": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "is-buffer": {
++++++++ "version": "2.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz",
++++++++ "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==",
++++++++ "dev": true
++++++++ },
++++++++ "is-callable": {
++++++++ "version": "1.2.2",
++++++++ "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.2.tgz",
++++++++ "integrity": "sha512-dnMqspv5nU3LoewK2N/y7KLtxtakvTuaCsU9FU50/QDmdbHNy/4/JuRtMHqRU22o3q+W89YQndQEeCVwK+3qrA=="
++++++++ },
++++++++ "is-date-object": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz",
++++++++ "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g=="
++++++++ },
++++++++ "is-extglob": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
++++++++ "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=",
++++++++ "dev": true
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
++++++++ "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=",
++++++++ "dev": true
++++++++ },
++++++++ "is-glob": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz",
++++++++ "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-extglob": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "is-map": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-map/-/is-map-2.0.1.tgz",
++++++++ "integrity": "sha512-T/S49scO8plUiAOA2DBTBG3JHpn1yiw0kRp6dgiZ0v2/6twi5eiB0rHtHFH9ZIrvlWc6+4O+m4zg5+Z833aXgw==",
++++++++ "dev": true
++++++++ },
++++++++ "is-negative-zero": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.0.tgz",
++++++++ "integrity": "sha1-lVOxIbD6wohp2p7UWeIMdUN4hGE="
++++++++ },
++++++++ "is-number": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
++++++++ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
++++++++ "dev": true
++++++++ },
++++++++ "is-plain-obj": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
++++++++ "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
++++++++ "dev": true
++++++++ },
++++++++ "is-regex": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.1.tgz",
++++++++ "integrity": "sha512-1+QkEcxiLlB7VEyFtyBg94e08OAsvq7FUBgApTq/w2ymCLyKJgDPsybBENVtA7XCQEgEXxKPonG+mvYRxh/LIg==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "is-set": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-set/-/is-set-2.0.1.tgz",
++++++++ "integrity": "sha512-eJEzOtVyenDs1TMzSQ3kU3K+E0GUS9sno+F0OBT97xsgcJsF9nXMBtkT9/kut5JEpM7oL7X/0qxR17K3mcwIAA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-string": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/is-string/-/is-string-1.0.5.tgz",
++++++++ "integrity": "sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==",
++++++++ "dev": true
++++++++ },
++++++++ "is-symbol": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz",
++++++++ "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "isarray": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz",
++++++++ "integrity": "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==",
++++++++ "dev": true
++++++++ },
++++++++ "isexe": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
++++++++ "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-iterator": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/iterate-iterator/-/iterate-iterator-1.0.1.tgz",
++++++++ "integrity": "sha512-3Q6tudGN05kbkDQDI4CqjaBf4qf85w6W6GnuZDtUVYwKgtC1q8yxYX7CZed7N+tLzQqS6roujWvszf13T+n9aw==",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-value": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/iterate-value/-/iterate-value-1.0.2.tgz",
++++++++ "integrity": "sha512-A6fMAio4D2ot2r/TYzr4yUWrmwNdsN5xL7+HUiyACE4DXm+q8HtPcnFTp+NnW3k4N05tZ7FVYFFb2CR13NxyHQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-get-iterator": "^1.0.2",
++++++++ "iterate-iterator": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "js-tokens": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
++++++++ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
++++++++ "dev": true
++++++++ },
++++++++ "js-yaml": {
++++++++ "version": "3.14.0",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz",
++++++++ "integrity": "sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "argparse": "^1.0.7",
++++++++ "esprima": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "llparse-builder": {
++++++++ "version": "1.5.2",
++++++++ "resolved": "https://registry.npmjs.org/llparse-builder/-/llparse-builder-1.5.2.tgz",
++++++++ "integrity": "sha512-i862UNC3YUEdlfK/NUCJxlKjtWjgAI9AJXDRgjcfRHfwFt4Sf8eFPTRsc91/2R9MBZ0kyFdfhi8SVhMsZf1gNQ==",
++++++++ "requires": {
++++++++ "@types/debug": "4.1.5 ",
++++++++ "binary-search": "^1.3.6",
++++++++ "debug": "^4.2.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ=="
++++++++ },
++++++++ "debug": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz",
++++++++ "integrity": "sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==",
++++++++ "requires": {
++++++++ "ms": "2.1.2"
++++++++ }
++++++++ },
++++++++ "ms": {
++++++++ "version": "2.1.2",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
++++++++ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
++++++++ }
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",
++++++++ "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^5.0.0"
++++++++ }
++++++++ },
++++++++ "log-symbols": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz",
++++++++ "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "chalk": "^4.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-styles": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
++++++++ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^2.0.1"
++++++++ }
++++++++ },
++++++++ "chalk": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz",
++++++++ "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^4.1.0",
++++++++ "supports-color": "^7.1.0"
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "~1.1.4"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ },
++++++++ "has-flag": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
++++++++ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "7.2.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
++++++++ "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^4.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "make-error": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
++++++++ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
++++++++ "dev": true
++++++++ },
++++++++ "minimatch": {
++++++++ "version": "3.0.4",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
++++++++ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "brace-expansion": "^1.1.7"
++++++++ }
++++++++ },
++++++++ "mkdirp": {
++++++++ "version": "0.5.5",
++++++++ "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
++++++++ "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "minimist": "^1.2.5"
++++++++ },
++++++++ "dependencies": {
++++++++ "minimist": {
++++++++ "version": "1.2.5",
++++++++ "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
++++++++ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "mocha": {
++++++++ "version": "8.1.3",
++++++++ "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.1.3.tgz",
++++++++ "integrity": "sha512-ZbaYib4hT4PpF4bdSO2DohooKXIn4lDeiYqB+vTmCdr6l2woW0b6H3pf5x4sM5nwQMru9RvjjHYWVGltR50ZBw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-colors": "4.1.1",
++++++++ "browser-stdout": "1.3.1",
++++++++ "chokidar": "3.4.2",
++++++++ "debug": "4.1.1",
++++++++ "diff": "4.0.2",
++++++++ "escape-string-regexp": "4.0.0",
++++++++ "find-up": "5.0.0",
++++++++ "glob": "7.1.6",
++++++++ "growl": "1.10.5",
++++++++ "he": "1.2.0",
++++++++ "js-yaml": "3.14.0",
++++++++ "log-symbols": "4.0.0",
++++++++ "minimatch": "3.0.4",
++++++++ "ms": "2.1.2",
++++++++ "object.assign": "4.1.0",
++++++++ "promise.allsettled": "1.0.2",
++++++++ "serialize-javascript": "4.0.0",
++++++++ "strip-json-comments": "3.0.1",
++++++++ "supports-color": "7.1.0",
++++++++ "which": "2.0.2",
++++++++ "wide-align": "1.1.3",
++++++++ "workerpool": "6.0.0",
++++++++ "yargs": "13.3.2",
++++++++ "yargs-parser": "13.1.2",
++++++++ "yargs-unparser": "1.6.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "debug": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
++++++++ "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
++++++++ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.6",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
++++++++ "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "has-flag": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
++++++++ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
++++++++ "dev": true
++++++++ },
++++++++ "ms": {
++++++++ "version": "2.1.2",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
++++++++ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "7.1.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz",
++++++++ "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^4.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "ms": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
++++++++ "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
++++++++ },
++++++++ "normalize-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
++++++++ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
++++++++ "dev": true
++++++++ },
++++++++ "object-inspect": {
++++++++ "version": "1.8.0",
++++++++ "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.8.0.tgz",
++++++++ "integrity": "sha512-jLdtEOB112fORuypAyl/50VRVIBIdVQOSUUGQHzJ4xBSbit81zRarz7GThkEFZy1RceYrWYcPcBFPQwHyAc1gA=="
++++++++ },
++++++++ "object-keys": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
++++++++ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz",
++++++++ "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.2",
++++++++ "function-bind": "^1.1.1",
++++++++ "has-symbols": "^1.0.0",
++++++++ "object-keys": "^1.0.11"
++++++++ }
++++++++ },
++++++++ "once": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
++++++++ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.0.2.tgz",
++++++++ "integrity": "sha512-iwqZSOoWIW+Ew4kAGUlN16J4M7OB3ysMLSZtnhmqx7njIHFPlxWBX8xo3lVTyFVq6mI/lL9qt2IsN1sHwaxJkg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz",
++++++++ "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^3.0.2"
++++++++ }
++++++++ },
++++++++ "p-try": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
++++++++ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
++++++++ "dev": true
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
++++++++ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
++++++++ "dev": true
++++++++ },
++++++++ "path-is-absolute": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
++++++++ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
++++++++ "dev": true
++++++++ },
++++++++ "path-parse": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
++++++++ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==",
++++++++ "dev": true
++++++++ },
++++++++ "picomatch": {
++++++++ "version": "2.2.2",
++++++++ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz",
++++++++ "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==",
++++++++ "dev": true
++++++++ },
++++++++ "promise.allsettled": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/promise.allsettled/-/promise.allsettled-1.0.2.tgz",
++++++++ "integrity": "sha512-UpcYW5S1RaNKT6pd+s9jp9K9rlQge1UXKskec0j6Mmuq7UJCvlS2J2/s/yuPN8ehftf9HXMxWlKiPbGGUzpoRg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "array.prototype.map": "^1.0.1",
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "iterate-value": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "randombytes": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
++++++++ "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "safe-buffer": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "readdirp": {
++++++++ "version": "3.4.0",
++++++++ "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz",
++++++++ "integrity": "sha512-0xe001vZBnJEK+uKcj8qOhyAKPzIT+gStxWr3LCB0DwcXR5NZJ3IaC+yGnHCYzB/S7ov3m3EEbZI2zeNvX+hGQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "picomatch": "^2.2.1"
++++++++ }
++++++++ },
++++++++ "require-directory": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
++++++++ "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=",
++++++++ "dev": true
++++++++ },
++++++++ "require-main-filename": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
++++++++ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
++++++++ "dev": true
++++++++ },
++++++++ "resolve": {
++++++++ "version": "1.17.0",
++++++++ "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",
++++++++ "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "path-parse": "^1.0.6"
++++++++ }
++++++++ },
++++++++ "safe-buffer": {
++++++++ "version": "5.2.1",
++++++++ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
++++++++ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
++++++++ "dev": true
++++++++ },
++++++++ "semver": {
++++++++ "version": "5.7.1",
++++++++ "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
++++++++ "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
++++++++ "dev": true
++++++++ },
++++++++ "serialize-javascript": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-4.0.0.tgz",
++++++++ "integrity": "sha512-GaNA54380uFefWghODBWEGisLZFj00nS5ACs6yHa9nLqlLpVLO8ChDGeKRjZnV4Nh4n0Qi7nhYZD/9fCPzEqkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "randombytes": "^2.1.0"
++++++++ }
++++++++ },
++++++++ "set-blocking": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
++++++++ "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
++++++++ "dev": true
++++++++ },
++++++++ "source-map": {
++++++++ "version": "0.6.1",
++++++++ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
++++++++ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
++++++++ "dev": true
++++++++ },
++++++++ "source-map-support": {
++++++++ "version": "0.5.19",
++++++++ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz",
++++++++ "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "buffer-from": "^1.0.0",
++++++++ "source-map": "^0.6.0"
++++++++ }
++++++++ },
++++++++ "sprintf-js": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
++++++++ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
++++++++ "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimend": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz",
++++++++ "integrity": "sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimstart": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz",
++++++++ "integrity": "sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
++++++++ "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "strip-json-comments": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.0.1.tgz",
++++++++ "integrity": "sha512-VTyMAUfdm047mwKl+u79WIdrZxtFtn+nBxHeb844XBQ9uMNTuTHdx2hc5RiAJYqwTj3wc/xe5HLSdJSkJ+WfZw==",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "5.4.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
++++++++ "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "to-regex-range": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
++++++++ "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-number": "^7.0.0"
++++++++ }
++++++++ },
++++++++ "ts-node": {
++++++++ "version": "9.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.0.0.tgz",
++++++++ "integrity": "sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "arg": "^4.1.0",
++++++++ "diff": "^4.0.1",
++++++++ "make-error": "^1.1.1",
++++++++ "source-map-support": "^0.5.17",
++++++++ "yn": "3.1.1"
++++++++ }
++++++++ },
++++++++ "tslib": {
++++++++ "version": "1.13.0",
++++++++ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.13.0.tgz",
++++++++ "integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==",
++++++++ "dev": true
++++++++ },
++++++++ "tslint": {
++++++++ "version": "5.20.1",
++++++++ "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.20.1.tgz",
++++++++ "integrity": "sha512-EcMxhzCFt8k+/UP5r8waCf/lzmeSyVlqxqMEDQE7rWYiQky8KpIBz1JAoYXfROHrPZ1XXd43q8yQnULOLiBRQg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/code-frame": "^7.0.0",
++++++++ "builtin-modules": "^1.1.1",
++++++++ "chalk": "^2.3.0",
++++++++ "commander": "^2.12.1",
++++++++ "diff": "^4.0.1",
++++++++ "glob": "^7.1.1",
++++++++ "js-yaml": "^3.13.1",
++++++++ "minimatch": "^3.0.4",
++++++++ "mkdirp": "^0.5.1",
++++++++ "resolve": "^1.3.2",
++++++++ "semver": "^5.3.0",
++++++++ "tslib": "^1.8.0",
++++++++ "tsutils": "^2.29.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "tsutils": {
++++++++ "version": "2.29.0",
++++++++ "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz",
++++++++ "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "tslib": "^1.8.1"
++++++++ }
++++++++ },
++++++++ "typescript": {
++++++++ "version": "4.0.3",
++++++++ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz",
++++++++ "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==",
++++++++ "dev": true
++++++++ },
++++++++ "which": {
++++++++ "version": "2.0.2",
++++++++ "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
++++++++ "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "isexe": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "which-module": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",
++++++++ "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=",
++++++++ "dev": true
++++++++ },
++++++++ "wide-align": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz",
++++++++ "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^1.0.2 || 2"
++++++++ }
++++++++ },
++++++++ "workerpool": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.0.0.tgz",
++++++++ "integrity": "sha512-fU2OcNA/GVAJLLyKUoHkAgIhKb0JoCpSjLC/G2vYKxUjVmQwGbRVeoPJ1a8U4pnVofz4AQV5Y/NEw8oKqxEBtA==",
++++++++ "dev": true
++++++++ },
++++++++ "wrap-ansi": {
++++++++ "version": "5.1.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz",
++++++++ "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.0",
++++++++ "string-width": "^3.0.0",
++++++++ "strip-ansi": "^5.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "wrappy": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
++++++++ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
++++++++ "dev": true
++++++++ },
++++++++ "y18n": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz",
++++++++ "integrity": "sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==",
++++++++ "dev": true
++++++++ },
++++++++ "yargs": {
++++++++ "version": "13.3.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.3.2.tgz",
++++++++ "integrity": "sha512-AX3Zw5iPruN5ie6xGRIDgqkT+ZhnRlZMLMHAs8tg7nRruy2Nb+i5o9bwghAogtM08q1dpr2LVoS8KSTMYpWXUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^13.1.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "2.3.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
++++++++ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "13.1.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.2.tgz",
++++++++ "integrity": "sha512-3lbsNRf/j+A4QuSZfDRA7HRSfWrzO0YjqTJd5kjAq37Zep1CEgaYmrH9Q3GwPiB9cHyd1Y1UwggGhJGoxipbzg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ },
++++++++ "yargs-unparser": {
++++++++ "version": "1.6.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-1.6.1.tgz",
++++++++ "integrity": "sha512-qZV14lK9MWsGCmcr7u5oXGH0dbGqZAIxTDrWXZDo5zUr6b6iUmelNKO6x6R1dQT24AH3LgRxJpr8meWy2unolA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.3.1",
++++++++ "decamelize": "^1.2.0",
++++++++ "flat": "^4.1.0",
++++++++ "is-plain-obj": "^1.1.0",
++++++++ "yargs": "^14.2.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "2.3.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
++++++++ "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ },
++++++++ "yargs": {
++++++++ "version": "14.2.3",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-14.2.3.tgz",
++++++++ "integrity": "sha512-ZbotRWhF+lkjijC/VhmOT9wSgyBQ7+zr13+YLkhfsSiTriYsMzkTUFP18pFhWwBeMa5gUc1MzbhrO6/VB7c9Xg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "decamelize": "^1.2.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^15.0.1"
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "15.0.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz",
++++++++ "integrity": "sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yn": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
++++++++ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse-frontend",
++++++++ "version": "3.0.0",
++++++++ "description": "Frontend for LLParse compiler",
++++++++ "main": "lib/frontend.js",
++++++++ "types": "lib/frontend.d.ts",
++++++++ "scripts": {
++++++++ "build": "tsc",
++++++++ "clean": "rm -rf lib",
++++++++ "prepare": "npm run clean && npm run build",
++++++++ "lint": "tslint -c tslint.json src/**/*.ts test/**/*.ts",
++++++++ "fix-lint": "npm run lint -- --fix",
++++++++ "mocha": "mocha --timeout=10000 -r ts-node/register/type-check --reporter spec test/*-test.ts",
++++++++ "test": "npm run mocha && npm run lint"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+ssh://git@github.com/indutny/llparse-frontend.git"
++++++++ },
++++++++ "keywords": [
++++++++ "llparse",
++++++++ "frontend"
++++++++ ],
++++++++ "author": "Fedor Indutny <fedor@indutny.com> (http://darksi.de/)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/indutny/llparse-frontend/issues"
++++++++ },
++++++++ "homepage": "https://github.com/indutny/llparse-frontend#readme",
++++++++ "dependencies": {
++++++++ "debug": "^3.2.6",
++++++++ "llparse-builder": "^1.5.2"
++++++++ },
++++++++ "devDependencies": {
++++++++ "@types/debug": "^4.1.5",
++++++++ "@types/mocha": "^8.0.3",
++++++++ "@types/node": "^14.11.8",
++++++++ "mocha": "^8.1.3",
++++++++ "ts-node": "^9.0.0",
++++++++ "tslint": "^5.20.1",
++++++++ "typescript": "^4.0.3"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class And extends FieldValue {
++++++++ constructor(name: string, field: string, value: number) {
++++++++ super('match', `and_${field}_${toCacheKey(value)}`, name, field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export type Signature = 'match' | 'value' | 'span';
++++++++
++++++++export abstract class Code {
++++++++ constructor(public readonly signature: Signature,
++++++++ public readonly cacheKey: string,
++++++++ public readonly name: string) {
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Code, Signature } from './base';
++++++++
++++++++export abstract class External extends Code {
++++++++ constructor(signature: Signature, name: string) {
++++++++ super(signature, 'external_' + name, name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Signature } from './base';
++++++++import { Field } from './field';
++++++++
++++++++export abstract class FieldValue extends Field {
++++++++ constructor(signature: Signature, cacheKey: string, name: string,
++++++++ field: string, public readonly value: number) {
++++++++ super(signature, cacheKey, name, field);
++++++++
++++++++ assert.strictEqual(value, value | 0, 'FieldValue `value` must be integer');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Code, Signature } from './base';
++++++++
++++++++export abstract class Field extends Code {
++++++++ constructor(signature: Signature, cacheKey: string, name: string,
++++++++ public readonly field: string) {
++++++++ super(signature, cacheKey, name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export * from './and';
++++++++export * from './base';
++++++++export * from './external';
++++++++export * from './field-value';
++++++++export * from './field';
++++++++export * from './is-equal';
++++++++export * from './load';
++++++++export * from './match';
++++++++export * from './mul-add';
++++++++export * from './or';
++++++++export * from './span';
++++++++export * from './store';
++++++++export * from './test';
++++++++export * from './update';
++++++++export * from './value';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class IsEqual extends FieldValue {
++++++++ constructor(name: string, field: string, value: number) {
++++++++ super('match', `is_equal_${field}_${toCacheKey(value)}`, name, field,
++++++++ value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Field } from './field';
++++++++
++++++++export class Load extends Field {
++++++++ constructor(name: string, field: string) {
++++++++ super('match', `load_${field}`, name, field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { External } from './external';
++++++++
++++++++export class Match extends External {
++++++++ constructor(name: string) {
++++++++ super('match', name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { Field } from './field';
++++++++
++++++++export interface IMulAddOptions {
++++++++ readonly base: number;
++++++++ readonly max?: number;
++++++++ readonly signed: boolean;
++++++++}
++++++++
++++++++function toOptionsKey(options: IMulAddOptions): string {
++++++++ let res = `base_${toCacheKey(options.base)}`;
++++++++ if (options.max !== undefined) {
++++++++ res += `_max_${toCacheKey(options.max)}`;
++++++++ }
++++++++ if (options.signed !== undefined) {
++++++++ res += `_signed_${toCacheKey(options.signed)}`;
++++++++ }
++++++++ return res;
++++++++}
++++++++
++++++++export class MulAdd extends Field {
++++++++ constructor(name: string, field: string,
++++++++ public readonly options: IMulAddOptions) {
++++++++ super('value', `mul_add_${field}_${toOptionsKey(options)}`, name, field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Or extends FieldValue {
++++++++ constructor(name: string, field: string, value: number) {
++++++++ super('match', `or_${field}_${toCacheKey(value)}`, name, field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { External } from './external';
++++++++
++++++++export class Span extends External {
++++++++ constructor(name: string) {
++++++++ super('span', name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Field } from './field';
++++++++
++++++++export class Store extends Field {
++++++++ constructor(name: string, field: string) {
++++++++ super('value', `store_${field}`, name, field);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Test extends FieldValue {
++++++++ constructor(name: string, field: string, value: number) {
++++++++ super('match', `test_${field}_${toCacheKey(value)}`, name, field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { toCacheKey } from '../utils';
++++++++import { FieldValue } from './field-value';
++++++++
++++++++export class Update extends FieldValue {
++++++++ constructor(name: string, field: string, value: number) {
++++++++ super('match', `update_${field}_${toCacheKey(value)}`, name, field, value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { External } from './external';
++++++++
++++++++export class Value extends External {
++++++++ constructor(name: string) {
++++++++ super('value', name);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { ICodeImplementation } from '../implementation/code';
++++++++import { IImplementation } from '../implementation/full';
++++++++import { INodeImplementation } from '../implementation/node';
++++++++import { ITransformImplementation } from '../implementation/transform';
++++++++import { IWrap } from '../wrap';
++++++++import { ContainerWrap } from './wrap';
++++++++
++++++++export { ContainerWrap };
++++++++
++++++++export class Container {
++++++++ private readonly map: Map<string, IImplementation> = new Map();
++++++++
++++++++ public add(key: string, impl: IImplementation): void {
++++++++ assert(!this.map.has(key), `Duplicate implementation key: "${key}"`);
++++++++ this.map.set(key, impl);
++++++++ }
++++++++
++++++++ public build(): IImplementation {
++++++++ return {
++++++++ code: this.buildCode(),
++++++++ node: this.buildNode(),
++++++++ transform: this.buildTransform(),
++++++++ };
++++++++ }
++++++++
++++++++ public buildCode(): ICodeImplementation {
++++++++ return {
++++++++ And: this.combine((impl) => impl.code.And),
++++++++ IsEqual: this.combine((impl) => impl.code.IsEqual),
++++++++ Load: this.combine((impl) => impl.code.Load),
++++++++ Match: this.combine((impl) => impl.code.Match),
++++++++ MulAdd: this.combine((impl) => impl.code.MulAdd),
++++++++ Or: this.combine((impl) => impl.code.Or),
++++++++ Span: this.combine((impl) => impl.code.Span),
++++++++ Store: this.combine((impl) => impl.code.Store),
++++++++ Test: this.combine((impl) => impl.code.Test),
++++++++ Update: this.combine((impl) => impl.code.Update),
++++++++ Value: this.combine((impl) => impl.code.Value),
++++++++ };
++++++++ }
++++++++
++++++++ public buildNode(): INodeImplementation {
++++++++ return {
++++++++ Consume: this.combine((impl) => impl.node.Consume),
++++++++ Empty: this.combine((impl) => impl.node.Empty),
++++++++ Error: this.combine((impl) => impl.node.Error),
++++++++ Invoke: this.combine((impl) => impl.node.Invoke),
++++++++ Pause: this.combine((impl) => impl.node.Pause),
++++++++ Sequence: this.combine((impl) => impl.node.Sequence),
++++++++ Single: this.combine((impl) => impl.node.Single),
++++++++ SpanEnd: this.combine((impl) => impl.node.SpanEnd),
++++++++ SpanStart: this.combine((impl) => impl.node.SpanStart),
++++++++ TableLookup: this.combine((impl) => impl.node.TableLookup),
++++++++ };
++++++++ }
++++++++
++++++++ public buildTransform(): ITransformImplementation {
++++++++ return {
++++++++ ID: this.combine((impl) => impl.transform.ID),
++++++++ ToLower: this.combine((impl) => impl.transform.ToLower),
++++++++ ToLowerUnsafe: this.combine((impl) => impl.transform.ToLowerUnsafe),
++++++++ };
++++++++ }
++++++++
++++++++ private combine<T>(gather: (impl: IImplementation) => new(n: T) => IWrap<T>)
++++++++ : new(n: T) => ContainerWrap<T> {
++++++++ const wraps: Map<string, new(n: T) => IWrap<T>> = new Map();
++++++++ for (const [ key, impl ] of this.map) {
++++++++ wraps.set(key, gather(impl));
++++++++ }
++++++++
++++++++ return class ContainerWrapSingle extends ContainerWrap<T> {
++++++++ constructor(ref: T) {
++++++++ super(ref);
++++++++
++++++++ for (const [ key, impl ] of wraps) {
++++++++ this.map.set(key, new impl(ref));
++++++++ }
++++++++ }
++++++++ };
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { IWrap } from '../wrap';
++++++++
++++++++export class ContainerWrap<T> {
++++++++ protected readonly map: Map<string, IWrap<T>> = new Map();
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public get<R extends IWrap<T>>(key: string): R {
++++++++ assert(this.map.has(key), `Unknown implementation key "${key}"`);
++++++++ return this.map.get(key)! as R;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Node } from './node';
++++++++import { IWrap } from './wrap';
++++++++
++++++++export class Enumerator {
++++++++ public getAllNodes(root: IWrap<Node>): ReadonlyArray<IWrap<Node>> {
++++++++ const nodes: Set<IWrap<Node>> = new Set();
++++++++ const queue = [ root ];
++++++++
++++++++ while (queue.length !== 0) {
++++++++ const node = queue.pop()!;
++++++++ for (const slot of node.ref.getSlots()) {
++++++++ if (nodes.has(slot.node)) {
++++++++ continue;
++++++++ }
++++++++
++++++++ nodes.add(slot.node);
++++++++ queue.push(slot.node);
++++++++ }
++++++++ }
++++++++
++++++++ return Array.from(nodes);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as debugAPI from 'debug';
++++++++import * as source from 'llparse-builder';
++++++++
++++++++import * as frontend from './namespace/frontend';
++++++++import { Container, ContainerWrap } from './container';
++++++++import { IImplementation } from './implementation';
++++++++import { SpanField } from './span-field';
++++++++import { Trie, TrieEmpty, TrieNode, TrieSequence, TrieSingle } from './trie';
++++++++import { Identifier, IUniqueName } from './utils';
++++++++import { IWrap } from './wrap';
++++++++import { Enumerator } from './enumerator';
++++++++import { Peephole } from './peephole';
++++++++
++++++++const debug = debugAPI('llparse:translator');
++++++++
++++++++export { code, node, transform } from './namespace/frontend';
++++++++
++++++++export {
++++++++ source,
++++++++ Identifier,
++++++++ IUniqueName,
++++++++ IWrap,
++++++++ SpanField,
++++++++ Container,
++++++++ ContainerWrap,
++++++++};
++++++++
++++++++// Minimum number of cases of `single` node to make it eligable for
++++++++// `TableLookup` optimization
++++++++export const DEFAULT_MIN_TABLE_SIZE = 32;
++++++++
++++++++// Maximum width of entry in a table for a `TableLookup` optimization
++++++++export const DEFAULT_MAX_TABLE_WIDTH = 4;
++++++++
++++++++type WrappedNode = IWrap<frontend.node.Node>;
++++++++type WrappedCode = IWrap<frontend.code.Code>;
++++++++
++++++++export interface IFrontendLazyOptions {
++++++++ readonly maxTableElemWidth?: number;
++++++++ readonly minTableSize?: number;
++++++++}
++++++++
++++++++export interface IFrontendResult {
++++++++ readonly prefix: string;
++++++++ readonly properties: ReadonlyArray<source.Property>;
++++++++ readonly root: IWrap<frontend.node.Node>;
++++++++ readonly spans: ReadonlyArray<SpanField>;
++++++++ readonly resumptionTargets: ReadonlySet<WrappedNode>;
++++++++}
++++++++
++++++++interface IFrontendOptions {
++++++++ readonly maxTableElemWidth: number;
++++++++ readonly minTableSize: number;
++++++++}
++++++++
++++++++type MatchChildren = WrappedNode[];
++++++++type MatchResult = WrappedNode | ReadonlyArray<WrappedNode>;
++++++++
++++++++interface ITableLookupTarget {
++++++++ readonly keys: number[];
++++++++ readonly noAdvance: boolean;
++++++++ readonly trie: TrieEmpty;
++++++++}
++++++++
++++++++export class Frontend {
++++++++ private readonly options: IFrontendOptions;
++++++++
++++++++ private readonly id: Identifier = new Identifier(this.prefix + '__n_');
++++++++ private readonly codeId: Identifier = new Identifier(this.prefix + '__c_');
++++++++ private readonly map: Map<source.node.Node, WrappedNode> = new Map();
++++++++ private readonly spanMap: Map<source.Span, SpanField> = new Map();
++++++++ private readonly codeCache: Map<string, WrappedCode> = new Map();
++++++++ private readonly resumptionTargets: Set<WrappedNode> = new Set();
++++++++
++++++++ constructor(private readonly prefix: string,
++++++++ private readonly implementation: IImplementation,
++++++++ options: IFrontendLazyOptions = {}) {
++++++++ this.options = {
++++++++ maxTableElemWidth: options.maxTableElemWidth === undefined ?
++++++++ DEFAULT_MAX_TABLE_WIDTH : options.maxTableElemWidth,
++++++++ minTableSize: options.minTableSize === undefined ?
++++++++ DEFAULT_MIN_TABLE_SIZE : options.minTableSize,
++++++++ };
++++++++
++++++++ assert(0 < this.options.maxTableElemWidth,
++++++++ 'Invalid `options.maxTableElemWidth`, must be positive');
++++++++ }
++++++++
++++++++ public compile(root: source.node.Node,
++++++++ properties: ReadonlyArray<source.Property>): IFrontendResult {
++++++++ debug('checking loops');
++++++++ const lc = new source.LoopChecker();
++++++++ lc.check(root);
++++++++
++++++++ debug('allocating spans');
++++++++ const spanAllocator = new source.SpanAllocator();
++++++++ const sourceSpans = spanAllocator.allocate(root);
++++++++
++++++++ const spans = sourceSpans.concurrency.map((concurrent, index) => {
++++++++ const span = new SpanField(index, concurrent.map((sourceSpan) => {
++++++++ return this.translateSpanCode(sourceSpan.callback);
++++++++ }));
++++++++
++++++++ for (const sourceSpan of concurrent) {
++++++++ this.spanMap.set(sourceSpan, span);
++++++++ }
++++++++
++++++++ return span;
++++++++ });
++++++++
++++++++ debug('translating');
++++++++ let out = this.translate(root);
++++++++
++++++++ debug('enumerating');
++++++++ const enumerator = new Enumerator();
++++++++ let nodes = enumerator.getAllNodes(out);
++++++++
++++++++ debug('peephole optimization');
++++++++ const peephole = new Peephole();
++++++++ out = peephole.optimize(out, nodes);
++++++++
++++++++ debug('re-enumerating');
++++++++ nodes = enumerator.getAllNodes(out);
++++++++
++++++++ debug('registering resumption targets');
++++++++ this.resumptionTargets.add(out);
++++++++ for (const node of nodes) {
++++++++ this.registerNode(node);
++++++++ }
++++++++
++++++++ return {
++++++++ prefix: this.prefix,
++++++++ properties,
++++++++ resumptionTargets: this.resumptionTargets,
++++++++ root: out,
++++++++ spans,
++++++++ };
++++++++ }
++++++++
++++++++ // TODO(indutny): remove this in the next major release
++++++++ public getResumptionTargets(): ReadonlySet<WrappedNode> {
++++++++ return this.resumptionTargets;
++++++++ }
++++++++
++++++++ private translate(node: source.node.Node): WrappedNode {
++++++++ if (this.map.has(node)) {
++++++++ return this.map.get(node)!;
++++++++ }
++++++++
++++++++ const id = () => this.id.id(node.name);
++++++++
++++++++ const nodeImpl = this.implementation.node;
++++++++
++++++++ // Instantiate target class
++++++++ let result: MatchResult;
++++++++ if (node instanceof source.node.Error) {
++++++++ result = new nodeImpl.Error(
++++++++ new frontend.node.Error(id(), node.code, node.reason));
++++++++ } else if (node instanceof source.node.Pause) {
++++++++ result = new nodeImpl.Pause(
++++++++ new frontend.node.Pause(id(), node.code, node.reason));
++++++++ } else if (node instanceof source.node.Consume) {
++++++++ result = new nodeImpl.Consume(
++++++++ new frontend.node.Consume(id(), node.field));
++++++++ } else if (node instanceof source.node.SpanStart) {
++++++++ result = new nodeImpl.SpanStart(
++++++++ new frontend.node.SpanStart(id(), this.spanMap.get(node.span)!,
++++++++ this.translateSpanCode(node.span.callback)));
++++++++ } else if (node instanceof source.node.SpanEnd) {
++++++++ result = new nodeImpl.SpanEnd(
++++++++ new frontend.node.SpanEnd(id(), this.spanMap.get(node.span)!,
++++++++ this.translateSpanCode(node.span.callback)));
++++++++ } else if (node instanceof source.node.Invoke) {
++++++++ assert(node.code.signature === 'match' || node.code.signature === 'value',
++++++++ 'Passing `span` callback to `invoke` is not allowed');
++++++++ result = new nodeImpl.Invoke(
++++++++ new frontend.node.Invoke(id(), this.translateCode(node.code)));
++++++++ } else if (node instanceof source.node.Match) {
++++++++ result = this.translateMatch(node);
++++++++ } else {
++++++++ throw new Error(`Unknown node type for "${node.name}" ${node.constructor.toString()}`);
++++++++ }
++++++++
++++++++ // Initialize result
++++++++ const otherwise = node.getOtherwiseEdge();
++++++++
++++++++ if (Array.isArray(result)) {
++++++++ assert(node instanceof source.node.Match);
++++++++ const match = node as source.node.Match;
++++++++
++++++++ // TODO(indutny): move this to llparse-builder?
++++++++ assert.notStrictEqual(otherwise, undefined,
++++++++ `Node "${node.name}" has no \`.otherwise()\``);
++++++++
++++++++ // Assign otherwise to every node of Trie
++++++++ if (otherwise !== undefined) {
++++++++ for (const child of result) {
++++++++ if (!child.ref.otherwise) {
++++++++ child.ref.setOtherwise(this.translate(otherwise.node),
++++++++ otherwise.noAdvance);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ // Assign transform to every node of Trie
++++++++ const transform = this.translateTransform(match.getTransform());
++++++++ for (const child of result) {
++++++++ child.ref.setTransform(transform);
++++++++ }
++++++++
++++++++ assert(result.length >= 1);
++++++++ return result[0];
++++++++ } else {
++++++++ const single: WrappedNode = result as WrappedNode;
++++++++ assert(single.ref instanceof frontend.node.Node);
++++++++
++++++++ // Break loops
++++++++ this.map.set(node, single);
++++++++
++++++++ if (otherwise !== undefined) {
++++++++ single.ref.setOtherwise(this.translate(otherwise.node),
++++++++ otherwise.noAdvance);
++++++++ } else {
++++++++ // TODO(indutny): move this to llparse-builder?
++++++++ assert(node instanceof source.node.Error,
++++++++ `Node "${node.name}" has no \`.otherwise()\``);
++++++++ }
++++++++
++++++++ if (single.ref instanceof frontend.node.Invoke) {
++++++++ for (const edge of node) {
++++++++ single.ref.addEdge(edge.key as number, this.translate(edge.node));
++++++++ }
++++++++ } else {
++++++++ assert.strictEqual(Array.from(node).length, 0);
++++++++ }
++++++++
++++++++ return single;
++++++++ }
++++++++ }
++++++++
++++++++ private registerNode(node: any): void {
++++++++ const nodeImpl = this.implementation.node;
++++++++
++++++++ // Nodes with prologue check (start_pos != end_pos)
++++++++ if (node instanceof nodeImpl.Consume ||
++++++++ node instanceof nodeImpl.Empty ||
++++++++ node instanceof nodeImpl.Sequence ||
++++++++ node instanceof nodeImpl.Single ||
++++++++ node instanceof nodeImpl.SpanStart ||
++++++++ node instanceof nodeImpl.TableLookup) {
++++++++ this.resumptionTargets.add(node);
++++++++
++++++++ // Nodes that can interrupt the execution to be resumed at different node
++++++++ } else if (node instanceof nodeImpl.Pause ||
++++++++ node instanceof nodeImpl.SpanEnd) {
++++++++ this.resumptionTargets.add(node.ref.otherwise!.node);
++++++++ }
++++++++ }
++++++++
++++++++ private translateMatch(node: source.node.Match): MatchResult {
++++++++ const trie = new Trie(node.name);
++++++++
++++++++ const otherwise = node.getOtherwiseEdge();
++++++++ const trieNode = trie.build(Array.from(node));
++++++++ if (trieNode === undefined) {
++++++++ return new this.implementation.node.Empty(
++++++++ new frontend.node.Empty(this.id.id(node.name)));
++++++++ }
++++++++
++++++++ const children: MatchChildren = [];
++++++++ this.translateTrie(node, trieNode, children);
++++++++ assert(children.length >= 1);
++++++++
++++++++ return children;
++++++++ }
++++++++
++++++++ private translateTrie(node: source.node.Match, trie: TrieNode,
++++++++ children: MatchChildren): WrappedNode {
++++++++ if (trie instanceof TrieEmpty) {
++++++++ assert(this.map.has(node));
++++++++ return this.translate(trie.node);
++++++++ } else if (trie instanceof TrieSingle) {
++++++++ return this.translateSingle(node, trie, children);
++++++++ } else if (trie instanceof TrieSequence) {
++++++++ return this.translateSequence(node, trie, children);
++++++++ } else {
++++++++ throw new Error('Unknown trie node');
++++++++ }
++++++++ }
++++++++
++++++++ private translateSingle(node: source.node.Match, trie: TrieSingle,
++++++++ children: MatchChildren)
++++++++ : IWrap<frontend.node.Match> {
++++++++ // See if we can apply TableLookup optimization
++++++++ const maybeTable = this.maybeTableLookup(node, trie, children);
++++++++ if (maybeTable !== undefined) {
++++++++ return maybeTable;
++++++++ }
++++++++
++++++++ const single = new this.implementation.node.Single(
++++++++ new frontend.node.Single(this.id.id(node.name)));
++++++++ children.push(single);
++++++++
++++++++ // Break the loop
++++++++ if (!this.map.has(node)) {
++++++++ this.map.set(node, single);
++++++++ }
++++++++ for (const child of trie.children) {
++++++++ const childNode = this.translateTrie(node, child.node, children);
++++++++
++++++++ single.ref.addEdge({
++++++++ key: child.key,
++++++++ noAdvance: child.noAdvance,
++++++++ node: childNode,
++++++++ value: child.node instanceof TrieEmpty ? child.node.value : undefined,
++++++++ });
++++++++ }
++++++++
++++++++ const otherwise = trie.otherwise;
++++++++ if (otherwise) {
++++++++ single.ref.setOtherwise(
++++++++ this.translateTrie(node, otherwise, children),
++++++++ true,
++++++++ otherwise.value);
++++++++ }
++++++++
++++++++ return single;
++++++++ }
++++++++
++++++++ private maybeTableLookup(node: source.node.Match, trie: TrieSingle,
++++++++ children: MatchChildren)
++++++++ : IWrap<frontend.node.Match> | undefined {
++++++++ if (trie.children.length < this.options.minTableSize) {
++++++++ debug('not enough children of "%s" to allocate table, got %d need %d',
++++++++ node.name, trie.children.length, this.options.minTableSize);
++++++++ return undefined;
++++++++ }
++++++++
++++++++ const targets: Map<source.node.Node, ITableLookupTarget> = new Map();
++++++++
++++++++ const bailout = !trie.children.every((child) => {
++++++++ if (!(child.node instanceof TrieEmpty)) {
++++++++ debug('non-leaf trie child of "%s" prevents table allocation',
++++++++ node.name);
++++++++ return false;
++++++++ }
++++++++
++++++++ const empty: TrieEmpty = child.node;
++++++++
++++++++ // We can't pass values from the table yet
++++++++ if (empty.value !== undefined) {
++++++++ debug('value passing trie leaf of "%s" prevents table allocation',
++++++++ node.name);
++++++++ return false;
++++++++ }
++++++++
++++++++ const target = empty.node;
++++++++ if (!targets.has(target)) {
++++++++ targets.set(target, {
++++++++ keys: [ child.key ],
++++++++ noAdvance: child.noAdvance,
++++++++ trie: empty,
++++++++ });
++++++++ return true;
++++++++ }
++++++++
++++++++ const existing = targets.get(target)!;
++++++++
++++++++ // TODO(indutny): just use it as a sub-key?
++++++++ if (existing.noAdvance !== child.noAdvance) {
++++++++ debug(
++++++++ 'noAdvance mismatch in a trie leaf of "%s" prevents ' +
++++++++ 'table allocation',
++++++++ node.name);
++++++++ return false;
++++++++ }
++++++++
++++++++ existing.keys.push(child.key);
++++++++ return true;
++++++++ });
++++++++
++++++++ if (bailout) {
++++++++ return undefined;
++++++++ }
++++++++
++++++++ // We've width limit for this optimization
++++++++ if (targets.size >= (1 << this.options.maxTableElemWidth)) {
++++++++ debug('too many different trie targets of "%s" for a table allocation',
++++++++ node.name);
++++++++ return undefined;
++++++++ }
++++++++
++++++++ const table = new this.implementation.node.TableLookup(
++++++++ new frontend.node.TableLookup(this.id.id(node.name)));
++++++++ children.push(table);
++++++++
++++++++ // Break the loop
++++++++ if (!this.map.has(node)) {
++++++++ this.map.set(node, table);
++++++++ }
++++++++
++++++++ targets.forEach((target) => {
++++++++ const next = this.translateTrie(node, target.trie, children);
++++++++
++++++++ table.ref.addEdge({
++++++++ keys: target.keys,
++++++++ noAdvance: target.noAdvance,
++++++++ node: next,
++++++++ });
++++++++ });
++++++++
++++++++ debug('optimized "%s" to a table lookup node', node.name);
++++++++ return table;
++++++++ }
++++++++
++++++++ private translateSequence(node: source.node.Match, trie: TrieSequence,
++++++++ children: MatchChildren)
++++++++ : IWrap<frontend.node.Match> {
++++++++ const sequence = new this.implementation.node.Sequence(
++++++++ new frontend.node.Sequence(this.id.id(node.name), trie.select));
++++++++ children.push(sequence);
++++++++
++++++++ // Break the loop
++++++++ if (!this.map.has(node)) {
++++++++ this.map.set(node, sequence);
++++++++ }
++++++++
++++++++ const childNode = this.translateTrie(node, trie.child, children);
++++++++
++++++++ const value = trie.child instanceof TrieEmpty ?
++++++++ trie.child.value : undefined;
++++++++
++++++++ sequence.ref.setEdge(childNode, value);
++++++++
++++++++ return sequence;
++++++++ }
++++++++
++++++++ private translateCode(code: source.code.Code): WrappedCode {
++++++++ const prefixed = this.codeId.id(code.name).name;
++++++++ const codeImpl = this.implementation.code;
++++++++
++++++++ let res: WrappedCode;
++++++++ if (code instanceof source.code.IsEqual) {
++++++++ res = new codeImpl.IsEqual(
++++++++ new frontend.code.IsEqual(prefixed, code.field, code.value));
++++++++ } else if (code instanceof source.code.Load) {
++++++++ res = new codeImpl.Load(
++++++++ new frontend.code.Load(prefixed, code.field));
++++++++ } else if (code instanceof source.code.MulAdd) {
++++++++ // TODO(indutny): verify property type
++++++++ const m = new frontend.code.MulAdd(prefixed, code.field, {
++++++++ base: code.options.base,
++++++++ max: code.options.max,
++++++++ signed: code.options.signed === undefined ? true : code.options.signed,
++++++++ });
++++++++ res = new codeImpl.MulAdd(m);
++++++++ } else if (code instanceof source.code.And) {
++++++++ res = new codeImpl.And(
++++++++ new frontend.code.Or(prefixed, code.field, code.value));
++++++++ } else if (code instanceof source.code.Or) {
++++++++ res = new codeImpl.Or(
++++++++ new frontend.code.Or(prefixed, code.field, code.value));
++++++++ } else if (code instanceof source.code.Store) {
++++++++ res = new codeImpl.Store(
++++++++ new frontend.code.Store(prefixed, code.field));
++++++++ } else if (code instanceof source.code.Test) {
++++++++ res = new codeImpl.Test(
++++++++ new frontend.code.Test(prefixed, code.field, code.value));
++++++++ } else if (code instanceof source.code.Update) {
++++++++ res = new codeImpl.Update(
++++++++ new frontend.code.Update(prefixed, code.field, code.value));
++++++++
++++++++ // External callbacks
++++++++ } else if (code instanceof source.code.Span) {
++++++++ res = new codeImpl.Span(new frontend.code.Span(code.name));
++++++++ } else if (code instanceof source.code.Match) {
++++++++ res = new codeImpl.Match(new frontend.code.Match(code.name));
++++++++ } else if (code instanceof source.code.Value) {
++++++++ res = new codeImpl.Value(new frontend.code.Value(code.name));
++++++++ } else {
++++++++ throw new Error(`Unsupported code: "${code.name}"`);
++++++++ }
++++++++
++++++++ // Re-use instances to build them just once
++++++++ if (this.codeCache.has(res.ref.cacheKey)) {
++++++++ return this.codeCache.get(res.ref.cacheKey)!;
++++++++ }
++++++++
++++++++ this.codeCache.set(res.ref.cacheKey, res);
++++++++ return res;
++++++++ }
++++++++
++++++++ private translateSpanCode(code: source.code.Span): IWrap<frontend.code.Span> {
++++++++ return this.translateCode(code) as IWrap<frontend.code.Span>;
++++++++ }
++++++++
++++++++ private translateTransform(transform?: source.transform.Transform)
++++++++ : IWrap<frontend.transform.Transform> {
++++++++ const transformImpl = this.implementation.transform;
++++++++ if (transform === undefined) {
++++++++ return new transformImpl.ID(new frontend.transform.ID());
++++++++ } else if (transform.name === 'to_lower') {
++++++++ return new transformImpl.ToLower(
++++++++ new frontend.transform.ToLower());
++++++++ } else if (transform.name === 'to_lower_unsafe') {
++++++++ return new transformImpl.ToLowerUnsafe(
++++++++ new frontend.transform.ToLowerUnsafe());
++++++++ } else {
++++++++ throw new Error(`Unsupported transform: "${transform.name}"`);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as code from '../code';
++++++++import { IWrap } from '../wrap';
++++++++
++++++++export interface ICodeImplementation {
++++++++ readonly And: new(c: code.And) => IWrap<code.And>;
++++++++ readonly IsEqual: new(c: code.IsEqual) => IWrap<code.IsEqual>;
++++++++ readonly Load: new(c: code.Load) => IWrap<code.Load>;
++++++++ readonly Match: new(c: code.Match) => IWrap<code.Match>;
++++++++ readonly MulAdd: new(c: code.MulAdd) => IWrap<code.MulAdd>;
++++++++ readonly Or: new(c: code.Or) => IWrap<code.Or>;
++++++++ readonly Span: new(c: code.Span) => IWrap<code.Span>;
++++++++ readonly Store: new(c: code.Store) => IWrap<code.Store>;
++++++++ readonly Test: new(c: code.Test) => IWrap<code.Test>;
++++++++ readonly Update: new(c: code.Update) => IWrap<code.Update>;
++++++++ readonly Value: new(c: code.Value) => IWrap<code.Value>;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ICodeImplementation } from './code';
++++++++import { INodeImplementation } from './node';
++++++++import { ITransformImplementation } from './transform';
++++++++
++++++++export interface IImplementation {
++++++++ readonly code: ICodeImplementation;
++++++++ readonly node: INodeImplementation;
++++++++ readonly transform: ITransformImplementation;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export * from './code';
++++++++export * from './full';
++++++++export * from './node';
++++++++export * from './transform';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as node from '../node';
++++++++import { IWrap } from '../wrap';
++++++++
++++++++export interface INodeImplementation {
++++++++ readonly Consume: new(n: node.Consume) => IWrap<node.Consume>;
++++++++ readonly Empty: new(n: node.Empty) => IWrap<node.Empty>;
++++++++ readonly Error: new(n: node.Error) => IWrap<node.Error>;
++++++++ readonly Invoke: new(n: node.Invoke) => IWrap<node.Invoke>;
++++++++ readonly Pause: new(n: node.Pause) => IWrap<node.Pause>;
++++++++ readonly Sequence: new(n: node.Sequence) => IWrap<node.Sequence>;
++++++++ readonly Single: new(n: node.Single) => IWrap<node.Single>;
++++++++ readonly SpanEnd: new(n: node.SpanEnd) => IWrap<node.SpanEnd>;
++++++++ readonly SpanStart: new(n: node.SpanStart) => IWrap<node.SpanStart>;
++++++++ readonly TableLookup: new(n: node.TableLookup) => IWrap<node.TableLookup>;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as transform from '../transform';
++++++++import { IWrap } from '../wrap';
++++++++
++++++++export interface ITransformImplementation {
++++++++ readonly ID: new(t: transform.ID) => IWrap<transform.ID>;
++++++++ readonly ToLower: new(t: transform.ToLower) => IWrap<transform.ToLower>;
++++++++ readonly ToLowerUnsafe: new(t: transform.ToLowerUnsafe)
++++++++ => IWrap<transform.ToLowerUnsafe>;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as code from '../code';
++++++++import * as node from '../node';
++++++++import * as transform from '../transform';
++++++++
++++++++export { code, node, transform };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Slot } from './slot';
++++++++
++++++++export interface IReadonlyOtherwiseEdge {
++++++++ readonly node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++interface IOtherwiseEdge {
++++++++ node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++export abstract class Node {
++++++++ private privOtherwise: IOtherwiseEdge | undefined;
++++++++ private privSlots: ReadonlyArray<Slot> | undefined;
++++++++
++++++++ constructor(public readonly id: IUniqueName) {
++++++++ }
++++++++
++++++++ public setOtherwise(node: IWrap<Node>, noAdvance: boolean, value?: number) {
++++++++ this.privOtherwise = { node, noAdvance, value };
++++++++ }
++++++++
++++++++ public get otherwise(): IReadonlyOtherwiseEdge | undefined {
++++++++ return this.privOtherwise;
++++++++ }
++++++++
++++++++ public *getSlots() {
++++++++ if (this.privSlots === undefined) {
++++++++ this.privSlots = Array.from(this.buildSlots());
++++++++ }
++++++++
++++++++ yield* this.privSlots;
++++++++ }
++++++++
++++++++ protected *buildSlots() {
++++++++ const otherwise = this.privOtherwise;
++++++++ if (otherwise !== undefined) {
++++++++ yield new Slot(otherwise.node, (value) => otherwise.node = value);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { IUniqueName } from '../utils';
++++++++import { Node } from './base';
++++++++
++++++++export class Consume extends Node {
++++++++ constructor(id: IUniqueName, readonly field: string) {
++++++++ super(id);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Node } from './base';
++++++++
++++++++export class Empty extends Node {
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { IUniqueName } from '../utils';
++++++++import { Node } from './base';
++++++++
++++++++export class Error extends Node {
++++++++ constructor(id: IUniqueName, public readonly code: number,
++++++++ public readonly reason: string) {
++++++++ super(id);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export * from './base';
++++++++export * from './consume';
++++++++export * from './empty';
++++++++export * from './error';
++++++++export * from './invoke';
++++++++export * from './match';
++++++++export * from './pause';
++++++++export * from './sequence';
++++++++export * from './single';
++++++++export * from './slot';
++++++++export * from './span-end';
++++++++export * from './span-start';
++++++++export * from './table-lookup';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Code } from '../code';
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++import { Slot } from './slot';
++++++++
++++++++interface IInvokeEdge {
++++++++ readonly code: number;
++++++++ node: IWrap<Node>;
++++++++}
++++++++
++++++++export interface IReadonlyInvokeEdge {
++++++++ readonly code: number;
++++++++ readonly node: IWrap<Node>;
++++++++}
++++++++
++++++++export class Invoke extends Node {
++++++++ private readonly privEdges: IInvokeEdge[] = [];
++++++++
++++++++ constructor(id: IUniqueName, public readonly code: IWrap<Code>) {
++++++++ super(id);
++++++++ }
++++++++
++++++++ public addEdge(code: number, node: IWrap<Node>): void {
++++++++ this.privEdges.push({ code, node });
++++++++ }
++++++++
++++++++ public get edges(): ReadonlyArray<IReadonlyInvokeEdge> {
++++++++ return this.privEdges;
++++++++ }
++++++++
++++++++ protected *buildSlots() {
++++++++ for (const edge of this.privEdges) {
++++++++ yield new Slot(edge.node, (value) => edge.node = value);
++++++++ }
++++++++
++++++++ yield* super.buildSlots();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from '../transform';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++
++++++++export class Match extends Node {
++++++++ public transform?: IWrap<Transform>;
++++++++
++++++++ public setTransform(transform: IWrap<Transform>): void {
++++++++ this.transform = transform;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Error as ErrorNode } from './error';
++++++++
++++++++export class Pause extends ErrorNode {
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++import { Match } from './match';
++++++++import { Slot } from './slot';
++++++++
++++++++interface ISequenceEdge {
++++++++ node: IWrap<Node>;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++export interface IReadonlySequenceEdge {
++++++++ readonly node: IWrap<Node>;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++export class Sequence extends Match {
++++++++ private privEdge?: ISequenceEdge;
++++++++
++++++++ constructor(id: IUniqueName, public readonly select: Buffer) {
++++++++ super(id);
++++++++ }
++++++++
++++++++ public setEdge(node: IWrap<Node>, value?: number | undefined) {
++++++++ assert.strictEqual(this.privEdge, undefined);
++++++++ this.privEdge = { node, value };
++++++++ }
++++++++
++++++++ public get edge(): IReadonlySequenceEdge | undefined {
++++++++ return this.privEdge;
++++++++ }
++++++++
++++++++ protected *buildSlots() {
++++++++ const edge = this.privEdge;
++++++++ if (edge !== undefined) {
++++++++ yield new Slot(edge.node, (value) => edge.node = value);
++++++++ }
++++++++
++++++++ yield* super.buildSlots();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++import { Match } from './match';
++++++++import { Slot } from './slot';
++++++++
++++++++interface ISingleEdge {
++++++++ readonly key: number;
++++++++ node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++export interface IReadonlySingleEdge {
++++++++ readonly key: number;
++++++++ node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++export class Single extends Match {
++++++++ private readonly privEdges: ISingleEdge[] = [];
++++++++
++++++++ public addEdge(edge: IReadonlySingleEdge): void {
++++++++ this.privEdges.push({
++++++++ key: edge.key,
++++++++ noAdvance: edge.noAdvance,
++++++++ node: edge.node,
++++++++ value: edge.value,
++++++++ });
++++++++ }
++++++++
++++++++ public get edges(): ReadonlyArray<IReadonlySingleEdge> {
++++++++ return this.privEdges;
++++++++ }
++++++++
++++++++ protected *buildSlots() {
++++++++ for (const edge of this.privEdges) {
++++++++ yield new Slot(edge.node, (value) => edge.node = value);
++++++++ }
++++++++
++++++++ yield* super.buildSlots();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++
++++++++export class Slot {
++++++++ private privNode: IWrap<Node>;
++++++++
++++++++ constructor(node: IWrap<Node>,
++++++++ private readonly privUpdate: (value: IWrap<Node>) => void) {
++++++++ this.privNode = node;
++++++++ }
++++++++
++++++++ public get node(): IWrap<Node> {
++++++++ return this.privNode;
++++++++ }
++++++++
++++++++ public set node(value: IWrap<Node>) {
++++++++ this.privNode = value;
++++++++ this.privUpdate(value);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Span } from '../code';
++++++++import { SpanField } from '../span-field';
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanEnd extends Node {
++++++++ constructor(id: IUniqueName, public readonly field: SpanField,
++++++++ public readonly callback: IWrap<Span>) {
++++++++ super(id);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Span } from '../code';
++++++++import { SpanField } from '../span-field';
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanStart extends Node {
++++++++ constructor(id: IUniqueName, public readonly field: SpanField,
++++++++ public readonly callback: IWrap<Span>) {
++++++++ super(id);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { IUniqueName } from '../utils';
++++++++import { IWrap } from '../wrap';
++++++++import { Node } from './base';
++++++++import { Match } from './match';
++++++++import { Slot } from './slot';
++++++++
++++++++interface ITableEdge {
++++++++ readonly keys: ReadonlyArray<number>;
++++++++ node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++}
++++++++
++++++++export interface IReadonlyTableEdge {
++++++++ readonly keys: ReadonlyArray<number>;
++++++++ readonly node: IWrap<Node>;
++++++++ readonly noAdvance: boolean;
++++++++}
++++++++
++++++++export class TableLookup extends Match {
++++++++ private readonly privEdges: ITableEdge[] = [];
++++++++
++++++++ public addEdge(edge: IReadonlyTableEdge): void {
++++++++ this.privEdges.push({
++++++++ keys: edge.keys,
++++++++ noAdvance: edge.noAdvance,
++++++++ node: edge.node,
++++++++ });
++++++++ }
++++++++
++++++++ public get edges(): ReadonlyArray<IReadonlyTableEdge> {
++++++++ return this.privEdges;
++++++++ }
++++++++
++++++++ protected *buildSlots() {
++++++++ for (const edge of this.privEdges) {
++++++++ yield new Slot(edge.node, (value) => edge.node = value);
++++++++ }
++++++++
++++++++ yield* super.buildSlots();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Node, Empty } from './node';
++++++++import { IWrap } from './wrap';
++++++++
++++++++type WrapNode = IWrap<Node>;
++++++++type WrapList = ReadonlyArray<WrapNode>;
++++++++
++++++++export class Peephole {
++++++++ public optimize(root: WrapNode, nodes: WrapList): WrapNode {
++++++++ let changed = new Set(nodes);
++++++++
++++++++ while (changed.size !== 0) {
++++++++ const previous = changed;
++++++++ changed = new Set();
++++++++
++++++++ for (const node of previous) {
++++++++ if (this.optimizeNode(node)) {
++++++++ changed.add(node);
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ while (root.ref instanceof Empty) {
++++++++ if (!root.ref.otherwise!.noAdvance) {
++++++++ break;
++++++++ }
++++++++
++++++++ root = root.ref.otherwise!.node;
++++++++ }
++++++++
++++++++ return root;
++++++++ }
++++++++
++++++++ public optimizeNode(node: WrapNode): boolean {
++++++++ let changed = false;
++++++++ for (const slot of node.ref.getSlots()) {
++++++++ if (!(slot.node.ref instanceof Empty)) {
++++++++ continue;
++++++++ }
++++++++
++++++++ const otherwise = slot.node.ref.otherwise!;
++++++++
++++++++ // Node actively skips, can't optimize!
++++++++ if (!otherwise.noAdvance) {
++++++++ continue;
++++++++ }
++++++++
++++++++ slot.node = otherwise.node;
++++++++ changed = true;
++++++++ }
++++++++ return changed;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Span } from './code';
++++++++import { IWrap } from './wrap';
++++++++
++++++++export class SpanField {
++++++++ constructor(public readonly index: number,
++++++++ public readonly callbacks: ReadonlyArray<IWrap<Span>>) {
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class Transform {
++++++++ constructor(public readonly name: string) {
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++
++++++++export class ID extends Transform {
++++++++ constructor() {
++++++++ super('id');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export * from './base';
++++++++export * from './id';
++++++++export * from './to-lower';
++++++++export * from './to-lower-unsafe';
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLowerUnsafe extends Transform {
++++++++ constructor() {
++++++++ super('to_lower_unsafe');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLower extends Transform {
++++++++ constructor() {
++++++++ super('to_lower');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node as api } from 'llparse-builder';
++++++++import { TrieNode } from './node';
++++++++
++++++++export class TrieEmpty extends TrieNode {
++++++++ constructor(public readonly node: api.Node,
++++++++ public readonly value: number | undefined) {
++++++++ super();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++import { Edge, node as api } from 'llparse-builder';
++++++++
++++++++import { TrieEmpty } from './empty';
++++++++import { TrieNode } from './node';
++++++++import { TrieSequence } from './sequence';
++++++++import { ITrieSingleChild, TrieSingle } from './single';
++++++++
++++++++export { TrieEmpty, TrieNode, TrieSequence, TrieSingle };
++++++++
++++++++interface IEdge {
++++++++ readonly key: Buffer;
++++++++ readonly node: api.Node;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value: number | undefined;
++++++++}
++++++++
++++++++type Path = ReadonlyArray<Buffer>;
++++++++type EdgeArray = ReadonlyArray<IEdge>;
++++++++
++++++++export class Trie {
++++++++ constructor(private readonly name: string) {
++++++++ }
++++++++
++++++++ public build(edges: ReadonlyArray<Edge>): undefined | TrieNode {
++++++++ if (edges.length === 0) {
++++++++ return undefined;
++++++++ }
++++++++
++++++++ const internalEdges: IEdge[] = [];
++++++++ for (const edge of edges) {
++++++++ internalEdges.push({
++++++++ key: edge.key as Buffer,
++++++++ noAdvance: edge.noAdvance,
++++++++ node: edge.node,
++++++++ value: edge.value,
++++++++ });
++++++++ }
++++++++
++++++++ return this.level(internalEdges, []);
++++++++ }
++++++++
++++++++ private level(edges: EdgeArray, path: Path): TrieNode {
++++++++ const first = edges[0].key;
++++++++ const last = edges[edges.length - 1].key;
++++++++
++++++++ // Leaf
++++++++ if (edges.length === 1 && edges[0].key.length === 0) {
++++++++ return new TrieEmpty(edges[0].node, edges[0].value);
++++++++ }
++++++++
++++++++ // Find the longest common sub-string
++++++++ let common = 0;
++++++++ for (; common < first.length; common++) {
++++++++ if (first[common] !== last[common]) {
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ // Sequence
++++++++ if (common > 1) {
++++++++ return this.sequence(edges, first.slice(0, common), path);
++++++++ }
++++++++
++++++++ // Single
++++++++ return this.single(edges, path);
++++++++ }
++++++++
++++++++ private slice(edges: EdgeArray, off: number): EdgeArray {
++++++++ return edges.map((edge) => {
++++++++ return {
++++++++ key: edge.key.slice(off),
++++++++ noAdvance: edge.noAdvance,
++++++++ node: edge.node,
++++++++ value: edge.value,
++++++++ };
++++++++ }).sort((a, b) => {
++++++++ return a.key.compare(b.key);
++++++++ });
++++++++ }
++++++++
++++++++ private sequence(edges: EdgeArray, prefix: Buffer, path: Path): TrieNode {
++++++++ const sliced = this.slice(edges, prefix.length);
++++++++ const noAdvance = sliced.some((edge) => edge.noAdvance);
++++++++ assert(!noAdvance);
++++++++ const child = this.level(sliced, path.concat(prefix));
++++++++
++++++++ return new TrieSequence(prefix, child);
++++++++ }
++++++++
++++++++ private single(edges: EdgeArray, path: Path): TrieNode {
++++++++ // Check for duplicates
++++++++ if (edges[0].key.length === 0) {
++++++++ assert(path.length !== 0, `Empty root entry at "${this.name}"`);
++++++++ assert(edges.length === 1 || edges[1].key.length !== 0,
++++++++ `Duplicate entries in "${this.name}" at [ ${path.join(', ')} ]`);
++++++++ }
++++++++
++++++++ let otherwise: TrieEmpty | undefined;
++++++++ const keys: Map<number, IEdge[]> = new Map();
++++++++ for (const edge of edges) {
++++++++ if (edge.key.length === 0) {
++++++++ otherwise = new TrieEmpty(edge.node, edge.value);
++++++++ continue;
++++++++ }
++++++++ const key = edge.key[0];
++++++++
++++++++ if (keys.has(key)) {
++++++++ keys.get(key)!.push(edge);
++++++++ } else {
++++++++ keys.set(key, [ edge ]);
++++++++ }
++++++++ }
++++++++
++++++++ const children: ITrieSingleChild[] = [];
++++++++ keys.forEach((subEdges, key) => {
++++++++ const sliced = this.slice(subEdges, 1);
++++++++ const subpath = path.concat(Buffer.from([ key ]));
++++++++
++++++++ const noAdvance = subEdges.some((edge) => edge.noAdvance);
++++++++ const allSame = subEdges.every((edge) => edge.noAdvance === noAdvance);
++++++++
++++++++ assert(allSame || subEdges.length === 0,
++++++++ 'Conflicting `.peek()` and `.match()` entries in ' +
++++++++ `"${this.name}" at [ ${subpath.join(', ')} ]`);
++++++++
++++++++ children.push({
++++++++ key,
++++++++ noAdvance,
++++++++ node: this.level(sliced, subpath),
++++++++ });
++++++++ });
++++++++ return new TrieSingle(children, otherwise);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class TrieNode {
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node as api } from 'llparse-builder';
++++++++import { TrieNode } from './node';
++++++++
++++++++export class TrieSequence extends TrieNode {
++++++++ constructor(public readonly select: Buffer,
++++++++ public readonly child: TrieNode) {
++++++++ super();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node as api } from 'llparse-builder';
++++++++import { TrieEmpty } from './empty';
++++++++import { TrieNode } from './node';
++++++++
++++++++export interface ITrieSingleChild {
++++++++ readonly key: number;
++++++++ readonly noAdvance: boolean;
++++++++ readonly node: TrieNode;
++++++++}
++++++++
++++++++export class TrieSingle extends TrieNode {
++++++++ constructor(public readonly children: ReadonlyArray<ITrieSingleChild>,
++++++++ public readonly otherwise: TrieEmpty | undefined) {
++++++++ super();
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export interface IUniqueName {
++++++++ readonly name: string;
++++++++ readonly originalName: string;
++++++++}
++++++++
++++++++export class Identifier {
++++++++ private readonly ns: Set<string> = new Set();
++++++++
++++++++ constructor(private readonly prefix: string = '',
++++++++ private readonly postfix: string = '') {
++++++++ }
++++++++
++++++++ public id(name: string): IUniqueName {
++++++++ let target = this.prefix + name + this.postfix;
++++++++ if (this.ns.has(target)) {
++++++++ let i = 1;
++++++++ for (; i < this.ns.size; i++) {
++++++++ if (!this.ns.has(target + '_' + i)) {
++++++++ break;
++++++++ }
++++++++ }
++++++++
++++++++ target += '_' + i;
++++++++ }
++++++++
++++++++ this.ns.add(target);
++++++++ return {
++++++++ name: target,
++++++++ originalName: name,
++++++++ };
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export { Identifier, IUniqueName } from './identifier';
++++++++
++++++++export function toCacheKey(value: number | boolean): string {
++++++++ if (typeof value === 'number') {
++++++++ if (value < 0) {
++++++++ return 'm' + (-value);
++++++++ } else {
++++++++ return value.toString();
++++++++ }
++++++++ } else if (typeof value === 'boolean') {
++++++++ if (value === true) {
++++++++ return 'true';
++++++++ } else {
++++++++ return 'false';
++++++++ }
++++++++ } else {
++++++++ throw new Error(`Unsupported value: "${value}"`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export interface IWrap<T> {
++++++++ readonly ref: T;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { Builder } from 'llparse-builder';
++++++++
++++++++import { Container, ContainerWrap, Frontend, node } from '../src/frontend';
++++++++import implementation from './fixtures/a-implementation';
++++++++import { Node } from './fixtures/implementation/node/base';
++++++++
++++++++describe('llparse-frontend/Container', () => {
++++++++ let b: Builder;
++++++++ beforeEach(() => {
++++++++ b = new Builder();
++++++++ });
++++++++
++++++++ it('should translate nodes to implementation', () => {
++++++++ const comb = new Container();
++++++++ comb.add('a', implementation);
++++++++ comb.add('b', implementation);
++++++++
++++++++ const f = new Frontend('llparse', comb.build());
++++++++
++++++++ const root = b.node('root');
++++++++
++++++++ root.match('ab', root);
++++++++ root.match('acd', root);
++++++++ root.match('efg', root);
++++++++ root.otherwise(b.error(123, 'hello'));
++++++++
++++++++ const fRoot = f.compile(root, []).root as ContainerWrap<node.Node>;
++++++++
++++++++ const out: string[] = [];
++++++++ (fRoot.get('a') as Node<node.Node>).build(out);
++++++++
++++++++ assert.deepStrictEqual(out, [
++++++++ '<Single name=llparse__n_root k97=llparse__n_root_1 ' +
++++++++ 'k101=llparse__n_root_3 otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_1 k98=llparse__n_root ' +
++++++++ 'k99=llparse__n_root_2 otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_2 k100=llparse__n_root ' +
++++++++ 'otherwise-no_adv=llparse__n_error/>',
++++++++ '<ErrorNode name=llparse__n_error code=123 reason="hello"/>',
++++++++ '<Sequence name=llparse__n_root_3 select="6667" ' +
++++++++ 'otherwise-no_adv=llparse__n_error/>',
++++++++ ]);
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class And extends Code<code.And> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class Code<T> {
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(): string;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { And } from './and';
++++++++import { IsEqual } from './is-equal';
++++++++import { Load } from './load';
++++++++import { Match } from './match';
++++++++import { MulAdd } from './mul-add';
++++++++import { Or } from './or';
++++++++import { Span } from './span';
++++++++import { Store } from './store';
++++++++import { Test } from './test';
++++++++import { Update } from './update';
++++++++import { Value } from './value';
++++++++
++++++++export default {
++++++++ And, IsEqual, Load, Match, MulAdd, Or, Span, Store, Test, Update, Value,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class IsEqual extends Code<code.IsEqual> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Load extends Code<code.Load> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Match extends Code<code.Match> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class MulAdd extends Code<code.MulAdd> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Or extends Code<code.Or> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Span extends Code<code.Span> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Store extends Code<code.Store> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Test extends Code<code.Test> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Update extends Code<code.Update> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Value extends Code<code.Value> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import code from './code';
++++++++import node from './node';
++++++++import transform from './transform';
++++++++
++++++++export default { code, node, transform };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ContainerWrap, node } from '../../../../src/frontend';
++++++++
++++++++export abstract class Node<T extends node.Node> {
++++++++ private built: boolean = false;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public build(out: string[]): void {
++++++++ if (this.built) {
++++++++ return;
++++++++ }
++++++++
++++++++ this.built = true;
++++++++ this.doBuild(out);
++++++++
++++++++ if (this.ref.otherwise !== undefined) {
++++++++ const cwrap = this.ref.otherwise.node as ContainerWrap<T>;
++++++++ const otherwise = cwrap.get<Node<T>>('a');
++++++++ otherwise.build(out);
++++++++ }
++++++++ }
++++++++
++++++++ protected format(value: string): string {
++++++++ let otherwise: string = '';
++++++++ if (this.ref.otherwise !== undefined) {
++++++++ const otherwiseRef = this.ref.otherwise.node.ref;
++++++++ otherwise = ' otherwise' +
++++++++ `${this.ref.otherwise.noAdvance ? '-no_adv' : ''}=` +
++++++++ `${otherwiseRef.id.name}`;
++++++++ }
++++++++
++++++++ return `<${this.constructor.name} name=${this.ref.id.name} ` +
++++++++ `${value}${otherwise}/>`;
++++++++ }
++++++++
++++++++ protected abstract doBuild(out: string[]): void;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Consume extends Node<node.Consume> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(`field=${this.ref.field}`));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Empty extends Node<node.Empty> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++class ErrorNode extends Node<node.Error> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(`code=${this.ref.code} reason="${this.ref.reason}"`));
++++++++ }
++++++++}
++++++++
++++++++export { ErrorNode as Error };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Consume } from './consume';
++++++++import { Empty } from './empty';
++++++++import { Error } from './error';
++++++++import { Invoke } from './invoke';
++++++++import { Pause } from './pause';
++++++++import { Sequence } from './sequence';
++++++++import { Single } from './single';
++++++++import { SpanEnd } from './span-end';
++++++++import { SpanStart } from './span-start';
++++++++import { TableLookup } from './table-lookup';
++++++++
++++++++export default {
++++++++ Consume, Empty, Error, Invoke, Pause, Sequence, Single, SpanEnd,
++++++++ SpanStart, TableLookup,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Invoke extends Node<node.Invoke> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Pause extends Node<node.Pause> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Sequence extends Node<node.Sequence> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(`select="${this.ref.select.toString('hex')}"`));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ContainerWrap, node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Single extends Node<node.Single> {
++++++++ protected doBuild(out: string[]): void {
++++++++ const edges: string[] = [];
++++++++ for (const edge of this.ref.edges) {
++++++++ edges.push(`k${edge.key}${edge.noAdvance ? '-no_adv-' : ''}=` +
++++++++ `${edge.node.ref.id.name}`);
++++++++ }
++++++++ out.push(this.format(edges.join(' ')));
++++++++
++++++++ for (const edge of this.ref.edges) {
++++++++ const edgeNode = edge.node as ContainerWrap<node.Node>;
++++++++ edgeNode.get<Node<node.Node>>('a').build(out);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanEnd extends Node<node.SpanEnd> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanStart extends Node<node.SpanStart> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class TableLookup extends Node<node.TableLookup> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class Transform<T> {
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(): string;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ID extends Transform<transform.ID> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ID } from './id';
++++++++import { ToLower } from './to-lower';
++++++++import { ToLowerUnsafe } from './to-lower-unsafe';
++++++++
++++++++export default { ID, ToLower, ToLowerUnsafe };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLowerUnsafe extends Transform<transform.ToLowerUnsafe> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLower extends Transform<transform.ToLower> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class And extends Code<code.And> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class Code<T> {
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(): string;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { And } from './and';
++++++++import { IsEqual } from './is-equal';
++++++++import { Load } from './load';
++++++++import { Match } from './match';
++++++++import { MulAdd } from './mul-add';
++++++++import { Or } from './or';
++++++++import { Span } from './span';
++++++++import { Store } from './store';
++++++++import { Test } from './test';
++++++++import { Update } from './update';
++++++++import { Value } from './value';
++++++++
++++++++export default {
++++++++ And, IsEqual, Load, Match, MulAdd, Or, Span, Store, Test, Update, Value,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class IsEqual extends Code<code.IsEqual> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Load extends Code<code.Load> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Match extends Code<code.Match> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class MulAdd extends Code<code.MulAdd> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Or extends Code<code.Or> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Span extends Code<code.Span> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Store extends Code<code.Store> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Test extends Code<code.Test> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Update extends Code<code.Update> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { code } from '../../../../src/frontend';
++++++++import { Code } from './base';
++++++++
++++++++export class Value extends Code<code.Value> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import code from './code';
++++++++import node from './node';
++++++++import transform from './transform';
++++++++
++++++++export default { code, node, transform };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++
++++++++export abstract class Node<T extends node.Node> {
++++++++ private built: boolean = false;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public build(out: string[]): void {
++++++++ if (this.built) {
++++++++ return;
++++++++ }
++++++++
++++++++ this.built = true;
++++++++ this.doBuild(out);
++++++++
++++++++ if (this.ref.otherwise !== undefined) {
++++++++ (this.ref.otherwise.node as Node<T>).build(out);
++++++++ }
++++++++ }
++++++++
++++++++ protected format(value: string): string {
++++++++ let otherwise: string = '';
++++++++ if (this.ref.otherwise !== undefined) {
++++++++ const otherwiseRef = this.ref.otherwise.node.ref;
++++++++ otherwise = ' otherwise' +
++++++++ `${this.ref.otherwise.noAdvance ? '-no_adv' : ''}=` +
++++++++ `${otherwiseRef.id.name}`;
++++++++ if (this.ref.otherwise.value !== undefined) {
++++++++ otherwise += `:${this.ref.otherwise.value}`;
++++++++ }
++++++++ }
++++++++
++++++++ return `<${this.constructor.name} name=${this.ref.id.name} ` +
++++++++ `${value}${otherwise}/>`;
++++++++ }
++++++++
++++++++ protected abstract doBuild(out: string[]): void;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Consume extends Node<node.Consume> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(`field=${this.ref.field}`));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Empty extends Node<node.Empty> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++class ErrorNode extends Node<node.Error> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(`code=${this.ref.code} reason="${this.ref.reason}"`));
++++++++ }
++++++++}
++++++++
++++++++export { ErrorNode as Error };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { Consume } from './consume';
++++++++import { Empty } from './empty';
++++++++import { Error } from './error';
++++++++import { Invoke } from './invoke';
++++++++import { Pause } from './pause';
++++++++import { Sequence } from './sequence';
++++++++import { Single } from './single';
++++++++import { SpanEnd } from './span-end';
++++++++import { SpanStart } from './span-start';
++++++++import { TableLookup } from './table-lookup';
++++++++
++++++++export default {
++++++++ Consume, Empty, Error, Invoke, Pause, Sequence, Single, SpanEnd,
++++++++ SpanStart, TableLookup,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Invoke extends Node<node.Invoke> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Pause extends Node<node.Pause> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Sequence extends Node<node.Sequence> {
++++++++ protected doBuild(out: string[]): void {
++++++++ let str = `select="${this.ref.select.toString('hex')}" ` +
++++++++ `edge="${this.ref.edge!.node.ref.id.name}"`;
++++++++ if (this.ref.edge!.value !== undefined) {
++++++++ str += `:${this.ref.edge!.value}`;
++++++++ }
++++++++ out.push(this.format(str));
++++++++ const edgeNode = this.ref.edge!.node as Node<node.Node>;
++++++++ edgeNode.build(out);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class Single extends Node<node.Single> {
++++++++ protected doBuild(out: string[]): void {
++++++++ const edges: string[] = [];
++++++++ for (const edge of this.ref.edges) {
++++++++ let str = `k${edge.key}${edge.noAdvance ? '-no_adv-' : ''}=` +
++++++++ `${edge.node.ref.id.name}`;
++++++++ if (edge.value !== undefined) {
++++++++ str += `:${edge.value}`;
++++++++ }
++++++++ edges.push(str);
++++++++ }
++++++++ out.push(this.format(edges.join(' ')));
++++++++
++++++++ for (const edge of this.ref.edges) {
++++++++ const edgeNode = edge.node as Node<node.Node>;
++++++++ edgeNode.build(out);
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanEnd extends Node<node.SpanEnd> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanStart extends Node<node.SpanStart> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { node } from '../../../../src/frontend';
++++++++import { Node } from './base';
++++++++
++++++++export class TableLookup extends Node<node.TableLookup> {
++++++++ protected doBuild(out: string[]): void {
++++++++ out.push(this.format(''));
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export abstract class Transform<T> {
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(): string;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ID extends Transform<transform.ID> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ID } from './id';
++++++++import { ToLower } from './to-lower';
++++++++import { ToLowerUnsafe } from './to-lower-unsafe';
++++++++
++++++++export default { ID, ToLower, ToLowerUnsafe };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLowerUnsafe extends Transform<transform.ToLowerUnsafe> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { transform } from '../../../../src/frontend';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLower extends Transform<transform.ToLower> {
++++++++ public build(): string {
++++++++ return '';
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import * as source from 'llparse-builder';
++++++++
++++++++import { Frontend, node } from '../src/frontend';
++++++++import implementation from './fixtures/implementation';
++++++++import { Node } from './fixtures/implementation/node/base';
++++++++
++++++++function checkNodes(f: Frontend, root: source.node.Node,
++++++++ expected: ReadonlyArray<string>) {
++++++++ const fRoot = f.compile(root, []).root as Node<node.Node>;
++++++++
++++++++ const out: string[] = [];
++++++++ fRoot.build(out);
++++++++
++++++++ assert.deepStrictEqual(out, expected);
++++++++
++++++++ return fRoot;
++++++++}
++++++++
++++++++function checkResumptionTargets(f: Frontend, expected: ReadonlyArray<string>) {
++++++++ const targets = Array.from(f.getResumptionTargets()).map((t) => {
++++++++ return t.ref.id.name;
++++++++ });
++++++++
++++++++ assert.deepStrictEqual(targets, expected);
++++++++}
++++++++
++++++++describe('llparse-frontend', () => {
++++++++ let b: source.Builder;
++++++++ let f: Frontend;
++++++++ beforeEach(() => {
++++++++ b = new source.Builder();
++++++++ f = new Frontend('llparse', implementation);
++++++++ });
++++++++
++++++++ it('should translate nodes to implementation', () => {
++++++++ const root = b.node('root');
++++++++
++++++++ root.match('ab', root);
++++++++ root.match('acd', root);
++++++++ root.match('efg', root);
++++++++ root.otherwise(b.error(123, 'hello'));
++++++++
++++++++ checkNodes(f, root, [
++++++++ '<Single name=llparse__n_root k97=llparse__n_root_1 ' +
++++++++ 'k101=llparse__n_root_3 otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_1 k98=llparse__n_root ' +
++++++++ 'k99=llparse__n_root_2 otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_2 k100=llparse__n_root ' +
++++++++ 'otherwise-no_adv=llparse__n_error/>',
++++++++ '<ErrorNode name=llparse__n_error code=123 reason="hello"/>',
++++++++ '<Sequence name=llparse__n_root_3 select="6667" ' +
++++++++ 'edge=\"llparse__n_root\" ' +
++++++++ 'otherwise-no_adv=llparse__n_error/>',
++++++++ ]);
++++++++
++++++++ checkResumptionTargets(f, [
++++++++ 'llparse__n_root',
++++++++ 'llparse__n_root_1',
++++++++ 'llparse__n_root_3',
++++++++ 'llparse__n_root_2',
++++++++ ]);
++++++++ });
++++++++
++++++++ it('should do peephole optimization', () => {
++++++++ const root = b.node('root');
++++++++ const root1 = b.node('a');
++++++++ const root2 = b.node('b');
++++++++ const node1 = b.node('c');
++++++++ const node2 = b.node('d');
++++++++
++++++++ root.otherwise(root1);
++++++++ root1.otherwise(root2);
++++++++ root2.skipTo(node1);
++++++++ node1.otherwise(node2);
++++++++ node2.otherwise(root);
++++++++
++++++++ checkNodes(f, root, [
++++++++ '<Empty name=llparse__n_b otherwise=llparse__n_b/>',
++++++++ ]);
++++++++
++++++++ checkResumptionTargets(f, [
++++++++ 'llparse__n_b',
++++++++ ]);
++++++++ });
++++++++
++++++++ it('should generate proper resumption targets', () => {
++++++++ b.property('i64', 'counter');
++++++++
++++++++ const root = b.node('root');
++++++++ const end = b.node('end');
++++++++ const store = b.invoke(b.code.store('counter'));
++++++++
++++++++ root.select({ a: 1, b: 2 }, store);
++++++++ root.otherwise(b.error(1, 'okay'));
++++++++
++++++++ store.otherwise(end);
++++++++
++++++++ end.match('ohai', root);
++++++++ end.match('paus', b.pause(1, 'paused').otherwise(
++++++++ b.pause(2, 'paused').otherwise(root)));
++++++++ end.otherwise(b.error(2, 'ohai'));
++++++++
++++++++ checkNodes(f, root, [
++++++++ '<Single name=llparse__n_root k97=llparse__n_invoke_store_counter:1 ' +
++++++++ 'k98=llparse__n_invoke_store_counter:2 ' +
++++++++ 'otherwise-no_adv=llparse__n_error_1/>',
++++++++ '<Invoke name=llparse__n_invoke_store_counter ' +
++++++++ 'otherwise-no_adv=llparse__n_end/>',
++++++++ '<Single name=llparse__n_end k111=llparse__n_end_1 ' +
++++++++ 'k112=llparse__n_end_2 otherwise-no_adv=llparse__n_error/>',
++++++++ '<Sequence name=llparse__n_end_1 select="686169" ' +
++++++++ 'edge="llparse__n_root" otherwise-no_adv=llparse__n_error/>',
++++++++ '<ErrorNode name=llparse__n_error code=2 reason="ohai"/>',
++++++++ '<Sequence name=llparse__n_end_2 select="617573" ' +
++++++++ 'edge="llparse__n_pause" otherwise-no_adv=llparse__n_error/>',
++++++++ '<Pause name=llparse__n_pause otherwise-no_adv=llparse__n_pause_1/>',
++++++++ '<Pause name=llparse__n_pause_1 otherwise-no_adv=llparse__n_root/>',
++++++++ '<ErrorNode name=llparse__n_error_1 code=1 reason="okay"/>',
++++++++ ]);
++++++++
++++++++ checkResumptionTargets(f, [
++++++++ 'llparse__n_root',
++++++++ 'llparse__n_end',
++++++++ 'llparse__n_end_1',
++++++++ 'llparse__n_end_2',
++++++++ 'llparse__n_pause_1',
++++++++ ]);
++++++++ });
++++++++
++++++++ it('should translate Span code into Span', () => {
++++++++ const root = b.invoke(b.code.span('my_span'));
++++++++ root.otherwise(b.error(1, 'okay'));
++++++++
++++++++ const fRoot = checkNodes(f, root, [
++++++++ '<Invoke name=llparse__n_invoke_my_span ' +
++++++++ 'otherwise-no_adv=llparse__n_error/>',
++++++++ '<ErrorNode name=llparse__n_error code=1 reason="okay"/>',
++++++++ ]);
++++++++
++++++++ assert((fRoot.ref as any).code instanceof implementation.code.Span);
++++++++ });
++++++++
++++++++ it('should translate overlapping matches', () => {
++++++++ const root = b.node('root');
++++++++
++++++++ root.match('ab', root);
++++++++ root.match('abc', root);
++++++++ root.otherwise(b.error(123, 'hello'));
++++++++
++++++++ checkNodes(f, root, [
++++++++ '<Sequence name=llparse__n_root select="6162" edge="llparse__n_root_1" otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_1 k99=llparse__n_root otherwise-no_adv=llparse__n_root/>',
++++++++ '<ErrorNode name=llparse__n_error code=123 reason="hello"/>',
++++++++ ]);
++++++++
++++++++ checkResumptionTargets(f, [
++++++++ 'llparse__n_root',
++++++++ 'llparse__n_root_1',
++++++++ ]);
++++++++ });
++++++++
++++++++ it('should translate overlapping matches with values', () => {
++++++++ const root = b.node('root');
++++++++ const store = b.invoke(b.code.store('counter'));
++++++++
++++++++ root.select({
++++++++ ab: 1,
++++++++ abc: 2,
++++++++ }, store);
++++++++ store.otherwise(root);
++++++++ root.otherwise(b.error(123, 'hello'));
++++++++
++++++++ checkNodes(f, root, [
++++++++ '<Sequence name=llparse__n_root select="6162" edge="llparse__n_root_1" otherwise-no_adv=llparse__n_error/>',
++++++++ '<Single name=llparse__n_root_1 k99=llparse__n_invoke_store_counter:2 otherwise-no_adv=llparse__n_invoke_store_counter:1/>',
++++++++ '<Invoke name=llparse__n_invoke_store_counter otherwise-no_adv=llparse__n_root/>',
++++++++ '<ErrorNode name=llparse__n_error code=123 reason="hello"/>',
++++++++ ]);
++++++++
++++++++ checkResumptionTargets(f, [
++++++++ 'llparse__n_root',
++++++++ 'llparse__n_root_1',
++++++++ ]);
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "compilerOptions": {
++++++++ "strict": true,
++++++++ "target": "es2017",
++++++++ "module": "commonjs",
++++++++ "moduleResolution": "node",
++++++++ "outDir": "./lib",
++++++++ "declaration": true,
++++++++ "pretty": true,
++++++++ "sourceMap": true
++++++++ },
++++++++ "include": [
++++++++ "src/**/*.ts"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "defaultSeverity": "error",
++++++++ "extends": [
++++++++ "tslint:recommended"
++++++++ ],
++++++++ "jsRules": {},
++++++++ "rules": {
++++++++ "no-bitwise": null,
++++++++ "max-line-length": [true, 80],
++++++++ "max-classes-per-file": [true, 1, "exclude-class-expressions"],
++++++++ "quotemark": [
++++++++ true, "single", "avoid-escape", "avoid-template"
++++++++ ]
++++++++ },
++++++++ "rulesDirectory": []
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++node_modules/
++++++++npm-debug.log
++++++++lib/
++++++++test/tmp/
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++sudo: false
++++++++language: node_js
++++++++node_js:
++++++++ - "stable"
++++++++script:
++++++++ CFLAGS="-O0" npm test
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++llparse.org
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# Code of Conduct
++++++++
++++++++* [Node.js Code of Conduct](https://github.com/nodejs/admin/blob/master/CODE_OF_CONDUCT.md)
++++++++* [Node.js Moderation Policy](https://github.com/nodejs/admin/blob/master/Moderation-Policy.md)
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2018.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++# llparse
++++++++[](http://travis-ci.org/nodejs/llparse)
++++++++[](https://badge.fury.io/js/llparse)
++++++++
++++++++An API for compiling an incremental parser into a C output.
++++++++
++++++++## Usage
++++++++
++++++++```ts
++++++++import { LLParse } from 'llparse';
++++++++
++++++++const p = new LLParse('http_parser');
++++++++
++++++++const method = p.node('method');
++++++++const beforeUrl = p.node('before_url');
++++++++const urlSpan = p.span(p.code.span('on_url'));
++++++++const url = p.node('url');
++++++++const http = p.node('http');
++++++++
++++++++// Add custom uint8_t property to the state
++++++++p.property('i8', 'method');
++++++++
++++++++// Store method inside a custom property
++++++++const onMethod = p.invoke(p.code.store('method'), beforeUrl);
++++++++
++++++++// Invoke custom C function
++++++++const complete = p.invoke(p.code.match('on_complete'), {
++++++++ // Restart
++++++++ 0: method
++++++++}, p.error(4, '`on_complete` error'));
++++++++
++++++++method
++++++++ .select({
++++++++ 'HEAD': 0, 'GET': 1, 'POST': 2, 'PUT': 3,
++++++++ 'DELETE': 4, 'OPTIONS': 5, 'CONNECT': 6,
++++++++ 'TRACE': 7, 'PATCH': 8
++++++++ }, onMethod)
++++++++ .otherwise(p.error(5, 'Expected method'));
++++++++
++++++++beforeUrl
++++++++ .match(' ', beforeUrl)
++++++++ .otherwise(urlSpan.start(url));
++++++++
++++++++url
++++++++ .peek(' ', urlSpan.end(http))
++++++++ .skipTo(url);
++++++++
++++++++http
++++++++ .match(' HTTP/1.1\r\n\r\n', complete)
++++++++ .otherwise(p.error(6, 'Expected HTTP/1.1 and two newlines'));
++++++++
++++++++const artifacts = p.build(method);
++++++++console.log('----- C -----');
++++++++console.log(artifacts.c); // string
++++++++console.log('----- C END -----');
++++++++console.log('----- HEADER -----');
++++++++console.log(artifacts.header);
++++++++console.log('----- HEADER END -----');
++++++++```
++++++++
++++++++#### LICENSE
++++++++
++++++++This software is licensed under the MIT License.
++++++++
++++++++Copyright Fedor Indutny, 2020.
++++++++
++++++++Permission is hereby granted, free of charge, to any person obtaining a
++++++++copy of this software and associated documentation files (the
++++++++"Software"), to deal in the Software without restriction, including
++++++++without limitation the rights to use, copy, modify, merge, publish,
++++++++distribute, sublicense, and/or sell copies of the Software, and to permit
++++++++persons to whom the Software is furnished to do so, subject to the
++++++++following conditions:
++++++++
++++++++The above copyright notice and this permission notice shall be included
++++++++in all copies or substantial portions of the Software.
++++++++
++++++++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
++++++++OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
++++++++MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
++++++++NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
++++++++DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
++++++++OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
++++++++USE OR OTHER DEALINGS IN THE SOFTWARE.
++++++++
++++++++[3]: https://llvm.org/docs/LangRef.html
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++theme: jekyll-theme-midnight
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++http
++++++++*.c
++++++++*.ll
++++++++*.h
++++++++*.o
++++++++*.dSYM
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++CC ?= clang
++++++++
++++++++all: http
++++++++
++++++++http: main.c http_parser.bc
++++++++ $(CC) -g3 -flto -Os -fvisibility=hidden -Wall -I. http_parser.c main.c -o $@
++++++++
++++++++http_parser.bc: index.ts
++++++++ npx ts-node $<
++++++++
++++++++.PHONY = all
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { LLParse } from '../../src/api';
++++++++
++++++++const p = new LLParse('http_parser');
++++++++
++++++++const method = p.node('method');
++++++++const beforeUrl = p.node('before_url');
++++++++const urlSpan = p.span(p.code.span('on_url'));
++++++++const url = p.node('url');
++++++++const http = p.node('http');
++++++++
++++++++// Add custom uint8_t property to the state
++++++++p.property('i8', 'method');
++++++++
++++++++// Store method inside a custom property
++++++++const onMethod = p.invoke(p.code.store('method'), beforeUrl);
++++++++
++++++++// Invoke custom C function
++++++++const complete = p.invoke(p.code.match('on_complete'), {
++++++++ // Restart
++++++++ 0: method
++++++++}, p.error(4, '`on_complete` error'));
++++++++
++++++++method
++++++++ .select({
++++++++ 'HEAD': 0, 'GET': 1, 'POST': 2, 'PUT': 3,
++++++++ 'DELETE': 4, 'OPTIONS': 5, 'CONNECT': 6,
++++++++ 'TRACE': 7, 'PATCH': 8
++++++++ }, onMethod)
++++++++ .otherwise(p.error(5, 'Expected method'));
++++++++
++++++++beforeUrl
++++++++ .match(' ', beforeUrl)
++++++++ .otherwise(urlSpan.start(url));
++++++++
++++++++url
++++++++ .peek(' ', urlSpan.end(http))
++++++++ .skipTo(url);
++++++++
++++++++http
++++++++ .match(' HTTP/1.1\r\n\r\n', complete)
++++++++ .match(' HTTP/1.1\n\n', complete)
++++++++ .otherwise(p.error(6, 'Expected HTTP/1.1 and two newlines'));
++++++++
++++++++// Build
++++++++
++++++++const fs = require('fs');
++++++++const path = require('path');
++++++++
++++++++const artifacts = p.build(method);
++++++++fs.writeFileSync(path.join(__dirname, 'http_parser.h'), artifacts.header);
++++++++fs.writeFileSync(path.join(__dirname, 'http_parser.c'), artifacts.c);
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include <stdio.h>
++++++++#include <stdlib.h>
++++++++#include <string.h>
++++++++#include <sys/time.h>
++++++++
++++++++#include "http_parser.h"
++++++++
++++++++int on_url(http_parser_t* s, const char* p, const char* endp) {
++++++++ if (p == endp)
++++++++ return 0;
++++++++
++++++++ fprintf(stdout, "method=%d url_part=\"%.*s\"\n", s->method,
++++++++ (int) (endp - p), p);
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int on_complete(http_parser_t* s, const char* p, const char* endp) {
++++++++ fprintf(stdout, "on_complete\n");
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int main(int argc, char** argv) {
++++++++ http_parser_t s;
++++++++
++++++++ http_parser_init(&s);
++++++++
++++++++ for (;;) {
++++++++ char buf[16384];
++++++++ const char* input;
++++++++ const char* endp;
++++++++ int code;
++++++++
++++++++ input = fgets(buf, sizeof(buf), stdin);
++++++++ if (input == NULL)
++++++++ break;
++++++++
++++++++ endp = input + strlen(input);
++++++++ code = http_parser_execute(&s, input, endp);
++++++++ if (code != 0) {
++++++++ fprintf(stderr, "code=%d error=%d reason=%s\n", code, s.error, s.reason);
++++++++ return -1;
++++++++ }
++++++++ }
++++++++
++++++++ return 0;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse",
++++++++ "version": "7.1.1",
++++++++ "lockfileVersion": 1,
++++++++ "requires": true,
++++++++ "dependencies": {
++++++++ "@babel/code-frame": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.10.4.tgz",
++++++++ "integrity": "sha512-vG6SvB6oYEhvgisZNFRmRCUkLz11c7rp+tbNTynGqc6mS1d5ATd/sGyV6W0KZZnXRKMTzZDRgQT3Ou9jhpAfUg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/highlight": "^7.10.4"
++++++++ }
++++++++ },
++++++++ "@babel/helper-validator-identifier": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.10.4.tgz",
++++++++ "integrity": "sha512-3U9y+43hz7ZM+rzG24Qe2mufW5KhvFg/NhnNph+i9mgCtdTCtMJuI1TMkrIUiK7Ix4PYlRF9I5dhqaLYA/ADXw==",
++++++++ "dev": true
++++++++ },
++++++++ "@babel/highlight": {
++++++++ "version": "7.10.4",
++++++++ "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.10.4.tgz",
++++++++ "integrity": "sha512-i6rgnR/YgPEQzZZnbTHHuZdlE8qyoBNalD6F+q4vAFlcMEcqmkoG+mPqJYJCo63qPf74+Y1UZsl3l6f7/RIkmA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/helper-validator-identifier": "^7.10.4",
++++++++ "chalk": "^2.0.0",
++++++++ "js-tokens": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "@types/color-name": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz",
++++++++ "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/mocha": {
++++++++ "version": "8.0.3",
++++++++ "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-8.0.3.tgz",
++++++++ "integrity": "sha512-vyxR57nv8NfcU0GZu8EUXZLTbCMupIUwy95LJ6lllN+JRPG25CwMHoB1q5xKh8YKhQnHYRAn4yW2yuHbf/5xgg==",
++++++++ "dev": true
++++++++ },
++++++++ "@types/node": {
++++++++ "version": "14.11.8",
++++++++ "resolved": "https://registry.npmjs.org/@types/node/-/node-14.11.8.tgz",
++++++++ "integrity": "sha512-KPcKqKm5UKDkaYPTuXSx8wEP7vE9GnuaXIZKijwRYcePpZFDVuy2a57LarFKiORbHOuTOOwYzxVxcUzsh2P2Pw==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-colors": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz",
++++++++ "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-regex": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
++++++++ "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-styles": {
++++++++ "version": "3.2.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz",
++++++++ "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^1.9.0"
++++++++ }
++++++++ },
++++++++ "anymatch": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.1.tgz",
++++++++ "integrity": "sha512-mM8522psRCqzV+6LhomX5wgp25YVibjh8Wj23I5RPkPppSVSjyKD2A2mBJmWGa+KN7f2D6LNh9jkBCeyLktzjg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "normalize-path": "^3.0.0",
++++++++ "picomatch": "^2.0.4"
++++++++ }
++++++++ },
++++++++ "arg": {
++++++++ "version": "4.1.3",
++++++++ "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz",
++++++++ "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==",
++++++++ "dev": true
++++++++ },
++++++++ "argparse": {
++++++++ "version": "1.0.10",
++++++++ "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz",
++++++++ "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "sprintf-js": "~1.0.2"
++++++++ }
++++++++ },
++++++++ "array.prototype.map": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/array.prototype.map/-/array.prototype.map-1.0.2.tgz",
++++++++ "integrity": "sha512-Az3OYxgsa1g7xDYp86l0nnN4bcmuEITGe1rbdEBVkrqkzMgDcbdQ2R7r41pNzti+4NMces3H8gMmuioZUilLgw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "es-array-method-boxes-properly": "^1.0.0",
++++++++ "is-string": "^1.0.4"
++++++++ }
++++++++ },
++++++++ "balanced-match": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
++++++++ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
++++++++ "dev": true
++++++++ },
++++++++ "binary-extensions": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
++++++++ "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==",
++++++++ "dev": true
++++++++ },
++++++++ "binary-search": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/binary-search/-/binary-search-1.3.6.tgz",
++++++++ "integrity": "sha512-nbE1WxOTTrUWIfsfZ4aHGYu5DOuNkbxGokjV6Z2kxfJK3uaAb8zNK1muzOeipoLHZjInT4Br88BHpzevc681xA=="
++++++++ },
++++++++ "bitcode": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/bitcode/-/bitcode-1.2.0.tgz",
++++++++ "integrity": "sha512-cWgZK/ri/1ZUJ+UKEwP9Cqw10WY5wHz+boMxVO4vvc0btmxa2tMc2m2Zk9HYdCyx4b5+sgQM1/NCJPTIPO1XOw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "bitcode-builder": "^1.2.0"
++++++++ }
++++++++ },
++++++++ "bitcode-builder": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/bitcode-builder/-/bitcode-builder-1.2.0.tgz",
++++++++ "integrity": "sha512-biuJIhrog5d1IFMaKtHMJ8PJ1L3zxiWdclwYErjOBWf8Gwyqa4XwflvMufzcQw/OUeAArO1AqOrqsOFsWJ94OA==",
++++++++ "dev": true
++++++++ },
++++++++ "brace-expansion": {
++++++++ "version": "1.1.11",
++++++++ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
++++++++ "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "balanced-match": "^1.0.0",
++++++++ "concat-map": "0.0.1"
++++++++ }
++++++++ },
++++++++ "braces": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
++++++++ "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fill-range": "^7.0.1"
++++++++ }
++++++++ },
++++++++ "browser-stdout": {
++++++++ "version": "1.3.1",
++++++++ "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
++++++++ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
++++++++ "dev": true
++++++++ },
++++++++ "buffer-from": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
++++++++ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==",
++++++++ "dev": true
++++++++ },
++++++++ "builtin-modules": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz",
++++++++ "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
++++++++ "dev": true
++++++++ },
++++++++ "camelcase": {
++++++++ "version": "5.3.1",
++++++++ "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz",
++++++++ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==",
++++++++ "dev": true
++++++++ },
++++++++ "chalk": {
++++++++ "version": "2.4.2",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz",
++++++++ "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.1",
++++++++ "escape-string-regexp": "^1.0.5",
++++++++ "supports-color": "^5.3.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "supports-color": {
++++++++ "version": "5.5.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
++++++++ "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^3.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "chokidar": {
++++++++ "version": "3.4.2",
++++++++ "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.4.2.tgz",
++++++++ "integrity": "sha512-IZHaDeBeI+sZJRX7lGcXsdzgvZqKv6sECqsbErJA4mHWfpRrD8B97kSFN4cQz6nGBGiuFia1MKR4d6c1o8Cv7A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "anymatch": "~3.1.1",
++++++++ "braces": "~3.0.2",
++++++++ "fsevents": "~2.1.2",
++++++++ "glob-parent": "~5.1.0",
++++++++ "is-binary-path": "~2.1.0",
++++++++ "is-glob": "~4.0.1",
++++++++ "normalize-path": "~3.0.0",
++++++++ "readdirp": "~3.4.0"
++++++++ }
++++++++ },
++++++++ "cliui": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz",
++++++++ "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^4.2.0",
++++++++ "strip-ansi": "^6.0.0",
++++++++ "wrap-ansi": "^6.2.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz",
++++++++ "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "8.0.0",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
++++++++ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
++++++++ "dev": true
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
++++++++ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz",
++++++++ "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^8.0.0",
++++++++ "is-fullwidth-code-point": "^3.0.0",
++++++++ "strip-ansi": "^6.0.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz",
++++++++ "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^5.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "1.9.3",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz",
++++++++ "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "1.1.3"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
++++++++ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=",
++++++++ "dev": true
++++++++ },
++++++++ "commander": {
++++++++ "version": "2.20.3",
++++++++ "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
++++++++ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
++++++++ "dev": true
++++++++ },
++++++++ "concat-map": {
++++++++ "version": "0.0.1",
++++++++ "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
++++++++ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
++++++++ "dev": true
++++++++ },
++++++++ "debug": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz",
++++++++ "integrity": "sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==",
++++++++ "requires": {
++++++++ "ms": "2.1.2"
++++++++ }
++++++++ },
++++++++ "decamelize": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
++++++++ "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
++++++++ "dev": true
++++++++ },
++++++++ "define-properties": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz",
++++++++ "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
++++++++ "requires": {
++++++++ "object-keys": "^1.0.12"
++++++++ }
++++++++ },
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "7.0.3",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-7.0.3.tgz",
++++++++ "integrity": "sha512-CwBLREIQ7LvYFB0WyRvwhq5N5qPhc6PMjD6bYggFlI5YyDgl+0vxq5VHbMOFqLg7hfWzmu8T5Z1QofhmTIhItA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-abstract": {
++++++++ "version": "1.17.7",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.17.7.tgz",
++++++++ "integrity": "sha512-VBl/gnfcJ7OercKA9MVaegWsBHFjV492syMudcnQZvt/Dw8ezpcOHYZXa/J96O8vx+g4x65YKhxOwDUh63aS5g==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.1.tgz",
++++++++ "integrity": "sha512-VT/cxmx5yaoHSOTSyrCygIDFco+RsibY2NM0a4RdEeY/4KgqezwFtK1yr3U67xYhqJSlASm2pKhLVzPj2lr4bA==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.18.0-next.0",
++++++++ "has-symbols": "^1.0.1",
++++++++ "object-keys": "^1.1.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "es-abstract": {
++++++++ "version": "1.18.0-next.1",
++++++++ "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.18.0-next.1.tgz",
++++++++ "integrity": "sha512-I4UGspA0wpZXWENrdA0uHbnhte683t3qT/1VFH9aX2dA5PPSf6QW5HHXf5HImaqPmjXaVeVk4RGWnaylmV7uAA==",
++++++++ "requires": {
++++++++ "es-to-primitive": "^1.2.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "has": "^1.0.3",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-callable": "^1.2.2",
++++++++ "is-negative-zero": "^2.0.0",
++++++++ "is-regex": "^1.1.1",
++++++++ "object-inspect": "^1.8.0",
++++++++ "object-keys": "^1.1.1",
++++++++ "object.assign": "^4.1.1",
++++++++ "string.prototype.trimend": "^1.0.1",
++++++++ "string.prototype.trimstart": "^1.0.1"
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "es-array-method-boxes-properly": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/es-array-method-boxes-properly/-/es-array-method-boxes-properly-1.0.0.tgz",
++++++++ "integrity": "sha512-wd6JXUmyHmt8T5a2xreUwKcGPq6f1f+WwIJkijUqiGcJz1qqnZgP6XIK+QyIWU5lT7imeNxUll48bziG+TSYcA==",
++++++++ "dev": true
++++++++ },
++++++++ "es-get-iterator": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/es-get-iterator/-/es-get-iterator-1.1.0.tgz",
++++++++ "integrity": "sha512-UfrmHuWQlNMTs35e1ypnvikg6jCz3SK8v8ImvmDsh36fCVUR1MqoFDiyn0/k52C8NqO3YsO8Oe0azeesNuqSsQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-abstract": "^1.17.4",
++++++++ "has-symbols": "^1.0.1",
++++++++ "is-arguments": "^1.0.4",
++++++++ "is-map": "^2.0.1",
++++++++ "is-set": "^2.0.1",
++++++++ "is-string": "^1.0.5",
++++++++ "isarray": "^2.0.5"
++++++++ }
++++++++ },
++++++++ "es-to-primitive": {
++++++++ "version": "1.2.1",
++++++++ "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.1.tgz",
++++++++ "integrity": "sha512-QCOllgZJtaUo9miYBcLChTUaHNjJF3PYs1VidD7AwiEj1kYxKeQTctLAezAOH5ZKRH0g2IgPn6KwB4IT8iRpvA==",
++++++++ "requires": {
++++++++ "is-callable": "^1.1.4",
++++++++ "is-date-object": "^1.0.1",
++++++++ "is-symbol": "^1.0.2"
++++++++ }
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
++++++++ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
++++++++ "dev": true
++++++++ },
++++++++ "esm": {
++++++++ "version": "3.2.25",
++++++++ "resolved": "https://registry.npmjs.org/esm/-/esm-3.2.25.tgz",
++++++++ "integrity": "sha512-U1suiZ2oDVWv4zPO56S0NcR5QriEahGtdN2OR6FiOG4WJvcjBVFB0qI4+eKoWFH483PKGuLuu6V8Z4T5g63UVA==",
++++++++ "dev": true
++++++++ },
++++++++ "esprima": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
++++++++ "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
++++++++ "dev": true
++++++++ },
++++++++ "fill-range": {
++++++++ "version": "7.0.1",
++++++++ "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
++++++++ "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "to-regex-range": "^5.0.1"
++++++++ }
++++++++ },
++++++++ "find-up": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz",
++++++++ "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^6.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "flat": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/flat/-/flat-4.1.0.tgz",
++++++++ "integrity": "sha512-Px/TiLIznH7gEDlPXcUD4KnBusa6kR6ayRUVcnEAbreRIuhkqow/mun59BuRXwoYk7ZQOLW1ZM05ilIvK38hFw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-buffer": "~2.0.3"
++++++++ }
++++++++ },
++++++++ "fs.realpath": {
++++++++ "version": "1.0.0",
++++++++ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
++++++++ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
++++++++ "dev": true
++++++++ },
++++++++ "fsevents": {
++++++++ "version": "2.1.3",
++++++++ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz",
++++++++ "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==",
++++++++ "dev": true,
++++++++ "optional": true
++++++++ },
++++++++ "function-bind": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
++++++++ "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
++++++++ },
++++++++ "get-caller-file": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
++++++++ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.3",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz",
++++++++ "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "glob-parent": {
++++++++ "version": "5.1.1",
++++++++ "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.1.tgz",
++++++++ "integrity": "sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-glob": "^4.0.1"
++++++++ }
++++++++ },
++++++++ "growl": {
++++++++ "version": "1.10.5",
++++++++ "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz",
++++++++ "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==",
++++++++ "dev": true
++++++++ },
++++++++ "has": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
++++++++ "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
++++++++ "requires": {
++++++++ "function-bind": "^1.1.1"
++++++++ }
++++++++ },
++++++++ "has-flag": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
++++++++ "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=",
++++++++ "dev": true
++++++++ },
++++++++ "has-symbols": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.1.tgz",
++++++++ "integrity": "sha512-PLcsoqu++dmEIZB+6totNFKq/7Do+Z0u4oT0zKOJNl3lYK6vGwwu2hjHs+68OEZbTjiUE9bgOABXbP/GvrS0Kg=="
++++++++ },
++++++++ "he": {
++++++++ "version": "1.2.0",
++++++++ "resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
++++++++ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
++++++++ "dev": true
++++++++ },
++++++++ "inflight": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
++++++++ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "once": "^1.3.0",
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "inherits": {
++++++++ "version": "2.0.4",
++++++++ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
++++++++ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
++++++++ "dev": true
++++++++ },
++++++++ "is-arguments": {
++++++++ "version": "1.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-arguments/-/is-arguments-1.0.4.tgz",
++++++++ "integrity": "sha512-xPh0Rmt8NE65sNzvyUmWgI1tz3mKq74lGA0mL8LYZcoIzKOzDh6HmrYm3d18k60nHerC8A9Km8kYu87zfSFnLA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-binary-path": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
++++++++ "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "binary-extensions": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "is-buffer": {
++++++++ "version": "2.0.4",
++++++++ "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz",
++++++++ "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==",
++++++++ "dev": true
++++++++ },
++++++++ "is-callable": {
++++++++ "version": "1.2.2",
++++++++ "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.2.tgz",
++++++++ "integrity": "sha512-dnMqspv5nU3LoewK2N/y7KLtxtakvTuaCsU9FU50/QDmdbHNy/4/JuRtMHqRU22o3q+W89YQndQEeCVwK+3qrA=="
++++++++ },
++++++++ "is-date-object": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.2.tgz",
++++++++ "integrity": "sha512-USlDT524woQ08aoZFzh3/Z6ch9Y/EWXEHQ/AaRN0SkKq4t2Jw2R2339tSXmwuVoY7LLlBCbOIlx2myP/L5zk0g=="
++++++++ },
++++++++ "is-extglob": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
++++++++ "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=",
++++++++ "dev": true
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz",
++++++++ "integrity": "sha1-o7MKXE8ZkYMWeqq5O+764937ZU8=",
++++++++ "dev": true
++++++++ },
++++++++ "is-glob": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.1.tgz",
++++++++ "integrity": "sha512-5G0tKtBTFImOqDnLB2hG6Bp2qcKEFduo4tZu9MT/H6NQv/ghhy30o55ufafxJ/LdH79LLs2Kfrn85TLKyA7BUg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-extglob": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "is-map": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-map/-/is-map-2.0.1.tgz",
++++++++ "integrity": "sha512-T/S49scO8plUiAOA2DBTBG3JHpn1yiw0kRp6dgiZ0v2/6twi5eiB0rHtHFH9ZIrvlWc6+4O+m4zg5+Z833aXgw==",
++++++++ "dev": true
++++++++ },
++++++++ "is-negative-zero": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.0.tgz",
++++++++ "integrity": "sha1-lVOxIbD6wohp2p7UWeIMdUN4hGE="
++++++++ },
++++++++ "is-number": {
++++++++ "version": "7.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
++++++++ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
++++++++ "dev": true
++++++++ },
++++++++ "is-plain-obj": {
++++++++ "version": "1.1.0",
++++++++ "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-1.1.0.tgz",
++++++++ "integrity": "sha1-caUMhCnfync8kqOQpKA7OfzVHT4=",
++++++++ "dev": true
++++++++ },
++++++++ "is-regex": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.1.1.tgz",
++++++++ "integrity": "sha512-1+QkEcxiLlB7VEyFtyBg94e08OAsvq7FUBgApTq/w2ymCLyKJgDPsybBENVtA7XCQEgEXxKPonG+mvYRxh/LIg==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "is-set": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/is-set/-/is-set-2.0.1.tgz",
++++++++ "integrity": "sha512-eJEzOtVyenDs1TMzSQ3kU3K+E0GUS9sno+F0OBT97xsgcJsF9nXMBtkT9/kut5JEpM7oL7X/0qxR17K3mcwIAA==",
++++++++ "dev": true
++++++++ },
++++++++ "is-string": {
++++++++ "version": "1.0.5",
++++++++ "resolved": "https://registry.npmjs.org/is-string/-/is-string-1.0.5.tgz",
++++++++ "integrity": "sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==",
++++++++ "dev": true
++++++++ },
++++++++ "is-symbol": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.3.tgz",
++++++++ "integrity": "sha512-OwijhaRSgqvhm/0ZdAcXNZt9lYdKFpcRDT5ULUuYXPoT794UNOdU+gpT6Rzo7b4V2HUl/op6GqY894AZwv9faQ==",
++++++++ "requires": {
++++++++ "has-symbols": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "isarray": {
++++++++ "version": "2.0.5",
++++++++ "resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz",
++++++++ "integrity": "sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==",
++++++++ "dev": true
++++++++ },
++++++++ "isexe": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
++++++++ "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-iterator": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/iterate-iterator/-/iterate-iterator-1.0.1.tgz",
++++++++ "integrity": "sha512-3Q6tudGN05kbkDQDI4CqjaBf4qf85w6W6GnuZDtUVYwKgtC1q8yxYX7CZed7N+tLzQqS6roujWvszf13T+n9aw==",
++++++++ "dev": true
++++++++ },
++++++++ "iterate-value": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/iterate-value/-/iterate-value-1.0.2.tgz",
++++++++ "integrity": "sha512-A6fMAio4D2ot2r/TYzr4yUWrmwNdsN5xL7+HUiyACE4DXm+q8HtPcnFTp+NnW3k4N05tZ7FVYFFb2CR13NxyHQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "es-get-iterator": "^1.0.2",
++++++++ "iterate-iterator": "^1.0.1"
++++++++ }
++++++++ },
++++++++ "js-tokens": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
++++++++ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
++++++++ "dev": true
++++++++ },
++++++++ "js-yaml": {
++++++++ "version": "3.13.1",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz",
++++++++ "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "argparse": "^1.0.7",
++++++++ "esprima": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "llparse": {
++++++++ "version": "6.4.0",
++++++++ "resolved": "https://registry.npmjs.org/llparse/-/llparse-6.4.0.tgz",
++++++++ "integrity": "sha512-ySA+bj2wOLXrKmohAVMw0Nq84oHDPLdg+sUx4+VeSk1U72MEKfKAXS7zh82n15BRjWc/cVgWBN9RQAFdgk0g5Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "bitcode": "^1.2.0",
++++++++ "debug": "^3.2.6",
++++++++ "llparse-frontend": "^1.4.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "debug": {
++++++++ "version": "3.2.6",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
++++++++ "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "llparse-frontend": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/llparse-frontend/-/llparse-frontend-1.4.0.tgz",
++++++++ "integrity": "sha512-lUpGvGU9MDPb3k4Wbb0S7FgpceCirXVeFQQZjsYWB3fIEGU0Q6IEiTO91J6MLLN75gsxvGiWZaKVnmcHb7jh6g==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "debug": "^3.2.6",
++++++++ "llparse-builder": "^1.3.2"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "llparse-builder": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/llparse-builder/-/llparse-builder-1.4.0.tgz",
++++++++ "integrity": "sha512-mu0/zgAc1KdD6r+tjmRvF+YgoToQvBun4iXISRfSmx66b5qurckRpYjzBUYpHn0XVqKPRrGg86gMQKv8ogY3Rw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@types/debug": "0.0.30",
++++++++ "binary-search": "^1.3.6",
++++++++ "debug": "^3.2.6"
++++++++ },
++++++++ "dependencies": {
++++++++ "@types/debug": {
++++++++ "version": "0.0.30",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-0.0.30.tgz",
++++++++ "integrity": "sha512-orGL5LXERPYsLov6CWs3Fh6203+dXzJkR7OnddIr2514Hsecwc8xRpzCapshBbKFImCsvS/mk6+FWiN5LyZJAQ==",
++++++++ "dev": true
++++++++ },
++++++++ "debug": {
++++++++ "version": "3.2.6",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
++++++++ "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "llparse-frontend": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/llparse-frontend/-/llparse-frontend-3.0.0.tgz",
++++++++ "integrity": "sha512-G/o0Po2C+G5OtP8MJeQDjDf5qwDxcO7K6x4r6jqGsJwxk7yblbJnRqpmye7G/lZ8dD0Hv5neY4/KB5BhDmEc9Q==",
++++++++ "requires": {
++++++++ "debug": "^3.2.6",
++++++++ "llparse-builder": "^1.5.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "@types/debug": {
++++++++ "version": "4.1.5",
++++++++ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
++++++++ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ=="
++++++++ },
++++++++ "debug": {
++++++++ "version": "3.2.6",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
++++++++ "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "llparse-builder": {
++++++++ "version": "1.5.2",
++++++++ "resolved": "https://registry.npmjs.org/llparse-builder/-/llparse-builder-1.5.2.tgz",
++++++++ "integrity": "sha512-i862UNC3YUEdlfK/NUCJxlKjtWjgAI9AJXDRgjcfRHfwFt4Sf8eFPTRsc91/2R9MBZ0kyFdfhi8SVhMsZf1gNQ==",
++++++++ "requires": {
++++++++ "@types/debug": "4.1.5 ",
++++++++ "binary-search": "^1.3.6",
++++++++ "debug": "^4.2.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "debug": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.2.0.tgz",
++++++++ "integrity": "sha512-IX2ncY78vDTjZMFUdmsvIRFY2Cf4FnD0wRs+nQwJU8Lu99/tPFdb0VybiiMTPe3I6rQmwsqQqRBvxU+bZ/I8sg==",
++++++++ "requires": {
++++++++ "ms": "2.1.2"
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "llparse-test-fixture": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/llparse-test-fixture/-/llparse-test-fixture-5.0.1.tgz",
++++++++ "integrity": "sha512-BrnS70lxODcTXttLkfoSqn8DPbNuuSLFR48JnwxLimFkr8QRNBVbUku+bumIIo5Z7gAbIGNQXDOiSi2crMzS8Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "esm": "^3.2.25",
++++++++ "llparse": "^6.4.0",
++++++++ "yargs": "^15.4.1"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz",
++++++++ "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^5.0.0"
++++++++ }
++++++++ },
++++++++ "log-symbols": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.0.0.tgz",
++++++++ "integrity": "sha512-FN8JBzLx6CzeMrB0tg6pqlGU1wCrXW+ZXGH481kfsBqer0hToTIiHdjH4Mq8xJUbvATujKCvaREGWpGUionraA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "chalk": "^4.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-styles": {
++++++++ "version": "4.3.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
++++++++ "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-convert": "^2.0.1"
++++++++ }
++++++++ },
++++++++ "chalk": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz",
++++++++ "integrity": "sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^4.1.0",
++++++++ "supports-color": "^7.1.0"
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "~1.1.4"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "make-error": {
++++++++ "version": "1.3.6",
++++++++ "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
++++++++ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==",
++++++++ "dev": true
++++++++ },
++++++++ "minimatch": {
++++++++ "version": "3.0.4",
++++++++ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
++++++++ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "brace-expansion": "^1.1.7"
++++++++ }
++++++++ },
++++++++ "minimist": {
++++++++ "version": "1.2.5",
++++++++ "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
++++++++ "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
++++++++ "dev": true
++++++++ },
++++++++ "mkdirp": {
++++++++ "version": "0.5.5",
++++++++ "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.5.tgz",
++++++++ "integrity": "sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "minimist": "^1.2.5"
++++++++ }
++++++++ },
++++++++ "mocha": {
++++++++ "version": "8.1.3",
++++++++ "resolved": "https://registry.npmjs.org/mocha/-/mocha-8.1.3.tgz",
++++++++ "integrity": "sha512-ZbaYib4hT4PpF4bdSO2DohooKXIn4lDeiYqB+vTmCdr6l2woW0b6H3pf5x4sM5nwQMru9RvjjHYWVGltR50ZBw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-colors": "4.1.1",
++++++++ "browser-stdout": "1.3.1",
++++++++ "chokidar": "3.4.2",
++++++++ "debug": "4.1.1",
++++++++ "diff": "4.0.2",
++++++++ "escape-string-regexp": "4.0.0",
++++++++ "find-up": "5.0.0",
++++++++ "glob": "7.1.6",
++++++++ "growl": "1.10.5",
++++++++ "he": "1.2.0",
++++++++ "js-yaml": "3.14.0",
++++++++ "log-symbols": "4.0.0",
++++++++ "minimatch": "3.0.4",
++++++++ "ms": "2.1.2",
++++++++ "object.assign": "4.1.0",
++++++++ "promise.allsettled": "1.0.2",
++++++++ "serialize-javascript": "4.0.0",
++++++++ "strip-json-comments": "3.0.1",
++++++++ "supports-color": "7.1.0",
++++++++ "which": "2.0.2",
++++++++ "wide-align": "1.1.3",
++++++++ "workerpool": "6.0.0",
++++++++ "yargs": "13.3.2",
++++++++ "yargs-parser": "13.1.2",
++++++++ "yargs-unparser": "1.6.1"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "cliui": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz",
++++++++ "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^3.1.0",
++++++++ "strip-ansi": "^5.2.0",
++++++++ "wrap-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "debug": {
++++++++ "version": "4.1.1",
++++++++ "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
++++++++ "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ms": "^2.1.1"
++++++++ }
++++++++ },
++++++++ "escape-string-regexp": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
++++++++ "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
++++++++ "dev": true
++++++++ },
++++++++ "glob": {
++++++++ "version": "7.1.6",
++++++++ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
++++++++ "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "fs.realpath": "^1.0.0",
++++++++ "inflight": "^1.0.4",
++++++++ "inherits": "2",
++++++++ "minimatch": "^3.0.4",
++++++++ "once": "^1.3.0",
++++++++ "path-is-absolute": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "js-yaml": {
++++++++ "version": "3.14.0",
++++++++ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz",
++++++++ "integrity": "sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "argparse": "^1.0.7",
++++++++ "esprima": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ },
++++++++ "wrap-ansi": {
++++++++ "version": "5.1.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz",
++++++++ "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.0",
++++++++ "string-width": "^3.0.0",
++++++++ "strip-ansi": "^5.0.0"
++++++++ }
++++++++ },
++++++++ "yargs": {
++++++++ "version": "13.3.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-13.3.2.tgz",
++++++++ "integrity": "sha512-AX3Zw5iPruN5ie6xGRIDgqkT+ZhnRlZMLMHAs8tg7nRruy2Nb+i5o9bwghAogtM08q1dpr2LVoS8KSTMYpWXUw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^13.1.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "13.1.2",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-13.1.2.tgz",
++++++++ "integrity": "sha512-3lbsNRf/j+A4QuSZfDRA7HRSfWrzO0YjqTJd5kjAq37Zep1CEgaYmrH9Q3GwPiB9cHyd1Y1UwggGhJGoxipbzg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "ms": {
++++++++ "version": "2.1.2",
++++++++ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
++++++++ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
++++++++ },
++++++++ "normalize-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
++++++++ "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==",
++++++++ "dev": true
++++++++ },
++++++++ "object-inspect": {
++++++++ "version": "1.8.0",
++++++++ "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.8.0.tgz",
++++++++ "integrity": "sha512-jLdtEOB112fORuypAyl/50VRVIBIdVQOSUUGQHzJ4xBSbit81zRarz7GThkEFZy1RceYrWYcPcBFPQwHyAc1gA=="
++++++++ },
++++++++ "object-keys": {
++++++++ "version": "1.1.1",
++++++++ "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz",
++++++++ "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
++++++++ },
++++++++ "object.assign": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/object.assign/-/object.assign-4.1.0.tgz",
++++++++ "integrity": "sha512-exHJeq6kBKj58mqGyTQ9DFvrZC/eR6OwxzoM9YRoGBqrXYonaFyGiFMuc9VZrXf7DarreEwMpurG3dd+CNyW5w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "define-properties": "^1.1.2",
++++++++ "function-bind": "^1.1.1",
++++++++ "has-symbols": "^1.0.0",
++++++++ "object-keys": "^1.0.11"
++++++++ }
++++++++ },
++++++++ "once": {
++++++++ "version": "1.4.0",
++++++++ "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
++++++++ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "wrappy": "1"
++++++++ }
++++++++ },
++++++++ "p-limit": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz",
++++++++ "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz",
++++++++ "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^3.0.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "p-limit": {
++++++++ "version": "3.0.2",
++++++++ "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.0.2.tgz",
++++++++ "integrity": "sha512-iwqZSOoWIW+Ew4kAGUlN16J4M7OB3ysMLSZtnhmqx7njIHFPlxWBX8xo3lVTyFVq6mI/lL9qt2IsN1sHwaxJkg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-try": "^2.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "p-try": {
++++++++ "version": "2.2.0",
++++++++ "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
++++++++ "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
++++++++ "dev": true
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
++++++++ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
++++++++ "dev": true
++++++++ },
++++++++ "path-is-absolute": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
++++++++ "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
++++++++ "dev": true
++++++++ },
++++++++ "path-parse": {
++++++++ "version": "1.0.6",
++++++++ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
++++++++ "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==",
++++++++ "dev": true
++++++++ },
++++++++ "picomatch": {
++++++++ "version": "2.2.2",
++++++++ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz",
++++++++ "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==",
++++++++ "dev": true
++++++++ },
++++++++ "promise.allsettled": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/promise.allsettled/-/promise.allsettled-1.0.2.tgz",
++++++++ "integrity": "sha512-UpcYW5S1RaNKT6pd+s9jp9K9rlQge1UXKskec0j6Mmuq7UJCvlS2J2/s/yuPN8ehftf9HXMxWlKiPbGGUzpoRg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "array.prototype.map": "^1.0.1",
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.0-next.1",
++++++++ "function-bind": "^1.1.1",
++++++++ "iterate-value": "^1.0.0"
++++++++ }
++++++++ },
++++++++ "randombytes": {
++++++++ "version": "2.1.0",
++++++++ "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz",
++++++++ "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "safe-buffer": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "readdirp": {
++++++++ "version": "3.4.0",
++++++++ "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.4.0.tgz",
++++++++ "integrity": "sha512-0xe001vZBnJEK+uKcj8qOhyAKPzIT+gStxWr3LCB0DwcXR5NZJ3IaC+yGnHCYzB/S7ov3m3EEbZI2zeNvX+hGQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "picomatch": "^2.2.1"
++++++++ }
++++++++ },
++++++++ "require-directory": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
++++++++ "integrity": "sha1-jGStX9MNqxyXbiNE/+f3kqam30I=",
++++++++ "dev": true
++++++++ },
++++++++ "require-main-filename": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz",
++++++++ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==",
++++++++ "dev": true
++++++++ },
++++++++ "resolve": {
++++++++ "version": "1.17.0",
++++++++ "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz",
++++++++ "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "path-parse": "^1.0.6"
++++++++ }
++++++++ },
++++++++ "safe-buffer": {
++++++++ "version": "5.2.1",
++++++++ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
++++++++ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
++++++++ "dev": true
++++++++ },
++++++++ "semver": {
++++++++ "version": "5.7.1",
++++++++ "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
++++++++ "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
++++++++ "dev": true
++++++++ },
++++++++ "serialize-javascript": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-4.0.0.tgz",
++++++++ "integrity": "sha512-GaNA54380uFefWghODBWEGisLZFj00nS5ACs6yHa9nLqlLpVLO8ChDGeKRjZnV4Nh4n0Qi7nhYZD/9fCPzEqkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "randombytes": "^2.1.0"
++++++++ }
++++++++ },
++++++++ "set-blocking": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/set-blocking/-/set-blocking-2.0.0.tgz",
++++++++ "integrity": "sha1-BF+XgtARrppoA93TgrJDkrPYkPc=",
++++++++ "dev": true
++++++++ },
++++++++ "source-map": {
++++++++ "version": "0.6.1",
++++++++ "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
++++++++ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
++++++++ "dev": true
++++++++ },
++++++++ "source-map-support": {
++++++++ "version": "0.5.19",
++++++++ "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz",
++++++++ "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "buffer-from": "^1.0.0",
++++++++ "source-map": "^0.6.0"
++++++++ }
++++++++ },
++++++++ "sprintf-js": {
++++++++ "version": "1.0.3",
++++++++ "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz",
++++++++ "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "2.1.1",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz",
++++++++ "integrity": "sha512-nOqH59deCq9SRHlxq1Aw85Jnt4w6KvLKqWVik6oA9ZklXLNIOlqg4F2yrT1MVaTjAqvVwdfeZ7w7aCvJD7ugkw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimend": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimend/-/string.prototype.trimend-1.0.1.tgz",
++++++++ "integrity": "sha512-LRPxFUaTtpqYsTeNKaFOw3R4bxIzWOnbQ837QfBylo8jIxtcbK/A/sMV7Q+OAV/vWo+7s25pOE10KYSjaSO06g==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "string.prototype.trimstart": {
++++++++ "version": "1.0.1",
++++++++ "resolved": "https://registry.npmjs.org/string.prototype.trimstart/-/string.prototype.trimstart-1.0.1.tgz",
++++++++ "integrity": "sha512-XxZn+QpvrBI1FOcg6dIpxUPgWCPuNXvMD72aaRaUQv1eD4e/Qy8i/hFTe0BUmD60p/QA6bh1avmuPTfNjqVWRw==",
++++++++ "requires": {
++++++++ "define-properties": "^1.1.3",
++++++++ "es-abstract": "^1.17.5"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
++++++++ "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "strip-json-comments": {
++++++++ "version": "3.0.1",
++++++++ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.0.1.tgz",
++++++++ "integrity": "sha512-VTyMAUfdm047mwKl+u79WIdrZxtFtn+nBxHeb844XBQ9uMNTuTHdx2hc5RiAJYqwTj3wc/xe5HLSdJSkJ+WfZw==",
++++++++ "dev": true
++++++++ },
++++++++ "supports-color": {
++++++++ "version": "7.1.0",
++++++++ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz",
++++++++ "integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "has-flag": "^4.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "has-flag": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
++++++++ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "to-regex-range": {
++++++++ "version": "5.0.1",
++++++++ "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
++++++++ "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "is-number": "^7.0.0"
++++++++ }
++++++++ },
++++++++ "ts-node": {
++++++++ "version": "9.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-9.0.0.tgz",
++++++++ "integrity": "sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "arg": "^4.1.0",
++++++++ "diff": "^4.0.1",
++++++++ "make-error": "^1.1.1",
++++++++ "source-map-support": "^0.5.17",
++++++++ "yn": "3.1.1"
++++++++ }
++++++++ },
++++++++ "tslib": {
++++++++ "version": "1.14.1",
++++++++ "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
++++++++ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
++++++++ "dev": true
++++++++ },
++++++++ "tslint": {
++++++++ "version": "6.1.3",
++++++++ "resolved": "https://registry.npmjs.org/tslint/-/tslint-6.1.3.tgz",
++++++++ "integrity": "sha512-IbR4nkT96EQOvKE2PW/djGz8iGNeJ4rF2mBfiYaR/nvUWYKJhLwimoJKgjIFEIDibBtOevj7BqCRL4oHeWWUCg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@babel/code-frame": "^7.0.0",
++++++++ "builtin-modules": "^1.1.1",
++++++++ "chalk": "^2.3.0",
++++++++ "commander": "^2.12.1",
++++++++ "diff": "^4.0.1",
++++++++ "glob": "^7.1.1",
++++++++ "js-yaml": "^3.13.1",
++++++++ "minimatch": "^3.0.4",
++++++++ "mkdirp": "^0.5.3",
++++++++ "resolve": "^1.3.2",
++++++++ "semver": "^5.3.0",
++++++++ "tslib": "^1.13.0",
++++++++ "tsutils": "^2.29.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "diff": {
++++++++ "version": "4.0.2",
++++++++ "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
++++++++ "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++ },
++++++++ "tsutils": {
++++++++ "version": "2.29.0",
++++++++ "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz",
++++++++ "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "tslib": "^1.8.1"
++++++++ }
++++++++ },
++++++++ "typescript": {
++++++++ "version": "4.0.3",
++++++++ "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz",
++++++++ "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==",
++++++++ "dev": true
++++++++ },
++++++++ "which": {
++++++++ "version": "2.0.2",
++++++++ "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
++++++++ "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "isexe": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "which-module": {
++++++++ "version": "2.0.0",
++++++++ "resolved": "https://registry.npmjs.org/which-module/-/which-module-2.0.0.tgz",
++++++++ "integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=",
++++++++ "dev": true
++++++++ },
++++++++ "wide-align": {
++++++++ "version": "1.1.3",
++++++++ "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz",
++++++++ "integrity": "sha512-QGkOQc8XL6Bt5PwnsExKBPuMKBxnGxWWW3fU55Xt4feHozMUhdUMaBCk290qpm/wG5u/RSKzwdAC4i51YigihA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^1.0.2 || 2"
++++++++ }
++++++++ },
++++++++ "workerpool": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.0.0.tgz",
++++++++ "integrity": "sha512-fU2OcNA/GVAJLLyKUoHkAgIhKb0JoCpSjLC/G2vYKxUjVmQwGbRVeoPJ1a8U4pnVofz4AQV5Y/NEw8oKqxEBtA==",
++++++++ "dev": true
++++++++ },
++++++++ "wrap-ansi": {
++++++++ "version": "6.2.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz",
++++++++ "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^4.0.0",
++++++++ "string-width": "^4.1.0",
++++++++ "strip-ansi": "^6.0.0"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz",
++++++++ "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==",
++++++++ "dev": true
++++++++ },
++++++++ "ansi-styles": {
++++++++ "version": "4.2.1",
++++++++ "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.2.1.tgz",
++++++++ "integrity": "sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "@types/color-name": "^1.1.1",
++++++++ "color-convert": "^2.0.1"
++++++++ }
++++++++ },
++++++++ "color-convert": {
++++++++ "version": "2.0.1",
++++++++ "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
++++++++ "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "color-name": "~1.1.4"
++++++++ }
++++++++ },
++++++++ "color-name": {
++++++++ "version": "1.1.4",
++++++++ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
++++++++ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "8.0.0",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
++++++++ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
++++++++ "dev": true
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
++++++++ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz",
++++++++ "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^8.0.0",
++++++++ "is-fullwidth-code-point": "^3.0.0",
++++++++ "strip-ansi": "^6.0.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz",
++++++++ "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^5.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "wrappy": {
++++++++ "version": "1.0.2",
++++++++ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
++++++++ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
++++++++ "dev": true
++++++++ },
++++++++ "y18n": {
++++++++ "version": "4.0.1",
++++++++ "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.1.tgz",
++++++++ "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==",
++++++++ "dev": true
++++++++ },
++++++++ "yargs": {
++++++++ "version": "15.4.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz",
++++++++ "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^6.0.0",
++++++++ "decamelize": "^1.2.0",
++++++++ "find-up": "^4.1.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^4.2.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^18.1.2"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz",
++++++++ "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg==",
++++++++ "dev": true
++++++++ },
++++++++ "emoji-regex": {
++++++++ "version": "8.0.0",
++++++++ "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz",
++++++++ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==",
++++++++ "dev": true
++++++++ },
++++++++ "find-up": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
++++++++ "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^5.0.0",
++++++++ "path-exists": "^4.0.0"
++++++++ }
++++++++ },
++++++++ "is-fullwidth-code-point": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
++++++++ "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==",
++++++++ "dev": true
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz",
++++++++ "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^4.1.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
++++++++ "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.2.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "4.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
++++++++ "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "4.2.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.0.tgz",
++++++++ "integrity": "sha512-zUz5JD+tgqtuDjMhwIg5uFVV3dtqZ9yQJlZVfq4I01/K5Paj5UHj7VyrQOJvzawSVlKpObApbfD0Ed6yJc+1eg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^8.0.0",
++++++++ "is-fullwidth-code-point": "^3.0.0",
++++++++ "strip-ansi": "^6.0.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "6.0.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz",
++++++++ "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^5.0.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "18.1.3",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz",
++++++++ "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ },
++++++++ "yargs-unparser": {
++++++++ "version": "1.6.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-1.6.1.tgz",
++++++++ "integrity": "sha512-qZV14lK9MWsGCmcr7u5oXGH0dbGqZAIxTDrWXZDo5zUr6b6iUmelNKO6x6R1dQT24AH3LgRxJpr8meWy2unolA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.3.1",
++++++++ "decamelize": "^1.2.0",
++++++++ "flat": "^4.1.0",
++++++++ "is-plain-obj": "^1.1.0",
++++++++ "yargs": "^14.2.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "ansi-regex": {
++++++++ "version": "4.1.0",
++++++++ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-4.1.0.tgz",
++++++++ "integrity": "sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg==",
++++++++ "dev": true
++++++++ },
++++++++ "cliui": {
++++++++ "version": "5.0.0",
++++++++ "resolved": "https://registry.npmjs.org/cliui/-/cliui-5.0.0.tgz",
++++++++ "integrity": "sha512-PYeGSEmmHM6zvoef2w8TPzlrnNpXIjTipYK780YswmIP9vjxmd6Y2a3CB2Ks6/AU8NHjZugXvo8w3oWM2qnwXA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "string-width": "^3.1.0",
++++++++ "strip-ansi": "^5.2.0",
++++++++ "wrap-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "find-up": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/find-up/-/find-up-3.0.0.tgz",
++++++++ "integrity": "sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "locate-path": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "locate-path": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
++++++++ "integrity": "sha512-7AO748wWnIhNqAuaty2ZWHkQHRSNfPVIsPIfwEOWO22AmaoVrWavlOcMR5nzTLNYvp36X220/maaRsrec1G65A==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-locate": "^3.0.0",
++++++++ "path-exists": "^3.0.0"
++++++++ }
++++++++ },
++++++++ "p-locate": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-3.0.0.tgz",
++++++++ "integrity": "sha512-x+12w/To+4GFfgJhBEpiDcLozRJGegY+Ei7/z0tSLkMmxGZNybVMSfWj9aJn8Z5Fc7dBUNJOOVgPv2H7IwulSQ==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "p-limit": "^2.0.0"
++++++++ }
++++++++ },
++++++++ "path-exists": {
++++++++ "version": "3.0.0",
++++++++ "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz",
++++++++ "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=",
++++++++ "dev": true
++++++++ },
++++++++ "string-width": {
++++++++ "version": "3.1.0",
++++++++ "resolved": "https://registry.npmjs.org/string-width/-/string-width-3.1.0.tgz",
++++++++ "integrity": "sha512-vafcv6KjVZKSgz06oM/H6GDBrAtz8vdhQakGjFIvNrHA6y3HCF1CInLy+QLq8dTJPQ1b+KDUqDFctkdRW44e1w==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "emoji-regex": "^7.0.1",
++++++++ "is-fullwidth-code-point": "^2.0.0",
++++++++ "strip-ansi": "^5.1.0"
++++++++ }
++++++++ },
++++++++ "strip-ansi": {
++++++++ "version": "5.2.0",
++++++++ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz",
++++++++ "integrity": "sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-regex": "^4.1.0"
++++++++ }
++++++++ },
++++++++ "wrap-ansi": {
++++++++ "version": "5.1.0",
++++++++ "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-5.1.0.tgz",
++++++++ "integrity": "sha512-QC1/iN/2/RPVJ5jYK8BGttj5z83LmSKmvbvrXPNCLZSEb32KKVDJDl/MOt2N01qU2H/FkzEa9PKto1BqDjtd7Q==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "ansi-styles": "^3.2.0",
++++++++ "string-width": "^3.0.0",
++++++++ "strip-ansi": "^5.0.0"
++++++++ }
++++++++ },
++++++++ "yargs": {
++++++++ "version": "14.2.3",
++++++++ "resolved": "https://registry.npmjs.org/yargs/-/yargs-14.2.3.tgz",
++++++++ "integrity": "sha512-ZbotRWhF+lkjijC/VhmOT9wSgyBQ7+zr13+YLkhfsSiTriYsMzkTUFP18pFhWwBeMa5gUc1MzbhrO6/VB7c9Xg==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "cliui": "^5.0.0",
++++++++ "decamelize": "^1.2.0",
++++++++ "find-up": "^3.0.0",
++++++++ "get-caller-file": "^2.0.1",
++++++++ "require-directory": "^2.1.1",
++++++++ "require-main-filename": "^2.0.0",
++++++++ "set-blocking": "^2.0.0",
++++++++ "string-width": "^3.0.0",
++++++++ "which-module": "^2.0.0",
++++++++ "y18n": "^4.0.0",
++++++++ "yargs-parser": "^15.0.1"
++++++++ }
++++++++ },
++++++++ "yargs-parser": {
++++++++ "version": "15.0.1",
++++++++ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-15.0.1.tgz",
++++++++ "integrity": "sha512-0OAMV2mAZQrs3FkNpDQcBk1x5HXb8X4twADss4S0Iuk+2dGnLOE/fRHrsYm542GduMveyA77OF4wrNJuanRCWw==",
++++++++ "dev": true,
++++++++ "requires": {
++++++++ "camelcase": "^5.0.0",
++++++++ "decamelize": "^1.2.0"
++++++++ }
++++++++ }
++++++++ }
++++++++ },
++++++++ "yn": {
++++++++ "version": "3.1.1",
++++++++ "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz",
++++++++ "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==",
++++++++ "dev": true
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "name": "llparse",
++++++++ "version": "7.1.1",
++++++++ "description": "Compile incremental parsers to C code",
++++++++ "main": "lib/api.js",
++++++++ "types": "lib/api.d.ts",
++++++++ "files": [
++++++++ "lib",
++++++++ "src"
++++++++ ],
++++++++ "scripts": {
++++++++ "build": "tsc",
++++++++ "clean": "rm -rf lib",
++++++++ "prepare": "npm run clean && npm run build",
++++++++ "lint": "tslint -c tslint.json src/**/*.ts test/**/*.ts",
++++++++ "fix-lint": "npm run lint -- --fix",
++++++++ "mocha": "mocha --timeout=10000 -r ts-node/register/type-check --reporter spec test/*-test.ts",
++++++++ "test": "npm run mocha && npm run lint"
++++++++ },
++++++++ "repository": {
++++++++ "type": "git",
++++++++ "url": "git+ssh://git@github.com/nodejs/llparse.git"
++++++++ },
++++++++ "keywords": [
++++++++ "llparse",
++++++++ "compiler"
++++++++ ],
++++++++ "author": "Fedor Indutny <fedor@indutny.com> (http://darksi.de/)",
++++++++ "license": "MIT",
++++++++ "bugs": {
++++++++ "url": "https://github.com/nodejs/llparse/issues"
++++++++ },
++++++++ "homepage": "https://github.com/nodejs/llparse#readme",
++++++++ "devDependencies": {
++++++++ "@types/debug": "^4.1.5",
++++++++ "@types/mocha": "^8.0.3",
++++++++ "@types/node": "^14.11.8",
++++++++ "esm": "^3.2.25",
++++++++ "llparse-test-fixture": "^5.0.1",
++++++++ "mocha": "^8.1.3",
++++++++ "ts-node": "^9.0.0",
++++++++ "tslint": "^6.1.3",
++++++++ "typescript": "^4.0.3"
++++++++ },
++++++++ "dependencies": {
++++++++ "debug": "^4.2.0",
++++++++ "llparse-frontend": "^3.0.0"
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import source = frontend.source;
++++++++
++++++++import { Compiler, ICompilerOptions, ICompilerResult } from './compiler';
++++++++
++++++++export { source, ICompilerOptions, ICompilerResult };
++++++++
++++++++// TODO(indutny): API for disabling/short-circuiting spans
++++++++
++++++++/**
++++++++ * LLParse graph builder and compiler.
++++++++ */
++++++++export class LLParse extends source.Builder {
++++++++ /**
++++++++ * The prefix controls the names of methods and state struct in generated
++++++++ * public C headers:
++++++++ *
++++++++ * ```c
++++++++ * // state struct
++++++++ * struct PREFIX_t {
++++++++ * ...
++++++++ * }
++++++++ *
++++++++ * int PREFIX_init(PREFIX_t* state);
++++++++ * int PREFIX_execute(PREFIX_t* state, const char* p, const char* endp);
++++++++ * ```
++++++++ *
++++++++ * @param prefix Prefix to be used when generating public API.
++++++++ */
++++++++ constructor(private readonly prefix: string = 'llparse') {
++++++++ super();
++++++++ }
++++++++
++++++++ /**
++++++++ * Compile LLParse graph to the C code and C headers
++++++++ *
++++++++ * @param root Root node of the parse graph (see `.node()`)
++++++++ * @param options Compiler options.
++++++++ */
++++++++ public build(root: source.node.Node, options: ICompilerOptions = {})
++++++++ : ICompilerResult {
++++++++ const c = new Compiler(this.prefix, options);
++++++++
++++++++ return c.compile(root, this.properties);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++import source = frontend.source;
++++++++
++++++++export interface IHeaderBuilderOptions {
++++++++ readonly prefix: string;
++++++++ readonly headerGuard?: string;
++++++++ readonly properties: ReadonlyArray<source.Property>;
++++++++ readonly spans: ReadonlyArray<frontend.SpanField>;
++++++++}
++++++++
++++++++export class HeaderBuilder {
++++++++ public build(options: IHeaderBuilderOptions): string {
++++++++ let res = '';
++++++++ const PREFIX = options.prefix.toUpperCase().replace(/[^a-z]/gi, '_');
++++++++ const DEFINE = options.headerGuard === undefined ?
++++++++ `INCLUDE_${PREFIX}_H_` : options.headerGuard;
++++++++
++++++++ res += `#ifndef ${DEFINE}\n`;
++++++++ res += `#define ${DEFINE}\n`;
++++++++ res += '#ifdef __cplusplus\n';
++++++++ res += 'extern "C" {\n';
++++++++ res += '#endif\n';
++++++++ res += '\n';
++++++++
++++++++ res += '#include <stdint.h>\n';
++++++++ res += '\n';
++++++++
++++++++ // Structure
++++++++ res += `typedef struct ${options.prefix}_s ${options.prefix}_t;\n`;
++++++++ res += `struct ${options.prefix}_s {\n`;
++++++++ res += ' int32_t _index;\n';
++++++++
++++++++ for (const [ index, field ] of options.spans.entries()) {
++++++++ res += ` void* _span_pos${index};\n`;
++++++++ if (field.callbacks.length > 1) {
++++++++ res += ` void* _span_cb${index};\n`;
++++++++ }
++++++++ }
++++++++
++++++++ res += ' int32_t error;\n';
++++++++ res += ' const char* reason;\n';
++++++++ res += ' const char* error_pos;\n';
++++++++ res += ' void* data;\n';
++++++++ res += ' void* _current;\n';
++++++++
++++++++ for (const prop of options.properties) {
++++++++ let ty: string;
++++++++ if (prop.ty === 'i8') {
++++++++ ty = 'uint8_t';
++++++++ } else if (prop.ty === 'i16') {
++++++++ ty = 'uint16_t';
++++++++ } else if (prop.ty === 'i32') {
++++++++ ty = 'uint32_t';
++++++++ } else if (prop.ty === 'i64') {
++++++++ ty = 'uint64_t';
++++++++ } else if (prop.ty === 'ptr') {
++++++++ ty = 'void*';
++++++++ } else {
++++++++ throw new Error(
++++++++ `Unknown state property type: "${prop.ty}"`);
++++++++ }
++++++++ res += ` ${ty} ${prop.name};\n`;
++++++++ }
++++++++ res += '};\n';
++++++++
++++++++ res += '\n';
++++++++
++++++++ res += `int ${options.prefix}_init(${options.prefix}_t* s);\n`;
++++++++ res += `int ${options.prefix}_execute(${options.prefix}_t* s, ` +
++++++++ 'const char* p, const char* endp);\n';
++++++++
++++++++ res += '\n';
++++++++ res += '#ifdef __cplusplus\n';
++++++++ res += '} /* extern "C" *\/\n';
++++++++ res += '#endif\n';
++++++++ res += `#endif /* ${DEFINE} *\/\n`;
++++++++
++++++++ return res;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as debugAPI from 'debug';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import source = frontend.source;
++++++++
++++++++import * as cImpl from '../implementation/c';
++++++++import { HeaderBuilder } from './header-builder';
++++++++
++++++++const debug = debugAPI('llparse:compiler');
++++++++
++++++++export interface ICompilerOptions {
++++++++ /**
++++++++ * Debug method name
++++++++ *
++++++++ * The method must have following signature:
++++++++ *
++++++++ * ```c
++++++++ * void debug(llparse_t* state, const char* p, const char* endp,
++++++++ * const char* msg);
++++++++ * ```
++++++++ *
++++++++ * Where `llparse_t` is a parser state type.
++++++++ */
++++++++ readonly debug?: string;
++++++++
++++++++ /**
++++++++ * What guard define to use in `#ifndef` in C headers.
++++++++ *
++++++++ * Default value: `prefix` argument
++++++++ */
++++++++ readonly headerGuard?: string;
++++++++
++++++++ /** Optional frontend configuration */
++++++++ readonly frontend?: frontend.IFrontendLazyOptions;
++++++++
++++++++ /** Optional C-backend configuration */
++++++++ readonly c?: cImpl.ICPublicOptions;
++++++++}
++++++++
++++++++export interface ICompilerResult {
++++++++ /**
++++++++ * Textual C code
++++++++ */
++++++++ readonly c: string;
++++++++
++++++++ /**
++++++++ * Textual C header file
++++++++ */
++++++++ readonly header: string;
++++++++}
++++++++
++++++++export class Compiler {
++++++++ constructor(public readonly prefix: string,
++++++++ public readonly options: ICompilerOptions) {
++++++++ }
++++++++
++++++++ public compile(root: source.node.Node,
++++++++ properties: ReadonlyArray<source.Property>): ICompilerResult {
++++++++ debug('Combining implementations');
++++++++ const container = new frontend.Container();
++++++++
++++++++ const c = new cImpl.CCompiler(container, Object.assign({
++++++++ debug: this.options.debug,
++++++++ }, this.options.c));
++++++++
++++++++ debug('Running frontend pass');
++++++++ const f = new frontend.Frontend(this.prefix,
++++++++ container.build(),
++++++++ this.options.frontend);
++++++++ const info = f.compile(root, properties);
++++++++
++++++++ debug('Building header');
++++++++ const hb = new HeaderBuilder();
++++++++
++++++++ const header = hb.build({
++++++++ headerGuard: this.options.headerGuard,
++++++++ prefix: this.prefix,
++++++++ properties,
++++++++ spans: info.spans,
++++++++ });
++++++++
++++++++ debug('Building C');
++++++++ return {
++++++++ header,
++++++++ c: c.compile(info),
++++++++ };
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class And extends Field<frontend.code.And> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`${this.field(ctx)} &= ${this.ref.value};`);
++++++++ out.push('return 0;');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++
++++++++export abstract class Code<T extends frontend.code.Code> {
++++++++ protected cachedDecl: string | undefined;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(ctx: Compilation, out: string[]): void;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Code } from './base';
++++++++
++++++++export abstract class External<T extends frontend.code.External>
++++++++ extends Code<T> {
++++++++
++++++++ public build(ctx: Compilation, out: string[]): void {
++++++++ out.push(`int ${this.ref.name}(`);
++++++++ out.push(` ${ctx.prefix}_t* s, const unsigned char* p,`);
++++++++ if (this.ref.signature === 'value') {
++++++++ out.push(' const unsigned char* endp,');
++++++++ out.push(' int value);');
++++++++ } else {
++++++++ out.push(' const unsigned char* endp);');
++++++++ }
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Code } from './base';
++++++++
++++++++export abstract class Field<T extends frontend.code.Field> extends Code<T> {
++++++++ public build(ctx: Compilation, out: string[]): void {
++++++++ out.push(`int ${this.ref.name}(`);
++++++++ out.push(` ${ctx.prefix}_t* ${ctx.stateArg()},`);
++++++++ out.push(` const unsigned char* ${ctx.posArg()},`);
++++++++ if (this.ref.signature === 'value') {
++++++++ out.push(` const unsigned char* ${ctx.endPosArg()},`);
++++++++ out.push(` int ${ctx.matchVar()}) {`);
++++++++ } else {
++++++++ out.push(` const unsigned char* ${ctx.endPosArg()}) {`);
++++++++ }
++++++++ const tmp: string[] = [];
++++++++ this.doBuild(ctx, tmp);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push('}');
++++++++ }
++++++++
++++++++ protected abstract doBuild(ctx: Compilation, out: string[]): void;
++++++++
++++++++ protected field(ctx: Compilation): string {
++++++++ return `${ctx.stateArg()}->${this.ref.field}`;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { And } from './and';
++++++++import { External } from './external';
++++++++import { IsEqual } from './is-equal';
++++++++import { Load } from './load';
++++++++import { MulAdd } from './mul-add';
++++++++import { Or } from './or';
++++++++import { Store } from './store';
++++++++import { Test } from './test';
++++++++import { Update } from './update';
++++++++
++++++++export * from './base';
++++++++
++++++++export default {
++++++++ And,
++++++++ IsEqual,
++++++++ Load,
++++++++ Match: class Match extends External<frontend.code.External> {},
++++++++ MulAdd,
++++++++ Or,
++++++++ Span: class Span extends External<frontend.code.Span> {},
++++++++ Store,
++++++++ Test,
++++++++ Update,
++++++++ Value: class Value extends External<frontend.code.Value> {},
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class IsEqual extends Field<frontend.code.IsEqual> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`return ${this.field(ctx)} == ${this.ref.value};`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class Load extends Field<frontend.code.Load> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`return ${this.field(ctx)};`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { SIGNED_LIMITS, UNSIGNED_LIMITS, SIGNED_TYPES } from '../constants';
++++++++import { Field } from './field';
++++++++
++++++++export class MulAdd extends Field<frontend.code.MulAdd> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ const options = this.ref.options;
++++++++ const ty = ctx.getFieldType(this.ref.field);
++++++++
++++++++ let field = this.field(ctx);
++++++++ if (options.signed) {
++++++++ assert(SIGNED_TYPES.has(ty), `Unexpected mulAdd type "${ty}"`);
++++++++ const targetTy = SIGNED_TYPES.get(ty)!;
++++++++ out.push(`${targetTy}* field = (${targetTy}*) &${field};`);
++++++++ field = '(*field)';
++++++++ }
++++++++
++++++++ const match = ctx.matchVar();
++++++++
++++++++ const limits = options.signed ? SIGNED_LIMITS : UNSIGNED_LIMITS;
++++++++ assert(limits.has(ty), `Unexpected mulAdd type "${ty}"`);
++++++++ const [ min, max ] = limits.get(ty)!;
++++++++
++++++++ const mulMax = `${max} / ${options.base}`;
++++++++ const mulMin = `${min} / ${options.base}`;
++++++++
++++++++ out.push('/* Multiplication overflow */');
++++++++ out.push(`if (${field} > ${mulMax}) {`);
++++++++ out.push(' return 1;');
++++++++ out.push('}');
++++++++ if (options.signed) {
++++++++ out.push(`if (${field} < ${mulMin}) {`);
++++++++ out.push(' return 1;');
++++++++ out.push('}');
++++++++ }
++++++++ out.push('');
++++++++
++++++++ out.push(`${field} *= ${options.base};`);
++++++++ out.push('');
++++++++
++++++++ out.push('/* Addition overflow */');
++++++++ out.push(`if (${match} >= 0) {`);
++++++++ out.push(` if (${field} > ${max} - ${match}) {`);
++++++++ out.push(' return 1;');
++++++++ out.push(' }');
++++++++ out.push('} else {');
++++++++ out.push(` if (${field} < ${min} - ${match}) {`);
++++++++ out.push(' return 1;');
++++++++ out.push(' }');
++++++++ out.push('}');
++++++++
++++++++ out.push(`${field} += ${match};`);
++++++++
++++++++ if (options.max !== undefined) {
++++++++ out.push('');
++++++++ out.push('/* Enforce maximum */');
++++++++ out.push(`if (${field} > ${options.max}) {`);
++++++++ out.push(' return 1;');
++++++++ out.push('}');
++++++++ }
++++++++
++++++++ out.push('return 0;');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class Or extends Field<frontend.code.Or> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`${this.field(ctx)} |= ${this.ref.value};`);
++++++++ out.push('return 0;');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class Store extends Field<frontend.code.Store> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`${this.field(ctx)} = ${ctx.matchVar()};`);
++++++++ out.push('return 0;');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class Test extends Field<frontend.code.Test> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ const value = this.ref.value;
++++++++ out.push(`return (${this.field(ctx)} & ${value}) == ${value};`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Field } from './field';
++++++++
++++++++export class Update extends Field<frontend.code.Update> {
++++++++ protected doBuild(ctx: Compilation, out: string[]): void {
++++++++ out.push(`${this.field(ctx)} = ${this.ref.value};`);
++++++++ out.push('return 0;');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import {
++++++++ CONTAINER_KEY, STATE_ERROR,
++++++++ ARG_STATE, ARG_POS, ARG_ENDPOS,
++++++++ VAR_MATCH,
++++++++ STATE_PREFIX, LABEL_PREFIX, BLOB_PREFIX,
++++++++ SEQUENCE_COMPLETE, SEQUENCE_MISMATCH, SEQUENCE_PAUSE,
++++++++} from './constants';
++++++++import { Code } from './code';
++++++++import { Node } from './node';
++++++++import { Transform } from './transform';
++++++++import { MatchSequence } from './helpers/match-sequence';
++++++++
++++++++// Number of hex words per line of blob declaration
++++++++const BLOB_GROUP_SIZE = 11;
++++++++
++++++++type WrappedNode = frontend.IWrap<frontend.node.Node>;
++++++++
++++++++interface IBlob {
++++++++ readonly alignment: number | undefined;
++++++++ readonly buffer: Buffer;
++++++++ readonly name: string;
++++++++}
++++++++
++++++++// TODO(indutny): deduplicate
++++++++export interface ICompilationOptions {
++++++++ readonly debug?: string;
++++++++}
++++++++
++++++++// TODO(indutny): deduplicate
++++++++export interface ICompilationProperty {
++++++++ readonly name: string;
++++++++ readonly ty: string;
++++++++}
++++++++
++++++++export class Compilation {
++++++++ private readonly stateMap: Map<string, ReadonlyArray<string>> = new Map();
++++++++ private readonly blobs: Map<Buffer, IBlob> = new Map();
++++++++ private readonly codeMap: Map<string, Code<frontend.code.Code>> = new Map();
++++++++ private readonly matchSequence:
++++++++ Map<string, MatchSequence> = new Map();
++++++++ private readonly resumptionTargets: Set<string> = new Set();
++++++++
++++++++ constructor(public readonly prefix: string,
++++++++ private readonly properties: ReadonlyArray<ICompilationProperty>,
++++++++ resumptionTargets: ReadonlySet<WrappedNode>,
++++++++ private readonly options: ICompilationOptions) {
++++++++ for (const node of resumptionTargets) {
++++++++ this.resumptionTargets.add(STATE_PREFIX + node.ref.id.name);
++++++++ }
++++++++ }
++++++++
++++++++ private buildStateEnum(out: string[]): void {
++++++++ out.push('enum llparse_state_e {');
++++++++ out.push(` ${STATE_ERROR},`);
++++++++ for (const stateName of this.stateMap.keys()) {
++++++++ if (this.resumptionTargets.has(stateName)) {
++++++++ out.push(` ${stateName},`);
++++++++ }
++++++++ }
++++++++ out.push('};');
++++++++ out.push('typedef enum llparse_state_e llparse_state_t;');
++++++++ }
++++++++
++++++++ private buildBlobs(out: string[]): void {
++++++++ if (this.blobs.size === 0) {
++++++++ return;
++++++++ }
++++++++
++++++++ for (const blob of this.blobs.values()) {
++++++++ const buffer = blob.buffer;
++++++++ let align = '';
++++++++ if (blob.alignment) {
++++++++ align = ` ALIGN(${blob.alignment})`;
++++++++ }
++++++++
++++++++ if (blob.alignment) {
++++++++ out.push('#ifdef __SSE4_2__');
++++++++ }
++++++++ out.push(`static const unsigned char${align} ${blob.name}[] = {`);
++++++++
++++++++ for (let i = 0; i < buffer.length; i += BLOB_GROUP_SIZE) {
++++++++ const limit = Math.min(buffer.length, i + BLOB_GROUP_SIZE);
++++++++ const hex: string[] = [];
++++++++ for (let j = i; j < limit; j++) {
++++++++ const value = buffer[j] as number;
++++++++
++++++++ const ch = String.fromCharCode(value);
++++++++ // `'`, `\`
++++++++ if (value === 0x27 || value === 0x5c) {
++++++++ hex.push(`'\\${ch}'`);
++++++++ } else if (value >= 0x20 && value <= 0x7e) {
++++++++ hex.push(`'${ch}'`);
++++++++ } else {
++++++++ hex.push(`0x${value.toString(16)}`);
++++++++ }
++++++++ }
++++++++ let line = ' ' + hex.join(', ');
++++++++ if (limit !== buffer.length) {
++++++++ line += ',';
++++++++ }
++++++++ out.push(line);
++++++++ }
++++++++
++++++++ out.push(`};`);
++++++++ if (blob.alignment) {
++++++++ out.push('#endif /* __SSE4_2__ */');
++++++++ }
++++++++ }
++++++++ out.push('');
++++++++ }
++++++++
++++++++ private buildMatchSequence(out: string[]): void {
++++++++ if (this.matchSequence.size === 0) {
++++++++ return;
++++++++ }
++++++++
++++++++ MatchSequence.buildGlobals(out);
++++++++ out.push('');
++++++++
++++++++ for (const match of this.matchSequence.values()) {
++++++++ match.build(this, out);
++++++++ out.push('');
++++++++ }
++++++++ }
++++++++
++++++++ public reserveSpans(spans: ReadonlyArray<frontend.SpanField>): void {
++++++++ for (const span of spans) {
++++++++ for (const callback of span.callbacks) {
++++++++ this.buildCode(this.unwrapCode(callback));
++++++++ }
++++++++ }
++++++++ }
++++++++
++++++++ public debug(out: string[], message: string): void {
++++++++ if (this.options.debug === undefined) {
++++++++ return;
++++++++ }
++++++++
++++++++ const args = [
++++++++ this.stateArg(),
++++++++ `(const char*) ${this.posArg()}`,
++++++++ `(const char*) ${this.endPosArg()}`,
++++++++ ];
++++++++
++++++++ out.push(`${this.options.debug}(${args.join(', ')},`);
++++++++ out.push(` ${this.cstring(message)});`);
++++++++ }
++++++++
++++++++ public buildGlobals(out: string[]): void {
++++++++ if (this.options.debug !== undefined) {
++++++++ out.push(`void ${this.options.debug}(`);
++++++++ out.push(` ${this.prefix}_t* s, const char* p, const char* endp,`);
++++++++ out.push(' const char* msg);');
++++++++ }
++++++++
++++++++ this.buildBlobs(out);
++++++++ this.buildMatchSequence(out);
++++++++ this.buildStateEnum(out);
++++++++
++++++++ for (const code of this.codeMap.values()) {
++++++++ out.push('');
++++++++ code.build(this, out);
++++++++ }
++++++++ }
++++++++
++++++++ public buildResumptionStates(out: string[]): void {
++++++++ this.stateMap.forEach((lines, name) => {
++++++++ if (!this.resumptionTargets.has(name)) {
++++++++ return;
++++++++ }
++++++++ out.push(`case ${name}:`);
++++++++ out.push(`${LABEL_PREFIX}${name}: {`);
++++++++ lines.forEach((line) => out.push(` ${line}`));
++++++++ out.push(' /* UNREACHABLE */;');
++++++++ out.push(' abort();');
++++++++ out.push('}');
++++++++ });
++++++++ }
++++++++
++++++++ public buildInternalStates(out: string[]): void {
++++++++ this.stateMap.forEach((lines, name) => {
++++++++ if (this.resumptionTargets.has(name)) {
++++++++ return;
++++++++ }
++++++++ out.push(`${LABEL_PREFIX}${name}: {`);
++++++++ lines.forEach((line) => out.push(` ${line}`));
++++++++ out.push(' /* UNREACHABLE */;');
++++++++ out.push(' abort();');
++++++++ out.push('}');
++++++++ });
++++++++ }
++++++++
++++++++ public addState(state: string, lines: ReadonlyArray<string>): void {
++++++++ assert(!this.stateMap.has(state));
++++++++ this.stateMap.set(state, lines);
++++++++ }
++++++++
++++++++ public buildCode(code: Code<frontend.code.Code>): string {
++++++++ if (this.codeMap.has(code.ref.name)) {
++++++++ assert.strictEqual(this.codeMap.get(code.ref.name)!, code,
++++++++ `Code name conflict for "${code.ref.name}"`);
++++++++ } else {
++++++++ this.codeMap.set(code.ref.name, code);
++++++++ }
++++++++ return code.ref.name;
++++++++ }
++++++++
++++++++ public getFieldType(field: string): string {
++++++++ for (const property of this.properties) {
++++++++ if (property.name === field) {
++++++++ return property.ty;
++++++++ }
++++++++ }
++++++++ throw new Error(`Field "${field}" not found`);
++++++++ }
++++++++
++++++++ // Helpers
++++++++
++++++++ public unwrapCode(code: frontend.IWrap<frontend.code.Code>)
++++++++ : Code<frontend.code.Code> {
++++++++ const container = code as frontend.ContainerWrap<frontend.code.Code>;
++++++++ return container.get(CONTAINER_KEY);
++++++++ }
++++++++
++++++++ public unwrapNode(node: WrappedNode): Node<frontend.node.Node> {
++++++++ const container = node as frontend.ContainerWrap<frontend.node.Node>;
++++++++ return container.get(CONTAINER_KEY);
++++++++ }
++++++++
++++++++ public unwrapTransform(node: frontend.IWrap<frontend.transform.Transform>)
++++++++ : Transform<frontend.transform.Transform> {
++++++++ const container =
++++++++ node as frontend.ContainerWrap<frontend.transform.Transform>;
++++++++ return container.get(CONTAINER_KEY);
++++++++ }
++++++++
++++++++ public indent(out: string[], lines: ReadonlyArray<string>, pad: string) {
++++++++ for (const line of lines) {
++++++++ out.push(`${pad}${line}`);
++++++++ }
++++++++ }
++++++++
++++++++ // MatchSequence cache
++++++++
++++++++ public getMatchSequence(
++++++++ transform: frontend.IWrap<frontend.transform.Transform>, select: Buffer)
++++++++ : string {
++++++++ const wrap = this.unwrapTransform(transform);
++++++++
++++++++ let res: MatchSequence;
++++++++ if (this.matchSequence.has(wrap.ref.name)) {
++++++++ res = this.matchSequence.get(wrap.ref.name)!;
++++++++ } else {
++++++++ res = new MatchSequence(wrap);
++++++++ this.matchSequence.set(wrap.ref.name, res);
++++++++ }
++++++++
++++++++ return res.getName();
++++++++ }
++++++++
++++++++ // Arguments
++++++++
++++++++ public stateArg(): string {
++++++++ return ARG_STATE;
++++++++ }
++++++++
++++++++ public posArg(): string {
++++++++ return ARG_POS;
++++++++ }
++++++++
++++++++ public endPosArg(): string {
++++++++ return ARG_ENDPOS;
++++++++ }
++++++++
++++++++ public matchVar(): string {
++++++++ return VAR_MATCH;
++++++++ }
++++++++
++++++++ // State fields
++++++++
++++++++ public indexField(): string {
++++++++ return this.stateField('_index');
++++++++ }
++++++++
++++++++ public currentField(): string {
++++++++ return this.stateField('_current');
++++++++ }
++++++++
++++++++ public errorField(): string {
++++++++ return this.stateField('error');
++++++++ }
++++++++
++++++++ public reasonField(): string {
++++++++ return this.stateField('reason');
++++++++ }
++++++++
++++++++ public errorPosField(): string {
++++++++ return this.stateField('error_pos');
++++++++ }
++++++++
++++++++ public spanPosField(index: number): string {
++++++++ return this.stateField(`_span_pos${index}`);
++++++++ }
++++++++
++++++++ public spanCbField(index: number): string {
++++++++ return this.stateField(`_span_cb${index}`);
++++++++ }
++++++++
++++++++ public stateField(name: string): string {
++++++++ return `${this.stateArg()}->${name}`;
++++++++ }
++++++++
++++++++ // Globals
++++++++
++++++++ public cstring(value: string): string {
++++++++ return JSON.stringify(value);
++++++++ }
++++++++
++++++++ public blob(value: Buffer, alignment?: number): string {
++++++++ if (this.blobs.has(value)) {
++++++++ return this.blobs.get(value)!.name;
++++++++ }
++++++++
++++++++ const res = BLOB_PREFIX + this.blobs.size;
++++++++ this.blobs.set(value, {
++++++++ alignment,
++++++++ buffer: value,
++++++++ name: res,
++++++++ });
++++++++ return res;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++export const CONTAINER_KEY = 'c';
++++++++
++++++++export const LABEL_PREFIX = '';
++++++++export const STATE_PREFIX = 's_n_';
++++++++export const STATE_ERROR = 's_error';
++++++++
++++++++export const BLOB_PREFIX = 'llparse_blob';
++++++++
++++++++export const ARG_STATE = 'state';
++++++++export const ARG_POS = 'p';
++++++++export const ARG_ENDPOS = 'endp';
++++++++
++++++++export const VAR_MATCH = 'match';
++++++++
++++++++// MatchSequence
++++++++
++++++++export const SEQUENCE_COMPLETE = 'kMatchComplete';
++++++++export const SEQUENCE_MISMATCH = 'kMatchMismatch';
++++++++export const SEQUENCE_PAUSE = 'kMatchPause';
++++++++
++++++++export const SIGNED_LIMITS: Map<string, [ string, string ]> = new Map();
++++++++SIGNED_LIMITS.set('i8', [ '-0x80', '0x7f' ]);
++++++++SIGNED_LIMITS.set('i16', [ '-0x8000', '0x7fff' ]);
++++++++SIGNED_LIMITS.set('i32', [ '(-0x7fffffff - 1)', '0x7fffffff' ]);
++++++++SIGNED_LIMITS.set('i64', [ '(-0x7fffffffffffffffLL - 1)',
++++++++ '0x7fffffffffffffffLL' ]);
++++++++
++++++++export const UNSIGNED_LIMITS: Map<string, [ string, string ]> = new Map();
++++++++UNSIGNED_LIMITS.set('i8', [ '0', '0xff' ]);
++++++++UNSIGNED_LIMITS.set('i8', [ '0', '0xff' ]);
++++++++UNSIGNED_LIMITS.set('i16', [ '0', '0xffff' ]);
++++++++UNSIGNED_LIMITS.set('i32', [ '0', '0xffffffff' ]);
++++++++UNSIGNED_LIMITS.set('i64', [ '0ULL', '0xffffffffffffffffULL' ]);
++++++++
++++++++export const UNSIGNED_TYPES: Map<string, string> = new Map();
++++++++UNSIGNED_TYPES.set('i8', 'uint8_t');
++++++++UNSIGNED_TYPES.set('i16', 'uint16_t');
++++++++UNSIGNED_TYPES.set('i32', 'uint32_t');
++++++++UNSIGNED_TYPES.set('i64', 'uint64_t');
++++++++
++++++++export const SIGNED_TYPES: Map<string, string> = new Map();
++++++++SIGNED_TYPES.set('i8', 'int8_t');
++++++++SIGNED_TYPES.set('i16', 'int16_t');
++++++++SIGNED_TYPES.set('i32', 'int32_t');
++++++++SIGNED_TYPES.set('i64', 'int64_t');
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import { Buffer } from 'buffer';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import {
++++++++ SEQUENCE_COMPLETE, SEQUENCE_MISMATCH, SEQUENCE_PAUSE,
++++++++} from '../constants';
++++++++import { Transform } from '../transform';
++++++++import { Compilation } from '../compilation';
++++++++
++++++++type TransformWrap = Transform<frontend.transform.Transform>;
++++++++
++++++++export class MatchSequence {
++++++++ constructor(private readonly transform: TransformWrap) {
++++++++ }
++++++++
++++++++ public static buildGlobals(out: string[]): void {
++++++++ out.push('enum llparse_match_status_e {');
++++++++ out.push(` ${SEQUENCE_COMPLETE},`);
++++++++ out.push(` ${SEQUENCE_PAUSE},`);
++++++++ out.push(` ${SEQUENCE_MISMATCH}`);
++++++++ out.push('};');
++++++++ out.push('typedef enum llparse_match_status_e llparse_match_status_t;');
++++++++ out.push('');
++++++++ out.push('struct llparse_match_s {');
++++++++ out.push(' llparse_match_status_t status;');
++++++++ out.push(' const unsigned char* current;');
++++++++ out.push('};');
++++++++ out.push('typedef struct llparse_match_s llparse_match_t;');
++++++++ }
++++++++
++++++++ public getName(): string {
++++++++ return `llparse__match_sequence_${this.transform.ref.name}`;
++++++++ }
++++++++
++++++++ public build(ctx: Compilation, out: string[]): void {
++++++++ out.push(`static llparse_match_t ${this.getName()}(`);
++++++++ out.push(` ${ctx.prefix}_t* s, const unsigned char* p,`);
++++++++ out.push(' const unsigned char* endp,');
++++++++ out.push(' const unsigned char* seq, uint32_t seq_len) {');
++++++++
++++++++ // Vars
++++++++ out.push(' uint32_t index;');
++++++++ out.push(' llparse_match_t res;');
++++++++ out.push('');
++++++++
++++++++ // Body
++++++++ out.push(' index = s->_index;');
++++++++ out.push(' for (; p != endp; p++) {');
++++++++ out.push(' unsigned char current;');
++++++++ out.push('');
++++++++ out.push(` current = ${this.transform.build(ctx, '*p')};`);
++++++++ out.push(' if (current == seq[index]) {');
++++++++ out.push(' if (++index == seq_len) {');
++++++++ out.push(` res.status = ${SEQUENCE_COMPLETE};`);
++++++++ out.push(' goto reset;');
++++++++ out.push(' }');
++++++++ out.push(' } else {');
++++++++ out.push(` res.status = ${SEQUENCE_MISMATCH};`);
++++++++ out.push(' goto reset;');
++++++++ out.push(' }');
++++++++ out.push(' }');
++++++++
++++++++ out.push(' s->_index = index;');
++++++++ out.push(` res.status = ${SEQUENCE_PAUSE};`);
++++++++ out.push(' res.current = p;');
++++++++ out.push(' return res;');
++++++++
++++++++ out.push('reset:');
++++++++ out.push(' s->_index = 0;');
++++++++ out.push(' res.current = p;');
++++++++ out.push(' return res;');
++++++++ out.push('}');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import {
++++++++ ARG_STATE, ARG_POS, ARG_ENDPOS,
++++++++ STATE_ERROR,
++++++++ VAR_MATCH,
++++++++ CONTAINER_KEY,
++++++++} from './constants';
++++++++import { Compilation } from './compilation';
++++++++import code from './code';
++++++++import node from './node';
++++++++import { Node } from './node';
++++++++import transform from './transform';
++++++++
++++++++export interface ICCompilerOptions {
++++++++ readonly debug?: string;
++++++++ readonly header?: string;
++++++++}
++++++++
++++++++export interface ICPublicOptions {
++++++++ readonly header?: string;
++++++++}
++++++++
++++++++export class CCompiler {
++++++++ constructor(container: frontend.Container,
++++++++ public readonly options: ICCompilerOptions) {
++++++++ container.add(CONTAINER_KEY, { code, node, transform });
++++++++ }
++++++++
++++++++ public compile(info: frontend.IFrontendResult): string {
++++++++ const compilation = new Compilation(info.prefix, info.properties,
++++++++ info.resumptionTargets, this.options);
++++++++ const out: string[] = [];
++++++++
++++++++ out.push('#include <stdlib.h>');
++++++++ out.push('#include <stdint.h>');
++++++++ out.push('#include <string.h>');
++++++++ out.push('');
++++++++
++++++++ // NOTE: Inspired by https://github.com/h2o/picohttpparser
++++++++ // TODO(indutny): Windows support for SSE4.2.
++++++++ // See: https://github.com/nodejs/llparse/pull/24#discussion_r299789676
++++++++ // (There is no `__SSE4_2__` define for MSVC)
++++++++ out.push('#ifdef __SSE4_2__');
++++++++ out.push(' #ifdef _MSC_VER');
++++++++ out.push(' #include <nmmintrin.h>');
++++++++ out.push(' #else /* !_MSC_VER */');
++++++++ out.push(' #include <x86intrin.h>');
++++++++ out.push(' #endif /* _MSC_VER */');
++++++++ out.push('#endif /* __SSE4_2__ */');
++++++++ out.push('');
++++++++
++++++++ out.push('#ifdef _MSC_VER');
++++++++ out.push(' #define ALIGN(n) _declspec(align(n))');
++++++++ out.push('#else /* !_MSC_VER */');
++++++++ out.push(' #define ALIGN(n) __attribute__((aligned(n)))');
++++++++ out.push('#endif /* _MSC_VER */');
++++++++
++++++++ out.push('');
++++++++ out.push(`#include "${this.options.header || info.prefix}.h"`);
++++++++ out.push(``);
++++++++ out.push(`typedef int (*${info.prefix}__span_cb)(`);
++++++++ out.push(` ${info.prefix}_t*, const char*, const char*);`);
++++++++ out.push('');
++++++++
++++++++ // Queue span callbacks to be built before `executeSpans()` code gets called
++++++++ // below.
++++++++ compilation.reserveSpans(info.spans);
++++++++
++++++++ const root = info.root as frontend.ContainerWrap<frontend.node.Node>;
++++++++ const rootState = root.get<Node<frontend.node.Node>>(CONTAINER_KEY)
++++++++ .build(compilation);
++++++++
++++++++ compilation.buildGlobals(out);
++++++++ out.push('');
++++++++
++++++++ out.push(`int ${info.prefix}_init(${info.prefix}_t* ${ARG_STATE}) {`);
++++++++ out.push(` memset(${ARG_STATE}, 0, sizeof(*${ARG_STATE}));`);
++++++++ out.push(` ${ARG_STATE}->_current = (void*) (intptr_t) ${rootState};`);
++++++++ out.push(' return 0;');
++++++++ out.push('}');
++++++++ out.push('');
++++++++
++++++++ out.push(`static llparse_state_t ${info.prefix}__run(`);
++++++++ out.push(` ${info.prefix}_t* ${ARG_STATE},`);
++++++++ out.push(` const unsigned char* ${ARG_POS},`);
++++++++ out.push(` const unsigned char* ${ARG_ENDPOS}) {`);
++++++++ out.push(` int ${VAR_MATCH};`);
++++++++ out.push(` switch ((llparse_state_t) (intptr_t) ` +
++++++++ `${compilation.currentField()}) {`);
++++++++
++++++++ let tmp: string[] = [];
++++++++ compilation.buildResumptionStates(tmp);
++++++++ compilation.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' default:');
++++++++ out.push(' /* UNREACHABLE */');
++++++++ out.push(' abort();');
++++++++ out.push(' }');
++++++++
++++++++ tmp = [];
++++++++ compilation.buildInternalStates(tmp);
++++++++ compilation.indent(out, tmp, ' ');
++++++++
++++++++ out.push('}');
++++++++ out.push('');
++++++++
++++++++
++++++++ out.push(`int ${info.prefix}_execute(${info.prefix}_t* ${ARG_STATE}, ` +
++++++++ `const char* ${ARG_POS}, const char* ${ARG_ENDPOS}) {`);
++++++++ out.push(' llparse_state_t next;');
++++++++ out.push('');
++++++++
++++++++ out.push(' /* check lingering errors */');
++++++++ out.push(` if (${compilation.errorField()} != 0) {`);
++++++++ out.push(` return ${compilation.errorField()};`);
++++++++ out.push(' }');
++++++++ out.push('');
++++++++
++++++++ tmp = [];
++++++++ this.restartSpans(compilation, info, tmp);
++++++++ compilation.indent(out, tmp, ' ');
++++++++
++++++++ const args = [
++++++++ compilation.stateArg(),
++++++++ `(const unsigned char*) ${compilation.posArg()}`,
++++++++ `(const unsigned char*) ${compilation.endPosArg()}`,
++++++++ ];
++++++++ out.push(` next = ${info.prefix}__run(${args.join(', ')});`);
++++++++ out.push(` if (next == ${STATE_ERROR}) {`);
++++++++ out.push(` return ${compilation.errorField()};`);
++++++++ out.push(' }');
++++++++ out.push(` ${compilation.currentField()} = (void*) (intptr_t) next;`);
++++++++ out.push('');
++++++++
++++++++ tmp = [];
++++++++ this.executeSpans(compilation, info, tmp);
++++++++ compilation.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' return 0;');
++++++++ out.push('}');
++++++++
++++++++ return out.join('\n');
++++++++ }
++++++++
++++++++ private restartSpans(ctx: Compilation, info: frontend.IFrontendResult,
++++++++ out: string[]): void {
++++++++ if (info.spans.length === 0) {
++++++++ return;
++++++++ }
++++++++
++++++++ out.push('/* restart spans */');
++++++++ for (const span of info.spans) {
++++++++ const posField = ctx.spanPosField(span.index);
++++++++
++++++++ out.push(`if (${posField} != NULL) {`);
++++++++ out.push(` ${posField} = (void*) ${ctx.posArg()};`);
++++++++ out.push('}');
++++++++ }
++++++++ out.push('');
++++++++ }
++++++++
++++++++ private executeSpans(ctx: Compilation, info: frontend.IFrontendResult,
++++++++ out: string[]): void {
++++++++ if (info.spans.length === 0) {
++++++++ return;
++++++++ }
++++++++
++++++++ out.push('/* execute spans */');
++++++++ for (const span of info.spans) {
++++++++ const posField = ctx.spanPosField(span.index);
++++++++ let callback: string;
++++++++ if (span.callbacks.length === 1) {
++++++++ callback = ctx.buildCode(ctx.unwrapCode(span.callbacks[0]));
++++++++ } else {
++++++++ callback = `(${info.prefix}__span_cb) ` + ctx.spanCbField(span.index);
++++++++ callback = `(${callback})`;
++++++++ }
++++++++
++++++++ const args = [
++++++++ ctx.stateArg(), posField, `(const char*) ${ctx.endPosArg()}`,
++++++++ ];
++++++++
++++++++ out.push(`if (${posField} != NULL) {`);
++++++++ out.push(' int error;');
++++++++ out.push('');
++++++++ out.push(` error = ${callback}(${args.join(', ')});`);
++++++++
++++++++ // TODO(indutny): de-duplicate this here and in SpanEnd
++++++++ out.push(' if (error != 0) {');
++++++++ out.push(` ${ctx.errorField()} = error;`);
++++++++ out.push(` ${ctx.errorPosField()} = ${ctx.endPosArg()};`);
++++++++ out.push(' return error;');
++++++++ out.push(' }');
++++++++ out.push('}');
++++++++ }
++++++++ out.push('');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import {
++++++++ STATE_PREFIX, LABEL_PREFIX,
++++++++} from '../constants';
++++++++
++++++++export interface INodeEdge {
++++++++ readonly node: frontend.IWrap<frontend.node.Node>;
++++++++ readonly noAdvance: boolean;
++++++++ readonly value?: number;
++++++++}
++++++++
++++++++export abstract class Node<T extends frontend.node.Node> {
++++++++ protected cachedDecl: string | undefined;
++++++++ protected privCompilation: Compilation | undefined;
++++++++
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public build(compilation: Compilation): string {
++++++++ if (this.cachedDecl !== undefined) {
++++++++ return this.cachedDecl;
++++++++ }
++++++++
++++++++ const res = STATE_PREFIX + this.ref.id.name;
++++++++ this.cachedDecl = res;
++++++++
++++++++ this.privCompilation = compilation;
++++++++
++++++++ const out: string[] = [];
++++++++ compilation.debug(out,
++++++++ `Entering node "${this.ref.id.originalName}" ("${this.ref.id.name}")`);
++++++++ this.doBuild(out);
++++++++
++++++++ compilation.addState(res, out);
++++++++
++++++++ return res;
++++++++ }
++++++++
++++++++ protected get compilation(): Compilation {
++++++++ assert(this.privCompilation !== undefined);
++++++++ return this.privCompilation!;
++++++++ }
++++++++
++++++++ protected prologue(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ out.push(`if (${ctx.posArg()} == ${ctx.endPosArg()}) {`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ this.pause(tmp);
++++++++ this.compilation.indent(out, tmp, ' ');
++++++++
++++++++ out.push('}');
++++++++ }
++++++++
++++++++ protected pause(out: string[]): void {
++++++++ out.push(`return ${this.cachedDecl};`);
++++++++ }
++++++++
++++++++ protected tailTo(out: string[], edge: INodeEdge): void {
++++++++ const ctx = this.compilation;
++++++++ const target = ctx.unwrapNode(edge.node).build(ctx);
++++++++
++++++++ if (!edge.noAdvance) {
++++++++ out.push(`${ctx.posArg()}++;`);
++++++++ }
++++++++ if (edge.value !== undefined) {
++++++++ out.push(`${ctx.matchVar()} = ${edge.value};`);
++++++++ }
++++++++ out.push(`goto ${LABEL_PREFIX}${target};`);
++++++++ }
++++++++
++++++++ protected abstract doBuild(out: string[]): void;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++export class Consume extends Node<frontend.node.Consume> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ const index = ctx.stateField(this.ref.field);
++++++++ const ty = ctx.getFieldType(this.ref.field);
++++++++
++++++++ let fieldTy: string;
++++++++ if (ty === 'i64') {
++++++++ fieldTy = 'uint64_t';
++++++++ } else if (ty === 'i32') {
++++++++ fieldTy = 'uint32_t';
++++++++ } else if (ty === 'i16') {
++++++++ fieldTy = 'uint16_t';
++++++++ } else if (ty === 'i8') {
++++++++ fieldTy = 'uint8_t';
++++++++ } else {
++++++++ throw new Error(
++++++++ `Unsupported type ${ty} of field ${this.ref.field} for consume node`);
++++++++ }
++++++++
++++++++ out.push('size_t avail;');
++++++++ out.push(`${fieldTy} need;`);
++++++++
++++++++ out.push('');
++++++++ out.push(`avail = ${ctx.endPosArg()} - ${ctx.posArg()};`);
++++++++ out.push(`need = ${index};`);
++++++++
++++++++ // Note: `avail` or `need` are going to coerced to the largest
++++++++ // datatype needed to hold either of the values.
++++++++ out.push('if (avail >= need) {');
++++++++ out.push(` p += need;`);
++++++++ out.push(` ${index} = 0;`);
++++++++ const tmp: string[] = [];
++++++++ this.tailTo(tmp, this.ref.otherwise!);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push('}');
++++++++ out.push('');
++++++++
++++++++ out.push(`${index} -= avail;`);
++++++++ this.pause(out);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++export class Empty extends Node<frontend.node.Empty> {
++++++++ public doBuild(out: string[]): void {
++++++++ const otherwise = this.ref.otherwise!;
++++++++
++++++++ if (!otherwise.noAdvance) {
++++++++ this.prologue(out);
++++++++ }
++++++++
++++++++ this.tailTo(out, otherwise);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { STATE_ERROR } from '../constants';
++++++++import { Node } from './base';
++++++++
++++++++class ErrorNode<T extends frontend.node.Error> extends Node<T> {
++++++++ protected storeError(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ let hexCode: string;
++++++++ if (this.ref.code < 0) {
++++++++ hexCode = `-0x` + this.ref.code.toString(16);
++++++++ } else {
++++++++ hexCode = '0x' + this.ref.code.toString(16);
++++++++ }
++++++++
++++++++ out.push(`${ctx.errorField()} = ${hexCode};`);
++++++++ out.push(`${ctx.reasonField()} = ${ctx.cstring(this.ref.reason)};`);
++++++++ out.push(`${ctx.errorPosField()} = (const char*) ${ctx.posArg()};`);
++++++++ }
++++++++
++++++++ public doBuild(out: string[]): void {
++++++++ this.storeError(out);
++++++++
++++++++ // Non-recoverable state
++++++++ out.push(`${this.compilation.currentField()} = ` +
++++++++ `(void*) (intptr_t) ${STATE_ERROR};`);
++++++++ out.push(`return ${STATE_ERROR};`);
++++++++ }
++++++++}
++++++++
++++++++export { ErrorNode as Error };
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Consume } from './consume';
++++++++import { Empty } from './empty';
++++++++import { Error as ErrorNode } from './error';
++++++++import { Invoke } from './invoke';
++++++++import { Pause } from './pause';
++++++++import { Sequence } from './sequence';
++++++++import { Single } from './single';
++++++++import { SpanEnd } from './span-end';
++++++++import { SpanStart } from './span-start';
++++++++import { TableLookup } from './table-lookup';
++++++++
++++++++export { Node } from './base';
++++++++
++++++++export default {
++++++++ Consume,
++++++++ Empty,
++++++++ Error: class Error extends ErrorNode<frontend.node.Error> {},
++++++++ Invoke,
++++++++ Pause,
++++++++ Sequence,
++++++++ Single,
++++++++ SpanEnd,
++++++++ SpanStart,
++++++++ TableLookup,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++export class Invoke extends Node<frontend.node.Invoke> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ const code = ctx.unwrapCode(this.ref.code);
++++++++ const codeDecl = ctx.buildCode(code);
++++++++
++++++++ const args: string[] = [
++++++++ ctx.stateArg(),
++++++++ ctx.posArg(),
++++++++ ctx.endPosArg(),
++++++++ ];
++++++++
++++++++ const signature = code.ref.signature;
++++++++ if (signature === 'value') {
++++++++ args.push(ctx.matchVar());
++++++++ }
++++++++
++++++++ out.push(`switch (${codeDecl}(${args.join(', ')})) {`);
++++++++ let tmp: string[];
++++++++
++++++++ for (const edge of this.ref.edges) {
++++++++ out.push(` case ${edge.code}:`);
++++++++ tmp = [];
++++++++ this.tailTo(tmp, {
++++++++ noAdvance: true,
++++++++ node: edge.node,
++++++++ value: undefined,
++++++++ });
++++++++ ctx.indent(out, tmp, ' ');
++++++++ }
++++++++
++++++++ out.push(' default:');
++++++++ tmp = [];
++++++++ this.tailTo(tmp, this.ref.otherwise!);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push('}');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { STATE_ERROR } from '../constants';
++++++++import { Error as ErrorNode } from './error';
++++++++
++++++++export class Pause extends ErrorNode<frontend.node.Pause> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ this.storeError(out);
++++++++
++++++++ // Recoverable state
++++++++ const otherwise = ctx.unwrapNode(this.ref.otherwise!.node).build(ctx);
++++++++ out.push(`${ctx.currentField()} = ` +
++++++++ `(void*) (intptr_t) ${otherwise};`);
++++++++ out.push(`return ${STATE_ERROR};`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import {
++++++++ SEQUENCE_COMPLETE, SEQUENCE_MISMATCH, SEQUENCE_PAUSE,
++++++++} from '../constants';
++++++++import { Node } from './base';
++++++++
++++++++export class Sequence extends Node<frontend.node.Sequence> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ out.push('llparse_match_t match_seq;');
++++++++ out.push('');
++++++++
++++++++ this.prologue(out);
++++++++
++++++++ const matchSequence = ctx.getMatchSequence(this.ref.transform!,
++++++++ this.ref.select);
++++++++
++++++++ out.push(`match_seq = ${matchSequence}(${ctx.stateArg()}, ` +
++++++++ `${ctx.posArg()}, ` +
++++++++ `${ctx.endPosArg()}, ${ctx.blob(this.ref.select)}, ` +
++++++++ `${this.ref.select.length});`);
++++++++ out.push('p = match_seq.current;');
++++++++
++++++++ let tmp: string[];
++++++++
++++++++ out.push('switch (match_seq.status) {');
++++++++
++++++++ out.push(` case ${SEQUENCE_COMPLETE}: {`);
++++++++ tmp = [];
++++++++ this.tailTo(tmp, {
++++++++ noAdvance: false,
++++++++ node: this.ref.edge!.node,
++++++++ value: this.ref.edge!.value,
++++++++ });
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push(' }');
++++++++
++++++++ out.push(` case ${SEQUENCE_PAUSE}: {`);
++++++++ tmp = [];
++++++++ this.pause(tmp);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push(' }');
++++++++
++++++++ out.push(` case ${SEQUENCE_MISMATCH}: {`);
++++++++ tmp = [];
++++++++ this.tailTo(tmp, this.ref.otherwise!);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push(' }');
++++++++
++++++++ out.push('}');
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++export class Single extends Node<frontend.node.Single> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ const otherwise = this.ref.otherwise!;
++++++++
++++++++ this.prologue(out);
++++++++
++++++++ const transform = ctx.unwrapTransform(this.ref.transform!);
++++++++ const current = transform.build(ctx, `*${ctx.posArg()}`);
++++++++
++++++++ out.push(`switch (${current}) {`)
++++++++ this.ref.edges.forEach((edge) => {
++++++++ let ch: string;
++++++++
++++++++ // Non-printable ASCII, or single-quote, or forward slash
++++++++ if (edge.key < 0x20 || edge.key > 0x7e || edge.key === 0x27 ||
++++++++ edge.key === 0x5c) {
++++++++ ch = edge.key.toString();
++++++++ } else {
++++++++ ch = `'${String.fromCharCode(edge.key)}'`;
++++++++ }
++++++++ out.push(` case ${ch}: {`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ this.tailTo(tmp, edge);
++++++++ ctx.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' }');
++++++++ });
++++++++
++++++++ out.push(` default: {`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ this.tailTo(tmp, otherwise);
++++++++ ctx.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' }');
++++++++
++++++++ out.push(`}`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { STATE_ERROR } from '../constants';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanEnd extends Node<frontend.node.SpanEnd> {
++++++++ public doBuild(out: string[]): void {
++++++++ out.push('const unsigned char* start;');
++++++++ out.push('int err;');
++++++++ out.push('');
++++++++
++++++++ const ctx = this.compilation;
++++++++ const field = this.ref.field;
++++++++ const posField = ctx.spanPosField(field.index);
++++++++
++++++++ // Load start position
++++++++ out.push(`start = ${posField};`);
++++++++
++++++++ // ...and reset
++++++++ out.push(`${posField} = NULL;`);
++++++++
++++++++ // Invoke callback
++++++++ const callback = ctx.buildCode(ctx.unwrapCode(this.ref.callback));
++++++++ out.push(`err = ${callback}(${ctx.stateArg()}, start, ${ctx.posArg()});`);
++++++++
++++++++ out.push('if (err != 0) {');
++++++++ const tmp: string[] = [];
++++++++ this.buildError(tmp, 'err');
++++++++ ctx.indent(out, tmp, ' ');
++++++++ out.push('}');
++++++++
++++++++ const otherwise = this.ref.otherwise!;
++++++++ this.tailTo(out, otherwise);
++++++++ }
++++++++
++++++++ private buildError(out: string[], code: string) {
++++++++ const ctx = this.compilation;
++++++++
++++++++ out.push(`${ctx.errorField()} = ${code};`);
++++++++
++++++++ const otherwise = this.ref.otherwise!;
++++++++ let resumePos = ctx.posArg();
++++++++ if (!otherwise.noAdvance) {
++++++++ resumePos = `(${resumePos} + 1)`;
++++++++ }
++++++++
++++++++ out.push(`${ctx.errorPosField()} = (const char*) ${resumePos};`);
++++++++
++++++++ const resumptionTarget = ctx.unwrapNode(otherwise.node).build(ctx);
++++++++ out.push(`${ctx.currentField()} = ` +
++++++++ `(void*) (intptr_t) ${resumptionTarget};`);
++++++++
++++++++ out.push(`return ${STATE_ERROR};`);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++export class SpanStart extends Node<frontend.node.SpanStart> {
++++++++ public doBuild(out: string[]): void {
++++++++ // Prevent spurious empty spans
++++++++ this.prologue(out);
++++++++
++++++++ const ctx = this.compilation;
++++++++ const field = this.ref.field;
++++++++
++++++++ const posField = ctx.spanPosField(field.index);
++++++++ out.push(`${posField} = (void*) ${ctx.posArg()};`);
++++++++
++++++++ if (field.callbacks.length > 1) {
++++++++ const cbField = ctx.spanCbField(field.index);
++++++++ const callback = ctx.unwrapCode(this.ref.callback);
++++++++ out.push(`${cbField} = ${ctx.buildCode(callback)};`);
++++++++ }
++++++++
++++++++ const otherwise = this.ref.otherwise!;
++++++++ this.tailTo(out, otherwise);
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Node } from './base';
++++++++
++++++++const MAX_CHAR = 0xff;
++++++++const TABLE_GROUP = 16;
++++++++
++++++++// _mm_cmpestri takes 8 ranges
++++++++const SSE_RANGES_LEN = 16;
++++++++// _mm_cmpestri takes 128bit input
++++++++const SSE_RANGES_PAD = 16;
++++++++const MAX_SSE_CALLS = 2;
++++++++const SSE_ALIGNMENT = 16;
++++++++
++++++++interface ITable {
++++++++ readonly name: string;
++++++++ readonly declaration: ReadonlyArray<string>;
++++++++}
++++++++
++++++++export class TableLookup extends Node<frontend.node.TableLookup> {
++++++++ public doBuild(out: string[]): void {
++++++++ const ctx = this.compilation;
++++++++
++++++++ const table = this.buildTable();
++++++++ for (const line of table.declaration) {
++++++++ out.push(line);
++++++++ }
++++++++
++++++++ this.prologue(out);
++++++++
++++++++ const transform = ctx.unwrapTransform(this.ref.transform!);
++++++++
++++++++ // Try to vectorize nodes matching characters and looping to themselves
++++++++ // NOTE: `switch` below triggers when there is not enough characters in the
++++++++ // stream for vectorized processing.
++++++++ this.buildSSE(out);
++++++++
++++++++ const current = transform.build(ctx, `*${ctx.posArg()}`);
++++++++ out.push(`switch (${table.name}[(uint8_t) ${current}]) {`);
++++++++
++++++++ for (const [ index, edge ] of this.ref.edges.entries()) {
++++++++ out.push(` case ${index + 1}: {`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ const edge = this.ref.edges[index];
++++++++ this.tailTo(tmp, {
++++++++ noAdvance: edge.noAdvance,
++++++++ node: edge.node,
++++++++ value: undefined,
++++++++ });
++++++++ ctx.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' }');
++++++++ }
++++++++
++++++++ out.push(` default: {`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ this.tailTo(tmp, this.ref.otherwise!);
++++++++ ctx.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' }');
++++++++ out.push('}');
++++++++ }
++++++++
++++++++ private buildSSE(out: string[]): boolean {
++++++++ const ctx = this.compilation;
++++++++
++++++++ // Transformation is not supported atm
++++++++ if (this.ref.transform && this.ref.transform.ref.name !== 'id') {
++++++++ return false;
++++++++ }
++++++++
++++++++ if (this.ref.edges.length !== 1) {
++++++++ return false;
++++++++ }
++++++++
++++++++ const edge = this.ref.edges[0];
++++++++ if (edge.node.ref !== this.ref) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // NOTE: keys are sorted
++++++++ let ranges: number[] = [];
++++++++ let first: number | undefined;
++++++++ let last: number | undefined;
++++++++ for (const key of edge.keys) {
++++++++ if (first === undefined) {
++++++++ first = key;
++++++++ }
++++++++ if (last === undefined) {
++++++++ last = key;
++++++++ }
++++++++
++++++++ if (key - last > 1) {
++++++++ ranges.push(first, last);
++++++++ first = key;
++++++++ }
++++++++ last = key;
++++++++ }
++++++++ if (first !== undefined && last !== undefined) {
++++++++ ranges.push(first, last);
++++++++ }
++++++++
++++++++ if (ranges.length === 0) {
++++++++ return false;
++++++++ }
++++++++
++++++++ // Way too many calls would be required
++++++++ if (ranges.length > MAX_SSE_CALLS * SSE_RANGES_LEN) {
++++++++ return false;
++++++++ }
++++++++
++++++++ out.push('#ifdef __SSE4_2__');
++++++++ out.push(`if (${ctx.endPosArg()} - ${ctx.posArg()} >= 16) {`);
++++++++ out.push(' __m128i ranges;');
++++++++ out.push(' __m128i input;');
++++++++ out.push(' int avail;');
++++++++ out.push(' int match_len;');
++++++++ out.push('');
++++++++ out.push(' /* Load input */');
++++++++ out.push(` input = _mm_loadu_si128((__m128i const*) ${ctx.posArg()});`);
++++++++ for (let off = 0; off < ranges.length; off += SSE_RANGES_LEN) {
++++++++ const subRanges = ranges.slice(off, off + SSE_RANGES_LEN);
++++++++
++++++++ let paddedRanges = subRanges.slice();
++++++++ while (paddedRanges.length < SSE_RANGES_PAD) {
++++++++ paddedRanges.push(0);
++++++++ }
++++++++
++++++++ const blob = ctx.blob(Buffer.from(paddedRanges), SSE_ALIGNMENT);
++++++++ out.push(` ranges = _mm_loadu_si128((__m128i const*) ${blob});`);
++++++++ out.push('');
++++++++
++++++++ out.push(' /* Find first character that does not match `ranges` */');
++++++++ out.push(` match_len = _mm_cmpestri(ranges, ${subRanges.length},`);
++++++++ out.push(' input, 16,');
++++++++ out.push(' _SIDD_UBYTE_OPS | _SIDD_CMP_RANGES |');
++++++++ out.push(' _SIDD_NEGATIVE_POLARITY);');
++++++++ out.push('');
++++++++ out.push(' if (match_len != 0) {');
++++++++ out.push(` ${ctx.posArg()} += match_len;`);
++++++++
++++++++ const tmp: string[] = [];
++++++++ assert.strictEqual(edge.noAdvance, false);
++++++++ this.tailTo(tmp, {
++++++++ noAdvance: true,
++++++++ node: edge.node,
++++++++ });
++++++++ ctx.indent(out, tmp, ' ');
++++++++
++++++++ out.push(' }');
++++++++ }
++++++++
++++++++ {
++++++++ const tmp: string[] = [];
++++++++ this.tailTo(tmp, this.ref.otherwise!);
++++++++ ctx.indent(out, tmp, ' ');
++++++++ }
++++++++ out.push('}');
++++++++
++++++++ out.push('#endif /* __SSE4_2__ */');
++++++++
++++++++ return true;
++++++++ }
++++++++
++++++++ private buildTable(): ITable {
++++++++ const table: number[] = new Array(MAX_CHAR + 1).fill(0);
++++++++
++++++++ for (const [ index, edge ] of this.ref.edges.entries()) {
++++++++ edge.keys.forEach((key) => {
++++++++ assert.strictEqual(table[key], 0);
++++++++ table[key] = index + 1;
++++++++ });
++++++++ }
++++++++
++++++++ const lines = [
++++++++ 'static uint8_t lookup_table[] = {',
++++++++ ];
++++++++ for (let i = 0; i < table.length; i += TABLE_GROUP) {
++++++++ let line = ` ${table.slice(i, i + TABLE_GROUP).join(', ')}`;
++++++++ if (i + TABLE_GROUP < table.length) {
++++++++ line += ',';
++++++++ }
++++++++ lines.push(line);
++++++++ }
++++++++ lines.push('};');
++++++++
++++++++ return {
++++++++ name: 'lookup_table',
++++++++ declaration: lines,
++++++++ };
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++
++++++++export abstract class Transform<T extends frontend.transform.Transform> {
++++++++ constructor(public readonly ref: T) {
++++++++ }
++++++++
++++++++ public abstract build(ctx: Compilation, value: string): string;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Transform } from './base';
++++++++
++++++++export class ID extends Transform<frontend.transform.ID> {
++++++++ public build(ctx: Compilation, value: string): string {
++++++++ // Identity transformation
++++++++ return value;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { ID } from './id';
++++++++import { ToLower } from './to-lower';
++++++++import { ToLowerUnsafe } from './to-lower-unsafe';
++++++++
++++++++export { Transform } from './base';
++++++++
++++++++export default {
++++++++ ID,
++++++++ ToLower,
++++++++ ToLowerUnsafe,
++++++++};
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLowerUnsafe extends Transform<frontend.transform.ToLowerUnsafe> {
++++++++ public build(ctx: Compilation, value: string): string {
++++++++ return `((${value}) | 0x20)`;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as frontend from 'llparse-frontend';
++++++++
++++++++import { Compilation } from '../compilation';
++++++++import { Transform } from './base';
++++++++
++++++++export class ToLower extends Transform<frontend.transform.ToLower> {
++++++++ public build(ctx: Compilation, value: string): string {
++++++++ return `((${value}) >= 'A' && (${value}) <= 'Z' ? ` +
++++++++ `(${value} | 0x20) : (${value}))`;
++++++++ }
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import { build, NUM_SELECT, printMatch, printOff } from './fixtures';
++++++++
++++++++describe('llparse/code', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ describe('`.mulAdd()`', () => {
++++++++ it('should operate normally', async () => {
++++++++ const start = p.node('start');
++++++++ const dot = p.node('dot');
++++++++
++++++++ p.property('i64', 'counter');
++++++++
++++++++ const is1337 = p.invoke(p.code.load('counter'), {
++++++++ 1337: printOff(p, p.invoke(p.code.update('counter', 0), start)),
++++++++ }, p.error(1, 'Invalid result'));
++++++++
++++++++ const count = p.invoke(p.code.mulAdd('counter', { base: 10 }), start);
++++++++
++++++++ start
++++++++ .select(NUM_SELECT, count)
++++++++ .otherwise(dot);
++++++++
++++++++ dot
++++++++ .match('.', is1337)
++++++++ .otherwise(p.error(1, 'Unexpected'));
++++++++
++++++++ const binary = await build(p, start, 'mul-add');
++++++++ await binary.check('1337.', 'off=5\n');
++++++++ });
++++++++
++++++++ it('should operate fail on overflow', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ p.property('i8', 'counter');
++++++++
++++++++ const count = p.invoke(p.code.mulAdd('counter', { base: 10 }), {
++++++++ 1: printOff(p, start),
++++++++ }, start);
++++++++
++++++++ start
++++++++ .select(NUM_SELECT, count)
++++++++ .otherwise(p.error(1, 'Unexpected'));
++++++++
++++++++ const binary = await build(p, start, 'mul-add-overflow');
++++++++ await binary.check('1111', 'off=4\n');
++++++++ });
++++++++
++++++++ it('should operate fail on greater than max', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ p.property('i64', 'counter');
++++++++
++++++++ const count = p.invoke(p.code.mulAdd('counter', {
++++++++ base: 10,
++++++++ max: 1000,
++++++++ }), {
++++++++ 1: printOff(p, start),
++++++++ }, start);
++++++++
++++++++ start
++++++++ .select(NUM_SELECT, count)
++++++++ .otherwise(p.error(1, 'Unexpected'));
++++++++
++++++++ const binary = await build(p, start, 'mul-add-max-overflow');
++++++++ await binary.check('1111', 'off=4\n');
++++++++ });
++++++++ });
++++++++
++++++++ describe('`.update()`', () => {
++++++++ it('should operate normally', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ p.property('i64', 'counter');
++++++++
++++++++ const update = p.invoke(p.code.update('counter', 42));
++++++++
++++++++ start
++++++++ .skipTo(update);
++++++++
++++++++ update
++++++++ .otherwise(p.invoke(p.code.load('counter'), {
++++++++ 42: printOff(p, start),
++++++++ }, p.error(1, 'Unexpected')));
++++++++
++++++++ const binary = await build(p, start, 'update');
++++++++ await binary.check('.', 'off=1\n');
++++++++ });
++++++++ });
++++++++
++++++++ describe('`.isEqual()`', () => {
++++++++ it('should operate normally', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ p.property('i64', 'counter');
++++++++
++++++++ const check = p.invoke(p.code.isEqual('counter', 1), {
++++++++ 0: printOff(p, start),
++++++++ 1: start,
++++++++ }, p.error(1, 'Unexpected'));
++++++++
++++++++ start
++++++++ .select(NUM_SELECT, p.invoke(p.code.store('counter'), check))
++++++++ .otherwise(p.error(1, 'Unexpected'));
++++++++
++++++++ const binary = await build(p, start, 'is-equal');
++++++++ await binary.check('010', 'off=1\noff=3\n');
++++++++ });
++++++++ });
++++++++
++++++++ describe('`.or()`/`.and()`/`.test()`', () => {
++++++++ it('should set and retrieve bits', async () => {
++++++++ const start = p.node('start');
++++++++ const test = p.node('test');
++++++++
++++++++ p.property('i64', 'flag');
++++++++
++++++++ start
++++++++ .match('1', p.invoke(p.code.or('flag', 1), start))
++++++++ .match('2', p.invoke(p.code.or('flag', 2), start))
++++++++ .match('4', p.invoke(p.code.or('flag', 4), start))
++++++++ // Reset
++++++++ .match('r', p.invoke(p.code.update('flag', 0), start))
++++++++ // Partial Reset
++++++++ .match('p', p.invoke(p.code.and('flag', ~1), start))
++++++++ // Test
++++++++ .match('-', test)
++++++++ .otherwise(p.error(1, 'start'));
++++++++
++++++++ test
++++++++ .match('1', p.invoke(p.code.test('flag', 1), {
++++++++ 0: test,
++++++++ 1: printOff(p, test),
++++++++ }, p.error(2, 'test-1')))
++++++++ .match('2', p.invoke(p.code.test('flag', 2), {
++++++++ 0: test,
++++++++ 1: printOff(p, test),
++++++++ }, p.error(3, 'test-2')))
++++++++ .match('4', p.invoke(p.code.test('flag', 4), {
++++++++ 0: test,
++++++++ 1: printOff(p, test),
++++++++ }, p.error(4, 'test-3')))
++++++++ .match('7', p.invoke(p.code.test('flag', 7), {
++++++++ 0: test,
++++++++ 1: printOff(p, test),
++++++++ }, p.error(5, 'test-7')))
++++++++ // Restart
++++++++ .match('.', start)
++++++++ .otherwise(p.error(6, 'test'));
++++++++
++++++++ const binary = await build(p, start, 'or-test');
++++++++ await binary.check('1-124.2-1247.4-1247.r4-124.r12p-12', [
++++++++ 'off=3',
++++++++ 'off=9', 'off=10',
++++++++ 'off=16', 'off=17', 'off=18', 'off=19',
++++++++ 'off=26',
++++++++ 'off=34',
++++++++ ]);
++++++++ });
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import {
++++++++ ALPHA, build, NUM, NUM_SELECT, printMatch, printOff,
++++++++} from './fixtures';
++++++++
++++++++describe('llparse/Compiler', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ it('should compile simple parser', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.match(' ', start);
++++++++
++++++++ start.match('HTTP', printOff(p, start));
++++++++
++++++++ start.select({
++++++++ CONNECT: 6,
++++++++ DELETE: 4,
++++++++ GET: 1,
++++++++ HEAD: 0,
++++++++ OPTIONS: 5,
++++++++ PATCH: 8,
++++++++ POST: 2,
++++++++ PUT: 3,
++++++++ TRACE: 7,
++++++++ }, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'simple');
++++++++ await binary.check('GET', 'off=3 match=1\n');
++++++++ });
++++++++
++++++++ it('should optimize shallow select', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.select(NUM_SELECT, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'shallow');
++++++++ await binary.check('012', 'off=1 match=0\noff=2 match=1\noff=3 match=2\n');
++++++++ });
++++++++
++++++++ it('should support key-value select', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.select('0', 0, printMatch(p, start));
++++++++ start.select('1', 1, printMatch(p, start));
++++++++ start.select('2', 2, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'kv-select');
++++++++ await binary.check('012', 'off=1 match=0\noff=2 match=1\noff=3 match=2\n');
++++++++ });
++++++++
++++++++ it('should support multi-match', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.match([ ' ', '\t', '\r', '\n' ], start);
++++++++
++++++++ start.select({
++++++++ A: 0,
++++++++ B: 1,
++++++++ }, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'multi-match');
++++++++ await binary.check(
++++++++ 'A B\t\tA\r\nA',
++++++++ 'off=1 match=0\noff=3 match=1\noff=6 match=0\noff=9 match=0\n');
++++++++ });
++++++++
++++++++ it('should support numeric-match', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.match(32, start);
++++++++
++++++++ start.select({
++++++++ A: 0,
++++++++ B: 1,
++++++++ }, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'multi-match');
++++++++ await binary.check(
++++++++ 'A B A A',
++++++++ 'off=1 match=0\noff=3 match=1\noff=6 match=0\noff=9 match=0\n');
++++++++ });
++++++++
++++++++ it('should support custom state properties', async () => {
++++++++ const start = p.node('start');
++++++++ const error = p.error(3, 'Invalid word');
++++++++
++++++++ p.property('i8', 'custom');
++++++++
++++++++ const second = p.invoke(p.code.load('custom'), {
++++++++ 0: p.invoke(p.code.match('llparse__print_zero'), { 0: start }, error),
++++++++ 1: p.invoke(p.code.match('llparse__print_one'), { 0: start }, error),
++++++++ }, error);
++++++++
++++++++ start
++++++++ .select({
++++++++ 0: 0,
++++++++ 1: 1,
++++++++ }, p.invoke(p.code.store('custom'), second))
++++++++ .otherwise(error);
++++++++
++++++++ const binary = await build(p, start, 'custom-prop');
++++++++ await binary.check('0110', 'off=1 0\noff=2 1\noff=3 1\noff=4 0\n');
++++++++ });
++++++++
++++++++ it('should return error code/reason', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.match('a', start);
++++++++ start.otherwise(p.error(42, 'some reason'));
++++++++
++++++++ const binary = await build(p, start, 'error');
++++++++ await binary.check('aab', 'off=2 error code=42 reason="some reason"\n');
++++++++ });
++++++++
++++++++ it('should not merge `.match()` with `.peek()`', async () => {
++++++++ const maybeCr = p.node('maybeCr');
++++++++ const lf = p.node('lf');
++++++++
++++++++ maybeCr.peek('\n', lf);
++++++++ maybeCr.match('\r', lf);
++++++++ maybeCr.otherwise(p.error(1, 'error'));
++++++++
++++++++ lf.match('\n', printOff(p, maybeCr));
++++++++ lf.otherwise(p.error(2, 'error'));
++++++++
++++++++ const binary = await build(p, maybeCr, 'no-merge');
++++++++ await binary.check('\r\n\n', 'off=2\noff=3\n');
++++++++ });
++++++++
++++++++ describe('`.match()`', () => {
++++++++ it('should compile to a single-bit table-lookup node', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .match(ALPHA, start)
++++++++ .skipTo(printOff(p, start));
++++++++
++++++++ // TODO(indutny): validate compilation result?
++++++++ const binary = await build(p, start, 'match-bit-check');
++++++++ await binary.check('pecan.is.dead.', 'off=6\noff=9\noff=14\n');
++++++++ });
++++++++
++++++++ it('should compile to a multi-bit table-lookup node', async () => {
++++++++ const start = p.node('start');
++++++++ const another = p.node('another');
++++++++
++++++++ start
++++++++ .match(ALPHA, start)
++++++++ .peek(NUM, another)
++++++++ .skipTo(printOff(p, start));
++++++++
++++++++ another
++++++++ .match(NUM, another)
++++++++ .otherwise(start);
++++++++
++++++++ // TODO(indutny): validate compilation result?
++++++++ const binary = await build(p, start, 'match-multi-bit-check');
++++++++ await binary.check('pecan.135.is.dead.',
++++++++ 'off=6\noff=10\noff=13\noff=18\n');
++++++++ });
++++++++
++++++++ it('should not overflow on signed char in table-lookup node', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .match(ALPHA, start)
++++++++ .match([ 0xc3, 0xbc ], start)
++++++++ .skipTo(printOff(p, start));
++++++++
++++++++ // TODO(indutny): validate compilation result?
++++++++ const binary = await build(p, start, 'match-bit-check');
++++++++ await binary.check('Düsseldorf.', 'off=12\n');
++++++++ });
++++++++
++++++++ it('should match single quotes and forward slashes', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .match('\'', printOff(p, start))
++++++++ .match('\\', printOff(p, start))
++++++++ .otherwise(p.error(3, 'Invalid char'));
++++++++
++++++++ // TODO(indutny): validate compilation result?
++++++++ const binary = await build(p, start, 'escape-char');
++++++++ await binary.check('\\\'', 'off=1\noff=2\n');
++++++++ });
++++++++
++++++++ it('should hit SSE4.2 optimization for table-lookup', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .match(ALPHA, start)
++++++++ .skipTo(printOff(p, start));
++++++++
++++++++ // TODO(indutny): validate compilation result?
++++++++ const binary = await build(p, start, 'match-bit-check-sse');
++++++++ await binary.check('abcdabcdabcdabcdabcdabcdabcd.abcd.',
++++++++ 'off=29\noff=34\n');
++++++++ });
++++++++
++++++++ it('should compile overlapping matches', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start.select({
++++++++ aa: 1,
++++++++ aab: 2,
++++++++ }, printMatch(p, start));
++++++++
++++++++ start.otherwise(p.error(3, 'Invalid word'));
++++++++
++++++++ const binary = await build(p, start, 'overlapping-matches');
++++++++ await binary.check('aaaabaa', 'off=2 match=1\noff=5 match=2\n');
++++++++ });
++++++++ });
++++++++
++++++++ describe('`.peek()`', () => {
++++++++ it('should not advance position', async () => {
++++++++ const start = p.node('start');
++++++++ const ab = p.node('ab');
++++++++ const error = p.error(3, 'Invalid word');
++++++++
++++++++ start
++++++++ .peek([ 'a', 'b' ], ab)
++++++++ .otherwise(error);
++++++++
++++++++ ab
++++++++ .match([ 'a', 'b' ], printOff(p, start))
++++++++ .otherwise(error);
++++++++
++++++++ const binary = await build(p, start, 'peek');
++++++++ await binary.check('ab', 'off=1\noff=2\n');
++++++++ });
++++++++ });
++++++++
++++++++ describe('`.otherwise()`', () => {
++++++++ it('should not advance position by default', async () => {
++++++++ const a = p.node('a');
++++++++ const b = p.node('b');
++++++++
++++++++ a
++++++++ .match('A', a)
++++++++ .otherwise(b);
++++++++
++++++++ b
++++++++ .match('B', printOff(p, b))
++++++++ .skipTo(a);
++++++++
++++++++ const binary = await build(p, a, 'otherwise-noadvance');
++++++++ await binary.check('AABAB', 'off=3\noff=5\n');
++++++++ });
++++++++
++++++++ it('should advance when it is `.skipTo()`', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .match(' ', printOff(p, start))
++++++++ .skipTo(start);
++++++++
++++++++ const binary = await build(p, start, 'otherwise-skip');
++++++++ await binary.check('HELLO WORLD', 'off=6\n');
++++++++ });
++++++++
++++++++ it('should skip everything with `.skipTo()`', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .skipTo(start);
++++++++
++++++++ const binary = await build(p, start, 'all-skip');
++++++++ await binary.check('HELLO WORLD', '\n');
++++++++ });
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import { build, NUM_SELECT, printMatch, printOff } from './fixtures';
++++++++
++++++++describe('llparse/consume', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ it('should consume bytes with i8 field', async () => {
++++++++ p.property('i8', 'to_consume');
++++++++
++++++++ const start = p.node('start');
++++++++ const consume = p.consume('to_consume');
++++++++
++++++++ start.select(NUM_SELECT, p.invoke(p.code.store('to_consume'), consume));
++++++++
++++++++ start
++++++++ .otherwise(p.error(1, 'unexpected'));
++++++++
++++++++ consume
++++++++ .otherwise(printOff(p, start));
++++++++
++++++++ const binary = await build(p, start, 'consume');
++++++++ await binary.check('3aaa2bb1a01b', 'off=4\noff=7\noff=9\noff=10\noff=12\n');
++++++++ });
++++++++
++++++++ it('should consume bytes with i64 field', async () => {
++++++++ p.property('i64', 'to_consume');
++++++++
++++++++ const start = p.node('start');
++++++++ const consume = p.consume('to_consume');
++++++++
++++++++ start.select(NUM_SELECT, p.invoke(p.code.store('to_consume'), consume));
++++++++
++++++++ start
++++++++ .otherwise(p.error(1, 'unexpected'));
++++++++
++++++++ consume
++++++++ .otherwise(printOff(p, start));
++++++++
++++++++ const binary = await build(p, start, 'consume-i64');
++++++++ await binary.check('3aaa2bb1a01b', 'off=4\noff=7\noff=9\noff=10\noff=12\n');
++++++++ });
++++++++
++++++++ it('should consume bytes with untruncated i64 field', async () => {
++++++++ p.property('i64', 'to_consume');
++++++++
++++++++ const start = p.node('start');
++++++++ const consume = p.consume('to_consume');
++++++++
++++++++ start
++++++++ .select(
++++++++ NUM_SELECT,
++++++++ p.invoke(p.code.mulAdd('to_consume', { base: 10 }), start)
++++++++ )
++++++++ .skipTo(consume);
++++++++
++++++++ consume
++++++++ .otherwise(printOff(p, start));
++++++++
++++++++ const binary = await build(p, start, 'consume-untruncated-i64');
++++++++ await binary.check('4294967297.xxxxxxxx', '\n');
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++#include "fixture.h"
++++++++
++++++++int llparse__print_zero(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ llparse__print(p, endp, "0");
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int llparse__print_one(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ llparse__print(p, endp, "1");
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int llparse__print_off(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ llparse__print(p, endp, "");
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int llparse__print_match(llparse_t* s, const char* p, const char* endp,
++++++++ int value) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ llparse__print(p, endp, "match=%d", value);
++++++++ return 0;
++++++++}
++++++++
++++++++
++++++++int llparse__on_dot(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ return llparse__print_span("dot", p, endp);
++++++++}
++++++++
++++++++
++++++++int llparse__on_dash(llparse_t* s, const char* p, const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ return llparse__print_span("dash", p, endp);
++++++++}
++++++++
++++++++
++++++++int llparse__on_underscore(llparse_t* s, const char* p,
++++++++ const char* endp) {
++++++++ if (llparse__in_bench)
++++++++ return 0;
++++++++ return llparse__print_span("underscore", p, endp);
++++++++}
++++++++
++++++++
++++++++/* A span callback, really */
++++++++int llparse__please_fail(llparse_t* s, const char* p, const char* endp) {
++++++++ s->reason = "please fail";
++++++++ if (llparse__in_bench)
++++++++ return 1;
++++++++ return 1;
++++++++}
++++++++
++++++++
++++++++/* A span callback, really */
++++++++static int llparse__pause_once_counter;
++++++++
++++++++int llparse__pause_once(llparse_t* s, const char* p, const char* endp) {
++++++++ if (!llparse__in_bench)
++++++++ llparse__print_span("pause", p, endp);
++++++++
++++++++ if (llparse__pause_once_counter != 0)
++++++++ return 0;
++++++++ llparse__pause_once_counter = 1;
++++++++
++++++++ return LLPARSE__ERROR_PAUSE;
++++++++}
++++++++
++++++++
++++++++int llparse__test_init() {
++++++++ llparse__pause_once_counter = 0;
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import { source } from 'llparse-frontend';
++++++++import { Fixture, FixtureResult } from 'llparse-test-fixture';
++++++++import * as path from 'path';
++++++++
++++++++import { LLParse } from '../../src/api';
++++++++
++++++++export { ERROR_PAUSE } from 'llparse-test-fixture';
++++++++
++++++++const fixtures = new Fixture({
++++++++ buildDir: path.join(__dirname, '..', 'tmp'),
++++++++ extra: [
++++++++ '-msse4.2',
++++++++ '-DLLPARSE__TEST_INIT=llparse__test_init',
++++++++ path.join(__dirname, 'extra.c'),
++++++++ ],
++++++++});
++++++++
++++++++export function build(llparse: LLParse, node: source.node.Node, outFile: string)
++++++++ : Promise<FixtureResult> {
++++++++ return fixtures.build(llparse.build(node, {
++++++++ c: {
++++++++ header: outFile,
++++++++ },
++++++++ }), outFile);
++++++++}
++++++++
++++++++export function printMatch(p: LLParse, next: source.node.Node)
++++++++ : source.node.Node {
++++++++ const code = p.code.value('llparse__print_match');
++++++++ const res = p.invoke(code, next);
++++++++ return res;
++++++++}
++++++++
++++++++export function printOff(p: LLParse, next: source.node.Node): source.node.Node {
++++++++ const code = p.code.match('llparse__print_off');
++++++++ return p.invoke(code, next);
++++++++}
++++++++
++++++++export const NUM_SELECT: { readonly [key: string]: number } = {
++++++++ 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9,
++++++++};
++++++++
++++++++export const NUM: ReadonlyArray<string> = [
++++++++ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
++++++++];
++++++++
++++++++export const ALPHA: ReadonlyArray<string> = [
++++++++ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
++++++++ 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
++++++++ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
++++++++ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
++++++++];
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import { build, ERROR_PAUSE, printMatch, printOff } from './fixtures';
++++++++
++++++++describe('llparse/resumption', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ it('should resume after span end pause', async () => {
++++++++ const start = p.node('start');
++++++++ const a = p.node('a');
++++++++ const span = p.span(p.code.span('llparse__pause_once'));
++++++++
++++++++ start
++++++++ .peek('a', span.start(a))
++++++++ .skipTo(start);
++++++++
++++++++ a
++++++++ .match('a', a)
++++++++ .otherwise(span.end(start));
++++++++
++++++++ const binary = await build(p, start, 'resume-span');
++++++++
++++++++ await binary.check('baaab',
++++++++ new RegExp(
++++++++ '^(' +
++++++++ 'off=\\d+ pause\\noff=1 len=3 span\\[pause\\]="aaa"' +
++++++++ '|' +
++++++++ 'off=1 len=3 span\\[pause\\]="aaa"\noff=4 pause' +
++++++++ ')\\n$'
++++++++ , 'g'));
++++++++ });
++++++++
++++++++ it('should resume after `pause` node', async () => {
++++++++ const start = p.node('start');
++++++++ const pause = p.pause(ERROR_PAUSE, 'paused');
++++++++
++++++++ start
++++++++ .match('p', pause)
++++++++ .skipTo(start);
++++++++
++++++++ pause
++++++++ .otherwise(printOff(p, start));
++++++++
++++++++ const binary = await build(p, start, 'resume-pause');
++++++++
++++++++ await binary.check('..p....p..',
++++++++ 'off=3 pause\noff=3\noff=8 pause\noff=8\n');
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import { build, printMatch, printOff } from './fixtures';
++++++++
++++++++describe('llparse/spans', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ it('should invoke span callback', async () => {
++++++++ const start = p.node('start');
++++++++ const dot = p.node('dot');
++++++++ const dash = p.node('dash');
++++++++ const underscore = p.node('underscore');
++++++++
++++++++ const span = {
++++++++ dash: p.span(p.code.span('llparse__on_dash')),
++++++++ dot: p.span(p.code.span('llparse__on_dot')),
++++++++ underscore: p.span(p.code.span('llparse__on_underscore')),
++++++++ };
++++++++
++++++++ start.otherwise(span.dot.start(dot));
++++++++
++++++++ dot
++++++++ .match('.', dot)
++++++++ .peek('-', span.dash.start(dash))
++++++++ .peek('_', span.underscore.start(underscore))
++++++++ .skipTo(span.dot.end(start));
++++++++
++++++++ dash
++++++++ .match('-', dash)
++++++++ .otherwise(span.dash.end(dot));
++++++++
++++++++ underscore
++++++++ .match('_', underscore)
++++++++ .otherwise(span.underscore.end(dot));
++++++++
++++++++ const binary = await build(p, start, 'span');
++++++++ await binary.check('..--..__..',
++++++++ 'off=2 len=2 span[dash]="--"\n' +
++++++++ 'off=6 len=2 span[underscore]="__"\n' +
++++++++ 'off=0 len=10 span[dot]="..--..__.."\n');
++++++++ });
++++++++
++++++++ it('should return error', async () => {
++++++++ const start = p.node('start');
++++++++ const dot = p.node('dot');
++++++++
++++++++ const span = {
++++++++ pleaseFail: p.span(p.code.span('llparse__please_fail')),
++++++++ };
++++++++
++++++++ start.otherwise(span.pleaseFail.start(dot));
++++++++
++++++++ dot
++++++++ .match('.', dot)
++++++++ .skipTo(span.pleaseFail.end(start));
++++++++
++++++++ const binary = await build(p, start, 'span-error');
++++++++
++++++++ await binary.check(
++++++++ '....a',
++++++++ /off=\d+ error code=1 reason="please fail"\n/);
++++++++ });
++++++++
++++++++ it('should return error at `executeSpans()`', async () => {
++++++++ const start = p.node('start');
++++++++ const dot = p.node('dot');
++++++++
++++++++ const span = {
++++++++ pleaseFail: p.span(p.code.span('llparse__please_fail')),
++++++++ };
++++++++
++++++++ start.otherwise(span.pleaseFail.start(dot));
++++++++
++++++++ dot
++++++++ .match('.', dot)
++++++++ .skipTo(span.pleaseFail.end(start));
++++++++
++++++++ const binary = await build(p, start, 'span-error-execute');
++++++++
++++++++ await binary.check(
++++++++ '.........',
++++++++ /off=9 error code=1 reason="please fail"\n/, { scan: 100 });
++++++++ });
++++++++
++++++++ it('should not invoke spurious span callback', async () => {
++++++++ const start = p.node('start');
++++++++ const dot = p.node('dot');
++++++++ const span = p.span(p.code.span('llparse__on_dot'));
++++++++
++++++++ start
++++++++ .match('hello', span.start(dot))
++++++++ .skipTo(start);
++++++++
++++++++ dot
++++++++ .match('.', dot)
++++++++ .skipTo(span.end(start));
++++++++
++++++++ const binary = await build(p, start, 'span-spurious');
++++++++ await binary.check('hello', [ '' ]);
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++import * as assert from 'assert';
++++++++
++++++++import { LLParse } from '../src/api';
++++++++
++++++++import { build, printMatch, printOff } from './fixtures';
++++++++
++++++++describe('llparse/transform', () => {
++++++++ let p: LLParse;
++++++++
++++++++ beforeEach(() => {
++++++++ p = new LLParse();
++++++++ });
++++++++
++++++++ it('should apply transformation before the match', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .transform(p.transform.toLowerUnsafe())
++++++++ .match('connect', printOff(p, start))
++++++++ .match('close', printOff(p, start))
++++++++ .otherwise(p.error(1, 'error'));
++++++++
++++++++ const binary = await build(p, start, 'transform-lower');
++++++++ await binary.check('connectCLOSEcOnNeCt', 'off=7\noff=12\noff=19\n');
++++++++ });
++++++++
++++++++ it('should apply safe `toLower()` transformation', async () => {
++++++++ const start = p.node('start');
++++++++
++++++++ start
++++++++ .transform(p.transform.toLower())
++++++++ .select({
++++++++ 'a-b': 1,
++++++++ 'a\rb': 2,
++++++++ }, printMatch(p, start))
++++++++ .otherwise(p.error(1, 'error'));
++++++++
++++++++ const binary = await build(p, start, 'transform-safe-lower');
++++++++ await binary.check('A-ba\rB', 'off=3 match=1\noff=6 match=2\n');
++++++++ });
++++++++});
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "compilerOptions": {
++++++++ "strict": true,
++++++++ "target": "es2017",
++++++++ "module": "commonjs",
++++++++ "moduleResolution": "node",
++++++++ "outDir": "./lib",
++++++++ "declaration": true,
++++++++ "pretty": true,
++++++++ "sourceMap": true
++++++++ },
++++++++ "include": [
++++++++ "src/**/*.ts"
++++++++ ]
++++++++}
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
--- /dev/null
++++++++{
++++++++ "defaultSeverity": "error",
++++++++ "extends": [
++++++++ "tslint:recommended"
++++++++ ],
++++++++ "jsRules": {},
++++++++ "rules": {
++++++++ "no-bitwise": null,
++++++++ "max-line-length": [true, 80],
++++++++ "max-classes-per-file": [true, 1, "exclude-class-expressions"],
++++++++ "quotemark": [
++++++++ true, "single", "avoid-escape", "avoid-template"
++++++++ ]
++++++++ },
++++++++ "rulesDirectory": []
++++++++}