function InternalDecoder(options, codec) {
    StringDecoder.call(this, codec.enc);
}
 buffers.read(15, function(buf) {
     str = str + decoder.write(buf);
 });
Beispiel #3
0
 this.pty.on('data', data => {
   if (this.ended) {
     return;
   }
   this.emit('data', decoder.write(data));
 });
var server = http.createServer(function(req,res){

  // Parse the url
  var parsedUrl = url.parse(req.url, true);

  // Get the path
  var path = parsedUrl.pathname;
  var trimmedPath = path.replace(/^\/+|\/+$/g, '');

  // Get the query string as an object
  var queryStringObject = parsedUrl.query;

  // Get the HTTP method
  var method = req.method.toLowerCase();

  //Get the headers as an object
  var headers = req.headers;

  // Get the payload,if any
  var decoder = new StringDecoder('utf-8');
  var buffer = '';
  req.on('data', function(data) {
      buffer += decoder.write(data);
  });
  req.on('end', function() {
      buffer += decoder.end();

      // Check the router for a matching path for a handler. If one is not found, use the notFound handler instead.
      var chosenHandler = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound;

      // Construct the data object to send to the handler
      var data = {
        'trimmedPath' : trimmedPath,
        'queryStringObject' : queryStringObject,
        'method' : method,
        'headers' : headers,
        'payload' : buffer
      };

      // Route the request to the handler specified in the router
      chosenHandler(data,function(statusCode,payload){

        // Use the status code returned from the handler, or set the default status code to 200
        statusCode = typeof(statusCode) == 'number' ? statusCode : 200;

        // Use the payload returned from the handler, or set the default payload to an empty object
        payload = typeof(payload) == 'object'? payload : {};

        // Convert the payload to a string
        var payloadString = JSON.stringify(payload);

        // Return the response
        res.setHeader('Content-Type', 'application/json');
        res.writeHead(statusCode);
        res.end(payloadString);
        console.log("Returning this response: ",statusCode,payloadString);

      });

  });
});
Beispiel #5
0
	self._transform = function(chunk, enc, done) {
		data += decoder.write(chunk);
		handleState();
		done();
	};
Beispiel #6
0
 response.on('data', function (chunk) {
   if (typeof chunk === 'string') response.body += chunk
   else response.body += decoder.write(chunk)
 })
 svgFontStream.on('data', chunk => {
   content += decoder.write(chunk);
 });
Beispiel #8
0
 remoteRes.on('data', function(chunk) {
   var textChunk = decoder.write(chunk);
   console.log('Remote server response:', textChunk);
   res.send(textChunk);
 });
