Skip to content

Commit

Permalink
init url module
Browse files Browse the repository at this point in the history
  • Loading branch information
imaitland committed Mar 26, 2024
1 parent a011bf6 commit a7d86a1
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 137 deletions.
72 changes: 72 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,78 @@ _Also available globally_

[clearTimeout](https://nodejs.org/api/timers.html#cleartimeouttimeout)

## url
```typescript
export class URL {
constructor(input: string, base?: string | URL);

hash: string;
host: string;
hostname: string;
href: string;
origin: string;
password: string;
pathname: string;
port: string;
protocol: string;
search: string;
searchParams: URLSearchParams;
username: string;

canParse(input: string, base?: string): boolean;
toString(): string;
}
```

### TODO, URL see tracking tickets:
```typescript
// Additional utilities in the URL module
export function domainToASCII(domain: string): string;

export function domainToUnicode(domain: string): string;

export function fileURLToPath(url: string | URL): string;

export function pathToFileURL(path: string): URL;

export function urlToHttpOptions(url: URL): {
protocol?: string;
hostname?: string;
port?: string;
path?: string;
};
```

## URLSearchParams
```typescript
export class URLSearchParams {
constructor(init?: string | string[][] | Record<string, string> | URLSearchParams);

// Methods
append(name: string, value: string): void;
delete(name: string): void;
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
set(name: string, value: string): void;
sort(): void;

[Symbol.iterator](): IterableIterator<[string, string]>;
entries(): IterableIterator<[string, string]>;
values(): IterableIterator<string>;

toString(): string;
}
```


### TODO, URLSearchParams see tracking tickets:

```typescript
URLSearchParams.sort(): void;
URLSearchParams.keys(): IterableIterator<string>;
```

## util

> [!IMPORTANT]
Expand Down
3 changes: 2 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const ES_BUILD_OPTIONS = {
"buffer",
"xml",
"net",
"url"
],
};

Expand Down Expand Up @@ -570,7 +571,7 @@ async function buildLibrary() {
await esbuild.build({
...defaultLibEsBuildOption,
entryPoints: testEntryPoints,
external: [...ES_BUILD_OPTIONS.external, "@aws-sdk", "@smithy", "uuid"],
external: [...ES_BUILD_OPTIONS.external, "@aws-sdk", "@smithy"],
});
}

Expand Down
Binary file added llrt
Binary file not shown.
4 changes: 2 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod fetch;
mod headers;
mod request;
mod response;
mod url;
mod url_search_params;
pub mod url;
pub mod url_search_params;

use rquickjs::{Class, Ctx, Result};

Expand Down
29 changes: 23 additions & 6 deletions src/http/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,31 @@ impl<'js> URL<'js> {
}

#[qjs(static)]
pub fn can_parse(input: Value<'js>) -> bool {
if input.is_string() {
match input.get::<String>() {
Ok(string_val) => Url::parse(&string_val).is_ok(),
Err(_) => false,
pub fn can_parse(ctx: Ctx<'js>, input: Value<'js>, base: Opt<Value<'js>>) -> bool {
if let Some(base) = base.0 {
let base_string = match get_string(&ctx, base) {
Ok(s) => s,
Err(_) => return false,
};
let path_string = match get_string(&ctx, input) {
Ok(s) => s,
Err(_) => return false,
};

match base_string.parse::<Url>() {
Ok(base_url) => base_url.join(&path_string).is_ok(),
Err(_) => false, // Base URL parsing failed
}
} else {
false
// Handle the case where base is not provided
if input.is_string() {
match input.get::<String>() {
Ok(string_val) => Url::parse(&string_val).is_ok(),
Err(_) => false,
}
} else {
false
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod security;
mod stream;
mod test_utils;
mod timers;
mod url;
mod utils;
mod uuid;
mod vm;
Expand Down
32 changes: 32 additions & 0 deletions src/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use rquickjs::{
module::{Declarations, Exports, ModuleDef},
Class, Ctx, Result,
};

use crate::{
http::{url::URL, url_search_params::URLSearchParams},
module::export_default,
};
pub struct UrlModule;

impl ModuleDef for UrlModule {
fn declare(declare: &mut Declarations) -> Result<()> {
declare.declare(stringify!(URL))?;
declare.declare(stringify!(URLSearchParams))?;

declare.declare("default")?;
Ok(())
}

fn evaluate<'js>(ctx: &Ctx<'js>, exports: &mut Exports<'js>) -> Result<()> {
export_default(ctx, exports, |default| {
Class::<URL>::define(default)?;
Class::<URLSearchParams>::define(default)?;
Ok(())
})?;

Ok(())
}
}
4 changes: 3 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use crate::{
path::{dirname, join_path, resolve_path, PathModule},
process::ProcessModule,
timers::TimersModule,
url::UrlModule,
utils::{
class::get_class_name,
clone::structured_clone,
Expand Down Expand Up @@ -128,7 +129,8 @@ create_modules!(
"child_process" => ChildProcessModule,
"util" => UtilModule,
"uuid" => UuidModule,
"process" => ProcessModule
"process" => ProcessModule,
"url" => UrlModule
);

struct ModuleInfo<T: ModuleDef> {
Expand Down
Loading

0 comments on commit a7d86a1

Please sign in to comment.