Example #1
0
File: asm.js Project: js-js/jit.js
BaseAsm.prototype.emitl = function emitl(buf) {
  this._reserve(4);

  if (Buffer.isBuffer(buf)) {
    assert.equal(buf.length, 4);
    this._buffer.copyFrom(buf);
  } else {
    this._buffer.writeUInt32LE(buf);
  }
};
Example #2
0
          node.findNode(infohash, function(err, msg) {
            if (err) return;
            if (!msg || !Buffer.isBuffer(msg.nodes)) return;

            // Add nodes
            var nodes = utils.decodeNodes(msg.nodes);
            nodes.forEach(function(node) {
              self.addNode(node);
            });
          });
Example #3
0
function supportsFiletype (typeOrBuffer) {
  var type
  if (Buffer.isBuffer(typeOrBuffer)) {
    type = getFileType(typeOrBuffer)
  } else if (typeof typeOrBuffer === 'string') {
    type = typeOrBuffer
  }

  return SUPPORTED_FILETYPES.indexOf(type) !== -1
}
Example #4
0
function hmac(fn, key, data) {
  if(!Buffer.isBuffer(key)) key = new Buffer(key)
  if(!Buffer.isBuffer(data)) data = new Buffer(data)

  if(key.length > blocksize) {
    key = fn(key)
  } else if(key.length < blocksize) {
    key = Buffer.concat([key, zeroBuffer], blocksize)
  }

  var ipad = new Buffer(blocksize), opad = new Buffer(blocksize)
  for(var i = 0; i < blocksize; i++) {
    ipad[i] = key[i] ^ 0x36
    opad[i] = key[i] ^ 0x5C
  }

  var hash = fn(Buffer.concat([ipad, data]))
  return fn(Buffer.concat([opad, hash]))
}
function chunkInvalid(state, chunk) {
  var er = null;
  if (!Buffer.isBuffer(chunk) &&
    'string' !== typeof chunk &&
    chunk !== null &&
    chunk !== undefined && !state.objectMode && !er) {
    er = new TypeError('Invalid non-string/buffer chunk');
  }
  return er;
}
Example #6
0
StringReader.prototype.resume = function () {
    if (this.encoding && Buffer.isBuffer(this.data)) {
        this.emit('data', this.data.toString(this.encoding));
    }
    else {
        this.emit('data', this.data);
    }
    this.emit('end');
    this.emit('close');
}
Example #7
0
export function encryptWithCipher (cipherName, data, key, cb) {
  assert(typeof cipherName === 'string', 'expected "String" cipherName')
  assert(Buffer.isBuffer(data), 'expected Buffer "data"')
  assert(Buffer.isBuffer(key), 'expected Buffer "key"')
  assert(typeof cb === 'function', 'expected Function "cb"')
  return RNAES.encryptWithCipher(
    cipherName,
    toBase64String(data),
    toBase64String(key),
    function (err, result) {
      if (err) return cb(normalizeError(err))

      cb(null, {
        ciphertext: toBuffer(result.ciphertext),
        iv: toBuffer(result.iv)
      })
    }
  )
}
Example #8
0
 Stream.prototype.write = function write(data, encoding, cb) {
   if (typeof encoding === 'function' && !cb) {
     cb = encoding;
     encoding = null;
   }
   if (!Buffer.isBuffer(data))
     return this._write(new Buffer(data, encoding), null, cb);
   else
     return this._write(data, encoding, cb);
 };
export const toByteArray = (data) => {
	switch (true) {
		case Buffer.isBuffer(data):
			return bufferToByteArray(data);
		case typeof data === 'string':
			return stringToByteArray(data);
		default:
			throw new Error(`Can't convert ${typeof data} to ByteArray.`);
	}
};
Example #10
0
 var streamWrite = function (chunk, encoding, callback) {
   if (Buffer.isBuffer(chunk)) {
     chunk = chunk.toString(encoding)
   }
   process.log(chunk)
   if (callback) {
     callback()
   }
   return true
 }
Example #11
0
            stream.on('data', function(newFile){
                numberOfNewFiles++;
                should.exist(newFile);
                should.exist(newFile.path);
                should.exist(newFile.relative);
                should.exist(newFile.contents);

                newFile.relative.should.equal('styles.css');
                newFile.contents.toString('utf8').should.equal('');
                Buffer.isBuffer(newFile.contents).should.equal(true);
            });
