byte-data-stream
Advanced tools
Comparing version 0.0.3-rokka0 to 0.0.4-rokka0
{ | ||
"name": "byte-data-stream", | ||
"version": "0.0.3-rokka0", | ||
"version": "0.0.4-rokka0", | ||
"description": "Readable & writable byte data stream", | ||
@@ -24,3 +24,7 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/kmoon2437/byte-data-stream#readme" | ||
"homepage": "https://github.com/kmoon2437/byte-data-stream#readme", | ||
"dependencies": { | ||
"signed-varint": "^2.0.1", | ||
"varint": "^6.0.0" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # byte-data-stream | ||
stream.read_uint16_le(); // read unsigned little-endian 16-bit integer | ||
stream.read_var_uint(); // read unsigned varint | ||
stream.read_var_uint_le(); // read unsigned little-endian varint | ||
// ... | ||
@@ -24,3 +24,3 @@ | ||
stream.write_uint16_le(49861); // write unsigned little-endian 16-bit integer | ||
stream.write_var_uint(92736296525); // write unsigned varint | ||
stream.write_var_uint_le(92736296525); // write unsigned little-endian varint | ||
// ... | ||
@@ -31,2 +31,5 @@ ``` | ||
```js | ||
// properties | ||
stream.i; // position | ||
// methods for reading data | ||
@@ -51,3 +54,6 @@ stream.read_int8(); | ||
stream.read_float64_le(); | ||
stream.read_var_uint(max_byte_length = Infinity); | ||
stream.read_var_int_be(max_byte_length = Infinity); | ||
stream.read_var_int_le(max_byte_length = Infinity); | ||
stream.read_var_uint_be(max_byte_length = Infinity); | ||
stream.read_var_uint_le(max_byte_length = Infinity); | ||
@@ -73,3 +79,6 @@ // methods for writing data | ||
stream.write_float64_le(val); | ||
stream.write_var_uint(val); | ||
stream.write_var_int_be(val); | ||
stream.write_var_int_le(val); | ||
stream.write_var_uint_be(val); | ||
stream.write_var_uint_le(val); | ||
``` |
@@ -0,1 +1,4 @@ | ||
const var_int = require('signed-varint'); | ||
const var_uint = require('varint'); | ||
/** | ||
@@ -119,11 +122,10 @@ * ㅆ발 이거 ㅈㄴ 노가다임 | ||
read_var_uint(max_byte_length = Infinity){ | ||
let num = 0; | ||
let i = 0,v = 0; | ||
read_var_int_bytes(max_byte_length = Infinity){ | ||
let bytes = []; | ||
let i = 0; | ||
while(max_byte_length > i++){ | ||
v = this.read_uint8(); | ||
if(v & 128){ | ||
num += v & 127; | ||
num <<= 7; | ||
}else return num + v; | ||
bytes.push(this.read_uint8()); | ||
if(!(bytes[bytes.length-1] & 128)){ | ||
return bytes; | ||
} | ||
} | ||
@@ -133,5 +135,30 @@ throw new RangeError(`0x${this.i.toString(16)}: Variable integer length cannot exceed ${max_byte_length} bytes`); | ||
read_var_int_be(max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length).reverse(); | ||
bytes[0] += 128; | ||
bytes[bytes.length-1] -= 128; | ||
return var_int.decode(bytes); | ||
} | ||
read_var_int_le(max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length); | ||
return var_int.decode(bytes); | ||
} | ||
read_var_uint_be(max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length).reverse(); | ||
bytes[0] += 128; | ||
bytes[bytes.length-1] -= 128; | ||
return var_uint.decode(bytes); | ||
} | ||
read_var_uint_le(max_byte_length = Infinity){ | ||
let bytes = this.read_var_int_bytes(max_byte_length); | ||
return var_uint.decode(bytes); | ||
} | ||
// ArrayBuffer를 확장한다. | ||
expand_buffer(len){ | ||
if(this.buf.byteLength >= this.i+len) return; | ||
len = (this.i+len)-this.buf.byteLength; | ||
let buf = [...new Uint8Array(this.buf)]; | ||
@@ -252,25 +279,31 @@ this.buf = new ArrayBuffer(buf.length+len); | ||
write_var_uint(val){ | ||
if(Number.MAX_SAFE_INTEGER && val > Number.MAX_SAFE_INTEGER){ | ||
throw new RangeError('Could not encode varint'); | ||
} | ||
let a = []; | ||
let i = 0; | ||
while(val > 2147483647){ | ||
a[i++] = (val & 255) | 128; | ||
val /= 128; | ||
} | ||
while(val & -129){ | ||
a[i++] = (val & 255) | 128; | ||
val >>>= 7; | ||
} | ||
a[i] = val || 0; | ||
write_var_int_be(val){ | ||
let a = var_int.encode(val).reverse(); | ||
a[0] += 128; | ||
a[a.length-1] -= 128; | ||
this.expand_buffer(a.length); | ||
a.forEach(n => this.write_uint8(n)); | ||
this.i += a.length; | ||
} | ||
write_var_int_le(val){ | ||
let a = var_int.encode(val); | ||
this.expand_buffer(a.length); | ||
a.forEach(n => this.write_uint8(n)); | ||
} | ||
write_var_uint_be(val){ | ||
let a = var_uint.encode(val).reverse(); | ||
a[0] += 128; | ||
a[a.length-1] -= 128; | ||
this.expand_buffer(a.length); | ||
a.forEach(n => this.write_uint8(n)); | ||
} | ||
write_var_uint_le(val){ | ||
let a = var_uint.encode(val); | ||
this.expand_buffer(a.length); | ||
console.log(a); | ||
a.forEach(n => this.write_uint8(n)); | ||
} | ||
ensure_array_buffer(buf){ | ||
@@ -277,0 +310,0 @@ if(buf){ |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
12381
286
79
2
1
+ Addedsigned-varint@^2.0.1
+ Addedvarint@^6.0.0
+ Addedsigned-varint@2.0.1(transitive)
+ Addedvarint@5.0.26.0.0(transitive)