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();
if (ws[_readyState] === WebSocket.CLOSING) {
await core.opAsync("op_ws_close", { rid: wsRid });
await core.opAsync("op_ws_close", wsRid);
ws[_readyState] = WebSocket.CLOSED;

View file

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

View file

@ -145,21 +145,20 @@
};
options.signal?.[add](abort);
PromisePrototypeThen(
core.opAsync("op_ws_create", {
url: this[_url],
protocols: options.protocols
core.opAsync(
"op_ws_create",
this[_url],
options.protocols
? ArrayPrototypeJoin(options.protocols, ", ")
: "",
cancelHandle: cancelRid,
headers: headerListFromHeaders(headers),
}),
cancelRid,
headerListFromHeaders(headers),
),
(create) => {
options.signal?.[remove](abort);
if (this[_earlyClose]) {
PromisePrototypeThen(
core.opAsync("op_ws_close", {
rid: create.rid,
}),
core.opAsync("op_ws_close", create.rid),
() => {
PromisePrototypeThen(
(async () => {
@ -369,11 +368,7 @@
this[_earlyClose] = true;
} else if (this[_closed].state === "pending") {
PromisePrototypeCatch(
core.opAsync("op_ws_close", {
rid: this[_rid],
code,
reason: closeInfo.reason,
}),
core.opAsync("op_ws_close", this[_rid], code, closeInfo.reason),
(err) => {
this[_rid] && core.tryClose(this[_rid]);
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)]
#[serde(rename_all = "camelCase")]
pub struct CreateResponse {
@ -234,7 +225,10 @@ pub struct CreateResponse {
#[op]
pub async fn op_ws_create<WP>(
state: Rc<RefCell<OpState>>,
args: CreateArgs,
url: String,
protocols: String,
cancel_handle: Option<ResourceId>,
headers: Option<Vec<(ByteString, ByteString)>>,
) -> Result<CreateResponse, AnyError>
where
WP: WebSocketPermissions + 'static,
@ -242,13 +236,13 @@ where
{
let mut s = state.borrow_mut();
s.borrow_mut::<WP>()
.check_net_url(&url::Url::parse(&args.url)?)
.check_net_url(&url::Url::parse(&url)?)
.expect(
"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
.borrow_mut()
.resource_table
@ -264,16 +258,16 @@ where
.and_then(|it| it.0.clone());
let root_cert_store = state.borrow().borrow::<WsRootStore>().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);
request = request.header("User-Agent", user_agent);
if !args.protocols.is_empty() {
request = request.header("Sec-WebSocket-Protocol", args.protocols);
if !protocols.is_empty() {
request = request.header("Sec-WebSocket-Protocol", protocols);
}
if let Some(headers) = args.headers {
if let Some(headers) = headers {
for (key, value) in headers {
let name = HeaderName::from_bytes(&key)
.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();
}
@ -401,23 +395,17 @@ pub async fn op_ws_send(
Ok(())
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CloseArgs {
rid: ResourceId,
code: Option<u16>,
reason: Option<String>,
}
#[op]
pub async fn op_ws_close(
state: Rc<RefCell<OpState>>,
args: CloseArgs,
rid: ResourceId,
code: Option<u16>,
reason: Option<String>,
) -> Result<(), AnyError> {
let rid = args.rid;
let msg = Message::Close(args.code.map(|c| CloseFrame {
let rid = rid;
let msg = Message::Close(code.map(|c| CloseFrame {
code: CloseCode::from(c),
reason: match args.reason {
reason: match reason {
Some(reason) => Cow::from(reason),
None => Default::default(),
},