Skip to content
Myr

proxy.ts

This file

Path

src/core/proxy.ts

380 lines · 10.5 KiB · TypeScript

Last changed by

fix: undefined assigned to a prototype-inherited key gets dropped (#1262)Alexander Kireyevbefore adoption16e225b

History of this file

Take it

The walk stopped after 200 commits: lines older than d91a659 point at it.

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 1–13why, one step back
1import {
2 has,3 is,4 isDraftable,5 shallowCopy,6 latest,7 ImmerBaseState,8 ImmerState,9 Drafted,10 AnyObject,11 AnyArray,12 Objectish,13 getCurrentScope,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 14why, one step back
14 getPrototypeOf,

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 15–17why, one step back
15 DRAFT_STATE,
16 die,17 createProxy,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 18why, one step back
18 ArchType,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 19–23why, one step back
19 handleCrossReference,
20 WRITABLE,21 CONFIGURABLE,22 ENUMERABLE,23 VALUE,

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 24–25why, one step back
24 isArray,
25 isArrayIndex

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 26–33why, one step back
26} from "../internal"
2728interface ProxyBaseState extends ImmerBaseState {29 parent_?: ImmerState30 revoke_(): void31}3233export interface ProxyObjectState extends ProxyBaseState {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 34why, one step back
34 type_: ArchType.Object

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 35–40why, one step back
35 base_: any
36 copy_: any37 draft_: Drafted<AnyObject, ProxyObjectState>38}3940export interface ProxyArrayState extends ProxyBaseState {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 41why, one step back
41 type_: ArchType.Array

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 42–44why, one step back
42 base_: AnyArray
43 copy_: AnyArray | null44 draft_: Drafted<AnyArray, ProxyArrayState>

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 45–46why, one step back
45 operationMethod?: string
46 allIndicesReassigned_?: boolean

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 47–58why, one step back
47}
4849type ProxyState = ProxyObjectState | ProxyArrayState5051/**52 * Returns a new draft of the `base` object.53 *54 * The second argument is the parent draft-state (used internally).55 */56export function createProxyProxy<T extends Objectish>(57 base: T,58 parent?: ImmerState

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 59–60why, one step back
59): [Drafted<T, ProxyState>, ProxyState] {
60 const baseIsArray = isArray(base)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 61why, one step back
61 const state: ProxyState = {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 62why, one step back
62 type_: baseIsArray ? ArchType.Array : (ArchType.Object as any),

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 63–69why, one step back
63 // Track which produce call this is associated with.
64 scope_: parent ? parent.scope_ : getCurrentScope()!,65 // True for both shallow and deep changes.66 modified_: false,67 // Used during finalization.68 finalized_: false,69 // Track which properties have been assigned (true) or deleted (false).

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 70–71why, one step back
70 // actually instantiated in `prepareCopy()`
71 assigned_: undefined,

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 72–81why, one step back
72 // The parent draft state.
73 parent_: parent,74 // The base state.75 base_: base,76 // The base proxy.77 draft_: null as any, // set below78 // The base copy with any updated values.79 copy_: null,80 // Called by the `produce` function.81 revoke_: null as any,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 82–84why, one step back
82 isManual_: false,
83 // `callbacks` actually gets assigned in `createProxy`84 callbacks_: undefined as any

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 85–94why, one step back
85 }
8687 // the traps must target something, a bit like the 'real' base.88 // but also, we need to be able to determine from the target what the relevant state is89 // (to avoid creating traps per instance to capture the state in closure,90 // and to avoid creating weird hidden properties as well)91 // So the trick is to use 'state' as the actual 'target'! (and make sure we intercept everything)92 // Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb93 let target: T = state as any94 let traps: ProxyHandler<object | Array<any>> = objectTraps

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 95why, one step back
95 if (baseIsArray) {

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 96–102why, one step back
96 target = [state] as any
97 traps = arrayTraps98 }99100 const {revoke, proxy} = Proxy.revocable(target, traps)101 state.draft_ = proxy as any102 state.revoke_ = revoke

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 103why, one step back
103 return [proxy as any, state]

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 104–112why, one step back
104}
105106/**107 * Object drafts108 */109export const objectTraps: ProxyHandler<ProxyState> = {110 get(state, prop) {111 if (prop === DRAFT_STATE) return state112

fix: prevent prototype pollution via constructor.prototype access (CVE-2026-XXXX) (#1259)Michel Weststratebefore adoption48fc378

lines 113–138why, one step back
113 // Guard against prototype pollution via constructor and __proto__
114 // We allow access but wrap in a proxy that blocks prototype chain traversal115 if (prop === "constructor" || prop === "__proto__") {116 const source = latest(state)117 const value = source[prop]118 // Return a proxy that allows calling the constructor but blocks access to prototype119 return new Proxy(value || {}, {120 get: (target, key) => {121 // Block __proto__ and prototype access chains122 if (key === "__proto__" || key === "prototype") {123 return Object.freeze(Object.create(null))124 }125 // Allow normal property access for legitimate use126 return Reflect.get(target, key)127 },128 set: () => {129 // Silently ignore writes to prevent pollution130 return true131 },132 apply: (target, thisArg, args) => {133 // Allow constructor to be called as a function (e.g., draft.arr.constructor(1))134 return Reflect.apply(target as Function, thisArg, args)135 }136 })137 }138

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 139–149why, one step back
139 let arrayPlugin = state.scope_.arrayMethodsPlugin_
140 const isArrayWithStringProp =141 state.type_ === ArchType.Array && typeof prop === "string"142 // Intercept array methods so that we can override143 // behavior and skip proxy creation for perf144 if (isArrayWithStringProp) {145 if (arrayPlugin?.isArrayOperationMethod(prop)) {146 return arrayPlugin.createMethodInterceptor(state, prop)147 }148 }149

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 150why, one step back
150 const source = latest(state)

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 151why, one step back
151 if (!has(source, prop, state.type_)) {

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 152–156why, one step back
152 // non-existing or non-own property...
153 return readPropFromProto(state, source, prop)154 }155 const value = source[prop]156 if (state.finalized_ || !isDraftable(value)) {

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 157–170why, one step back
157 return value
158 }159160 // During mutating array operations, defer proxy creation for array elements161 // This optimization avoids creating unnecessary proxies during sort/reverse162 if (163 isArrayWithStringProp &&164 (state as ProxyArrayState).operationMethod &&165 arrayPlugin?.isMutatingArrayMethod(166 (state as ProxyArrayState).operationMethod!167 ) &&168 isArrayIndex(prop)169 ) {170 // Return raw value during mutating operations, create proxy only if modified

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 171–176why, one step back
171 return value
172 }173 // Check for existing draft in modified state.174 // Assigned values are never drafted. This catches any drafts we created, too.175 if (value === peek(state.base_, prop)) {176 prepareCopy(state)

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 177–181why, one step back
177 // Ensure array keys are always numbers
178 const childKey = state.type_ === ArchType.Array ? +(prop as string) : prop179 const childDraft = createProxy(state.scope_, value, state, childKey)180181 return (state.copy_![childKey] = childDraft)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 182–185why, one step back
182 }
183 return value184 },185 has(state, prop) {

fix: prevent prototype pollution via constructor.prototype access (CVE-2026-XXXX) (#1259)Michel Weststratebefore adoption48fc378

lines 186–193why, one step back
186 // Block reserved properties from being detected
187 if (188 prop === "constructor" ||189 prop === "__proto__" ||190 prop === "prototype"191 ) {192 return false193 }

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 194–203why, one step back
194 return prop in latest(state)
195 },196 ownKeys(state) {197 return Reflect.ownKeys(latest(state))198 },199 set(200 state: ProxyObjectState,201 prop: string /* strictly not, but helps TS */,202 value203 ) {

fix: prevent prototype pollution via constructor.prototype access (CVE-2026-XXXX) (#1259)Michel Weststratebefore adoption48fc378

lines 204–213why, one step back
204 // Guard against prototype pollution - prevent assignment to reserved properties
205 // that could lead to Object.prototype pollution206 if (207 prop === "constructor" ||208 prop === "__proto__" ||209 prop === "prototype"210 ) {211 return true212 }213

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 214–228why, one step back
214 const desc = getDescriptorFromProto(latest(state), prop)
215 if (desc?.set) {216 // special case: if this write is captured by a setter, we have217 // to trigger it with the correct context218 desc.set.call(state.draft_, value)219 return true220 }221 if (!state.modified_) {222 // the last check is because we need to be able to distinguish setting a non-existing to undefined (which is a change)223 // from setting an existing property with value undefined to undefined (which is not a change)224 const current = peek(latest(state), prop)225 // special case, if we assigning the original value to a draft, we can ignore the assignment226 const currentState: ProxyObjectState = current?.[DRAFT_STATE]227 if (currentState && currentState.base_ === value) {228 state.copy_![prop] = value

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 229why, one step back
229 state.assigned_!.set(prop, false)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 230–231why, one step back
230 return true
231 }

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 232–235why, one step back
232 if (
233 is(value, current) &&234 (value !== undefined || has(state.base_, prop, state.type_))235 )

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 236–241why, one step back
236 return true
237 prepareCopy(state)238 markChanged(state)239 }240241 if (

fix: special case NaN in setter (#912)Raybefore adoption5721bb7

lines 242–243why, one step back
242 (state.copy_![prop] === value &&
243 // special case: handle new props with value 'undefined'

fix: undefined assigned to a prototype-inherited key gets dropped (#1262)Alexander Kireyevbefore adoption16e225b

line 244why, one step back
244 (value !== undefined || has(state.copy_, prop, state.type_))) ||

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 245why, one step back
245 // special case: NaN

fix: special case NaN in setter (#912)Raybefore adoption5721bb7

line 246why, one step back
246 (Number.isNaN(value) && Number.isNaN(state.copy_![prop]))

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 247–251why, one step back
247 )
248 return true249250 // @ts-ignore251 state.copy_![prop] = value

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 252–254why, one step back
252 state.assigned_!.set(prop, true)
253254 handleCrossReference(state, prop, value)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 255–257why, one step back
255 return true
256 },257 deleteProperty(state, prop: string) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 258why, one step back
258 prepareCopy(state)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 259–260why, one step back
259 // The `undefined` check is a fast path for pre-existing keys.
260 if (peek(state.base_, prop) !== undefined || prop in state.base_) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 261why, one step back
261 state.assigned_!.set(prop, false)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 262–264why, one step back
262 markChanged(state)
263 } else {264 // if an originally not assigned property was deleted

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 265why, one step back
265 state.assigned_!.delete(prop)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 266why, one step back
266 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 267–269why, one step back
267 if (state.copy_) {
268 delete state.copy_[prop]269 }

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 270–278why, one step back
270 return true
271 },272 // Note: We never coerce `desc.value` into an Immer draft, because we can't make273 // the same guarantee in ES5 mode.274 getOwnPropertyDescriptor(state, prop) {275 const owner = latest(state)276 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)277 if (!desc) return desc278 return {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 279–282why, one step back
279 [WRITABLE]: true,
280 [CONFIGURABLE]: state.type_ !== ArchType.Array || prop !== "length",281 [ENUMERABLE]: desc[ENUMERABLE],282 [VALUE]: owner[prop]

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 283–288why, one step back
283 }
284 },285 defineProperty() {286 die(11)287 },288 getPrototypeOf(state) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 289why, one step back
289 return getPrototypeOf(state.base_)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 290–300why, one step back
290 },
291 setPrototypeOf() {292 die(12)293 }294}295296/**297 * Array drafts298 */299300const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

