Jelajahi Sumber

@uppy/transloadit: remove Socket.io (#4281)

* @uppy/transloadit: implement Server-sent event API

* pass events from event stream to JS

* @uppy/transloadit: remove Socket.io

Now that SSE is available, WebSockets do not bring much to the table.

---------

Co-authored-by: Artur Paikin <artur@arturpaikin.com>
Antoine du Hamel 1 tahun lalu
induk
melakukan
8a32e8e2e7

+ 1 - 2
packages/@uppy/transloadit/package.json

@@ -32,8 +32,7 @@
     "@uppy/provider-views": "workspace:^",
     "@uppy/tus": "workspace:^",
     "@uppy/utils": "workspace:^",
-    "component-emitter": "^1.2.1",
-    "socket.io-client": "^4.1.3"
+    "component-emitter": "^1.2.1"
   },
   "peerDependencies": {
     "@uppy/core": "workspace:^"

+ 6 - 69
packages/@uppy/transloadit/src/Assembly.js

@@ -1,9 +1,7 @@
 import Emitter from 'component-emitter'
-import { io } from 'socket.io-client'
 import has from '@uppy/utils/lib/hasProperty'
 import NetworkError from '@uppy/utils/lib/NetworkError'
 import fetchWithNetworkError from '@uppy/utils/lib/fetchWithNetworkError'
-import parseUrl from './parseUrl.js'
 
 const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING'
 const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING'
@@ -43,8 +41,6 @@ class TransloaditAssembly extends Emitter {
 
     // The current assembly status.
     this.status = assembly
-    // The socket.io connection.
-    this.socket = null
     // The interval timer for full status updates.
     this.pollInterval = null
     // Whether this assembly has been closed (finished or errored)
@@ -56,7 +52,6 @@ class TransloaditAssembly extends Emitter {
 
   connect () {
     this.#connectServerSentEvents()
-    this.#connectSocket()
     this.#beginPolling()
   }
 
@@ -122,58 +117,6 @@ class TransloaditAssembly extends Emitter {
     })
   }
 
-  #connectSocket () {
-    const parsed = parseUrl(this.status.websocket_url)
-    const socket = io(parsed.origin, {
-      transports: ['websocket'],
-      path: parsed.pathname,
-    })
-
-    socket.on('connect', () => {
-      socket.emit('assembly_connect', {
-        id: this.status.assembly_id,
-      })
-
-      this.emit('connect')
-    })
-
-    socket.on('connect_error', () => {
-      socket.disconnect()
-      this.socket = null
-    })
-
-    socket.on('assembly_finished', () => {
-      this.#onFinished()
-    })
-
-    socket.on('assembly_upload_finished', (file) => {
-      this.emit('upload', file)
-      this.status.uploads.push(file)
-    })
-
-    socket.on('assembly_uploading_finished', () => {
-      this.emit('executing')
-    })
-
-    socket.on('assembly_upload_meta_data_extracted', () => {
-      this.emit('metadata')
-      this.#fetchStatus({ diff: false })
-    })
-
-    socket.on('assembly_result_finished', (stepName, result) => {
-      this.emit('result', stepName, result)
-      ;(this.status.results[stepName] ??= []).push(result)
-    })
-
-    socket.on('assembly_error', (status) => {
-      // Refetch for updated status code
-      this.#fetchStatus({ diff: false })
-      this.#onError(status)
-    })
-
-    this.socket = socket
-  }
-
   #onError (status) {
     this.emit('error', Object.assign(new Error(status.msg), status))
     this.close()
