Skip to content

Commit b0ab0bc

Browse files
Copilotstreamich
andcommitted
Implement CSON parser from scratch without cson-parser dependency
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent fe208a8 commit b0ab0bc

File tree

10 files changed

+668
-67
lines changed

10 files changed

+668
-67
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"dependencies": {
6868
"@jsonjoy.com/base64": "^1.1.1",
6969
"@jsonjoy.com/util": "^1.1.2",
70-
"cson-parser": "^4.0.9",
7170
"hyperdyperid": "^1.2.0",
7271
"thingies": "^1.20.0"
7372
},

src/cson/CsonDecoder.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {Reader} from '@jsonjoy.com/util/lib/buffers/Reader';
22
import type {BinaryJsonDecoder, PackValue} from '../types';
3+
import {CsonParser} from './CsonParser';
34

45
export class CsonDecoder implements BinaryJsonDecoder {
56
public reader = new Reader();
7+
private parser = new CsonParser();
68

79
public read(uint8: Uint8Array): unknown {
810
this.reader.reset(uint8);
@@ -12,11 +14,10 @@ export class CsonDecoder implements BinaryJsonDecoder {
1214
public decode(uint8: Uint8Array): unknown {
1315
// Convert Uint8Array to string
1416
const csonText = new TextDecoder().decode(uint8);
15-
17+
1618
try {
17-
// Use the cson-parser library to parse the CSON text
18-
const csonParser = require('cson-parser');
19-
return csonParser.parse(csonText);
19+
// Use our custom CSON parser
20+
return this.parser.parse(csonText);
2021
} catch (error) {
2122
const errorMessage = error instanceof Error ? error.message : String(error);
2223
throw new Error(`CSON parsing error: ${errorMessage}`);
@@ -29,4 +30,4 @@ export class CsonDecoder implements BinaryJsonDecoder {
2930
const remainingBytes = this.reader.uint8.slice(this.reader.x);
3031
return this.decode(remainingBytes);
3132
}
32-
}
33+
}

src/cson/CsonEncoder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class CsonEncoder implements BinaryJsonEncoder {
88

99
constructor(
1010
public readonly writer: IWriter & IWriterGrowable,
11-
options: CsonEncoderOptions = {}
11+
options: CsonEncoderOptions = {},
1212
) {
1313
this.options = {
1414
indent: options.indent ?? 2,
@@ -94,19 +94,19 @@ export class CsonEncoder implements BinaryJsonEncoder {
9494
}
9595

9696
public writeBin(buf: Uint8Array): void {
97-
// Convert binary data to base64 string in CSON format
97+
// Convert binary data to base64 string in CSON format
9898
const writer = this.writer;
9999
const length = buf.length;
100-
100+
101101
writer.ascii("Buffer.from('");
102-
102+
103103
// Use the existing base64 utility from the json-pack library
104104
const tempBuffer = new ArrayBuffer(((length + 2) / 3) * 4);
105105
const tempView = new DataView(tempBuffer);
106106
const base64Length = toBase64Bin(buf, 0, length, tempView, 0);
107107
const base64Array = new Uint8Array(tempBuffer, 0, base64Length);
108108
const base64String = new TextDecoder().decode(base64Array);
109-
109+
110110
writer.ascii(base64String);
111111
writer.ascii("', 'base64')");
112112
}
@@ -226,4 +226,4 @@ export class CsonEncoder implements BinaryJsonEncoder {
226226
}
227227
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key);
228228
}
229-
}
229+
}

0 commit comments

Comments
 (0)