Skip to content

fix: normalize JSON-serialized Buffer in getImage/getFile#444

Merged
macterra merged 1 commit intomainfrom
fix-buffer-deserialization
Apr 17, 2026
Merged

fix: normalize JSON-serialized Buffer in getImage/getFile#444
macterra merged 1 commit intomainfrom
fix-buffer-deserialization

Conversation

@macterra
Copy link
Copy Markdown
Collaborator

Problem: KeymasterClient.getImage() and getFile() return file data as a plain object { type: "Buffer", data: [137, 80, ...] } instead of a real Buffer. This happens because the keymaster server sends the response via res.json(), and Node's Buffer.toJSON() serializes to that format. The client never reconstitutes it.

This causes the CLI's get-asset-image and get-asset-file commands to crash with ERR_INVALID_ARG_TYPE when calling fs.writeFileSync().

Fix: Add a normalizeBufferData() helper that detects the JSON-serialized Buffer pattern and converts it back to a real Buffer. Applied in both getImage() and getFile().

Other binary methods (getVaultItem, getDmailAttachment) are not affected — they already use responseType: 'arraybuffer' with Buffer.from().

Server routes for GET /images/:id and GET /files/:id now support
content negotiation. Clients sending Accept: application/octet-stream
receive binary data with metadata in X-Metadata header, matching the
existing vault/dmail pattern. JSON responses preserved as default for
backward compatibility.

- keymaster-api: add binary response path for images and files routes
- keymaster-client: use responseType arraybuffer for getImage/getFile
- herald: remove manual Buffer normalization workaround
- client tests: update nock mocks for binary responses
@macterra macterra force-pushed the fix-buffer-deserialization branch from e108f66 to 0ef2f5e Compare April 17, 2026 00:34
@macterra macterra merged commit 3317c72 into main Apr 17, 2026
25 checks passed
@macterra macterra deleted the fix-buffer-deserialization branch April 17, 2026 00:55
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.

1 participant