Skip to content
Myr

fix(client): fix websocket client protocol (#2479)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* chore(client): create replaceUrlProtocol * chore(client): fix websocket client url protocol bug * chore(deno_dist): generate deno code * chore(client): add test * chore(client): change test case description * chore(client): add https, wss test

authored by naporitan · committed by GitHub · parent b0d9417 · Browse files · 6 files+131 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+131 added−7 removed

  • tests:+89 added−1 removed
  • other:+42 added−6 removed

6 files+131 added−7 removed

modified:deno_dist/client/client.ts+12 added-3 removed
modified:deno_dist/client/utils.ts+9 added
13 unmodified lines
14 }15 return urlString16}1718export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {19 switch (protocol) {20 case 'ws':21 return urlString.replace(/^http/, 'ws')22 case 'http':23 return urlString.replace(/^ws/, 'http')24 }25}2627export const removeIndexString = (urlSting: string) => {28 return urlSting.replace(/\/index$/, '')29}30
21 unmodified lines
modified:src/client/client.test.ts+56 added
2 unmodified lines
3/* eslint-disable @typescript-eslint/ban-ts-comment */4import { rest } from 'msw'5import { setupServer } from 'msw/node'6import { expectTypeOf, vi } from 'vitest'7import { upgradeWebSocket } from '../helper'8import { Hono } from '../hono'9import { parse } from '../utils/cookie'10import type { Equal, Expect } from '../utils/types'11import { validator } from '../validator'
674 unmodified lines
686 const data = await res.json()687 expect(data.requestDynamic).toEqual('two')688 })689})690691describe('WebSocket URL Protocol Translation', () => {692 const app = new Hono()693 const route = app.get(694 '/',695 upgradeWebSocket((c) => ({696 onMessage(event, ws) {697 console.log(`Message from client: ${event.data}`)698 ws.send('Hello from server!')699 },700 onClose: () => {701 console.log('Connection closed')702 },703 }))704 )705706 type AppType = typeof route707708 const server = setupServer()709 const webSocketMock = vi.fn()710711 beforeAll(() => server.listen())712 beforeEach(() => {713 vi.stubGlobal('WebSocket', webSocketMock)714 })715 afterEach(() => {716 vi.clearAllMocks()717 server.resetHandlers()718 })719 afterAll(() => server.close())720721 it('Translates HTTP to ws', async () => {722 const client = hc<AppType>('http://localhost')723 client.index.$ws()724 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')725 })726727 it('Translates HTTPS to wss', async () => {728 const client = hc<AppType>('https://localhost')729 client.index.$ws()730 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')731 })732733 it('Keeps ws unchanged', async () => {734 const client = hc<AppType>('ws://localhost')735 client.index.$ws()736 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index')737 })738739 it('Keeps wss unchanged', async () => {740 const client = hc<AppType>('wss://localhost')741 client.index.$ws()742 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')743 })744})
modified:src/client/client.ts+12 added-3 removed
1 unmodified line
2import type { ValidationTargets } from '../types'3import { serialize } from '../utils/cookie'4import type { UnionToIntersection } from '../utils/types'5import type { Callback, Client, ClientRequestOptions } from './types'6import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './utils'6import {7 deepMerge,8 mergePath,9 removeIndexString,10 replaceUrlParam,11 replaceUrlProtocol,12} from './utils'1314const createProxy = (callback: Callback, path: string[]) => {15 const proxy: unknown = new Proxy(() => {}, {16 get(_obj, key) {
135 unmodified lines
152 }153 return new URL(url)154 }155 if (method === 'ws') {150 const targetUrl =151 opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url156 const targetUrl = replaceUrlProtocol(157 opts.args[0] && opts.args[0].param ? replaceUrlParam(url, opts.args[0].param) : url,158 'ws'159 )160161 return new WebSocket(targetUrl)162 }163164 const req = new ClientRequestImpl(url, method)
7 unmodified lines
modified:src/client/utils.test.ts+33 added-1 removed
1import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './utils'1import {2 deepMerge,3 mergePath,4 removeIndexString,5 replaceUrlParam,6 replaceUrlProtocol,7} from './utils'89describe('mergePath', () => {10 it('Should merge paths correctly', () => {11 expect(mergePath('http://localhost', '/api')).toBe('http://localhost/api')
35 unmodified lines
47 expect(replacedUrl).toBe('http://localhost/year/2024/month/2')48 })49})5051describe('replaceUrlProtocol', () => {52 it('Should replace http to ws', () => {53 const url = 'http://localhost'54 const newUrl = replaceUrlProtocol(url, 'ws')55 expect(newUrl).toBe('ws://localhost')56 })5758 it('Should replace https to wss', () => {59 const url = 'https://localhost'60 const newUrl = replaceUrlProtocol(url, 'ws')61 expect(newUrl).toBe('wss://localhost')62 })6364 it('Should replace ws to http', () => {65 const url = 'ws://localhost'66 const newUrl = replaceUrlProtocol(url, 'http')67 expect(newUrl).toBe('http://localhost')68 })6970 it('Should replace wss to https', () => {71 const url = 'wss://localhost'72 const newUrl = replaceUrlProtocol(url, 'http')73 expect(newUrl).toBe('https://localhost')74 })75})7677describe('removeIndexString', () => {78 it('Should remove last `/index` string', () => {79 let url = 'http://localhost/index'80 let newUrl = removeIndexString(url)
37 unmodified lines
modified:src/client/utils.ts+9 added
13 unmodified lines
14 }15 return urlString16}1718export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {19 switch (protocol) {20 case 'ws':21 return urlString.replace(/^http/, 'ws')22 case 'http':23 return urlString.replace(/^ws/, 'http')24 }25}2627export const removeIndexString = (urlSting: string) => {28 return urlSting.replace(/\/index$/, '')29}30
21 unmodified lines