(specifier: string)
| 122 | /^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/; |
| 123 | |
| 124 | function parseDataUri(specifier: string): { |
| 125 | mime: string; |
| 126 | code: string | Buffer; |
| 127 | } { |
| 128 | const match = specifier.match(dataURIRegex); |
| 129 | if (!match || !match.groups) { |
| 130 | throw new Error('Invalid data URI'); |
| 131 | } |
| 132 | const {mime, encoding, code} = match.groups; |
| 133 | if (mime === 'application/wasm') { |
| 134 | if (!encoding) throw new Error('Missing data URI encoding'); |
| 135 | if (encoding !== 'base64') { |
| 136 | throw new Error(`Invalid data URI encoding: ${encoding}`); |
| 137 | } |
| 138 | return {code: Buffer.from(code, 'base64'), mime}; |
| 139 | } |
| 140 | if (!encoding || encoding === 'charset=utf-8') { |
| 141 | return {code: decodeURIComponent(code), mime}; |
| 142 | } |
| 143 | if (encoding === 'base64') { |
| 144 | return {code: Buffer.from(code, 'base64').toString(), mime}; |
| 145 | } |
| 146 | throw new Error(`Invalid data URI encoding: ${encoding}`); |
| 147 | } |
| 148 | |
| 149 | // Mirrors Node's `validateAttributes` in lib/internal/modules/esm/assert.js. |
| 150 | // The only deliberate divergence: missing `type: 'json'` warns instead of |
no test coverage detected