/
dataUrl.ts
76 lines (70 loc) · 3.04 KB
/
dataUrl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Generates a string of the following format:
*
* `data:[mediaType][;charset=<encoding>[;base64],<data>`
*
* - `encoding` - defaults to `utf8` for text data
* @param data
* @param mediaType - The mediaType is a [MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) type string
* @param attributes - Additional attributes
*/
export function encodeDataUrl(
data: string | Buffer,
mediaType: string,
attributes?: Iterable<readonly [string, string]> | undefined
): string {
if (typeof data === 'string') return encodeString(data, mediaType, attributes);
const attribs = encodeAttributes(attributes || []);
return `data:${mediaType}${attribs};base64,${data.toString('base64url')}`;
}
export function toDataUrl(
data: string | Buffer,
mediaType: string,
attributes?: Iterable<[string, string]> | undefined
): URL {
return new URL(encodeDataUrl(data, mediaType, attributes));
}
function encodeString(
data: string,
mediaType: string | undefined,
attributes: Iterable<readonly [string, string]> | undefined
): string {
mediaType = mediaType || 'text/plain';
attributes = attributes || [];
const asUrlComp = encodeURIComponent(data);
const asBase64 = Buffer.from(data).toString('base64url');
const useBase64 = asBase64.length < asUrlComp.length - 7;
const encoded = useBase64 ? asBase64 : asUrlComp;
// Ensure charset is first.
const attribMap = new Map([['charset', 'utf8'] as readonly [string, string]].concat([...attributes]));
attribMap.set('charset', 'utf8'); // Make sure it is always `utf8`.
const attribs = encodeAttributes(attribMap);
return `data:${mediaType}${attribs}${useBase64 ? ';base64' : ''},${encoded}`;
}
export interface DecodedDataUrl {
data: Buffer;
mediaType: string;
encoding?: string | undefined;
attributes: Map<string, string>;
}
function encodeAttributes(attributes: Iterable<readonly [string, string]>): string {
return [...attributes].map(([key, value]) => `;${key}=${encodeURIComponent(value)}`).join('');
}
const dataUrlRegExHead = /^data:(?<mediaType>[^;,]*)(?<attributes>(?:;[^=]+=[^;,]*)*)(?<base64>;base64)?$/;
export function decodeDataUrl(url: string): DecodedDataUrl {
const [head, encodedData] = url.split(',', 2);
if (!head || encodedData === undefined) throw Error('Not a data url');
const match = head.match(dataUrlRegExHead);
if (!match || !match.groups) throw Error('Not a data url');
const mediaType = match.groups['mediaType'] || '';
const rawAttributes = (match.groups['attributes'] || '')
.split(';')
.filter((a) => !!a)
.map((entry) => entry.split('=', 2))
.map(([key, value]) => [key, decodeURIComponent(value)] as [string, string]);
const attributes = new Map(rawAttributes);
const encoding = attributes.get('charset');
const isBase64 = !!match.groups['base64'];
const data = isBase64 ? Buffer.from(encodedData, 'base64url') : Buffer.from(decodeURIComponent(encodedData));
return { mediaType, data, encoding, attributes };
}