FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code-server/src/node/wsRouter.ts at asher/enable-webkit · coder/code-server · GitHub
coder
code-server
Repository navigation
Code
Issues
144
(144)
Pull requests
14
(14)
Discussions
Actions
Projects
Security and quality
1
(1)
Insights
Expand file tree
Breadcrumbs
code-server
/
src
/
node
/
wsRouter.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (58 loc) · 1.8 KB
Breadcrumbs
code-server
/
src
/
node
/
wsRouter.ts
Copy path
File metadata and controls
68 lines (58 loc) · 1.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import
*
as
express
from
"express"
import
*
as
expressCore
from
"express-serve-static-core"
import
*
as
http
from
"http"
import
*
as
stream
from
"stream"
import
Websocket
from
"ws"
export
interface
WebsocketRequest
extends
express
.
Request
{
ws
:
stream
.
Duplex
head
:
Buffer
}
interface
InternalWebsocketRequest
extends
WebsocketRequest
{
_ws_handled
:
boolean
}
export
const
handleUpgrade
=
(
app
:
express
.
Express
,
server
:
http
.
Server
)
:
void
=>
{
server
.
on
(
"upgrade"
,
(
req
,
socket
,
head
)
=>
{
socket
.
pause
(
)
const
wreq
=
req
as
InternalWebsocketRequest
wreq
.
ws
=
socket
wreq
.
head
=
head
wreq
.
_ws_handled
=
false
// Send the request off to be handled by Express.
;
(
app
as
any
)
.
handle
(
wreq
,
new
http
.
ServerResponse
(
wreq
)
,
(
)
=>
{
if
(
!
wreq
.
_ws_handled
)
{
socket
.
end
(
"HTTP/1.1 404 Not Found\r\n\r\n"
)
}
}
)
}
)
}
export
type
WebSocketHandler
=
(
req
:
WebsocketRequest
,
res
:
express
.
Response
,
next
:
express
.
NextFunction
,
)
=>
void
|
Promise
<
void
>
export
class
WebsocketRouter
{
public
readonly
router
=
express
.
Router
(
)
/**
* Handle a websocket at this route. Note that websockets are immediately
* paused when they come in.
*
* If the origin header exists it must match the host or the connection will
* be prevented.
*/
public
ws
(
route
:
expressCore
.
PathParams
,
...
handlers
:
WebSocketHandler
[
]
)
:
void
{
this
.
router
.
get
(
route
,
...
handlers
.
map
(
(
handler
)
=>
{
const
wrapped
:
express
.
Handler
=
(
req
,
res
,
next
)
=>
{
;
(
req
as
InternalWebsocketRequest
)
.
_ws_handled
=
true
return
handler
(
req
as
WebsocketRequest
,
res
,
next
)
}
return
wrapped
}
)
,
)
}
}
export
function
Router
(
)
:
WebsocketRouter
{
return
new
WebsocketRouter
(
)
}
export
const
wss
=
new
Websocket
.
Server
(
{
noServer
:
true
}
)
Back
|
FazBrowse Home
|
New Git URL