Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blob support #1524

Closed
wants to merge 2 commits into from
Closed

Conversation

SjxSubham
Copy link

@SjxSubham SjxSubham commented Dec 2, 2024

Closes #1523

✅ Checklist

  • I have followed every step in the contributing guide
  • The PR title follows the convention.
  • I ran and tested the code works

Serialization and Deserialization

Blob Support

The ioSerialization.ts file now supports serialization and deserialization of Blob objects.

Example Usage

import { stringifyIO, parsePacket } from './ioSerialization';

// Serialize a Blob object
const blob = new Blob(['Hello, world!'], { type: 'text/plain' });
const serialized = await stringifyIO(blob);
console.log(serialized);

// Deserialize a Blob object
const deserialized = await parsePacket(serialized);
console.log(await deserialized.text());


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **New Features**
	- Added support for `Blob` objects in serialization and deserialization processes.
	- Enhanced `stringifyIO` and `parsePacket` functions to handle `Blob` data types.

- **Tests**
	- Introduced a new test suite for IO serialization, covering `Blob`, JSON, and string data types to ensure proper functionality and data integrity.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Copy link

changeset-bot bot commented Dec 2, 2024

⚠️ No Changeset found

Latest commit: c44e55b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Dec 2, 2024

Walkthrough

The changes introduce support for Blob objects in the stringifyIO and parsePacket functions within the ioSerialization module. The stringifyIO function now reads Blob instances as data URLs and returns them with the appropriate data type. The parsePacket function has been updated to handle packets with the data type "application/blob", decoding the data URL back into a Blob. Additionally, a new test suite has been added to validate these functionalities across various data types, including Blob, JSON, and plain strings.

Changes

File Change Summary
packages/core/src/v3/utils/ioSerialization.ts Added support for Blob objects in stringifyIO and parsePacket. Updated method signatures accordingly.
tests/e2e/ioSerialization.test.ts Introduced a test suite for stringifyIO and parsePacket, including tests for Blob, JSON, and string types.

Assessment against linked issues

