Skip to content

Commit

Permalink
feat: Remove SafeBuffer & support base64 in the browser (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
prantlf and phated committed Oct 10, 2022
1 parent 08ab6a5 commit 3197441
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
55 changes: 49 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var fs = require('fs');
var path = require('path');
var SafeBuffer = require('safe-buffer');

Object.defineProperty(exports, 'commentRegex', {
get: function getCommentRegex () {
Expand All @@ -16,9 +15,30 @@ Object.defineProperty(exports, 'mapFileCommentRegex', {
}
});

var decodeBase64;
if (typeof Buffer !== 'undefined') {
if (typeof Buffer.from === 'function') {
decodeBase64 = decodeBase64WithBufferFrom;
} else {
decodeBase64 = decodeBase64WithNewBuffer;
}
} else {
decodeBase64 = decodeBase64WithAtob;
}

function decodeBase64WithBufferFrom(base64) {
return Buffer.from(base64, 'base64').toString();
}

function decodeBase64WithNewBuffer(base64) {
if (typeof value === 'number') {
throw new TypeError('The value to decode must not be of type number.');
}
return new Buffer(base64, 'base64').toString();
}

function decodeBase64(base64) {
return (SafeBuffer.Buffer.from(base64, 'base64') || "").toString();
function decodeBase64WithAtob(base64) {
return decodeURIComponent(escape(atob(base64)));
}

function stripComment(sm) {
Expand Down Expand Up @@ -56,10 +76,33 @@ Converter.prototype.toJSON = function (space) {
return JSON.stringify(this.sourcemap, null, space);
};

Converter.prototype.toBase64 = function () {
if (typeof Buffer !== 'undefined') {
if (typeof Buffer.from === 'function') {
Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
} else {
Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
}
} else {
Converter.prototype.toBase64 = encodeBase64WithBtoa;
}

function encodeBase64WithBufferFrom() {
var json = this.toJSON();
return (SafeBuffer.Buffer.from(json, 'utf8') || "").toString('base64');
};
return Buffer.from(json, 'utf8').toString('base64');
}

function encodeBase64WithNewBuffer() {
var json = this.toJSON();
if (typeof json === 'number') {
throw new TypeError('The json to encode must not be of type number.');
}
return new Buffer(json, 'utf8').toString('base64');
}

function encodeBase64WithBtoa() {
var json = this.toJSON();
return btoa(unescape(encodeURIComponent(json)));
}

Converter.prototype.toComment = function (options) {
var base64 = this.toBase64();
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"url": "git://github.com/thlorenz/convert-source-map.git"
},
"homepage": "https://github.com/thlorenz/convert-source-map",
"dependencies": {
"safe-buffer": "~5.1.1"
},
"devDependencies": {
"inline-source-map": "~0.6.2",
"tap": "~9.0.0"
Expand Down

0 comments on commit 3197441

Please sign in to comment.