1 | import type { Middleware } from 'redux'
|
2 | import { getTimeMeasureUtils } from './utils'
|
3 |
|
4 | type EntryProcessor = (key: string, value: any) => any
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export function isImmutableDefault(value: unknown): boolean {
|
12 | return typeof value !== 'object' || value == null || Object.isFrozen(value)
|
13 | }
|
14 |
|
15 | export function trackForMutations(
|
16 | isImmutable: IsImmutableFunc,
|
17 | ignorePaths: IgnorePaths | undefined,
|
18 | obj: any,
|
19 | ) {
|
20 | const trackedProperties = trackProperties(isImmutable, ignorePaths, obj)
|
21 | return {
|
22 | detectMutations() {
|
23 | return detectMutations(isImmutable, ignorePaths, trackedProperties, obj)
|
24 | },
|
25 | }
|
26 | }
|
27 |
|
28 | interface TrackedProperty {
|
29 | value: any
|
30 | children: Record<string, any>
|
31 | }
|
32 |
|
33 | function trackProperties(
|
34 | isImmutable: IsImmutableFunc,
|
35 | ignorePaths: IgnorePaths = [],
|
36 | obj: Record<string, any>,
|
37 | path: string = '',
|
38 | checkedObjects: Set<Record<string, any>> = new Set(),
|
39 | ) {
|
40 | const tracked: Partial<TrackedProperty> = { value: obj }
|
41 |
|
42 | if (!isImmutable(obj) && !checkedObjects.has(obj)) {
|
43 | checkedObjects.add(obj)
|
44 | tracked.children = {}
|
45 |
|
46 | for (const key in obj) {
|
47 | const childPath = path ? path + '.' + key : key
|
48 | if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
|
49 | continue
|
50 | }
|
51 |
|
52 | tracked.children[key] = trackProperties(
|
53 | isImmutable,
|
54 | ignorePaths,
|
55 | obj[key],
|
56 | childPath,
|
57 | )
|
58 | }
|
59 | }
|
60 | return tracked as TrackedProperty
|
61 | }
|
62 |
|
63 | type IgnorePaths = readonly (string | RegExp)[]
|
64 |
|
65 | function detectMutations(
|
66 | isImmutable: IsImmutableFunc,
|
67 | ignoredPaths: IgnorePaths = [],
|
68 | trackedProperty: TrackedProperty,
|
69 | obj: any,
|
70 | sameParentRef: boolean = false,
|
71 | path: string = '',
|
72 | ): { wasMutated: boolean; path?: string } {
|
73 | const prevObj = trackedProperty ? trackedProperty.value : undefined
|
74 |
|
75 | const sameRef = prevObj === obj
|
76 |
|
77 | if (sameParentRef && !sameRef && !Number.isNaN(obj)) {
|
78 | return { wasMutated: true, path }
|
79 | }
|
80 |
|
81 | if (isImmutable(prevObj) || isImmutable(obj)) {
|
82 | return { wasMutated: false }
|
83 | }
|
84 |
|
85 |
|
86 | const keysToDetect: Record<string, boolean> = {}
|
87 | for (let key in trackedProperty.children) {
|
88 | keysToDetect[key] = true
|
89 | }
|
90 | for (let key in obj) {
|
91 | keysToDetect[key] = true
|
92 | }
|
93 |
|
94 | const hasIgnoredPaths = ignoredPaths.length > 0
|
95 |
|
96 | for (let key in keysToDetect) {
|
97 | const nestedPath = path ? path + '.' + key : key
|
98 |
|
99 | if (hasIgnoredPaths) {
|
100 | const hasMatches = ignoredPaths.some((ignored) => {
|
101 | if (ignored instanceof RegExp) {
|
102 | return ignored.test(nestedPath)
|
103 | }
|
104 | return nestedPath === ignored
|
105 | })
|
106 | if (hasMatches) {
|
107 | continue
|
108 | }
|
109 | }
|
110 |
|
111 | const result = detectMutations(
|
112 | isImmutable,
|
113 | ignoredPaths,
|
114 | trackedProperty.children[key],
|
115 | obj[key],
|
116 | sameRef,
|
117 | nestedPath,
|
118 | )
|
119 |
|
120 | if (result.wasMutated) {
|
121 | return result
|
122 | }
|
123 | }
|
124 | return { wasMutated: false }
|
125 | }
|
126 |
|
127 | type IsImmutableFunc = (value: any) => boolean
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | export interface ImmutableStateInvariantMiddlewareOptions {
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | isImmutable?: IsImmutableFunc
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | ignoredPaths?: IgnorePaths
|
148 |
|
149 | warnAfter?: number
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | export function createImmutableStateInvariantMiddleware(
|
162 | options: ImmutableStateInvariantMiddlewareOptions = {},
|
163 | ): Middleware {
|
164 | if (process.env.NODE_ENV === 'production') {
|
165 | return () => (next) => (action) => next(action)
|
166 | } else {
|
167 | function stringify(
|
168 | obj: any,
|
169 | serializer?: EntryProcessor,
|
170 | indent?: string | number,
|
171 | decycler?: EntryProcessor,
|
172 | ): string {
|
173 | return JSON.stringify(obj, getSerialize(serializer, decycler), indent)
|
174 | }
|
175 |
|
176 | function getSerialize(
|
177 | serializer?: EntryProcessor,
|
178 | decycler?: EntryProcessor,
|
179 | ): EntryProcessor {
|
180 | let stack: any[] = [],
|
181 | keys: any[] = []
|
182 |
|
183 | if (!decycler)
|
184 | decycler = function (_: string, value: any) {
|
185 | if (stack[0] === value) return '[Circular ~]'
|
186 | return (
|
187 | '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']'
|
188 | )
|
189 | }
|
190 |
|
191 | return function (this: any, key: string, value: any) {
|
192 | if (stack.length > 0) {
|
193 | var thisPos = stack.indexOf(this)
|
194 | ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
|
195 | ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
|
196 | if (~stack.indexOf(value)) value = decycler!.call(this, key, value)
|
197 | } else stack.push(value)
|
198 |
|
199 | return serializer == null ? value : serializer.call(this, key, value)
|
200 | }
|
201 | }
|
202 |
|
203 | let {
|
204 | isImmutable = isImmutableDefault,
|
205 | ignoredPaths,
|
206 | warnAfter = 32,
|
207 | } = options
|
208 |
|
209 | const track = trackForMutations.bind(null, isImmutable, ignoredPaths)
|
210 |
|
211 | return ({ getState }) => {
|
212 | let state = getState()
|
213 | let tracker = track(state)
|
214 |
|
215 | let result
|
216 | return (next) => (action) => {
|
217 | const measureUtils = getTimeMeasureUtils(
|
218 | warnAfter,
|
219 | 'ImmutableStateInvariantMiddleware',
|
220 | )
|
221 |
|
222 | measureUtils.measureTime(() => {
|
223 | state = getState()
|
224 |
|
225 | result = tracker.detectMutations()
|
226 |
|
227 | tracker = track(state)
|
228 |
|
229 | if (result.wasMutated) {
|
230 | throw new Error(
|
231 | `A state mutation was detected between dispatches, in the path '${
|
232 | result.path || ''
|
233 | }'. This may cause incorrect behavior. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`,
|
234 | )
|
235 | }
|
236 | })
|
237 |
|
238 | const dispatchedAction = next(action)
|
239 |
|
240 | measureUtils.measureTime(() => {
|
241 | state = getState()
|
242 |
|
243 | result = tracker.detectMutations()
|
244 |
|
245 | tracker = track(state)
|
246 |
|
247 | if (result.wasMutated) {
|
248 | throw new Error(
|
249 | `A state mutation was detected inside a dispatch, in the path: ${
|
250 | result.path || ''
|
251 | }. Take a look at the reducer(s) handling the action ${stringify(
|
252 | action,
|
253 | )}. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`,
|
254 | )
|
255 | }
|
256 | })
|
257 |
|
258 | measureUtils.warnIfExceeded()
|
259 |
|
260 | return dispatchedAction
|
261 | }
|
262 | }
|
263 | }
|
264 | }
|