@sanity/diff-patch 5.0.0 → 6.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/src/diffPatch.ts CHANGED
@@ -1,34 +1,44 @@
1
- import {cleanupEfficiency, makeDiff, makePatches, stringifyPatches} from '@sanity/diff-match-patch'
1
+ import {makePatches, stringifyPatches} from '@sanity/diff-match-patch'
2
2
  import {DiffError} from './diffError.js'
3
- import {type Path, pathToString} from './paths.js'
3
+ import {isKeyedObject, type KeyedSanityObject, type Path, pathToString} from './paths.js'
4
4
  import {validateProperty} from './validate.js'
5
5
  import {
6
6
  type Patch,
7
- type SetPatch,
8
7
  type UnsetPatch,
9
- type InsertAfterPatch,
10
8
  type DiffMatchPatch,
11
- type SanityInsertPatch,
12
- type SanityPatch,
13
- type SanitySetPatch,
14
- type SanityUnsetPatch,
15
- type SanityDiffMatchPatch,
16
9
  type SanityPatchMutation,
10
+ type SanityPatchOperations,
11
+ type SanitySetPatchOperation,
12
+ type SanityUnsetPatchOperation,
13
+ type SanityInsertPatchOperation,
14
+ type SanityDiffMatchPatchOperation,
17
15
  } from './patches.js'
16
+ import {difference, intersection} from './setOperations.js'
18
17
 
19
- const ignoredKeys = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
18
+ /**
19
+ * Document keys that are ignored during diff operations.
20
+ * These are system-managed fields that should not be included in patches on
21
+ * top-level documents and should not be diffed with diff-match-patch.
22
+ */
23
+ const SYSTEM_KEYS = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
20
24
 
21
- type PrimitiveValue = string | number | boolean | null | undefined
25
+ /**
26
+ * Maximum size of strings to consider for diff-match-patch (1MB)
27
+ * Based on testing showing consistently good performance up to this size
28
+ */
29
+ const DMP_MAX_STRING_SIZE = 1_000_000
22
30
 
23
31
  /**
24
- * An object (record) that has a `_key` property
25
- *
26
- * @internal
32
+ * Maximum difference in string length before falling back to set operations (40%)
33
+ * Above this threshold, likely indicates text replacement which can be slow
27
34
  */
28
- export interface KeyedSanityObject {
29
- [key: string]: unknown
30
- _key: string
31
- }
35
+ const DMP_MAX_STRING_LENGTH_CHANGE_RATIO = 0.4
36
+
37
+ /**
38
+ * Minimum string size to apply change ratio check (10KB)
39
+ * Small strings are always fast regardless of change ratio
40
+ */
41
+ const DMP_MIN_SIZE_FOR_RATIO_CHECK = 10_000
32
42
 
33
43
  /**
34
44
  * An object (record) that _may_ have a `_key` property
@@ -51,37 +61,6 @@ export interface DocumentStub {
51
61
  [key: string]: unknown
52
62
  }
53
63
 
54
- /**
55
- * Options for the diff-match-patch algorithm.
56
- *
57
- * @public
58
- */
59
- export interface DiffMatchPatchOptions {
60
- /**
61
- * Whether or not to use diff-match-patch at all
62
- *
63
- * @defaultValue `true`
64
- */
65
- enabled: boolean
66
-
67
- /**
68
- * Threshold at which to start using diff-match-patch instead of a regular `set` patch.
69
- *
70
- * @defaultValue `30`
71
- */
72
- lengthThresholdAbsolute: number
73
-
74
- /**
75
- * Only use generated diff-match-patch if the patch length is less than or equal to
76
- * (targetString * relative). Example: A 100 character target with a relative factor
77
- * of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
78
- * it will fall back to a regular `set` patch.
79
- *
80
- * @defaultValue `1.2`
81
- */
82
- lengthThresholdRelative: number
83
- }
84
-
85
64
  /**
86
65
  * Options for the patch generator
87
66
  *
@@ -110,80 +89,30 @@ export interface PatchOptions {
110
89
  * @defaultValue `undefined` (do not apply revision check)
111
90
  */
112
91
  ifRevisionID?: string | true
113
-
114
- /**
115
- * Whether or not to hide warnings during the diff process.
116
- *
117
- * @defaultValue `false`
118
- */
119
- hideWarnings?: boolean
120
-
121
- /**
122
- * Options for the diff-match-patch algorithm.
123
- */
124
- diffMatchPatch?: Partial<DiffMatchPatchOptions>
125
- }
126
-
127
- /**
128
- * Options for diff generation, where all DMP properties are required
129
- *
130
- * @public
131
- */
132
- export type DiffOptions = PatchOptions & {diffMatchPatch: Required<DiffMatchPatchOptions>}
133
-
134
- const defaultOptions = {
135
- hideWarnings: false,
136
- diffMatchPatch: {
137
- enabled: true,
138
-
139
- // Only use diff-match-patch if target string is longer than this threshold
140
- lengthThresholdAbsolute: 30,
141
-
142
- // Only use generated diff-match-patch if the patch length is less than or equal to
143
- // (targetString * relative). Example: A 100 character target with a relative factor
144
- // of 1.2 will allow a 120 character diff-match-patch. If larger than this number,
145
- // it will fall back to a regular `set` patch.
146
- lengthThresholdRelative: 1.2,
147
- },
148
- } satisfies DiffOptions
149
-
150
- /**
151
- * Merges the default options with the passed in options.
152
- *
153
- * @param options - Options to merge with the defaults
154
- * @returns Merged options
155
- */
156
- function mergeOptions(options: PatchOptions): DiffOptions {
157
- return {
158
- ...defaultOptions,
159
- ...options,
160
- diffMatchPatch: {...defaultOptions.diffMatchPatch, ...(options.diffMatchPatch || {})},
161
- }
162
92
  }
