node/lib/internal/modules/esm/get_source.js
Geoffrey Booth 2551a21553 module: loader getSource, getFormat, transform hooks
PR-URL: https://github.com/nodejs/node/pull/30986
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
2020-01-07 01:31:05 +02:00

36 lines
977 B
JavaScript

'use strict';
const { Buffer } = require('buffer');
const fs = require('fs');
const { URL } = require('url');
const { promisify } = require('internal/util');
const {
ERR_INVALID_URL,
ERR_INVALID_URL_SCHEME,
} = require('internal/errors').codes;
const readFileAsync = promisify(fs.readFile);
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
async function defaultGetSource(url, { format } = {}, defaultGetSource) {
const parsed = new URL(url);
if (parsed.protocol === 'file:') {
return {
source: await readFileAsync(parsed)
};
} else if (parsed.protocol === 'data:') {
const match = DATA_URL_PATTERN.exec(parsed.pathname);
if (!match) {
throw new ERR_INVALID_URL(url);
}
const [ , base64, body ] = match;
return {
source: Buffer.from(body, base64 ? 'base64' : 'utf8')
};
} else {
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
}
}
exports.defaultGetSource = defaultGetSource;