Bladeren bron

@uppy/companion-client: remove `Socket` (#5198)

Mikael Finstad 11 maanden geleden
bovenliggende
commit
2db071c893

+ 5 - 0
docs/guides/migration-guides.md

@@ -17,6 +17,11 @@ These cover all the major Uppy versions and how to migrate to them.
   - The static `getExtraConfig` property has been renamed to
   - The static `getExtraConfig` property has been renamed to
     `getExtraGrantConfig`.
     `getExtraGrantConfig`.
 
 
+### `@uppy/companion-client`
+
+- `Socket` class is no longer in use and has been removed. Unless you used this
+  class you don’t need to do anything.
+
 ## Migrate from Robodog to Uppy plugins
 ## Migrate from Robodog to Uppy plugins
 
 
 Uppy is flexible and extensible through plugins. But the integration code could
 Uppy is flexible and extensible through plugins. But the integration code could

+ 0 - 202
packages/@uppy/companion-client/src/Socket.test.ts

@@ -1,202 +0,0 @@
-import {
-  afterEach,
-  beforeEach,
-  vi,
-  describe,
-  it,
-  expect,
-  type Mock,
-} from 'vitest'
-import UppySocket from './Socket.ts'
-
-describe('Socket', () => {
-  let webSocketConstructorSpy: Mock
-  let webSocketCloseSpy: Mock
-  let webSocketSendSpy: Mock
-
-  beforeEach(() => {
-    webSocketConstructorSpy = vi.fn()
-    webSocketCloseSpy = vi.fn()
-    webSocketSendSpy = vi.fn()
-
-    // @ts-expect-error WebSocket expects a lot more to be present but we don't care for this test
-    globalThis.WebSocket = class WebSocket {
-      constructor(target: string) {
-        webSocketConstructorSpy(target)
-      }
-
-      // eslint-disable-next-line class-methods-use-this
-      close(args: any) {
-        webSocketCloseSpy(args)
-      }
-
-      // eslint-disable-next-line class-methods-use-this
-      send(json: any) {
-        webSocketSendSpy(json)
-      }
-
-      triggerOpen() {
-        // @ts-expect-error exist
-        this.onopen()
-      }
-
-      triggerClose() {
-        // @ts-expect-error exist
-        this.onclose()
-      }
-    }
-  })
-  afterEach(() => {
-    // @ts-expect-error not allowed but needed for test
-    globalThis.WebSocket = undefined
-  })
-
-  it('should expose a class', () => {
-    expect(UppySocket.name).toEqual('UppySocket')
-    expect(
-      new UppySocket({
-        target: 'foo',
-      }) instanceof UppySocket,
-    )
-  })
-
-  it('should setup a new WebSocket', () => {
-    new UppySocket({ target: 'foo' }) // eslint-disable-line no-new
-    expect(webSocketConstructorSpy.mock.calls[0][0]).toEqual('foo')
-  })
-
-  it('should send a message via the websocket if the connection is open', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-    // @ts-expect-error not allowed but needed for test
-    const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
-    webSocketInstance.triggerOpen()
-
-    uppySocket.send('bar', 'boo')
-    expect(webSocketSendSpy.mock.calls.length).toEqual(1)
-    expect(webSocketSendSpy.mock.calls[0]).toEqual([
-      JSON.stringify({ action: 'bar', payload: 'boo' }),
-    ])
-  })
-
-  it('should queue the message for the websocket if the connection is not open', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-
-    uppySocket.send('bar', 'boo')
-    // @ts-expect-error not allowed but needed for test
-    expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([
-      { action: 'bar', payload: 'boo' },
-    ])
-    expect(webSocketSendSpy.mock.calls.length).toEqual(0)
-  })
-
-  it('should queue any messages for the websocket if the connection is not open, then send them when the connection is open', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-    // @ts-expect-error not allowed but needed for test
-    const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
-
-    uppySocket.send('bar', 'boo')
-    uppySocket.send('moo', 'baa')
-    // @ts-expect-error not allowed but needed for test
-    expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([
-      { action: 'bar', payload: 'boo' },
-      { action: 'moo', payload: 'baa' },
-    ])
-    expect(webSocketSendSpy.mock.calls.length).toEqual(0)
-
-    webSocketInstance.triggerOpen()
-
-    // @ts-expect-error not allowed but needed for test
-    expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
-    expect(webSocketSendSpy.mock.calls.length).toEqual(2)
-    expect(webSocketSendSpy.mock.calls[0]).toEqual([
-      JSON.stringify({ action: 'bar', payload: 'boo' }),
-    ])
-    expect(webSocketSendSpy.mock.calls[1]).toEqual([
-      JSON.stringify({ action: 'moo', payload: 'baa' }),
-    ])
-  })
-
-  it('should start queuing any messages when the websocket connection is closed', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-    // @ts-expect-error not allowed but needed for test
-    const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
-    webSocketInstance.triggerOpen()
-    uppySocket.send('bar', 'boo')
-    // @ts-expect-error not allowed but needed for test
-    expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([])
-
-    webSocketInstance.triggerClose()
-    uppySocket.send('bar', 'boo')
-    // @ts-expect-error not allowed but needed for test
-    expect(uppySocket[Symbol.for('uppy test: getQueued')]()).toEqual([
-      { action: 'bar', payload: 'boo' },
-    ])
-  })
-
-  it('should close the websocket when it is force closed', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-    // @ts-expect-error not allowed but needed for test
-    const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
-    webSocketInstance.triggerOpen()
-
-    uppySocket.close()
-    expect(webSocketCloseSpy.mock.calls.length).toEqual(1)
-  })
-
-  it('should be able to subscribe to messages received on the websocket', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-    // @ts-expect-error not allowed but needed for test
-    const webSocketInstance = uppySocket[Symbol.for('uppy test: getSocket')]()
-
-    const emitterListenerMock = vi.fn()
-    uppySocket.on('hi', emitterListenerMock)
-
-    webSocketInstance.triggerOpen()
-    webSocketInstance.onmessage({
-      data: JSON.stringify({ action: 'hi', payload: 'ho' }),
-    })
-    expect(emitterListenerMock.mock.calls).toEqual([
-      ['ho', undefined, undefined, undefined, undefined, undefined],
-    ])
-  })
-
-  it('should be able to emit messages and subscribe to them', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-
-    const emitterListenerMock = vi.fn()
-    uppySocket.on('hi', emitterListenerMock)
-
-    uppySocket.emit('hi', 'ho')
-    uppySocket.emit('hi', 'ho')
-    uppySocket.emit('hi', 'off to work we go')
-
-    expect(emitterListenerMock.mock.calls).toEqual([
-      ['ho', undefined, undefined, undefined, undefined, undefined],
-      ['ho', undefined, undefined, undefined, undefined, undefined],
-      [
-        'off to work we go',
-        undefined,
-        undefined,
-        undefined,
-        undefined,
-        undefined,
-      ],
-    ])
-  })
-
-  it('should be able to subscribe to the first event for a particular action', () => {
-    const uppySocket = new UppySocket({ target: 'foo' })
-
-    const emitterListenerMock = vi.fn()
-    uppySocket.once('hi', emitterListenerMock)
-
-    uppySocket.emit('hi', 'ho')
-    uppySocket.emit('hi', 'ho')
-    uppySocket.emit('hi', 'off to work we go')
-
-    expect(emitterListenerMock.mock.calls.length).toEqual(1)
-    expect(emitterListenerMock.mock.calls).toEqual([
-      ['ho', undefined, undefined, undefined, undefined, undefined],
-    ])
-  })
-})

