1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

chore(ext/websocket): custom arity (#14202)

This commit is contained in:
Divy Srivastava 2022-04-04 15:19:50 +05:30 committed by GitHub
parent cc49b5e0d8
commit 995d1666ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 61 deletions

View file

@ -328,7 +328,7 @@
httpConn.close(); httpConn.close();
if (ws[_readyState] === WebSocket.CLOSING) { if (ws[_readyState] === WebSocket.CLOSING) {
await core.opAsync("op_ws_close", { rid: wsRid }); await core.opAsync("op_ws_close", wsRid);
ws[_readyState] = WebSocket.CLOSED; ws[_readyState] = WebSocket.CLOSED;

View file

@ -223,10 +223,11 @@
} }
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_create", { core.opAsync(
url: wsURL.href, "op_ws_create",
protocols: ArrayPrototypeJoin(protocols, ", "), wsURL.href,
}), ArrayPrototypeJoin(protocols, ", "),
),
(create) => { (create) => {
this[_rid] = create.rid; this[_rid] = create.rid;
this[_extensions] = create.extensions; this[_extensions] = create.extensions;
@ -234,9 +235,7 @@
if (this[_readyState] === CLOSING) { if (this[_readyState] === CLOSING) {
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_close", { core.opAsync("op_ws_close", this[_rid]),
rid: this[_rid],
}),
() => { () => {
this[_readyState] = CLOSED; this[_readyState] = CLOSED;
@ -369,11 +368,7 @@
this[_readyState] = CLOSING; this[_readyState] = CLOSING;
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_close", { core.opAsync("op_ws_close", this[_rid], code, reason),
rid: this[_rid],
code,
reason,
}),
() => { () => {
this[_readyState] = CLOSED; this[_readyState] = CLOSED;
const event = new CloseEvent("close", { const event = new CloseEvent("close", {
@ -473,11 +468,7 @@
this[_idleTimeoutTimeout] = setTimeout(async () => { this[_idleTimeoutTimeout] = setTimeout(async () => {
this[_readyState] = CLOSING; this[_readyState] = CLOSING;
const reason = "No response from ping frame."; const reason = "No response from ping frame.";
await core.opAsync("op_ws_close", { await core.opAsync("op_ws_close", this[_rid], 1001, reason);
rid: this[_rid],
code: 1001,
reason,
});
this[_readyState] = CLOSED; this[_readyState] = CLOSED;
const errEvent = new ErrorEvent("error", { const errEvent = new ErrorEvent("error", {

View file

@ -145,21 +145,20 @@
}; };
options.signal?.[add](abort); options.signal?.[add](abort);
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_create", { core.opAsync(
url: this[_url], "op_ws_create",
protocols: options.protocols this[_url],
options.protocols
? ArrayPrototypeJoin(options.protocols, ", ") ? ArrayPrototypeJoin(options.protocols, ", ")
: "", : "",
cancelHandle: cancelRid, cancelRid,
headers: headerListFromHeaders(headers), headerListFromHeaders(headers),
}), ),
(create) => { (create) => {
options.signal?.[remove](abort); options.signal?.[remove](abort);
if (this[_earlyClose]) { if (this[_earlyClose]) {
PromisePrototypeThen( PromisePrototypeThen(
core.opAsync("op_ws_close", { core.opAsync("op_ws_close", create.rid),
rid: create.rid,
}),
() => { () => {
PromisePrototypeThen( PromisePrototypeThen(
(async () => { (async () => {
@ -369,11 +368,7 @@
this[_earlyClose] = true; this[_earlyClose] = true;
} else if (this[_closed].state === "pending") { } else if (this[_closed].state === "pending") {
PromisePrototypeCatch( PromisePrototypeCatch(
core.opAsync("op_ws_close", { core.opAsync("op_ws_close", this[_rid], code, closeInfo.reason),
rid: this[_rid],
code,
reason: closeInfo.reason,
}),
(err) => { (err) => {
this[_rid] && core.tryClose(this[_rid]); this[_rid] && core.tryClose(this[_rid]);
this[_closed].reject(err); this[_closed].reject(err);

View file

@ -214,15 +214,6 @@ where
} }
} }
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateArgs {
url: String,
protocols: String,
cancel_handle: Option<ResourceId>,
headers: Option<Vec<(ByteString, ByteString)>>,
}
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct CreateResponse { pub struct CreateResponse {
@ -234,7 +225,10 @@ pub struct CreateResponse {
#[op] #[op]
pub async fn op_ws_create<WP>( pub async fn op_ws_create<WP>(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
args: CreateArgs, url: String,
protocols: String,
cancel_handle: Option<ResourceId>,
headers: Option<Vec<(ByteString, ByteString)>>,
) -> Result<CreateResponse, AnyError> ) -> Result<CreateResponse, AnyError>
where where
WP: WebSocketPermissions + 'static, WP: WebSocketPermissions + 'static,
@ -242,13 +236,13 @@ where
{ {
let mut s = state.borrow_mut(); let mut s = state.borrow_mut();
s.borrow_mut::<WP>() s.borrow_mut::<WP>()
.check_net_url(&url::Url::parse(&args.url)?) .check_net_url(&url::Url::parse(&url)?)
.expect( .expect(
"Permission check should have been done in op_ws_check_permission", "Permission check should have been done in op_ws_check_permission",
); );
} }
let cancel_resource = if let Some(cancel_rid) = args.cancel_handle { let cancel_resource = if let Some(cancel_rid) = cancel_handle {
let r = state let r = state
.borrow_mut() .borrow_mut()
.resource_table .resource_table
@ -264,16 +258,16 @@ where
.and_then(|it| it.0.clone()); .and_then(|it| it.0.clone());
let root_cert_store = state.borrow().borrow::<WsRootStore>().0.clone(); let root_cert_store = state.borrow().borrow::<WsRootStore>().0.clone();
let user_agent = state.borrow().borrow::<WsUserAgent>().0.clone(); let user_agent = state.borrow().borrow::<WsUserAgent>().0.clone();
let uri: Uri = args.url.parse()?; let uri: Uri = url.parse()?;
let mut request = Request::builder().method(Method::GET).uri(&uri); let mut request = Request::builder().method(Method::GET).uri(&uri);
request = request.header("User-Agent", user_agent); request = request.header("User-Agent", user_agent);
if !args.protocols.is_empty() { if !protocols.is_empty() {
request = request.header("Sec-WebSocket-Protocol", args.protocols); request = request.header("Sec-WebSocket-Protocol", protocols);
} }
if let Some(headers) = args.headers { if let Some(headers) = headers {
for (key, value) in headers { for (key, value) in headers {
let name = HeaderName::from_bytes(&key) let name = HeaderName::from_bytes(&key)
.map_err(|err| type_error(err.to_string()))?; .map_err(|err| type_error(err.to_string()))?;
@ -339,7 +333,7 @@ where
)) ))
})?; })?;
if let Some(cancel_rid) = args.cancel_handle { if let Some(cancel_rid) = cancel_handle {
state.borrow_mut().resource_table.close(cancel_rid).ok(); state.borrow_mut().resource_table.close(cancel_rid).ok();
} }
@ -401,23 +395,17 @@ pub async fn op_ws_send(
Ok(()) Ok(())
} }
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CloseArgs {
rid: ResourceId,
code: Option<u16>,
reason: Option<String>,
}
#[op] #[op]
pub async fn op_ws_close( pub async fn op_ws_close(
state: Rc<RefCell<OpState>>, state: Rc<RefCell<OpState>>,
args: CloseArgs, rid: ResourceId,
code: Option<u16>,
reason: Option<String>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let rid = args.rid; let rid = rid;
let msg = Message::Close(args.code.map(|c| CloseFrame { let msg = Message::Close(code.map(|c| CloseFrame {
code: CloseCode::from(c), code: CloseCode::from(c),
reason: match args.reason { reason: match reason {
Some(reason) => Cow::from(reason), Some(reason) => Cow::from(reason),
None => Default::default(), None => Default::default(),
}, },