@readme/data-urls 1.0.0
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/CHANGELOG.md +7 -0
- package/LICENSE +67 -0
- package/README.md +37 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +45 -0
- package/package.json +53 -0
- package/src/index.ts +62 -0
- package/tsconfig.json +12 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Copyright (c) 2022, ReadMe
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
4
|
+
with or without fee is hereby granted, provided that the above copyright notice
|
|
5
|
+
and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
9
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
11
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
12
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
13
|
+
THIS SOFTWARE.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## parse-data-url
|
|
18
|
+
https://github.com/killmenot/parse-data-url
|
|
19
|
+
|
|
20
|
+
The MIT License (MIT)
|
|
21
|
+
|
|
22
|
+
Copyright (c) Alexey Kucherenko
|
|
23
|
+
|
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
25
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
26
|
+
in the Software without restriction, including without limitation the rights
|
|
27
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
28
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
29
|
+
furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all
|
|
32
|
+
copies or substantial portions of the Software.
|
|
33
|
+
|
|
34
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
35
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
37
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
38
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
39
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
40
|
+
SOFTWARE.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## valid-data-url
|
|
45
|
+
https://github.com/killmenot/valid-data-url
|
|
46
|
+
|
|
47
|
+
The MIT License (MIT)
|
|
48
|
+
|
|
49
|
+
Copyright (c) Alexey Kucherenko
|
|
50
|
+
|
|
51
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
52
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
53
|
+
in the Software without restriction, including without limitation the rights
|
|
54
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
55
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
56
|
+
furnished to do so, subject to the following conditions:
|
|
57
|
+
|
|
58
|
+
The above copyright notice and this permission notice shall be included in all
|
|
59
|
+
copies or substantial portions of the Software.
|
|
60
|
+
|
|
61
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
62
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
63
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
64
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
65
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
66
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
67
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @readme/data-urls
|
|
2
|
+
A utility for parsing and validating [data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs).
|
|
3
|
+
|
|
4
|
+
[](https://github.com/readmeio/data-urls/)
|
|
5
|
+
[](https://npm.im/@readme/data-urls)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
[](https://readme.io)
|
|
9
|
+
|
|
10
|
+
This library is a loose fork of [parse-data-url](https://npm.im/parse-data-url) and [valid-data-url](https://npm.im/valid-data-url).
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
npm install --save @readme/data-urls
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
```js
|
|
20
|
+
import { parse, validate } from '@readme/data-urls';
|
|
21
|
+
|
|
22
|
+
const url = 'data:image/png;name=owlbert-shrub.png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACNUlEQVR4AWJkIA2wCQHqJgcouZ0ADv+SmayR28PibPOP2jafzdp6Lh/v2bVt2+1zbdvtbtbMpGfmvmD8jStc9uB3v75gdNmqtJqsa6qCDEBPRVH7XpKk1P9yHLZv3mG6keV9BIsWGqNuvNaiy+YoARFIXFXwf6Frjz/X9oS4LNRkty4QH/0MSnnWDHORHV3pJsiozRk0zWW/9MEi0A9ffqOhf5RRDW6SGLT/ZXcT8C1hWvao8iF1c4ecKx5eejW1bii1y1asW14P5XsUNkogmvVQg2aPLOnrqss9qE81OYlAucRrN/LrqlBSWYufv34jq7gKseA7MCr0IOhffForGsTWDBJnqE2/B0H+hKRRG3DtxEGk6QBZQ9UFClMSnTJe/cCVI5uQJJpx+8AyOBUPPv+JIpJjgJqCZzKT0AGdAgT/1OLtSzsKI2/gspnxvGExZYFADcoSsqM10SADF4jC4TCCUDN8nih+eoIgPAdGeajBNxyMH20JwsPNMwSicUjBCAwmAZ6GkCmA7I+qCxTGRHSkVzZiCRmKAsRkuVmWiENrt6gL5Di7iw4oogFSINIkCTU09hNAP64CREPUBbzAx9ABrc2I3w2NeY5DMByHu8AGY3YyeoKIuSmBWCD6OuqLPJXe/Dpvkb3FuoDbdENKuBVJ4nxZJpKgWoUXCMcYUwBwDfR8FxZPN4wfWK2Zunq7tGLmZMOc7x7ZUn/RuDmrX0H/4FffQ2teylCecuWmDNsTnainfwFPuNZTR3MemwAAAABJRU5ErkJggg==';
|
|
23
|
+
|
|
24
|
+
console.log(validate(url)); // true
|
|
25
|
+
console.log(parse(url));
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
{
|
|
30
|
+
mediaType: 'image/png;name=owlbert-shrub.png',
|
|
31
|
+
contentType: 'image/png',
|
|
32
|
+
name: 'owlbert-shrub.png',
|
|
33
|
+
base64: true,
|
|
34
|
+
data: 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACNUlEQVR4AWJkIA2wCQHqJgcouZ0ADv+SmayR28PibPOP2jafzdp6Lh/v2bVt2+1zbdvtbtbMpGfmvmD8jStc9uB3v75gdNmqtJqsa6qCDEBPRVH7XpKk1P9yHLZv3mG6keV9BIsWGqNuvNaiy+YoARFIXFXwf6Frjz/X9oS4LNRkty4QH/0MSnnWDHORHV3pJsiozRk0zWW/9MEi0A9ffqOhf5RRDW6SGLT/ZXcT8C1hWvao8iF1c4ecKx5eejW1bii1y1asW14P5XsUNkogmvVQg2aPLOnrqss9qE81OYlAucRrN/LrqlBSWYufv34jq7gKseA7MCr0IOhffForGsTWDBJnqE2/B0H+hKRRG3DtxEGk6QBZQ9UFClMSnTJe/cCVI5uQJJpx+8AyOBUPPv+JIpJjgJqCZzKT0AGdAgT/1OLtSzsKI2/gspnxvGExZYFADcoSsqM10SADF4jC4TCCUDN8nih+eoIgPAdGeajBNxyMH20JwsPNMwSicUjBCAwmAZ6GkCmA7I+qCxTGRHSkVzZiCRmKAsRkuVmWiENrt6gL5Di7iw4oogFSINIkCTU09hNAP64CREPUBbzAx9ABrc2I3w2NeY5DMByHu8AGY3YyeoKIuSmBWCD6OuqLPJXe/Dpvkb3FuoDbdENKuBVJ4nxZJpKgWoUXCMcYUwBwDfR8FxZPN4wfWK2Zunq7tGLmZMOc7x7ZUn/RuDmrX0H/4FffQ2teylCecuWmDNsTnainfwFPuNZTR3MemwAAAABJRU5ErkJggg==',
|
|
35
|
+
toBuffer: [Function (anonymous)]
|
|
36
|
+
}
|
|
37
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/**
|
|
3
|
+
* Determine if a given data URL is valid or not.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/data_URIs}
|
|
6
|
+
* @see {@link http://tools.ietf.org/html/rfc2397}
|
|
7
|
+
* @see {@link http://tools.ietf.org/html/rfc2396#section2}
|
|
8
|
+
*/
|
|
9
|
+
declare function validate(str: string): boolean;
|
|
10
|
+
export declare type DataURL = {
|
|
11
|
+
mediaType?: string;
|
|
12
|
+
contentType?: string;
|
|
13
|
+
base64: boolean;
|
|
14
|
+
data: string;
|
|
15
|
+
toBuffer: () => Buffer;
|
|
16
|
+
[k: string]: any;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Parse a given data URL into its individual parts.
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
declare function parse(str: string): false | DataURL;
|
|
23
|
+
export { parse, validate };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.validate = exports.parse = void 0;
|
|
4
|
+
var DATA_URL_REGEX =
|
|
5
|
+
// eslint-disable-next-line unicorn/no-unsafe-regex
|
|
6
|
+
/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s<>]*?)$/i;
|
|
7
|
+
/**
|
|
8
|
+
* Determine if a given data URL is valid or not.
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/data_URIs}
|
|
11
|
+
* @see {@link http://tools.ietf.org/html/rfc2397}
|
|
12
|
+
* @see {@link http://tools.ietf.org/html/rfc2396#section2}
|
|
13
|
+
*/
|
|
14
|
+
function validate(str) {
|
|
15
|
+
return DATA_URL_REGEX.test((str || '').trim());
|
|
16
|
+
}
|
|
17
|
+
exports.validate = validate;
|
|
18
|
+
/**
|
|
19
|
+
* Parse a given data URL into its individual parts.
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
function parse(str) {
|
|
23
|
+
if (!validate(str)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
var parts = str.trim().match(DATA_URL_REGEX);
|
|
27
|
+
var parsed = {};
|
|
28
|
+
if (parts[1]) {
|
|
29
|
+
parsed.mediaType = parts[1].toLowerCase();
|
|
30
|
+
var mediaTypeParts = parts[1].split(';').map(function (x) { return x.toLowerCase(); });
|
|
31
|
+
parsed.contentType = mediaTypeParts[0];
|
|
32
|
+
mediaTypeParts.slice(1).forEach(function (attribute) {
|
|
33
|
+
var p = attribute.split('=');
|
|
34
|
+
parsed[p[0]] = p[1];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
parsed.base64 = !!parts[parts.length - 2];
|
|
38
|
+
parsed.data = parts[parts.length - 1] || '';
|
|
39
|
+
parsed.toBuffer = function () {
|
|
40
|
+
var encoding = parsed.base64 ? 'base64' : 'utf8';
|
|
41
|
+
return Buffer.from(parsed.data, encoding);
|
|
42
|
+
};
|
|
43
|
+
return parsed;
|
|
44
|
+
}
|
|
45
|
+
exports.parse = parse;
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@readme/data-urls",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A utility for parsing and validating data URLs.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=14"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"lint": "eslint . --ext .js,.ts",
|
|
13
|
+
"prebuild": "rm -rf dist/",
|
|
14
|
+
"prepack": "npm run build",
|
|
15
|
+
"pretest": "npm run lint",
|
|
16
|
+
"prettier": "prettier --list-different --write \"./**/**.{js,ts}\"",
|
|
17
|
+
"release": "npx conventional-changelog-cli -i CHANGELOG.md -s",
|
|
18
|
+
"test:browser": "karma start --single-run",
|
|
19
|
+
"test:browser:chrome": "karma start --browsers=Chrome --single-run=false",
|
|
20
|
+
"test:browser:debug": "karma start --single-run=false",
|
|
21
|
+
"test": "nyc mocha \"test/**/*.test.ts\""
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git@github.com:readmeio/data-urls.git"
|
|
26
|
+
},
|
|
27
|
+
"license": "ISC",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/readmeio/data-urls/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/readmeio/data-urls#readme",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@jsdevtools/host-environment": "^2.1.2",
|
|
34
|
+
"@jsdevtools/karma-config": "^3.1.7",
|
|
35
|
+
"@readme/eslint-config": "^9.0.0",
|
|
36
|
+
"@types/chai": "^4.3.1",
|
|
37
|
+
"@types/mocha": "^9.1.1",
|
|
38
|
+
"@types/node": "^18.0.0",
|
|
39
|
+
"chai": "^4.3.4",
|
|
40
|
+
"eslint": "^8.14.0",
|
|
41
|
+
"mocha": "^10.0.0",
|
|
42
|
+
"nyc": "^15.1.0",
|
|
43
|
+
"prettier": "^2.6.2",
|
|
44
|
+
"ts-loader": "^8.4.0",
|
|
45
|
+
"ts-node": "^10.7.0",
|
|
46
|
+
"typescript": "^4.6.3",
|
|
47
|
+
"webpack": "^4.46.0"
|
|
48
|
+
},
|
|
49
|
+
"browserslist": [
|
|
50
|
+
"last 2 versions"
|
|
51
|
+
],
|
|
52
|
+
"prettier": "@readme/eslint-config/prettier"
|
|
53
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const DATA_URL_REGEX =
|
|
2
|
+
// eslint-disable-next-line unicorn/no-unsafe-regex
|
|
3
|
+
/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s<>]*?)$/i;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Determine if a given data URL is valid or not.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/data_URIs}
|
|
9
|
+
* @see {@link http://tools.ietf.org/html/rfc2397}
|
|
10
|
+
* @see {@link http://tools.ietf.org/html/rfc2396#section2}
|
|
11
|
+
*/
|
|
12
|
+
function validate(str: string) {
|
|
13
|
+
return DATA_URL_REGEX.test((str || '').trim());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type DataURL = {
|
|
17
|
+
mediaType?: string;
|
|
18
|
+
contentType?: string;
|
|
19
|
+
base64: boolean;
|
|
20
|
+
data: string;
|
|
21
|
+
toBuffer: () => Buffer;
|
|
22
|
+
[k: string]: any;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parse a given data URL into its individual parts.
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
29
|
+
function parse(str: string) {
|
|
30
|
+
if (!validate(str)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const parts = str.trim().match(DATA_URL_REGEX);
|
|
35
|
+
const parsed = {} as DataURL;
|
|
36
|
+
|
|
37
|
+
if (parts[1]) {
|
|
38
|
+
parsed.mediaType = parts[1].toLowerCase();
|
|
39
|
+
|
|
40
|
+
const mediaTypeParts = parts[1].split(';').map(x => x.toLowerCase());
|
|
41
|
+
|
|
42
|
+
parsed.contentType = mediaTypeParts[0];
|
|
43
|
+
|
|
44
|
+
mediaTypeParts.slice(1).forEach(attribute => {
|
|
45
|
+
const p = attribute.split('=');
|
|
46
|
+
parsed[p[0]] = p[1];
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
parsed.base64 = !!parts[parts.length - 2];
|
|
51
|
+
parsed.data = parts[parts.length - 1] || '';
|
|
52
|
+
|
|
53
|
+
parsed.toBuffer = () => {
|
|
54
|
+
const encoding = parsed.base64 ? 'base64' : 'utf8';
|
|
55
|
+
|
|
56
|
+
return Buffer.from(parsed.data, encoding);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return parsed;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { parse, validate };
|