163
93
 
164
94
  /**
165
95
  * Generates an array of mutations for Sanity, based on the differences between
166
96
  * the two passed documents/trees.
167
97
  *
168
- * @param itemA - The first document/tree to compare
169
- * @param itemB - The second document/tree to compare
98
+ * @param source - The first document/tree to compare
99
+ * @param target - The second document/tree to compare
170
100
  * @param opts - Options for the diff generation
171
101
  * @returns Array of mutations
172
102
  * @public
173
103
  */
174
104
  export function diffPatch(
175
- itemA: DocumentStub,
176
- itemB: DocumentStub,
177
- opts?: PatchOptions,
105
+ source: DocumentStub,
106
+ target: DocumentStub,
107
+ options: PatchOptions = {},
178
108
  ): SanityPatchMutation[] {
179
- const options = mergeOptions(opts || {})
180
- const id = options.id || (itemA._id === itemB._id && itemA._id)
109
+ const id = options.id || (source._id === target._id && source._id)
181
110
  const revisionLocked = options.ifRevisionID
182
- const ifRevisionID = typeof revisionLocked === 'boolean' ? itemA._rev : revisionLocked
111
+ const ifRevisionID = typeof revisionLocked === 'boolean' ? source._rev : revisionLocked
183
112
  const basePath = options.basePath || []
184
113
  if (!id) {
185
114
  throw new Error(
186
- '_id on itemA and itemB not present or differs, specify document id the mutations should be applied to',
115
+ '_id on source and target not present or differs, specify document id the mutations should be applied to',
187
116
  )
188
117
  }
189
118
 
@@ -193,94 +122,95 @@ export function diffPatch(
193
122
  )
194
123
  }
195
124
 
196
- if (basePath.length === 0 && itemA._type !== itemB._type) {
197
- throw new Error(`_type is immutable and cannot be changed (${itemA._type} => ${itemB._type})`)
125
+ if (basePath.length === 0 && source._type !== target._type) {
126
+ throw new Error(`_type is immutable and cannot be changed (${source._type} => ${target._type})`)
198
127
  }
199
128
 
200
- const operations = diffItem(itemA, itemB, options, basePath, [])
201
- return serializePatches(operations, {id, ifRevisionID: revisionLocked ? ifRevisionID : undefined})
129
+ const operations = diffItem(source, target, basePath, [])
130
+ return serializePatches(operations).map((patchOperations, i) => ({
131
+ patch: {
132
+ id,
133
+ // only add `ifRevisionID` to the first patch
134
+ ...(i === 0 && ifRevisionID && {ifRevisionID}),
135
+ ...patchOperations,
136
+ },
137
+ }))
202
138
  }
203
139
 
204
140
  /**
205
- * Diffs two items and returns an array of patches.
206
- * Note that this is different from `diffPatch`, which generates _mutations_.
141
+ * Generates an array of patch operation objects for Sanity, based on the
142
+ * differences between the two passed values
207
143
  *
208
- * @param itemA - The first item to compare
209
- * @param itemB - The second item to compare
210
- * @param opts - Options for the diff generation
211
- * @param path - Path to the current item
212
- * @param patches - Array of patches to append the results to. Note that this is MUTATED.
213
- * @returns Array of patches
144
+ * @param source - The source value to start off with
145
+ * @param target - The target value that the patch operations will aim to create
146
+ * @param basePath - An optional path that will be prefixed to all subsequent patch operations
147
+ * @returns Array of mutations
214
148
  * @public
215
149
  */
