|
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
|
var bindEditor = require('gulf-contenteditable') |
|
var domOT = require('dom-ot') |
|
var gulf = require('gulf') |
|
|
|
document.addEventListener('DOMContentLoaded', function () { |
|
var editable = document.querySelector('#doc[contenteditable]') |
|
var editableReplica = document.querySelector('#replica') |
|
var docB = bindEditor(editable) |
|
var docBReplica = bindEditor(editableReplica) |
|
|
|
gulf.Document.create( |
|
new gulf.MemoryAdapter, |
|
domOT, |
|
document.createElement('div'), |
|
function (er, docA) { |
|
|
|
var linkB = docB.masterLink() |
|
var linkBReplica = docBReplica.masterLink() |
|
|
|
linkB.pipe(docA.slaveLink()).pipe(linkB) |
|
linkBReplica.pipe(docA.slaveLink()).pipe(linkBReplica) |
|
|
|
}) |
|
}) |
|
},{"dom-ot":12,"gulf":22,"gulf-contenteditable":21}],2:[function(require,module,exports){ |
|
var Changeset = require('./Changeset') |
|
, Retain = require('./operations/Retain') |
|
, Skip = require('./operations/Skip') |
|
, Insert = require('./operations/Insert') |
|
|
|
function Builder() { |
|
this.ops = [] |
|
this.addendum = '' |
|
this.removendum = '' |
|
} |
|
|
|
module.exports = Builder |
|
|
|
Builder.prototype.keep = |
|
Builder.prototype.retain = function(len) { |
|
this.ops.push(new Retain(len)) |
|
return this |
|
} |
|
|
|
Builder.prototype.delete = |
|
Builder.prototype.skip = function(str) { |
|
this.removendum += str |
|
this.ops.push(new Skip(str.length)) |
|
return this |
|
} |
|
|
|
Builder.prototype.add = |
|
Builder.prototype.insert = function(str) { |
|
this.addendum += str |
|
this.ops.push(new Insert(str.length)) |
|
return this |
|
} |
|
|
|
Builder.prototype.end = function() { |
|
var cs = new Changeset(this.ops) |
|
cs.addendum = this.addendum |
|
cs.removendum = this.removendum |
|
return cs |
|
} |
|
},{"./Changeset":3,"./operations/Insert":8,"./operations/Retain":9,"./operations/Skip":10}],3:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational transformation (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* 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 sequence of consecutive operations |
|
* |
|
* @param ops.. <Operation> all passed operations will be added to the changeset |
|
*/ |
|
function Changeset(ops/*or ops..*/) { |
|
this.addendum = "" |
|
this.removendum = "" |
|
this.inputLength = 0 |
|
this.outputLength = 0 |
|
|
|
if(!Array.isArray(ops)) ops = arguments |
|
for(var i=0; i<ops.length; i++) { |
|
this.push(ops[i]) |
|
this.inputLength += ops[i].input |
|
this.outputLength += ops[i].output |
|
} |
|
} |
|
|
|
// True inheritance |
|
Changeset.prototype = Object.create(Array.prototype, { |
|
constructor: { |
|
value: Changeset, |
|
enumerable: false, |
|
writable: true, |
|
configurable: true |
|
} |
|
}); |
|
module.exports = Changeset |
|
|
|
var TextTransform = require('./TextTransform') |
|
, ChangesetTransform = require('./ChangesetTransform') |
|
|
|
var Retain = require('./operations/Retain') |
|
, Skip = require('./operations/Skip') |
|
, Insert = require('./operations/Insert') |
|
|
|
var Builder = require('./Builder') |
|
|
|
/** |
|
* Returns an array containing the ops that are within the passed range |
|
* (only op.input is counted; thus not counting inserts to the range length, yet they are part of the range) |
|
*/ |
|
Changeset.prototype.subrange = function(start, len) { |
|
var range = [] |
|
, op, oplen |
|
, l=0 |
|
for(var i=0, pos=0; i<this.length && l < len; i++) { |
|
op = this[i] |
|
if(op.length+pos > start) { |
|
if(op.input) { |
|
if(op.length != Infinity) oplen = op.length -Math.max(0, start-pos) -Math.max(0, (op.length+pos)-(start+len)) |
|
else oplen = len |
|
range.push( op.derive(oplen) ) // (Don't copy over more than len param allows) |
|
} |
|
else { |
|
range.push( op.derive(op.length) ) |
|
oplen = 0 |
|
} |
|
l += oplen |
|
} |
|
pos += op.input |
|
} |
|
return range |
|
} |
|
|
|
/** |
|
* Merge two changesets (that are based on the same state!) so that the resulting changseset |
|
* has the same effect as both orignal ones applied one after the other |
|
* |
|
* @param otherCs <Changeset> |
|
* @param left <boolean> Which op to choose if there's an insert tie (If you use this function in a distributed, synchronous environment, be sure to invert this param on the other site, otherwise it can be omitted safely)) |
|
* @returns <Changeset> |
|
*/ |
|
Changeset.prototype.merge = function(otherCs, left) { |
|
if(!(otherCs instanceof Changeset)) { |
|
throw new Error('Argument must be a #<Changeset>, but received '+otherCs.__proto__.constructor.name) |
|
} |
|
|
|
var newops = [] |
|
, addPtr1 = 0 |
|
, remPtr1 = 0 |
|
, addPtr2 = 0 |
|
, remPtr2 = 0 |
|
, newaddendum = '' |
|
, newremovendum = '' |
|
|
|
zip(this, otherCs, function(op1, op2) { |
|
// console.log(newops) |
|
// console.log(op1, op2) |
|
|
|
if(op1 && !op1.input && (!op2 || op2.input || left)) { // if it's a tie -- "left" breaks it. |
|
newops.push(op1.merge().clone()) |
|
newaddendum += this.addendum.substr(addPtr1, op1.length) // overtake added chars |
|
addPtr1 += op1.length |
|
op1.length = 0 // don't gimme that one again. |
|
return |
|
} |
|
|
|
if(op2 && !op2.input && (!op1 || op1.input || !left)) {// if it's a tie -- "left" breaks it. |
|
newops.push(op2.merge().clone()) |
|
newaddendum += otherCs.addendum.substr(addPtr2, op2.length) // overtake added chars |
|
addPtr2 += op2.length |
|
op2.length = 0 // don't gimme that one again. |
|
return |
|
} |
|
|
|
// XXX Move addendum and removendum stuff to indiv. ops |
|
// XXX Move everything below this to op1.merge(op2) |
|
|
|
if(op2 && !op2.output) { |
|
newops.push(op2.merge(op1).clone()) |
|
newremovendum += otherCs.removendum.substr(remPtr2, op2.length) // overtake removed chars |
|
remPtr2 += op2.length |
|
if(op1) op1.length = 0 // don't gimme these again. |
|
op2.length = 0 |
|
return |
|
} |
|
|
|
if(op1 && !op1.output) { |
|
newops.push(op1.merge(op2).clone()) |
|
newremovendum += this.removendum.substr(remPtr1, op1.length) // overtake removed chars |
|
remPtr1 += op1.length |
|
op1.length = 0 // don't gimme that one again. |
|
if(op2) op2.length = 0 |
|
return |
|
} |
|
|
|
if((op1 && op1.input == op1.output)) { |
|
newops.push(op1.merge(op2).clone()) |
|
op1.length = 0 // don't gimme these again. |
|
if(op2) op2.length = 0 |
|
return |
|
} |
|
|
|
console.log('oops', arguments) |
|
throw new Error('oops. This case hasn\'t been considered by the developer (error code: PBCAC)') |
|
}.bind(this)) |
|
|
|
var newCs = new Changeset(newops) |
|
newCs.addendum = newaddendum |
|
newCs.removendum = newremovendum |
|
|
|
return newCs |
|
} |
|
|
|
/** |
|
* A private and quite handy function that slices ops into equally long pieces and applies them on a mapping function |
|
* that can determine the iteration steps by setting op.length to 0 on an op (equals using .next() in a usual iterator) |
|
*/ |
|
function zip(cs1, cs2, func) { |
|
var opstack1 = cs1.map(function(op) {return op.clone()}) // copy ops |
|
, opstack2 = cs2.map(function(op) {return op.clone()}) |
|
|
|
var op2, op1 |
|
while(opstack1.length || opstack2.length) {// iterate through all outstanding ops of this cs |
|
op1 = opstack1[0]? opstack1[0].clone() : null |
|
op2 = opstack2[0]? opstack2[0].clone() : null |
|
|
|
if(op1) { |
|
if(op2) op1 = op1.derive(Math.min(op1.length, op2.length)) // slice 'em into equally long pieces |
|
if(opstack1[0].length > op1.length) opstack1[0] = opstack1[0].derive(opstack1[0].length-op1.length) |
|
else opstack1.shift() |
|
} |
|
|
|
if(op2) { |
|
if(op1) op2 = op2.derive(Math.min(op1.length, op2.length)) // slice 'em into equally long pieces |
|
if(opstack2[0].length > op2.length) opstack2[0] = opstack2[0].derive(opstack2[0].length-op2.length) |
|
else opstack2.shift() |
|
} |
|
|
|
func(op1, op2) |
|
|
|
if(op1 && op1.length) opstack1.unshift(op1) |
|
if(op2 && op2.length) opstack2.unshift(op2) |
|
} |
|
} |
|
|
|
/** |
|
* Inclusion Transformation (IT) or Forward Transformation |
|
* |
|
* transforms the operations of the current changeset against the |
|
* all operations in another changeset in such a way that the |
|
* effects of the latter are effectively included. |
|
* This is basically like a applying the other cs on this one. |
|
* |
|
* @param otherCs <Changeset> |
|
* @param left <boolean> Which op to choose if there's an insert tie (If you use this function in a distributed, synchronous environment, be sure to invert this param on the other site, otherwise it can be omitted safely) |
|
* |
|
* @returns <Changeset> |
|
*/ |
|
Changeset.prototype.transformAgainst = function(otherCs, left) { |
|
if(!(otherCs instanceof Changeset)) { |
|
throw new Error('Argument to Changeset#transformAgainst must be a #<Changeset>, but received '+otherCs.__proto__.constructor.name) |
|
} |
|
|
|
if(this.inputLength != otherCs.inputLength) { |
|
throw new Error('Can\'t transform changesets with differing inputLength: '+this.inputLength+' and '+otherCs.inputLength) |
|
} |
|
|
|
var transformation = new ChangesetTransform(this, [new Retain(Infinity)]) |
|
otherCs.forEach(function(op) { |
|
var nextOp = this.subrange(transformation.pos, Infinity)[0] // next op of this cs |
|
if(nextOp && !nextOp.input && !op.input && left) { // two inserts tied; left breaks it |
|
transformation.writeOutput(transformation.readInput(nextOp.length)) |
|
} |
|
op.apply(transformation) |
|
}.bind(this)) |
|
|
|
return transformation.result() |
|
} |
|
|
|
/** |
|
* Exclusion Transformation (ET) or Backwards Transformation |
|
* |
|
* transforms all operations in the current changeset against the operations |
|
* in another changeset in such a way that the impact of the latter are effectively excluded |
|
* |
|
* @param changeset <Changeset> the changeset to substract from this one |
|
* @param left <boolean> Which op to choose if there's an insert tie (If you use this function in a distributed, synchronous environment, be sure to invert this param on the other site, otherwise it can be omitted safely) |
|
* @returns <Changeset> |
|
*/ |
|
Changeset.prototype.substract = function(changeset, left) { |
|
// The current operations assume that the changes in |
|
// `changeset` happened before, so for each of those ops |
|
// we create an operation that undoes its effect and |
|
// transform all our operations on top of the inverse changes |
|
return this.transformAgainst(changeset.invert(), left) |
|
} |
|
|
|
/** |
|
* Returns the inverse Changeset of the current one |
|
* |
|
* Changeset.invert().apply(Changeset.apply(document)) == document |
|
*/ |
|
Changeset.prototype.invert = function() { |
|
// invert all ops |
|
var newCs = new Changeset(this.map(function(op) { |
|
return op.invert() |
|
})) |
|
|
|
// removendum becomes addendum and vice versa |
|
newCs.addendum = this.removendum |
|
newCs.removendum = this.addendum |
|
|
|
return newCs |
|
} |
|
|
|
/** |
|
* Applies this changeset on a text |
|
*/ |
|
Changeset.prototype.apply = function(input) { |
|
// pre-requisites |
|
if(input.length != this.inputLength) throw new Error('Input length doesn\'t match expected length. expected: '+this.inputLength+'; actual: '+input.length) |
|
|
|
var operation = new TextTransform(input, this.addendum, this.removendum) |
|
|
|
this.forEach(function(op) { |
|
// each Operation has access to all pointers as well as the input, addendum and removendum (the latter are immutable) |
|
op.apply(operation) |
|
}.bind(this)) |
|
|
|
return operation.result() |
|
} |
|
|
|
/** |
|
* Returns an array of strings describing this changeset's operations |
|
*/ |
|
Changeset.prototype.inspect = function() { |
|
var j = 0 |
|
return this.map(function(op) { |
|
var string = '' |
|
|
|
if(!op.input) { // if Insert |
|
string = this.addendum.substr(j,op.length) |
|
j += op.length |
|
return string |
|
} |
|
|
|
for(var i=0; i<op.length; i++) string += op.symbol |
|
return string |
|
}.bind(this)).join('') |
|
} |
|
|
|
/** |
|
* Serializes the given changeset in order to return a (hopefully) more compact representation |
|
* than json that can be sent through a network or stored in a database |
|
* |
|
* Numbers are converted to the base 36, unsafe chars in the text are urlencoded |
|
* |
|
* @param cs <Changeset> The changeset to be serialized |
|
* @returns <String> The serialized changeset |
|
*/ |
|
Changeset.prototype.pack = function() { |
|
var packed = this.map(function(op) { |
|
return op.pack() |
|
}).join('') |
|
|
|
var addendum = this.addendum.replace(/%/g, '%25').replace(/\|/g, '%7C') |
|
, removendum = this.removendum.replace(/%/g, '%25').replace(/\|/g, '%7C') |
|
return packed+'|'+addendum+'|'+removendum |
|
} |
|
Changeset.prototype.toString = function() { |
|
return this.pack() |
|
} |
|
|
|
/** |
|
* Unserializes the output of cs.text.Changeset#toString() |
|
* |
|
* @param packed <String> The serialized changeset |
|
* @param <cs.Changeset> |
|
*/ |
|
Changeset.unpack = function(packed) { |
|
if(packed == '') throw new Error('Cannot unpack from empty string') |
|
var components = packed.split('|') |
|
, opstring = components[0] |
|
, addendum = components[1].replace(/%7c/gi, '|').replace(/%25/g, '%') |
|
, removendum = components[2].replace(/%7c/gi, '|').replace(/%25/g, '%') |
|
|
|
var matches = opstring.match(/[=+-]([^=+-])+/g) |
|
if(!matches) throw new Error('Cannot unpack invalidly serialized op string') |
|
|
|
var ops = [] |
|
matches.forEach(function(s) { |
|
var symbol = s.substr(0,1) |
|
, data = s.substr(1) |
|
if(Skip.prototype.symbol == symbol) return ops.push(Skip.unpack(data)) |
|
if(Insert.prototype.symbol == symbol) return ops.push(Insert.unpack(data)) |
|
if(Retain.prototype.symbol == symbol) return ops.push(Retain.unpack(data)) |
|
throw new Error('Invalid changeset representation passed to Changeset.unpack') |
|
}) |
|
|
|
var cs = new Changeset(ops) |
|
cs.addendum = addendum |
|
cs.removendum = removendum |
|
|
|
return cs |
|
} |
|
|
|
Changeset.create = function() { |
|
return new Builder |
|
} |
|
|
|
/** |
|
* Returns a Changeset containing the operations needed to transform text1 into text2 |
|
* |
|
* @param text1 <String> |
|
* @param text2 <String> |
|
*/ |
|
Changeset.fromDiff = function(diff) { |
|
/** |
|
* The data structure representing a diff is an array of tuples: |
|
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']] |
|
* which means: delete 'Hello', add 'Goodbye' and keep ' world.' |
|
*/ |
|
var DIFF_DELETE = -1; |
|
var DIFF_INSERT = 1; |
|
var DIFF_EQUAL = 0; |
|
|
|
var ops = [] |
|
, removendum = '' |
|
, addendum = '' |
|
|
|
diff.forEach(function(d) { |
|
if (DIFF_DELETE == d[0]) { |
|
ops.push(new Skip(d[1].length)) |
|
removendum += d[1] |
|
} |
|
|
|
if (DIFF_INSERT == d[0]) { |
|
ops.push(new Insert(d[1].length)) |
|
addendum += d[1] |
|
} |
|
|
|
if(DIFF_EQUAL == d[0]) { |
|
ops.push(new Retain(d[1].length)) |
|
} |
|
}) |
|
|
|
var cs = new Changeset(ops) |
|
cs.addendum = addendum |
|
cs.removendum = removendum |
|
return cs |
|
} |
|
},{"./Builder":2,"./ChangesetTransform":4,"./TextTransform":6,"./operations/Insert":8,"./operations/Retain":9,"./operations/Skip":10}],4:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational ChangesetTransform (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Retain = require('./operations/Retain') |
|
, Skip = require('./operations/Skip') |
|
, Insert = require('./operations/Insert') |
|
, Changeset = require('./Changeset') |
|
|
|
|
|
function ChangesetTransform(inputCs, addendum) { |
|
this.output = [] |
|
this.addendum = addendum |
|
this.newRemovendum = '' |
|
this.newAddendum = '' |
|
|
|
this.cs = inputCs |
|
this.pos = 0 |
|
this.addendumPointer = 0 |
|
this.removendumPointer = 0 |
|
} |
|
module.exports = ChangesetTransform |
|
|
|
ChangesetTransform.prototype.readInput = function (len) { |
|
var ret = this.cs.subrange(this.pos, len) |
|
this.pos += len |
|
return ret |
|
} |
|
|
|
ChangesetTransform.prototype.readAddendum = function (len) { |
|
//return [new Retain(len)] |
|
var ret = this.subrange(this.addendum, this.addendumPointer, len) |
|
this.addendumPointer += len |
|
return ret |
|
} |
|
|
|
ChangesetTransform.prototype.writeRemovendum = function (range) { |
|
range |
|
.filter(function(op) {return !op.output}) |
|
.forEach(function(op) { |
|
this.removendumPointer += op.length |
|
}.bind(this)) |
|
} |
|
|
|
ChangesetTransform.prototype.writeOutput = function (range) { |
|
this.output = this.output.concat(range) |
|
range |
|
.filter(function(op) {return !op.output}) |
|
.forEach(function(op) { |
|
this.newRemovendum += this.cs.removendum.substr(this.removendumPointer, op.length) |
|
this.removendumPointer += op.length |
|
}.bind(this)) |
|
} |
|
|
|
ChangesetTransform.prototype.subrange = function (range, start, len) { |
|
if(len) return this.cs.subrange.call(range, start, len) |
|
else return range.filter(function(op){ return !op.input}) |
|
} |
|
|
|
ChangesetTransform.prototype.result = function() { |
|
this.writeOutput(this.readInput(Infinity)) |
|
var newCs = new Changeset(this.output) |
|
newCs.addendum = this.cs.addendum |
|
newCs.removendum = this.newRemovendum |
|
return newCs |
|
} |
|
},{"./Changeset":3,"./operations/Insert":8,"./operations/Retain":9,"./operations/Skip":10}],5:[function(require,module,exports){ |
|
function Operator() { |
|
} |
|
|
|
module.exports = Operator |
|
|
|
Operator.prototype.clone = function() { |
|
return this.derive(this.length) |
|
} |
|
|
|
Operator.prototype.derive = function(len) { |
|
return new (this.constructor)(len) |
|
} |
|
|
|
Operator.prototype.pack = function() { |
|
return this.symbol + (this.length).toString(36) |
|
} |
|
},{}],6:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational Apply (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Retain = require('./operations/Retain') |
|
, Skip = require('./operations/Skip') |
|
, Insert = require('./operations/Insert') |
|
, Insert = require('./Changeset') |
|
|
|
|
|
function TextTransform(input, addendum, removendum) { |
|
this.output = '' |
|
|
|
this.input = input |
|
this.addendum = addendum |
|
this.removendum = removendum |
|
this.pos = 0 |
|
this.addPos = 0 |
|
this.remPos = 0 |
|
} |
|
module.exports = TextTransform |
|
|
|
TextTransform.prototype.readInput = function (len) { |
|
var ret = this.input.substr(this.pos, len) |
|
this.pos += len |
|
return ret |
|
} |
|
|
|
TextTransform.prototype.readAddendum = function (len) { |
|
var ret = this.addendum.substr(this.addPos, len) |
|
this.addPos += len |
|
return ret |
|
} |
|
|
|
TextTransform.prototype.writeRemovendum = function (range) { |
|
//var expected = this.removendum.substr(this.remPos, range.length) |
|
//if(range != expected) throw new Error('Removed chars don\'t match removendum. expected: '+expected+'; actual: '+range) |
|
this.remPos += range.length |
|
} |
|
|
|
TextTransform.prototype.writeOutput = function (range) { |
|
this.output += range |
|
} |
|
|
|
TextTransform.prototype.subrange = function (range, start, len) { |
|
return range.substr(start, len) |
|
} |
|
|
|
TextTransform.prototype.result = function() { |
|
this.writeOutput(this.readInput(Infinity)) |
|
return this.output |
|
} |
|
},{"./Changeset":3,"./operations/Insert":8,"./operations/Retain":9,"./operations/Skip":10}],7:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational transformation (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Changeset = require('./Changeset') |
|
, Retain = require('./operations/Retain') |
|
, Skip = require('./operations/Skip') |
|
, Insert = require('./operations/Insert') |
|
|
|
exports.Operator = require('./Operator') |
|
exports.Changeset = Changeset |
|
exports.Insert = Insert |
|
exports.Retain = Retain |
|
exports.Skip = Skip |
|
|
|
if('undefined' != typeof window) window.changesets = exports |
|
|
|
/** |
|
* Serializes the given changeset in order to return a (hopefully) more compact representation |
|
* that can be sent through a network or stored in a database |
|
* @alias cs.text.Changeset#pack |
|
*/ |
|
exports.pack = function(cs) { |
|
return cs.pack() |
|
} |
|
|
|
/** |
|
* Unserializes the output of cs.text.pack |
|
* @alias cs.text.Changeset.unpack |
|
*/ |
|
exports.unpack = function(packed) { |
|
return Changeset.unpack(packed) |
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
* shareJS ot type API sepc support |
|
*/ |
|
|
|
exports.name = 'changesets' |
|
exports.url = 'https://github.com/marcelklehr/changesets' |
|
|
|
/** |
|
* create([initialText]) |
|
* |
|
* creates a snapshot (optionally with supplied intial text) |
|
*/ |
|
exports.create = function(initText) { |
|
return initText || '' |
|
} |
|
|
|
/** |
|
* Apply a changeset on a snapshot creating a new one |
|
* |
|
* The old snapshot object mustn't be used after calling apply on it |
|
* returns the resulting |
|
*/ |
|
exports.apply = function(snapshot, op) { |
|
op = exports.unpack(op) |
|
return op.apply(snapshot) |
|
} |
|
|
|
/** |
|
* Transform changeset1 against changeset2 |
|
*/ |
|
exports.transform = function (op1, op2, side) { |
|
op1 = exports.unpack(op1) |
|
op2 = exports.unpack(op2) |
|
return exports.pack(op1.transformAgainst(op2, ('left'==side))) |
|
} |
|
|
|
/** |
|
* Merge two changesets into one |
|
*/ |
|
exports.compose = function (op1, op2) { |
|
op1 = exports.unpack(op1) |
|
op2 = exports.unpack(op2) |
|
return exports.pack(op1.merge(op2)) |
|
} |
|
|
|
/** |
|
* Invert a changeset |
|
*/ |
|
exports.invert = function(op) { |
|
return op.invert() |
|
} |
|
},{"./Changeset":3,"./Operator":5,"./operations/Insert":8,"./operations/Retain":9,"./operations/Skip":10}],8:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational transformation (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Operator = require('../Operator') |
|
|
|
/** |
|
* Insert Operator |
|
* Defined by: |
|
* - length |
|
* - input=0 |
|
* - output=length |
|
* |
|
* @param length <Number> How many chars to be inserted |
|
*/ |
|
function Insert(length) { |
|
this.length = length |
|
this.input = 0 |
|
this.output = length |
|
} |
|
|
|
// True inheritance |
|
Insert.prototype = Object.create(Operator.prototype, { |
|
constructor: { |
|
value: Insert, |
|
enumerable: false, |
|
writable: true, |
|
configurable: true |
|
} |
|
}); |
|
module.exports = Insert |
|
Insert.prototype.symbol = '+' |
|
|
|
var Skip = require('./Skip') |
|
, Retain = require('./Retain') |
|
|
|
Insert.prototype.apply = function(t) { |
|
t.writeOutput(t.readAddendum(this.output)) |
|
} |
|
|
|
Insert.prototype.merge = function() { |
|
return this |
|
} |
|
|
|
Insert.prototype.invert = function() { |
|
return new Skip(this.length) |
|
} |
|
|
|
Insert.unpack = function(data) { |
|
return new Insert(parseInt(data, 36)) |
|
} |
|
},{"../Operator":5,"./Retain":9,"./Skip":10}],9:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational transformation (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Operator = require('../Operator') |
|
|
|
/** |
|
* Retain Operator |
|
* Defined by: |
|
* - length |
|
* - input=output=length |
|
* |
|
* @param length <Number> How many chars to retain |
|
*/ |
|
function Retain(length) { |
|
this.length = length |
|
this.input = length |
|
this.output = length |
|
} |
|
|
|
// True inheritance |
|
Retain.prototype = Object.create(Operator.prototype, { |
|
constructor: { |
|
value: Retain, |
|
enumerable: false, |
|
writable: true, |
|
configurable: true |
|
} |
|
}); |
|
module.exports = Retain |
|
Retain.prototype.symbol = '=' |
|
|
|
Retain.prototype.apply = function(t) { |
|
t.writeOutput(t.readInput(this.input)) |
|
} |
|
|
|
Retain.prototype.invert = function() { |
|
return this |
|
} |
|
|
|
Retain.prototype.merge = function(op2) { |
|
return this |
|
} |
|
|
|
Retain.unpack = function(data) { |
|
return new Retain(parseInt(data, 36)) |
|
} |
|
},{"../Operator":5}],10:[function(require,module,exports){ |
|
/*! |
|
* changesets |
|
* A Changeset library incorporating operational transformation (OT) |
|
* Copyright 2012 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
|
|
var Operator = require('../Operator') |
|
|
|
/** |
|
* Skip Operator |
|
* Defined by: |
|
* - length |
|
* - input=length |
|
* - output=0 |
|
* |
|
* @param length <Number> How many chars to be Skip |
|
*/ |
|
function Skip(length) { |
|
this.length = length |
|
this.input = length |
|
this.output = 0 |
|
} |
|
|
|
// True inheritance |
|
Skip.prototype = Object.create(Operator.prototype, { |
|
constructor: { |
|
value: Skip, |
|
enumerable: false, |
|
writable: true, |
|
configurable: true |
|
} |
|
}); |
|
module.exports = Skip |
|
Skip.prototype.symbol = '-' |
|
|
|
var Insert = require('./Insert') |
|
, Retain = require('./Retain') |
|
, Changeset = require('../Changeset') |
|
|
|
Skip.prototype.apply = function(t) { |
|
var input = t.readInput(this.input) |
|
t.writeRemovendum(input) |
|
t.writeOutput(t.subrange(input, 0, this.output)) // retain Inserts in my range |
|
} |
|
|
|
Skip.prototype.merge = function(op2) { |
|
return this |
|
} |
|
|
|
Skip.prototype.invert = function() { |
|
return new Insert(this.length) |
|
} |
|
|
|
Skip.unpack = function(data) { |
|
return new Skip(parseInt(data, 36)) |
|
} |
|
},{"../Changeset":3,"../Operator":5,"./Insert":8,"./Retain":9}],11:[function(require,module,exports){ |
|
/** |
|
* Diff Match and Patch |
|
* |
|
* Copyright 2006 Google Inc. |
|
* http://code.google.com/p/google-diff-match-patch/ |
|
* |
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
* you may not use this file except in compliance with the License. |
|
* You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
/** |
|
* @fileoverview Computes the difference between two texts to create a patch. |
|
* Applies the patch onto another text, allowing for errors. |
|
* @author [email protected] (Neil Fraser) |
|
*/ |
|
|
|
/** |
|
* Class containing the diff, match and patch methods. |
|
* @constructor |
|
*/ |
|
function diff_match_patch() { |
|
|
|
// Defaults. |
|
// Redefine these in your program to override the defaults. |
|
|
|
// Number of seconds to map a diff before giving up (0 for infinity). |
|
this.Diff_Timeout = 1.0; |
|
// Cost of an empty edit operation in terms of edit characters. |
|
this.Diff_EditCost = 4; |
|
// The size beyond which the double-ended diff activates. |
|
// Double-ending is twice as fast, but less accurate. |
|
this.Diff_DualThreshold = 32; |
|
// At what point is no match declared (0.0 = perfection, 1.0 = very loose). |
|
this.Match_Threshold = 0.5; |
|
// How far to search for a match (0 = exact location, 1000+ = broad match). |
|
// A match this many characters away from the expected location will add |
|
// 1.0 to the score (0.0 is a perfect match). |
|
this.Match_Distance = 1000; |
|
// When deleting a large block of text (over ~64 characters), how close does |
|
// the contents have to match the expected contents. (0.0 = perfection, |
|
// 1.0 = very loose). Note that Match_Threshold controls how closely the |
|
// end points of a delete need to match. |
|
this.Patch_DeleteThreshold = 0.5; |
|
// Chunk size for context length. |
|
this.Patch_Margin = 4; |
|
|
|
/** |
|
* Compute the number of bits in an int. |
|
* The normal answer for JavaScript is 32. |
|
* @return {number} Max bits |
|
*/ |
|
function getMaxBits() { |
|
var maxbits = 0; |
|
var oldi = 1; |
|
var newi = 2; |
|
while (oldi != newi) { |
|
maxbits++; |
|
oldi = newi; |
|
newi = newi << 1; |
|
} |
|
return maxbits; |
|
} |
|
// How many bits in a number? |
|
this.Match_MaxBits = getMaxBits(); |
|
} |
|
|
|
|
|
// DIFF FUNCTIONS |
|
|
|
|
|
/** |
|
* The data structure representing a diff is an array of tuples: |
|
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']] |
|
* which means: delete 'Hello', add 'Goodbye' and keep ' world.' |
|
*/ |
|
var DIFF_DELETE = -1; |
|
var DIFF_INSERT = 1; |
|
var DIFF_EQUAL = 0; |
|
|
|
|
|
/** |
|
* Find the differences between two texts. Simplifies the problem by stripping |
|
* any common prefix or suffix off the texts before diffing. |
|
* @param {string} text1 Old string to be diffed. |
|
* @param {string} text2 New string to be diffed. |
|
* @param {boolean} opt_checklines Optional speedup flag. If present and false, |
|
* then don't run a line-level diff first to identify the changed areas. |
|
* Defaults to true, which does a faster, slightly less optimal diff |
|
* @return {Array.<Array.<number|string>>} Array of diff tuples. |
|
*/ |
|
diff_match_patch.prototype.diff_main = function(text1, text2, opt_checklines) { |
|
// Check for null inputs. |
|
if (text1 == null || text2 == null) { |
|
throw new Error('Null input. (diff_main)'); |
|
} |
|
|
|
// Check for equality (speedup). |
|
if (text1 == text2) { |
|
return [[DIFF_EQUAL, text1]]; |
|
} |
|
|
|
if (typeof opt_checklines == 'undefined') { |
|
opt_checklines = true; |
|
} |
|
var checklines = opt_checklines; |
|
|
|
// Trim off common prefix (speedup). |
|
var commonlength = this.diff_commonPrefix(text1, text2); |
|
var commonprefix = text1.substring(0, commonlength); |
|
text1 = text1.substring(commonlength); |
|
text2 = text2.substring(commonlength); |
|
|
|
// Trim off common suffix (speedup). |
|
commonlength = this.diff_commonSuffix(text1, text2); |
|
var commonsuffix = text1.substring(text1.length - commonlength); |
|
text1 = text1.substring(0, text1.length - commonlength); |
|
text2 = text2.substring(0, text2.length - commonlength); |
|
|
|
// Compute the diff on the middle block. |
|
var diffs = this.diff_compute(text1, text2, checklines); |
|
|
|
// Restore the prefix and suffix. |
|
if (commonprefix) { |
|
diffs.unshift([DIFF_EQUAL, commonprefix]); |
|
} |
|
if (commonsuffix) { |
|
diffs.push([DIFF_EQUAL, commonsuffix]); |
|
} |
|
this.diff_cleanupMerge(diffs); |
|
return diffs; |
|
}; |
|
|
|
|
|
/** |
|
* Find the differences between two texts. Assumes that the texts do not |
|
* have any common prefix or suffix. |
|
* @param {string} text1 Old string to be diffed. |
|
* @param {string} text2 New string to be diffed. |
|
* @param {boolean} checklines Speedup flag. If false, then don't run a |
|
* line-level diff first to identify the changed areas. |
|
* If true, then run a faster, slightly less optimal diff |
|
* @return {Array.<Array.<number|string>>} Array of diff tuples. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_compute = function(text1, text2, checklines) { |
|
var diffs; |
|
|
|
if (!text1) { |
|
// Just add some text (speedup). |
|
return [[DIFF_INSERT, text2]]; |
|
} |
|
|
|
if (!text2) { |
|
// Just delete some text (speedup). |
|
return [[DIFF_DELETE, text1]]; |
|
} |
|
|
|
var longtext = text1.length > text2.length ? text1 : text2; |
|
var shorttext = text1.length > text2.length ? text2 : text1; |
|
var i = longtext.indexOf(shorttext); |
|
if (i != -1) { |
|
// Shorter text is inside the longer text (speedup). |
|
diffs = [[DIFF_INSERT, longtext.substring(0, i)], |
|
[DIFF_EQUAL, shorttext], |
|
[DIFF_INSERT, longtext.substring(i + shorttext.length)]]; |
|
// Swap insertions for deletions if diff is reversed. |
|
if (text1.length > text2.length) { |
|
diffs[0][0] = diffs[2][0] = DIFF_DELETE; |
|
} |
|
return diffs; |
|
} |
|
longtext = shorttext = null; // Garbage collect. |
|
|
|
// Check to see if the problem can be split in two. |
|
var hm = this.diff_halfMatch(text1, text2); |
|
if (hm) { |
|
// A half-match was found, sort out the return data. |
|
var text1_a = hm[0]; |
|
var text1_b = hm[1]; |
|
var text2_a = hm[2]; |
|
var text2_b = hm[3]; |
|
var mid_common = hm[4]; |
|
// Send both pairs off for separate processing. |
|
var diffs_a = this.diff_main(text1_a, text2_a, checklines); |
|
var diffs_b = this.diff_main(text1_b, text2_b, checklines); |
|
// Merge the results. |
|
return diffs_a.concat([[DIFF_EQUAL, mid_common]], diffs_b); |
|
} |
|
|
|
// Perform a real diff. |
|
if (checklines && (text1.length < 100 || text2.length < 100)) { |
|
// Too trivial for the overhead. |
|
checklines = false; |
|
} |
|
var linearray; |
|
if (checklines) { |
|
// Scan the text on a line-by-line basis first. |
|
var a = this.diff_linesToChars(text1, text2); |
|
text1 = a[0]; |
|
text2 = a[1]; |
|
linearray = a[2]; |
|
} |
|
diffs = this.diff_map(text1, text2); |
|
if (!diffs) { |
|
// No acceptable result. |
|
diffs = [[DIFF_DELETE, text1], [DIFF_INSERT, text2]]; |
|
} |
|
if (checklines) { |
|
// Convert the diff back to original text. |
|
this.diff_charsToLines(diffs, linearray); |
|
// Eliminate freak matches (e.g. blank lines) |
|
this.diff_cleanupSemantic(diffs); |
|
|
|
// Rediff any replacement blocks, this time character-by-character. |
|
// Add a dummy entry at the end. |
|
diffs.push([DIFF_EQUAL, '']); |
|
var pointer = 0; |
|
var count_delete = 0; |
|
var count_insert = 0; |
|
var text_delete = ''; |
|
var text_insert = ''; |
|
while (pointer < diffs.length) { |
|
switch (diffs[pointer][0]) { |
|
case DIFF_INSERT: |
|
count_insert++; |
|
text_insert += diffs[pointer][1]; |
|
break; |
|
case DIFF_DELETE: |
|
count_delete++; |
|
text_delete += diffs[pointer][1]; |
|
break; |
|
case DIFF_EQUAL: |
|
// Upon reaching an equality, check for prior redundancies. |
|
if (count_delete >= 1 && count_insert >= 1) { |
|
// Delete the offending records and add the merged ones. |
|
var a = this.diff_main(text_delete, text_insert, false); |
|
diffs.splice(pointer - count_delete - count_insert, |
|
count_delete + count_insert); |
|
pointer = pointer - count_delete - count_insert; |
|
for (var j = a.length - 1; j >= 0; j--) { |
|
diffs.splice(pointer, 0, a[j]); |
|
} |
|
pointer = pointer + a.length; |
|
} |
|
count_insert = 0; |
|
count_delete = 0; |
|
text_delete = ''; |
|
text_insert = ''; |
|
break; |
|
} |
|
pointer++; |
|
} |
|
diffs.pop(); // Remove the dummy entry at the end. |
|
} |
|
return diffs; |
|
}; |
|
|
|
|
|
/** |
|
* Split two texts into an array of strings. Reduce the texts to a string of |
|
* hashes where each Unicode character represents one line. |
|
* @param {string} text1 First string. |
|
* @param {string} text2 Second string. |
|
* @return {Array.<string|Array.<string>>} Three element Array, containing the |
|
* encoded text1, the encoded text2 and the array of unique strings. The |
|
* zeroth element of the array of unique strings is intentionally blank. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_linesToChars = function(text1, text2) { |
|
var lineArray = []; // e.g. lineArray[4] == 'Hello\n' |
|
var lineHash = {}; // e.g. lineHash['Hello\n'] == 4 |
|
|
|
// '\x00' is a valid character, but various debuggers don't like it. |
|
// So we'll insert a junk entry to avoid generating a null character. |
|
lineArray[0] = ''; |
|
|
|
/** |
|
* Split a text into an array of strings. Reduce the texts to a string of |
|
* hashes where each Unicode character represents one line. |
|
* Modifies linearray and linehash through being a closure. |
|
* @param {string} text String to encode. |
|
* @return {string} Encoded string. |
|
* @private |
|
*/ |
|
function diff_linesToCharsMunge(text) { |
|
var chars = ''; |
|
// Walk the text, pulling out a substring for each line. |
|
// text.split('\n') would would temporarily double our memory footprint. |
|
// Modifying text would create many large strings to garbage collect. |
|
var lineStart = 0; |
|
var lineEnd = -1; |
|
// Keeping our own length variable is faster than looking it up. |
|
var lineArrayLength = lineArray.length; |
|
while (lineEnd < text.length - 1) { |
|
lineEnd = text.indexOf('\n', lineStart); |
|
if (lineEnd == -1) { |
|
lineEnd = text.length - 1; |
|
} |
|
var line = text.substring(lineStart, lineEnd + 1); |
|
lineStart = lineEnd + 1; |
|
|
|
if (lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) : |
|
(lineHash[line] !== undefined)) { |
|
chars += String.fromCharCode(lineHash[line]); |
|
} else { |
|
chars += String.fromCharCode(lineArrayLength); |
|
lineHash[line] = lineArrayLength; |
|
lineArray[lineArrayLength++] = line; |
|
} |
|
} |
|
return chars; |
|
} |
|
|
|
var chars1 = diff_linesToCharsMunge(text1); |
|
var chars2 = diff_linesToCharsMunge(text2); |
|
return [chars1, chars2, lineArray]; |
|
}; |
|
|
|
|
|
/** |
|
* Rehydrate the text in a diff from a string of line hashes to real lines of |
|
* text. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @param {Array.<string>} lineArray Array of unique strings. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_charsToLines = function(diffs, lineArray) { |
|
for (var x = 0; x < diffs.length; x++) { |
|
var chars = diffs[x][1]; |
|
var text = []; |
|
for (var y = 0; y < chars.length; y++) { |
|
text[y] = lineArray[chars.charCodeAt(y)]; |
|
} |
|
diffs[x][1] = text.join(''); |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* Explore the intersection points between the two texts. |
|
* @param {string} text1 Old string to be diffed. |
|
* @param {string} text2 New string to be diffed. |
|
* @return {?Array.<Array.<number|string>>} Array of diff tuples or null if no |
|
* diff available. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_map = function(text1, text2) { |
|
// Don't run for too long. |
|
var ms_end = (new Date()).getTime() + this.Diff_Timeout * 1000; |
|
// Cache the text lengths to prevent multiple calls. |
|
var text1_length = text1.length; |
|
var text2_length = text2.length; |
|
var max_d = text1_length + text2_length - 1; |
|
var doubleEnd = this.Diff_DualThreshold * 2 < max_d; |
|
// JavaScript efficiency note: (x << 32) + y doesn't work since numbers are |
|
// only 32 bit. Use x + ',' + y to create a hash instead. |
|
var v_map1 = []; |
|
var v_map2 = []; |
|
var v1 = {}; |
|
var v2 = {}; |
|
v1[1] = 0; |
|
v2[1] = 0; |
|
var x, y; |
|
var footstep; // Used to track overlapping paths. |
|
var footsteps = {}; |
|
var done = false; |
|
// If the total number of characters is odd, then the front path will collide |
|
// with the reverse path. |
|
var front = (text1_length + text2_length) % 2; |
|
for (var d = 0; d < max_d; d++) { |
|
// Bail out if timeout reached. |
|
if (this.Diff_Timeout > 0 && (new Date()).getTime() > ms_end) { |
|
return null; |
|
} |
|
|
|
// Walk the front path one step. |
|
v_map1[d] = {}; |
|
for (var k = -d; k <= d; k += 2) { |
|
if (k == -d || k != d && v1[k - 1] < v1[k + 1]) { |
|
x = v1[k + 1]; |
|
} else { |
|
x = v1[k - 1] + 1; |
|
} |
|
y = x - k; |
|
if (doubleEnd) { |
|
footstep = x + ',' + y; |
|
if (front && footsteps[footstep] !== undefined) { |
|
done = true; |
|
} |
|
if (!front) { |
|
footsteps[footstep] = d; |
|
} |
|
} |
|
while (!done && x < text1_length && y < text2_length && |
|
text1.charAt(x) == text2.charAt(y)) { |
|
x++; |
|
y++; |
|
if (doubleEnd) { |
|
footstep = x + ',' + y; |
|
if (front && footsteps[footstep] !== undefined) { |
|
done = true; |
|
} |
|
if (!front) { |
|
footsteps[footstep] = d; |
|
} |
|
} |
|
} |
|
v1[k] = x; |
|
v_map1[d][x + ',' + y] = true; |
|
if (x == text1_length && y == text2_length) { |
|
// Reached the end in single-path mode. |
|
return this.diff_path1(v_map1, text1, text2); |
|
} else if (done) { |
|
// Front path ran over reverse path. |
|
v_map2 = v_map2.slice(0, footsteps[footstep] + 1); |
|
var a = this.diff_path1(v_map1, text1.substring(0, x), |
|
text2.substring(0, y)); |
|
return a.concat(this.diff_path2(v_map2, text1.substring(x), |
|
text2.substring(y))); |
|
} |
|
} |
|
|
|
if (doubleEnd) { |
|
// Walk the reverse path one step. |
|
v_map2[d] = {}; |
|
for (var k = -d; k <= d; k += 2) { |
|
if (k == -d || k != d && v2[k - 1] < v2[k + 1]) { |
|
x = v2[k + 1]; |
|
} else { |
|
x = v2[k - 1] + 1; |
|
} |
|
y = x - k; |
|
footstep = (text1_length - x) + ',' + (text2_length - y); |
|
if (!front && footsteps[footstep] !== undefined) { |
|
done = true; |
|
} |
|
if (front) { |
|
footsteps[footstep] = d; |
|
} |
|
while (!done && x < text1_length && y < text2_length && |
|
text1.charAt(text1_length - x - 1) == |
|
text2.charAt(text2_length - y - 1)) { |
|
x++; |
|
y++; |
|
footstep = (text1_length - x) + ',' + (text2_length - y); |
|
if (!front && footsteps[footstep] !== undefined) { |
|
done = true; |
|
} |
|
if (front) { |
|
footsteps[footstep] = d; |
|
} |
|
} |
|
v2[k] = x; |
|
v_map2[d][x + ',' + y] = true; |
|
if (done) { |
|
// Reverse path ran over front path. |
|
v_map1 = v_map1.slice(0, footsteps[footstep] + 1); |
|
var a = this.diff_path1(v_map1, text1.substring(0, text1_length - x), |
|
text2.substring(0, text2_length - y)); |
|
return a.concat(this.diff_path2(v_map2, |
|
text1.substring(text1_length - x), |
|
text2.substring(text2_length - y))); |
|
} |
|
} |
|
} |
|
} |
|
// Number of diffs equals number of characters, no commonality at all. |
|
return null; |
|
}; |
|
|
|
|
|
/** |
|
* Work from the middle back to the start to determine the path. |
|
* @param {Array.<Object>} v_map Array of paths. |
|
* @param {string} text1 Old string fragment to be diffed. |
|
* @param {string} text2 New string fragment to be diffed. |
|
* @return {Array.<Array.<number|string>>} Array of diff tuples. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_path1 = function(v_map, text1, text2) { |
|
var path = []; |
|
var x = text1.length; |
|
var y = text2.length; |
|
/** @type {?number} */ |
|
var last_op = null; |
|
for (var d = v_map.length - 2; d >= 0; d--) { |
|
while (1) { |
|
if (v_map[d][(x - 1) + ',' + y] !== undefined) { |
|
x--; |
|
if (last_op === DIFF_DELETE) { |
|
path[0][1] = text1.charAt(x) + path[0][1]; |
|
} else { |
|
path.unshift([DIFF_DELETE, text1.charAt(x)]); |
|
} |
|
last_op = DIFF_DELETE; |
|
break; |
|
} else if (v_map[d][x + ',' + (y - 1)] !== undefined) { |
|
y--; |
|
if (last_op === DIFF_INSERT) { |
|
path[0][1] = text2.charAt(y) + path[0][1]; |
|
} else { |
|
path.unshift([DIFF_INSERT, text2.charAt(y)]); |
|
} |
|
last_op = DIFF_INSERT; |
|
break; |
|
} else { |
|
x--; |
|
y--; |
|
if (text1.charAt(x) != text2.charAt(y)) { |
|
throw new Error('No diagonal. Can\'t happen. (diff_path1)'); |
|
} |
|
if (last_op === DIFF_EQUAL) { |
|
path[0][1] = text1.charAt(x) + path[0][1]; |
|
} else { |
|
path.unshift([DIFF_EQUAL, text1.charAt(x)]); |
|
} |
|
last_op = DIFF_EQUAL; |
|
} |
|
} |
|
} |
|
return path; |
|
}; |
|
|
|
|
|
/** |
|
* Work from the middle back to the end to determine the path. |
|
* @param {Array.<Object>} v_map Array of paths. |
|
* @param {string} text1 Old string fragment to be diffed. |
|
* @param {string} text2 New string fragment to be diffed. |
|
* @return {Array.<Array.<number|string>>} Array of diff tuples. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.diff_path2 = function(v_map, text1, text2) { |
|
var path = []; |
|
var pathLength = 0; |
|
var x = text1.length; |
|
var y = text2.length; |
|
/** @type {?number} */ |
|
var last_op = null; |
|
for (var d = v_map.length - 2; d >= 0; d--) { |
|
while (1) { |
|
if (v_map[d][(x - 1) + ',' + y] !== undefined) { |
|
x--; |
|
if (last_op === DIFF_DELETE) { |
|
path[pathLength - 1][1] += text1.charAt(text1.length - x - 1); |
|
} else { |
|
path[pathLength++] = |
|
[DIFF_DELETE, text1.charAt(text1.length - x - 1)]; |
|
} |
|
last_op = DIFF_DELETE; |
|
break; |
|
} else if (v_map[d][x + ',' + (y - 1)] !== undefined) { |
|
y--; |
|
if (last_op === DIFF_INSERT) { |
|
path[pathLength - 1][1] += text2.charAt(text2.length - y - 1); |
|
} else { |
|
path[pathLength++] = |
|
[DIFF_INSERT, text2.charAt(text2.length - y - 1)]; |
|
} |
|
last_op = DIFF_INSERT; |
|
break; |
|
} else { |
|
x--; |
|
y--; |
|
if (text1.charAt(text1.length - x - 1) != |
|
text2.charAt(text2.length - y - 1)) { |
|
throw new Error('No diagonal. Can\'t happen. (diff_path2)'); |
|
} |
|
if (last_op === DIFF_EQUAL) { |
|
path[pathLength - 1][1] += text1.charAt(text1.length - x - 1); |
|
} else { |
|
path[pathLength++] = |
|
[DIFF_EQUAL, text1.charAt(text1.length - x - 1)]; |
|
} |
|
last_op = DIFF_EQUAL; |
|
} |
|
} |
|
} |
|
return path; |
|
}; |
|
|
|
|
|
/** |
|
* Determine the common prefix of two strings |
|
* @param {string} text1 First string. |
|
* @param {string} text2 Second string. |
|
* @return {number} The number of characters common to the start of each |
|
* string. |
|
*/ |
|
diff_match_patch.prototype.diff_commonPrefix = function(text1, text2) { |
|
// Quick check for common null cases. |
|
if (!text1 || !text2 || text1.charAt(0) != text2.charAt(0)) { |
|
return 0; |
|
} |
|
// Binary search. |
|
// Performance analysis: http://neil.fraser.name/news/2007/10/09/ |
|
var pointermin = 0; |
|
var pointermax = Math.min(text1.length, text2.length); |
|
var pointermid = pointermax; |
|
var pointerstart = 0; |
|
while (pointermin < pointermid) { |
|
if (text1.substring(pointerstart, pointermid) == |
|
text2.substring(pointerstart, pointermid)) { |
|
pointermin = pointermid; |
|
pointerstart = pointermin; |
|
} else { |
|
pointermax = pointermid; |
|
} |
|
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin); |
|
} |
|
return pointermid; |
|
}; |
|
|
|
|
|
/** |
|
* Determine the common suffix of two strings |
|
* @param {string} text1 First string. |
|
* @param {string} text2 Second string. |
|
* @return {number} The number of characters common to the end of each string. |
|
*/ |
|
diff_match_patch.prototype.diff_commonSuffix = function(text1, text2) { |
|
// Quick check for common null cases. |
|
if (!text1 || !text2 || text1.charAt(text1.length - 1) != |
|
text2.charAt(text2.length - 1)) { |
|
return 0; |
|
} |
|
// Binary search. |
|
// Performance analysis: http://neil.fraser.name/news/2007/10/09/ |
|
var pointermin = 0; |
|
var pointermax = Math.min(text1.length, text2.length); |
|
var pointermid = pointermax; |
|
var pointerend = 0; |
|
while (pointermin < pointermid) { |
|
if (text1.substring(text1.length - pointermid, text1.length - pointerend) == |
|
text2.substring(text2.length - pointermid, text2.length - pointerend)) { |
|
pointermin = pointermid; |
|
pointerend = pointermin; |
|
} else { |
|
pointermax = pointermid; |
|
} |
|
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin); |
|
} |
|
return pointermid; |
|
}; |
|
|
|
|
|
/** |
|
* Do the two texts share a substring which is at least half the length of the |
|
* longer text? |
|
* @param {string} text1 First string. |
|
* @param {string} text2 Second string. |
|
* @return {?Array.<string>} Five element Array, containing the prefix of |
|
* text1, the suffix of text1, the prefix of text2, the suffix of |
|
* text2 and the common middle. Or null if there was no match. |
|
*/ |
|
diff_match_patch.prototype.diff_halfMatch = function(text1, text2) { |
|
var longtext = text1.length > text2.length ? text1 : text2; |
|
var shorttext = text1.length > text2.length ? text2 : text1; |
|
if (longtext.length < 10 || shorttext.length < 1) { |
|
return null; // Pointless. |
|
} |
|
var dmp = this; // 'this' becomes 'window' in a closure. |
|
|
|
/** |
|
* Does a substring of shorttext exist within longtext such that the substring |
|
* is at least half the length of longtext? |
|
* Closure, but does not reference any external variables. |
|
* @param {string} longtext Longer string. |
|
* @param {string} shorttext Shorter string. |
|
* @param {number} i Start index of quarter length substring within longtext |
|
* @return {?Array.<string>} Five element Array, containing the prefix of |
|
* longtext, the suffix of longtext, the prefix of shorttext, the suffix |
|
* of shorttext and the common middle. Or null if there was no match. |
|
* @private |
|
*/ |
|
function diff_halfMatchI(longtext, shorttext, i) { |
|
// Start with a 1/4 length substring at position i as a seed. |
|
var seed = longtext.substring(i, i + Math.floor(longtext.length / 4)); |
|
var j = -1; |
|
var best_common = ''; |
|
var best_longtext_a, best_longtext_b, best_shorttext_a, best_shorttext_b; |
|
while ((j = shorttext.indexOf(seed, j + 1)) != -1) { |
|
var prefixLength = dmp.diff_commonPrefix(longtext.substring(i), |
|
shorttext.substring(j)); |
|
var suffixLength = dmp.diff_commonSuffix(longtext.substring(0, i), |
|
shorttext.substring(0, j)); |
|
if (best_common.length < suffixLength + prefixLength) { |
|
best_common = shorttext.substring(j - suffixLength, j) + |
|
shorttext.substring(j, j + prefixLength); |
|
best_longtext_a = longtext.substring(0, i - suffixLength); |
|
best_longtext_b = longtext.substring(i + prefixLength); |
|
best_shorttext_a = shorttext.substring(0, j - suffixLength); |
|
best_shorttext_b = shorttext.substring(j + prefixLength); |
|
} |
|
} |
|
if (best_common.length >= longtext.length / 2) { |
|
return [best_longtext_a, best_longtext_b, |
|
best_shorttext_a, best_shorttext_b, best_common]; |
|
} else { |
|
return null; |
|
} |
|
} |
|
|
|
// First check if the second quarter is the seed for a half-match. |
|
var hm1 = diff_halfMatchI(longtext, shorttext, |
|
Math.ceil(longtext.length / 4)); |
|
// Check again based on the third quarter. |
|
var hm2 = diff_halfMatchI(longtext, shorttext, |
|
Math.ceil(longtext.length / 2)); |
|
var hm; |
|
if (!hm1 && !hm2) { |
|
return null; |
|
} else if (!hm2) { |
|
hm = hm1; |
|
} else if (!hm1) { |
|
hm = hm2; |
|
} else { |
|
// Both matched. Select the longest. |
|
hm = hm1[4].length > hm2[4].length ? hm1 : hm2; |
|
} |
|
|
|
// A half-match was found, sort out the return data. |
|
var text1_a, text1_b, text2_a, text2_b; |
|
if (text1.length > text2.length) { |
|
text1_a = hm[0]; |
|
text1_b = hm[1]; |
|
text2_a = hm[2]; |
|
text2_b = hm[3]; |
|
} else { |
|
text2_a = hm[0]; |
|
text2_b = hm[1]; |
|
text1_a = hm[2]; |
|
text1_b = hm[3]; |
|
} |
|
var mid_common = hm[4]; |
|
return [text1_a, text1_b, text2_a, text2_b, mid_common]; |
|
}; |
|
|
|
|
|
/** |
|
* Reduce the number of edits by eliminating semantically trivial equalities. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
*/ |
|
diff_match_patch.prototype.diff_cleanupSemantic = function(diffs) { |
|
var changes = false; |
|
var equalities = []; // Stack of indices where equalities are found. |
|
var equalitiesLength = 0; // Keeping our own length var is faster in JS. |
|
var lastequality = null; // Always equal to equalities[equalitiesLength-1][1] |
|
var pointer = 0; // Index of current position. |
|
// Number of characters that changed prior to the equality. |
|
var length_changes1 = 0; |
|
// Number of characters that changed after the equality. |
|
var length_changes2 = 0; |
|
while (pointer < diffs.length) { |
|
if (diffs[pointer][0] == DIFF_EQUAL) { // equality found |
|
equalities[equalitiesLength++] = pointer; |
|
length_changes1 = length_changes2; |
|
length_changes2 = 0; |
|
lastequality = diffs[pointer][1]; |
|
} else { // an insertion or deletion |
|
length_changes2 += diffs[pointer][1].length; |
|
if (lastequality !== null && (lastequality.length <= length_changes1) && |
|
(lastequality.length <= length_changes2)) { |
|
// Duplicate record |
|
diffs.splice(equalities[equalitiesLength - 1], 0, |
|
[DIFF_DELETE, lastequality]); |
|
// Change second copy to insert. |
|
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; |
|
// Throw away the equality we just deleted. |
|
equalitiesLength--; |
|
// Throw away the previous equality (it needs to be reevaluated). |
|
equalitiesLength--; |
|
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1; |
|
length_changes1 = 0; // Reset the counters. |
|
length_changes2 = 0; |
|
lastequality = null; |
|
changes = true; |
|
} |
|
} |
|
pointer++; |
|
} |
|
if (changes) { |
|
this.diff_cleanupMerge(diffs); |
|
} |
|
this.diff_cleanupSemanticLossless(diffs); |
|
}; |
|
|
|
|
|
/** |
|
* Look for single edits surrounded on both sides by equalities |
|
* which can be shifted sideways to align the edit to a word boundary. |
|
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
*/ |
|
diff_match_patch.prototype.diff_cleanupSemanticLossless = function(diffs) { |
|
// Define some regex patterns for matching boundaries. |
|
var punctuation = /[^a-zA-Z0-9]/; |
|
var whitespace = /\s/; |
|
var linebreak = /[\r\n]/; |
|
var blanklineEnd = /\n\r?\n$/; |
|
var blanklineStart = /^\r?\n\r?\n/; |
|
|
|
/** |
|
* Given two strings, compute a score representing whether the internal |
|
* boundary falls on logical boundaries. |
|
* Scores range from 5 (best) to 0 (worst). |
|
* Closure, makes reference to regex patterns defined above. |
|
* @param {string} one First string. |
|
* @param {string} two Second string. |
|
* @return {number} The score. |
|
*/ |
|
function diff_cleanupSemanticScore(one, two) { |
|
if (!one || !two) { |
|
// Edges are the best. |
|
return 5; |
|
} |
|
|
|
// Each port of this function behaves slightly differently due to |
|
// subtle differences in each language's definition of things like |
|
// 'whitespace'. Since this function's purpose is largely cosmetic, |
|
// the choice has been made to use each language's native features |
|
// rather than force total conformity. |
|
var score = 0; |
|
// One point for non-alphanumeric. |
|
if (one.charAt(one.length - 1).match(punctuation) || |
|
two.charAt(0).match(punctuation)) { |
|
score++; |
|
// Two points for whitespace. |
|
if (one.charAt(one.length - 1).match(whitespace) || |
|
two.charAt(0).match(whitespace)) { |
|
score++; |
|
// Three points for line breaks. |
|
if (one.charAt(one.length - 1).match(linebreak) || |
|
two.charAt(0).match(linebreak)) { |
|
score++; |
|
// Four points for blank lines. |
|
if (one.match(blanklineEnd) || two.match(blanklineStart)) { |
|
score++; |
|
} |
|
} |
|
} |
|
} |
|
return score; |
|
} |
|
|
|
var pointer = 1; |
|
// Intentionally ignore the first and last element (don't need checking). |
|
while (pointer < diffs.length - 1) { |
|
if (diffs[pointer - 1][0] == DIFF_EQUAL && |
|
diffs[pointer + 1][0] == DIFF_EQUAL) { |
|
// This is a single edit surrounded by equalities. |
|
var equality1 = diffs[pointer - 1][1]; |
|
var edit = diffs[pointer][1]; |
|
var equality2 = diffs[pointer + 1][1]; |
|
|
|
// First, shift the edit as far left as possible. |
|
var commonOffset = this.diff_commonSuffix(equality1, edit); |
|
if (commonOffset) { |
|
var commonString = edit.substring(edit.length - commonOffset); |
|
equality1 = equality1.substring(0, equality1.length - commonOffset); |
|
edit = commonString + edit.substring(0, edit.length - commonOffset); |
|
equality2 = commonString + equality2; |
|
} |
|
|
|
// Second, step character by character right, looking for the best fit. |
|
var bestEquality1 = equality1; |
|
var bestEdit = edit; |
|
var bestEquality2 = equality2; |
|
var bestScore = diff_cleanupSemanticScore(equality1, edit) + |
|
diff_cleanupSemanticScore(edit, equality2); |
|
while (edit.charAt(0) === equality2.charAt(0)) { |
|
equality1 += edit.charAt(0); |
|
edit = edit.substring(1) + equality2.charAt(0); |
|
equality2 = equality2.substring(1); |
|
var score = diff_cleanupSemanticScore(equality1, edit) + |
|
diff_cleanupSemanticScore(edit, equality2); |
|
// The >= encourages trailing rather than leading whitespace on edits. |
|
if (score >= bestScore) { |
|
bestScore = score; |
|
bestEquality1 = equality1; |
|
bestEdit = edit; |
|
bestEquality2 = equality2; |
|
} |
|
} |
|
|
|
if (diffs[pointer - 1][1] != bestEquality1) { |
|
// We have an improvement, save it back to the diff. |
|
if (bestEquality1) { |
|
diffs[pointer - 1][1] = bestEquality1; |
|
} else { |
|
diffs.splice(pointer - 1, 1); |
|
pointer--; |
|
} |
|
diffs[pointer][1] = bestEdit; |
|
if (bestEquality2) { |
|
diffs[pointer + 1][1] = bestEquality2; |
|
} else { |
|
diffs.splice(pointer + 1, 1); |
|
pointer--; |
|
} |
|
} |
|
} |
|
pointer++; |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* Reduce the number of edits by eliminating operationally trivial equalities. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
*/ |
|
diff_match_patch.prototype.diff_cleanupEfficiency = function(diffs) { |
|
var changes = false; |
|
var equalities = []; // Stack of indices where equalities are found. |
|
var equalitiesLength = 0; // Keeping our own length var is faster in JS. |
|
var lastequality = ''; // Always equal to equalities[equalitiesLength-1][1] |
|
var pointer = 0; // Index of current position. |
|
// Is there an insertion operation before the last equality. |
|
var pre_ins = false; |
|
// Is there a deletion operation before the last equality. |
|
var pre_del = false; |
|
// Is there an insertion operation after the last equality. |
|
var post_ins = false; |
|
// Is there a deletion operation after the last equality. |
|
var post_del = false; |
|
while (pointer < diffs.length) { |
|
if (diffs[pointer][0] == DIFF_EQUAL) { // equality found |
|
if (diffs[pointer][1].length < this.Diff_EditCost && |
|
(post_ins || post_del)) { |
|
// Candidate found. |
|
equalities[equalitiesLength++] = pointer; |
|
pre_ins = post_ins; |
|
pre_del = post_del; |
|
lastequality = diffs[pointer][1]; |
|
} else { |
|
// Not a candidate, and can never become one. |
|
equalitiesLength = 0; |
|
lastequality = ''; |
|
} |
|
post_ins = post_del = false; |
|
} else { // an insertion or deletion |
|
if (diffs[pointer][0] == DIFF_DELETE) { |
|
post_del = true; |
|
} else { |
|
post_ins = true; |
|
} |
|
/* |
|
* Five types to be split: |
|
* <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del> |
|
* <ins>A</ins>X<ins>C</ins><del>D</del> |
|
* <ins>A</ins><del>B</del>X<ins>C</ins> |
|
* <ins>A</del>X<ins>C</ins><del>D</del> |
|
* <ins>A</ins><del>B</del>X<del>C</del> |
|
*/ |
|
if (lastequality && ((pre_ins && pre_del && post_ins && post_del) || |
|
((lastequality.length < this.Diff_EditCost / 2) && |
|
(pre_ins + pre_del + post_ins + post_del) == 3))) { |
|
// Duplicate record |
|
diffs.splice(equalities[equalitiesLength - 1], 0, |
|
[DIFF_DELETE, lastequality]); |
|
// Change second copy to insert. |
|
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; |
|
equalitiesLength--; // Throw away the equality we just deleted; |
|
lastequality = ''; |
|
if (pre_ins && pre_del) { |
|
// No changes made which could affect previous entry, keep going. |
|
post_ins = post_del = true; |
|
equalitiesLength = 0; |
|
} else { |
|
equalitiesLength--; // Throw away the previous equality; |
|
pointer = equalitiesLength > 0 ? |
|
equalities[equalitiesLength - 1] : -1; |
|
post_ins = post_del = false; |
|
} |
|
changes = true; |
|
} |
|
} |
|
pointer++; |
|
} |
|
|
|
if (changes) { |
|
this.diff_cleanupMerge(diffs); |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* Reorder and merge like edit sections. Merge equalities. |
|
* Any edit section can move as long as it doesn't cross an equality. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
*/ |
|
diff_match_patch.prototype.diff_cleanupMerge = function(diffs) { |
|
diffs.push([DIFF_EQUAL, '']); // Add a dummy entry at the end. |
|
var pointer = 0; |
|
var count_delete = 0; |
|
var count_insert = 0; |
|
var text_delete = ''; |
|
var text_insert = ''; |
|
var commonlength; |
|
while (pointer < diffs.length) { |
|
switch (diffs[pointer][0]) { |
|
case DIFF_INSERT: |
|
count_insert++; |
|
text_insert += diffs[pointer][1]; |
|
pointer++; |
|
break; |
|
case DIFF_DELETE: |
|
count_delete++; |
|
text_delete += diffs[pointer][1]; |
|
pointer++; |
|
break; |
|
case DIFF_EQUAL: |
|
// Upon reaching an equality, check for prior redundancies. |
|
if (count_delete !== 0 || count_insert !== 0) { |
|
if (count_delete !== 0 && count_insert !== 0) { |
|
// Factor out any common prefixies. |
|
commonlength = this.diff_commonPrefix(text_insert, text_delete); |
|
if (commonlength !== 0) { |
|
if ((pointer - count_delete - count_insert) > 0 && |
|
diffs[pointer - count_delete - count_insert - 1][0] == |
|
DIFF_EQUAL) { |
|
diffs[pointer - count_delete - count_insert - 1][1] += |
|
text_insert.substring(0, commonlength); |
|
} else { |
|
diffs.splice(0, 0, [DIFF_EQUAL, |
|
text_insert.substring(0, commonlength)]); |
|
pointer++; |
|
} |
|
text_insert = text_insert.substring(commonlength); |
|
text_delete = text_delete.substring(commonlength); |
|
} |
|
// Factor out any common suffixies. |
|
commonlength = this.diff_commonSuffix(text_insert, text_delete); |
|
if (commonlength !== 0) { |
|
diffs[pointer][1] = text_insert.substring(text_insert.length - |
|
commonlength) + diffs[pointer][1]; |
|
text_insert = text_insert.substring(0, text_insert.length - |
|
commonlength); |
|
text_delete = text_delete.substring(0, text_delete.length - |
|
commonlength); |
|
} |
|
} |
|
// Delete the offending records and add the merged ones. |
|
if (count_delete === 0) { |
|
diffs.splice(pointer - count_delete - count_insert, |
|
count_delete + count_insert, [DIFF_INSERT, text_insert]); |
|
} else if (count_insert === 0) { |
|
diffs.splice(pointer - count_delete - count_insert, |
|
count_delete + count_insert, [DIFF_DELETE, text_delete]); |
|
} else { |
|
diffs.splice(pointer - count_delete - count_insert, |
|
count_delete + count_insert, [DIFF_DELETE, text_delete], |
|
[DIFF_INSERT, text_insert]); |
|
} |
|
pointer = pointer - count_delete - count_insert + |
|
(count_delete ? 1 : 0) + (count_insert ? 1 : 0) + 1; |
|
} else if (pointer !== 0 && diffs[pointer - 1][0] == DIFF_EQUAL) { |
|
// Merge this equality with the previous one. |
|
diffs[pointer - 1][1] += diffs[pointer][1]; |
|
diffs.splice(pointer, 1); |
|
} else { |
|
pointer++; |
|
} |
|
count_insert = 0; |
|
count_delete = 0; |
|
text_delete = ''; |
|
text_insert = ''; |
|
break; |
|
} |
|
} |
|
if (diffs[diffs.length - 1][1] === '') { |
|
diffs.pop(); // Remove the dummy entry at the end. |
|
} |
|
|
|
// Second pass: look for single edits surrounded on both sides by equalities |
|
// which can be shifted sideways to eliminate an equality. |
|
// e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC |
|
var changes = false; |
|
pointer = 1; |
|
// Intentionally ignore the first and last element (don't need checking). |
|
while (pointer < diffs.length - 1) { |
|
if (diffs[pointer - 1][0] == DIFF_EQUAL && |
|
diffs[pointer + 1][0] == DIFF_EQUAL) { |
|
// This is a single edit surrounded by equalities. |
|
if (diffs[pointer][1].substring(diffs[pointer][1].length - |
|
diffs[pointer - 1][1].length) == diffs[pointer - 1][1]) { |
|
// Shift the edit over the previous equality. |
|
diffs[pointer][1] = diffs[pointer - 1][1] + |
|
diffs[pointer][1].substring(0, diffs[pointer][1].length - |
|
diffs[pointer - 1][1].length); |
|
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1]; |
|
diffs.splice(pointer - 1, 1); |
|
changes = true; |
|
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) == |
|
diffs[pointer + 1][1]) { |
|
// Shift the edit over the next equality. |
|
diffs[pointer - 1][1] += diffs[pointer + 1][1]; |
|
diffs[pointer][1] = |
|
diffs[pointer][1].substring(diffs[pointer + 1][1].length) + |
|
diffs[pointer + 1][1]; |
|
diffs.splice(pointer + 1, 1); |
|
changes = true; |
|
} |
|
} |
|
pointer++; |
|
} |
|
// If shifts were made, the diff needs reordering and another shift sweep. |
|
if (changes) { |
|
this.diff_cleanupMerge(diffs); |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* loc is a location in text1, compute and return the equivalent location in |
|
* text2. |
|
* e.g. 'The cat' vs 'The big cat', 1->1, 5->8 |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @param {number} loc Location within text1. |
|
* @return {number} Location within text2. |
|
*/ |
|
diff_match_patch.prototype.diff_xIndex = function(diffs, loc) { |
|
var chars1 = 0; |
|
var chars2 = 0; |
|
var last_chars1 = 0; |
|
var last_chars2 = 0; |
|
var x; |
|
for (x = 0; x < diffs.length; x++) { |
|
if (diffs[x][0] !== DIFF_INSERT) { // Equality or deletion. |
|
chars1 += diffs[x][1].length; |
|
} |
|
if (diffs[x][0] !== DIFF_DELETE) { // Equality or insertion. |
|
chars2 += diffs[x][1].length; |
|
} |
|
if (chars1 > loc) { // Overshot the location. |
|
break; |
|
} |
|
last_chars1 = chars1; |
|
last_chars2 = chars2; |
|
} |
|
// Was the location was deleted? |
|
if (diffs.length != x && diffs[x][0] === DIFF_DELETE) { |
|
return last_chars2; |
|
} |
|
// Add the remaining character length. |
|
return last_chars2 + (loc - last_chars1); |
|
}; |
|
|
|
|
|
/** |
|
* Convert a diff array into a pretty HTML report. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @return {string} HTML representation. |
|
*/ |
|
diff_match_patch.prototype.diff_prettyHtml = function(diffs) { |
|
var html = []; |
|
var i = 0; |
|
for (var x = 0; x < diffs.length; x++) { |
|
var op = diffs[x][0]; // Operation (insert, delete, equal) |
|
var data = diffs[x][1]; // Text of change. |
|
var text = data.replace(/&/g, '&').replace(/</g, '<') |
|
.replace(/>/g, '>').replace(/\n/g, '¶<BR>'); |
|
switch (op) { |
|
case DIFF_INSERT: |
|
html[x] = '<INS STYLE="background:#E6FFE6;" TITLE="i=' + i + '">' + |
|
text + '</INS>'; |
|
break; |
|
case DIFF_DELETE: |
|
html[x] = '<DEL STYLE="background:#FFE6E6;" TITLE="i=' + i + '">' + |
|
text + '</DEL>'; |
|
break; |
|
case DIFF_EQUAL: |
|
html[x] = '<SPAN TITLE="i=' + i + '">' + text + '</SPAN>'; |
|
break; |
|
} |
|
if (op !== DIFF_DELETE) { |
|
i += data.length; |
|
} |
|
} |
|
return html.join(''); |
|
}; |
|
|
|
|
|
/** |
|
* Compute and return the source text (all equalities and deletions). |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @return {string} Source text. |
|
*/ |
|
diff_match_patch.prototype.diff_text1 = function(diffs) { |
|
var text = []; |
|
for (var x = 0; x < diffs.length; x++) { |
|
if (diffs[x][0] !== DIFF_INSERT) { |
|
text[x] = diffs[x][1]; |
|
} |
|
} |
|
return text.join(''); |
|
}; |
|
|
|
|
|
/** |
|
* Compute and return the destination text (all equalities and insertions). |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @return {string} Destination text. |
|
*/ |
|
diff_match_patch.prototype.diff_text2 = function(diffs) { |
|
var text = []; |
|
for (var x = 0; x < diffs.length; x++) { |
|
if (diffs[x][0] !== DIFF_DELETE) { |
|
text[x] = diffs[x][1]; |
|
} |
|
} |
|
return text.join(''); |
|
}; |
|
|
|
|
|
/** |
|
* Compute the Levenshtein distance; the number of inserted, deleted or |
|
* substituted characters. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @return {number} Number of changes. |
|
*/ |
|
diff_match_patch.prototype.diff_levenshtein = function(diffs) { |
|
var levenshtein = 0; |
|
var insertions = 0; |
|
var deletions = 0; |
|
for (var x = 0; x < diffs.length; x++) { |
|
var op = diffs[x][0]; |
|
var data = diffs[x][1]; |
|
switch (op) { |
|
case DIFF_INSERT: |
|
insertions += data.length; |
|
break; |
|
case DIFF_DELETE: |
|
deletions += data.length; |
|
break; |
|
case DIFF_EQUAL: |
|
// A deletion and an insertion is one substitution. |
|
levenshtein += Math.max(insertions, deletions); |
|
insertions = 0; |
|
deletions = 0; |
|
break; |
|
} |
|
} |
|
levenshtein += Math.max(insertions, deletions); |
|
return levenshtein; |
|
}; |
|
|
|
|
|
/** |
|
* Crush the diff into an encoded string which describes the operations |
|
* required to transform text1 into text2. |
|
* E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. |
|
* Operations are tab-separated. Inserted text is escaped using %xx notation. |
|
* @param {Array.<Array.<number|string>>} diffs Array of diff tuples. |
|
* @return {string} Delta text. |
|
*/ |
|
diff_match_patch.prototype.diff_toDelta = function(diffs) { |
|
var text = []; |
|
for (var x = 0; x < diffs.length; x++) { |
|
switch (diffs[x][0]) { |
|
case DIFF_INSERT: |
|
text[x] = '+' + encodeURI(diffs[x][1]); |
|
break; |
|
case DIFF_DELETE: |
|
text[x] = '-' + diffs[x][1].length; |
|
break; |
|
case DIFF_EQUAL: |
|
text[x] = '=' + diffs[x][1].length; |
|
break; |
|
} |
|
} |
|
// Opera doesn't know how to encode char 0. |
|
return text.join('\t').replace(/\x00/g, '%00').replace(/%20/g, ' '); |
|
}; |
|
|
|
|
|
/** |
|
* Given the original text1, and an encoded string which describes the |
|
* operations required to transform text1 into text2, compute the full diff. |
|
* @param {string} text1 Source string for the diff. |
|
* @param {string} delta Delta text. |
|
* @return {Array.<Array.<number|string>>} Array of diff tuples. |
|
* @throws {Error} If invalid input. |
|
*/ |
|
diff_match_patch.prototype.diff_fromDelta = function(text1, delta) { |
|
var diffs = []; |
|
var diffsLength = 0; // Keeping our own length var is faster in JS. |
|
var pointer = 0; // Cursor in text1 |
|
// Opera doesn't know how to decode char 0. |
|
delta = delta.replace(/%00/g, '\0'); |
|
var tokens = delta.split(/\t/g); |
|
for (var x = 0; x < tokens.length; x++) { |
|
// Each token begins with a one character parameter which specifies the |
|
// operation of this token (delete, insert, equality). |
|
var param = tokens[x].substring(1); |
|
switch (tokens[x].charAt(0)) { |
|
case '+': |
|
try { |
|
diffs[diffsLength++] = [DIFF_INSERT, decodeURI(param)]; |
|
} catch (ex) { |
|
// Malformed URI sequence. |
|
throw new Error('Illegal escape in diff_fromDelta: ' + param); |
|
} |
|
break; |
|
case '-': |
|
// Fall through. |
|
case '=': |
|
var n = parseInt(param, 10); |
|
if (isNaN(n) || n < 0) { |
|
throw new Error('Invalid number in diff_fromDelta: ' + param); |
|
} |
|
var text = text1.substring(pointer, pointer += n); |
|
if (tokens[x].charAt(0) == '=') { |
|
diffs[diffsLength++] = [DIFF_EQUAL, text]; |
|
} else { |
|
diffs[diffsLength++] = [DIFF_DELETE, text]; |
|
} |
|
break; |
|
default: |
|
// Blank tokens are ok (from a trailing \t). |
|
// Anything else is an error. |
|
if (tokens[x]) { |
|
throw new Error('Invalid diff operation in diff_fromDelta: ' + |
|
tokens[x]); |
|
} |
|
} |
|
} |
|
if (pointer != text1.length) { |
|
throw new Error('Delta length (' + pointer + |
|
') does not equal source text length (' + text1.length + ').'); |
|
} |
|
return diffs; |
|
}; |
|
|
|
|
|
// MATCH FUNCTIONS |
|
|
|
|
|
/** |
|
* Locate the best instance of 'pattern' in 'text' near 'loc'. |
|
* @param {string} text The text to search. |
|
* @param {string} pattern The pattern to search for. |
|
* @param {number} loc The location to search around. |
|
* @return {number} Best match index or -1. |
|
*/ |
|
diff_match_patch.prototype.match_main = function(text, pattern, loc) { |
|
// Check for null inputs. |
|
if (text == null || pattern == null || loc == null) { |
|
throw new Error('Null input. (match_main)'); |
|
} |
|
|
|
loc = Math.max(0, Math.min(loc, text.length)); |
|
if (text == pattern) { |
|
// Shortcut (potentially not guaranteed by the algorithm) |
|
return 0; |
|
} else if (!text.length) { |
|
// Nothing to match. |
|
return -1; |
|
} else if (text.substring(loc, loc + pattern.length) == pattern) { |
|
// Perfect match at the perfect spot! (Includes case of null pattern) |
|
return loc; |
|
} else { |
|
// Do a fuzzy compare. |
|
return this.match_bitap(text, pattern, loc); |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* Locate the best instance of 'pattern' in 'text' near 'loc' using the |
|
* Bitap algorithm. |
|
* @param {string} text The text to search. |
|
* @param {string} pattern The pattern to search for. |
|
* @param {number} loc The location to search around. |
|
* @return {number} Best match index or -1. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.match_bitap = function(text, pattern, loc) { |
|
if (pattern.length > this.Match_MaxBits) { |
|
throw new Error('Pattern too long for this browser.'); |
|
} |
|
|
|
// Initialise the alphabet. |
|
var s = this.match_alphabet(pattern); |
|
|
|
var dmp = this; // 'this' becomes 'window' in a closure. |
|
|
|
/** |
|
* Compute and return the score for a match with e errors and x location. |
|
* Accesses loc and pattern through being a closure. |
|
* @param {number} e Number of errors in match. |
|
* @param {number} x Location of match. |
|
* @return {number} Overall score for match (0.0 = good, 1.0 = bad). |
|
* @private |
|
*/ |
|
function match_bitapScore(e, x) { |
|
var accuracy = e / pattern.length; |
|
var proximity = Math.abs(loc - x); |
|
if (!dmp.Match_Distance) { |
|
// Dodge divide by zero error. |
|
return proximity ? 1.0 : accuracy; |
|
} |
|
return accuracy + (proximity / dmp.Match_Distance); |
|
} |
|
|
|
// Highest score beyond which we give up. |
|
var score_threshold = this.Match_Threshold; |
|
// Is there a nearby exact match? (speedup) |
|
var best_loc = text.indexOf(pattern, loc); |
|
if (best_loc != -1) { |
|
score_threshold = Math.min(match_bitapScore(0, best_loc), score_threshold); |
|
// What about in the other direction? (speedup) |
|
best_loc = text.lastIndexOf(pattern, loc + pattern.length); |
|
if (best_loc != -1) { |
|
score_threshold = |
|
Math.min(match_bitapScore(0, best_loc), score_threshold); |
|
} |
|
} |
|
|
|
// Initialise the bit arrays. |
|
var matchmask = 1 << (pattern.length - 1); |
|
best_loc = -1; |
|
|
|
var bin_min, bin_mid; |
|
var bin_max = pattern.length + text.length; |
|
var last_rd; |
|
for (var d = 0; d < pattern.length; d++) { |
|
// Scan for the best match; each iteration allows for one more error. |
|
// Run a binary search to determine how far from 'loc' we can stray at this |
|
// error level. |
|
bin_min = 0; |
|
bin_mid = bin_max; |
|
while (bin_min < bin_mid) { |
|
if (match_bitapScore(d, loc + bin_mid) <= score_threshold) { |
|
bin_min = bin_mid; |
|
} else { |
|
bin_max = bin_mid; |
|
} |
|
bin_mid = Math.floor((bin_max - bin_min) / 2 + bin_min); |
|
} |
|
// Use the result from this iteration as the maximum for the next. |
|
bin_max = bin_mid; |
|
var start = Math.max(1, loc - bin_mid + 1); |
|
var finish = Math.min(loc + bin_mid, text.length) + pattern.length; |
|
|
|
var rd = Array(finish + 2); |
|
rd[finish + 1] = (1 << d) - 1; |
|
for (var j = finish; j >= start; j--) { |
|
// The alphabet (s) is a sparse hash, so the following line generates |
|
// warnings. |
|
var charMatch = s[text.charAt(j - 1)]; |
|
if (d === 0) { // First pass: exact match. |
|
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch; |
|
} else { // Subsequent passes: fuzzy match. |
|
rd[j] = ((rd[j + 1] << 1) | 1) & charMatch | |
|
(((last_rd[j + 1] | last_rd[j]) << 1) | 1) | |
|
last_rd[j + 1]; |
|
} |
|
if (rd[j] & matchmask) { |
|
var score = match_bitapScore(d, j - 1); |
|
// This match will almost certainly be better than any existing match. |
|
// But check anyway. |
|
if (score <= score_threshold) { |
|
// Told you so. |
|
score_threshold = score; |
|
best_loc = j - 1; |
|
if (best_loc > loc) { |
|
// When passing loc, don't exceed our current distance from loc. |
|
start = Math.max(1, 2 * loc - best_loc); |
|
} else { |
|
// Already passed loc, downhill from here on in. |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
// No hope for a (better) match at greater error levels. |
|
if (match_bitapScore(d + 1, loc) > score_threshold) { |
|
break; |
|
} |
|
last_rd = rd; |
|
} |
|
return best_loc; |
|
}; |
|
|
|
|
|
/** |
|
* Initialise the alphabet for the Bitap algorithm. |
|
* @param {string} pattern The text to encode. |
|
* @return {Object} Hash of character locations. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.match_alphabet = function(pattern) { |
|
var s = {}; |
|
for (var i = 0; i < pattern.length; i++) { |
|
s[pattern.charAt(i)] = 0; |
|
} |
|
for (var i = 0; i < pattern.length; i++) { |
|
s[pattern.charAt(i)] |= 1 << (pattern.length - i - 1); |
|
} |
|
return s; |
|
}; |
|
|
|
|
|
// PATCH FUNCTIONS |
|
|
|
|
|
/** |
|
* Increase the context until it is unique, |
|
* but don't let the pattern expand beyond Match_MaxBits. |
|
* @param {patch_obj} patch The patch to grow. |
|
* @param {string} text Source text. |
|
* @private |
|
*/ |
|
diff_match_patch.prototype.patch_addContext = function(patch, text) { |
|
if (text.length == 0) { |
|
return; |
|
} |
|
var pattern = text.substring(patch.start2, patch.start2 + patch.length1); |
|
var padding = 0; |
|
|
|
// Look for the first and last matches of pattern in text. If two different |
|
// matches are found, increase the pattern length. |
|
while (text.indexOf(pattern) != text.lastIndexOf(pattern) && |
|
pattern.length < this.Match_MaxBits - this.Patch_Margin - |
|
this.Patch_Margin) { |
|
padding += this.Patch_Margin; |
|
pattern = text.substring(patch.start2 - padding, |
|
patch.start2 + patch.length1 + padding); |
|
} |
|
// Add one chunk for good luck. |
|
padding += this.Patch_Margin; |
|
|
|
// Add the prefix. |
|
var prefix = text.substring(patch.start2 - padding, patch.start2); |
|
if (prefix) { |
|
patch.diffs.unshift([DIFF_EQUAL, prefix]); |
|
} |
|
// Add the suffix. |
|
var suffix = text.substring(patch.start2 + patch.length1, |
|
patch.start2 + patch.length1 + padding); |
|
if (suffix) { |
|
patch.diffs.push([DIFF_EQUAL, suffix]); |
|
} |
|
|
|
// Roll back the start points. |
|
patch.start1 -= prefix.length; |
|
patch.start2 -= prefix.length; |
|
// Extend the lengths. |
|
patch.length1 += prefix.length + suffix.length; |
|
patch.length2 += prefix.length + suffix.length; |
|
}; |
|
|
|
|
|
/** |
|
* Compute a list of patches to turn text1 into text2. |
|
* Use diffs if provided, otherwise compute it ourselves. |
|
* There are four ways to call this function, depending on what data is |
|
* available to the caller: |
|
* Method 1: |
|
* a = text1, b = text2 |
|
* Method 2: |
|
* a = diffs |
|
* Method 3 (optimal): |
|
* a = text1, b = diffs |
|
* Method 4 (deprecated, use method 3): |
|
* a = text1, b = text2, c = diffs |
|
* |
|
* @param {string|Array.<Array.<number|string>>} a text1 (methods 1,3,4) or |
|
* Array of diff tuples for text1 to text2 (method 2). |
|
* @param {string|Array.<Array.<number|string>>} opt_b text2 (methods 1,4) or |
|
* Array of diff tuples for text1 to text2 (method 3) or undefined (method 2). |
|
* @param {string|Array.<Array.<number|string>>} opt_c Array of diff tuples for |
|
* text1 to text2 (method 4) or undefined (methods 1,2,3). |
|
* @return {Array.<patch_obj>} Array of patch objects. |
|
*/ |
|
diff_match_patch.prototype.patch_make = function(a, opt_b, opt_c) { |
|
var text1, diffs; |
|
if (typeof a == 'string' && typeof opt_b == 'string' && |
|
typeof opt_c == 'undefined') { |
|
// Method 1: text1, text2 |
|
// Compute diffs from text1 and text2. |
|
text1 = a; |
|
diffs = this.diff_main(text1, opt_b, true); |
|
if (diffs.length > 2) { |
|
this.diff_cleanupSemantic(diffs); |
|
this.diff_cleanupEfficiency(diffs); |
|
} |
|
} else if (a && typeof a == 'object' && typeof opt_b == 'undefined' && |
|
typeof opt_c == 'undefined') { |
|
// Method 2: diffs |
|
// Compute text1 from diffs. |
|
diffs = a; |
|
text1 = this.diff_text1(diffs); |
|
} else if (typeof a == 'string' && opt_b && typeof opt_b == 'object' && |
|
typeof opt_c == 'undefined') { |
|
// Method 3: text1, diffs |
|
text1 = a; |
|
diffs = opt_b; |
|
} else if (typeof a == 'string' && typeof opt_b == 'string' && |
|
opt_c && typeof opt_c == 'object') { |
|
// Method 4: text1, text2, diffs |
|
// text2 is not used. |
|
text1 = a; |
|
diffs = opt_c; |
|
} else { |
|
throw new Error('Unknown call format to patch_make.'); |
|
} |
|
|
|
if (diffs.length === 0) { |
|
return []; // Get rid of the null case. |
|
} |
|
var patches = []; |
|
var patch = new patch_obj(); |
|
var patchDiffLength = 0; // Keeping our own length var is faster in JS. |
|
var char_count1 = 0; // Number of characters into the text1 string. |
|
var char_count2 = 0; // Number of characters into the text2 string. |
|
// Start with text1 (prepatch_text) and apply the diffs until we arrive at |
|
// text2 (postpatch_text). We recreate the patches one by one to determine |
|
// context info. |
|
var prepatch_text = text1; |
|
var postpatch_text = text1; |
|
for (var x = 0; x < diffs.length; x++) { |
|
var diff_type = diffs[x][0]; |
|
var diff_text = diffs[x][1]; |
|
|
|
if (!patchDiffLength && diff_type !== DIFF_EQUAL) { |
|
// A new patch starts here. |
|
patch.start1 = char_count1; |
|
patch.start2 = char_count2; |
|
} |
|
|
|
switch (diff_type) { |
|
case DIFF_INSERT: |
|
patch.diffs[patchDiffLength++] = diffs[x]; |
|
patch.length2 += diff_text.length; |
|
postpatch_text = postpatch_text.substring(0, char_count2) + diff_text + |
|
postpatch_text.substring(char_count2); |
|
break; |
|
case DIFF_DELETE: |
|
patch.length1 += diff_text.length; |
|
patch.diffs[patchDiffLength++] = diffs[x]; |
|
postpatch_text = postpatch_text.substring(0, char_count2) + |
|
postpatch_text.substring(char_count2 + |
|
diff_text.length); |
|
break; |
|
case DIFF_EQUAL: |
|
if (diff_text.length <= 2 * this.Patch_Margin && |
|
patchDiffLength && diffs.length != x + 1) { |
|
// Small equality inside a patch. |
|
patch.diffs[patchDiffLength++] = diffs[x]; |
|
patch.length1 += diff_text.length; |
|
patch.length2 += diff_text.length; |
|
} else if (diff_text.length >= 2 * this.Patch_Margin) { |
|
// Time for a new patch. |
|
if (patchDiffLength) { |
|
this.patch_addContext(patch, prepatch_text); |
|
patches.push(patch); |
|
patch = new patch_obj(); |
|
patchDiffLength = 0; |
|
// Unlike Unidiff, our patch lists have a rolling context. |
|
// http://code.google.com/p/google-diff-match-patch/wiki/Unidiff |
|
// Update prepatch text & pos to reflect the application of the |
|
// just completed patch. |
|
prepatch_text = postpatch_text; |
|
char_count1 = char_count2; |
|
} |
|
} |
|
break; |
|
} |
|
|
|
// Update the current character count. |
|
if (diff_type !== DIFF_INSERT) { |
|
char_count1 += diff_text.length; |
|
} |
|
if (diff_type !== DIFF_DELETE) { |
|
char_count2 += diff_text.length; |
|
} |
|
} |
|
// Pick up the leftover patch if not empty. |
|
if (patchDiffLength) { |
|
this.patch_addContext(patch, prepatch_text); |
|
patches.push(patch); |
|
} |
|
|
|
return patches; |
|
}; |
|
|
|
|
|
/** |
|
* Given an array of patches, return another array that is identical. |
|
* @param {Array.<patch_obj>} patches Array of patch objects. |
|
* @return {Array.<patch_obj>} Array of patch objects. |
|
*/ |
|
diff_match_patch.prototype.patch_deepCopy = function(patches) { |
|
// Making deep copies is hard in JavaScript. |
|
var patchesCopy = []; |
|
for (var x = 0; x < patches.length; x++) { |
|
var patch = patches[x]; |
|
var patchCopy = new patch_obj(); |
|
patchCopy.diffs = []; |
|
for (var y = 0; y < patch.diffs.length; y++) { |
|
patchCopy.diffs[y] = patch.diffs[y].slice(); |
|
} |
|
patchCopy.start1 = patch.start1; |
|
patchCopy.start2 = patch.start2; |
|
patchCopy.length1 = patch.length1; |
|
patchCopy.length2 = patch.length2; |
|
patchesCopy[x] = patchCopy; |
|
} |
|
return patchesCopy; |
|
}; |
|
|
|
|
|
/** |
|
* Merge a set of patches onto the text. Return a patched text, as well |
|
* as a list of true/false values indicating which patches were applied. |
|
* @param {Array.<patch_obj>} patches Array of patch objects. |
|
* @param {string} text Old text. |
|
* @return {Array.<string|Array.<boolean>>} Two element Array, containing the |
|
* new text and an array of boolean values. |
|
*/ |
|
diff_match_patch.prototype.patch_apply = function(patches, text) { |
|
if (patches.length == 0) { |
|
return [text, []]; |
|
} |
|
|
|
// Deep copy the patches so that no changes are made to originals. |
|
patches = this.patch_deepCopy(patches); |
|
|
|
var nullPadding = this.patch_addPadding(patches); |
|
text = nullPadding + text + nullPadding; |
|
|
|
this.patch_splitMax(patches); |
|
// delta keeps track of the offset between the expected and actual location |
|
// of the previous patch. If there are patches expected at positions 10 and |
|
// 20, but the first patch was found at 12, delta is 2 and the second patch |
|
// has an effective expected position of 22. |
|
var delta = 0; |
|
var results = []; |
|
for (var x = 0; x < patches.length; x++) { |
|
var expected_loc = patches[x].start2 + delta; |
|
var text1 = this.diff_text1(patches[x].diffs); |
|
var start_loc; |
|
var end_loc = -1; |
|
if (text1.length > this.Match_MaxBits) { |
|
// patch_splitMax will only provide an oversized pattern in the case of |
|
// a monster delete. |
|
start_loc = this.match_main(text, text1.substring(0, this.Match_MaxBits), |
|
expected_loc); |
|
if (start_loc != -1) { |
|
end_loc = this.match_main(text, |
|
text1.substring(text1.length - this.Match_MaxBits), |
|
expected_loc + text1.length - this.Match_MaxBits); |
|
if (end_loc == -1 || start_loc >= end_loc) { |
|
// Can't find valid trailing context. Drop this patch. |
|
start_loc = -1; |
|
} |
|
} |
|
} else { |
|
start_loc = this.match_main(text, text1, expected_loc); |
|
} |
|
if (start_loc == -1) { |
|
// No match found. :( |
|
results[x] = false; |
|
// Subtract the delta for this failed patch from subsequent patches. |
|
delta -= patches[x].length2 - patches[x].length1; |
|
} else { |
|
// Found a match. :) |
|
results[x] = true; |
|
delta = start_loc - expected_loc; |
|
var text2; |
|
if (end_loc == -1) { |
|
text2 = text.substring(start_loc, start_loc + text1.length); |
|
} else { |
|
text2 = text.substring(start_loc, end_loc + this.Match_MaxBits); |
|
} |
|
if (text1 == text2) { |
|
// Perfect match, just shove the replacement text in. |
|
text = text.substring(0, start_loc) + |
|
this.diff_text2(patches[x].diffs) + |
|
text.substring(start_loc + text1.length); |
|
} else { |
|
// Imperfect match. Run a diff to get a framework of equivalent |
|
// indices. |
|
var diffs = this.diff_main(text1, text2, false); |
|
if (text1.length > this.Match_MaxBits && |
|
this.diff_levenshtein(diffs) / text1.length > |
|
this.Patch_DeleteThreshold) { |
|
// The end points match, but the content is unacceptably bad. |
|
results[x] = false; |
|
} else { |
|
this.diff_cleanupSemanticLossless(diffs); |
|
var index1 = 0; |
|
var index2; |
|
for (var y = 0; y < patches[x].diffs.length; y++) { |
|
var mod = patches[x].diffs[y]; |
|
if (mod[0] !== DIFF_EQUAL) { |
|
index2 = this.diff_xIndex(diffs, index1); |
|
} |
|
if (mod[0] === DIFF_INSERT) { // Insertion |
|
text = text.substring(0, start_loc + index2) + mod[1] + |
|
text.substring(start_loc + index2); |
|
} else if (mod[0] === DIFF_DELETE) { // Deletion |
|
text = text.substring(0, start_loc + index2) + |
|
text.substring(start_loc + this.diff_xIndex(diffs, |
|
index1 + mod[1].length)); |
|
} |
|
if (mod[0] !== DIFF_DELETE) { |
|
index1 += mod[1].length; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
// Strip the padding off. |
|
text = text.substring(nullPadding.length, text.length - nullPadding.length); |
|
return [text, results]; |
|
}; |
|
|
|
|
|
/** |
|
* Add some padding on text start and end so that edges can match something. |
|
* Intended to be called only from within patch_apply. |
|
* @param {Array.<patch_obj>} patches Array of patch objects. |
|
* @return {string} The padding string added to each side. |
|
*/ |
|
diff_match_patch.prototype.patch_addPadding = function(patches) { |
|
var paddingLength = this.Patch_Margin; |
|
var nullPadding = ''; |
|
for (var x = 1; x <= paddingLength; x++) { |
|
nullPadding += String.fromCharCode(x); |
|
} |
|
|
|
// Bump all the patches forward. |
|
for (var x = 0; x < patches.length; x++) { |
|
patches[x].start1 += paddingLength; |
|
patches[x].start2 += paddingLength; |
|
} |
|
|
|
// Add some padding on start of first diff. |
|
var patch = patches[0]; |
|
var diffs = patch.diffs; |
|
if (diffs.length == 0 || diffs[0][0] != DIFF_EQUAL) { |
|
// Add nullPadding equality. |
|
diffs.unshift([DIFF_EQUAL, nullPadding]); |
|
patch.start1 -= paddingLength; // Should be 0. |
|
patch.start2 -= paddingLength; // Should be 0. |
|
patch.length1 += paddingLength; |
|
patch.length2 += paddingLength; |
|
} else if (paddingLength > diffs[0][1].length) { |
|
// Grow first equality. |
|
var extraLength = paddingLength - diffs[0][1].length; |
|
diffs[0][1] = nullPadding.substring(diffs[0][1].length) + diffs[0][1]; |
|
patch.start1 -= extraLength; |
|
patch.start2 -= extraLength; |
|
patch.length1 += extraLength; |
|
patch.length2 += extraLength; |
|
} |
|
|
|
// Add some padding on end of last diff. |
|
patch = patches[patches.length - 1]; |
|
diffs = patch.diffs; |
|
if (diffs.length == 0 || diffs[diffs.length - 1][0] != DIFF_EQUAL) { |
|
// Add nullPadding equality. |
|
diffs.push([DIFF_EQUAL, nullPadding]); |
|
patch.length1 += paddingLength; |
|
patch.length2 += paddingLength; |
|
} else if (paddingLength > diffs[diffs.length - 1][1].length) { |
|
// Grow last equality. |
|
var extraLength = paddingLength - diffs[diffs.length - 1][1].length; |
|
diffs[diffs.length - 1][1] += nullPadding.substring(0, extraLength); |
|
patch.length1 += extraLength; |
|
patch.length2 += extraLength; |
|
} |
|
|
|
return nullPadding; |
|
}; |
|
|
|
|
|
/** |
|
* Look through the patches and break up any which are longer than the maximum |
|
* limit of the match algorithm. |
|
* @param {Array.<patch_obj>} patches Array of patch objects. |
|
*/ |
|
diff_match_patch.prototype.patch_splitMax = function(patches) { |
|
for (var x = 0; x < patches.length; x++) { |
|
if (patches[x].length1 > this.Match_MaxBits) { |
|
var bigpatch = patches[x]; |
|
// Remove the big old patch. |
|
patches.splice(x--, 1); |
|
var patch_size = this.Match_MaxBits; |
|
var start1 = bigpatch.start1; |
|
var start2 = bigpatch.start2; |
|
var precontext = ''; |
|
while (bigpatch.diffs.length !== 0) { |
|
// Create one of several smaller patches. |
|
var patch = new patch_obj(); |
|
var empty = true; |
|
patch.start1 = start1 - precontext.length; |
|
patch.start2 = start2 - precontext.length; |
|
if (precontext !== '') { |
|
patch.length1 = patch.length2 = precontext.length; |
|
patch.diffs.push([DIFF_EQUAL, precontext]); |
|
} |
|
while (bigpatch.diffs.length !== 0 && |
|
patch.length1 < patch_size - this.Patch_Margin) { |
|
var diff_type = bigpatch.diffs[0][0]; |
|
var diff_text = bigpatch.diffs[0][1]; |
|
if (diff_type === DIFF_INSERT) { |
|
// Insertions are harmless. |
|
patch.length2 += diff_text.length; |
|
start2 += diff_text.length; |
|
patch.diffs.push(bigpatch.diffs.shift()); |
|
empty = false; |
|
} else if (diff_type === DIFF_DELETE && patch.diffs.length == 1 && |
|
patch.diffs[0][0] == DIFF_EQUAL && |
|
diff_text.length > 2 * patch_size) { |
|
// This is a large deletion. Let it pass in one chunk. |
|
patch.length1 += diff_text.length; |
|
start1 += diff_text.length; |
|
empty = false; |
|
patch.diffs.push([diff_type, diff_text]); |
|
bigpatch.diffs.shift(); |
|
} else { |
|
// Deletion or equality. Only take as much as we can stomach. |
|
diff_text = diff_text.substring(0, patch_size - patch.length1 - |
|
this.Patch_Margin); |
|
patch.length1 += diff_text.length; |
|
start1 += diff_text.length; |
|
if (diff_type === DIFF_EQUAL) { |
|
patch.length2 += diff_text.length; |
|
start2 += diff_text.length; |
|
} else { |
|
empty = false; |
|
} |
|
patch.diffs.push([diff_type, diff_text]); |
|
if (diff_text == bigpatch.diffs[0][1]) { |
|
bigpatch.diffs.shift(); |
|
} else { |
|
bigpatch.diffs[0][1] = |
|
bigpatch.diffs[0][1].substring(diff_text.length); |
|
} |
|
} |
|
} |
|
// Compute the head context for the next patch. |
|
precontext = this.diff_text2(patch.diffs); |
|
precontext = |
|
precontext.substring(precontext.length - this.Patch_Margin); |
|
// Append the end context for this patch. |
|
var postcontext = this.diff_text1(bigpatch.diffs) |
|
.substring(0, this.Patch_Margin); |
|
if (postcontext !== '') { |
|
patch.length1 += postcontext.length; |
|
patch.length2 += postcontext.length; |
|
if (patch.diffs.length !== 0 && |
|
patch.diffs[patch.diffs.length - 1][0] === DIFF_EQUAL) { |
|
patch.diffs[patch.diffs.length - 1][1] += postcontext; |
|
} else { |
|
patch.diffs.push([DIFF_EQUAL, postcontext]); |
|
} |
|
} |
|
if (!empty) { |
|
patches.splice(++x, 0, patch); |
|
} |
|
} |
|
} |
|
} |
|
}; |
|
|
|
|
|
/** |
|
* Take a list of patches and return a textual representation. |
|
* @param {Array.<patch_obj>} patches Array of patch objects. |
|
* @return {string} Text representation of patches. |
|
*/ |
|
diff_match_patch.prototype.patch_toText = function(patches) { |
|
var text = []; |
|
for (var x = 0; x < patches.length; x++) { |
|
text[x] = patches[x]; |
|
} |
|
return text.join(''); |
|
}; |
|
|
|
|
|
/** |
|
* Parse a textual representation of patches and return a list of patch objects. |
|
* @param {string} textline Text representation of patches. |
|
* @return {Array.<patch_obj>} Array of patch objects. |
|
* @throws {Error} If invalid input. |
|
*/ |
|
diff_match_patch.prototype.patch_fromText = function(textline) { |
|
var patches = []; |
|
if (!textline) { |
|
return patches; |
|
} |
|
// Opera doesn't know how to decode char 0. |
|
textline = textline.replace(/%00/g, '\0'); |
|
var text = textline.split('\n'); |
|
var textPointer = 0; |
|
while (textPointer < text.length) { |
|
var m = text[textPointer].match(/^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/); |
|
if (!m) { |
|
throw new Error('Invalid patch string: ' + text[textPointer]); |
|
} |
|
var patch = new patch_obj(); |
|
patches.push(patch); |
|
patch.start1 = parseInt(m[1], 10); |
|
if (m[2] === '') { |
|
patch.start1--; |
|
patch.length1 = 1; |
|
} else if (m[2] == '0') { |
|
patch.length1 = 0; |
|
} else { |
|
patch.start1--; |
|
patch.length1 = parseInt(m[2], 10); |
|
} |
|
|
|
patch.start2 = parseInt(m[3], 10); |
|
if (m[4] === '') { |
|
patch.start2--; |
|
patch.length2 = 1; |
|
} else if (m[4] == '0') { |
|
patch.length2 = 0; |
|
} else { |
|
patch.start2--; |
|
patch.length2 = parseInt(m[4], 10); |
|
} |
|
textPointer++; |
|
|
|
while (textPointer < text.length) { |
|
var sign = text[textPointer].charAt(0); |
|
try { |
|
var line = decodeURI(text[textPointer].substring(1)); |
|
} catch (ex) { |
|
// Malformed URI sequence. |
|
throw new Error('Illegal escape in patch_fromText: ' + line); |
|
} |
|
if (sign == '-') { |
|
// Deletion. |
|
patch.diffs.push([DIFF_DELETE, line]); |
|
} else if (sign == '+') { |
|
// Insertion. |
|
patch.diffs.push([DIFF_INSERT, line]); |
|
} else if (sign == ' ') { |
|
// Minor equality. |
|
patch.diffs.push([DIFF_EQUAL, line]); |
|
} else if (sign == '@') { |
|
// Start of next patch. |
|
break; |
|
} else if (sign === '') { |
|
// Blank line? Whatever. |
|
} else { |
|
// WTF? |
|
throw new Error('Invalid patch mode "' + sign + '" in: ' + line); |
|
} |
|
textPointer++; |
|
} |
|
} |
|
return patches; |
|
}; |
|
|
|
|
|
/** |
|
* Class representing one patch operation. |
|
* @constructor |
|
*/ |
|
function patch_obj() { |
|
/** @type {Array.<Array.<number|string>>} */ |
|
this.diffs = []; |
|
/** @type {?number} */ |
|
this.start1 = null; |
|
/** @type {?number} */ |
|
this.start2 = null; |
|
/** @type {number} */ |
|
this.length1 = 0; |
|
/** @type {number} */ |
|
this.length2 = 0; |
|
} |
|
|
|
|
|
/** |
|
* Emmulate GNU diff's format. |
|
* Header: @@ -382,8 +481,9 @@ |
|
* Indicies are printed as 1-based, not 0-based. |
|
* @return {string} The GNU diff string. |
|
*/ |
|
patch_obj.prototype.toString = function() { |
|
var coords1, coords2; |
|
if (this.length1 === 0) { |
|
coords1 = this.start1 + ',0'; |
|
} else if (this.length1 == 1) { |
|
coords1 = this.start1 + 1; |
|
} else { |
|
coords1 = (this.start1 + 1) + ',' + this.length1; |
|
} |
|
if (this.length2 === 0) { |
|
coords2 = this.start2 + ',0'; |
|
} else if (this.length2 == 1) { |
|
coords2 = this.start2 + 1; |
|
} else { |
|
coords2 = (this.start2 + 1) + ',' + this.length2; |
|
} |
|
var text = ['@@ -' + coords1 + ' +' + coords2 + ' @@\n']; |
|
var op; |
|
// Escape the body of the patch with %xx notation. |
|
for (var x = 0; x < this.diffs.length; x++) { |
|
switch (this.diffs[x][0]) { |
|
case DIFF_INSERT: |
|
op = '+'; |
|
break; |
|
case DIFF_DELETE: |
|
op = '-'; |
|
break; |
|
case DIFF_EQUAL: |
|
op = ' '; |
|
break; |
|
} |
|
text[x + 1] = op + encodeURI(this.diffs[x][1]) + '\n'; |
|
} |
|
// Opera doesn't know how to encode char 0. |
|
return text.join('').replace(/\x00/g, '%00').replace(/%20/g, ' '); |
|
}; |
|
|
|
|
|
// Export these global variables so that they survive Google's JS compiler. |
|
/*changed by lfborjas: changed `window` for `exports` to make it suitable for the node.js module conventions*/ |
|
exports['diff_match_patch'] = diff_match_patch; |
|
exports['patch_obj'] = patch_obj; |
|
exports['DIFF_DELETE'] = DIFF_DELETE; |
|
exports['DIFF_INSERT'] = DIFF_INSERT; |
|
exports['DIFF_EQUAL'] = DIFF_EQUAL; |
|
|
|
},{}],12:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
|
|
var virtualize = require('vdom-virtualize') |
|
, serialize = require('vdom-serialize') |
|
, nodeAt = require('domnode-at-path') |
|
, pathTo = require('path-to-domnode') |
|
, Changeset = require('changesets').Changeset |
|
, ManipulateText = require('./lib/ops/manipulate-text') |
|
|
|
exports.create = function(initialData) { |
|
var requireJSDOM = 'jsdom-no-contextify' |
|
, jsdom = require(requireJSDOM).jsdom |
|
var document = jsdom(initialData||'<div>Hello world</div>') |
|
return document.body.firstChild |
|
} |
|
|
|
exports.apply = function(snapshot, ops) { |
|
snapshot = snapshot.cloneNode(true) |
|
unpackOps(ops) |
|
.forEach(function(op) { |
|
op.apply(snapshot) |
|
}) |
|
return snapshot |
|
} |
|
|
|
exports.transform = function(ops1, ops2, side) { |
|
return unpackOps(ops1).map(function(op1) { |
|
unpackOps(ops2).forEach(function(op2) { |
|
op1.transformAgainst(op2, ('left'==side)) |
|
}) |
|
return op1 |
|
}) |
|
} |
|
|
|
exports.compose = function(ops1, ops2) { |
|
//exports.transform(ops2, ops1) |
|
return ops1.concat(ops2) |
|
} |
|
|
|
exports.transformCursor = function(range, ops, rootNode) { |
|
var start, end, newrange = {} |
|
if(rootNode.contains(range.startContainer) && range.startContainer.nodeValue) { |
|
start = transformCaret({container: range.startContainer, offset: range.startOffset}, ops, rootNode) |
|
newrange.startContainer = start.container |
|
newrange.startOffset = start.offset |
|
} |
|
else { |
|
newrange.startContainer = range.startContainer |
|
newrange.startOffset = range.startOffset |
|
} |
|
if(rootNode.contains(range.endContainer) && range.endContainer.nodeValue) { |
|
end = transformCaret({container: range.endContainer, offset: range.endOffset}, ops, rootNode) |
|
newrange.endContainer = end.container |
|
newrange.endOffset = end.offset |
|
} |
|
else { |
|
newrange.endContainer = range.endContainer |
|
newrange.endOffset = range.endOffset |
|
} |
|
|
|
return newrange |
|
} |
|
|
|
function transformCaret(caret, ops, rootNode) { |
|
var cs = Changeset.create() |
|
.retain(caret.offset) |
|
.insert('|') |
|
.retain(caret.container.nodeValue.length-caret.offset) |
|
.end() |
|
.pack() |
|
var cursorOps = [new ManipulateText(pathTo(caret.container, rootNode), cs)] |
|
cursorOps = exports.transform(cursorOps, ops, 'right') |
|
var cursorOp = cursorOps[0] |
|
var container = nodeAt(cursorOp.path, rootNode) |
|
var offset = countInitialRetains(cursorOp.diff) |
|
return {container: container, offset: offset} |
|
} |
|
|
|
function countInitialRetains(cs) { |
|
var length = 0 |
|
var ops = Changeset.unpack(cs) |
|
for(var i=0; i<ops.length; i++) { |
|
if(ops[i].input === ops[i].output) length += ops[i].length |
|
else break |
|
} |
|
return length |
|
} |
|
|
|
exports.serialize = function(snapshot) { |
|
return serialize(virtualize(snapshot)) |
|
} |
|
|
|
exports.deserialize = function(data, document) { |
|
var virtual = JSON.parse(data) |
|
if(!document) { |
|
if('undefined' == typeof window) { |
|
var requireJSDOM = 'jsdom-no-contextify' |
|
, jsdom = require(requireJSDOM).jsdom |
|
document = jsdom('<div></div>') |
|
}else{ |
|
document = window.document |
|
} |
|
} |
|
return devirtualize(document, virtual) |
|
} |
|
|
|
function devirtualize(document, virtual) { |
|
var node |
|
if(typeof virtual.tagName !== 'undefined') { |
|
if(virtual.namespace) { |
|
node = document.createElementNS(virtual.namespace, virtual.tagName) |
|
}else{ |
|
node = document.createElement(virtual.tagName) |
|
} |
|
} |
|
if(typeof virtual.text !== 'undefined') { |
|
node = document.createTextNode(virtual.text) |
|
} |
|
if(virtual.properties) { |
|
for(var prop in virtual.properties) { |
|
if(prop == 'attributes') { |
|
for(var attr in virtual.properties.attributes) { |
|
node.setAttribute(attr, virtual.properties.attributes[attr]) |
|
} |
|
}else { |
|
if('object' === typeof virtual.properties[prop]) { |
|
for(var p in virtual.properties[prop]) { |
|
node[prop][p] = virtual.properties[prop][p] |
|
} |
|
}else{ |
|
node[prop] = virtual.properties[prop] |
|
} |
|
} |
|
} |
|
} |
|
if(virtual.children) { |
|
virtual.children |
|
.map(devirtualize.bind(null, document)) |
|
.forEach(function(n) { |
|
node.appendChild(n) |
|
}) |
|
} |
|
return node |
|
} |
|
|
|
var unpackOps = exports.unpackOps = function(unpackOps) { |
|
return unpackOps.map(function(op) { |
|
switch(op.type) { |
|
case 'Move': |
|
return new exports.Move(op.from, op.to, op.element) |
|
case 'Manipulate': |
|
return new exports.Manipulate(op.path, op.prop, op.value) |
|
case 'ManipulateText': |
|
return new exports.ManipulateText(op.path, op.diff) |
|
default: |
|
throw new Error('Unknown op type: '+op.type) |
|
} |
|
}) |
|
} |
|
|
|
exports.Move = require('./lib/index').Move |
|
exports.Manipulate = require('./lib/index').Manipulate |
|
exports.ManipulateText = require('./lib/index').ManipulateText |
|
exports.adapters = require('./lib/index').adapters |
|
|
|
},{"./lib/index":14,"./lib/ops/manipulate-text":17,"changesets":7,"domnode-at-path":20,"path-to-domnode":31,"vdom-serialize":34,"vdom-virtualize":35}],13:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Manipulate = require('./ops/manipulate') |
|
var ManipulateText = require('./ops/manipulate-text') |
|
var Move = require('./ops/move') |
|
var isPrefixOf = require('./ops/is-prefix') |
|
var pathTo = require('path-to-domnode') |
|
var serialize = require('../index').serialize |
|
var Changeset = require('changesets').Changeset |
|
, diff_match_patch = require('diff_match_patch').diff_match_patch |
|
, diffEngine = new diff_match_patch |
|
|
|
exports.import = summaryToOplist |
|
|
|
|
|
/* |
|
Create a new MutationSummary Observer that will call |
|
your callback with a sumary, each time a change matching one of your queries |
|
is made to node in your rootNode. |
|
|
|
Feed the summary to this function to create an oplist. |
|
|
|
``` |
|
var observer = new MutationSummary({ |
|
callback: handleChanges, // required |
|
rootNode: myDiv, // (defaults to window.document) |
|
observeOwnChanges: false // (defaults to false) |
|
oldPreviousSibling: false // defaults to false -- I don't understand this |
|
queries: [ |
|
{ ... } |
|
] |
|
}); |
|
``` |
|
|
|
You will probably want to know about all changes: |
|
|
|
``` |
|
{ all: true } |
|
``` |
|
|
|
A summary in this case would look like the following: |
|
|
|
``` |
|
{ |
|
added: [ array of <node> ], |
|
removed: [ array of <node> ], |
|
reparented: [ array of <node> ], |
|
reordered: [ array of <node> ], |
|
attributeChanged: { |
|
attributeName1: [ array of <element> ], |
|
attributeName2: [ array of <element> ], ... |
|
}, |
|
characterDataChanged: [array of <node>], |
|
|
|
getOldAttribute: function(element, attrName) { … }, // previous attribute value |
|
getOldCharaterData: function(node) { … }, // text before re-setting text |
|
getOldParentNode: function(node) { ... }, // parent before (re)moving |
|
getOldPreviousSibling: function(node) { ... } // position before reordering |
|
} |
|
``` |
|
|
|
*/ |
|
|
|
function summaryToOplist(summary, rootNode) { |
|
var oplist = [] |
|
|
|
summary.projection.processChildlistChanges() |
|
|
|
|
|
if (Array.isArray(summary.removed)) { |
|
summary.removed.forEach(function(node) { |
|
var oldParent = summary.getOldParentNode(node) |
|
if(oldParent && !rootNode.contains(oldParent)) { |
|
// my parent doesn't exist anymore, so it has prolly been removed |
|
// ergo this delete operation is pointless |
|
return |
|
} |
|
oplist.push(new Move(node.domOT_path, null, serialize(node))) |
|
}) |
|
} |
|
|
|
if (Array.isArray(summary.reparented)) { |
|
summary.reparented.forEach(function(node) { |
|
var oldParent = summary.getOldParentNode(node) |
|
, oldPath = node.domOT_path |
|
|
|
if(oldParent && !rootNode.contains(oldParent)) { |
|
// my parent doesn't exist anymore, so it has prolly been removed |
|
// ergo, this can be treated as an insert (this is the worst case and prolly needs fixing!) |
|
console.log('Cannot find oldParent of reparented node:', oldParent, oldParent.domOT_path) |
|
oldPath = null |
|
} |
|
oplist.push(new Move(oldPath, pathTo(node, rootNode), serialize(node))) |
|
}) |
|
} |
|
|
|
if (Array.isArray(summary.reordered)) { |
|
summary.reordered.forEach(function(node) { |
|
var oldParent = summary.getOldParentNode(node) |
|
if(oldParent && !rootNode.contains(oldParent)) { |
|
// parent was removed, ergo this is op pointless |
|
return |
|
} |
|
oplist.push(new Move(node.domOT_path, pathTo(node, rootNode))) |
|
}) |
|
} |
|
|
|
if (Array.isArray(summary.added)) { |
|
summary.added |
|
//.reverse()// traverse added elements in reverse order, because MutationSummary puts children first for some reason |
|
.forEach(function(node) { |
|
oplist.push(new Move(null, pathTo(node, rootNode), serialize(node))) |
|
}) |
|
} |
|
|
|
oplist.sort(sortOps) |
|
// console.log(JSON.stringify(oplist, null, ' ')) |
|
// transfrom moves and manipulates against (re)moves |
|
oplist.forEach(function(op2, i) { |
|
oplist.forEach(function(op, j) { |
|
if(op === op2) return |
|
var path = (op.from || op.path) |
|
, to |
|
if(path && i<j) { |
|
to = op.to |
|
op.transformAgainst(op2) |
|
op.to = to |
|
} |
|
}) |
|
}) |
|
// console.log(JSON.stringify(oplist, null, ' ')) |
|
|
|
|
|
// Wrapping |
|
// e.g. <div>hello <i>world</i></div> |
|
// -> <div>hello <b><i>world</i></b></div> |
|
// Since Move(nul, [0,1]) already contains <i>world</i> |
|
// we extract <i>world</i> from Move(nul, [0,1]) |
|
oplist.forEach(function(op1) { |
|
// Check if a child of this node is already being moved (i.e. this is a wrapping) |
|
var op2 |
|
oplist.some(function(op) { |
|
if(op === op1) return false |
|
if(isPrefixOf(op1.to, op.to)) { |
|
op2 = op |
|
return true |
|
} |
|
return false |
|
}) |
|
// if we've found a node being wrapped by this one |
|
// AND op1 is an insert... |
|
if(op2 && !op1.from) {// XXX: What should happen if op1 is not an insert? (prolly extremely unlikely) |
|
var vnode = JSON.parse(op1.element) |
|
vnode.children = [] |
|
op1.element = JSON.stringify(vnode) |
|
} |
|
}) |
|
|
|
if(summary.attributeChanged) { |
|
for(var attr in summary.attributeChanged) { |
|
summary.attributeChanged[attr].forEach(function(node) { |
|
oplist.push(new Manipulate(pathTo(node, rootNode), attr, node.getAttribute(attr))) |
|
}) |
|
} |
|
} |
|
|
|
if(Array.isArray(summary.characterDataChanged)) { |
|
summary.characterDataChanged.forEach(function(node) { |
|
var diff = diffEngine.diff_main(summary.getOldCharacterData(node), node.nodeValue) |
|
, changeset = Changeset.fromDiff(diff).pack() |
|
oplist.push(new ManipulateText(pathTo(node, rootNode), changeset)) |
|
}) |
|
} |
|
|
|
oplist.sort(sortOps) |
|
|
|
return oplist |
|
} |
|
|
|
function sortOps(op1, op2) { |
|
var path1 = (op1.path || op1.to || op1.from).map(strPad.bind(null, '00000')).join('') // XXX: Hard limit: Can't correctly sort ops with path elements longer than 5 digits |
|
var path2 = (op2.path || op2.to || op2.from).map(strPad.bind(null, '00000')).join('') |
|
return path1 == path2? 0 : (path1 > path2? 1 : -1) |
|
} |
|
|
|
function strPad(paddingValue, str) { |
|
return String(paddingValue + str).slice(-paddingValue.length); |
|
} |
|
|
|
exports.createIndex = function(rootNode) { |
|
setIndex(rootNode, []) |
|
} |
|
|
|
function setIndex(node, path) { |
|
node.domOT_path = path |
|
if(node.childNodes) { |
|
for(var i=0; i<node.childNodes.length; i++) { |
|
setIndex(node.childNodes[i], path.concat([i])) |
|
} |
|
} |
|
} |
|
|
|
},{"../index":12,"./ops/is-prefix":15,"./ops/manipulate":18,"./ops/manipulate-text":17,"./ops/move":19,"changesets":7,"diff_match_patch":11,"path-to-domnode":31}],14:[function(require,module,exports){ |
|
module.exports = { |
|
adapters: { |
|
mutationSummary: require('./adapter-mutationSummary') |
|
} |
|
, Move: require('./ops/move') |
|
, Manipulate: require('./ops/manipulate') |
|
, ManipulateText: require('./ops/manipulate-text') |
|
} |
|
|
|
},{"./adapter-mutationSummary":13,"./ops/manipulate":18,"./ops/manipulate-text":17,"./ops/move":19}],15:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
// basically checks if 2 is an ancestor of 1 |
|
// or rather, if path1 is a prefix of path2 |
|
module.exports = function isPrefixOf(path1, path2) { |
|
if(!path1 || !path2) return false |
|
|
|
// if path1 (the supposed ancestor) is deeper or as deep in the tree as |
|
// path2 (the supposed descendant), it cannot be an ancestor |
|
if(path2.length < path1.length) |
|
return false |
|
|
|
// Now, path2 is longer than or as long as path1, |
|
// so we can safely check all indices of path1 on path2 |
|
|
|
// so, let's check all steps and see if path1 is a prefix of path2 |
|
for(var i=0; i<path1.length; i++) { |
|
if(path1[i] != path2[i]) return false |
|
} |
|
|
|
return true // path1 is prefix of path1! |
|
} |
|
|
|
},{}],16:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var isPrefixOf = require('./is-prefix') |
|
module.exports = function isYoungerSiblingOf(path1, path2) { |
|
// check if path2 is on a higher tree level |
|
if(path2.length > path1.length) return false |
|
|
|
// check if they have the same parent |
|
var parent = path1.concat() |
|
do { |
|
parent.pop() |
|
if(isPrefixOf(parent, path2) && parent.length < path2.length) { |
|
if(path1[parent.length] > path2[parent.length] && parent.length+1 === path2.length) |
|
return parent.length |
|
} |
|
} while(parent.length); |
|
|
|
return false |
|
} |
|
|
|
module.exports.isYoungerOrEqualSiblingOf = function isYoungerOrEqualSiblingOf(path1, path2) { |
|
// check if path2 is on a higher tree level |
|
if(path2.length > path1.length) return false |
|
|
|
// check if they have the same parent |
|
var parent = path1.concat() |
|
do { |
|
parent.pop() |
|
if(isPrefixOf(parent, path2) && parent.length < path2.length) { |
|
if(path1[parent.length] >= path2[parent.length] && parent.length+1 === path2.length) |
|
return parent.length |
|
} |
|
} while(parent.length); |
|
|
|
return false |
|
} |
|
|
|
},{"./is-prefix":15}],17:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Manipulate = require('./manipulate') |
|
, Move = require('./move') |
|
, nodeAt = require('domnode-at-path') |
|
, changesets = require('changesets') |
|
|
|
// Paths are arrays, where [] is the root node |
|
|
|
function ManipulateText(path, diff) { |
|
this.path = path |
|
this.diff = diff |
|
this.type = 'ManipulateText' |
|
} |
|
|
|
module.exports = ManipulateText |
|
|
|
ManipulateText.prototype.transformAgainst = function (op, left) { |
|
if(!this.path) return |
|
if(op instanceof Move) { |
|
return Manipulate.prototype.transformAgainst.call(this, op) |
|
} |
|
if(op instanceof ManipulateText && op.path.join() == this.path.join()) { |
|
this.diff = changesets.transform(this.diff, op.diff, left? 'left': 'right') |
|
} |
|
} |
|
|
|
ManipulateText.prototype.merge = function(op) { |
|
if(op instanceof ManipulateText && op.path.join() === this.path.join()) { |
|
this.diff = changesets.compose(this.diff, op.diff) |
|
return this |
|
}else { |
|
return |
|
} |
|
} |
|
|
|
ManipulateText.prototype.apply = function (rootNode, index, dry) { |
|
if(!this.path || dry) return |
|
var targetNode = nodeAt(this.path, rootNode) |
|
if(!targetNode) throw new ReferenceError('Node not found: '+this.path.join(',')) |
|
targetNode.nodeValue = changesets.apply(targetNode.nodeValue, this.diff) |
|
} |
|
|
|
},{"./manipulate":18,"./move":19,"changesets":7,"domnode-at-path":20}],18:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Move = require('./move') |
|
, isYoungerSiblingOf = require('./is-youngerSibling') |
|
, isYoungerOrEqualSiblingOf = isYoungerSiblingOf.isYoungerOrEqualSiblingOf |
|
, isPrefixOf = require('./is-prefix') |
|
, nodeAt = require('domnode-at-path') |
|
|
|
// Paths are arrays, where [] is the root node |
|
function Manipulate(path, prop, value) { |
|
this.path = path |
|
this.prop = prop |
|
this.value = value |
|
this.type = 'Manipulate' |
|
} |
|
|
|
module.exports = Manipulate |
|
|
|
Manipulate.prototype.transformAgainst = function (op, left) { |
|
if(op instanceof Move && this.path) { |
|
var path = this.path.slice(0) |
|
, level |
|
|
|
if (op.from) { |
|
if (isPrefixOf(op.from, this.path)) { |
|
if(op.to) |
|
path = Array.prototype.concat(op.to, this.path.slice(op.from.length)) // go with the flow man. |
|
else |
|
path = null |
|
} |
|
|
|
if ((level = isYoungerSiblingOf(this.path, op.from)) !== false) |
|
path[level]-- // (shift to left) |
|
} |
|
|
|
if (op.to) { |
|
if ((level = isYoungerOrEqualSiblingOf(this.path, op.to)) !== false) |
|
if(this.path[level] === op.to[level]) { |
|
if(!left) path[level]++ // (shift to right) |
|
}else{ |
|
path[level]++ |
|
} |
|
} |
|
|
|
this.path = path |
|
} |
|
} |
|
|
|
/** |
|
* Incorporate the changes of an earlier operation |
|
*/ |
|
Manipulate.prototype.merge = function(op) { |
|
if(op instanceof Manipulate && op.path.join() === this.path.join() && op.prop === this.prop) { |
|
return this |
|
}else{ |
|
return |
|
} |
|
} |
|
|
|
Manipulate.prototype.apply = function (rootNode, index) { |
|
if(!this.path) return |
|
var myNode = nodeAt(this.path, rootNode) |
|
|
|
if(!myNode) throw new Error('Doesn\'t fit! Trying to manipulate a non-existing node: '+this.path.join(',')) |
|
|
|
myNode.setAttribute(this.prop, this.value) |
|
} |
|
|
|
},{"./is-prefix":15,"./is-youngerSibling":16,"./move":19,"domnode-at-path":20}],19:[function(require,module,exports){ |
|
/** |
|
* dom-ot - Operational transform library for DOM operations |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var isYoungerSiblingOf = require('./is-youngerSibling') |
|
, isYoungerOrEqualSiblingOf = isYoungerSiblingOf.isYoungerOrEqualSiblingOf |
|
, isPrefixOf = require('./is-prefix') |
|
, nodeAt = require('domnode-at-path') |
|
|
|
// Paths are arrays, where [] is the root node |
|
|
|
// If from is null, it's an insert |
|
// If to is null, it's a removal |
|
// element will hold the html to insert (or remove if you need invertabilty) |
|
// `to` should not be transformed against `from`, i.e. `to` paths expect |
|
// a tree where `from` is already removed! |
|
function Move(from, to, element/*(optional)*/) { |
|
this.from = from |
|
this.to = to |
|
this.element = element |
|
this.type = 'Move' |
|
} |
|
module.exports = Move |
|
|
|
Move.prototype.transformAgainst = function (op, left) { |
|
if(op instanceof Move) { |
|
var from = this.from && this.from.slice(0) |
|
, to = this.to && this.to.slice(0) |
|
, level |
|
|
|
if(this.from) { |
|
|
|
if(op.from) { |
|
if (isPrefixOf(op.from, this.from)) { |
|
if(op.to) |
|
from = Array.prototype.concat.call(op.to, this.from.slice(op.from.length)) // change from to be descendant of op.to instead of in op.from |
|
else |
|
from = null |
|
} |
|
|
|
if ((level = isYoungerSiblingOf(this.from, op.from)) !== false) |
|
from[level]-- // (shift to left) |
|
} |
|
|
|
if(op.to) { |
|
if ((level = isYoungerOrEqualSiblingOf(this.from, op.to)) !== false) |
|
if(this.from[level] === op.to[level]) { |
|
if(!left) from[level]++ // (shift to right) |
|
}else{ |
|
from[level]++ |
|
} |
|
} |
|
|
|
} |
|
|
|
if (this.to) { |
|
|
|
if(op.from) { |
|
if ((level = isYoungerSiblingOf(this.to, op.from)) !== false) |
|
to[level]-- // (shift to left) |
|
|
|
if (isPrefixOf(op.from, this.to) && op.from.length < this.to.length) { |
|
if(op.to) |
|
to = Array.prototype.concat.call(op.to, this.to.slice(op.from.length)) |
|
else |
|
to = null |
|
} |
|
} |
|
|
|
if(op.to) { |
|
if ((level = isYoungerOrEqualSiblingOf(this.to, op.to)) !== false) |
|
if(this.to[level] === op.to[level]){ |
|
if(!left) to[level]++ // (shift to right) |
|
}else{ |
|
to[level]++ |
|
} |
|
} |
|
|
|
} |
|
|
|
this.from = from |
|
this.to = to |
|
} |
|
} |
|
|
|
/** |
|
* Merge an older operation with this one |
|
*/ |
|
Move.prototype.merge = function(op) { |
|
if(op instanceof Move && op.to.join() === this.from.join()) { |
|
this.from = op.from |
|
return this |
|
}else { |
|
return |
|
} |
|
} |
|
|
|
Move.prototype.apply = function(rootNode, index, dry) { |
|
var myNode |
|
if(dry) return this.applyDry(rootNode, index) |
|
|
|
if (this.from) { |
|
myNode = nodeAt(this.from, rootNode) |
|
var oldParent = nodeAt(this.from.slice(0, this.from.length-1), rootNode) |
|
if(!myNode) throw new ReferenceError('Node not found: '+this.from.join(',')) |
|
oldParent.removeChild(myNode) |
|
} else { |
|
myNode = require('../../').deserialize(this.element, rootNode.ownerDocument) |
|
} |
|
|
|
if (this.to) { |
|
var newSibling = nodeAt(this.to, rootNode) |
|
var newParent = nodeAt(this.to.slice(0, this.to.length-1), rootNode) |
|
|
|
if(!newParent) throw new Error('Doesn\'t fit! Designated parent for insertion doesn\'t exist.') |
|
|
|
if(newSibling) { |
|
newParent.insertBefore(myNode, newSibling) |
|
} |
|
else if(newParent.childNodes.length == this.to[this.to.length-1]) { |
|
newParent.appendChild(myNode) |
|
} else { |
|
throw new Error('Doesn\'t fit! Trying to insert a node at a non-existing location.') |
|
} |
|
} |
|
|
|
if(index) this.applyDry(rootNode) |
|
} |
|
|
|
Move.prototype.applyDry = function(rootNode) { |
|
if(this.to) { |
|
var parentPath = this.to.slice(0, this.to.length-1) |
|
, parent = nodeAt(parentPath, rootNode) |
|
, node = nodeAt(this.to, rootNode) |
|
|
|
// set new location + set new location for child nodes |
|
setIndex(node, this.to) |
|
|
|
// shift all after this.to |
|
for(var i=0; i<parent.childNodes.length; i++) { |
|
setIndex(parent.childNodes[i], parentPath.concat([i])) |
|
} |
|
} |
|
} |
|
|
|
|
|
function setIndex(node, path) { |
|
node.domOT_path = path |
|
if(node.childNodes) { |
|
for(var i=0; i<node.childNodes.length; i++) { |
|
setIndex(node.childNodes[i], path.concat([i])) |
|
} |
|
} |
|
} |
|
|
|
},{"../../":12,"./is-prefix":15,"./is-youngerSibling":16,"domnode-at-path":20}],20:[function(require,module,exports){ |
|
/** |
|
* domnode-at-path |
|
* Copyright (C) 2016 Marcel Klehr <[email protected]> |
|
*/ |
|
// Paths are arrays, where [] is the root node |
|
module.exports = nodeAt |
|
function nodeAt(path, rootNode) { |
|
// Returns undefined if the node could not be found |
|
if(!rootNode) return |
|
|
|
if(path.length == 0) { |
|
return rootNode |
|
} |
|
|
|
var firstIndex = path[0] |
|
|
|
// Node#childNodes is a liste of *all* child nodes, including CharacterData |
|
// Element#children contains elements only. |
|
var curNode = rootNode.childNodes[firstIndex] |
|
|
|
return nodeAt(path.slice(1), curNode) |
|
} |
|
|
|
},{}],21:[function(require,module,exports){ |
|
/** |
|
* gulf-contenteditable |
|
* Copyright (C) 2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var gulf = require('gulf') |
|
, domOT = require('dom-ot') |
|
, MutationSummary = require('mutation-summary') |
|
|
|
module.exports = function(contenteditable) { |
|
var doc = new gulf.EditableDocument(new gulf.MemoryAdapter, domOT) |
|
doc.rootNode = contenteditable |
|
|
|
doc._setContents = function(newcontent, cb) { |
|
observer.disconnect() |
|
contenteditable.innerHTML = '' |
|
for(var i=0; i<newcontent.childNodes.length; i++) { |
|
contenteditable.appendChild(newcontent.childNodes[i].cloneNode(/*deep:*/true)) |
|
} |
|
domOT.adapters.mutationSummary.createIndex(contenteditable) |
|
observer.reconnect() |
|
cb() |
|
} |
|
|
|
doc._change = function(changes, cb) { |
|
observer.disconnect() |
|
console.log('_change', changes) |
|
|
|
var ops = domOT.unpackOps(changes) |
|
retainSelection(doc.rootNode, ops, function() { |
|
ops.forEach(function(op) { |
|
op.apply(contenteditable, /*index:*/true) |
|
}) |
|
}) |
|
|
|
observer.reconnect() |
|
cb() |
|
} |
|
|
|
doc._collectChanges = function(cb) { |
|
// changes are automatically collected by MutationSummary |
|
cb() |
|
} |
|
|
|
var observer = new MutationSummary({ |
|
rootNode: contenteditable, // (defaults to window.document) |
|
oldPreviousSibling: true, |
|
queries: [ |
|
{ all: true} |
|
], |
|
callback: onChange |
|
}) |
|
doc.mutationSummary = observer |
|
|
|
function onChange(summaries) { |
|
var ops = domOT.adapters.mutationSummary.import(summaries[0], contenteditable) |
|
ops = ops.filter(function(op) { |
|
// filter out changes to the root node |
|
if(op.path) return !!op.path.length |
|
else return true |
|
}) |
|
if(!ops.length) return |
|
console.log(ops) |
|
doc.update(ops) |
|
ops.forEach(function(op) { |
|
op.apply(contenteditable, /*index:*/true, /*dry:*/true) |
|
}) |
|
} |
|
|
|
return doc |
|
} |
|
|
|
function retainSelection(rootNode, ops, fn) { |
|
var selection = rootNode.ownerDocument.defaultView.getSelection() |
|
, ranges = [] |
|
for(var i=0; i<selection.rangeCount; i++) { |
|
var range = selection.getRangeAt(i) |
|
ranges.push(domOT.transformCursor(range, ops, rootNode)) |
|
} |
|
fn() |
|
selection.removeAllRanges() |
|
ranges.forEach(function(r) { |
|
var range = new Range |
|
if(r.startContainer) range.setStart(r.startContainer, r.startOffset) |
|
if(r.endContainer) range.setEnd(r.endContainer, r.endOffset) |
|
selection.addRange(range) |
|
}) |
|
} |
|
},{"dom-ot":12,"gulf":22,"mutation-summary":30}],22:[function(require,module,exports){ |
|
module.exports = { |
|
Link: require('./lib/Link') |
|
, Document: require('./lib/Document') |
|
, EditableDocument: require('./lib/EditableDocument') |
|
, Edit: require('./lib/Edit') |
|
, History: require('./lib/History') |
|
, MemoryAdapter: require('./lib/MemoryAdapter') |
|
} |
|
|
|
},{"./lib/Document":23,"./lib/Edit":24,"./lib/EditableDocument":25,"./lib/History":26,"./lib/Link":27,"./lib/MemoryAdapter":28}],23:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Link = require('./Link') |
|
, Edit = require('./Edit') |
|
, History = require('./History') |
|
, queue = require('queue') |
|
, EventEmitter = require('events').EventEmitter |
|
|
|
function Document(adapter, ottype) { |
|
EventEmitter.apply(this) |
|
this.id |
|
this.adapter = adapter |
|
this.ottype = ottype |
|
|
|
this.content = null |
|
this.history = new History(this) |
|
this.initialized = false |
|
|
|
this.slaves = [] |
|
this.links = [] |
|
this.master = null |
|
|
|
this.queue = queue() |
|
this.queue.concurrency = 1 |
|
this.queue.start() |
|
|
|
if(!this.ottype) throw new Error('Document: No ottype specified') |
|
if(!this.adapter) throw new Error('Document: No adapter specified') |
|
} |
|
|
|
module.exports = Document |
|
|
|
Document.prototype = Object.create(EventEmitter.prototype, { constructor: { value: Document }}) |
|
|
|
/** |
|
* Creates a new document |
|
*/ |
|
Document.create = function(adapter, ottype, content, cb) { |
|
var doc = new Document(adapter, ottype) |
|
doc.history.createDocument({ |
|
contents: ottype.serialize? ottype.serialize(content) : content |
|
, edit: Edit.newInitial(ottype) |
|
}, function(er, id) { |
|
if(er) return cb(er) |
|
doc.initialized = true |
|
doc.content = content |
|
doc.id = id |
|
doc.emit('init') |
|
cb(null, doc) |
|
}) |
|
return doc |
|
} |
|
|
|
Document.load = function(adapter, ottype, id, cb) { |
|
var doc = new Document(adapter, ottype) |
|
doc.adapter.getLatestSnapshot(id, function(er, snapshot) { |
|
if(er) return cb(er) |
|
doc.initialized = true |
|
doc.content = ottype.deserialize? |
|
ottype.deserialize(snapshot.contents) |
|
: snapshot.contents |
|
doc.id = id |
|
doc.emit('init') |
|
cb(null, doc) |
|
}) |
|
return doc |
|
} |
|
|
|
/** |
|
* Creates a new Link and attaches it as a slave |
|
* @param opts Options to be passed to Link constructor |
|
*/ |
|
Document.prototype.slaveLink = function(opts) { |
|
var link = new Link(opts) |
|
this.attachSlaveLink(link) |
|
return link |
|
} |
|
|
|
/** |
|
* Creates a new Link and attaches it as master |
|
* (You will want to listen to the link's 'close' event) |
|
* @param opts Options to be passed to Link constructor |
|
*/ |
|
Document.prototype.masterLink = function(opts) { |
|
var link = new Link(opts) |
|
this.attachMasterLink(link) |
|
return link |
|
} |
|
|
|
// XXX Detach link! |
|
|
|
/** |
|
* Attaches a link as master |
|
*/ |
|
Document.prototype.attachMasterLink = function(link) { |
|
this.master = link |
|
this.attachLink(link) |
|
|
|
link.on('editError', function() { |
|
link.send('requestInit') |
|
}.bind(this)) |
|
|
|
link.on('close', function() { |
|
this.master = null |
|
}.bind(this)) |
|
} |
|
|
|
/** |
|
* Attaches a link as a slave |
|
*/ |
|
Document.prototype.attachSlaveLink = function(link) { |
|
this.slaves.push(link) |
|
this.attachLink(link) |
|
|
|
link.on('editError', function() { |
|
this.history.latest(function(er, latest) { |
|
if(er) return this.emit('error', er) |
|
var content = (this.ottype.serialize)? this.ottype.serialize(this.content) : this.content |
|
link.send('init', {contents: content, edit: latest.edit.pack()}) |
|
}.bind(this)) |
|
}.bind(this)) |
|
|
|
link.on('close', function onclose() { |
|
this.slaves.splice(this.slaves.indexOf(link), 1) |
|
}.bind(this)) |
|
} |
|
|
|
Document.prototype.attachLink = function(link) { |
|
if(~this.links.indexOf(link)) return; |
|
|
|
this.links.push(link) |
|
|
|
// Other end requests init? can do. |
|
link.on('link:requestInit', function() { |
|
this.receiveRequestInit(link) |
|
}.bind(this)) |
|
|
|
// Other side sends init. |
|
link.on('link:init', function(data) { |
|
this.receiveInit(data, link) |
|
}.bind(this)) |
|
|
|
link.on('link:requestHistorySince', function(since) { |
|
this.receiveRequestHistorySince(since, link) |
|
}.bind(this)) |
|
|
|
// Other side sends edit. |
|
link.on('link:edit', function onedit(edit) { |
|
this.receiveEdit(edit, link.authenticated, link) |
|
}.bind(this)) |
|
|
|
link.on('close', function onclose() { |
|
this.links.splice(this.links.indexOf(link), 1) |
|
}.bind(this)) |
|
|
|
// If we don't know the document yet, request its content |
|
if(null === this.content) link.send('requestInit') |
|
} |
|
|
|
Document.prototype.receiveRequestInit = function(link) { |
|
if(!this.initialized) { |
|
return this.once('init', function() { |
|
this.receiveRequestInit(link) |
|
}.bind(this)) |
|
} |
|
this.history.latest(function(er, latest) { |
|
if(er) return this.emit('error', er) |
|
var content = (this.ottype.serialize)? this.ottype.serialize(this.content) : this.content |
|
link.send('init', {contents: content, edit: latest.edit.pack()}) |
|
}.bind(this)) |
|
} |
|
|
|
/** |
|
* Receive init |
|
* |
|
* @param data {Object} Example: {contents: "", edit: <Edit..>} |
|
* @param fromLink |
|
*/ |
|
Document.prototype.receiveInit = function(data, fromLink) { |
|
// I'm master? Don't go bossing me around! |
|
if(!this.master || fromLink !== this.master) return |
|
|
|
var content = data.contents |
|
, initialEdit = data.edit |
|
|
|
if(this.ottype.deserialize) { |
|
content = this.ottype.deserialize(content) |
|
} |
|
|
|
initialEdit = Edit.unpack(initialEdit, this.ottype) |
|
|
|
this.links.forEach(function(link) { link.reset() }) |
|
this.content = content |
|
|
|
this.history.reset() |
|
this.history.storeSnapshot({id: initialEdit.id, contents: data.contents, edit: initialEdit}, function() { |
|
// I got an init, so my slaves get one, too |
|
this.slaves.forEach(function(slave) { |
|
slave.send('init', data) |
|
}) |
|
}.bind(this)) |
|
|
|
this.initialized = true |
|
this.emit('init') |
|
|
|
return content |
|
} |
|
|
|
/** |
|
* Receive a requestHistorySince message |
|
* |
|
* @param sinceEditId String The last known edit id by the slave |
|
* @param fromLink |
|
*/ |
|
Document.prototype.receiveRequestHistorySince = function(sinceEditId, fromLink) { |
|
// Check to see if we know that edit |
|
this.history.remembers(sinceEditId, function(er, remembersEdit) { |
|
if(er) return this.emit('error', er) |
|
|
|
if(!remembersEdit) { |
|
// Sorry pal. |
|
// XXX: Maybe we should send an 'init' here? |
|
return |
|
} |
|
|
|
this.history.getAllAfter(sinceEditId, function(er, snapshots) { |
|
if(er) return this.emit('error', er) |
|
|
|
fromLink.reset() |
|
|
|
snapshots |
|
.map(function(s) { |
|
return s.edit |
|
}) |
|
.forEach(fromLink.sendEdit.bind(fromLink)) |
|
}.bind(this)) |
|
|
|
}.bind(this)) |
|
} |
|
|
|
/** |
|
* Receive an edit |
|
* |
|
* @param edit <Edit> |
|
* @paramfromLink <Link> |
|
*/ |
|
Document.prototype.receiveEdit = function(edit, author, fromLink, callback) { |
|
console.log('receiveEdit', edit) |
|
edit = Edit.unpack(edit, this.ottype) |
|
if (!this.master || fromLink === this.master) { |
|
// Edit comes from master, or even better: we are master, yea baby! |
|
this.queue.push(function(cb) { |
|
this.dispatchEdit(edit, author, fromLink, function(er, edit) { |
|
cb() |
|
callback && callback(er, edit) |
|
}) |
|
}.bind(this)) |
|
this.queue.start() |
|
}else { |
|
// check with master first |
|
this.master.sendEdit(edit, function onack(err, edit) { |
|
this.queue.push(function(cb) { |
|
this.dispatchEdit(edit, author,fromLink, function(er, edit) { |
|
cb() |
|
callback && callback(er, edit) |
|
}) |
|
}.bind(this)) |
|
this.queue.start() |
|
}.bind(this)) |
|
} |
|
} |
|
|
|
/** |
|
* Dispatch a received edit |
|
* |
|
* @param edit <Edit> |
|
* @param fromLink <Link> (optional> |
|
*/ |
|
Document.prototype.dispatchEdit = function(edit, author, fromLink, cb) { |
|
|
|
// Also check if this might be sentEdit, cause if we've requested History, then |
|
// the other side has reset their queue and thus destroyed all acks. |
|
// So, fromLink.sentEdit might have been accepted, but we might not havw got the ACK |
|
if(fromLink&& fromLink.sentEdit && fromLink.sentEdit.id === edit.id) { |
|
fromLink.sentEdit.callback(null, fromLink.sentEdit) |
|
fromLink.sentEdit = null |
|
setImmediate(function() { |
|
fromLink._read(0) |
|
}) |
|
cb() |
|
return |
|
} |
|
|
|
this.history.remembers(edit.id, function(er, remembers) { |
|
if(er) return this.emit('error',er) |
|
|
|
if (remembers) { |
|
// We've got this edit already. |
|
|
|
// If I'm master then we need to queue the ack |
|
// Slaves have to send it straight away |
|
if(fromLink === this.master) fromLink.send('ack' ,edit.id) |
|
else if (fromLink) fromLink.sendAck(edit.id) |
|
|
|
return cb(null, edit) |
|
} |
|
|
|
// Check integrity of this edit |
|
this.history.remembers(edit.parent, function(er, remembersParent) { |
|
if (!remembersParent) { |
|
if(fromLink === this.master) { |
|
// we probably missed some edits, let's ask master! |
|
this.history.latest(function(er, latestSnapshot) { |
|
if(er) return this.emit('error', er) |
|
this.master.send('requestHistorySince', latestSnapshot.edit.id) |
|
}.bind(this)) |
|
return cb() |
|
}else { |
|
// I'm master, I can't have missed that edit. So, throw and re-init! |
|
var e = new Error('Edit "'+edit.id+'" has unknown parent "'+edit.parent+'"') |
|
fromLink && fromLink.emit('editError', e) |
|
return cb(e) |
|
} |
|
} |
|
|
|
|
|
this.sanitizeEdit(edit, fromLink, function apply(er, edit) { |
|
if(er) { |
|
fromLink && fromLink.emit('editError', er) |
|
return cb(er) |
|
} |
|
|
|
// EditableDocuments are initialized asynchronously, we have to wait |
|
// for their callback event... |
|
if(!this.initialized) { |
|
this.once('editableInitialized', apply.bind(this, null, edit)) |
|
return |
|
} |
|
|
|
this.applyEdit(edit, false, function(er) { |
|
if(er) { |
|
fromLink && fromLink.emit('editError', er) |
|
return cb(er) |
|
} |
|
|
|
// add to history |
|
var content = this.ottype.serialize? |
|
this.ottype.serialize(this.content) |
|
: this.content |
|
this.history.storeSnapshot({ |
|
id: edit.id |
|
, contents: content |
|
, edit: edit |
|
, author: author |
|
}, function(er) { |
|
if(er) { |
|
this.emit('error', er) |
|
return cb(er) |
|
} |
|
|
|
// If I'm master then we need to queue the ack |
|
// Slaves have to send it straight away |
|
if(fromLink && fromLink === this.master) fromLink.send('ack' ,edit.id) |
|
else if (fromLink) fromLink.sendAck(edit.id) |
|
|
|
this.distributeEdit(edit, fromLink) |
|
this.emit('edit', edit) |
|
cb(null, edit) |
|
|
|
}.bind(this)) |
|
}.bind(this)) |
|
}.bind(this)) |
|
}.bind(this)) |
|
}.bind(this)) |
|
} |
|
|
|
/** |
|
* Returns an edit that is able to be applied |
|
*/ |
|
Document.prototype.sanitizeEdit = function(edit, fromLink, cb) { |
|
|
|
if(this.master === fromLink) { |
|
// We are not allowed to apply anything without consent from master anyway, |
|
// so we don't need to transform anything coming from master. |
|
cb(null, edit) |
|
}else { |
|
// We are master! |
|
|
|
// Transform against missed edits from history that have happened in the meantime |
|
this.history.getAllAfter(edit.parent, function(er, missed) { |
|
if(er) return cb(er) |
|
|
|
try { |
|
missed.forEach(function(oldSnapshot) { |
|
try { |
|
edit.follow(oldSnapshot.edit) |
|
}catch(e) { |
|
e.message = 'Transforming '+edit.id+' against '+oldSnapshot.edit.id+' failed: '+e.message |
|
throw e |
|
} |
|
}) |
|
}catch(e) { |
|
cb(e) |
|
return |
|
} |
|
|
|
if(missed.length > 10) { |
|
// this one apparently missed a lot of edits, looks like this is a reconnect |
|
missed.forEach(function(oldSnapshot) { |
|
fromLink.send('edit', oldSnapshot.edit.pack()) |
|
}) |
|
} |
|
|
|
cb(null, edit) |
|
}.bind(this)) |
|
} |
|
} |
|
|
|
Document.prototype.applyEdit = function(edit, ownEdit, cb) { |
|
// apply changes |
|
console.log('Document: apply edit', edit) |
|
try { |
|
this.content = edit.apply(this.content) |
|
cb() |
|
}catch(e) { |
|
e.message = 'Applying edit failed: '+e.message |
|
cb(e) |
|
} |
|
} |
|
|
|
Document.prototype.distributeEdit = function(edit, fromLink) { |
|
// forward edit |
|
this.links.forEach(function(link) { |
|
if(link === fromLink) return |
|
if(link === this.master) return |
|
|
|
link.sendEdit(edit) |
|
}.bind(this)) |
|
} |
|
|
|
},{"./Edit":24,"./History":26,"./Link":27,"events":49,"queue":32}],24:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
function Edit(ottype) { |
|
this.id |
|
this.changeset |
|
this.parent |
|
this.ottype = ottype |
|
if(!ottype) throw new Error('Edit: No ottype specified') |
|
} |
|
|
|
module.exports = Edit |
|
|
|
Edit.unpack = function(json, ottype) { |
|
json = JSON.parse(json) |
|
|
|
var edit = new Edit(ottype) |
|
edit.id = json.id |
|
if(json.cs) { |
|
edit.changeset = JSON.parse(json.cs) |
|
} |
|
edit.parent = json.parent |
|
return edit |
|
} |
|
|
|
/** |
|
* Returns an empty edit. |
|
*/ |
|
Edit.newInitial = function(ottype) { |
|
var edit = new Edit(ottype) |
|
edit.id = randString() |
|
return edit |
|
} |
|
|
|
/** |
|
* Returns an edit with the changes in cs |
|
*/ |
|
Edit.newFromChangeset = function(cs, ottype) { |
|
var edit = new Edit(ottype) |
|
edit.id = randString() |
|
edit.changeset = cs |
|
return edit |
|
} |
|
|
|
/** |
|
* Returns an edit overtaking every necessary attrib from the snapshot |
|
*/ |
|
Edit.fromSnapshot = function(s, ottype) { |
|
var edit = new Edit(ottype) |
|
edit.changeset = s.changes? JSON.parse(s.changes) : s.changes |
|
edit.id = s.id |
|
edit.parent = s.parent |
|
return edit |
|
} |
|
|
|
/** |
|
* Applies this edit on a snapshot |
|
*/ |
|
Edit.prototype.apply = function(snapshot) { |
|
if(!this.changeset) return snapshot |
|
return this.ottype.apply(snapshot, this.changeset) |
|
} |
|
|
|
/** |
|
* Transforms this edit to come after the passed |
|
* edit (rewrites history). |
|
*/ |
|
Edit.prototype.follow = function(edit, left) { |
|
if(this.parent != edit.parent) throw new Error('Trying to follow an edit that is not a direct sibling.') |
|
this.transformAgainst(edit, left) |
|
this.parent = edit.id |
|
} |
|
|
|
/** |
|
* Transforms the cahnges this edit makes against the ones that |
|
* the passed edit makes. This doesn't rewrite history, but manipulates silently. |
|
*/ |
|
Edit.prototype.transformAgainst = function(edit, left) { |
|
this.changeset = this.ottype.transform(this.changeset, edit.changeset, /*side:*/left?'left':'right') |
|
} |
|
|
|
Edit.prototype.merge = function(edit) { |
|
return Edit.newFromChangeset(this.ottype.compose(this.changeset, edit.changeset), this.ottype) |
|
} |
|
|
|
Edit.prototype.pack = function() { |
|
var o = { |
|
parent: this.parent |
|
, id: this.id |
|
} |
|
if(this.changeset) { |
|
o.cs = JSON.stringify(this.changeset) |
|
} |
|
return JSON.stringify(o) |
|
} |
|
|
|
Edit.prototype.clone = function() { |
|
var edit = new Edit(this.ottype) |
|
edit.id = this.id |
|
edit.parent = this.parent |
|
edit.changeset = this.changeset |
|
return edit |
|
} |
|
|
|
function randString() { |
|
var str = '' |
|
while (str.length < 20) { |
|
str += (Math.random()*1E7<<0x5).toString(36) |
|
} |
|
return str |
|
} |
|
module.exports.randString = randString |
|
|
|
},{}],25:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Document = require('./Document') |
|
, Edit = require('./Edit') |
|
|
|
function EditableDocument() { |
|
this.initialized = false |
|
Document.apply(this, arguments) |
|
} |
|
|
|
module.exports = EditableDocument |
|
|
|
EditableDocument.prototype = Object.create(Document.prototype, { constructor: { value: EditableDocument }}) |
|
|
|
// overrides Document#attachSlaveLink |
|
EditableDocument.prototype.attachSlaveLink = function() { |
|
// EditableDocuments can only have a master link! Nothing else, because we |
|
// need to take care of our own edits here, which are live! |
|
// -- we don't want to mess with other docs' edits! |
|
throw new Error('You can\'t attach a slave to an editable document!') |
|
} |
|
|
|
// overrides Document#receiveInit |
|
EditableDocument.prototype.receiveInit = function(data, fromLink) { |
|
var content = Document.prototype.receiveInit.call(this, data, fromLink) |
|
this.initialized = false |
|
this._setContents(content, function(er) { |
|
if(er) return this.emit('error', er) |
|
this.initialized = true |
|
this.emit('editableInitialized') |
|
}.bind(this)) |
|
} |
|
|
|
/** |
|
* Update is called when a modification has been made |
|
* |
|
* @param cs A changeset that can be swallowed by the ottype |
|
*/ |
|
EditableDocument.prototype.update = function(cs) { |
|
if(null === this.content) throw new Error('Document has not been initialized') |
|
|
|
var edit = Edit.newFromChangeset(cs, this.ottype) |
|
|
|
this.history.latest(function(er, latestSnapshot) { |
|
if(er) throw er |
|
|
|
edit.parent = latestSnapshot.edit.id |
|
|
|
this.emit('update', edit) |
|
|
|
// Merge into the queue for increased collab speed |
|
if(this.master.queue.length && 'edit' === this.master.queue[this.master.queue.length-1][0]) { |
|
var pendingEdit = this.master.queue.pop()[1] |
|
, parent = pendingEdit.parent |
|
, callback = pendingEdit.callback |
|
pendingEdit = pendingEdit.merge(edit) |
|
pendingEdit.callback = callback |
|
pendingEdit.parent = parent |
|
this.master.queue.push(['edit', pendingEdit]) |
|
return |
|
} |
|
|
|
this.master.sendEdit(edit, function onack(err, edit) { |
|
// Update queue |
|
this.master.queue.forEach(function(pending) { |
|
if('edit' === pending[0]) { |
|
pending[1].parent = edit.id |
|
} |
|
}) |
|
this.applyEdit(edit, true, function() { |
|
//this.distributeEdit(edit) // Unnecessary round trip |
|
this.history.storeSnapshot({contents: this.content, edit: edit}) |
|
}.bind(this)) |
|
}.bind(this)) |
|
}.bind(this)) |
|
} |
|
|
|
// overrides Document#applyEdit |
|
EditableDocument.prototype.applyEdit = function(edit, ownEdit, cb) { |
|
// apply changes |
|
console.log('EditableDocument: apply edit', edit, ownEdit) |
|
try { |
|
this.content = edit.apply(this.content) |
|
|
|
if(!ownEdit) { |
|
// Collect undetected local changes, before applying the new edit |
|
this._collectChanges(function(er) { |
|
if(er) return cb(er) |
|
|
|
// Transform against possibly missed edits that have happened in the meantime, |
|
// so that we can apply it |
|
|
|
var incoming = edit |
|
, incomingOriginal |
|
|
|
if(this.master.sentEdit) { |
|
incomingOriginal = incoming.clone() |
|
incoming.transformAgainst(this.master.sentEdit, true) |
|
this.master.sentEdit.follow(incomingOriginal) // Why!? So that our history is correct |
|
} |
|
|
|
incomingOriginal = incoming.clone() |
|
|
|
// transform incoming against pending |
|
this.master.queue.forEach(function(pending) { |
|
if('edit' === pending[0]) incoming.transformAgainst(pending[1], true) |
|
}) |
|
|
|
// Transform pending edits against the incoming one |
|
var first = true |
|
this.master.queue.forEach(function(pending) { |
|
if(pending[0] !== 'edit') return |
|
var pendingEdit = pending[1] |
|
if(first) { |
|
pendingEdit.follow(incomingOriginal) // transform + adjust parentage for the first in the line |
|
first = false |
|
} |
|
else { |
|
pendingEdit.transformAgainst(incomingOriginal) // all others have their predecessors as parents |
|
} |
|
|
|
incomingOriginal.transformAgainst(pendingEdit) |
|
}) |
|
this._change(incoming.changeset, cb) |
|
}.bind(this)) |
|
}else{ |
|
cb() |
|
} |
|
}catch(e) { |
|
e.message = 'Applying edit failed: '+e.message |
|
cb(e) |
|
} |
|
} |
|
|
|
},{"./Document":23,"./Edit":24}],26:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Edit = require('./Edit') |
|
// Stores revisions that are synced with the server |
|
function History(document) { |
|
this.document = document |
|
} |
|
module.exports = History |
|
|
|
History.prototype.reset = function() { |
|
if('function' == typeof this.document.adapter.reset) this.document.adapter.reset() |
|
} |
|
|
|
History.prototype.createDocument = function(snapshot, cb) { |
|
this.document.adapter.createDocument({ |
|
id: snapshot.edit.id |
|
, changes: JSON.stringify(snapshot.edit.changeset) |
|
, contents: snapshot.contents |
|
, author: snapshot.author |
|
}, cb) |
|
} |
|
|
|
History.prototype.earliest = function(cb) { |
|
this.document.adapter.getFirstSnapshot(this.document.id, function(er, snapshot) { |
|
if(er) return cb && cb(er) |
|
snapshot = { edit: Edit.fromSnapshot(snapshot, this.document.ottype) |
|
, contents: snapshot.contents |
|
} |
|
cb(null, snapshot) |
|
}.bind(this)) |
|
} |
|
|
|
History.prototype.latest = function(cb) { |
|
this.document.adapter.getLatestSnapshot(this.document.id, function(er, snapshot) { |
|
if(er) return cb && cb(er) |
|
if(!snapshot) return cb(new Error('No snapshot found')) |
|
snapshot = { edit: Edit.fromSnapshot(snapshot, this.document.ottype) |
|
, contents: snapshot.contents |
|
} |
|
cb(null, snapshot) |
|
}.bind(this)) |
|
} |
|
|
|
History.prototype.storeSnapshot = function(snapshot, cb) { |
|
this.latest(function(er, latest) { |
|
if(er) cb && cb(er) |
|
if(latest && latest.edit.id != snapshot.edit.parent) cb && cb(new Error('This edit\'s parent is not the latest edit in history: '+JSON.stringify(snapshot))) |
|
if(!snapshot.edit.id) snapshot.edit.id = Edit.randString() |
|
this.document.adapter.storeSnapshot(this.document.id, { |
|
id: snapshot.edit.id |
|
, changes: JSON.stringify(snapshot.edit.changeset) |
|
, parent: snapshot.edit.parent |
|
, contents: snapshot.contents |
|
, author: snapshot.author |
|
}, function(er) { |
|
cb && cb(er) |
|
}) |
|
}.bind(this)) |
|
} |
|
|
|
History.prototype.remembers = function(snapshotId, cb) { |
|
this.document.adapter.existsSnapshot(this.document.id, snapshotId, function(er, remembers) { |
|
cb && cb(er, remembers) |
|
}) |
|
} |
|
|
|
History.prototype.getAllAfter = function(snapshotId, cb) { |
|
this.document.adapter.getSnapshotsAfter(this.document.id, snapshotId, function(er, snapshots) { |
|
if(er) return cb && cb(er) |
|
snapshots = snapshots.map(function(snapshot) { |
|
return { edit: Edit.fromSnapshot(snapshot, this.document.ottype) |
|
, contents: snapshot.contents |
|
} |
|
}.bind(this)) |
|
cb && cb(er, snapshots) |
|
}.bind(this)) |
|
} |
|
|
|
},{"./Edit":24}],27:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
var Duplex = require('stream').Duplex |
|
require('setimmediate') |
|
var SECONDS = 1000 |
|
|
|
/** |
|
* This is a Link |
|
* The public should interact with it via node's streams API (ie. using .pipe etc.) |
|
* Internally, it emits events ("link:*") that are picked up by the Document it is attached to. |
|
*/ |
|
function Link (opts) { |
|
if(!opts) opts = {} |
|
this.timeout = opts.timeout || 10*SECONDS |
|
this.credentials = opts.credentials |
|
this.authenticateFn = opts.authenticate |
|
this.authorizeReadFn = opts.authorizeRead |
|
this.authorizeWriteFn = opts.authorizeWrite |
|
this.authenticated |
|
this.sentEdit |
|
this.sentRequestInit |
|
this.queue |
|
|
|
Duplex.call(this, {allowHalfOpen: false, objectMode: true}) |
|
|
|
this.on('error', function(er) { |
|
console.warn('Error in link', 'undefined'!=typeof window? er : er.stack || er) |
|
}.bind(this)) |
|
|
|
this.on('editError', function(er) { |
|
console.warn('EditError in link', 'undefined'!=typeof window? er : er.stack || er) |
|
}) |
|
|
|
this.reset() |
|
} |
|
Link.prototype = Object.create(Duplex.prototype, { constructor: { value: Link }}) |
|
|
|
module.exports = Link |
|
|
|
Link.prototype.reset = function() { |
|
this.queue = [] |
|
if(this.sentEdit) clearTimeout(this.sentEdit.timeout) |
|
this.sentEdit = null |
|
} |
|
|
|
/** |
|
* Pipeline an event |
|
* Please, Don't send edits with this method! Use .sendEdit() to queue it, like everyone else. |
|
*/ |
|
Link.prototype.send = function(event/*, args..*/) { |
|
if('requestInit' === event) this.sentRequestInit = true |
|
|
|
var msg = Array.prototype.slice.call(arguments) |
|
|
|
// Authorize message |
|
this.authorizeRead(msg, function(er, authorized) { |
|
if(er) return this.emit('error', er) |
|
|
|
// If unauthorized, tell them |
|
if(!authorized) return this.sendUnauthorized() |
|
|
|
// If this is an edit, add a timeout, after which we retry |
|
if('edit' === event) { |
|
var edit = msg[1] |
|
, cb = edit.callback |
|
edit.timeout = setTimeout(function() { |
|
this.send('edit', edit) |
|
}.bind(this), this.timeout) |
|
edit.callback = function() { |
|
clearTimeout(edit.timeout) |
|
cb && cb.apply(null, arguments) |
|
} |
|
msg[1] = edit.pack() |
|
} |
|
|
|
var data = JSON.stringify(msg) |
|
console.log('->', data) |
|
this.push(data) |
|
}.bind(this)) |
|
} |
|
|
|
Link.prototype.sendUnauthenticated = function() { |
|
this.push(JSON.stringify(['unauthenticated'])) |
|
} |
|
|
|
Link.prototype.sendAuthenticate = function() { |
|
this.push(JSON.stringify(['authenticate', this.credentials])) |
|
} |
|
|
|
Link.prototype.sendAuthenticated = function(status) { |
|
this.push(JSON.stringify(['authenticated', status])) |
|
} |
|
|
|
Link.prototype.sendUnauthorized = function() { |
|
this.push(JSON.stringify(['unauthorized'])) |
|
} |
|
|
|
/* |
|
* Put an edit into the queue |
|
* @param edit {Edit} the edit to send through this link |
|
* @param cb {Function} Get callback when the edit has been acknowledged (optional) |
|
*/ |
|
Link.prototype.sendEdit = function(edit, cb) { |
|
if(cb) edit.callback = cb |
|
|
|
if(this.queue.length || this.sentEdit) { |
|
this.queue.push(['edit', edit]) |
|
} |
|
else { |
|
this.sentEdit = edit |
|
this.send('edit', edit) |
|
} |
|
} |
|
|
|
Link.prototype.sendAck = function(editId) { |
|
if(this.queue.length || this.sentEdit) { |
|
this.queue.push(['ack', editId]) |
|
} |
|
else { |
|
this.send('ack', editId) |
|
} |
|
} |
|
|
|
// This is only used to push edits from the queue into the pipeline. |
|
// All other events are pushed directly in .send() |
|
Link.prototype._read = function() { |
|
if(this.sentEdit) return |
|
if(!this.queue[0]) return |
|
var msg |
|
while(msg = this.queue.shift()) { |
|
if('edit' === msg[0]) { |
|
this.sentEdit = msg[1] |
|
} |
|
|
|
this.send.apply(this, msg) |
|
if('edit' === msg[0]) break |
|
} |
|
} |
|
|
|
Link.prototype._write = function(buf, enc, cb) { |
|
console.log('<- _write:', buf.toString()) |
|
var args = JSON.parse(buf.toString()) |
|
|
|
// ['authenticate', Mixed] |
|
if(args[0] === 'authenticate') { |
|
this.authenticate(args[1], function(er, authed) { |
|
this.authenticated = authed |
|
this.sendAuthenticated(!!(!er && authed)) |
|
cb() |
|
}.bind(this)) |
|
return |
|
} |
|
|
|
// ['authenticated', Bool] |
|
if(args[0] === 'authenticated') { |
|
if(!args[1]) return this.emit('error', new Error('Authentication failed')) |
|
if(this.sentRequestInit) this.send('requestInit') |
|
else if(this.sentEdit) this.send('edit', this.sentEdit) |
|
cb() |
|
return |
|
} |
|
|
|
// ['unauthenticated'] |
|
if(args[0] === 'unauthenticated') { |
|
this.sendAuthenticate() |
|
cb() |
|
return |
|
} |
|
|
|
// ['unauthorized'] |
|
if(args[0] === 'unauthorized') { |
|
this.send('requestInit') |
|
cb() |
|
return |
|
} |
|
|
|
if(!this.authenticated && this.authenticateFn) { |
|
this.sendUnauthenticated() |
|
cb() |
|
return |
|
} |
|
|
|
if(args[0] === 'init') { |
|
this.sentRequestInit = false |
|
} |
|
|
|
this.authorizeWrite(args, function(er, authorized) { |
|
|
|
if(er) this.emit('error', er) |
|
if(!authorized) { |
|
this.sendUnauthorized() |
|
cb() |
|
return |
|
} |
|
|
|
// Intercept acks for shifting the queue and calling callbacks |
|
if(args[0] == 'ack') { |
|
var id = args[1] |
|
|
|
if(this.sentEdit && typeof(this.sentEdit.callback) == 'function') { |
|
// Callback |
|
this.sentEdit.id = id |
|
// The nextTick shim for browsers doesn't seem to enforce the call order |
|
// (_read is called below and they must be in that order), so we call directly |
|
//nextTick(this.sentEdit.callback.bind(null, null, this.sentEdit)) |
|
try { |
|
this.sentEdit.callback(null, this.sentEdit) |
|
}catch(e) { |
|
this.emit('error', e) |
|
} |
|
delete this.sentEdit.callback |
|
} |
|
this.sentEdit = null |
|
|
|
setImmediate(function() { |
|
this._read(0) |
|
}.bind(this)) |
|
} |
|
|
|
args[0] = 'link:'+args[0] |
|
this.emit.apply(this, args) |
|
cb() |
|
}.bind(this)) |
|
} |
|
|
|
Link.prototype.authenticate = function(credentials, cb) { |
|
this.authenticateFn(credentials, cb) |
|
} |
|
|
|
Link.prototype.authorizeWrite = function(msg, cb) { |
|
if(!this.authorizeWriteFn) return cb(null, true) |
|
this.authorizeWriteFn(msg, this.authenticated, cb) |
|
} |
|
|
|
Link.prototype.authorizeRead = function(msg, cb) { |
|
if(!this.authorizeReadFn) return cb(null, true) |
|
this.authorizeReadFn(msg, this.authenticated, cb) |
|
} |
|
|
|
},{"setimmediate":33,"stream":66}],28:[function(require,module,exports){ |
|
/** |
|
* gulf - Sync anything! |
|
* Copyright (C) 2013-2015 Marcel Klehr <[email protected]> |
|
* |
|
* This program is free software: you can redistribute it and/or modify |
|
* it under the terms of the GNU Lesser General Public License as published by |
|
* the Free Software Foundation, either version 3 of the License, or |
|
* (at your option) any later version. |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU Lesser General Public License |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
*/ |
|
// Stores revisions that are synced with the server |
|
function MemoryAdapter() { |
|
this.reset() |
|
} |
|
module.exports = MemoryAdapter |
|
|
|
MemoryAdapter.prototype.createDocument = function(initialSnapshot, cb) { |
|
this.reset() |
|
this.storeSnapshot(null, initialSnapshot, cb) |
|
} |
|
|
|
MemoryAdapter.prototype.getFirstSnapshot = function(docId, cb) { |
|
if(!this.history.length) return cb() |
|
var snapshot = this.snapshots[this.history[0]] |
|
snapshot = JSON.parse(JSON.stringify(snapshot)) |
|
cb(null, snapshot) |
|
} |
|
|
|
MemoryAdapter.prototype.getLatestSnapshot = function(docId, cb) { |
|
if(!this.history.length) return cb() |
|
var snapshot = this.snapshots[this.history[this.history.length-1]] |
|
snapshot = JSON.parse(JSON.stringify(snapshot)) |
|
cb(null, snapshot) |
|
} |
|
|
|
MemoryAdapter.prototype.storeSnapshot = function(docId, snapshot, cb) { |
|
this.history.push(snapshot.id) |
|
this.snapshots[snapshot.id] = JSON.parse(JSON.stringify(snapshot)) |
|
cb() |
|
} |
|
|
|
MemoryAdapter.prototype.reset = function() { |
|
this.snapshots = {} |
|
this.history = [] |
|
} |
|
|
|
MemoryAdapter.prototype.existsSnapshot = function(docId, editId, cb) { |
|
cb(null, !!this.snapshots[editId]) |
|
} |
|
|
|
MemoryAdapter.prototype.getSnapshotsAfter = function(docId, editId, cb) { |
|
var arr = [] |
|
for(var i = this.history.indexOf(editId)+1; i < this.history.length; i++) { |
|
arr.push(JSON.parse(JSON.stringify(this.snapshots[this.history[i]]))) |
|
} |
|
cb(null, arr) |
|
} |
|
|
|
},{}],29:[function(require,module,exports){ |
|
if (typeof Object.create === 'function') { |
|
// implementation from standard node.js 'util' module |
|
module.exports = function inherits(ctor, superCtor) { |
|
ctor.super_ = superCtor |
|
ctor.prototype = Object.create(superCtor.prototype, { |
|
constructor: { |
|
value: ctor, |
|
enumerable: false, |
|
writable: true, |
|
configurable: true |
|
} |
|
}); |
|
}; |
|
} else { |
|
// old school shim for old browsers |
|
module.exports = function inherits(ctor, superCtor) { |
|
ctor.super_ = superCtor |
|
var TempCtor = function () {} |
|
TempCtor.prototype = superCtor.prototype |
|
ctor.prototype = new TempCtor() |
|
ctor.prototype.constructor = ctor |
|
} |
|
} |
|
|
|
},{}],30:[function(require,module,exports){ |
|
// Copyright 2011 Google Inc. |
|
// |
|
// Licensed under the Apache License, Version 2.0 (the "License"); |
|
// you may not use this file except in compliance with the License. |
|
// You may obtain a copy of the License at |
|
// |
|
// http://www.apache.org/licenses/LICENSE-2.0 |
|
// |
|
// Unless required by applicable law or agreed to in writing, software |
|
// distributed under the License is distributed on an "AS IS" BASIS, |
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
// See the License for the specific language governing permissions and |
|
// limitations under the License. |
|
var __extends = this.__extends || function (d, b) { |
|
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; |
|
function __() { this.constructor = d; } |
|
__.prototype = b.prototype; |
|
d.prototype = new __(); |
|
}; |
|
var MutationObserverCtor; |
|
if (typeof WebKitMutationObserver !== 'undefined') |
|
MutationObserverCtor = WebKitMutationObserver; |
|
else |
|
MutationObserverCtor = MutationObserver; |
|
|
|
if (MutationObserverCtor === undefined) { |
|
console.error('DOM Mutation Observers are required.'); |
|
console.error('https://developer.mozilla.org/en-US/docs/DOM/MutationObserver'); |
|
throw Error('DOM Mutation Observers are required'); |
|
} |
|
|
|
var NodeMap = (function () { |
|
function NodeMap() { |
|
this.nodes = []; |
|
this.values = []; |
|
} |
|
NodeMap.prototype.isIndex = function (s) { |
|
return +s === s >>> 0; |
|
}; |
|
|
|
NodeMap.prototype.nodeId = function (node) { |
|
var id = node[NodeMap.ID_PROP]; |
|
if (!id) |
|
id = node[NodeMap.ID_PROP] = NodeMap.nextId_++; |
|
return id; |
|
}; |
|
|
|
NodeMap.prototype.set = function (node, value) { |
|
var id = this.nodeId(node); |
|
this.nodes[id] = node; |
|
this.values[id] = value; |
|
}; |
|
|
|
NodeMap.prototype.get = function (node) { |
|
var id = this.nodeId(node); |
|
return this.values[id]; |
|
}; |
|
|
|
NodeMap.prototype.has = function (node) { |
|
return this.nodeId(node) in this.nodes; |
|
}; |
|
|
|
NodeMap.prototype.delete = function (node) { |
|
var id = this.nodeId(node); |
|
delete this.nodes[id]; |
|
this.values[id] = undefined; |
|
}; |
|
|
|
NodeMap.prototype.keys = function () { |
|
var nodes = []; |
|
for (var id in this.nodes) { |
|
if (!this.isIndex(id)) |
|
continue; |
|
nodes.push(this.nodes[id]); |
|
} |
|
|
|
return nodes; |
|
}; |
|
NodeMap.ID_PROP = '__mutation_summary_node_map_id__'; |
|
NodeMap.nextId_ = 1; |
|
return NodeMap; |
|
})(); |
|
|
|
/** |
|
* var reachableMatchableProduct = [ |
|
* // STAYED_OUT, ENTERED, STAYED_IN, EXITED |
|
* [ STAYED_OUT, STAYED_OUT, STAYED_OUT, STAYED_OUT ], // STAYED_OUT |
|
* [ STAYED_OUT, ENTERED, ENTERED, STAYED_OUT ], // ENTERED |
|
* [ STAYED_OUT, ENTERED, STAYED_IN, EXITED ], // STAYED_IN |
|
* [ STAYED_OUT, STAYED_OUT, EXITED, EXITED ] // EXITED |
|
* ]; |
|
*/ |
|
var Movement; |
|
(function (Movement) { |
|
Movement[Movement["STAYED_OUT"] = 0] = "STAYED_OUT"; |
|
Movement[Movement["ENTERED"] = 1] = "ENTERED"; |
|
Movement[Movement["STAYED_IN"] = 2] = "STAYED_IN"; |
|
Movement[Movement["REPARENTED"] = 3] = "REPARENTED"; |
|
Movement[Movement["REORDERED"] = 4] = "REORDERED"; |
|
Movement[Movement["EXITED"] = 5] = "EXITED"; |
|
})(Movement || (Movement = {})); |
|
|
|
function enteredOrExited(changeType) { |
|
return changeType === 1 /* ENTERED */ || changeType === 5 /* EXITED */; |
|
} |
|
|
|
var NodeChange = (function () { |
|
function NodeChange(node, childList, attributes, characterData, oldParentNode, added, attributeOldValues, characterDataOldValue) { |
|
if (typeof childList === "undefined") { childList = false; } |
|
if (typeof attributes === "undefined") { attributes = false; } |
|
if (typeof characterData === "undefined") { characterData = false; } |
|
if (typeof oldParentNode === "undefined") { oldParentNode = null; } |
|
if (typeof added === "undefined") { added = false; } |
|
if (typeof attributeOldValues === "undefined") { attributeOldValues = null; } |
|
if (typeof characterDataOldValue === "undefined") { characterDataOldValue = null; } |
|
this.node = node; |
|
this.childList = childList; |
|
this.attributes = attributes; |
|
this.characterData = characterData; |
|
this.oldParentNode = oldParentNode; |
|
this.added = added; |
|
this.attributeOldValues = attributeOldValues; |
|
this.characterDataOldValue = characterDataOldValue; |
|
this.isCaseInsensitive = this.node.nodeType === Node.ELEMENT_NODE && this.node instanceof HTMLElement && this.node.ownerDocument instanceof HTMLDocument; |
|
} |
|
NodeChange.prototype.getAttributeOldValue = function (name) { |
|
if (!this.attributeOldValues) |
|
return undefined; |
|
if (this.isCaseInsensitive) |
|
name = name.toLowerCase(); |
|
return this.attributeOldValues[name]; |
|
}; |
|
|
|
NodeChange.prototype.getAttributeNamesMutated = function () { |
|
var names = []; |
|
if (!this.attributeOldValues) |
|
return names; |
|
for (var name in this.attributeOldValues) { |
|
names.push(name); |
|
} |
|
return names; |
|
}; |
|
|
|
NodeChange.prototype.attributeMutated = function (name, oldValue) { |
|
this.attributes = true; |
|
this.attributeOldValues = this.attributeOldValues || {}; |
|
|
|
if (name in this.attributeOldValues) |
|
return; |
|
|
|
this.attributeOldValues[name] = oldValue; |
|
}; |
|
|
|
NodeChange.prototype.characterDataMutated = function (oldValue) { |
|
if (this.characterData) |
|
return; |
|
this.characterData = true; |
|
this.characterDataOldValue = oldValue; |
|
}; |
|
|
|
// Note: is it possible to receive a removal followed by a removal. This |
|
// can occur if the removed node is added to an non-observed node, that |
|
// node is added to the observed area, and then the node removed from |
|
// it. |
|
NodeChange.prototype.removedFromParent = function (parent) { |
|
this.childList = true; |
|
if (this.added || this.oldParentNode) |
|
this.added = false; |
|
else |
|
this.oldParentNode = parent; |
|
}; |
|
|
|
NodeChange.prototype.insertedIntoParent = function () { |
|
this.childList = true; |
|
this.added = true; |
|
}; |
|
|
|
// An node's oldParent is |
|
// -its present parent, if its parentNode was not changed. |
|
// -null if the first thing that happened to it was an add. |
|
// -the node it was removed from if the first thing that happened to it |
|
// was a remove. |
|
NodeChange.prototype.getOldParent = function () { |
|
if (this.childList) { |
|
if (this.oldParentNode) |
|
return this.oldParentNode; |
|
if (this.added) |
|
return null; |
|
} |
|
|
|
return this.node.parentNode; |
|
}; |
|
return NodeChange; |
|
})(); |
|
|
|
var ChildListChange = (function () { |
|
function ChildListChange() { |
|
this.added = new NodeMap(); |
|
this.removed = new NodeMap(); |
|
this.maybeMoved = new NodeMap(); |
|
this.oldPrevious = new NodeMap(); |
|
this.moved = undefined; |
|
} |
|
return ChildListChange; |
|
})(); |
|
|
|
var TreeChanges = (function (_super) { |
|
__extends(TreeChanges, _super); |
|
function TreeChanges(rootNode, mutations) { |
|
_super.call(this); |
|
|
|
this.rootNode = rootNode; |
|
this.reachableCache = undefined; |
|
this.wasReachableCache = undefined; |
|
this.anyParentsChanged = false; |
|
this.anyAttributesChanged = false; |
|
this.anyCharacterDataChanged = false; |
|
|
|
for (var m = 0; m < mutations.length; m++) { |
|
var mutation = mutations[m]; |
|
switch (mutation.type) { |
|
case 'childList': |
|
this.anyParentsChanged = true; |
|
for (var i = 0; i < mutation.removedNodes.length; i++) { |
|
var node = mutation.removedNodes[i]; |
|
this.getChange(node).removedFromParent(mutation.target); |
|
} |
|
for (var i = 0; i < mutation.addedNodes.length; i++) { |
|
var node = mutation.addedNodes[i]; |
|
this.getChange(node).insertedIntoParent(); |
|
} |
|
break; |
|
|
|
case 'attributes': |
|
this.anyAttributesChanged = true; |
|
var change = this.getChange(mutation.target); |
|
change.attributeMutated(mutation.attributeName, mutation.oldValue); |
|
break; |
|
|
|
case 'characterData': |
|
this.anyCharacterDataChanged = true; |
|
var change = this.getChange(mutation.target); |
|
change.characterDataMutated(mutation.oldValue); |
|
break; |
|
} |
|
} |
|
} |
|
TreeChanges.prototype.getChange = function (node) { |
|
var change = this.get(node); |
|
if (!change) { |
|
change = new NodeChange(node); |
|
this.set(node, change); |
|
} |
|
return change; |
|
}; |
|
|
|
TreeChanges.prototype.getOldParent = function (node) { |
|
var change = this.get(node); |
|
return change ? change.getOldParent() : node.parentNode; |
|
}; |
|
|
|
TreeChanges.prototype.getIsReachable = function (node) { |
|
if (node === this.rootNode) |
|
return true; |
|
if (!node) |
|
return false; |
|
|
|
this.reachableCache = this.reachableCache || new NodeMap(); |
|
var isReachable = this.reachableCache.get(node); |
|
if (isReachable === undefined) { |
|
isReachable = this.getIsReachable(node.parentNode); |
|
this.reachableCache.set(node, isReachable); |
|
} |
|
return isReachable; |
|
}; |
|
|
|
// A node wasReachable if its oldParent wasReachable. |
|
TreeChanges.prototype.getWasReachable = function (node) { |
|
if (node === this.rootNode) |
|
return true; |
|
if (!node) |
|
return false; |
|
|
|
this.wasReachableCache = this.wasReachableCache || new NodeMap(); |
|
var wasReachable = this.wasReachableCache.get(node); |
|
if (wasReachable === undefined) { |
|
wasReachable = this.getWasReachable(this.getOldParent(node)); |
|
this.wasReachableCache.set(node, wasReachable); |
|
} |
|
return wasReachable; |
|
}; |
|
|
|
TreeChanges.prototype.reachabilityChange = function (node) { |
|
if (this.getIsReachable(node)) { |
|
return this.getWasReachable(node) ? 2 /* STAYED_IN */ : 1 /* ENTERED */; |
|
} |
|
|
|
return this.getWasReachable(node) ? 5 /* EXITED */ : 0 /* STAYED_OUT */; |
|
}; |
|
return TreeChanges; |
|
})(NodeMap); |
|
|
|
var MutationProjection = (function () { |
|
// TOOD(any) |
|
function MutationProjection(rootNode, mutations, selectors, calcReordered, calcOldPreviousSibling) { |
|
this.rootNode = rootNode; |
|
this.mutations = mutations; |
|
this.selectors = selectors; |
|
this.calcReordered = calcReordered; |
|
this.calcOldPreviousSibling = calcOldPreviousSibling; |
|
this.treeChanges = new TreeChanges(rootNode, mutations); |
|
this.entered = []; |
|
this.exited = []; |
|
this.stayedIn = new NodeMap(); |
|
this.visited = new NodeMap(); |
|
this.childListChangeMap = undefined; |
|
this.characterDataOnly = undefined; |
|
this.matchCache = undefined; |
|
|
|
this.processMutations(); |
|
} |
|
MutationProjection.prototype.processMutations = function () { |
|
if (!this.treeChanges.anyParentsChanged && !this.treeChanges.anyAttributesChanged) |
|
return; |
|
|
|
var changedNodes = this.treeChanges.keys(); |
|
for (var i = 0; i < changedNodes.length; i++) { |
|
this.visitNode(changedNodes[i], undefined); |
|
} |
|
}; |
|
|
|
MutationProjection.prototype.visitNode = function (node, parentReachable) { |
|
if (this.visited.has(node)) |
|
return; |
|
|
|
this.visited.set(node, true); |
|
|
|
var change = this.treeChanges.get(node); |
|
var reachable = parentReachable; |
|
|
|
// node inherits its parent's reachability change unless |
|
// its parentNode was mutated. |
|
if ((change && change.childList) || reachable == undefined) |
|
reachable = this.treeChanges.reachabilityChange(node); |
|
|
|
if (reachable === 0 /* STAYED_OUT */) |
|
return; |
|
|
|
// Cache match results for sub-patterns. |
|
this.matchabilityChange(node); |
|
|
|
if (reachable === 1 /* ENTERED */) { |
|
this.entered.push(node); |
|
} else if (reachable === 5 /* EXITED */) { |
|
this.exited.push(node); |
|
this.ensureHasOldPreviousSiblingIfNeeded(node); |
|
} else if (reachable === 2 /* STAYED_IN */) { |
|
var movement = 2 /* STAYED_IN */; |
|
|
|
if (change && change.childList) { |
|
if (change.oldParentNode !== node.parentNode) { |
|
movement = 3 /* REPARENTED */; |
|
this.ensureHasOldPreviousSiblingIfNeeded(node); |
|
} else if (this.calcReordered && this.wasReordered(node)) { |
|
movement = 4 /* REORDERED */; |
|
} |
|
} |
|
|
|
this.stayedIn.set(node, movement); |
|
} |
|
|
|
if (reachable === 2 /* STAYED_IN */) |
|
return; |
|
|
|
for (var child = node.firstChild; child; child = child.nextSibling) { |
|
this.visitNode(child, reachable); |
|
} |
|
}; |
|
|
|
MutationProjection.prototype.ensureHasOldPreviousSiblingIfNeeded = function (node) { |
|
if (!this.calcOldPreviousSibling) |
|
return; |
|
|
|
this.processChildlistChanges(); |
|
|
|
var parentNode = node.parentNode; |
|
var nodeChange = this.treeChanges.get(node); |
|
if (nodeChange && nodeChange.oldParentNode) |
|
parentNode = nodeChange.oldParentNode; |
|
|
|
var change = this.childListChangeMap.get(parentNode); |
|
if (!change) { |
|
change = new ChildListChange(); |
|
this.childListChangeMap.set(parentNode, change); |
|
} |
|
|
|
if (!change.oldPrevious.has(node)) { |
|
change.oldPrevious.set(node, node.previousSibling); |
|
} |
|
}; |
|
|
|
MutationProjection.prototype.getChanged = function (summary, selectors, characterDataOnly) { |
|
this.selectors = selectors; |
|
this.characterDataOnly = characterDataOnly; |
|
|
|
for (var i = 0; i < this.entered.length; i++) { |
|
var node = this.entered[i]; |
|
var matchable = this.matchabilityChange(node); |
|
if (matchable === 1 /* ENTERED */ || matchable === 2 /* STAYED_IN */) |
|
summary.added.push(node); |
|
} |
|
|
|
var stayedInNodes = this.stayedIn.keys(); |
|
for (var i = 0; i < stayedInNodes.length; i++) { |
|
var node = stayedInNodes[i]; |
|
var matchable = this.matchabilityChange(node); |
|
|
|
if (matchable === 1 /* ENTERED */) { |
|
summary.added.push(node); |
|
} else if (matchable === 5 /* EXITED */) { |
|
summary.removed.push(node); |
|
} else if (matchable === 2 /* STAYED_IN */ && (summary.reparented || summary.reordered)) { |
|
var movement = this.stayedIn.get(node); |
|
if (summary.reparented && movement === 3 /* REPARENTED */) |
|
summary.reparented.push(node); |
|
else if (summary.reordered && movement === 4 /* REORDERED */) |
|
summary.reordered.push(node); |
|
} |
|
} |
|
|
|
for (var i = 0; i < this.exited.length; i++) { |
|
var node = this.exited[i]; |
|
var matchable = this.matchabilityChange(node); |
|
if (matchable === 5 /* EXITED */ || matchable === 2 /* STAYED_IN */) |
|
summary.removed.push(node); |
|
} |
|
}; |
|
|
|
MutationProjection.prototype.getOldParentNode = function (node) { |
|
var change = this.treeChanges.get(node); |
|
if (change && change.childList) |
|
return change.oldParentNode ? change.oldParentNode : null; |
|
|
|
var reachabilityChange = this.treeChanges.reachabilityChange(node); |
|
if (reachabilityChange === 0 /* STAYED_OUT */ || reachabilityChange === 1 /* ENTERED */) |
|
throw Error('getOldParentNode requested on invalid node.'); |
|
|
|
return node.parentNode; |
|
}; |
|
|
|
MutationProjection.prototype.getOldPreviousSibling = function (node) { |
|
var parentNode = node.parentNode; |
|
var nodeChange = this.treeChanges.get(node); |
|
if (nodeChange && nodeChange.oldParentNode) |
|
parentNode = nodeChange.oldParentNode; |
|
|
|
var change = this.childListChangeMap.get(parentNode); |
|
if (!change) |
|
throw Error('getOldPreviousSibling requested on invalid node.'); |
|
|
|
return change.oldPrevious.get(node); |
|
}; |
|
|
|
MutationProjection.prototype.getOldAttribute = function (element, attrName) { |
|
var change = this.treeChanges.get(element); |
|
if (!change || !change.attributes) |
|
throw Error('getOldAttribute requested on invalid node.'); |
|
|
|
var value = change.getAttributeOldValue(attrName); |
|
if (value === undefined) |
|
throw Error('getOldAttribute requested for unchanged attribute name.'); |
|
|
|
return value; |
|
}; |
|
|
|
MutationProjection.prototype.attributeChangedNodes = function (includeAttributes) { |
|
if (!this.treeChanges.anyAttributesChanged) |
|
return {}; |
|
|
|
var attributeFilter; |
|
var caseInsensitiveFilter; |
|
if (includeAttributes) { |
|
attributeFilter = {}; |
|
caseInsensitiveFilter = {}; |
|
for (var i = 0; i < includeAttributes.length; i++) { |
|
var attrName = includeAttributes[i]; |
|
attributeFilter[attrName] = true; |
|
caseInsensitiveFilter[attrName.toLowerCase()] = attrName; |
|
} |
|
} |
|
|
|
var result = {}; |
|
var nodes = this.treeChanges.keys(); |
|
|
|
for (var i = 0; i < nodes.length; i++) { |
|
var node = nodes[i]; |
|
|
|
var change = this.treeChanges.get(node); |
|
if (!change.attributes) |
|
continue; |
|
|
|
if (2 /* STAYED_IN */ !== this.treeChanges.reachabilityChange(node) || 2 /* STAYED_IN */ !== this.matchabilityChange(node)) { |
|
continue; |
|
} |
|
|
|
var element = node; |
|
var changedAttrNames = change.getAttributeNamesMutated(); |
|
for (var j = 0; j < changedAttrNames.length; j++) { |
|
var attrName = changedAttrNames[j]; |
|
|
|
if (attributeFilter && !attributeFilter[attrName] && !(change.isCaseInsensitive && caseInsensitiveFilter[attrName])) { |
|
continue; |
|
} |
|
|
|
var oldValue = change.getAttributeOldValue(attrName); |
|
if (oldValue === element.getAttribute(attrName)) |
|
continue; |
|
|
|
if (caseInsensitiveFilter && change.isCaseInsensitive) |
|
attrName = caseInsensitiveFilter[attrName]; |
|
|
|
result[attrName] = result[attrName] || []; |
|
result[attrName].push(element); |
|
} |
|
} |
|
|
|
return result; |
|
}; |
|
|
|
MutationProjection.prototype.getOldCharacterData = function (node) { |
|
var change = this.treeChanges.get(node); |
|
if (!change || !change.characterData) |
|
throw Error('getOldCharacterData requested on invalid node.'); |
|
|
|
return change.characterDataOldValue; |
|
}; |
|
|
|
MutationProjection.prototype.getCharacterDataChanged = function () { |
|
if (!this.treeChanges.anyCharacterDataChanged) |
|
return []; |
|
|
|
var nodes = this.treeChanges.keys(); |
|
var result = []; |
|
for (var i = 0; i < nodes.length; i++) { |
|
var target = nodes[i]; |
|
if (2 /* STAYED_IN */ !== this.treeChanges.reachabilityChange(target)) |
|
continue; |
|
|
|
var change = this.treeChanges.get(target); |
|
if (!change.characterData || target.textContent == change.characterDataOldValue) |
|
continue; |
|
|
|
result.push(target); |
|
} |
|
|
|
return result; |
|
}; |
|
|
|
MutationProjection.prototype.computeMatchabilityChange = function (selector, el) { |
|
if (!this.matchCache) |
|
this.matchCache = []; |
|
if (!this.matchCache[selector.uid]) |
|
this.matchCache[selector.uid] = new NodeMap(); |
|
|
|
var cache = this.matchCache[selector.uid]; |
|
var result = cache.get(el); |
|
if (result === undefined) { |
|
result = selector.matchabilityChange(el, this.treeChanges.get(el)); |
|
cache.set(el, result); |
|
} |
|
return result; |
|
}; |
|
|
|
MutationProjection.prototype.matchabilityChange = function (node) { |
|
var _this = this; |
|
// TODO(rafaelw): Include PI, CDATA? |
|
// Only include text nodes. |
|
if (this.characterDataOnly) { |
|
switch (node.nodeType) { |
|
case Node.COMMENT_NODE: |
|
case Node.TEXT_NODE: |
|
return 2 /* STAYED_IN */; |
|
default: |
|
return 0 /* STAYED_OUT */; |
|
} |
|
} |
|
|
|
// No element filter. Include all nodes. |
|
if (!this.selectors) |
|
return 2 /* STAYED_IN */; |
|
|
|
// Element filter. Exclude non-elements. |
|
if (node.nodeType !== Node.ELEMENT_NODE) |
|
return 0 /* STAYED_OUT */; |
|
|
|
var el = node; |
|
|
|
var matchChanges = this.selectors.map(function (selector) { |
|
return _this.computeMatchabilityChange(selector, el); |
|
}); |
|
|
|
var accum = 0 /* STAYED_OUT */; |
|
var i = 0; |
|
|
|
while (accum !== 2 /* STAYED_IN */ && i < matchChanges.length) { |
|
switch (matchChanges[i]) { |
|
case 2 /* STAYED_IN */: |
|
accum = 2 /* STAYED_IN */; |
|
break; |
|
case 1 /* ENTERED */: |
|
if (accum === 5 /* EXITED */) |
|
accum = 2 /* STAYED_IN */; |
|
else |
|
accum = 1 /* ENTERED */; |
|
break; |
|
case 5 /* EXITED */: |
|
if (accum === 1 /* ENTERED */) |
|
accum = 2 /* STAYED_IN */; |
|
else |
|
accum = 5 /* EXITED */; |
|
break; |
|
} |
|
|
|
i++; |
|
} |
|
|
|
return accum; |
|
}; |
|
|
|
MutationProjection.prototype.getChildlistChange = function (el) { |
|
var change = this.childListChangeMap.get(el); |
|
if (!change) { |
|
change = new ChildListChange(); |
|
this.childListChangeMap.set(el, change); |
|
} |
|
|
|
return change; |
|
}; |
|
|
|
MutationProjection.prototype.processChildlistChanges = function () { |
|
if (this.childListChangeMap) |
|
return; |
|
|
|
this.childListChangeMap = new NodeMap(); |
|
|
|
for (var i = 0; i < this.mutations.length; i++) { |
|
var mutation = this.mutations[i]; |
|
if (mutation.type != 'childList') |
|
continue; |
|
|
|
if (this.treeChanges.reachabilityChange(mutation.target) !== 2 /* STAYED_IN */ && !this.calcOldPreviousSibling) |
|
continue; |
|
|
|
var change = this.getChildlistChange(mutation.target); |
|
|
|
var oldPrevious = mutation.previousSibling; |
|
|
|
function recordOldPrevious(node, previous) { |
|
if (!node || change.oldPrevious.has(node) || change.added.has(node) || change.maybeMoved.has(node)) |
|
return; |
|
|
|
if (previous && (change.added.has(previous) || change.maybeMoved.has(previous))) |
|
return; |
|
|
|
change.oldPrevious.set(node, previous); |
|
} |
|
|
|
for (var j = 0; j < mutation.removedNodes.length; j++) { |
|
var node = mutation.removedNodes[j]; |
|
recordOldPrevious(node, oldPrevious); |
|
|
|
if (change.added.has(node)) { |
|
change.added.delete(node); |
|
} else { |
|
change.removed.set(node, true); |
|
change.maybeMoved.delete(node); |
|
} |
|
|
|
oldPrevious = node; |
|
} |
|
|
|
recordOldPrevious(mutation.nextSibling, oldPrevious); |
|
|
|
for (var j = 0; j < mutation.addedNodes.length; j++) { |
|
var node = mutation.addedNodes[j]; |
|
if (change.removed.has(node)) { |
|
change.removed.delete(node); |
|
change.maybeMoved.set(node, true); |
|
} else { |
|
change.added.set(node, true); |
|
} |
|
} |
|
} |
|
}; |
|
|
|
MutationProjection.prototype.wasReordered = function (node) { |
|
if (!this.treeChanges.anyParentsChanged) |
|
return false; |
|
|
|
this.processChildlistChanges(); |
|
|
|
var parentNode = node.parentNode; |
|
var nodeChange = this.treeChanges.get(node); |
|
if (nodeChange && nodeChange.oldParentNode) |
|
parentNode = nodeChange.oldParentNode; |
|
|
|
var change = this.childListChangeMap.get(parentNode); |
|
if (!change) |
|
return false; |
|
|
|
if (change.moved) |
|
return change.moved.get(node); |
|
|
|
change.moved = new NodeMap(); |
|
var pendingMoveDecision = new NodeMap(); |
|
|
|
function isMoved(node) { |
|
if (!node) |
|
return false; |
|
if (!change.maybeMoved.has(node)) |
|
return false; |
|
|
|
var didMove = change.moved.get(node); |
|
if (didMove !== undefined) |
|
return didMove; |
|
|
|
if (pendingMoveDecision.has(node)) { |
|
didMove = true; |
|
} else { |
|
pendingMoveDecision.set(node, true); |
|
didMove = getPrevious(node) !== getOldPrevious(node); |
|
} |
|
|
|
if (pendingMoveDecision.has(node)) { |
|
pendingMoveDecision.delete(node); |
|
change.moved.set(node, didMove); |
|
} else { |
|
didMove = change.moved.get(node); |
|
} |
|
|
|
return didMove; |
|
} |
|
|
|
var oldPreviousCache = new NodeMap(); |
|
function getOldPrevious(node) { |
|
var oldPrevious = oldPreviousCache.get(node); |
|
if (oldPrevious !== undefined) |
|
return oldPrevious; |
|
|
|
oldPrevious = change.oldPrevious.get(node); |
|
while (oldPrevious && (change.removed.has(oldPrevious) || isMoved(oldPrevious))) { |
|
oldPrevious = getOldPrevious(oldPrevious); |
|
} |
|
|
|
if (oldPrevious === undefined) |
|
oldPrevious = node.previousSibling; |
|
oldPreviousCache.set(node, oldPrevious); |
|
|
|
return oldPrevious; |
|
} |
|
|
|
var previousCache = new NodeMap(); |
|
function getPrevious(node) { |
|
if (previousCache.has(node)) |
|
return previousCache.get(node); |
|
|
|
var previous = node.previousSibling; |
|
while (previous && (change.added.has(previous) || isMoved(previous))) |
|
previous = previous.previousSibling; |
|
|
|
previousCache.set(node, previous); |
|
return previous; |
|
} |
|
|
|
change.maybeMoved.keys().forEach(isMoved); |
|
return change.moved.get(node); |
|
}; |
|
return MutationProjection; |
|
})(); |
|
|
|
var Summary = (function () { |
|
function Summary(projection, query) { |
|
var _this = this; |
|
this.projection = projection; |
|
this.added = []; |
|
this.removed = []; |
|
this.reparented = query.all || query.element ? [] : undefined; |
|
this.reordered = query.all ? [] : undefined; |
|
|
|
projection.getChanged(this, query.elementFilter, query.characterData); |
|
|
|
if (query.all || query.attribute || query.attributeList) { |
|
var filter = query.attribute ? [query.attribute] : query.attributeList; |
|
var attributeChanged = projection.attributeChangedNodes(filter); |
|
|
|
if (query.attribute) { |
|
this.valueChanged = attributeChanged[query.attribute] || []; |
|
} else { |
|
this.attributeChanged = attributeChanged; |
|
if (query.attributeList) { |
|
query.attributeList.forEach(function (attrName) { |
|
if (!_this.attributeChanged.hasOwnProperty(attrName)) |
|
_this.attributeChanged[attrName] = []; |
|
}); |
|
} |
|
} |
|
} |
|
|
|
if (query.all || query.characterData) { |
|
var characterDataChanged = projection.getCharacterDataChanged(); |
|
|
|
if (query.characterData) |
|
this.valueChanged = characterDataChanged; |
|
else |
|
this.characterDataChanged = characterDataChanged; |
|
} |
|
|
|
if (this.reordered) |
|
this.getOldPreviousSibling = projection.getOldPreviousSibling.bind(projection); |
|
} |
|
Summary.prototype.getOldParentNode = function (node) { |
|
return this.projection.getOldParentNode(node); |
|
}; |
|
|
|
Summary.prototype.getOldAttribute = function (node, name) { |
|
return this.projection.getOldAttribute(node, name); |
|
}; |
|
|
|
Summary.prototype.getOldCharacterData = function (node) { |
|
return this.projection.getOldCharacterData(node); |
|
}; |
|
|
|
Summary.prototype.getOldPreviousSibling = function (node) { |
|
return this.projection.getOldPreviousSibling(node); |
|
}; |
|
return Summary; |
|
})(); |
|
|
|
// TODO(rafaelw): Allow ':' and '.' as valid name characters. |
|
var validNameInitialChar = /[a-zA-Z_]+/; |
|
var validNameNonInitialChar = /[a-zA-Z0-9_\-]+/; |
|
|
|
// TODO(rafaelw): Consider allowing backslash in the attrValue. |
|
// TODO(rafaelw): There's got a to be way to represent this state machine |
|
// more compactly??? |
|
function escapeQuotes(value) { |
|
return '"' + value.replace(/"/, '\\\"') + '"'; |
|
} |
|
|
|
var Qualifier = (function () { |
|
function Qualifier() { |
|
} |
|
Qualifier.prototype.matches = function (oldValue) { |
|
if (oldValue === null) |
|
return false; |
|
|
|
if (this.attrValue === undefined) |
|
return true; |
|
|
|
if (!this.contains) |
|
return this.attrValue == oldValue; |
|
|
|
var tokens = oldValue.split(' '); |
|
for (var i = 0; i < tokens.length; i++) { |
|
if (this.attrValue === tokens[i]) |
|
return true; |
|
} |
|
|
|
return false; |
|
}; |
|
|
|
Qualifier.prototype.toString = function () { |
|
if (this.attrName === 'class' && this.contains) |
|
return '.' + this.attrValue; |
|
|
|
if (this.attrName === 'id' && !this.contains) |
|
return '#' + this.attrValue; |
|
|
|
if (this.contains) |
|
return '[' + this.attrName + '~=' + escapeQuotes(this.attrValue) + ']'; |
|
|
|
if ('attrValue' in this) |
|
return '[' + this.attrName + '=' + escapeQuotes(this.attrValue) + ']'; |
|
|
|
return '[' + this.attrName + ']'; |
|
}; |
|
return Qualifier; |
|
})(); |
|
|
|
var Selector = (function () { |
|
function Selector() { |
|
this.uid = Selector.nextUid++; |
|
this.qualifiers = []; |
|
} |
|
Object.defineProperty(Selector.prototype, "caseInsensitiveTagName", { |
|
get: function () { |
|
return this.tagName.toUpperCase(); |
|
}, |
|
enumerable: true, |
|
configurable: true |
|
}); |
|
|
|
Object.defineProperty(Selector.prototype, "selectorString", { |
|
get: function () { |
|
return this.tagName + this.qualifiers.join(''); |
|
}, |
|
enumerable: true, |
|
configurable: true |
|
}); |
|
|
|
Selector.prototype.isMatching = function (el) { |
|
return el[Selector.matchesSelector](this.selectorString); |
|
}; |
|
|
|
Selector.prototype.wasMatching = function (el, change, isMatching) { |
|
if (!change || !change.attributes) |
|
return isMatching; |
|
|
|
var tagName = change.isCaseInsensitive ? this.caseInsensitiveTagName : this.tagName; |
|
if (tagName !== '*' && tagName !== el.tagName) |
|
return false; |
|
|
|
var attributeOldValues = []; |
|
var anyChanged = false; |
|
for (var i = 0; i < this.qualifiers.length; i++) { |
|
var qualifier = this.qualifiers[i]; |
|
var oldValue = change.getAttributeOldValue(qualifier.attrName); |
|
attributeOldValues.push(oldValue); |
|
anyChanged = anyChanged || (oldValue !== undefined); |
|
} |
|
|
|
if (!anyChanged) |
|
return isMatching; |
|
|
|
for (var i = 0; i < this.qualifiers.length; i++) { |
|
var qualifier = this.qualifiers[i]; |
|
var oldValue = attributeOldValues[i]; |
|
if (oldValue === undefined) |
|
oldValue = el.getAttribute(qualifier.attrName); |
|
if (!qualifier.matches(oldValue)) |
|
return false; |
|
} |
|
|
|
return true; |
|
}; |
|
|
|
Selector.prototype.matchabilityChange = function (el, change) { |
|
var isMatching = this.isMatching(el); |
|
if (isMatching) |
|
return this.wasMatching(el, change, isMatching) ? 2 /* STAYED_IN */ : 1 /* ENTERED */; |
|
else |
|
return this.wasMatching(el, change, isMatching) ? 5 /* EXITED */ : 0 /* STAYED_OUT */; |
|
}; |
|
|
|
Selector.parseSelectors = function (input) { |
|
var selectors = []; |
|
var currentSelector; |
|
var currentQualifier; |
|
|
|
function newSelector() { |
|
if (currentSelector) { |
|
if (currentQualifier) { |
|
currentSelector.qualifiers.push(currentQualifier); |
|
currentQualifier = undefined; |
|
} |
|
|
|
selectors.push(currentSelector); |
|
} |
|
currentSelector = new Selector(); |
|
} |
|
|
|
function newQualifier() { |
|
if (currentQualifier) |
|
currentSelector.qualifiers.push(currentQualifier); |
|
|
|
currentQualifier = new Qualifier(); |
|
} |
|
|
|
var WHITESPACE = /\s/; |
|
var valueQuoteChar; |
|
var SYNTAX_ERROR = 'Invalid or unsupported selector syntax.'; |
|
|
|
var SELECTOR = 1; |
|
var TAG_NAME = 2; |
|
var QUALIFIER = 3; |
|
var QUALIFIER_NAME_FIRST_CHAR = 4; |
|
var QUALIFIER_NAME = 5; |
|
var ATTR_NAME_FIRST_CHAR = 6; |
|
var ATTR_NAME = 7; |
|
var EQUIV_OR_ATTR_QUAL_END = 8; |
|
var EQUAL = 9; |
|
var ATTR_QUAL_END = 10; |
|
var VALUE_FIRST_CHAR = 11; |
|
var VALUE = 12; |
|
var QUOTED_VALUE = 13; |
|
var SELECTOR_SEPARATOR = 14; |
|
|
|
var state = SELECTOR; |
|
var i = 0; |
|
while (i < input.length) { |
|
var c = input[i++]; |
|
|
|
switch (state) { |
|
case SELECTOR: |
|
if (c.match(validNameInitialChar)) { |
|
newSelector(); |
|
currentSelector.tagName = c; |
|
state = TAG_NAME; |
|
break; |
|
} |
|
|
|
if (c == '*') { |
|
newSelector(); |
|
currentSelector.tagName = '*'; |
|
state = QUALIFIER; |
|
break; |
|
} |
|
|
|
if (c == '.') { |
|
newSelector(); |
|
newQualifier(); |
|
currentSelector.tagName = '*'; |
|
currentQualifier.attrName = 'class'; |
|
currentQualifier.contains = true; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '#') { |
|
newSelector(); |
|
newQualifier(); |
|
currentSelector.tagName = '*'; |
|
currentQualifier.attrName = 'id'; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '[') { |
|
newSelector(); |
|
newQualifier(); |
|
currentSelector.tagName = '*'; |
|
currentQualifier.attrName = ''; |
|
state = ATTR_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case TAG_NAME: |
|
if (c.match(validNameNonInitialChar)) { |
|
currentSelector.tagName += c; |
|
break; |
|
} |
|
|
|
if (c == '.') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'class'; |
|
currentQualifier.contains = true; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '#') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'id'; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '[') { |
|
newQualifier(); |
|
currentQualifier.attrName = ''; |
|
state = ATTR_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) { |
|
state = SELECTOR_SEPARATOR; |
|
break; |
|
} |
|
|
|
if (c == ',') { |
|
state = SELECTOR; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case QUALIFIER: |
|
if (c == '.') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'class'; |
|
currentQualifier.contains = true; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '#') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'id'; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '[') { |
|
newQualifier(); |
|
currentQualifier.attrName = ''; |
|
state = ATTR_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) { |
|
state = SELECTOR_SEPARATOR; |
|
break; |
|
} |
|
|
|
if (c == ',') { |
|
state = SELECTOR; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case QUALIFIER_NAME_FIRST_CHAR: |
|
if (c.match(validNameInitialChar)) { |
|
currentQualifier.attrValue = c; |
|
state = QUALIFIER_NAME; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case QUALIFIER_NAME: |
|
if (c.match(validNameNonInitialChar)) { |
|
currentQualifier.attrValue += c; |
|
break; |
|
} |
|
|
|
if (c == '.') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'class'; |
|
currentQualifier.contains = true; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '#') { |
|
newQualifier(); |
|
currentQualifier.attrName = 'id'; |
|
state = QUALIFIER_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
if (c == '[') { |
|
newQualifier(); |
|
state = ATTR_NAME_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) { |
|
state = SELECTOR_SEPARATOR; |
|
break; |
|
} |
|
if (c == ',') { |
|
state = SELECTOR; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case ATTR_NAME_FIRST_CHAR: |
|
if (c.match(validNameInitialChar)) { |
|
currentQualifier.attrName = c; |
|
state = ATTR_NAME; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case ATTR_NAME: |
|
if (c.match(validNameNonInitialChar)) { |
|
currentQualifier.attrName += c; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) { |
|
state = EQUIV_OR_ATTR_QUAL_END; |
|
break; |
|
} |
|
|
|
if (c == '~') { |
|
currentQualifier.contains = true; |
|
state = EQUAL; |
|
break; |
|
} |
|
|
|
if (c == '=') { |
|
currentQualifier.attrValue = ''; |
|
state = VALUE_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c == ']') { |
|
state = QUALIFIER; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case EQUIV_OR_ATTR_QUAL_END: |
|
if (c == '~') { |
|
currentQualifier.contains = true; |
|
state = EQUAL; |
|
break; |
|
} |
|
|
|
if (c == '=') { |
|
currentQualifier.attrValue = ''; |
|
state = VALUE_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
if (c == ']') { |
|
state = QUALIFIER; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case EQUAL: |
|
if (c == '=') { |
|
currentQualifier.attrValue = ''; |
|
state = VALUE_FIRST_CHAR; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case ATTR_QUAL_END: |
|
if (c == ']') { |
|
state = QUALIFIER; |
|
break; |
|
} |
|
|
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
throw Error(SYNTAX_ERROR); |
|
|
|
case VALUE_FIRST_CHAR: |
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
if (c == '"' || c == "'") { |
|
valueQuoteChar = c; |
|
state = QUOTED_VALUE; |
|
break; |
|
} |
|
|
|
currentQualifier.attrValue += c; |
|
state = VALUE; |
|
break; |
|
|
|
case VALUE: |
|
if (c.match(WHITESPACE)) { |
|
state = ATTR_QUAL_END; |
|
break; |
|
} |
|
if (c == ']') { |
|
state = QUALIFIER; |
|
break; |
|
} |
|
if (c == "'" || c == '"') |
|
throw Error(SYNTAX_ERROR); |
|
|
|
currentQualifier.attrValue += c; |
|
break; |
|
|
|
case QUOTED_VALUE: |
|
if (c == valueQuoteChar) { |
|
state = ATTR_QUAL_END; |
|
break; |
|
} |
|
|
|
currentQualifier.attrValue += c; |
|
break; |
|
|
|
case SELECTOR_SEPARATOR: |
|
if (c.match(WHITESPACE)) |
|
break; |
|
|
|
if (c == ',') { |
|
state = SELECTOR; |
|
break; |
|
} |
|
|
|
throw Error(SYNTAX_ERROR); |
|
} |
|
} |
|
|
|
switch (state) { |
|
case SELECTOR: |
|
case TAG_NAME: |
|
case QUALIFIER: |
|
case QUALIFIER_NAME: |
|
case SELECTOR_SEPARATOR: |
|
// Valid end states. |
|
newSelector(); |
|
break; |
|
default: |
|
throw Error(SYNTAX_ERROR); |
|
} |
|
|
|
if (!selectors.length) |
|
throw Error(SYNTAX_ERROR); |
|
|
|
return selectors; |
|
}; |
|
Selector.nextUid = 1; |
|
Selector.matchesSelector = (function () { |
|
var element = document.createElement('div'); |
|
if (typeof element['webkitMatchesSelector'] === 'function') |
|
return 'webkitMatchesSelector'; |
|
if (typeof element['mozMatchesSelector'] === 'function') |
|
return 'mozMatchesSelector'; |
|
if (typeof element['msMatchesSelector'] === 'function') |
|
return 'msMatchesSelector'; |
|
|
|
return 'matchesSelector'; |
|
})(); |
|
return Selector; |
|
})(); |
|
|
|
var attributeFilterPattern = /^([a-zA-Z:_]+[a-zA-Z0-9_\-:\.]*)$/; |
|
|
|
function validateAttribute(attribute) { |
|
if (typeof attribute != 'string') |
|
throw Error('Invalid request opion. attribute must be a non-zero length string.'); |
|
|
|
attribute = attribute.trim(); |
|
|
|
if (!attribute) |
|
throw Error('Invalid request opion. attribute must be a non-zero length string.'); |
|
|
|
if (!attribute.match(attributeFilterPattern)) |
|
throw Error('Invalid request option. invalid attribute name: ' + attribute); |
|
|
|
return attribute; |
|
} |
|
|
|
function validateElementAttributes(attribs) { |
|
if (!attribs.trim().length) |
|
throw Error('Invalid request option: elementAttributes must contain at least one attribute.'); |
|
|
|
var lowerAttributes = {}; |
|
var attributes = {}; |
|
|
|
var tokens = attribs.split(/\s+/); |
|
for (var i = 0; i < tokens.length; i++) { |
|
var name = tokens[i]; |
|
if (!name) |
|
continue; |
|
|
|
var name = validateAttribute(name); |
|
var nameLower = name.toLowerCase(); |
|
if (lowerAttributes[nameLower]) |
|
throw Error('Invalid request option: observing multiple case variations of the same attribute is not supported.'); |
|
|
|
attributes[name] = true; |
|
lowerAttributes[nameLower] = true; |
|
} |
|
|
|
return Object.keys(attributes); |
|
} |
|
|
|
function elementFilterAttributes(selectors) { |
|
var attributes = {}; |
|
|
|
selectors.forEach(function (selector) { |
|
selector.qualifiers.forEach(function (qualifier) { |
|
attributes[qualifier.attrName] = true; |
|
}); |
|
}); |
|
|
|
return Object.keys(attributes); |
|
} |
|
|
|
var MutationSummary = (function () { |
|
function MutationSummary(opts) { |
|
var _this = this; |
|
this.connected = false; |
|
this.options = MutationSummary.validateOptions(opts); |
|
this.observerOptions = MutationSummary.createObserverOptions(this.options.queries); |
|
this.root = this.options.rootNode; |
|
this.callback = this.options.callback; |
|
|
|
this.elementFilter = Array.prototype.concat.apply([], this.options.queries.map(function (query) { |
|
return query.elementFilter ? query.elementFilter : []; |
|
})); |
|
if (!this.elementFilter.length) |
|
this.elementFilter = undefined; |
|
|
|
this.calcReordered = this.options.queries.some(function (query) { |
|
return query.all; |
|
}); |
|
|
|
this.queryValidators = []; // TODO(rafaelw): Shouldn't always define this. |
|
if (MutationSummary.createQueryValidator) { |
|
this.queryValidators = this.options.queries.map(function (query) { |
|
return MutationSummary.createQueryValidator(_this.root, query); |
|
}); |
|
} |
|
|
|
this.observer = new MutationObserverCtor(function (mutations) { |
|
_this.observerCallback(mutations); |
|
}); |
|
|
|
this.reconnect(); |
|
} |
|
MutationSummary.createObserverOptions = function (queries) { |
|
var observerOptions = { |
|
childList: true, |
|
subtree: true |
|
}; |
|
|
|
var attributeFilter; |
|
function observeAttributes(attributes) { |
|
if (observerOptions.attributes && !attributeFilter) |
|
return; |
|
|
|
observerOptions.attributes = true; |
|
observerOptions.attributeOldValue = true; |
|
|
|
if (!attributes) { |
|
// observe all. |
|
attributeFilter = undefined; |
|
return; |
|
} |
|
|
|
// add to observed. |
|
attributeFilter = attributeFilter || {}; |
|
attributes.forEach(function (attribute) { |
|
attributeFilter[attribute] = true; |
|
attributeFilter[attribute.toLowerCase()] = true; |
|
}); |
|
} |
|
|
|
queries.forEach(function (query) { |
|
if (query.characterData) { |
|
observerOptions.characterData = true; |
|
observerOptions.characterDataOldValue = true; |
|
return; |
|
} |
|
|
|
if (query.all) { |
|
observeAttributes(); |
|
observerOptions.characterData = true; |
|
observerOptions.characterDataOldValue = true; |
|
return; |
|
} |
|
|
|
if (query.attribute) { |
|
observeAttributes([query.attribute.trim()]); |
|
return; |
|
} |
|
|
|
var attributes = elementFilterAttributes(query.elementFilter).concat(query.attributeList || []); |
|
if (attributes.length) |
|
observeAttributes(attributes); |
|
}); |
|
|
|
if (attributeFilter) |
|
observerOptions.attributeFilter = Object.keys(attributeFilter); |
|
|
|
return observerOptions; |
|
}; |
|
|
|
MutationSummary.validateOptions = function (options) { |
|
for (var prop in options) { |
|
if (!(prop in MutationSummary.optionKeys)) |
|
throw Error('Invalid option: ' + prop); |
|
} |
|
|
|
if (typeof options.callback !== 'function') |
|
throw Error('Invalid options: callback is required and must be a function'); |
|
|
|
if (!options.queries || !options.queries.length) |
|
throw Error('Invalid options: queries must contain at least one query request object.'); |
|
|
|
var opts = { |
|
callback: options.callback, |
|
rootNode: options.rootNode || document, |
|
observeOwnChanges: !!options.observeOwnChanges, |
|
oldPreviousSibling: !!options.oldPreviousSibling, |
|
queries: [] |
|
}; |
|
|
|
for (var i = 0; i < options.queries.length; i++) { |
|
var request = options.queries[i]; |
|
|
|
// all |
|
if (request.all) { |
|
if (Object.keys(request).length > 1) |
|
throw Error('Invalid request option. all has no options.'); |
|
|
|
opts.queries.push({ all: true }); |
|
continue; |
|
} |
|
|
|
// attribute |
|
if ('attribute' in request) { |
|
var query = { |
|
attribute: validateAttribute(request.attribute) |
|
}; |
|
|
|
query.elementFilter = Selector.parseSelectors('*[' + query.attribute + ']'); |
|
|
|
if (Object.keys(request).length > 1) |
|
throw Error('Invalid request option. attribute has no options.'); |
|
|
|
opts.queries.push(query); |
|
continue; |
|
} |
|
|
|
// element |
|
if ('element' in request) { |
|
var requestOptionCount = Object.keys(request).length; |
|
var query = { |
|
element: request.element, |
|
elementFilter: Selector.parseSelectors(request.element) |
|
}; |
|
|
|
if (request.hasOwnProperty('elementAttributes')) { |
|
query.attributeList = validateElementAttributes(request.elementAttributes); |
|
requestOptionCount--; |
|
} |
|
|
|
if (requestOptionCount > 1) |
|
throw Error('Invalid request option. element only allows elementAttributes option.'); |
|
|
|
opts.queries.push(query); |
|
continue; |
|
} |
|
|
|
// characterData |
|
if (request.characterData) { |
|
if (Object.keys(request).length > 1) |
|
throw Error('Invalid request option. characterData has no options.'); |
|
|
|
opts.queries.push({ characterData: true }); |
|
continue; |
|
} |
|
|
|
throw Error('Invalid request option. Unknown query request.'); |
|
} |
|
|
|
return opts; |
|
}; |
|
|
|
MutationSummary.prototype.createSummaries = function (mutations) { |
|
if (!mutations || !mutations.length) |
|
return []; |
|
|
|
var projection = new MutationProjection(this.root, mutations, this.elementFilter, this.calcReordered, this.options.oldPreviousSibling); |
|
|
|
var summaries = []; |
|
for (var i = 0; i < this.options.queries.length; i++) { |
|
summaries.push(new Summary(projection, this.options.queries[i])); |
|
} |
|
|
|
return summaries; |
|
}; |
|
|
|
MutationSummary.prototype.checkpointQueryValidators = function () { |
|
this.queryValidators.forEach(function (validator) { |
|
if (validator) |
|
validator.recordPreviousState(); |
|
}); |
|
}; |
|
|
|
MutationSummary.prototype.runQueryValidators = function (summaries) { |
|
this.queryValidators.forEach(function (validator, index) { |
|
if (validator) |
|
validator.validate(summaries[index]); |
|
}); |
|
}; |
|
|
|
MutationSummary.prototype.changesToReport = function (summaries) { |
|
return summaries.some(function (summary) { |
|
var summaryProps = [ |
|
'added', 'removed', 'reordered', 'reparented', |
|
'valueChanged', 'characterDataChanged']; |
|
if (summaryProps.some(function (prop) { |
|
return summary[prop] && summary[prop].length; |
|
})) |
|
return true; |
|
|
|
if (summary.attributeChanged) { |
|
var attrNames = Object.keys(summary.attributeChanged); |
|
var attrsChanged = attrNames.some(function (attrName) { |
|
return !!summary.attributeChanged[attrName].length; |
|
}); |
|
if (attrsChanged) |
|
return true; |
|
} |
|
return false; |
|
}); |
|
}; |
|
|
|
MutationSummary.prototype.observerCallback = function (mutations) { |
|
if (!this.options.observeOwnChanges) |
|
this.observer.disconnect(); |
|
|
|
var summaries = this.createSummaries(mutations); |
|
this.runQueryValidators(summaries); |
|
|
|
if (this.options.observeOwnChanges) |
|
this.checkpointQueryValidators(); |
|
|
|
if (this.changesToReport(summaries)) |
|
this.callback(summaries); |
|
|
|
// disconnect() may have been called during the callback. |
|
if (!this.options.observeOwnChanges && this.connected) { |
|
this.checkpointQueryValidators(); |
|
this.observer.observe(this.root, this.observerOptions); |
|
} |
|
}; |
|
|
|
MutationSummary.prototype.reconnect = function () { |
|
if (this.connected) |
|
throw Error('Already connected'); |
|
|
|
this.observer.observe(this.root, this.observerOptions); |
|
this.connected = true; |
|
this.checkpointQueryValidators(); |
|
}; |
|
|
|
MutationSummary.prototype.takeSummaries = function () { |
|
if (!this.connected) |
|
throw Error('Not connected'); |
|
|
|
var summaries = this.createSummaries(this.observer.takeRecords()); |
|
return this.changesToReport(summaries) ? summaries : undefined; |
|
}; |
|
|
|
MutationSummary.prototype.disconnect = function () { |
|
var summaries = this.takeSummaries(); |
|
this.observer.disconnect(); |
|
this.connected = false; |
|
return summaries; |
|
}; |
|
MutationSummary.NodeMap = NodeMap; |
|
MutationSummary.parseElementFilter = Selector.parseSelectors; |
|
|
|
MutationSummary.optionKeys = { |
|
'callback': true, |
|
'queries': true, |
|
'rootNode': true, |
|
'oldPreviousSibling': true, |
|
'observeOwnChanges': true |
|
}; |
|
return MutationSummary; |
|
})(); |
|
|
|
module.exports = MutationSummary |
|
},{}],31:[function(require,module,exports){ |
|
/** |
|
* path-to-domnode |
|
* Copyright (C) 2016 Marcel Klehr <[email protected]> |
|
*/ |
|
module.exports = function pathTo(node, root) { |
|
if(!root) throw new Error('No root node specified.') |
|
|
|
if(node === root) return [] |
|
|
|
if(!root.contains(node)) { |
|
throw new Error('Cannot determine path. Node is not a descendant of root node.') |
|
} |
|
|
|
// The number of older siblings equals my index in the list of childNodes |
|
var myIndex = 0, n = node |
|
while(n.previousSibling) { |
|
n = n.previousSibling |
|
myIndex++ |
|
} |
|
|
|
var parentPath = pathTo(node.parentNode, root) |
|
parentPath.push(myIndex) |
|
|
|
return parentPath |
|
} |
|
},{}],32:[function(require,module,exports){ |
|
var inherits = require('inherits'); |
|
var EventEmitter = require('events').EventEmitter; |
|
|
|
module.exports = Queue; |
|
|
|
function Queue(options) { |
|
if (!(this instanceof Queue)) |
|
return new Queue(options); |
|
|
|
EventEmitter.call(this); |
|
options = options || {}; |
|
this.concurrency = options.concurrency || Infinity; |
|
this.timeout = options.timeout || 0; |
|
this.pending = 0; |
|
this.session = 0; |
|
this.running = false; |
|
this.jobs = []; |
|
} |
|
inherits(Queue, EventEmitter); |
|
|
|
var arrayMethods = [ |
|
'push', |
|
'unshift', |
|
'splice', |
|
'pop', |
|
'shift', |
|
'slice', |
|
'reverse', |
|
'indexOf', |
|
'lastIndexOf' |
|
]; |
|
|
|
for (var method in arrayMethods) (function(method) { |
|
Queue.prototype[method] = function() { |
|
return Array.prototype[method].apply(this.jobs, arguments); |
|
}; |
|
})(arrayMethods[method]); |
|
|
|
Object.defineProperty(Queue.prototype, 'length', { get: function() { |
|
return this.pending + this.jobs.length; |
|
}}); |
|
|
|
Queue.prototype.start = function(cb) { |
|
if (cb) { |
|
callOnErrorOrEnd.call(this, cb); |
|
} |
|
|
|
if (this.pending === this.concurrency) { |
|
return; |
|
} |
|
|
|
if (this.jobs.length === 0) { |
|
if (this.pending === 0) { |
|
done.call(this); |
|
} |
|
return; |
|
} |
|
|
|
var self = this; |
|
var job = this.jobs.shift(); |
|
var once = true; |
|
var session = this.session; |
|
var timeoutId = null; |
|
var didTimeout = false; |
|
|
|
function next(err, result) { |
|
if (once && self.session === session) { |
|
once = false; |
|
self.pending--; |
|
if (timeoutId !== null) { |
|
clearTimeout(timeoutId); |
|
} |
|
|
|
if (err) { |
|
self.emit('error', err, job); |
|
} else if (didTimeout === false) { |
|
self.emit('success', result, job); |
|
} |
|
|
|
if (self.session === session) { |
|
if (self.pending === 0 && self.jobs.length === 0) { |
|
done.call(self); |
|
} else if (self.running) { |
|
self.start(); |
|
} |
|
} |
|
} |
|
} |
|
|
|
if (this.timeout) { |
|
timeoutId = setTimeout(function() { |
|
didTimeout = true; |
|
if (self.listeners('timeout').length > 0) { |
|
self.emit('timeout', next, job); |
|
} else { |
|
next(); |
|
} |
|
}, this.timeout); |
|
} |
|
|
|
this.pending++; |
|
this.running = true; |
|
job(next); |
|
|
|
if (this.jobs.length > 0) { |
|
this.start(); |
|
} |
|
}; |
|
|
|
Queue.prototype.stop = function() { |
|
this.running = false; |
|
}; |
|
|
|
Queue.prototype.end = function(err) { |
|
this.jobs.length = 0; |
|
this.pending = 0; |
|
done.call(this, err); |
|
}; |
|
|
|
function callOnErrorOrEnd(cb) { |
|
var self = this; |
|
this.on('error', onerror); |
|
this.on('end', onend); |
|
|
|
function onerror(err) { self.end(err); } |
|
function onend(err) { |
|
self.removeListener('error', onerror); |
|
self.removeListener('end', onend); |
|
cb(err); |
|
} |
|
} |
|
|
|
function done(err) { |
|
this.session++; |
|
this.running = false; |
|
this.emit('end', err); |
|
} |
|
|
|
},{"events":49,"inherits":29}],33:[function(require,module,exports){ |
|
(function (process,global){ |
|
(function (global, undefined) { |
|
"use strict"; |
|
|
|
if (global.setImmediate) { |
|
return; |
|
} |
|
|
|
var nextHandle = 1; // Spec says greater than zero |
|
var tasksByHandle = {}; |
|
var currentlyRunningATask = false; |
|
var doc = global.document; |
|
var setImmediate; |
|
|
|
function addFromSetImmediateArguments(args) { |
|
tasksByHandle[nextHandle] = partiallyApplied.apply(undefined, args); |
|
return nextHandle++; |
|
} |
|
|
|
// This function accepts the same arguments as setImmediate, but |
|
// returns a function that requires no arguments. |
|
function partiallyApplied(handler) { |
|
var args = [].slice.call(arguments, 1); |
|
return function() { |
|
if (typeof handler === "function") { |
|
handler.apply(undefined, args); |
|
} else { |
|
(new Function("" + handler))(); |
|
} |
|
}; |
|
} |
|
|
|
function runIfPresent(handle) { |
|
// From the spec: "Wait until any invocations of this algorithm started before this one have completed." |
|
// So if we're currently running a task, we'll need to delay this invocation. |
|
if (currentlyRunningATask) { |
|
// Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a |
|
// "too much recursion" error. |
|
setTimeout(partiallyApplied(runIfPresent, handle), 0); |
|
} else { |
|
var task = tasksByHandle[handle]; |
|
if (task) { |
|
currentlyRunningATask = true; |
|
try { |
|
task(); |
|
} finally { |
|
clearImmediate(handle); |
|
currentlyRunningATask = false; |
|
} |
|
} |
|
} |
|
} |
|
|
|
function clearImmediate(handle) { |
|
delete tasksByHandle[handle]; |
|
} |
|
|
|
function installNextTickImplementation() { |
|
setImmediate = function() { |
|
var handle = addFromSetImmediateArguments(arguments); |
|
process.nextTick(partiallyApplied(runIfPresent, handle)); |
|
return handle; |
|
}; |
|
} |
|
|
|
function canUsePostMessage() { |
|
// The test against `importScripts` prevents this implementation from being installed inside a web worker, |
|
// where `global.postMessage` means something completely different and can't be used for this purpose. |
|
if (global.postMessage && !global.importScripts) { |
|
var postMessageIsAsynchronous = true; |
|
var oldOnMessage = global.onmessage; |
|
global.onmessage = function() { |
|
postMessageIsAsynchronous = false; |
|
}; |
|
global.postMessage("", "*"); |
|
global.onmessage = oldOnMessage; |
|
return postMessageIsAsynchronous; |
|
} |
|
} |
|
|
|
function installPostMessageImplementation() { |
|
// Installs an event handler on `global` for the `message` event: see |
|
// * https://developer.mozilla.org/en/DOM/window.postMessage |
|
// * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages |
|
|
|
var messagePrefix = "setImmediate$" + Math.random() + "$"; |
|
var onGlobalMessage = function(event) { |
|
if (event.source === global && |
|
typeof event.data === "string" && |
|
event.data.indexOf(messagePrefix) === 0) { |
|
runIfPresent(+event.data.slice(messagePrefix.length)); |
|
} |
|
}; |
|
|
|
if (global.addEventListener) { |
|
global.addEventListener("message", onGlobalMessage, false); |
|
} else { |
|
global.attachEvent("onmessage", onGlobalMessage); |
|
} |
|
|
|
setImmediate = function() { |
|
var handle = addFromSetImmediateArguments(arguments); |
|
global.postMessage(messagePrefix + handle, "*"); |
|
return handle; |
|
}; |
|
} |
|
|
|
function installMessageChannelImplementation() { |
|
var channel = new MessageChannel(); |
|
channel.port1.onmessage = function(event) { |
|
var handle = event.data; |
|
runIfPresent(handle); |
|
}; |
|
|
|
setImmediate = function() { |
|
var handle = addFromSetImmediateArguments(arguments); |
|
channel.port2.postMessage(handle); |
|
return handle; |
|
}; |
|
} |
|
|
|
function installReadyStateChangeImplementation() { |
|
var html = doc.documentElement; |
|
setImmediate = function() { |
|
var handle = addFromSetImmediateArguments(arguments); |
|
// Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted |
|
// into the document. Do so, thus queuing up the task. Remember to clean up once it's been called. |
|
var script = doc.createElement("script"); |
|
script.onreadystatechange = function () { |
|
runIfPresent(handle); |
|
script.onreadystatechange = null; |
|
html.removeChild(script); |
|
script = null; |
|
}; |
|
html.appendChild(script); |
|
return handle; |
|
}; |
|
} |
|
|
|
function installSetTimeoutImplementation() { |
|
setImmediate = function() { |
|
var handle = addFromSetImmediateArguments(arguments); |
|
setTimeout(partiallyApplied(runIfPresent, handle), 0); |
|
return handle; |
|
}; |
|
} |
|
|
|
// If supported, we should attach to the prototype of global, since that is where setTimeout et al. live. |
|
var attachTo = Object.getPrototypeOf && Object.getPrototypeOf(global); |
|
attachTo = attachTo && attachTo.setTimeout ? attachTo : global; |
|
|
|
// Don't get fooled by e.g. browserify environments. |
|
if ({}.toString.call(global.process) === "[object process]") { |
|
// For Node.js before 0.9 |
|
installNextTickImplementation(); |
|
|
|
} else if (canUsePostMessage()) { |
|
// For non-IE10 modern browsers |
|
installPostMessageImplementation(); |
|
|
|
} else if (global.MessageChannel) { |
|
// For web workers, where supported |
|
installMessageChannelImplementation(); |
|
|
|
} else if (doc && "onreadystatechange" in doc.createElement("script")) { |
|
// For IE 6–8 |
|
installReadyStateChangeImplementation(); |
|
|
|
} else { |
|
// For older browsers |
|
installSetTimeoutImplementation(); |
|
} |
|
|
|
attachTo.setImmediate = setImmediate; |
|
attachTo.clearImmediate = clearImmediate; |
|
}(typeof self === "undefined" ? typeof global === "undefined" ? this : global : self)); |
|
|
|
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) |
|
},{"_process":55}],34:[function(require,module,exports){ |
|
module.exports = serialize |
|
|
|
function serialize(vdom) { |
|
return JSON.stringify(serializeVNode(vdom)) |
|
} |
|
|
|
function serializeVNode(vnode) { |
|
if('Thunk' === vnode.type) vnode = vnode.render() |
|
if(vnode.children) vnode.children = vnode.children.map(serializeVNode) |
|
vnode.type = vnode.type |
|
vnode.version = vnode.version |
|
return vnode |
|
} |
|
},{}],35:[function(require,module,exports){ |
|
/*! |
|
* vdom-virtualize |
|
* Copyright 2014 by Marcel Klehr <[email protected]> |
|
* |
|
* (MIT LICENSE) |
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
* of this software and associated documentation files (the "Software"), to deal |
|
* in the Software without restriction, including without limitation the rights |
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
* copies of the Software, and to permit persons to whom the Software is |
|
* furnished to do so, subject to the following conditions: |
|
* |
|
* The above copyright notice and this permission notice shall be included in |
|
* all copies or substantial portions of the Software. |
|
* |
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
* THE SOFTWARE. |
|
*/ |
|
var VNode = require("virtual-dom/vnode/vnode") |
|
, VText = require("virtual-dom/vnode/vtext") |
|
, VComment = require("./vcomment") |
|
|
|
module.exports = createVNode |
|
|
|
function createVNode(domNode, key) { |
|
key = key || null // XXX: Leave out `key` for now... merely used for (re-)ordering |
|
|
|
if(domNode.nodeType == 1) return createFromElement(domNode, key) |
|
if(domNode.nodeType == 3) return createFromTextNode(domNode, key) |
|
if(domNode.nodeType == 8) return createFromCommentNode(domNode, key) |
|
return |
|
} |
|
|
|
createVNode.fromHTML = function(html, key) { |
|
var rootNode = null; |
|
|
|
try { |
|
// Everything except iOS 7 Safari, IE 8/9, Andriod Browser 4.1/4.3 |
|
var parser = new DOMParser(); |
|
var doc = parser.parseFromString(html, 'text/html'); |
|
rootNode = doc.documentElement; |
|
} catch(e) { |
|
// Old browsers |
|
var ifr = document.createElement('iframe'); |
|
ifr.setAttribute('data-content', html); |
|
ifr.src = 'javascript: window.frameElement.getAttribute("data-content");'; |
|
document.head.appendChild(ifr); |
|
rootNode = ifr.contentDocument.documentElement; |
|
setTimeout(function() { |
|
ifr.remove(); // Garbage collection |
|
}, 0); |
|
} |
|
|
|
return createVNode(rootNode, key); |
|
}; |
|
|
|
function createFromTextNode(tNode) { |
|
return new VText(tNode.nodeValue) |
|
} |
|
|
|
|
|
function createFromCommentNode(cNode) { |
|
return new VComment(cNode.nodeValue) |
|
} |
|
|
|
|
|
function createFromElement(el) { |
|
var tagName = el.tagName |
|
, namespace = el.namespaceURI == 'http://www.w3.org/1999/xhtml'? null : el.namespaceURI |
|
, properties = getElementProperties(el) |
|
, children = [] |
|
|
|
for (var i = 0; i < el.childNodes.length; i++) { |
|
children.push(createVNode(el.childNodes[i]/*, i*/)) |
|
} |
|
|
|
return new VNode(tagName, properties, children, null, namespace) |
|
} |
|
|
|
|
|
function getElementProperties(el) { |
|
var obj = {} |
|
|
|
for(var i=0; i<props.length; i++) { |
|
var propName = props[i] |
|
if(!el[propName]) continue |
|
|
|
// Special case: style |
|
// .style is a DOMStyleDeclaration, thus we need to iterate over all |
|
// rules to create a hash of applied css properties. |
|
// |
|
// You can directly set a specific .style[prop] = value so patching with vdom |
|
// is possible. |
|
if("style" == propName) { |
|
var css = {} |
|
, styleProp |
|
if ('undefined' !== typeof el.style.length) { |
|
for(var j=0; j<el.style.length; j++) { |
|
styleProp = el.style[j] |
|
css[styleProp] = el.style.getPropertyValue(styleProp) // XXX: add support for "!important" via getPropertyPriority()! |
|
} |
|
} else { // IE8 |
|
for (var styleProp in el.style) { |
|
if (el.style[styleProp] && el.style.hasOwnProperty(styleProp)) { |
|
css[styleProp] = el.style[styleProp]; |
|
} |
|
} |
|
} |
|
|
|
if(Object.keys(css).length) obj[propName] = css |
|
continue |
|
} |
|
|
|
// https://msdn.microsoft.com/en-us/library/cc848861%28v=vs.85%29.aspx |
|
// The img element does not support the HREF content attribute. |
|
// In addition, the href property is read-only for the img Document Object Model (DOM) object |
|
if (el.tagName.toLowerCase() === 'img' && propName === 'href') { |
|
continue; |
|
} |
|
|
|
// Special case: dataset |
|
// we can iterate over .dataset with a simple for..in loop. |
|
// The all-time foo with data-* attribs is the dash-snake to camelCase |
|
// conversion. |
|
// |
|
// *This is compatible with h(), but not with every browser, thus this section was removed in favor |
|
// of attributes (specified below)!* |
|
// |
|
// .dataset properties are directly accessible as transparent getters/setters, so |
|
// patching with vdom is possible. |
|
/*if("dataset" == propName) { |
|
var data = {} |
|
for(var p in el.dataset) { |
|
data[p] = el.dataset[p] |
|
} |
|
obj[propName] = data |
|
return |
|
}*/ |
|
|
|
// Special case: attributes |
|
// these are a NamedNodeMap, but we can just convert them to a hash for vdom, |
|
// because of https://github.com/Matt-Esch/virtual-dom/blob/master/vdom/apply-properties.js#L57 |
|
if("attributes" == propName){ |
|
var atts = Array.prototype.slice.call(el[propName]); |
|
var hash = {} |
|
for(var k=0; k<atts.length; k++){ |
|
var name = atts[k].name; |
|
if(obj[name] || obj[attrBlacklist[name]]) continue; |
|
hash[name] = el.getAttribute(name); |
|
} |
|
obj[propName] = hash; |
|
continue |
|
} |
|
if("tabIndex" == propName && el.tabIndex === -1) continue |
|
|
|
// Special case: contentEditable |
|
// browser use 'inherit' by default on all nodes, but does not allow setting it to '' |
|
// diffing virtualize dom will trigger error |
|
// ref: https://github.com/Matt-Esch/virtual-dom/issues/176 |
|
if("contentEditable" == propName && el[propName] === 'inherit') continue |
|
|
|
if('object' === typeof el[propName]) continue |
|
|
|
// default: just copy the property |
|
obj[propName] = el[propName] |
|
} |
|
|
|
return obj |
|
} |
|
|
|
/** |
|
* DOMNode property white list |
|
* Taken from https://github.com/Raynos/react/blob/dom-property-config/src/browser/ui/dom/DefaultDOMPropertyConfig.js |
|
*/ |
|
var props = |
|
|
|
module.exports.properties = [ |
|
"accept" |
|
,"accessKey" |
|
,"action" |
|
,"alt" |
|
,"async" |
|
,"autoComplete" |
|
,"autoPlay" |
|
,"cellPadding" |
|
,"cellSpacing" |
|
,"checked" |
|
,"className" |
|
,"colSpan" |
|
,"content" |
|
,"contentEditable" |
|
,"controls" |
|
,"crossOrigin" |
|
,"data" |
|
//,"dataset" removed since attributes handles data-attributes |
|
,"defer" |
|
,"dir" |
|
,"download" |
|
,"draggable" |
|
,"encType" |
|
,"formNoValidate" |
|
,"href" |
|
,"hrefLang" |
|
,"htmlFor" |
|
,"httpEquiv" |
|
,"icon" |
|
,"id" |
|
,"label" |
|
,"lang" |
|
,"list" |
|
,"loop" |
|
,"max" |
|
,"mediaGroup" |
|
,"method" |
|
,"min" |
|
,"multiple" |
|
,"muted" |
|
,"name" |
|
,"noValidate" |
|
,"pattern" |
|
,"placeholder" |
|
,"poster" |
|
,"preload" |
|
,"radioGroup" |
|
,"readOnly" |
|
,"rel" |
|
,"required" |
|
,"rowSpan" |
|
,"sandbox" |
|
,"scope" |
|
,"scrollLeft" |
|
,"scrolling" |
|
,"scrollTop" |
|
,"selected" |
|
,"span" |
|
,"spellCheck" |
|
,"src" |
|
,"srcDoc" |
|
,"srcSet" |
|
,"start" |
|
,"step" |
|
,"style" |
|
,"tabIndex" |
|
,"target" |
|
,"title" |
|
,"type" |
|
,"value" |
|
|
|
// Non-standard Properties |
|
,"autoCapitalize" |
|
,"autoCorrect" |
|
,"property" |
|
|
|
, "attributes" |
|
] |
|
|
|
var attrBlacklist = |
|
module.exports.attrBlacklist = { |
|
'class': 'className' |
|
} |
|
},{"./vcomment":36,"virtual-dom/vnode/vnode":42,"virtual-dom/vnode/vtext":43}],36:[function(require,module,exports){ |
|
module.exports = VirtualComment |
|
|
|
function VirtualComment(text) { |
|
this.text = String(text) |
|
} |
|
|
|
VirtualComment.prototype.type = 'Widget' |
|
|
|
VirtualComment.prototype.init = function() { |
|
return document.createComment(this.text) |
|
} |
|
|
|
VirtualComment.prototype.update = function(previous, domNode) { |
|
if(this.text === previous.text) return |
|
domNode.nodeValue = this.text |
|
} |
|
|
|
},{}],37:[function(require,module,exports){ |
|
module.exports = isThunk |
|
|
|
function isThunk(t) { |
|
return t && t.type === "Thunk" |
|
} |
|
|
|
},{}],38:[function(require,module,exports){ |
|
module.exports = isHook |
|
|
|
function isHook(hook) { |
|
return hook && |
|
(typeof hook.hook === "function" && !hook.hasOwnProperty("hook") || |
|
typeof hook.unhook === "function" && !hook.hasOwnProperty("unhook")) |
|
} |
|
|
|
},{}],39:[function(require,module,exports){ |
|
var version = require("./version") |
|
|
|
module.exports = isVirtualNode |
|
|
|
function isVirtualNode(x) { |
|
return x && x.type === "VirtualNode" && x.version === version |
|
} |
|
|
|
},{"./version":41}],40:[function(require,module,exports){ |
|
module.exports = isWidget |
|
|
|
function isWidget(w) { |
|
return w && w.type === "Widget" |
|
} |
|
|
|
},{}],41:[function(require,module,exports){ |
|
module.exports = "2" |
|
|
|
},{}],42:[function(require,module,exports){ |
|
var version = require("./version") |
|
var isVNode = require("./is-vnode") |
|
var isWidget = require("./is-widget") |
|
var isThunk = require("./is-thunk") |
|
var isVHook = require("./is-vhook") |
|
|
|
module.exports = VirtualNode |
|
|
|
var noProperties = {} |
|
var noChildren = [] |
|
|
|
function VirtualNode(tagName, properties, children, key, namespace) { |
|
this.tagName = tagName |
|
this.properties = properties || noProperties |
|
this.children = children || noChildren |
|
this.key = key != null ? String(key) : undefined |
|
this.namespace = (typeof namespace === "string") ? namespace : null |
|
|
|
var count = (children && children.length) || 0 |
|
var descendants = 0 |
|
var hasWidgets = false |
|
var hasThunks = false |
|
var descendantHooks = false |
|
var hooks |
|
|
|
for (var propName in properties) { |
|
if (properties.hasOwnProperty(propName)) { |
|
var property = properties[propName] |
|
if (isVHook(property) && property.unhook) { |
|
if (!hooks) { |
|
hooks = {} |
|
} |
|
|
|
hooks[propName] = property |
|
} |
|
} |
|
} |
|
|
|
for (var i = 0; i < count; i++) { |
|
var child = children[i] |
|
if (isVNode(child)) { |
|
descendants += child.count || 0 |
|
|
|
if (!hasWidgets && child.hasWidgets) { |
|
hasWidgets = true |
|
} |
|
|
|
if (!hasThunks && child.hasThunks) { |
|
hasThunks = true |
|
} |
|
|
|
if (!descendantHooks && (child.hooks || child.descendantHooks)) { |
|
descendantHooks = true |
|
} |
|
} else if (!hasWidgets && isWidget(child)) { |
|
if (typeof child.destroy === "function") { |
|
hasWidgets = true |
|
} |
|
} else if (!hasThunks && isThunk(child)) { |
|
hasThunks = true; |
|
} |
|
} |
|
|
|
this.count = count + descendants |
|
this.hasWidgets = hasWidgets |
|
this.hasThunks = hasThunks |
|
this.hooks = hooks |
|
this.descendantHooks = descendantHooks |
|
} |
|
|
|
VirtualNode.prototype.version = version |
|
VirtualNode.prototype.type = "VirtualNode" |
|
|
|
},{"./is-thunk":37,"./is-vhook":38,"./is-vnode":39,"./is-widget":40,"./version":41}],43:[function(require,module,exports){ |
|
var version = require("./version") |
|
|
|
module.exports = VirtualText |
|
|
|
function VirtualText(text) { |
|
this.text = String(text) |
|
} |
|
|
|
VirtualText.prototype.version = version |
|
VirtualText.prototype.type = "VirtualText" |
|
|
|
},{"./version":41}],44:[function(require,module,exports){ |
|
'use strict' |
|
|
|
exports.toByteArray = toByteArray |
|
exports.fromByteArray = fromByteArray |
|
|
|
var lookup = [] |
|
var revLookup = [] |
|
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array |
|
|
|
function init () { |
|
var i |
|
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' |
|
var len = code.length |
|
|
|
for (i = 0; i < len; i++) { |
|
lookup[i] = code[i] |
|
} |
|
|
|
for (i = 0; i < len; ++i) { |
|
revLookup[code.charCodeAt(i)] = i |
|
} |
|
revLookup['-'.charCodeAt(0)] = 62 |
|
revLookup['_'.charCodeAt(0)] = 63 |
|
} |
|
|
|
init() |
|
|
|
function toByteArray (b64) { |
|
var i, j, l, tmp, placeHolders, arr |
|
var len = b64.length |
|
|
|
if (len % 4 > 0) { |
|
throw new Error('Invalid string. Length must be a multiple of 4') |
|
} |
|
|
|
// the number of equal signs (place holders) |
|
// if there are two placeholders, than the two characters before it |
|
// represent one byte |
|
// if there is only one, then the three characters before it represent 2 bytes |
|
// this is just a cheap hack to not do indexOf twice |
|
placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 |
|
|
|
// base64 is 4/3 + up to two characters of the original data |
|
arr = new Arr(len * 3 / 4 - placeHolders) |
|
|
|
// if there are placeholders, only get up to the last complete 4 chars |
|
l = placeHolders > 0 ? len - 4 : len |
|
|
|
var L = 0 |
|
|
|
for (i = 0, j = 0; i < l; i += 4, j += 3) { |
|
tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] |
|
arr[L++] = (tmp & 0xFF0000) >> 16 |
|
arr[L++] = (tmp & 0xFF00) >> 8 |
|
arr[L++] = tmp & 0xFF |
|
} |
|
|
|
if (placeHolders === 2) { |
|
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) |
|
arr[L++] = tmp & 0xFF |
|
} else if (placeHolders === 1) { |
|
tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) |
|
arr[L++] = (tmp >> 8) & 0xFF |
|
arr[L++] = tmp & 0xFF |
|
} |
|
|
|
return arr |
|
} |
|
|
|
function tripletToBase64 (num) { |
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] |
|
} |
|
|
|
function encodeChunk (uint8, start, end) { |
|
var tmp |
|
var output = [] |
|
for (var i = start; i < end; i += 3) { |
|
tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) |
|
output.push(tripletToBase64(tmp)) |
|
} |
|
return output.join('') |
|
} |
|
|
|
function fromByteArray (uint8) { |
|
var tmp |
|
var len = uint8.length |
|
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes |
|
var output = '' |
|
var parts = [] |
|
var maxChunkLength = 16383 // must be multiple of 3 |
|
|
|
// go through the array every three bytes, we'll deal with trailing stuff later |
|
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { |
|
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) |
|
} |
|
|
|
// pad the end with zeros, but make sure to not forget the extra bytes |
|
if (extraBytes === 1) { |
|
tmp = uint8[len - 1] |
|
output += lookup[tmp >> 2] |
|
output += lookup[(tmp << 4) & 0x3F] |
|
output += '==' |
|
} else if (extraBytes === 2) { |
|
tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) |
|
output += lookup[tmp >> 10] |
|
output += lookup[(tmp >> 4) & 0x3F] |
|
output += lookup[(tmp << 2) & 0x3F] |
|
output += '=' |
|
} |
|
|
|
parts.push(output) |
|
|
|
return parts.join('') |
|
} |
|
|
|
},{}],45:[function(require,module,exports){ |
|
|
|
},{}],46:[function(require,module,exports){ |
|
(function (global){ |
|
/*! |
|
* The buffer module from node.js, for the browser. |
|
* |
|
* @author Feross Aboukhadijeh <[email protected]> <http://feross.org> |
|
* @license MIT |
|
*/ |
|
/* eslint-disable no-proto */ |
|
|
|
'use strict' |
|
|
|
var base64 = require('base64-js') |
|
var ieee754 = require('ieee754') |
|
var isArray = require('isarray') |
|
|
|
exports.Buffer = Buffer |
|
exports.SlowBuffer = SlowBuffer |
|
exports.INSPECT_MAX_BYTES = 50 |
|
Buffer.poolSize = 8192 // not used by this implementation |
|
|
|
var rootParent = {} |
|
|
|
/** |
|
* If `Buffer.TYPED_ARRAY_SUPPORT`: |
|
* === true Use Uint8Array implementation (fastest) |
|
* === false Use Object implementation (most compatible, even IE6) |
|
* |
|
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, |
|
* Opera 11.6+, iOS 4.2+. |
|
* |
|
* Due to various browser bugs, sometimes the Object implementation will be used even |
|
* when the browser supports typed arrays. |
|
* |
|
* Note: |
|
* |
|
* - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances, |
|
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438. |
|
* |
|
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function. |
|
* |
|
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of |
|
* incorrect length in some situations. |
|
|
|
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they |
|
* get the Object implementation, which is slower but behaves correctly. |
|
*/ |
|
Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined |
|
? global.TYPED_ARRAY_SUPPORT |
|
: typedArraySupport() |
|
|
|
function typedArraySupport () { |
|
try { |
|
var arr = new Uint8Array(1) |
|
arr.foo = function () { return 42 } |
|
return arr.foo() === 42 && // typed array instances can be augmented |
|
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` |
|
arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray` |
|
} catch (e) { |
|
return false |
|
} |
|
} |
|
|
|
function kMaxLength () { |
|
return Buffer.TYPED_ARRAY_SUPPORT |
|
? 0x7fffffff |
|
: 0x3fffffff |
|
} |
|
|
|
/** |
|
* The Buffer constructor returns instances of `Uint8Array` that have their |
|
* prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of |
|
* `Uint8Array`, so the returned instances will have all the node `Buffer` methods |
|
* and the `Uint8Array` methods. Square bracket notation works as expected -- it |
|
* returns a single octet. |
|
* |
|
* The `Uint8Array` prototype remains unmodified. |
|
*/ |
|
function Buffer (arg) { |
|
if (!(this instanceof Buffer)) { |
|
// Avoid going through an ArgumentsAdaptorTrampoline in the common case. |
|
if (arguments.length > 1) return new Buffer(arg, arguments[1]) |
|
return new Buffer(arg) |
|
} |
|
|
|
if (!Buffer.TYPED_ARRAY_SUPPORT) { |
|
this.length = 0 |
|
this.parent = undefined |
|
} |
|
|
|
// Common case. |
|
if (typeof arg === 'number') { |
|
return fromNumber(this, arg) |
|
} |
|
|
|
// Slightly less common case. |
|
if (typeof arg === 'string') { |
|
return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8') |
|
} |
|
|
|
// Unusual. |
|
return fromObject(this, arg) |
|
} |
|
|
|
// TODO: Legacy, not needed anymore. Remove in next major version. |
|
Buffer._augment = function (arr) { |
|
arr.__proto__ = Buffer.prototype |
|
return arr |
|
} |
|
|
|
function fromNumber (that, length) { |
|
that = allocate(that, length < 0 ? 0 : checked(length) | 0) |
|
if (!Buffer.TYPED_ARRAY_SUPPORT) { |
|
for (var i = 0; i < length; i++) { |
|
that[i] = 0 |
|
} |
|
} |
|
return that |
|
} |
|
|
|
function fromString (that, string, encoding) { |
|
if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8' |
|
|
|
// Assumption: byteLength() return value is always < kMaxLength. |
|
var length = byteLength(string, encoding) | 0 |
|
that = allocate(that, length) |
|
|
|
that.write(string, encoding) |
|
return that |
|
} |
|
|
|
function fromObject (that, object) { |
|
if (Buffer.isBuffer(object)) return fromBuffer(that, object) |
|
|
|
if (isArray(object)) return fromArray(that, object) |
|
|
|
if (object == null) { |
|
throw new TypeError('must start with number, buffer, array or string') |
|
} |
|
|
|
if (typeof ArrayBuffer !== 'undefined') { |
|
if (object.buffer instanceof ArrayBuffer) { |
|
return fromTypedArray(that, object) |
|
} |
|
if (object instanceof ArrayBuffer) { |
|
return fromArrayBuffer(that, object) |
|
} |
|
} |
|
|
|
if (object.length) return fromArrayLike(that, object) |
|
|
|
return fromJsonObject(that, object) |
|
} |
|
|
|
function fromBuffer (that, buffer) { |
|
var length = checked(buffer.length) | 0 |
|
that = allocate(that, length) |
|
buffer.copy(that, 0, 0, length) |
|
return that |
|
} |
|
|
|
function fromArray (that, array) { |
|
var length = checked(array.length) | 0 |
|
that = allocate(that, length) |
|
for (var i = 0; i < length; i += 1) { |
|
that[i] = array[i] & 255 |
|
} |
|
return that |
|
} |
|
|
|
// Duplicate of fromArray() to keep fromArray() monomorphic. |
|
function fromTypedArray (that, array) { |
|
var length = checked(array.length) | 0 |
|
that = allocate(that, length) |
|
// Truncating the elements is probably not what people expect from typed |
|
// arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior |
|
// of the old Buffer constructor. |
|
for (var i = 0; i < length; i += 1) { |
|
that[i] = array[i] & 255 |
|
} |
|
return that |
|
} |
|
|
|
function fromArrayBuffer (that, array) { |
|
array.byteLength // this throws if `array` is not a valid ArrayBuffer |
|
|
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
// Return an augmented `Uint8Array` instance, for best performance |
|
that = new Uint8Array(array) |
|
that.__proto__ = Buffer.prototype |
|
} else { |
|
// Fallback: Return an object instance of the Buffer class |
|
that = fromTypedArray(that, new Uint8Array(array)) |
|
} |
|
return that |
|
} |
|
|
|
function fromArrayLike (that, array) { |
|
var length = checked(array.length) | 0 |
|
that = allocate(that, length) |
|
for (var i = 0; i < length; i += 1) { |
|
that[i] = array[i] & 255 |
|
} |
|
return that |
|
} |
|
|
|
// Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object. |
|
// Returns a zero-length buffer for inputs that don't conform to the spec. |
|
function fromJsonObject (that, object) { |
|
var array |
|
var length = 0 |
|
|
|
if (object.type === 'Buffer' && isArray(object.data)) { |
|
array = object.data |
|
length = checked(array.length) | 0 |
|
} |
|
that = allocate(that, length) |
|
|
|
for (var i = 0; i < length; i += 1) { |
|
that[i] = array[i] & 255 |
|
} |
|
return that |
|
} |
|
|
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
Buffer.prototype.__proto__ = Uint8Array.prototype |
|
Buffer.__proto__ = Uint8Array |
|
if (typeof Symbol !== 'undefined' && Symbol.species && |
|
Buffer[Symbol.species] === Buffer) { |
|
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 |
|
Object.defineProperty(Buffer, Symbol.species, { |
|
value: null, |
|
configurable: true |
|
}) |
|
} |
|
} else { |
|
// pre-set for values that may exist in the future |
|
Buffer.prototype.length = undefined |
|
Buffer.prototype.parent = undefined |
|
} |
|
|
|
function allocate (that, length) { |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
// Return an augmented `Uint8Array` instance, for best performance |
|
that = new Uint8Array(length) |
|
that.__proto__ = Buffer.prototype |
|
} else { |
|
// Fallback: Return an object instance of the Buffer class |
|
that.length = length |
|
} |
|
|
|
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 |
|
if (fromPool) that.parent = rootParent |
|
|
|
return that |
|
} |
|
|
|
function checked (length) { |
|
// Note: cannot use `length < kMaxLength` here because that fails when |
|
// length is NaN (which is otherwise coerced to zero.) |
|
if (length >= kMaxLength()) { |
|
throw new RangeError('Attempt to allocate Buffer larger than maximum ' + |
|
'size: 0x' + kMaxLength().toString(16) + ' bytes') |
|
} |
|
return length | 0 |
|
} |
|
|
|
function SlowBuffer (subject, encoding) { |
|
if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding) |
|
|
|
var buf = new Buffer(subject, encoding) |
|
delete buf.parent |
|
return buf |
|
} |
|
|
|
Buffer.isBuffer = function isBuffer (b) { |
|
return !!(b != null && b._isBuffer) |
|
} |
|
|
|
Buffer.compare = function compare (a, b) { |
|
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { |
|
throw new TypeError('Arguments must be Buffers') |
|
} |
|
|
|
if (a === b) return 0 |
|
|
|
var x = a.length |
|
var y = b.length |
|
|
|
var i = 0 |
|
var len = Math.min(x, y) |
|
while (i < len) { |
|
if (a[i] !== b[i]) break |
|
|
|
++i |
|
} |
|
|
|
if (i !== len) { |
|
x = a[i] |
|
y = b[i] |
|
} |
|
|
|
if (x < y) return -1 |
|
if (y < x) return 1 |
|
return 0 |
|
} |
|
|
|
Buffer.isEncoding = function isEncoding (encoding) { |
|
switch (String(encoding).toLowerCase()) { |
|
case 'hex': |
|
case 'utf8': |
|
case 'utf-8': |
|
case 'ascii': |
|
case 'binary': |
|
case 'base64': |
|
case 'raw': |
|
case 'ucs2': |
|
case 'ucs-2': |
|
case 'utf16le': |
|
case 'utf-16le': |
|
return true |
|
default: |
|
return false |
|
} |
|
} |
|
|
|
Buffer.concat = function concat (list, length) { |
|
if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.') |
|
|
|
if (list.length === 0) { |
|
return new Buffer(0) |
|
} |
|
|
|
var i |
|
if (length === undefined) { |
|
length = 0 |
|
for (i = 0; i < list.length; i++) { |
|
length += list[i].length |
|
} |
|
} |
|
|
|
var buf = new Buffer(length) |
|
var pos = 0 |
|
for (i = 0; i < list.length; i++) { |
|
var item = list[i] |
|
item.copy(buf, pos) |
|
pos += item.length |
|
} |
|
return buf |
|
} |
|
|
|
function byteLength (string, encoding) { |
|
if (typeof string !== 'string') string = '' + string |
|
|
|
var len = string.length |
|
if (len === 0) return 0 |
|
|
|
// Use a for loop to avoid recursion |
|
var loweredCase = false |
|
for (;;) { |
|
switch (encoding) { |
|
case 'ascii': |
|
case 'binary': |
|
// Deprecated |
|
case 'raw': |
|
case 'raws': |
|
return len |
|
case 'utf8': |
|
case 'utf-8': |
|
return utf8ToBytes(string).length |
|
case 'ucs2': |
|
case 'ucs-2': |
|
case 'utf16le': |
|
case 'utf-16le': |
|
return len * 2 |
|
case 'hex': |
|
return len >>> 1 |
|
case 'base64': |
|
return base64ToBytes(string).length |
|
default: |
|
if (loweredCase) return utf8ToBytes(string).length // assume utf8 |
|
encoding = ('' + encoding).toLowerCase() |
|
loweredCase = true |
|
} |
|
} |
|
} |
|
Buffer.byteLength = byteLength |
|
|
|
function slowToString (encoding, start, end) { |
|
var loweredCase = false |
|
|
|
start = start | 0 |
|
end = end === undefined || end === Infinity ? this.length : end | 0 |
|
|
|
if (!encoding) encoding = 'utf8' |
|
if (start < 0) start = 0 |
|
if (end > this.length) end = this.length |
|
if (end <= start) return '' |
|
|
|
while (true) { |
|
switch (encoding) { |
|
case 'hex': |
|
return hexSlice(this, start, end) |
|
|
|
case 'utf8': |
|
case 'utf-8': |
|
return utf8Slice(this, start, end) |
|
|
|
case 'ascii': |
|
return asciiSlice(this, start, end) |
|
|
|
case 'binary': |
|
return binarySlice(this, start, end) |
|
|
|
case 'base64': |
|
return base64Slice(this, start, end) |
|
|
|
case 'ucs2': |
|
case 'ucs-2': |
|
case 'utf16le': |
|
case 'utf-16le': |
|
return utf16leSlice(this, start, end) |
|
|
|
default: |
|
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) |
|
encoding = (encoding + '').toLowerCase() |
|
loweredCase = true |
|
} |
|
} |
|
} |
|
|
|
// The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect |
|
// Buffer instances. |
|
Buffer.prototype._isBuffer = true |
|
|
|
Buffer.prototype.toString = function toString () { |
|
var length = this.length | 0 |
|
if (length === 0) return '' |
|
if (arguments.length === 0) return utf8Slice(this, 0, length) |
|
return slowToString.apply(this, arguments) |
|
} |
|
|
|
Buffer.prototype.equals = function equals (b) { |
|
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') |
|
if (this === b) return true |
|
return Buffer.compare(this, b) === 0 |
|
} |
|
|
|
Buffer.prototype.inspect = function inspect () { |
|
var str = '' |
|
var max = exports.INSPECT_MAX_BYTES |
|
if (this.length > 0) { |
|
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') |
|
if (this.length > max) str += ' ... ' |
|
} |
|
return '<Buffer ' + str + '>' |
|
} |
|
|
|
Buffer.prototype.compare = function compare (b) { |
|
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') |
|
if (this === b) return 0 |
|
return Buffer.compare(this, b) |
|
} |
|
|
|
Buffer.prototype.indexOf = function indexOf (val, byteOffset) { |
|
if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff |
|
else if (byteOffset < -0x80000000) byteOffset = -0x80000000 |
|
byteOffset >>= 0 |
|
|
|
if (this.length === 0) return -1 |
|
if (byteOffset >= this.length) return -1 |
|
|
|
// Negative offsets start from the end of the buffer |
|
if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0) |
|
|
|
if (typeof val === 'string') { |
|
if (val.length === 0) return -1 // special case: looking for empty string always fails |
|
return String.prototype.indexOf.call(this, val, byteOffset) |
|
} |
|
if (Buffer.isBuffer(val)) { |
|
return arrayIndexOf(this, val, byteOffset) |
|
} |
|
if (typeof val === 'number') { |
|
if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') { |
|
return Uint8Array.prototype.indexOf.call(this, val, byteOffset) |
|
} |
|
return arrayIndexOf(this, [ val ], byteOffset) |
|
} |
|
|
|
function arrayIndexOf (arr, val, byteOffset) { |
|
var foundIndex = -1 |
|
for (var i = 0; byteOffset + i < arr.length; i++) { |
|
if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) { |
|
if (foundIndex === -1) foundIndex = i |
|
if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex |
|
} else { |
|
foundIndex = -1 |
|
} |
|
} |
|
return -1 |
|
} |
|
|
|
throw new TypeError('val must be string, number or Buffer') |
|
} |
|
|
|
function hexWrite (buf, string, offset, length) { |
|
offset = Number(offset) || 0 |
|
var remaining = buf.length - offset |
|
if (!length) { |
|
length = remaining |
|
} else { |
|
length = Number(length) |
|
if (length > remaining) { |
|
length = remaining |
|
} |
|
} |
|
|
|
// must be an even number of digits |
|
var strLen = string.length |
|
if (strLen % 2 !== 0) throw new Error('Invalid hex string') |
|
|
|
if (length > strLen / 2) { |
|
length = strLen / 2 |
|
} |
|
for (var i = 0; i < length; i++) { |
|
var parsed = parseInt(string.substr(i * 2, 2), 16) |
|
if (isNaN(parsed)) throw new Error('Invalid hex string') |
|
buf[offset + i] = parsed |
|
} |
|
return i |
|
} |
|
|
|
function utf8Write (buf, string, offset, length) { |
|
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) |
|
} |
|
|
|
function asciiWrite (buf, string, offset, length) { |
|
return blitBuffer(asciiToBytes(string), buf, offset, length) |
|
} |
|
|
|
function binaryWrite (buf, string, offset, length) { |
|
return asciiWrite(buf, string, offset, length) |
|
} |
|
|
|
function base64Write (buf, string, offset, length) { |
|
return blitBuffer(base64ToBytes(string), buf, offset, length) |
|
} |
|
|
|
function ucs2Write (buf, string, offset, length) { |
|
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) |
|
} |
|
|
|
Buffer.prototype.write = function write (string, offset, length, encoding) { |
|
// Buffer#write(string) |
|
if (offset === undefined) { |
|
encoding = 'utf8' |
|
length = this.length |
|
offset = 0 |
|
// Buffer#write(string, encoding) |
|
} else if (length === undefined && typeof offset === 'string') { |
|
encoding = offset |
|
length = this.length |
|
offset = 0 |
|
// Buffer#write(string, offset[, length][, encoding]) |
|
} else if (isFinite(offset)) { |
|
offset = offset | 0 |
|
if (isFinite(length)) { |
|
length = length | 0 |
|
if (encoding === undefined) encoding = 'utf8' |
|
} else { |
|
encoding = length |
|
length = undefined |
|
} |
|
// legacy write(string, encoding, offset, length) - remove in v0.13 |
|
} else { |
|
var swap = encoding |
|
encoding = offset |
|
offset = length | 0 |
|
length = swap |
|
} |
|
|
|
var remaining = this.length - offset |
|
if (length === undefined || length > remaining) length = remaining |
|
|
|
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { |
|
throw new RangeError('attempt to write outside buffer bounds') |
|
} |
|
|
|
if (!encoding) encoding = 'utf8' |
|
|
|
var loweredCase = false |
|
for (;;) { |
|
switch (encoding) { |
|
case 'hex': |
|
return hexWrite(this, string, offset, length) |
|
|
|
case 'utf8': |
|
case 'utf-8': |
|
return utf8Write(this, string, offset, length) |
|
|
|
case 'ascii': |
|
return asciiWrite(this, string, offset, length) |
|
|
|
case 'binary': |
|
return binaryWrite(this, string, offset, length) |
|
|
|
case 'base64': |
|
// Warning: maxLength not taken into account in base64Write |
|
return base64Write(this, string, offset, length) |
|
|
|
case 'ucs2': |
|
case 'ucs-2': |
|
case 'utf16le': |
|
case 'utf-16le': |
|
return ucs2Write(this, string, offset, length) |
|
|
|
default: |
|
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) |
|
encoding = ('' + encoding).toLowerCase() |
|
loweredCase = true |
|
} |
|
} |
|
} |
|
|
|
Buffer.prototype.toJSON = function toJSON () { |
|
return { |
|
type: 'Buffer', |
|
data: Array.prototype.slice.call(this._arr || this, 0) |
|
} |
|
} |
|
|
|
function base64Slice (buf, start, end) { |
|
if (start === 0 && end === buf.length) { |
|
return base64.fromByteArray(buf) |
|
} else { |
|
return base64.fromByteArray(buf.slice(start, end)) |
|
} |
|
} |
|
|
|
function utf8Slice (buf, start, end) { |
|
end = Math.min(buf.length, end) |
|
var res = [] |
|
|
|
var i = start |
|
while (i < end) { |
|
var firstByte = buf[i] |
|
var codePoint = null |
|
var bytesPerSequence = (firstByte > 0xEF) ? 4 |
|
: (firstByte > 0xDF) ? 3 |
|
: (firstByte > 0xBF) ? 2 |
|
: 1 |
|
|
|
if (i + bytesPerSequence <= end) { |
|
var secondByte, thirdByte, fourthByte, tempCodePoint |
|
|
|
switch (bytesPerSequence) { |
|
case 1: |
|
if (firstByte < 0x80) { |
|
codePoint = firstByte |
|
} |
|
break |
|
case 2: |
|
secondByte = buf[i + 1] |
|
if ((secondByte & 0xC0) === 0x80) { |
|
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) |
|
if (tempCodePoint > 0x7F) { |
|
codePoint = tempCodePoint |
|
} |
|
} |
|
break |
|
case 3: |
|
secondByte = buf[i + 1] |
|
thirdByte = buf[i + 2] |
|
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { |
|
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) |
|
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { |
|
codePoint = tempCodePoint |
|
} |
|
} |
|
break |
|
case 4: |
|
secondByte = buf[i + 1] |
|
thirdByte = buf[i + 2] |
|
fourthByte = buf[i + 3] |
|
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { |
|
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) |
|
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { |
|
codePoint = tempCodePoint |
|
} |
|
} |
|
} |
|
} |
|
|
|
if (codePoint === null) { |
|
// we did not generate a valid codePoint so insert a |
|
// replacement char (U+FFFD) and advance only 1 byte |
|
codePoint = 0xFFFD |
|
bytesPerSequence = 1 |
|
} else if (codePoint > 0xFFFF) { |
|
// encode to utf16 (surrogate pair dance) |
|
codePoint -= 0x10000 |
|
res.push(codePoint >>> 10 & 0x3FF | 0xD800) |
|
codePoint = 0xDC00 | codePoint & 0x3FF |
|
} |
|
|
|
res.push(codePoint) |
|
i += bytesPerSequence |
|
} |
|
|
|
return decodeCodePointsArray(res) |
|
} |
|
|
|
// Based on http://stackoverflow.com/a/22747272/680742, the browser with |
|
// the lowest limit is Chrome, with 0x10000 args. |
|
// We go 1 magnitude less, for safety |
|
var MAX_ARGUMENTS_LENGTH = 0x1000 |
|
|
|
function decodeCodePointsArray (codePoints) { |
|
var len = codePoints.length |
|
if (len <= MAX_ARGUMENTS_LENGTH) { |
|
return String.fromCharCode.apply(String, codePoints) // avoid extra slice() |
|
} |
|
|
|
// Decode in chunks to avoid "call stack size exceeded". |
|
var res = '' |
|
var i = 0 |
|
while (i < len) { |
|
res += String.fromCharCode.apply( |
|
String, |
|
codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) |
|
) |
|
} |
|
return res |
|
} |
|
|
|
function asciiSlice (buf, start, end) { |
|
var ret = '' |
|
end = Math.min(buf.length, end) |
|
|
|
for (var i = start; i < end; i++) { |
|
ret += String.fromCharCode(buf[i] & 0x7F) |
|
} |
|
return ret |
|
} |
|
|
|
function binarySlice (buf, start, end) { |
|
var ret = '' |
|
end = Math.min(buf.length, end) |
|
|
|
for (var i = start; i < end; i++) { |
|
ret += String.fromCharCode(buf[i]) |
|
} |
|
return ret |
|
} |
|
|
|
function hexSlice (buf, start, end) { |
|
var len = buf.length |
|
|
|
if (!start || start < 0) start = 0 |
|
if (!end || end < 0 || end > len) end = len |
|
|
|
var out = '' |
|
for (var i = start; i < end; i++) { |
|
out += toHex(buf[i]) |
|
} |
|
return out |
|
} |
|
|
|
function utf16leSlice (buf, start, end) { |
|
var bytes = buf.slice(start, end) |
|
var res = '' |
|
for (var i = 0; i < bytes.length; i += 2) { |
|
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256) |
|
} |
|
return res |
|
} |
|
|
|
Buffer.prototype.slice = function slice (start, end) { |
|
var len = this.length |
|
start = ~~start |
|
end = end === undefined ? len : ~~end |
|
|
|
if (start < 0) { |
|
start += len |
|
if (start < 0) start = 0 |
|
} else if (start > len) { |
|
start = len |
|
} |
|
|
|
if (end < 0) { |
|
end += len |
|
if (end < 0) end = 0 |
|
} else if (end > len) { |
|
end = len |
|
} |
|
|
|
if (end < start) end = start |
|
|
|
var newBuf |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
newBuf = this.subarray(start, end) |
|
newBuf.__proto__ = Buffer.prototype |
|
} else { |
|
var sliceLen = end - start |
|
newBuf = new Buffer(sliceLen, undefined) |
|
for (var i = 0; i < sliceLen; i++) { |
|
newBuf[i] = this[i + start] |
|
} |
|
} |
|
|
|
if (newBuf.length) newBuf.parent = this.parent || this |
|
|
|
return newBuf |
|
} |
|
|
|
/* |
|
* Need to make sure that buffer isn't trying to write out of bounds. |
|
*/ |
|
function checkOffset (offset, ext, length) { |
|
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') |
|
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') |
|
} |
|
|
|
Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) checkOffset(offset, byteLength, this.length) |
|
|
|
var val = this[offset] |
|
var mul = 1 |
|
var i = 0 |
|
while (++i < byteLength && (mul *= 0x100)) { |
|
val += this[offset + i] * mul |
|
} |
|
|
|
return val |
|
} |
|
|
|
Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) { |
|
checkOffset(offset, byteLength, this.length) |
|
} |
|
|
|
var val = this[offset + --byteLength] |
|
var mul = 1 |
|
while (byteLength > 0 && (mul *= 0x100)) { |
|
val += this[offset + --byteLength] * mul |
|
} |
|
|
|
return val |
|
} |
|
|
|
Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 1, this.length) |
|
return this[offset] |
|
} |
|
|
|
Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 2, this.length) |
|
return this[offset] | (this[offset + 1] << 8) |
|
} |
|
|
|
Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 2, this.length) |
|
return (this[offset] << 8) | this[offset + 1] |
|
} |
|
|
|
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
|
|
return ((this[offset]) | |
|
(this[offset + 1] << 8) | |
|
(this[offset + 2] << 16)) + |
|
(this[offset + 3] * 0x1000000) |
|
} |
|
|
|
Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
|
|
return (this[offset] * 0x1000000) + |
|
((this[offset + 1] << 16) | |
|
(this[offset + 2] << 8) | |
|
this[offset + 3]) |
|
} |
|
|
|
Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) checkOffset(offset, byteLength, this.length) |
|
|
|
var val = this[offset] |
|
var mul = 1 |
|
var i = 0 |
|
while (++i < byteLength && (mul *= 0x100)) { |
|
val += this[offset + i] * mul |
|
} |
|
mul *= 0x80 |
|
|
|
if (val >= mul) val -= Math.pow(2, 8 * byteLength) |
|
|
|
return val |
|
} |
|
|
|
Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) checkOffset(offset, byteLength, this.length) |
|
|
|
var i = byteLength |
|
var mul = 1 |
|
var val = this[offset + --i] |
|
while (i > 0 && (mul *= 0x100)) { |
|
val += this[offset + --i] * mul |
|
} |
|
mul *= 0x80 |
|
|
|
if (val >= mul) val -= Math.pow(2, 8 * byteLength) |
|
|
|
return val |
|
} |
|
|
|
Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 1, this.length) |
|
if (!(this[offset] & 0x80)) return (this[offset]) |
|
return ((0xff - this[offset] + 1) * -1) |
|
} |
|
|
|
Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 2, this.length) |
|
var val = this[offset] | (this[offset + 1] << 8) |
|
return (val & 0x8000) ? val | 0xFFFF0000 : val |
|
} |
|
|
|
Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 2, this.length) |
|
var val = this[offset + 1] | (this[offset] << 8) |
|
return (val & 0x8000) ? val | 0xFFFF0000 : val |
|
} |
|
|
|
Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
|
|
return (this[offset]) | |
|
(this[offset + 1] << 8) | |
|
(this[offset + 2] << 16) | |
|
(this[offset + 3] << 24) |
|
} |
|
|
|
Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
|
|
return (this[offset] << 24) | |
|
(this[offset + 1] << 16) | |
|
(this[offset + 2] << 8) | |
|
(this[offset + 3]) |
|
} |
|
|
|
Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
return ieee754.read(this, offset, true, 23, 4) |
|
} |
|
|
|
Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 4, this.length) |
|
return ieee754.read(this, offset, false, 23, 4) |
|
} |
|
|
|
Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 8, this.length) |
|
return ieee754.read(this, offset, true, 52, 8) |
|
} |
|
|
|
Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { |
|
if (!noAssert) checkOffset(offset, 8, this.length) |
|
return ieee754.read(this, offset, false, 52, 8) |
|
} |
|
|
|
function checkInt (buf, value, offset, ext, max, min) { |
|
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') |
|
if (value > max || value < min) throw new RangeError('value is out of bounds') |
|
if (offset + ext > buf.length) throw new RangeError('index out of range') |
|
} |
|
|
|
Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) |
|
|
|
var mul = 1 |
|
var i = 0 |
|
this[offset] = value & 0xFF |
|
while (++i < byteLength && (mul *= 0x100)) { |
|
this[offset + i] = (value / mul) & 0xFF |
|
} |
|
|
|
return offset + byteLength |
|
} |
|
|
|
Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
byteLength = byteLength | 0 |
|
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) |
|
|
|
var i = byteLength - 1 |
|
var mul = 1 |
|
this[offset + i] = value & 0xFF |
|
while (--i >= 0 && (mul *= 0x100)) { |
|
this[offset + i] = (value / mul) & 0xFF |
|
} |
|
|
|
return offset + byteLength |
|
} |
|
|
|
Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) |
|
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) |
|
this[offset] = (value & 0xff) |
|
return offset + 1 |
|
} |
|
|
|
function objectWriteUInt16 (buf, value, offset, littleEndian) { |
|
if (value < 0) value = 0xffff + value + 1 |
|
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { |
|
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> |
|
(littleEndian ? i : 1 - i) * 8 |
|
} |
|
} |
|
|
|
Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value & 0xff) |
|
this[offset + 1] = (value >>> 8) |
|
} else { |
|
objectWriteUInt16(this, value, offset, true) |
|
} |
|
return offset + 2 |
|
} |
|
|
|
Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value >>> 8) |
|
this[offset + 1] = (value & 0xff) |
|
} else { |
|
objectWriteUInt16(this, value, offset, false) |
|
} |
|
return offset + 2 |
|
} |
|
|
|
function objectWriteUInt32 (buf, value, offset, littleEndian) { |
|
if (value < 0) value = 0xffffffff + value + 1 |
|
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { |
|
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff |
|
} |
|
} |
|
|
|
Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset + 3] = (value >>> 24) |
|
this[offset + 2] = (value >>> 16) |
|
this[offset + 1] = (value >>> 8) |
|
this[offset] = (value & 0xff) |
|
} else { |
|
objectWriteUInt32(this, value, offset, true) |
|
} |
|
return offset + 4 |
|
} |
|
|
|
Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value >>> 24) |
|
this[offset + 1] = (value >>> 16) |
|
this[offset + 2] = (value >>> 8) |
|
this[offset + 3] = (value & 0xff) |
|
} else { |
|
objectWriteUInt32(this, value, offset, false) |
|
} |
|
return offset + 4 |
|
} |
|
|
|
Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) { |
|
var limit = Math.pow(2, 8 * byteLength - 1) |
|
|
|
checkInt(this, value, offset, byteLength, limit - 1, -limit) |
|
} |
|
|
|
var i = 0 |
|
var mul = 1 |
|
var sub = value < 0 ? 1 : 0 |
|
this[offset] = value & 0xFF |
|
while (++i < byteLength && (mul *= 0x100)) { |
|
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF |
|
} |
|
|
|
return offset + byteLength |
|
} |
|
|
|
Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) { |
|
var limit = Math.pow(2, 8 * byteLength - 1) |
|
|
|
checkInt(this, value, offset, byteLength, limit - 1, -limit) |
|
} |
|
|
|
var i = byteLength - 1 |
|
var mul = 1 |
|
var sub = value < 0 ? 1 : 0 |
|
this[offset + i] = value & 0xFF |
|
while (--i >= 0 && (mul *= 0x100)) { |
|
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF |
|
} |
|
|
|
return offset + byteLength |
|
} |
|
|
|
Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) |
|
if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) |
|
if (value < 0) value = 0xff + value + 1 |
|
this[offset] = (value & 0xff) |
|
return offset + 1 |
|
} |
|
|
|
Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value & 0xff) |
|
this[offset + 1] = (value >>> 8) |
|
} else { |
|
objectWriteUInt16(this, value, offset, true) |
|
} |
|
return offset + 2 |
|
} |
|
|
|
Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value >>> 8) |
|
this[offset + 1] = (value & 0xff) |
|
} else { |
|
objectWriteUInt16(this, value, offset, false) |
|
} |
|
return offset + 2 |
|
} |
|
|
|
Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value & 0xff) |
|
this[offset + 1] = (value >>> 8) |
|
this[offset + 2] = (value >>> 16) |
|
this[offset + 3] = (value >>> 24) |
|
} else { |
|
objectWriteUInt32(this, value, offset, true) |
|
} |
|
return offset + 4 |
|
} |
|
|
|
Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { |
|
value = +value |
|
offset = offset | 0 |
|
if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) |
|
if (value < 0) value = 0xffffffff + value + 1 |
|
if (Buffer.TYPED_ARRAY_SUPPORT) { |
|
this[offset] = (value >>> 24) |
|
this[offset + 1] = (value >>> 16) |
|
this[offset + 2] = (value >>> 8) |
|
this[offset + 3] = (value & 0xff) |
|
} else { |
|
objectWriteUInt32(this, value, offset, false) |
|
} |
|
return offset + 4 |
|
} |
|
|
|
function checkIEEE754 (buf, value, offset, ext, max, min) { |
|
if (offset + ext > buf.length) throw new RangeError('index out of range') |
|
if (offset < 0) throw new RangeError('index out of range') |
|
} |
|
|
|
function writeFloat (buf, value, offset, littleEndian, noAssert) { |
|
if (!noAssert) { |
|
checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) |
|
} |
|
ieee754.write(buf, value, offset, littleEndian, 23, 4) |
|
return offset + 4 |
|
} |
|
|
|
Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { |
|
return writeFloat(this, value, offset, true, noAssert) |
|
} |
|
|
|
Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { |
|
return writeFloat(this, value, offset, false, noAssert) |
|
} |
|
|
|
function writeDouble (buf, value, offset, littleEndian, noAssert) { |
|
if (!noAssert) { |
|
checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) |
|
} |
|
ieee754.write(buf, value, offset, littleEndian, 52, 8) |
|
return offset + 8 |
|
} |
|
|
|
Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { |
|
return writeDouble(this, value, offset, true, noAssert) |
|
} |
|
|
|
Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { |
|
return writeDouble(this, value, offset, false, noAssert) |
|
} |
|
|
|
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) |
|
Buffer.prototype.copy = function copy (target, targetStart, start, end) { |
|
if (!start) start = 0 |
|
if (!end && end !== 0) end = this.length |
|
if (targetStart >= target.length) targetStart = target.length |
|
if (!targetStart) targetStart = 0 |
|
if (end > 0 && end < start) end = start |
|
|
|
// Copy 0 bytes; we're done |
|
if (end === start) return 0 |
|
if (target.length === 0 || this.length === 0) return 0 |
|
|
|
// Fatal error conditions |
|
if (targetStart < 0) { |
|
throw new RangeError('targetStart out of bounds') |
|
} |
|
if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') |
|
if (end < 0) throw new RangeError('sourceEnd out of bounds') |
|
|
|
// Are we oob? |
|
if (end > this.length) end = this.length |
|
if (target.length - targetStart < end - start) { |
|
end = target.length - targetStart + start |
|
} |
|
|
|
var len = end - start |
|
var i |
|
|
|
if (this === target && start < targetStart && targetStart < end) { |
|
// descending copy from end |
|
for (i = len - 1; i >= 0; i--) { |
|
target[i + targetStart] = this[i + start] |
|
} |
|
} else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) { |
|
// ascending copy from start |
|
for (i = 0; i < len; i++) { |
|
target[i + targetStart] = this[i + start] |
|
} |
|
} else { |
|
Uint8Array.prototype.set.call( |
|
target, |
|
this.subarray(start, start + len), |
|
targetStart |
|
) |
|
} |
|
|
|
return len |
|
} |
|
|
|
// fill(value, start=0, end=buffer.length) |
|
Buffer.prototype.fill = function fill (value, start, end) { |
|
if (!value) value = 0 |
|
if (!start) start = 0 |
|
if (!end) end = this.length |
|
|
|
if (end < start) throw new RangeError('end < start') |
|
|
|
// Fill 0 bytes; we're done |
|
if (end === start) return |
|
if (this.length === 0) return |
|
|
|
if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') |
|
if (end < 0 || end > this.length) throw new RangeError('end out of bounds') |
|
|
|
var i |
|
if (typeof value === 'number') { |
|
for (i = start; i < end; i++) { |
|
this[i] = value |
|
} |
|
} else { |
|
var bytes = utf8ToBytes(value.toString()) |
|
var len = bytes.length |
|
for (i = start; i < end; i++) { |
|
this[i] = bytes[i % len] |
|
} |
|
} |
|
|
|
return this |
|
} |
|
|
|
// HELPER FUNCTIONS |
|
// ================ |
|
|
|
var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g |
|
|
|
function base64clean (str) { |
|
// Node strips out invalid characters like \n and \t from the string, base64-js does not |
|
str = stringtrim(str).replace(INVALID_BASE64_RE, '') |
|
// Node converts strings with length < 2 to '' |
|
if (str.length < 2) return '' |
|
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not |
|
while (str.length % 4 !== 0) { |
|
str = str + '=' |
|
} |
|
return str |
|
} |
|
|
|
function stringtrim (str) { |
|
if (str.trim) return str.trim() |
|
return str.replace(/^\s+|\s+$/g, '') |
|
} |
|
|
|
function toHex (n) { |
|
if (n < 16) return '0' + n.toString(16) |
|
return n.toString(16) |
|
} |
|
|
|
function utf8ToBytes (string, units) { |
|
units = units || Infinity |
|
var codePoint |
|
var length = string.length |
|
var leadSurrogate = null |
|
var bytes = [] |
|
|
|
for (var i = 0; i < length; i++) { |
|
codePoint = string.charCodeAt(i) |
|
|
|
// is surrogate component |
|
if (codePoint > 0xD7FF && codePoint < 0xE000) { |
|
// last char was a lead |
|
if (!leadSurrogate) { |
|
// no lead yet |
|
if (codePoint > 0xDBFF) { |
|
// unexpected trail |
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
|
continue |
|
} else if (i + 1 === length) { |
|
// unpaired lead |
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
|
continue |
|
} |
|
|
|
// valid lead |
|
leadSurrogate = codePoint |
|
|
|
continue |
|
} |
|
|
|
// 2 leads in a row |
|
if (codePoint < 0xDC00) { |
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
|
leadSurrogate = codePoint |
|
continue |
|
} |
|
|
|
// valid surrogate pair |
|
codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 |
|
} else if (leadSurrogate) { |
|
// valid bmp char, but last char was a lead |
|
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) |
|
} |
|
|
|
leadSurrogate = null |
|
|
|
// encode utf8 |
|
if (codePoint < 0x80) { |
|
if ((units -= 1) < 0) break |
|
bytes.push(codePoint) |
|
} else if (codePoint < 0x800) { |
|
if ((units -= 2) < 0) break |
|
bytes.push( |
|
codePoint >> 0x6 | 0xC0, |
|
codePoint & 0x3F | 0x80 |
|
) |
|
} else if (codePoint < 0x10000) { |
|
if ((units -= 3) < 0) break |
|
bytes.push( |
|
codePoint >> 0xC | 0xE0, |
|
codePoint >> 0x6 & 0x3F | 0x80, |
|
codePoint & 0x3F | 0x80 |
|
) |
|
} else if (codePoint < 0x110000) { |
|
if ((units -= 4) < 0) break |
|
bytes.push( |
|
codePoint >> 0x12 | 0xF0, |
|
codePoint >> 0xC & 0x3F | 0x80, |
|
codePoint >> 0x6 & 0x3F | 0x80, |
|
codePoint & 0x3F | 0x80 |
|
) |
|
} else { |
|
throw new Error('Invalid code point') |
|
} |
|
} |
|
|
|
return bytes |
|
} |
|
|
|
function asciiToBytes (str) { |
|
var byteArray = [] |
|
for (var i = 0; i < str.length; i++) { |
|
// Node's code seems to be doing this and not & 0x7F.. |
|
byteArray.push(str.charCodeAt(i) & 0xFF) |
|
} |
|
return byteArray |
|
} |
|
|
|
function utf16leToBytes (str, units) { |
|
var c, hi, lo |
|
var byteArray = [] |
|
for (var i = 0; i < str.length; i++) { |
|
if ((units -= 2) < 0) break |
|
|
|
c = str.charCodeAt(i) |
|
hi = c >> 8 |
|
lo = c % 256 |
|
byteArray.push(lo) |
|
byteArray.push(hi) |
|
} |
|
|
|
return byteArray |
|
} |
|
|
|
function base64ToBytes (str) { |
|
return base64.toByteArray(base64clean(str)) |
|
} |
|
|
|
function blitBuffer (src, dst, offset, length) { |
|
for (var i = 0; i < length; i++) { |
|
if ((i + offset >= dst.length) || (i >= src.length)) break |
|
dst[i + offset] = src[i] |
|
} |
|
return i |
|
} |
|
|
|
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) |
|
},{"base64-js":44,"ieee754":50,"isarray":47}],47:[function(require,module,exports){ |
|
var toString = {}.toString; |
|
|
|
module.exports = Array.isArray || function (arr) { |
|
return toString.call(arr) == '[object Array]'; |
|
}; |
|
|
|
},{}],48:[function(require,module,exports){ |
|
(function (Buffer){ |
|
// 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. |
|
|
|
// NOTE: These type checking functions intentionally don't use `instanceof` |
|
// because it is fragile and can be easily faked with `Object.create()`. |
|
|
|
function isArray(arg) { |
|
if (Array.isArray) { |
|
return Array.isArray(arg); |
|
} |
|
return objectToString(arg) === '[object Array]'; |
|
} |
|
exports.isArray = isArray; |
|
|
|
function isBoolean(arg) { |
|
return typeof arg === 'boolean'; |
|
} |
|
exports.isBoolean = isBoolean; |
|
|
|
function isNull(arg) { |
|
return arg === null; |
|
} |
|
exports.isNull = isNull; |
|
|
|
function isNullOrUndefined(arg) { |
|
return arg == null; |
|
} |
|
exports.isNullOrUndefined = isNullOrUndefined; |
|
|
|
function isNumber(arg) { |
|
return typeof arg === 'number'; |
|
} |
|
exports.isNumber = isNumber; |
|
|
|
function isString(arg) { |
|
return typeof arg === 'string'; |
|
} |
|
exports.isString = isString; |
|
|
|
function isSymbol(arg) { |
|
return typeof arg === 'symbol'; |
|
} |
|
exports.isSymbol = isSymbol; |
|
|
|
function isUndefined(arg) { |
|
return arg === void 0; |
|
} |
|
exports.isUndefined = isUndefined; |
|
|
|
function isRegExp(re) { |
|
return objectToString(re) === '[object RegExp]'; |
|
} |
|
exports.isRegExp = isRegExp; |
|
|
|
function isObject(arg) { |
|
return typeof arg === 'object' && arg !== null; |
|
} |
|
exports.isObject = isObject; |
|
|
|
function isDate(d) { |
|
return objectToString(d) === '[object Date]'; |
|
} |
|
exports.isDate = isDate; |
|
|
|
function isError(e) { |
|
return (objectToString(e) === '[object Error]' || e instanceof Error); |
|
} |
|
exports.isError = isError; |
|
|
|
function isFunction(arg) { |
|
return typeof arg === 'function'; |
|
} |
|
exports.isFunction = isFunction; |
|
|
|
function isPrimitive(arg) { |
|
return arg === null || |
|
typeof arg === 'boolean' || |
|
typeof arg === 'number' || |
|
typeof arg === 'string' || |
|
typeof arg === 'symbol' || // ES6 symbol |
|
typeof arg === 'undefined'; |
|
} |
|
exports.isPrimitive = isPrimitive; |
|
|
|
exports.isBuffer = Buffer.isBuffer; |
|
|
|
function objectToString(o) { |
|
return Object.prototype.toString.call(o); |
|
} |
|
|
|
}).call(this,{"isBuffer":require("../../is-buffer/index.js")}) |
|
},{"../../is-buffer/index.js":52}],49:[function(require,module,exports){ |
|
// 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. |
|
|
|
function EventEmitter() { |
|
this._events = this._events || {}; |
|
this._maxListeners = this._maxListeners || undefined; |
|
} |
|
module.exports = EventEmitter; |
|
|
|
// Backwards-compat with node 0.10.x |
|
EventEmitter.EventEmitter = EventEmitter; |
|
|
|
EventEmitter.prototype._events = undefined; |
|
EventEmitter.prototype._maxListeners = undefined; |
|
|
|
// By default EventEmitters will print a warning if more than 10 listeners are |
|
// added to it. This is a useful default which helps finding memory leaks. |
|
EventEmitter.defaultMaxListeners = 10; |
|
|
|
// Obviously not all Emitters should be limited to 10. This function allows |
|
// that to be increased. Set to zero for unlimited. |
|
EventEmitter.prototype.setMaxListeners = function(n) { |
|
if (!isNumber(n) || n < 0 || isNaN(n)) |
|
throw TypeError('n must be a positive number'); |
|
this._maxListeners = n; |
|
return this; |
|
}; |
|
|
|
EventEmitter.prototype.emit = function(type) { |
|
var er, handler, len, args, i, listeners; |
|
|
|
if (!this._events) |
|
this._events = {}; |
|
|
|
// If there is no 'error' event listener then throw. |
|
if (type === 'error') { |
|
if (!this._events.error || |
|
(isObject(this._events.error) && !this._events.error.length)) { |
|
er = arguments[1]; |
|
if (er instanceof Error) { |
|
throw er; // Unhandled 'error' event |
|
} |
|
throw TypeError('Uncaught, unspecified "error" event.'); |
|
} |
|
} |
|
|
|
handler = this._events[type]; |
|
|
|
if (isUndefined(handler)) |
|
return false; |
|
|
|
if (isFunction(handler)) { |
|
switch (arguments.length) { |
|
// fast cases |
|
case 1: |
|
handler.call(this); |
|
break; |
|
case 2: |
|
handler.call(this, arguments[1]); |
|
break; |
|
case 3: |
|
handler.call(this, arguments[1], arguments[2]); |
|
break; |
|
// slower |
|
default: |
|
args = Array.prototype.slice.call(arguments, 1); |
|
handler.apply(this, args); |
|
} |
|
} else if (isObject(handler)) { |
|
args = Array.prototype.slice.call(arguments, 1); |
|
listeners = handler.slice(); |
|
len = listeners.length; |
|
for (i = 0; i < len; i++) |
|
listeners[i].apply(this, args); |
|
} |
|
|
|
return true; |
|
}; |
|
|
|
EventEmitter.prototype.addListener = function(type, listener) { |
|
var m; |
|
|
|
if (!isFunction(listener)) |
|
throw TypeError('listener must be a function'); |
|
|
|
if (!this._events) |
|
this._events = {}; |
|
|
|
// To avoid recursion in the case that type === "newListener"! Before |
|
// adding it to the listeners, first emit "newListener". |
|
if (this._events.newListener) |
|
this.emit('newListener', type, |
|
isFunction(listener.listener) ? |
|
listener.listener : listener); |
|
|
|
if (!this._events[type]) |
|
// Optimize the case of one listener. Don't need the extra array object. |
|
this._events[type] = listener; |
|
else if (isObject(this._events[type])) |
|
// If we've already got an array, just append. |
|
this._events[type].push(listener); |
|
else |
|
// Adding the second element, need to change to array. |
|
this._events[type] = [this._events[type], listener]; |
|
|
|
// Check for listener leak |
|
if (isObject(this._events[type]) && !this._events[type].warned) { |
|
if (!isUndefined(this._maxListeners)) { |
|
m = this._maxListeners; |
|
} else { |
|
m = EventEmitter.defaultMaxListeners; |
|
} |
|
|
|
if (m && m > 0 && this._events[type].length > m) { |
|
this._events[type].warned = true; |
|
console.error('(node) warning: possible EventEmitter memory ' + |
|
'leak detected. %d listeners added. ' + |
|
'Use emitter.setMaxListeners() to increase limit.', |
|
this._events[type].length); |
|
if (typeof console.trace === 'function') { |
|
// not supported in IE 10 |
|
console.trace(); |
|
} |
|
} |
|
} |
|
|
|
return this; |
|
}; |
|
|
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener; |
|
|
|
EventEmitter.prototype.once = function(type, listener) { |
|
if (!isFunction(listener)) |
|
throw TypeError('listener must be a function'); |
|
|
|
var fired = false; |
|
|
|
function g() { |
|
this.removeListener(type, g); |
|
|
|
if (!fired) { |
|
fired = true; |
|
listener.apply(this, arguments); |
|
} |
|
} |
|
|
|
g.listener = listener; |
|
this.on(type, g); |
|
|
|
return this; |
|
}; |
|
|
|
// emits a 'removeListener' event iff the listener was removed |
|
EventEmitter.prototype.removeListener = function(type, listener) { |
|
var list, position, length, i; |
|
|
|
if (!isFunction(listener)) |
|
throw TypeError('listener must be a function'); |
|
|
|
if (!this._events || !this._events[type]) |
|
return this; |
|
|
|
list = this._events[type]; |
|
length = list.length; |
|
position = -1; |
|
|
|
if (list === listener || |
|
(isFunction(list.listener) && list.listener === listener)) { |
|
delete this._events[type]; |
|
if (this._events.removeListener) |
|
this.emit('removeListener', type, listener); |
|
|
|
} else if (isObject(list)) { |
|
for (i = length; i-- > 0;) { |
|
if (list[i] === listener || |
|
(list[i].listener && list[i].listener === listener)) { |
|
position = i; |
|
break; |
|
} |
|
} |
|
|
|
if (position < 0) |
|
return this; |
|
|
|
if (list.length === 1) { |
|
list.length = 0; |
|
delete this._events[type]; |
|
} else { |
|
list.splice(position, 1); |
|
} |
|
|
|
if (this._events.removeListener) |
|
this.emit('removeListener', type, listener); |
|
} |
|
|
|
return this; |
|
}; |
|
|
|
EventEmitter.prototype.removeAllListeners = function(type) { |
|
var key, listeners; |
|
|
|
if (!this._events) |
|
return this; |
|
|
|
// not listening for removeListener, no need to emit |
|
if (!this._events.removeListener) { |
|
if (arguments.length === 0) |
|
this._events = {}; |
|
else if (this._events[type]) |
|
delete this._events[type]; |
|
return this; |
|
} |
|
|
|
// emit removeListener for all listeners on all events |
|
if (arguments.length === 0) { |
|
for (key in this._events) { |
|
if (key === 'removeListener') continue; |
|
this.removeAllListeners(key); |
|
} |
|
this.removeAllListeners('removeListener'); |
|
this._events = {}; |
|
return this; |
|
} |
|
|
|
listeners = this._events[type]; |
|
|
|
if (isFunction(listeners)) { |
|
this.removeListener(type, listeners); |
|
} else if (listeners) { |
|
// LIFO order |
|
while (listeners.length) |
|
this.removeListener(type, listeners[listeners.length - 1]); |
|
} |
|
delete this._events[type]; |
|
|
|
return this; |
|
}; |
|
|
|
EventEmitter.prototype.listeners = function(type) { |
|
var ret; |
|
if (!this._events || !this._events[type]) |
|
ret = []; |
|
else if (isFunction(this._events[type])) |
|
ret = [this._events[type]]; |
|
else |
|
ret = this._events[type].slice(); |
|
return ret; |
|
}; |
|
|
|
EventEmitter.prototype.listenerCount = function(type) { |
|
if (this._events) { |
|
var evlistener = this._events[type]; |
|
|
|
if (isFunction(evlistener)) |
|
return 1; |
|
else if (evlistener) |
|
return evlistener.length; |
|
} |
|
return 0; |
|
}; |
|
|
|
EventEmitter.listenerCount = function(emitter, type) { |
|
return emitter.listenerCount(type); |
|
}; |
|
|
|
function isFunction(arg) { |
|
return typeof arg === 'function'; |
|
} |
|
|
|
function isNumber(arg) { |
|
return typeof arg === 'number'; |
|
} |
|
|
|
function isObject(arg) { |
|
return typeof arg === 'object' && arg !== null; |
|
} |
|
|
|
function isUndefined(arg) { |
|
return arg === void 0; |
|
} |
|
|
|
},{}],50:[function(require,module,exports){ |
|
exports.read = function (buffer, offset, isLE, mLen, nBytes) { |
|
var e, m |
|
var eLen = nBytes * 8 - mLen - 1 |
|
var eMax = (1 << eLen) - 1 |
|
var eBias = eMax >> 1 |
|
var nBits = -7 |
|
var i = isLE ? (nBytes - 1) : 0 |
|
var d = isLE ? -1 : 1 |
|
var s = buffer[offset + i] |
|
|
|
i += d |
|
|
|
e = s & ((1 << (-nBits)) - 1) |
|
s >>= (-nBits) |
|
nBits += eLen |
|
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} |
|
|
|
m = e & ((1 << (-nBits)) - 1) |
|
e >>= (-nBits) |
|
nBits += mLen |
|
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} |
|
|
|
if (e === 0) { |
|
e = 1 - eBias |
|
} else if (e === eMax) { |
|
return m ? NaN : ((s ? -1 : 1) * Infinity) |
|
} else { |
|
m = m + Math.pow(2, mLen) |
|
e = e - eBias |
|
} |
|
return (s ? -1 : 1) * m * Math.pow(2, e - mLen) |
|
} |
|
|
|
exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { |
|
var e, m, c |
|
var eLen = nBytes * 8 - mLen - 1 |
|
var eMax = (1 << eLen) - 1 |
|
var eBias = eMax >> 1 |
|
var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) |
|
var i = isLE ? 0 : (nBytes - 1) |
|
var d = isLE ? 1 : -1 |
|
var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 |
|
|
|
value = Math.abs(value) |
|
|
|
if (isNaN(value) || value === Infinity) { |
|
m = isNaN(value) ? 1 : 0 |
|
e = eMax |
|
} else { |
|
e = Math.floor(Math.log(value) / Math.LN2) |
|
if (value * (c = Math.pow(2, -e)) < 1) { |
|
e-- |
|
c *= 2 |
|
} |
|
if (e + eBias >= 1) { |
|
value += rt / c |
|
} else { |
|
value += rt * Math.pow(2, 1 - eBias) |
|
} |
|
if (value * c >= 2) { |
|
e++ |
|
c /= 2 |
|
} |
|
|
|
if (e + eBias >= eMax) { |
|
m = 0 |
|
e = eMax |
|
} else if (e + eBias >= 1) { |
|
m = (value * c - 1) * Math.pow(2, mLen) |
|
e = e + eBias |
|
} else { |
|
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) |
|
e = 0 |
|
} |
|
} |
|
|
|
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} |
|
|
|
e = (e << mLen) | m |
|
eLen += mLen |
|
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} |
|
|
|
buffer[offset + i - d] |= s * 128 |
|
} |
|
|
|
},{}],51:[function(require,module,exports){ |
|
arguments[4][29][0].apply(exports,arguments) |
|
},{"dup":29}],52:[function(require,module,exports){ |
|
/** |
|
* Determine if an object is Buffer |
|
* |
|
* Author: Feross Aboukhadijeh <[email protected]> <http://feross.org> |
|
* License: MIT |
|
* |
|
* `npm install is-buffer` |
|
*/ |
|
|
|
module.exports = function (obj) { |
|
return !!(obj != null && |
|
(obj._isBuffer || // For Safari 5-7 (missing Object.prototype.constructor) |
|
(obj.constructor && |
|
typeof obj.constructor.isBuffer === 'function' && |
|
obj.constructor.isBuffer(obj)) |
|
)) |
|
} |
|
|
|
},{}],53:[function(require,module,exports){ |
|
module.exports = Array.isArray || function (arr) { |
|
return Object.prototype.toString.call(arr) == '[object Array]'; |
|
}; |
|
|
|
},{}],54:[function(require,module,exports){ |
|
(function (process){ |
|
'use strict'; |
|
|
|
if (!process.version || |
|
process.version.indexOf('v0.') === 0 || |
|
process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { |
|
module.exports = nextTick; |
|
} else { |
|
module.exports = process.nextTick; |
|
} |
|
|
|
function nextTick(fn) { |
|
var args = new Array(arguments.length - 1); |
|
var i = 0; |
|
while (i < args.length) { |
|
args[i++] = arguments[i]; |
|
} |
|
process.nextTick(function afterTick() { |
|
fn.apply(null, args); |
|
}); |
|
} |
|
|
|
}).call(this,require('_process')) |
|
},{"_process":55}],55:[function(require,module,exports){ |
|
// shim for using process in browser |
|
|
|
var process = module.exports = {}; |
|
var queue = []; |
|
var draining = false; |
|
var currentQueue; |
|
var queueIndex = -1; |
|
|
|
function cleanUpNextTick() { |
|
draining = false; |
|
if (currentQueue.length) { |
|
queue = currentQueue.concat(queue); |
|
} else { |
|
queueIndex = -1; |
|
} |
|
if (queue.length) { |
|
drainQueue(); |
|
} |
|
} |
|
|
|
function drainQueue() { |
|
if (draining) { |
|
return; |
|
} |
|
var timeout = setTimeout(cleanUpNextTick); |
|
draining = true; |
|
|
|
var len = queue.length; |
|
while(len) { |
|
currentQueue = queue; |
|
queue = []; |
|
while (++queueIndex < len) { |
|
if (currentQueue) { |
|
currentQueue[queueIndex].run(); |
|
} |
|
} |
|
queueIndex = -1; |
|
len = queue.length; |
|
} |
|
currentQueue = null; |
|
draining = false; |
|
clearTimeout(timeout); |
|
} |
|
|
|
process.nextTick = function (fun) { |
|
var args = new Array(arguments.length - 1); |
|
if (arguments.length > 1) { |
|
for (var i = 1; i < arguments.length; i++) { |
|
args[i - 1] = arguments[i]; |
|
} |
|
} |
|
queue.push(new Item(fun, args)); |
|
if (queue.length === 1 && !draining) { |
|
setTimeout(drainQueue, 0); |
|
} |
|
}; |
|
|
|
// v8 likes predictible objects |
|
function Item(fun, array) { |
|
this.fun = fun; |
|
this.array = array; |
|
} |
|
Item.prototype.run = function () { |
|
this.fun.apply(null, this.array); |
|
}; |
|
process.title = 'browser'; |
|
process.browser = true; |
|
process.env = {}; |
|
process.argv = []; |
|
process.version = ''; // empty string to avoid regexp issues |
|
process.versions = {}; |
|
|
|
function noop() {} |
|
|
|
process.on = noop; |
|
process.addListener = noop; |
|
process.once = noop; |
|
process.off = noop; |
|
process.removeListener = noop; |
|
process.removeAllListeners = noop; |
|
process.emit = noop; |
|
|
|
process.binding = function (name) { |
|
throw new Error('process.binding is not supported'); |
|
}; |
|
|
|
process.cwd = function () { return '/' }; |
|
process.chdir = function (dir) { |
|
throw new Error('process.chdir is not supported'); |
|
}; |
|
process.umask = function() { return 0; }; |
|
|
|
},{}],56:[function(require,module,exports){ |
|
module.exports = require("./lib/_stream_duplex.js") |
|
|
|
},{"./lib/_stream_duplex.js":57}],57:[function(require,module,exports){ |
|
// 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; |
|
|
|
/*<replacement>*/ |
|
var processNextTick = require('process-nextick-args'); |
|
/*</replacement>*/ |
|
|
|
|
|
|
|
/*<replacement>*/ |
|
var util = require('core-util-is'); |
|
util.inherits = require('inherits'); |
|
/*</replacement>*/ |
|
|
|
var Readable = require('./_stream_readable'); |
|
var Writable = require('./_stream_writable'); |
|
|
|
util.inherits(Duplex, Readable); |
|
|
|
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); |
|
|
|
if (options && options.readable === false) |
|
this.readable = false; |
|
|
|
if (options && options.writable === false) |
|
this.writable = false; |
|
|
|
this.allowHalfOpen = true; |
|
if (options && options.allowHalfOpen === false) |
|
this.allowHalfOpen = false; |
|
|
|
this.once('end', onend); |
|
} |
|
|
|
// the no-half-open enforcer |
|
function onend() { |
|
// if we allow half-open state, or if the writable side ended, |
|
// then we're ok. |
|
if (this.allowHalfOpen || this._writableState.ended) |
|
return; |
|
|
|
// no more data can be written. |
|
// But allow more writes to happen in this tick. |
|
processNextTick(onEndNT, this); |
|
} |
|
|
|
function onEndNT(self) { |
|
self.end(); |
|
} |
|
|
|
function forEach (xs, f) { |
|
for (var i = 0, l = xs.length; i < l; i++) { |
|
f(xs[i], i); |
|
} |
|
} |
|
|
|
},{"./_stream_readable":59,"./_stream_writable":61,"core-util-is":48,"inherits":51,"process-nextick-args":54}],58:[function(require,module,exports){ |
|
// 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'); |
|
|
|
/*<replacement>*/ |
|
var util = require('core-util-is'); |
|
util.inherits = require('inherits'); |
|
/*</replacement>*/ |
|
|
|
util.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); |
|
}; |
|
|
|
},{"./_stream_transform":60,"core-util-is":48,"inherits":51}],59:[function(require,module,exports){ |
|
(function (process){ |
|
'use strict'; |
|
|
|
module.exports = Readable; |
|
|
|
/*<replacement>*/ |
|
var processNextTick = require('process-nextick-args'); |
|
/*</replacement>*/ |
|
|
|
|
|
/*<replacement>*/ |
|
var isArray = require('isarray'); |
|
/*</replacement>*/ |
|
|
|
|
|
/*<replacement>*/ |
|
var Buffer = require('buffer').Buffer; |
|
/*</replacement>*/ |
|
|
|
Readable.ReadableState = ReadableState; |
|
|
|
var EE = require('events'); |
|
|
|
/*<replacement>*/ |
|
var EElistenerCount = function(emitter, type) { |
|
return emitter.listeners(type).length; |
|
}; |
|
/*</replacement>*/ |
|
|
|
|
|
|
|
/*<replacement>*/ |
|
var Stream; |
|
(function (){try{ |
|
Stream = require('st' + 'ream'); |
|
}catch(_){}finally{ |
|
if (!Stream) |
|
Stream = require('events').EventEmitter; |
|
}}()) |
|
/*</replacement>*/ |
|
|
|
var Buffer = require('buffer').Buffer; |
|
|
|
/*<replacement>*/ |
|
var util = require('core-util-is'); |
|
util.inherits = require('inherits'); |
|
/*</replacement>*/ |
|
|
|
|
|
|
|
/*<replacement>*/ |
|
var debugUtil = require('util'); |
|
var debug; |
|
if (debugUtil && debugUtil.debuglog) { |
|
debug = debugUtil.debuglog('stream'); |
|
} else { |
|
debug = function () {}; |
|
} |
|
/*</replacement>*/ |
|
|
|
var StringDecoder; |
|
|
|
util.inherits(Readable, Stream); |
|
|
|
var Duplex; |
|
function ReadableState(options, stream) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
options = options || {}; |
|
|
|
// object stream flag. Used to make read(n) ignore n and to |
|
// make all the buffer merging and length checks go away |
|
this.objectMode = !!options.objectMode; |
|
|
|
if (stream instanceof Duplex) |
|
this.objectMode = this.objectMode || !!options.readableObjectMode; |
|
|
|
// the point at which it stops calling _read() to fill the buffer |
|
// Note: 0 is a valid value, means "don't call _read preemptively ever" |
|
var hwm = options.highWaterMark; |
|
var defaultHwm = this.objectMode ? 16 : 16 * 1024; |
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; |
|
|
|
// cast to ints. |
|
this.highWaterMark = ~~this.highWaterMark; |
|
|
|
this.buffer = []; |
|
this.length = 0; |
|
this.pipes = null; |
|
this.pipesCount = 0; |
|
this.flowing = null; |
|
this.ended = false; |
|
this.endEmitted = false; |
|
this.reading = false; |
|
|
|
// 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; |
|
|
|
// whenever we return null, then we set a flag to say |
|
// that we're awaiting a 'readable' event emission. |
|
this.needReadable = false; |
|
this.emittedReadable = false; |
|
this.readableListening = false; |
|
|
|
// 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'; |
|
|
|
// when piping, we only care about 'readable' events that happen |
|
// after read()ing all the bytes and not getting any pushback. |
|
this.ranOut = false; |
|
|
|
// the number of writers that are awaiting a drain event in .pipe()s |
|
this.awaitDrain = 0; |
|
|
|
// if true, a maybeReadMore has been scheduled |
|
this.readingMore = false; |
|
|
|
this.decoder = null; |
|
this.encoding = null; |
|
if (options.encoding) { |
|
if (!StringDecoder) |
|
StringDecoder = require('string_decoder/').StringDecoder; |
|
this.decoder = new StringDecoder(options.encoding); |
|
this.encoding = options.encoding; |
|
} |
|
} |
|
|
|
var Duplex; |
|
function Readable(options) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
if (!(this instanceof Readable)) |
|
return new Readable(options); |
|
|
|
this._readableState = new ReadableState(options, this); |
|
|
|
// legacy |
|
this.readable = true; |
|
|
|
if (options && typeof options.read === 'function') |
|
this._read = options.read; |
|
|
|
Stream.call(this); |
|
} |
|
|
|
// Manually shove something into the read() buffer. |
|
// This returns true if the highWaterMark has not been hit yet, |
|
// similar to how Writable.write() returns true if you should |
|
// write() some more. |
|
Readable.prototype.push = function(chunk, encoding) { |
|
var state = this._readableState; |
|
|
|
if (!state.objectMode && typeof chunk === 'string') { |
|
encoding = encoding || state.defaultEncoding; |
|
if (encoding !== state.encoding) { |
|
chunk = new Buffer(chunk, encoding); |
|
encoding = ''; |
|
} |
|
} |
|
|
|
return readableAddChunk(this, state, chunk, encoding, false); |
|
}; |
|
|
|
// Unshift should *always* be something directly out of read() |
|
Readable.prototype.unshift = function(chunk) { |
|
var state = this._readableState; |
|
return readableAddChunk(this, state, chunk, '', true); |
|
}; |
|
|
|
Readable.prototype.isPaused = function() { |
|
return this._readableState.flowing === false; |
|
}; |
|
|
|
function readableAddChunk(stream, state, chunk, encoding, addToFront) { |
|
var er = chunkInvalid(state, chunk); |
|
if (er) { |
|
stream.emit('error', er); |
|
} else if (chunk === null) { |
|
state.reading = false; |
|
onEofChunk(stream, state); |
|
} else if (state.objectMode || chunk && chunk.length > 0) { |
|
if (state.ended && !addToFront) { |
|
var e = new Error('stream.push() after EOF'); |
|
stream.emit('error', e); |
|
} else if (state.endEmitted && addToFront) { |
|
var e = new Error('stream.unshift() after end event'); |
|
stream.emit('error', e); |
|
} else { |
|
if (state.decoder && !addToFront && !encoding) |
|
chunk = state.decoder.write(chunk); |
|
|
|
if (!addToFront) |
|
state.reading = false; |
|
|
|
// if we want the data now, just emit it. |
|
if (state.flowing && state.length === 0 && !state.sync) { |
|
stream.emit('data', chunk); |
|
stream.read(0); |
|
} else { |
|
// update the buffer info. |
|
state.length += state.objectMode ? 1 : chunk.length; |
|
if (addToFront) |
|
state.buffer.unshift(chunk); |
|
else |
|
state.buffer.push(chunk); |
|
|
|
if (state.needReadable) |
|
emitReadable(stream); |
|
} |
|
|
|
maybeReadMore(stream, state); |
|
} |
|
} else if (!addToFront) { |
|
state.reading = false; |
|
} |
|
|
|
return needMoreData(state); |
|
} |
|
|
|
|
|
// if it's past the high water mark, we can push in some more. |
|
// Also, if we have no data yet, we can stand some |
|
// more bytes. This is to work around cases where hwm=0, |
|
// such as the repl. Also, if the push() triggered a |
|
// readable event, and the user called read(largeNumber) such that |
|
// needReadable was set, then we ought to push more, so that another |
|
// 'readable' event will be triggered. |
|
function needMoreData(state) { |
|
return !state.ended && |
|
(state.needReadable || |
|
state.length < state.highWaterMark || |
|
state.length === 0); |
|
} |
|
|
|
// backwards compatibility. |
|
Readable.prototype.setEncoding = function(enc) { |
|
if (!StringDecoder) |
|
StringDecoder = require('string_decoder/').StringDecoder; |
|
this._readableState.decoder = new StringDecoder(enc); |
|
this._readableState.encoding = enc; |
|
return this; |
|
}; |
|
|
|
// Don't raise the hwm > 8MB |
|
var MAX_HWM = 0x800000; |
|
function computeNewHighWaterMark(n) { |
|
if (n >= MAX_HWM) { |
|
n = MAX_HWM; |
|
} else { |
|
// Get the next highest power of 2 |
|
n--; |
|
n |= n >>> 1; |
|
n |= n >>> 2; |
|
n |= n >>> 4; |
|
n |= n >>> 8; |
|
n |= n >>> 16; |
|
n++; |
|
} |
|
return n; |
|
} |
|
|
|
function howMuchToRead(n, state) { |
|
if (state.length === 0 && state.ended) |
|
return 0; |
|
|
|
if (state.objectMode) |
|
return n === 0 ? 0 : 1; |
|
|
|
if (n === null || isNaN(n)) { |
|
// only flow one buffer at a time |
|
if (state.flowing && state.buffer.length) |
|
return state.buffer[0].length; |
|
else |
|
return state.length; |
|
} |
|
|
|
if (n <= 0) |
|
return 0; |
|
|
|
// If we're asking for more than the target buffer level, |
|
// then raise the water mark. Bump up to the next highest |
|
// power of 2, to prevent increasing it excessively in tiny |
|
// amounts. |
|
if (n > state.highWaterMark) |
|
state.highWaterMark = computeNewHighWaterMark(n); |
|
|
|
// don't have that much. return null, unless we've ended. |
|
if (n > state.length) { |
|
if (!state.ended) { |
|
state.needReadable = true; |
|
return 0; |
|
} else { |
|
return state.length; |
|
} |
|
} |
|
|
|
return n; |
|
} |
|
|
|
// you can override either this method, or the async _read(n) below. |
|
Readable.prototype.read = function(n) { |
|
debug('read', n); |
|
var state = this._readableState; |
|
var nOrig = n; |
|
|
|
if (typeof n !== 'number' || n > 0) |
|
state.emittedReadable = false; |
|
|
|
// if we're doing read(0) to trigger a readable event, but we |
|
// already have a bunch of data in the buffer, then just trigger |
|
// the 'readable' event and move on. |
|
if (n === 0 && |
|
state.needReadable && |
|
(state.length >= state.highWaterMark || state.ended)) { |
|
debug('read: emitReadable', state.length, state.ended); |
|
if (state.length === 0 && state.ended) |
|
endReadable(this); |
|
else |
|
emitReadable(this); |
|
return null; |
|
} |
|
|
|
n = howMuchToRead(n, state); |
|
|
|
// if we've ended, and we're now clear, then finish it up. |
|
if (n === 0 && state.ended) { |
|
if (state.length === 0) |
|
endReadable(this); |
|
return null; |
|
} |
|
|
|
// All the actual chunk generation logic needs to be |
|
// *below* the call to _read. The reason is that in certain |
|
// synthetic stream cases, such as passthrough streams, _read |
|
// may be a completely synchronous operation which may change |
|
// the state of the read buffer, providing enough data when |
|
// before there was *not* enough. |
|
// |
|
// So, the steps are: |
|
// 1. Figure out what the state of things will be after we do |
|
// a read from the buffer. |
|
// |
|
// 2. If that resulting state will trigger a _read, then call _read. |
|
// Note that this may be asynchronous, or synchronous. Yes, it is |
|
// deeply ugly to write APIs this way, but that still doesn't mean |
|
// that the Readable class should behave improperly, as streams are |
|
// designed to be sync/async agnostic. |
|
// Take note if the _read call is sync or async (ie, if the read call |
|
// has returned yet), so that we know whether or not it's safe to emit |
|
// 'readable' etc. |
|
// |
|
// 3. Actually pull the requested chunks out of the buffer and return. |
|
|
|
// if we need a readable event, then we need to do some reading. |
|
var doRead = state.needReadable; |
|
debug('need readable', doRead); |
|
|
|
// if we currently have less than the highWaterMark, then also read some |
|
if (state.length === 0 || state.length - n < state.highWaterMark) { |
|
doRead = true; |
|
debug('length less than watermark', doRead); |
|
} |
|
|
|
// however, if we've ended, then there's no point, and if we're already |
|
// reading, then it's unnecessary. |
|
if (state.ended || state.reading) { |
|
doRead = false; |
|
debug('reading or ended', doRead); |
|
} |
|
|
|
if (doRead) { |
|
debug('do read'); |
|
state.reading = true; |
|
state.sync = true; |
|
// if the length is currently zero, then we *need* a readable event. |
|
if (state.length === 0) |
|
state.needReadable = true; |
|
// call internal read method |
|
this._read(state.highWaterMark); |
|
state.sync = false; |
|
} |
|
|
|
// If _read pushed data synchronously, then `reading` will be false, |
|
// and we need to re-evaluate how much data we can return to the user. |
|
if (doRead && !state.reading) |
|
n = howMuchToRead(nOrig, state); |
|
|
|
var ret; |
|
if (n > 0) |
|
ret = fromList(n, state); |
|
else |
|
ret = null; |
|
|
|
if (ret === null) { |
|
state.needReadable = true; |
|
n = 0; |
|
} |
|
|
|
state.length -= n; |
|
|
|
// If we have nothing in the buffer, then we want to know |
|
// as soon as we *do* get something into the buffer. |
|
if (state.length === 0 && !state.ended) |
|
state.needReadable = true; |
|
|
|
// If we tried to read() past the EOF, then emit end on the next tick. |
|
if (nOrig !== n && state.ended && state.length === 0) |
|
endReadable(this); |
|
|
|
if (ret !== null) |
|
this.emit('data', ret); |
|
|
|
return ret; |
|
}; |
|
|
|
function chunkInvalid(state, chunk) { |
|
var er = null; |
|
if (!(Buffer.isBuffer(chunk)) && |
|
typeof chunk !== 'string' && |
|
chunk !== null && |
|
chunk !== undefined && |
|
!state.objectMode) { |
|
er = new TypeError('Invalid non-string/buffer chunk'); |
|
} |
|
return er; |
|
} |
|
|
|
|
|
function onEofChunk(stream, state) { |
|
if (state.ended) return; |
|
if (state.decoder) { |
|
var chunk = state.decoder.end(); |
|
if (chunk && chunk.length) { |
|
state.buffer.push(chunk); |
|
state.length += state.objectMode ? 1 : chunk.length; |
|
} |
|
} |
|
state.ended = true; |
|
|
|
// emit 'readable' now to make sure it gets picked up. |
|
emitReadable(stream); |
|
} |
|
|
|
// Don't emit readable right away in sync mode, because this can trigger |
|
// another read() call => stack overflow. This way, it might trigger |
|
// a nextTick recursion warning, but that's not so bad. |
|
function emitReadable(stream) { |
|
var state = stream._readableState; |
|
state.needReadable = false; |
|
if (!state.emittedReadable) { |
|
debug('emitReadable', state.flowing); |
|
state.emittedReadable = true; |
|
if (state.sync) |
|
processNextTick(emitReadable_, stream); |
|
else |
|
emitReadable_(stream); |
|
} |
|
} |
|
|
|
function emitReadable_(stream) { |
|
debug('emit readable'); |
|
stream.emit('readable'); |
|
flow(stream); |
|
} |
|
|
|
|
|
// at this point, the user has presumably seen the 'readable' event, |
|
// and called read() to consume some data. that may have triggered |
|
// in turn another _read(n) call, in which case reading = true if |
|
// it's in progress. |
|
// However, if we're not ended, or reading, and the length < hwm, |
|
// then go ahead and try to read some more preemptively. |
|
function maybeReadMore(stream, state) { |
|
if (!state.readingMore) { |
|
state.readingMore = true; |
|
processNextTick(maybeReadMore_, stream, state); |
|
} |
|
} |
|
|
|
function maybeReadMore_(stream, state) { |
|
var len = state.length; |
|
while (!state.reading && !state.flowing && !state.ended && |
|
state.length < state.highWaterMark) { |
|
debug('maybeReadMore read 0'); |
|
stream.read(0); |
|
if (len === state.length) |
|
// didn't get any data, stop spinning. |
|
break; |
|
else |
|
len = state.length; |
|
} |
|
state.readingMore = false; |
|
} |
|
|
|
// abstract method. to be overridden in specific implementation classes. |
|
// call cb(er, data) where data is <= n in length. |
|
// for virtual (non-string, non-buffer) streams, "length" is somewhat |
|
// arbitrary, and perhaps not very meaningful. |
|
Readable.prototype._read = function(n) { |
|
this.emit('error', new Error('not implemented')); |
|
}; |
|
|
|
Readable.prototype.pipe = function(dest, pipeOpts) { |
|
var src = this; |
|
var state = this._readableState; |
|
|
|
switch (state.pipesCount) { |
|
case 0: |
|
state.pipes = dest; |
|
break; |
|
case 1: |
|
state.pipes = [state.pipes, dest]; |
|
break; |
|
default: |
|
state.pipes.push(dest); |
|
break; |
|
} |
|
state.pipesCount += 1; |
|
debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); |
|
|
|
var doEnd = (!pipeOpts || pipeOpts.end !== false) && |
|
dest !== process.stdout && |
|
dest !== process.stderr; |
|
|
|
var endFn = doEnd ? onend : cleanup; |
|
if (state.endEmitted) |
|
processNextTick(endFn); |
|
else |
|
src.once('end', endFn); |
|
|
|
dest.on('unpipe', onunpipe); |
|
function onunpipe(readable) { |
|
debug('onunpipe'); |
|
if (readable === src) { |
|
cleanup(); |
|
} |
|
} |
|
|
|
function onend() { |
|
debug('onend'); |
|
dest.end(); |
|
} |
|
|
|
// when the dest drains, it reduces the awaitDrain counter |
|
// on the source. This would be more elegant with a .once() |
|
// handler in flow(), but adding and removing repeatedly is |
|
// too slow. |
|
var ondrain = pipeOnDrain(src); |
|
dest.on('drain', ondrain); |
|
|
|
var cleanedUp = false; |
|
function cleanup() { |
|
debug('cleanup'); |
|
// cleanup event handlers once the pipe is broken |
|
dest.removeListener('close', onclose); |
|
dest.removeListener('finish', onfinish); |
|
dest.removeListener('drain', ondrain); |
|
dest.removeListener('error', onerror); |
|
dest.removeListener('unpipe', onunpipe); |
|
src.removeListener('end', onend); |
|
src.removeListener('end', cleanup); |
|
src.removeListener('data', ondata); |
|
|
|
cleanedUp = true; |
|
|
|
// if the reader is waiting for a drain event from this |
|
// specific writer, then it would cause it to never start |
|
// flowing again. |
|
// So, if this is awaiting a drain, then we just call it now. |
|
// If we don't know, then assume that we are waiting for one. |
|
if (state.awaitDrain && |
|
(!dest._writableState || dest._writableState.needDrain)) |
|
ondrain(); |
|
} |
|
|
|
src.on('data', ondata); |
|
function ondata(chunk) { |
|
debug('ondata'); |
|
var ret = dest.write(chunk); |
|
if (false === ret) { |
|
// If the user unpiped during `dest.write()`, it is possible |
|
// to get stuck in a permanently paused state if that write |
|
// also returned false. |
|
if (state.pipesCount === 1 && |
|
state.pipes[0] === dest && |
|
src.listenerCount('data') === 1 && |
|
!cleanedUp) { |
|
debug('false write response, pause', src._readableState.awaitDrain); |
|
src._readableState.awaitDrain++; |
|
} |
|
src.pause(); |
|
} |
|
} |
|
|
|
// if the dest has an error, then stop piping into it. |
|
// however, don't suppress the throwing behavior for this. |
|
function onerror(er) { |
|
debug('onerror', er); |
|
unpipe(); |
|
dest.removeListener('error', onerror); |
|
if (EElistenerCount(dest, 'error') === 0) |
|
dest.emit('error', er); |
|
} |
|
// This is a brutally ugly hack to make sure that our error handler |
|
// is attached before any userland ones. NEVER DO THIS. |
|
if (!dest._events || !dest._events.error) |
|
dest.on('error', onerror); |
|
else if (isArray(dest._events.error)) |
|
dest._events.error.unshift(onerror); |
|
else |
|
dest._events.error = [onerror, dest._events.error]; |
|
|
|
|
|
// Both close and finish should trigger unpipe, but only once. |
|
function onclose() { |
|
dest.removeListener('finish', onfinish); |
|
unpipe(); |
|
} |
|
dest.once('close', onclose); |
|
function onfinish() { |
|
debug('onfinish'); |
|
dest.removeListener('close', onclose); |
|
unpipe(); |
|
} |
|
dest.once('finish', onfinish); |
|
|
|
function unpipe() { |
|
debug('unpipe'); |
|
src.unpipe(dest); |
|
} |
|
|
|
// tell the dest that it's being piped to |
|
dest.emit('pipe', src); |
|
|
|
// start the flow if it hasn't been started already. |
|
if (!state.flowing) { |
|
debug('pipe resume'); |
|
src.resume(); |
|
} |
|
|
|
return dest; |
|
}; |
|
|
|
function pipeOnDrain(src) { |
|
return function() { |
|
var state = src._readableState; |
|
debug('pipeOnDrain', state.awaitDrain); |
|
if (state.awaitDrain) |
|
state.awaitDrain--; |
|
if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { |
|
state.flowing = true; |
|
flow(src); |
|
} |
|
}; |
|
} |
|
|
|
|
|
Readable.prototype.unpipe = function(dest) { |
|
var state = this._readableState; |
|
|
|
// if we're not piping anywhere, then do nothing. |
|
if (state.pipesCount === 0) |
|
return this; |
|
|
|
// just one destination. most common case. |
|
if (state.pipesCount === 1) { |
|
// passed in one, but it's not the right one. |
|
if (dest && dest !== state.pipes) |
|
return this; |
|
|
|
if (!dest) |
|
dest = state.pipes; |
|
|
|
// got a match. |
|
state.pipes = null; |
|
state.pipesCount = 0; |
|
state.flowing = false; |
|
if (dest) |
|
dest.emit('unpipe', this); |
|
return this; |
|
} |
|
|
|
// slow case. multiple pipe destinations. |
|
|
|
if (!dest) { |
|
// remove all. |
|
var dests = state.pipes; |
|
var len = state.pipesCount; |
|
state.pipes = null; |
|
state.pipesCount = 0; |
|
state.flowing = false; |
|
|
|
for (var i = 0; i < len; i++) |
|
dests[i].emit('unpipe', this); |
|
return this; |
|
} |
|
|
|
// try to find the right one. |
|
var i = indexOf(state.pipes, dest); |
|
if (i === -1) |
|
return this; |
|
|
|
state.pipes.splice(i, 1); |
|
state.pipesCount -= 1; |
|
if (state.pipesCount === 1) |
|
state.pipes = state.pipes[0]; |
|
|
|
dest.emit('unpipe', this); |
|
|
|
return this; |
|
}; |
|
|
|
// set up data events if they are asked for |
|
// Ensure readable listeners eventually get something |
|
Readable.prototype.on = function(ev, fn) { |
|
var res = Stream.prototype.on.call(this, ev, fn); |
|
|
|
// If listening to data, and it has not explicitly been paused, |
|
// then call resume to start the flow of data on the next tick. |
|
if (ev === 'data' && false !== this._readableState.flowing) { |
|
this.resume(); |
|
} |
|
|
|
if (ev === 'readable' && this.readable) { |
|
var state = this._readableState; |
|
if (!state.readableListening) { |
|
state.readableListening = true; |
|
state.emittedReadable = false; |
|
state.needReadable = true; |
|
if (!state.reading) { |
|
processNextTick(nReadingNextTick, this); |
|
} else if (state.length) { |
|
emitReadable(this, state); |
|
} |
|
} |
|
} |
|
|
|
return res; |
|
}; |
|
Readable.prototype.addListener = Readable.prototype.on; |
|
|
|
function nReadingNextTick(self) { |
|
debug('readable nexttick read 0'); |
|
self.read(0); |
|
} |
|
|
|
// pause() and resume() are remnants of the legacy readable stream API |
|
// If the user uses them, then switch into old mode. |
|
Readable.prototype.resume = function() { |
|
var state = this._readableState; |
|
if (!state.flowing) { |
|
debug('resume'); |
|
state.flowing = true; |
|
resume(this, state); |
|
} |
|
return this; |
|
}; |
|
|
|
function resume(stream, state) { |
|
if (!state.resumeScheduled) { |
|
state.resumeScheduled = true; |
|
processNextTick(resume_, stream, state); |
|
} |
|
} |
|
|
|
function resume_(stream, state) { |
|
if (!state.reading) { |
|
debug('resume read 0'); |
|
stream.read(0); |
|
} |
|
|
|
state.resumeScheduled = false; |
|
stream.emit('resume'); |
|
flow(stream); |
|
if (state.flowing && !state.reading) |
|
stream.read(0); |
|
} |
|
|
|
Readable.prototype.pause = function() { |
|
debug('call pause flowing=%j', this._readableState.flowing); |
|
if (false !== this._readableState.flowing) { |
|
debug('pause'); |
|
this._readableState.flowing = false; |
|
this.emit('pause'); |
|
} |
|
return this; |
|
}; |
|
|
|
function flow(stream) { |
|
var state = stream._readableState; |
|
debug('flow', state.flowing); |
|
if (state.flowing) { |
|
do { |
|
var chunk = stream.read(); |
|
} while (null !== chunk && state.flowing); |
|
} |
|
} |
|
|
|
// wrap an old-style stream as the async data source. |
|
// This is *not* part of the readable stream interface. |
|
// It is an ugly unfortunate mess of history. |
|
Readable.prototype.wrap = function(stream) { |
|
var state = this._readableState; |
|
var paused = false; |
|
|
|
var self = this; |
|
stream.on('end', function() { |
|
debug('wrapped end'); |
|
if (state.decoder && !state.ended) { |
|
var chunk = state.decoder.end(); |
|
if (chunk && chunk.length) |
|
self.push(chunk); |
|
} |
|
|
|
self.push(null); |
|
}); |
|
|
|
stream.on('data', function(chunk) { |
|
debug('wrapped data'); |
|
if (state.decoder) |
|
chunk = state.decoder.write(chunk); |
|
|
|
// don't skip over falsy values in objectMode |
|
if (state.objectMode && (chunk === null || chunk === undefined)) |
|
return; |
|
else if (!state.objectMode && (!chunk || !chunk.length)) |
|
return; |
|
|
|
var ret = self.push(chunk); |
|
if (!ret) { |
|
paused = true; |
|
stream.pause(); |
|
} |
|
}); |
|
|
|
// proxy all the other methods. |
|
// important when wrapping filters and duplexes. |
|
for (var i in stream) { |
|
if (this[i] === undefined && typeof stream[i] === 'function') { |
|
this[i] = function(method) { return function() { |
|
return stream[method].apply(stream, arguments); |
|
}; }(i); |
|
} |
|
} |
|
|
|
// proxy certain important events. |
|
var events = ['error', 'close', 'destroy', 'pause', 'resume']; |
|
forEach(events, function(ev) { |
|
stream.on(ev, self.emit.bind(self, ev)); |
|
}); |
|
|
|
// when we try to consume some more bytes, simply unpause the |
|
// underlying stream. |
|
self._read = function(n) { |
|
debug('wrapped _read', n); |
|
if (paused) { |
|
paused = false; |
|
stream.resume(); |
|
} |
|
}; |
|
|
|
return self; |
|
}; |
|
|
|
|
|
// exposed for testing purposes only. |
|
Readable._fromList = fromList; |
|
|
|
// Pluck off n bytes from an array of buffers. |
|
// Length is the combined lengths of all the buffers in the list. |
|
function fromList(n, state) { |
|
var list = state.buffer; |
|
var length = state.length; |
|
var stringMode = !!state.decoder; |
|
var objectMode = !!state.objectMode; |
|
var ret; |
|
|
|
// nothing in the list, definitely empty. |
|
if (list.length === 0) |
|
return null; |
|
|
|
if (length === 0) |
|
ret = null; |
|
else if (objectMode) |
|
ret = list.shift(); |
|
else if (!n || n >= length) { |
|
// read it all, truncate the array. |
|
if (stringMode) |
|
ret = list.join(''); |
|
else if (list.length === 1) |
|
ret = list[0]; |
|
else |
|
ret = Buffer.concat(list, length); |
|
list.length = 0; |
|
} else { |
|
// read just some of it. |
|
if (n < list[0].length) { |
|
// just take a part of the first list item. |
|
// slice is the same for buffers and strings. |
|
var buf = list[0]; |
|
ret = buf.slice(0, n); |
|
list[0] = buf.slice(n); |
|
} else if (n === list[0].length) { |
|
// first list is a perfect match |
|
ret = list.shift(); |
|
} else { |
|
// complex case. |
|
// we have enough to cover it, but it spans past the first buffer. |
|
if (stringMode) |
|
ret = ''; |
|
else |
|
ret = new Buffer(n); |
|
|
|
var c = 0; |
|
for (var i = 0, l = list.length; i < l && c < n; i++) { |
|
var buf = list[0]; |
|
var cpy = Math.min(n - c, buf.length); |
|
|
|
if (stringMode) |
|
ret += buf.slice(0, cpy); |
|
else |
|
buf.copy(ret, c, 0, cpy); |
|
|
|
if (cpy < buf.length) |
|
list[0] = buf.slice(cpy); |
|
else |
|
list.shift(); |
|
|
|
c += cpy; |
|
} |
|
} |
|
} |
|
|
|
return ret; |
|
} |
|
|
|
function endReadable(stream) { |
|
var state = stream._readableState; |
|
|
|
// If we get here before consuming all the bytes, then that is a |
|
// bug in node. Should never happen. |
|
if (state.length > 0) |
|
throw new Error('endReadable called on non-empty stream'); |
|
|
|
if (!state.endEmitted) { |
|
state.ended = true; |
|
processNextTick(endReadableNT, state, stream); |
|
} |
|
} |
|
|
|
function endReadableNT(state, stream) { |
|
// Check that we didn't get one last unshift. |
|
if (!state.endEmitted && state.length === 0) { |
|
state.endEmitted = true; |
|
stream.readable = false; |
|
stream.emit('end'); |
|
} |
|
} |
|
|
|
function forEach (xs, f) { |
|
for (var i = 0, l = xs.length; i < l; i++) { |
|
f(xs[i], i); |
|
} |
|
} |
|
|
|
function indexOf (xs, x) { |
|
for (var i = 0, l = xs.length; i < l; i++) { |
|
if (xs[i] === x) return i; |
|
} |
|
return -1; |
|
} |
|
|
|
}).call(this,require('_process')) |
|
},{"./_stream_duplex":57,"_process":55,"buffer":46,"core-util-is":48,"events":49,"inherits":51,"isarray":53,"process-nextick-args":54,"string_decoder/":67,"util":45}],60:[function(require,module,exports){ |
|
// 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 Duplex = require('./_stream_duplex'); |
|
|
|
/*<replacement>*/ |
|
var util = require('core-util-is'); |
|
util.inherits = require('inherits'); |
|
/*</replacement>*/ |
|
|
|
util.inherits(Transform, Duplex); |
|
|
|
|
|
function TransformState(stream) { |
|
this.afterTransform = function(er, data) { |
|
return afterTransform(stream, er, data); |
|
}; |
|
|
|
this.needTransform = false; |
|
this.transforming = false; |
|
this.writecb = null; |
|
this.writechunk = null; |
|
} |
|
|
|
function afterTransform(stream, er, data) { |
|
var ts = stream._transformState; |
|
ts.transforming = false; |
|
|
|
var cb = ts.writecb; |
|
|
|
if (!cb) |
|
return stream.emit('error', new Error('no writecb in Transform class')); |
|
|
|
ts.writechunk = null; |
|
ts.writecb = null; |
|
|
|
if (data !== null && data !== undefined) |
|
stream.push(data); |
|
|
|
if (cb) |
|
cb(er); |
|
|
|
var rs = stream._readableState; |
|
rs.reading = false; |
|
if (rs.needReadable || rs.length < rs.highWaterMark) { |
|
stream._read(rs.highWaterMark); |
|
} |
|
} |
|
|
|
|
|
function Transform(options) { |
|
if (!(this instanceof Transform)) |
|
return new Transform(options); |
|
|
|
Duplex.call(this, options); |
|
|
|
this._transformState = new TransformState(this); |
|
|
|
// when the writable side finishes, then flush out anything remaining. |
|
var stream = this; |
|
|
|
// 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; |
|
} |
|
|
|
this.once('prefinish', function() { |
|
if (typeof this._flush === 'function') |
|
this._flush(function(er) { |
|
done(stream, er); |
|
}); |
|
else |
|
done(stream); |
|
}); |
|
} |
|
|
|
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) { |
|
throw new Error('not implemented'); |
|
}; |
|
|
|
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.writecb && !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; |
|
} |
|
}; |
|
|
|
|
|
function done(stream, er) { |
|
if (er) |
|
return stream.emit('error', er); |
|
|
|
// if there's nothing in the write buffer, then that means |
|
// that nothing more will ever be provided |
|
var ws = stream._writableState; |
|
var ts = stream._transformState; |
|
|
|
if (ws.length) |
|
throw new Error('calling transform done when ws.length != 0'); |
|
|
|
if (ts.transforming) |
|
throw new Error('calling transform done when still transforming'); |
|
|
|
return stream.push(null); |
|
} |
|
|
|
},{"./_stream_duplex":57,"core-util-is":48,"inherits":51}],61:[function(require,module,exports){ |
|
// 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>*/ |
|
var processNextTick = require('process-nextick-args'); |
|
/*</replacement>*/ |
|
|
|
|
|
/*<replacement>*/ |
|
var Buffer = require('buffer').Buffer; |
|
/*</replacement>*/ |
|
|
|
Writable.WritableState = WritableState; |
|
|
|
|
|
/*<replacement>*/ |
|
var util = require('core-util-is'); |
|
util.inherits = require('inherits'); |
|
/*</replacement>*/ |
|
|
|
|
|
/*<replacement>*/ |
|
var internalUtil = { |
|
deprecate: require('util-deprecate') |
|
}; |
|
/*</replacement>*/ |
|
|
|
|
|
|
|
/*<replacement>*/ |
|
var Stream; |
|
(function (){try{ |
|
Stream = require('st' + 'ream'); |
|
}catch(_){}finally{ |
|
if (!Stream) |
|
Stream = require('events').EventEmitter; |
|
}}()) |
|
/*</replacement>*/ |
|
|
|
var Buffer = require('buffer').Buffer; |
|
|
|
util.inherits(Writable, Stream); |
|
|
|
function nop() {} |
|
|
|
function WriteReq(chunk, encoding, cb) { |
|
this.chunk = chunk; |
|
this.encoding = encoding; |
|
this.callback = cb; |
|
this.next = null; |
|
} |
|
|
|
var Duplex; |
|
function WritableState(options, stream) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
options = options || {}; |
|
|
|
// object stream flag to indicate whether or not this stream |
|
// contains buffers or objects. |
|
this.objectMode = !!options.objectMode; |
|
|
|
if (stream instanceof Duplex) |
|
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() |
|
var hwm = options.highWaterMark; |
|
var defaultHwm = this.objectMode ? 16 : 16 * 1024; |
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; |
|
|
|
// cast to ints. |
|
this.highWaterMark = ~~this.highWaterMark; |
|
|
|
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; |
|
|
|
// 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; |
|
} |
|
|
|
WritableState.prototype.getBuffer = function writableStateGetBuffer() { |
|
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() { |
|
return this.getBuffer(); |
|
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + |
|
'instead.') |
|
}); |
|
}catch(_){}}()); |
|
|
|
|
|
var Duplex; |
|
function Writable(options) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
// Writable ctor is applied to Duplexes, though they're not |
|
// instanceof Writable, they're instanceof Readable. |
|
if (!(this instanceof Writable) && !(this instanceof Duplex)) |
|
return new Writable(options); |
|
|
|
this._writableState = new WritableState(options, this); |
|
|
|
// legacy. |
|
this.writable = true; |
|
|
|
if (options) { |
|
if (typeof options.write === 'function') |
|
this._write = options.write; |
|
|
|
if (typeof options.writev === 'function') |
|
this._writev = options.writev; |
|
} |
|
|
|
Stream.call(this); |
|
} |
|
|
|
// Otherwise people can pipe Writable streams, which is just wrong. |
|
Writable.prototype.pipe = function() { |
|
this.emit('error', new Error('Cannot pipe. Not readable.')); |
|
}; |
|
|
|
|
|
function writeAfterEnd(stream, cb) { |
|
var er = new Error('write after end'); |
|
// TODO: defer error events consistently everywhere, not just the cb |
|
stream.emit('error', er); |
|
processNextTick(cb, er); |
|
} |
|
|
|
// If we get something that is not a buffer, string, null, or undefined, |
|
// and we're not in objectMode, then that's an error. |
|
// Otherwise stream chunks are all considered to be of length=1, and the |
|
// watermarks determine how many objects to keep in the buffer, rather than |
|
// how many bytes or characters. |
|
function validChunk(stream, state, chunk, cb) { |
|
var valid = true; |
|
|
|
if (!(Buffer.isBuffer(chunk)) && |
|
typeof chunk !== 'string' && |
|
chunk !== null && |
|
chunk !== undefined && |
|
!state.objectMode) { |
|
var er = new TypeError('Invalid non-string/buffer chunk'); |
|
stream.emit('error', er); |
|
processNextTick(cb, er); |
|
valid = false; |
|
} |
|
return valid; |
|
} |
|
|
|
Writable.prototype.write = function(chunk, encoding, cb) { |
|
var state = this._writableState; |
|
var ret = false; |
|
|
|
if (typeof encoding === 'function') { |
|
cb = encoding; |
|
encoding = null; |
|
} |
|
|
|
if (Buffer.isBuffer(chunk)) |
|
encoding = 'buffer'; |
|
else if (!encoding) |
|
encoding = state.defaultEncoding; |
|
|
|
if (typeof cb !== 'function') |
|
cb = nop; |
|
|
|
if (state.ended) |
|
writeAfterEnd(this, cb); |
|
else if (validChunk(this, state, chunk, cb)) { |
|
state.pendingcb++; |
|
ret = writeOrBuffer(this, state, chunk, encoding, cb); |
|
} |
|
|
|
return ret; |
|
}; |
|
|
|
Writable.prototype.cork = function() { |
|
var state = this._writableState; |
|
|
|
state.corked++; |
|
}; |
|
|
|
Writable.prototype.uncork = function() { |
|
var state = this._writableState; |
|
|
|
if (state.corked) { |
|
state.corked--; |
|
|
|
if (!state.writing && |
|
!state.corked && |
|
!state.finished && |
|
!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 TypeError('Unknown encoding: ' + encoding); |
|
this._writableState.defaultEncoding = encoding; |
|
}; |
|
|
|
function decodeChunk(state, chunk, encoding) { |
|
if (!state.objectMode && |
|
state.decodeStrings !== false && |
|
typeof chunk === 'string') { |
|
chunk = new Buffer(chunk, encoding); |
|
} |
|
return chunk; |
|
} |
|
|
|
// 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, chunk, encoding, cb) { |
|
chunk = decodeChunk(state, chunk, encoding); |
|
|
|
if (Buffer.isBuffer(chunk)) |
|
encoding = 'buffer'; |
|
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 = new WriteReq(chunk, encoding, cb); |
|
if (last) { |
|
last.next = state.lastBufferedRequest; |
|
} else { |
|
state.bufferedRequest = state.lastBufferedRequest; |
|
} |
|
} 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 (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) |
|
processNextTick(cb, er); |
|
else |
|
cb(er); |
|
|
|
stream._writableState.errorEmitted = true; |
|
stream.emit('error', er); |
|
} |
|
|
|
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; |
|
|
|
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); |
|
|
|
if (!finished && |
|
!state.corked && |
|
!state.bufferProcessing && |
|
state.bufferedRequest) { |
|
clearBuffer(stream, state); |
|
} |
|
|
|
if (sync) { |
|
processNextTick(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 buffer = []; |
|
var cbs = []; |
|
while (entry) { |
|
cbs.push(entry.callback); |
|
buffer.push(entry); |
|
entry = entry.next; |
|
} |
|
|
|
// count the one we are adding, as well. |
|
// TODO(isaacs) clean this up |
|
state.pendingcb++; |
|
state.lastBufferedRequest = null; |
|
doWrite(stream, state, true, state.length, buffer, '', function(err) { |
|
for (var i = 0; i < cbs.length; i++) { |
|
state.pendingcb--; |
|
cbs[i](err); |
|
} |
|
}); |
|
|
|
// Clear buffer |
|
} 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; |
|
// 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 Error('not implemented')); |
|
}; |
|
|
|
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 && !state.finished) |
|
endWritable(this, state, cb); |
|
}; |
|
|
|
|
|
function needFinish(state) { |
|
return (state.ending && |
|
state.length === 0 && |
|
state.bufferedRequest === null && |
|
!state.finished && |
|
!state.writing); |
|
} |
|
|
|
function prefinish(stream, state) { |
|
if (!state.prefinished) { |
|
state.prefinished = true; |
|
stream.emit('prefinish'); |
|
} |
|
} |
|
|
|
function finishMaybe(stream, state) { |
|
var need = needFinish(state); |
|
if (need) { |
|
if (state.pendingcb === 0) { |
|
prefinish(stream, state); |
|
state.finished = true; |
|
stream.emit('finish'); |
|
} else { |
|
prefinish(stream, state); |
|
} |
|
} |
|
return need; |
|
} |
|
|
|
function endWritable(stream, state, cb) { |
|
state.ending = true; |
|
finishMaybe(stream, state); |
|
if (cb) { |
|
if (state.finished) |
|
processNextTick(cb); |
|
else |
|
stream.once('finish', cb); |
|
} |
|
state.ended = true; |
|
} |
|
|
|
},{"./_stream_duplex":57,"buffer":46,"core-util-is":48,"events":49,"inherits":51,"process-nextick-args":54,"util-deprecate":68}],62:[function(require,module,exports){ |
|
module.exports = require("./lib/_stream_passthrough.js") |
|
|
|
},{"./lib/_stream_passthrough.js":58}],63:[function(require,module,exports){ |
|
var Stream = (function (){ |
|
try { |
|
return require('st' + 'ream'); // hack to fix a circular dependency issue when used with browserify |
|
} catch(_){} |
|
}()); |
|
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'); |
|
|
|
},{"./lib/_stream_duplex.js":57,"./lib/_stream_passthrough.js":58,"./lib/_stream_readable.js":59,"./lib/_stream_transform.js":60,"./lib/_stream_writable.js":61}],64:[function(require,module,exports){ |
|
module.exports = require("./lib/_stream_transform.js") |
|
|
|
},{"./lib/_stream_transform.js":60}],65:[function(require,module,exports){ |
|
module.exports = require("./lib/_stream_writable.js") |
|
|
|
},{"./lib/_stream_writable.js":61}],66:[function(require,module,exports){ |
|
// 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. |
|
|
|
module.exports = Stream; |
|
|
|
var EE = require('events').EventEmitter; |
|
var inherits = require('inherits'); |
|
|
|
inherits(Stream, EE); |
|
Stream.Readable = require('readable-stream/readable.js'); |
|
Stream.Writable = require('readable-stream/writable.js'); |
|
Stream.Duplex = require('readable-stream/duplex.js'); |
|
Stream.Transform = require('readable-stream/transform.js'); |
|
Stream.PassThrough = require('readable-stream/passthrough.js'); |
|
|
|
// Backwards-compat with node 0.4.x |
|
Stream.Stream = Stream; |
|
|
|
|
|
|
|
// old-style streams. Note that the pipe method (the only relevant |
|
// part of this class) is overridden in the Readable class. |
|
|
|
function Stream() { |
|
EE.call(this); |
|
} |
|
|
|
Stream.prototype.pipe = function(dest, options) { |
|
var source = this; |
|
|
|
function ondata(chunk) { |
|
if (dest.writable) { |
|
if (false === dest.write(chunk) && source.pause) { |
|
source.pause(); |
|
} |
|
} |
|
} |
|
|
|
source.on('data', ondata); |
|
|
|
function ondrain() { |
|
if (source.readable && source.resume) { |
|
source.resume(); |
|
} |
|
} |
|
|
|
dest.on('drain', ondrain); |
|
|
|
// If the 'end' option is not supplied, dest.end() will be called when |
|
// source gets the 'end' or 'close' events. Only dest.end() once. |
|
if (!dest._isStdio && (!options || options.end !== false)) { |
|
source.on('end', onend); |
|
source.on('close', onclose); |
|
} |
|
|
|
var didOnEnd = false; |
|
function onend() { |
|
if (didOnEnd) return; |
|
didOnEnd = true; |
|
|
|
dest.end(); |
|
} |
|
|
|
|
|
function onclose() { |
|
if (didOnEnd) return; |
|
didOnEnd = true; |
|
|
|
if (typeof dest.destroy === 'function') dest.destroy(); |
|
} |
|
|
|
// don't leave dangling pipes when there are errors. |
|
function onerror(er) { |
|
cleanup(); |
|
if (EE.listenerCount(this, 'error') === 0) { |
|
throw er; // Unhandled stream error in pipe. |
|
} |
|
} |
|
|
|
source.on('error', onerror); |
|
dest.on('error', onerror); |
|
|
|
// remove all the event listeners that were added. |
|
function cleanup() { |
|
source.removeListener('data', ondata); |
|
dest.removeListener('drain', ondrain); |
|
|
|
source.removeListener('end', onend); |
|
source.removeListener('close', onclose); |
|
|
|
source.removeListener('error', onerror); |
|
dest.removeListener('error', onerror); |
|
|
|
source.removeListener('end', cleanup); |
|
source.removeListener('close', cleanup); |
|
|
|
dest.removeListener('close', cleanup); |
|
} |
|
|
|
source.on('end', cleanup); |
|
source.on('close', cleanup); |
|
|
|
dest.on('close', cleanup); |
|
|
|
dest.emit('pipe', source); |
|
|
|
// Allow for unix-like usage: A.pipe(B).pipe(C) |
|
return dest; |
|
}; |
|
|
|
},{"events":49,"inherits":51,"readable-stream/duplex.js":56,"readable-stream/passthrough.js":62,"readable-stream/readable.js":63,"readable-stream/transform.js":64,"readable-stream/writable.js":65}],67:[function(require,module,exports){ |
|
// 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. |
|
|
|
var Buffer = require('buffer').Buffer; |
|
|
|
var isBufferEncoding = Buffer.isEncoding |
|
|| function(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 assertEncoding(encoding) { |
|
if (encoding && !isBufferEncoding(encoding)) { |
|
throw new Error('Unknown encoding: ' + encoding); |
|
} |
|
} |
|
|
|
// StringDecoder provides an interface for efficiently splitting a series of |
|
// buffers into a series of JS strings without breaking apart multi-byte |
|
// characters. CESU-8 is handled as part of the UTF-8 encoding. |
|
// |
|
// @TODO Handling all encodings inside a single object makes it very difficult |
|
// to reason about this code, so it should be split up in the future. |
|
// @TODO There should be a utf8-strict encoding that rejects invalid UTF-8 code |
|
// points as used by CESU-8. |
|
var StringDecoder = exports.StringDecoder = function(encoding) { |
|
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); |
|
assertEncoding(encoding); |
|
switch (this.encoding) { |
|
case 'utf8': |
|
// CESU-8 represents each of Surrogate Pair by 3-bytes |
|
this.surrogateSize = 3; |
|
break; |
|
case 'ucs2': |
|
case 'utf16le': |
|
// UTF-16 represents each of Surrogate Pair by 2-bytes |
|
this.surrogateSize = 2; |
|
this.detectIncompleteChar = utf16DetectIncompleteChar; |
|
break; |
|
case 'base64': |
|
// Base-64 stores 3 bytes in 4 chars, and pads the remainder. |
|
this.surrogateSize = 3; |
|
this.detectIncompleteChar = base64DetectIncompleteChar; |
|
break; |
|
default: |
|
this.write = passThroughWrite; |
|
return; |
|
} |
|
|
|
// Enough space to store all bytes of a single character. UTF-8 needs 4 |
|
// bytes, but CESU-8 may require up to 6 (3 bytes per surrogate). |
|
this.charBuffer = new Buffer(6); |
|
// Number of bytes received for the current incomplete multi-byte character. |
|
this.charReceived = 0; |
|
// Number of bytes expected for the current incomplete multi-byte character. |
|
this.charLength = 0; |
|
}; |
|
|
|
|
|
// write decodes the given buffer and returns it as JS string that is |
|
// guaranteed to not contain any partial multi-byte characters. Any partial |
|
// character found at the end of the buffer is buffered up, and will be |
|
// returned when calling write again with the remaining bytes. |
|
// |
|
// Note: Converting a Buffer containing an orphan surrogate to a String |
|
// currently works, but converting a String to a Buffer (via `new Buffer`, or |
|
// Buffer#write) will replace incomplete surrogates with the unicode |
|
// replacement character. See https://codereview.chromium.org/121173009/ . |
|
StringDecoder.prototype.write = function(buffer) { |
|
var charStr = ''; |
|
// if our last write ended with an incomplete multibyte character |
|
while (this.charLength) { |
|
// determine how many remaining bytes this buffer has to offer for this char |
|
var available = (buffer.length >= this.charLength - this.charReceived) ? |
|
this.charLength - this.charReceived : |
|
buffer.length; |
|
|
|
// add the new bytes to the char buffer |
|
buffer.copy(this.charBuffer, this.charReceived, 0, available); |
|
this.charReceived += available; |
|
|
|
if (this.charReceived < this.charLength) { |
|
// still not enough chars in this buffer? wait for more ... |
|
return ''; |
|
} |
|
|
|
// remove bytes belonging to the current character from the buffer |
|
buffer = buffer.slice(available, buffer.length); |
|
|
|
// get the character that was split |
|
charStr = this.charBuffer.slice(0, this.charLength).toString(this.encoding); |
|
|
|
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character |
|
var charCode = charStr.charCodeAt(charStr.length - 1); |
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
|
this.charLength += this.surrogateSize; |
|
charStr = ''; |
|
continue; |
|
} |
|
this.charReceived = this.charLength = 0; |
|
|
|
// if there are no more bytes in this buffer, just emit our char |
|
if (buffer.length === 0) { |
|
return charStr; |
|
} |
|
break; |
|
} |
|
|
|
// determine and set charLength / charReceived |
|
this.detectIncompleteChar(buffer); |
|
|
|
var end = buffer.length; |
|
if (this.charLength) { |
|
// buffer the incomplete character bytes we got |
|
buffer.copy(this.charBuffer, 0, buffer.length - this.charReceived, end); |
|
end -= this.charReceived; |
|
} |
|
|
|
charStr += buffer.toString(this.encoding, 0, end); |
|
|
|
var end = charStr.length - 1; |
|
var charCode = charStr.charCodeAt(end); |
|
// CESU-8: lead surrogate (D800-DBFF) is also the incomplete character |
|
if (charCode >= 0xD800 && charCode <= 0xDBFF) { |
|
var size = this.surrogateSize; |
|
this.charLength += size; |
|
this.charReceived += size; |
|
this.charBuffer.copy(this.charBuffer, size, 0, size); |
|
buffer.copy(this.charBuffer, 0, 0, size); |
|
return charStr.substring(0, end); |
|
} |
|
|
|
// or just emit the charStr |
|
return charStr; |
|
}; |
|
|
|
// detectIncompleteChar determines if there is an incomplete UTF-8 character at |
|
// the end of the given buffer. If so, it sets this.charLength to the byte |
|
// length that character, and sets this.charReceived to the number of bytes |
|
// that are available for this character. |
|
StringDecoder.prototype.detectIncompleteChar = function(buffer) { |
|
// determine how many bytes we have to check at the end of this buffer |
|
var i = (buffer.length >= 3) ? 3 : buffer.length; |
|
|
|
// Figure out if one of the last i bytes of our buffer announces an |
|
// incomplete char. |
|
for (; i > 0; i--) { |
|
var c = buffer[buffer.length - i]; |
|
|
|
// See http://en.wikipedia.org/wiki/UTF-8#Description |
|
|
|
// 110XXXXX |
|
if (i == 1 && c >> 5 == 0x06) { |
|
this.charLength = 2; |
|
break; |
|
} |
|
|
|
// 1110XXXX |
|
if (i <= 2 && c >> 4 == 0x0E) { |
|
this.charLength = 3; |
|
break; |
|
} |
|
|
|
// 11110XXX |
|
if (i <= 3 && c >> 3 == 0x1E) { |
|
this.charLength = 4; |
|
break; |
|
} |
|
} |
|
this.charReceived = i; |
|
}; |
|
|
|
StringDecoder.prototype.end = function(buffer) { |
|
var res = ''; |
|
if (buffer && buffer.length) |
|
res = this.write(buffer); |
|
|
|
if (this.charReceived) { |
|
var cr = this.charReceived; |
|
var buf = this.charBuffer; |
|
var enc = this.encoding; |
|
res += buf.slice(0, cr).toString(enc); |
|
} |
|
|
|
return res; |
|
}; |
|
|
|
function passThroughWrite(buffer) { |
|
return buffer.toString(this.encoding); |
|
} |
|
|
|
function utf16DetectIncompleteChar(buffer) { |
|
this.charReceived = buffer.length % 2; |
|
this.charLength = this.charReceived ? 2 : 0; |
|
} |
|
|
|
function base64DetectIncompleteChar(buffer) { |
|
this.charReceived = buffer.length % 3; |
|
this.charLength = this.charReceived ? 3 : 0; |
|
} |
|
|
|
},{"buffer":46}],68:[function(require,module,exports){ |
|
(function (global){ |
|
|
|
/** |
|
* Module exports. |
|
*/ |
|
|
|
module.exports = deprecate; |
|
|
|
/** |
|
* Mark that a method should not be used. |
|
* Returns a modified function which warns once by default. |
|
* |
|
* If `localStorage.noDeprecation = true` is set, then it is a no-op. |
|
* |
|
* If `localStorage.throwDeprecation = true` is set, then deprecated functions |
|
* will throw an Error when invoked. |
|
* |
|
* If `localStorage.traceDeprecation = true` is set, then deprecated functions |
|
* will invoke `console.trace()` instead of `console.error()`. |
|
* |
|
* @param {Function} fn - the function to deprecate |
|
* @param {String} msg - the string to print to the console when `fn` is invoked |
|
* @returns {Function} a new "deprecated" version of `fn` |
|
* @api public |
|
*/ |
|
|
|
function deprecate (fn, msg) { |
|
if (config('noDeprecation')) { |
|
return fn; |
|
} |
|
|
|
var warned = false; |
|
function deprecated() { |
|
if (!warned) { |
|
if (config('throwDeprecation')) { |
|
throw new Error(msg); |
|
} else if (config('traceDeprecation')) { |
|
console.trace(msg); |
|
} else { |
|
console.warn(msg); |
|
} |
|
warned = true; |
|
} |
|
return fn.apply(this, arguments); |
|
} |
|
|
|
return deprecated; |
|
} |
|
|
|
/** |
|
* Checks `localStorage` for boolean values for the given `name`. |
|
* |
|
* @param {String} name |
|
* @returns {Boolean} |
|
* @api private |
|
*/ |
|
|
|
function config (name) { |
|
// accessing global.localStorage can trigger a DOMException in sandboxed iframes |
|
try { |
|
if (!global.localStorage) return false; |
|
} catch (_) { |
|
return false; |
|
} |
|
var val = global.localStorage[name]; |
|
if (null == val) return false; |
|
return String(val).toLowerCase() === 'true'; |
|
} |
|
|
|
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) |
|
},{}]},{},[1]); |