wasm-rquickjs 0.3.5

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
use rquickjs::prelude::*;
use url::Url;

// Native functions for the URL implementation
#[rquickjs::module(rename = "camelCase")]
pub mod native_module {
    use rquickjs::class::Trace;
    use rquickjs::convert::Coerced;
    use rquickjs::prelude::*;
    use rquickjs::{Ctx, Exception, JsLifetime, Value};
    use url::Url;

    #[derive(JsLifetime, Trace)]
    #[rquickjs::class(rename = "URL")]
    pub struct JsUrl {
        #[qjs(skip_trace)]
        url: Url,
    }

    #[rquickjs::methods(rename_all = "camelCase")]
    impl JsUrl {
        #[qjs(constructor)]
        pub fn new(
            url: Coerced<String>,
            base_url: Opt<Option<Coerced<String>>>,
            ctx: Ctx<'_>,
        ) -> rquickjs::Result<Self> {
            let url = url.0;
            let base = Opt(base_url.0.flatten().map(|c| c.0));

            match super::parse_url(url, base) {
                Ok(url) => Ok(Self { url }),
                Err(err) => Err(ctx.throw(
                    Exception::from_message(ctx.clone(), &format!("Invalid URL: {err}"))
                        .unwrap()
                        .into(),
                )),
            }
        }

        /// The hash property of the URL interface is a string containing a "#" followed by the fragment identifier of the URL.
        /// If the URL does not have a fragment identifier, this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "hash")]
        pub fn get_hash(&self) -> String {
            self.url
                .fragment()
                .map(|s| format!("#{s}"))
                .unwrap_or_default()
        }

        #[qjs(set, enumerable, rename = "hash")]
        pub fn set_hash(&mut self, value: Coerced<String>) {
            let value = value.0;
            if value.is_empty() {
                self.url.set_fragment(None);
            } else if let Some(s) = value.strip_prefix('#') {
                self.url.set_fragment(Some(s));
            } else {
                self.url.set_fragment(Some(&value));
            }
        }

        /// The host property of the URL interface is a string containing the host, which is the hostname, and then,
        /// if the port of the URL is nonempty, a ":", followed by the port of the URL.
        /// If the URL does not have a hostname, this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "host")]
        pub fn get_host(&self) -> String {
            if let Some(host) = self.url.host_str() {
                if let Some(port) = self.url.port() {
                    format!("{host}:{port}")
                } else {
                    host.to_string()
                }
            } else {
                String::new()
            }
        }

        #[qjs(set, enumerable, rename = "host")]
        pub fn set_host(&mut self, value: Coerced<String>, ctx: Ctx<'_>) -> rquickjs::Result<()> {
            let value = value.0;
            if value.is_empty() {
                let _ = self.url.set_host(None);
                Ok(())
            } else if let Some((host, port_str)) = value.rsplit_once(':') {
                if let Err(err) = self.url.set_host(Some(host)) {
                    Err(ctx.throw(
                        Exception::from_message(
                            ctx.clone(),
                            &format!("Failed to set URL host: {err}"),
                        )
                        .unwrap()
                        .into(),
                    ))
                } else if let Ok(port) = port_str.parse::<u16>() {
                    let _ = self.url.set_port(Some(port));
                    Ok(())
                } else {
                    Err(ctx.throw(
                        Exception::from_message(
                            ctx.clone(),
                            "Failed to set URL host: invalid port",
                        )
                        .unwrap()
                        .into(),
                    ))
                }
            } else if let Err(err) = self.url.set_host(Some(&value)) {
                Err(ctx.throw(
                    Exception::from_message(ctx.clone(), &format!("Failed to set URL host: {err}"))
                        .unwrap()
                        .into(),
                ))
            } else {
                Ok(())
            }
        }

        /// The hostname property of the URL interface is a string containing either the domain name or IP address of the URL.
        /// If the URL does not have a hostname, this property contains an empty string, "".
        /// IPv4 and IPv6 addresses are normalized, such as stripping leading zeros, and domain names are converted to IDN.
        #[qjs(get, enumerable, rename = "hostname")]
        pub fn get_hostname(&self) -> String {
            self.url
                .host_str()
                .map(|s| s.to_string())
                .unwrap_or_default()
        }