module.exports = function onTextParam(part) {

  var self = this;
  var field = part.name;
  var value = '';
  var decoder = new StringDecoder(this.form.encoding);

  // Track fields that receive multiple param values
  //
  // > FUTURE: remove this, it's no longer necessary and is a bit confusing
  // > (see notes below about the new, header-based approach for encoding nested
  // > dictionaries and arrays in text params of multipart/form-data requests)
  self.multifields = self.multifields || {};

  // After control has been relinquished, any textparams received should be ignored
  // since its too late to include them in `req.body` (subsequent app code is already running)
  // So emit a warning.
  if (this._hasPassedControlToApp) {
    this.emit('warning',
      'Unable to expose body parameter `'+field+'` in streaming upload!\n'+
      'Client tried to send a text parameter ('+field+') ' +
      'after one or more files had already been sent.\n'+
      'Make sure you always send text params first, then your files.\n'+
      '(In an HTML form, it\'s as easy as making sure your text inputs are listed before your file inputs.'
    );
    // Redirect the part stream into the data toilet.
    // This ensures that the stream will close properly.
    part.on('readable', function onBytesAvailable() {
      // Read until the buffer is dry.
      while (null !== part.read()) { /* no-op */ }
    });
    return;
  }

  // Track the newly detected param
  var textParamMetadata = {
    done: false,
    stream: part
  };
  this.textParams.push(textParamMetadata);


  // Now receive bytes from the text param:

  // FUTURE: Try to use pipe instead, or better yet, defer to the built-in handling
  // w/i formidable/multiparty (as long as we're absolutely certain that doesn't
  // trigger writing .tmp files to disk.)
  part.on('readable', function onBytesAvailable() {

    var buffer = '';
    var chunk;
    while (null !== (chunk = part.read())) {
      buffer += chunk;
    }

    // New bytes available for text param:
    if (buffer) {

      // FUTURE: make `maxFieldsSize` directly configurable via `options`
      self.form._fieldsSize += buffer.length;
      if (self.form._fieldsSize > self.form.maxFieldsSize) {
        self.form._error(new Error('maxFieldsSize exceeded, received ' + self.form._fieldsSize + ' bytes of field data'));
        return;
      }
      value += decoder.write(buffer);
      debug('Parser: Read a chunk of textparam through field `' + field + '`');
      return;
    }//•

    // Otherwise, IWMIH, buffer is null, meaning we've now received all of the
    // bytes from the textparam.
    debug('Parser: Done reading textparam through field `' + field + '`');

    // Disregard this field if it is a problematic field name (e.g. "constructor" or "__proto__").
    // > Inspired by https://github.com/ljharb/qs/commit/dbf049d9fafb5c0716a3bcae80af6f4b8337b9f4
    // > And https://github.com/sailshq/lodash/commit/3f3d78dc3f756f4148a3832798e868ca7192c817#diff-6d186b954a58d5bb740f73d84fe39073R640
    if (Object.prototype.hasOwnProperty(field)) {
      textParamMetadata.done = true;// « see below for explanation of what this is doing
      return;
    }//•

    // Since this is a multipart HTTP request, there's no widely-adopted and
    // widely-trusted convention for sending big, complex data structures as
    // text parameters from the client, then losslessly rehydrating them on
    // the server.
    //
    // So to work around this, we'll check for a special "X-JSON-MPU-Params" header
    // in the request (could be sent by anything, but if present, it was likely
    // sent from Parasails/Cloud SDK).  If this header is set, and clearly
    // formatted for our use case here, then we'll attempt to JSON.parse() the
    // body text params that it indicates.
    // > Note that we are tolerant towards problems, since it is hypothetically
    // > possible it could be sent for some unrelated reason.
    //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // > Note that text parameters can (probably) not be encoded in a format like
    // > JSON as part of the multipart upload HTTP spec  (at least not cleanly).
    // > For more information/discussion on this, see:
    // > • https://github.com/postmanlabs/postman-app-support/issues/3331
    // > • https://github.com/postmanlabs/postman-app-support/issues/1104
    // >
    // > Also note that Sails/parasails/Cloud SDK automatically appends a special
    // > header to the request called "X-JSON-MPU-Params" which identifies the names
    // > of text parameters that are sent as stringified JSON and thus should be
    // > parsed as such on the server.  (Only relevant for "multipart/form-data".)
    // >
    // > Last but not least, some historical context:
    // >  • https://github.com/sailshq/machine-as-action/commit/16062c568d0587ea0b228613a686071666b6e690
    // >  • see GitHub comments on that commit for related changes in other packages
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if (_.isFunction(self.req.get)) {
      var rawXJSONMPUParamsHeader = self.req.get('X-JSON-MPU-Params');
      if (rawXJSONMPUParamsHeader && _.isString(rawXJSONMPUParamsHeader) && _.find(rawXJSONMPUParamsHeader.split(','), function(paramName) { return paramName === field; })) {
        try {
          value = JSON.parse(value);
        } catch (unusedErr) { /* Silently ignore any JSON parsing errors (as explained above) */ }
      }//fi
    }//fi

    // If `req.body` already contains `field`, and this is the first duplicate value
    // (i.e. the second value to come in for this param) track it as a "multifield"
    // and build an array of param values.
    // (We have to do this in case the original value was an array itself- we wouldn't
    // want to push subsequent values onto THAT array, y'know?)
    //
    // > FUTURE: remove this "multifield" behavior (see above for explanation)
    if (self.req.body[field]) {
      if (self.multifields[field]) {
        self.req.body[field].push(value);
      } else {
        debug('`' + field + '` param already exists in req.body, converting into a "multifield"...');
        self.req.body[field] = [self.req.body[field]];
        self.multifields[field] = true;
        self.req.body[field].push(value);
      }
    } else {
      self.req.body[field] = value;
    }

    // Mark that this textParam is done streaming in data in its
    // `textParamMetadata` object.  This is monitored and used so
    // we know to wait for any known textParams to finish streaming
    // before we pass control to the app.
    textParamMetadata.done = true;

  });


};
Beispiel #10
0
 phantom.stderr.on("data", function(data) {
     log("PhantomJS ran into errors:\n");
     log(decoder.write(data));
 });
 var handler = function(data) {
     process.stdout.write(data ? (decoder.write(data) + decoder.end()) : "");
 };
