diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hero.mjs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index a4ca217..131b796 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -210,6 +210,29 @@ export const interceptorsFn = ({ }; } }, + websocketHandshake: async (req, next) => { + if (!req.upgrade) { + return await next(req); + } + + const { method, headers} = req; + const { isValid, response } = validateUpgrade(method, headers); + if (!isValid) { + return response; + } + + const _response = await next(req); + const hash = computeHash(headers["sec-websocket-key"]); + + return { + status: 101, + headers: { + "Connection": "Upgrade", + "Upgrade": "websocket", + "Sec-WebSocket-Accept": hash, + }, + }; + }, }); export const interceptors = interceptorsFn(); @@ -219,6 +242,7 @@ export const defaultInterceptors = [ interceptors.contentType, interceptors.requestId, interceptors.logged, + interceptors.websocketHandshake, ]; export const chainInterceptors = arr => |