        #[qjs(set, enumerable, rename = "hostname")]
        pub fn set_hostname(
            &mut self,
            value: Coerced<String>,
            ctx: Ctx<'_>,
        ) -> rquickjs::Result<()> {
            let value = value.0;
            if let Err(err) = self.url.set_host(Some(&value)) {
                Err(ctx.throw(
                    Exception::from_message(
                        ctx.clone(),
                        &format!("Failed to set URL hostname: {err}"),
                    )
                    .unwrap()
                    .into(),
                ))
            } else {
                Ok(())
            }
        }

        // The href property of the URL interface is a string containing the whole URL.
        #[qjs(get, enumerable, rename = "href")]
        pub fn get_href(&self) -> String {
            self.url.to_string()
        }

        #[qjs(set, enumerable, rename = "href")]
        pub fn set_href(&mut self, value: Coerced<String>, ctx: Ctx<'_>) -> rquickjs::Result<()> {
            let value = value.0;
            match Url::parse(&value) {
                Ok(url) => {
                    self.url = url;
                    Ok(())
                }
                Err(err) => Err(Exception::throw_type(
                    &ctx,
                    &format!("Failed to set URL href: {err}"),
                )),
            }
        }

        #[qjs(get, enumerable, rename = "origin")]
        pub fn get_origin(&self) -> String {
            self.url.origin().unicode_serialization()
        }

        /// The password property of the URL interface is a string containing the password component of the URL.
        /// If the URL does not have a password, this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "password")]
        pub fn get_password(&self) -> String {
            self.url
                .password()
                .map(|s| s.to_string())
                .unwrap_or_default()
        }

        #[qjs(set, enumerable, rename = "password")]
        pub fn set_password(&mut self, value: Coerced<String>) {
            let value = value.0;
            if value.is_empty() {
                let _ = self.url.set_password(None);
            } else {
                let _ = self.url.set_password(Some(&value));
            }
        }

        /// The pathname property of the URL interface represents a location in a hierarchical structure.
        /// It is a string constructed from a list of path segments, each of which is prefixed by a / character.
        #[qjs(get, enumerable, rename = "pathname")]
        pub fn get_pathname(&self) -> String {
            self.url.path().to_string()
        }

        #[qjs(set, enumerable, rename = "pathname")]
        pub fn set_pathname(&mut self, value: Coerced<String>) {
            let value = value.0;
            self.url.set_path(&value);
        }

        /// The port property of the URL interface is a string containing the port number of the URL.
        /// If the port is the default for the protocol (80 for ws: and http:, 443 for wss: and https:, and 21 for ftp:),
        /// this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "port")]
        pub fn get_port(&self) -> String {
            self.url.port().map(|s| s.to_string()).unwrap_or_default()
        }

        #[qjs(set, enumerable, rename = "port")]
        pub fn set_port(&mut self, value: Coerced<String>, ctx: Ctx<'_>) -> rquickjs::Result<()> {
            let value = value.0;
            if value.is_empty() {
                let _ = self.url.set_port(None);
                Ok(())
            } else {
                match value.parse::<u16>() {
                    Ok(port) => {
                        let _ = self.url.set_port(Some(port));
                        Ok(())
                    }
                    Err(err) => Err(ctx.throw(
                        Exception::from_message(
                            ctx.clone(),
                            &format!("Failed to parse port: {err}"),
                        )
                        .unwrap()
                        .into(),
                    )),
                }
            }
        }

        /// The protocol property of the URL interface is a string containing the protocol or scheme of the URL, including the final ":".
        #[qjs(get, enumerable, rename = "protocol")]
        pub fn get_protocol(&self) -> String {
            format!("{}:", self.url.scheme())
        }

        #[qjs(set, enumerable, rename = "protocol")]
        pub fn set_protocol(&mut self, value: Coerced<String>) {
            let value = value.0;
            let _ = self.url.set_scheme(value.trim_end_matches(':'));
        }

        // The search property of the URL interface is a search string, also called a query string,
        // that is a string containing a "?" followed by the parameters of the URL.
        // If the URL does not have a search query, this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "search")]
        pub fn get_search(&self) -> String {
            self.url
                .query()
                .map(|s| format!("?{s}"))
                .unwrap_or_default()
        }

        #[qjs(set, enumerable, rename = "search")]
        pub fn set_search(&mut self, value: Coerced<String>) {
            let value = value.0;
            if value.is_empty() {
                self.url.set_query(None);
            } else if let Some(s) = value.strip_prefix('?') {
                self.url.set_query(Some(s));
            } else {
                self.url.set_query(Some(&value));
            }
        }

        /// The username property of the URL interface is a string containing the username component of the URL.
        /// If the URL does not have a username, this property contains an empty string, "".
        #[qjs(get, enumerable, rename = "username")]
        pub fn get_username(&self) -> String {
            self.url.username().to_string()
        }

