Parent PR: #55
Source comment: #55 (comment)
File: lib/cast/server/storage.ts:140
Confidence: 45 = 50 +20 specific +15 ≤10 lines −25 vague −15 semantics
Interpretation
saveAssetFile calls Buffer.from(bytes) on the incoming Uint8Array, which copies the data. For large uploads (up to UPLOAD_MAX_BYTES = 5MB) this doubles peak memory briefly. A zero-copy Buffer view over the same ArrayBuffer would avoid the copy.
Proposed approach
- Replace
Buffer.from(bytes) with Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength) to create a view instead of a copy
- Verify the StorageAdapter's
writeFile doesn't mutate the buffer (it shouldn't, but confirm)
Blockers / questions
Uint8Array from Request.arrayBuffer() may or may not share the underlying ArrayBuffer with other data — need to verify byteOffset is safe
- 5MB copy is ~2ms on modern hardware — this is a micro-optimization
Context for shft
Files to read:
lib/cast/server/storage.ts — saveAssetFile function
lib/cast/server/storage-adapter.ts — writeFile contract on LocalFsAdapter
app/api/upload/route.ts — where bytes originates (new Uint8Array(await req.arrayBuffer()))
Acceptance criteria:
Feedback loops:
Parent PR: #55
Source comment: #55 (comment)
File:
lib/cast/server/storage.ts:140Confidence: 45 = 50 +20 specific +15 ≤10 lines −25 vague −15 semantics
Interpretation
saveAssetFilecallsBuffer.from(bytes)on the incomingUint8Array, which copies the data. For large uploads (up toUPLOAD_MAX_BYTES= 5MB) this doubles peak memory briefly. A zero-copyBufferview over the sameArrayBufferwould avoid the copy.Proposed approach
Buffer.from(bytes)withBuffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)to create a view instead of a copywriteFiledoesn't mutate the buffer (it shouldn't, but confirm)Blockers / questions
Uint8ArrayfromRequest.arrayBuffer()may or may not share the underlyingArrayBufferwith other data — need to verifybyteOffsetis safeContext for shft
Files to read:
lib/cast/server/storage.ts—saveAssetFilefunctionlib/cast/server/storage-adapter.ts—writeFilecontract onLocalFsAdapterapp/api/upload/route.ts— wherebytesoriginates (new Uint8Array(await req.arrayBuffer()))Acceptance criteria:
Buffer.from(bytes)replaced with zero-copy viewArrayBufferFeedback loops:
pnpm testpnpm typecheck