Skip to content

Commit 264ff68

Browse files
committed
fix: Gatsby support
1 parent 3ab5d2c commit 264ff68

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 1.0.4
4+
5+
### Fixed
6+
7+
Gatsby support
8+
39
## 1.0.3
410

511
### Fixed

package-lock.json

+1-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"dependencies": {
3939
"@solana/web3.js": "^1.10.1",
4040
"assert": "^2.0.0",
41-
"buffer": "^6.0.1",
42-
"read-bigint": "^0.1.6"
41+
"buffer": "^6.0.1"
4342
}
4443
}

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Buffer } from 'buffer'
2-
import { readBigInt64LE, readBigUInt64LE } from 'read-bigint'
31
import { PublicKey } from '@solana/web3.js'
2+
import { Buffer } from 'buffer'
3+
import { readBigInt64LE, readBigUInt64LE } from './readBig'
44

55
export const Magic = 0xa1b2c3d4
66
export const Version1 = 1

src/readBig.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Buffer } from 'buffer'
2+
3+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L758
4+
const ERR_BUFFER_OUT_OF_BOUNDS = () => new Error('Attempt to access memory outside buffer bounds')
5+
6+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L968
7+
const ERR_INVALID_ARG_TYPE = (name: string, expected: string, actual: any) =>
8+
new Error(`The "${name}" argument must be of type ${expected}. Received ${actual}`)
9+
10+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L1262
11+
const ERR_OUT_OF_RANGE = (str: string, range: string, received: number) =>
12+
new Error(`The value of "${str} is out of range. It must be ${range}. Received ${received}`)
13+
14+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/validators.js#L127-L130
15+
function validateNumber(value: any, name: string) {
16+
if (typeof value !== 'number') throw ERR_INVALID_ARG_TYPE(name, 'number', value)
17+
}
18+
19+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L68-L80
20+
function boundsError(value: number, length: number) {
21+
if (Math.floor(value) !== value) {
22+
validateNumber(value, 'offset')
23+
throw ERR_OUT_OF_RANGE('offset', 'an integer', value)
24+
}
25+
26+
if (length < 0) throw ERR_BUFFER_OUT_OF_BOUNDS()
27+
28+
throw ERR_OUT_OF_RANGE('offset', `>= 0 and <= ${length}`, value)
29+
}
30+
31+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L129-L145
32+
export function readBigInt64LE(buffer: Buffer, offset = 0): bigint {
33+
validateNumber(offset, 'offset')
34+
const first = buffer[offset]
35+
const last = buffer[offset + 7]
36+
if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8)
37+
// tslint:disable-next-line:no-bitwise
38+
const val = buffer[offset + 4] + buffer[offset + 5] * 2 ** 8 + buffer[offset + 6] * 2 ** 16 + (last << 24) // Overflow
39+
return (
40+
(BigInt(val) << BigInt(32)) + // tslint:disable-line:no-bitwise
41+
BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24)
42+
)
43+
}
44+
45+
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L89-L107
46+
export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint {
47+
validateNumber(offset, 'offset')
48+
const first = buffer[offset]
49+
const last = buffer[offset + 7]
50+
if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8)
51+
52+
const lo = first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24
53+
54+
const hi = buffer[++offset] + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + last * 2 ** 24
55+
56+
return BigInt(lo) + (BigInt(hi) << BigInt(32)) // tslint:disable-line:no-bitwise
57+
}

0 commit comments

Comments
 (0)