-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
160 lines (136 loc) · 4.79 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { AccountFilterFlags } = require("tigerbeetle-node");
const { createClient } = require("tigerbeetle-node");
const { randomFillSync } = require("crypto");
const { resolve4 } = require("dns/promises");
const TB_ADDRESSES = process.env.TB_ADDRESSES!;
const TB_PORT = process.env.TB_PORT!;
const hostnames = TB_ADDRESSES.split(",");
const addresses = (await Promise.all(hostnames.map(async (hostname) => {
const ip = await resolve4(hostname);
console.log(hostname, ip);
if (ip.length >= 1) {
return [`${ip[0]}:${TB_PORT}`];
}
return [];
}))).flatMap((i) => i);
console.log(addresses);
const client = createClient({
cluster_id: 0n,
replica_addresses: addresses,
});
console.log(client)
const account1 = {
id: 1n, // TigerBeetle time-based ID.
debits_pending: 0n,
debits_posted: 0n,
credits_pending: 0n,
credits_posted: 0n,
user_data_128: 0n,
user_data_64: 0n,
user_data_32: 0,
reserved: 0,
ledger: 1,
code: 718,
flags: 0,
timestamp: 0n,
};
const account2 = {
id: 2n, // TigerBeetle time-based ID.
debits_pending: 0n,
debits_posted: 0n,
credits_pending: 0n,
credits_posted: 0n,
user_data_128: 0n,
user_data_64: 0n,
user_data_32: 0,
reserved: 0,
ledger: 1,
code: 718,
flags: 0,
timestamp: 0n,
};
const account_errors = await client.createAccounts([account1, account2]);
// const accounts = await client.lookupAccounts([1n]);
let idLastTimestamp = 0;
let idLastBuffer = new ArrayBuffer(16);
/**
* Generates a Universally Unique and Sortable Identifier as a u128 bigint.
*
* @remarks
* Based on {@link https://github.com/ulid/spec}, IDs returned are guaranteed to be monotonically
* increasing.
*/
export function id(): bigint {
// Ensure timestamp monotonically increases and generate a new random on each new timestamp.
let timestamp = Date.now()
if (timestamp <= idLastTimestamp) {
timestamp = idLastTimestamp
} else {
idLastTimestamp = timestamp
randomFillSync(new Uint8Array(idLastBuffer))
}
const idLastBufferDv = new DataView(idLastBuffer);
// Increment the u80 in idLastBuffer using carry arithmetic on u32s (as JS doesn't have fast u64).
const littleEndian = true
const randomLo32 = idLastBufferDv.getUint32(0, littleEndian) + 1
const randomHi32 = idLastBufferDv.getUint32(4, littleEndian) + (randomLo32 > 0xFFFFFFFF ? 1 : 0)
const randomHi16 = idLastBufferDv.getUint16(8, littleEndian) + (randomHi32 > 0xFFFFFFFF ? 1 : 0)
if (randomHi16 > 0xFFFF) {
throw new Error('random bits overflow on monotonic increment')
}
// Store the incremented random monotonic and the timestamp into the buffer.
idLastBufferDv.setUint32(0, randomLo32 & 0xFFFFFFFF, littleEndian)
idLastBufferDv.setUint32(4, randomHi32 & 0xFFFFFFFF, littleEndian)
idLastBufferDv.setUint16(8, randomHi16, littleEndian) // No need to mask since checked above.
idLastBufferDv.setUint16(10, timestamp & 0xFFFF, littleEndian) // timestamp lo.
idLastBufferDv.setUint32(12, (timestamp >>> 16) & 0xFFFFFFFF, littleEndian) // timestamp hi.
// Then return the buffer's contents as a little-endian u128 bigint.
const lo = idLastBufferDv.getBigUint64(0, littleEndian)
const hi = idLastBufferDv.getBigUint64(8, littleEndian)
return (hi << 64n) | lo
}
const server = Bun.serve({
port: 3000,
async fetch(req) {
const path = new URL(req.url).pathname;
// respond with text/html
if (path === "/") {
const filter = {
account_id: 1n,
user_data_128: 0n, // No filter by UserData.
user_data_64: 0n,
user_data_32: 0,
code: 0, // No filter by Code.
timestamp_min: 0n, // No filter by Timestamp.
timestamp_max: 0n, // No filter by Timestamp.
limit: 10, // Limit to ten balances at most.
flags: AccountFilterFlags.debits | // Include transfer from the debit side.
AccountFilterFlags.credits | // Include transfer from the credit side.
AccountFilterFlags.reversed, // Sort by timestamp in reverse-chronological order.
};
const txs = await client.getAccountTransfers(filter);
const txsj = JSON.stringify(txs, (_, v) => typeof v === 'bigint' ? v.toString() : v);
return Response.json(txsj);
};
if (path === "/trx") {
const trx = {
id: id(), // TigerBeetle time-based ID.
debit_account_id: 1n,
credit_account_id: 2n,
amount: 10n,
pending_id: 0n,
user_data_128: 0n,
user_data_64: 0n,
user_data_32: 0,
timeout: 0,
ledger: 1,
code: 720,
flags: 0,
timestamp: 0n,
}
await client.createTransfers([trx])
return new Response(`${trx.id}`);
}
},
});
console.log(`Listening on localhost:${server.port}`);