rearrage_stuff

This commit is contained in:
Kim Ravn Hansen
2025-09-16 11:26:40 +02:00
parent 40e8c5e0ab
commit 3f11ebe6dc
4937 changed files with 1146031 additions and 134 deletions

53
node_modules/varint/README.md generated vendored Normal file
View File

@@ -0,0 +1,53 @@
# varint
encode whole numbers to an array of [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and also decode them.
```javascript
var varint = require('varint')
var bytes = varint.encode(300) // === [0xAC, 0x02]
varint.decode(bytes) // 300
varint.decode.bytes // 2 (the last decode() call required 2 bytes)
```
## api
### varint = require('varint')
### varint.encode(num[, buffer=[], offset=0]) -> buffer
Encodes `num` into `buffer` starting at `offset`. returns `buffer`, with the encoded varint written into it. If `buffer` is not provided, it will default to a new array.
`varint.encode.bytes` will now be set to the number of bytes
modified.
### varint.decode(data[, offset=0]) -> number
decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
Throws a `RangeError` when `data` does not represent a valid encoding.
### varint.decode.bytes
if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
### varint.encode.bytes
similar to `decode.bytes` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array). you can access this via `varint.encode.bytes` which holds the number of bytes written in the last encode.
### varint.encodingLength(num)
returns the number of bytes this number will be encoded as, up to a maximum of 8.
## usage notes
If varint is passed a buffer that does not contain a valid end
byte, then `decode` will throw `RangeError`, and `decode.bytes`
will be set to 0. If you are reading from a streaming source,
it's okay to pass an incomplete buffer into `decode`, detect this
case, and then concatenate the next buffer.
# License
MIT

57
node_modules/varint/bench.js generated vendored Normal file
View File

@@ -0,0 +1,57 @@
var N = 1e7
var M = 10
/*
benchmark encoding and decoding N random integers.
A number is encoded into a buffer, (the buffer is reused so
that allocation does not affect the benchmark)
to test the effect on performance of invalid records
(i.e. too short, with the Most Significant Byte missing)
every M items, attempt to decode from a shorter slice of the buffer.
This will probably be produce an invalid result. We do not
need to write into that buffer - because it refurs to the same memory as
the full size buffer.
run with INVALID=1 to include N/M invalid decodes.
results:
with no invalid decodes, I get about 2428 decodes/ms
with invalid decodes:
old code that overruns buffer: 1122 decodes/ms
check length & return undefined: 2439 decodecs/ms
check length & return NaN: 2434 d/ms
check length & return -1: 2400 d/ms
conclusion, it doesn't make a significant difference whether
what is returned to show an invalid read,
but if you overrun the buffer the cost is considerable.
recomendation: return undefined
*/
var buffer = new Buffer(8)
var _buffer = buffer.slice(0, 4)
var varint = require('./')
var l = N
var invalid = 0
includeInvalid = !!process.env.INVALID
var start = Date.now()
while (l--) {
var int = Math.floor(Math.random()*0x01fffffffffffff)
varint.encode(int, buffer, 0)
//console.log(int, varint.decode(buffer, 0))
//every 1000 varints, do one that will be too short,
//measure
if(includeInvalid && !(l%M)) {
if(undefined == varint.decode(_buffer, 0))
invalid ++
} else
if(int !== varint.decode(buffer, 0))
throw new Error('decode was incorrect')
}
console.log('decode&encode/ms, invalidDecodes')
console.log(N/(Date.now() - start) + ',', invalid)

29
node_modules/varint/decode.js generated vendored Normal file
View File

@@ -0,0 +1,29 @@
module.exports = read
var MSB = 0x80
, REST = 0x7F
function read(buf, offset) {
var res = 0
, offset = offset || 0
, shift = 0
, counter = offset
, b
, l = buf.length
do {
if (counter >= l || shift > 49) {
read.bytes = 0
throw new RangeError('Could not decode varint')
}
b = buf[counter++]
res += shift < 28
? (b & REST) << shift
: (b & REST) * Math.pow(2, shift)
shift += 7
} while (b >= MSB)
read.bytes = counter - offset
return res
}

30
node_modules/varint/encode.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
module.exports = encode
var MSB = 0x80
, REST = 0x7F
, MSBALL = ~REST
, INT = Math.pow(2, 31)
function encode(num, out, offset) {
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
encode.bytes = 0
throw new RangeError('Could not encode varint')
}
out = out || []
offset = offset || 0
var oldOffset = offset
while(num >= INT) {
out[offset++] = (num & 0xFF) | MSB
num /= 128
}
while(num & MSBALL) {
out[offset++] = (num & 0xFF) | MSB
num >>>= 7
}
out[offset] = num | 0
encode.bytes = offset - oldOffset + 1
return out
}

5
node_modules/varint/index.js generated vendored Normal file
View File

@@ -0,0 +1,5 @@
module.exports = {
encode: require('./encode.js')
, decode: require('./decode.js')
, encodingLength: require('./length.js')
}

25
node_modules/varint/length.js generated vendored Normal file
View File