        #[qjs(set, enumerable, rename = "username")]
        pub fn set_username(&mut self, value: Coerced<String>) {
            let value = value.0;
            let _ = self.url.set_username(&value);
        }

        #[qjs(rename = "toJSON")]
        pub fn to_json(&self) -> String {
            self.get_href()
        }

        #[qjs(rename = "toString")]
        #[allow(clippy::inherent_to_string)]
        pub fn to_string(&self) -> String {
            self.get_href()
        }

        #[qjs(static, rename = "canParse")]
        pub fn can_parse(url: Coerced<String>, base: Opt<Coerced<String>>) -> bool {
            let url = url.0;
            let base = Opt(base.0.map(|c| c.0));
            super::parse_url(url, base).is_ok()
        }

        #[qjs(static, rename = "createObjectURL")]
        pub fn create_object_url<'js>(
            object: Value<'js>,
            ctx: Ctx<'js>,
        ) -> rquickjs::Result<String> {
            if !object.is_object() {
                return Err(Exception::throw_type(
                    &ctx,
                    "The argument must be an instance of Blob",
                ));
            }
            let id = uuid::Uuid::new_v4().to_string();
            let url = format!("blob:nodedata:{id}");
            // Store the blob in a global registry
            let global = ctx.globals();
            let registry: rquickjs::Object<'js> = global
                .get::<_, rquickjs::Object>("__blobURLRegistry")
                .unwrap_or_else(|_| {
                    let obj = rquickjs::Object::new(ctx.clone()).unwrap();
                    global.set("__blobURLRegistry", obj.clone()).unwrap();
                    obj
                });
            registry.set(&url as &str, object)?;
            Ok(url)
        }

        #[qjs(static, rename = "parse")]
        pub fn parse(url: Coerced<String>, base: Opt<Coerced<String>>) -> Option<JsUrl> {
            let url = url.0;
            let base = Opt(base.0.map(|c| c.0));
            super::parse_url(url, base).map(|url| Self { url }).ok()
        }

        #[qjs(static, rename = "revokeObjectURL")]
        pub fn revoke_object_url(
            object_url: Coerced<String>,
            ctx: Ctx<'_>,
        ) -> rquickjs::Result<()> {
            let object_url = object_url.0;
            let global = ctx.globals();
            if let Ok(registry) = global.get::<_, rquickjs::Object>("__blobURLRegistry") {
                registry.set(
                    &object_url as &str,
                    rquickjs::Value::new_undefined(ctx.clone()),
                )?;
            }
            Ok(())
        }
    }
}

fn parse_url(raw_url: String, base: Opt<String>) -> Result<Url, String> {
    match Url::parse(&raw_url) {
        Ok(url) => {
            if url.scheme() != "" {
                Ok(url)
            } else {
                parse_url_with_base(raw_url, base.0, None)
            }
        }
        Err(err) => parse_url_with_base(raw_url, base.0, Some(err.to_string())),
    }
}

fn parse_url_with_base(
    url: String,
    base: Option<String>,
    err: Option<String>,
) -> Result<Url, String> {
    if let Some(base) = base {
        match Url::parse(&base) {
            Ok(base_url) => match base_url.join(&url) {
                Ok(url) => Ok(url),
                Err(err) => Err(format!("Failed to join URL with base URL: {err}")),
            },
            Err(err) => Err(format!("Failed to parse base URL: {err}")),
        }
    } else if let Some(err) = err {
        Err(format!("Failed to parse URL: {err}"))
    } else {
        Err("Missing base URL".to_string())
    }
}

// JS implementation: URLSearchParams polyfill + node:url APIs
pub const URL_JS: &str = include_str!("url.js");

// Re-export for aliases
pub const REEXPORT_JS: &str = r#"export * from 'node:url'; export { default } from 'node:url';"#;

// JS code wiring the URL module into the global context
pub const WIRE_JS: &str = r#"
        import { URL as __wasm_rquickjs_URL, URLSearchParams as __wasm_rquickjs_USP } from '__wasm_rquickjs_builtin/url';
        Object.defineProperty(globalThis, 'URL', {
            value: __wasm_rquickjs_URL,
            writable: true,
            enumerable: false,
            configurable: true,
        });
        Object.defineProperty(globalThis, 'URLSearchParams', {
            value: __wasm_rquickjs_USP,
            writable: true,
            enumerable: false,
            configurable: true,
        });
    "#;