wasm-rquickjs 0.4.4

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! WASI Preview 3 `node:http` client native bridge.
//!
//! This is the Preview 3 counterpart of [`super::node_http`](node_http.rs). It exposes the *exact
//! same* native class contract (`NodeHttpClientRequest` and `NodeHttpIncomingResponse`) so that the
//! shared JavaScript implementation in `node_http.js` / `node_http_server.js` can be reused verbatim
//! on both generation targets. The only difference is the transport: the Preview 2 file drives
//! requests through `wasip2::http::outgoing_handler` (which depends on `wstd`/`wasip2` pollables),
//! whereas this file uses the `wasip3` crate's `wasi:http/client` binding
//! (`wasip3::http::client::send`) and Component Model async `stream<u8>` / `future` bodies. No
//! `wstd`, `wasip2`, or Preview 2 pollables are used.
//!
//! Per the project's `node:http` transport rule, every client request goes through `wasi:http`;
//! there is no `node:net` loopback fallback. The `node:http` *server* is built on `node:net`
//! (which is itself ported to Preview 3), so it works on this path unchanged.
//!
//! ## Request lifecycle
//!
//! `node_http.js` drives a client request as: create → `start()` → `writeStream()`* → `finish()` →
//! `waitForResponse()` → `getResponse()`, then reads the response body incrementally via
//! `NodeHttpIncomingResponse::readBodyChunk()`.
//!
//! The request body is **buffered** across the `write*` calls and sent once the body is finished.
//! The outgoing `wasi:http/client.send` subtask and the request-body upload are then driven
//! **concurrently** via [`futures::future::join`] (mirroring the buffered `send_once` path in the
//! Preview 3 `fetch` transport). This concurrency is required: the `send` subtask only drains the
//! request body `stream<u8>` while it is being polled, so writing body chunks while the send future
//! sits unpolled would deadlock the shared async executor. The response body is still streamed
//! chunk-by-chunk (not read to completion eagerly).

use super::http_body::ResponseBody;
use rquickjs::class::Trace;
use rquickjs::prelude::List;
use rquickjs::{Ctx, Exception, JsLifetime, TypedArray};
use std::future::Future;
use std::pin::Pin;
use url::Url;
use wasip3::http::types::{ErrorCode, Fields, Method, Request, Response, Scheme, Trailers};

/// Boxed, pinned future that drives a `wasi:http/client.send` and the request-body upload
/// concurrently, resolving once the response head is available.
type SendFuture = Pin<Box<dyn Future<Output = Result<Response, ErrorCode>>>>;

#[rquickjs::module]
pub mod native_module {
    pub use super::NodeHttpClientRequest;
    pub use super::NodeHttpIncomingResponse;
}

pub(crate) struct RawResponse {
    status: u16,
    headers: Vec<Vec<String>>,
    response: Response,
}

enum RequestState {
    /// Accumulating request headers and body. The body is buffered here across `write*` calls.
    Building {
        body: Vec<u8>,
    },
    /// Transient state while an async write/finish is in progress. Prevents re-entrant calls from
    /// seeing `Aborted` incorrectly.
    Writing,
    /// The request body is finished; the joined send+upload future is in flight.
    Sending {
        send_future: SendFuture,
    },
    ResponseReady(RawResponse),
    Consumed,
    Aborted,
}

#[derive(Trace, JsLifetime)]
#[rquickjs::class(rename_all = "camelCase")]
pub struct NodeHttpClientRequest {
    method: String,
    url: String,
    #[qjs(skip_trace)]
    headers: Vec<(String, String)>,
    #[qjs(skip_trace)]
    state: RequestState,
    aborted: bool,
}

impl Default for NodeHttpClientRequest {
    fn default() -> Self {
        Self::new("GET".to_string(), "http://localhost".to_string())
    }
}

