byte-data-stream
Advanced tools
Comparing version 0.1.2-rokka0 to 1.0.0-rokka0
{ | ||
"name": "byte-data-stream", | ||
"version": "0.1.2-rokka0", | ||
"version": "1.0.0-rokka0", | ||
"description": "Readable & writable byte data stream", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,21 +12,21 @@ # byte-data-stream | ||
stream.read_int8(); // read signed byte | ||
stream.read_uint8(); // read unsigned byte | ||
stream.read_bytes(8); // read many bytes | ||
stream.read_int16(); // read signed big-endian 16-bit integer | ||
stream.read_uint16(true); // read unsigned little-endian 16-bit integer | ||
stream.read_var_uint(); // read unsigned big-endian varint | ||
stream.readInt8(); // read signed byte | ||
stream.readUint8(); // read unsigned byte | ||
stream.readBytes(8); // read many bytes | ||
stream.readInt16(); // read signed big-endian 16-bit integer | ||
stream.readUint16(true); // read unsigned little-endian 16-bit integer | ||
stream.readVarUint(); // read unsigned big-endian varint | ||
// ... | ||
let stream2 = new ByteStream(); | ||
stream2.write_int8(96); // write signed byte | ||
stream2.write_uint8(192); // write unsigned byte | ||
stream2.write_bytes([19,72,11,21,19,72,11,21]); // write many bytes | ||
stream2.write_int16(1972); // write signed big-endian 16-bit integer | ||
stream2.write_uint16(49861,true); // write unsigned little-endian 16-bit integer | ||
stream2.write_var_uint(92736296525); // write unsigned big-endian varint | ||
stream2.writeInt8(96); // write signed byte | ||
stream2.writeUint8(192); // write unsigned byte | ||
stream2.writeBytes([19,72,11,21,19,72,11,21]); // write many bytes | ||
stream2.writeInt16(1972); // write signed big-endian 16-bit integer | ||
stream2.writeUint16(49861,true); // write unsigned little-endian 16-bit integer | ||
stream2.writeVarUint(92736296525); // write unsigned big-endian varint | ||
// ... | ||
// this is the class for test | ||
// the usage is the same, | ||
// the usage is same, | ||
// but it only increases the length | ||
@@ -46,30 +46,30 @@ // without creating an ArrayBuffer to be written | ||
// methods for reading data | ||
stream.read_int8(); | ||
stream.read_uint8(); | ||
stream.read_bytes(length); | ||
stream.read_int16(little_endian); | ||
stream.read_uint16(little_endian); | ||
stream.read_int32(little_endian); | ||
stream.read_uint32(little_endian); | ||
stream.read_big_int64(little_endian); | ||
stream.read_big_uint64(little_endian); | ||
stream.read_float32(little_endian); | ||
stream.read_float64(little_endian); | ||
stream.read_var_int(little_endian,max_byte_length = Infinity); | ||
stream.read_var_uint(little_endian,max_byte_length = Infinity); | ||
stream.readInt8(); | ||
stream.readUint8(); | ||
stream.readBytes(length); | ||
stream.readInt16(littleEndian); | ||
stream.readUint16(littleEndian); | ||
stream.readInt32(littleEndian); | ||
stream.readUint32(littleEndian); | ||
stream.readBigInt64(littleEndian); | ||
stream.readBigUint64(littleEndian); | ||
stream.readFloat32(littleEndian); | ||
stream.readFloat64(littleEndian); | ||
stream.readVarInt(littleEndian,maxByteLength = Infinity); | ||
stream.readVarUint(littleEndian,maxByteLength = Infinity); | ||
// methods for writing data | ||
stream.write_int8(val); | ||
stream.write_uint8(val); | ||
stream.write_bytes(bytes); | ||
stream.write_int16(val,little_endian); | ||
stream.write_uint16(val,little_endian); | ||
stream.write_int32(val,little_endian); | ||
stream.write_uint32(val,little_endian); | ||
stream.write_big_int64(val,little_endian); | ||
stream.write_big_uint64(val,little_endian); | ||
stream.write_float32(val,little_endian); | ||
stream.write_float64(val,little_endian); | ||
stream.write_var_int(val,little_endian); | ||
stream.write_var_uint(val,little_endian); | ||
stream.writeInt8(val); | ||
stream.writeUint8(val); | ||
stream.writeBytes(bytes); | ||
stream.writeInt16(val,littleEndian); | ||
stream.writeUint16(val,littleEndian); | ||
stream.writeInt32(val,littleEndian); | ||
stream.writeUint32(val,littleEndian); | ||
stream.writeBigInt64(val,littleEndian); | ||
stream.writeBigUint64(val,littleEndian); | ||
stream.writeFloat32(val,littleEndian); | ||
stream.writeFloat64(val,littleEndian); | ||
stream.writeVarInt(val,littleEndian); | ||
stream.writeVarUint(val,littleEndian); | ||
``` |
@@ -1,3 +0,3 @@ | ||
const var_int = require('signed-varint'); | ||
const var_uint = require('varint'); | ||
const varint = require('signed-varint'); | ||
const varuint = require('varint'); | ||
@@ -9,3 +9,3 @@ /** | ||
constructor(buf){ | ||
this.buf = buf ? this.ensure_array_buffer(buf) : new ArrayBuffer(); | ||
this.buf = buf ? this.ensureArrayBuffer(buf) : new ArrayBuffer(); | ||
this.i = 0; | ||
@@ -20,14 +20,14 @@ this.view = new DataView(this.buf); | ||
read_int8(){ | ||
readInt8(){ | ||
return this.view.getInt8(this.i++); | ||
} | ||
read_uint8(){ | ||
readUint8(){ | ||
return this.view.getUint8(this.i++); | ||
} | ||
read_bytes(length){ | ||
readBytes(length){ | ||
/*let arr = new Uint8Array(length); | ||
for(let i = 0;i < length;i++){ | ||
arr[i] = this.read_uint8(); | ||
arr[i] = this.readUint8(); | ||
}*/ | ||
@@ -40,4 +40,4 @@ if(this.i+length > this.buf.byteLength){ | ||
read_int16(little_endian){ | ||
let n = this.view.getInt16(this.i,little_endian); | ||
readInt16(littleEndian){ | ||
let n = this.view.getInt16(this.i,littleEndian); | ||
this.i += 2; | ||
@@ -47,4 +47,4 @@ return n; | ||
read_uint16(little_endian){ | ||
let n = this.view.getUint16(this.i,little_endian); | ||
readUint16(littleEndian){ | ||
let n = this.view.getUint16(this.i,littleEndian); | ||
this.i += 2; | ||
@@ -54,4 +54,4 @@ return n; | ||
read_int32(little_endian){ | ||
let n = this.view.getInt32(this.i,little_endian); | ||
readInt32(littleEndian){ | ||
let n = this.view.getInt32(this.i,littleEndian); | ||
this.i += 4; | ||
@@ -61,4 +61,4 @@ return n; | ||
read_uint32(little_endian){ | ||
let n = this.view.getUint32(this.i,little_endian); | ||
readUint32(littleEndian){ | ||
let n = this.view.getUint32(this.i,littleEndian); | ||
this.i += 4; | ||
@@ -68,4 +68,4 @@ return n; | ||
read_big_int64(little_endian){ | ||
let n = this.view.getBigInt64(this.i,little_endian); | ||
readBigInt64(littleEndian){ | ||
let n = this.view.getBigInt64(this.i,littleEndian); | ||
this.i += 8; | ||
@@ -75,4 +75,4 @@ return n; | ||
read_big_uint64(little_endian){ | ||
let n = this.view.getBigUint64(this.i,little_endian); | ||
readBigUint64(littleEndian){ | ||
let n = this.view.getBigUint64(this.i,littleEndian); | ||
this.i += 8; | ||
@@ -82,4 +82,4 @@ return n; | ||
read_float32(little_endian){ | ||
let n = this.view.getFloat32(this.i,little_endian); | ||
readFloat32(littleEndian){ | ||
let n = this.view.getFloat32(this.i,littleEndian); | ||
this.i += 4; | ||
@@ -89,4 +89,4 @@ return n; | ||
read_float64(little_endian){ | ||
let n = this.view.getFloat64(this.i,little_endian); | ||
readFloat64(littleEndian){ | ||
let n = this.view.getFloat64(this.i,littleEndian); | ||
this.i += 8; | ||
@@ -96,7 +96,7 @@ return n; | ||
read_var_int_bytes(max_byte_length = Infinity){ | ||
readVarIntBytes(maxByteLength = Infinity){ | ||
let bytes = []; | ||
let i = 0; | ||
while(max_byte_length > i++){ | ||
bytes.push(this.read_uint8()); | ||
while(maxByteLength > i++){ | ||
bytes.push(this.readUint8()); | ||
if(!(bytes[bytes.length-1] & 128)){ | ||
@@ -106,8 +106,8 @@ return bytes; | ||
} | ||
throw new RangeError(`0x${this.i.toString(16)}: Variable integer length cannot exceed ${max_byte_length} bytes`); | ||
throw new RangeError(`0x${this.i.toString(16)}: Variable integer length cannot exceed ${maxByteLength} bytes`); | ||
} | ||
read_var_int(little_endian,max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length); | ||
if(!little_endian){ | ||
readVarInt(littleEndian,maxByteLength = Infinity){ | ||
let bytes = this.readVarIntBytes(maxByteLength); | ||
if(!littleEndian){ | ||
bytes = bytes.reverse(); | ||
@@ -117,8 +117,8 @@ bytes[0] += 128; | ||
} | ||
return var_int.decode(bytes); | ||
return varint.decode(bytes); | ||
} | ||
read_var_uint(little_endian,max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length); | ||
if(!little_endian){ | ||
readVarUint(littleEndian,maxByteLength = Infinity){ | ||
let bytes = this.readVarIntBytes(maxByteLength); | ||
if(!littleEndian){ | ||
bytes = bytes.reverse(); | ||
@@ -128,7 +128,7 @@ bytes[0] += 128; | ||
} | ||
return var_uint.decode(bytes); | ||
return varuint.decode(bytes); | ||
} | ||
// ArrayBuffer를 확장한다. | ||
expand_buffer(len){ | ||
expandBuffer(len){ | ||
if(this.buf.byteLength >= this.i+len) return; | ||
@@ -144,14 +144,14 @@ len = (this.i+len)-this.buf.byteLength; | ||
write_int8(val){ | ||
this.expand_buffer(1); | ||
writeInt8(val){ | ||
this.expandBuffer(1); | ||
this.view.setInt8(this.i++,val); | ||
} | ||
write_uint8(val){ | ||
this.expand_buffer(1); | ||
writeUint8(val){ | ||
this.expandBuffer(1); | ||
this.view.setUint8(this.i++,val); | ||
} | ||
write_bytes(bytes){ | ||
this.expand_buffer(bytes.length); | ||
writeBytes(bytes){ | ||
this.expandBuffer(bytes.length); | ||
this.u8.set(bytes,this.i); | ||
@@ -161,53 +161,53 @@ this.i += bytes.length; | ||
write_int16(val,little_endian){ | ||
this.expand_buffer(2); | ||
let n = this.view.setInt16(this.i,val,little_endian); | ||
writeInt16(val,littleEndian){ | ||
this.expandBuffer(2); | ||
let n = this.view.setInt16(this.i,val,littleEndian); | ||
this.i += 2; | ||
} | ||
write_uint16(val,little_endian){ | ||
this.expand_buffer(2); | ||
let n = this.view.setUint16(this.i,val,little_endian); | ||
writeUint16(val,littleEndian){ | ||
this.expandBuffer(2); | ||
let n = this.view.setUint16(this.i,val,littleEndian); | ||
this.i += 2; | ||
} | ||
write_int32(val,little_endian){ | ||
this.expand_buffer(4); | ||
let n = this.view.setInt32(this.i,val,little_endian); | ||
writeInt32(val,littleEndian){ | ||
this.expandBuffer(4); | ||
let n = this.view.setInt32(this.i,val,littleEndian); | ||
this.i += 4; | ||
} | ||
write_uint32(val,little_endian){ | ||
this.expand_buffer(4); | ||
let n = this.view.setUint32(this.i,val,little_endian); | ||
writeUint32(val,littleEndian){ | ||
this.expandBuffer(4); | ||
let n = this.view.setUint32(this.i,val,littleEndian); | ||
this.i += 4; | ||
} | ||
write_big_int64(val,little_endian){ | ||
this.expand_buffer(8); | ||
let n = this.view.setBigInt64(this.i,val,little_endian); | ||
writeBigInt64(val,littleEndian){ | ||
this.expandBuffer(8); | ||
let n = this.view.setBigInt64(this.i,val,littleEndian); | ||
this.i += 8; | ||
} | ||
write_big_uint64(val,little_endian){ | ||
this.expand_buffer(8); | ||
let n = this.view.setBigUint64(this.i,val,little_endian); | ||
writeBigUint64(val,littleEndian){ | ||
this.expandBuffer(8); | ||
let n = this.view.setBigUint64(this.i,val,littleEndian); | ||
this.i += 8; | ||
} | ||
write_float32(val,little_endian){ | ||
this.expand_buffer(4); | ||
let n = this.view.setFloat32(this.i,val,little_endian); | ||
writeFloat32(val,littleEndian){ | ||
this.expandBuffer(4); | ||
let n = this.view.setFloat32(this.i,val,littleEndian); | ||
this.i += 4; | ||
} | ||
write_float64(val,little_endian){ | ||
this.expand_buffer(8); | ||
let n = this.view.setFloat64(this.i,val,little_endian); | ||
writeFloat64(val,littleEndian){ | ||
this.expandBuffer(8); | ||
let n = this.view.setFloat64(this.i,val,littleEndian); | ||
this.i += 8; | ||
} | ||
write_var_int(val,little_endian){ | ||
let a = var_int.encode(val); | ||
if(!little_endian){ | ||
writeVarInt(val,littleEndian){ | ||
let a = varint.encode(val); | ||
if(!littleEndian){ | ||
a = a.reverse(); | ||
@@ -217,8 +217,8 @@ a[0] += 128; | ||
} | ||
a.forEach(n => this.write_uint8(n)); | ||
a.forEach(n => this.writeUint8(n)); | ||
} | ||
write_var_uint(val,little_endian){ | ||
let a = var_uint.encode(val); | ||
if(!little_endian){ | ||
writeVarUint(val,littleEndian){ | ||
let a = varuint.encode(val); | ||
if(!littleEndian){ | ||
a = a.reverse(); | ||
@@ -228,6 +228,6 @@ a[0] += 128; | ||
} | ||
a.forEach(n => this.write_uint8(n)); | ||
a.forEach(n => this.writeUint8(n)); | ||
} | ||
ensure_array_buffer(buf){ | ||
ensureArrayBuffer(buf){ | ||
if(buf){ | ||
@@ -234,0 +234,0 @@ if(buf instanceof ArrayBuffer){ |
@@ -1,3 +0,3 @@ | ||
const var_int = require('signed-varint'); | ||
const var_uint = require('varint'); | ||
const varint = require('signed-varint'); | ||
const varuint = require('varint'); | ||
@@ -11,4 +11,7 @@ class WriteOnlyException extends Error{ | ||
module.exports = class ByteStreamSimulator{ | ||
constructor(){ | ||
/** | ||
* ㅆ발 이거 ㅈㄴ 노가다임 | ||
*/ | ||
module.exports = class ByteStream{ | ||
constructor(buf){ | ||
this.i = 0; | ||
@@ -18,55 +21,59 @@ this.length = 0; | ||
read_int8(){ | ||
get buffer(){ | ||
return this.buf; | ||
} | ||
readInt8(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_uint8(){ | ||
readUint8(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_bytes(){ | ||
readBytes(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_int16(){ | ||
readInt16(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_uint16(){ | ||
readUint16(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_int32(){ | ||
readInt32(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_uint32(){ | ||
readUint32(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_big_int64(){ | ||
readBigInt64(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_big_uint64(){ | ||
readBigUint64(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_float32(){ | ||
readFloat32(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_float64(){ | ||
readFloat64(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_var_int_bytes(){ | ||
readVarIntBytes(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_var_int(){ | ||
readVarInt(){ | ||
throw new WriteOnlyException(null); | ||
} | ||
read_var_uint(){ | ||
readVarUint(){ | ||
throw new WriteOnlyException(null); | ||
@@ -76,3 +83,3 @@ } | ||
// ArrayBuffer를 확장한다. | ||
expand_buffer(len){ | ||
expandBuffer(len){ | ||
if(this.length >= this.i+len) return; | ||
@@ -82,81 +89,68 @@ this.length += (this.i+len)-this.length; | ||
write_int8(){ | ||
this.expand_buffer(1); | ||
writeInt8(){ | ||
this.expandBuffer(1); | ||
this.i++; | ||
} | ||
write_uint8(){ | ||
this.expand_buffer(1); | ||
writeUint8(){ | ||
this.expandBuffer(1); | ||
this.i++; | ||
} | ||
write_bytes(bytes){ | ||
writeBytes(bytes){ | ||
let b = [...bytes]; | ||
for(let i = 0;i < b.length;i++){ | ||
this.write_uint8(); | ||
this.writeUint8(); | ||
} | ||
} | ||
write_int16(){ | ||
this.expand_buffer(2); | ||
writeInt16(){ | ||
this.expandBuffer(2); | ||
this.i += 2; | ||
} | ||
write_uint16(){ | ||
this.expand_buffer(2); | ||
writeUint16(){ | ||
this.expandBuffer(2); | ||
this.i += 2; | ||
} | ||
write_int32(){ | ||
this.expand_buffer(4); | ||
writeInt32(){ | ||
this.expandBuffer(4); | ||
this.i += 4; | ||
} | ||
write_uint32(){ | ||
this.expand_buffer(4); | ||
writeUint32(){ | ||
this.expandBuffer(4); | ||
this.i += 4; | ||
} | ||
write_big_int64(){ | ||
this.expand_buffer(8); | ||
writeBigInt64(){ | ||
this.expandBuffer(8); | ||
this.i += 8; | ||
} | ||
write_big_uint64(){ | ||
this.expand_buffer(8); | ||
writeBigUint64(){ | ||
this.expandBuffer(8); | ||
this.i += 8; | ||
} | ||
write_float32(){ | ||
this.expand_buffer(4); | ||
writeFloat32(){ | ||
this.expandBuffer(4); | ||
this.i += 4; | ||
} | ||
write_float64(){ | ||
this.expand_buffer(8); | ||
writeFloat64(){ | ||
this.expandBuffer(8); | ||
this.i += 8; | ||
} | ||
write_var_int(val){ | ||
let a = var_int.encode(val); | ||
a.forEach(() => this.write_uint8()); | ||
writeVarInt(val){ | ||
let a = varint.encode(val); | ||
a.forEach(() => this.writeUint8()); | ||
} | ||
write_var_uint(val){ | ||
let a = var_uint.encode(val) | ||
a.forEach(() => this.write_uint8()); | ||
writeVarUint(val){ | ||
let a = varuint.encode(val); | ||
a.forEach(() => this.writeUint8()); | ||
} | ||
ensure_array_buffer(buf){ | ||
if(buf){ | ||
if(buf instanceof ArrayBuffer){ | ||
return buf; | ||
} | ||
// nodejs Buffer는 Uint8Array의 하위 클래스다 | ||
if(buf instanceof Uint8Array){ | ||
return new Uint8Array(buf).buffer; | ||
} | ||
} | ||
throw new Error('Unsupported buffer type, need ArrayBuffer or Uint8Array or nodejs Buffer'); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12970
317