Skip to content

Commit

Permalink
feat(js-runtime): implement proper CloseEvent & MessageEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Jun 4, 2023
1 parent f84b28a commit 9163bcc
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 255 deletions.
4 changes: 2 additions & 2 deletions crates/runtime_isolate/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl PromiseResult {
PromiseResult::Error(error) => v8_string(scope, &error).into(),
PromiseResult::String(str) => v8_string(scope, &str).into(),
PromiseResult::Undefined => v8::undefined(scope).into(),
PromiseResult::WsInfo(ws_id, protocols, extensions) => {
ws_info_to_v8((ws_id, protocols, extensions), scope).into()
PromiseResult::WsInfo(ws_id, protocol, extensions) => {
ws_info_to_v8((ws_id, protocol, extensions), scope).into()
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions crates/runtime_isolate/src/bindings/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ pub fn create_websocket_init(
args: v8::FunctionCallbackArguments,
) -> Result<CreateArg> {
let url = args.get(0).to_rust_string_lossy(scope);
let protocols = args.get(1).to_rust_string_lossy(scope);
let protocol = args.get(1).to_rust_string_lossy(scope);

Ok((url, protocols))
Ok((url, protocol))
}

pub async fn create_websocket_binding<'a>(
Expand All @@ -80,20 +80,20 @@ pub async fn create_websocket_binding<'a>(
arg: CreateArg,
) -> BindingResult {
let url = arg.0;
let protocols = arg.1;
let protocol = arg.1;

let res = new_ws(url, protocols).await;
let res = new_ws(url, protocol).await;

let mut table = table.lock().await;

match res {
Ok((ws, protocols, extensions)) => {
Ok((ws, protocol, extensions)) => {
let ws_id = ws.get_id().to_string();

table.insert(ws.get_id(), ws);
return BindingResult {
id,
result: PromiseResult::WsInfo(ws_id, protocols, extensions),
result: PromiseResult::WsInfo(ws_id, protocol, extensions),
};
}
Err(error) => BindingResult {
Expand Down Expand Up @@ -329,7 +329,7 @@ pub fn ws_info_to_v8<'a>(
names.push(v8_string(scope, "wsId").into());
values.push(v8_string(scope, &ws_info.0).into());

names.push(v8_string(scope, "protocols").into());
names.push(v8_string(scope, "protocol").into());
values.push(v8_string(scope, &ws_info.1).into());

names.push(v8_string(scope, "extensions").into());
Expand Down
6 changes: 3 additions & 3 deletions crates/runtime_websocket/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ pub fn create_default_root_cert_store() -> RootCertStore {
root_cert_store
}

pub async fn new_ws(url: String, protocols: String) -> Result<(Ws, String, String)> {
pub async fn new_ws(url: String, protocol: String) -> Result<(Ws, String, String)> {
let uri: Uri = url.parse()?;
let mut request = Request::builder().method(Method::GET).uri(&uri);

request = request.header("User-Agent", "Lagon/serverless rusty_v8/0.71.2");

if !protocols.is_empty() {
request = request.header("Sec-WebSocket-Protocol", protocols);
if !protocol.is_empty() {
request = request.header("Sec-WebSocket-Protocol", protocol);
}

let request = request.body(())?;
Expand Down
4 changes: 2 additions & 2 deletions packages/js-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import './runtime/global/process';
import './runtime/global/crypto';
import './runtime/global/navigator';
import './runtime/global/timers';
import './runtime/global/webscoket';
import './runtime/global/websocket';
import './runtime/http/URLSearchParams';
import './runtime/http/URL';
import './runtime/http/URLPattern';
Expand Down Expand Up @@ -105,7 +105,7 @@ declare global {
generateKey(
algorithm: RsaHashedKeyGenParams | EcKeyGenParams | HmacKeyGenParams | AesKeyGenParams,
): Promise<ArrayBuffer>;
createWebsocket(url: string, protocols: string): Promise<{ wsId: string; protocols: string; extensions: string }>;
createWebsocket(url: string, protocol: string): Promise<{ wsId: string; protocol: string; extensions: string }>;
websocketEvent(wsId: string): Promise<string | ArrayBuffer>;
websocketClose(wsId: string, code?: number, reason?: string): Promise<void>;
websocketSend(wsId: string, data: string | ArrayBuffer): Promise<void>;
Expand Down
241 changes: 0 additions & 241 deletions packages/js-runtime/src/runtime/global/webscoket.ts

This file was deleted.

Loading

0 comments on commit 9163bcc

Please sign in to comment.