patches.ts
This file
Path
src/plugins/patches.ts
301 lines · 7.2 KiB · TypeScript
Last changed by
fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
History of this fileTake it
fix: #768 `immerable` field being lost during patch value cloning (#771)Michel Weststratebefore adoptione0b7c01
line 1why, one step back1import {immerable} from "../immer"Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 2–18why, one step back2import {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 back19 ProxyType,20 Archtype,Immer 7Michel Weststratebefore adoption467ea5d
line 21why, one step back21 die,fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 22why, one step back22 isDraft,fix: #791 return 'nothing' should produce undefined patchMichel Weststratebefore adoption5412c9f
lines 23–24why, one step back23 isDraftable,24 NOTHINGMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 25–38why, one step back25} 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 back39 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 back42 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 back48 case ProxyType.ES5Array:49 case ProxyType.ProxyArray:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 50why, one step back50 return generateArrayPatches(state, basePath, patches, inversePatches)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 51why, one step back51 case ProxyType.Set:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 52–77why, one step back52 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 back78 for (let i = 0; i < base_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 79–83why, one step back79 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 back84 // Need to maybe clone it, as it can in fact be the original value85 // 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 back87 })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 back91 value: clonePatchValueIfNeeded(base_[i])Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 92–96why, one step back92 })93 }94 }9596 // Process added indices.Immer 7Michel Weststratebefore adoption467ea5d
line 97why, one step back97 for (let i = base_.length; i < copy_.length; i++) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 98why, one step back98 const path = basePath.concat([i])Immer 7Michel Weststratebefore adoption467ea5d
line 99why, one step back99 patches.push({Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 100–101why, one step back100 op: ADD,101 path,fix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 102–104why, one step back102 // Need to maybe clone it, as it can in fact be the original value103 // 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 back105 })106 }107 if (base_.length < copy_.length) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 108why, one step back108 inversePatches.push({Immer 7Michel Weststratebefore adoption467ea5d
lines 109–111why, one step back109 op: REPLACE,110 path: basePath.concat(["length"]),111 value: base_.lengthMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 112–134why, one step back112 })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 === REMOVEfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 135–136why, one step back135 ? {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–185why, one step back137 )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_(fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 186why, one step back186 baseValue: any,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 187–193why, one step back187 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 back194 value: replacement === NOTHING ? undefined : replacementMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 195–198why, one step back195 })196 inversePatches.push({197 op: REPLACE,198 path: [],fix: No patches being generated for root primitive replacements. Fixes #888Michel Weststratebefore adoption0f96270
line 199why, one step back199 value: baseValueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 200–208why, one step back200 })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
line 209why, one step back209 const parentType = getArchtype(base)fix(security): Follow up on CVE-2020-28477 where `path: [["__proto__"], "x"]` could still pollute the prototypeMichel Weststratebefore adoptionfa671e5
line 210why, one step back210 const p = "" + path[i]fix: Fixed security issue #738: prototype pollution possible when applying patches CVE-2020-28477Michel Weststratebefore adoptionda2bd4f
lines 211–212why, one step back211 // See #738, avoid prototype pollution212 if (chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 213why, one step back213 (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 back214 (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 back219 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 back228 case Archtype.Map:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 229–230why, one step back229 return base.set(key, value)230 /* istanbul ignore next */chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 231why, one step back231 case Archtype.Set:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 232–241why, one step back232 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 back242 case Archtype.Array:fix: Append to array when key is "-" (#872)Louis DeSciolibefore adoption2afdb1b
lines 243–245why, one step back243 return key === "-"244 ? base.push(value)245 : base.splice(key as any, 0, value)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 246why, one step back246 case Archtype.Map:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 247why, one step back247 return base.set(key, value)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 248why, one step back248 case Archtype.Set:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 249–254why, one step back249 return base.add(value)250 default:251 return (base[key] = value)252 }253 case REMOVE:254 switch (type) {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 255why, one step back255 case Archtype.Array:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 256why, one step back256 return base.splice(key as any, 1)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 257why, one step back257 case Archtype.Map:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 258why, one step back258 return base.delete(key)chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 259why, one step back259 case Archtype.Set:Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 260–276why, one step back260 return base.delete(patch.value)261 default:262 return delete base[key]263 }264 default:265 die(17, op)266 }267 })268269 return draft270 }271272 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?273 // E.g. auto-draft when new objects from outside are assigned and modified?274 // (See failing test when deepClone just returns obj)275 function deepClonePatchValue<T>(obj: T): T276 function deepClonePatchValue(obj: any) {fix: undraftable values should not be cloned for patches, fixes #676Michel Weststratebefore adoption1b70ad5
line 277why, one step back277 if (!isDraftable(obj)) return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 278–285why, one step back278 if (Array.isArray(obj)) return obj.map(deepClonePatchValue)279 if (isMap(obj))280 return new Map(281 Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])282 )283 if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))284 const cloned = Object.create(Object.getPrototypeOf(obj))285 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 286why, one step back286 if (has(obj, immerable)) cloned[immerable] = obj[immerable]Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 287why, one step back287 return clonedfix: Ensure values in patches are never a draft, fixes #559Michel Weststratebefore adoption33ecbd6
lines 288–293why, one step back288 }289290 function clonePatchValueIfNeeded<T>(obj: T): T {291 if (isDraft(obj)) {292 return deepClonePatchValue(obj)293 } else return objMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 294–301why, one step back294 }295296 loadPlugin("Patches", {297 applyPatches_,298 generatePatches_,299 generateReplacementPatches_300 })301}