Skip to content

Commit d23075c

Browse files
fix: test CI issues
1 parent eb963d7 commit d23075c

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
lines changed

package-lock.json

Lines changed: 29 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
"eslint-plugin-prettier": "^5.0.1",
2727
"jest": "^29.7.0",
2828
"prettier": "^3.0.3",
29-
"random-bigint": "^0.0.1",
3029
"ts-jest": "^29.1.1",
3130
"ts-node": "^10.9.2",
3231
"tsup": "^8.0.1",
3332
"typescript": "^5.2.2",
3433
"uuid": "^9.0.1"
3534
}
36-
}
35+
}

src/test/RandomBigint.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/test/randomBigint.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// https://github.com/bnoordhuis/random-bigint/blob/master/index.js
2+
3+
import { randomBytes } from "crypto"
4+
5+
export function random(bits: number) {
6+
if (bits < 0)
7+
throw new RangeError('bits < 0')
8+
9+
// @ts-ignore
10+
const n = (bits >>> 3) + !!(bits & 7) // Round up to next byte.
11+
const r = 8*n - bits
12+
const s = 8 - r
13+
const m = (1 << s) - 1 // Bits to mask off from MSB.
14+
15+
const bytes = randomBytes(n)
16+
17+
maskbits(m, bytes)
18+
19+
return bytes2bigint(bytes)
20+
}
21+
22+
function maskbits(m: number, bytes: Buffer) {
23+
// Mask off bits from the MSB that are > log2(bits).
24+
// |bytes| is treated as a big-endian bigint so byte 0 is the MSB.
25+
if (bytes.length > 0)
26+
bytes[0] &= m
27+
}
28+
29+
function bytes2bigint(bytes: Buffer) {
30+
let result = BigInt(0)
31+
32+
const n = bytes.length
33+
34+
// Read input in 8 byte slices. This is, on average and at the time
35+
// of writing, about 35x faster for large inputs than processing them
36+
// one byte at a time.
37+
if (n >= 8) {
38+
const view = new DataView(bytes.buffer, bytes.byteOffset)
39+
40+
for (let i = 0, k = n & ~7; i < k; i += 8) {
41+
const x = view.getBigUint64(i, false)
42+
result = (result << BigInt(64)) + x
43+
}
44+
}
45+
46+
// Now mop up any remaining bytes.
47+
for (let i = n & ~7; i < n; i++)
48+
result = result * BigInt(256) + BigInt(bytes[i])
49+
50+
return result
51+
}

src/test/setup.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { readFileSync } from "node:fs"
22
import { argv, env } from "node:process"
33
import { WASI } from "wasi"
4-
import random from "random-bigint"
54
import { v4 } from "uuid"
65
import { NavigraphNavigationDataInterface } from "../js"
76
import { WEBASSEMBLY_PATH, WORK_FOLDER_PATH } from "./constants"
87
import "dotenv/config"
8+
import { random } from "./randomBigint"
99

1010
enum PanelService {
1111
POST_QUERY = 1,
@@ -157,6 +157,7 @@ let wasmFunctionTable: WebAssembly.Table // The table of callback functions in t
157157
* Maps request ids to a tuple of the returned data's pointer, and the data's size
158158
*/
159159
const promiseResults = new Map<bigint, [number, number]>()
160+
const failedRequests: bigint[] = [];
160161

161162
wasmInstance = new WebAssembly.Instance(wasmModule, {
162163
wasi_snapshot_preview1: wasiSystem.wasiImport,
@@ -205,7 +206,7 @@ wasmInstance = new WebAssembly.Instance(wasmModule, {
205206
fsNetworkHttpRequestGet: (urlPointer: number, paramPointer: number, callback: number, ctx: number) => {
206207
const url = readString(urlPointer)
207208

208-
const requestId: bigint = random(32) // Setting it to 64 does... strange things
209+
const requestId = random(16) // Extra bits get lopped off by WASM, this number works
209210

210211
// Currently the only network request is for the navigation data zip which is downloaded as a blob
211212
fetch(url)
@@ -222,11 +223,20 @@ wasmInstance = new WebAssembly.Instance(wasmModule, {
222223
func(requestId, 200, ctx)
223224
})
224225
.catch(err => {
225-
console.error(err)
226+
failedRequests.push(requestId);
226227
})
227228

228229
return requestId
229230
},
231+
fsNetworkHttpRequestGetState: (requestId: bigint) => {
232+
if(failedRequests.includes(requestId)) {
233+
return 4 // FS_NETWORK_HTTP_REQUEST_STATE_FAILED
234+
}
235+
if(promiseResults.has(requestId)) {
236+
return 3 // FS_NETWORK_HTTP_REQUEST_STATE_DATA_READY
237+
}
238+
return 2 // FS_NETWORK_HTTP_REQUEST_STATE_WAITING_FOR_DATA
239+
}
230240
},
231241
}) as WasmInstance
232242

0 commit comments

Comments
 (0)