@atcute/cbor 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bytes.js CHANGED
@@ -1,11 +1,11 @@
1
- import { decode, encode } from './base64.js';
1
+ import { fromBase64, toBase64 } from '@atcute/multibase';
2
2
  export class BytesWrapper {
3
3
  buf;
4
4
  constructor(buf) {
5
5
  this.buf = buf;
6
6
  }
7
7
  get $bytes() {
8
- return encode(this.buf);
8
+ return toBase64(this.buf);
9
9
  }
10
10
  toJSON() {
11
11
  return { $bytes: this.$bytes };
@@ -18,6 +18,6 @@ export const fromBytes = (bytes) => {
18
18
  if (bytes instanceof BytesWrapper) {
19
19
  return bytes.buf;
20
20
  }
21
- return decode(bytes.$bytes);
21
+ return fromBase64(bytes.$bytes);
22
22
  };
23
23
  //# sourceMappingURL=bytes.js.map
package/dist/bytes.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../lib/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAM7C,MAAM,OAAO,YAAY;IACL;IAAnB,YAAmB,GAAe;QAAf,QAAG,GAAH,GAAG,CAAY;IAAG,CAAC;IAEtC,IAAI,MAAM;QACT,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IAED,MAAM;QACL,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC;CACD;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAAe,EAAS,EAAE;IACjD,OAAO,IAAI,YAAY,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAY,EAAc,EAAE;IACrD,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC,CAAC"}
1
+ {"version":3,"file":"bytes.js","sourceRoot":"","sources":["../lib/bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAMzD,MAAM,OAAO,YAAY;IACL;IAAnB,YAAmB,GAAe;QAAf,QAAG,GAAH,GAAG,CAAY;IAAG,CAAC;IAEtC,IAAI,MAAM;QACT,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,MAAM;QACL,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC;IAChC,CAAC;CACD;AAED,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAAe,EAAS,EAAE;IACjD,OAAO,IAAI,YAAY,CAAC,GAAG,CAAC,CAAC;AAC9B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAY,EAAc,EAAE;IACrD,IAAI,KAAK,YAAY,YAAY,EAAE,CAAC;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,OAAO,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC,CAAC"}
package/dist/cid-link.js CHANGED
@@ -1,5 +1,5 @@
1
- import * as base32 from '@atcute/base32';
2
1
  import { decode, parse } from '@atcute/cid';
2
+ import { toBase32 } from '@atcute/multibase';
3
3
  export class CIDLinkWrapper {
4
4
  $bytes;
5
5
  constructor($bytes) {
@@ -9,7 +9,7 @@ export class CIDLinkWrapper {
9
9
  return decode(this.$bytes);
10
10
  }
11
11
  get $link() {
12
- return 'b' + base32.encode(this.$bytes);
12
+ return 'b' + toBase32(this.$bytes);
13
13
  }
14
14
  toJSON() {
15
15
  return { $link: this.$link };
@@ -1 +1 @@
1
- {"version":3,"file":"cid-link.js","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAY,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAMtD,MAAM,OAAO,cAAc;IACP;IAAnB,YAAmB,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAEzC,IAAI,IAAI;QACP,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IAED,IAAI,KAAK;QACR,OAAO,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACzC,CAAC;IAED,MAAM;QACL,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;CACD;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAuB,EAAW,EAAE;IAC7D,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;QACjC,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAa,EAAO,EAAE;IACjD,IAAI,IAAI,YAAY,cAAc,EAAE,CAAC;QACpC,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC,CAAC"}
1
+ {"version":3,"file":"cid-link.js","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAM7C,MAAM,OAAO,cAAc;IACP;IAAnB,YAAmB,MAAkB;QAAlB,WAAM,GAAN,MAAM,CAAY;IAAG,CAAC;IAEzC,IAAI,IAAI;QACP,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5B,CAAC;IAED,IAAI,KAAK;QACR,OAAO,GAAG,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;IAED,MAAM;QACL,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;CACD;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,KAAuB,EAAW,EAAE;IAC7D,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;QACjC,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAED,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACxC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,IAAa,EAAO,EAAE;IACjD,IAAI,IAAI,YAAY,cAAc,EAAE,CAAC;QACpC,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC1B,CAAC,CAAC"}
package/lib/bytes.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { fromBase64, toBase64 } from '@atcute/multibase';
2
+
3
+ export interface Bytes {
4
+ $bytes: string;
5
+ }
6
+
7
+ export class BytesWrapper implements Bytes {
8
+ constructor(public buf: Uint8Array) {}
9
+
10
+ get $bytes(): string {
11
+ return toBase64(this.buf);
12
+ }
13
+
14
+ toJSON(): Bytes {
15
+ return { $bytes: this.$bytes };
16
+ }
17
+ }
18
+
19
+ export const toBytes = (buf: Uint8Array): Bytes => {
20
+ return new BytesWrapper(buf);
21
+ };
22
+
23
+ export const fromBytes = (bytes: Bytes): Uint8Array => {
24
+ if (bytes instanceof BytesWrapper) {
25
+ return bytes.buf;
26
+ }
27
+
28
+ return fromBase64(bytes.$bytes);
29
+ };
@@ -0,0 +1,38 @@
1
+ import { type CID, decode, parse } from '@atcute/cid';
2
+ import { toBase32 } from '@atcute/multibase';
3
+
4
+ export interface CIDLink {
5
+ $link: string;
6
+ }
7
+
8
+ export class CIDLinkWrapper implements CIDLink {
9
+ constructor(public $bytes: Uint8Array) {}
10
+
11
+ get $cid(): CID {
12
+ return decode(this.$bytes);
13
+ }
14
+
15
+ get $link(): string {
16
+ return 'b' + toBase32(this.$bytes);
17
+ }
18
+
19
+ toJSON(): CIDLink {
20
+ return { $link: this.$link };
21
+ }
22
+ }
23
+
24
+ export const toCIDLink = (value: CID | Uint8Array): CIDLink => {
25
+ if (value instanceof Uint8Array) {
26
+ return new CIDLinkWrapper(value);
27
+ }
28
+
29
+ return new CIDLinkWrapper(value.bytes);
30
+ };
31
+
32
+ export const fromCIDLink = (link: CIDLink): CID => {
33
+ if (link instanceof CIDLinkWrapper) {
34
+ return link.$cid;
35
+ }
36
+
37
+ return parse(link.$link);
38
+ };
package/lib/decode.ts ADDED
@@ -0,0 +1,206 @@
1
+ import { toBytes, type Bytes } from './bytes.js';
2
+ import { toCIDLink, type CIDLink } from './cid-link.js';
3
+
4
+ const utf8d = new TextDecoder();
5
+
6
+ interface State {
7
+ b: Uint8Array;
8
+ v: DataView;
9
+ p: number;
10
+ }
11
+
12
+ const readArgument = (state: State, info: number): number => {
13
+ if (info < 24) {
14
+ return info;
15
+ }
16
+
17
+ switch (info) {
18
+ case 24:
19
+ return readUint8(state);
20
+ case 25:
21
+ return readUint16(state);
22
+ case 26:
23
+ return readUint32(state);
24
+ case 27:
25
+ return readUint64(state);
26
+ }
27
+
28
+ throw new Error(`invalid argument encoding; got ${info}`);
29
+ };
30
+
31
+ const readFloat64 = (state: State): number => {
32
+ const value = state.v.getFloat64(state.p);
33
+
34
+ state.p += 8;
35
+ return value;
36
+ };
37
+
38
+ const readUint8 = (state: State): number => {
39
+ const value = state.v.getUint8(state.p);
40
+
41
+ state.p += 1;
42
+ return value;
43
+ };
44
+
45
+ const readUint16 = (state: State): number => {
46
+ const value = state.v.getUint16(state.p);
47
+
48
+ state.p += 2;
49
+ return value;
50
+ };
51
+
52
+ const readUint32 = (state: State): number => {
53
+ const value = state.v.getUint32(state.p);
54
+
55
+ state.p += 4;
56
+ return value;
57
+ };
58
+
59
+ const readUint64 = (state: State): number => {
60
+ const hi = state.v.getUint32(state.p);
61
+ const lo = state.v.getUint32(state.p + 4);
62
+
63
+ if (hi > 0x1fffff) {
64
+ throw new RangeError(`can't decode integers beyond safe integer range`);
65
+ }
66
+
67
+ // prettier-ignore
68
+ const value = (hi * (2 ** 32)) + lo;
69
+
70
+ state.p += 8;
71
+ return value;
72
+ };
73
+
74
+ const readString = (state: State, length: number): string => {
75
+ const slice = state.b.subarray(state.p, (state.p += length));
76
+
77
+ return utf8d.decode(slice);
78
+ };
79
+
80
+ const readBytes = (state: State, length: number): Bytes => {
81
+ const slice = state.b.subarray(state.p, (state.p += length));
82
+
83
+ return toBytes(slice);
84
+ };
85
+
86
+ const readCid = (state: State, length: number): CIDLink => {
87
+ // CID bytes are prefixed with 0x00 for historical reasons, apparently.
88
+ const slice = state.b.subarray(state.p + 1, (state.p += length));
89
+
90
+ return toCIDLink(slice);
91
+ };
92
+
93
+ const readValue = (state: State): any => {
94
+ const prelude = readUint8(state);
95
+
96
+ const type = prelude >> 5;
97
+ const info = prelude & 0x1f;
98
+
99
+ if (type === 0) {
100
+ const value = readArgument(state, info);
101
+ return value;
102
+ }
103
+
104
+ if (type === 1) {
105
+ const value = readArgument(state, info);
106
+ return -1 - value;
107
+ }
108
+
109
+ if (type === 2) {
110
+ const len = readArgument(state, info);
111
+ return readBytes(state, len);
112
+ }
113
+
114
+ if (type === 3) {
115
+ const len = readArgument(state, info);
116
+
117
+ return readString(state, len);
118
+ }
119
+
120
+ if (type === 4) {
121
+ const len = readArgument(state, info);
122
+ const array = new Array(len);
123
+
124
+ for (let idx = 0; idx < len; idx++) {
125
+ array[idx] = readValue(state);
126
+ }
127
+
128
+ return array;
129
+ }
130
+
131
+ if (type === 5) {
132
+ const len = readArgument(state, info);
133
+ const object: Record<string, unknown> = {};
134
+
135
+ for (let idx = 0; idx < len; idx++) {
136
+ const key = readValue(state);
137
+ if (typeof key !== 'string') {
138
+ throw new TypeError(`expected map to only have string keys; got ${typeof key}`);
139
+ }
140
+
141
+ object[key] = readValue(state);
142
+ }
143
+
144
+ return object;
145
+ }
146
+
147
+ if (type === 6) {
148
+ const tag = readArgument(state, info);
149
+
150
+ if (tag === 42) {
151
+ const prelude = readUint8(state);
152
+
153
+ const type = prelude >> 5;
154
+ const info = prelude & 0x1f;
155
+
156
+ if (type !== 2) {
157
+ throw new TypeError(`expected cid tag to have bytes value; got ${type}`);
158
+ }
159
+
160
+ const len = readArgument(state, info);
161
+
162
+ return readCid(state, len);
163
+ }
164
+
165
+ throw new TypeError(`unsupported tag; got ${tag}`);
166
+ }
167
+
168
+ if (type === 7) {
169
+ switch (info) {
170
+ case 20:
171
+ return false;
172
+ case 21:
173
+ return true;
174
+ case 22:
175
+ return null;
176
+ case 27:
177
+ return readFloat64(state);
178
+ }
179
+
180
+ throw new Error(`invalid simple value; got ${info}`);
181
+ }
182
+
183
+ throw new TypeError(`invalid type; got ${type}`);
184
+ };
185
+
186
+ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array] => {
187
+ const state: State = {
188
+ b: buf,
189
+ v: new DataView(buf.buffer, buf.byteOffset, buf.byteLength),
190
+ p: 0,
191
+ };
192
+
193
+ const value = readValue(state);
194
+ const remainder = buf.subarray(state.p);
195
+
196
+ return [value, remainder];
197
+ };
198
+
199
+ export const decode = (buf: Uint8Array): any => {
200
+ const [value, remainder] = decodeFirst(buf);
201
+ if (remainder.length !== 0) {
202
+ throw new Error(`decoded value contains remainder`);
203
+ }
204
+
205
+ return value;
206
+ };
package/lib/encode.ts ADDED
@@ -0,0 +1,304 @@
1
+ import { BytesWrapper, fromBytes, type Bytes } from './bytes.js';
2
+ import { CIDLinkWrapper, fromCIDLink, type CIDLink } from './cid-link.js';
3
+
4
+ const CHUNK_SIZE = 1024;
5
+
6
+ const utf8e = new TextEncoder();
7
+
8
+ interface State {
9
+ c: Uint8Array[];
10
+ b: ArrayBuffer;
11
+ v: DataView;
12
+ p: number;
13
+ }
14
+
15
+ const resizeIfNeeded = (state: State, needed: number): void => {
16
+ const buf = state.b;
17
+ const pos = state.p;
18
+
19
+ if (buf.byteLength < pos + needed) {
20
+ state.c.push(new Uint8Array(buf, 0, pos));
21
+
22
+ state.b = new ArrayBuffer(Math.max(CHUNK_SIZE, needed));
23
+ state.v = new DataView(state.b);
24
+ state.p = 0;
25
+ }
26
+ };
27
+
28
+ const getInfo = (arg: number): number => {
29
+ if (arg < 24) {
30
+ return arg;
31
+ } else if (arg < 0x100) {
32
+ return 24;
33
+ } else if (arg < 0x10000) {
34
+ return 25;
35
+ } else if (arg < 0x100000000) {
36
+ return 26;
37
+ } else {
38
+ return 27;
39
+ }
40
+ };
41
+
42
+ const writeFloat64 = (state: State, val: number): void => {
43
+ resizeIfNeeded(state, 8);
44
+
45
+ state.v.setFloat64(state.p, val);
46
+ state.p += 8;
47
+ };
48
+
49
+ const writeUint8 = (state: State, val: number): void => {
50
+ resizeIfNeeded(state, 1);
51
+
52
+ state.v.setUint8(state.p, val);
53
+ state.p += 1;
54
+ };
55
+
56
+ const writeUint16 = (state: State, val: number): void => {
57
+ resizeIfNeeded(state, 2);
58
+
59
+ state.v.setUint16(state.p, val);
60
+ state.p += 2;
61
+ };
62
+
63
+ const writeUint32 = (state: State, val: number): void => {
64
+ resizeIfNeeded(state, 4);
65
+
66
+ state.v.setUint32(state.p, val);
67
+ state.p += 4;
68
+ };
69
+
70
+ const writeUint64 = (state: State, val: number): void => {
71
+ const hi = (val / 2 ** 32) | 0;
72
+ const lo = val >>> 0;
73
+
74
+ resizeIfNeeded(state, 8);
75
+
76
+ state.v.setUint32(state.p, hi);
77
+ state.v.setUint32(state.p + 4, lo);
78
+ state.p += 8;
79
+ };
80
+
81
+ const writeTypeAndArgument = (state: State, type: number, arg: number): void => {
82
+ const info = getInfo(arg);
83
+
84
+ writeUint8(state, (type << 5) | info);
85
+
86
+ switch (info) {
87
+ case 24:
88
+ return writeUint8(state, arg);
89
+ case 25:
90
+ return writeUint16(state, arg);
91
+ case 26:
92
+ return writeUint32(state, arg);
93
+ case 27:
94
+ return writeUint64(state, arg);
95
+ }
96
+ };
97
+
98
+ const writeInteger = (state: State, val: number): void => {
99
+ if (val < 0) {
100
+ writeTypeAndArgument(state, 1, -val - 1);
101
+ } else {
102
+ writeTypeAndArgument(state, 0, val);
103
+ }
104
+ };
105
+
106
+ const writeFloat = (state: State, val: number): void => {
107
+ writeUint8(state, 0xe0 | 27);
108
+ writeFloat64(state, val);
109
+ };
110
+
111
+ const writeNumber = (state: State, val: number): void => {
112
+ if (Number.isNaN(val)) {
113
+ throw new RangeError(`NaN values not supported`);
114
+ }
115
+
116
+ if (val > Number.MAX_SAFE_INTEGER || val < Number.MIN_SAFE_INTEGER) {
117
+ throw new RangeError(`can't encode numbers beyond safe integer range`);
118
+ }
119
+
120
+ if (Number.isInteger(val)) {
121
+ writeInteger(state, val);
122
+ } else {
123
+ writeFloat(state, val);
124
+ }
125
+ };
126
+
127
+ const writeString = (state: State, val: string): void => {
128
+ const buf = utf8e.encode(val);
129
+ const len = buf.byteLength;
130
+
131
+ writeTypeAndArgument(state, 3, len);
132
+ resizeIfNeeded(state, len);
133
+
134
+ new Uint8Array(state.b, state.p).set(buf);
135
+
136
+ state.p += len;
137
+ };
138
+
139
+ const writeBytes = (state: State, val: Bytes): void => {
140
+ const buf = fromBytes(val);
141
+ const len = buf.byteLength;
142
+
143
+ writeTypeAndArgument(state, 2, len);
144
+ resizeIfNeeded(state, len);
145
+
146
+ new Uint8Array(state.b, state.p, len).set(buf);
147
+
148
+ state.p += len;
149
+ };
150
+
151
+ const writeCid = (state: State, val: CIDLink): void => {
152
+ // CID bytes are prefixed with 0x00 for historical reasons, apparently.
153
+ const buf = fromCIDLink(val).bytes;
154
+ const len = buf.byteLength + 1;
155
+
156
+ writeTypeAndArgument(state, 6, 42);
157
+ writeTypeAndArgument(state, 2, len);
158
+
159
+ resizeIfNeeded(state, len);
160
+
161
+ new Uint8Array(state.b, state.p + 1, len - 1).set(buf);
162
+
163
+ state.p += len;
164
+ };
165
+
166
+ const writeValue = (state: State, val: any): void => {
167
+ if (val === undefined) {
168
+ throw new TypeError(`undefined values not supported`);
169
+ }
170
+
171
+ if (val === null) {
172
+ return writeUint8(state, 0xf6);
173
+ }
174
+
175
+ if (val === false) {
176
+ return writeUint8(state, 0xf4);
177
+ }
178
+
179
+ if (val === true) {
180
+ return writeUint8(state, 0xf5);
181
+ }
182
+
183
+ if (typeof val === 'number') {
184
+ return writeNumber(state, val);
185
+ }
186
+
187
+ if (typeof val === 'string') {
188
+ return writeString(state, val);
189
+ }
190
+
191
+ if (typeof val === 'object') {
192
+ if (isArray(val)) {
193
+ const len = val.length;
194
+
195
+ writeTypeAndArgument(state, 4, len);
196
+
197
+ for (let idx = 0; idx < len; idx++) {
198
+ const v = val[idx];
199
+ writeValue(state, v);
200
+ }
201
+
202
+ return;
203
+ }
204
+
205
+ if ('$link' in val) {
206
+ if (val instanceof CIDLinkWrapper || typeof val.$link === 'string') {
207
+ writeCid(state, val);
208
+ return;
209
+ }
210
+
211
+ throw new TypeError(`unexpected cid-link value`);
212
+ }
213
+
214
+ if ('$bytes' in val) {
215
+ if (val instanceof BytesWrapper || typeof val.$bytes === 'string') {
216
+ writeBytes(state, val);
217
+ return;
218
+ }
219
+
220
+ throw new TypeError(`unexpected bytes value`);
221
+ }
222
+
223
+ if (isPlainObject(val)) {
224
+ const keys = Object.keys(val)
225
+ .filter((key) => typeof key === 'string' && val[key] !== undefined)
226
+ .sort(compareKeys);
227
+
228
+ const len = keys.length;
229
+
230
+ writeTypeAndArgument(state, 5, len);
231
+
232
+ for (let idx = 0; idx < len; idx++) {
233
+ const key = keys[idx];
234
+
235
+ writeString(state, key);
236
+ writeValue(state, val[key]);
237
+ }
238
+
239
+ return;
240
+ }
241
+ }
242
+
243
+ throw new TypeError(`unsupported type: ${val}`);
244
+ };
245
+
246
+ const createState = (): State => {
247
+ const buf = new ArrayBuffer(CHUNK_SIZE);
248
+
249
+ return {
250
+ c: [],
251
+ b: buf,
252
+ v: new DataView(buf),
253
+ p: 0,
254
+ };
255
+ };
256
+
257
+ export const encode = (value: any): Uint8Array => {
258
+ const state = createState();
259
+ const chunks = state.c;
260
+
261
+ writeValue(state, value);
262
+ chunks.push(new Uint8Array(state.b, 0, state.p));
263
+
264
+ let size = 0;
265
+ let written = 0;
266
+
267
+ let len = chunks.length;
268
+ let idx: number;
269
+
270
+ for (idx = 0; idx < len; idx++) {
271
+ size += chunks[idx].byteLength;
272
+ }
273
+
274
+ const u8 = new Uint8Array(size);
275
+
276
+ for (idx = 0; idx < len; idx++) {
277
+ const chunk = chunks[idx];
278
+
279
+ u8.set(chunk, written);
280
+ written += chunk.byteLength;
281
+ }
282
+
283
+ return u8;
284
+ };
285
+
286
+ const isArray = Array.isArray;
287
+ const isPlainObject = (v: any): boolean => {
288
+ if (typeof v !== 'object' || v === null) {
289
+ return false;
290
+ }
291
+
292
+ const proto = Object.getPrototypeOf(v);
293
+ return proto === Object.prototype || proto === null;
294
+ };
295
+
296
+ const compareKeys = (a: string, b: string): number => {
297
+ if (a.length < b.length) {
298
+ return -1;
299
+ } else if (b.length < a.length) {
300
+ return 1;
301
+ } else {
302
+ return a < b ? -1 : 1;
303
+ }
304
+ };
package/lib/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { BytesWrapper, fromBytes, toBytes, type Bytes } from './bytes.js';
2
+ export { CIDLinkWrapper, fromCIDLink, toCIDLink, type CIDLink } from './cid-link.js';
3
+
4
+ export { decode, decodeFirst } from './decode.js';
5
+ export { encode } from './encode.js';
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@atcute/cbor",
4
- "version": "1.0.5",
4
+ "version": "1.0.7",
5
5
  "description": "DAG-CBOR codec that deals in AT Protocol's HTTP wire format",
6
6
  "license": "MIT",
7
7
  "repository": {
8
- "url": "https://codeberg.org/mary-ext/atcute"
8
+ "url": "https://github.com/mary-ext/atcute",
9
+ "directory": "packages/utilities/cbor"
9
10
  },
10
11
  "files": [
11
- "dist/"
12
+ "dist/",
13
+ "lib/",
14
+ "!lib/**/*.bench.ts",
15
+ "!lib/**/*.test.ts"
12
16
  ],
13
17
  "exports": {
14
18
  ".": "./dist/index.js"
@@ -16,12 +20,12 @@
16
20
  "sideEffects": false,
17
21
  "devDependencies": {
18
22
  "@ipld/dag-cbor": "^9.2.1",
19
- "@types/bun": "^1.1.11",
23
+ "@types/bun": "^1.1.12",
20
24
  "mitata": "^1.0.10"
21
25
  },
22
26
  "dependencies": {
23
- "@atcute/base32": "^1.0.0",
24
- "@atcute/cid": "^1.0.1"
27
+ "@atcute/cid": "^1.0.3",
28
+ "@atcute/multibase": "^1.0.0"
25
29
  },
26
30
  "scripts": {
27
31
  "build": "tsc --project tsconfig.build.json",
package/dist/base64.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const decode: (str: string) => Uint8Array;
2
- export declare const encode: (data: Uint8Array) => string;
package/dist/base64.js DELETED
@@ -1,72 +0,0 @@
1
- const BASE64_CHARSET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
2
- const BASE64_BITS = 6;
3
- const BASE64_CODES = /*#__PURE__*/ (() => {
4
- const codes = {};
5
- for (let i = 0, ilen = BASE64_CHARSET.length; i < ilen; i++) {
6
- codes[BASE64_CHARSET[i]] = i;
7
- }
8
- return codes;
9
- })();
10
- export const decode = (str) => {
11
- // Calculate padding
12
- // let end = str.length;
13
- // while (str[end - 1] === '=') {
14
- // --end;
15
- // }
16
- const end = str.length;
17
- // Allocate
18
- const out = new Uint8Array(((end * BASE64_BITS) / 8) | 0);
19
- // Parse
20
- let bits = 0; // Number of bits currently in the buffer
21
- let buffer = 0; // Bits waiting to be written out, MSB first
22
- let written = 0; // Next byte to write
23
- for (let i = 0; i < end; ++i) {
24
- // Read one character from the string:
25
- const value = BASE64_CODES[str[i]];
26
- if (value === undefined) {
27
- throw new SyntaxError(`invalid base64 string`);
28
- }
29
- // Append the bits to the buffer:
30
- buffer = (buffer << BASE64_BITS) | value;
31
- bits += BASE64_BITS;
32
- // Write out some bits if the buffer has a byte's worth:
33
- if (bits >= 8) {
34
- bits -= 8;
35
- out[written++] = 0xff & (buffer >> bits);
36
- }
37
- }
38
- // Verify
39
- if (bits >= BASE64_BITS || (0xff & (buffer << (8 - bits))) !== 0) {
40
- throw new SyntaxError('unexpected end of data');
41
- }
42
- return out;
43
- };
44
- export const encode = (data) => {
45
- // const pad = BASE64_CHARSET[BASE64_CHARSET.length - 1] === '=';
46
- const mask = (1 << BASE64_BITS) - 1;
47
- let out = '';
48
- let bits = 0; // Number of bits currently in the buffer
49
- let buffer = 0; // Bits waiting to be written out, MSB first
50
- for (let i = 0; i < data.length; ++i) {
51
- // Slurp data into the buffer:
52
- buffer = (buffer << 8) | data[i];
53
- bits += 8;
54
- // Write out as much as we can:
55
- while (bits > BASE64_BITS) {
56
- bits -= BASE64_BITS;
57
- out += BASE64_CHARSET[mask & (buffer >> bits)];
58
- }
59
- }
60
- // Partial character:
61
- if (bits !== 0) {
62
- out += BASE64_CHARSET[mask & (buffer << (BASE64_BITS - bits))];
63
- }
64
- // Add padding characters until we hit a byte boundary:
65
- // if (pad) {
66
- // while (((out.length * BASE64_BITS) & 7) !== 0) {
67
- // out += '=';
68
- // }
69
- // }
70
- return out;
71
- };
72
- //# sourceMappingURL=base64.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base64.js","sourceRoot":"","sources":["../lib/base64.ts"],"names":[],"mappings":"AAAA,MAAM,cAAc,GAAG,kEAAkE,CAAC;AAC1F,MAAM,WAAW,GAAG,CAAC,CAAC;AAEtB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