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

@c8/simple-promised-file

Package Overview
Dependencies
Maintainers
5
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@c8/simple-promised-file - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

examples/1.jpg

20

examples/image.js
'use strict'
const path = require('path')
const image = require('../lib/image')
const Image = require('../lib/image')
image(path.join(__dirname, '1.jpg'))
Image(path.join(__dirname, '1.jpg'))
.then((img) => {
// We still have inherited File getters available
console.log(img.size)
console.log(img.mime)
img.mime
.then((mime) => {
console.log(`Mime: ${mime}`)
})
.catch((err) => {
console.log(err)
})
img.isImage
.then((result) => {
console.log(`IsImage: ${result}`)
})
.catch((err) => {
console.log(err)
})
// Async getters

@@ -13,0 +27,0 @@ img

@@ -5,2 +5,3 @@ 'use strict'

const mime = require('mime')
const createError = require('create-error')

@@ -40,4 +41,18 @@ /**

/**
* @returns {ReadError} Cannot read a file in this.path
*/
static get ReadError () {
return createError('ReadError')
}
/**
* @returns {TypeError} Invalid file type
*/
static get TypeError () {
return createError('TypeError')
}
}
module.exports = AbstractFile

@@ -5,2 +5,4 @@ 'use strict'

const AbstractFile = require('./abstract-file')
const fileType = require('file-type')
const readChunk = require('read-chunk')

@@ -27,3 +29,3 @@ /**

.catch((err) => {
reject(err)
reject(new File.ReadError(err.message))
})

@@ -45,3 +47,3 @@ })

} else {
reject('Invalid file path provided')
reject(`Error reading file stats for ${this.path}`)
}

@@ -51,4 +53,60 @@ })

}
/**
* First try to determine mime by reading magic bytes, it that fails, falls back to extension check.
*
* @returns {Promise}
*/
get mime () {
return new Promise((resolve, reject) => {
this.magicBytesMime
.then((mime) => {
resolve(mime)
})
.catch(() => {
resolve(super.mime)
})
})
}
/**
* Get mime type by reading magic bytes.
*
* @returns {Promise}
*/
get magicBytesMime () {
return new Promise((resolve, reject) => {
readChunk(this.path, 0, 262)
.then((buffer) => {
let type = fileType(buffer)
if (type.mime) {
resolve(type.mime)
} else {
reject(null)
}
})
.catch((err) => {
reject(err)
})
})
}
/**
* Checks whether tthe file is an image based on magic number on start of the file (file-type npm package)
*
* @return {Boolean} [description]
*/
get isImage () {
return new Promise((resolve, reject) => {
this.magicBytesMime
.then((mime) => {
resolve(mime.split('/')[0] === 'image')
})
.catch(() => {
resolve(false) // for now sufficient
})
})
}
}
module.exports = File

@@ -47,4 +47,12 @@ 'use strict'

return new Promise((resolve, reject) => {
let image
new File(path)
.then((img) => {
image = img
return img.isImage
})
.then((isImage) => {
if (!isImage) throw new File.TypeError('This file is not an image')
let resource = gm(path)

@@ -62,5 +70,5 @@

_.extend(img, resource)
_.extend(image, resource)
resolve(img)
resolve(image)
})

@@ -67,0 +75,0 @@ .catch((err) => {

7

package.json
{
"name": "@c8/simple-promised-file",
"version": "0.0.4",
"version": "0.0.5",
"description": "A small promise library with basic file functionality and promise-wrapped gm image module.",

@@ -28,6 +28,9 @@ "main": "index.js",

"dependencies": {
"create-error": "^0.3.1",
"file-type": "^3.8.0",
"filesize": "^3.2.1",
"gm": "^1.22.0",
"lodash": "^4.11.1",
"mime": "^1.3.4"
"mime": "^1.3.4",
"read-chunk": "^2.0.0"
},

@@ -34,0 +37,0 @@ "devDependencies": {

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