Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix: address second-round review findings on streamed uploads

Nevo Davidc2944e14e461
diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
index 543c53eda..5a48ff51b 100644
--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts
+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
@@ -185,10 +185,10 @@ export abstract class SocialAbstract {
method: 'HEAD',
dispatcher: getSsrfSafeDispatcher(),
} as any);
- const length = head.headers.get('content-length');
+ const length = Number(head.headers.get('content-length'));
// A failed HEAD can still carry a content-length (of the error body),
- // which would register the media with a garbage size downstream.
- if (!head.ok || !length) {
+ // and a zero/NaN size would poison chunk-count math downstream.
+ if (!head.ok || !Number.isFinite(length) || length <= 0) {
throw new BadBody(
identifier,
'{}',
@@ -196,7 +196,7 @@ export abstract class SocialAbstract {
'Could not determine the media size for upload'
);
}
- return Number(length);
+ return length;
}
return statSync(path).size;
diff --git a/libraries/nestjs-libraries/src/integrations/social/bluesky.provider.ts b/libraries/nestjs-libraries/src/integrations/social/bluesky.provider.ts
index bcbbb5a43..edba9f530 100644
--- a/libraries/nestjs-libraries/src/integrations/social/bluesky.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/bluesky.provider.ts
@@ -96,7 +96,7 @@ async function uploadVideo(
// @ts-ignore - undici-only option; blocks SSRF to internal IPs
dispatcher: getSsrfSafeDispatcher(),
});
- if (!videoResponse.ok) {
+ if (!videoResponse.ok || !videoResponse.body) {
throw new Error(`Failed to fetch video: ${videoResponse.statusText}`);
}
diff --git a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
index c4c5e5641..b908485ee 100644
--- a/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/x.provider.ts
@@ -464,10 +464,13 @@ export class XProvider extends SocialAbstract implements SocialProvider {
}>(`media/upload/${mediaId}/finalize`);
let processing = finalize.data.processing_info;
- let attempts = 0;
- const maxAttempts = 100; // X drives the pace via check_after_secs (~1-5s each)
+ // X drives the pace via check_after_secs; cap on accumulated wait time
+ // (long videos can legitimately process for many minutes) instead of an
+ // attempt count, but never poll forever.
+ let waitedMs = 0;
+ const maxWaitMs = 30 * 60 * 1000;
while (processing && processing.state !== 'succeeded') {
- if (processing.state === 'failed' || attempts >= maxAttempts) {
+ if (processing.state === 'failed' || waitedMs >= maxWaitMs) {
throw new BadBody(
this.identifier,
JSON.stringify(processing),
@@ -480,14 +483,15 @@ export class XProvider extends SocialAbstract implements SocialProvider {
);
}
- await timer((processing.check_after_secs || 1) * 1000);
+ const waitMs = (processing.check_after_secs || 1) * 1000;
+ await timer(waitMs);
+ waitedMs += waitMs;
const status = await client.v2.get<{
data: {
processing_info?: { state: string; check_after_secs?: number };
};
}>('media/upload', { command: 'STATUS', media_id: mediaId });
processing = status.data.processing_info;
- attempts++;
}
return mediaId;