+ 0 - 107
packages/@uppy/companion-client/src/Socket.ts

@@ -1,107 +0,0 @@
-// eslint-disable-next-line @typescript-eslint/ban-ts-comment
-// @ts-ignore no types
-import ee from 'namespace-emitter'
-
-type Opts = {
-  autoOpen?: boolean
-  target: string
-}
-
-export default class UppySocket {
-  #queued: Array<{ action: string; payload: unknown }> = []
-
-  #emitter = ee()
-
-  #isOpen = false
-
-  #socket: WebSocket | null
-
-  opts: Opts
-
-  constructor(opts: Opts) {
-    this.opts = opts
-
-    if (!opts || opts.autoOpen !== false) {
-      this.open()
-    }
-  }
-
-  get isOpen(): boolean {
-    return this.#isOpen
-  }
-
-  private [Symbol.for('uppy test: getSocket')](): WebSocket | null {
-    return this.#socket
-  }
-
-  private [Symbol.for('uppy test: getQueued')](): Array<{
-    action: string
-    payload: unknown
-  }> {
-    return this.#queued
-  }
-
-  open(): void {
-    if (this.#socket != null) return
-
-    this.#socket = new WebSocket(this.opts.target)
-
-    this.#socket.onopen = () => {
-      this.#isOpen = true
-
-      while (this.#queued.length > 0 && this.#isOpen) {
-        const first = this.#queued.shift()!
-        this.send(first.action, first.payload)
-      }
-    }
-
-    this.#socket.onclose = () => {
-      this.#isOpen = false
-      this.#socket = null
-    }
-
-    this.#socket.onmessage = this.#handleMessage
-  }
-
-  close(): void {
-    this.#socket?.close()
-  }
-
-  send(action: string, payload: unknown): void {
-    // attach uuid
-
-    if (!this.#isOpen) {
-      this.#queued.push({ action, payload })
-      return
-    }
-
-    this.#socket!.send(
-      JSON.stringify({
-        action,
-        payload,
-      }),
-    )
-  }
-
-  on(action: string, handler: () => void): void {
-    this.#emitter.on(action, handler)
-  }
-
-  emit(action: string, payload: unknown): void {
-    this.#emitter.emit(action, payload)
-  }
-
-  once(action: string, handler: () => void): void {
-    this.#emitter.once(action, handler)
-  }
-
-  #handleMessage = (e: MessageEvent<any>) => {
-    try {
-      const message = JSON.parse(e.data)
-      this.emit(message.action, message.payload)
-    } catch (err) {
-      // TODO: use a more robust error handler.
-      console.log(err) // eslint-disable-line no-console
-    }
-  }
-}

+ 0 - 3
packages/@uppy/companion-client/src/index.ts

@@ -11,6 +11,3 @@ export { default as getAllowedHosts } from './getAllowedHosts.ts'
 export * as tokenStorage from './tokenStorage.ts'
 export * as tokenStorage from './tokenStorage.ts'
 
 
 export type { CompanionPluginOptions } from './CompanionPluginOptions.ts'
 export type { CompanionPluginOptions } from './CompanionPluginOptions.ts'
-
-// TODO: remove in the next major
-export { default as Socket } from './Socket.ts'