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

feat(redis): opt-in raw support #561

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

cjpearson
Copy link
Contributor

Resolves #559

This change adds a new saveRawAsBinary option to the redis driver. When enabled, the driver will use the setBuffer and getBuffer methods of ioredis to directly save Buffers rather than base64 encoding and saving as a string. The option is not enabled by default as that would be a backwards incompatible change.

When #528 is implemented, this could be made the default for {type: 'bytes'}. (Perhaps then saveBytesAsBinary is a better name?) It would also be possible to drop the base64 encoding entirely and instead allow users to manually encode and save as { type: 'text' }.

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

Attention: Patch coverage is 64.15094% with 19 lines in your changes missing coverage. Please review.

Project coverage is 60.07%. Comparing base (4d61c78) to head (b92591e).
Report is 160 commits behind head on main.

Files with missing lines Patch % Lines
src/drivers/redis.ts 64.15% 11 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #561      +/-   ##
==========================================
- Coverage   65.05%   60.07%   -4.98%     
==========================================
  Files          39       42       +3     
  Lines        4055     3702     -353     
  Branches      487      615     +128     
==========================================
- Hits         2638     2224     -414     
- Misses       1408     1467      +59     
- Partials        9       11       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/_utils.ts Outdated Show resolved Hide resolved
src/drivers/redis.ts Outdated Show resolved Hide resolved
@cjpearson cjpearson marked this pull request as ready for review January 3, 2025 10:51
@pi0 pi0 changed the title feat(redis): add saveRawAsBinary driver option feat(redis): opt-in raw support Jan 3, 2025
@@ -72,7 +72,7 @@ const storage = createStorage({
- `cluster`: List of redis nodes to use for cluster mode. Takes precedence over `url` and `host` options.
- `clusterOptions`: Options to use for cluster mode.
- `ttl`: Default TTL for all items in **seconds**.
- `useRaw`: If enabled, `getItemRaw` and `setItemRaw` will use binary data instead of base64 encoded strings. (this option will be enabled by default in the next major version.)
- `raw`: If enabled, `getItemRaw` and `setItemRaw` will use binary data instead of base64 encoded strings. (this option will be enabled by default in the next major version.)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would suggest something like binary/useBinary here. I think it makes the effect a bit more clear because it changes how raw data is stored, but does not enable/disable the ability to save raw data. Or it could be inverted to base64 so in the future the default value can be false.

Copy link
Member

@pi0 pi0 Jan 3, 2025

Choose a reason for hiding this comment

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

I agree that in general binary could be more describing than raw. In the context of unstorage, we call binary-compatible feature raw which was the reason I was thinking to use this.

Considering it is for short-term solution, are you happy we go with raw?

(Also check fb2977a, I have extended normalization)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extending normalization to additional variants of TypedArray makes sense, but it should only allow for binary data and continue to throw when other data is passed. Otherwise it would lead to some unexpected behavior as you save an object but get a Buffer back.

That's also why I think the different naming is helpful. binary means that setItemRaw accepts binary and getItemRaw returns binary. Whereas the existing raw behavior accepts binary and other things.

Copy link
Member

@pi0 pi0 Jan 3, 2025

Choose a reason for hiding this comment

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

This is consistent with generic unstorage.*raw* interface behavior -- happy to change it in the future (so ALL calls to setItemRaw will be restricted to accept binary only not objects) but normally, users should use the same key either for raw or non-raw purposes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm would it be possible to add an additional option in that case? Either on the driver or the method? What I'm looking for is a way to save binary data and ensure it is binary and saved as binary and when getting, that I get a Buffer in return.

My assumption is that is how getItem(key, { type: 'bytes'})/setItem(key, value, { type: 'bytes'}) would work in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Yes exactly for type: 'bytes' we can get strict.

Currently, if someone passes a string to setItemRaw, getItemRaw gives a Buffer representation of Utf8 bytes which isn't that odd ,is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's what I find a bit unexpected 😅

Do you think there would be an issue if we added type: 'bytes' as a method option here? It would be just for the redis driver at the moment, but done in a forward compatible manner.

Or is it better to use something else and save that wording for later

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.

[redis] support native binary storage
2 participants