Socket
Book a DemoInstallSign in
Socket

magic-types

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-types

[简体中文](readme.zh-cn.md)

0.0.4
latest
npmnpm
Version published
Maintainers
1
Created
Source

Magic Types

简体中文

Get the MIME of the data by parsing the Magic Number in the file header, supporting most common file types.

Features

  • Supports browsers and Nodejs
  • Efficient and no need for complete data, can infer MIME directly from url/Base64/ArrayBuffer data
  • Simple and small (5kb), no dependencies

Usage

npm module

npm i magic-types
import { fetchMime, base64Mime, arraybufferMime } from 'magic-types'

// use fetch request to read header data speculation -> image/png
console.log(await fetchMime("https://en.wikipedia.org/static/images/icons/wikipedia.png")) // -> image/png

// using base64 header data speculation -> image/png
console.log(base64Mime("iVBORw0KGgoAAAANSUhEUgAAAMgAAAC3CAMAAABg8uG4AAACNFBMVEVMaXGHh4jc3N6bm5yNjo6Vlpjh4eOnqKuysrPW19mwsbOtra5vb2 /Fxsd4eHiBg4SY"))

// use ArrayBuffer header data speculation -> image/png
const res = await fetch("https://en.wikipedia.org/static/images/icons/wikipedia.png")
const buffer = await res.arrayBuffer()
console.log(arraybufferMime(buffer))

From CDN

<script src="https://unpkg.com/magic-types/dist/magic-types.umd.js"></script>
<!-- or https://www.jsdelivr.com/npm/magic-types/dist/umd/magic-types.umd.js -->

Document

base64Mime(base64String: string): string

Use base64 headers to infer the document MIME type

fetchMime(url: string, requestInit: RequestInit): Promise

For fetch requests, it is recommended to use Content-Type as the MIME type in preference to using this method blindly.

arraybufferMime(arraybuffer: ArrayBuffer): string

Using ArrayBuffer header data speculation

License

MIT

FAQs

Package last updated on 16 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.