UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

78 lines (58 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataHandler = void 0; /** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ /* strict */ //separator for messages according to the protocol const TWO_CRLF = "\r\n\r\n"; class DataHandler { constructor() { this._rawData = new Buffer(0); this._contentLength = -1; } handleData(data, messageProcessor) { this._rawData = Buffer.concat([this._rawData, data]); // the following code parses a message according to the protocol. while (this._rawData.length > 0) { // if we know what length we are expecting if (this._contentLength >= 0) { // we have enough data to check for the expected message if (this._rawData.byteLength >= this._contentLength) { // first get the expected message let message = this._rawData.toString("utf8", 0, this._contentLength); // reduce the buffer by the message we got this._rawData = this._rawData.slice(this._contentLength); // reset the content length to ensure it is extracted for the next message this._contentLength = -1; // process the message messageProcessor(message); continue; // there may be more complete messages to process } } else { // if we don't know the length to expect, we need to extract it first let idx = this._rawData.indexOf(TWO_CRLF); if (idx !== -1) { let header = this._rawData.toString("utf8", 0, idx); let lines = header.split("\r\n"); for (let i = 0; i < lines.length; i++) { let pair = lines[i].split(/: +/); if (pair[0] === "Content-Length") { this._contentLength = parseInt(pair[1], 10); // reset the contentlength if it is invalid if (isNaN(this._contentLength)) this._contentLength = -1; } } this._rawData = this._rawData.slice(idx + TWO_CRLF.length); continue; } // if we don't find the length we fall through and break } break; } } } exports.DataHandler = DataHandler; //# sourceMappingURL=DataHandler.js.map