Skip to content

Commit

Permalink
Fix many lurking undefined errors
Browse files Browse the repository at this point in the history
there was an assumption that assembly is required and is never undefined
even though it is typed as optional and assigned to undefined in onError
  • Loading branch information
mifi committed Aug 9, 2024
1 parent 9810cc8 commit d440bf8
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions packages/@uppy/transloadit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ export default class Transloadit<
const files = this.uppy
.getFiles()
.filter(({ id }) => fileIDs.includes(id))

console.log(files)

Check warning on line 419 in packages/@uppy/transloadit/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint JavaScript/TypeScript

Unexpected console statement
if (files.length === 0) {
// All files have been removed, cancelling.
await this.client.cancelAssembly(newAssembly)
Expand Down Expand Up @@ -603,10 +605,11 @@ export default class Transloadit<
* When an Assembly has finished processing, get the final state
* and emit it.
*/
#onAssemblyFinished(status: AssemblyResponse) {
const url = status.assembly_ssl_url
#onAssemblyFinished(assembly: Assembly) {
const url = assembly.status.assembly_ssl_url
this.client.getAssemblyStatus(url).then((finalStatus) => {
this.assembly!.status = finalStatus
// eslint-disable-next-line no-param-reassign
assembly.status = finalStatus
this.uppy.emit('transloadit:complete', finalStatus)
})
}
Expand All @@ -621,8 +624,9 @@ export default class Transloadit<
* When all files are removed, cancel in-progress Assemblies.
*/
#onCancelAll = async () => {
if (!this.assembly) return
try {
await this.#cancelAssembly(this.assembly!.status)
await this.#cancelAssembly(this.assembly.status)
} catch (err) {
this.uppy.log(err)
}
Expand Down Expand Up @@ -697,12 +701,13 @@ export default class Transloadit<
// Set up the Assembly instances and AssemblyWatchers for existing Assemblies.
const restoreAssemblies = () => {
this.#createAssemblyWatcher(previousAssembly.assembly_id)
this.#connectAssembly(this.assembly!)
if (this.assembly == null) throw new Error('Assembly was nullish')
this.#connectAssembly(this.assembly)
}

// Force-update all Assemblies to check for missed events.
const updateAssemblies = () => {
return this.assembly!.update()
return this.assembly?.update()
}

// Restore all Assembly state.
Expand Down Expand Up @@ -768,11 +773,11 @@ export default class Transloadit<

if (this.opts.waitForEncoding) {
assembly.on('finished', () => {
this.#onAssemblyFinished(assembly.status)
this.#onAssemblyFinished(assembly)
})
} else if (this.opts.waitForMetadata) {
assembly.on('metadata', () => {
this.#onAssemblyFinished(assembly.status)
this.#onAssemblyFinished(assembly)
})
}

Expand Down Expand Up @@ -835,19 +840,22 @@ export default class Transloadit<
})
}

const assemblyID = this.assembly!.status.assembly_id
const assemblyID = this.assembly?.status.assembly_id

const closeSocketConnections = () => {
this.assembly!.close()
this.assembly?.close()
}

// If we don't have to wait for encoding metadata or results, we can close
// the socket immediately and finish the upload.
if (!this.#shouldWaitAfterUpload()) {
closeSocketConnections()
this.uppy.addResultData(uploadID, {
transloadit: [this.assembly!.status],
})
const status = this.assembly?.status
if (status != null) {
this.uppy.addResultData(uploadID, {
transloadit: [status],
})
}
return Promise.resolve()
}

Expand All @@ -870,9 +878,12 @@ export default class Transloadit<

return this.#watcher.promise.then(() => {
closeSocketConnections()
this.uppy.addResultData(uploadID, {
transloadit: [this.assembly!.status],
})
const status = this.assembly?.status
if (status != null) {
this.uppy.addResultData(uploadID, {
transloadit: [status],
})
}
})
}

Expand Down Expand Up @@ -982,7 +993,7 @@ export default class Transloadit<
}

getAssembly(): AssemblyResponse | undefined {
return this.assembly!.status
return this.assembly?.status
}

getAssemblyFiles(assemblyID: string): UppyFile<M, B>[] {
Expand Down

0 comments on commit d440bf8

Please sign in to comment.