Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Aug 4, 2024
1 parent ad38125 commit 1941095
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
9 changes: 4 additions & 5 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
sourceMapRebase,
pathRelativeIfSub,
stringAbbrev,
stringOrBufferCast,
readFileAsync
} from './util';
import {data} from './uri';
Expand Down Expand Up @@ -84,9 +83,9 @@ export default async function (source, map, meta) {
const resolverMulti = createResolverMulti(resolver);

// If passed a buffer then convert to a string or emit warning and skip.
let sourceString = null;
let text;
try {
sourceString = stringOrBufferCast(source, 'utf8');
text = typeof source === 'string' ? source : source.toString('utf8');
} catch (err) {
emitWarning(new Exception(`Failed to cast source to string: ${err}`));
// eslint-disable-next-line prefer-rest-params
Expand All @@ -95,7 +94,7 @@ export default async function (source, map, meta) {
}

// Parse source map comment or pass the arguments straight through.
const parsed = commentParse(sourceString);
const parsed = commentParse(text);
if (!parsed) {
// eslint-disable-next-line prefer-rest-params
callback(null, ...arguments);
Expand Down Expand Up @@ -126,7 +125,7 @@ export default async function (source, map, meta) {

// Attempt to decode source map content.
try {
mapCode = dataURI.body().toString(dataURI.charset);
mapCode = dataURI.text();
} catch (err) {
emitWarning(
new Exception(`Failed to decode data URI: ${mapInfo}: ${err}`)
Expand Down
20 changes: 17 additions & 3 deletions src/uri.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const US_ASCII = 'US-ASCII';

/**
* Decode percents in string.
*
Expand Down Expand Up @@ -32,7 +34,7 @@ export function data(uri) {
: mediaInfo;
const [mimeType] = mediaType.split(';', 1);
const m2 = mediaType.match(/;charset=([^;]*)(;|$)/iu);
const charset = (m2 ? m2[1] : '') || 'US-ASCII';
const charset = (m2 ? m2[1] : '') || US_ASCII;
return {
mediaType,
mimeType,
Expand All @@ -47,8 +49,20 @@ export function data(uri) {
*/
body() {
return Buffer.from(
decodePercents(data),
base64 ? 'base64' : 'utf8'
decodePercents(this.data),
this.base64 ? 'base64' : 'utf8'
);
},

/**
* Get body as text.
*
* @returns {string} Text data.
*/
text() {
const {charset} = this;
return this.body().toString(
charset.toUpperCase() === US_ASCII ? 'ascii' : charset
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/uri.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for (const [uri, info] of [
strictEqual(d.charset, info.charset);
strictEqual(d.base64, info.base64);
strictEqual(d.data, info.data);
strictEqual(d.body().toString(), info.body);
strictEqual(d.text(), info.body);
} else {
strictEqual(d, null);
}
Expand Down
14 changes: 0 additions & 14 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,6 @@ export function decodeURISafe(str) {
return null;
}

/**
* Convert string or Buffer to string.
*
* @param {string|Buffer} data String or Buffer data.
* @param {string} encoding Buffer encoding.
* @returns {string} String value.
*/
export function stringOrBufferCast(data, encoding = 'utf8') {
if (typeof data === 'string') {
return data;
}
return data.toString(encoding);
}

/**
* Read a file asyncronousely.
*
Expand Down
9 changes: 0 additions & 9 deletions src/util.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
pathRelativeIfSub,
stringAbbrev,
decodeURISafe,
stringOrBufferCast,
readFileAsync
} from './util';

Expand Down Expand Up @@ -255,14 +254,6 @@ test('decodeURISafe: invalid', () => {
strictEqual(decodeURISafe('%%'), null);
});

test('stringOrBufferCast: string', () => {
strictEqual(stringOrBufferCast('hello'), 'hello');
});

test('stringOrBufferCast: buffer', () => {
strictEqual(stringOrBufferCast(Buffer.from('hello')), 'hello');
});

test('readFileAsync: pass', async () => {
const pkg = JSON.parse(await readFileAsync('package.json', 'utf8'));
strictEqual(typeof pkg, 'object');
Expand Down

0 comments on commit 1941095

Please sign in to comment.