Skip to content

feat(upload): upload straight to the final storage path with a signed token - #360

Merged
riglar merged 1 commit into
devfrom
feat/signed-direct-upload
Nov 24, 2026
Merged

riglar merged 1 commit into
devfrom
feat/signed-direct-upload

Conversation

@riglar

@riglar riglar commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What this changes

Today the CLI uploads the Supabase copy of a binary or flow zip to a temporary path. When the upload is finalised, the API copies it to its final path. For a large binary, that copy is slow.

With this change, the CLI asks for a signed upload token when it requests an upload URL. If the API returns one, the CLI uploads straight to the final path, so there's nothing to copy.

How it works

  • getBinaryUploadUrl and getFlowUploadUrl now send signedUpload: true.
    • An API that supports this returns signedUpload: { path, token, expiresInSeconds }.
    • An API that doesn't support it ignores the field, and nothing changes.
  • With a token, the Supabase copy goes through TUS to /storage/v1/upload/resumable/sign, with the token in the x-signature header.
    • The token only allows writing to that one path, and it can't overwrite an existing file.
    • x-upsert is no longer sent on this path.
  • Finalise sends the final path with useTus: true, so the API checks the file is there and skips its copy. submitFlowTest already sends useTus: true; it now gets the final path too.

Token expiry

Storage checks the token on every TUS request, and a token lasts about two hours. So:

  • Before each request, the CLI gets a fresh token from the new POST /uploads/refreshUploadToken if the current one has less than 10 minutes left.
  • If Storage refuses a request because of the signature (a 400, 401 or 403 that mentions the JWT or signature), the CLI gets a fresh token and retries.
  • Either way the path stays the same, so the same TUS upload carries on.
  • The CLI asks for at most 5 fresh tokens per upload, so a bad token can't loop forever.

Fallback

  • If the signed upload fails for any reason, the CLI uploads to the temp path as before and finalises with that path.
  • If the API offers no token, the CLI uses the temp path, as today.

The Backblaze upload is unchanged.

Tests

New test/unit/signed-upload.test.ts:

  • Runs against a small local TUS server and checks that:
    • the token is sent to the signed endpoint for the final path, without x-upsert
    • a refused token is refreshed and the upload carries on
    • a token close to expiry is refreshed before the first request
    • refreshing stops after the limit
  • uploadSupabaseCopy:
    • uses the final path when a token is offered
    • asks for a fresh token for the same path
    • falls back to the temp path on failure
    • uses the temp path when no token is offered

pnpm lint, pnpm typecheck, pnpm test:unit (779 passing) and pnpm build all pass.

Not yet tested against real Storage. That needs the matching API change deployed to dev. Before releasing, check on dev:

  • a normal upload
  • resuming after the token expires
  • a path mismatch is rejected
  • overwriting an existing file is rejected

🤖 Generated with Claude Code

… token

Ask the API for a signed upload token when getting an upload URL. When one
comes back, upload the Supabase copy with TUS straight to the final path
through /storage/v1/upload/resumable/sign, sending the token as
x-signature, so the API has nothing to copy when it finalises.

The token is refreshed before a request when it is close to expiry, and
when Storage refuses it, so a long upload carries on. If the signed upload
fails, or the API offers none, the CLI uploads to the temp path as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread test/unit/signed-upload.test.ts Dismissed
@claude

claude Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@riglar
riglar merged commit 00b2807 into dev Nov 24, 2026
11 checks passed
@riglar
riglar deleted the feat/signed-direct-upload branch November 24, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants