-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(build): Prevent Node's Buffer
module from being included in browser bundles
#3372
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
Merged
lobsterkatie
merged 5 commits into
kmclb-dynamic-sampling-data-in-envelopes
from
kmclb-stop-bundling-buffer-module
Apr 12, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
512387b
consolidate error handling
lobsterkatie 323a275
get Buffer and atob/btoa from global object explicitly
lobsterkatie 14636e8
add typechecking
lobsterkatie 9228f1e
test in browser as well as node
lobsterkatie a957d9d
minor fixes
lobsterkatie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { base64ToUnicode, unicodeToBase64 } from '@sentry/utils'; | ||
import { expect } from 'chai'; | ||
|
||
// See https://tools.ietf.org/html/rfc4648#section-4 for base64 spec | ||
// eslint-disable-next-line no-useless-escape | ||
const BASE64_REGEX = /([a-zA-Z0-9+/]{4})*(|([a-zA-Z0-9+/]{3}=)|([a-zA-Z0-9+/]{2}==))/; | ||
|
||
// NOTE: These tests are copied (and adapted for chai syntax) from `string.test.ts` in `@sentry/utils`. The | ||
// base64-conversion functions have a different implementation in browser and node, so they're copied here to prove they | ||
// work in a real live browser. If you make changes here, make sure to also port them over to that copy. | ||
describe('base64ToUnicode/unicodeToBase64', () => { | ||
const unicodeString = 'Dogs are great!'; | ||
const base64String = 'RG9ncyBhcmUgZ3JlYXQh'; | ||
|
||
it('converts to valid base64', () => { | ||
expect(BASE64_REGEX.test(unicodeToBase64(unicodeString))).to.be.true; | ||
}); | ||
|
||
it('works as expected', () => { | ||
expect(unicodeToBase64(unicodeString)).to.equal(base64String); | ||
expect(base64ToUnicode(base64String)).to.equal(unicodeString); | ||
}); | ||
|
||
it('conversion functions are inverses', () => { | ||
expect(base64ToUnicode(unicodeToBase64(unicodeString))).to.equal(unicodeString); | ||
expect(unicodeToBase64(base64ToUnicode(base64String))).to.equal(base64String); | ||
}); | ||
|
||
it('can handle and preserve multi-byte characters in original string', () => { | ||
['🐶', 'Καλό κορίτσι, Μάιζεϊ!', 'Of margir hundar! Ég geri ráð fyrir að ég þurfi stærra rúm.'].forEach(orig => { | ||
expect(() => { | ||
unicodeToBase64(orig); | ||
}).not.to.throw; | ||
expect(base64ToUnicode(unicodeToBase64(orig))).to.equal(orig); | ||
}); | ||
}); | ||
|
||
it('throws an error when given invalid input', () => { | ||
expect(() => { | ||
unicodeToBase64(null as any); | ||
}).to.throw('Unable to convert to base64'); | ||
expect(() => { | ||
unicodeToBase64(undefined as any); | ||
}).to.throw('Unable to convert to base64'); | ||
expect(() => { | ||
unicodeToBase64({} as any); | ||
}).to.throw('Unable to convert to base64'); | ||
|
||
expect(() => { | ||
base64ToUnicode(null as any); | ||
}).to.throw('Unable to convert from base64'); | ||
expect(() => { | ||
base64ToUnicode(undefined as any); | ||
}).to.throw('Unable to convert from base64'); | ||
expect(() => { | ||
base64ToUnicode({} as any); | ||
}).to.throw('Unable to convert from base64'); | ||
|
||
// Note that by design, in node base64 encoding and decoding will accept any string, whether or not it's valid | ||
// base64, by ignoring all invalid characters, including whitespace. Therefore, no wacky strings have been included | ||
// here because they don't actually error. | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.