Beispiel #12
0
 phantom.stdout.on("data", function(data) {
     log("Output from PhantomJS:\n");
     log(decoder.write(data));
 });
Beispiel #13
0
 req.pipe(concatStream(function(data) {
     var decoder = new stringDecoder.StringDecoder();
     var xml = decoder.write(data);
     //TODO: req.body = XML.parse(xml);
     next();
 }));
Beispiel #14
0
 req.on('data', data => {
   buffer += decoder.write(data)
 })
Beispiel #15
0
function main(conf) {
  const encoding = conf.encoding;
  const inLen = conf.inlen | 0;
  const chunkLen = conf.chunk | 0;
  const n = conf.n | 0;

  var alpha;
  var buf;
  const chunks = [];
  var str = '';
  const isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8');
  var i;

  if (encoding === 'ascii' || encoding === 'base64-ascii')
    alpha = ASC_ALPHA;
  else if (encoding === 'utf8' || encoding === 'base64-utf8')
    alpha = UTF8_ALPHA;
  else if (encoding === 'utf16le') {
    buf = UTF16_BUF;
    str = Buffer.alloc(0);
  } else
    throw new Error('Bad encoding');

  const sd = new StringDecoder(isBase64 ? 'base64' : encoding);

  for (i = 0; i < inLen; ++i) {
    if (i > 0 && (i % chunkLen) === 0 && !isBase64) {
      if (alpha) {
        chunks.push(Buffer.from(str, encoding));
        str = '';
      } else {
        chunks.push(str);
        str = Buffer.alloc(0);
      }
    }
    if (alpha)
      str += alpha[i % alpha.length];
    else {
      var start = i;
      var end = i + 2;
      if (i % 2 !== 0) {
        ++start;
        ++end;
      }
      str = Buffer.concat([
        str,
        buf.slice(start % buf.length, end % buf.length)
      ]);
    }
  }

  if (!alpha) {
    if (str.length > 0)
      chunks.push(str);
  } else if (str.length > 0 && !isBase64)
    chunks.push(Buffer.from(str, encoding));

  if (isBase64) {
    str = Buffer.from(str, 'utf8').toString('base64');
    while (str.length > 0) {
      const len = Math.min(chunkLen, str.length);
      chunks.push(Buffer.from(str.substring(0, len), 'utf8'));
      str = str.substring(len);
    }
  }

  var nChunks = chunks.length;

  bench.start();
  for (i = 0; i < n; ++i) {
    for (var j = 0; j < nChunks; ++j)
      sd.write(chunks[j]);
  }
  bench.end(n);
}
  part.on('readable', function onBytesAvailable() {

    var buffer = '';
    var chunk;
    while (null !== (chunk = part.read())) {
      buffer += chunk;
    }

    // New bytes available for text param:
    if (buffer) {

      // FUTURE: make `maxFieldsSize` directly configurable via `options`
      self.form._fieldsSize += buffer.length;
      if (self.form._fieldsSize > self.form.maxFieldsSize) {
        self.form._error(new Error('maxFieldsSize exceeded, received ' + self.form._fieldsSize + ' bytes of field data'));
        return;
      }
      value += decoder.write(buffer);
      debug('Parser: Read a chunk of textparam through field `' + field + '`');
      return;
    }//•

    // Otherwise, IWMIH, buffer is null, meaning we've now received all of the
    // bytes from the textparam.
    debug('Parser: Done reading textparam through field `' + field + '`');

    // Disregard this field if it is a problematic field name (e.g. "constructor" or "__proto__").
    // > Inspired by https://github.com/ljharb/qs/commit/dbf049d9fafb5c0716a3bcae80af6f4b8337b9f4
    // > And https://github.com/sailshq/lodash/commit/3f3d78dc3f756f4148a3832798e868ca7192c817#diff-6d186b954a58d5bb740f73d84fe39073R640
    if (Object.prototype.hasOwnProperty(field)) {
      textParamMetadata.done = true;// « see below for explanation of what this is doing
      return;
    }//•

    // Since this is a multipart HTTP request, there's no widely-adopted and
    // widely-trusted convention for sending big, complex data structures as
    // text parameters from the client, then losslessly rehydrating them on
    // the server.
    //
    // So to work around this, we'll check for a special "X-JSON-MPU-Params" header
    // in the request (could be sent by anything, but if present, it was likely
    // sent from Parasails/Cloud SDK).  If this header is set, and clearly
    // formatted for our use case here, then we'll attempt to JSON.parse() the
    // body text params that it indicates.
    // > Note that we are tolerant towards problems, since it is hypothetically
    // > possible it could be sent for some unrelated reason.
    //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // > Note that text parameters can (probably) not be encoded in a format like
    // > JSON as part of the multipart upload HTTP spec  (at least not cleanly).
    // > For more information/discussion on this, see:
    // > • https://github.com/postmanlabs/postman-app-support/issues/3331
    // > • https://github.com/postmanlabs/postman-app-support/issues/1104
    // >
    // > Also note that Sails/parasails/Cloud SDK automatically appends a special
    // > header to the request called "X-JSON-MPU-Params" which identifies the names
    // > of text parameters that are sent as stringified JSON and thus should be
    // > parsed as such on the server.  (Only relevant for "multipart/form-data".)
    // >
    // > Last but not least, some historical context:
    // >  • https://github.com/sailshq/machine-as-action/commit/16062c568d0587ea0b228613a686071666b6e690
    // >  • see GitHub comments on that commit for related changes in other packages
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    if (_.isFunction(self.req.get)) {
      var rawXJSONMPUParamsHeader = self.req.get('X-JSON-MPU-Params');
      if (rawXJSONMPUParamsHeader && _.isString(rawXJSONMPUParamsHeader) && _.find(rawXJSONMPUParamsHeader.split(','), function(paramName) { return paramName === field; })) {
        try {
          value = JSON.parse(value);
        } catch (unusedErr) { /* Silently ignore any JSON parsing errors (as explained above) */ }
      }//fi
    }//fi

    // If `req.body` already contains `field`, and this is the first duplicate value
    // (i.e. the second value to come in for this param) track it as a "multifield"
    // and build an array of param values.
    // (We have to do this in case the original value was an array itself- we wouldn't
    // want to push subsequent values onto THAT array, y'know?)
    //
    // > FUTURE: remove this "multifield" behavior (see above for explanation)
    if (self.req.body[field]) {
      if (self.multifields[field]) {
        self.req.body[field].push(value);
      } else {
        debug('`' + field + '` param already exists in req.body, converting into a "multifield"...');
        self.req.body[field] = [self.req.body[field]];
        self.multifields[field] = true;
        self.req.body[field].push(value);
      }
    } else {
      self.req.body[field] = value;
    }

    // Mark that this textParam is done streaming in data in its
    // `textParamMetadata` object.  This is monitored and used so
    // we know to wait for any known textParams to finish streaming
    // before we pass control to the app.
    textParamMetadata.done = true;

  });