#[rquickjs::methods(rename_all = "camelCase")]
impl NodeHttpClientRequest {
    #[qjs(constructor)]
    pub fn new(method: String, url: String) -> Self {
        NodeHttpClientRequest {
            method,
            url,
            headers: Vec::new(),
            state: RequestState::Building { body: Vec::new() },
            aborted: false,
        }
    }

    pub async fn start<'js>(&mut self, ctx: Ctx<'js>) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }
        // The request body is buffered until `finish`, so `start` only validates the URL eagerly
        // (matching the Preview 2 path, which fails a malformed URL when the request starts).
        if matches!(self.state, RequestState::Building { .. }) {
            Url::parse(&self.url)
                .map_err(|_| Exception::throw_message(&ctx, "failed to parse url"))?;
        }
        Ok(())
    }

    pub fn write<'js>(
        &mut self,
        ctx: Ctx<'js>,
        chunk: TypedArray<'js, u8>,
    ) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        let bytes = chunk.as_bytes().ok_or_else(|| {
            Exception::throw_message(&ctx, "the Uint8Array passed to write is detached")
        })?;

        self.append_body(&ctx, bytes)
    }

    pub fn write_string<'js>(&mut self, ctx: Ctx<'js>, data: String) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        self.append_body(&ctx, data.as_bytes())
    }

    pub async fn write_stream<'js>(
        &mut self,
        ctx: Ctx<'js>,
        chunk: TypedArray<'js, u8>,
    ) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        let bytes = chunk.as_bytes().ok_or_else(|| {
            Exception::throw_message(&ctx, "the Uint8Array passed to write is detached")
        })?;

        self.append_body(&ctx, bytes)
    }

    pub async fn write_string_stream<'js>(
        &mut self,
        ctx: Ctx<'js>,
        data: String,
    ) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        self.append_body(&ctx, data.as_bytes())
    }

    pub fn set_header<'js>(
        &mut self,
        ctx: Ctx<'js>,
        name: String,
        value: String,
    ) -> rquickjs::Result<()> {
        if !matches!(self.state, RequestState::Building { .. }) {
            return Err(Exception::throw_message(
                &ctx,
                "Cannot set headers after request has been sent",
            ));
        }
        let lower = name.to_ascii_lowercase();
        self.headers
            .retain(|(n, _)| n.to_ascii_lowercase() != lower);
        self.headers.push((name, value));
        Ok(())
    }

    pub fn append_header<'js>(
        &mut self,
        ctx: Ctx<'js>,
        name: String,
        value: String,
    ) -> rquickjs::Result<()> {
        if !matches!(self.state, RequestState::Building { .. }) {
            return Err(Exception::throw_message(
                &ctx,
                "Cannot set headers after request has been sent",
            ));
        }
        self.headers.push((name, value));
        Ok(())
    }

    pub fn remove_header<'js>(&mut self, ctx: Ctx<'js>, name: String) -> rquickjs::Result<()> {
        if !matches!(self.state, RequestState::Building { .. }) {
            return Err(Exception::throw_message(
                &ctx,
                "Cannot remove headers after request has been sent",
            ));
        }
        let lower = name.to_ascii_lowercase();
        self.headers
            .retain(|(n, _)| n.to_ascii_lowercase() != lower);
        Ok(())
    }

    pub async fn finish<'js>(
        &mut self,
        ctx: Ctx<'js>,
        chunk: Option<TypedArray<'js, u8>>,
    ) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        if let Some(chunk) = chunk {
            let bytes = chunk.as_bytes().ok_or_else(|| {
                Exception::throw_message(&ctx, "the Uint8Array passed to finish is detached")
            })?;
            self.append_body(&ctx, bytes)?;
        }

        // Take the buffered body and build the outgoing request. The send subtask and the body
        // upload are driven concurrently by the joined future so the request body stream is drained
        // while `send` is in flight.
        let taken = std::mem::replace(&mut self.state, RequestState::Writing);
        let RequestState::Building { body } = taken else {
            self.state = taken;
            return Err(Exception::throw_message(
                &ctx,
                "Cannot finish: request has already been sent",
            ));
        };

        let parsed_url = match Url::parse(&self.url) {
            Ok(url) => url,
            Err(_) => {
                self.state = RequestState::Aborted;
                self.aborted = true;
                return Err(Exception::throw_message(&ctx, "failed to parse url"));
            }
        };

        let fields = build_fields(&self.headers);
        let (mut body_tx, body_rx) = wasip3::wit_stream::new();
        let (trailers_tx, trailers_rx) =
            wasip3::wit_future::new(|| Ok::<Option<Trailers>, ErrorCode>(None));
        // `_transmit` resolves to the request body transmission result; upload failures surface as
        // a rejected `send`, so it is dropped here.
        let (request, _transmit) = Request::new(fields, Some(body_rx), trailers_rx, None);

        let method = parse_method(&self.method);
        let scheme = parse_scheme(parsed_url.scheme());
        let authority = url_authority(&parsed_url);
        let path_with_query = url_path_with_query(&parsed_url);
        apply_request_targets(
            &ctx,
            &request,
            &method,
            scheme.as_ref(),
            authority.as_deref(),
            &path_with_query,
        )?;

        let send_future: SendFuture = Box::pin(async move {
            let write_fut = async move {
                // `write_all` returns any bytes it could not deliver; dropping the writer closes
                // the request body stream and dropping the trailers writer resolves it to the
                // default `Ok(None)` (no trailers).
                let _remaining = body_tx.write_all(body).await;
                drop(body_tx);
                drop(trailers_tx);
            };
            let (send_result, ()) =
                futures::future::join(wasip3::http::client::send(request), write_fut).await;
            send_result
        });

        self.state = RequestState::Sending { send_future };
        Ok(())
    }

    pub async fn wait_for_response<'js>(&mut self, ctx: Ctx<'js>) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        let taken = std::mem::replace(&mut self.state, RequestState::Writing);
        if let RequestState::Sending { send_future } = taken {
            let response = send_future
                .await
                .map_err(|err| throw_http_error(&ctx, &err))?;

            let status = response.get_status_code();
            let headers = fields_to_pairs(&response.get_headers());

            self.state = RequestState::ResponseReady(RawResponse {
                status,
                headers,
                response,
            });
            Ok(())
        } else {
            self.state = taken;
            Err(Exception::throw_message(
                &ctx,
                "Cannot wait for response: request body not finished",
            ))
        }
    }

    pub async fn end<'js>(
        &mut self,
        ctx: Ctx<'js>,
        chunk: Option<TypedArray<'js, u8>>,
    ) -> rquickjs::Result<()> {
        if self.aborted {
            return Err(Exception::throw_message(&ctx, "Request has been aborted"));
        }

        self.finish(ctx.clone(), chunk).await?;
        self.wait_for_response(ctx).await?;
        Ok(())
    }

    pub fn get_response<'js>(&mut self, _ctx: Ctx<'js>) -> Option<NodeHttpIncomingResponse> {
        if self.aborted {
            return None;
        }

        let taken = std::mem::replace(&mut self.state, RequestState::Consumed);
        if let RequestState::ResponseReady(raw) = taken {
            Some(NodeHttpIncomingResponse::from_raw_response(raw))
        } else {
            self.state = taken;
            None
        }
    }

    pub fn abort(&mut self) {
        self.aborted = true;
        self.state = RequestState::Aborted;
    }
}

