Skip to content
Myr

patches.ts

This file

Path

src/plugins/patches.ts

299 lines · 7.1 KiB · TypeScript

Last changed by

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

History of this file

Take it

fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01

line 1why, one step back
1import {immerable} from "../immer"

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 2–18why, one step back
2import {
3 ImmerState,4 Patch,5 SetState,6 ES5ArrayState,7 ProxyArrayState,8 MapState,9 ES5ObjectState,10 ProxyObjectState,11 PatchPath,12 get,13 each,14 has,15 getArchtype,16 isSet,17 isMap,18 loadPlugin,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

lines 19–20why, one step back
19 ProxyType,
20 Archtype,

Immer 7Michel Weststratebefore adoption467ea5d

line 21why, one step back
21 die,

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

line 22why, one step back
22 isDraft,

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

lines 23–24why, one step back
23 isDraftable,
24 NOTHING

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 25–38why, one step back
25} from "../internal"
2627export function enablePatches() {28 const REPLACE = "replace"29 const ADD = "add"30 const REMOVE = "remove"3132 function generatePatches_(33 state: ImmerState,34 basePath: PatchPath,35 patches: Patch[],36 inversePatches: Patch[]37 ): void {38 switch (state.type_) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

lines 39–41why, one step back
39 case ProxyType.ProxyObject:
40 case ProxyType.ES5Object:41 case ProxyType.Map:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 42–47why, one step back
42 return generatePatchesFromAssigned(
43 state,44 basePath,45 patches,46 inversePatches47 )

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

lines 48–49why, one step back
48 case ProxyType.ES5Array:
49 case ProxyType.ProxyArray:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 50why, one step back
50 return generateArrayPatches(state, basePath, patches, inversePatches)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 51why, one step back
51 case ProxyType.Set:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 52–77why, one step back
52 return generateSetPatches(
53 (state as any) as SetState,54 basePath,55 patches,56 inversePatches57 )58 }59 }6061 function generateArrayPatches(62 state: ES5ArrayState | ProxyArrayState,63 basePath: PatchPath,64 patches: Patch[],65 inversePatches: Patch[]66 ) {67 let {base_, assigned_} = state68 let copy_ = state.copy_!6970 // Reduce complexity by ensuring `base` is never longer.71 if (copy_.length < base_.length) {72 // @ts-ignore73 ;[base_, copy_] = [copy_, base_]74 ;[patches, inversePatches] = [inversePatches, patches]75 }7677 // Process replaced indices.

Immer 7Michel Weststratebefore adoption467ea5d

line 78why, one step back
78 for (let i = 0; i < base_.length; i++) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 79–83why, one step back
79 if (assigned_[i] && copy_[i] !== base_[i]) {
80 const path = basePath.concat([i])81 patches.push({82 op: REPLACE,83 path,

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 84–86why, one step back
84 // Need to maybe clone it, as it can in fact be the original value
85 // due to the base/copy inversion at the start of this function86 value: clonePatchValueIfNeeded(copy_[i])

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 87–90why, one step back
87 })
88 inversePatches.push({89 op: REPLACE,90 path,

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

line 91why, one step back
91 value: clonePatchValueIfNeeded(base_[i])

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 92–96why, one step back
92 })
93 }94 }9596 // Process added indices.

Immer 7Michel Weststratebefore adoption467ea5d

line 97why, one step back
97 for (let i = base_.length; i < copy_.length; i++) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 98why, one step back
98 const path = basePath.concat([i])

Immer 7Michel Weststratebefore adoption467ea5d