Beispiel #17
0
 Note.get(note_id, function(err, note) {
     if (err) return res.json({err: err});
     var decoder = new StringDecoder('utf8');
     note.content = decoder.write(note.content);
     res.json(note);
 });
Beispiel #18
0
readable.on('data',function(chunk){
console.log(chunk);
console.log(decoder.write(chunk));
});
Beispiel #19
0
 sequence.forEach(function(write) {
   output += decoder.write(input.slice(write[0], write[1]));
 });
Beispiel #20
0
  this._pasv(function(err, sock, id) {
    if (err)
      return cb(err);

    if (self._worker[id].getQueue()[0] && self._worker[id].getQueue()[0].cmd === 'ABOR') {
      sock.destroy();
      return cb();
    }

    var sockerr, done = false, replies = 0, entries, buffer = '', source = sock;
    var decoder = new StringDecoder('utf8');

    if (zcomp) {
      source = zlib.createInflate();
      sock.pipe(source);
    }

    source.on('data', function(chunk) {
      buffer += decoder.write(chunk);
    });
    source.once('error', function(err) {
      if (!sock.aborting)
        sockerr = err;
    });
    source.once('end', ondone);
    source.once('close', ondone);

    function ondone() {
      if (decoder) {
        buffer += decoder.end();
        decoder = null;
      }
      done = true;
      final();
    }
    function final() {
      if (done && replies === 2) {
        replies = 3;
        if (sockerr)
          return cb(new Error('Unexpected data connection error: ' + sockerr));
        if (sock.aborting)
          return cb();

        // process received data
        entries = buffer.split(RE_EOL);
        entries.pop(); // ending EOL
        var parsed = [];
        for (var i = 0, len = entries.length; i < len; ++i) {
          var parsedVal = Parser.parseListEntry(entries[i]);
          if (parsedVal !== null)
            parsed.push(parsedVal);
        }

        if (zcomp) {
          self._sendc(id, 'MODE S', function() {
            cb(undefined, parsed);
          }, true);
        } else
          cb(undefined, parsed);
      }
    }

    if (zcomp) {
      self._sendc(id, 'MODE Z', function(err, text, code) {
        if (err) {
          sock.destroy();
          return cb(makeError(code, 'Compression not supported'));
        }
        sendList();
      }, true);
    } else
      sendList();

    function sendList() {
      // this callback will be executed multiple times, the first is when server
      // replies with 150 and then a final reply to indicate whether the
      // transfer was actually a success or not
      self._sendc(id, cmd, function(err, text, code) {
        if (err) {
          sock.destroy();
          if (zcomp) {
            self._sendc(id, 'MODE S', function() {
              cb(err);
            }, true);
          } else
            cb(err);
          return;
        }

        // some servers may not open a data connection for empty directories
        if (++replies === 1 && code === 226) {
          replies = 2;
          sock.destroy();
          final();
        } else if (replies === 2)
          final();
      }, true);
    }
  });
