-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #549 from streamich/dag-json
DAG-JSON
- Loading branch information
Showing
20 changed files
with
536 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import {JsonDecoder} from './JsonDecoder'; | ||
import {findEndingQuote} from './util'; | ||
import type {PackValue} from '../types'; | ||
import {createFromBase64Bin} from '../../util/base64/createFromBase64Bin'; | ||
|
||
export const fromBase64Bin = createFromBase64Bin(undefined, ''); | ||
|
||
export class JsonDecoderDag extends JsonDecoder { | ||
public readObj(): PackValue | Record<string, PackValue> | Uint8Array | unknown { | ||
const bytes = this.tryReadBytes(); | ||
if (bytes) return bytes; | ||
const cid = this.tryReadCid(); | ||
if (cid) return cid; | ||
return super.readObj(); | ||
} | ||
|
||
protected tryReadBytes(): Uint8Array | undefined { | ||
const reader = this.reader; | ||
const x = reader.x; | ||
if (reader.u8() !== 0x7b) { | ||
// { | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x22 || reader.u8() !== 0x2f || reader.u8() !== 0x22) { | ||
// "/" | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x3a) { | ||
// : | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x7b) { | ||
// { | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if ( | ||
reader.u8() !== 0x22 || | ||
reader.u8() !== 0x62 || | ||
reader.u8() !== 0x79 || | ||
reader.u8() !== 0x74 || | ||
reader.u8() !== 0x65 || | ||
reader.u8() !== 0x73 || | ||
reader.u8() !== 0x22 | ||
) { | ||
// "bytes" | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x3a) { | ||
// : | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x22) { | ||
// " | ||
reader.x = x; | ||
return; | ||
} | ||
const bufStart = reader.x; | ||
const bufEnd = findEndingQuote(reader.uint8, bufStart); | ||
reader.x = 1 + bufEnd; | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x7d) { | ||
// } | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x7d) { | ||
// } | ||
reader.x = x; | ||
return; | ||
} | ||
const bin = fromBase64Bin(reader.view, bufStart, bufEnd - bufStart); | ||
return bin; | ||
} | ||
|
||
protected tryReadCid(): undefined | unknown { | ||
const reader = this.reader; | ||
const x = reader.x; | ||
if (reader.u8() !== 0x7b) { | ||
// { | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x22 || reader.u8() !== 0x2f || reader.u8() !== 0x22) { | ||
// "/" | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x3a) { | ||
// : | ||
reader.x = x; | ||
return; | ||
} | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x22) { | ||
// " | ||
reader.x = x; | ||
return; | ||
} | ||
const bufStart = reader.x; | ||
const bufEnd = findEndingQuote(reader.uint8, bufStart); | ||
reader.x = 1 + bufEnd; | ||
this.skipWhitespace(); | ||
if (reader.u8() !== 0x7d) { | ||
// } | ||
reader.x = x; | ||
return; | ||
} | ||
const finalX = reader.x; | ||
reader.x = bufStart; | ||
const cid = reader.ascii(bufEnd - bufStart); | ||
reader.x = finalX; | ||
return this.readCid(cid); | ||
} | ||
|
||
public readCid(cid: string): unknown { | ||
return cid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {JsonEncoderStable} from './JsonEncoderStable'; | ||
import {createToBase64Bin} from '../../util/base64/createToBase64Bin'; | ||
|
||
const objBaseLength = '{"/":{"bytes":""}}'.length; | ||
const cidBaseLength = '{"/":""}'.length; | ||
const base64Encode = createToBase64Bin(undefined, ''); | ||
|
||
/** | ||
* Base class for implementing DAG-JSON encoders. | ||
* | ||
* @see https://ipld.io/specs/codecs/dag-json/spec/ | ||
*/ | ||
export class JsonEncoderDag extends JsonEncoderStable { | ||
/** | ||
* Encodes binary data as nested `["/", "bytes"]` object encoded in Base64 | ||
* without padding. | ||
* | ||
* Example: | ||
* | ||
* ```json | ||
* {"/":{"bytes":"aGVsbG8gd29ybGQ"}} | ||
* ``` | ||
* | ||
* @param buf Binary data to write. | ||
*/ | ||
public writeBin(buf: Uint8Array): void { | ||
const writer = this.writer; | ||
const length = buf.length; | ||
writer.ensureCapacity(objBaseLength + (length << 1)); | ||
const view = writer.view; | ||
const uint8 = writer.uint8; | ||
let x = writer.x; | ||
view.setUint32(x, 0x7b222f22); // {"/" | ||
x += 4; | ||
view.setUint32(x, 0x3a7b2262); // :{"b | ||
x += 4; | ||
view.setUint32(x, 0x79746573); // ytes | ||
x += 4; | ||
view.setUint16(x, 0x223a); // ": | ||
x += 2; | ||
uint8[x] = 0x22; // " | ||
x += 1; | ||
x = base64Encode(buf, 0, length, view, x); | ||
view.setUint16(x, 0x227d); // "} | ||
x += 2; | ||
uint8[x] = 0x7d; // } | ||
x += 1; | ||
writer.x = x; | ||
} | ||
|
||
public writeCid(cid: string): void { | ||
const writer = this.writer; | ||
writer.ensureCapacity(cidBaseLength + cid.length); | ||
writer.u32(0x7b222f22); // {"/" | ||
writer.u16(0x3a22); // :" | ||
writer.ascii(cid); | ||
writer.u16(0x227d); // "} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {JsonEncoder} from './JsonEncoder'; | ||
import {sort} from '../../util/sort/insertion2'; | ||
import {objKeyCmp} from '../util/objKeyCmp'; | ||
|
||
export class JsonEncoderStable extends JsonEncoder { | ||
public writeObj(obj: Record<string, unknown>): void { | ||
const writer = this.writer; | ||
const keys = Object.keys(obj); | ||
sort(keys, objKeyCmp); | ||
const length = keys.length; | ||
if (!length) return writer.u16(0x7b7d); // {} | ||
writer.u8(0x7b); // { | ||
for (let i = 0; i < length; i++) { | ||
const key = keys[i]; | ||
const value = obj[key]; | ||
this.writeStr(key); | ||
writer.u8(0x3a); // : | ||
this.writeAny(value); | ||
writer.u8(0x2c); // , | ||
} | ||
writer.uint8[writer.x - 1] = 0x7d; // } | ||
} | ||
} |
Oops, something went wrong.