@common.js/data-uri-to-buffer 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
- package/src/index.ts +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @common.js/data-uri-to-buffer
|
|
2
|
+
|
|
3
|
+
The [data-uri-to-buffer](https://www.npmjs.com/package/data-uri-to-buffer) package exported as CommonJS modules.
|
|
4
|
+
|
|
5
|
+
Exported from [data-uri-to-buffer@4.0.1](https://www.npmjs.com/package/data-uri-to-buffer/v/4.0.1) using https://github.com/etienne-martin/common.js.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export interface MimeBuffer extends Buffer {
|
|
3
|
+
type: string;
|
|
4
|
+
typeFull: string;
|
|
5
|
+
charset: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Returns a `Buffer` instance from the given data URI `uri`.
|
|
9
|
+
*
|
|
10
|
+
* @param {String} uri Data URI to turn into a Buffer instance
|
|
11
|
+
* @returns {Buffer} Buffer instance from Data URI
|
|
12
|
+
* @api public
|
|
13
|
+
*/
|
|
14
|
+
export declare function dataUriToBuffer(uri: string): MimeBuffer;
|
|
15
|
+
export default dataUriToBuffer;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a `Buffer` instance from the given data URI `uri`.
|
|
3
|
+
*
|
|
4
|
+
* @param {String} uri Data URI to turn into a Buffer instance
|
|
5
|
+
* @returns {Buffer} Buffer instance from Data URI
|
|
6
|
+
* @api public
|
|
7
|
+
*/ "use strict";
|
|
8
|
+
Object.defineProperty(exports, "__esModule", {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
function _export(target, all) {
|
|
12
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: all[name]
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
_export(exports, {
|
|
18
|
+
dataUriToBuffer: function() {
|
|
19
|
+
return dataUriToBuffer;
|
|
20
|
+
},
|
|
21
|
+
default: function() {
|
|
22
|
+
return _default //# sourceMappingURL=index.js.map
|
|
23
|
+
;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
function dataUriToBuffer(uri) {
|
|
27
|
+
if (!/^data:/i.test(uri)) {
|
|
28
|
+
throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")');
|
|
29
|
+
}
|
|
30
|
+
// strip newlines
|
|
31
|
+
uri = uri.replace(/\r?\n/g, "");
|
|
32
|
+
// split the URI up into the "metadata" and the "data" portions
|
|
33
|
+
var firstComma = uri.indexOf(",");
|
|
34
|
+
if (firstComma === -1 || firstComma <= 4) {
|
|
35
|
+
throw new TypeError("malformed data: URI");
|
|
36
|
+
}
|
|
37
|
+
// remove the "data:" scheme and parse the metadata
|
|
38
|
+
var meta = uri.substring(5, firstComma).split(";");
|
|
39
|
+
var charset = "";
|
|
40
|
+
var base64 = false;
|
|
41
|
+
var type = meta[0] || "text/plain";
|
|
42
|
+
var typeFull = type;
|
|
43
|
+
for(var i = 1; i < meta.length; i++){
|
|
44
|
+
if (meta[i] === "base64") {
|
|
45
|
+
base64 = true;
|
|
46
|
+
} else if (meta[i]) {
|
|
47
|
+
typeFull += ";".concat(meta[i]);
|
|
48
|
+
if (meta[i].indexOf("charset=") === 0) {
|
|
49
|
+
charset = meta[i].substring(8);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// defaults to US-ASCII only if type is not provided
|
|
54
|
+
if (!meta[0] && !charset.length) {
|
|
55
|
+
typeFull += ";charset=US-ASCII";
|
|
56
|
+
charset = "US-ASCII";
|
|
57
|
+
}
|
|
58
|
+
// get the encoded data portion and decode URI-encoded chars
|
|
59
|
+
var encoding = base64 ? "base64" : "ascii";
|
|
60
|
+
var data = unescape(uri.substring(firstComma + 1));
|
|
61
|
+
var buffer = Buffer.from(data, encoding);
|
|
62
|
+
// set `.type` and `.typeFull` properties to MIME type
|
|
63
|
+
buffer.type = type;
|
|
64
|
+
buffer.typeFull = typeFull;
|
|
65
|
+
// set the `.charset` property
|
|
66
|
+
buffer.charset = charset;
|
|
67
|
+
return buffer;
|
|
68
|
+
}
|
|
69
|
+
var _default = dataUriToBuffer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW;IAC1C,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QACzB,MAAM,IAAI,SAAS,CAClB,kEAAkE,CAClE,CAAC;KACF;IAED,iBAAiB;IACjB,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAEhC,+DAA+D;IAC/D,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACpC,IAAI,UAAU,KAAK,CAAC,CAAC,IAAI,UAAU,IAAI,CAAC,EAAE;QACzC,MAAM,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAC;KAC3C;IAED,mDAAmD;IACnD,MAAM,IAAI,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAErD,IAAI,OAAO,GAAG,EAAE,CAAC;IACjB,IAAI,MAAM,GAAG,KAAK,CAAC;IACnB,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC;IACrC,IAAI,QAAQ,GAAG,IAAI,CAAC;IACpB,KAAK,IAAI,CAAC,GAAG,C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"description": "data-uri-to-buffer package exported as CommonJS modules",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"test": "jest"
|
|
15
|
+
},
|
|
16
|
+
"repository": "etienne-martin/common.js",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">= 12"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/TooTallNate/node-data-uri-to-buffer/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/jest": "^27.0.2",
|
|
27
|
+
"@types/node": "^12.20.36",
|
|
28
|
+
"jest": "^27.3.1",
|
|
29
|
+
"ts-jest": "^27.0.7",
|
|
30
|
+
"typescript": "^4.4.4"
|
|
31
|
+
},
|
|
32
|
+
"jest": {
|
|
33
|
+
"preset": "ts-jest",
|
|
34
|
+
"globals": {
|
|
35
|
+
"ts-jest": {
|
|
36
|
+
"diagnostics": false,
|
|
37
|
+
"isolatedModules": true
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"verbose": false,
|
|
41
|
+
"testEnvironment": "node",
|
|
42
|
+
"testMatch": [
|
|
43
|
+
"<rootDir>/test/**/*.test.ts"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {}
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export interface MimeBuffer extends Buffer {
|
|
2
|
+
type: string;
|
|
3
|
+
typeFull: string;
|
|
4
|
+
charset: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Returns a `Buffer` instance from the given data URI `uri`.
|
|
9
|
+
*
|
|
10
|
+
* @param {String} uri Data URI to turn into a Buffer instance
|
|
11
|
+
* @returns {Buffer} Buffer instance from Data URI
|
|
12
|
+
* @api public
|
|
13
|
+
*/
|
|
14
|
+
export function dataUriToBuffer(uri: string): MimeBuffer {
|
|
15
|
+
if (!/^data:/i.test(uri)) {
|
|
16
|
+
throw new TypeError(
|
|
17
|
+
'`uri` does not appear to be a Data URI (must begin with "data:")'
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// strip newlines
|
|
22
|
+
uri = uri.replace(/\r?\n/g, '');
|
|
23
|
+
|
|
24
|
+
// split the URI up into the "metadata" and the "data" portions
|
|
25
|
+
const firstComma = uri.indexOf(',');
|
|
26
|
+
if (firstComma === -1 || firstComma <= 4) {
|
|
27
|
+
throw new TypeError('malformed data: URI');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// remove the "data:" scheme and parse the metadata
|
|
31
|
+
const meta = uri.substring(5, firstComma).split(';');
|
|
32
|
+
|
|
33
|
+
let charset = '';
|
|
34
|
+
let base64 = false;
|
|
35
|
+
const type = meta[0] || 'text/plain';
|
|
36
|
+
let typeFull = type;
|
|
37
|
+
for (let i = 1; i < meta.length; i++) {
|
|
38
|
+
if (meta[i] === 'base64') {
|
|
39
|
+
base64 = true;
|
|
40
|
+
} else if(meta[i]) {
|
|
41
|
+
typeFull += `;${ meta[i]}`;
|
|
42
|
+
if (meta[i].indexOf('charset=') === 0) {
|
|
43
|
+
charset = meta[i].substring(8);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// defaults to US-ASCII only if type is not provided
|
|
48
|
+
if (!meta[0] && !charset.length) {
|
|
49
|
+
typeFull += ';charset=US-ASCII';
|
|
50
|
+
charset = 'US-ASCII';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// get the encoded data portion and decode URI-encoded chars
|
|
54
|
+
const encoding = base64 ? 'base64' : 'ascii';
|
|
55
|
+
const data = unescape(uri.substring(firstComma + 1));
|
|
56
|
+
const buffer = Buffer.from(data, encoding) as MimeBuffer;
|
|
57
|
+
|
|
58
|
+
// set `.type` and `.typeFull` properties to MIME type
|
|
59
|
+
buffer.type = type;
|
|
60
|
+
buffer.typeFull = typeFull;
|
|
61
|
+
|
|
62
|
+
// set the `.charset` property
|
|
63
|
+
buffer.charset = charset;
|
|
64
|
+
|
|
65
|
+
return buffer;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default dataUriToBuffer;
|