lines 301–304why, one step back
301// Use `for..in` instead of `each` to work around a weird
302// prod test suite issue303for (let key in objectTraps) {304 let fn = objectTraps[key as keyof typeof objectTraps] as Function

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 305why, one step back
305 // @ts-ignore

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 306why, one step back
306 arrayTraps[key] = function () {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 307–309why, one step back
307 const args = arguments
308 args[0] = args[0][0]309 return fn.apply(this, args)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 310why, one step back
310 }

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

line 311why, one step back
311}

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 312why, one step back
312arrayTraps.deleteProperty = function (state, prop) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 313–314why, one step back
313 if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop as any)))
314 die(13)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 315–317why, one step back
315 // @ts-ignore
316 return arrayTraps.set!.call(this, state, prop, undefined)317}

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

line 318why, one step back
318arrayTraps.set = function (state, prop, value) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 319–324why, one step back
319 if (
320 process.env.NODE_ENV !== "production" &&321 prop !== "length" &&322 isNaN(parseInt(prop as any))323 )324 die(14)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 325–337why, one step back
325 return objectTraps.set!.call(this, state[0], prop, value, state[0])
326}327328// Access a property without creating an Immer draft.329function peek(draft: Drafted, prop: PropertyKey) {330 const state = draft[DRAFT_STATE]331 const source = state ? latest(state) : draft332 return source[prop]333}334335function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {336 const desc = getDescriptorFromProto(source, prop)337 return desc

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 338–339why, one step back
338 ? VALUE in desc
339 ? desc[VALUE]

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 340why, one step back
340 : // This is a very special case, if the prop is a getter defined by the

