Skip to content

Commit dfdb1ac

Browse files
authored
chore(nodejs): update examples (#3)
1 parent 0cb88b4 commit dfdb1ac

File tree

3 files changed

+67
-129
lines changed

3 files changed

+67
-129
lines changed

examples.manifest.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
NodeJS client library [repo](https://github.com/questdb/nodejs-questdb-client).
66
- name: ilp-auth-tls
77
lang: javascript
8-
path: examples/auth_and_tls.js
8+
path: examples/auth_tls.js
99
header: |-
1010
NodeJS client library [repo](https://github.com/questdb/nodejs-questdb-client).
1111
auth:
12-
kid: testUser1
13-
d: 5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48
14-
x: fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU
15-
y: Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac
12+
kid: testapp
13+
d: 9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8
14+
x: aultdA0PjhD_cWViqKKyL5chm6H1n-BiZBo_48T-uqc
15+
y: __ptaol41JWSpTTL525yVEfzmY8A6Vi_QrW1FjKcHMg
1616
addr:
1717
host: localhost
1818
port: 9009

examples/auth_tls.js

Lines changed: 33 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,37 @@
1-
const { TLSSocket } = require("tls")
2-
const { Crypto } = require("node-webcrypto-ossl")
3-
4-
const client = new TLSSocket()
5-
const crypto = new Crypto()
6-
7-
const HOST = "localhost"
8-
const PORT = 9009
9-
10-
const JWK = {
11-
kid: "testUser1",
12-
kty: "EC",
13-
d: "5UjEMuA0Pj5pjK8a-fa24dyIf-Es5mYny3oE_Wmus48",
14-
crv: "P-256",
15-
x: "fLKYEaoEb9lrn3nkwLDA-M_xnuFOdSt9y0Z7_vWSHLU",
16-
y: "Dt5tbS1dEDMSYfym3fgMv0B99szno-dFc1rYF9t0aac",
17-
}
18-
19-
async function write(data) {
20-
return new Promise((resolve) => {
21-
client.write(data, () => {
22-
resolve()
23-
})
24-
})
25-
}
26-
27-
async function authenticate(challenge) {
28-
// Check for trailing \\n which ends the challenge
29-
if (challenge.slice(-1).readInt8() === 10) {
30-
const apiKey = await crypto.subtle.importKey(
31-
"jwk",
32-
JWK,
33-
{ name: "ECDSA", namedCurve: "P-256" },
34-
true,
35-
["sign"],
36-
)
37-
38-
const signature = await crypto.subtle.sign(
39-
{ name: "ECDSA", hash: "SHA-256" },
40-
apiKey,
41-
challenge.slice(0, challenge.length - 1),
42-
)
43-
44-
await write(`${Buffer.from(signature).toString("base64")}\n`)
45-
46-
return true
47-
}
48-
49-
return false
50-
}
51-
52-
async function sendData() {
53-
const now = Date.now()
54-
await write(`test,location=us temperature=22.4 ${now * 1e6}`)
55-
await write(`test,location=us temperature=21.4 ${now * 1e6}`)
56-
}
1+
const { Sender } = require("@questdb/nodejs-client");
572

583
async function run() {
59-
let authenticated = false
60-
let data
61-
62-
client.on("data", async function (raw) {
63-
data = !data ? raw : Buffer.concat([data, raw])
64-
65-
if (!authenticated) {
66-
authenticated = await authenticate(data)
67-
await sendData()
68-
setTimeout(() => {
69-
client.destroy()
70-
}, 0)
71-
}
72-
})
73-
74-
client.on("ready", async function () {
75-
await write(JWK.kid)
76-
})
77-
78-
client.connect(PORT, HOST)
4+
// construct a JsonWebKey
5+
const CLIENT_ID = "testapp";
6+
const PRIVATE_KEY = "9b9x5WhJywDEuo1KGQWSPNxtX-6X6R2BRCKhYMMY6n8";
7+
const PUBLIC_KEY = {
8+
x: "aultdA0PjhD_cWViqKKyL5chm6H1n-BiZBo_48T-uqc",
9+
y: "__ptaol41JWSpTTL525yVEfzmY8A6Vi_QrW1FjKcHMg"
10+
};
11+
const JWK = {
12+
...PUBLIC_KEY,
13+
d: PRIVATE_KEY,
14+
kid: CLIENT_ID,
15+
kty: "EC",
16+
crv: "P-256",
17+
};
18+
19+
// pass the JsonWebKey to the sender
20+
// will use it for authentication
21+
const sender = new Sender({bufferSize: 4096, jwk: JWK});
22+
23+
// connect() takes an optional second argument
24+
// if 'true' passed the connection is secured with TLS encryption
25+
await sender.connect({port: 9009, host: "127.0.0.1"}, true);
26+
27+
// send the data over the authenticated and secure connection
28+
sender.table("prices").symbol("instrument", "EURUSD")
29+
.floatColumn("bid", 1.0197).floatColumn("ask", 1.0224).atNow();
30+
await sender.flush();
31+
32+
// close the connection after all rows ingested
33+
await sender.close();
34+
return 0;
7935
}
8036

81-
run()
37+
run().then(value => console.log(value)).catch(err => console.log(err));

examples/basic.js

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,31 @@
1-
// Raw socket connection with no validation and string quoting logic.
2-
// Refer to protocol description:
3-
// http://questdb.io/docs/reference/api/ilp/overview
4-
5-
"use strict"
6-
7-
const net = require("net")
8-
9-
const client = new net.Socket()
10-
11-
const HOST = "localhost"
12-
const PORT = 9009
13-
14-
function run() {
15-
client.connect(PORT, HOST, () => {
16-
const rows = [
17-
`trades,name=test_ilp1 value=12.4 ${Date.now() * 1e6}`,
18-
`trades,name=test_ilp2 value=11.4 ${Date.now() * 1e6}`,
19-
]
20-
21-
function write(idx) {
22-
if (idx === rows.length) {
23-
client.destroy()
24-
return
25-
}
26-
27-
client.write(rows[idx] + "\n", (err) => {
28-
if (err) {
29-
console.error(err)
30-
process.exit(1)
31-
}
32-
write(++idx)
33-
})
34-
}
35-
36-
write(0)
37-
})
38-
39-
client.on("error", (err) => {
40-
console.error(err)
41-
process.exit(1)
42-
})
43-
44-
client.on("close", () => {
45-
console.log("Connection closed")
46-
})
1+
const { Sender } = require("@questdb/nodejs-client");
2+
3+
async function run() {
4+
// create a sender with a 4k buffer
5+
const sender = new Sender({bufferSize: 4096});
6+
7+
// connect to QuestDB
8+
// host and port are required in connect options
9+
await sender.connect({port: 9009, host: "127.0.0.1"});
10+
11+
// add rows to the buffer of the sender
12+
sender.table("prices").symbol("instrument", "EURUSD")
13+
.floatColumn("bid", 1.0195).floatColumn("ask", 1.0221).atNow();
14+
sender.table("prices").symbol("instrument", "GBPUSD")
15+
.floatColumn("bid", 1.2076).floatColumn("ask", 1.2082).atNow();
16+
17+
// flush the buffer of the sender, sending the data to QuestDB
18+
// the buffer is cleared after the data is sent and the sender is ready to accept new data
19+
await sender.flush();
20+
21+
// add rows to the buffer again and send it to the server
22+
sender.table("prices").symbol("instrument", "EURUSD")
23+
.floatColumn("bid", 1.0197).floatColumn("ask", 1.0224).atNow();
24+
await sender.flush();
25+
26+
// close the connection after all rows ingested
27+
await sender.close();
28+
return 0;
4729
}
4830

49-
run()
31+
run().then(value => console.log(value)).catch(err => console.log(err));

0 commit comments

Comments
 (0)