fix!: throw InvalidDataLengthError when input exceeds MAX_DATA_LENGTH#136
fix!: throw InvalidDataLengthError when input exceeds MAX_DATA_LENGTH#136achingbrain merged 6 commits intoalanshaw:masterfrom
Conversation
|
@achingbrain lint issue fixed. |
achingbrain
left a comment
There was a problem hiding this comment.
LGTM. This should be released as a major since there's now a default max input length - it'll now throw in places it didn't previously.
Co-authored-by: Alex Potsides <alex@achingbrain.net>
|
@achingbrain In 8f31186 There are some comments so I can ask you questions: it('should not encode message data that is too long PROMISE', async () => {
const customMaxDataLength = MAX_DATA_LENGTH
const input = new Uint8Array(customMaxDataLength + 1) // Create a buffer larger than the max allowed
// Wrap in a Promise to ensure compatibility with async assertions
await expect(
Promise.resolve().then(() => lp.encode.single(input))
).to.eventually.be.rejected.with.property('code', 'ERR_MSG_DATA_TOO_LONG')
// ).to.eventually.be.rejectedWith(InvalidDataLengthError).and.have.property('code', 'ERR_MSG_DATA_TOO_LONG')
})I tried to use the same matchers to be slightly more expressive, but I have to wrap |
There is an example of the pattern you should be able to use at https://github.com/libp2p/js-libp2p/blob/31a15a1483e0af0f9ede24de0a7f1d24bf9d408d/packages/kad-dht/test/record/selection.spec.ts#L19-L23 basically you can expect |
|
@SgtPooki @achingbrain Should be ready with enhanced test assertions with expressive matchers |
|
cc @alanshaw I don't have access to this repo and Alex is out this week |
## [10.0.0](v9.1.1...v10.0.0) (2025-02-03) ### ⚠ BREAKING CHANGES * outgoing messages must now be shorter than the `maxDataLength` option, or 4MB if no option is passed ### Bug Fixes * throw InvalidDataLengthError when input exceeds MAX_DATA_LENGTH ([#136](#136)) ([7a56aaf](7a56aaf))
|
🎉 This PR is included in version 10.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Description
Changes Made:
Created a new
constants.tsfile:MAX_DATA_LENGTHandMAX_LENGTH_LENGTH.Modified
decode.tsandencode.ts:constants.ts, ensuring that the constants are defined only once and are accessible throughout the codebase.Added a check for
MAX_DATA_LENGTHinencode.ts:encode.tsto throw anInvalidDataLengthErrorif the input exceeds the definedMAX_DATA_LENGTH. This helps to avoid processing data that's too large.Added tests:
MAX_DATA_LENGTH.Motivation:
This change improves maintainability by centralizing constant definitions in
constants.ts. It also ensures that input validation forMAX_DATA_LENGTHand the custommaxDataLengthis consistently applied and prevents potential issues when dealing with large data.How to Test:
MAX_DATA_LENGTHor the custommaxDataLengthNotes:
InvalidDataLengthErrorclass has been utilized to handle cases where the data exceeds the maximum allowed length.constants.tsis updated if the limits (e.g.,MAX_DATA_LENGTH) need to change in the future.