impl NodeHttpClientRequest {
    /// Appends bytes to the buffered request body, erroring if the request has already been sent.
    fn append_body(&mut self, ctx: &Ctx<'_>, bytes: &[u8]) -> rquickjs::Result<()> {
        match &mut self.state {
            RequestState::Building { body } => {
                body.extend_from_slice(bytes);
                Ok(())
            }
            _ => Err(Exception::throw_message(
                ctx,
                "Cannot write after request body has been finished",
            )),
        }
    }
}

#[derive(Trace, JsLifetime)]
#[rquickjs::class(rename_all = "camelCase")]
pub struct NodeHttpIncomingResponse {
    #[qjs(skip_trace)]
    body: ResponseBody,
    headers: Vec<Vec<String>>,
    status: u16,
}

impl Default for NodeHttpIncomingResponse {
    fn default() -> Self {
        Self::new()
    }
}

#[rquickjs::methods(rename_all = "camelCase")]
impl NodeHttpIncomingResponse {
    #[qjs(constructor)]
    pub fn new() -> Self {
        NodeHttpIncomingResponse {
            body: ResponseBody::empty(),
            headers: Vec::new(),
            status: 0,
        }
    }

    #[qjs(skip)]
    pub(crate) fn from_raw_response(raw: RawResponse) -> Self {
        NodeHttpIncomingResponse {
            body: ResponseBody::new(raw.response),
            headers: raw.headers,
            status: raw.status,
        }
    }

