base64-image-mime
Advanced tools
Comparing version 1.0.0 to 1.0.1
const atob = require('atob') | ||
const jpegTypes = ["jpg", "jpeg", "jfif", "pjpeg", "pjp"] | ||
const mimeTypes = { | ||
@@ -15,3 +14,3 @@ png: 'image/png', | ||
function getImageMime(base64Encoded) { | ||
function getImageMime (base64Encoded) { | ||
if (base64Encoded.startsWith('data:')) { | ||
@@ -24,2 +23,4 @@ const found = base64Encoded.match(/(?<=data:)\S*(?=;base64)/g) | ||
if (!found) { | ||
const hex = Buffer.from(base64Encoded, 'base64').toString('hex') | ||
if (hex.startsWith('ffd8ff')) return mimeTypes.jpeg | ||
return null | ||
@@ -33,5 +34,4 @@ } else { | ||
module.exports = { | ||
getImageMime: getImageMime | ||
} | ||
} |
const { getImageMime } = require('./index') | ||
const randomStr = 'dafkqlwjelkqwje' | ||
const png = 'iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACGFjVEwAAAAUAAAAABwt8VIAAAAaZmNUTAAAAAAAAABkAAAAZAAAAAAAAAAAAEsD6AEAzUsGtwAAEhFJREFUeNrtmwuMHVd5x3'; | ||
const png = 'iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAACGFjVEwAAAAUAAAAABwt8VIAAAAaZmNUTAAAAAAAAABkAAAAZAAAAAAAAAAAAEsD6AEAzUsGtwAAEhFJREFUeNrtmwuMHVd5x3' | ||
const webp = | ||
'UklGRnq0AABXRUJQVlA4IG60AABQpgKdASoCA9gAPm0qkUYkIiGhL1Tt+IANiWgAtopAV39UO6z5/+c/cH8sPlU5B7RPgH3n/L/9n2jdenYfmEdOfp/2if7/1p/0b/X+xD/Rv8P+0Xrx/uZ79f3d9Wf7nep5/2f3b+A39I9Un+tf6z//9l/6Vvm0f/D2mv7B/2fTc0zX7j43fsfWv+mfd/8r/D/6D1G/'; | ||
'UklGRnq0AABXRUJQVlA4IG60AABQpgKdASoCA9gAPm0qkUYkIiGhL1Tt+IANiWgAtopAV39UO6z5/+c/cH8sPlU5B7RPgH3n/L/9n2jdenYfmEdOfp/2if7/1p/0b/X+xD/Rv8P+0Xrx/uZ79f3d9Wf7nep5/2f3b+A39I9Un+tf6z//9l/6Vvm0f/D2mv7B/2fTc0zX7j43fsfWv+mfd/8r/D/6D1G/' | ||
const jpeg = | ||
"/9j/4AAQSkZJRgABAQEAYABgAAD/4QBmRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAEAAAMBAAUAAAABAAAAVgMDAAEAAAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAOw1ESAAQAAAABA"; | ||
'/9j/4AAQSkZJRgABAQEAYABgAAD/4QBmRXhpZgAATU0AKgAAAAgABgESAAMAAAABAAEAAAMBAAUAAAABAAAAVgMDAAEAAAABAAAAAFEQAAEAAAABAQAAAFERAAQAAAABAAAOw1ESAAQAAAABA' | ||
const gif = | ||
"R0lGODlhsASvBPcAAAQBAQ0MDAsGBBIODBUIBRQRDhoTDg4OERIOERQUFRoVFBsZFhYWGBkXGRsbHBYZGwoSFyIdGyQaFi0UD"; | ||
'R0lGODlhsASvBPcAAAQBAQ0MDAsGBBIODBUIBRQRDhoTDg4OERIOERQUFRoVFBsZFhYWGBkXGRsbHBYZGwoSFyIdGyQaFi0UD' | ||
const prefixPng = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA' | ||
const prefixJpeg = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/' | ||
const prefixGif = 'data:image/gif;base64,R0lGODlh4AHgAfcBMQDO9wD/AAECAQHlyQL2mQM3EgSLUAfO6QkIBwu78g8NDQ8fGRAKBhAPEhQTEhcWFxcaGRkYGBkZGRwXFh4dGx4dIB8jbh/CjyAeHSDumiFolCIeHSKJiiMjKiMuTSQcGSS+vCYkIief7ygcGCgoLClKXiolIissNC0WES0mIy4qKi' | ||
const hexTestJpeg = '/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgk' | ||
test('return null on non base64', () => { | ||
expect(getImageMime(randomStr)).toBe(null); | ||
}); | ||
expect(getImageMime(randomStr)).toBe(null) | ||
}) | ||
test('parse png success', () => { | ||
expect(getImageMime(png)).toBe('image/png'); | ||
expect(getImageMime(prefixPng)).toBe('image/png'); | ||
}); | ||
expect(getImageMime(png)).toBe('image/png') | ||
expect(getImageMime(prefixPng)).toBe('image/png') | ||
}) | ||
test('parse webp success', () => { | ||
expect(getImageMime(webp)).toBe('image/webp'); | ||
}); | ||
expect(getImageMime(webp)).toBe('image/webp') | ||
}) | ||
test('parse gif success', () => { | ||
expect(getImageMime(gif)).toBe('image/gif'); | ||
expect(getImageMime(prefixGif)).toBe('image/gif'); | ||
}); | ||
expect(getImageMime(gif)).toBe('image/gif') | ||
expect(getImageMime(prefixGif)).toBe('image/gif') | ||
}) | ||
test('parse jpeg success', () => { | ||
expect(getImageMime(jpeg)).toBe('image/jpeg'); | ||
expect(getImageMime(prefixJpeg)).toBe('image/jpeg'); | ||
}); | ||
expect(getImageMime(jpeg)).toBe('image/jpeg') | ||
expect(getImageMime(hexTestJpeg)).toBe('image/jpeg') | ||
expect(getImageMime(prefixJpeg)).toBe('image/jpeg') | ||
}) |
{ | ||
"name": "base64-image-mime", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Get mimetype from base64-encoded images", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Base64-image-mime | ||
## A small utility to get mimetype from base64-encoded images | ||
### Currently support: | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
### Currently support mime-type: | ||
- image/png | ||
@@ -12,5 +12,13 @@ - image/gif | ||
Works for with prefix & without prefix base64 encoded image | ||
### Installation | ||
``` | ||
npm install base64-image-mime | ||
// or with yarn | ||
yarn add base64-image-mime | ||
``` | ||
### Usage | ||
Can detect both with prefix & without prefix base64 encoded image | ||
```js | ||
@@ -17,0 +25,0 @@ const { getImageMime } = require('base64-image-mime') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6177
5
71
48
1
1