diff --git a/packages/web/src.ts/index.ts b/packages/web/src.ts/index.ts index 5cf3fd8ba..0c0871e8c 100644 --- a/packages/web/src.ts/index.ts +++ b/packages/web/src.ts/index.ts @@ -82,6 +82,12 @@ export type FetchJsonResponse = { type Header = { key: string, value: string }; +function unpercent(value: string): Uint8Array { + return toUtf8Bytes(value.replace(/%([0-9a-f][0-9a-f])/gi, (all, code) => { + return String.fromCharCode(parseInt(code, 16)); + })); +} + // This API is still a work in progress; the future changes will likely be: // - ConnectionInfo => FetchDataRequest // - FetchDataRequest.body? = string | Uint8Array | { contentType: string, data: string | Uint8Array } @@ -165,15 +171,15 @@ export function _fetchData(connection: string | ConnectionInfo, } } - const reData = new RegExp("^data:([a-z0-9-]+/[a-z0-9-]+);base64,(.*)$", "i"); + const reData = new RegExp("^data:([^;:]*)?(;base64)?,(.*)$", "i"); const dataMatch = ((url) ? url.match(reData): null); if (dataMatch) { try { const response = { statusCode: 200, statusMessage: "OK", - headers: { "content-type": dataMatch[1] }, - body: base64Decode(dataMatch[2]) + headers: { "content-type": (dataMatch[1] || "text/plain")}, + body: (dataMatch[2] ? base64Decode(dataMatch[3]): unpercent(dataMatch[3])) }; let result: T = response.body;