line 99why, one step back
99 patches.push({

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 100–101why, one step back
100 op: ADD,
101 path,

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 102–104why, one step back
102 // Need to maybe clone it, as it can in fact be the original value
103 // due to the base/copy inversion at the start of this function104 value: clonePatchValueIfNeeded(copy_[i])

Immer 7Michel Weststratebefore adoption467ea5d

lines 105–107why, one step back
105 })
106 }107 if (base_.length < copy_.length) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 108why, one step back
108 inversePatches.push({

Immer 7Michel Weststratebefore adoption467ea5d

lines 109–111why, one step back
109 op: REPLACE,
110 path: basePath.concat(["length"]),111 value: base_.length

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 112–134why, one step back
112 })
113 }114 }115116 // This is used for both Map objects and normal objects.117 function generatePatchesFromAssigned(118 state: MapState | ES5ObjectState | ProxyObjectState,119 basePath: PatchPath,120 patches: Patch[],121 inversePatches: Patch[]122 ) {123 const {base_, copy_} = state124 each(state.assigned_!, (key, assignedValue) => {125 const origValue = get(base_, key)126 const value = get(copy_!, key)127 const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD128 if (origValue === value && op === REPLACE) return129 const path = basePath.concat(key as any)130 patches.push(op === REMOVE ? {op, path} : {op, path, value})131 inversePatches.push(132 op === ADD133 ? {op: REMOVE, path}134 : op === REMOVE

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 135–136why, one step back
135 ? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}
136 : {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 137–193why, one step back
137 )
138 })139 }140141 function generateSetPatches(142 state: SetState,143 basePath: PatchPath,144 patches: Patch[],145 inversePatches: Patch[]146 ) {147 let {base_, copy_} = state148149 let i = 0150 base_.forEach((value: any) => {151 if (!copy_!.has(value)) {152 const path = basePath.concat([i])153 patches.push({154 op: REMOVE,155 path,156 value157 })158 inversePatches.unshift({159 op: ADD,160 path,161 value162 })163 }164 i++165 })166 i = 0167 copy_!.forEach((value: any) => {168 if (!base_.has(value)) {169 const path = basePath.concat([i])170 patches.push({171 op: ADD,172 path,173 value174 })175 inversePatches.unshift({176 op: REMOVE,177 path,178 value179 })180 }181 i++182 })183 }184185 function generateReplacementPatches_(186 rootState: ImmerState,187 replacement: any,188 patches: Patch[],189 inversePatches: Patch[]190 ): void {191 patches.push({192 op: REPLACE,193 path: [],

fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f

line 194why, one step back
194 value: replacement === NOTHING ? undefined : replacement

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 195–208why, one step back
195 })
196 inversePatches.push({197 op: REPLACE,198 path: [],199 value: rootState.base_200 })201 }202203 function applyPatches_<T>(draft: T, patches: Patch[]): T {204 patches.forEach(patch => {205 const {path, op} = patch206207 let base: any = draft208 for (let i = 0; i < path.length - 1; i++) {

fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f

lines 209–212why, one step back
209 const parentType = getArchtype(base)
210 const p = path[i]211 // See #738, avoid prototype pollution212 if (

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 213why, one step back
213 (parentType === Archtype.Object || parentType === Archtype.Array) &&

fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f

lines 214–218why, one step back
214 (p === "__proto__" || p === "constructor")
215 )216 die(24)217 if (typeof base === "function" && p === "prototype") die(24)218 base = get(base, p)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 219–227why, one step back
219 if (typeof base !== "object") die(15, path.join("/"))
220 }221222 const type = getArchtype(base)223 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411224 const key = path[path.length - 1]225 switch (op) {226 case REPLACE:227 switch (type) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 228why, one step back
228 case Archtype.Map:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 229–230why, one step back
229 return base.set(key, value)
230 /* istanbul ignore next */

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 231why, one step back
231 case Archtype.Set:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 232–241why, one step back
232 die(16)
233 default:234 // if value is an object, then it's assigned by reference235 // in the following add or remove ops, the value field inside the patch will also be modifyed236 // so we use value from the cloned patch237 // @ts-ignore238 return (base[key] = value)239 }240 case ADD:241 switch (type) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 242why, one step back
242 case Archtype.Array:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 243why, one step back
243 return base.splice(key as any, 0, value)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 244why, one step back
244 case Archtype.Map:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 245why, one step back
245 return base.set(key, value)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 246why, one step back
246 case Archtype.Set:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 247–252why, one step back
247 return base.add(value)
248 default:249 return (base[key] = value)250 }251 case REMOVE:252 switch (type) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 253why, one step back
253 case Archtype.Array:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 254why, one step back
254 return base.splice(key as any, 1)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 255why, one step back
255 case Archtype.Map:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 256why, one step back
256 return base.delete(key)

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 257why, one step back
257 case Archtype.Set:

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 258–274why, one step back
258 return base.delete(patch.value)
259 default:260 return delete base[key]261 }262 default:263 die(17, op)264 }265 })266267 return draft268 }269270 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?271 // E.g. auto-draft when new objects from outside are assigned and modified?272 // (See failing test when deepClone just returns obj)273 function deepClonePatchValue<T>(obj: T): T274 function deepClonePatchValue(obj: any) {

fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5

line 275why, one step back
275 if (!isDraftable(obj)) return obj

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 276–283why, one step back
276 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)
277 if (isMap(obj))278 return new Map(279 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])280 )281 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))282 const cloned = Object.create(Object.getPrototypeOf(obj))283 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])

fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01

line 284why, one step back
284 if (has(obj, immerable)) cloned[immerable] = obj[immerable]

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 285why, one step back
285 return cloned

fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6

lines 286–291why, one step back
286 }
287288 function clonePatchValueIfNeeded<T>(obj: T): T {289 if (isDraft(obj)) {290 return deepClonePatchValue(obj)291 } else return obj

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 292–299why, one step back
292 }
293294 loadPlugin("Patches", {295 applyPatches_,296 generatePatches_,297 generateReplacementPatches_298 })299}