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" ) ;
57
2
58
3
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 ;
79
35
}
80
36
81
- run ( )
37
+ run ( ) . then ( value => console . log ( value ) ) . catch ( err => console . log ( err ) ) ;
0 commit comments