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