216
- export function diffItem(
217
- itemA: unknown,
218
- itemB: unknown,
219
- opts: DiffOptions = defaultOptions,
150
+ export function diffValue(
151
+ source: unknown,
152
+ target: unknown,
153
+ basePath: Path = [],
154
+ ): SanityPatchOperations[] {
155
+ return serializePatches(diffItem(source, target, basePath))
156
+ }
157
+
158
+ function diffItem(
159
+ source: unknown,
160
+ target: unknown,
220
161
  path: Path = [],
221
162
  patches: Patch[] = [],
222
163
  ): Patch[] {
223
- if (itemA === itemB) {
164
+ if (source === target) {
224
165
  return patches
225
166
  }
226
167
 
227
- const aType = Array.isArray(itemA) ? 'array' : typeof itemA
228
- const bType = Array.isArray(itemB) ? 'array' : typeof itemB
229
-
230
- const aIsUndefined = aType === 'undefined'
231
- const bIsUndefined = bType === 'undefined'
232
-
233
- if (aIsUndefined && !bIsUndefined) {
234
- patches.push({op: 'set', path, value: itemB})
168
+ if (typeof source === 'string' && typeof target === 'string') {
169
+ diffString(source, target, path, patches)
235
170
  return patches
236
171
  }
237
172
 
238
- if (!aIsUndefined && bIsUndefined) {
239
- patches.push({op: 'unset', path})
173
+ if (Array.isArray(source) && Array.isArray(target)) {
174
+ diffArray(source, target, path, patches)
240
175
  return patches
241
176
  }
242
177
 
243
- const options = mergeOptions(opts)
244
- const dataType = aIsUndefined ? bType : aType
245
- const isContainer = dataType === 'object' || dataType === 'array'
246
- if (!isContainer) {
247
- return diffPrimitive(itemA as PrimitiveValue, itemB as PrimitiveValue, options, path, patches)
178
+ if (isRecord(source) && isRecord(target)) {
179
+ diffObject(source, target, path, patches)
180
+ return patches
248
181
  }
249
182
 
250
- if (aType !== bType) {
251
- // Array => Object / Object => Array
252
- patches.push({op: 'set', path, value: itemB})
183
+ if (target === undefined) {
184
+ patches.push({op: 'unset', path})
253
185
  return patches
254
186
  }
255
187
 
256
- return dataType === 'array'
257
- ? diffArray(itemA as unknown[], itemB as unknown[], options, path, patches)
258
- : diffObject(itemA as object, itemB as object, options, path, patches)
188
+ patches.push({op: 'set', path, value: target})
189
+ return patches
259
190
  }
260
191
 
261
192
  function diffObject(
262
- itemA: SanityObject,
263
- itemB: SanityObject,
264
- options: DiffOptions,
193
+ source: Record<string, unknown>,
194
+ target: Record<string, unknown>,
265
195
  path: Path,
266
196
  patches: Patch[],
267
197
  ) {
268
198
  const atRoot = path.length === 0
269
- const aKeys = Object.keys(itemA)
199
+ const aKeys = Object.keys(source)
270
200
  .filter(atRoot ? isNotIgnoredKey : yes)
271
- .map((key) => validateProperty(key, itemA[key], path))
201
+ .map((key) => validateProperty(key, source[key], path))
272
202
 
273
203
  const aKeysLength = aKeys.length
274
- const bKeys = Object.keys(itemB)
204
+ const bKeys = Object.keys(target)
275
205
  .filter(atRoot ? isNotIgnoredKey : yes)
276
- .map((key) => validateProperty(key, itemB[key], path))
206
+ .map((key) => validateProperty(key, target[key], path))
277
207
 
278
208
  const bKeysLength = bKeys.length
279
209
 
280
210
  // Check for deleted items
281
211
  for (let i = 0; i < aKeysLength; i++) {
282
212
  const key = aKeys[i]
283
- if (!(key in itemB)) {
213
+ if (!(key in target)) {
284
214
  patches.push({op: 'unset', path: path.concat(key)})
285
215
  }
286
216
  }
@@ -288,286 +218,457 @@ function diffObject(
288
218
  // Check for changed items
289
219
  for (let i = 0; i < bKeysLength; i++) {
290
220
  const key = bKeys[i]
291
- diffItem(itemA[key], itemB[key], options, path.concat([key]), patches)
221
+ diffItem(source[key], target[key], path.concat([key]), patches)
292
222
  }
293
223
 
294
224
  return patches
295
225
  }
296
226
 
297
- function diffArray(
298
- itemA: unknown[],
299
- itemB: unknown[],
300
- options: DiffOptions,
301
- path: Path,
302
- patches: Patch[],
303
- ) {
227
+ function diffArray(source: unknown[], target: unknown[], path: Path, patches: Patch[]) {
228
+ if (isUniquelyKeyed(source) && isUniquelyKeyed(target)) {
229
+ return diffArrayByKey(source, target, path, patches)
230
+ }
231
+
232
+ return diffArrayByIndex(source, target, path, patches)
233
+ }
234
+
235
+ function diffArrayByIndex(source: unknown[], target: unknown[], path: Path, patches: Patch[]) {
304
236
  // Check for new items
305
- if (itemB.length > itemA.length) {
237
+ if (target.length > source.length) {
306
238
  patches.push({
307
239
  op: 'insert',
308
- after: path.concat([-1]),
309
- items: itemB.slice(itemA.length).map((item, i) => nullifyUndefined(item, path, i, options)),
240
+ position: 'after',
241
+ path: path.concat([-1]),
242
+ items: target.slice(source.length).map(nullifyUndefined),
310
243
  })
311
244
  }
312
245
 
313
246
  // Check for deleted items
314
- if (itemB.length < itemA.length) {
315
- const isSingle = itemA.length - itemB.length === 1
316
- const unsetItems = itemA.slice(itemB.length)
317
-
318
- // If we're revision locked, we can safely unset ranges (eg 5:<end-of-array>).
319
- // Also, if we don't have unique array keys, we can't use any better approach
320
- // than array indexes. If we _do_ have unique array keys, we'll want to unset
321
- // by key, as this is safer in a realtime, collaborative setting
322
- if (isRevisionLocked(options) || !isUniquelyKeyed(unsetItems)) {
323
- patches.push({
324
- op: 'unset',
325
- path: path.concat([isSingle ? itemB.length : [itemB.length, '']]),
326
- })
327
- } else {
247
+ if (target.length < source.length) {
248
+ const isSingle = source.length - target.length === 1
249
+ const unsetItems = source.slice(target.length)
250
+
251
+ // If we have unique array keys, we'll want to unset by key, as this is
252
+ // safer in a realtime, collaborative setting
253
+ if (isUniquelyKeyed(unsetItems)) {
328
254
  patches.push(
329
255
  ...unsetItems.map(
330
256
  (item): UnsetPatch => ({op: 'unset', path: path.concat({_key: item._key})}),
331
257
  ),
332
258
  )
259
+ } else {
260
+ patches.push({
261
+ op: 'unset',
262
+ path: path.concat([isSingle ? target.length : [target.length, '']]),
263
+ })
333
264
  }
334
265
  }
335
266
 
336
267
  // Check for illegal array contents
337
- for (let i = 0; i < itemB.length; i++) {
338
- if (Array.isArray(itemB[i])) {
339
- throw new DiffError('Multi-dimensional arrays not supported', path.concat(i), itemB[i])
268
+ for (let i = 0; i < target.length; i++) {
269
+ if (Array.isArray(target[i])) {
270
+ throw new DiffError('Multi-dimensional arrays not supported', path.concat(i), target[i])
340
271
  }
341
272
  }
342
273
 
343
- const overlapping = Math.min(itemA.length, itemB.length)
344
- const segmentA = itemA.slice(0, overlapping)
345
- const segmentB = itemB.slice(0, overlapping)
274
+ const overlapping = Math.min(source.length, target.length)
275
+ const segmentA = source.slice(0, overlapping)
276
+ const segmentB = target.slice(0, overlapping)
346
277
 
347
- return isUniquelyKeyed(segmentA) && isUniquelyKeyed(segmentB)
348
- ? diffArrayByKey(segmentA, segmentB, options, path, patches)
349
- : diffArrayByIndex(segmentA, segmentB, options, path, patches)
350
- }
351
-
352
- function diffArrayByIndex(
353
- itemA: unknown[],
354
- itemB: unknown[],
355
- options: DiffOptions,
356
- path: Path,
357
- patches: Patch[],
358
- ) {
359
- for (let i = 0; i < itemA.length; i++) {
360
- diffItem(
361
- itemA[i],
362
- nullifyUndefined(itemB[i], path, i, options),
363
- options,
364
- path.concat(i),
365
- patches,
366
- )
278
+ for (let i = 0; i < segmentA.length; i++) {
279
+ diffItem(segmentA[i], nullifyUndefined(segmentB[i]), path.concat(i), patches)
367
280
  }
368
281
 
369
282
  return patches
370
283
  }
371
284
 
285
+ /**
286
+ * Diffs two arrays of keyed objects by their `_key` properties.
287
+ *
288
+ * This approach is preferred over index-based diffing for collaborative editing scenarios
289
+ * because it generates patches that are more resilient to concurrent modifications.
290
+ * When multiple users edit the same array simultaneously, key-based patches have better
291
+ * conflict resolution characteristics than index-based patches.
292
+ *
293
+ * The function handles three main operations:
294
+ * 1. **Reordering**: When existing items change positions within the array
295
+ * 2. **Content changes**: When the content of existing items is modified
296
+ * 3. **Insertions/Deletions**: When items are added or removed from the array
297
+ *
298
+ * @param source - The original array with keyed objects
299
+ * @param target - The target array with keyed objects
300
+ * @param path - The path to this array within the document
301
+ * @param patches - Array to accumulate generated patches
302
+ * @returns The patches array with new patches appended
303
+ */
372
304
  function diffArrayByKey(
373
- itemA: KeyedSanityObject[],
374
- itemB: KeyedSanityObject[],
375
- options: DiffOptions,
305
+ source: KeyedSanityObject[],
306
+ target: KeyedSanityObject[],
376
307
  path: Path,
377
308
  patches: Patch[],
378
309
  ) {
379
- const keyedA = indexByKey(itemA)
380
- const keyedB = indexByKey(itemB)
310
+ // Create lookup maps for efficient key-based access to array items
311
+ const sourceItemsByKey = new Map(source.map((item) => [item._key, item]))
312
+ const targetItemsByKey = new Map(target.map((item) => [item._key, item]))
313
+
314
+ // Categorize keys by their presence in source vs target arrays
315
+ const sourceKeys = new Set(sourceItemsByKey.keys())
316
+ const targetKeys = new Set(targetItemsByKey.keys())
317
+ const keysRemovedFromSource = difference(sourceKeys, targetKeys)
318
+ const keysAddedToTarget = difference(targetKeys, sourceKeys)
319
+ const keysInBothArrays = intersection(sourceKeys, targetKeys)
320
+
321
+ // Handle reordering of existing items within the array.
322
+ // We detect reordering by comparing the relative positions of keys that exist in both arrays,
323
+ // excluding keys that were added or removed (since they don't participate in reordering).
324
+ const sourceKeysStillPresent = Array.from(difference(sourceKeys, keysRemovedFromSource))
325
+ const targetKeysAlreadyPresent = Array.from(difference(targetKeys, keysAddedToTarget))
326
+
327
+ // Track which keys need to be reordered by comparing their relative positions
328
+ const keyReorderOperations: {sourceKey: string; targetKey: string}[] = []
329
+
330
+ for (let i = 0; i < keysInBothArrays.size; i++) {
331
+ const keyAtPositionInSource = sourceKeysStillPresent[i]
332
+ const keyAtPositionInTarget = targetKeysAlreadyPresent[i]
333
+
334
+ // If different keys occupy the same relative position, a reorder is needed
335
+ if (keyAtPositionInSource !== keyAtPositionInTarget) {
336
+ keyReorderOperations.push({
337
+ sourceKey: keyAtPositionInSource,
338
+ targetKey: keyAtPositionInTarget,
339
+ })
340
+ }
341
+ }
342
+
343
+ // Generate reorder patch if any items changed positions
344
+ if (keyReorderOperations.length) {
345
+ patches.push({
346
+ op: 'reorder',
347
+ path,
348
+ snapshot: source,
349
+ reorders: keyReorderOperations,
350
+ })
351
+ }
381
352
 
382
- // There's a bunch of hard/semi-hard problems related to using keys
383
- // Unless we have the exact same order, just use indexes for now
384
- if (!arrayIsEqual(keyedA.keys, keyedB.keys)) {
385
- return diffArrayByIndex(itemA, itemB, options, path, patches)
353
+ // Process content changes for items that exist in both arrays
354
+ for (const key of keysInBothArrays) {
355
+ diffItem(sourceItemsByKey.get(key), targetItemsByKey.get(key), [...path, {_key: key}], patches)
386
356
  }
387
357
 
388
- for (let i = 0; i < keyedB.keys.length; i++) {
389
- const key = keyedB.keys[i]
390
- const valueA = keyedA.index[key]
391
- const valueB = nullifyUndefined(keyedB.index[key], path, i, options)
392
- diffItem(valueA, valueB, options, path.concat({_key: key}), patches)
358
+ // Remove items that no longer exist in the target array
359
+ for (const keyToRemove of keysRemovedFromSource) {
360
+ patches.push({op: 'unset', path: [...path, {_key: keyToRemove}]})
361
+ }
362
+
363
+ // Insert new items that were added to the target array
364
+ // We batch consecutive insertions for efficiency and insert them at the correct positions
365
+ if (keysAddedToTarget.size) {
366
+ let insertionAnchorKey: string // The key after which we'll insert pending items
367
+ let itemsPendingInsertion: unknown[] = []
368
+
369
+ const flushPendingInsertions = () => {
370
+ if (itemsPendingInsertion.length) {
371
+ patches.push({
372
+ op: 'insert',
373
+ // Insert after the anchor key if we have one, otherwise insert at the beginning
374
+ ...(insertionAnchorKey
375
+ ? {position: 'after', path: [...path, {_key: insertionAnchorKey}]}
376
+ : {position: 'before', path: [...path, 0]}),
377
+ items: itemsPendingInsertion,
378
+ })
379
+ }
380
+ }
381
+
382
+ // Walk through the target array to determine where new items should be inserted
383
+ for (const key of targetKeys) {
384
+ if (keysAddedToTarget.has(key)) {
385
+ // This is a new item - add it to the pending insertion batch
386
+ itemsPendingInsertion.push(targetItemsByKey.get(key)!)
387
+ } else if (keysInBothArrays.has(key)) {
388
+ // This is an existing item - flush any pending insertions before it
389
+ flushPendingInsertions()
390
+ insertionAnchorKey = key
391
+ itemsPendingInsertion = []
392
+ }
393
+ }
394
+
395
+ // Flush any remaining insertions at the end
396
+ flushPendingInsertions()
393
397
  }
394
398
 
395
399
  return patches
396
400
  }
397
401
 
398
- function getDiffMatchPatch(
399
- itemA: PrimitiveValue,
400
- itemB: PrimitiveValue,
401
- options: DiffOptions,
402
- path: Path,
403
- ): DiffMatchPatch | undefined {
404
- const {enabled, lengthThresholdRelative, lengthThresholdAbsolute} = options.diffMatchPatch
405
- const segment = path[path.length - 1]
406
- if (
407
- !enabled ||
408
- // Don't use for anything but strings
409
- typeof itemA !== 'string' ||
410
- typeof itemB !== 'string' ||
411
- // Don't use for `_key`, `_ref` etc
412
- (typeof segment === 'string' && segment[0] === '_') ||
413
- // Don't use on short strings
414
- itemB.length < lengthThresholdAbsolute
415
- ) {
416
- return undefined
402
+ /**
403
+ * Determines whether to use diff-match-patch or fallback to a `set` operation
404
+ * when creating a patch to transform a `source` string to `target` string.
405
+ *
406
+ * `diffMatchPatch` patches are typically preferred to `set` operations because
407
+ * they handle conflicts better (when multiple editors work simultaneously) by
408
+ * preserving the user's intended and allowing for 3-way merges.
409
+ *
410
+ * **Heuristic rationale:**
411
+ *
412
+ * Perf analysis revealed that string length has minimal impact on small,
413
+ * keystroke-level changes, but large text replacements (high change ratio) can
414
+ * trigger worst-case algorithm behavior. The 40% change ratio threshold is a
415
+ * simple heuristic that catches problematic replacement scenarios while
416
+ * allowing the algorithm to excel at insertions and deletions.
417
+ *
418
+ * **Performance characteristics (tested on M2 MacBook Pro):**
419
+ *
420
+ * *Keystroke-level editing (most common use case):*
421
+ * - Small strings (1KB-10KB): 0ms for 1-5 keystrokes, consistently sub-millisecond
422
+ * - Medium strings (50KB-200KB): 0ms for 1-5 keystrokes, consistently sub-millisecond
423
+ * - 10 simultaneous keystrokes: ~12ms on 100KB strings
424
+ *
425
+ * *Copy-paste operations (less frequent):*
426
+ * - Small copy-paste operations (<50KB): 0-10ms regardless of string length
427
+ * - Large insertions/deletions (50KB+): 0-50ms (excellent performance)
428
+ * - Large text replacements (50KB+): 70ms-2s+ (can be slow due to algorithm complexity)
429
+ *
430
+ * **Algorithm details:**
431
+ * Uses Myers' diff algorithm with O(ND) time complexity where N=text length and D=edit distance.
432
+ * Includes optimizations: common prefix/suffix removal, line-mode processing, and timeout protection.
433
+ *
434
+ *
435
+ * **Test methodology:**
436
+ * - Generated realistic word-based text patterns
437
+ * - Simulated actual editing behaviors (keystrokes vs copy-paste)
438
+ * - Measured performance across string sizes from 1KB to 10MB
439
+ * - Validated against edge cases including repetitive text and scattered changes
440
+ *
441
+ * @param source - The previous version of the text
442
+ * @param target - The new version of the text
443
+ * @returns true if diff-match-patch should be used, false if fallback to set operation
444
+ *
445
+ * @example
446
+ * ```typescript
447
+ * // Keystroke editing - always fast
448
+ * shouldUseDiffMatchPatch(largeDoc, largeDocWithTypo) // true, ~0ms
449
+ *
450
+ * // Small paste - always fast
451
+ * shouldUseDiffMatchPatch(doc, docWithSmallInsertion) // true, ~0ms
452
+ *
453
+ * // Large replacement - potentially slow
454
+ * shouldUseDiffMatchPatch(article, completelyDifferentArticle) // false, use set
455
+ * ```
456
+ *
457
+ * Compatible with @sanity/diff-match-patch@3.2.0
458
+ */
459
+ export function shouldUseDiffMatchPatch(source: string, target: string): boolean {
460
+ const maxLength = Math.max(source.length, target.length)
461
+
462
+ // Always reject strings larger than our tested size limit
463
+ if (maxLength > DMP_MAX_STRING_SIZE) {
464
+ return false
465
+ }
466
+
467
+ // For small strings, always use diff-match-patch regardless of change ratio
468
+ // Performance testing showed these are always fast (<10ms)
469
+ if (maxLength < DMP_MIN_SIZE_FOR_RATIO_CHECK) {
470
+ return true
417
471
  }
418
472
 
419
- let strPatch = ''
473
+ // Calculate the change ratio to detect large text replacements
474
+ // High ratios indicate replacement scenarios which can trigger slow algorithm paths
475
+ const lengthDifference = Math.abs(target.length - source.length)
476
+ const changeRatio = lengthDifference / maxLength
477
+
478
+ // If change ratio is high, likely a replacement operation that could be slow
479
+ // Fall back to set operation for better user experience
480
+ if (changeRatio > DMP_MAX_STRING_LENGTH_CHANGE_RATIO) {
481
+ return false
482
+ }
483
+
484
+ // All other cases: use diff-match-patch
485
+ // This covers keystroke editing and insertion/deletion scenarios which perform excellently
486
+ return true
487
+ }
488
+
489
+ function getDiffMatchPatch(source: string, target: string, path: Path): DiffMatchPatch | undefined {
490
+ const last = path.at(-1)
491
+ // don't use diff-match-patch for system keys
492
+ if (typeof last === 'string' && last.startsWith('_')) return undefined
493
+ if (!shouldUseDiffMatchPatch(source, target)) return undefined
494
+
420
495
  try {
421
- const patch = makeDiff(itemA, itemB)
422
- const diff = cleanupEfficiency(patch)
423
- strPatch = stringifyPatches(makePatches(diff))
496
+ // Using `makePatches(string, string)` directly instead of the multi-step approach e.g.
497
+ // `stringifyPatches(makePatches(cleanupEfficiency(makeDiff(source, target))))`.
498
+ // this is because `makePatches` internally handles diff generation and
499
+ // automatically applies both `cleanupSemantic()` and `cleanupEfficiency()`
500
+ // when beneficial, resulting in cleaner code with near identical performance and
501
+ // better error handling.
502
+ // [source](https://github.com/sanity-io/diff-match-patch/blob/v3.2.0/src/patch/make.ts#L67-L76)
503
+ //
504
+ // Performance validation (M2 MacBook Pro):
505
+ // Both approaches measured at identical performance:
506
+ // - 10KB strings: 0-1ms total processing time
507
+ // - 100KB strings: 0-1ms total processing time
508
+ // - Individual step breakdown: makeDiff(0ms) + cleanup(0ms) + makePatches(0ms) + stringify(~1ms)
509
+ const strPatch = stringifyPatches(makePatches(source, target))
510
+ return {op: 'diffMatchPatch', path, value: strPatch}
424
511
  } catch (err) {
425
512
  // Fall back to using regular set patch
426
513
  return undefined
427
514
  }
428
-
429
- // Don't use patch if it's longer than allowed relative threshold.
430
- // Allow a 120 character patch for a 100 character string,
431
- // but don't allow a 800 character patch for a 500 character value.
432
- return strPatch.length > itemB.length * lengthThresholdRelative
433
- ? undefined
434
- : {op: 'diffMatchPatch', path, value: strPatch}
435
515
  }
436
516
 
437
- function diffPrimitive(
438
- itemA: PrimitiveValue,
439
- itemB: PrimitiveValue,
440
- options: DiffOptions,
441
- path: Path,
442
- patches: Patch[],
443
- ): Patch[] {
444
- const dmp = getDiffMatchPatch(itemA, itemB, options, path)
445
-
446
- patches.push(
447
- dmp || {
448
- op: 'set',
449
- path,
450
- value: itemB,
451
- },
452
- )
453
-
517
+ function diffString(source: string, target: string, path: Path, patches: Patch[]) {
518
+ const dmp = getDiffMatchPatch(source, target, path)
519
+ patches.push(dmp ?? {op: 'set', path, value: target})
454
520
  return patches
455
521
  }
456
522
 
457
523
  function isNotIgnoredKey(key: string) {
458
- return ignoredKeys.indexOf(key) === -1
524
+ return SYSTEM_KEYS.indexOf(key) === -1
459
525
  }
460
526
 
461
- function serializePatches(
462
- patches: Patch[],
463
- options: {id: string; ifRevisionID?: string},
464
- ): SanityPatchMutation[] {
465
- if (patches.length === 0) {
466
- return []
467
- }
468
-
469
- const {id, ifRevisionID} = options
470
- const set = patches.filter((patch): patch is SetPatch => patch.op === 'set')
471
- const unset = patches.filter((patch): patch is UnsetPatch => patch.op === 'unset')
472
- const insert = patches.filter((patch): patch is InsertAfterPatch => patch.op === 'insert')
473
- const dmp = patches.filter((patch): patch is DiffMatchPatch => patch.op === 'diffMatchPatch')
474
-
475
- const withSet =
476
- set.length > 0 &&
477
- set.reduce(
478
- (patch: SanitySetPatch, item: SetPatch) => {
479
- const path = pathToString(item.path)
480
- patch.set[path] = item.value
481
- return patch
482
- },
483
- {id, set: {}},
484
- )
485
-
486
- const withUnset =
487
- unset.length > 0 &&
488
- unset.reduce(
489
- (patch: SanityUnsetPatch, item: UnsetPatch) => {
490
- const path = pathToString(item.path)
491
- patch.unset.push(path)
492
- return patch
493
- },
494
- {id, unset: []},
495
- )
496
-
497
- const withInsert = insert.reduce((acc: SanityInsertPatch[], item: InsertAfterPatch) => {
498
- const after = pathToString(item.after)
499
- return acc.concat({id, insert: {after, items: item.items}})
500
- }, [])
501
-
502
- const withDmp =
503
- dmp.length > 0 &&
504
- dmp.reduce(
505
- (patch: SanityDiffMatchPatch, item: DiffMatchPatch) => {
506
- const path = pathToString(item.path)
507
- patch.diffMatchPatch[path] = item.value
508
- return patch
509
- },
510
- {id, diffMatchPatch: {}},
511
- )
512
-
513
- const patchSet: SanityPatch[] = [withUnset, withSet, withDmp, ...withInsert].filter(
514
- (item): item is SanityPatch => item !== false,
515
- )
527
+ // mutually exclusive operations
528
+ type SanityPatchOperation =
529
+ | SanitySetPatchOperation
530
+ | SanityUnsetPatchOperation
531
+ | SanityInsertPatchOperation
532
+ | SanityDiffMatchPatchOperation
533
+
534
+ function serializePatches(patches: Patch[], curr?: SanityPatchOperation): SanityPatchOperations[] {
535
+ const [patch, ...rest] = patches
536
+ if (!patch) return curr ? [curr] : []
537
+
538
+ switch (patch.op) {
539
+ case 'set':
540
+ case 'diffMatchPatch': {
541
+ // TODO: reconfigure eslint to use @typescript-eslint/no-unused-vars
542
+ // eslint-disable-next-line no-unused-vars
543
+ type CurrentOp = Extract<SanityPatchOperation, {[K in typeof patch.op]: {}}>
544
+ const emptyOp = {[patch.op]: {}} as CurrentOp
545
+
546
+ if (!curr) return serializePatches(patches, emptyOp)
547
+ if (!(patch.op in curr)) return [curr, ...serializePatches(patches, emptyOp)]
548
+
549
+ Object.assign((curr as CurrentOp)[patch.op], {[pathToString(patch.path)]: patch.value})
550
+ return serializePatches(rest, curr)
551
+ }
552
+ case 'unset': {
553
+ const emptyOp = {unset: []}
554
+ if (!curr) return serializePatches(patches, emptyOp)
555
+ if (!('unset' in curr)) return [curr, ...serializePatches(patches, emptyOp)]
516
556
 
517
- return patchSet.map((patch, i) => ({
518
- patch: ifRevisionID && i === 0 ? {...patch, ifRevisionID} : patch,
519
- }))
557
+ curr.unset.push(pathToString(patch.path))
558
+ return serializePatches(rest, curr)
559
+ }
560
+ case 'insert': {
561
+ if (curr) return [curr, ...serializePatches(patches)]
562
+
563
+ return [
564
+ {
565
+ insert: {
566
+ [patch.position]: pathToString(patch.path),
567
+ items: patch.items,
568
+ },
569
+ } as SanityInsertPatchOperation,
570
+ ...serializePatches(rest),
571
+ ]
572
+ }
573
+ case 'reorder': {
574
+ if (curr) return [curr, ...serializePatches(patches)]
575
+
576
+ // REORDER STRATEGY: Two-phase approach to avoid key collisions
577
+ //
578
+ // Problem: Direct key swaps can cause collisions. For example, swapping A↔B:
579
+ // - Set A's content to B: ✓
580
+ // - Set B's content to A: ✗ (A's content was already overwritten)
581
+ //
582
+ // Solution: Use temporary keys as an intermediate step
583
+ // Phase 1: Move all items to temporary keys with their final content
584
+ // Phase 2: Update just the _key property to restore the final keys
585
+
586
+ // Phase 1: Move items to collision-safe temporary keys
587
+ const tempKeyOperations: SanityPatchOperations = {}
588
+ tempKeyOperations.set = {}
589
+
590
+ for (const {sourceKey, targetKey} of patch.reorders) {
591
+ const temporaryKey = `__temp_reorder_${sourceKey}__`
592
+ const finalContentForThisPosition =
593
+ patch.snapshot[getIndexForKey(patch.snapshot, targetKey)]
594
+
595
+ Object.assign(tempKeyOperations.set, {
596
+ [pathToString([...patch.path, {_key: sourceKey}])]: {
597
+ ...finalContentForThisPosition,
598
+ _key: temporaryKey,
599
+ },
600
+ })
601
+ }
602
+
603
+ // Phase 2: Update _key properties to restore the intended final keys
604
+ const finalKeyOperations: SanityPatchOperations = {}
605
+ finalKeyOperations.set = {}
606
+
607
+ for (const {sourceKey, targetKey} of patch.reorders) {
608
+ const temporaryKey = `__temp_reorder_${sourceKey}__`
609
+
610
+ Object.assign(finalKeyOperations.set, {
611
+ [pathToString([...patch.path, {_key: temporaryKey}, '_key'])]: targetKey,
612
+ })
613
+ }
614
+
615
+ return [tempKeyOperations, finalKeyOperations, ...serializePatches(rest)]
616
+ }
617
+ default: {
618
+ return []
619
+ }
620
+ }
520
621
  }
521
622
 
522
623
  function isUniquelyKeyed(arr: unknown[]): arr is KeyedSanityObject[] {
523
- const keys = []
624
+ const seenKeys = new Set<string>()
524
625
 
525
- for (let i = 0; i < arr.length; i++) {
526
- const key = getKey(arr[i])
527
- if (!key || keys.indexOf(key) !== -1) {
528
- return false
529
- }
626
+ for (const item of arr) {
627
+ // Each item must be a keyed object with a _key property
628
+ if (!isKeyedObject(item)) return false
629
+
630
+ // Each _key must be unique within the array
631
+ if (seenKeys.has(item._key)) return false
530
632
 
531
- keys.push(key)
633
+ seenKeys.add(item._key)
532
634
  }
533
635
 
534
636
  return true
535
637
  }
536
638
 
537
- function getKey(obj: unknown) {
538
- return typeof obj === 'object' && obj !== null && (obj as KeyedSanityObject)._key
539
- }
639
+ // Cache to avoid recomputing key-to-index mappings for the same array
640
+ const keyToIndexCache = new WeakMap<KeyedSanityObject[], Record<string, number>>()
641
+
642
+ function getIndexForKey(keyedArray: KeyedSanityObject[], targetKey: string) {
643
+ const cachedMapping = keyToIndexCache.get(keyedArray)
644
+ if (cachedMapping) return cachedMapping[targetKey]
540
645
 
541
- function indexByKey(arr: KeyedSanityObject[]) {
542
- return arr.reduce(
543
- (acc, item) => {
544
- acc.keys.push(item._key)
545
- acc.index[item._key] = item
546
- return acc
646
+ // Build a mapping from _key to array index
647
+ const keyToIndexMapping = keyedArray.reduce<Record<string, number>>(
648
+ (mapping, {_key}, arrayIndex) => {
649
+ mapping[_key] = arrayIndex
650
+ return mapping
547
651
  },
548
- {keys: [] as string[], index: {} as {[key: string]: KeyedSanityObject}},
652
+ {},
549
653
  )
550
- }
551
-
552
- function arrayIsEqual(itemA: unknown[], itemB: unknown[]) {
553
- return itemA.length === itemB.length && itemA.every((item, i) => itemB[i] === item)
554
- }
555
654
 
556
- function nullifyUndefined(item: unknown, path: Path, index: number, options: PatchOptions) {
557
- if (typeof item !== 'undefined') {
558
- return item
559
- }
655
+ keyToIndexCache.set(keyedArray, keyToIndexMapping)
560
656
 
561
- if (!options.hideWarnings) {
562
- const serializedPath = pathToString(path.concat(index))
563
- console.warn(`undefined value in array converted to null (at '${serializedPath}')`)
564
- }
657
+ return keyToIndexMapping[targetKey]
658
+ }
565
659
 
566
- return null
660
+ function isRecord(value: unknown): value is Record<string, unknown> {
661
+ return typeof value === 'object' && !!value && !Array.isArray(value)
567
662
  }
568
663
 
569
- function isRevisionLocked(options: PatchOptions): boolean {
570
- return Boolean(options.ifRevisionID)
664
+ /**
665
+ * Simplify returns `null` if the value given was `undefined`. This behavior
666
+ * is the same as how `JSON.stringify` works so this is relatively expected
667
+ * behavior.
668
+ */
669
+ function nullifyUndefined(item: unknown) {
670
+ if (item === undefined) return null
671
+ return item
571
672
  }
572
673
 
573
674
  function yes(_: unknown) {