Skip to content
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

fix: Replace SafeBuffer with Buffer, support BASE64 in the browser #74

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 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,28 @@ Object.defineProperty(exports, 'mapFileCommentRegex', {
}
});

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

function decodeBase64(base64) {
return (SafeBuffer.Buffer.from(base64, 'base64') || "").toString();
function decodeBase64WithBufferFrom(base64) {
return Buffer.from(base64, 'base64').toString();
}
function decodeBase64WithNewBuffer(base64) {
phated marked this conversation as resolved.
Show resolved Hide resolved
if (typeof value === 'number') {
throw new TypeError('The value to decode must not be of type number.');
}
return new Buffer(base64, 'base64').toString();
}
function decodeBase64WithAtob(base64) {
return decodeURIComponent(escape(atob(base64)));
}

function stripComment(sm) {
Expand Down Expand Up @@ -56,10 +74,31 @@ 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() {
phated marked this conversation as resolved.
Show resolved Hide resolved
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