From db1cf2a5a8f285e6153fe2a3336aa7074e9e27f4 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Thu, 13 Jan 2022 09:05:58 +0100 Subject: [PATCH 1/4] fix: Replace SafeBuffer with Buffer, support BASE64 in the browser This is a part of changes to get this module usable in the web browser and more lightweight at the same time. * Remove the safe-buffer dependency. Use the Bufer constructor and Buffer.from depending on what is available. * Use atob/btoa with a workaround for UTF-8 in the browser. --- index.js | 32 +++++++++++++++++++++++--------- package.json | 3 --- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index d3265f0..9cc9264 100644 --- a/index.js +++ b/index.js @@ -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 () { @@ -16,10 +15,16 @@ Object.defineProperty(exports, 'mapFileCommentRegex', { } }); - -function decodeBase64(base64) { - return (SafeBuffer.Buffer.from(base64, 'base64') || "").toString(); -} +var decodeBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? + function decodeBase64(base64) { + return Buffer.from(base64, 'base64').toString(); + } : + function decodeBase64(base64) { + return new Buffer(base64, 'base64').toString(); + } : + function decodeBase64(base64) { + return decodeURIComponent(escape(atob(base64))); + }; function stripComment(sm) { return sm.split(',').pop(); @@ -56,10 +61,19 @@ Converter.prototype.toJSON = function (space) { return JSON.stringify(this.sourcemap, null, space); }; -Converter.prototype.toBase64 = function () { - var json = this.toJSON(); - return (SafeBuffer.Buffer.from(json, 'utf8') || "").toString('base64'); -}; +Converter.prototype.toBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? + function () { + var json = this.toJSON(); + return Buffer.from(json, 'utf8').toString('base64'); + } : + function () { + var json = this.toJSON(); + return new Buffer(json, 'utf8').toString('base64'); + } : + function () { + var json = this.toJSON(); + return btoa(unescape(encodeURIComponent(json))); + }; Converter.prototype.toComment = function (options) { var base64 = this.toBase64(); diff --git a/package.json b/package.json index 07ff61f..56a8f8e 100644 --- a/package.json +++ b/package.json @@ -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" From aa527366390100d3a935c896119d57c8283d1929 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 10 Oct 2022 12:23:00 -0700 Subject: [PATCH 2/4] if statement & named functions for decodeBase64, fail on typeof number in new buffer --- index.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 9cc9264..79d398f 100644 --- a/index.js +++ b/index.js @@ -15,16 +15,29 @@ Object.defineProperty(exports, 'mapFileCommentRegex', { } }); -var decodeBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? - function decodeBase64(base64) { - return Buffer.from(base64, 'base64').toString(); - } : - function decodeBase64(base64) { - return new Buffer(base64, 'base64').toString(); - } : - function decodeBase64(base64) { - return decodeURIComponent(escape(atob(base64))); - }; +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 decodeBase64WithAtob(base64) { + return decodeURIComponent(escape(atob(base64))); +} function stripComment(sm) { return sm.split(',').pop(); From cb8f059aaf17c92ec07788727725e035e36f5a30 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 10 Oct 2022 12:26:15 -0700 Subject: [PATCH 3/4] encode with if statements & named functions, error if number with new buffer --- index.js | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 79d398f..3f3651a 100644 --- a/index.js +++ b/index.js @@ -74,19 +74,31 @@ Converter.prototype.toJSON = function (space) { return JSON.stringify(this.sourcemap, null, space); }; -Converter.prototype.toBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? - function () { - var json = this.toJSON(); - return Buffer.from(json, 'utf8').toString('base64'); - } : - function () { - var json = this.toJSON(); - return new Buffer(json, 'utf8').toString('base64'); - } : - function () { - var json = this.toJSON(); - return btoa(unescape(encodeURIComponent(json))); - }; +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 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(); From 576953cb54917d4a5ea85418567d26b241583f62 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 10 Oct 2022 13:13:32 -0700 Subject: [PATCH 4/4] spaces between named functions --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index 3f3651a..dc602d7 100644 --- a/index.js +++ b/index.js @@ -29,12 +29,14 @@ if (typeof Buffer !== 'undefined') { 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 decodeBase64WithAtob(base64) { return decodeURIComponent(escape(atob(base64))); } @@ -88,6 +90,7 @@ function encodeBase64WithBufferFrom() { var json = this.toJSON(); return Buffer.from(json, 'utf8').toString('base64'); } + function encodeBase64WithNewBuffer() { var json = this.toJSON(); if (typeof json === 'number') { @@ -95,6 +98,7 @@ function encodeBase64WithNewBuffer() { } return new Buffer(json, 'utf8').toString('base64'); } + function encodeBase64WithBtoa() { var json = this.toJSON(); return btoa(unescape(encodeURIComponent(json)));