123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.CborEncoderFast = void 0;
- const Writer_1 = require("@jsonjoy.com/util/lib/buffers/Writer");
- const isSafeInteger = Number.isSafeInteger;
- class CborEncoderFast {
- constructor(writer = new Writer_1.Writer()) {
- this.writer = writer;
- }
- encode(value) {
- this.writeAny(value);
- return this.writer.flush();
- }
- encodeToSlice(value) {
- this.writeAny(value);
- return this.writer.flushSlice();
- }
- writeAny(value) {
- switch (typeof value) {
- case 'number':
- return this.writeNumber(value);
- case 'string':
- return this.writeStr(value);
- case 'boolean':
- return this.writer.u8(0xf4 + +value);
- case 'object': {
- if (!value)
- return this.writer.u8(0xf6);
- const constructor = value.constructor;
- switch (constructor) {
- case Array:
- return this.writeArr(value);
- default:
- return this.writeObj(value);
- }
- }
- }
- }
- writeCbor() {
- this.writer.u8u16(0xd9, 0xd9f7);
- }
- writeEnd() {
- this.writer.u8(255);
- }
- writeNull() {
- this.writer.u8(0xf6);
- }
- writeBoolean(bool) {
- if (bool)
- this.writer.u8(0xf5);
- else
- this.writer.u8(0xf4);
- }
- writeNumber(num) {
- if (isSafeInteger(num))
- this.writeInteger(num);
- else if (typeof num === 'bigint')
- this.writeBigInt(num);
- else
- this.writeFloat(num);
- }
- writeBigInt(int) {
- if (int >= 0)
- this.writeBigUint(int);
- else
- this.writeBigSint(int);
- }
- writeBigUint(uint) {
- if (uint <= Number.MAX_SAFE_INTEGER)
- return this.writeUInteger(Number(uint));
- this.writer.u8u64(0x1b, uint);
- }
- writeBigSint(int) {
- if (int >= Number.MIN_SAFE_INTEGER)
- return this.encodeNint(Number(int));
- const uint = -BigInt(1) - int;
- this.writer.u8u64(0x3b, uint);
- }
- writeInteger(int) {
- if (int >= 0)
- this.writeUInteger(int);
- else
- this.encodeNint(int);
- }
- writeUInteger(uint) {
- const writer = this.writer;
- writer.ensureCapacity(9);
- const uint8 = writer.uint8;
- let x = writer.x;
- if (uint <= 23) {
- uint8[x++] = 0 + uint;
- }
- else if (uint <= 0xff) {
- uint8[x++] = 0x18;
- uint8[x++] = uint;
- }
- else if (uint <= 0xffff) {
- uint8[x++] = 0x19;
- writer.view.setUint16(x, uint);
- x += 2;
- }
- else if (uint <= 0xffffffff) {
- uint8[x++] = 0x1a;
- writer.view.setUint32(x, uint);
- x += 4;
- }
- else {
- uint8[x++] = 0x1b;
- writer.view.setBigUint64(x, BigInt(uint));
- x += 8;
- }
- writer.x = x;
- }
- encodeNumber(num) {
- this.writeNumber(num);
- }
- encodeInteger(int) {
- this.writeInteger(int);
- }
- encodeUint(uint) {
- this.writeUInteger(uint);
- }
- encodeNint(int) {
- const uint = -1 - int;
- const writer = this.writer;
- writer.ensureCapacity(9);
- const uint8 = writer.uint8;
- let x = writer.x;
- if (uint < 24) {
- uint8[x++] = 32 + uint;
- }
- else if (uint <= 0xff) {
- uint8[x++] = 0x38;
- uint8[x++] = uint;
- }
- else if (uint <= 0xffff) {
- uint8[x++] = 0x39;
- writer.view.setUint16(x, uint);
- x += 2;
- }
- else if (uint <= 0xffffffff) {
- uint8[x++] = 0x3a;
- writer.view.setUint32(x, uint);
- x += 4;
- }
- else {
- uint8[x++] = 0x3b;
- writer.view.setBigUint64(x, BigInt(uint));
- x += 8;
- }
- writer.x = x;
- }
- writeFloat(float) {
- this.writer.u8f64(0xfb, float);
- }
- writeBin(buf) {
- const length = buf.length;
- this.writeBinHdr(length);
- this.writer.buf(buf, length);
- }
- writeBinHdr(length) {
- const writer = this.writer;
- if (length <= 23)
- writer.u8(64 + length);
- else if (length <= 0xff)
- writer.u16((0x58 << 8) + length);
- else if (length <= 0xffff)
- writer.u8u16(0x59, length);
- else if (length <= 0xffffffff)
- writer.u8u32(0x5a, length);
- else
- writer.u8u64(0x5b, length);
- }
- writeStr(str) {
- const writer = this.writer;
- const length = str.length;
- const maxSize = length * 4;
- writer.ensureCapacity(5 + maxSize);
- const uint8 = writer.uint8;
- let lengthOffset = writer.x;
- if (maxSize <= 23)
- writer.x++;
- else if (maxSize <= 0xff) {
- uint8[writer.x++] = 0x78;
- lengthOffset = writer.x;
- writer.x++;
- }
- else if (maxSize <= 0xffff) {
- uint8[writer.x++] = 0x79;
- lengthOffset = writer.x;
- writer.x += 2;
- }
- else {
- uint8[writer.x++] = 0x7a;
- lengthOffset = writer.x;
- writer.x += 4;
- }
- const bytesWritten = writer.utf8(str);
- if (maxSize <= 23)
- uint8[lengthOffset] = 96 + bytesWritten;
- else if (maxSize <= 0xff)
- uint8[lengthOffset] = bytesWritten;
- else if (maxSize <= 0xffff)
- writer.view.setUint16(lengthOffset, bytesWritten);
- else
- writer.view.setUint32(lengthOffset, bytesWritten);
- }
- writeStrHdr(length) {
- const writer = this.writer;
- if (length <= 23)
- writer.u8(96 + length);
- else if (length <= 0xff)
- writer.u16((0x78 << 8) + length);
- else if (length <= 0xffff)
- writer.u8u16(0x79, length);
- else
- writer.u8u32(0x7a, length);
- }
- writeAsciiStr(str) {
- this.writeStrHdr(str.length);
- this.writer.ascii(str);
- }
- writeArr(arr) {
- const length = arr.length;
- this.writeArrHdr(length);
- for (let i = 0; i < length; i++)
- this.writeAny(arr[i]);
- }
- writeArrHdr(length) {
- const writer = this.writer;
- if (length <= 23)
- writer.u8(128 + length);
- else if (length <= 0xff)
- writer.u16((0x98 << 8) + length);
- else if (length <= 0xffff)
- writer.u8u16(0x99, length);
- else if (length <= 0xffffffff)
- writer.u8u32(0x9a, length);
- else
- writer.u8u64(0x9b, length);
- }
- writeObj(obj) {
- const keys = Object.keys(obj);
- const length = keys.length;
- this.writeObjHdr(length);
- for (let i = 0; i < length; i++) {
- const key = keys[i];
- this.writeStr(key);
- this.writeAny(obj[key]);
- }
- }
- writeObjHdr(length) {
- const writer = this.writer;
- if (length <= 23)
- writer.u8(160 + length);
- else if (length <= 0xff)
- writer.u16((0xb8 << 8) + length);
- else if (length <= 0xffff)
- writer.u8u16(0xb9, length);
- else if (length <= 0xffffffff)
- writer.u8u32(0xba, length);
- else
- writer.u8u64(0xbb, length);
- }
- writeMapHdr(length) {
- this.writeObjHdr(length);
- }
- writeStartMap() {
- this.writer.u8(0xbf);
- }
- writeTag(tag, value) {
- this.writeTagHdr(tag);
- this.writeAny(value);
- }
- writeTagHdr(tag) {
- const writer = this.writer;
- if (tag <= 23)
- writer.u8(192 + tag);
- else if (tag <= 0xff)
- writer.u16((0xd8 << 8) + tag);
- else if (tag <= 0xffff)
- writer.u8u16(0xd9, tag);
- else if (tag <= 0xffffffff)
- writer.u8u32(0xda, tag);
- else
- writer.u8u64(0xdb, tag);
- }
- writeTkn(value) {
- const writer = this.writer;
- if (value <= 23)
- writer.u8(224 + value);
- else if (value <= 0xff)
- writer.u16((0xf8 << 8) + value);
- }
- writeStartStr() {
- this.writer.u8(0x7f);
- }
- writeStrChunk(str) {
- throw new Error('Not implemented');
- }
- writeEndStr() {
- throw new Error('Not implemented');
- }
- writeStartBin() {
- this.writer.u8(0x5f);
- }
- writeBinChunk(buf) {
- throw new Error('Not implemented');
- }
- writeEndBin() {
- throw new Error('Not implemented');
- }
- writeStartArr() {
- this.writer.u8(0x9f);
- }
- writeArrChunk(item) {
- throw new Error('Not implemented');
- }
- writeEndArr() {
- this.writer.u8(255);
- }
- writeStartObj() {
- this.writer.u8(0xbf);
- }
- writeObjChunk(key, value) {
- throw new Error('Not implemented');
- }
- writeEndObj() {
- this.writer.u8(255);
- }
- }
- exports.CborEncoderFast = CborEncoderFast;
- //# sourceMappingURL=CborEncoderFast.js.map
|