Skip to content

Commit

Permalink
Add ability to stream file uploads instead of use multipart form data (
Browse files Browse the repository at this point in the history
…#409)

* Add streaming file upload endpoint

* Add file upload progress logging

* Use stream endpoint for files from CLI

* Add progress bar to CLI upload

* Use Transform for size calculation

This prevents a race condition

* Remove timeout that was causing process to hang

* Remove excess logging from adding features

* Use headersTimeout to keep fetches from timing out

* Log error cause in global error handling

* Fixup

* Combine file upload endpoints

* Lint fix

* Lint fix

* temporarily disable docker tests
  • Loading branch information
garrettjstevens authored Jul 11, 2024
1 parent 2cd8372 commit 2698455
Show file tree
Hide file tree
Showing 27 changed files with 328 additions and 230 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ jobs:
- name: Run CLI tests
run: python3 ./test/test.py TestCLI
working-directory: packages/apollo-cli
- name: Run docker tests
working-directory: packages/apollo-cli
run: python3 ./test/test_docker.py
# - name: Run docker tests
# working-directory: packages/apollo-cli
# run: python3 ./test/test_docker.py
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@oclif/core": "^3.18.2",
"@oclif/plugin-help": "^6.0.8",
"bson": "^6.3.0",
"cli-progress": "^3.12.0",
"conf": "^12.0.0",
"joi": "^17.7.0",
"open": "^10.1.0",
Expand All @@ -53,6 +54,7 @@
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@oclif/test": "^3.1.3",
"@types/chai": "^4",
"@types/cli-progress": "^3",
"@types/inquirer": "^9.0.7",
"@types/mocha": "^10",
"@types/node": "^18.14.2",
Expand Down
5 changes: 3 additions & 2 deletions packages/apollo-cli/src/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
}

protected async catch(err: Error & { exitCode?: number }): Promise<unknown> {
// add any custom logic to handle errors from the command
// or simply return the parent class error handling
if (err.cause instanceof Error) {
console.error(err.cause)
}
return super.catch(err)
}

Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/assembly/add-fasta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export default class Get extends BaseCommand<typeof Get> {
)
throw new Error(errorMessage)
}
this.exit(0)
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/assembly/add-gff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ export default class AddGff extends BaseCommand<typeof AddGff> {
)
throw new Error(errorMessage)
}
this.exit(0)
}
}
15 changes: 3 additions & 12 deletions packages/apollo-cli/src/commands/assembly/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import { Flags } from '@oclif/core'
import { Response, fetch } from 'undici'
import { Agent, RequestInit, Response, fetch } from 'undici'

import { BaseCommand } from '../../baseCommand.js'
import {
Expand Down Expand Up @@ -29,19 +29,11 @@ async function getSequence(
url.search = searchParams.toString()
const uri = url.toString()

const controller = new AbortController()
setTimeout(
() => {
controller.abort()
},
24 * 60 * 60 * 1000,
)

const auth = {
const auth: RequestInit = {
headers: {
authorization: `Bearer ${accessToken}`,
},
signal: controller.signal,
dispatcher: new Agent({ headersTimeout: 60 * 60 * 1000 }),
}
const response = await fetch(uri, auth)
if (!response.ok) {
Expand Down Expand Up @@ -147,7 +139,6 @@ export default class ApolloCmd extends BaseCommand<typeof ApolloCmd> {
this.log(header)
this.log(splitStringIntoChunks(seq, 80).join('\n'))
}
this.exit(0)
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/add-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export default class Get extends BaseCommand<typeof Get> {
)
throw new Error(errorMessage)
}
this.exit(0)
}

private async addChild(
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export default class Copy extends BaseCommand<typeof Copy> {
)
throw new Error(errorMessage)
}
this.exit(0)
}

private async copyFeature(
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,5 @@ export default class Delete extends BaseCommand<typeof Delete> {
}
}
}
this.exit(0)
}
}
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/edit-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,5 @@ export default class EditAttibute extends BaseCommand<typeof EditAttibute> {
)
throw new Error(errorMessage)
}
this.exit(0)
}
}
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/edit-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,5 @@ export default class Get extends BaseCommand<typeof Get> {
)
throw new Error(errorMessage)
}
this.exit(0)
}
}
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@ export default class Get extends BaseCommand<typeof Get> {
throw new Error(errorMessage)
}
}
this.exit(0)
}
}
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/get-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export default class Get extends BaseCommand<typeof Get> {
results.push(res)
}
this.log(JSON.stringify(results, null, 2))
this.exit(0)
}

private async getFeatureId(
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-cli/src/commands/feature/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export default class Get extends BaseCommand<typeof Get> {
}
}
this.log(JSON.stringify(results, null, 2))
this.exit(0)
}

private async getFeatures(
Expand Down
78 changes: 22 additions & 56 deletions packages/apollo-cli/src/commands/feature/import.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'node:fs'

import { Flags } from '@oclif/core'
import { Response, fetch } from 'undici'
import { Agent, RequestInit, fetch } from 'undici'

import { BaseCommand } from '../../baseCommand.js'
import {
Expand Down Expand Up @@ -69,64 +69,30 @@ export default class Import extends BaseCommand<typeof Import> {
'text/x-gff3',
)

const res: Response = await importFeatures(
access.address,
access.accessToken,
assembly[0],
uploadId,
flags['delete-existing'],
)
if (!res.ok) {
const body = {
typeName: 'AddFeaturesFromFileChange',
assembly: assembly[0],
fileId: uploadId,
deleteExistingFeatures: flags['delete-existing'],
}
const auth: RequestInit = {
method: 'POST',
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${access.accessToken}`,
'Content-Type': 'application/json',
},
dispatcher: new Agent({ headersTimeout: 60 * 60 * 1000 }),
}

const url = new URL(localhostToAddress(`${access.address}/changes`))
const response = await fetch(url, auth)
if (!response.ok) {
const errorMessage = await createFetchErrorMessage(
res,
'Import features failed',
response,
'importFeatures failed',
)
throw new Error(errorMessage)
}
this.exit(0)
}
}

async function importFeatures(
address: string,
accessToken: string,
assembly: string,
fileId: string,
deleteExistingFeatures: boolean,
): Promise<Response> {
const body = {
typeName: 'AddFeaturesFromFileChange',
assembly,
fileId,
deleteExistingFeatures,
}

const controller = new AbortController()
setTimeout(
() => {
controller.abort()
},
24 * 60 * 60 * 1000,
)

const auth = {
method: 'POST',
body: JSON.stringify(body),
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
signal: controller.signal,
}

const url = new URL(localhostToAddress(`${address}/changes`))
const response = await fetch(url, auth)
if (!response.ok) {
const errorMessage = await createFetchErrorMessage(
response,
'importFeatures failed',
)
throw new Error(errorMessage)
}
return response
}
Loading

0 comments on commit 2698455

Please sign in to comment.