Skip to content

Commit 2ab5143

Browse files
authored
Merge pull request #2 from BadMachine/split-implementations
Split implementations
2 parents cfa29e0 + 623f149 commit 2ab5143

File tree

30 files changed

+654
-317
lines changed

30 files changed

+654
-317
lines changed

Cargo.toml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,19 @@ version = "3.2"
2828
git = "https://github.com/BadMachine/napi-rs"
2929
branch = "fix/readable-stream-byte-mode-lock"
3030
package = "napi"
31-
features = ["full","experimental","tokio","web_stream","tokio-stream"]
31+
#features = ["full","experimental","tokio","web_stream","tokio-stream"]
32+
features = ["experimental", "web_stream", "serde-json", "napi10"]
3233

3334
[dependencies.reqwest]
3435
version = "0.12"
3536
features = ["json","rustls-tls-native-roots-no-provider","gzip"]
3637

37-
[dependencies.tonic]
38-
version = "0.13"
39-
features = ["gzip","tls-native-roots","tls-webpki-roots", "tls-ring"] # since rustls update it requires to manually initialize cryptoprovider
38+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
39+
tonic = { version = "0.13", features = ["gzip","tls-native-roots","tls-webpki-roots","tls-ring"] }
40+
arrow = { version = "56.0.0", features = ["arrow-json"] }
41+
arrow-flight = { version = "56.0.0", features = ["flight-sql"] }
42+
serde_arrow = { version = "0.13.5", features = ["arrow-56"] }
4043

41-
[dependencies.arrow]
42-
version = "56.0.0"
43-
features = ["arrow-json"]
44-
45-
[dependencies.arrow-flight]
46-
version = "56.0.0"
47-
features = ["flight-sql"]
48-
49-
[dependencies.serde_arrow]
50-
version = "0.13.5"
51-
features = ["arrow-56"]
5244

5345
[build-dependencies]
5446
napi-build = "2.2.3"

__test__/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test('Test sql query from cloud serverless', async (t) => {
1313

1414
const arr = []
1515
for await (const item of result) {
16+
console.log(item)
1617
arr.push(item)
1718
}
1819

browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from 'influxdb3-napi-wasm32-wasi'
1+
export * from '@badmachine/influxdb3-napi-wasm32-wasi'

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"bench": "node --import @oxc-node/core/register benchmark/bench.ts",
5959
"build": "napi build --platform --release --no-const-enum",
6060
"build:wasm-unknown": "napi build --platform --release -t wasm32-unknown-unknown",
61-
"build:wasip2": "napi build --platform --release -t wasm32-wasip2",
61+
"build:wasip1-threads": "napi build --platform --release -t wasm32-wasip1-threads",
6262
"build:debug": "napi build --platform",
6363
"format": "run-p format:prettier format:rs format:toml",
6464
"format:prettier": "prettier . -w",
@@ -139,5 +139,8 @@
139139
"@badmachine/influxdb3-napi-win32-arm64-msvc": "1.0.0",
140140
"@badmachine/influxdb3-napi-win32-ia32-msvc": "1.0.0",
141141
"@badmachine/influxdb3-napi-win32-x64-msvc": "1.0.0"
142+
},
143+
"dependencies": {
144+
"@napi-rs/wasm-runtime": "^1.0.3"
142145
}
143146
}

src/bin/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use influxdb3_napi::client::options::{QueryPayload, QueryType};
2-
use napi::bindgen_prelude::Either3;
31
use napi::tokio;
4-
use napi::tokio_stream::StreamExt;
52

63
#[tokio::main]
74
#[cfg(feature = "native")]
85
async fn main() {
6+
println!("Not implemented yet for native binary");
97
}
108