Beispiel #21
0
var buf4=new Buffer(12);
buf4.write('zhenglei',0,6,'utf8');
buf4.write('好帅',6,6,'utf8');
console.log(buf4.toString());
//乱码的处理
var buf5=new Buffer('郑磊有才');
console.log(buf5);
var buf1=buf5.slice(0,7);
var buf2=buf5.slice(7);
console.log(buf1);
console.log(buf2);
console.log(buf1.toString());
console.log(buf2.toString());
//StringDecoder
var StringDecoder=require('string_decoder').StringDecoder;
var decoder=new StringDecoder();
console.log(decoder.write(buf1));
console.log(decoder.write(buf2));
//Number 双精度的Double
//value,offset
var buf=new Buffer(4);
buf.writeInt8(0,0);
buf.writeInt8(16,1);
buf.writeInt8(32,2);
buf.writeInt8(64,3);
console.log(buf);
console.log(buf.readInt8(0));
console.log(buf.readInt8(1));
console.log(buf.readInt8(2));
console.log(buf.readInt8(3));
Beispiel #22
0
 source.on('data', function(chunk) {
   buffer += decoder.write(chunk);
 });
 req.on('data', function(data) {
     buffer += decoder.write(data);
 });
			res.on('data', (d) => {
				resRaw.push(DECODER.write(d));
			});
