edge64 0.0.0-stage → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -2
- package/package.json +26 -4
- package/src/index.d.ts +26 -0
- package/src/index.js +81 -0
package/README.md
CHANGED
|
@@ -1,3 +1,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# edge64
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Edge/Workers-native base64 <-> image conversion. Uses only `fetch`, `atob`/`btoa` and `ArrayBuffer` — no Node `fs`, `axios`, or `request` assumptions, so it runs on Cloudflare Workers, Deno, browsers, and Node alike.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Existing packages (`image-to-base64`, `base64-img`, `node-base64-image`) assume a Node environment with filesystem access. This library targets edge runtimes first, with MIME type detection from magic bytes rather than trusting file extensions or `content-type` headers alone.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install edge64
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { imageUrlToBase64, base64ToBytes, bytesToDataUri, sniffMimeType } from "edge64";
|
|
19
|
+
|
|
20
|
+
const { mime, base64, dataUri } = await imageUrlToBase64("https://example.com/cat.png");
|
|
21
|
+
|
|
22
|
+
const bytes = base64ToBytes(dataUri);
|
|
23
|
+
|
|
24
|
+
const uri = bytesToDataUri(bytes);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
- `sniffMimeType(bytes)` — detect `image/png`, `image/jpeg`, `image/gif`, `image/webp`, `image/avif` from magic bytes. Returns `null` if unrecognized.
|
|
30
|
+
- `imageUrlToBase64(url, init?)` — fetches a URL and returns `{ mime, base64, dataUri }`.
|
|
31
|
+
- `base64ToBytes(input)` — accepts a raw base64 string or a full data URI, returns `Uint8Array`.
|
|
32
|
+
- `bytesToDataUri(bytes, mimeHint?)` — builds a data URI, sniffing the MIME type if not provided.
|
|
33
|
+
|
|
34
|
+
## Supported formats
|
|
35
|
+
|
|
36
|
+
PNG, JPEG, GIF, WebP, AVIF — detected from magic bytes, not file extensions.
|
|
37
|
+
|
|
38
|
+
## Docs
|
|
39
|
+
|
|
40
|
+
Full documentation: https://docs.proflare.dev/npm/edge-64/
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "edge64",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Edge/Workers-native base64 <-> image conversion using fetch, with magic-byte MIME sniffing",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"base64",
|
|
16
|
+
"image",
|
|
17
|
+
"cloudflare-workers",
|
|
18
|
+
"edge",
|
|
19
|
+
"fetch"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "ProFlare",
|
|
23
|
+
"homepage": "https://docs.proflare.dev/npm/edge-64/",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/darkflareplays8/edge-64.git"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type SniffedMime =
|
|
2
|
+
| "image/png"
|
|
3
|
+
| "image/jpeg"
|
|
4
|
+
| "image/gif"
|
|
5
|
+
| "image/webp"
|
|
6
|
+
| "image/avif";
|
|
7
|
+
|
|
8
|
+
export function sniffMimeType(bytes: Uint8Array | ArrayBuffer): SniffedMime | null;
|
|
9
|
+
|
|
10
|
+
export interface ImageFetchResult {
|
|
11
|
+
mime: string;
|
|
12
|
+
base64: string;
|
|
13
|
+
dataUri: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function imageUrlToBase64(
|
|
17
|
+
url: string,
|
|
18
|
+
init?: RequestInit
|
|
19
|
+
): Promise<ImageFetchResult>;
|
|
20
|
+
|
|
21
|
+
export function base64ToBytes(input: string): Uint8Array;
|
|
22
|
+
|
|
23
|
+
export function bytesToDataUri(
|
|
24
|
+
bytes: Uint8Array | ArrayBuffer,
|
|
25
|
+
mimeHint?: string
|
|
26
|
+
): string;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
const MAGIC_BYTES = [
|
|
2
|
+
{ mime: "image/png", bytes: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a] },
|
|
3
|
+
{ mime: "image/jpeg", bytes: [0xff, 0xd8, 0xff] },
|
|
4
|
+
{ mime: "image/gif", bytes: [0x47, 0x49, 0x46, 0x38] },
|
|
5
|
+
{ mime: "image/webp", bytes: [0x52, 0x49, 0x46, 0x46], offset: 0, extra: { bytes: [0x57, 0x45, 0x42, 0x50], offset: 8 } },
|
|
6
|
+
{ mime: "image/avif", bytes: [0x66, 0x74, 0x79, 0x70, 0x61, 0x76, 0x69, 0x66], offset: 4 },
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
function matches(view, pattern, offset = 0) {
|
|
10
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
11
|
+
if (view[offset + i] !== pattern[i]) return false;
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function sniffMimeType(bytes) {
|
|
17
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
18
|
+
for (const entry of MAGIC_BYTES) {
|
|
19
|
+
const offset = entry.offset ?? 0;
|
|
20
|
+
if (matches(view, entry.bytes, offset)) {
|
|
21
|
+
if (entry.extra) {
|
|
22
|
+
if (matches(view, entry.extra.bytes, entry.extra.offset)) return entry.mime;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
return entry.mime;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function toBase64(bytes) {
|
|
32
|
+
let binary = "";
|
|
33
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
34
|
+
for (let i = 0; i < view.length; i++) {
|
|
35
|
+
binary += String.fromCharCode(view[i]);
|
|
36
|
+
}
|
|
37
|
+
return btoa(binary);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function fromBase64(base64) {
|
|
41
|
+
const binary = atob(base64);
|
|
42
|
+
const bytes = new Uint8Array(binary.length);
|
|
43
|
+
for (let i = 0; i < binary.length; i++) {
|
|
44
|
+
bytes[i] = binary.charCodeAt(i);
|
|
45
|
+
}
|
|
46
|
+
return bytes;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function imageUrlToBase64(url, init) {
|
|
50
|
+
const res = await fetch(url, init);
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
throw new Error(`edge64: fetch failed with status ${res.status}`);
|
|
53
|
+
}
|
|
54
|
+
const buffer = await res.arrayBuffer();
|
|
55
|
+
const bytes = new Uint8Array(buffer);
|
|
56
|
+
const contentType = res.headers.get("content-type");
|
|
57
|
+
const mime = contentType && contentType.startsWith("image/") ? contentType.split(";")[0] : sniffMimeType(bytes);
|
|
58
|
+
if (!mime) {
|
|
59
|
+
throw new Error("edge64: could not determine image MIME type");
|
|
60
|
+
}
|
|
61
|
+
const base64 = toBase64(bytes);
|
|
62
|
+
return {
|
|
63
|
+
mime,
|
|
64
|
+
base64,
|
|
65
|
+
dataUri: `data:${mime};base64,${base64}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function base64ToBytes(input) {
|
|
70
|
+
const base64 = input.startsWith("data:") ? input.slice(input.indexOf(",") + 1) : input;
|
|
71
|
+
return fromBase64(base64);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function bytesToDataUri(bytes, mimeHint) {
|
|
75
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
76
|
+
const mime = mimeHint ?? sniffMimeType(view);
|
|
77
|
+
if (!mime) {
|
|
78
|
+
throw new Error("edge64: could not determine image MIME type");
|
|
79
|
+
}
|
|
80
|
+
return `data:${mime};base64,${toBase64(view)}`;
|
|
81
|
+
}
|