mirror of
https://github.com/kennethreitz/bake.git
synced 2026-06-05 23:00:17 +00:00
cleanup
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
Copyright 2010, 2011, Chris Winberry <chris@winberry.net>. 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.
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
# htmlparser2
|
||||
|
||||
[](https://npmjs.org/package/htmlparser2)
|
||||
[](https://npmjs.org/package/htmlparser2)
|
||||
[](http://travis-ci.org/fb55/htmlparser2)
|
||||
[](https://coveralls.io/r/fb55/htmlparser2)
|
||||
|
||||
A forgiving HTML/XML/RSS parser. The parser can handle streams and provides a callback interface.
|
||||
|
||||
## Installation
|
||||
npm install htmlparser2
|
||||
|
||||
A live demo of htmlparser2 is available [here](https://astexplorer.net/#/2AmVrGuGVJ).
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
var htmlparser = require("htmlparser2");
|
||||
var parser = new htmlparser.Parser({
|
||||
onopentag: function(name, attribs){
|
||||
if(name === "script" && attribs.type === "text/javascript"){
|
||||
console.log("JS! Hooray!");
|
||||
}
|
||||
},
|
||||
ontext: function(text){
|
||||
console.log("-->", text);
|
||||
},
|
||||
onclosetag: function(tagname){
|
||||
if(tagname === "script"){
|
||||
console.log("That's it?!");
|
||||
}
|
||||
}
|
||||
}, {decodeEntities: true});
|
||||
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</ script>");
|
||||
parser.end();
|
||||
```
|
||||
|
||||
Output (simplified):
|
||||
|
||||
```
|
||||
--> Xyz
|
||||
JS! Hooray!
|
||||
--> var foo = '<<bar>>';
|
||||
That's it?!
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Read more about the parser and its options in the [wiki](https://github.com/fb55/htmlparser2/wiki/Parser-options).
|
||||
|
||||
## Get a DOM
|
||||
The `DomHandler` (known as `DefaultHandler` in the original `htmlparser` module) produces a DOM (document object model) that can be manipulated using the [`DomUtils`](https://github.com/fb55/DomUtils) helper.
|
||||
|
||||
The `DomHandler`, while still bundled with this module, was moved to its [own module](https://github.com/fb55/domhandler). Have a look at it for further information.
|
||||
|
||||
## Parsing RSS/RDF/Atom Feeds
|
||||
|
||||
```javascript
|
||||
new htmlparser.FeedHandler(function(<error> error, <object> feed){
|
||||
...
|
||||
});
|
||||
```
|
||||
|
||||
Note: While the provided feed handler works for most feeds, you might want to use [danmactough/node-feedparser](https://github.com/danmactough/node-feedparser), which is much better tested and actively maintained.
|
||||
|
||||
## Performance
|
||||
|
||||
After having some artificial benchmarks for some time, __@AndreasMadsen__ published his [`htmlparser-benchmark`](https://github.com/AndreasMadsen/htmlparser-benchmark), which benchmarks HTML parses based on real-world websites.
|
||||
|
||||
At the time of writing, the latest versions of all supported parsers show the following performance characteristics on [Travis CI](https://travis-ci.org/AndreasMadsen/htmlparser-benchmark/builds/10805007) (please note that Travis doesn't guarantee equal conditions for all tests):
|
||||
|
||||
```
|
||||
gumbo-parser : 34.9208 ms/file ± 21.4238
|
||||
html-parser : 24.8224 ms/file ± 15.8703
|
||||
html5 : 419.597 ms/file ± 264.265
|
||||
htmlparser : 60.0722 ms/file ± 384.844
|
||||
htmlparser2-dom: 12.0749 ms/file ± 6.49474
|
||||
htmlparser2 : 7.49130 ms/file ± 5.74368
|
||||
hubbub : 30.4980 ms/file ± 16.4682
|
||||
libxmljs : 14.1338 ms/file ± 18.6541
|
||||
parse5 : 22.0439 ms/file ± 15.3743
|
||||
sax : 49.6513 ms/file ± 26.6032
|
||||
```
|
||||
|
||||
## How does this module differ from [node-htmlparser](https://github.com/tautologistics/node-htmlparser)?
|
||||
|
||||
This is a fork of the `htmlparser` module. The main difference is that this is intended to be used only with node (it runs on other platforms using [browserify](https://github.com/substack/node-browserify)). `htmlparser2` was rewritten multiple times and, while it maintains an API that's compatible with `htmlparser` in most cases, the projects don't share any code anymore.
|
||||
|
||||
The parser now provides a callback interface close to [sax.js](https://github.com/isaacs/sax-js) (originally targeted at [readabilitySAX](https://github.com/fb55/readabilitysax)). As a result, old handlers won't work anymore.
|
||||
|
||||
The `DefaultHandler` and the `RssHandler` were renamed to clarify their purpose (to `DomHandler` and `FeedHandler`). The old names are still available when requiring `htmlparser2`, your code should work as expected.
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
module.exports = CollectingHandler;
|
||||
|
||||
function CollectingHandler(cbs) {
|
||||
this._cbs = cbs || {};
|
||||
this.events = [];
|
||||
}
|
||||
|
||||
var EVENTS = require("./").EVENTS;
|
||||
Object.keys(EVENTS).forEach(function(name) {
|
||||
if (EVENTS[name] === 0) {
|
||||
name = "on" + name;
|
||||
CollectingHandler.prototype[name] = function() {
|
||||
this.events.push([name]);
|
||||
if (this._cbs[name]) this._cbs[name]();
|
||||
};
|
||||
} else if (EVENTS[name] === 1) {
|
||||
name = "on" + name;
|
||||
CollectingHandler.prototype[name] = function(a) {
|
||||
this.events.push([name, a]);
|
||||
if (this._cbs[name]) this._cbs[name](a);
|
||||
};
|
||||
} else if (EVENTS[name] === 2) {
|
||||
name = "on" + name;
|
||||
CollectingHandler.prototype[name] = function(a, b) {
|
||||
this.events.push([name, a, b]);
|
||||
if (this._cbs[name]) this._cbs[name](a, b);
|
||||
};
|
||||
} else {
|
||||
throw Error("wrong number of arguments");
|
||||
}
|
||||
});
|
||||
|
||||
CollectingHandler.prototype.onreset = function() {
|
||||
this.events = [];
|
||||
if (this._cbs.onreset) this._cbs.onreset();
|
||||
};
|
||||
|
||||
CollectingHandler.prototype.restart = function() {
|
||||
if (this._cbs.onreset) this._cbs.onreset();
|
||||
|
||||
for (var i = 0, len = this.events.length; i < len; i++) {
|
||||
if (this._cbs[this.events[i][0]]) {
|
||||
var num = this.events[i].length;
|
||||
|
||||
if (num === 1) {
|
||||
this._cbs[this.events[i][0]]();
|
||||
} else if (num === 2) {
|
||||
this._cbs[this.events[i][0]](this.events[i][1]);
|
||||
} else {
|
||||
this._cbs[this.events[i][0]](
|
||||
this.events[i][1],
|
||||
this.events[i][2]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
var DomHandler = require("domhandler");
|
||||
var DomUtils = require("domutils");
|
||||
|
||||
//TODO: make this a streamable handler
|
||||
function FeedHandler(callback, options) {
|
||||
this.init(callback, options);
|
||||
}
|
||||
|
||||
require("inherits")(FeedHandler, DomHandler);
|
||||
|
||||
FeedHandler.prototype.init = DomHandler;
|
||||
|
||||
function getElements(what, where) {
|
||||
return DomUtils.getElementsByTagName(what, where, true);
|
||||
}
|
||||
function getOneElement(what, where) {
|
||||
return DomUtils.getElementsByTagName(what, where, true, 1)[0];
|
||||
}
|
||||
function fetch(what, where, recurse) {
|
||||
return DomUtils.getText(
|
||||
DomUtils.getElementsByTagName(what, where, recurse, 1)
|
||||
).trim();
|
||||
}
|
||||
|
||||
function addConditionally(obj, prop, what, where, recurse) {
|
||||
var tmp = fetch(what, where, recurse);
|
||||
if (tmp) obj[prop] = tmp;
|
||||
}
|
||||
|
||||
var isValidFeed = function(value) {
|
||||
return value === "rss" || value === "feed" || value === "rdf:RDF";
|
||||
};
|
||||
|
||||
FeedHandler.prototype.onend = function() {
|
||||
var feed = {},
|
||||
feedRoot = getOneElement(isValidFeed, this.dom),
|
||||
tmp,
|
||||
childs;
|
||||
|
||||
if (feedRoot) {
|
||||
if (feedRoot.name === "feed") {
|
||||
childs = feedRoot.children;
|
||||
|
||||
feed.type = "atom";
|
||||
addConditionally(feed, "id", "id", childs);
|
||||
addConditionally(feed, "title", "title", childs);
|
||||
if (
|
||||
(tmp = getOneElement("link", childs)) &&
|
||||
(tmp = tmp.attribs) &&
|
||||
(tmp = tmp.href)
|
||||
)
|
||||
feed.link = tmp;
|
||||
addConditionally(feed, "description", "subtitle", childs);
|
||||
if ((tmp = fetch("updated", childs))) feed.updated = new Date(tmp);
|
||||
addConditionally(feed, "author", "email", childs, true);
|
||||
|
||||
feed.items = getElements("entry", childs).map(function(item) {
|
||||
var entry = {},
|
||||
tmp;
|
||||
|
||||
item = item.children;
|
||||
|
||||
addConditionally(entry, "id", "id", item);
|
||||
addConditionally(entry, "title", "title", item);
|
||||
if (
|
||||
(tmp = getOneElement("link", item)) &&
|
||||
(tmp = tmp.attribs) &&
|
||||
(tmp = tmp.href)
|
||||
)
|
||||
entry.link = tmp;
|
||||
if ((tmp = fetch("summary", item) || fetch("content", item)))
|
||||
entry.description = tmp;
|
||||
if ((tmp = fetch("updated", item)))
|
||||
entry.pubDate = new Date(tmp);
|
||||
return entry;
|
||||
});
|
||||
} else {
|
||||
childs = getOneElement("channel", feedRoot.children).children;
|
||||
|
||||
feed.type = feedRoot.name.substr(0, 3);
|
||||
feed.id = "";
|
||||
addConditionally(feed, "title", "title", childs);
|
||||
addConditionally(feed, "link", "link", childs);
|
||||
addConditionally(feed, "description", "description", childs);
|
||||
if ((tmp = fetch("lastBuildDate", childs)))
|
||||
feed.updated = new Date(tmp);
|
||||
addConditionally(feed, "author", "managingEditor", childs, true);
|
||||
|
||||
feed.items = getElements("item", feedRoot.children).map(function(
|
||||
item
|
||||
) {
|
||||
var entry = {},
|
||||
tmp;
|
||||
|
||||
item = item.children;
|
||||
|
||||
addConditionally(entry, "id", "guid", item);
|
||||
addConditionally(entry, "title", "title", item);
|
||||
addConditionally(entry, "link", "link", item);
|
||||
addConditionally(entry, "description", "description", item);
|
||||
if ((tmp = fetch("pubDate", item)))
|
||||
entry.pubDate = new Date(tmp);
|
||||
return entry;
|
||||
});
|
||||
}
|
||||
}
|
||||
this.dom = feed;
|
||||
DomHandler.prototype._handleCallback.call(
|
||||
this,
|
||||
feedRoot ? null : Error("couldn't find root of feed")
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = FeedHandler;
|
||||
+382
@@ -0,0 +1,382 @@
|
||||
var Tokenizer = require("./Tokenizer.js");
|
||||
|
||||
/*
|
||||
Options:
|
||||
|
||||
xmlMode: Disables the special behavior for script/style tags (false by default)
|
||||
lowerCaseAttributeNames: call .toLowerCase for each attribute name (true if xmlMode is `false`)
|
||||
lowerCaseTags: call .toLowerCase for each tag name (true if xmlMode is `false`)
|
||||
*/
|
||||
|
||||
/*
|
||||
Callbacks:
|
||||
|
||||
oncdataend,
|
||||
oncdatastart,
|
||||
onclosetag,
|
||||
oncomment,
|
||||
oncommentend,
|
||||
onerror,
|
||||
onopentag,
|
||||
onprocessinginstruction,
|
||||
onreset,
|
||||
ontext
|
||||
*/
|
||||
|
||||
var formTags = {
|
||||
input: true,
|
||||
option: true,
|
||||
optgroup: true,
|
||||
select: true,
|
||||
button: true,
|
||||
datalist: true,
|
||||
textarea: true
|
||||
};
|
||||
|
||||
var openImpliesClose = {
|
||||
tr: { tr: true, th: true, td: true },
|
||||
th: { th: true },
|
||||
td: { thead: true, th: true, td: true },
|
||||
body: { head: true, link: true, script: true },
|
||||
li: { li: true },
|
||||
p: { p: true },
|
||||
h1: { p: true },
|
||||
h2: { p: true },
|
||||
h3: { p: true },
|
||||
h4: { p: true },
|
||||
h5: { p: true },
|
||||
h6: { p: true },
|
||||
select: formTags,
|
||||
input: formTags,
|
||||
output: formTags,
|
||||
button: formTags,
|
||||
datalist: formTags,
|
||||
textarea: formTags,
|
||||
option: { option: true },
|
||||
optgroup: { optgroup: true }
|
||||
};
|
||||
|
||||
var voidElements = {
|
||||
__proto__: null,
|
||||
area: true,
|
||||
base: true,
|
||||
basefont: true,
|
||||
br: true,
|
||||
col: true,
|
||||
command: true,
|
||||
embed: true,
|
||||
frame: true,
|
||||
hr: true,
|
||||
img: true,
|
||||
input: true,
|
||||
isindex: true,
|
||||
keygen: true,
|
||||
link: true,
|
||||
meta: true,
|
||||
param: true,
|
||||
source: true,
|
||||
track: true,
|
||||
wbr: true
|
||||
};
|
||||
|
||||
var foreignContextElements = {
|
||||
__proto__: null,
|
||||
math: true,
|
||||
svg: true
|
||||
};
|
||||
var htmlIntegrationElements = {
|
||||
__proto__: null,
|
||||
mi: true,
|
||||
mo: true,
|
||||
mn: true,
|
||||
ms: true,
|
||||
mtext: true,
|
||||
"annotation-xml": true,
|
||||
foreignObject: true,
|
||||
desc: true,
|
||||
title: true
|
||||
};
|
||||
|
||||
var re_nameEnd = /\s|\//;
|
||||
|
||||
function Parser(cbs, options) {
|
||||
this._options = options || {};
|
||||
this._cbs = cbs || {};
|
||||
|
||||
this._tagname = "";
|
||||
this._attribname = "";
|
||||
this._attribvalue = "";
|
||||
this._attribs = null;
|
||||
this._stack = [];
|
||||
this._foreignContext = [];
|
||||
|
||||
this.startIndex = 0;
|
||||
this.endIndex = null;
|
||||
|
||||
this._lowerCaseTagNames =
|
||||
"lowerCaseTags" in this._options
|
||||
? !!this._options.lowerCaseTags
|
||||
: !this._options.xmlMode;
|
||||
this._lowerCaseAttributeNames =
|
||||
"lowerCaseAttributeNames" in this._options
|
||||
? !!this._options.lowerCaseAttributeNames
|
||||
: !this._options.xmlMode;
|
||||
|
||||
if (this._options.Tokenizer) {
|
||||
Tokenizer = this._options.Tokenizer;
|
||||
}
|
||||
this._tokenizer = new Tokenizer(this._options, this);
|
||||
|
||||
if (this._cbs.onparserinit) this._cbs.onparserinit(this);
|
||||
}
|
||||
|
||||
require("inherits")(Parser, require("events").EventEmitter);
|
||||
|
||||
Parser.prototype._updatePosition = function(initialOffset) {
|
||||
if (this.endIndex === null) {
|
||||
if (this._tokenizer._sectionStart <= initialOffset) {
|
||||
this.startIndex = 0;
|
||||
} else {
|
||||
this.startIndex = this._tokenizer._sectionStart - initialOffset;
|
||||
}
|
||||
} else this.startIndex = this.endIndex + 1;
|
||||
this.endIndex = this._tokenizer.getAbsoluteIndex();
|
||||
};
|
||||
|
||||
//Tokenizer event handlers
|
||||
Parser.prototype.ontext = function(data) {
|
||||
this._updatePosition(1);
|
||||
this.endIndex--;
|
||||
|
||||
if (this._cbs.ontext) this._cbs.ontext(data);
|
||||
};
|
||||
|
||||
Parser.prototype.onopentagname = function(name) {
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
|
||||
this._tagname = name;
|
||||
|
||||
if (!this._options.xmlMode && name in openImpliesClose) {
|
||||
for (
|
||||
var el;
|
||||
(el = this._stack[this._stack.length - 1]) in
|
||||
openImpliesClose[name];
|
||||
this.onclosetag(el)
|
||||
);
|
||||
}
|
||||
|
||||
if (this._options.xmlMode || !(name in voidElements)) {
|
||||
this._stack.push(name);
|
||||
if (name in foreignContextElements) this._foreignContext.push(true);
|
||||
else if (name in htmlIntegrationElements)
|
||||
this._foreignContext.push(false);
|
||||
}
|
||||
|
||||
if (this._cbs.onopentagname) this._cbs.onopentagname(name);
|
||||
if (this._cbs.onopentag) this._attribs = {};
|
||||
};
|
||||
|
||||
Parser.prototype.onopentagend = function() {
|
||||
this._updatePosition(1);
|
||||
|
||||
if (this._attribs) {
|
||||
if (this._cbs.onopentag)
|
||||
this._cbs.onopentag(this._tagname, this._attribs);
|
||||
this._attribs = null;
|
||||
}
|
||||
|
||||
if (
|
||||
!this._options.xmlMode &&
|
||||
this._cbs.onclosetag &&
|
||||
this._tagname in voidElements
|
||||
) {
|
||||
this._cbs.onclosetag(this._tagname);
|
||||
}
|
||||
|
||||
this._tagname = "";
|
||||
};
|
||||
|
||||
Parser.prototype.onclosetag = function(name) {
|
||||
this._updatePosition(1);
|
||||
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
|
||||
if (name in foreignContextElements || name in htmlIntegrationElements) {
|
||||
this._foreignContext.pop();
|
||||
}
|
||||
|
||||
if (
|
||||
this._stack.length &&
|
||||
(!(name in voidElements) || this._options.xmlMode)
|
||||
) {
|
||||
var pos = this._stack.lastIndexOf(name);
|
||||
if (pos !== -1) {
|
||||
if (this._cbs.onclosetag) {
|
||||
pos = this._stack.length - pos;
|
||||
while (pos--) this._cbs.onclosetag(this._stack.pop());
|
||||
} else this._stack.length = pos;
|
||||
} else if (name === "p" && !this._options.xmlMode) {
|
||||
this.onopentagname(name);
|
||||
this._closeCurrentTag();
|
||||
}
|
||||
} else if (!this._options.xmlMode && (name === "br" || name === "p")) {
|
||||
this.onopentagname(name);
|
||||
this._closeCurrentTag();
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype.onselfclosingtag = function() {
|
||||
if (
|
||||
this._options.xmlMode ||
|
||||
this._options.recognizeSelfClosing ||
|
||||
this._foreignContext[this._foreignContext.length - 1]
|
||||
) {
|
||||
this._closeCurrentTag();
|
||||
} else {
|
||||
this.onopentagend();
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype._closeCurrentTag = function() {
|
||||
var name = this._tagname;
|
||||
|
||||
this.onopentagend();
|
||||
|
||||
//self-closing tags will be on the top of the stack
|
||||
//(cheaper check than in onclosetag)
|
||||
if (this._stack[this._stack.length - 1] === name) {
|
||||
if (this._cbs.onclosetag) {
|
||||
this._cbs.onclosetag(name);
|
||||
}
|
||||
this._stack.pop();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype.onattribname = function(name) {
|
||||
if (this._lowerCaseAttributeNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
this._attribname = name;
|
||||
};
|
||||
|
||||
Parser.prototype.onattribdata = function(value) {
|
||||
this._attribvalue += value;
|
||||
};
|
||||
|
||||
Parser.prototype.onattribend = function() {
|
||||
if (this._cbs.onattribute)
|
||||
this._cbs.onattribute(this._attribname, this._attribvalue);
|
||||
if (
|
||||
this._attribs &&
|
||||
!Object.prototype.hasOwnProperty.call(this._attribs, this._attribname)
|
||||
) {
|
||||
this._attribs[this._attribname] = this._attribvalue;
|
||||
}
|
||||
this._attribname = "";
|
||||
this._attribvalue = "";
|
||||
};
|
||||
|
||||
Parser.prototype._getInstructionName = function(value) {
|
||||
var idx = value.search(re_nameEnd),
|
||||
name = idx < 0 ? value : value.substr(0, idx);
|
||||
|
||||
if (this._lowerCaseTagNames) {
|
||||
name = name.toLowerCase();
|
||||
}
|
||||
|
||||
return name;
|
||||
};
|
||||
|
||||
Parser.prototype.ondeclaration = function(value) {
|
||||
if (this._cbs.onprocessinginstruction) {
|
||||
var name = this._getInstructionName(value);
|
||||
this._cbs.onprocessinginstruction("!" + name, "!" + value);
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype.onprocessinginstruction = function(value) {
|
||||
if (this._cbs.onprocessinginstruction) {
|
||||
var name = this._getInstructionName(value);
|
||||
this._cbs.onprocessinginstruction("?" + name, "?" + value);
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype.oncomment = function(value) {
|
||||
this._updatePosition(4);
|
||||
|
||||
if (this._cbs.oncomment) this._cbs.oncomment(value);
|
||||
if (this._cbs.oncommentend) this._cbs.oncommentend();
|
||||
};
|
||||
|
||||
Parser.prototype.oncdata = function(value) {
|
||||
this._updatePosition(1);
|
||||
|
||||
if (this._options.xmlMode || this._options.recognizeCDATA) {
|
||||
if (this._cbs.oncdatastart) this._cbs.oncdatastart();
|
||||
if (this._cbs.ontext) this._cbs.ontext(value);
|
||||
if (this._cbs.oncdataend) this._cbs.oncdataend();
|
||||
} else {
|
||||
this.oncomment("[CDATA[" + value + "]]");
|
||||
}
|
||||
};
|
||||
|
||||
Parser.prototype.onerror = function(err) {
|
||||
if (this._cbs.onerror) this._cbs.onerror(err);
|
||||
};
|
||||
|
||||
Parser.prototype.onend = function() {
|
||||
if (this._cbs.onclosetag) {
|
||||
for (
|
||||
var i = this._stack.length;
|
||||
i > 0;
|
||||
this._cbs.onclosetag(this._stack[--i])
|
||||
);
|
||||
}
|
||||
if (this._cbs.onend) this._cbs.onend();
|
||||
};
|
||||
|
||||
//Resets the parser to a blank state, ready to parse a new HTML document
|
||||
Parser.prototype.reset = function() {
|
||||
if (this._cbs.onreset) this._cbs.onreset();
|
||||
this._tokenizer.reset();
|
||||
|
||||
this._tagname = "";
|
||||
this._attribname = "";
|
||||
this._attribs = null;
|
||||
this._stack = [];
|
||||
|
||||
if (this._cbs.onparserinit) this._cbs.onparserinit(this);
|
||||
};
|
||||
|
||||
//Parses a complete HTML document and pushes it to the handler
|
||||
Parser.prototype.parseComplete = function(data) {
|
||||
this.reset();
|
||||
this.end(data);
|
||||
};
|
||||
|
||||
Parser.prototype.write = function(chunk) {
|
||||
this._tokenizer.write(chunk);
|
||||
};
|
||||
|
||||
Parser.prototype.end = function(chunk) {
|
||||
this._tokenizer.end(chunk);
|
||||
};
|
||||
|
||||
Parser.prototype.pause = function() {
|
||||
this._tokenizer.pause();
|
||||
};
|
||||
|
||||
Parser.prototype.resume = function() {
|
||||
this._tokenizer.resume();
|
||||
};
|
||||
|
||||
//alias for backwards compat
|
||||
Parser.prototype.parseChunk = Parser.prototype.write;
|
||||
Parser.prototype.done = Parser.prototype.end;
|
||||
|
||||
module.exports = Parser;
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
module.exports = ProxyHandler;
|
||||
|
||||
function ProxyHandler(cbs) {
|
||||
this._cbs = cbs || {};
|
||||
}
|
||||
|
||||
var EVENTS = require("./").EVENTS;
|
||||
Object.keys(EVENTS).forEach(function(name) {
|
||||
if (EVENTS[name] === 0) {
|
||||
name = "on" + name;
|
||||
ProxyHandler.prototype[name] = function() {
|
||||
if (this._cbs[name]) this._cbs[name]();
|
||||
};
|
||||
} else if (EVENTS[name] === 1) {
|
||||
name = "on" + name;
|
||||
ProxyHandler.prototype[name] = function(a) {
|
||||
if (this._cbs[name]) this._cbs[name](a);
|
||||
};
|
||||
} else if (EVENTS[name] === 2) {
|
||||
name = "on" + name;
|
||||
ProxyHandler.prototype[name] = function(a, b) {
|
||||
if (this._cbs[name]) this._cbs[name](a, b);
|
||||
};
|
||||
} else {
|
||||
throw Error("wrong number of arguments");
|
||||
}
|
||||
});
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
module.exports = Stream;
|
||||
|
||||
var Parser = require("./WritableStream.js");
|
||||
|
||||
function Stream(options) {
|
||||
Parser.call(this, new Cbs(this), options);
|
||||
}
|
||||
|
||||
require("inherits")(Stream, Parser);
|
||||
|
||||
Stream.prototype.readable = true;
|
||||
|
||||
function Cbs(scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
var EVENTS = require("../").EVENTS;
|
||||
|
||||
Object.keys(EVENTS).forEach(function(name) {
|
||||
if (EVENTS[name] === 0) {
|
||||
Cbs.prototype["on" + name] = function() {
|
||||
this.scope.emit(name);
|
||||
};
|
||||
} else if (EVENTS[name] === 1) {
|
||||
Cbs.prototype["on" + name] = function(a) {
|
||||
this.scope.emit(name, a);
|
||||
};
|
||||
} else if (EVENTS[name] === 2) {
|
||||
Cbs.prototype["on" + name] = function(a, b) {
|
||||
this.scope.emit(name, a, b);
|
||||
};
|
||||
} else {
|
||||
throw Error("wrong number of arguments!");
|
||||
}
|
||||
});
|
||||
+970
@@ -0,0 +1,970 @@
|
||||
module.exports = Tokenizer;
|
||||
|
||||
var decodeCodePoint = require("entities/lib/decode_codepoint.js");
|
||||
var entityMap = require("entities/maps/entities.json");
|
||||
var legacyMap = require("entities/maps/legacy.json");
|
||||
var xmlMap = require("entities/maps/xml.json");
|
||||
|
||||
var i = 0;
|
||||
|
||||
var TEXT = i++;
|
||||
var BEFORE_TAG_NAME = i++; //after <
|
||||
var IN_TAG_NAME = i++;
|
||||
var IN_SELF_CLOSING_TAG = i++;
|
||||
var BEFORE_CLOSING_TAG_NAME = i++;
|
||||
var IN_CLOSING_TAG_NAME = i++;
|
||||
var AFTER_CLOSING_TAG_NAME = i++;
|
||||
|
||||
//attributes
|
||||
var BEFORE_ATTRIBUTE_NAME = i++;
|
||||
var IN_ATTRIBUTE_NAME = i++;
|
||||
var AFTER_ATTRIBUTE_NAME = i++;
|
||||
var BEFORE_ATTRIBUTE_VALUE = i++;
|
||||
var IN_ATTRIBUTE_VALUE_DQ = i++; // "
|
||||
var IN_ATTRIBUTE_VALUE_SQ = i++; // '
|
||||
var IN_ATTRIBUTE_VALUE_NQ = i++;
|
||||
|
||||
//declarations
|
||||
var BEFORE_DECLARATION = i++; // !
|
||||
var IN_DECLARATION = i++;
|
||||
|
||||
//processing instructions
|
||||
var IN_PROCESSING_INSTRUCTION = i++; // ?
|
||||
|
||||
//comments
|
||||
var BEFORE_COMMENT = i++;
|
||||
var IN_COMMENT = i++;
|
||||
var AFTER_COMMENT_1 = i++;
|
||||
var AFTER_COMMENT_2 = i++;
|
||||
|
||||
//cdata
|
||||
var BEFORE_CDATA_1 = i++; // [
|
||||
var BEFORE_CDATA_2 = i++; // C
|
||||
var BEFORE_CDATA_3 = i++; // D
|
||||
var BEFORE_CDATA_4 = i++; // A
|
||||
var BEFORE_CDATA_5 = i++; // T
|
||||
var BEFORE_CDATA_6 = i++; // A
|
||||
var IN_CDATA = i++; // [
|
||||
var AFTER_CDATA_1 = i++; // ]
|
||||
var AFTER_CDATA_2 = i++; // ]
|
||||
|
||||
//special tags
|
||||
var BEFORE_SPECIAL = i++; //S
|
||||
var BEFORE_SPECIAL_END = i++; //S
|
||||
|
||||
var BEFORE_SCRIPT_1 = i++; //C
|
||||
var BEFORE_SCRIPT_2 = i++; //R
|
||||
var BEFORE_SCRIPT_3 = i++; //I
|
||||
var BEFORE_SCRIPT_4 = i++; //P
|
||||
var BEFORE_SCRIPT_5 = i++; //T
|
||||
var AFTER_SCRIPT_1 = i++; //C
|
||||
var AFTER_SCRIPT_2 = i++; //R
|
||||
var AFTER_SCRIPT_3 = i++; //I
|
||||
var AFTER_SCRIPT_4 = i++; //P
|
||||
var AFTER_SCRIPT_5 = i++; //T
|
||||
|
||||
var BEFORE_STYLE_1 = i++; //T
|
||||
var BEFORE_STYLE_2 = i++; //Y
|
||||
var BEFORE_STYLE_3 = i++; //L
|
||||
var BEFORE_STYLE_4 = i++; //E
|
||||
var AFTER_STYLE_1 = i++; //T
|
||||
var AFTER_STYLE_2 = i++; //Y
|
||||
var AFTER_STYLE_3 = i++; //L
|
||||
var AFTER_STYLE_4 = i++; //E
|
||||
|
||||
var BEFORE_ENTITY = i++; //&
|
||||
var BEFORE_NUMERIC_ENTITY = i++; //#
|
||||
var IN_NAMED_ENTITY = i++;
|
||||
var IN_NUMERIC_ENTITY = i++;
|
||||
var IN_HEX_ENTITY = i++; //X
|
||||
|
||||
var j = 0;
|
||||
|
||||
var SPECIAL_NONE = j++;
|
||||
var SPECIAL_SCRIPT = j++;
|
||||
var SPECIAL_STYLE = j++;
|
||||
|
||||
function whitespace(c) {
|
||||
return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
|
||||
}
|
||||
|
||||
function ifElseState(upper, SUCCESS, FAILURE) {
|
||||
var lower = upper.toLowerCase();
|
||||
|
||||
if (upper === lower) {
|
||||
return function(c) {
|
||||
if (c === lower) {
|
||||
this._state = SUCCESS;
|
||||
} else {
|
||||
this._state = FAILURE;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return function(c) {
|
||||
if (c === lower || c === upper) {
|
||||
this._state = SUCCESS;
|
||||
} else {
|
||||
this._state = FAILURE;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function consumeSpecialNameChar(upper, NEXT_STATE) {
|
||||
var lower = upper.toLowerCase();
|
||||
|
||||
return function(c) {
|
||||
if (c === lower || c === upper) {
|
||||
this._state = NEXT_STATE;
|
||||
} else {
|
||||
this._state = IN_TAG_NAME;
|
||||
this._index--; //consume the token again
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function Tokenizer(options, cbs) {
|
||||
this._state = TEXT;
|
||||
this._buffer = "";
|
||||
this._sectionStart = 0;
|
||||
this._index = 0;
|
||||
this._bufferOffset = 0; //chars removed from _buffer
|
||||
this._baseState = TEXT;
|
||||
this._special = SPECIAL_NONE;
|
||||
this._cbs = cbs;
|
||||
this._running = true;
|
||||
this._ended = false;
|
||||
this._xmlMode = !!(options && options.xmlMode);
|
||||
this._decodeEntities = !!(options && options.decodeEntities);
|
||||
}
|
||||
|
||||
Tokenizer.prototype._stateText = function(c) {
|
||||
if (c === "<") {
|
||||
if (this._index > this._sectionStart) {
|
||||
this._cbs.ontext(this._getSection());
|
||||
}
|
||||
this._state = BEFORE_TAG_NAME;
|
||||
this._sectionStart = this._index;
|
||||
} else if (
|
||||
this._decodeEntities &&
|
||||
this._special === SPECIAL_NONE &&
|
||||
c === "&"
|
||||
) {
|
||||
if (this._index > this._sectionStart) {
|
||||
this._cbs.ontext(this._getSection());
|
||||
}
|
||||
this._baseState = TEXT;
|
||||
this._state = BEFORE_ENTITY;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeTagName = function(c) {
|
||||
if (c === "/") {
|
||||
this._state = BEFORE_CLOSING_TAG_NAME;
|
||||
} else if (c === "<") {
|
||||
this._cbs.ontext(this._getSection());
|
||||
this._sectionStart = this._index;
|
||||
} else if (c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {
|
||||
this._state = TEXT;
|
||||
} else if (c === "!") {
|
||||
this._state = BEFORE_DECLARATION;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (c === "?") {
|
||||
this._state = IN_PROCESSING_INSTRUCTION;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else {
|
||||
this._state =
|
||||
!this._xmlMode && (c === "s" || c === "S")
|
||||
? BEFORE_SPECIAL
|
||||
: IN_TAG_NAME;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInTagName = function(c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._emitToken("onopentagname");
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
|
||||
if (whitespace(c));
|
||||
else if (c === ">") {
|
||||
this._state = TEXT;
|
||||
} else if (this._special !== SPECIAL_NONE) {
|
||||
if (c === "s" || c === "S") {
|
||||
this._state = BEFORE_SPECIAL_END;
|
||||
} else {
|
||||
this._state = TEXT;
|
||||
this._index--;
|
||||
}
|
||||
} else {
|
||||
this._state = IN_CLOSING_TAG_NAME;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInCloseingTagName = function(c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._emitToken("onclosetag");
|
||||
this._state = AFTER_CLOSING_TAG_NAME;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterCloseingTagName = function(c) {
|
||||
//skip everything until ">"
|
||||
if (c === ">") {
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeAttributeName = function(c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onopentagend();
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (c === "/") {
|
||||
this._state = IN_SELF_CLOSING_TAG;
|
||||
} else if (!whitespace(c)) {
|
||||
this._state = IN_ATTRIBUTE_NAME;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInSelfClosingTag = function(c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onselfclosingtag();
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (!whitespace(c)) {
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInAttributeName = function(c) {
|
||||
if (c === "=" || c === "/" || c === ">" || whitespace(c)) {
|
||||
this._cbs.onattribname(this._getSection());
|
||||
this._sectionStart = -1;
|
||||
this._state = AFTER_ATTRIBUTE_NAME;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterAttributeName = function(c) {
|
||||
if (c === "=") {
|
||||
this._state = BEFORE_ATTRIBUTE_VALUE;
|
||||
} else if (c === "/" || c === ">") {
|
||||
this._cbs.onattribend();
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
this._index--;
|
||||
} else if (!whitespace(c)) {
|
||||
this._cbs.onattribend();
|
||||
this._state = IN_ATTRIBUTE_NAME;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeAttributeValue = function(c) {
|
||||
if (c === '"') {
|
||||
this._state = IN_ATTRIBUTE_VALUE_DQ;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (c === "'") {
|
||||
this._state = IN_ATTRIBUTE_VALUE_SQ;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (!whitespace(c)) {
|
||||
this._state = IN_ATTRIBUTE_VALUE_NQ;
|
||||
this._sectionStart = this._index;
|
||||
this._index--; //reconsume token
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInAttributeValueDoubleQuotes = function(c) {
|
||||
if (c === '"') {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
} else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = BEFORE_ENTITY;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInAttributeValueSingleQuotes = function(c) {
|
||||
if (c === "'") {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
} else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = BEFORE_ENTITY;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInAttributeValueNoQuotes = function(c) {
|
||||
if (whitespace(c) || c === ">") {
|
||||
this._emitToken("onattribdata");
|
||||
this._cbs.onattribend();
|
||||
this._state = BEFORE_ATTRIBUTE_NAME;
|
||||
this._index--;
|
||||
} else if (this._decodeEntities && c === "&") {
|
||||
this._emitToken("onattribdata");
|
||||
this._baseState = this._state;
|
||||
this._state = BEFORE_ENTITY;
|
||||
this._sectionStart = this._index;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeDeclaration = function(c) {
|
||||
this._state =
|
||||
c === "["
|
||||
? BEFORE_CDATA_1
|
||||
: c === "-"
|
||||
? BEFORE_COMMENT
|
||||
: IN_DECLARATION;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInDeclaration = function(c) {
|
||||
if (c === ">") {
|
||||
this._cbs.ondeclaration(this._getSection());
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInProcessingInstruction = function(c) {
|
||||
if (c === ">") {
|
||||
this._cbs.onprocessinginstruction(this._getSection());
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeComment = function(c) {
|
||||
if (c === "-") {
|
||||
this._state = IN_COMMENT;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else {
|
||||
this._state = IN_DECLARATION;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInComment = function(c) {
|
||||
if (c === "-") this._state = AFTER_COMMENT_1;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterComment1 = function(c) {
|
||||
if (c === "-") {
|
||||
this._state = AFTER_COMMENT_2;
|
||||
} else {
|
||||
this._state = IN_COMMENT;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterComment2 = function(c) {
|
||||
if (c === ">") {
|
||||
//remove 2 trailing chars
|
||||
this._cbs.oncomment(
|
||||
this._buffer.substring(this._sectionStart, this._index - 2)
|
||||
);
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (c !== "-") {
|
||||
this._state = IN_COMMENT;
|
||||
}
|
||||
// else: stay in AFTER_COMMENT_2 (`--->`)
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeCdata1 = ifElseState(
|
||||
"C",
|
||||
BEFORE_CDATA_2,
|
||||
IN_DECLARATION
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeCdata2 = ifElseState(
|
||||
"D",
|
||||
BEFORE_CDATA_3,
|
||||
IN_DECLARATION
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeCdata3 = ifElseState(
|
||||
"A",
|
||||
BEFORE_CDATA_4,
|
||||
IN_DECLARATION
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeCdata4 = ifElseState(
|
||||
"T",
|
||||
BEFORE_CDATA_5,
|
||||
IN_DECLARATION
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeCdata5 = ifElseState(
|
||||
"A",
|
||||
BEFORE_CDATA_6,
|
||||
IN_DECLARATION
|
||||
);
|
||||
|
||||
Tokenizer.prototype._stateBeforeCdata6 = function(c) {
|
||||
if (c === "[") {
|
||||
this._state = IN_CDATA;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else {
|
||||
this._state = IN_DECLARATION;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInCdata = function(c) {
|
||||
if (c === "]") this._state = AFTER_CDATA_1;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterCdata1 = function(c) {
|
||||
if (c === "]") this._state = AFTER_CDATA_2;
|
||||
else this._state = IN_CDATA;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterCdata2 = function(c) {
|
||||
if (c === ">") {
|
||||
//remove 2 trailing chars
|
||||
this._cbs.oncdata(
|
||||
this._buffer.substring(this._sectionStart, this._index - 2)
|
||||
);
|
||||
this._state = TEXT;
|
||||
this._sectionStart = this._index + 1;
|
||||
} else if (c !== "]") {
|
||||
this._state = IN_CDATA;
|
||||
}
|
||||
//else: stay in AFTER_CDATA_2 (`]]]>`)
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeSpecial = function(c) {
|
||||
if (c === "c" || c === "C") {
|
||||
this._state = BEFORE_SCRIPT_1;
|
||||
} else if (c === "t" || c === "T") {
|
||||
this._state = BEFORE_STYLE_1;
|
||||
} else {
|
||||
this._state = IN_TAG_NAME;
|
||||
this._index--; //consume the token again
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeSpecialEnd = function(c) {
|
||||
if (this._special === SPECIAL_SCRIPT && (c === "c" || c === "C")) {
|
||||
this._state = AFTER_SCRIPT_1;
|
||||
} else if (this._special === SPECIAL_STYLE && (c === "t" || c === "T")) {
|
||||
this._state = AFTER_STYLE_1;
|
||||
} else this._state = TEXT;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeScript1 = consumeSpecialNameChar(
|
||||
"R",
|
||||
BEFORE_SCRIPT_2
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeScript2 = consumeSpecialNameChar(
|
||||
"I",
|
||||
BEFORE_SCRIPT_3
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeScript3 = consumeSpecialNameChar(
|
||||
"P",
|
||||
BEFORE_SCRIPT_4
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeScript4 = consumeSpecialNameChar(
|
||||
"T",
|
||||
BEFORE_SCRIPT_5
|
||||
);
|
||||
|
||||
Tokenizer.prototype._stateBeforeScript5 = function(c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._special = SPECIAL_SCRIPT;
|
||||
}
|
||||
this._state = IN_TAG_NAME;
|
||||
this._index--; //consume the token again
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterScript1 = ifElseState("R", AFTER_SCRIPT_2, TEXT);
|
||||
Tokenizer.prototype._stateAfterScript2 = ifElseState("I", AFTER_SCRIPT_3, TEXT);
|
||||
Tokenizer.prototype._stateAfterScript3 = ifElseState("P", AFTER_SCRIPT_4, TEXT);
|
||||
Tokenizer.prototype._stateAfterScript4 = ifElseState("T", AFTER_SCRIPT_5, TEXT);
|
||||
|
||||
Tokenizer.prototype._stateAfterScript5 = function(c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._special = SPECIAL_NONE;
|
||||
this._state = IN_CLOSING_TAG_NAME;
|
||||
this._sectionStart = this._index - 6;
|
||||
this._index--; //reconsume the token
|
||||
} else this._state = TEXT;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeStyle1 = consumeSpecialNameChar(
|
||||
"Y",
|
||||
BEFORE_STYLE_2
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeStyle2 = consumeSpecialNameChar(
|
||||
"L",
|
||||
BEFORE_STYLE_3
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeStyle3 = consumeSpecialNameChar(
|
||||
"E",
|
||||
BEFORE_STYLE_4
|
||||
);
|
||||
|
||||
Tokenizer.prototype._stateBeforeStyle4 = function(c) {
|
||||
if (c === "/" || c === ">" || whitespace(c)) {
|
||||
this._special = SPECIAL_STYLE;
|
||||
}
|
||||
this._state = IN_TAG_NAME;
|
||||
this._index--; //consume the token again
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateAfterStyle1 = ifElseState("Y", AFTER_STYLE_2, TEXT);
|
||||
Tokenizer.prototype._stateAfterStyle2 = ifElseState("L", AFTER_STYLE_3, TEXT);
|
||||
Tokenizer.prototype._stateAfterStyle3 = ifElseState("E", AFTER_STYLE_4, TEXT);
|
||||
|
||||
Tokenizer.prototype._stateAfterStyle4 = function(c) {
|
||||
if (c === ">" || whitespace(c)) {
|
||||
this._special = SPECIAL_NONE;
|
||||
this._state = IN_CLOSING_TAG_NAME;
|
||||
this._sectionStart = this._index - 5;
|
||||
this._index--; //reconsume the token
|
||||
} else this._state = TEXT;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateBeforeEntity = ifElseState(
|
||||
"#",
|
||||
BEFORE_NUMERIC_ENTITY,
|
||||
IN_NAMED_ENTITY
|
||||
);
|
||||
Tokenizer.prototype._stateBeforeNumericEntity = ifElseState(
|
||||
"X",
|
||||
IN_HEX_ENTITY,
|
||||
IN_NUMERIC_ENTITY
|
||||
);
|
||||
|
||||
//for entities terminated with a semicolon
|
||||
Tokenizer.prototype._parseNamedEntityStrict = function() {
|
||||
//offset = 1
|
||||
if (this._sectionStart + 1 < this._index) {
|
||||
var entity = this._buffer.substring(
|
||||
this._sectionStart + 1,
|
||||
this._index
|
||||
),
|
||||
map = this._xmlMode ? xmlMap : entityMap;
|
||||
|
||||
if (map.hasOwnProperty(entity)) {
|
||||
this._emitPartial(map[entity]);
|
||||
this._sectionStart = this._index + 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//parses legacy entities (without trailing semicolon)
|
||||
Tokenizer.prototype._parseLegacyEntity = function() {
|
||||
var start = this._sectionStart + 1,
|
||||
limit = this._index - start;
|
||||
|
||||
if (limit > 6) limit = 6; //the max length of legacy entities is 6
|
||||
|
||||
while (limit >= 2) {
|
||||
//the min length of legacy entities is 2
|
||||
var entity = this._buffer.substr(start, limit);
|
||||
|
||||
if (legacyMap.hasOwnProperty(entity)) {
|
||||
this._emitPartial(legacyMap[entity]);
|
||||
this._sectionStart += limit + 1;
|
||||
return;
|
||||
} else {
|
||||
limit--;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInNamedEntity = function(c) {
|
||||
if (c === ";") {
|
||||
this._parseNamedEntityStrict();
|
||||
if (this._sectionStart + 1 < this._index && !this._xmlMode) {
|
||||
this._parseLegacyEntity();
|
||||
}
|
||||
this._state = this._baseState;
|
||||
} else if (
|
||||
(c < "a" || c > "z") &&
|
||||
(c < "A" || c > "Z") &&
|
||||
(c < "0" || c > "9")
|
||||
) {
|
||||
if (this._xmlMode);
|
||||
else if (this._sectionStart + 1 === this._index);
|
||||
else if (this._baseState !== TEXT) {
|
||||
if (c !== "=") {
|
||||
this._parseNamedEntityStrict();
|
||||
}
|
||||
} else {
|
||||
this._parseLegacyEntity();
|
||||
}
|
||||
|
||||
this._state = this._baseState;
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._decodeNumericEntity = function(offset, base) {
|
||||
var sectionStart = this._sectionStart + offset;
|
||||
|
||||
if (sectionStart !== this._index) {
|
||||
//parse entity
|
||||
var entity = this._buffer.substring(sectionStart, this._index);
|
||||
var parsed = parseInt(entity, base);
|
||||
|
||||
this._emitPartial(decodeCodePoint(parsed));
|
||||
this._sectionStart = this._index;
|
||||
} else {
|
||||
this._sectionStart--;
|
||||
}
|
||||
|
||||
this._state = this._baseState;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInNumericEntity = function(c) {
|
||||
if (c === ";") {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
this._sectionStart++;
|
||||
} else if (c < "0" || c > "9") {
|
||||
if (!this._xmlMode) {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
} else {
|
||||
this._state = this._baseState;
|
||||
}
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._stateInHexEntity = function(c) {
|
||||
if (c === ";") {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
this._sectionStart++;
|
||||
} else if (
|
||||
(c < "a" || c > "f") &&
|
||||
(c < "A" || c > "F") &&
|
||||
(c < "0" || c > "9")
|
||||
) {
|
||||
if (!this._xmlMode) {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
} else {
|
||||
this._state = this._baseState;
|
||||
}
|
||||
this._index--;
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype._cleanup = function() {
|
||||
if (this._sectionStart < 0) {
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
} else if (this._running) {
|
||||
if (this._state === TEXT) {
|
||||
if (this._sectionStart !== this._index) {
|
||||
this._cbs.ontext(this._buffer.substr(this._sectionStart));
|
||||
}
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
} else if (this._sectionStart === this._index) {
|
||||
//the section just started
|
||||
this._buffer = "";
|
||||
this._bufferOffset += this._index;
|
||||
this._index = 0;
|
||||
} else {
|
||||
//remove everything unnecessary
|
||||
this._buffer = this._buffer.substr(this._sectionStart);
|
||||
this._index -= this._sectionStart;
|
||||
this._bufferOffset += this._sectionStart;
|
||||
}
|
||||
|
||||
this._sectionStart = 0;
|
||||
}
|
||||
};
|
||||
|
||||
//TODO make events conditional
|
||||
Tokenizer.prototype.write = function(chunk) {
|
||||
if (this._ended) this._cbs.onerror(Error(".write() after done!"));
|
||||
|
||||
this._buffer += chunk;
|
||||
this._parse();
|
||||
};
|
||||
|
||||
Tokenizer.prototype._parse = function() {
|
||||
while (this._index < this._buffer.length && this._running) {
|
||||
var c = this._buffer.charAt(this._index);
|
||||
if (this._state === TEXT) {
|
||||
this._stateText(c);
|
||||
} else if (this._state === BEFORE_TAG_NAME) {
|
||||
this._stateBeforeTagName(c);
|
||||
} else if (this._state === IN_TAG_NAME) {
|
||||
this._stateInTagName(c);
|
||||
} else if (this._state === BEFORE_CLOSING_TAG_NAME) {
|
||||
this._stateBeforeCloseingTagName(c);
|
||||
} else if (this._state === IN_CLOSING_TAG_NAME) {
|
||||
this._stateInCloseingTagName(c);
|
||||
} else if (this._state === AFTER_CLOSING_TAG_NAME) {
|
||||
this._stateAfterCloseingTagName(c);
|
||||
} else if (this._state === IN_SELF_CLOSING_TAG) {
|
||||
this._stateInSelfClosingTag(c);
|
||||
} else if (this._state === BEFORE_ATTRIBUTE_NAME) {
|
||||
|
||||
/*
|
||||
* attributes
|
||||
*/
|
||||
this._stateBeforeAttributeName(c);
|
||||
} else if (this._state === IN_ATTRIBUTE_NAME) {
|
||||
this._stateInAttributeName(c);
|
||||
} else if (this._state === AFTER_ATTRIBUTE_NAME) {
|
||||
this._stateAfterAttributeName(c);
|
||||
} else if (this._state === BEFORE_ATTRIBUTE_VALUE) {
|
||||
this._stateBeforeAttributeValue(c);
|
||||
} else if (this._state === IN_ATTRIBUTE_VALUE_DQ) {
|
||||
this._stateInAttributeValueDoubleQuotes(c);
|
||||
} else if (this._state === IN_ATTRIBUTE_VALUE_SQ) {
|
||||
this._stateInAttributeValueSingleQuotes(c);
|
||||
} else if (this._state === IN_ATTRIBUTE_VALUE_NQ) {
|
||||
this._stateInAttributeValueNoQuotes(c);
|
||||
} else if (this._state === BEFORE_DECLARATION) {
|
||||
|
||||
/*
|
||||
* declarations
|
||||
*/
|
||||
this._stateBeforeDeclaration(c);
|
||||
} else if (this._state === IN_DECLARATION) {
|
||||
this._stateInDeclaration(c);
|
||||
} else if (this._state === IN_PROCESSING_INSTRUCTION) {
|
||||
|
||||
/*
|
||||
* processing instructions
|
||||
*/
|
||||
this._stateInProcessingInstruction(c);
|
||||
} else if (this._state === BEFORE_COMMENT) {
|
||||
|
||||
/*
|
||||
* comments
|
||||
*/
|
||||
this._stateBeforeComment(c);
|
||||
} else if (this._state === IN_COMMENT) {
|
||||
this._stateInComment(c);
|
||||
} else if (this._state === AFTER_COMMENT_1) {
|
||||
this._stateAfterComment1(c);
|
||||
} else if (this._state === AFTER_COMMENT_2) {
|
||||
this._stateAfterComment2(c);
|
||||
} else if (this._state === BEFORE_CDATA_1) {
|
||||
|
||||
/*
|
||||
* cdata
|
||||
*/
|
||||
this._stateBeforeCdata1(c);
|
||||
} else if (this._state === BEFORE_CDATA_2) {
|
||||
this._stateBeforeCdata2(c);
|
||||
} else if (this._state === BEFORE_CDATA_3) {
|
||||
this._stateBeforeCdata3(c);
|
||||
} else if (this._state === BEFORE_CDATA_4) {
|
||||
this._stateBeforeCdata4(c);
|
||||
} else if (this._state === BEFORE_CDATA_5) {
|
||||
this._stateBeforeCdata5(c);
|
||||
} else if (this._state === BEFORE_CDATA_6) {
|
||||
this._stateBeforeCdata6(c);
|
||||
} else if (this._state === IN_CDATA) {
|
||||
this._stateInCdata(c);
|
||||
} else if (this._state === AFTER_CDATA_1) {
|
||||
this._stateAfterCdata1(c);
|
||||
} else if (this._state === AFTER_CDATA_2) {
|
||||
this._stateAfterCdata2(c);
|
||||
} else if (this._state === BEFORE_SPECIAL) {
|
||||
|
||||
/*
|
||||
* special tags
|
||||
*/
|
||||
this._stateBeforeSpecial(c);
|
||||
} else if (this._state === BEFORE_SPECIAL_END) {
|
||||
this._stateBeforeSpecialEnd(c);
|
||||
} else if (this._state === BEFORE_SCRIPT_1) {
|
||||
|
||||
/*
|
||||
* script
|
||||
*/
|
||||
this._stateBeforeScript1(c);
|
||||
} else if (this._state === BEFORE_SCRIPT_2) {
|
||||
this._stateBeforeScript2(c);
|
||||
} else if (this._state === BEFORE_SCRIPT_3) {
|
||||
this._stateBeforeScript3(c);
|
||||
} else if (this._state === BEFORE_SCRIPT_4) {
|
||||
this._stateBeforeScript4(c);
|
||||
} else if (this._state === BEFORE_SCRIPT_5) {
|
||||
this._stateBeforeScript5(c);
|
||||
} else if (this._state === AFTER_SCRIPT_1) {
|
||||
this._stateAfterScript1(c);
|
||||
} else if (this._state === AFTER_SCRIPT_2) {
|
||||
this._stateAfterScript2(c);
|
||||
} else if (this._state === AFTER_SCRIPT_3) {
|
||||
this._stateAfterScript3(c);
|
||||
} else if (this._state === AFTER_SCRIPT_4) {
|
||||
this._stateAfterScript4(c);
|
||||
} else if (this._state === AFTER_SCRIPT_5) {
|
||||
this._stateAfterScript5(c);
|
||||
} else if (this._state === BEFORE_STYLE_1) {
|
||||
|
||||
/*
|
||||
* style
|
||||
*/
|
||||
this._stateBeforeStyle1(c);
|
||||
} else if (this._state === BEFORE_STYLE_2) {
|
||||
this._stateBeforeStyle2(c);
|
||||
} else if (this._state === BEFORE_STYLE_3) {
|
||||
this._stateBeforeStyle3(c);
|
||||
} else if (this._state === BEFORE_STYLE_4) {
|
||||
this._stateBeforeStyle4(c);
|
||||
} else if (this._state === AFTER_STYLE_1) {
|
||||
this._stateAfterStyle1(c);
|
||||
} else if (this._state === AFTER_STYLE_2) {
|
||||
this._stateAfterStyle2(c);
|
||||
} else if (this._state === AFTER_STYLE_3) {
|
||||
this._stateAfterStyle3(c);
|
||||
} else if (this._state === AFTER_STYLE_4) {
|
||||
this._stateAfterStyle4(c);
|
||||
} else if (this._state === BEFORE_ENTITY) {
|
||||
|
||||
/*
|
||||
* entities
|
||||
*/
|
||||
this._stateBeforeEntity(c);
|
||||
} else if (this._state === BEFORE_NUMERIC_ENTITY) {
|
||||
this._stateBeforeNumericEntity(c);
|
||||
} else if (this._state === IN_NAMED_ENTITY) {
|
||||
this._stateInNamedEntity(c);
|
||||
} else if (this._state === IN_NUMERIC_ENTITY) {
|
||||
this._stateInNumericEntity(c);
|
||||
} else if (this._state === IN_HEX_ENTITY) {
|
||||
this._stateInHexEntity(c);
|
||||
} else {
|
||||
this._cbs.onerror(Error("unknown _state"), this._state);
|
||||
}
|
||||
|
||||
this._index++;
|
||||
}
|
||||
|
||||
this._cleanup();
|
||||
};
|
||||
|
||||
Tokenizer.prototype.pause = function() {
|
||||
this._running = false;
|
||||
};
|
||||
Tokenizer.prototype.resume = function() {
|
||||
this._running = true;
|
||||
|
||||
if (this._index < this._buffer.length) {
|
||||
this._parse();
|
||||
}
|
||||
if (this._ended) {
|
||||
this._finish();
|
||||
}
|
||||
};
|
||||
|
||||
Tokenizer.prototype.end = function(chunk) {
|
||||
if (this._ended) this._cbs.onerror(Error(".end() after done!"));
|
||||
if (chunk) this.write(chunk);
|
||||
|
||||
this._ended = true;
|
||||
|
||||
if (this._running) this._finish();
|
||||
};
|
||||
|
||||
Tokenizer.prototype._finish = function() {
|
||||
//if there is remaining data, emit it in a reasonable way
|
||||
if (this._sectionStart < this._index) {
|
||||
this._handleTrailingData();
|
||||
}
|
||||
|
||||
this._cbs.onend();
|
||||
};
|
||||
|
||||
Tokenizer.prototype._handleTrailingData = function() {
|
||||
var data = this._buffer.substr(this._sectionStart);
|
||||
|
||||
if (
|
||||
this._state === IN_CDATA ||
|
||||
this._state === AFTER_CDATA_1 ||
|
||||
this._state === AFTER_CDATA_2
|
||||
) {
|
||||
this._cbs.oncdata(data);
|
||||
} else if (
|
||||
this._state === IN_COMMENT ||
|
||||
this._state === AFTER_COMMENT_1 ||
|
||||
this._state === AFTER_COMMENT_2
|
||||
) {
|
||||
this._cbs.oncomment(data);
|
||||
} else if (this._state === IN_NAMED_ENTITY && !this._xmlMode) {
|
||||
this._parseLegacyEntity();
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
} else if (this._state === IN_NUMERIC_ENTITY && !this._xmlMode) {
|
||||
this._decodeNumericEntity(2, 10);
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
} else if (this._state === IN_HEX_ENTITY && !this._xmlMode) {
|
||||
this._decodeNumericEntity(3, 16);
|
||||
if (this._sectionStart < this._index) {
|
||||
this._state = this._baseState;
|
||||
this._handleTrailingData();
|
||||
}
|
||||
} else if (
|
||||
this._state !== IN_TAG_NAME &&
|
||||
this._state !== BEFORE_ATTRIBUTE_NAME &&
|
||||
this._state !== BEFORE_ATTRIBUTE_VALUE &&
|
||||
this._state !== AFTER_ATTRIBUTE_NAME &&
|
||||
this._state !== IN_ATTRIBUTE_NAME &&
|
||||
this._state !== IN_ATTRIBUTE_VALUE_SQ &&
|
||||
this._state !== IN_ATTRIBUTE_VALUE_DQ &&
|
||||
this._state !== IN_ATTRIBUTE_VALUE_NQ &&
|
||||
this._state !== IN_CLOSING_TAG_NAME
|
||||
) {
|
||||
this._cbs.ontext(data);
|
||||
}
|
||||
//else, ignore remaining data
|
||||
//TODO add a way to remove current tag
|
||||
};
|
||||
|
||||
Tokenizer.prototype.reset = function() {
|
||||
Tokenizer.call(
|
||||
this,
|
||||
{ xmlMode: this._xmlMode, decodeEntities: this._decodeEntities },
|
||||
this._cbs
|
||||
);
|
||||
};
|
||||
|
||||
Tokenizer.prototype.getAbsoluteIndex = function() {
|
||||
return this._bufferOffset + this._index;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._getSection = function() {
|
||||
return this._buffer.substring(this._sectionStart, this._index);
|
||||
};
|
||||
|
||||
Tokenizer.prototype._emitToken = function(name) {
|
||||
this._cbs[name](this._getSection());
|
||||
this._sectionStart = -1;
|
||||
};
|
||||
|
||||
Tokenizer.prototype._emitPartial = function(value) {
|
||||
if (this._baseState !== TEXT) {
|
||||
this._cbs.onattribdata(value); //TODO implement the new event
|
||||
} else {
|
||||
this._cbs.ontext(value);
|
||||
}
|
||||
};
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
module.exports = Stream;
|
||||
|
||||
var Parser = require("./Parser.js");
|
||||
var WritableStream = require("readable-stream").Writable;
|
||||
var StringDecoder = require("string_decoder").StringDecoder;
|
||||
var Buffer = require("buffer").Buffer;
|
||||
|
||||
function Stream(cbs, options) {
|
||||
var parser = (this._parser = new Parser(cbs, options));
|
||||
var decoder = (this._decoder = new StringDecoder());
|
||||
|
||||
WritableStream.call(this, { decodeStrings: false });
|
||||
|
||||
this.once("finish", function() {
|
||||
parser.end(decoder.end());
|
||||
});
|
||||
}
|
||||
|
||||
require("inherits")(Stream, WritableStream);
|
||||
|
||||
Stream.prototype._write = function(chunk, encoding, cb) {
|
||||
if (chunk instanceof Buffer) chunk = this._decoder.write(chunk);
|
||||
this._parser.write(chunk);
|
||||
cb();
|
||||
};
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
var Parser = require("./Parser.js");
|
||||
var DomHandler = require("domhandler");
|
||||
|
||||
function defineProp(name, value) {
|
||||
delete module.exports[name];
|
||||
module.exports[name] = value;
|
||||
return value;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Parser: Parser,
|
||||
Tokenizer: require("./Tokenizer.js"),
|
||||
ElementType: require("domelementtype"),
|
||||
DomHandler: DomHandler,
|
||||
get FeedHandler() {
|
||||
return defineProp("FeedHandler", require("./FeedHandler.js"));
|
||||
},
|
||||
get Stream() {
|
||||
return defineProp("Stream", require("./Stream.js"));
|
||||
},
|
||||
get WritableStream() {
|
||||
return defineProp("WritableStream", require("./WritableStream.js"));
|
||||
},
|
||||
get ProxyHandler() {
|
||||
return defineProp("ProxyHandler", require("./ProxyHandler.js"));
|
||||
},
|
||||
get DomUtils() {
|
||||
return defineProp("DomUtils", require("domutils"));
|
||||
},
|
||||
get CollectingHandler() {
|
||||
return defineProp(
|
||||
"CollectingHandler",
|
||||
require("./CollectingHandler.js")
|
||||
);
|
||||
},
|
||||
// For legacy support
|
||||
DefaultHandler: DomHandler,
|
||||
get RssHandler() {
|
||||
return defineProp("RssHandler", this.FeedHandler);
|
||||
},
|
||||
//helper methods
|
||||
parseDOM: function(data, options) {
|
||||
var handler = new DomHandler(options);
|
||||
new Parser(handler, options).end(data);
|
||||
return handler.dom;
|
||||
},
|
||||
parseFeed: function(feed, options) {
|
||||
var handler = new module.exports.FeedHandler(options);
|
||||
new Parser(handler, options).end(feed);
|
||||
return handler.dom;
|
||||
},
|
||||
createDomStream: function(cb, options, elementCb) {
|
||||
var handler = new DomHandler(cb, options, elementCb);
|
||||
return new Parser(handler, options);
|
||||
},
|
||||
// List of all events that the parser emits
|
||||
EVENTS: {
|
||||
/* Format: eventname: number of arguments */
|
||||
attribute: 2,
|
||||
cdatastart: 0,
|
||||
cdataend: 0,
|
||||
text: 1,
|
||||
processinginstruction: 2,
|
||||
comment: 1,
|
||||
commentend: 0,
|
||||
closetag: 1,
|
||||
opentag: 2,
|
||||
opentagname: 1,
|
||||
error: 1,
|
||||
end: 0
|
||||
}
|
||||
};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
sudo: true
|
||||
language: node_js
|
||||
node_js:
|
||||
- 8
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
Copyright (c) Felix Böhm
|
||||
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.
|
||||
|
||||
THIS 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,
|
||||
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
var DomUtils = module.exports;
|
||||
|
||||
[
|
||||
require("./lib/stringify"),
|
||||
require("./lib/traversal"),
|
||||
require("./lib/manipulation"),
|
||||
require("./lib/querying"),
|
||||
require("./lib/legacy"),
|
||||
require("./lib/helpers")
|
||||
].forEach(function(ext){
|
||||
Object.keys(ext).forEach(function(key){
|
||||
DomUtils[key] = ext[key].bind(DomUtils);
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+141
@@ -0,0 +1,141 @@
|
||||
// removeSubsets
|
||||
// Given an array of nodes, remove any member that is contained by another.
|
||||
exports.removeSubsets = function(nodes) {
|
||||
var idx = nodes.length, node, ancestor, replace;
|
||||
|
||||
// Check if each node (or one of its ancestors) is already contained in the
|
||||
// array.
|
||||
while (--idx > -1) {
|
||||
node = ancestor = nodes[idx];
|
||||
|
||||
// Temporarily remove the node under consideration
|
||||
nodes[idx] = null;
|
||||
replace = true;
|
||||
|
||||
while (ancestor) {
|
||||
if (nodes.indexOf(ancestor) > -1) {
|
||||
replace = false;
|
||||
nodes.splice(idx, 1);
|
||||
break;
|
||||
}
|
||||
ancestor = ancestor.parent;
|
||||
}
|
||||
|
||||
// If the node has been found to be unique, re-insert it.
|
||||
if (replace) {
|
||||
nodes[idx] = node;
|
||||
}
|
||||
}
|
||||
|
||||
return nodes;
|
||||
};
|
||||
|
||||
// Source: http://dom.spec.whatwg.org/#dom-node-comparedocumentposition
|
||||
var POSITION = {
|
||||
DISCONNECTED: 1,
|
||||
PRECEDING: 2,
|
||||
FOLLOWING: 4,
|
||||
CONTAINS: 8,
|
||||
CONTAINED_BY: 16
|
||||
};
|
||||
|
||||
// Compare the position of one node against another node in any other document.
|
||||
// The return value is a bitmask with the following values:
|
||||
//
|
||||
// document order:
|
||||
// > There is an ordering, document order, defined on all the nodes in the
|
||||
// > document corresponding to the order in which the first character of the
|
||||
// > XML representation of each node occurs in the XML representation of the
|
||||
// > document after expansion of general entities. Thus, the document element
|
||||
// > node will be the first node. Element nodes occur before their children.
|
||||
// > Thus, document order orders element nodes in order of the occurrence of
|
||||
// > their start-tag in the XML (after expansion of entities). The attribute
|
||||
// > nodes of an element occur after the element and before its children. The
|
||||
// > relative order of attribute nodes is implementation-dependent./
|
||||
// Source:
|
||||
// http://www.w3.org/TR/DOM-Level-3-Core/glossary.html#dt-document-order
|
||||
//
|
||||
// @argument {Node} nodaA The first node to use in the comparison
|
||||
// @argument {Node} nodeB The second node to use in the comparison
|
||||
//
|
||||
// @return {Number} A bitmask describing the input nodes' relative position.
|
||||
// See http://dom.spec.whatwg.org/#dom-node-comparedocumentposition for
|
||||
// a description of these values.
|
||||
var comparePos = exports.compareDocumentPosition = function(nodeA, nodeB) {
|
||||
var aParents = [];
|
||||
var bParents = [];
|
||||
var current, sharedParent, siblings, aSibling, bSibling, idx;
|
||||
|
||||
if (nodeA === nodeB) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
current = nodeA;
|
||||
while (current) {
|
||||
aParents.unshift(current);
|
||||
current = current.parent;
|
||||
}
|
||||
current = nodeB;
|
||||
while (current) {
|
||||
bParents.unshift(current);
|
||||
current = current.parent;
|
||||
}
|
||||
|
||||
idx = 0;
|
||||
while (aParents[idx] === bParents[idx]) {
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (idx === 0) {
|
||||
return POSITION.DISCONNECTED;
|
||||
}
|
||||
|
||||
sharedParent = aParents[idx - 1];
|
||||
siblings = sharedParent.children;
|
||||
aSibling = aParents[idx];
|
||||
bSibling = bParents[idx];
|
||||
|
||||
if (siblings.indexOf(aSibling) > siblings.indexOf(bSibling)) {
|
||||
if (sharedParent === nodeB) {
|
||||
return POSITION.FOLLOWING | POSITION.CONTAINED_BY;
|
||||
}
|
||||
return POSITION.FOLLOWING;
|
||||
} else {
|
||||
if (sharedParent === nodeA) {
|
||||
return POSITION.PRECEDING | POSITION.CONTAINS;
|
||||
}
|
||||
return POSITION.PRECEDING;
|
||||
}
|
||||
};
|
||||
|
||||
// Sort an array of nodes based on their relative position in the document and
|
||||
// remove any duplicate nodes. If the array contains nodes that do not belong
|
||||
// to the same document, sort order is unspecified.
|
||||
//
|
||||
// @argument {Array} nodes Array of DOM nodes
|
||||
//
|
||||
// @returns {Array} collection of unique nodes, sorted in document order
|
||||
exports.uniqueSort = function(nodes) {
|
||||
var idx = nodes.length, node, position;
|
||||
|
||||
nodes = nodes.slice();
|
||||
|
||||
while (--idx > -1) {
|
||||
node = nodes[idx];
|
||||
position = nodes.indexOf(node);
|
||||
if (position > -1 && position < idx) {
|
||||
nodes.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
nodes.sort(function(a, b) {
|
||||
var relative = comparePos(a, b);
|
||||
if (relative & POSITION.PRECEDING) {
|
||||
return -1;
|
||||
} else if (relative & POSITION.FOLLOWING) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
|
||||
return nodes;
|
||||
};
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
var ElementType = require("domelementtype");
|
||||
var isTag = exports.isTag = ElementType.isTag;
|
||||
|
||||
exports.testElement = function(options, element){
|
||||
for(var key in options){
|
||||
if(!options.hasOwnProperty(key));
|
||||
else if(key === "tag_name"){
|
||||
if(!isTag(element) || !options.tag_name(element.name)){
|
||||
return false;
|
||||
}
|
||||
} else if(key === "tag_type"){
|
||||
if(!options.tag_type(element.type)) return false;
|
||||
} else if(key === "tag_contains"){
|
||||
if(isTag(element) || !options.tag_contains(element.data)){
|
||||
return false;
|
||||
}
|
||||
} else if(!element.attribs || !options[key](element.attribs[key])){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
var Checks = {
|
||||
tag_name: function(name){
|
||||
if(typeof name === "function"){
|
||||
return function(elem){ return isTag(elem) && name(elem.name); };
|
||||
} else if(name === "*"){
|
||||
return isTag;
|
||||
} else {
|
||||
return function(elem){ return isTag(elem) && elem.name === name; };
|
||||
}
|
||||
},
|
||||
tag_type: function(type){
|
||||
if(typeof type === "function"){
|
||||
return function(elem){ return type(elem.type); };
|
||||
} else {
|
||||
return function(elem){ return elem.type === type; };
|
||||
}
|
||||
},
|
||||
tag_contains: function(data){
|
||||
if(typeof data === "function"){
|
||||
return function(elem){ return !isTag(elem) && data(elem.data); };
|
||||
} else {
|
||||
return function(elem){ return !isTag(elem) && elem.data === data; };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function getAttribCheck(attrib, value){
|
||||
if(typeof value === "function"){
|
||||
return function(elem){ return elem.attribs && value(elem.attribs[attrib]); };
|
||||
} else {
|
||||
return function(elem){ return elem.attribs && elem.attribs[attrib] === value; };
|
||||
}
|
||||
}
|
||||
|
||||
function combineFuncs(a, b){
|
||||
return function(elem){
|
||||
return a(elem) || b(elem);
|
||||
};
|
||||
}
|
||||
|
||||
exports.getElements = function(options, element, recurse, limit){
|
||||
var funcs = Object.keys(options).map(function(key){
|
||||
var value = options[key];
|
||||
return key in Checks ? Checks[key](value) : getAttribCheck(key, value);
|
||||
});
|
||||
|
||||
return funcs.length === 0 ? [] : this.filter(
|
||||
funcs.reduce(combineFuncs),
|
||||
element, recurse, limit
|
||||
);
|
||||
};
|
||||
|
||||
exports.getElementById = function(id, element, recurse){
|
||||
if(!Array.isArray(element)) element = [element];
|
||||
return this.findOne(getAttribCheck("id", id), element, recurse !== false);
|
||||
};
|
||||
|
||||
exports.getElementsByTagName = function(name, element, recurse, limit){
|
||||
return this.filter(Checks.tag_name(name), element, recurse, limit);
|
||||
};
|
||||
|
||||
exports.getElementsByTagType = function(type, element, recurse, limit){
|
||||
return this.filter(Checks.tag_type(type), element, recurse, limit);
|
||||
};
|
||||
Generated
Vendored
+77
@@ -0,0 +1,77 @@
|
||||
exports.removeElement = function(elem){
|
||||
if(elem.prev) elem.prev.next = elem.next;
|
||||
if(elem.next) elem.next.prev = elem.prev;
|
||||
|
||||
if(elem.parent){
|
||||
var childs = elem.parent.children;
|
||||
childs.splice(childs.lastIndexOf(elem), 1);
|
||||
}
|
||||
};
|
||||
|
||||
exports.replaceElement = function(elem, replacement){
|
||||
var prev = replacement.prev = elem.prev;
|
||||
if(prev){
|
||||
prev.next = replacement;
|
||||
}
|
||||
|
||||
var next = replacement.next = elem.next;
|
||||
if(next){
|
||||
next.prev = replacement;
|
||||
}
|
||||
|
||||
var parent = replacement.parent = elem.parent;
|
||||
if(parent){
|
||||
var childs = parent.children;
|
||||
childs[childs.lastIndexOf(elem)] = replacement;
|
||||
}
|
||||
};
|
||||
|
||||
exports.appendChild = function(elem, child){
|
||||
child.parent = elem;
|
||||
|
||||
if(elem.children.push(child) !== 1){
|
||||
var sibling = elem.children[elem.children.length - 2];
|
||||
sibling.next = child;
|
||||
child.prev = sibling;
|
||||
child.next = null;
|
||||
}
|
||||
};
|
||||
|
||||
exports.append = function(elem, next){
|
||||
var parent = elem.parent,
|
||||
currNext = elem.next;
|
||||
|
||||
next.next = currNext;
|
||||
next.prev = elem;
|
||||
elem.next = next;
|
||||
next.parent = parent;
|
||||
|
||||
if(currNext){
|
||||
currNext.prev = next;
|
||||
if(parent){
|
||||
var childs = parent.children;
|
||||
childs.splice(childs.lastIndexOf(currNext), 0, next);
|
||||
}
|
||||
} else if(parent){
|
||||
parent.children.push(next);
|
||||
}
|
||||
};
|
||||
|
||||
exports.prepend = function(elem, prev){
|
||||
var parent = elem.parent;
|
||||
if(parent){
|
||||
var childs = parent.children;
|
||||
childs.splice(childs.lastIndexOf(elem), 0, prev);
|
||||
}
|
||||
|
||||
if(elem.prev){
|
||||
elem.prev.next = prev;
|
||||
}
|
||||
|
||||
prev.parent = parent;
|
||||
prev.prev = elem.prev;
|
||||
prev.next = elem;
|
||||
elem.prev = prev;
|
||||
};
|
||||
|
||||
|
||||
Generated
Vendored
+95
@@ -0,0 +1,95 @@
|
||||
var isTag = require("domelementtype").isTag;
|
||||
|
||||
module.exports = {
|
||||
filter: filter,
|
||||
find: find,
|
||||
findOneChild: findOneChild,
|
||||
findOne: findOne,
|
||||
existsOne: existsOne,
|
||||
findAll: findAll
|
||||
};
|
||||
|
||||
function filter(test, element, recurse, limit){
|
||||
if(!Array.isArray(element)) element = [element];
|
||||
|
||||
if(typeof limit !== "number" || !isFinite(limit)){
|
||||
limit = Infinity;
|
||||
}
|
||||
return find(test, element, recurse !== false, limit);
|
||||
}
|
||||
|
||||
function find(test, elems, recurse, limit){
|
||||
var result = [], childs;
|
||||
|
||||
for(var i = 0, j = elems.length; i < j; i++){
|
||||
if(test(elems[i])){
|
||||
result.push(elems[i]);
|
||||
if(--limit <= 0) break;
|
||||
}
|
||||
|
||||
childs = elems[i].children;
|
||||
if(recurse && childs && childs.length > 0){
|
||||
childs = find(test, childs, recurse, limit);
|
||||
result = result.concat(childs);
|
||||
limit -= childs.length;
|
||||
if(limit <= 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function findOneChild(test, elems){
|
||||
for(var i = 0, l = elems.length; i < l; i++){
|
||||
if(test(elems[i])) return elems[i];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function findOne(test, elems){
|
||||
var elem = null;
|
||||
|
||||
for(var i = 0, l = elems.length; i < l && !elem; i++){
|
||||
if(!isTag(elems[i])){
|
||||
continue;
|
||||
} else if(test(elems[i])){
|
||||
elem = elems[i];
|
||||
} else if(elems[i].children.length > 0){
|
||||
elem = findOne(test, elems[i].children);
|
||||
}
|
||||
}
|
||||
|
||||
return elem;
|
||||
}
|
||||
|
||||
function existsOne(test, elems){
|
||||
for(var i = 0, l = elems.length; i < l; i++){
|
||||
if(
|
||||
isTag(elems[i]) && (
|
||||
test(elems[i]) || (
|
||||
elems[i].children.length > 0 &&
|
||||
existsOne(test, elems[i].children)
|
||||
)
|
||||
)
|
||||
){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function findAll(test, rootElems){
|
||||
var result = [];
|
||||
var stack = rootElems.slice();
|
||||
while(stack.length){
|
||||
var elem = stack.shift();
|
||||
if(!isTag(elem)) continue;
|
||||
if (elem.children && elem.children.length > 0) {
|
||||
stack.unshift.apply(stack, elem.children);
|
||||
}
|
||||
if(test(elem)) result.push(elem);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Generated
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
var ElementType = require("domelementtype"),
|
||||
getOuterHTML = require("dom-serializer"),
|
||||
isTag = ElementType.isTag;
|
||||
|
||||
module.exports = {
|
||||
getInnerHTML: getInnerHTML,
|
||||
getOuterHTML: getOuterHTML,
|
||||
getText: getText
|
||||
};
|
||||
|
||||
function getInnerHTML(elem, opts){
|
||||
return elem.children ? elem.children.map(function(elem){
|
||||
return getOuterHTML(elem, opts);
|
||||
}).join("") : "";
|
||||
}
|
||||
|
||||
function getText(elem){
|
||||
if(Array.isArray(elem)) return elem.map(getText).join("");
|
||||
if(isTag(elem)) return elem.name === "br" ? "\n" : getText(elem.children);
|
||||
if(elem.type === ElementType.CDATA) return getText(elem.children);
|
||||
if(elem.type === ElementType.Text) return elem.data;
|
||||
return "";
|
||||
}
|
||||
Generated
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
var getChildren = exports.getChildren = function(elem){
|
||||
return elem.children;
|
||||
};
|
||||
|
||||
var getParent = exports.getParent = function(elem){
|
||||
return elem.parent;
|
||||
};
|
||||
|
||||
exports.getSiblings = function(elem){
|
||||
var parent = getParent(elem);
|
||||
return parent ? getChildren(parent) : [elem];
|
||||
};
|
||||
|
||||
exports.getAttributeValue = function(elem, name){
|
||||
return elem.attribs && elem.attribs[name];
|
||||
};
|
||||
|
||||
exports.hasAttrib = function(elem, name){
|
||||
return !!elem.attribs && hasOwnProperty.call(elem.attribs, name);
|
||||
};
|
||||
|
||||
exports.getName = function(elem){
|
||||
return elem.name;
|
||||
};
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "domutils",
|
||||
"version": "1.7.0",
|
||||
"description": "utilities for working with htmlparser2's dom",
|
||||
"main": "index.js",
|
||||
"directories": {
|
||||
"test": "tests"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha test/tests/**.js && jshint index.js test/**/*.js lib/*.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/FB55/domutils.git"
|
||||
},
|
||||
"keywords": [
|
||||
"dom",
|
||||
"htmlparser2"
|
||||
],
|
||||
"dependencies": {
|
||||
"dom-serializer": "0",
|
||||
"domelementtype": "1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"htmlparser2": "~3.9.2",
|
||||
"domhandler": "2",
|
||||
"jshint": "~2.9.4",
|
||||
"mocha": "~3.2.0"
|
||||
},
|
||||
"author": "Felix Boehm <me@feedic.com>",
|
||||
"license": "BSD-2-Clause",
|
||||
"jshintConfig": {
|
||||
"proto": true,
|
||||
"unused": true,
|
||||
"eqnull": true,
|
||||
"undef": true,
|
||||
"quotmark": "double",
|
||||
"eqeqeq": true,
|
||||
"trailing": true,
|
||||
"node": true,
|
||||
"globals": {
|
||||
"describe": true,
|
||||
"it": true,
|
||||
"beforeEach": true
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Utilities for working with htmlparser2's dom
|
||||
|
||||
[](https://travis-ci.org/fb55/domutils)
|
||||
Generated
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
var makeDom = require("./utils").makeDom;
|
||||
var markup = Array(21).join(
|
||||
"<?xml><tag1 id='asdf'> <script>text</script> <!-- comment --> <tag2> text </tag1>"
|
||||
);
|
||||
|
||||
module.exports = makeDom(markup);
|
||||
Generated
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
var makeDom = require("../utils").makeDom;
|
||||
var helpers = require("../..");
|
||||
var assert = require("assert");
|
||||
|
||||
describe("helpers", function() {
|
||||
describe("removeSubsets", function() {
|
||||
var removeSubsets = helpers.removeSubsets;
|
||||
var dom = makeDom("<div><p><span></span></p><p></p></div>")[0];
|
||||
|
||||
it("removes identical trees", function() {
|
||||
var matches = removeSubsets([dom, dom]);
|
||||
assert.equal(matches.length, 1);
|
||||
});
|
||||
|
||||
it("Removes subsets found first", function() {
|
||||
var matches = removeSubsets([dom, dom.children[0].children[0]]);
|
||||
assert.equal(matches.length, 1);
|
||||
});
|
||||
|
||||
it("Removes subsets found last", function() {
|
||||
var matches = removeSubsets([dom.children[0], dom]);
|
||||
assert.equal(matches.length, 1);
|
||||
});
|
||||
|
||||
it("Does not remove unique trees", function() {
|
||||
var matches = removeSubsets([dom.children[0], dom.children[1]]);
|
||||
assert.equal(matches.length, 2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareDocumentPosition", function() {
|
||||
var compareDocumentPosition = helpers.compareDocumentPosition;
|
||||
var markup = "<div><p><span></span></p><a></a></div>";
|
||||
var dom = makeDom(markup)[0];
|
||||
var p = dom.children[0];
|
||||
var span = p.children[0];
|
||||
var a = dom.children[1];
|
||||
|
||||
it("reports when the first node occurs before the second indirectly", function() {
|
||||
assert.equal(compareDocumentPosition(span, a), 2);
|
||||
});
|
||||
|
||||
it("reports when the first node contains the second", function() {
|
||||
assert.equal(compareDocumentPosition(p, span), 10);
|
||||
});
|
||||
|
||||
it("reports when the first node occurs after the second indirectly", function() {
|
||||
assert.equal(compareDocumentPosition(a, span), 4);
|
||||
});
|
||||
|
||||
it("reports when the first node is contained by the second", function() {
|
||||
assert.equal(compareDocumentPosition(span, p), 20);
|
||||
});
|
||||
|
||||
it("reports when the nodes belong to separate documents", function() {
|
||||
var other = makeDom(markup)[0].children[0].children[0];
|
||||
|
||||
assert.equal(compareDocumentPosition(span, other), 1);
|
||||
});
|
||||
|
||||
it("reports when the nodes are identical", function() {
|
||||
assert.equal(compareDocumentPosition(span, span), 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("uniqueSort", function() {
|
||||
var uniqueSort = helpers.uniqueSort;
|
||||
var dom, p, span, a;
|
||||
|
||||
beforeEach(function() {
|
||||
dom = makeDom("<div><p><span></span></p><a></a></div>")[0];
|
||||
p = dom.children[0];
|
||||
span = p.children[0];
|
||||
a = dom.children[1];
|
||||
});
|
||||
|
||||
it("leaves unique elements untouched", function() {
|
||||
assert.deepEqual(uniqueSort([p, a]), [p, a]);
|
||||
});
|
||||
|
||||
it("removes duplicate elements", function() {
|
||||
assert.deepEqual(uniqueSort([p, a, p]), [p, a]);
|
||||
});
|
||||
|
||||
it("sorts nodes in document order", function() {
|
||||
assert.deepEqual(uniqueSort([a, dom, span, p]), [dom, p, span, a]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Generated
Vendored
+119
@@ -0,0 +1,119 @@
|
||||
var DomUtils = require("../..");
|
||||
var fixture = require("../fixture");
|
||||
var assert = require("assert");
|
||||
|
||||
// Set up expected structures
|
||||
var expected = {
|
||||
idAsdf: fixture[1],
|
||||
tag2: [],
|
||||
typeScript: []
|
||||
};
|
||||
for (var idx = 0; idx < 20; ++idx) {
|
||||
expected.tag2.push(fixture[idx*2 + 1].children[5]);
|
||||
expected.typeScript.push(fixture[idx*2 + 1].children[1]);
|
||||
}
|
||||
|
||||
describe("legacy", function() {
|
||||
describe("getElements", function() {
|
||||
var getElements = DomUtils.getElements;
|
||||
it("returns the node with the specified ID", function() {
|
||||
assert.deepEqual(
|
||||
getElements({ id: "asdf" }, fixture, true, 1),
|
||||
[expected.idAsdf]
|
||||
);
|
||||
});
|
||||
it("returns empty array for unknown IDs", function() {
|
||||
assert.deepEqual(getElements({ id: "asdfs" }, fixture, true), []);
|
||||
});
|
||||
it("returns the nodes with the specified tag name", function() {
|
||||
assert.deepEqual(
|
||||
getElements({ tag_name:"tag2" }, fixture, true),
|
||||
expected.tag2
|
||||
);
|
||||
});
|
||||
it("returns empty array for unknown tag names", function() {
|
||||
assert.deepEqual(
|
||||
getElements({ tag_name : "asdfs" }, fixture, true),
|
||||
[]
|
||||
);
|
||||
});
|
||||
it("returns the nodes with the specified tag type", function() {
|
||||
assert.deepEqual(
|
||||
getElements({ tag_type: "script" }, fixture, true),
|
||||
expected.typeScript
|
||||
);
|
||||
});
|
||||
it("returns empty array for unknown tag types", function() {
|
||||
assert.deepEqual(
|
||||
getElements({ tag_type: "video" }, fixture, true),
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getElementById", function() {
|
||||
var getElementById = DomUtils.getElementById;
|
||||
it("returns the specified node", function() {
|
||||
assert.equal(
|
||||
expected.idAsdf,
|
||||
getElementById("asdf", fixture, true)
|
||||
);
|
||||
});
|
||||
it("returns `null` for unknown IDs", function() {
|
||||
assert.equal(null, getElementById("asdfs", fixture, true));
|
||||
});
|
||||
});
|
||||
|
||||
describe("getElementsByTagName", function() {
|
||||
var getElementsByTagName = DomUtils.getElementsByTagName;
|
||||
it("returns the specified nodes", function() {
|
||||
assert.deepEqual(
|
||||
getElementsByTagName("tag2", fixture, true),
|
||||
expected.tag2
|
||||
);
|
||||
});
|
||||
it("returns empty array for unknown tag names", function() {
|
||||
assert.deepEqual(
|
||||
getElementsByTagName("tag23", fixture, true),
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getElementsByTagType", function() {
|
||||
var getElementsByTagType = DomUtils.getElementsByTagType;
|
||||
it("returns the specified nodes", function() {
|
||||
assert.deepEqual(
|
||||
getElementsByTagType("script", fixture, true),
|
||||
expected.typeScript
|
||||
);
|
||||
});
|
||||
it("returns empty array for unknown tag types", function() {
|
||||
assert.deepEqual(
|
||||
getElementsByTagType("video", fixture, true),
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getOuterHTML", function() {
|
||||
var getOuterHTML = DomUtils.getOuterHTML;
|
||||
it("Correctly renders the outer HTML", function() {
|
||||
assert.equal(
|
||||
getOuterHTML(fixture[1]),
|
||||
"<tag1 id=\"asdf\"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getInnerHTML", function() {
|
||||
var getInnerHTML = DomUtils.getInnerHTML;
|
||||
it("Correctly renders the inner HTML", function() {
|
||||
assert.equal(
|
||||
getInnerHTML(fixture[1]),
|
||||
" <script>text</script> <!-- comment --> <tag2> text </tag2>"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Generated
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
var makeDom = require("../utils").makeDom;
|
||||
var traversal = require("../..");
|
||||
var assert = require("assert");
|
||||
|
||||
describe("traversal", function() {
|
||||
describe("hasAttrib", function() {
|
||||
var hasAttrib = traversal.hasAttrib;
|
||||
|
||||
it("doesn't throw on text nodes", function() {
|
||||
var dom = makeDom("textnode");
|
||||
assert.doesNotThrow(function() {
|
||||
hasAttrib(dom[0], "some-attrib");
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
var htmlparser = require("htmlparser2");
|
||||
|
||||
exports.makeDom = function(markup) {
|
||||
var handler = new htmlparser.DomHandler(),
|
||||
parser = new htmlparser.Parser(handler);
|
||||
parser.write(markup);
|
||||
parser.done();
|
||||
return handler.dom;
|
||||
};
|
||||
Generated
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
# Developer's Certificate of Origin 1.1
|
||||
|
||||
By making a contribution to this project, I certify that:
|
||||
|
||||
* (a) The contribution was created in whole or in part by me and I
|
||||
have the right to submit it under the open source license
|
||||
indicated in the file; or
|
||||
|
||||
* (b) The contribution is based upon previous work that, to the best
|
||||
of my knowledge, is covered under an appropriate open source
|
||||
license and I have the right under that license to submit that
|
||||
work with modifications, whether created in whole or in part
|
||||
by me, under the same open source license (unless I am
|
||||
permitted to submit under a different license), as indicated
|
||||
in the file; or
|
||||
|
||||
* (c) The contribution was provided directly to me by some other
|
||||
person who certified (a), (b) or (c) and I have not modified
|
||||
it.
|
||||
|
||||
* (d) I understand and agree that this project and the contribution
|
||||
are public and that a record of the contribution (including all
|
||||
personal information I submit with it, including my sign-off) is
|
||||
maintained indefinitely and may be redistributed consistent with
|
||||
this project or the open source license(s) involved.
|
||||
|
||||
## Moderation Policy
|
||||
|
||||
The [Node.js Moderation Policy] applies to this WG.
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
The [Node.js Code of Conduct][] applies to this WG.
|
||||
|
||||
[Node.js Code of Conduct]:
|
||||
https://github.com/nodejs/node/blob/master/CODE_OF_CONDUCT.md
|
||||
[Node.js Moderation Policy]:
|
||||
https://github.com/nodejs/TSC/blob/master/Moderation-Policy.md
|
||||
Generated
Vendored
+136
@@ -0,0 +1,136 @@
|
||||
### Streams Working Group
|
||||
|
||||
The Node.js Streams is jointly governed by a Working Group
|
||||
(WG)
|
||||
that is responsible for high-level guidance of the project.
|
||||
|
||||
The WG has final authority over this project including:
|
||||
|
||||
* Technical direction
|
||||
* Project governance and process (including this policy)
|
||||
* Contribution policy
|
||||
* GitHub repository hosting
|
||||
* Conduct guidelines
|
||||
* Maintaining the list of additional Collaborators
|
||||
|
||||
For the current list of WG members, see the project
|
||||
[README.md](./README.md#current-project-team-members).
|
||||
|
||||
### Collaborators
|
||||
|
||||
The readable-stream GitHub repository is
|
||||
maintained by the WG and additional Collaborators who are added by the
|
||||
WG on an ongoing basis.
|
||||
|
||||
Individuals making significant and valuable contributions are made
|
||||
Collaborators and given commit-access to the project. These
|
||||
individuals are identified by the WG and their addition as
|
||||
Collaborators is discussed during the WG meeting.
|
||||
|
||||
_Note:_ If you make a significant contribution and are not considered
|
||||
for commit-access log an issue or contact a WG member directly and it
|
||||
will be brought up in the next WG meeting.
|
||||
|
||||
Modifications of the contents of the readable-stream repository are
|
||||
made on
|
||||
a collaborative basis. Anybody with a GitHub account may propose a
|
||||
modification via pull request and it will be considered by the project
|
||||
Collaborators. All pull requests must be reviewed and accepted by a
|
||||
Collaborator with sufficient expertise who is able to take full
|
||||
responsibility for the change. In the case of pull requests proposed
|
||||
by an existing Collaborator, an additional Collaborator is required
|
||||
for sign-off. Consensus should be sought if additional Collaborators
|
||||
participate and there is disagreement around a particular
|
||||
modification. See _Consensus Seeking Process_ below for further detail
|
||||
on the consensus model used for governance.
|
||||
|
||||
Collaborators may opt to elevate significant or controversial
|
||||
modifications, or modifications that have not found consensus to the
|
||||
WG for discussion by assigning the ***WG-agenda*** tag to a pull
|
||||
request or issue. The WG should serve as the final arbiter where
|
||||
required.
|
||||
|
||||
For the current list of Collaborators, see the project
|
||||
[README.md](./README.md#members).
|
||||
|
||||
### WG Membership
|
||||
|
||||
WG seats are not time-limited. There is no fixed size of the WG.
|
||||
However, the expected target is between 6 and 12, to ensure adequate
|
||||
coverage of important areas of expertise, balanced with the ability to
|
||||
make decisions efficiently.
|
||||
|
||||
There is no specific set of requirements or qualifications for WG
|
||||
membership beyond these rules.
|
||||
|
||||
The WG may add additional members to the WG by unanimous consensus.
|
||||
|
||||
A WG member may be removed from the WG by voluntary resignation, or by
|
||||
unanimous consensus of all other WG members.
|
||||
|
||||
Changes to WG membership should be posted in the agenda, and may be
|
||||
suggested as any other agenda item (see "WG Meetings" below).
|
||||
|
||||
If an addition or removal is proposed during a meeting, and the full
|
||||
WG is not in attendance to participate, then the addition or removal
|
||||
is added to the agenda for the subsequent meeting. This is to ensure
|
||||
that all members are given the opportunity to participate in all
|
||||
membership decisions. If a WG member is unable to attend a meeting
|
||||
where a planned membership decision is being made, then their consent
|
||||
is assumed.
|
||||
|
||||
No more than 1/3 of the WG members may be affiliated with the same
|
||||
employer. If removal or resignation of a WG member, or a change of
|
||||
employment by a WG member, creates a situation where more than 1/3 of
|
||||
the WG membership shares an employer, then the situation must be
|
||||
immediately remedied by the resignation or removal of one or more WG
|
||||
members affiliated with the over-represented employer(s).
|
||||
|
||||
### WG Meetings
|
||||
|
||||
The WG meets occasionally on a Google Hangout On Air. A designated moderator
|
||||
approved by the WG runs the meeting. Each meeting should be
|
||||
published to YouTube.
|
||||
|
||||
Items are added to the WG agenda that are considered contentious or
|
||||
are modifications of governance, contribution policy, WG membership,
|
||||
or release process.
|
||||
|
||||
The intention of the agenda is not to approve or review all patches;
|
||||
that should happen continuously on GitHub and be handled by the larger
|
||||
group of Collaborators.
|
||||
|
||||
Any community member or contributor can ask that something be added to
|
||||
the next meeting's agenda by logging a GitHub Issue. Any Collaborator,
|
||||
WG member or the moderator can add the item to the agenda by adding
|
||||
the ***WG-agenda*** tag to the issue.
|
||||
|
||||
Prior to each WG meeting the moderator will share the Agenda with
|
||||
members of the WG. WG members can add any items they like to the
|
||||
agenda at the beginning of each meeting. The moderator and the WG
|
||||
cannot veto or remove items.
|
||||
|
||||
The WG may invite persons or representatives from certain projects to
|
||||
participate in a non-voting capacity.
|
||||
|
||||
The moderator is responsible for summarizing the discussion of each
|
||||
agenda item and sends it as a pull request after the meeting.
|
||||
|
||||
### Consensus Seeking Process
|
||||
|
||||
The WG follows a
|
||||
[Consensus
|
||||
Seeking](http://en.wikipedia.org/wiki/Consensus-seeking_decision-making)
|
||||
decision-making model.
|
||||
|
||||
When an agenda item has appeared to reach a consensus the moderator
|
||||
will ask "Does anyone object?" as a final call for dissent from the
|
||||
consensus.
|
||||
|
||||
If an agenda item cannot reach a consensus a WG member can call for
|
||||
either a closing vote or a vote to table the issue to the next
|
||||
meeting. The call for a vote must be seconded by a majority of the WG
|
||||
or else the discussion will continue. Simple majority wins.
|
||||
|
||||
Note that changes to WG membership require a majority consensus. See
|
||||
"WG Membership" above.
|
||||
Generated
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
Node.js is licensed for use as follows:
|
||||
|
||||
"""
|
||||
Copyright Node.js contributors. 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.
|
||||
"""
|
||||
|
||||
This license applies to parts of Node.js originating from the
|
||||
https://github.com/joyent/node repository:
|
||||
|
||||
"""
|
||||
Copyright Joyent, Inc. and other Node contributors. 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.
|
||||
"""
|
||||
Generated
Vendored
+111
@@ -0,0 +1,111 @@
|
||||
# readable-stream
|
||||
|
||||
***Node.js core streams for userland*** [](https://travis-ci.com/nodejs/readable-stream)
|
||||
|
||||
|
||||
[](https://nodei.co/npm/readable-stream/)
|
||||
[](https://nodei.co/npm/readable-stream/)
|
||||
|
||||
|
||||
[](https://saucelabs.com/u/readabe-stream)
|
||||
|
||||
```bash
|
||||
npm install --save readable-stream
|
||||
```
|
||||
|
||||
This package is a mirror of the streams implementations in Node.js.
|
||||
|
||||
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v10.15.3/docs/api/stream.html).
|
||||
|
||||
If you want to guarantee a stable streams base, regardless of what version of
|
||||
Node you, or the users of your libraries are using, use **readable-stream** *only* and avoid the *"stream"* module in Node-core, for background see [this blogpost](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html).
|
||||
|
||||
As of version 2.0.0 **readable-stream** uses semantic versioning.
|
||||
|
||||
## Version 3.x.x
|
||||
|
||||
v3.x.x of `readable-stream` supports Node 6, 8, and 10, as well as
|
||||
evergreen browsers, IE 11 and latest Safari. The breaking changes
|
||||
introduced by v3 are composed by the combined breaking changes in [Node v9](https://nodejs.org/en/blog/release/v9.0.0/)
|
||||
and [Node v10](https://nodejs.org/en/blog/release/v10.0.0/), as follows:
|
||||
|
||||
1. Error codes: https://github.com/nodejs/node/pull/13310,
|
||||
https://github.com/nodejs/node/pull/13291,
|
||||
https://github.com/nodejs/node/pull/16589,
|
||||
https://github.com/nodejs/node/pull/15042,
|
||||
https://github.com/nodejs/node/pull/15665,
|
||||
https://github.com/nodejs/readable-stream/pull/344
|
||||
2. 'readable' have precedence over flowing
|
||||
https://github.com/nodejs/node/pull/18994
|
||||
3. make virtual methods errors consistent
|
||||
https://github.com/nodejs/node/pull/18813
|
||||
4. updated streams error handling
|
||||
https://github.com/nodejs/node/pull/18438
|
||||
5. writable.end should return this.
|
||||
https://github.com/nodejs/node/pull/18780
|
||||
6. readable continues to read when push('')
|
||||
https://github.com/nodejs/node/pull/18211
|
||||
7. add custom inspect to BufferList
|
||||
https://github.com/nodejs/node/pull/17907
|
||||
8. always defer 'readable' with nextTick
|
||||
https://github.com/nodejs/node/pull/17979
|
||||
|
||||
## Version 2.x.x
|
||||
|
||||
v2.x.x of `readable-stream` supports all Node.js version from 0.8, as well as
|
||||
evergreen browsers and IE 10 & 11.
|
||||
|
||||
### Big Thanks
|
||||
|
||||
Cross-browser Testing Platform and Open Source <3 Provided by [Sauce Labs][sauce]
|
||||
|
||||
# Usage
|
||||
|
||||
You can swap your `require('stream')` with `require('readable-stream')`
|
||||
without any changes, if you are just using one of the main classes and
|
||||
functions.
|
||||
|
||||
```js
|
||||
const {
|
||||
Readable,
|
||||
Writable,
|
||||
Transform,
|
||||
Duplex,
|
||||
pipeline,
|
||||
finished
|
||||
} = require('readable-stream')
|
||||
````
|
||||
|
||||
Note that `require('stream')` will return `Stream`, while
|
||||
`require('readable-stream')` will return `Readable`. We discourage using
|
||||
whatever is exported directly, but rather use one of the properties as
|
||||
shown in the example above.
|
||||
|
||||
# Streams Working Group
|
||||
|
||||
`readable-stream` is maintained by the Streams Working Group, which
|
||||
oversees the development and maintenance of the Streams API within
|
||||
Node.js. The responsibilities of the Streams Working Group include:
|
||||
|
||||
* Addressing stream issues on the Node.js issue tracker.
|
||||
* Authoring and editing stream documentation within the Node.js project.
|
||||
* Reviewing changes to stream subclasses within the Node.js project.
|
||||
* Redirecting changes to streams from the Node.js project to this
|
||||
project.
|
||||
* Assisting in the implementation of stream providers within Node.js.
|
||||
* Recommending versions of `readable-stream` to be included in Node.js.
|
||||
* Messaging about the future of streams to give the community advance
|
||||
notice of changes.
|
||||
|
||||
<a name="members"></a>
|
||||
## Team Members
|
||||
|
||||
* **Calvin Metcalf** ([@calvinmetcalf](https://github.com/calvinmetcalf)) <calvin.metcalf@gmail.com>
|
||||
- Release GPG key: F3EF5F62A87FC27A22E643F714CE4FF5015AA242
|
||||
* **Mathias Buus** ([@mafintosh](https://github.com/mafintosh)) <mathiasbuus@gmail.com>
|
||||
* **Matteo Collina** ([@mcollina](https://github.com/mcollina)) <matteo.collina@gmail.com>
|
||||
- Release GPG key: 3ABC01543F22DD2239285CDD818674489FBC127E
|
||||
* **Irina Shestak** ([@lrlna](https://github.com/lrlna)) <shestak.irina@gmail.com>
|
||||
* **Yoshua Wyuts** ([@yoshuawuyts](https://github.com/yoshuawuyts)) <yoshuawuyts@gmail.com>
|
||||
|
||||
[sauce]: https://saucelabs.com
|
||||
Generated
Vendored
+127
@@ -0,0 +1,127 @@
|
||||
'use strict';
|
||||
|
||||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
||||
|
||||
var codes = {};
|
||||
|
||||
function createErrorType(code, message, Base) {
|
||||
if (!Base) {
|
||||
Base = Error;
|
||||
}
|
||||
|
||||
function getMessage(arg1, arg2, arg3) {
|
||||
if (typeof message === 'string') {
|
||||
return message;
|
||||
} else {
|
||||
return message(arg1, arg2, arg3);
|
||||
}
|
||||
}
|
||||
|
||||
var NodeError =
|
||||
/*#__PURE__*/
|
||||
function (_Base) {
|
||||
_inheritsLoose(NodeError, _Base);
|
||||
|
||||
function NodeError(arg1, arg2, arg3) {
|
||||
return _Base.call(this, getMessage(arg1, arg2, arg3)) || this;
|
||||
}
|
||||
|
||||
return NodeError;
|
||||
}(Base);
|
||||
|
||||
NodeError.prototype.name = Base.name;
|
||||
NodeError.prototype.code = code;
|
||||
codes[code] = NodeError;
|
||||
} // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
|
||||
|
||||
|
||||
function oneOf(expected, thing) {
|
||||
if (Array.isArray(expected)) {
|
||||
var len = expected.length;
|
||||
expected = expected.map(function (i) {
|
||||
return String(i);
|
||||
});
|
||||
|
||||
if (len > 2) {
|
||||
return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1];
|
||||
} else if (len === 2) {
|
||||
return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]);
|
||||
} else {
|
||||
return "of ".concat(thing, " ").concat(expected[0]);
|
||||
}
|
||||
} else {
|
||||
return "of ".concat(thing, " ").concat(String(expected));
|
||||
}
|
||||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
|
||||
|
||||
|
||||
function startsWith(str, search, pos) {
|
||||
return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
|
||||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
||||
|
||||
|
||||
function endsWith(str, search, this_len) {
|
||||
if (this_len === undefined || this_len > str.length) {
|
||||
this_len = str.length;
|
||||
}
|
||||
|
||||
return str.substring(this_len - search.length, this_len) === search;
|
||||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
|
||||
|
||||
|
||||
function includes(str, search, start) {
|
||||
if (typeof start !== 'number') {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
if (start + search.length > str.length) {
|
||||
return false;
|
||||
} else {
|
||||
return str.indexOf(search, start) !== -1;
|
||||
}
|
||||
}
|
||||
|
||||
createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
|
||||
return 'The value "' + value + '" is invalid for option "' + name + '"';
|
||||
}, TypeError);
|
||||
createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
|
||||
// determiner: 'must be' or 'must not be'
|
||||
var determiner;
|
||||
|
||||
if (typeof expected === 'string' && startsWith(expected, 'not ')) {
|
||||
determiner = 'must not be';
|
||||
expected = expected.replace(/^not /, '');
|
||||
} else {
|
||||
determiner = 'must be';
|
||||
}
|
||||
|
||||
var msg;
|
||||
|
||||
if (endsWith(name, ' argument')) {
|
||||
// For cases like 'first argument'
|
||||
msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
|
||||
} else {
|
||||
var type = includes(name, '.') ? 'property' : 'argument';
|
||||
msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type'));
|
||||
}
|
||||
|
||||
msg += ". Received type ".concat(typeof actual);
|
||||
return msg;
|
||||
}, TypeError);
|
||||
createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
|
||||
createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
|
||||
return 'The ' + name + ' method is not implemented';
|
||||
});
|
||||
createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
|
||||
createErrorType('ERR_STREAM_DESTROYED', function (name) {
|
||||
return 'Cannot call ' + name + ' after a stream was destroyed';
|
||||
});
|
||||
createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
|
||||
createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
|
||||
createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
||||
createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
||||
createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
|
||||
return 'Unknown encoding: ' + arg;
|
||||
}, TypeError);
|
||||
createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
|
||||
module.exports.codes = codes;
|
||||
Generated
Vendored
+116
@@ -0,0 +1,116 @@
|
||||
'use strict';
|
||||
|
||||
const codes = {};
|
||||
|
||||
function createErrorType(code, message, Base) {
|
||||
if (!Base) {
|
||||
Base = Error
|
||||
}
|
||||
|
||||
function getMessage (arg1, arg2, arg3) {
|
||||
if (typeof message === 'string') {
|
||||
return message
|
||||
} else {
|
||||
return message(arg1, arg2, arg3)
|
||||
}
|
||||
}
|
||||
|
||||
class NodeError extends Base {
|
||||
constructor (arg1, arg2, arg3) {
|
||||
super(getMessage(arg1, arg2, arg3));
|
||||
}
|
||||
}
|
||||
|
||||
NodeError.prototype.name = Base.name;
|
||||
NodeError.prototype.code = code;
|
||||
|
||||
codes[code] = NodeError;
|
||||
}
|
||||
|
||||
// https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js
|
||||
function oneOf(expected, thing) {
|
||||
if (Array.isArray(expected)) {
|
||||
const len = expected.length;
|
||||
expected = expected.map((i) => String(i));
|
||||
if (len > 2) {
|
||||
return `one of ${thing} ${expected.slice(0, len - 1).join(', ')}, or ` +
|
||||
expected[len - 1];
|
||||
} else if (len === 2) {
|
||||
return `one of ${thing} ${expected[0]} or ${expected[1]}`;
|
||||
} else {
|
||||
return `of ${thing} ${expected[0]}`;
|
||||
}
|
||||
} else {
|
||||
return `of ${thing} ${String(expected)}`;
|
||||
}
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
|
||||
function startsWith(str, search, pos) {
|
||||
return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
|
||||
function endsWith(str, search, this_len) {
|
||||
if (this_len === undefined || this_len > str.length) {
|
||||
this_len = str.length;
|
||||
}
|
||||
return str.substring(this_len - search.length, this_len) === search;
|
||||
}
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
|
||||
function includes(str, search, start) {
|
||||
if (typeof start !== 'number') {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
if (start + search.length > str.length) {
|
||||
return false;
|
||||
} else {
|
||||
return str.indexOf(search, start) !== -1;
|
||||
}
|
||||
}
|
||||
|
||||
createErrorType('ERR_INVALID_OPT_VALUE', function (name, value) {
|
||||
return 'The value "' + value + '" is invalid for option "' + name + '"'
|
||||
}, TypeError);
|
||||
createErrorType('ERR_INVALID_ARG_TYPE', function (name, expected, actual) {
|
||||
// determiner: 'must be' or 'must not be'
|
||||
let determiner;
|
||||
if (typeof expected === 'string' && startsWith(expected, 'not ')) {
|
||||
determiner = 'must not be';
|
||||
expected = expected.replace(/^not /, '');
|
||||
} else {
|
||||
determiner = 'must be';
|
||||
}
|
||||
|
||||
let msg;
|
||||
if (endsWith(name, ' argument')) {
|
||||
// For cases like 'first argument'
|
||||
msg = `The ${name} ${determiner} ${oneOf(expected, 'type')}`;
|
||||
} else {
|
||||
const type = includes(name, '.') ? 'property' : 'argument';
|
||||
msg = `The "${name}" ${type} ${determiner} ${oneOf(expected, 'type')}`;
|
||||
}
|
||||
|
||||
msg += `. Received type ${typeof actual}`;
|
||||
return msg;
|
||||
}, TypeError);
|
||||
createErrorType('ERR_STREAM_PUSH_AFTER_EOF', 'stream.push() after EOF');
|
||||
createErrorType('ERR_METHOD_NOT_IMPLEMENTED', function (name) {
|
||||
return 'The ' + name + ' method is not implemented'
|
||||
});
|
||||
createErrorType('ERR_STREAM_PREMATURE_CLOSE', 'Premature close');
|
||||
createErrorType('ERR_STREAM_DESTROYED', function (name) {
|
||||
return 'Cannot call ' + name + ' after a stream was destroyed';
|
||||
});
|
||||
createErrorType('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times');
|
||||
createErrorType('ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable');
|
||||
createErrorType('ERR_STREAM_WRITE_AFTER_END', 'write after end');
|
||||
createErrorType('ERR_STREAM_NULL_VALUES', 'May not write null values to stream', TypeError);
|
||||
createErrorType('ERR_UNKNOWN_ENCODING', function (arg) {
|
||||
return 'Unknown encoding: ' + arg
|
||||
}, TypeError);
|
||||
createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event');
|
||||
|
||||
module.exports.codes = codes;
|
||||
Generated
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
var experimentalWarnings = new Set();
|
||||
|
||||
function emitExperimentalWarning(feature) {
|
||||
if (experimentalWarnings.has(feature)) return;
|
||||
var msg = feature + ' is an experimental feature. This feature could ' +
|
||||
'change at any time';
|
||||
experimentalWarnings.add(feature);
|
||||
process.emitWarning(msg, 'ExperimentalWarning');
|
||||
}
|
||||
|
||||
function noop() {}
|
||||
|
||||
module.exports.emitExperimentalWarning = process.emitWarning
|
||||
? emitExperimentalWarning
|
||||
: noop;
|
||||
Generated
Vendored
+139
@@ -0,0 +1,139 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// 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.
|
||||
// a duplex stream is just a stream that is both readable and writable.
|
||||
// Since JS doesn't have multiple prototypal inheritance, this class
|
||||
// prototypally inherits from Readable, and then parasitically from
|
||||
// Writable.
|
||||
'use strict';
|
||||
/*<replacement>*/
|
||||
|
||||
var objectKeys = Object.keys || function (obj) {
|
||||
var keys = [];
|
||||
|
||||
for (var key in obj) {
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
return keys;
|
||||
};
|
||||
/*</replacement>*/
|
||||
|
||||
|
||||
module.exports = Duplex;
|
||||
|
||||
var Readable = require('./_stream_readable');
|
||||
|
||||
var Writable = require('./_stream_writable');
|
||||
|
||||
require('inherits')(Duplex, Readable);
|
||||
|
||||
{
|
||||
// Allow the keys array to be GC'ed.
|
||||
var keys = objectKeys(Writable.prototype);
|
||||
|
||||
for (var v = 0; v < keys.length; v++) {
|
||||
var method = keys[v];
|
||||
if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method];
|
||||
}
|
||||
}
|
||||
|
||||
function Duplex(options) {
|
||||
if (!(this instanceof Duplex)) return new Duplex(options);
|
||||
Readable.call(this, options);
|
||||
Writable.call(this, options);
|
||||
this.allowHalfOpen = true;
|
||||
|
||||
if (options) {
|
||||
if (options.readable === false) this.readable = false;
|
||||
if (options.writable === false) this.writable = false;
|
||||
|
||||
if (options.allowHalfOpen === false) {
|
||||
this.allowHalfOpen = false;
|
||||
this.once('end', onend);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState.highWaterMark;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(Duplex.prototype, 'writableBuffer', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState && this._writableState.getBuffer();
|
||||
}
|
||||
});
|
||||
Object.defineProperty(Duplex.prototype, 'writableLength', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState.length;
|
||||
}
|
||||
}); // the no-half-open enforcer
|
||||
|
||||
function onend() {
|
||||
// If the writable side ended, then we're ok.
|
||||
if (this._writableState.ended) return; // no more data can be written.
|
||||
// But allow more writes to happen in this tick.
|
||||
|
||||
process.nextTick(onEndNT, this);
|
||||
}
|
||||
|
||||
function onEndNT(self) {
|
||||
self.end();
|
||||
}
|
||||
|
||||
Object.defineProperty(Duplex.prototype, 'destroyed', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
if (this._readableState === undefined || this._writableState === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this._readableState.destroyed && this._writableState.destroyed;
|
||||
},
|
||||
set: function set(value) {
|
||||
// we ignore the value if the stream
|
||||
// has not been initialized yet
|
||||
if (this._readableState === undefined || this._writableState === undefined) {
|
||||
return;
|
||||
} // backward compatibility, the user is explicitly
|
||||
// managing destroyed
|
||||
|
||||
|
||||
this._readableState.destroyed = value;
|
||||
this._writableState.destroyed = value;
|
||||
}
|
||||
});
|
||||
contrib/bake-vscode/node_modules/htmlparser2/node_modules/readable-stream/lib/_stream_passthrough.js
Generated
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// 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.
|
||||
// a passthrough stream.
|
||||
// basically just the most minimal sort of Transform stream.
|
||||
// Every written chunk gets output as-is.
|
||||
'use strict';
|
||||
|
||||
module.exports = PassThrough;
|
||||
|
||||
var Transform = require('./_stream_transform');
|
||||
|
||||
require('inherits')(PassThrough, Transform);
|
||||
|
||||
function PassThrough(options) {
|
||||
if (!(this instanceof PassThrough)) return new PassThrough(options);
|
||||
Transform.call(this, options);
|
||||
}
|
||||
|
||||
PassThrough.prototype._transform = function (chunk, encoding, cb) {
|
||||
cb(null, chunk);
|
||||
};
|
||||
Generated
Vendored
+1087
File diff suppressed because it is too large
Load Diff
Generated
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// 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.
|
||||
// a transform stream is a readable/writable stream where you do
|
||||
// something with the data. Sometimes it's called a "filter",
|
||||
// but that's not a great name for it, since that implies a thing where
|
||||
// some bits pass through, and others are simply ignored. (That would
|
||||
// be a valid example of a transform, of course.)
|
||||
//
|
||||
// While the output is causally related to the input, it's not a
|
||||
// necessarily symmetric or synchronous transformation. For example,
|
||||
// a zlib stream might take multiple plain-text writes(), and then
|
||||
// emit a single compressed chunk some time in the future.
|
||||
//
|
||||
// Here's how this works:
|
||||
//
|
||||
// The Transform stream has all the aspects of the readable and writable
|
||||
// stream classes. When you write(chunk), that calls _write(chunk,cb)
|
||||
// internally, and returns false if there's a lot of pending writes
|
||||
// buffered up. When you call read(), that calls _read(n) until
|
||||
// there's enough pending readable data buffered up.
|
||||
//
|
||||
// In a transform stream, the written data is placed in a buffer. When
|
||||
// _read(n) is called, it transforms the queued up data, calling the
|
||||
// buffered _write cb's as it consumes chunks. If consuming a single
|
||||
// written chunk would result in multiple output chunks, then the first
|
||||
// outputted bit calls the readcb, and subsequent chunks just go into
|
||||
// the read buffer, and will cause it to emit 'readable' if necessary.
|
||||
//
|
||||
// This way, back-pressure is actually determined by the reading side,
|
||||
// since _read has to be called to start processing a new chunk. However,
|
||||
// a pathological inflate type of transform can cause excessive buffering
|
||||
// here. For example, imagine a stream where every byte of input is
|
||||
// interpreted as an integer from 0-255, and then results in that many
|
||||
// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
|
||||
// 1kb of data being output. In this case, you could write a very small
|
||||
// amount of input, and end up with a very large amount of output. In
|
||||
// such a pathological inflating mechanism, there'd be no way to tell
|
||||
// the system to stop doing the transform. A single 4MB write could
|
||||
// cause the system to run out of memory.
|
||||
//
|
||||
// However, even in such a pathological case, only a single written chunk
|
||||
// would be consumed, and then the rest would wait (un-transformed) until
|
||||
// the results of the previous transformed chunk were consumed.
|
||||
'use strict';
|
||||
|
||||
module.exports = Transform;
|
||||
|
||||
var _require$codes = require('../errors').codes,
|
||||
ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
|
||||
ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK,
|
||||
ERR_TRANSFORM_ALREADY_TRANSFORMING = _require$codes.ERR_TRANSFORM_ALREADY_TRANSFORMING,
|
||||
ERR_TRANSFORM_WITH_LENGTH_0 = _require$codes.ERR_TRANSFORM_WITH_LENGTH_0;
|
||||
|
||||
var Duplex = require('./_stream_duplex');
|
||||
|
||||
require('inherits')(Transform, Duplex);
|
||||
|
||||
function afterTransform(er, data) {
|
||||
var ts = this._transformState;
|
||||
ts.transforming = false;
|
||||
var cb = ts.writecb;
|
||||
|
||||
if (cb === null) {
|
||||
return this.emit('error', new ERR_MULTIPLE_CALLBACK());
|
||||
}
|
||||
|
||||
ts.writechunk = null;
|
||||
ts.writecb = null;
|
||||
if (data != null) // single equals check for both `null` and `undefined`
|
||||
this.push(data);
|
||||
cb(er);
|
||||
var rs = this._readableState;
|
||||
rs.reading = false;
|
||||
|
||||
if (rs.needReadable || rs.length < rs.highWaterMark) {
|
||||
this._read(rs.highWaterMark);
|
||||
}
|
||||
}
|
||||
|
||||
function Transform(options) {
|
||||
if (!(this instanceof Transform)) return new Transform(options);
|
||||
Duplex.call(this, options);
|
||||
this._transformState = {
|
||||
afterTransform: afterTransform.bind(this),
|
||||
needTransform: false,
|
||||
transforming: false,
|
||||
writecb: null,
|
||||
writechunk: null,
|
||||
writeencoding: null
|
||||
}; // start out asking for a readable event once data is transformed.
|
||||
|
||||
this._readableState.needReadable = true; // we have implemented the _read method, and done the other things
|
||||
// that Readable wants before the first _read call, so unset the
|
||||
// sync guard flag.
|
||||
|
||||
this._readableState.sync = false;
|
||||
|
||||
if (options) {
|
||||
if (typeof options.transform === 'function') this._transform = options.transform;
|
||||
if (typeof options.flush === 'function') this._flush = options.flush;
|
||||
} // When the writable side finishes, then flush out anything remaining.
|
||||
|
||||
|
||||
this.on('prefinish', prefinish);
|
||||
}
|
||||
|
||||
function prefinish() {
|
||||
var _this = this;
|
||||
|
||||
if (typeof this._flush === 'function' && !this._readableState.destroyed) {
|
||||
this._flush(function (er, data) {
|
||||
done(_this, er, data);
|
||||
});
|
||||
} else {
|
||||
done(this, null, null);
|
||||
}
|
||||
}
|
||||
|
||||
Transform.prototype.push = function (chunk, encoding) {
|
||||
this._transformState.needTransform = false;
|
||||
return Duplex.prototype.push.call(this, chunk, encoding);
|
||||
}; // This is the part where you do stuff!
|
||||
// override this function in implementation classes.
|
||||
// 'chunk' is an input chunk.
|
||||
//
|
||||
// Call `push(newChunk)` to pass along transformed output
|
||||
// to the readable side. You may call 'push' zero or more times.
|
||||
//
|
||||
// Call `cb(err)` when you are done with this chunk. If you pass
|
||||
// an error, then that'll put the hurt on the whole operation. If you
|
||||
// never call cb(), then you'll never get another chunk.
|
||||
|
||||
|
||||
Transform.prototype._transform = function (chunk, encoding, cb) {
|
||||
cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
|
||||
};
|
||||
|
||||
Transform.prototype._write = function (chunk, encoding, cb) {
|
||||
var ts = this._transformState;
|
||||
ts.writecb = cb;
|
||||
ts.writechunk = chunk;
|
||||
ts.writeencoding = encoding;
|
||||
|
||||
if (!ts.transforming) {
|
||||
var rs = this._readableState;
|
||||
if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
|
||||
}
|
||||
}; // Doesn't matter what the args are here.
|
||||
// _transform does all the work.
|
||||
// That we got here means that the readable side wants more data.
|
||||
|
||||
|
||||
Transform.prototype._read = function (n) {
|
||||
var ts = this._transformState;
|
||||
|
||||
if (ts.writechunk !== null && !ts.transforming) {
|
||||
ts.transforming = true;
|
||||
|
||||
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
|
||||
} else {
|
||||
// mark that we need a transform, so that any data that comes in
|
||||
// will get processed, now that we've asked for it.
|
||||
ts.needTransform = true;
|
||||
}
|
||||
};
|
||||
|
||||
Transform.prototype._destroy = function (err, cb) {
|
||||
Duplex.prototype._destroy.call(this, err, function (err2) {
|
||||
cb(err2);
|
||||
});
|
||||
};
|
||||
|
||||
function done(stream, er, data) {
|
||||
if (er) return stream.emit('error', er);
|
||||
if (data != null) // single equals check for both `null` and `undefined`
|
||||
stream.push(data); // TODO(BridgeAR): Write a test for these two error cases
|
||||
// if there's nothing in the write buffer, then that means
|
||||
// that nothing more will ever be provided
|
||||
|
||||
if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0();
|
||||
if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
|
||||
return stream.push(null);
|
||||
}
|
||||
Generated
Vendored
+683
@@ -0,0 +1,683 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// 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.
|
||||
// A bit simpler than readable streams.
|
||||
// Implement an async ._write(chunk, encoding, cb), and it'll handle all
|
||||
// the drain event emission and buffering.
|
||||
'use strict';
|
||||
|
||||
module.exports = Writable;
|
||||
/* <replacement> */
|
||||
|
||||
function WriteReq(chunk, encoding, cb) {
|
||||
this.chunk = chunk;
|
||||
this.encoding = encoding;
|
||||
this.callback = cb;
|
||||
this.next = null;
|
||||
} // It seems a linked list but it is not
|
||||
// there will be only 2 of these for each stream
|
||||
|
||||
|
||||
function CorkedRequest(state) {
|
||||
var _this = this;
|
||||
|
||||
this.next = null;
|
||||
this.entry = null;
|
||||
|
||||
this.finish = function () {
|
||||
onCorkedFinish(_this, state);
|
||||
};
|
||||
}
|
||||
/* </replacement> */
|
||||
|
||||
/*<replacement>*/
|
||||
|
||||
|
||||
var Duplex;
|
||||
/*</replacement>*/
|
||||
|
||||
Writable.WritableState = WritableState;
|
||||
/*<replacement>*/
|
||||
|
||||
var internalUtil = {
|
||||
deprecate: require('util-deprecate')
|
||||
};
|
||||
/*</replacement>*/
|
||||
|
||||
/*<replacement>*/
|
||||
|
||||
var Stream = require('./internal/streams/stream');
|
||||
/*</replacement>*/
|
||||
|
||||
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
var OurUint8Array = global.Uint8Array || function () {};
|
||||
|
||||
function _uint8ArrayToBuffer(chunk) {
|
||||
return Buffer.from(chunk);
|
||||
}
|
||||
|
||||
function _isUint8Array(obj) {
|
||||
return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
|
||||
}
|
||||
|
||||
var destroyImpl = require('./internal/streams/destroy');
|
||||
|
||||
var _require = require('./internal/streams/state'),
|
||||
getHighWaterMark = _require.getHighWaterMark;
|
||||
|
||||
var _require$codes = require('../errors').codes,
|
||||
ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE,
|
||||
ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
|
||||
ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK,
|
||||
ERR_STREAM_CANNOT_PIPE = _require$codes.ERR_STREAM_CANNOT_PIPE,
|
||||
ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED,
|
||||
ERR_STREAM_NULL_VALUES = _require$codes.ERR_STREAM_NULL_VALUES,
|
||||
ERR_STREAM_WRITE_AFTER_END = _require$codes.ERR_STREAM_WRITE_AFTER_END,
|
||||
ERR_UNKNOWN_ENCODING = _require$codes.ERR_UNKNOWN_ENCODING;
|
||||
|
||||
require('inherits')(Writable, Stream);
|
||||
|
||||
function nop() {}
|
||||
|
||||
function WritableState(options, stream, isDuplex) {
|
||||
Duplex = Duplex || require('./_stream_duplex');
|
||||
options = options || {}; // Duplex streams are both readable and writable, but share
|
||||
// the same options object.
|
||||
// However, some cases require setting options to different
|
||||
// values for the readable and the writable sides of the duplex stream,
|
||||
// e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
|
||||
|
||||
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag to indicate whether or not this stream
|
||||
// contains buffers or objects.
|
||||
|
||||
this.objectMode = !!options.objectMode;
|
||||
if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // the point at which write() starts returning false
|
||||
// Note: 0 is a valid value, means that we always return false if
|
||||
// the entire buffer is not flushed immediately on write()
|
||||
|
||||
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex); // if _final has been called
|
||||
|
||||
this.finalCalled = false; // drain event flag.
|
||||
|
||||
this.needDrain = false; // at the start of calling end()
|
||||
|
||||
this.ending = false; // when end() has been called, and returned
|
||||
|
||||
this.ended = false; // when 'finish' is emitted
|
||||
|
||||
this.finished = false; // has it been destroyed
|
||||
|
||||
this.destroyed = false; // should we decode strings into buffers before passing to _write?
|
||||
// this is here so that some node-core streams can optimize string
|
||||
// handling at a lower level.
|
||||
|
||||
var noDecode = options.decodeStrings === false;
|
||||
this.decodeStrings = !noDecode; // Crypto is kind of old and crusty. Historically, its default string
|
||||
// encoding is 'binary' so we have to make this configurable.
|
||||
// Everything else in the universe uses 'utf8', though.
|
||||
|
||||
this.defaultEncoding = options.defaultEncoding || 'utf8'; // not an actual buffer we keep track of, but a measurement
|
||||
// of how much we're waiting to get pushed to some underlying
|
||||
// socket or file.
|
||||
|
||||
this.length = 0; // a flag to see when we're in the middle of a write.
|
||||
|
||||
this.writing = false; // when true all writes will be buffered until .uncork() call
|
||||
|
||||
this.corked = 0; // a flag to be able to tell if the onwrite cb is called immediately,
|
||||
// or on a later tick. We set this to true at first, because any
|
||||
// actions that shouldn't happen until "later" should generally also
|
||||
// not happen before the first write call.
|
||||
|
||||
this.sync = true; // a flag to know if we're processing previously buffered items, which
|
||||
// may call the _write() callback in the same tick, so that we don't
|
||||
// end up in an overlapped onwrite situation.
|
||||
|
||||
this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb)
|
||||
|
||||
this.onwrite = function (er) {
|
||||
onwrite(stream, er);
|
||||
}; // the callback that the user supplies to write(chunk,encoding,cb)
|
||||
|
||||
|
||||
this.writecb = null; // the amount that is being written when _write is called.
|
||||
|
||||
this.writelen = 0;
|
||||
this.bufferedRequest = null;
|
||||
this.lastBufferedRequest = null; // number of pending user-supplied write callbacks
|
||||
// this must be 0 before 'finish' can be emitted
|
||||
|
||||
this.pendingcb = 0; // emit prefinish if the only thing we're waiting for is _write cbs
|
||||
// This is relevant for synchronous Transform streams
|
||||
|
||||
this.prefinished = false; // True if the error was already emitted and should not be thrown again
|
||||
|
||||
this.errorEmitted = false; // Should close be emitted on destroy. Defaults to true.
|
||||
|
||||
this.emitClose = options.emitClose !== false; // count buffered requests
|
||||
|
||||
this.bufferedRequestCount = 0; // allocate the first CorkedRequest, there is always
|
||||
// one allocated and free to use, and we maintain at most two
|
||||
|
||||
this.corkedRequestsFree = new CorkedRequest(this);
|
||||
}
|
||||
|
||||
WritableState.prototype.getBuffer = function getBuffer() {
|
||||
var current = this.bufferedRequest;
|
||||
var out = [];
|
||||
|
||||
while (current) {
|
||||
out.push(current);
|
||||
current = current.next;
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
(function () {
|
||||
try {
|
||||
Object.defineProperty(WritableState.prototype, 'buffer', {
|
||||
get: internalUtil.deprecate(function writableStateBufferGetter() {
|
||||
return this.getBuffer();
|
||||
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003')
|
||||
});
|
||||
} catch (_) {}
|
||||
})(); // Test _writableState for inheritance to account for Duplex streams,
|
||||
// whose prototype chain only points to Readable.
|
||||
|
||||
|
||||
var realHasInstance;
|
||||
|
||||
if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') {
|
||||
realHasInstance = Function.prototype[Symbol.hasInstance];
|
||||
Object.defineProperty(Writable, Symbol.hasInstance, {
|
||||
value: function value(object) {
|
||||
if (realHasInstance.call(this, object)) return true;
|
||||
if (this !== Writable) return false;
|
||||
return object && object._writableState instanceof WritableState;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
realHasInstance = function realHasInstance(object) {
|
||||
return object instanceof this;
|
||||
};
|
||||
}
|
||||
|
||||
function Writable(options) {
|
||||
Duplex = Duplex || require('./_stream_duplex'); // Writable ctor is applied to Duplexes, too.
|
||||
// `realHasInstance` is necessary because using plain `instanceof`
|
||||
// would return false, as no `_writableState` property is attached.
|
||||
// Trying to use the custom `instanceof` for Writable here will also break the
|
||||
// Node.js LazyTransform implementation, which has a non-trivial getter for
|
||||
// `_writableState` that would lead to infinite recursion.
|
||||
// Checking for a Stream.Duplex instance is faster here instead of inside
|
||||
// the WritableState constructor, at least with V8 6.5
|
||||
|
||||
var isDuplex = this instanceof Duplex;
|
||||
if (!isDuplex && !realHasInstance.call(Writable, this)) return new Writable(options);
|
||||
this._writableState = new WritableState(options, this, isDuplex); // legacy.
|
||||
|
||||
this.writable = true;
|
||||
|
||||
if (options) {
|
||||
if (typeof options.write === 'function') this._write = options.write;
|
||||
if (typeof options.writev === 'function') this._writev = options.writev;
|
||||
if (typeof options.destroy === 'function') this._destroy = options.destroy;
|
||||
if (typeof options.final === 'function') this._final = options.final;
|
||||
}
|
||||
|
||||
Stream.call(this);
|
||||
} // Otherwise people can pipe Writable streams, which is just wrong.
|
||||
|
||||
|
||||
Writable.prototype.pipe = function () {
|
||||
this.emit('error', new ERR_STREAM_CANNOT_PIPE());
|
||||
};
|
||||
|
||||
function writeAfterEnd(stream, cb) {
|
||||
var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb
|
||||
|
||||
stream.emit('error', er);
|
||||
process.nextTick(cb, er);
|
||||
} // Checks that a user-supplied chunk is valid, especially for the particular
|
||||
// mode the stream is in. Currently this means that `null` is never accepted
|
||||
// and undefined/non-string values are only allowed in object mode.
|
||||
|
||||
|
||||
function validChunk(stream, state, chunk, cb) {
|
||||
var er;
|
||||
|
||||
if (chunk === null) {
|
||||
er = new ERR_STREAM_NULL_VALUES();
|
||||
} else if (typeof chunk !== 'string' && !state.objectMode) {
|
||||
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
|
||||
}
|
||||
|
||||
if (er) {
|
||||
stream.emit('error', er);
|
||||
process.nextTick(cb, er);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Writable.prototype.write = function (chunk, encoding, cb) {
|
||||
var state = this._writableState;
|
||||
var ret = false;
|
||||
|
||||
var isBuf = !state.objectMode && _isUint8Array(chunk);
|
||||
|
||||
if (isBuf && !Buffer.isBuffer(chunk)) {
|
||||
chunk = _uint8ArrayToBuffer(chunk);
|
||||
}
|
||||
|
||||
if (typeof encoding === 'function') {
|
||||
cb = encoding;
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding;
|
||||
if (typeof cb !== 'function') cb = nop;
|
||||
if (state.ending) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) {
|
||||
state.pendingcb++;
|
||||
ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
Writable.prototype.cork = function () {
|
||||
this._writableState.corked++;
|
||||
};
|
||||
|
||||
Writable.prototype.uncork = function () {
|
||||
var state = this._writableState;
|
||||
|
||||
if (state.corked) {
|
||||
state.corked--;
|
||||
if (!state.writing && !state.corked && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state);
|
||||
}
|
||||
};
|
||||
|
||||
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
|
||||
// node::ParseEncoding() requires lower case.
|
||||
if (typeof encoding === 'string') encoding = encoding.toLowerCase();
|
||||
if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new ERR_UNKNOWN_ENCODING(encoding);
|
||||
this._writableState.defaultEncoding = encoding;
|
||||
return this;
|
||||
};
|
||||
|
||||
Object.defineProperty(Writable.prototype, 'writableBuffer', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState && this._writableState.getBuffer();
|
||||
}
|
||||
});
|
||||
|
||||
function decodeChunk(state, chunk, encoding) {
|
||||
if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') {
|
||||
chunk = Buffer.from(chunk, encoding);
|
||||
}
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState.highWaterMark;
|
||||
}
|
||||
}); // if we're already writing something, then just put this
|
||||
// in the queue, and wait our turn. Otherwise, call _write
|
||||
// If we return false, then we need a drain event, so set that flag.
|
||||
|
||||
function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) {
|
||||
if (!isBuf) {
|
||||
var newChunk = decodeChunk(state, chunk, encoding);
|
||||
|
||||
if (chunk !== newChunk) {
|
||||
isBuf = true;
|
||||
encoding = 'buffer';
|
||||
chunk = newChunk;
|
||||
}
|
||||
}
|
||||
|
||||
var len = state.objectMode ? 1 : chunk.length;
|
||||
state.length += len;
|
||||
var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false.
|
||||
|
||||
if (!ret) state.needDrain = true;
|
||||
|
||||
if (state.writing || state.corked) {
|
||||
var last = state.lastBufferedRequest;
|
||||
state.lastBufferedRequest = {
|
||||
chunk: chunk,
|
||||
encoding: encoding,
|
||||
isBuf: isBuf,
|
||||
callback: cb,
|
||||
next: null
|
||||
};
|
||||
|
||||
if (last) {
|
||||
last.next = state.lastBufferedRequest;
|
||||
} else {
|
||||
state.bufferedRequest = state.lastBufferedRequest;
|
||||
}
|
||||
|
||||
state.bufferedRequestCount += 1;
|
||||
} else {
|
||||
doWrite(stream, state, false, len, chunk, encoding, cb);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
function doWrite(stream, state, writev, len, chunk, encoding, cb) {
|
||||
state.writelen = len;
|
||||
state.writecb = cb;
|
||||
state.writing = true;
|
||||
state.sync = true;
|
||||
if (state.destroyed) state.onwrite(new ERR_STREAM_DESTROYED('write'));else if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite);
|
||||
state.sync = false;
|
||||
}
|
||||
|
||||
function onwriteError(stream, state, sync, er, cb) {
|
||||
--state.pendingcb;
|
||||
|
||||
if (sync) {
|
||||
// defer the callback if we are being called synchronously
|
||||
// to avoid piling up things on the stack
|
||||
process.nextTick(cb, er); // this can emit finish, and it will always happen
|
||||
// after error
|
||||
|
||||
process.nextTick(finishMaybe, stream, state);
|
||||
stream._writableState.errorEmitted = true;
|
||||
stream.emit('error', er);
|
||||
} else {
|
||||
// the caller expect this to happen before if
|
||||
// it is async
|
||||
cb(er);
|
||||
stream._writableState.errorEmitted = true;
|
||||
stream.emit('error', er); // this can emit finish, but finish must
|
||||
// always follow error
|
||||
|
||||
finishMaybe(stream, state);
|
||||
}
|
||||
}
|
||||
|
||||
function onwriteStateUpdate(state) {
|
||||
state.writing = false;
|
||||
state.writecb = null;
|
||||
state.length -= state.writelen;
|
||||
state.writelen = 0;
|
||||
}
|
||||
|
||||
function onwrite(stream, er) {
|
||||
var state = stream._writableState;
|
||||
var sync = state.sync;
|
||||
var cb = state.writecb;
|
||||
if (typeof cb !== 'function') throw new ERR_MULTIPLE_CALLBACK();
|
||||
onwriteStateUpdate(state);
|
||||
if (er) onwriteError(stream, state, sync, er, cb);else {
|
||||
// Check if we're actually ready to finish, but don't emit yet
|
||||
var finished = needFinish(state) || stream.destroyed;
|
||||
|
||||
if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) {
|
||||
clearBuffer(stream, state);
|
||||
}
|
||||
|
||||
if (sync) {
|
||||
process.nextTick(afterWrite, stream, state, finished, cb);
|
||||
} else {
|
||||
afterWrite(stream, state, finished, cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function afterWrite(stream, state, finished, cb) {
|
||||
if (!finished) onwriteDrain(stream, state);
|
||||
state.pendingcb--;
|
||||
cb();
|
||||
finishMaybe(stream, state);
|
||||
} // Must force callback to be called on nextTick, so that we don't
|
||||
// emit 'drain' before the write() consumer gets the 'false' return
|
||||
// value, and has a chance to attach a 'drain' listener.
|
||||
|
||||
|
||||
function onwriteDrain(stream, state) {
|
||||
if (state.length === 0 && state.needDrain) {
|
||||
state.needDrain = false;
|
||||
stream.emit('drain');
|
||||
}
|
||||
} // if there's something in the buffer waiting, then process it
|
||||
|
||||
|
||||
function clearBuffer(stream, state) {
|
||||
state.bufferProcessing = true;
|
||||
var entry = state.bufferedRequest;
|
||||
|
||||
if (stream._writev && entry && entry.next) {
|
||||
// Fast case, write everything using _writev()
|
||||
var l = state.bufferedRequestCount;
|
||||
var buffer = new Array(l);
|
||||
var holder = state.corkedRequestsFree;
|
||||
holder.entry = entry;
|
||||
var count = 0;
|
||||
var allBuffers = true;
|
||||
|
||||
while (entry) {
|
||||
buffer[count] = entry;
|
||||
if (!entry.isBuf) allBuffers = false;
|
||||
entry = entry.next;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
buffer.allBuffers = allBuffers;
|
||||
doWrite(stream, state, true, state.length, buffer, '', holder.finish); // doWrite is almost always async, defer these to save a bit of time
|
||||
// as the hot path ends with doWrite
|
||||
|
||||
state.pendingcb++;
|
||||
state.lastBufferedRequest = null;
|
||||
|
||||
if (holder.next) {
|
||||
state.corkedRequestsFree = holder.next;
|
||||
holder.next = null;
|
||||
} else {
|
||||
state.corkedRequestsFree = new CorkedRequest(state);
|
||||
}
|
||||
|
||||
state.bufferedRequestCount = 0;
|
||||
} else {
|
||||
// Slow case, write chunks one-by-one
|
||||
while (entry) {
|
||||
var chunk = entry.chunk;
|
||||
var encoding = entry.encoding;
|
||||
var cb = entry.callback;
|
||||
var len = state.objectMode ? 1 : chunk.length;
|
||||
doWrite(stream, state, false, len, chunk, encoding, cb);
|
||||
entry = entry.next;
|
||||
state.bufferedRequestCount--; // if we didn't call the onwrite immediately, then
|
||||
// it means that we need to wait until it does.
|
||||
// also, that means that the chunk and cb are currently
|
||||
// being processed, so move the buffer counter past them.
|
||||
|
||||
if (state.writing) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (entry === null) state.lastBufferedRequest = null;
|
||||
}
|
||||
|
||||
state.bufferedRequest = entry;
|
||||
state.bufferProcessing = false;
|
||||
}
|
||||
|
||||
Writable.prototype._write = function (chunk, encoding, cb) {
|
||||
cb(new ERR_METHOD_NOT_IMPLEMENTED('_write()'));
|
||||
};
|
||||
|
||||
Writable.prototype._writev = null;
|
||||
|
||||
Writable.prototype.end = function (chunk, encoding, cb) {
|
||||
var state = this._writableState;
|
||||
|
||||
if (typeof chunk === 'function') {
|
||||
cb = chunk;
|
||||
chunk = null;
|
||||
encoding = null;
|
||||
} else if (typeof encoding === 'function') {
|
||||
cb = encoding;
|
||||
encoding = null;
|
||||
}
|
||||
|
||||
if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks
|
||||
|
||||
if (state.corked) {
|
||||
state.corked = 1;
|
||||
this.uncork();
|
||||
} // ignore unnecessary end() calls.
|
||||
|
||||
|
||||
if (!state.ending) endWritable(this, state, cb);
|
||||
return this;
|
||||
};
|
||||
|
||||
Object.defineProperty(Writable.prototype, 'writableLength', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
return this._writableState.length;
|
||||
}
|
||||
});
|
||||
|
||||
function needFinish(state) {
|
||||
return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing;
|
||||
}
|
||||
|
||||
function callFinal(stream, state) {
|
||||
stream._final(function (err) {
|
||||
state.pendingcb--;
|
||||
|
||||
if (err) {
|
||||
stream.emit('error', err);
|
||||
}
|
||||
|
||||
state.prefinished = true;
|
||||
stream.emit('prefinish');
|
||||
finishMaybe(stream, state);
|
||||
});
|
||||
}
|
||||
|
||||
function prefinish(stream, state) {
|
||||
if (!state.prefinished && !state.finalCalled) {
|
||||
if (typeof stream._final === 'function' && !state.destroyed) {
|
||||
state.pendingcb++;
|
||||
state.finalCalled = true;
|
||||
process.nextTick(callFinal, stream, state);
|
||||
} else {
|
||||
state.prefinished = true;
|
||||
stream.emit('prefinish');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function finishMaybe(stream, state) {
|
||||
var need = needFinish(state);
|
||||
|
||||
if (need) {
|
||||
prefinish(stream, state);
|
||||
|
||||
if (state.pendingcb === 0) {
|
||||
state.finished = true;
|
||||
stream.emit('finish');
|
||||
}
|
||||
}
|
||||
|
||||
return need;
|
||||
}
|
||||
|
||||
function endWritable(stream, state, cb) {
|
||||
state.ending = true;
|
||||
finishMaybe(stream, state);
|
||||
|
||||
if (cb) {
|
||||
if (state.finished) process.nextTick(cb);else stream.once('finish', cb);
|
||||
}
|
||||
|
||||
state.ended = true;
|
||||
stream.writable = false;
|
||||
}
|
||||
|
||||
function onCorkedFinish(corkReq, state, err) {
|
||||
var entry = corkReq.entry;
|
||||
corkReq.entry = null;
|
||||
|
||||
while (entry) {
|
||||
var cb = entry.callback;
|
||||
state.pendingcb--;
|
||||
cb(err);
|
||||
entry = entry.next;
|
||||
} // reuse the free corkReq.
|
||||
|
||||
|
||||
state.corkedRequestsFree.next = corkReq;
|
||||
}
|
||||
|
||||
Object.defineProperty(Writable.prototype, 'destroyed', {
|
||||
// making it explicit this property is not enumerable
|
||||
// because otherwise some prototype manipulation in
|
||||
// userland will fail
|
||||
enumerable: false,
|
||||
get: function get() {
|
||||
if (this._writableState === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this._writableState.destroyed;
|
||||
},
|
||||
set: function set(value) {
|
||||
// we ignore the value if the stream
|
||||
// has not been initialized yet
|
||||
if (!this._writableState) {
|
||||
return;
|
||||
} // backward compatibility, the user is explicitly
|
||||
// managing destroyed
|
||||
|
||||
|
||||
this._writableState.destroyed = value;
|
||||
}
|
||||
});
|
||||
Writable.prototype.destroy = destroyImpl.destroy;
|
||||
Writable.prototype._undestroy = destroyImpl.undestroy;
|
||||
|
||||
Writable.prototype._destroy = function (err, cb) {
|
||||
cb(err);
|
||||
};
|
||||
Generated
Vendored
+207
@@ -0,0 +1,207 @@
|
||||
'use strict';
|
||||
|
||||
var _Object$setPrototypeO;
|
||||
|
||||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
|
||||
var finished = require('./end-of-stream');
|
||||
|
||||
var kLastResolve = Symbol('lastResolve');
|
||||
var kLastReject = Symbol('lastReject');
|
||||
var kError = Symbol('error');
|
||||
var kEnded = Symbol('ended');
|
||||
var kLastPromise = Symbol('lastPromise');
|
||||
var kHandlePromise = Symbol('handlePromise');
|
||||
var kStream = Symbol('stream');
|
||||
|
||||
function createIterResult(value, done) {
|
||||
return {
|
||||
value: value,
|
||||
done: done
|
||||
};
|
||||
}
|
||||
|
||||
function readAndResolve(iter) {
|
||||
var resolve = iter[kLastResolve];
|
||||
|
||||
if (resolve !== null) {
|
||||
var data = iter[kStream].read(); // we defer if data is null
|
||||
// we can be expecting either 'end' or
|
||||
// 'error'
|
||||
|
||||
if (data !== null) {
|
||||
iter[kLastPromise] = null;
|
||||
iter[kLastResolve] = null;
|
||||
iter[kLastReject] = null;
|
||||
resolve(createIterResult(data, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onReadable(iter) {
|
||||
// we wait for the next tick, because it might
|
||||
// emit an error with process.nextTick
|
||||
process.nextTick(readAndResolve, iter);
|
||||
}
|
||||
|
||||
function wrapForNext(lastPromise, iter) {
|
||||
return function (resolve, reject) {
|
||||
lastPromise.then(function () {
|
||||
if (iter[kEnded]) {
|
||||
resolve(createIterResult(undefined, true));
|
||||
return;
|
||||
}
|
||||
|
||||
iter[kHandlePromise](resolve, reject);
|
||||
}, reject);
|
||||
};
|
||||
}
|
||||
|
||||
var AsyncIteratorPrototype = Object.getPrototypeOf(function () {});
|
||||
var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPrototypeO = {
|
||||
get stream() {
|
||||
return this[kStream];
|
||||
},
|
||||
|
||||
next: function next() {
|
||||
var _this = this;
|
||||
|
||||
// if we have detected an error in the meanwhile
|
||||
// reject straight away
|
||||
var error = this[kError];
|
||||
|
||||
if (error !== null) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
if (this[kEnded]) {
|
||||
return Promise.resolve(createIterResult(undefined, true));
|
||||
}
|
||||
|
||||
if (this[kStream].destroyed) {
|
||||
// We need to defer via nextTick because if .destroy(err) is
|
||||
// called, the error will be emitted via nextTick, and
|
||||
// we cannot guarantee that there is no error lingering around
|
||||
// waiting to be emitted.
|
||||
return new Promise(function (resolve, reject) {
|
||||
process.nextTick(function () {
|
||||
if (_this[kError]) {
|
||||
reject(_this[kError]);
|
||||
} else {
|
||||
resolve(createIterResult(undefined, true));
|
||||
}
|
||||
});
|
||||
});
|
||||
} // if we have multiple next() calls
|
||||
// we will wait for the previous Promise to finish
|
||||
// this logic is optimized to support for await loops,
|
||||
// where next() is only called once at a time
|
||||
|
||||
|
||||
var lastPromise = this[kLastPromise];
|
||||
var promise;
|
||||
|
||||
if (lastPromise) {
|
||||
promise = new Promise(wrapForNext(lastPromise, this));
|
||||
} else {
|
||||
// fast path needed to support multiple this.push()
|
||||
// without triggering the next() queue
|
||||
var data = this[kStream].read();
|
||||
|
||||
if (data !== null) {
|
||||
return Promise.resolve(createIterResult(data, false));
|
||||
}
|
||||
|
||||
promise = new Promise(this[kHandlePromise]);
|
||||
}
|
||||
|
||||
this[kLastPromise] = promise;
|
||||
return promise;
|
||||
}
|
||||
}, _defineProperty(_Object$setPrototypeO, Symbol.asyncIterator, function () {
|
||||
return this;
|
||||
}), _defineProperty(_Object$setPrototypeO, "return", function _return() {
|
||||
var _this2 = this;
|
||||
|
||||
// destroy(err, cb) is a private API
|
||||
// we can guarantee we have that here, because we control the
|
||||
// Readable class this is attached to
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this2[kStream].destroy(null, function (err) {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(createIterResult(undefined, true));
|
||||
});
|
||||
});
|
||||
}), _Object$setPrototypeO), AsyncIteratorPrototype);
|
||||
|
||||
var createReadableStreamAsyncIterator = function createReadableStreamAsyncIterator(stream) {
|
||||
var _Object$create;
|
||||
|
||||
var iterator = Object.create(ReadableStreamAsyncIteratorPrototype, (_Object$create = {}, _defineProperty(_Object$create, kStream, {
|
||||
value: stream,
|
||||
writable: true
|
||||
}), _defineProperty(_Object$create, kLastResolve, {
|
||||
value: null,
|
||||
writable: true
|
||||
}), _defineProperty(_Object$create, kLastReject, {
|
||||
value: null,
|
||||
writable: true
|
||||
}), _defineProperty(_Object$create, kError, {
|
||||
value: null,
|
||||
writable: true
|
||||
}), _defineProperty(_Object$create, kEnded, {
|
||||
value: stream._readableState.endEmitted,
|
||||
writable: true
|
||||
}), _defineProperty(_Object$create, kHandlePromise, {
|
||||
value: function value(resolve, reject) {
|
||||
var data = iterator[kStream].read();
|
||||
|
||||
if (data) {
|
||||
iterator[kLastPromise] = null;
|
||||
iterator[kLastResolve] = null;
|
||||
iterator[kLastReject] = null;
|
||||
resolve(createIterResult(data, false));
|
||||
} else {
|
||||
iterator[kLastResolve] = resolve;
|
||||
iterator[kLastReject] = reject;
|
||||
}
|
||||
},
|
||||
writable: true
|
||||
}), _Object$create));
|
||||
iterator[kLastPromise] = null;
|
||||
finished(stream, function (err) {
|
||||
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
|
||||
var reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise
|
||||
// returned by next() and store the error
|
||||
|
||||
if (reject !== null) {
|
||||
iterator[kLastPromise] = null;
|
||||
iterator[kLastResolve] = null;
|
||||
iterator[kLastReject] = null;
|
||||
reject(err);
|
||||
}
|
||||
|
||||
iterator[kError] = err;
|
||||
return;
|
||||
}
|
||||
|
||||
var resolve = iterator[kLastResolve];
|
||||
|
||||
if (resolve !== null) {
|
||||
iterator[kLastPromise] = null;
|
||||
iterator[kLastResolve] = null;
|
||||
iterator[kLastReject] = null;
|
||||
resolve(createIterResult(undefined, true));
|
||||
}
|
||||
|
||||
iterator[kEnded] = true;
|
||||
});
|
||||
stream.on('readable', onReadable.bind(null, iterator));
|
||||
return iterator;
|
||||
};
|
||||
|
||||
module.exports = createReadableStreamAsyncIterator;
|
||||
Generated
Vendored
+189
@@ -0,0 +1,189 @@
|
||||
'use strict';
|
||||
|
||||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
||||
|
||||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
|
||||
var _require = require('buffer'),
|
||||
Buffer = _require.Buffer;
|
||||
|
||||
var _require2 = require('util'),
|
||||
inspect = _require2.inspect;
|
||||
|
||||
var custom = inspect && inspect.custom || 'inspect';
|
||||
|
||||
function copyBuffer(src, target, offset) {
|
||||
Buffer.prototype.copy.call(src, target, offset);
|
||||
}
|
||||
|
||||
module.exports =
|
||||
/*#__PURE__*/
|
||||
function () {
|
||||
function BufferList() {
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
var _proto = BufferList.prototype;
|
||||
|
||||
_proto.push = function push(v) {
|
||||
var entry = {
|
||||
data: v,
|
||||
next: null
|
||||
};
|
||||
if (this.length > 0) this.tail.next = entry;else this.head = entry;
|
||||
this.tail = entry;
|
||||
++this.length;
|
||||
};
|
||||
|
||||
_proto.unshift = function unshift(v) {
|
||||
var entry = {
|
||||
data: v,
|
||||
next: this.head
|
||||
};
|
||||
if (this.length === 0) this.tail = entry;
|
||||
this.head = entry;
|
||||
++this.length;
|
||||
};
|
||||
|
||||
_proto.shift = function shift() {
|
||||
if (this.length === 0) return;
|
||||
var ret = this.head.data;
|
||||
if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next;
|
||||
--this.length;
|
||||
return ret;
|
||||
};
|
||||
|
||||
_proto.clear = function clear() {
|
||||
this.head = this.tail = null;
|
||||
this.length = 0;
|
||||
};
|
||||
|
||||
_proto.join = function join(s) {
|
||||
if (this.length === 0) return '';
|
||||
var p = this.head;
|
||||
var ret = '' + p.data;
|
||||
|
||||
while (p = p.next) {
|
||||
ret += s + p.data;
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
_proto.concat = function concat(n) {
|
||||
if (this.length === 0) return Buffer.alloc(0);
|
||||
var ret = Buffer.allocUnsafe(n >>> 0);
|
||||
var p = this.head;
|
||||
var i = 0;
|
||||
|
||||
while (p) {
|
||||
copyBuffer(p.data, ret, i);
|
||||
i += p.data.length;
|
||||
p = p.next;
|
||||
}
|
||||
|
||||
return ret;
|
||||
} // Consumes a specified amount of bytes or characters from the buffered data.
|
||||
;
|
||||
|
||||
_proto.consume = function consume(n, hasStrings) {
|
||||
var ret;
|
||||
|
||||
if (n < this.head.data.length) {
|
||||
// `slice` is the same for buffers and strings.
|
||||
ret = this.head.data.slice(0, n);
|
||||
this.head.data = this.head.data.slice(n);
|
||||
} else if (n === this.head.data.length) {
|
||||
// First chunk is a perfect match.
|
||||
ret = this.shift();
|
||||
} else {
|
||||
// Result spans more than one buffer.
|
||||
ret = hasStrings ? this._getString(n) : this._getBuffer(n);
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
_proto.first = function first() {
|
||||
return this.head.data;
|
||||
} // Consumes a specified amount of characters from the buffered data.
|
||||
;
|
||||
|
||||
_proto._getString = function _getString(n) {
|
||||
var p = this.head;
|
||||
var c = 1;
|
||||
var ret = p.data;
|
||||
n -= ret.length;
|
||||
|
||||
while (p = p.next) {
|
||||
var str = p.data;
|
||||
var nb = n > str.length ? str.length : n;
|
||||
if (nb === str.length) ret += str;else ret += str.slice(0, n);
|
||||
n -= nb;
|
||||
|
||||
if (n === 0) {
|
||||
if (nb === str.length) {
|
||||
++c;
|
||||
if (p.next) this.head = p.next;else this.head = this.tail = null;
|
||||
} else {
|
||||
this.head = p;
|
||||
p.data = str.slice(nb);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
++c;
|
||||
}
|
||||
|
||||
this.length -= c;
|
||||
return ret;
|
||||
} // Consumes a specified amount of bytes from the buffered data.
|
||||
;
|
||||
|
||||
_proto._getBuffer = function _getBuffer(n) {
|
||||
var ret = Buffer.allocUnsafe(n);
|
||||
var p = this.head;
|
||||
var c = 1;
|
||||
p.data.copy(ret);
|
||||
n -= p.data.length;
|
||||
|
||||
while (p = p.next) {
|
||||
var buf = p.data;
|
||||
var nb = n > buf.length ? buf.length : n;
|
||||
buf.copy(ret, ret.length - n, 0, nb);
|
||||
n -= nb;
|
||||
|
||||
if (n === 0) {
|
||||
if (nb === buf.length) {
|
||||
++c;
|
||||
if (p.next) this.head = p.next;else this.head = this.tail = null;
|
||||
} else {
|
||||
this.head = p;
|
||||
p.data = buf.slice(nb);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
++c;
|
||||
}
|
||||
|
||||
this.length -= c;
|
||||
return ret;
|
||||
} // Make sure the linked list only shows the minimal necessary information.
|
||||
;
|
||||
|
||||
_proto[custom] = function (_, options) {
|
||||
return inspect(this, _objectSpread({}, options, {
|
||||
// Only inspect one level.
|
||||
depth: 0,
|
||||
// It should not recurse.
|
||||
customInspect: false
|
||||
}));
|
||||
};
|
||||
|
||||
return BufferList;
|
||||
}();
|
||||
Generated
Vendored
+85
@@ -0,0 +1,85 @@
|
||||
'use strict'; // undocumented cb() API, needed for core, not for public API
|
||||
|
||||
function destroy(err, cb) {
|
||||
var _this = this;
|
||||
|
||||
var readableDestroyed = this._readableState && this._readableState.destroyed;
|
||||
var writableDestroyed = this._writableState && this._writableState.destroyed;
|
||||
|
||||
if (readableDestroyed || writableDestroyed) {
|
||||
if (cb) {
|
||||
cb(err);
|
||||
} else if (err && (!this._writableState || !this._writableState.errorEmitted)) {
|
||||
process.nextTick(emitErrorNT, this, err);
|
||||
}
|
||||
|
||||
return this;
|
||||
} // we set destroyed to true before firing error callbacks in order
|
||||
// to make it re-entrance safe in case destroy() is called within callbacks
|
||||
|
||||
|
||||
if (this._readableState) {
|
||||
this._readableState.destroyed = true;
|
||||
} // if this is a duplex stream mark the writable part as destroyed as well
|
||||
|
||||
|
||||
if (this._writableState) {
|
||||
this._writableState.destroyed = true;
|
||||
}
|
||||
|
||||
this._destroy(err || null, function (err) {
|
||||
if (!cb && err) {
|
||||
process.nextTick(emitErrorAndCloseNT, _this, err);
|
||||
|
||||
if (_this._writableState) {
|
||||
_this._writableState.errorEmitted = true;
|
||||
}
|
||||
} else if (cb) {
|
||||
process.nextTick(emitCloseNT, _this);
|
||||
cb(err);
|
||||
} else {
|
||||
process.nextTick(emitCloseNT, _this);
|
||||
}
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
function emitErrorAndCloseNT(self, err) {
|
||||
emitErrorNT(self, err);
|
||||
emitCloseNT(self);
|
||||
}
|
||||
|
||||
function emitCloseNT(self) {
|
||||
if (self._writableState && !self._writableState.emitClose) return;
|
||||
if (self._readableState && !self._readableState.emitClose) return;
|
||||
self.emit('close');
|
||||
}
|
||||
|
||||
function undestroy() {
|
||||
if (this._readableState) {
|
||||
this._readableState.destroyed = false;
|
||||
this._readableState.reading = false;
|
||||
this._readableState.ended = false;
|
||||
this._readableState.endEmitted = false;
|
||||
}
|
||||
|
||||
if (this._writableState) {
|
||||
this._writableState.destroyed = false;
|
||||
this._writableState.ended = false;
|
||||
this._writableState.ending = false;
|
||||
this._writableState.finalCalled = false;
|
||||
this._writableState.prefinished = false;
|
||||
this._writableState.finished = false;
|
||||
this._writableState.errorEmitted = false;
|
||||
}
|
||||
}
|
||||
|
||||
function emitErrorNT(self, err) {
|
||||
self.emit('error', err);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
destroy: destroy,
|
||||
undestroy: undestroy
|
||||
};
|
||||
Generated
Vendored
+104
@@ -0,0 +1,104 @@
|
||||
// Ported from https://github.com/mafintosh/end-of-stream with
|
||||
// permission from the author, Mathias Buus (@mafintosh).
|
||||
'use strict';
|
||||
|
||||
var ERR_STREAM_PREMATURE_CLOSE = require('../../../errors').codes.ERR_STREAM_PREMATURE_CLOSE;
|
||||
|
||||
function once(callback) {
|
||||
var called = false;
|
||||
return function () {
|
||||
if (called) return;
|
||||
called = true;
|
||||
|
||||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
||||
args[_key] = arguments[_key];
|
||||
}
|
||||
|
||||
callback.apply(this, args);
|
||||
};
|
||||
}
|
||||
|
||||
function noop() {}
|
||||
|
||||
function isRequest(stream) {
|
||||
return stream.setHeader && typeof stream.abort === 'function';
|
||||
}
|
||||
|
||||
function eos(stream, opts, callback) {
|
||||
if (typeof opts === 'function') return eos(stream, null, opts);
|
||||
if (!opts) opts = {};
|
||||
callback = once(callback || noop);
|
||||
var readable = opts.readable || opts.readable !== false && stream.readable;
|
||||
var writable = opts.writable || opts.writable !== false && stream.writable;
|
||||
|
||||
var onlegacyfinish = function onlegacyfinish() {
|
||||
if (!stream.writable) onfinish();
|
||||
};
|
||||
|
||||
var writableEnded = stream._writableState && stream._writableState.finished;
|
||||
|
||||
var onfinish = function onfinish() {
|
||||
writable = false;
|
||||
writableEnded = true;
|
||||
if (!readable) callback.call(stream);
|
||||
};
|
||||
|
||||
var readableEnded = stream._readableState && stream._readableState.endEmitted;
|
||||
|
||||
var onend = function onend() {
|
||||
readable = false;
|
||||
readableEnded = true;
|
||||
if (!writable) callback.call(stream);
|
||||
};
|
||||
|
||||
var onerror = function onerror(err) {
|
||||
callback.call(stream, err);
|
||||
};
|
||||
|
||||
var onclose = function onclose() {
|
||||
var err;
|
||||
|
||||
if (readable && !readableEnded) {
|
||||
if (!stream._readableState || !stream._readableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
|
||||
return callback.call(stream, err);
|
||||
}
|
||||
|
||||
if (writable && !writableEnded) {
|
||||
if (!stream._writableState || !stream._writableState.ended) err = new ERR_STREAM_PREMATURE_CLOSE();
|
||||
return callback.call(stream, err);
|
||||
}
|
||||
};
|
||||
|
||||
var onrequest = function onrequest() {
|
||||
stream.req.on('finish', onfinish);
|
||||
};
|
||||
|
||||
if (isRequest(stream)) {
|
||||
stream.on('complete', onfinish);
|
||||
stream.on('abort', onclose);
|
||||
if (stream.req) onrequest();else stream.on('request', onrequest);
|
||||
} else if (writable && !stream._writableState) {
|
||||
// legacy streams
|
||||
stream.on('end', onlegacyfinish);
|
||||
stream.on('close', onlegacyfinish);
|
||||
}
|
||||
|
||||
stream.on('end', onend);
|
||||
stream.on('finish', onfinish);
|
||||
if (opts.error !== false) stream.on('error', onerror);
|
||||
stream.on('close', onclose);
|
||||
return function () {
|
||||
stream.removeListener('complete', onfinish);
|
||||
stream.removeListener('abort', onclose);
|
||||
stream.removeListener('request', onrequest);
|
||||
if (stream.req) stream.req.removeListener('finish', onfinish);
|
||||
stream.removeListener('end', onlegacyfinish);
|
||||
stream.removeListener('close', onlegacyfinish);
|
||||
stream.removeListener('finish', onfinish);
|
||||
stream.removeListener('end', onend);
|
||||
stream.removeListener('error', onerror);
|
||||
stream.removeListener('close', onclose);
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = eos;
|
||||
Generated
Vendored
+97
@@ -0,0 +1,97 @@
|
||||
// Ported from https://github.com/mafintosh/pump with
|
||||
// permission from the author, Mathias Buus (@mafintosh).
|
||||
'use strict';
|
||||
|
||||
var eos;
|
||||
|
||||
function once(callback) {
|
||||
var called = false;
|
||||
return function () {
|
||||
if (called) return;
|
||||
called = true;
|
||||
callback.apply(void 0, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
var _require$codes = require('../../../errors').codes,
|
||||
ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS,
|
||||
ERR_STREAM_DESTROYED = _require$codes.ERR_STREAM_DESTROYED;
|
||||
|
||||
function noop(err) {
|
||||
// Rethrow the error if it exists to avoid swallowing it
|
||||
if (err) throw err;
|
||||
}
|
||||
|
||||
function isRequest(stream) {
|
||||
return stream.setHeader && typeof stream.abort === 'function';
|
||||
}
|
||||
|
||||
function destroyer(stream, reading, writing, callback) {
|
||||
callback = once(callback);
|
||||
var closed = false;
|
||||
stream.on('close', function () {
|
||||
closed = true;
|
||||
});
|
||||
if (eos === undefined) eos = require('./end-of-stream');
|
||||
eos(stream, {
|
||||
readable: reading,
|
||||
writable: writing
|
||||
}, function (err) {
|
||||
if (err) return callback(err);
|
||||
closed = true;
|
||||
callback();
|
||||
});
|
||||
var destroyed = false;
|
||||
return function (err) {
|
||||
if (closed) return;
|
||||
if (destroyed) return;
|
||||
destroyed = true; // request.destroy just do .end - .abort is what we want
|
||||
|
||||
if (isRequest(stream)) return stream.abort();
|
||||
if (typeof stream.destroy === 'function') return stream.destroy();
|
||||
callback(err || new ERR_STREAM_DESTROYED('pipe'));
|
||||
};
|
||||
}
|
||||
|
||||
function call(fn) {
|
||||
fn();
|
||||
}
|
||||
|
||||
function pipe(from, to) {
|
||||
return from.pipe(to);
|
||||
}
|
||||
|
||||
function popCallback(streams) {
|
||||
if (!streams.length) return noop;
|
||||
if (typeof streams[streams.length - 1] !== 'function') return noop;
|
||||
return streams.pop();
|
||||
}
|
||||
|
||||
function pipeline() {
|
||||
for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) {
|
||||
streams[_key] = arguments[_key];
|
||||
}
|
||||
|
||||
var callback = popCallback(streams);
|
||||
if (Array.isArray(streams[0])) streams = streams[0];
|
||||
|
||||
if (streams.length < 2) {
|
||||
throw new ERR_MISSING_ARGS('streams');
|
||||
}
|
||||
|
||||
var error;
|
||||
var destroys = streams.map(function (stream, i) {
|
||||
var reading = i < streams.length - 1;
|
||||
var writing = i > 0;
|
||||
return destroyer(stream, reading, writing, function (err) {
|
||||
if (!error) error = err;
|
||||
if (err) destroys.forEach(call);
|
||||
if (reading) return;
|
||||
destroys.forEach(call);
|
||||
callback(error);
|
||||
});
|
||||
});
|
||||
return streams.reduce(pipe);
|
||||
}
|
||||
|
||||
module.exports = pipeline;
|
||||
Generated
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
var ERR_INVALID_OPT_VALUE = require('../../../errors').codes.ERR_INVALID_OPT_VALUE;
|
||||
|
||||
function highWaterMarkFrom(options, isDuplex, duplexKey) {
|
||||
return options.highWaterMark != null ? options.highWaterMark : isDuplex ? options[duplexKey] : null;
|
||||
}
|
||||
|
||||
function getHighWaterMark(state, options, duplexKey, isDuplex) {
|
||||
var hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
|
||||
|
||||
if (hwm != null) {
|
||||
if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) {
|
||||
var name = isDuplex ? duplexKey : 'highWaterMark';
|
||||
throw new ERR_INVALID_OPT_VALUE(name, hwm);
|
||||
}
|
||||
|
||||
return Math.floor(hwm);
|
||||
} // Default value
|
||||
|
||||
|
||||
return state.objectMode ? 16 : 16 * 1024;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getHighWaterMark: getHighWaterMark
|
||||
};
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
module.exports = require('events').EventEmitter;
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
module.exports = require('stream');
|
||||
Generated
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"name": "readable-stream",
|
||||
"version": "3.4.0",
|
||||
"description": "Streams3, a user-land copy of the stream library from Node.js",
|
||||
"main": "readable.js",
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
},
|
||||
"dependencies": {
|
||||
"inherits": "^2.0.3",
|
||||
"string_decoder": "^1.1.1",
|
||||
"util-deprecate": "^1.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.2.0",
|
||||
"@babel/core": "^7.2.0",
|
||||
"@babel/polyfill": "^7.0.0",
|
||||
"@babel/preset-env": "^7.2.0",
|
||||
"airtap": "0.0.9",
|
||||
"assert": "^1.4.0",
|
||||
"bl": "^2.0.0",
|
||||
"deep-strict-equal": "^0.2.0",
|
||||
"glob": "^7.1.2",
|
||||
"gunzip-maybe": "^1.4.1",
|
||||
"hyperquest": "^2.1.3",
|
||||
"lolex": "^2.6.0",
|
||||
"nyc": "^11.0.0",
|
||||
"pump": "^3.0.0",
|
||||
"rimraf": "^2.6.2",
|
||||
"tap": "^12.0.0",
|
||||
"tape": "^4.9.0",
|
||||
"tar-fs": "^1.16.2",
|
||||
"util-promisify": "^2.1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tap -J --no-esm test/parallel/*.js test/ours/*.js",
|
||||
"ci": "TAP=1 tap --no-esm test/parallel/*.js test/ours/*.js | tee test.tap",
|
||||
"test-browsers": "airtap --sauce-connect --loopback airtap.local -- test/browser.js",
|
||||
"test-browser-local": "airtap --open --local -- test/browser.js",
|
||||
"cover": "nyc npm test",
|
||||
"report": "nyc report --reporter=lcov",
|
||||
"update-browser-errors": "babel -o errors-browser.js errors.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/nodejs/readable-stream"
|
||||
},
|
||||
"keywords": [
|
||||
"readable",
|
||||
"stream",
|
||||
"pipe"
|
||||
],
|
||||
"browser": {
|
||||
"util": false,
|
||||
"worker_threads": false,
|
||||
"./errors": "./errors-browser.js",
|
||||
"./readable.js": "./readable-browser.js",
|
||||
"./lib/internal/streams/stream.js": "./lib/internal/streams/stream-browser.js"
|
||||
},
|
||||
"nyc": {
|
||||
"include": [
|
||||
"lib/**.js"
|
||||
]
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
Generated
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
exports = module.exports = require('./lib/_stream_readable.js');
|
||||
exports.Stream = exports;
|
||||
exports.Readable = exports;
|
||||
exports.Writable = require('./lib/_stream_writable.js');
|
||||
exports.Duplex = require('./lib/_stream_duplex.js');
|
||||
exports.Transform = require('./lib/_stream_transform.js');
|
||||
exports.PassThrough = require('./lib/_stream_passthrough.js');
|
||||
exports.finished = require('./lib/internal/streams/end-of-stream.js');
|
||||
exports.pipeline = require('./lib/internal/streams/pipeline.js');
|
||||
Generated
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
var Stream = require('stream');
|
||||
if (process.env.READABLE_STREAM === 'disable' && Stream) {
|
||||
module.exports = Stream.Readable;
|
||||
Object.assign(module.exports, Stream);
|
||||
module.exports.Stream = Stream;
|
||||
} else {
|
||||
exports = module.exports = require('./lib/_stream_readable.js');
|
||||
exports.Stream = Stream || exports;
|
||||
exports.Readable = exports;
|
||||
exports.Writable = require('./lib/_stream_writable.js');
|
||||
exports.Duplex = require('./lib/_stream_duplex.js');
|
||||
exports.Transform = require('./lib/_stream_transform.js');
|
||||
exports.PassThrough = require('./lib/_stream_passthrough.js');
|
||||
exports.finished = require('./lib/internal/streams/end-of-stream.js');
|
||||
exports.pipeline = require('./lib/internal/streams/pipeline.js');
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
Node.js is licensed for use as follows:
|
||||
|
||||
"""
|
||||
Copyright Node.js contributors. 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.
|
||||
"""
|
||||
|
||||
This license applies to parts of Node.js originating from the
|
||||
https://github.com/joyent/node repository:
|
||||
|
||||
"""
|
||||
Copyright Joyent, Inc. and other Node contributors. 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.
|
||||
"""
|
||||
|
||||
Generated
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
# string_decoder
|
||||
|
||||
***Node-core v8.9.4 string_decoder for userland***
|
||||
|
||||
|
||||
[](https://nodei.co/npm/string_decoder/)
|
||||
[](https://nodei.co/npm/string_decoder/)
|
||||
|
||||
|
||||
```bash
|
||||
npm install --save string_decoder
|
||||
```
|
||||
|
||||
***Node-core string_decoder for userland***
|
||||
|
||||
This package is a mirror of the string_decoder implementation in Node-core.
|
||||
|
||||
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v8.9.4/docs/api/).
|
||||
|
||||
As of version 1.0.0 **string_decoder** uses semantic versioning.
|
||||
|
||||
## Previous versions
|
||||
|
||||
Previous version numbers match the versions found in Node core, e.g. 0.10.24 matches Node 0.10.24, likewise 0.11.10 matches Node 0.11.10.
|
||||
|
||||
## Update
|
||||
|
||||
The *build/* directory contains a build script that will scrape the source from the [nodejs/node](https://github.com/nodejs/node) repo given a specific Node version.
|
||||
|
||||
## Streams Working Group
|
||||
|
||||
`string_decoder` is maintained by the Streams Working Group, which
|
||||
oversees the development and maintenance of the Streams API within
|
||||
Node.js. The responsibilities of the Streams Working Group include:
|
||||
|
||||
* Addressing stream issues on the Node.js issue tracker.
|
||||
* Authoring and editing stream documentation within the Node.js project.
|
||||
* Reviewing changes to stream subclasses within the Node.js project.
|
||||
* Redirecting changes to streams from the Node.js project to this
|
||||
project.
|
||||
* Assisting in the implementation of stream providers within Node.js.
|
||||
* Recommending versions of `readable-stream` to be included in Node.js.
|
||||
* Messaging about the future of streams to give the community advance
|
||||
notice of changes.
|
||||
|
||||
See [readable-stream](https://github.com/nodejs/readable-stream) for
|
||||
more details.
|
||||
Generated
Vendored
+296
@@ -0,0 +1,296 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// 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.
|
||||
|
||||
'use strict';
|
||||
|
||||
/*<replacement>*/
|
||||
|
||||
var Buffer = require('safe-buffer').Buffer;
|
||||
/*</replacement>*/
|
||||
|
||||
var isEncoding = Buffer.isEncoding || function (encoding) {
|
||||
encoding = '' + encoding;
|
||||
switch (encoding && encoding.toLowerCase()) {
|
||||
case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
function _normalizeEncoding(enc) {
|
||||
if (!enc) return 'utf8';
|
||||
var retried;
|
||||
while (true) {
|
||||
switch (enc) {
|
||||
case 'utf8':
|
||||
case 'utf-8':
|
||||
return 'utf8';
|
||||
case 'ucs2':
|
||||
case 'ucs-2':
|
||||
case 'utf16le':
|
||||
case 'utf-16le':
|
||||
return 'utf16le';
|
||||
case 'latin1':
|
||||
case 'binary':
|
||||
return 'latin1';
|
||||
case 'base64':
|
||||
case 'ascii':
|
||||
case 'hex':
|
||||
return enc;
|
||||
default:
|
||||
if (retried) return; // undefined
|
||||
enc = ('' + enc).toLowerCase();
|
||||
retried = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Do not cache `Buffer.isEncoding` when checking encoding names as some
|
||||
// modules monkey-patch it to support additional encodings
|
||||
function normalizeEncoding(enc) {
|
||||
var nenc = _normalizeEncoding(enc);
|
||||
if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
|
||||
return nenc || enc;
|
||||
}
|
||||
|
||||
// StringDecoder provides an interface for efficiently splitting a series of
|
||||
// buffers into a series of JS strings without breaking apart multi-byte
|
||||
// characters.
|
||||
exports.StringDecoder = StringDecoder;
|
||||
function StringDecoder(encoding) {
|
||||
this.encoding = normalizeEncoding(encoding);
|
||||
var nb;
|
||||
switch (this.encoding) {
|
||||
case 'utf16le':
|
||||
this.text = utf16Text;
|
||||
this.end = utf16End;
|
||||
nb = 4;
|
||||
break;
|
||||
case 'utf8':
|
||||
this.fillLast = utf8FillLast;
|
||||
nb = 4;
|
||||
break;
|
||||
case 'base64':
|
||||
this.text = base64Text;
|
||||
this.end = base64End;
|
||||
nb = 3;
|
||||
break;
|
||||
default:
|
||||
this.write = simpleWrite;
|
||||
this.end = simpleEnd;
|
||||
return;
|
||||
}
|
||||
this.lastNeed = 0;
|
||||
this.lastTotal = 0;
|
||||
this.lastChar = Buffer.allocUnsafe(nb);
|
||||
}
|
||||
|
||||
StringDecoder.prototype.write = function (buf) {
|
||||
if (buf.length === 0) return '';
|
||||
var r;
|
||||
var i;
|
||||
if (this.lastNeed) {
|
||||
r = this.fillLast(buf);
|
||||
if (r === undefined) return '';
|
||||
i = this.lastNeed;
|
||||
this.lastNeed = 0;
|
||||
} else {
|
||||
i = 0;
|
||||
}
|
||||
if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
|
||||
return r || '';
|
||||
};
|
||||
|
||||
StringDecoder.prototype.end = utf8End;
|
||||
|
||||
// Returns only complete characters in a Buffer
|
||||
StringDecoder.prototype.text = utf8Text;
|
||||
|
||||
// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
|
||||
StringDecoder.prototype.fillLast = function (buf) {
|
||||
if (this.lastNeed <= buf.length) {
|
||||
buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
|
||||
return this.lastChar.toString(this.encoding, 0, this.lastTotal);
|
||||
}
|
||||
buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
|
||||
this.lastNeed -= buf.length;
|
||||
};
|
||||
|
||||
// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
|
||||
// continuation byte. If an invalid byte is detected, -2 is returned.
|
||||
function utf8CheckByte(byte) {
|
||||
if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
|
||||
return byte >> 6 === 0x02 ? -1 : -2;
|
||||
}
|
||||
|
||||
// Checks at most 3 bytes at the end of a Buffer in order to detect an
|
||||
// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
|
||||
// needed to complete the UTF-8 character (if applicable) are returned.
|
||||
function utf8CheckIncomplete(self, buf, i) {
|
||||
var j = buf.length - 1;
|
||||
if (j < i) return 0;
|
||||
var nb = utf8CheckByte(buf[j]);
|
||||
if (nb >= 0) {
|
||||
if (nb > 0) self.lastNeed = nb - 1;
|
||||
return nb;
|
||||
}
|
||||
if (--j < i || nb === -2) return 0;
|
||||
nb = utf8CheckByte(buf[j]);
|
||||
if (nb >= 0) {
|
||||
if (nb > 0) self.lastNeed = nb - 2;
|
||||
return nb;
|
||||
}
|
||||
if (--j < i || nb === -2) return 0;
|
||||
nb = utf8CheckByte(buf[j]);
|
||||
if (nb >= 0) {
|
||||
if (nb > 0) {
|
||||
if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
|
||||
}
|
||||
return nb;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Validates as many continuation bytes for a multi-byte UTF-8 character as
|
||||
// needed or are available. If we see a non-continuation byte where we expect
|
||||
// one, we "replace" the validated continuation bytes we've seen so far with
|
||||
// a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
|
||||
// behavior. The continuation byte check is included three times in the case
|
||||
// where all of the continuation bytes for a character exist in the same buffer.
|
||||
// It is also done this way as a slight performance increase instead of using a
|
||||
// loop.
|
||||
function utf8CheckExtraBytes(self, buf, p) {
|
||||
if ((buf[0] & 0xC0) !== 0x80) {
|
||||
self.lastNeed = 0;
|
||||
return '\ufffd';
|
||||
}
|
||||
if (self.lastNeed > 1 && buf.length > 1) {
|
||||
if ((buf[1] & 0xC0) !== 0x80) {
|
||||
self.lastNeed = 1;
|
||||
return '\ufffd';
|
||||
}
|
||||
if (self.lastNeed > 2 && buf.length > 2) {
|
||||
if ((buf[2] & 0xC0) !== 0x80) {
|
||||
self.lastNeed = 2;
|
||||
return '\ufffd';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
|
||||
function utf8FillLast(buf) {
|
||||
var p = this.lastTotal - this.lastNeed;
|
||||
var r = utf8CheckExtraBytes(this, buf, p);
|
||||
if (r !== undefined) return r;
|
||||
if (this.lastNeed <= buf.length) {
|
||||
buf.copy(this.lastChar, p, 0, this.lastNeed);
|
||||
return this.lastChar.toString(this.encoding, 0, this.lastTotal);
|
||||
}
|
||||
buf.copy(this.lastChar, p, 0, buf.length);
|
||||
this.lastNeed -= buf.length;
|
||||
}
|
||||
|
||||
// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
|
||||
// partial character, the character's bytes are buffered until the required
|
||||
// number of bytes are available.
|
||||
function utf8Text(buf, i) {
|
||||
var total = utf8CheckIncomplete(this, buf, i);
|
||||
if (!this.lastNeed) return buf.toString('utf8', i);
|
||||
this.lastTotal = total;
|
||||
var end = buf.length - (total - this.lastNeed);
|
||||
buf.copy(this.lastChar, 0, end);
|
||||
return buf.toString('utf8', i, end);
|
||||
}
|
||||
|
||||
// For UTF-8, a replacement character is added when ending on a partial
|
||||
// character.
|
||||
function utf8End(buf) {
|
||||
var r = buf && buf.length ? this.write(buf) : '';
|
||||
if (this.lastNeed) return r + '\ufffd';
|
||||
return r;
|
||||
}
|
||||
|
||||
// UTF-16LE typically needs two bytes per character, but even if we have an even
|
||||
// number of bytes available, we need to check if we end on a leading/high
|
||||
// surrogate. In that case, we need to wait for the next two bytes in order to
|
||||
// decode the last character properly.
|
||||
function utf16Text(buf, i) {
|
||||
if ((buf.length - i) % 2 === 0) {
|
||||
var r = buf.toString('utf16le', i);
|
||||
if (r) {
|
||||
var c = r.charCodeAt(r.length - 1);
|
||||
if (c >= 0xD800 && c <= 0xDBFF) {
|
||||
this.lastNeed = 2;
|
||||
this.lastTotal = 4;
|
||||
this.lastChar[0] = buf[buf.length - 2];
|
||||
this.lastChar[1] = buf[buf.length - 1];
|
||||
return r.slice(0, -1);
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
this.lastNeed = 1;
|
||||
this.lastTotal = 2;
|
||||
this.lastChar[0] = buf[buf.length - 1];
|
||||
return buf.toString('utf16le', i, buf.length - 1);
|
||||
}
|
||||
|
||||
// For UTF-16LE we do not explicitly append special replacement characters if we
|
||||
// end on a partial character, we simply let v8 handle that.
|
||||
function utf16End(buf) {
|
||||
var r = buf && buf.length ? this.write(buf) : '';
|
||||
if (this.lastNeed) {
|
||||
var end = this.lastTotal - this.lastNeed;
|
||||
return r + this.lastChar.toString('utf16le', 0, end);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
function base64Text(buf, i) {
|
||||
var n = (buf.length - i) % 3;
|
||||
if (n === 0) return buf.toString('base64', i);
|
||||
this.lastNeed = 3 - n;
|
||||
this.lastTotal = 3;
|
||||
if (n === 1) {
|
||||
this.lastChar[0] = buf[buf.length - 1];
|
||||
} else {
|
||||
this.lastChar[0] = buf[buf.length - 2];
|
||||
this.lastChar[1] = buf[buf.length - 1];
|
||||
}
|
||||
return buf.toString('base64', i, buf.length - n);
|
||||
}
|
||||
|
||||
function base64End(buf) {
|
||||
var r = buf && buf.length ? this.write(buf) : '';
|
||||
if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
|
||||
return r;
|
||||
}
|
||||
|
||||
// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
|
||||
function simpleWrite(buf) {
|
||||
return buf.toString(this.encoding);
|
||||
}
|
||||
|
||||
function simpleEnd(buf) {
|
||||
return buf && buf.length ? this.write(buf) : '';
|
||||
}
|
||||
Generated
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "string_decoder",
|
||||
"version": "1.3.0",
|
||||
"description": "The string_decoder module from Node core",
|
||||
"main": "lib/string_decoder.js",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"dependencies": {
|
||||
"safe-buffer": "~5.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-polyfill": "^6.23.0",
|
||||
"core-util-is": "^1.0.2",
|
||||
"inherits": "^2.0.3",
|
||||
"tap": "~0.4.8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tap test/parallel/*.js && node test/verify-dependencies",
|
||||
"ci": "tap test/parallel/*.js test/ours/*.js --tap | tee test.tap && node test/verify-dependencies.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/nodejs/string_decoder.git"
|
||||
},
|
||||
"homepage": "https://github.com/nodejs/string_decoder",
|
||||
"keywords": [
|
||||
"string",
|
||||
"decoder",
|
||||
"browser",
|
||||
"browserify"
|
||||
],
|
||||
"license": "MIT"
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
{
|
||||
"name": "htmlparser2",
|
||||
"description": "Fast & forgiving HTML/XML/RSS parser",
|
||||
"version": "3.10.1",
|
||||
"author": "Felix Boehm <me@feedic.com>",
|
||||
"keywords": [
|
||||
"html",
|
||||
"parser",
|
||||
"streams",
|
||||
"xml",
|
||||
"dom",
|
||||
"rss",
|
||||
"feed",
|
||||
"atom"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/fb55/htmlparser2.git"
|
||||
},
|
||||
"bugs": {
|
||||
"mail": "me@feedic.com",
|
||||
"url": "http://github.com/fb55/htmlparser2/issues"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "lib/"
|
||||
},
|
||||
"main": "lib/index.js",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"scripts": {
|
||||
"lcov": "istanbul cover _mocha --report lcovonly -- -R spec",
|
||||
"coveralls": "npm run lint && npm run lcov && (cat coverage/lcov.info | coveralls || exit 0)",
|
||||
"test": "mocha && npm run lint",
|
||||
"lint": "eslint lib test"
|
||||
},
|
||||
"dependencies": {
|
||||
"domelementtype": "^1.3.1",
|
||||
"domhandler": "^2.3.0",
|
||||
"domutils": "^1.5.1",
|
||||
"entities": "^1.1.1",
|
||||
"inherits": "^2.0.1",
|
||||
"readable-stream": "^3.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"coveralls": "^3.0.1",
|
||||
"eslint": "^5.13.0",
|
||||
"istanbul": "^0.4.3",
|
||||
"mocha": "^5.2.0",
|
||||
"mocha-lcov-reporter": "^1.2.0"
|
||||
},
|
||||
"browser": {
|
||||
"readable-stream": false
|
||||
},
|
||||
"license": "MIT",
|
||||
"prettier": {
|
||||
"tabWidth": 4
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user