data-uri-to-buffer
Advanced tools
Comparing version 6.0.1 to 6.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dataUriToBuffer = void 0; | ||
const common_1 = require("./common"); | ||
function base64ToArrayBuffer(base64) { | ||
@@ -46,47 +47,3 @@ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
*/ | ||
function dataUriToBuffer(uri) { | ||
uri = String(uri); | ||
if (!/^data:/i.test(uri)) { | ||
throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")'); | ||
} | ||
// strip newlines | ||
uri = uri.replace(/\r?\n/g, ''); | ||
// split the URI up into the "metadata" and the "data" portions | ||
const firstComma = uri.indexOf(','); | ||
if (firstComma === -1 || firstComma <= 4) { | ||
throw new TypeError('malformed data: URI'); | ||
} | ||
// remove the "data:" scheme and parse the metadata | ||
const meta = uri.substring(5, firstComma).split(';'); | ||
let charset = ''; | ||
let base64 = false; | ||
const type = meta[0] || 'text/plain'; | ||
let typeFull = type; | ||
for (let i = 1; i < meta.length; i++) { | ||
if (meta[i] === 'base64') { | ||
base64 = true; | ||
} | ||
else if (meta[i]) { | ||
typeFull += `;${meta[i]}`; | ||
if (meta[i].indexOf('charset=') === 0) { | ||
charset = meta[i].substring(8); | ||
} | ||
} | ||
} | ||
// defaults to US-ASCII only if type is not provided | ||
if (!meta[0] && !charset.length) { | ||
typeFull += ';charset=US-ASCII'; | ||
charset = 'US-ASCII'; | ||
} | ||
// get the encoded data portion and decode URI-encoded chars | ||
const data = unescape(uri.substring(firstComma + 1)); | ||
const buffer = base64 ? base64ToArrayBuffer(data) : stringToBuffer(data); | ||
return { | ||
type, | ||
typeFull, | ||
charset, | ||
buffer, | ||
}; | ||
} | ||
exports.dataUriToBuffer = dataUriToBuffer; | ||
exports.dataUriToBuffer = (0, common_1.makeDataUriToBuffer)({ stringToBuffer, base64ToArrayBuffer }); | ||
//# sourceMappingURL=index.js.map |
@@ -1,7 +0,2 @@ | ||
export interface ParsedDataURI { | ||
type: string; | ||
typeFull: string; | ||
charset: string; | ||
buffer: ArrayBuffer; | ||
} | ||
export type { ParsedDataURI } from './common'; | ||
/** | ||
@@ -12,3 +7,3 @@ * Returns a `Buffer` instance from the given data URI `uri`. | ||
*/ | ||
export declare function dataUriToBuffer(uri: string | URL): ParsedDataURI; | ||
export declare const dataUriToBuffer: (uri: string | URL) => import("./common").ParsedDataURI; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dataUriToBuffer = void 0; | ||
const common_1 = require("./common"); | ||
function base64ToArrayBuffer(base64) { | ||
@@ -46,47 +47,3 @@ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
*/ | ||
function dataUriToBuffer(uri) { | ||
uri = String(uri); | ||
if (!/^data:/i.test(uri)) { | ||
throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")'); | ||
} | ||
// strip newlines | ||
uri = uri.replace(/\r?\n/g, ''); | ||
// split the URI up into the "metadata" and the "data" portions | ||
const firstComma = uri.indexOf(','); | ||
if (firstComma === -1 || firstComma <= 4) { | ||
throw new TypeError('malformed data: URI'); | ||
} | ||
// remove the "data:" scheme and parse the metadata | ||
const meta = uri.substring(5, firstComma).split(';'); | ||
let charset = ''; | ||
let base64 = false; | ||
const type = meta[0] || 'text/plain'; | ||
let typeFull = type; | ||
for (let i = 1; i < meta.length; i++) { | ||
if (meta[i] === 'base64') { | ||
base64 = true; | ||
} | ||
else if (meta[i]) { | ||
typeFull += `;${meta[i]}`; | ||
if (meta[i].indexOf('charset=') === 0) { | ||
charset = meta[i].substring(8); | ||
} | ||
} | ||
} | ||
// defaults to US-ASCII only if type is not provided | ||
if (!meta[0] && !charset.length) { | ||
typeFull += ';charset=US-ASCII'; | ||
charset = 'US-ASCII'; | ||
} | ||
// get the encoded data portion and decode URI-encoded chars | ||
const data = unescape(uri.substring(firstComma + 1)); | ||
const buffer = base64 ? base64ToArrayBuffer(data) : stringToBuffer(data); | ||
return { | ||
type, | ||
typeFull, | ||
charset, | ||
buffer, | ||
}; | ||
} | ||
exports.dataUriToBuffer = dataUriToBuffer; | ||
exports.dataUriToBuffer = (0, common_1.makeDataUriToBuffer)({ stringToBuffer, base64ToArrayBuffer }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "data-uri-to-buffer", | ||
"version": "6.0.1", | ||
"version": "6.0.2", | ||
"description": "Create an ArrayBuffer instance from a Data URI string", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"node": "./dist/node.js", | ||
"default": "./dist/index.js" | ||
}, | ||
"files": [ | ||
@@ -8,0 +12,0 @@ "dist" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16799
16
202
1