@opensea/seadn 1.0.6 → 2.0.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.
- package/README.md +20 -4
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -38
- package/src/index.ts +1 -50
- package/src/sdk.spec.ts +67 -0
- package/src/sdk.ts +77 -0
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.modern.js +0 -2
- package/dist/index.modern.js.map +0 -1
- package/dist/index.module.js +0 -2
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/src/index.test.ts +0 -45
package/README.md
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
# SeaDN Javascript SDK
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Getting Started
|
|
4
|
+
|
|
5
|
+
### Installation
|
|
6
|
+
|
|
7
|
+
**npm**
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @opensea/seadn
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**yarn**
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
yarn install @opensea/seadn
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**pnpm**
|
|
4
20
|
|
|
5
21
|
```sh
|
|
6
|
-
|
|
22
|
+
pnpm add @opensea/seadn
|
|
7
23
|
```
|
|
8
24
|
|
|
9
|
-
|
|
25
|
+
### Usage
|
|
10
26
|
|
|
11
27
|
```js
|
|
12
|
-
import { resizeImage } from
|
|
28
|
+
import { resizeImage } from "@opensea/seadn";
|
|
13
29
|
|
|
14
30
|
const resized = resizeImage(image, { height: 100, width: 100 });
|
|
15
31
|
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type MediaFormat = "webp" | "avif" | "jpeg" | "png";
|
|
2
|
+
type MediaParams = {
|
|
3
|
+
width?: number | `${number}`;
|
|
4
|
+
height?: number | `${number}`;
|
|
5
|
+
format?: MediaFormat;
|
|
6
|
+
};
|
|
7
|
+
declare function optimize(image: string | URL, { width, height, format }: MediaParams): string;
|
|
8
|
+
declare function canBeOptimized(url: URL): boolean;
|
|
9
|
+
|
|
10
|
+
export { type MediaFormat, type MediaParams, canBeOptimized, optimize };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
type MediaFormat = "webp" | "avif" | "jpeg" | "png";
|
|
2
|
+
type MediaParams = {
|
|
3
|
+
width?: number | `${number}`;
|
|
4
|
+
height?: number | `${number}`;
|
|
5
|
+
format?: MediaFormat;
|
|
6
|
+
};
|
|
7
|
+
declare function optimize(image: string | URL, { width, height, format }: MediaParams): string;
|
|
8
|
+
declare function canBeOptimized(url: URL): boolean;
|
|
9
|
+
|
|
10
|
+
export { type MediaFormat, type MediaParams, canBeOptimized, optimize };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var a=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var u=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},p=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of f(t))!d.call(e,n)&&n!==r&&a(e,n,{get:()=>t[n],enumerable:!(i=c(t,n))||i.enumerable});return e};var m=e=>p(a({},"__esModule",{value:!0}),e);var l={};u(l,{canBeOptimized:()=>s,optimize:()=>o});module.exports=m(l);function o(e,{width:t,height:r,format:i}){try{typeof e=="string"&&(e=new URL(e))}catch(b){return e.toString()}if(!(e instanceof URL))return e;if(!s(e))return e.toString();let n=new URLSearchParams;return r!==void 0&&n.set("h",String(r)),t!==void 0&&n.set("w",String(t)),i!==void 0&&n.set("format",i),e.search=n.toString(),e.toString()}function s(e){if(!h(e))return!1;let t=e.pathname.split(".");if(t.length<=1)return!0;switch(t[t.length-1]){case"png":case"jpeg":case"webp":case"avif":case"ico":case void 0:return!0;default:return!1}}function h(e){switch(e.hostname){case"i2.dev.seadn.io":case"i2c.dev.seadn.io":case"i2.seadn.io":case"i2c.seadn.io":return!0;default:return!1}}0&&(module.exports={canBeOptimized,optimize});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/sdk.ts"],"sourcesContent":["export { optimize, MediaParams, MediaFormat, canBeOptimized } from \"./sdk\";\n","export type MediaFormat = \"webp\" | \"avif\" | \"jpeg\" | \"png\";\n\nexport type MediaParams = {\n width?: number | `${number}`;\n height?: number | `${number}`;\n format?: MediaFormat;\n};\n\nexport function optimize(\n image: string | URL,\n { width, height, format }: MediaParams,\n): string {\n try {\n if (typeof image === \"string\") {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n if (!canBeOptimized(image)) {\n return image.toString();\n }\n\n const params = new URLSearchParams();\n if (height !== undefined) {\n params.set(\"h\", String(height));\n }\n if (width !== undefined) {\n params.set(\"w\", String(width));\n }\n if (format !== undefined) {\n params.set(\"format\", format);\n }\n image.search = params.toString();\n return image.toString();\n}\n\nexport function canBeOptimized(url: URL): boolean {\n if (!isSeadnURL(url)) {\n return false;\n }\n\n const split = url.pathname.split(\".\");\n if (split.length <= 1) {\n return true;\n }\n\n const extension = split[split.length - 1];\n switch (extension) {\n case \"png\":\n case \"jpeg\":\n case \"webp\":\n case \"avif\":\n case \"ico\":\n case undefined:\n return true;\n default:\n return false;\n }\n}\n\nexport function isSeadnURL(url: URL): boolean {\n switch (url.hostname) {\n case \"i2.dev.seadn.io\":\n case \"i2c.dev.seadn.io\":\n case \"i2.seadn.io\":\n case \"i2c.seadn.io\":\n return true;\n default:\n return false;\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,aAAAC,IAAA,eAAAC,EAAAJ,GCQO,SAASK,EACdC,EACA,CAAE,MAAAC,EAAO,OAAAC,EAAQ,OAAAC,CAAO,EAChB,CACR,GAAI,CACE,OAAOH,GAAU,WACnBA,EAAQ,IAAI,IAAIA,CAAK,EAEzB,OAAQI,EAAA,CACN,OAAOJ,EAAM,SAAS,CACxB,CAEA,GAAI,EAAEA,aAAiB,KACrB,OAAOA,EAGT,GAAI,CAACK,EAAeL,CAAK,EACvB,OAAOA,EAAM,SAAS,EAGxB,IAAMM,EAAS,IAAI,gBACnB,OAAIJ,IAAW,QACbI,EAAO,IAAI,IAAK,OAAOJ,CAAM,CAAC,EAE5BD,IAAU,QACZK,EAAO,IAAI,IAAK,OAAOL,CAAK,CAAC,EAE3BE,IAAW,QACbG,EAAO,IAAI,SAAUH,CAAM,EAE7BH,EAAM,OAASM,EAAO,SAAS,EACxBN,EAAM,SAAS,CACxB,CAEO,SAASK,EAAeE,EAAmB,CAChD,GAAI,CAACC,EAAWD,CAAG,EACjB,MAAO,GAGT,IAAME,EAAQF,EAAI,SAAS,MAAM,GAAG,EACpC,GAAIE,EAAM,QAAU,EAClB,MAAO,GAIT,OADkBA,EAAMA,EAAM,OAAS,CAAC,EACrB,CACjB,IAAK,MACL,IAAK,OACL,IAAK,OACL,IAAK,OACL,IAAK,MACL,KAAK,OACH,MAAO,GACT,QACE,MAAO,EACX,CACF,CAEO,SAASD,EAAWD,EAAmB,CAC5C,OAAQA,EAAI,SAAU,CACpB,IAAK,kBACL,IAAK,mBACL,IAAK,cACL,IAAK,eACH,MAAO,GACT,QACE,MAAO,EACX,CACF","names":["src_exports","__export","canBeOptimized","optimize","__toCommonJS","optimize","image","width","height","format","e","canBeOptimized","params","url","isSeadnURL","split"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function s(e,{width:t,height:r,format:i}){try{typeof e=="string"&&(e=new URL(e))}catch(c){return e.toString()}if(!(e instanceof URL))return e;if(!a(e))return e.toString();let n=new URLSearchParams;return r!==void 0&&n.set("h",String(r)),t!==void 0&&n.set("w",String(t)),i!==void 0&&n.set("format",i),e.search=n.toString(),e.toString()}function a(e){if(!o(e))return!1;let t=e.pathname.split(".");if(t.length<=1)return!0;switch(t[t.length-1]){case"png":case"jpeg":case"webp":case"avif":case"ico":case void 0:return!0;default:return!1}}function o(e){switch(e.hostname){case"i2.dev.seadn.io":case"i2c.dev.seadn.io":case"i2.seadn.io":case"i2c.seadn.io":return!0;default:return!1}}export{a as canBeOptimized,s as optimize};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/sdk.ts"],"sourcesContent":["export type MediaFormat = \"webp\" | \"avif\" | \"jpeg\" | \"png\";\n\nexport type MediaParams = {\n width?: number | `${number}`;\n height?: number | `${number}`;\n format?: MediaFormat;\n};\n\nexport function optimize(\n image: string | URL,\n { width, height, format }: MediaParams,\n): string {\n try {\n if (typeof image === \"string\") {\n image = new URL(image);\n }\n } catch {\n return image.toString();\n }\n\n if (!(image instanceof URL)) {\n return image;\n }\n\n if (!canBeOptimized(image)) {\n return image.toString();\n }\n\n const params = new URLSearchParams();\n if (height !== undefined) {\n params.set(\"h\", String(height));\n }\n if (width !== undefined) {\n params.set(\"w\", String(width));\n }\n if (format !== undefined) {\n params.set(\"format\", format);\n }\n image.search = params.toString();\n return image.toString();\n}\n\nexport function canBeOptimized(url: URL): boolean {\n if (!isSeadnURL(url)) {\n return false;\n }\n\n const split = url.pathname.split(\".\");\n if (split.length <= 1) {\n return true;\n }\n\n const extension = split[split.length - 1];\n switch (extension) {\n case \"png\":\n case \"jpeg\":\n case \"webp\":\n case \"avif\":\n case \"ico\":\n case undefined:\n return true;\n default:\n return false;\n }\n}\n\nexport function isSeadnURL(url: URL): boolean {\n switch (url.hostname) {\n case \"i2.dev.seadn.io\":\n case \"i2c.dev.seadn.io\":\n case \"i2.seadn.io\":\n case \"i2c.seadn.io\":\n return true;\n default:\n return false;\n }\n}\n"],"mappings":"AAQO,SAASA,EACdC,EACA,CAAE,MAAAC,EAAO,OAAAC,EAAQ,OAAAC,CAAO,EAChB,CACR,GAAI,CACE,OAAOH,GAAU,WACnBA,EAAQ,IAAI,IAAIA,CAAK,EAEzB,OAAQI,EAAA,CACN,OAAOJ,EAAM,SAAS,CACxB,CAEA,GAAI,EAAEA,aAAiB,KACrB,OAAOA,EAGT,GAAI,CAACK,EAAeL,CAAK,EACvB,OAAOA,EAAM,SAAS,EAGxB,IAAMM,EAAS,IAAI,gBACnB,OAAIJ,IAAW,QACbI,EAAO,IAAI,IAAK,OAAOJ,CAAM,CAAC,EAE5BD,IAAU,QACZK,EAAO,IAAI,IAAK,OAAOL,CAAK,CAAC,EAE3BE,IAAW,QACbG,EAAO,IAAI,SAAUH,CAAM,EAE7BH,EAAM,OAASM,EAAO,SAAS,EACxBN,EAAM,SAAS,CACxB,CAEO,SAASK,EAAeE,EAAmB,CAChD,GAAI,CAACC,EAAWD,CAAG,EACjB,MAAO,GAGT,IAAME,EAAQF,EAAI,SAAS,MAAM,GAAG,EACpC,GAAIE,EAAM,QAAU,EAClB,MAAO,GAIT,OADkBA,EAAMA,EAAM,OAAS,CAAC,EACrB,CACjB,IAAK,MACL,IAAK,OACL,IAAK,OACL,IAAK,OACL,IAAK,MACL,KAAK,OACH,MAAO,GACT,QACE,MAAO,EACX,CACF,CAEO,SAASD,EAAWD,EAAmB,CAC5C,OAAQA,EAAI,SAAU,CACpB,IAAK,kBACL,IAAK,mBACL,IAAK,cACL,IAAK,eACH,MAAO,GACT,QACE,MAAO,EACX,CACF","names":["optimize","image","width","height","format","e","canBeOptimized","params","url","isSeadnURL","split"]}
|
package/package.json
CHANGED
|
@@ -1,51 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opensea/seadn",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Javascript SDK to work with SeaDN",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "OpenSea Developers",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
},
|
|
12
|
-
"main": "./dist/index.cjs",
|
|
13
|
-
"module": "./dist/index.module.js",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./src/index.ts",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
14
11
|
"files": [
|
|
15
12
|
"./dist",
|
|
16
13
|
"./src"
|
|
17
14
|
],
|
|
18
15
|
"scripts": {
|
|
19
|
-
"build": "
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"prettier:check": "prettier --check .",
|
|
24
|
-
"prettier:fix": "prettier --write .",
|
|
25
|
-
"prettier:package.json:check": "prettier-package-json --list-different",
|
|
26
|
-
"test": "vitest"
|
|
16
|
+
"build": "tsup",
|
|
17
|
+
"build:typecheck": "attw --pack",
|
|
18
|
+
"test": "vitest",
|
|
19
|
+
"typecheck": "tsc"
|
|
27
20
|
},
|
|
28
|
-
"sideEffects": false,
|
|
29
|
-
"types": "./dist/index.d.ts",
|
|
30
21
|
"devDependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"microbundle": "0.15.1",
|
|
37
|
-
"prettier": "2.8.4",
|
|
38
|
-
"prettier-package-json": "2.8.0",
|
|
39
|
-
"vitest": "0.29.1"
|
|
40
|
-
},
|
|
41
|
-
"lint-staged": {
|
|
42
|
-
"package.json": [
|
|
43
|
-
"prettier-package-json --write"
|
|
44
|
-
],
|
|
45
|
-
"**/*.{ts,tsx,js,jsx,html,md,mdx,yml,json}": [
|
|
46
|
-
"prettier --write"
|
|
47
|
-
]
|
|
48
|
-
},
|
|
49
|
-
"source": "src/index.ts",
|
|
50
|
-
"unpkg": "./dist/index.umd.js"
|
|
22
|
+
"@arethetypeswrong/cli": "0.13.4",
|
|
23
|
+
"tsup": "8.0.1",
|
|
24
|
+
"typescript": "5.3.3",
|
|
25
|
+
"vitest": "1.1.0"
|
|
26
|
+
}
|
|
51
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,50 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
freezeAnimation?: boolean;
|
|
3
|
-
dpr?: number;
|
|
4
|
-
width?: number;
|
|
5
|
-
height?: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const resizeImage = (
|
|
9
|
-
image: string | URL,
|
|
10
|
-
{ width, height, dpr = 1, freezeAnimation = false }: ResizeParams
|
|
11
|
-
): string => {
|
|
12
|
-
if (typeof image === 'string') {
|
|
13
|
-
image = new URL(image);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const params = new URLSearchParams({
|
|
17
|
-
auto: 'format',
|
|
18
|
-
dpr: String(dpr)
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
if (width !== undefined) {
|
|
22
|
-
params.set('w', String(width));
|
|
23
|
-
}
|
|
24
|
-
if (height !== undefined) {
|
|
25
|
-
params.set('h', String(height));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (freezeAnimation) {
|
|
29
|
-
params.set('fr', '1');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
image.search = params.toString();
|
|
33
|
-
return image.toString();
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export const isSupportedFormat = (image: string | URL) => {
|
|
37
|
-
if (typeof image === 'string') {
|
|
38
|
-
image = new URL(image);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const { pathname } = image;
|
|
42
|
-
return (
|
|
43
|
-
pathname.endsWith('.jpg') ||
|
|
44
|
-
pathname.endsWith('.jpeg') ||
|
|
45
|
-
pathname.endsWith('.png') ||
|
|
46
|
-
pathname.endsWith('.webp') ||
|
|
47
|
-
pathname.endsWith('.gif') ||
|
|
48
|
-
pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions
|
|
49
|
-
);
|
|
50
|
-
};
|
|
1
|
+
export { optimize, MediaParams, MediaFormat, canBeOptimized } from "./sdk";
|
package/src/sdk.spec.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { test, expect } from "vitest";
|
|
2
|
+
import { MediaParams, optimize } from "./sdk";
|
|
3
|
+
|
|
4
|
+
test.each([
|
|
5
|
+
[undefined, {} satisfies MediaParams, undefined],
|
|
6
|
+
["", {} satisfies MediaParams, ""],
|
|
7
|
+
["random", {} satisfies MediaParams, "random"],
|
|
8
|
+
[[], {} satisfies MediaParams, []],
|
|
9
|
+
[{}, {} satisfies MediaParams, {}],
|
|
10
|
+
[
|
|
11
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
12
|
+
{} satisfies MediaParams,
|
|
13
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
14
|
+
],
|
|
15
|
+
[
|
|
16
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
17
|
+
{ width: 100 } satisfies MediaParams,
|
|
18
|
+
"https://i2.seadn.io/bored-ape.png?w=100",
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
22
|
+
{ height: 100 } satisfies MediaParams,
|
|
23
|
+
"https://i2.seadn.io/bored-ape.png?h=100",
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
27
|
+
{ width: 100, height: 100 } satisfies MediaParams,
|
|
28
|
+
"https://i2.seadn.io/bored-ape.png?h=100&w=100",
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
"https://i2.seadn.io/bored-ape.png",
|
|
32
|
+
{ height: 100, width: 100 } satisfies MediaParams,
|
|
33
|
+
"https://i2.seadn.io/bored-ape.png?h=100&w=100",
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
"https://i2.seadn.io/bored-ape.svg",
|
|
37
|
+
{ height: 100, width: 100 } satisfies MediaParams,
|
|
38
|
+
"https://i2.seadn.io/bored-ape.svg",
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
"https://i2.seadn.io/bored-ape.png.x",
|
|
42
|
+
{ height: 100, width: 100 } satisfies MediaParams,
|
|
43
|
+
"https://i2.seadn.io/bored-ape.png.x",
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
"https://i2.seadn.io/bored-ape.svg",
|
|
47
|
+
{ height: 100, width: 100 } satisfies MediaParams,
|
|
48
|
+
"https://i2.seadn.io/bored-ape.svg",
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
"https://i2.seadn.io/bored-ape",
|
|
52
|
+
{ height: 100, width: 100 } satisfies MediaParams,
|
|
53
|
+
"https://i2.seadn.io/bored-ape?h=100&w=100",
|
|
54
|
+
],
|
|
55
|
+
[
|
|
56
|
+
"https://i2.seadn.io/bored-ape",
|
|
57
|
+
{ height: 100, width: 100, format: "avif" } satisfies MediaParams,
|
|
58
|
+
"https://i2.seadn.io/bored-ape?h=100&w=100&format=avif",
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
"https://xyz.com/bored-ape.png",
|
|
62
|
+
{ height: 100, width: 100, format: "avif" } satisfies MediaParams,
|
|
63
|
+
"https://xyz.com/bored-ape.png",
|
|
64
|
+
],
|
|
65
|
+
])("resizeImage(%o, %o) -> %o", (url, resizeParams, expected) => {
|
|
66
|
+
expect(optimize(url as string, resizeParams)).toEqual(expected);
|
|
67
|
+
});
|
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export type MediaFormat = "webp" | "avif" | "jpeg" | "png";
|
|
2
|
+
|
|
3
|
+
export type MediaParams = {
|
|
4
|
+
width?: number | `${number}`;
|
|
5
|
+
height?: number | `${number}`;
|
|
6
|
+
format?: MediaFormat;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function optimize(
|
|
10
|
+
image: string | URL,
|
|
11
|
+
{ width, height, format }: MediaParams,
|
|
12
|
+
): string {
|
|
13
|
+
try {
|
|
14
|
+
if (typeof image === "string") {
|
|
15
|
+
image = new URL(image);
|
|
16
|
+
}
|
|
17
|
+
} catch {
|
|
18
|
+
return image.toString();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!(image instanceof URL)) {
|
|
22
|
+
return image;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!canBeOptimized(image)) {
|
|
26
|
+
return image.toString();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const params = new URLSearchParams();
|
|
30
|
+
if (height !== undefined) {
|
|
31
|
+
params.set("h", String(height));
|
|
32
|
+
}
|
|
33
|
+
if (width !== undefined) {
|
|
34
|
+
params.set("w", String(width));
|
|
35
|
+
}
|
|
36
|
+
if (format !== undefined) {
|
|
37
|
+
params.set("format", format);
|
|
38
|
+
}
|
|
39
|
+
image.search = params.toString();
|
|
40
|
+
return image.toString();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function canBeOptimized(url: URL): boolean {
|
|
44
|
+
if (!isSeadnURL(url)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const split = url.pathname.split(".");
|
|
49
|
+
if (split.length <= 1) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const extension = split[split.length - 1];
|
|
54
|
+
switch (extension) {
|
|
55
|
+
case "png":
|
|
56
|
+
case "jpeg":
|
|
57
|
+
case "webp":
|
|
58
|
+
case "avif":
|
|
59
|
+
case "ico":
|
|
60
|
+
case undefined:
|
|
61
|
+
return true;
|
|
62
|
+
default:
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function isSeadnURL(url: URL): boolean {
|
|
68
|
+
switch (url.hostname) {
|
|
69
|
+
case "i2.dev.seadn.io":
|
|
70
|
+
case "i2c.dev.seadn.io":
|
|
71
|
+
case "i2.seadn.io":
|
|
72
|
+
case "i2c.seadn.io":
|
|
73
|
+
return true;
|
|
74
|
+
default:
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
exports.isSupportedFormat=function(t){"string"==typeof t&&(t=new URL(t));var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")},exports.resizeImage=function(t,e){var r=e.width,i=e.height,n=e.dpr,s=void 0===n?1:n,o=e.freezeAnimation,a=void 0!==o&&o;"string"==typeof t&&(t=new URL(t));var g=new URLSearchParams({auto:"format",dpr:String(s)});return void 0!==r&&g.set("w",String(r)),void 0!==i&&g.set("h",String(i)),a&&g.set("fr","1"),t.search=g.toString(),t.toString()};
|
|
2
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["image","URL","pathname","endsWith","startsWith","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","params","URLSearchParams","auto","String","undefined","set","search","toString"],"mappings":"0BAmCiC,SAACA,GACX,iBAAVA,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAQE,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,sBA1C2B,SACzBJ,EAAmBK,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVV,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAMY,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,SAEVU,IAAXT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,IAGrBI,GACFC,EAAOK,IAAI,KAAM,KAGnBjB,EAAMkB,OAASN,EAAOO,WACfnB,EAAMmB,UACf"}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,WAAW,UACf,MAAM,GAAG,GAAG,2CACkC,YAAY,KAChE,MAuBF,CAAC;AAEF,eAAO,MAAM,iBAAiB,UAAW,MAAM,GAAG,GAAG,YAcpD,CAAC"}
|
package/dist/index.modern.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
const t=(t,{width:e,height:n,dpr:r=1,freezeAnimation:i=!1})=>{"string"==typeof t&&(t=new URL(t));const s=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==e&&s.set("w",String(e)),void 0!==n&&s.set("h",String(n)),i&&s.set("fr","1"),t.search=s.toString(),t.toString()},e=t=>{"string"==typeof t&&(t=new URL(t));const{pathname:e}=t;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};export{e as isSupportedFormat,t as resizeImage};
|
|
2
|
-
//# sourceMappingURL=index.modern.js.map
|
package/dist/index.modern.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["resizeImage","image","width","height","dpr","freezeAnimation","URL","params","URLSearchParams","auto","String","undefined","set","search","toString","isSupportedFormat","pathname","endsWith","startsWith"],"mappings":"AAOa,MAAAA,EAAcA,CACzBC,GACEC,QAAOC,SAAQC,IAAAA,EAAM,EAAGC,gBAAAA,GAAkB,MAEvB,iBAAVJ,IACTA,EAAQ,IAAIK,IAAIL,IAGlB,MAAMM,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,SAEVS,IAAXR,GACFI,EAAOK,IAAI,IAAKF,OAAOP,IAGrBE,GACFE,EAAOK,IAAI,KAAM,KAGnBX,EAAMY,OAASN,EAAOO,WACfb,EAAMa,UACf,EAEaC,EAAqBd,IACX,iBAAVA,IACTA,EAAQ,IAAIK,IAAIL,IAGlB,MAAMe,SAAEA,GAAaf,EACrB,OACEe,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAAO"}
|
package/dist/index.module.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
var t=function(t,e){var n=e.width,r=e.height,i=e.dpr,a=void 0===i?1:i,o=e.freezeAnimation,s=void 0!==o&&o;"string"==typeof t&&(t=new URL(t));var g=new URLSearchParams({auto:"format",dpr:String(a)});return void 0!==n&&g.set("w",String(n)),void 0!==r&&g.set("h",String(r)),s&&g.set("fr","1"),t.search=g.toString(),t.toString()},e=function(t){"string"==typeof t&&(t=new URL(t));var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};export{e as isSupportedFormat,t as resizeImage};
|
|
2
|
-
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.module.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["resizeImage","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","URL","params","URLSearchParams","auto","String","undefined","set","search","toString","isSupportedFormat","pathname","endsWith","startsWith"],"mappings":"AAOa,IAAAA,EAAc,SACzBC,EAAmBC,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVN,IACTA,EAAQ,IAAIQ,IAAIR,IAGlB,IAAMS,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNN,IAAKO,OAAOP,KAed,YAZcQ,IAAVX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,SAEVW,IAAXV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,IAGrBI,GACFE,EAAOK,IAAI,KAAM,KAGnBd,EAAMe,OAASN,EAAOO,WACfhB,EAAMgB,UACf,EAEaC,EAAoB,SAACjB,GACX,iBAAVA,IACTA,EAAQ,IAAIQ,IAAIR,IAGlB,IAAQkB,EAAalB,EAAbkB,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB"}
|
package/dist/index.umd.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).seadn={})}(this,function(e){e.isSupportedFormat=function(e){"string"==typeof e&&(e=new URL(e));var t=e.pathname;return t.endsWith(".jpg")||t.endsWith(".jpeg")||t.endsWith(".png")||t.endsWith(".webp")||t.endsWith(".gif")||t.startsWith("/gae/")},e.resizeImage=function(e,t){var n=t.width,i=t.height,o=t.dpr,r=void 0===o?1:o,s=t.freezeAnimation,d=void 0!==s&&s;"string"==typeof e&&(e=new URL(e));var f=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==n&&f.set("w",String(n)),void 0!==i&&f.set("h",String(i)),d&&f.set("fr","1"),e.search=f.toString(),e.toString()}});
|
|
2
|
-
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number;\n height?: number;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\n): string => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const params = new URLSearchParams({\n auto: 'format',\n dpr: String(dpr)\n });\n\n if (width !== undefined) {\n params.set('w', String(width));\n }\n if (height !== undefined) {\n params.set('h', String(height));\n }\n\n if (freezeAnimation) {\n params.set('fr', '1');\n }\n\n image.search = params.toString();\n return image.toString();\n};\n\nexport const isSupportedFormat = (image: string | URL) => {\n if (typeof image === 'string') {\n image = new URL(image);\n }\n\n const { pathname } = image;\n return (\n pathname.endsWith('.jpg') ||\n pathname.endsWith('.jpeg') ||\n pathname.endsWith('.png') ||\n pathname.endsWith('.webp') ||\n pathname.endsWith('.gif') ||\n pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions\n );\n};\n"],"names":["image","URL","pathname","endsWith","startsWith","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","params","URLSearchParams","auto","String","undefined","set","search","toString"],"mappings":"mPAmCiC,SAACA,GACX,iBAAVA,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAQE,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,gBA1C2B,SACzBJ,EAAmBK,GAET,IADRC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAE5B,iBAAVV,IACTA,EAAQ,IAAIC,IAAID,IAGlB,IAAMY,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNL,IAAKM,OAAON,KAed,YAZcO,IAAVV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,SAEVU,IAAXT,GACFK,EAAOK,IAAI,IAAKF,OAAOR,IAGrBI,GACFC,EAAOK,IAAI,KAAM,KAGnBjB,EAAMkB,OAASN,EAAOO,WACfnB,EAAMmB,UACf"}
|
package/src/index.test.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { expect, test } from 'vitest';
|
|
2
|
-
import { resizeImage, isSupportedFormat } from '.';
|
|
3
|
-
|
|
4
|
-
test.each([
|
|
5
|
-
[
|
|
6
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
|
|
7
|
-
{ height: 100 },
|
|
8
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&h=100'
|
|
9
|
-
],
|
|
10
|
-
[
|
|
11
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
|
|
12
|
-
{ width: 100 },
|
|
13
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100'
|
|
14
|
-
],
|
|
15
|
-
[
|
|
16
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
|
|
17
|
-
{ height: 100, width: 100 },
|
|
18
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100&h=100'
|
|
19
|
-
],
|
|
20
|
-
[
|
|
21
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100&h=100',
|
|
22
|
-
{ height: 200, width: 200 },
|
|
23
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=200&h=200'
|
|
24
|
-
],
|
|
25
|
-
[
|
|
26
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png',
|
|
27
|
-
{ dpr: 5, freezeAnimation: true },
|
|
28
|
-
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=5&fr=1'
|
|
29
|
-
]
|
|
30
|
-
])('resizeSeadnImage(%i, %i) -> %i', (url, resizeParams, expected) => {
|
|
31
|
-
expect(resizeImage(url, resizeParams)).toBe(expected);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test.each([
|
|
35
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png', true],
|
|
36
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpg', true],
|
|
37
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpeg', true],
|
|
38
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
|
|
39
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
|
|
40
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
|
|
41
|
-
['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
|
|
42
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false]
|
|
43
|
-
])('isSupportedFormat(%i) -> %i', (url, expected) => {
|
|
44
|
-
expect(isSupportedFormat(url)).toBe(expected);
|
|
45
|
-
});
|