WebSocketConnection.prototype.pong = function(binaryPayload) {
    var frame = new WebSocketFrame(this.maskBytes, this.frameHeader, this.config);
    frame.opcode = 0x0A; // WebSocketOpcode.PONG
    if (Buffer.isBuffer(binaryPayload) && binaryPayload.length > 125) {
        // console.warn("WebSocket: Data for pong is longer than 125 bytes.  Truncating.");
        binaryPayload = binaryPayload.slice(0,124);
    }
    frame.binaryPayload = binaryPayload;
    frame.fin = true;
    this.sendFrame(frame);
};
WebSocketConnection.prototype.send = function(data) {
    if (Buffer.isBuffer(data)) {
        this.sendBytes(data);
    }
    else if (typeof(data['toString']) === 'function') {
        this.sendUTF(data);
    }
    else {
        throw new Error("Data provided must either be a Node Buffer or implement toString()")
    }
};
Example #14
0
// 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;
}
 zkk.a_get(path, false, function(rc3, error3, stat3, value3) { // response
     if( Buffer.isBuffer(value3) === false ) {
         console.log('ERROR (p2) value3 is not a Buffer, is: ' + (typeof(value3)));
         console.log(require('sys').inspect(value3));
         err = true;
     }
     if( err == false ) {
         console.log('passed');
     }
     process.exit(0);
 });
Example #16
0
function DecoderBuffer(base, options) {
  Reporter.call(this, options);
  if (!Buffer.isBuffer(base)) {
    this.error('Input not Buffer');
    return;
  }

  this.base = base;
  this.offset = 0;
  this.length = base.length;
}
Example #17
0
File: Pub.js Project: akhavr/jkurwa
Pub.prototype.verify = function (hash_val, sign, fmt) {
    if(fmt === undefined) {
        fmt = detect_sign_format(sign);
    }
    if(Buffer.isBuffer(hash_val)) {
        hash_val = new Field(util.add_zero(hash_val, true), 'buf8', this.curve);
    }

    sign = parse_sign(sign, fmt, this.curve);
    return this.help_verify(hash_val, sign.s, sign.r);
};
Example #18
0
img.Image.prototype.overlay = function(image) {
    // Allow passing in PNG buffers instead of image objects.
    if (Buffer.isBuffer(image)) {
        image = new img.Image().load(image);
    }

    // Begin processing again once the image is loaded.
    if (!image.width) image.once('load', this.process.bind(this));

    return overlay.apply(this, arguments);
};
Example #19
0
Trigger.prototype.withEmoji = function containingEmoji(emoji) {
  if (!Buffer.isBuffer(emoji)) {
    throw Errors.InvalidArgumentError({
      argument: 'emoji',
      given: emoji,
      expected: 'buffer'
    });
  }
  this.conditions.push(Message.withEmoji(emoji));
  return this;
};
Example #20
0
module.exports = (0, _define_crc2.default)('crc-16-modbus', function (buf, previous) {
    if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _buffer.Buffer)(buf);

    var crc = typeof previous !== 'undefined' ? ~~previous : 0xffff;

    for (var index = 0; index < buf.length; index++) {
        var byte = buf[index];
        crc = (TABLE[(crc ^ byte) & 0xff] ^ crc >> 8) & 0xffff;
    }

    return crc;
});
Example #21
0
module.exports = defineCrc('crc-32', function (buf, previous) {
  if (!Buffer.isBuffer(buf)) buf = Buffer(buf);

  let crc = previous === 0 ? 0 : ~~previous ^ -1

  for (let index = 0; index < buf.length; index++) {
    const byte = buf[index];
    crc = TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
  }

  return crc ^ -1;
});
Example #22
0
 response.on('data', function(chunk) {
     if (!callback) return;
     payload += chunk.length;
     var err = checkMaxLength(max_length, payload);
     if (err) {
         response.socket.end();
         error(err);
     } else {
         if (!Buffer.isBuffer(chunk)) chunk = new Buffer(chunk)
         out.push(chunk);
     }
 });