    #[qjs(get)]
    pub fn status(&self) -> u16 {
        self.status
    }

    #[qjs(get)]
    pub fn headers(&self) -> Vec<Vec<String>> {
        self.headers.clone()
    }

    pub fn discard_body(&mut self) {
        // Dropping the response / stream reader / result future discards the body without reading.
        self.body.discard();
    }

    pub async fn read_body_chunk<'js>(
        &mut self,
        ctx: Ctx<'js>,
    ) -> rquickjs::Result<List<(Option<TypedArray<'js, u8>>, bool)>> {
        match self.body.read_chunk().await {
            Ok(Some(bytes)) => {
                let chunk = TypedArray::new_copy(ctx.clone(), bytes).map_err(|_| {
                    Exception::throw_message(
                        &ctx,
                        "Failed to create TypedArray from response body chunk",
                    )
                })?;
                Ok(List((Some(chunk), false)))
            }
            Ok(None) => Ok(List((None, true))),
            Err(error) => Err(Exception::throw_message(&ctx, &error)),
        }
    }
}

fn http_error_to_node_code(err: &ErrorCode) -> &'static str {
    match err {
        ErrorCode::ConnectionTerminated => "ECONNRESET",
        ErrorCode::ConnectionReadTimeout => "ETIMEDOUT",
        ErrorCode::ConnectionWriteTimeout => "ETIMEDOUT",
        ErrorCode::ConnectionTimeout => "ETIMEDOUT",
        ErrorCode::HttpResponseTimeout => "ETIMEDOUT",
        ErrorCode::ConnectionRefused => "ECONNREFUSED",
        ErrorCode::DnsTimeout => "ENOTFOUND",
        ErrorCode::DnsError(e) => {
            if e.rcode.as_deref() == Some("NXDOMAIN") || e.info_code == Some(3) {
                "ENOTFOUND"
            } else {
                "EAI_FAIL"
            }
        }
        ErrorCode::DestinationNotFound => "ENOTFOUND",
        ErrorCode::DestinationUnavailable => "ECONNREFUSED",
        ErrorCode::HttpResponseIncomplete => "ECONNRESET",
        ErrorCode::HttpProtocolError => "ECONNRESET",
        ErrorCode::InternalError(_) => "ECONNRESET",
        _ => "EIO",
    }
}

fn throw_http_error(ctx: &Ctx<'_>, err: &ErrorCode) -> rquickjs::Error {
    let code = http_error_to_node_code(err);
    let message = format!("{err:?}");
    let escaped = message.replace('\\', "\\\\").replace('"', "\\\"");
    Exception::throw_message(
        ctx,
        &format!("{{\"code\":\"{code}\",\"syscall\":\"request\",\"message\":\"{escaped}\"}}"),
    )
}

// ---------------------------------------------------------------------------
// wasip3 request helpers (mirrors the Preview 2 native contract).
// ---------------------------------------------------------------------------

