MCPcopy
hub / github.com/webpack/webpack / encodeDataUri

Function encodeDataUri

lib/asset/AssetGenerator.js:147–178  ·  view source on GitHub ↗
(encoding, source)

Source from the content-addressed store, hash-verified

145 * @returns {string} encoded data
146 */
147const 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.

Callers 1

generateDataUriMethod · 0.85

Calls 3

sourceMethod · 0.80
toStringMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected