mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 13:40:37 +00:00

Refs: https://github.com/nodejs/node/pull/32644 Refs: https://github.com/nodejs/node/pull/32662 PR-URL: https://github.com/nodejs/node/pull/32667 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
35 lines
964 B
JavaScript
35 lines
964 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')
|
|
};
|
|
}
|
|
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
|
|
}
|
|
exports.defaultGetSource = defaultGetSource;
|