Beispiel #25
0
var HTTPReaderStream = function(options) {
	var self = this;
	Transform.call(self, options);
	var state = States.TRY_STATUS;
	var data = '';
	var decoder = new StringDecoder(STRING_ENCODING);
	var contentLength = null;

	var parseStatusLine = function() {
		var p = data.indexOf(consts.CRLF);
		if (p === -1) {
			return;
		}
		var statusLine = HTTPStatusLineRE.exec(data.slice(0, p));
		if (!statusLine) {
			return;
		}
		data = data.slice(p + 2);
		state = States.TRY_HEADERS;
		self.emit('status', statusLine[1]);
		parseHeaders();
	};

	var parseHeaders = function() {
		var p = data.indexOf(consts.CRLF + consts.CRLF);
		if (p === -1) {
			return;
		}
		var headers = data.slice(0, p).split(consts.CRLF);
		if (!headers || !headers.length) {
			return;
		}
		data = data.slice(p + 4);
		state = States.TRY_CONTENT;
		contentLength = null;
		headers.forEach(function(line) {
			var match = HTTPHeaderRE.exec(line.toLowerCase());
			if (match) {
				if (match[1] === 'transfer-encoding' && match[2].trim() === 'chunked') {
					state = States.TRY_CHUNK;
				} else if (match[1] === 'content-length') {
					contentLength = parseInt(match[2].trim());
				}
			}
		});
		self.emit('headers', headers);
		handleState();
	};

	var parseChunk = function() {
		var p = data.indexOf(consts.CRLF);
		while (p !== -1) {
			var size = parseInt(data.slice(0, p), 16);
			if (size === 0) {
				data = data.slice(p + 4);
				state = States.TRY_STATUS;
				self.emit('chunked_end');
				return;
			}
			var tmp = data.slice(p + 2);
			if (tmp.length < (size + 2)) {
				return;
			}
			self.push(new Buffer(tmp.slice(0, size), STRING_ENCODING));
			data = tmp.slice(size + 2);
			p = data.indexOf(consts.CRLF);
		}
	};

	var parseContent = function() {
		var len = contentLength !== null ? contentLength : data.length;
		if (len > data.length) {
			// XXX we should have a limit here, or we could buffer "forever"
			return;
		}
		self.push(new Buffer(data.slice(0, len), STRING_ENCODING));
		data = data.slice(len);
		state = States.TRY_STATUS;
		process.nextTick(parseStatusLine);
	};

	var handleState = function() {
		switch (state) {
		case States.TRY_STATUS:
			parseStatusLine();
			break;
		case States.TRY_HEADERS:
			parseHeaders();
			break;
		case States.TRY_CHUNK:
			parseChunk();
			break;
		case States.TRY_CONTENT:
			parseContent();
			break;
		}
	};

	self._transform = function(chunk, enc, done) {
		data += decoder.write(chunk);
		handleState();
		done();
	};

	self._flush = function(done) {
		handleState();
		done();		
	};

	return self;
};
 let addToBuffer = data => {
   let str = this.decoder.write(data);
   this.outbuff += str;
   debug(str);
 }
bufUTF8 = new Buffer("Some UTF8 Text \u00b6 \u30c6 \u20ac", 'utf8');

console.log(bufUTF8.toString());
console.log(bufUTF8.toString('utf8', 5, 9));

var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');

console.log(decoder.write(bufUTF8));
console.log(bufUTF8[18].toString(16));
console.log(bufUTF8.readUInt32BE(18).toString(16));
Beispiel #28
0
export function bufferToString(arrayBuffer) {
	let StringDecoder = require('string_decoder').StringDecoder,
		decoder = new StringDecoder('utf8'),
		tmpBuf = new Buffer(arrayBuffer);
	return decoder.write(tmpBuf);
}
Beispiel #29
0
 req.on("data", function (d) {
   b += decoder.write(d)
 })
 response.on('data', function (chunk) {
     data += decoder.write(chunk);
 });