chore: Merge branch 'unional-prettier'Michel Weststratebefore adoption37eaf8f

lines 341–342why, one step back
341 // prototype, we should invoke it with the draft as context!
342 desc.get?.call(state.draft_)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 343–351why, one step back
343 : undefined
344}345346function getDescriptorFromProto(347 source: any,348 prop: PropertyKey349): PropertyDescriptor | undefined {350 // 'in' checks proto!351 if (!(prop in source)) return undefined

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 352why, one step back
352 let proto = getPrototypeOf(source)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 353–355why, one step back
353 while (proto) {
354 const desc = Object.getOwnPropertyDescriptor(proto, prop)355 if (desc) return desc

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 356why, one step back
356 proto = getPrototypeOf(proto)

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 357–369why, one step back
357 }
358 return undefined359}360361export function markChanged(state: ImmerState) {362 if (!state.modified_) {363 state.modified_ = true364 if (state.parent_) {365 markChanged(state.parent_)366 }367 }368}369

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 370why, one step back
370export function prepareCopy(state: ImmerState) {

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 371why, one step back
371 if (!state.copy_) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 372–374why, one step back
372 // Actually create the `assigned_` map now that we
373 // know this is a modified draft.374 state.assigned_ = new Map()

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 375–378why, one step back
375 state.copy_ = shallowCopy(
376 state.base_,377 state.scope_.immer_.useStrictShallowCopy_378 )

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 379–380why, one step back
379 }
380}