1'use strict'; 2 3const { getOptionValue } = require('internal/options'); 4const manifest = getOptionValue('--experimental-policy') ? 5 require('internal/process/policy').manifest : 6 null; 7 8const { Buffer } = require('buffer'); 9 10const fs = require('fs'); 11const { URL } = require('url'); 12const { promisify } = require('internal/util'); 13const { 14 ERR_INVALID_URL, 15 ERR_INVALID_URL_SCHEME, 16} = require('internal/errors').codes; 17const readFileAsync = promisify(fs.readFile); 18 19const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; 20 21async function defaultGetSource(url, { format } = {}, defaultGetSource) { 22 const parsed = new URL(url); 23 let source; 24 if (parsed.protocol === 'file:') { 25 source = await readFileAsync(parsed); 26 } else if (parsed.protocol === 'data:') { 27 const match = DATA_URL_PATTERN.exec(parsed.pathname); 28 if (!match) { 29 throw new ERR_INVALID_URL(url); 30 } 31 const [ , base64, body ] = match; 32 source = Buffer.from(body, base64 ? 'base64' : 'utf8'); 33 } else { 34 throw new ERR_INVALID_URL_SCHEME(['file', 'data']); 35 } 36 if (manifest) { 37 manifest.assertIntegrity(parsed, source); 38 } 39 return { source }; 40} 41exports.defaultGetSource = defaultGetSource; 42