/
Assembly.js
285 lines (239 loc) · 7.68 KB
/
Assembly.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import Emitter from 'component-emitter'
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'
// We used to lazy load socket.io to avoid a console error
// in IE 10 when the Transloadit plugin is not used.
// (The console.error call comes from `buffer`. I
// think we actually don't use that part of socket.io
// at all…)
// TODO: remove this hack in the next release.
let socketIo
function requireSocketIo () {
// eslint-disable-next-line no-return-assign, no-restricted-globals, global-require, no-undef
return socketIo ??= require('socket.io-client')
}
const ASSEMBLY_UPLOADING = 'ASSEMBLY_UPLOADING'
const ASSEMBLY_EXECUTING = 'ASSEMBLY_EXECUTING'
const ASSEMBLY_COMPLETED = 'ASSEMBLY_COMPLETED'
const statusOrder = [
ASSEMBLY_UPLOADING,
ASSEMBLY_EXECUTING,
ASSEMBLY_COMPLETED,
]
/**
* Check that an assembly status is equal to or larger than some desired status.
* It checks for things that are larger so that a comparison like this works,
* when the old assembly status is UPLOADING but the new is FINISHED:
*
* !isStatus(oldStatus, ASSEMBLY_EXECUTING) && isStatus(newState, ASSEMBLY_EXECUTING)
*
* …so that we can emit the 'executing' event even if the execution step was so
* fast that we missed it.
*/
function isStatus (status, test) {
return statusOrder.indexOf(status) >= statusOrder.indexOf(test)
}
class TransloaditAssembly extends Emitter {
#rateLimitedQueue
#fetchWithNetworkError
#previousFetchStatusStillPending = false
constructor (assembly, rateLimitedQueue) {
super()
// 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)
this.closed = false
this.#rateLimitedQueue = rateLimitedQueue
this.#fetchWithNetworkError = rateLimitedQueue.wrapPromiseFunction(fetchWithNetworkError)
}
connect () {
this.#connectSocket()
this.#beginPolling()
}
#onFinished () {
this.emit('finished')
this.close()
}
#connectSocket () {
const parsed = parseUrl(this.status.websocket_url)
const socket = requireSocketIo().connect(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)
if (!this.status.results[stepName]) {
this.status.results[stepName] = []
}
this.status.results[stepName].push(result)
})
socket.on('assembly_error', (err) => {
this.#onError(err)
// Refetch for updated status code
this.#fetchStatus({ diff: false })
})
this.socket = socket
}
#onError (err) {
this.emit('error', Object.assign(new Error(err.message), err))
this.close()
}
/**
* 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
* events.
*/
#beginPolling () {
this.pollInterval = setInterval(() => {
if (!this.socket || !this.socket.connected) {
this.#fetchStatus()
}
}, 2000)
}
/**
* Reload assembly status. Useful if the socket doesn't work.
*
* Pass `diff: false` to avoid emitting diff events, instead only emitting
* 'status'.
*/
async #fetchStatus ({ diff = true } = {}) {
if (this.closed || this.#rateLimitedQueue.isPaused || this.#previousFetchStatusStillPending) return
try {
this.#previousFetchStatusStillPending = true
const response = await this.#fetchWithNetworkError(this.status.assembly_ssl_url)
this.#previousFetchStatusStillPending = false
if (this.closed) return
if (response.status === 429) {
this.#rateLimitedQueue.rateLimit(2_000)
return
}
if (!response.ok) {
this.#onError(new NetworkError(response.statusText))
return
}
const status = await response.json()
// Avoid updating if we closed during this request's lifetime.
if (this.closed) return
this.emit('status', status)
if (diff) {
this.updateStatus(status)
} else {
this.status = status
}
} catch (err) {
this.#onError(err)
}
}
update () {
return this.#fetchStatus({ diff: true })
}
/**
* Update this assembly's status with a full new object. Events will be
* emitted for status changes, new files, and new results.
*
* @param {object} next The new assembly status object.
*/
updateStatus (next) {
this.#diffStatus(this.status, next)
this.status = next
}
/**
* Diff two assembly statuses, and emit the events necessary to go from `prev`
* to `next`.
*
* @param {object} prev The previous assembly status.
* @param {object} next The new assembly status.
*/
#diffStatus (prev, next) {
const prevStatus = prev.ok
const nextStatus = next.ok
if (next.error && !prev.error) {
return this.#onError(next)
}
// Desired emit order:
// - executing
// - (n × upload)
// - metadata
// - (m × result)
// - finished
// The below checks run in this order, that way even if we jump from
// UPLOADING straight to FINISHED all the events are emitted as expected.
const nowExecuting = isStatus(nextStatus, ASSEMBLY_EXECUTING)
&& !isStatus(prevStatus, ASSEMBLY_EXECUTING)
if (nowExecuting) {
// Without WebSockets, 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.
this.emit('executing')
}
// Find new uploaded files.
Object.keys(next.uploads)
.filter((upload) => !has(prev.uploads, upload))
.forEach((upload) => {
this.emit('upload', next.uploads[upload])
})
if (nowExecuting) {
this.emit('metadata')
}
// Find new results.
Object.keys(next.results).forEach((stepName) => {
const nextResults = next.results[stepName]
const prevResults = prev.results[stepName]
nextResults
.filter((n) => !prevResults || !prevResults.some((p) => p.id === n.id))
.forEach((result) => {
this.emit('result', stepName, result)
})
})
if (isStatus(nextStatus, ASSEMBLY_COMPLETED)
&& !isStatus(prevStatus, ASSEMBLY_COMPLETED)) {
this.emit('finished')
}
return undefined
}
/**
* Stop updating this assembly.
*/
close () {
this.closed = true
if (this.socket) {
this.socket.disconnect()
this.socket = null
}
clearInterval(this.pollInterval)
this.pollInterval = null
}
}
export default TransloaditAssembly