@@ -181,20 +124,18 @@ class TransloaditAssembly extends Emitter {
 
   /**
    * Begin polling for assembly status changes. This sends a request to the
-   * assembly status endpoint every so often, if the socket is not connected.
-   * If the socket connection fails or takes a long time, we won't miss any
+   * assembly status endpoint every so often, if SSE connection failed.
+   * If the SSE connection fails or takes a long time, we won't miss any
    * events.
    */
   #beginPolling () {
     this.pollInterval = setInterval(() => {
-      if (!this.socket || !this.socket.connected) {
-        this.#fetchStatus()
-      }
+      this.#fetchStatus()
     }, 2000)
   }
 
   /**
-   * Reload assembly status. Useful if the socket doesn't work.
+   * Reload assembly status. Useful if SSE doesn't work.
    *
    * Pass `diff: false` to avoid emitting diff events, instead only emitting
    * 'status'.
@@ -276,10 +217,10 @@ class TransloaditAssembly extends Emitter {
     const nowExecuting = isStatus(nextStatus, ASSEMBLY_EXECUTING)
       && !isStatus(prevStatus, ASSEMBLY_EXECUTING)
     if (nowExecuting) {
-      // Without WebSockets, this is our only way to tell if uploading finished.
+      // Without SSE, this is our only way to tell if uploading finished.
       // Hence, we emit this just before the 'upload's and before the 'metadata'
       // event for the most intuitive ordering, corresponding to the _usual_
-      // ordering (if not guaranteed) that you'd get on the WebSocket.
+      // ordering (if not guaranteed) that you'd get on SSE.
       this.emit('executing')
     }
 
@@ -323,10 +264,6 @@ class TransloaditAssembly extends Emitter {
       this.#sse.close()
       this.#sse = null
     }
-    if (this.socket) {
-      this.socket.disconnect()
-      this.socket = null
-    }
     clearInterval(this.pollInterval)
     this.pollInterval = null
   }

+ 0 - 19
packages/@uppy/transloadit/src/parseUrl.js

@@ -1,19 +0,0 @@
-export default function parseUrl (url) {
-  const scheme = /^\w+:\/\//.exec(url)
-  let i = 0
-  if (scheme) {
-    i = scheme[0].length + 1
-  }
-  const slashIndex = url.indexOf('/', i)
-  if (slashIndex === -1) {
-    return {
-      origin: url,
-      pathname: '/',
-    }
-  }
-
-  return {
-    origin: url.slice(0, slashIndex),
-    pathname: url.slice(slashIndex),
-  }
-}

+ 0 - 18
packages/@uppy/transloadit/src/parseUrl.test.js

@@ -1,18 +0,0 @@
-import { describe, expect, it } from '@jest/globals'
-import parseUrl from './parseUrl.js'
-
-describe('Transloadit/parseUrl', () => {
-  it('splits a url into origin and pathname', () => {
-    expect(parseUrl('http://api2.transloadit.com/ws2012')).toEqual({
-      origin: 'http://api2.transloadit.com',
-      pathname: '/ws2012',
-    })
-  })
-
-  it('defaults to pathname=/ if absent', () => {
-    expect(parseUrl('http://api2.transloadit.com')).toEqual({
-      origin: 'http://api2.transloadit.com',
-      pathname: '/',
-    })
-  })
-})

+ 0 - 50
yarn.lock

@@ -7902,13 +7902,6 @@ __metadata:
   languageName: node
   linkType: hard
 
-"@socket.io/component-emitter@npm:~3.1.0":
-  version: 3.1.0
-  resolution: "@socket.io/component-emitter@npm:3.1.0"
-  checksum: db069d95425b419de1514dffe945cc439795f6a8ef5b9465715acf5b8b50798e2c91b8719cbf5434b3fe7de179d6cdcd503c277b7871cb3dd03febb69bdd50fa
-  languageName: node
-  linkType: hard
-
 "@swc/core-darwin-arm64@npm:1.3.49":
   version: 1.3.49
   resolution: "@swc/core-darwin-arm64@npm:1.3.49"
@@ -10046,7 +10039,6 @@ __metadata:
     "@uppy/tus": "workspace:^"
     "@uppy/utils": "workspace:^"
     component-emitter: ^1.2.1
-    socket.io-client: ^4.1.3
     whatwg-fetch: ^3.6.2
   peerDependencies:
     "@uppy/core": "workspace:^"
@@ -14853,19 +14845,6 @@ __metadata:
   languageName: node
   linkType: hard
 
-"engine.io-client@npm:~6.2.1":
-  version: 6.2.2
-  resolution: "engine.io-client@npm:6.2.2"
-  dependencies:
-    "@socket.io/component-emitter": ~3.1.0
-    debug: ~4.3.1
-    engine.io-parser: ~5.0.3
-    ws: ~8.2.3
-    xmlhttprequest-ssl: ~2.0.0
-  checksum: bda989d88d663cda5f1fbe6b235dba35b80ba9e947685b94b40d3daf4545ccdcb54232d2ad210bbe9b5e0b73b0e019b54716d1285ed300e1f1c7ad97fd6cafaf
-  languageName: node
-  linkType: hard
-
 "engine.io-parser@npm:~5.0.3":
   version: 5.0.4
   resolution: "engine.io-parser@npm:5.0.4"
@@ -28621,18 +28600,6 @@ __metadata:
   languageName: node
   linkType: hard
 
-"socket.io-client@npm:^4.1.3":
-  version: 4.5.1
-  resolution: "socket.io-client@npm:4.5.1"
-  dependencies:
-    "@socket.io/component-emitter": ~3.1.0
-    debug: ~4.3.2
-    engine.io-client: ~6.2.1
-    socket.io-parser: ~4.2.0
-  checksum: e6e5ff1bb4b5714195b961274925cf23de81e070258d2ec1c8e12fcd9cebf4b4725c5fcff58699b23de8a260884f272f4e7e1e1146c0c72b75028fc438d069aa
-  languageName: node
-  linkType: hard
-
 "socket.io-parser@npm:~4.0.4":
   version: 4.0.5
   resolution: "socket.io-parser@npm:4.0.5"
@@ -28644,16 +28611,6 @@ __metadata:
   languageName: node
   linkType: hard
 
-"socket.io-parser@npm:~4.2.0":
-  version: 4.2.1
-  resolution: "socket.io-parser@npm:4.2.1"
-  dependencies:
-    "@socket.io/component-emitter": ~3.1.0
-    debug: ~4.3.1
-  checksum: 2582202f22538d7e6b4436991378cb4cea3b2f8219cda24923ae35afd291ab5ad6120e7d093e41738256b6c6ad10c667dd25753c2d9a2340fead04e9286f152d
-  languageName: node
-  linkType: hard
-
 "socket.io@npm:^4.4.1":
   version: 4.5.1
   resolution: "socket.io@npm:4.5.1"
@@ -32331,13 +32288,6 @@ __metadata:
   languageName: node
   linkType: hard
 
-"xmlhttprequest-ssl@npm:~2.0.0":
-  version: 2.0.0
-  resolution: "xmlhttprequest-ssl@npm:2.0.0"
-  checksum: 1e98df67f004fec15754392a131343ea92e6ab5ac4d77e842378c5c4e4fd5b6a9134b169d96842cc19422d77b1606b8df84a5685562b3b698cb68441636f827e
-  languageName: node
-  linkType: hard
-
 "xtend@npm:^4.0.0, xtend@npm:~4.0.1":
   version: 4.0.2
   resolution: "xtend@npm:4.0.2"