Example #23
0
var crc16kermit = (0, _define_crc2.default)('kermit', function (buf, previous) {
  if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _create_buffer2.default)(buf);

  var crc = typeof previous !== 'undefined' ? ~~previous : 0x0000;

  for (var index = 0; index < buf.length; index++) {
    var byte = buf[index];
    crc = (TABLE[(crc ^ byte) & 0xff] ^ crc >> 8) & 0xffff;
  }

  return crc;
});
Example #24
0
var crc81wire = (0, _define_crc2.default)('dallas-1-wire', function (buf, previous) {
  if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _create_buffer2.default)(buf);

  var crc = ~~previous;

  for (var index = 0; index < buf.length; index++) {
    var byte = buf[index];
    crc = TABLE[(crc ^ byte) & 0xff] & 0xff;
  }

  return crc;
});
Example #25
0
        stream.on('data', function(newFile){
            should.exist(newFile);
            should.exist(newFile.path);
            should.exist(newFile.relative);
            should.exist(newFile.contents);

            var newfileStr = iconv.decode(newFile.contents, 'UTF-8');
            var expectedContentsStr = iconv.decode(expectedContents, 'UTF-8')

            newfileStr.should.equal(expectedContentsStr);
            Buffer.isBuffer(newFile.contents).should.equal(true);
        });
Example #26
0
/**
 * A class representation of the BSON Binary type.
 *
 * Sub types
 *  - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type.
 *  - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type.
 *  - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type.
 *  - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type.
 *  - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type.
 *  - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type.
 *
 * @class
 * @param {Buffer} buffer a buffer object containing the binary data.
 * @param {Number} [subType] the option binary type.
 * @return {Binary}
 */
function Binary(buffer, subType) {
  if (!(this instanceof Binary)) return new Binary(buffer, subType);

  if (
    buffer != null &&
    !(typeof buffer === 'string') &&
    !Buffer.isBuffer(buffer) &&
    !(buffer instanceof Uint8Array) &&
    !Array.isArray(buffer)
  ) {
    throw new Error('only String, Buffer, Uint8Array or Array accepted');
  }

  this._bsontype = 'Binary';

  if (buffer instanceof Number) {
    this.sub_type = buffer;
    this.position = 0;
  } else {
    this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType;
    this.position = 0;
  }

  if (buffer != null && !(buffer instanceof Number)) {
    // Only accept Buffer, Uint8Array or Arrays
    if (typeof buffer === 'string') {
      // Different ways of writing the length of the string for the different types
      if (typeof Buffer !== 'undefined') {
        this.buffer = new Buffer(buffer);
      } else if (
        typeof Uint8Array !== 'undefined' ||
        Object.prototype.toString.call(buffer) === '[object Array]'
      ) {
        this.buffer = writeStringToArray(buffer);
      } else {
        throw new Error('only String, Buffer, Uint8Array or Array accepted');
      }
    } else {
      this.buffer = buffer;
    }
    this.position = buffer.length;
  } else {
    if (typeof Buffer !== 'undefined') {
      this.buffer = new Buffer(Binary.BUFFER_SIZE);
    } else if (typeof Uint8Array !== 'undefined') {
      this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE));
    } else {
      this.buffer = new Array(Binary.BUFFER_SIZE);
    }
    // Set position to start of buffer
    this.position = 0;
  }
}
module.exports = (0, _define_crc2.default)('crc-32', function (buf, previous) {
  if (!_buffer.Buffer.isBuffer(buf)) buf = (0, _create_buffer2.default)(buf);

  var crc = previous === 0 ? 0 : ~~previous ^ -1;

  for (var index = 0; index < buf.length; index++) {
    var byte = buf[index];
    crc = TABLE[(crc ^ byte) & 0xff] ^ crc >>> 8;
  }

  return crc ^ -1;
});
Example #28
0
module.exports = defineCrc('kermit', function (buf, previous) {
  if (!Buffer.isBuffer(buf)) buf = Buffer(buf);

  let crc = typeof(previous) !== 'undefined' ? ~~previous : 0x0000;

  for (let index = 0; index < buf.length; index++) {
    const byte = buf[index];
    crc = ((TABLE[(crc ^ byte) & 0xff] ^ (crc >> 8)) & 0xffff);
  }

  return crc;
});
Example #29
0
module.exports = defineCrc('crc-16', function (buf, previous) {
  if (!Buffer.isBuffer(buf)) buf = Buffer(buf);

  let crc = ~~previous;

  for (let index = 0; index < buf.length; index++) {
    const byte = buf[index];
    crc = ((TABLE[(crc ^ byte) & 0xff] ^ (crc >> 8)) & 0xffff);
  }

  return crc;
});
Example #30
0
LocalWrapper.prototype.touchContent = function(path, content)
{
	if (typeof content==='string') {
		this.touchedContent[path] = new Buffer(content, 'utf-8');

	} else if (Buffer.isBuffer(content)) {
		this.touchedContent[path] = content;

	} else {
		throw new Error('Invalid content for file ' + path + '. Expected string or Buffer.');
	}
};