@think-grid-labs/snapbolt 0.1.3 → 0.1.4
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/package.json +4 -3
- package/pkg/README.md +36 -0
- package/pkg/package.json +21 -0
- package/pkg/snapbolt.d.ts +38 -0
- package/pkg/snapbolt.js +181 -0
- package/pkg/snapbolt_bg.wasm +0 -0
- package/pkg/snapbolt_bg.wasm.d.ts +9 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@think-grid-labs/snapbolt",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Unified image optimization toolkit for the browser and React",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -20,11 +20,12 @@
|
|
|
20
20
|
"types": "./pkg/snapbolt.d.ts",
|
|
21
21
|
"import": "./pkg/snapbolt.js",
|
|
22
22
|
"default": "./pkg/snapbolt.js"
|
|
23
|
-
}
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
24
25
|
},
|
|
25
26
|
"scripts": {
|
|
26
27
|
"build": "npm run build:wasm && npm run build:ts",
|
|
27
|
-
"build:wasm": "npx wasm-pack build --target web --out-dir pkg --release",
|
|
28
|
+
"build:wasm": "npx wasm-pack build --target web --out-dir pkg --release && rm pkg/.gitignore",
|
|
28
29
|
"build:ts": "tsc",
|
|
29
30
|
"test": "vitest"
|
|
30
31
|
},
|
package/pkg/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @think-grid-labs/snapbolt
|
|
2
|
+
|
|
3
|
+
A high-performance image optimization toolkit powered by Rust and WebAssembly.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
This toolkit provides professional-grade image optimization (resizing and JPEG/WebP encoding) that runs entirely on the client side.
|
|
7
|
+
|
|
8
|
+
## Quick Start
|
|
9
|
+
|
|
10
|
+
### 1. Install
|
|
11
|
+
```bash
|
|
12
|
+
npm install @think-grid-labs/snapbolt
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 2. Sync WASM Binary
|
|
16
|
+
You must ensure the `.wasm` file is available in your project's `public` folder:
|
|
17
|
+
```bash
|
|
18
|
+
npx @think-grid-labs/snapbolt-cli sync ./public
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### 3. Use with React
|
|
22
|
+
```tsx
|
|
23
|
+
import { useImageOptimizer } from '@think-grid-labs/snapbolt';
|
|
24
|
+
|
|
25
|
+
const SmartImage = ({ src }) => {
|
|
26
|
+
const { optimizedUrl, loading } = useImageOptimizer(src, 75, 300);
|
|
27
|
+
return <img src={optimizedUrl || src} alt="Optimized" />;
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
- **Client-Side Optimization**: Zero server cost.
|
|
33
|
+
- **Blazing Fast**: Powered by a Rust core.
|
|
34
|
+
- **React Ready**: Easy-to-use hooks.
|
|
35
|
+
|
|
36
|
+
For full documentation, visit our [GitHub Repository](https://github.com/ThinkGrid-Labs/snapbolt).
|
package/pkg/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "snapbolt",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "High-performance image optimization toolkit powered by Rust and WASM",
|
|
5
|
+
"version": "0.1.2",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ThinkGrid-Labs/snapbolt"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"snapbolt_bg.wasm",
|
|
13
|
+
"snapbolt.js",
|
|
14
|
+
"snapbolt.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"main": "snapbolt.js",
|
|
17
|
+
"types": "snapbolt.d.ts",
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./snippets/*"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export function optimize_image_sync(input: Uint8Array, quality: number): Uint8Array;
|
|
5
|
+
|
|
6
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
7
|
+
|
|
8
|
+
export interface InitOutput {
|
|
9
|
+
readonly memory: WebAssembly.Memory;
|
|
10
|
+
readonly optimize_image_sync: (a: number, b: number, c: number) => [number, number, number, number];
|
|
11
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
12
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
13
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
14
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
15
|
+
readonly __wbindgen_start: () => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
22
|
+
* a precompiled `WebAssembly.Module`.
|
|
23
|
+
*
|
|
24
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
25
|
+
*
|
|
26
|
+
* @returns {InitOutput}
|
|
27
|
+
*/
|
|
28
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
32
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
33
|
+
*
|
|
34
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
35
|
+
*
|
|
36
|
+
* @returns {Promise<InitOutput>}
|
|
37
|
+
*/
|
|
38
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
package/pkg/snapbolt.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/* @ts-self-types="./snapbolt.d.ts" */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {Uint8Array} input
|
|
5
|
+
* @param {number} quality
|
|
6
|
+
* @returns {Uint8Array}
|
|
7
|
+
*/
|
|
8
|
+
export function optimize_image_sync(input, quality) {
|
|
9
|
+
const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
|
|
10
|
+
const len0 = WASM_VECTOR_LEN;
|
|
11
|
+
const ret = wasm.optimize_image_sync(ptr0, len0, quality);
|
|
12
|
+
if (ret[3]) {
|
|
13
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
14
|
+
}
|
|
15
|
+
var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
16
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
17
|
+
return v2;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function __wbg_get_imports() {
|
|
21
|
+
const import0 = {
|
|
22
|
+
__proto__: null,
|
|
23
|
+
__wbindgen_cast_0000000000000001: function(arg0, arg1) {
|
|
24
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
25
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
26
|
+
return ret;
|
|
27
|
+
},
|
|
28
|
+
__wbindgen_init_externref_table: function() {
|
|
29
|
+
const table = wasm.__wbindgen_externrefs;
|
|
30
|
+
const offset = table.grow(4);
|
|
31
|
+
table.set(0, undefined);
|
|
32
|
+
table.set(offset + 0, undefined);
|
|
33
|
+
table.set(offset + 1, null);
|
|
34
|
+
table.set(offset + 2, true);
|
|
35
|
+
table.set(offset + 3, false);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
__proto__: null,
|
|
40
|
+
"./snapbolt_bg.js": import0,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
45
|
+
ptr = ptr >>> 0;
|
|
46
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getStringFromWasm0(ptr, len) {
|
|
50
|
+
ptr = ptr >>> 0;
|
|
51
|
+
return decodeText(ptr, len);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let cachedUint8ArrayMemory0 = null;
|
|
55
|
+
function getUint8ArrayMemory0() {
|
|
56
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
57
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
58
|
+
}
|
|
59
|
+
return cachedUint8ArrayMemory0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
63
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
64
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
65
|
+
WASM_VECTOR_LEN = arg.length;
|
|
66
|
+
return ptr;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function takeFromExternrefTable0(idx) {
|
|
70
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
71
|
+
wasm.__externref_table_dealloc(idx);
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
76
|
+
cachedTextDecoder.decode();
|
|
77
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
78
|
+
let numBytesDecoded = 0;
|
|
79
|
+
function decodeText(ptr, len) {
|
|
80
|
+
numBytesDecoded += len;
|
|
81
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
82
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
83
|
+
cachedTextDecoder.decode();
|
|
84
|
+
numBytesDecoded = len;
|
|
85
|
+
}
|
|
86
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let WASM_VECTOR_LEN = 0;
|
|
90
|
+
|
|
91
|
+
let wasmModule, wasm;
|
|
92
|
+
function __wbg_finalize_init(instance, module) {
|
|
93
|
+
wasm = instance.exports;
|
|
94
|
+
wasmModule = module;
|
|
95
|
+
cachedUint8ArrayMemory0 = null;
|
|
96
|
+
wasm.__wbindgen_start();
|
|
97
|
+
return wasm;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function __wbg_load(module, imports) {
|
|
101
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
102
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
103
|
+
try {
|
|
104
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
105
|
+
} catch (e) {
|
|
106
|
+
const validResponse = module.ok && expectedResponseType(module.type);
|
|
107
|
+
|
|
108
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
109
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
110
|
+
|
|
111
|
+
} else { throw e; }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const bytes = await module.arrayBuffer();
|
|
116
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
117
|
+
} else {
|
|
118
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
119
|
+
|
|
120
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
121
|
+
return { instance, module };
|
|
122
|
+
} else {
|
|
123
|
+
return instance;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function expectedResponseType(type) {
|
|
128
|
+
switch (type) {
|
|
129
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function initSync(module) {
|
|
136
|
+
if (wasm !== undefined) return wasm;
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
if (module !== undefined) {
|
|
140
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
141
|
+
({module} = module)
|
|
142
|
+
} else {
|
|
143
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const imports = __wbg_get_imports();
|
|
148
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
149
|
+
module = new WebAssembly.Module(module);
|
|
150
|
+
}
|
|
151
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
152
|
+
return __wbg_finalize_init(instance, module);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function __wbg_init(module_or_path) {
|
|
156
|
+
if (wasm !== undefined) return wasm;
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
if (module_or_path !== undefined) {
|
|
160
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
161
|
+
({module_or_path} = module_or_path)
|
|
162
|
+
} else {
|
|
163
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (module_or_path === undefined) {
|
|
168
|
+
module_or_path = new URL('snapbolt_bg.wasm', import.meta.url);
|
|
169
|
+
}
|
|
170
|
+
const imports = __wbg_get_imports();
|
|
171
|
+
|
|
172
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
173
|
+
module_or_path = fetch(module_or_path);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
177
|
+
|
|
178
|
+
return __wbg_finalize_init(instance, module);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const optimize_image_sync: (a: number, b: number, c: number) => [number, number, number, number];
|
|
5
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
6
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
7
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
8
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
9
|
+
export const __wbindgen_start: () => void;
|