Skip to content

start bin_ins operation implementation #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/json-patch/codec/compact/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type OPCODE_SPLIT = OPCODE.split | 'split';
export type OPCODE_STARTS = OPCODE.starts | 'starts';
export type OPCODE_STR_DEL = OPCODE.str_del | 'str_del';
export type OPCODE_STR_INS = OPCODE.str_ins | 'str_ins';
export type OPCODE_BIN_INS = OPCODE.bin_ins | 'bin_ins';
export type OPCODE_TEST = OPCODE.test | 'test';
export type OPCODE_TEST_STRING = OPCODE.test_string | 'test_string';
export type OPCODE_TEST_STRING_LEN = OPCODE.test_string_len | 'test_string_len';
Expand Down Expand Up @@ -246,6 +247,11 @@ export type CompactStrDelOp =
*/
export type CompactStrInsOp = [opcode: OPCODE_STR_INS, path: string | Path, pos: number, str: string];

/**
* @category JSON Patch Extended
*/
export type CompactBinInsOp = [opcode: OPCODE_BIN_INS, path: string | Path, pos: number, bin: Uint8Array];

/**
* @category JSON Patch Extended
*/
Expand Down
6 changes: 3 additions & 3 deletions src/json-patch/codec/json/decode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type {Op, PredicateOp} from '../../op';
import type {Operation} from './types';
import {OpAdd} from '../../op/OpAdd';
import {OpRemove} from '../../op/OpRemove';
import {OpReplace} from '../../op/OpReplace';
Expand Down Expand Up @@ -30,8 +28,10 @@ import {OpNot} from '../../op/OpNot';
import {OpMatches} from '../../op/OpMatches';
import {OpType} from '../../op/OpType';
import {toPath} from '@jsonjoy.com/json-pointer';
import type {JsonPatchOptions} from '../../types';
import {createMatcherDefault} from '../../util';
import type {Op, PredicateOp} from '../../op';
import type {Operation} from './types';
import type {JsonPatchOptions} from '../../types';

export const operationToOp = (op: Operation, options: JsonPatchOptions): Op => {
switch (op.op) {
Expand Down
23 changes: 23 additions & 0 deletions src/json-patch/codec/json/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ export interface OperationOr extends OperationBase {
export type JsonPatchExtendedOperation =
| OperationStrIns
| OperationStrDel
| OperationBinIns
| OperationBinDel
| OperationFlip
| OperationInc
| OperationSplit
Expand Down Expand Up @@ -302,6 +304,27 @@ export interface OperationStrDel extends OperationBase {
readonly len?: number;
}

/**
* Inserts a `value` blob into a blob at position `pos`.
*
* @category JSON Patch Extended
*/
export interface OperationBinIns extends OperationBase {
readonly op: 'bin_ins';
readonly pos: number;
readonly bin: Uint8Array;
}

/**
* @category JSON Patch Extended
*/
export interface OperationBinDel extends OperationBase {
readonly op: 'bin_del';
readonly pos: number;
readonly bin?: Uint8Array;
readonly len?: number;
}

/**
* Flips boolean value to the opposite one.
*
Expand Down
4 changes: 4 additions & 0 deletions src/json-patch/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export enum OPCODE {
and = 43,
not = 44,
or = 45,

// Binary editing.
bin_ins = 60,
bin_del = 61,
}
63 changes: 63 additions & 0 deletions src/json-patch/op/OpBinIns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {AbstractOp} from './AbstractOp';
import {OPCODE} from '../constants';
import {find, type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import type {CompactBinInsOp, OPCODE_BIN_INS} from '../codec/compact/types';
import type {OperationBinIns} from '../types';
import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack';

/**
* @category JSON Patch Extended
*/
export class OpBinIns extends AbstractOp<'bin_ins'> {
constructor(
path: Path,
public readonly pos: number,
public readonly bin: Uint8Array,
) {
super(path);
}

public op() {
return 'bin_ins' as const;
}

public code() {
return OPCODE.bin_ins;
}

public apply(doc: unknown) {
const {val, key, obj} = find(doc, this.path);
if (!(val instanceof Uint8Array)) {
if (val !== undefined) throw new Error('NOT_BIN');
if (this.pos !== 0) throw new Error('POS');
}
const bin: Uint8Array = val instanceof Uint8Array ? val : new Uint8Array(0);
const pos = Math.min(this.pos, bin.length);
const result = new Uint8Array(bin.length + this.bin.length);
result.set(bin.slice(0, pos), 0);
result.set(this.bin, pos);
result.set(bin.slice(pos), pos + this.bin.length);
if (obj) (obj as any)[key as any] = result;
else doc = result;
return {doc, old: val};
}

public toJson(parent?: AbstractOp): OperationBinIns {
const op: OperationBinIns = {
op: 'bin_ins',
path: formatJsonPointer(this.path),
pos: this.pos,
bin: this.bin,
};
return op;
}

public toCompact(parent: undefined | AbstractOp, verbose: boolean): CompactBinInsOp {
const opcode: OPCODE_BIN_INS = verbose ? 'bin_ins' : OPCODE.bin_ins;
return [opcode, this.path, this.pos, this.bin];
}

public encode(encoder: IMessagePackEncoder, parent?: AbstractOp) {
throw new Error('Not implemented');
}
}
3 changes: 3 additions & 0 deletions src/json-patch/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export type JsonPatchExtendedOpType =
// Operations needed for text collaboration.
| 'str_ins'
| 'str_del'
// Operations needed for binary blob collaboration.
| 'bin_ins'
| 'bin_del'
// Operations needed for Slate.js.
| 'split'
| 'merge'
Expand Down
Loading