Skip to content

Commit 0740082

Browse files
committed
[patch] use binary encoding function that does not overflow
If the qr codes get too big (in total file size), then the current function will blow up. This one works better, tested manually by updating the dist/build.js, and updating example.html to point to that instead of the production cdn hosted websdk
1 parent 152106f commit 0740082

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

example.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ <h4>QR Code</h4>
8383
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
8484
<script type="text/javascript">
8585
(function(b,r,a,n,c,h,_,s,d,k){if(!b[n]||!b[n]._q){for(;s<_.length;)c(h,_[s++]);d=r.createElement(a);d.async=1;d.src="https://cdn.branch.io/branch-latest.min.js";k=r.getElementsByTagName(a)[0];k.parentNode.insertBefore(d,k);b[n]=h}})(window,document,"script","branch",function(b,r){b[r]=function(){b._q.push([r,arguments])}},{_q:[],_v:1},"addListener banner closeBanner closeJourney data deepview deepviewCta first init link logout removeListener setBranchViewData setIdentity track trackCommerceEvent logEvent disableTracking getBrowserFingerprintId crossPlatformIds lastAttributedTouchData setAPIResponseCallback qrCode setRequestMetaData setDMAParamsForEEA setAPIUrl getAPIUrl".split(" "), 0);
86-
branch.setAPIUrl("https://api.stage.branch.io");
8786

8887
branch.setAPIResponseCallback(function(url, method, requestBody, error, status, responseBody) {
8988
console.log('Request: ' + method + ' ' + url + ' body=' + JSON.stringify(requestBody));

src/6_branch.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,19 +1340,22 @@ Branch.prototype['qrCode'] = wrap(
13401340
this._api(
13411341
resources.qrCode,
13421342
utils.cleanLinkData(linkData),
1343-
function(error, rawBuffer) {
1344-
function QrCode() { }
1345-
if (!error) {
1346-
QrCode['rawBuffer'] = rawBuffer;
1347-
QrCode['base64'] = function() {
1348-
// First Encode array buffer as UTF-8 String, then Base64 Encode
1349-
if (this['rawBuffer']) {
1350-
return btoa(String.fromCharCode.apply(null, new Uint8Array(this['rawBuffer'])));
1351-
}
1352-
throw Error('QrCode.rawBuffer is empty.');
1353-
};
1354-
}
1355-
return done(error || null, QrCode || null);
1343+
(error, rawBuffer) => {
1344+
if (error) return done(error, null);
1345+
1346+
const QrCode = {
1347+
rawBuffer,
1348+
base64() {
1349+
if (!this.rawBuffer) throw new Error('QrCode.rawBuffer is empty.');
1350+
// Encode the Uint8Array to a binary string, then convert to base64
1351+
const binaryString = Array.from(new Uint8Array(this.rawBuffer))
1352+
.map(byte => String.fromCharCode(byte))
1353+
.join('');
1354+
return btoa(binaryString);
1355+
}
1356+
};
1357+
1358+
return done(null, QrCode);
13561359
}
13571360
);
13581361
}

0 commit comments

Comments
 (0)