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 | 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | import {JsonEncoderStable} from './JsonEncoderStable';
import {createToBase64Bin} from '@jsonjoy.com/base64/lib/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); // "}
}
}
|