@mutables.app/image-optimizer 0.0.0-stage → 0.2.0

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.
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2014 Alex Crichton
2
+
3
+ Permission is hereby granted, free of charge, to any
4
+ person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the
6
+ Software without restriction, including without
7
+ limitation the rights to use, copy, modify, merge,
8
+ publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software
10
+ is furnished to do so, subject to the following
11
+ conditions:
12
+
13
+ The above copyright notice and this permission notice
14
+ shall be included in all copies or substantial portions
15
+ of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
+ DEALINGS IN THE SOFTWARE.
package/node.js ADDED
@@ -0,0 +1,3 @@
1
+ import { optimize, optimize_jpeg } from './dist/optimizer.node.js';
2
+ import { createImage } from './pipeline.js';
3
+ export const image = createImage(optimize, optimize_jpeg);
package/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
2
  "name": "@mutables.app/image-optimizer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/mutables-app/image-optimizer.git"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "node": "./node.js",
14
+ "default": "./index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "*.js",
19
+ "*.d.ts",
20
+ "dist",
21
+ "licenses"
22
+ ],
23
+ "sideEffects": [
24
+ "./dist/*"
25
+ ],
26
+ "homepage": "https://github.com/mutables-app/image-optimizer#readme",
27
+ "bugs": {
28
+ "url": "https://github.com/mutables-app/image-optimizer/issues"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }
package/pipeline.js ADDED
@@ -0,0 +1,70 @@
1
+ const MAX_INPUT = 20 * 1024 * 1024;
2
+ const dimension = value => {
3
+ if (value === undefined) return 0;
4
+ if (!Number.isInteger(value) || value < 1 || value > 16383) throw new RangeError('Dimensions must be integers from 1 to 16383');
5
+ return value;
6
+ };
7
+ export function createImage(optimize, optimizeJpeg) {
8
+ return function image(input, { maxPixels = 8_000_000 } = {}) {
9
+ if (!Number.isInteger(maxPixels) || maxPixels < 1 || maxPixels > 16_000_000) throw new RangeError('maxPixels must be 1–16000000');
10
+ // Snapshot byte input: callers can reuse or mutate their input while awaiting.
11
+ let snapshot;
12
+ if (input instanceof Blob) {
13
+ if (input.size > MAX_INPUT) throw new RangeError('Input exceeds 20 MiB');
14
+ snapshot = input;
15
+ } else if (input instanceof ArrayBuffer) {
16
+ if (input.byteLength > MAX_INPUT) throw new RangeError('Input exceeds 20 MiB');
17
+ snapshot = new Uint8Array(input.slice(0));
18
+ }
19
+ else if (ArrayBuffer.isView(input)) {
20
+ if (input.byteLength > MAX_INPUT) throw new RangeError('Input exceeds 20 MiB');
21
+ snapshot = new Uint8Array(input.buffer, input.byteOffset, input.byteLength).slice();
22
+ }
23
+ else throw new TypeError('Expected Blob, ArrayBuffer, or typed array');
24
+ if (snapshot.byteLength > MAX_INPUT) throw new RangeError('Input exceeds 20 MiB');
25
+ let width = 0, height = 0, inside = false, withoutEnlargement = false;
26
+ let format = 'webp';
27
+ let quality = 80, lossless = false, method = -1, scaled = false;
28
+ const pipeline = {
29
+ resize(w, h, options = {}) {
30
+ width = dimension(w); height = dimension(h);
31
+ if (!width && !height) throw new RangeError('Provide a width or height');
32
+ if (options.fit !== undefined && !['fill', 'inside'].includes(options.fit)) throw new TypeError('fit must be fill or inside');
33
+ if (options.withoutEnlargement !== undefined && typeof options.withoutEnlargement !== 'boolean') throw new TypeError('withoutEnlargement must be boolean');
34
+ inside = options.fit === 'inside'; withoutEnlargement = options.withoutEnlargement ?? false;
35
+ return pipeline;
36
+ },
37
+ webp(options = {}) {
38
+ format = 'webp';
39
+ quality = options.quality ?? 80; lossless = options.lossless ?? false;
40
+ if (options.method !== undefined) throw new TypeError('method is internal; use speed: "fast"');
41
+ if (options.speed !== undefined && options.speed !== 'fast') throw new RangeError('speed must be "fast"');
42
+ method = options.speed === 'fast' ? 1 : -1;
43
+ scaled = options.speed === 'fast';
44
+ if (!Number.isFinite(quality) || quality < 0 || quality > 100) throw new RangeError('quality must be 0–100');
45
+ if (typeof lossless !== 'boolean') throw new TypeError('lossless must be boolean');
46
+ return pipeline;
47
+ },
48
+ jpeg(options = {}) {
49
+ quality = options.quality ?? 80;
50
+ if (!Number.isFinite(quality) || quality < 0 || quality > 100) throw new RangeError('quality must be 0–100');
51
+ if (options.speed !== undefined && options.speed !== 'fast') throw new RangeError('speed must be "fast"');
52
+ if (options.lossless !== undefined) throw new TypeError('JPEG output does not support lossless');
53
+ if (options.method !== undefined) throw new TypeError('method is internal; use speed: "fast"');
54
+ format = 'jpeg'; scaled = options.speed === 'fast'; lossless = false;
55
+ return pipeline;
56
+ },
57
+ async bytes() {
58
+ const encode = format === 'jpeg' ? optimizeJpeg : optimize;
59
+ const args = format === 'jpeg'
60
+ ? [width, height, inside, withoutEnlargement, quality, maxPixels, scaled]
61
+ : [width, height, inside, withoutEnlargement, quality, lossless, maxPixels, method, scaled];
62
+ const bytes = snapshot instanceof Blob ? new Uint8Array(await snapshot.arrayBuffer()) : snapshot;
63
+ try { return encode(bytes, ...args); }
64
+ catch (error) { throw error instanceof Error ? error : new Error(String(error)); }
65
+ },
66
+ async blob() { const type = format === 'jpeg' ? 'image/jpeg' : 'image/webp'; return new Blob([await pipeline.bytes()], { type }); },
67
+ };
68
+ return pipeline;
69
+ };
70
+ }