-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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: Allow explicit null encoding for readFile/writeFile #18534
feat: Allow explicit null encoding for readFile/writeFile #18534
Conversation
Thanks for taking the time to open a PR!
|
Test summaryRun details
View run in Cypress Dashboard ➡️ This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard |
I haven't written docs for these changes yet, wanted to get some eyes on it and see if the approach was right before first. @sainthkh, @jennifer-shehane suggested you might be interested in taking a look at these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello!
The code is good and there are only a few minor changes.
1. User facing changelog
When { encoding: null } is passed to cy.readFile(), the file is treated as binary and read as a Buffer. Similarly, { encoding: null } passed to cy.writeFile() allows direct writing of buffers.
Although { encoding: value }
works, it is recommended to pass encoding
as the second arg. So, it might be better to change this like:
When
null
is passed toencoding
ofcy.readFile()
, the file is treated as binary and read as a Buffer. Similarly,null
passed toencoding
of cy.writeFile() allows direct writing of buffers.
2. Add null
to Encodings
types.
cypress/cli/types/cypress.d.ts
Line 5675 in a045e4f
type Encodings = 'ascii' | 'base64' | 'binary' | 'hex' | 'latin1' | 'utf8' | 'utf-8' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le' |
3. I haven't tested it myself, but it seems that cy.fixture
would work correctly if null
is given as encoding. I think this should be added to the cy.fixture
doc, too. As we all know, this issue started with cy.fixture
problem.
Done.
Done.
It doesn't work with fixtures at the moment. I'll fix it in an upcoming commit here - the behavior should definitely be symetrical between read/writeFile and fixture. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I just left a comment about the missing comment.
I don't have authority to commit this PR. So, someone in the team will do the job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These comments should be changed or they'll be unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. 👍
packages/driver/cypress/integration/commands/assertions_spec.js
Outdated
Show resolved
Hide resolved
packages/driver/cypress/integration/commands/navigation_spec.js
Outdated
Show resolved
Hide resolved
if (options.encoding === null) { | ||
response = Buffer.from(response) | ||
} else if (response instanceof ArrayBuffer) { | ||
// Cypress' behavior is to base64 encode binary files if the user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flotwig - As part of removing the server-side transform of buffers to base64 before transmitting (based on your review, which I 100% agree with), we now have to explicitly do so on the driver side unless a null encoding is specified, in order to maintain backwards compatibility.
In a vacuum it would make more conceptual sense to remove the default of utf8 for undefined encoding (and unknown file types) and base64 encoding for binary files, but that would turn this into a breaking change, so instead I ended up with this fairly messy set of conditionals spread around the driver in order to maintain backwards compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a vacuum it would make more conceptual sense to remove the default of utf8 for undefined encoding (and unknown file types) and base64 encoding for binary files, but that would turn this into a breaking change
Yeah, and I wonder if that breaking change would even be welcome. I would guess that many people are relying on the existing behavior that treats everything as utf8
by default.
if (options.encoding === null) { | ||
response = Buffer.from(response) | ||
} else if (response instanceof ArrayBuffer) { | ||
// Cypress' behavior is to base64 encode binary files if the user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a vacuum it would make more conceptual sense to remove the default of utf8 for undefined encoding (and unknown file types) and base64 encoding for binary files, but that would turn this into a breaking change
Yeah, and I wonder if that breaking change would even be welcome. I would guess that many people are relying on the existing behavior that treats everything as utf8
by default.
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
User facing changelog
When
null
is passed as theencoding
to cy.readFile(), the file is treated as binary and read as aBuffer
. Similarly,null
passed as theencoding
to cy.writeFile() allows direct writing of buffers.If
encoding
is unspecified, the default remains 'utf8', matching current behavior.Additional details
This should significantly ease the effort of working with binary files from within cypress tests.
As an example:
How has the user experience changed?
(this screenshot does not exactly match the above example code)
PR Tasks
cypress-documentation
? Add explicit null as an option for encoding cypress-documentation#4172