Skip to content

Commit 3700ba0

Browse files
committed
fix: use klona instead of structuredClone for cloning options
1 parent e7d5b38 commit 3700ba0

File tree

5 files changed

+105
-7
lines changed

5 files changed

+105
-7
lines changed

.changeset/ninety-turkeys-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-cloudinary": patch
3+
---
4+
5+
fix: use `klona` instead of `structuredClone` for cloning options

packages/svelte-cloudinary/src/config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { VERSION as SVELTE_CLOUDINARY_VERSION } from './version';
22
import { VERSION as SVELTE_VERSION } from 'svelte/compiler';
33
import { setContext, getContext } from 'svelte';
4+
import { klona } from './internal/klona';
45
import { defu } from 'defu';
56
import type {
67
AnalyticsOptions,
@@ -58,7 +59,7 @@ export function mergeGlobalConfig(
5859

5960
const config = defu(
6061
// Overrides have the highest priorty
61-
structuredClone(configOverride),
62+
klona(configOverride),
6263

6364
// Merge the global config `cloudName` and `apiKey`
6465
{
@@ -75,7 +76,7 @@ export function mergeGlobalConfig(
7576

7677
const analytics = defu(
7778
// Overrides have the highest priorty
78-
structuredClone(analyticsOverride),
79+
klona(analyticsOverride),
7980
// Merge the global config analytics
8081
globalConfig?.analytics,
8182
// Merge the default analytics
@@ -112,10 +113,7 @@ export function mergeGlobalConfig(
112113
* </script>
113114
*/
114115
export function configureCloudinary(globalConfig: GlobalCloudinaryConfig) {
115-
setContext<GlobalCloudinaryConfig>(
116-
STORE_KEY,
117-
structuredClone(globalConfig),
118-
);
116+
setContext<GlobalCloudinaryConfig>(STORE_KEY, klona(globalConfig));
119117
}
120118

121119
export function getGlobalConfig(): GlobalCloudinaryConfig | null {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This file is MIT Licensed from Luke Edwards
2+
// https://github.com/lukeed/klona/blob/e563341d88f433e74a9b4c3c0372d4ba55d2f79e/license
3+
// https://github.com/lukeed/klona/blob/e563341d88f433e74a9b4c3c0372d4ba55d2f79e/index.d.ts
4+
5+
export function klona<T>(input: T): T;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// This file is MIT Licensed from Luke Edwards
2+
// https://github.com/lukeed/klona/blob/e563341d88f433e74a9b4c3c0372d4ba55d2f79e/license
3+
// https://github.com/lukeed/klona/blob/e563341d88f433e74a9b4c3c0372d4ba55d2f79e/src/index.js
4+
5+
// @ts-nocheck
6+
7+
export function klona(x) {
8+
if (typeof x !== 'object') return x;
9+
10+
var k,
11+
tmp,
12+
str = Object.prototype.toString.call(x);
13+
14+
if (str === '[object Object]') {
15+
if (x.constructor !== Object && typeof x.constructor === 'function') {
16+
tmp = new x.constructor();
17+
for (k in x) {
18+
if (x.hasOwnProperty(k) && tmp[k] !== x[k]) {
19+
tmp[k] = klona(x[k]);
20+
}
21+
}
22+
} else {
23+
tmp = {}; // null
24+
for (k in x) {
25+
if (k === '__proto__') {
26+
Object.defineProperty(tmp, k, {
27+
value: klona(x[k]),
28+
configurable: true,
29+
enumerable: true,
30+
writable: true,
31+
});
32+
} else {
33+
tmp[k] = klona(x[k]);
34+
}
35+
}
36+
}
37+
return tmp;
38+
}
39+
40+
if (str === '[object Array]') {
41+
k = x.length;
42+
for (tmp = Array(k); k--; ) {
43+
tmp[k] = klona(x[k]);
44+
}
45+
return tmp;
46+
}
47+
48+
if (str === '[object Set]') {
49+
tmp = new Set();
50+
x.forEach(function (val) {
51+
tmp.add(klona(val));
52+
});
53+
return tmp;
54+
}
55+
56+
if (str === '[object Map]') {
57+
tmp = new Map();
58+
x.forEach(function (val, key) {
59+
tmp.set(klona(key), klona(val));
60+
});
61+
return tmp;
62+
}
63+
64+
if (str === '[object Date]') {
65+
return new Date(+x);
66+
}
67+
68+
if (str === '[object RegExp]') {
69+
tmp = new RegExp(x.source, x.flags);
70+
tmp.lastIndex = x.lastIndex;
71+
return tmp;
72+
}
73+
74+
if (str === '[object DataView]') {
75+
return new x.constructor(klona(x.buffer));
76+
}
77+
78+
if (str === '[object ArrayBuffer]') {
79+
return x.slice(0);
80+
}
81+
82+
// ArrayBuffer.isView(x)
83+
// ~> `new` bcuz `Buffer.slice` => ref
84+
if (str.slice(-6) === 'Array]') {
85+
return new x.constructor(x);
86+
}
87+
88+
return x;
89+
}

packages/svelte-cloudinary/src/internal/loader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import type { CldImageProps } from '../components/CldImage.svelte';
22
import type { ImageOptions } from '@cloudinary-util/url-loader';
33
import { getCldImageUrl } from '../helpers/getCldImageUrl';
44
import type { ImageProps } from '@unpic/svelte';
5+
import { klona } from './klona';
56

67
type URLTransformer = Exclude<ImageProps['transformer'], undefined>;
78

89
export function createLoader(props: CldImageProps): URLTransformer {
9-
const { config, ...imageProps } = structuredClone(props);
10+
const { config, ...imageProps } = klona(props);
1011

1112
// Normalize width and height to number to allow flexibility in how the values
1213
// are passed through as props

0 commit comments

Comments
 (0)