Skip to content

Commit 4c86dc9

Browse files
committed
Add support for all data URL formats (#3341).
1 parent d083522 commit 4c86dc9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/web/src.ts/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export type FetchJsonResponse = {
8282

8383
type Header = { key: string, value: string };
8484

85+
function unpercent(value: string): Uint8Array {
86+
return toUtf8Bytes(value.replace(/%([0-9a-f][0-9a-f])/gi, (all, code) => {
87+
return String.fromCharCode(parseInt(code, 16));
88+
}));
89+
}
90+
8591
// This API is still a work in progress; the future changes will likely be:
8692
// - ConnectionInfo => FetchDataRequest<T = any>
8793
// - FetchDataRequest.body? = string | Uint8Array | { contentType: string, data: string | Uint8Array }
@@ -165,15 +171,15 @@ export function _fetchData<T = Uint8Array>(connection: string | ConnectionInfo,
165171
}
166172
}
167173

168-
const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i");
174+
const reData = new RegExp("^data:([^;:]*)?(;base64)?,(.*)$", "i");
169175
const dataMatch = ((url) ? url.match(reData): null);
170176
if (dataMatch) {
171177
try {
172178
const response = {
173179
statusCode: 200,
174180
statusMessage: "OK",
175-
headers: { "content-type": dataMatch[1] },
176-
body: base64Decode(dataMatch[2])
181+
headers: { "content-type": (dataMatch[1] || "text/plain")},
182+
body: (dataMatch[2] ? base64Decode(dataMatch[3]): unpercent(dataMatch[3]))
177183
};
178184

179185
let result: T = <T><unknown>response.body;

0 commit comments

Comments
 (0)