Skip to content

Commit edbc56c

Browse files
committed
fix: vendor varint for pure ESM
1 parent fd0ce17 commit edbc56c

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"build": "npm_config_yes=true npx ipjs@latest build --tests",
9+
"build:vendor": "npx brrp -x varint > vendor/varint.js",
910
"publish": "npm_config_yes=true npx ipjs@latest publish",
1011
"lint": "standard",
1112
"test:cjs": "npm run build && mocha dist/cjs/node-test/test-*.js && npm run test:cjs:browser",
@@ -71,8 +72,7 @@
7172
"dependencies": {
7273
"base-x": "^3.0.8",
7374
"buffer": "^5.6.0",
74-
"cids": "^1.0.0",
75-
"varint": "^5.0.0"
75+
"cids": "^1.0.0"
7676
},
7777
"directories": {
7878
"test": "test"

src/varint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import varint from 'varint'
1+
import varint from '../vendor/varint.js'
22

33
/**
44
* @param {Uint8Array} data

vendor/varint.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var encode_1 = encode;
2+
3+
var MSB = 0x80
4+
, REST = 0x7F
5+
, MSBALL = ~REST
6+
, INT = Math.pow(2, 31);
7+
8+
function encode(num, out, offset) {
9+
out = out || [];
10+
offset = offset || 0;
11+
var oldOffset = offset;
12+
13+
while(num >= INT) {
14+
out[offset++] = (num & 0xFF) | MSB;
15+
num /= 128;
16+
}
17+
while(num & MSBALL) {
18+
out[offset++] = (num & 0xFF) | MSB;
19+
num >>>= 7;
20+
}
21+
out[offset] = num | 0;
22+
23+
encode.bytes = offset - oldOffset + 1;
24+
25+
return out
26+
}
27+
28+
var decode = read;
29+
30+
var MSB$1 = 0x80
31+
, REST$1 = 0x7F;
32+
33+
function read(buf, offset) {
34+
var res = 0
35+
, offset = offset || 0
36+
, shift = 0
37+
, counter = offset
38+
, b
39+
, l = buf.length;
40+
41+
do {
42+
if (counter >= l) {
43+
read.bytes = 0;
44+
throw new RangeError('Could not decode varint')
45+
}
46+
b = buf[counter++];
47+
res += shift < 28
48+
? (b & REST$1) << shift
49+
: (b & REST$1) * Math.pow(2, shift);
50+
shift += 7;
51+
} while (b >= MSB$1)
52+
53+
read.bytes = counter - offset;
54+
55+
return res
56+
}
57+
58+
var N1 = Math.pow(2, 7);
59+
var N2 = Math.pow(2, 14);
60+
var N3 = Math.pow(2, 21);
61+
var N4 = Math.pow(2, 28);
62+
var N5 = Math.pow(2, 35);
63+
var N6 = Math.pow(2, 42);
64+
var N7 = Math.pow(2, 49);
65+
var N8 = Math.pow(2, 56);
66+
var N9 = Math.pow(2, 63);
67+
68+
var length = function (value) {
69+
return (
70+
value < N1 ? 1
71+
: value < N2 ? 2
72+
: value < N3 ? 3
73+
: value < N4 ? 4
74+
: value < N5 ? 5
75+
: value < N6 ? 6
76+
: value < N7 ? 7
77+
: value < N8 ? 8
78+
: value < N9 ? 9
79+
: 10
80+
)
81+
};
82+
83+
var varint = {
84+
encode: encode_1
85+
, decode: decode
86+
, encodingLength: length
87+
};
88+
89+
var _brrp_varint = varint;
90+
91+
export default _brrp_varint;

0 commit comments

Comments
 (0)