Objective Addressed Explanation
Add support for Blob in payloads and outputs. (#1523)

🐰 In the meadow, where data flows,
A Blob now dances, as everyone knows.
With strings and JSON, they play in delight,
Serialization magic, oh what a sight!
Hooray for the changes, let's hop and cheer,
For Blob in our payloads, we hold so dear! 🌟

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/core/src/v3/utils/ioSerialization.ts

Oops! Something went wrong! :(

ESLint: 8.45.0

ESLint couldn't find the config "custom" to extend from. Please check that the name of the config is correct.

The config "custom" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
tests/e2e/ioSerialization.test.ts (1)

4-13: Consider adding more test cases for Blob serialization.

While the basic Blob test is good, consider adding these scenarios:

  • Empty Blobs
  • Blobs with different MIME types
  • Large Blobs (near system limits)
  • Error cases (e.g., corrupted data URLs)
packages/core/src/v3/utils/ioSerialization.ts (2)

201-210: Consider adding size limits for Blob handling.

Large Blobs could cause memory issues when converting to data URLs. Consider adding size validation.

if (value instanceof Blob) {
+ const MAX_BLOB_SIZE = 50 * 1024 * 1024; // 50MB limit
+ if (value.size > MAX_BLOB_SIZE) {
+   throw new Error(`Blob size ${value.size} exceeds limit of ${MAX_BLOB_SIZE} bytes`);
+ }
  const reader = new FileReader();
  return new Promise((resolve, reject) => {

236-243: Optimize Blob parsing performance.

The current implementation uses a loop to copy bytes. Consider using more efficient methods.

-const byteString = atob(value.data.split(',')[1]);
-const ab = new ArrayBuffer(byteString.length);
-const ia = new Uint8Array(ab);
-for (let i = 0; i < byteString.length; i++) {
-  ia[i] = byteString.charCodeAt(i);
-}
+const base64 = value.data.split(',')[1];
+const binary = atob(base64);
+const bytes = Uint8Array.from(binary, char => char.charCodeAt(0));
🧰 Tools
🪛 Biome (1.9.4)

[error] 236-236: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 237-237: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 238-238: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 239-239: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f7bf7bc and c44e55b.

📒 Files selected for processing (2)
  • packages/core/src/v3/utils/ioSerialization.ts (1 hunks)
  • tests/e2e/ioSerialization.test.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/core/src/v3/utils/ioSerialization.ts

[error] 231-231: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 236-236: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 237-237: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 238-238: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 239-239: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 192-192: Shouldn't redeclare 'stringifyIO'. Consider to delete it or rename it.

'stringifyIO' is defined here:

(lint/suspicious/noRedeclare)


[error] 222-222: Shouldn't redeclare 'parsePacket'. Consider to delete it or rename it.

'parsePacket' is defined here:

(lint/suspicious/noRedeclare)

🔇 Additional comments (2)
tests/e2e/ioSerialization.test.ts (1)

15-31: LGTM! Comprehensive tests for JSON and string handling.

The test cases for JSON objects and strings are well-structured and cover the basic scenarios.

packages/core/src/v3/utils/ioSerialization.ts (1)

235-244: Verify Blob type handling in the codebase.

Need to ensure that Blob handling is consistently implemented across the codebase.

✅ Verification successful

Let me gather more context about the implementation details by checking the test file and the full implementation.


Blob handling is correctly and consistently implemented

The implementation of Blob serialization and deserialization is well-tested and properly handled:

  • Serialization converts Blobs to data URLs using FileReader
  • Deserialization correctly reconstructs Blobs from data URLs preserving MIME types
  • Test coverage confirms the roundtrip functionality
  • The implementation is isolated to the IO serialization module and doesn't require changes elsewhere
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for Blob usage in the codebase
echo "Searching for Blob usage patterns..."
rg -l "instanceof Blob" 

# Search for data URL handling
echo "Searching for data URL handling..."
rg "data:.*base64"

# Search for potential Blob consumers
echo "Searching for potential Blob consumers..."
ast-grep --pattern 'new Blob($$$)'

Length of output: 625


Script:

#!/bin/bash
# Check the test implementation
echo "Checking test implementation..."
cat tests/e2e/ioSerialization.test.ts

# Check the full serialization implementation
echo "Checking serialization implementation..."
cat packages/core/src/v3/utils/ioSerialization.ts

Length of output: 13881

🧰 Tools
🪛 Biome (1.9.4)

[error] 236-236: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 237-237: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 238-238: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 239-239: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)

Comment on lines +191 to +219
// Add Blob support in the stringifyIO function
export async function stringifyIO(value: any): Promise<IOPacket> {
if (value === undefined) {
return { dataType: "application/json" };
}

if (typeof value === "string") {
return { data: value, dataType: "text/plain" };
}

if (value instanceof Blob) {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onloadend = () => {
resolve({ data: reader.result as string, dataType: "application/blob" });
};
reader.onerror = reject;
reader.readAsDataURL(value);
});
}

try {
const { stringify } = await loadSuperJSON();
const data = stringify(value);
return { data, dataType: "application/super+json" };
} catch {
return { data: value, dataType: "application/json" };
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix function redeclaration and add error handling.

The stringifyIO function is redeclared, which will cause runtime errors. Additionally, the Blob handling needs better error handling.

Apply this diff to fix the issues:

-// Add Blob support in the stringifyIO function
-export async function stringifyIO(value: any): Promise<IOPacket> {
+// Update existing stringifyIO function
-if (value === undefined) {
-  return { dataType: "application/json" };
-}
-
-if (typeof value === "string") {
-  return { data: value, dataType: "text/plain" };
-}
-
+// Add Blob support
if (value instanceof Blob) {
  const reader = new FileReader();
  return new Promise((resolve, reject) => {
    reader.onloadend = () => {
+     if (!reader.result) {
+       reject(new Error('Failed to read Blob'));
+       return;
+     }
      resolve({ data: reader.result as string, dataType: "application/blob" });
    };
    reader.onerror = reject;
    reader.readAsDataURL(value);
  });
}

-try {
-  const { stringify } = await loadSuperJSON();
-  const data = stringify(value);
-  return { data, dataType: "application/super+json" };
-} catch {
-  return { data: value, dataType: "application/json" };
-}

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Biome (1.9.4)

[error] 192-192: Shouldn't redeclare 'stringifyIO'. Consider to delete it or rename it.

'stringifyIO' is defined here:

(lint/suspicious/noRedeclare)

Comment on lines +221 to +249
// Add Blob support in the parsePacket function
export async function parsePacket(value: IOPacket): Promise<any> {
if (!value.data) {
return undefined;
}

switch (value.dataType) {
case "application/json":
return JSON.parse(value.data);
case "application/super+json":
const { parse } = await loadSuperJSON();
return parse(value.data);
case "text/plain":
return value.data;
case "application/blob":
const byteString = atob(value.data.split(',')[1]);
const mimeString = value.data.split(',')[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: mimeString });
case "application/store":
throw new Error(`Cannot parse an application/store packet (${value.data}). Needs to be imported first.`);
default:
return value.data;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix switch case declarations and add validation.

The switch case declarations need block scoping, and the Blob parsing needs input validation.

Apply this diff to fix the issues:

-// Add Blob support in the parsePacket function
-export async function parsePacket(value: IOPacket): Promise<any> {
 case "application/blob": {
+  if (!value.data || !value.data.includes(',')) {
+    throw new Error('Invalid data URL format');
+  }
   const byteString = atob(value.data.split(',')[1]);
   const mimeString = value.data.split(',')[0].split(':')[1].split(';')[0];
+  if (!mimeString) {
+    throw new Error('Invalid MIME type in data URL');
+  }
   const ab = new ArrayBuffer(byteString.length);
   const ia = new Uint8Array(ab);
   for (let i = 0; i < byteString.length; i++) {
     ia[i] = byteString.charCodeAt(i);
   }
   return new Blob([ab], { type: mimeString });
+  break;
 }

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Biome (1.9.4)

[error] 231-231: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 236-236: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 237-237: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 238-238: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 239-239: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.

The declaration is defined in this switch clause:

Unsafe fix: Wrap the declaration in a block.

(lint/correctness/noSwitchDeclarations)


[error] 222-222: Shouldn't redeclare 'parsePacket'. Consider to delete it or rename it.

'parsePacket' is defined here:

(lint/suspicious/noRedeclare)

Copy link
Member

@matt-aitken matt-aitken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have been clearer in the GitHub issue.

We don't want to create a new dataType, we just need to extend the existing application/super+json one.

You can see that we already extend SuperJSON here:

superjson.registerCustom<Buffer, number[]>(

We just need to do the same thing for Blob.

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.

[TRI-4135] Add support for Blob in payloads and outputs.
2 participants