From 2a349f1260a460c77d3c1ae71f9088f35a0b5d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20De=20Caluw=C3=A9?= Date: Fri, 11 Mar 2016 18:52:03 +0100 Subject: [PATCH] WIP --- encoder.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 encoder.js diff --git a/encoder.js b/encoder.js new file mode 100644 index 0000000..6b62030 --- /dev/null +++ b/encoder.js @@ -0,0 +1,91 @@ +/** + * @author Tom De Caluwé + * @copyright 2016 Tom De Caluwé + * @license Apache-2.0 + * + * 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. + */ + +'use strict' + +var EventEmitter = require('events'); + +var Configuration = require('./configuration.js'); +var Tokenizer = require('./tokenizer.js'); + +var push = function (encoder, chunk) { + if (encoder._paused) { + this._buffer.push(chunk); + } else { + encoder.emit('data', chunk); + } +} + +var Encoder = function () { + EventEmitter.call(this); + this._buffer = []; + this._paused = true; + this._start = 0; + this._configuration = new Configuration(); + this._tokenizer = new Tokenizer(this._configuration); +} + +Encoder.prototype = Object.create(EventEmitter.prototype); + +Encoder.prototype.opensegment = function (segment) { + this.push(segment); +} + +Encoder.prototype.element = function (components) { +} + +Encoder.prototype.pause = function () { + this._paused = true; +} + +Encoder.prototype.pipe = function (destination) { + destination.setDefaultEncoding('utf8'); + this.on('data', function (chunk) { + destination.write(chunk); + }); +} + +Encoder.prototype.resume = function () { + for (var i = 0; i < this._buffer.length; i++) { + this.emit('data', this._buffer[i]); + } + this._buffer.length = 0; + this._paused = false; +} + +Encoder.prototype.read = function () { + return this._buffer[this._start++]; +} + +Encoder.prototype.unpipe = function (destination) {} + +Encoder.prototype.unshift = function (chunk) { + if (this._paused) { + this._buffer.unshift(chunk); + } else { + this.emit('data', chunk); + } +} + +Encoder.states = { + empty: 0, + segment: 1, + element: 2, + component: 3, + data: 4 +};