Skip to content

Commit

Permalink
fix(cli,js-runtime): allow any protocols for URL parsing (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz authored May 6, 2023
1 parent 3101c9e commit d6e39ae
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-dolls-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/runtime': patch
---

Improve Request/Response error messages
6 changes: 6 additions & 0 deletions .changeset/plenty-toys-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lagon/cli': patch
'@lagon/js-runtime': patch
---

Allow any protocols for URL parsing
8 changes: 1 addition & 7 deletions crates/runtime_http/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,10 @@ impl TryFrom<&Request> for http::request::Builder {
.uri(&request.url)
.method::<&str>(request.method.into());

let builder_headers = match builder.headers_mut() {
Some(headers) => headers,
None => return Err(anyhow!("Invalid headers")),
};

if let Some(headers) = &request.headers {
for (key, value) in headers {
for value in value {
builder_headers
.append(HeaderName::from_str(key)?, HeaderValue::from_str(value)?);
builder = builder.header(key, value);
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions crates/runtime_http/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use anyhow::{anyhow, Result};
use hyper::{
body::{self, Bytes},
header::HeaderName,
http::{self, HeaderValue},
Body, Response as HyperResponse,
http, Body, Response as HyperResponse,
};
use lagon_runtime_v8_utils::{
extract_v8_headers_object, extract_v8_integer, extract_v8_string, v8_headers_object,
v8_integer, v8_string,
};
use std::str::FromStr;

use crate::{FromV8, Headers, IntoV8};

Expand Down Expand Up @@ -129,16 +126,10 @@ impl TryFrom<&Response> for http::response::Builder {
fn try_from(response: &Response) -> Result<Self, Self::Error> {
let mut builder = HyperResponse::builder().status(response.status);

let builder_headers = match builder.headers_mut() {
Some(headers) => headers,
None => return Err(anyhow!("Invalid headers")),
};

if let Some(headers) = &response.headers {
for (key, value) in headers {
for value in value {
builder_headers
.append(HeaderName::from_str(key)?, HeaderValue::from_str(value)?);
builder = builder.header(key, value);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/js-runtime/src/__tests__/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,26 @@ describe('URL', () => {
expect(url.href).toEqual('https://foo:[email protected]/en-US/docs/Web/API/URL/password');
});
});

describe('any protocol', () => {
it('should work with mysql protocol', () => {
const url = new URL('mysql://user:[email protected]/db?sslaccept=strict');
expect(url.protocol).toEqual('mysql:');
expect(url.username).toEqual('user');
expect(url.password).toEqual('pass');
expect(url.host).toEqual('my.host');
expect(url.pathname).toEqual('/db');
expect(url.search).toEqual('?sslaccept=strict');
});

it('should work with postgres protocol', () => {
const url = new URL('postgres://user:[email protected]/db');
expect(url.protocol).toEqual('postgres:');
expect(url.username).toEqual('user');
expect(url.password).toEqual('pass');
expect(url.host).toEqual('my.host');
expect(url.pathname).toEqual('/db');
expect(url.search).toEqual('');
});
});
});
2 changes: 1 addition & 1 deletion packages/js-runtime/src/runtime/http/URL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

const result =
// eslint-disable-next-line
/((?:blob|file):)?(https?\:)\/\/(?:(.*):(.*)@)?([^:\/?#]*)(?:\:([0-9]+))?([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/.exec(
/((?:blob|file):)?([a-z]+\:)\/\/(?:(.*):(.*)@)?([^:\/?#]*)(?:\:([0-9]+))?([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/.exec(
finalUrl,
);

Expand Down

0 comments on commit d6e39ae

Please sign in to comment.