@opensea/seadn 1.0.4 → 1.0.7
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 +42 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.modern.js +1 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +10 -10
- package/src/index.test.ts +35 -7
- package/src/index.ts +44 -9
package/README.md
CHANGED
|
@@ -8,8 +8,50 @@ npm i @opensea/seadn
|
|
|
8
8
|
|
|
9
9
|
## Usage
|
|
10
10
|
|
|
11
|
+
**Resizing**
|
|
12
|
+
|
|
11
13
|
```js
|
|
12
14
|
import { resizeImage } from '@opensea/seadn';
|
|
13
15
|
|
|
14
16
|
const resized = resizeImage(image, { height: 100, width: 100 });
|
|
15
17
|
```
|
|
18
|
+
|
|
19
|
+
**Usage with next/image**
|
|
20
|
+
|
|
21
|
+
```jsx
|
|
22
|
+
import { isSupportedURL, resizeImage } from '@opensea/seadn';
|
|
23
|
+
import type { ImageLoader, ImageProps } from 'next/image';
|
|
24
|
+
|
|
25
|
+
// custom loader
|
|
26
|
+
export const seadnLoader: ImageLoader = ({ src, width }) => {
|
|
27
|
+
if (!isSupportedURL(src)) {
|
|
28
|
+
return src;
|
|
29
|
+
}
|
|
30
|
+
return resizeImage(src, { width });
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// prop overrides
|
|
34
|
+
export const nextImageProps = <T extends ImageProps>({
|
|
35
|
+
src,
|
|
36
|
+
...rest
|
|
37
|
+
}: T): ImageProps => {
|
|
38
|
+
if (isSupportedURL(src)) {
|
|
39
|
+
return {
|
|
40
|
+
...rest,
|
|
41
|
+
src: resizeImage(src as string, {
|
|
42
|
+
width: rest.width,
|
|
43
|
+
height: rest.height
|
|
44
|
+
}),
|
|
45
|
+
unoptimized: true
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return { src, ...rest };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// MyComponent.tsx
|
|
52
|
+
return (
|
|
53
|
+
<>
|
|
54
|
+
<Image {...nextImageProps({ alt: 'House', height: 200, src: imageUrl, width: 200 })} />
|
|
55
|
+
</>
|
|
56
|
+
)
|
|
57
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
var t=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var r=t.pathname;return r.endsWith(".jpg")||r.endsWith(".jpeg")||r.endsWith(".png")||r.endsWith(".webp")||r.endsWith(".gif")||r.startsWith("/gae/")};exports.isSupportedFormat=t,exports.isSupportedURL=function(r){try{"string"==typeof r&&(r=new URL(r))}catch(t){return!1}return r instanceof URL&&"i.seadn.io"===r.hostname&&t(r)},exports.resizeImage=function(t,r){var e=r.width,n=r.height,i=r.dpr,o=void 0===i?1:i,s=r.freezeAnimation,a=void 0!==s&&s;try{"string"==typeof t&&(t=new URL(t))}catch(r){return t.toString()}if(!(t instanceof URL))return t;var h=new URLSearchParams({auto:"format",dpr:String(o)});return void 0!==e&&h.set("w",String(e)),void 0!==n&&h.set("h",String(n)),a&&h.set("fr","1"),t.search=h.toString(),t.toString()};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n dpr?: number;\n width?: number
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\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 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 = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\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\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["isSupportedFormat","src","URL","_unused2","pathname","endsWith","startsWith","_unused3","hostname","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search"],"mappings":"AAOa,IAoCAA,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAE,GACA,OACD,CAAA,CAED,KAAMF,aAAeC,KACnB,OAAO,EAGT,IAAQE,EAAaH,EAAbG,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,qDAE8B,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeC,KAGG,eAAjBD,EAAIO,UAA6BR,EAAkBC,EAC5D,sBAxE2B,SACzBQ,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIP,IAAIO,GAEnB,CAAC,MAAAQ,GACA,OAAOR,EAAMS,UACd,CAED,KAAMT,aAAiBP,KACrB,OAAOO,EAGT,IAAMU,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,SAEVY,IAAXX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,IAGrBI,GACFG,EAAOK,IAAI,KAAM,KAGnBf,EAAMgB,OAASN,EAAOD,WACfT,EAAMS,UACf"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
export type ResizeParams = {
|
|
3
|
+
freezeAnimation?: boolean;
|
|
3
4
|
dpr?: number;
|
|
4
|
-
width?: number
|
|
5
|
-
height?: number
|
|
5
|
+
width?: number | `${number}`;
|
|
6
|
+
height?: number | `${number}`;
|
|
6
7
|
};
|
|
7
|
-
export declare const resizeImage: (image: string | URL, { width, height, dpr }: ResizeParams) => string;
|
|
8
|
-
export declare const isSupportedFormat: (
|
|
8
|
+
export declare const resizeImage: (image: string | URL, { width, height, dpr, freezeAnimation }: ResizeParams) => string;
|
|
9
|
+
export declare const isSupportedFormat: (src: string | URL) => boolean;
|
|
10
|
+
export declare const isSupportedURL: (src: string | URL) => boolean;
|
|
9
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,MAAM,MAAM,YAAY,GAAG;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;
|
|
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,GAAG,GAAG,MAAM,EAAE,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC;CAC/B,CAAC;AAEF,eAAO,MAAM,WAAW,UACf,MAAM,GAAG,GAAG,2CACkC,YAAY,KAChE,MA+BF,CAAC;AAEF,eAAO,MAAM,iBAAiB,QAAS,MAAM,GAAG,GAAG,KAAG,OAsBrD,CAAC;AAEF,eAAO,MAAM,cAAc,QAAS,MAAM,GAAG,GAAG,KAAG,OAYlD,CAAC"}
|
package/dist/index.modern.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const t=(t,{width:
|
|
1
|
+
const t=(t,{width:n,height:e,dpr:r=1,freezeAnimation:i=!1})=>{try{"string"==typeof t&&(t=new URL(t))}catch(n){return t.toString()}if(!(t instanceof URL))return t;const s=new URLSearchParams({auto:"format",dpr:String(r)});return void 0!==n&&s.set("w",String(n)),void 0!==e&&s.set("h",String(e)),i&&s.set("fr","1"),t.search=s.toString(),t.toString()},n=t=>{try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;const{pathname:n}=t;return n.endsWith(".jpg")||n.endsWith(".jpeg")||n.endsWith(".png")||n.endsWith(".webp")||n.endsWith(".gif")||n.startsWith("/gae/")},e=t=>{try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&n(t)};export{n as isSupportedFormat,e as isSupportedURL,t as resizeImage};
|
|
2
2
|
//# sourceMappingURL=index.modern.js.map
|
package/dist/index.modern.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n dpr?: number;\n width?: number
|
|
1
|
+
{"version":3,"file":"index.modern.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\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 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 = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\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\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["resizeImage","image","width","height","dpr","freezeAnimation","URL","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search","isSupportedFormat","src","_unused2","pathname","endsWith","startsWith","isSupportedURL","_unused3","hostname"],"mappings":"AAOa,MAAAA,EAAcA,CACzBC,GACEC,QAAOC,SAAQC,IAAAA,EAAM,EAAGC,gBAAAA,GAAkB,MAE5C,IACuB,iBAAVJ,IACTA,EAAQ,IAAIK,IAAIL,GAEnB,CAAC,MAAAM,GACA,OAAON,EAAMO,UACd,CAED,KAAMP,aAAiBK,KACrB,OAAOL,EAGT,MAAMQ,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,SAEVW,IAAXV,GACFM,EAAOK,IAAI,IAAKF,OAAOT,IAGrBE,GACFI,EAAOK,IAAI,KAAM,KAGnBb,EAAMc,OAASN,EAAOD,WACfP,EAAMO,UACf,EAEaQ,EAAqBC,IAChC,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAC,GACA,OACD,CAAA,CAED,KAAMD,aAAeX,KACnB,OACD,EAED,MAAMa,SAAEA,GAAaF,EACrB,OACEE,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAAO,EAIlBC,EAAkBL,IAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAM,GACA,QACD,CACD,OAAMN,aAAeX,KAGG,eAAjBW,EAAIO,UAA6BR,EAAkBC,EAAG"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var t=function(t,n){var r=n.width,e=n.height,i=n.dpr,a=void 0===i?1:i;"string"==typeof t&&(t=new URL(t));var
|
|
1
|
+
var t=function(t,n){var r=n.width,e=n.height,i=n.dpr,a=void 0===i?1:i,o=n.freezeAnimation,s=void 0!==o&&o;try{"string"==typeof t&&(t=new URL(t))}catch(n){return t.toString()}if(!(t instanceof URL))return t;var h=new URLSearchParams({auto:"format",dpr:String(a)});return void 0!==r&&h.set("w",String(r)),void 0!==e&&h.set("h",String(e)),s&&h.set("fr","1"),t.search=h.toString(),t.toString()},n=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var n=t.pathname;return n.endsWith(".jpg")||n.endsWith(".jpeg")||n.endsWith(".png")||n.endsWith(".webp")||n.endsWith(".gif")||n.startsWith("/gae/")},r=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&n(t)};export{n as isSupportedFormat,r as isSupportedURL,t as resizeImage};
|
|
2
2
|
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.module.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n dpr?: number;\n width?: number
|
|
1
|
+
{"version":3,"file":"index.module.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\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 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 = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\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\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["resizeImage","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","URL","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search","isSupportedFormat","src","_unused2","pathname","endsWith","startsWith","isSupportedURL","_unused3","hostname"],"mappings":"AAOa,IAAAA,EAAc,SACzBC,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIQ,IAAIR,GAEnB,CAAC,MAAAS,GACA,OAAOT,EAAMU,UACd,CAED,KAAMV,aAAiBQ,KACrB,OAAOR,EAGT,IAAMW,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNR,IAAKS,OAAOT,KAed,YAZcU,IAAVb,GACFS,EAAOK,IAAI,IAAKF,OAAOZ,SAEVa,IAAXZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,IAGrBI,GACFI,EAAOK,IAAI,KAAM,KAGnBhB,EAAMiB,OAASN,EAAOD,WACfV,EAAMU,UACf,EAEaQ,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAC,GACA,OACD,CAAA,CAED,KAAMD,aAAeX,KACnB,OAAO,EAGT,IAAQa,EAAaF,EAAbE,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,EAEaC,EAAiB,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIX,IAAIW,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeX,KAGG,eAAjBW,EAAIO,UAA6BR,EAAkBC,EAC5D"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t||self).seadn={})}(this,function(t){var e=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}if(!(t instanceof URL))return!1;var e=t.pathname;return e.endsWith(".jpg")||e.endsWith(".jpeg")||e.endsWith(".png")||e.endsWith(".webp")||e.endsWith(".gif")||e.startsWith("/gae/")};t.isSupportedFormat=e,t.isSupportedURL=function(t){try{"string"==typeof t&&(t=new URL(t))}catch(t){return!1}return t instanceof URL&&"i.seadn.io"===t.hostname&&e(t)},t.resizeImage=function(t,e){var n=e.width,i=e.height,r=e.dpr,o=void 0===r?1:r,s=e.freezeAnimation,a=void 0!==s&&s;try{"string"==typeof t&&(t=new URL(t))}catch(e){return t.toString()}if(!(t instanceof URL))return t;var f=new URLSearchParams({auto:"format",dpr:String(o)});return void 0!==n&&f.set("w",String(n)),void 0!==i&&f.set("h",String(i)),a&&f.set("fr","1"),t.search=f.toString(),t.toString()}});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n dpr?: number;\n width?: number
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/index.ts"],"sourcesContent":["export type ResizeParams = {\n freezeAnimation?: boolean;\n dpr?: number;\n width?: number | `${number}`;\n height?: number | `${number}`;\n};\n\nexport const resizeImage = (\n image: string | URL,\n { width, height, dpr = 1, freezeAnimation = false }: ResizeParams\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 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 = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n\n if (!(src instanceof URL)) {\n return false;\n }\n\n const { pathname } = src;\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\nexport const isSupportedURL = (src: string | URL): boolean => {\n try {\n if (typeof src === 'string') {\n src = new URL(src);\n }\n } catch {\n return false;\n }\n if (!(src instanceof URL)) {\n return false;\n }\n return src.hostname === 'i.seadn.io' && isSupportedFormat(src);\n};\n"],"names":["isSupportedFormat","src","URL","_unused2","pathname","endsWith","startsWith","_unused3","hostname","image","_ref","width","height","_ref$dpr","dpr","_ref$freezeAnimation","freezeAnimation","_unused","toString","params","URLSearchParams","auto","String","undefined","set","search"],"mappings":"+NAOa,IAoCAA,EAAoB,SAACC,GAChC,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAE,GACA,OACD,CAAA,CAED,KAAMF,aAAeC,KACnB,OAAO,EAGT,IAAQE,EAAaH,EAAbG,SACR,OACEA,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASC,SAAS,UAClBD,EAASC,SAAS,SAClBD,EAASE,WAAW,QAExB,yCAE8B,SAACL,GAC7B,IACqB,iBAARA,IACTA,EAAM,IAAIC,IAAID,GAEjB,CAAC,MAAAM,GACA,OACD,CAAA,CACD,OAAMN,aAAeC,KAGG,eAAjBD,EAAIO,UAA6BR,EAAkBC,EAC5D,gBAxE2B,SACzBQ,EAAmBC,GACjB,IAAAC,EAAKD,EAALC,MAAOC,EAAMF,EAANE,OAAMC,EAAAH,EAAEI,IAAAA,OAAM,IAAHD,EAAG,EAACA,EAAAE,EAAAL,EAAEM,gBAAAA,OAAkB,IAAHD,GAAQA,EAEjD,IACuB,iBAAVN,IACTA,EAAQ,IAAIP,IAAIO,GAEnB,CAAC,MAAAQ,GACA,OAAOR,EAAMS,UACd,CAED,KAAMT,aAAiBP,KACrB,OAAOO,EAGT,IAAMU,EAAS,IAAIC,gBAAgB,CACjCC,KAAM,SACNP,IAAKQ,OAAOR,KAed,YAZcS,IAAVZ,GACFQ,EAAOK,IAAI,IAAKF,OAAOX,SAEVY,IAAXX,GACFO,EAAOK,IAAI,IAAKF,OAAOV,IAGrBI,GACFG,EAAOK,IAAI,KAAM,KAGnBf,EAAMgB,OAASN,EAAOD,WACfT,EAAMS,UACf"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opensea/seadn",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Javascript SDK to work with SeaDN",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "OpenSea Developers",
|
|
@@ -28,15 +28,15 @@
|
|
|
28
28
|
"sideEffects": false,
|
|
29
29
|
"types": "./dist/index.d.ts",
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@vitest/coverage-
|
|
32
|
-
"concurrently": "
|
|
33
|
-
"esbuild": "
|
|
34
|
-
"husky": "
|
|
35
|
-
"lint-staged": "
|
|
36
|
-
"microbundle": "
|
|
37
|
-
"prettier": "
|
|
38
|
-
"prettier-package-json": "
|
|
39
|
-
"vitest": "
|
|
31
|
+
"@vitest/coverage-v8": "0.33.0",
|
|
32
|
+
"concurrently": "8.2.0",
|
|
33
|
+
"esbuild": "0.19.0",
|
|
34
|
+
"husky": "8.0.3",
|
|
35
|
+
"lint-staged": "13.2.3",
|
|
36
|
+
"microbundle": "0.15.1",
|
|
37
|
+
"prettier": "3.0.1",
|
|
38
|
+
"prettier-package-json": "2.8.0",
|
|
39
|
+
"vitest": "0.33.0"
|
|
40
40
|
},
|
|
41
41
|
"lint-staged": {
|
|
42
42
|
"package.json": [
|
package/src/index.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { expect, test } from 'vitest';
|
|
2
|
-
import { resizeImage, isSupportedFormat } from '.';
|
|
2
|
+
import { resizeImage, isSupportedFormat, isSupportedURL } from '.';
|
|
3
3
|
|
|
4
4
|
test.each([
|
|
5
5
|
[
|
|
@@ -21,9 +21,34 @@ test.each([
|
|
|
21
21
|
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=100&h=100',
|
|
22
22
|
{ height: 200, width: 200 },
|
|
23
23
|
'https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png?auto=format&dpr=1&w=200&h=200'
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
|
|
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
|
+
['unknown', { dpr: 5, freezeAnimation: true }, 'unknown'],
|
|
31
|
+
[5, { dpr: 5, freezeAnimation: true }, 5]
|
|
32
|
+
])(
|
|
33
|
+
'resizeImage(%url, %resizeParams) -> %expected',
|
|
34
|
+
(url, resizeParams, expected) => {
|
|
35
|
+
expect(resizeImage(url as string, resizeParams)).toBe(expected);
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
test.each([
|
|
40
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.png', true],
|
|
41
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpg', true],
|
|
42
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.jpeg', true],
|
|
43
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
|
|
44
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
|
|
45
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
|
|
46
|
+
['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
|
|
47
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
|
|
48
|
+
['unknown', false],
|
|
49
|
+
[5, false]
|
|
50
|
+
])('isSupportedFormat(%url) -> %expected', (url, expected) => {
|
|
51
|
+
expect(isSupportedFormat(url as string)).toBe(expected);
|
|
27
52
|
});
|
|
28
53
|
|
|
29
54
|
test.each([
|
|
@@ -34,7 +59,10 @@ test.each([
|
|
|
34
59
|
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.webp', true],
|
|
35
60
|
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.gif', true],
|
|
36
61
|
['https://i.seadn.io/gae/files/057bb30c6d80d54b78439927b2f07676', true],
|
|
37
|
-
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false]
|
|
38
|
-
|
|
39
|
-
|
|
62
|
+
['https://i.seadn.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
|
|
63
|
+
['https://i.seadn2.io/gcs/files/057bb30c6d80d54b78439927b2f07676.mp3', false],
|
|
64
|
+
['unknown', false],
|
|
65
|
+
[5, false]
|
|
66
|
+
])('isSeadnUrl(%url) -> %expected', (url, expected) => {
|
|
67
|
+
expect(isSupportedURL(url as string)).toBe(expected);
|
|
40
68
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
export type ResizeParams = {
|
|
2
|
+
freezeAnimation?: boolean;
|
|
2
3
|
dpr?: number;
|
|
3
|
-
width?: number
|
|
4
|
-
height?: number
|
|
4
|
+
width?: number | `${number}`;
|
|
5
|
+
height?: number | `${number}`;
|
|
5
6
|
};
|
|
6
7
|
|
|
7
8
|
export const resizeImage = (
|
|
8
9
|
image: string | URL,
|
|
9
|
-
{ width, height, dpr = 1 }: ResizeParams
|
|
10
|
+
{ width, height, dpr = 1, freezeAnimation = false }: ResizeParams
|
|
10
11
|
): string => {
|
|
11
|
-
|
|
12
|
-
image
|
|
12
|
+
try {
|
|
13
|
+
if (typeof image === 'string') {
|
|
14
|
+
image = new URL(image);
|
|
15
|
+
}
|
|
16
|
+
} catch {
|
|
17
|
+
return image.toString();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (!(image instanceof URL)) {
|
|
21
|
+
return image;
|
|
13
22
|
}
|
|
14
23
|
|
|
15
24
|
const params = new URLSearchParams({
|
|
@@ -24,16 +33,28 @@ export const resizeImage = (
|
|
|
24
33
|
params.set('h', String(height));
|
|
25
34
|
}
|
|
26
35
|
|
|
36
|
+
if (freezeAnimation) {
|
|
37
|
+
params.set('fr', '1');
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
image.search = params.toString();
|
|
28
41
|
return image.toString();
|
|
29
42
|
};
|
|
30
43
|
|
|
31
|
-
export const isSupportedFormat = (
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
export const isSupportedFormat = (src: string | URL): boolean => {
|
|
45
|
+
try {
|
|
46
|
+
if (typeof src === 'string') {
|
|
47
|
+
src = new URL(src);
|
|
48
|
+
}
|
|
49
|
+
} catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!(src instanceof URL)) {
|
|
54
|
+
return false;
|
|
34
55
|
}
|
|
35
56
|
|
|
36
|
-
const { pathname } =
|
|
57
|
+
const { pathname } = src;
|
|
37
58
|
return (
|
|
38
59
|
pathname.endsWith('.jpg') ||
|
|
39
60
|
pathname.endsWith('.jpeg') ||
|
|
@@ -43,3 +64,17 @@ export const isSupportedFormat = (image: string | URL) => {
|
|
|
43
64
|
pathname.startsWith('/gae/') // GoogleAppEngine URLs do not contain extensions
|
|
44
65
|
);
|
|
45
66
|
};
|
|
67
|
+
|
|
68
|
+
export const isSupportedURL = (src: string | URL): boolean => {
|
|
69
|
+
try {
|
|
70
|
+
if (typeof src === 'string') {
|
|
71
|
+
src = new URL(src);
|
|
72
|
+
}
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (!(src instanceof URL)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return src.hostname === 'i.seadn.io' && isSupportedFormat(src);
|
|
80
|
+
};
|