# magic-types

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

Latest version **0.0.4** (published 2023-04-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install magic-types
pnpm add magic-types
yarn add magic-types
bun add magic-types
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2023-04-16 |
| First published | 2023-04-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 47.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | dyingsunlight |

## Links

- npm: https://www.npmjs.com/package/magic-types
- npm.io page: https://npm.io/package/magic-types

## Recent versions

- 0.0.4 (latest) — 2023-04-16
- 0.0.3 — 2023-04-13
- 0.0.2 — 2023-04-12
- 0.0.1 — 2023-04-12

## README

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

Get the `MIME` of the data by parsing the [Magic Number](https://en.wikipedia.org/wiki/Magic_number_(programming)) 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
```shell
npm i magic-types
```
```typescript
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
```html
<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<string>
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

---
_Source: https://npm.io/package/magic-types · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
