@atproto/common 0.6.4 → 0.6.5

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/src/streams.ts DELETED
@@ -1,239 +0,0 @@
1
- import {
2
- Duplex,
3
- PassThrough,
4
- Readable,
5
- Stream,
6
- Transform,
7
- TransformCallback,
8
- pipeline,
9
- } from 'node:stream'
10
- import { createBrotliDecompress, createGunzip, createInflate } from 'node:zlib'
11
-
12
- export const forwardStreamErrors = (...streams: Stream[]) => {
13
- for (let i = 1; i < streams.length; ++i) {
14
- const prev = streams[i - 1]
15
- const next = streams[i]
16
-
17
- prev.once('error', (err) => next.emit('error', err))
18
- }
19
- }
20
-
21
- export const cloneStream = (stream: Readable): Readable => {
22
- const passthrough = new PassThrough()
23
- forwardStreamErrors(stream, passthrough)
24
- return stream.pipe(passthrough)
25
- }
26
-
27
- export const streamSize = async (stream: Readable): Promise<number> => {
28
- let size = 0
29
- for await (const chunk of stream) {
30
- size += Buffer.byteLength(chunk)
31
- }
32
- return size
33
- }
34
-
35
- export const streamToBytes = async (stream: AsyncIterable<Uint8Array>) =>
36
- // @NOTE Though Buffer is a sub-class of Uint8Array, we have observed
37
- // inconsistencies when using a Buffer in place of Uint8Array. For this
38
- // reason, we convert the Buffer to a Uint8Array.
39
- new Uint8Array(await streamToNodeBuffer(stream))
40
-
41
- // streamToBuffer identifier name already taken by @atproto/common-web
42
- export const streamToNodeBuffer = async (
43
- stream: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,
44
- ): Promise<Buffer> => {
45
- const chunks: Uint8Array[] = []
46
- let totalLength = 0 // keep track of total length for Buffer.concat
47
- for await (const chunk of stream) {
48
- if (chunk instanceof Uint8Array) {
49
- chunks.push(chunk)
50
- totalLength += Buffer.byteLength(chunk)
51
- } else {
52
- throw new TypeError('expected Uint8Array')
53
- }
54
- }
55
- return Buffer.concat(chunks, totalLength)
56
- }
57
-
58
- export const byteIterableToStream = (
59
- iter: AsyncIterable<Uint8Array>,
60
- ): Readable => {
61
- return Readable.from(iter, { objectMode: false })
62
- }
63
-
64
- /**
65
- * Coalesce a stream of Uint8Array chunks into larger chunks of at least the
66
- * specified size ({@link minChunkSize}). This is useful for optimizing
67
- * downstream processing that benefits from larger chunk sizes, such as
68
- * compression or hashing.
69
- */
70
- export const coalesceByteStream = (
71
- stream: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,
72
- minChunkSize: number,
73
- ): Readable => {
74
- if (!Number.isInteger(minChunkSize) || minChunkSize < 1) {
75
- throw new TypeError('minChunkSize must be a positive integer')
76
- }
77
-
78
- // @NOTE On Node 22, this *does* return a PassThrough ("@types/node"
79
- // incorrectly types it as Writable).
80
- return pipeline(stream, coalesce, (_err) => {
81
- // Errors are expected to be handled through the stream
82
- }) as PassThrough
83
-
84
- // @NOTE This implementation is not NodeJS specific and could be exported as
85
- // utility (from "@atproto/common-web") if needed. We don't do it now to avoid
86
- // increasing the API surface of our packages.
87
- async function* coalesce(
88
- iter: Iterable<Uint8Array> | AsyncIterable<Uint8Array>,
89
- ): AsyncGenerator<Uint8Array> {
90
- // @NOTE This implementation avoids as many un-necessary copies as possible
91
- // and only buffers incoming chunks when they are smaller than the
92
- // coalescing buffer.
93
-
94
- let buffer = new Uint8Array(minChunkSize)
95
- let offset = 0
96
-
97
- for await (const chunk of iter) {
98
- const freeSpace = buffer.length - offset
99
- if (freeSpace > chunk.length) {
100
- // If the incoming chunk is smaller than the free space, we copy the
101
- // entire chunk into the coalescing buffer and continue to the next
102
- // chunk
103
- buffer.set(chunk, offset)
104
- offset += chunk.length
105
- } else if (offset === 0 && chunk.length >= minChunkSize) {
106
- // If the coalescing buffer is empty and the incoming chunk is larger
107
- // than the coalescing buffer, we can skip any copying and yield the
108
- // incoming chunk directly
109
- yield chunk
110
- } else {
111
- // Otherwise, we need to copy as much of the incoming chunk as we can
112
- // into the coalescing buffer and yield the full coalescing buffer.
113
- buffer.set(chunk.subarray(0, freeSpace), offset)
114
- yield buffer
115
-
116
- // We create a new coalescing buffer (for future use)
117
- buffer = new Uint8Array(minChunkSize)
118
- offset = 0
119
-
120
- const remainingBytes = chunk.subarray(freeSpace)
121
- if (remainingBytes.length > minChunkSize) {
122
- // If the remaining of chunk is still too big to fit in the
123
- // coalescing buffer, we yield it directly without copying it
124
- yield remainingBytes
125
- } else if (remainingBytes.length > 0) {
126
- // Otherwise, we copy the remaining bytes into the coalescing buffer
127
- // and continue to the next chunk
128
- buffer.set(remainingBytes, offset)
129
- offset += remainingBytes.length
130
- }
131
- }
132
- }
133
-
134
- // Yield any remaining bytes in the coalescing buffer
135
- if (offset > 0) {
136
- yield buffer.subarray(0, offset)
137
- }
138
- }
139
- }
140
-
141
- export const bytesToStream = (bytes: Uint8Array): Readable => {
142
- const stream = new Readable()
143
- stream.push(bytes)
144
- stream.push(null)
145
- return stream
146
- }
147
-
148
- export class MaxSizeChecker extends Transform {
149
- totalSize = 0
150
- constructor(
151
- public maxSize: number,
152
- public createError: () => Error,
153
- ) {
154
- super()
155
- }
156
- _transform(chunk: Uint8Array, _enc: BufferEncoding, cb: TransformCallback) {
157
- this.totalSize += chunk.length
158
- if (this.totalSize > this.maxSize) {
159
- cb(this.createError())
160
- } else {
161
- cb(null, chunk)
162
- }
163
- }
164
- }
165
-
166
- export function decodeStream(
167
- stream: Readable,
168
- contentEncoding?: string | string[],
169
- ): Readable
170
- export function decodeStream(
171
- stream: AsyncIterable<Uint8Array>,
172
- contentEncoding?: string | string[],
173
- ): AsyncIterable<Uint8Array> | Readable
174
- export function decodeStream(
175
- stream: Readable | AsyncIterable<Uint8Array>,
176
- contentEncoding?: string | string[],
177
- ): Readable | AsyncIterable<Uint8Array> {
178
- const decoders = createDecoders(contentEncoding)
179
- if (decoders.length === 0) return stream
180
- return pipeline([stream as Readable, ...decoders], () => {}) as Duplex
181
- }
182
-
183
- /**
184
- * Create a series of decoding streams based on the content-encoding header. The
185
- * resulting streams should be piped together to decode the content.
186
- *
187
- * @see {@link https://datatracker.ietf.org/doc/html/rfc9110#section-8.4.1}
188
- */
189
- export function createDecoders(contentEncoding?: string | string[]): Duplex[] {
190
- const decoders: Duplex[] = []
191
-
192
- if (contentEncoding?.length) {
193
- const encodings: string[] = Array.isArray(contentEncoding)
194
- ? contentEncoding.flatMap(commaSplit)
195
- : contentEncoding.split(',')
196
- for (const encoding of encodings) {
197
- const normalizedEncoding = normalizeEncoding(encoding)
198
-
199
- // @NOTE
200
- // > The default (identity) encoding [...] is used only in the
201
- // > Accept-Encoding header, and SHOULD NOT be used in the
202
- // > Content-Encoding header.
203
- if (normalizedEncoding === 'identity') continue
204
-
205
- decoders.push(createDecoder(normalizedEncoding))
206
- }
207
- }
208
-
209
- return decoders.reverse()
210
- }
211
-
212
- function commaSplit(header: string): string[] {
213
- return header.split(',')
214
- }
215
-
216
- function normalizeEncoding(encoding: string) {
217
- // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1
218
- // > All content-coding values are case-insensitive...
219
- return encoding.trim().toLowerCase()
220
- }
221
-
222
- function createDecoder(normalizedEncoding: string): Duplex {
223
- switch (normalizedEncoding) {
224
- // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2
225
- case 'gzip':
226
- case 'x-gzip':
227
- return createGunzip()
228
- case 'deflate':
229
- return createInflate()
230
- case 'br':
231
- return createBrotliDecompress()
232
- case 'identity':
233
- return new PassThrough()
234
- default:
235
- throw new TypeError(
236
- `Unsupported content-encoding: "${normalizedEncoding}"`,
237
- )
238
- }
239
- }
@@ -1,34 +0,0 @@
1
- import * as ui8 from 'uint8arrays'
2
- import { CID } from '@atproto/lex-data'
3
- import { cborDecodeMulti, cborEncode } from '../src/index.js'
4
-
5
- describe('ipld decode multi', () => {
6
- it('decodes concatenated dag-cbor messages', async () => {
7
- const one = {
8
- a: 123,
9
- b: CID.parse(
10
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
11
- ),
12
- }
13
- const two = {
14
- c: new Uint8Array([1, 2, 3]),
15
- d: CID.parse(
16
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
17
- ),
18
- }
19
- const encoded = ui8.concat([cborEncode(one), cborEncode(two)])
20
- const decoded = cborDecodeMulti(encoded)
21
- expect(decoded.length).toBe(2)
22
- expect(decoded[0]).toEqual(one)
23
- expect(decoded[1]).toEqual(two)
24
- })
25
-
26
- it('parses safe ints as number', async () => {
27
- const one = {
28
- test: Number.MAX_SAFE_INTEGER,
29
- }
30
- const encoded = cborEncode(one)
31
- const decoded = cborDecodeMulti(encoded)
32
- expect(Number.isInteger(decoded[0]?.['test'])).toBe(true)
33
- })
34
- })
@@ -1,277 +0,0 @@
1
- import { parseCid } from '@atproto/lex-data'
2
-
3
- export const vectors = [
4
- {
5
- name: 'basic',
6
- json: {
7
- string: 'abc',
8
- unicode: 'a~öñ©⽘☎𓋓😀👨‍👩‍👧‍👧',
9
- integer: 123,
10
- bool: true,
11
- null: null,
12
- array: ['abc', 'def', 'ghi'],
13
- object: {
14
- string: 'abc',
15
- number: 123,
16
- bool: true,
17
- arr: ['abc', 'def', 'ghi'],
18
- },
19
- },
20
- ipld: {
21
- string: 'abc',
22
- unicode: 'a~öñ©⽘☎𓋓😀👨‍👩‍👧‍👧',
23
- integer: 123,
24
- bool: true,
25
- null: null,
26
- array: ['abc', 'def', 'ghi'],
27
- object: {
28
- string: 'abc',
29
- number: 123,
30
- bool: true,
31
- arr: ['abc', 'def', 'ghi'],
32
- },
33
- },
34
- cbor: new Uint8Array([
35
- 167, 100, 98, 111, 111, 108, 245, 100, 110, 117, 108, 108, 246, 101, 97,
36
- 114, 114, 97, 121, 131, 99, 97, 98, 99, 99, 100, 101, 102, 99, 103, 104,
37
- 105, 102, 111, 98, 106, 101, 99, 116, 164, 99, 97, 114, 114, 131, 99, 97,
38
- 98, 99, 99, 100, 101, 102, 99, 103, 104, 105, 100, 98, 111, 111, 108, 245,
39
- 102, 110, 117, 109, 98, 101, 114, 24, 123, 102, 115, 116, 114, 105, 110,
40
- 103, 99, 97, 98, 99, 102, 115, 116, 114, 105, 110, 103, 99, 97, 98, 99,
41
- 103, 105, 110, 116, 101, 103, 101, 114, 24, 123, 103, 117, 110, 105, 99,
42
- 111, 100, 101, 120, 47, 97, 126, 195, 182, 195, 177, 194, 169, 226, 189,
43
- 152, 226, 152, 142, 240, 147, 139, 147, 240, 159, 152, 128, 240, 159, 145,
44
- 168, 226, 128, 141, 240, 159, 145, 169, 226, 128, 141, 240, 159, 145, 167,
45
- 226, 128, 141, 240, 159, 145, 167,
46
- ]),
47
- cid: 'bafyreiclp443lavogvhj3d2ob2cxbfuscni2k5jk7bebjzg7khl3esabwq',
48
- },
49
- {
50
- name: 'ipld',
51
- json: {
52
- a: {
53
- $link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
54
- },
55
- b: {
56
- $bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
57
- },
58
- c: {
59
- $type: 'blob',
60
- ref: {
61
- $link: 'bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity',
62
- },
63
- mimeType: 'image/jpeg',
64
- size: 10000,
65
- },
66
- },
67
- ipld: {
68
- a: parseCid(
69
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
70
- ),
71
- b: new Uint8Array([
72
- 156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253,
73
- 65, 60, 242, 11, 98, 238, 213, 118, 248, 157, 238, 190, 176, 26, 194,
74
- 204, 141,
75
- ]),
76
- c: {
77
- $type: 'blob',
78
- ref: parseCid(
79
- 'bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity',
80
- ),
81
- mimeType: 'image/jpeg',
82
- size: 10000,
83
- },
84
- },
85
- cbor: new Uint8Array([
86
- 163, 97, 97, 216, 42, 88, 37, 0, 1, 113, 18, 32, 101, 6, 42, 90, 90, 0,
87
- 252, 22, 215, 60, 105, 68, 35, 124, 203, 193, 91, 28, 74, 114, 52, 72,
88
- 147, 54, 137, 29, 9, 23, 65, 162, 57, 208, 97, 98, 88, 32, 156, 81, 17,
89
- 142, 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253, 65, 60, 242, 11,
90
- 98, 238, 213, 118, 248, 157, 238, 190, 176, 26, 194, 204, 141, 97, 99,
91
- 164, 99, 114, 101, 102, 216, 42, 88, 37, 0, 1, 85, 18, 32, 66, 88, 207,
92
- 255, 120, 246, 19, 105, 118, 151, 86, 63, 146, 108, 145, 229, 211, 87, 77,
93
- 46, 162, 90, 231, 237, 146, 214, 235, 252, 35, 163, 136, 158, 100, 115,
94
- 105, 122, 101, 25, 39, 16, 101, 36, 116, 121, 112, 101, 100, 98, 108, 111,
95
- 98, 104, 109, 105, 109, 101, 84, 121, 112, 101, 106, 105, 109, 97, 103,
96
- 101, 47, 106, 112, 101, 103,
97
- ]),
98
- cid: 'bafyreihldkhcwijkde7gx4rpkkuw7pl6lbyu5gieunyc7ihactn5bkd2nm',
99
- },
100
- {
101
- name: 'ipldArray',
102
- json: [
103
- {
104
- $link: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
105
- },
106
- {
107
- $link: 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
108
- },
109
- {
110
- $link: 'bafyreiaizynclnqiolq7byfpjjtgqzn4sfrsgn7z2hhf6bo4utdwkin7ke',
111
- },
112
- {
113
- $link: 'bafyreifd4w4tcr5tluxz7osjtnofffvtsmgdqcfrfi6evjde4pl27lrjpy',
114
- },
115
- ],
116
- ipld: [
117
- parseCid('bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a'),
118
- parseCid('bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q'),
119
- parseCid('bafyreiaizynclnqiolq7byfpjjtgqzn4sfrsgn7z2hhf6bo4utdwkin7ke'),
120
- parseCid('bafyreifd4w4tcr5tluxz7osjtnofffvtsmgdqcfrfi6evjde4pl27lrjpy'),
121
- ],
122
- cbor: new Uint8Array([
123
- 132, 216, 42, 88, 37, 0, 1, 113, 18, 32, 101, 6, 42, 90, 90, 0, 252, 22,
124
- 215, 60, 105, 68, 35, 124, 203, 193, 91, 28, 74, 114, 52, 72, 147, 54,
125
- 137, 29, 9, 23, 65, 162, 57, 208, 216, 42, 88, 37, 0, 1, 113, 18, 32, 206,
126
- 188, 253, 200, 24, 248, 158, 85, 31, 33, 95, 133, 103, 145, 125, 120, 196,
127
- 209, 14, 220, 33, 139, 148, 27, 165, 214, 150, 172, 255, 213, 142, 244,
128
- 216, 42, 88, 37, 0, 1, 113, 18, 32, 8, 206, 26, 37, 182, 8, 114, 225, 240,
129
- 224, 175, 74, 102, 104, 101, 188, 145, 99, 35, 55, 249, 209, 206, 95, 5,
130
- 220, 164, 199, 101, 33, 191, 81, 216, 42, 88, 37, 0, 1, 113, 18, 32, 163,
131
- 229, 185, 49, 71, 179, 93, 47, 159, 186, 73, 155, 92, 82, 150, 179, 147,
132
- 12, 56, 8, 177, 42, 60, 74, 164, 100, 227, 215, 175, 174, 41, 126,
133
- ]),
134
- cid: 'bafyreiaj3udmqlqrcbjxjayzuxwp64gt64olcbjfrkldzoqponpru6gq4m',
135
- },
136
- {
137
- name: 'ipldNested',
138
- json: {
139
- a: {
140
- b: [
141
- {
142
- d: [
143
- {
144
- $link:
145
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
146
- },
147
- {
148
- $link:
149
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
150
- },
151
- ],
152
- e: [
153
- {
154
- $bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
155
- },
156
- {
157
- $bytes: 'iE+sPoHobU9tSIqGI+309LLCcWQIRmEXwxcoDt19tas',
158
- },
159
- ],
160
- },
161
- ],
162
- },
163
- },
164
- ipld: {
165
- a: {
166
- b: [
167
- {
168
- d: [
169
- parseCid(
170
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
171
- ),
172
- parseCid(
173
- 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
174
- ),
175
- ],
176
- e: [
177
- new Uint8Array([
178
- 156, 81, 17, 142, 242, 203, 139, 15, 106, 155, 142, 73, 174,
179
- 161, 253, 65, 60, 242, 11, 98, 238, 213, 118, 248, 157, 238,
180
- 190, 176, 26, 194, 204, 141,
181
- ]),
182
- new Uint8Array([
183
- 136, 79, 172, 62, 129, 232, 109, 79, 109, 72, 138, 134, 35, 237,
184
- 244, 244, 178, 194, 113, 100, 8, 70, 97, 23, 195, 23, 40, 14,
185
- 221, 125, 181, 171,
186
- ]),
187
- ],
188
- },
189
- ],
190
- },
191
- },
192
- cbor: new Uint8Array([
193
- 161, 97, 97, 161, 97, 98, 129, 162, 97, 100, 130, 216, 42, 88, 37, 0, 1,
194
- 113, 18, 32, 101, 6, 42, 90, 90, 0, 252, 22, 215, 60, 105, 68, 35, 124,
195
- 203, 193, 91, 28, 74, 114, 52, 72, 147, 54, 137, 29, 9, 23, 65, 162, 57,
196
- 208, 216, 42, 88, 37, 0, 1, 113, 18, 32, 101, 6, 42, 90, 90, 0, 252, 22,
197
- 215, 60, 105, 68, 35, 124, 203, 193, 91, 28, 74, 114, 52, 72, 147, 54,
198
- 137, 29, 9, 23, 65, 162, 57, 208, 97, 101, 130, 88, 32, 156, 81, 17, 142,
199
- 242, 203, 139, 15, 106, 155, 142, 73, 174, 161, 253, 65, 60, 242, 11, 98,
200
- 238, 213, 118, 248, 157, 238, 190, 176, 26, 194, 204, 141, 88, 32, 136,
201
- 79, 172, 62, 129, 232, 109, 79, 109, 72, 138, 134, 35, 237, 244, 244, 178,
202
- 194, 113, 100, 8, 70, 97, 23, 195, 23, 40, 14, 221, 125, 181, 171,
203
- ]),
204
- cid: 'bafyreid3imdulnhgeytpf6uk7zahjvrsqlofkmm5b5ub2maw4kqus6jp4i',
205
- },
206
- {
207
- name: 'poorlyFormatted',
208
- json: {
209
- a: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
210
- b: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
211
- c: {
212
- $link: 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
213
- another: 'bad value',
214
- },
215
- d: {
216
- $bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
217
- another: 'bad value',
218
- },
219
- e: {
220
- '/': 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
221
- },
222
- f: {
223
- '/': {
224
- bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
225
- },
226
- },
227
- },
228
- ipld: {
229
- a: 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
230
- b: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
231
- c: {
232
- $link: 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
233
- another: 'bad value',
234
- },
235
- d: {
236
- $bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
237
- another: 'bad value',
238
- },
239
- e: {
240
- '/': 'bafyreigoxt64qghytzkr6ik7qvtzc7lyytiq5xbbrokbxjows2wp7vmo6q',
241
- },
242
- f: {
243
- '/': {
244
- bytes: 'nFERjvLLiw9qm45JrqH9QTzyC2Lu1Xb4ne6+sBrCzI0',
245
- },
246
- },
247
- },
248
- cbor: new Uint8Array([
249
- 166, 97, 97, 120, 59, 98, 97, 102, 121, 114, 101, 105, 100, 102, 97, 121,
250
- 118, 102, 117, 119, 113, 97, 55, 113, 108, 110, 111, 112, 100, 106, 105,
251
- 113, 114, 120, 122, 115, 54, 98, 108, 109, 111, 101, 117, 52, 114, 117,
252
- 106, 99, 106, 116, 110, 99, 105, 53, 98, 101, 108, 117, 100, 105, 114,
253
- 122, 50, 97, 97, 98, 120, 43, 110, 70, 69, 82, 106, 118, 76, 76, 105, 119,
254
- 57, 113, 109, 52, 53, 74, 114, 113, 72, 57, 81, 84, 122, 121, 67, 50, 76,
255
- 117, 49, 88, 98, 52, 110, 101, 54, 43, 115, 66, 114, 67, 122, 73, 48, 97,
256
- 99, 162, 101, 36, 108, 105, 110, 107, 120, 59, 98, 97, 102, 121, 114, 101,
257
- 105, 103, 111, 120, 116, 54, 52, 113, 103, 104, 121, 116, 122, 107, 114,
258
- 54, 105, 107, 55, 113, 118, 116, 122, 99, 55, 108, 121, 121, 116, 105,
259
- 113, 53, 120, 98, 98, 114, 111, 107, 98, 120, 106, 111, 119, 115, 50, 119,
260
- 112, 55, 118, 109, 111, 54, 113, 103, 97, 110, 111, 116, 104, 101, 114,
261
- 105, 98, 97, 100, 32, 118, 97, 108, 117, 101, 97, 100, 162, 102, 36, 98,
262
- 121, 116, 101, 115, 120, 43, 110, 70, 69, 82, 106, 118, 76, 76, 105, 119,
263
- 57, 113, 109, 52, 53, 74, 114, 113, 72, 57, 81, 84, 122, 121, 67, 50, 76,
264
- 117, 49, 88, 98, 52, 110, 101, 54, 43, 115, 66, 114, 67, 122, 73, 48, 103,
265
- 97, 110, 111, 116, 104, 101, 114, 105, 98, 97, 100, 32, 118, 97, 108, 117,
266
- 101, 97, 101, 161, 97, 47, 120, 59, 98, 97, 102, 121, 114, 101, 105, 103,
267
- 111, 120, 116, 54, 52, 113, 103, 104, 121, 116, 122, 107, 114, 54, 105,
268
- 107, 55, 113, 118, 116, 122, 99, 55, 108, 121, 121, 116, 105, 113, 53,
269
- 120, 98, 98, 114, 111, 107, 98, 120, 106, 111, 119, 115, 50, 119, 112, 55,
270
- 118, 109, 111, 54, 113, 97, 102, 161, 97, 47, 161, 101, 98, 121, 116, 101,
271
- 115, 120, 43, 110, 70, 69, 82, 106, 118, 76, 76, 105, 119, 57, 113, 109,
272
- 52, 53, 74, 114, 113, 72, 57, 81, 84, 122, 121, 67, 50, 76, 117, 49, 88,
273
- 98, 52, 110, 101, 54, 43, 115, 66, 114, 67, 122, 73, 48,
274
- ]),
275
- cid: 'bafyreico7wgbbfe6dpfsuednrtrlh6t2yjl6xq5rf32gl3pgwhwxk77cn4',
276
- },
277
- ]
@@ -1,29 +0,0 @@
1
- import * as ui8 from 'uint8arrays'
2
- import {
3
- cborDecode,
4
- cborEncode,
5
- cidForCbor,
6
- ipldEquals,
7
- ipldToJson,
8
- jsonToIpld,
9
- } from '../src/index.js'
10
- import { vectors } from './ipld-vectors.js'
11
-
12
- describe('ipld', () => {
13
- for (const vector of vectors) {
14
- it(`passes test vector: ${vector.name}`, async () => {
15
- const ipld = jsonToIpld(vector.json)
16
- const json = ipldToJson(ipld)
17
- const cbor = cborEncode(ipld)
18
- const ipldAgain = cborDecode(cbor)
19
- const jsonAgain = ipldToJson(ipldAgain)
20
- const cid = await cidForCbor(ipld)
21
- expect(json).toEqual(vector.json)
22
- expect(jsonAgain).toEqual(vector.json)
23
- expect(ipldEquals(ipld, vector.ipld)).toBeTruthy()
24
- expect(ipldEquals(ipldAgain, vector.ipld)).toBeTruthy()
25
- expect(ui8.equals(cbor, vector.cbor)).toBeTruthy()
26
- expect(cid.toString()).toEqual(vector.cid)
27
- })
28
- }
29
- })