@crombolo/core 0.18.0 → 0.19.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/package.json +1 -1
- package/src/json-patch.ts +36 -11
package/package.json
CHANGED
package/src/json-patch.ts
CHANGED
|
@@ -19,14 +19,22 @@ type Step =
|
|
|
19
19
|
|
|
20
20
|
class PatchError extends Error {}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
/** Names that would reach an object's prototype rather than its own data. */
|
|
23
|
+
const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
|
|
24
|
+
|
|
25
|
+
function unescapeSegment(segment: string, path: string): string {
|
|
26
|
+
const key = segment.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
27
|
+
if (FORBIDDEN_KEYS.has(key))
|
|
28
|
+
throw new PatchError(`"${key}" may not appear in a path, in "${path}".`)
|
|
29
|
+
return key
|
|
30
|
+
}
|
|
23
31
|
|
|
24
32
|
function parseFilter(body: string, path: string): Step {
|
|
25
33
|
const conditions = body.split(',').map((pair): [string, string | null] => {
|
|
26
34
|
const at = pair.indexOf('=')
|
|
27
35
|
if (at <= 0) throw new PatchError(`The filter "[${body}]" in "${path}" needs key=value pairs.`)
|
|
28
|
-
const value = unescapeSegment(pair.slice(at + 1))
|
|
29
|
-
return [unescapeSegment(pair.slice(0, at)), value.toLowerCase() === 'null' ? null : value]
|
|
36
|
+
const value = unescapeSegment(pair.slice(at + 1), path)
|
|
37
|
+
return [unescapeSegment(pair.slice(0, at), path), value.toLowerCase() === 'null' ? null : value]
|
|
30
38
|
})
|
|
31
39
|
return { kind: 'filter', conditions }
|
|
32
40
|
}
|
|
@@ -39,12 +47,12 @@ export function parsePatchPath(path: string): Step[] {
|
|
|
39
47
|
const open = segment.indexOf('[')
|
|
40
48
|
if (open === -1) {
|
|
41
49
|
steps.push(
|
|
42
|
-
segment === '-' ? { kind: 'append' } : { kind: 'key', key: unescapeSegment(segment) },
|
|
50
|
+
segment === '-' ? { kind: 'append' } : { kind: 'key', key: unescapeSegment(segment, path) },
|
|
43
51
|
)
|
|
44
52
|
continue
|
|
45
53
|
}
|
|
46
54
|
if (!segment.endsWith(']')) throw new PatchError(`The filter in "${path}" is not closed.`)
|
|
47
|
-
if (open > 0) steps.push({ kind: 'key', key: unescapeSegment(segment.slice(0, open)) })
|
|
55
|
+
if (open > 0) steps.push({ kind: 'key', key: unescapeSegment(segment.slice(0, open), path) })
|
|
48
56
|
steps.push(parseFilter(segment.slice(open + 1, -1), path))
|
|
49
57
|
}
|
|
50
58
|
steps.forEach((step, index) => {
|
|
@@ -63,8 +71,8 @@ function matchIndex(array: unknown[], step: Extract<Step, { kind: 'filter' }>, p
|
|
|
63
71
|
isRecord(element) &&
|
|
64
72
|
step.conditions.every(([key, value]) =>
|
|
65
73
|
value === null
|
|
66
|
-
? element
|
|
67
|
-
: element[key] === value,
|
|
74
|
+
? !Object.hasOwn(element, key) || element[key] === null
|
|
75
|
+
: Object.hasOwn(element, key) && element[key] === value,
|
|
68
76
|
),
|
|
69
77
|
)
|
|
70
78
|
if (index === -1) throw new PatchError(`Nothing in the list matches the filter in "${path}".`)
|
|
@@ -85,13 +93,21 @@ function child(container: unknown, step: Step, path: string): unknown {
|
|
|
85
93
|
if (step.kind === 'filter') return container[matchIndex(container, step, path)]
|
|
86
94
|
if (step.kind === 'key') return container[arrayIndex(container, step.key, path, false)]
|
|
87
95
|
} else if (isRecord(container) && step.kind === 'key') {
|
|
88
|
-
if (!(
|
|
96
|
+
if (!Object.hasOwn(container, step.key))
|
|
97
|
+
throw new PatchError(`There is no "${step.key}" in "${path}".`)
|
|
89
98
|
return container[step.key]
|
|
90
99
|
}
|
|
91
100
|
throw new PatchError(`"${path}" does not lead anywhere in the document.`)
|
|
92
101
|
}
|
|
93
102
|
|
|
94
|
-
|
|
103
|
+
/** Checks an operation's shape and path without touching the document. */
|
|
104
|
+
function validate(operation: PatchOperation): Step[] {
|
|
105
|
+
if (
|
|
106
|
+
!isRecord(operation) ||
|
|
107
|
+
typeof operation.op !== 'string' ||
|
|
108
|
+
typeof operation.path !== 'string'
|
|
109
|
+
)
|
|
110
|
+
throw new PatchError('Every operation needs an op and a path.')
|
|
95
111
|
const { op, path } = operation
|
|
96
112
|
if (op !== 'replace' && op !== 'add' && op !== 'remove')
|
|
97
113
|
throw new PatchError(
|
|
@@ -103,6 +119,12 @@ function applyOne(root: unknown, operation: PatchOperation): unknown {
|
|
|
103
119
|
const last = steps[steps.length - 1] as Step
|
|
104
120
|
if (last.kind === 'append' && op !== 'add')
|
|
105
121
|
throw new PatchError(`"-" appends, so only add may use it, in "${path}".`)
|
|
122
|
+
return steps
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function applyOne(root: unknown, operation: PatchOperation, steps: Step[]): unknown {
|
|
126
|
+
const { op, path } = operation
|
|
127
|
+
const last = steps[steps.length - 1] as Step
|
|
106
128
|
let parent = root
|
|
107
129
|
for (const step of steps.slice(0, -1)) parent = child(parent, step, path)
|
|
108
130
|
|
|
@@ -120,7 +142,7 @@ function applyOne(root: unknown, operation: PatchOperation): unknown {
|
|
|
120
142
|
}
|
|
121
143
|
if (isRecord(parent) && last.kind === 'key') {
|
|
122
144
|
if (op === 'remove') {
|
|
123
|
-
if (!(last.key
|
|
145
|
+
if (!Object.hasOwn(parent, last.key))
|
|
124
146
|
throw new PatchError(`There is no "${last.key}" to remove, in "${path}".`)
|
|
125
147
|
delete parent[last.key]
|
|
126
148
|
} else parent[last.key] = operation.value
|
|
@@ -134,8 +156,11 @@ export function applyJsonPatch(
|
|
|
134
156
|
operations: readonly PatchOperation[],
|
|
135
157
|
): PatchResult {
|
|
136
158
|
try {
|
|
159
|
+
const parsed = operations.map(validate)
|
|
137
160
|
let value = structuredClone(document)
|
|
138
|
-
|
|
161
|
+
operations.forEach((operation, index) => {
|
|
162
|
+
value = applyOne(value, operation, parsed[index] as Step[])
|
|
163
|
+
})
|
|
139
164
|
return { ok: true, value }
|
|
140
165
|
} catch (error) {
|
|
141
166
|
if (error instanceof PatchError) return { ok: false, reason: error.message }
|