From 8e46ed4e0d7fd91e00203f1a1c18dd0049fdb33c Mon Sep 17 00:00:00 2001 From: Kris Zyp Date: Sun, 6 Nov 2022 09:15:35 -0700 Subject: [PATCH] Fixes for bun --- index.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ae13927..3e4afef 100644 --- a/index.js +++ b/index.js @@ -65,7 +65,7 @@ export function writeKey(key, target, position, inSequence) { } } else { if (target.utf8Write) - position += target.utf8Write(key, position) + position += target.utf8Write(key, position, 0xffffffff) else position += textEncoder.encodeInto(key, target.subarray(position)).written if (position > target.length - 4) @@ -305,7 +305,23 @@ function finishUtf8(byte1, src) { } } -const readString = eval(makeStringBuilder()) +const readString = + typeof process !== 'undefined' && process.isBun ? // the eval in bun doesn't properly closure on position, so we + // have to manually update it + (function(reading) { + let { setPosition, getPosition, readString } = reading; + return (source) => { + setPosition(position); + let value = readString(source); + position = getPosition(); + return value; + }; + })((new Function('fromCharCode', 'let position; let readString = ' + makeStringBuilder() + + ';return {' + + 'setPosition(p) { position = p },' + + 'getPosition() { return position },' + + 'readString }'))(fromCharCode)) : + eval(makeStringBuilder()) export function compareKeys(a, b) { // compare with type consistency that matches binary comparison