fn parse_method(method: &str) -> Method {
    // Match the standard methods by their canonical (upper-case) spelling only; any other token
    // becomes an extension method with its original case preserved, exactly like the Preview 2 path.
    match method {
        "GET" => Method::Get,
        "HEAD" => Method::Head,
        "POST" => Method::Post,
        "PUT" => Method::Put,
        "DELETE" => Method::Delete,
        "CONNECT" => Method::Connect,
        "OPTIONS" => Method::Options,
        "TRACE" => Method::Trace,
        "PATCH" => Method::Patch,
        other => Method::Other(other.to_string()),
    }
}

fn parse_scheme(scheme: &str) -> Option<Scheme> {
    match scheme {
        "http" => Some(Scheme::Http),
        "https" => Some(Scheme::Https),
        "" => None,
        other => Some(Scheme::Other(other.to_string())),
    }
}

fn url_authority(url: &Url) -> Option<String> {
    let host = url.host_str()?;
    Some(match url.port() {
        Some(port) => format!("{host}:{port}"),
        None => host.to_string(),
    })
}

fn url_path_with_query(url: &Url) -> String {
    match url.query() {
        Some(query) => format!("{}?{}", url.path(), query),
        None => url.path().to_string(),
    }
}

fn apply_request_targets(
    ctx: &Ctx<'_>,
    request: &Request,
    method: &Method,
    scheme: Option<&Scheme>,
    authority: Option<&str>,
    path_with_query: &str,
) -> rquickjs::Result<()> {
    request
        .set_method(method)
        .map_err(|_| Exception::throw_message(ctx, "failed to set method"))?;
    request
        .set_scheme(scheme)
        .map_err(|_| Exception::throw_message(ctx, "failed to set scheme"))?;
    request
        .set_authority(authority)
        .map_err(|_| Exception::throw_message(ctx, "failed to set authority"))?;
    request
        .set_path_with_query(Some(path_with_query))
        .map_err(|_| Exception::throw_message(ctx, "failed to set path"))?;
    Ok(())
}

/// Builds a `wasi:http` `fields` resource from a header list, skipping the `host` header (which the
/// transport derives from the request authority).
fn build_fields(headers: &[(String, String)]) -> Fields {
    let fields = Fields::new();
    for (name, value) in headers {
        if name.eq_ignore_ascii_case("host") {
            continue;
        }
        // Best effort: ignore headers the host rejects rather than failing the whole request.
        let _ = fields.append(name, value.as_bytes());
    }
    fields
}

/// Hop-by-hop / forbidden headers that the wasmtime Preview 2 host strips from
/// incoming response fields (`remove_forbidden_headers`). The Preview 3 host
/// passes response headers through unfiltered, so we apply the same filter here
/// to keep the two generation targets behaviorally identical.
fn is_forbidden_response_header(name: &str) -> bool {
    matches!(
        name.to_ascii_lowercase().as_str(),
        "connection"
            | "keep-alive"
            | "proxy-connection"
            | "proxy-authenticate"
            | "proxy-authorization"
            | "transfer-encoding"
            | "upgrade"
            | "host"
            | "http2-settings"
    )
}

fn fields_to_pairs(fields: &Fields) -> Vec<Vec<String>> {
    fields
        .copy_all()
        .into_iter()
        .filter(|(name, _)| !is_forbidden_response_header(name))
        .map(|(name, value)| {
            vec![
                name,
                String::from_utf8(value).unwrap_or_else(|_| "Invalid header value".to_string()),
            ]
        })
        .collect()
}

// ---------------------------------------------------------------------------
// JavaScript sources (shared with the Preview 2 path).
// ---------------------------------------------------------------------------

pub const NODE_HTTP_JS: &str = include_str!("node_http.js");
pub const NODE_HTTP_SERVER_JS: &str = include_str!("node_http_server.js");
pub const HTTP_INCOMING_JS: &str = include_str!("node_http_incoming.js");
pub const HTTP_COMMON_JS: &str = include_str!("node_http_common.js");
pub const HTTP_AGENT_JS: &str = include_str!("node_http_agent.js");
pub const REEXPORT_JS: &str = r#"export * from 'node:http'; export { default } from 'node:http';"#;