@@ -0,0 +1,25 @@
var N1 = Math.pow(2, 7)
var N2 = Math.pow(2, 14)
var N3 = Math.pow(2, 21)
var N4 = Math.pow(2, 28)
var N5 = Math.pow(2, 35)
var N6 = Math.pow(2, 42)
var N7 = Math.pow(2, 49)
var N8 = Math.pow(2, 56)
var N9 = Math.pow(2, 63)
module.exports = function (value) {
return (
value < N1 ? 1
: value < N2 ? 2
: value < N3 ? 3
: value < N4 ? 4
: value < N5 ? 5
: value < N6 ? 6
: value < N7 ? 7
: value < N8 ? 8
: value < N9 ? 9
: 10
)
}

24
node_modules/varint/package.json generated vendored Normal file
View File

@@ -0,0 +1,24 @@
{
"name": "varint",
"version": "6.0.0",
"description": "protobuf-style varint bytes - use msb to create integer values of varying sizes",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/chrisdickinson/varint.git"
},
"keywords": [
"varint",
"protobuf",
"encode",
"decode"
],
"author": "Chris Dickinson <chris@neversaw.us>",
"license": "MIT",
"devDependencies": {
"tape": "~2.12.3"
}
}

156
node_modules/varint/test.js generated vendored Normal file
View File

@@ -0,0 +1,156 @@
var varint = require('./index')
, test = require('tape')
, decode = varint.decode
, encode = varint.encode
, encodingLength = varint.encodingLength
test('fuzz test', function(assert) {
var expect
, encoded
for(var i = 0, len = 100; i < len; ++i) {
expect = randint(0x7FFFFFFF)
encoded = encode(expect)
var data = decode(encoded)
assert.equal(expect, data, 'fuzz test: ' + expect.toString())
assert.equal(decode.bytes, encoded.length)
}
assert.end()
})
test('test single byte works as expected', function(assert) {
var buf = new Uint8Array(2)
buf[0] = 172
buf[1] = 2
var data = decode(buf)
assert.equal(data, 300, 'should equal 300')
assert.equal(decode.bytes, 2)
assert.end()
})
test('test encode works as expected', function(assert) {
var out = []
assert.deepEqual(encode(300), [0xAC, 0x02])
assert.end()
})
test('test decode single bytes', function(assert) {
var expected = randint(parseInt('1111111', '2'))
var buf = new Uint8Array(1)
buf[0] = expected
var data = decode(buf)
assert.equal(data, expected)
assert.equal(decode.bytes, 1)
assert.end()
})
test('test decode multiple bytes with zero', function(assert) {
var expected = randint(parseInt('1111111', '2'))
var buf = new Uint8Array(2)
buf[0] = 128
buf[1] = expected
var data = decode(buf)
assert.equal(data, expected << 7)
assert.equal(decode.bytes, 2)
assert.end()
})
test('encode single byte', function(assert) {
var expected = randint(parseInt('1111111', '2'))
assert.deepEqual(encode(expected), [expected])
assert.equal(encode.bytes, 1)
assert.end()
})
test('encode multiple byte with zero first byte', function(assert) {
var expected = 0x0F00
assert.deepEqual(encode(expected), [0x80, 0x1E])
assert.equal(encode.bytes, 2)
assert.end()
})
test('big integers', function (assert) {
var bigs = []
for(var i = 32; i <= 53; i++) (function (i) {
bigs.push(Math.pow(2, i) - 1)
})(i)
bigs.forEach(function (n) {
var data = encode(n)
console.error(n, '->', data)
assert.equal(decode(data), n)
assert.notEqual(decode(data), n - 1)
})
assert.end()
})
test('fuzz test - big', function(assert) {
var expect
, encoded
var MAX_INTD = Number.MAX_SAFE_INTEGER
var MAX_INT = Math.pow(2, 31)
for(var i = 0, len = 100; i < len; ++i) {
expect = randint(MAX_INTD - MAX_INT) + MAX_INT
encoded = encode(expect)
var data = decode(encoded)
assert.equal(expect, data, 'fuzz test: ' + expect.toString())
assert.equal(decode.bytes, encoded.length)
}
assert.end()
})
test('encodingLength', function (assert) {
for(var i = 0; i <= 53; i++) {
var n = Math.pow(2, i) - 1
assert.equal(encode(n).length, encodingLength(n))
}
assert.end()
})
test('buffer too short', function (assert) {
var value = encode(9812938912312)
var buffer = encode(value)
var l = buffer.length
while(l--) {
try {
var val = decode(buffer.slice(0, l))
} catch (err) {
assert.equal(err.constructor, RangeError)
assert.equal(decode.bytes, 0)
}
}
assert.end()
})
test('buffer too long', function (assert) {
var buffer = Uint8Array.from(
Array.from({length: 150}, function () { return 0xff })
.concat(Array.from({length: 1}, function () { return 0x1 }))
)
try {
var val = decode(buffer)
encode(val)
assert.fail('expected an error received value instead: ' + val)
} catch (err) {
assert.equal(err.constructor, RangeError)
assert.equal(decode.bytes, 0)
}
assert.end()
})
function randint(range) {
return Math.floor(Math.random() * range)
}