@sanity/client 7.16.0 → 7.18.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 +46 -2
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-cjs/isRecord.cjs.map +1 -1
- package/dist/_chunks-cjs/resolveEditInfo.cjs +1 -1
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaClean.cjs +34 -12
- package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +3 -57
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/_chunks-es/isRecord.js.map +1 -1
- package/dist/_chunks-es/resolveEditInfo.js +1 -1
- package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
- package/dist/_chunks-es/stegaClean.js +35 -13
- package/dist/_chunks-es/stegaClean.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +4 -58
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/csm.cjs.map +1 -1
- package/dist/csm.js.map +1 -1
- package/dist/index.browser.cjs +2 -2
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +37 -2
- package/dist/index.browser.d.ts +37 -2
- package/dist/index.browser.js +3 -3
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -2
- package/dist/index.d.ts +37 -2
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/media-library.cjs.map +1 -1
- package/dist/media-library.d.cts +26 -1
- package/dist/media-library.d.ts +26 -1
- package/dist/media-library.js.map +1 -1
- package/dist/stega.browser.cjs.map +1 -1
- package/dist/stega.browser.d.cts +37 -2
- package/dist/stega.browser.d.ts +37 -2
- package/dist/stega.browser.js +1 -1
- package/dist/stega.browser.js.map +1 -1
- package/dist/stega.cjs.map +1 -1
- package/dist/stega.d.cts +37 -2
- package/dist/stega.d.ts +37 -2
- package/dist/stega.js +1 -1
- package/dist/stega.js.map +1 -1
- package/package.json +2 -2
- package/src/media-library.ts +2 -0
- package/src/projects/ProjectsClient.ts +22 -2
- package/src/stega/stegaEncodeSourceMap.ts +4 -3
- package/src/types.ts +26 -1
- package/umd/sanityClient.js +55 -88
- package/umd/sanityClient.min.js +2 -2
- package/src/stega/stega.ts +0 -163
package/src/stega/stega.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
// ---------- CONSTANTS ----------
|
|
3
|
-
const ZERO_WIDTHS = [
|
|
4
|
-
8203, // U+200B ZERO WIDTH SPACE
|
|
5
|
-
8204, // U+200C ZERO WIDTH NON-JOINER
|
|
6
|
-
8205, // U+200D ZERO WIDTH JOINER
|
|
7
|
-
65279, // U+FEFF ZERO WIDTH NO-BREAK SPACE
|
|
8
|
-
]
|
|
9
|
-
|
|
10
|
-
const ZERO_WIDTHS_CHAR_CODES = ZERO_WIDTHS.map((x) => String.fromCharCode(x))
|
|
11
|
-
|
|
12
|
-
const LEGACY_WIDTHS = [
|
|
13
|
-
8203, 8204, 8205, 8290, 8291, 8288, 65279, 8289, 119155, 119156, 119157, 119158, 119159, 119160,
|
|
14
|
-
119161, 119162,
|
|
15
|
-
]
|
|
16
|
-
|
|
17
|
-
const ZERO_WIDTH_MAP = Object.fromEntries(ZERO_WIDTHS.map((cp, i) => [cp, i]))
|
|
18
|
-
const LEGACY_WIDTH_MAP = Object.fromEntries(LEGACY_WIDTHS.map((cp, i) => [cp, i.toString(16)]))
|
|
19
|
-
|
|
20
|
-
// Base prefix for new encoding — compression flag appended as 5th char
|
|
21
|
-
const PREFIX = String.fromCodePoint(ZERO_WIDTHS[0]).repeat(4)
|
|
22
|
-
|
|
23
|
-
const ALL_WIDTHS = [...ZERO_WIDTHS, ...LEGACY_WIDTHS]
|
|
24
|
-
const WIDTH_HEXES = ALL_WIDTHS.map((cp) => `\\u{${cp.toString(16)}}`).join('')
|
|
25
|
-
|
|
26
|
-
export const STEGA_REGEX = new RegExp(`[${WIDTH_HEXES}]{4,}`, 'gu')
|
|
27
|
-
|
|
28
|
-
// ---------- ENCODE ----------
|
|
29
|
-
export function stegaEncode(data: any) {
|
|
30
|
-
if (data === undefined) return ''
|
|
31
|
-
|
|
32
|
-
const json = typeof data === 'string' ? data : JSON.stringify(data)
|
|
33
|
-
// On nodejs we could use Buffer instead (it is faster) but we need to identify if we are running on node
|
|
34
|
-
const bytes = new TextEncoder().encode(json)
|
|
35
|
-
// Using a string and concatenating the result as we are looping is faster
|
|
36
|
-
// than creating an array and merging at the end
|
|
37
|
-
let out = ''
|
|
38
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
39
|
-
const b = bytes[i]
|
|
40
|
-
out +=
|
|
41
|
-
ZERO_WIDTHS_CHAR_CODES[(b >> 6) & 3] +
|
|
42
|
-
ZERO_WIDTHS_CHAR_CODES[(b >> 4) & 3] +
|
|
43
|
-
ZERO_WIDTHS_CHAR_CODES[(b >> 2) & 3] +
|
|
44
|
-
ZERO_WIDTHS_CHAR_CODES[b & 3]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return PREFIX + out
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ---------- DECODE ----------
|
|
51
|
-
export function stegaDecode(str: string) {
|
|
52
|
-
if (!str) return undefined
|
|
53
|
-
const match = str.match(STEGA_REGEX)
|
|
54
|
-
if (!match) return undefined
|
|
55
|
-
|
|
56
|
-
const encoded = match[0]
|
|
57
|
-
if (encoded.length % 2 === 0) {
|
|
58
|
-
if (encoded.length % 4 || !encoded.startsWith(PREFIX)) {
|
|
59
|
-
// Legacy hex-based encoding
|
|
60
|
-
return decodeLegacy(encoded)
|
|
61
|
-
}
|
|
62
|
-
} else throw new Error('Encoded data has invalid length')
|
|
63
|
-
const payload = encoded.slice(4)
|
|
64
|
-
const chars = Array.from(payload)
|
|
65
|
-
const bytes = new Uint8Array(chars.length / 4)
|
|
66
|
-
|
|
67
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
68
|
-
bytes[i] =
|
|
69
|
-
(ZERO_WIDTH_MAP[chars[i * 4].codePointAt(0) ?? 0] << 6) |
|
|
70
|
-
(ZERO_WIDTH_MAP[chars[i * 4 + 1].codePointAt(0) ?? 0] << 4) |
|
|
71
|
-
(ZERO_WIDTH_MAP[chars[i * 4 + 2].codePointAt(0) ?? 0] << 2) |
|
|
72
|
-
ZERO_WIDTH_MAP[chars[i * 4 + 3].codePointAt(0) ?? 0]
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
const json = new TextDecoder().decode(bytes)
|
|
77
|
-
return JSON.parse(json)
|
|
78
|
-
} catch {
|
|
79
|
-
return undefined
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// ---------- LEGACY DECODER ----------
|
|
84
|
-
function decodeLegacy(chars: string, single = false) {
|
|
85
|
-
const bytes = []
|
|
86
|
-
|
|
87
|
-
for (let i = chars.length / 2; i-- > 0; ) {
|
|
88
|
-
const hexPair = `${LEGACY_WIDTH_MAP[chars[i * 2].codePointAt(0) ?? 0]}${LEGACY_WIDTH_MAP[chars[i * 2 + 1].codePointAt(0) ?? 0]}`
|
|
89
|
-
bytes.unshift(String.fromCharCode(parseInt(hexPair, 16)))
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const decoded = []
|
|
93
|
-
const queue = [bytes.join('')]
|
|
94
|
-
let attempts = 10
|
|
95
|
-
|
|
96
|
-
while (queue.length) {
|
|
97
|
-
const chunk = queue.shift() ?? ''
|
|
98
|
-
try {
|
|
99
|
-
const parsed = JSON.parse(chunk)
|
|
100
|
-
decoded.push(parsed)
|
|
101
|
-
if (single) return decoded
|
|
102
|
-
} catch (err: any) {
|
|
103
|
-
if (!attempts--) throw err
|
|
104
|
-
const pos = err.message.match(/\sposition\s(\d+)$/)?.[1]
|
|
105
|
-
if (!pos) throw err
|
|
106
|
-
queue.unshift(chunk.substring(0, +pos), chunk.substring(+pos))
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return decoded
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// ---------- UTILITIES ----------
|
|
114
|
-
export function stegaCombine(visible: any, metadata: any, skip: 'auto' | boolean = 'auto') {
|
|
115
|
-
if (skip === true || (skip === 'auto' && !isDateLike(visible) && !isUrlLike(visible))) {
|
|
116
|
-
return `${visible}${stegaEncode(metadata)}`
|
|
117
|
-
}
|
|
118
|
-
return visible
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function stegaClean(input: string) {
|
|
122
|
-
if (input == null) return input
|
|
123
|
-
const cleaned = JSON.stringify(input).replace(STEGA_REGEX, '')
|
|
124
|
-
return JSON.parse(cleaned)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export function stegaSplit(str: string) {
|
|
128
|
-
const match = str.match(STEGA_REGEX)
|
|
129
|
-
return {
|
|
130
|
-
cleaned: str.replace(STEGA_REGEX, ''),
|
|
131
|
-
encoded: match ? match[0] : '',
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// ---------- HELPERS ----------
|
|
136
|
-
function isUrlLike(t: any) {
|
|
137
|
-
try {
|
|
138
|
-
new URL(t, t.startsWith('/') ? 'https://example.com' : undefined)
|
|
139
|
-
return true
|
|
140
|
-
} catch {
|
|
141
|
-
return false
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function isDateLike(t: any) {
|
|
146
|
-
if (!t || typeof t !== 'string') return false
|
|
147
|
-
return Boolean(Date.parse(t))
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function stegaDecodeAll(data: string): string[] {
|
|
151
|
-
const e = data.match(STEGA_REGEX)
|
|
152
|
-
if (e) return e.map((r) => stegaDecode(r)).flat()
|
|
153
|
-
return []
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export default {
|
|
157
|
-
stegaEncode,
|
|
158
|
-
stegaDecode,
|
|
159
|
-
stegaCombine,
|
|
160
|
-
stegaClean,
|
|
161
|
-
stegaSplit,
|
|
162
|
-
stegaDecodeAll,
|
|
163
|
-
}
|