fix(x): drop chunked upload chunk size from 5MB to 1MB#31
Merged
Conversation
Production was returning HTTP 413 with empty body on the first APPEND segment of every video upload to X v2 — surfacing to users as 'An unknown X error occurred.' Empty-body 413 is the classic signature of an edge/CDN rejection: the X gateway is denying the request before X's application code sees it. The X v2 reference docs say 'max chunk size: 5MB', but every canonical reference uses 1MB: - X's official Python quickstart: `chunk_size = 1024 * 1024` - X's official JavaScript quickstart: `const chunkSize = 1024 * 1024` - twitter-api-v2 (the most-used Node SDK, used by Postiz et al.): `chunkSize: number = 1024 * 1024` 5MB plus multipart-form overhead apparently exceeds an undocumented edge limit. 1MB is the empirically safe size everyone converges on. Changes: - XPublisher chunked APPEND now uses 1MB chunks. An 8MB video uploads as 8 segments instead of 2; more roundtrips but actually succeeds. - Set explicit Content-Type on each chunk attach (matches the simple upload path in the same file). - XPublishException::fromApiResponse maps HTTP 413 to ErrorCategory::MediaFormat with the message 'Media chunk rejected by X (payload too large).' so we don't surface 413 as 'unknown' if it ever recurs. Test: unit test covering the 413→MediaFormat mapping. Full suite green (1503 passed, 2 skipped).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
Every video post to X has been failing in production with the cryptic error 'An unknown X error occurred.' Logs show the pattern:
```
production.ERROR: X chunked upload APPEND error {"status":413,"body":"","segment":0}
production.ERROR: Social publish failed: An unknown X error occurred.
```
Status 413 with empty body on the very first chunk — the classic signature of an edge/CDN rejection (the gateway is denying the request before X's application code sees it).
Root cause
We were using 5MB chunks (max documented size per X v2 reference), but every canonical reference uses 1MB:
The docs say 5MB max, but with multipart-form overhead, the actual HTTP request body exceeds an undocumented edge limit. 1MB is the empirically safe size everyone converges on.
Changes
XPublisher.php:209— drop chunk size from `5 * 1024 * 1024` to `1024 * 1024`. An 8MB video uploads as 8 APPEND segments instead of 2; more roundtrips but actually succeeds.XPublisher.php:224— set explicit `Content-Type` on each chunk attach (matches the simple-upload path in the same file at line 144-151).`XPublishException::fromApiResponse` — map HTTP 413 to `ErrorCategory::MediaFormat` with the message 'Media chunk rejected by X (payload too large).' so we don't surface 413 as 'unknown' if it ever recurs for any reason.
Test plan