Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

data-uri-to-buffer

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

data-uri-to-buffer - npm Package Compare versions

Comparing version 5.0.1 to 6.0.0

LICENSE

56

./dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dataUriToBuffer = void 0;
function base64ToArrayBuffer(base64) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const bytes = [];
for (let i = 0; i < base64.length; i += 4) {
const idx0 = chars.indexOf(base64.charAt(i));
const idx1 = chars.indexOf(base64.charAt(i + 1));
const idx2 = base64.charAt(i + 2) === '='
? 0
: chars.indexOf(base64.charAt(i + 2));
const idx3 = base64.charAt(i + 3) === '='
? 0
: chars.indexOf(base64.charAt(i + 3));
const bin0 = (idx0 << 2) | (idx1 >> 4);
const bin1 = ((idx1 & 15) << 4) | (idx2 >> 2);
const bin2 = ((idx2 & 3) << 6) | idx3;
bytes.push(bin0);
if (base64.charAt(i + 2) !== '=')
bytes.push(bin1);
if (base64.charAt(i + 3) !== '=')
bytes.push(bin2);
}
const buffer = new ArrayBuffer(bytes.length);
const view = new Uint8Array(buffer);
view.set(bytes);
return buffer;
}
function stringToBuffer(str) {
// Create a buffer with length equal to the string length
const buffer = new ArrayBuffer(str.length);
// Create a view to manipulate the buffer content
const view = new Uint8Array(buffer);
// Iterate over the string and populate the buffer with ASCII codes
for (let i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return buffer;
}
/**

@@ -8,6 +45,5 @@ * Returns a `Buffer` instance from the given data URI `uri`.

* @param {String} uri Data URI to turn into a Buffer instance
* @returns {Buffer} Buffer instance from Data URI
* @api public
*/
function dataUriToBuffer(uri) {
uri = String(uri);
if (!/^data:/i.test(uri)) {

@@ -46,14 +82,12 @@ throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")');

// get the encoded data portion and decode URI-encoded chars
const encoding = base64 ? 'base64' : 'ascii';
const data = unescape(uri.substring(firstComma + 1));
const buffer = Buffer.from(data, encoding);
// set `.type` and `.typeFull` properties to MIME type
buffer.type = type;
buffer.typeFull = typeFull;
// set the `.charset` property
buffer.charset = charset;
return buffer;
const buffer = base64 ? base64ToArrayBuffer(data) : stringToBuffer(data);
return {
type,
typeFull,
charset,
buffer,
};
}
exports.dataUriToBuffer = dataUriToBuffer;
exports.default = dataUriToBuffer;
//# sourceMappingURL=index.js.map

8

dist/index.d.ts
/// <reference types="node" />
export interface MimeBuffer extends Buffer {
export interface ParsedDataURI {
type: string;
typeFull: string;
charset: string;
buffer: ArrayBuffer;
}

@@ -11,7 +12,4 @@ /**

* @param {String} uri Data URI to turn into a Buffer instance
* @returns {Buffer} Buffer instance from Data URI
* @api public
*/
export declare function dataUriToBuffer(uri: string): MimeBuffer;
export default dataUriToBuffer;
export declare function dataUriToBuffer(uri: string | URL): ParsedDataURI;
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.dataUriToBuffer = void 0;
function base64ToArrayBuffer(base64) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const bytes = [];
for (let i = 0; i < base64.length; i += 4) {
const idx0 = chars.indexOf(base64.charAt(i));
const idx1 = chars.indexOf(base64.charAt(i + 1));
const idx2 = base64.charAt(i + 2) === '='
? 0
: chars.indexOf(base64.charAt(i + 2));
const idx3 = base64.charAt(i + 3) === '='
? 0
: chars.indexOf(base64.charAt(i + 3));
const bin0 = (idx0 << 2) | (idx1 >> 4);
const bin1 = ((idx1 & 15) << 4) | (idx2 >> 2);
const bin2 = ((idx2 & 3) << 6) | idx3;
bytes.push(bin0);
if (base64.charAt(i + 2) !== '=')
bytes.push(bin1);
if (base64.charAt(i + 3) !== '=')
bytes.push(bin2);
}
const buffer = new ArrayBuffer(bytes.length);
const view = new Uint8Array(buffer);
view.set(bytes);
return buffer;
}
function stringToBuffer(str) {
// Create a buffer with length equal to the string length
const buffer = new ArrayBuffer(str.length);
// Create a view to manipulate the buffer content
const view = new Uint8Array(buffer);
// Iterate over the string and populate the buffer with ASCII codes
for (let i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return buffer;
}
/**

@@ -8,6 +45,5 @@ * Returns a `Buffer` instance from the given data URI `uri`.

* @param {String} uri Data URI to turn into a Buffer instance
* @returns {Buffer} Buffer instance from Data URI
* @api public
*/
function dataUriToBuffer(uri) {
uri = String(uri);
if (!/^data:/i.test(uri)) {

@@ -46,14 +82,12 @@ throw new TypeError('`uri` does not appear to be a Data URI (must begin with "data:")');

// get the encoded data portion and decode URI-encoded chars
const encoding = base64 ? 'base64' : 'ascii';
const data = unescape(uri.substring(firstComma + 1));
const buffer = Buffer.from(data, encoding);
// set `.type` and `.typeFull` properties to MIME type
buffer.type = type;
buffer.typeFull = typeFull;
// set the `.charset` property
buffer.charset = charset;
return buffer;
const buffer = base64 ? base64ToArrayBuffer(data) : stringToBuffer(data);
return {
type,
typeFull,
charset,
buffer,
};
}
exports.dataUriToBuffer = dataUriToBuffer;
exports.default = dataUriToBuffer;
//# sourceMappingURL=index.js.map
{
"name": "data-uri-to-buffer",
"version": "5.0.1",
"description": "Generate a Buffer instance from a Data URI string",
"version": "6.0.0",
"description": "Create an ArrayBuffer instance from a Data URI string",
"main": "./dist/index.js",

@@ -6,0 +6,0 @@ "types": "./dist/index.d.ts",

data-uri-to-buffer
==================
### Generate a Buffer instance from a [Data URI][rfc] string
### Create an ArrayBuffer instance from a [Data URI][rfc] string
This module accepts a ["data" URI][rfc] String of data, and returns a
node.js `Buffer` instance with the decoded data.
This module accepts a ["data" URI][rfc] String of data, and returns
an `ArrayBuffer` instance with the decoded data.
This module is intended to work on a large variety of JavaScript
runtimes, including Node.js and web browsers.
Example
-------
``` js
```typescript
import { dataUriToBuffer } from 'data-uri-to-buffer';

@@ -16,4 +19,4 @@

let uri = 'data:,Hello%2C%20World!';
let decoded = dataUriToBuffer(uri);
console.log(decoded.toString());
let parsed = dataUriToBuffer(uri);
console.log(new TextDecoder().decode(parsed.buffer));
// 'Hello, World!'

@@ -23,4 +26,4 @@

uri = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D';
decoded = dataUriToBuffer(uri);
console.log(decoded.toString());
parsed = dataUriToBuffer(uri);
console.log(new TextDecoder().decode(parsed.buffer));
// 'Hello, World!'

@@ -33,47 +36,29 @@ ```

### dataUriToBuffer(String uri) → Buffer
```typescript
export interface ParsedDataURI {
type: string;
typeFull: string;
charset: string;
buffer: ArrayBuffer;
}
```
The `type` property on the Buffer instance gets set to the main type portion of
### dataUriToBuffer(uri: string | URL) → ParsedDataURI
The `type` property gets set to the main type portion of
the "mediatype" portion of the "data" URI, or defaults to `"text/plain"` if not
specified.
The `typeFull` property on the Buffer instance gets set to the entire
The `typeFull` property gets set to the entire
"mediatype" portion of the "data" URI (including all parameters), or defaults
to `"text/plain;charset=US-ASCII"` if not specified.
The `charset` property on the Buffer instance gets set to the Charset portion of
The `charset` property gets set to the Charset portion of
the "mediatype" portion of the "data" URI, or defaults to `"US-ASCII"` if the
entire type is not specified, or defaults to `""` otherwise.
*Note*: If the only the main type is specified but not the charset, e.g.
*Note*: If only the main type is specified but not the charset, e.g.
`"data:text/plain,abc"`, the charset is set to the empty string. The spec only
defaults to US-ASCII as charset if the entire type is not specified.
License
-------
(The MIT License)
Copyright (c) 2014 Nathan Rajlich &lt;nathan@tootallnate.net&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[rfc]: http://tools.ietf.org/html/rfc2397

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc