-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the package compatible with browser targets natively
Remove the need for a polyfill by providing an implementation that uses web APIs.
- Loading branch information
Showing
5 changed files
with
410 additions
and
341 deletions.
There are no files selected for viewing
This file contains 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,42 @@ | ||
const replacementCharacterBuffer = new Uint8Array([0xef, 0xbf, 0xbd]); | ||
|
||
const base64js = require('base64-js'); | ||
|
||
module.exports = { | ||
fromBase64: (b64str) => { | ||
try { | ||
return base64js.toByteArray(b64str); | ||
} catch (e) { | ||
return new Uint8Array(); | ||
} | ||
}, | ||
|
||
toUtf8: TextEncoder.prototype.encode.bind(new TextEncoder()), | ||
fromUtf8: TextDecoder.prototype.decode.bind(new TextDecoder()), | ||
fromAscii: TextDecoder.prototype.decode.bind(new TextDecoder('ascii')), | ||
|
||
allocByteBuffer: (length) => { | ||
return new Uint8Array(length); | ||
}, | ||
|
||
includesReplacementCharacter: (haystack) => { | ||
const needle = replacementCharacterBuffer; | ||
if (haystack.length < needle.length) { | ||
return false; | ||
} | ||
let fromIndex = 0; | ||
while (fromIndex !== haystack.length - 3) { | ||
const foundFirst = haystack[fromIndex] === needle[0]; | ||
const foundSecond = haystack[fromIndex + 1] === needle[1]; | ||
const foundThird = haystack[fromIndex + 2] === needle[2]; | ||
|
||
if (foundFirst && foundSecond && foundThird) { | ||
return true; | ||
} else { | ||
fromIndex += 1; | ||
} | ||
} | ||
|
||
return false; | ||
}, | ||
}; |
This file contains 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,21 @@ | ||
const replacementCharacterBuffer = new Uint8Array([0xef, 0xbf, 0xbd]); | ||
|
||
module.exports = { | ||
fromBase64: (b64str) => { | ||
return Buffer.from(b64str, 'base64'); | ||
}, | ||
|
||
toUtf8: (str) => { | ||
return Buffer.from(str, 'utf-8'); | ||
}, | ||
fromUtf8: String, | ||
fromAscii: (buffer) => { | ||
return buffer.toString('ascii'); | ||
}, | ||
|
||
allocByteBuffer: Buffer.alloc, | ||
|
||
includesReplacementCharacter: (haystack) => { | ||
return haystack.includes(replacementCharacterBuffer); | ||
}, | ||
}; |
This file contains 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 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
Oops, something went wrong.