(encoding, source)
| 145 | * @returns {string} encoded data |
| 146 | */ |
| 147 | const encodeDataUri = (encoding, source) => { |
| 148 | /** @type {string | undefined} */ |
| 149 | let encodedContent; |
| 150 | |
| 151 | switch (encoding) { |
| 152 | case "base64": { |
| 153 | encodedContent = source.buffer().toString("base64"); |
| 154 | break; |
| 155 | } |
| 156 | case false: { |
| 157 | const content = source.source(); |
| 158 | |
| 159 | if (typeof content !== "string") { |
| 160 | encodedContent = content.toString("utf8"); |
| 161 | } |
| 162 | |
| 163 | encodedContent = encodeURIComponent( |
| 164 | /** @type {string} */ |
| 165 | (encodedContent) |
| 166 | ).replace( |
| 167 | /[!'()*]/g, |
| 168 | (character) => |
| 169 | `%${/** @type {number} */ (character.codePointAt(0)).toString(16)}` |
| 170 | ); |
| 171 | break; |
| 172 | } |
| 173 | default: |
| 174 | throw new Error(`Unsupported encoding '${encoding}'`); |
| 175 | } |
| 176 | |
| 177 | return encodedContent; |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * Decodes data uri content. |
no test coverage detected