119
#[tokio::main]
10+
#[cfg(not(target_arch = "wasm32"))]
1211
#[cfg(not(feature = "native"))]
1312
async fn main() {
1413
println!("Not implemented yet");

src/client/base.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// CURRENTLY NAPI-RS DOES NOT SUPPORT TRAIT IMPLEMENTATIONS
2+
3+
// use napi::bindgen_prelude::{Either3, ReadableStream};
4+
// use napi::Env;
5+
// use crate::client::options::{FlightOptions, QueryPayload};
6+
// use crate::client::WriteOptions;
7+
// use crate::serializer::library_serializer::LibraryReturnType;
8+
// use crate::serializer::Serializer;
9+
//
10+
// pub trait InfluxClientTrait {
11+
// fn new(
12+
// addr: String,
13+
// token: Option<String>,
14+
// serializer: Option<Serializer>,
15+
// options: Option<FlightOptions>,
16+
// ) -> Self;
17+
//
18+
// fn query(
19+
// &mut self,
20+
// query_payload: QueryPayload,
21+
// env: &Env,
22+
// ) -> napi::Result<
23+
// Either3<
24+
// ReadableStream<'_, LibraryReturnType>,
25+
// ReadableStream<'_, serde_json::Map<String, serde_json::Value>>,
26+
// ReadableStream<'_, napi::bindgen_prelude::Buffer>,
27+
// >,
28+
// >;
29+
//
30+
// fn write(
31+
// &mut self,
32+
// lines: Vec<String>,
33+
// database: String,
34+
// write_options: Option<WriteOptions>,
35+
// org: Option<String>,
36+
// ) -> napi::Result<()>;
37+
// }

src/client/browser/client.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use crate::client::http_client::get_http_client;
2+
use crate::client::options::{FlightOptions, QueryPayload, WriteOptions};
3+
use crate::serializer::browser::{Serializer};
4+
use napi::bindgen_prelude::{Buffer, Either, ReadableStream};
5+
use napi::Env;
6+
use napi::tokio_stream::wrappers::ReceiverStream;
7+
use reqwest::Client;
8+
use serde::{Deserialize, Serialize};
9+
use serde_json::{Map, Value};
10+
use crate::client::browser::HttpQueryResponseV1;
11+
use crate::query::browser::query_processor::into_stream;
12+
use napi::bindgen_prelude::*;
13+
14+
#[napi_derive::napi]
15+
pub struct InfluxDBClient {
16+
addr: String,
17+
http_client: Client,
18+
serializer: Serializer,
19+
options: FlightOptions,
20+
}
21+
22+
// replace it with #[napi_derive::napi] in the future
23+
// impl InfluxClientTrait for InfluxDBClient {
24+
#[napi_derive::napi]
25+
impl InfluxDBClient {
26+
#[napi(constructor)]
27+
pub fn new(
28+
addr: String,
29+
token: Option<String>,
30+
serializer: Option<Serializer>,
31+
options: Option<FlightOptions>,
32+
) -> Self {
33+
Self {
34+
addr,
35+
http_client: get_http_client(token.unwrap_or(String::from(""))),
36+
serializer: serializer.unwrap_or_default(),
37+
options: options.unwrap_or_default(),
38+
}
39+
}
40+
41+
#[napi_derive::napi]
42+
pub fn query(
43+
&mut self,
44+
query_payload: QueryPayload,
45+
env: &Env,
46+
) -> napi::Result<
47+
Either<
48+
ReadableStream<'_, Map<String, Value>>,
49+
ReadableStream<'_, Buffer>,
50+
>,
51+
> {
52+
let stream = self.query_inner(query_payload, env)?;
53+
Ok(Either::A(stream))
54+
}
55+
56+
pub fn query_inner(
57+
&mut self,
58+
query_payload: QueryPayload,
59+
env: &Env,
60+
) -> Result<ReadableStream<'_, serde_json::Map<String, serde_json::Value>>> {
61+
use napi::bindgen_prelude::block_on;
62+
63+
let stream: ReceiverStream<Result<serde_json::Map<String, serde_json::Value>>> = block_on(async {
64+
let url = format!("{}/query", self.addr);
65+
66+
let response = self
67+
.http_client
68+
.get(&url)
69+
.query(&[
70+
("db", query_payload.database.clone()),
71+
("q", query_payload.query.clone()),
72+
])
73+
.send()
74+
.await
75+
.map_err(|e| Error::from_reason(format!("HTTP request failed: {}", e)))?;
76+
77+
let status = response.status();
78+
if !status.is_success() {
79+
return Err(Error::from_reason(format!(
80+
"InfluxDB returned non-success status: {}",
81+
status
82+
)));
83+
}
84+
85+
let data: HttpQueryResponseV1 = response
86+
.json()
87+
.await
88+
.map_err(|e| Error::from_reason(format!("Failed to parse JSON: {}", e)))?;
89+
90+
Ok(into_stream(data))
91+
})?;
92+
93+
ReadableStream::new(env, stream)
94+
}
95+
96+
#[napi_derive::napi]
97+
pub fn write(
98+
&mut self,
99+
lines: Vec<String>,
100+
database: String,
101+
write_options: Option<WriteOptions>,
102+
org: Option<String>,
103+
) -> Result<()> {
104+
todo!()
105+
}
106+
}

src/client/browser/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
pub(crate) mod client;
4+
5+
#[derive(Deserialize, Serialize)]
6+
pub struct Series {
7+
name: String,
8+
pub(crate)columns: Vec<String>,
9+
pub(crate)values: Vec<Vec<serde_json::Value>>,
10+
}
11+
12+
#[derive(Deserialize, Serialize)]
13+
pub struct QueryDataV1 {
14+
statement_id: u32,
15+
pub(crate) series: Vec<Series>,
16+
}
17+
18+
#[derive(Deserialize, Serialize)]
19+
pub(crate) struct HttpQueryResponseV1 {
20+
pub(crate)results: Vec<QueryDataV1>,
21+
}

0 commit comments

Comments
 (0)