Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | 2x 2x 2x 2x 2x 2x 2x 166x 166x 166x 166x 166x 166x 214x 214x 214x 14x 14x 14x 10x 10x 10x 10x 24x 10x 4x 4x 4x 4x 8x 8x 8x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 10x 10x 10x 4x 4x 4x 10x 10x 10x 2x 2x 2x 2x 4x 4x 4x 4x 4x 10x 10x 10x 4x 4x 4x 4x 4x 4x 4x 12x 4x 4x 4x 4x 4x 12x 4x 8x 8x 8x 8x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 4x 4x 4x 4x 4x 4x 4x 34x 34x 34x 34x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 6x 14x 14x 14x 6x 6x 144x 144x 48x 10x 10x 10x 242x 242x 242x 398x 398x 398x 42x 42x 42x 4x | import { type AbstractPredicateOp, type Op, OpAdd, OpAnd, OpContains, OpCopy, OpDefined, OpEnds, OpExtend, OpFlip, OpIn, OpInc, OpLess, OpMatches, OpMerge, OpMore, OpMove, OpNot, OpOr, OpRemove, OpReplace, OpSplit, OpStarts, OpStrDel, OpStrIns, OpTest, OpTestString, OpTestStringLen, OpTestType, OpType, OpUndefined, } from '../../op'; import {MsgPackDecoderFast} from '@jsonjoy.com/json-pack/lib/msgpack/MsgPackDecoderFast'; import {OPCODE} from '../../constants'; import type {Path} from '@jsonjoy.com/json-pointer'; import type {JsonPatchTypes} from '../json/types'; import {createMatcherDefault} from '../../util'; import type {JsonPatchOptions} from '../../types'; import type {Reader} from '@jsonjoy.com/util/lib/buffers/Reader'; export class Decoder extends MsgPackDecoderFast<Reader> { constructor(private readonly options: JsonPatchOptions) { super(); } public decode(uint8: Uint8Array): Op[] { this.reader.reset(uint8); return this.decodePatch(); } protected decodePatch(): Op[] { const len = this.decodeArrayHeader(); const ops: Op[] = []; for (let i = 0; i < len; i++) ops.push(this.decodeOp(undefined)); return ops; } protected decodeOp(parent: Op | undefined): Op { const length = this.decodeArrayHeader(); const opcode = this.reader.u8(); switch (opcode) { case OPCODE.add: { const path = this.decodeArray() as Path; const value = this.val(); return new OpAdd(path, value); } case OPCODE.and: { const path = this.decodePath(parent); const length = this.decodeArrayHeader(); const ops: AbstractPredicateOp[] = []; const op = new OpAnd(path, ops); for (let i = 0; i < length; i++) ops.push(this.decodeOp(op) as AbstractPredicateOp); return op; } case OPCODE.contains: { const path = this.decodePath(parent); const value = this.decodeString(); const ignoreCase = length > 3; return new OpContains(path, value, ignoreCase); } case OPCODE.copy: { const path = this.decodeArray() as Path; const from = this.decodeArray() as Path; return new OpCopy(path, from); } case OPCODE.defined: { const path = this.decodePath(parent); return new OpDefined(path); } case OPCODE.ends: { const path = this.decodePath(parent); const value = this.decodeString(); const ignoreCase = length > 3; return new OpEnds(path, value, ignoreCase); } case OPCODE.extend: { const path = this.decodeArray() as Path; const props = this.decodeObject() as Record<string, unknown>; const deleteNull = length > 3; return new OpExtend(path, props, deleteNull); } case OPCODE.flip: { const path = this.decodeArray() as Path; return new OpFlip(path); } case OPCODE.in: { const path = this.decodePath(parent); const value = this.decodeArray(); return new OpIn(path, value); } case OPCODE.inc: { const path = this.decodePath(parent); const inc = this.val() as number; return new OpInc(path, inc); } case OPCODE.less: { const path = this.decodePath(parent); const value = this.val() as number; return new OpLess(path, value); } case OPCODE.matches: { const path = this.decodePath(parent); const value = this.decodeString(); const ignoreCase = length > 3; return new OpMatches(path, value, ignoreCase, this.options.createMatcher || createMatcherDefault); } case OPCODE.merge: { const hasProps = length > 3; const path = this.decodeArray() as Path; const pos = this.val() as number; const props = hasProps ? this.decodeObject() : null; return new OpMerge(path, pos, props); } case OPCODE.more: { const path = this.decodePath(parent); const value = this.val() as number; return new OpMore(path, value); } case OPCODE.move: { const path = this.decodeArray() as Path; const from = this.decodeArray() as Path; return new OpMove(path, from); } case OPCODE.not: { const path = this.decodePath(parent); const length = this.decodeArrayHeader(); const ops: AbstractPredicateOp[] = []; const op = new OpNot(path, ops); for (let i = 0; i < length; i++) ops.push(this.decodeOp(op) as AbstractPredicateOp); return op; } case OPCODE.or: { const path = this.decodePath(parent); const length = this.decodeArrayHeader(); const ops: AbstractPredicateOp[] = []; const op = new OpOr(path, ops); for (let i = 0; i < length; i++) ops.push(this.decodeOp(op) as AbstractPredicateOp); return op; } case OPCODE.remove: { const path = this.decodeArray() as Path; const hasOldValue = length > 2; const oldValue = hasOldValue ? this.val() : undefined; return new OpRemove(path, oldValue); } case OPCODE.replace: { const path = this.decodeArray() as Path; const value = this.val(); const hasOldValue = length > 3; const oldValue = hasOldValue ? this.val() : undefined; return new OpReplace(path, value, oldValue); } case OPCODE.split: { const path = this.decodeArray() as Path; const pos = this.val() as number; const hasProps = length > 3; const props = hasProps ? this.decodeObject() : null; return new OpSplit(path, pos, props); } case OPCODE.starts: { const ignoreCase = length > 3; const path = this.decodePath(parent); const value = this.decodeString(); return new OpStarts(path, value, ignoreCase); } case OPCODE.str_del: { const hasStr = length < 5; const path = this.decodeArray() as Path; const pos = this.val() as number; if (hasStr) { const str = this.decodeString(); return new OpStrDel(path, pos, str, undefined); } else { this.reader.u8(); const len = this.val() as number; return new OpStrDel(path, pos, undefined, len); } } case OPCODE.str_ins: { const path = this.decodeArray() as Path; const pos = this.val() as number; const str = this.decodeString(); return new OpStrIns(path, pos, str); } case OPCODE.test: { const not = length > 3; const path = this.decodePath(parent); const value = this.val(); return new OpTest(path, value, not); } case OPCODE.test_string: { const not = length > 4; const path = this.decodePath(parent); const pos = this.val() as number; const str = this.decodeString(); return new OpTestString(path, pos, str, not); } case OPCODE.test_string_len: { const not = length > 3; const path = this.decodePath(parent); const len = this.val() as number; return new OpTestStringLen(path, len, not); } case OPCODE.test_type: { const path = this.decodePath(parent); const type = this.decodeArray() as JsonPatchTypes[]; return new OpTestType(path, type); } case OPCODE.type: { const path = this.decodePath(parent); const value = this.decodeString() as JsonPatchTypes; return new OpType(path, value); } case OPCODE.undefined: { const path = this.decodePath(parent); return new OpUndefined(path); } } throw new Error('OP_UNKNOWN'); } protected decodePath(parent: Op | undefined): Path { const path = this.decodeArray() as Path; if (!parent) return path; return [...parent.path, ...path]; } protected decodeObject(): object { const reader = this.reader; const byte = reader.u8(); if (byte <= 0xbf) return this.obj(byte & 0b1111); else Eif (byte === 0xde) return this.obj(reader.u16()); /* 0xdf */ else return this.obj(reader.u32()); } protected decodeArray(): unknown[] { const reader = this.reader; const byte = reader.u8(); if (byte < 0b10011111) return this.arr(byte & 0b1111); else Eif (byte === 0xdc) return this.arr(reader.u16()); else return this.arr(reader.u32()); } protected decodeArrayHeader(): number { const reader = this.reader; const byte = reader.u8(); if (byte < 0b10011111) return byte & 0b1111; else Eif (byte === 0xdc) return reader.u16(); else return reader.u32(); } protected decodeString(): string { const reader = this.reader; const byte = reader.u8(); if (byte <= 0xbf) return reader.utf8(byte & 0b11111); else if (byte === 0xd9) return reader.utf8(reader.u8()); else Eif (byte === 0xda) return reader.utf8(reader.u16()); /* 0xDB */ else return reader.utf8(reader.u32()); } } |