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

getBuffer() does not match the behavior of ioredis #1154

Closed
ryanwitt opened this issue Feb 27, 2022 · 1 comment · Fixed by #1155
Closed

getBuffer() does not match the behavior of ioredis #1154

ryanwitt opened this issue Feb 27, 2022 · 1 comment · Fixed by #1155
Labels

Comments

@ryanwitt
Copy link
Contributor

Consider the following example:

const Redis = require('ioredis-mock')
const redis = new Redis()
const value = Buffer.from('1T+HJWDNyTa4jJXwoBbV6Q==', 'base64')
await redis.set('foo', value)
const returnedValue = await redis.getBuffer('foo')
assert.equal(value.toString('base64'), returnedValue.toString('base64'))

Result:

Uncaught:
AssertionError [ERR_ASSERTION]: '1T+HJWDNyTa4jJXwoBbV6Q==' == '77+9P++/vSVg77+977+9Nu+/ve+/ve+/ve+/vRbvv73vv70='
    at REPL37:1:8
    at Script.runInThisContext (vm.js:134:12)
    at REPLServer.defaultEval (repl.js:566:29)
    at bound (domain.js:421:15)
    at REPLServer.runBound [as eval] (domain.js:432:12)
    at REPLServer.onLine (repl.js:909:10)
    at REPLServer.emit (events.js:412:35)
    at REPLServer.emit (domain.js:475:12)
    at REPLServer.Interface._onLine (readline.js:434:10)
    at REPLServer.Interface._line (readline.js:791:8) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: '1T+HJWDNyTa4jJXwoBbV6Q==',
  expected: '77+9P++/vSVg77+977+9Nu+/ve+/ve+/ve+/vRbvv73vv70=',
  operator: '=='
}

What's going on here? Well, it looks like the implementation for get always calls Buffer.toString() on values that were originally stored as buffers. This encodes the data returned as utf8, which breaks on bytes that don't have representation in unicode. From the node Buffer docs:

When decoding a Buffer into a string that does not exclusively contain valid UTF-8 data, the Unicode replacement character U+FFFD � will be used to represent those errors.

To bring this into compliance with ioredis, we likely need to change the getBuffer implementation to detect that a key was stored as a buffer and return it as such without interpretation. I think this will yield the same behavior as ioredis get and getBuffer.

Will send PR.

@stipsan
Copy link
Owner

stipsan commented Feb 27, 2022

🎉 This issue has been resolved in version 7.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants