@libp2p/autonat-v2 0.0.0-2d6079bc1

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.
@@ -0,0 +1,614 @@
1
+ import { decodeMessage, encodeMessage, enumeration, MaxLengthError, message } from 'protons-runtime'
2
+ import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
3
+ import type { Codec, DecodeOptions } from 'protons-runtime'
4
+ import type { Uint8ArrayList } from 'uint8arraylist'
5
+
6
+ export interface Message {
7
+ dialRequest?: DialRequest
8
+ dialResponse?: DialResponse
9
+ dialDataRequest?: DialDataRequest
10
+ dialDataResponse?: DialDataResponse
11
+ }
12
+
13
+ export namespace Message {
14
+ let _codec: Codec<Message>
15
+
16
+ export const codec = (): Codec<Message> => {
17
+ if (_codec == null) {
18
+ _codec = message<Message>((obj, w, opts = {}) => {
19
+ if (opts.lengthDelimited !== false) {
20
+ w.fork()
21
+ }
22
+
23
+ obj = { ...obj }
24
+
25
+ if (obj.dialDataResponse != null) {
26
+ obj.dialDataRequest = undefined
27
+ obj.dialResponse = undefined
28
+ obj.dialRequest = undefined
29
+ }
30
+
31
+ if (obj.dialDataRequest != null) {
32
+ obj.dialDataResponse = undefined
33
+ obj.dialResponse = undefined
34
+ obj.dialRequest = undefined
35
+ }
36
+
37
+ if (obj.dialResponse != null) {
38
+ obj.dialDataResponse = undefined
39
+ obj.dialDataRequest = undefined
40
+ obj.dialRequest = undefined
41
+ }
42
+
43
+ if (obj.dialRequest != null) {
44
+ obj.dialDataResponse = undefined
45
+ obj.dialDataRequest = undefined
46
+ obj.dialResponse = undefined
47
+ }
48
+
49
+ if (obj.dialRequest != null) {
50
+ w.uint32(10)
51
+ DialRequest.codec().encode(obj.dialRequest, w)
52
+ }
53
+
54
+ if (obj.dialResponse != null) {
55
+ w.uint32(18)
56
+ DialResponse.codec().encode(obj.dialResponse, w)
57
+ }
58
+
59
+ if (obj.dialDataRequest != null) {
60
+ w.uint32(26)
61
+ DialDataRequest.codec().encode(obj.dialDataRequest, w)
62
+ }
63
+
64
+ if (obj.dialDataResponse != null) {
65
+ w.uint32(34)
66
+ DialDataResponse.codec().encode(obj.dialDataResponse, w)
67
+ }
68
+
69
+ if (opts.lengthDelimited !== false) {
70
+ w.ldelim()
71
+ }
72
+ }, (reader, length, opts = {}) => {
73
+ const obj: any = {}
74
+
75
+ const end = length == null ? reader.len : reader.pos + length
76
+
77
+ while (reader.pos < end) {
78
+ const tag = reader.uint32()
79
+
80
+ switch (tag >>> 3) {
81
+ case 1: {
82
+ obj.dialRequest = DialRequest.codec().decode(reader, reader.uint32(), {
83
+ limits: opts.limits?.dialRequest
84
+ })
85
+ break
86
+ }
87
+ case 2: {
88
+ obj.dialResponse = DialResponse.codec().decode(reader, reader.uint32(), {
89
+ limits: opts.limits?.dialResponse
90
+ })
91
+ break
92
+ }
93
+ case 3: {
94
+ obj.dialDataRequest = DialDataRequest.codec().decode(reader, reader.uint32(), {
95
+ limits: opts.limits?.dialDataRequest
96
+ })
97
+ break
98
+ }
99
+ case 4: {
100
+ obj.dialDataResponse = DialDataResponse.codec().decode(reader, reader.uint32(), {
101
+ limits: opts.limits?.dialDataResponse
102
+ })
103
+ break
104
+ }
105
+ default: {
106
+ reader.skipType(tag & 7)
107
+ break
108
+ }
109
+ }
110
+ }
111
+
112
+ if (obj.dialDataResponse != null) {
113
+ delete obj.dialDataRequest
114
+ delete obj.dialResponse
115
+ delete obj.dialRequest
116
+ }
117
+
118
+ if (obj.dialDataRequest != null) {
119
+ delete obj.dialDataResponse
120
+ delete obj.dialResponse
121
+ delete obj.dialRequest
122
+ }
123
+
124
+ if (obj.dialResponse != null) {
125
+ delete obj.dialDataResponse
126
+ delete obj.dialDataRequest
127
+ delete obj.dialRequest
128
+ }
129
+
130
+ if (obj.dialRequest != null) {
131
+ delete obj.dialDataResponse
132
+ delete obj.dialDataRequest
133
+ delete obj.dialResponse
134
+ }
135
+
136
+ return obj
137
+ })
138
+ }
139
+
140
+ return _codec
141
+ }
142
+
143
+ export const encode = (obj: Partial<Message>): Uint8Array => {
144
+ return encodeMessage(obj, Message.codec())
145
+ }
146
+
147
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<Message>): Message => {
148
+ return decodeMessage(buf, Message.codec(), opts)
149
+ }
150
+ }
151
+
152
+ export interface DialRequest {
153
+ addrs: Uint8Array[]
154
+ nonce: bigint
155
+ }
156
+
157
+ export namespace DialRequest {
158
+ let _codec: Codec<DialRequest>
159
+
160
+ export const codec = (): Codec<DialRequest> => {
161
+ if (_codec == null) {
162
+ _codec = message<DialRequest>((obj, w, opts = {}) => {
163
+ if (opts.lengthDelimited !== false) {
164
+ w.fork()
165
+ }
166
+
167
+ if (obj.addrs != null) {
168
+ for (const value of obj.addrs) {
169
+ w.uint32(10)
170
+ w.bytes(value)
171
+ }
172
+ }
173
+
174
+ if ((obj.nonce != null && obj.nonce !== 0n)) {
175
+ w.uint32(17)
176
+ w.fixed64(obj.nonce)
177
+ }
178
+
179
+ if (opts.lengthDelimited !== false) {
180
+ w.ldelim()
181
+ }
182
+ }, (reader, length, opts = {}) => {
183
+ const obj: any = {
184
+ addrs: [],
185
+ nonce: 0n
186
+ }
187
+
188
+ const end = length == null ? reader.len : reader.pos + length
189
+
190
+ while (reader.pos < end) {
191
+ const tag = reader.uint32()
192
+
193
+ switch (tag >>> 3) {
194
+ case 1: {
195
+ if (opts.limits?.addrs != null && obj.addrs.length === opts.limits.addrs) {
196
+ throw new MaxLengthError('Decode error - map field "addrs" had too many elements')
197
+ }
198
+
199
+ obj.addrs.push(reader.bytes())
200
+ break
201
+ }
202
+ case 2: {
203
+ obj.nonce = reader.fixed64()
204
+ break
205
+ }
206
+ default: {
207
+ reader.skipType(tag & 7)
208
+ break
209
+ }
210
+ }
211
+ }
212
+
213
+ return obj
214
+ })
215
+ }
216
+
217
+ return _codec
218
+ }
219
+
220
+ export const encode = (obj: Partial<DialRequest>): Uint8Array => {
221
+ return encodeMessage(obj, DialRequest.codec())
222
+ }
223
+
224
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialRequest>): DialRequest => {
225
+ return decodeMessage(buf, DialRequest.codec(), opts)
226
+ }
227
+ }
228
+
229
+ export interface DialDataRequest {
230
+ addrIdx: number
231
+ numBytes: bigint
232
+ }
233
+
234
+ export namespace DialDataRequest {
235
+ let _codec: Codec<DialDataRequest>
236
+
237
+ export const codec = (): Codec<DialDataRequest> => {
238
+ if (_codec == null) {
239
+ _codec = message<DialDataRequest>((obj, w, opts = {}) => {
240
+ if (opts.lengthDelimited !== false) {
241
+ w.fork()
242
+ }
243
+
244
+ if ((obj.addrIdx != null && obj.addrIdx !== 0)) {
245
+ w.uint32(8)
246
+ w.uint32(obj.addrIdx)
247
+ }
248
+
249
+ if ((obj.numBytes != null && obj.numBytes !== 0n)) {
250
+ w.uint32(16)
251
+ w.uint64(obj.numBytes)
252
+ }
253
+
254
+ if (opts.lengthDelimited !== false) {
255
+ w.ldelim()
256
+ }
257
+ }, (reader, length, opts = {}) => {
258
+ const obj: any = {
259
+ addrIdx: 0,
260
+ numBytes: 0n
261
+ }
262
+
263
+ const end = length == null ? reader.len : reader.pos + length
264
+
265
+ while (reader.pos < end) {
266
+ const tag = reader.uint32()
267
+
268
+ switch (tag >>> 3) {
269
+ case 1: {
270
+ obj.addrIdx = reader.uint32()
271
+ break
272
+ }
273
+ case 2: {
274
+ obj.numBytes = reader.uint64()
275
+ break
276
+ }
277
+ default: {
278
+ reader.skipType(tag & 7)
279
+ break
280
+ }
281
+ }
282
+ }
283
+
284
+ return obj
285
+ })
286
+ }
287
+
288
+ return _codec
289
+ }
290
+
291
+ export const encode = (obj: Partial<DialDataRequest>): Uint8Array => {
292
+ return encodeMessage(obj, DialDataRequest.codec())
293
+ }
294
+
295
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialDataRequest>): DialDataRequest => {
296
+ return decodeMessage(buf, DialDataRequest.codec(), opts)
297
+ }
298
+ }
299
+
300
+ export enum DialStatus {
301
+ UNUSED = 'UNUSED',
302
+ E_DIAL_ERROR = 'E_DIAL_ERROR',
303
+ E_DIAL_BACK_ERROR = 'E_DIAL_BACK_ERROR',
304
+ OK = 'OK'
305
+ }
306
+
307
+ enum __DialStatusValues {
308
+ UNUSED = 0,
309
+ E_DIAL_ERROR = 100,
310
+ E_DIAL_BACK_ERROR = 101,
311
+ OK = 200
312
+ }
313
+
314
+ export namespace DialStatus {
315
+ export const codec = (): Codec<DialStatus> => {
316
+ return enumeration<DialStatus>(__DialStatusValues)
317
+ }
318
+ }
319
+
320
+ export interface DialResponse {
321
+ status: DialResponse.ResponseStatus
322
+ addrIdx: number
323
+ dialStatus: DialStatus
324
+ }
325
+
326
+ export namespace DialResponse {
327
+ export enum ResponseStatus {
328
+ E_INTERNAL_ERROR = 'E_INTERNAL_ERROR',
329
+ E_REQUEST_REJECTED = 'E_REQUEST_REJECTED',
330
+ E_DIAL_REFUSED = 'E_DIAL_REFUSED',
331
+ OK = 'OK'
332
+ }
333
+
334
+ enum __ResponseStatusValues {
335
+ E_INTERNAL_ERROR = 0,
336
+ E_REQUEST_REJECTED = 100,
337
+ E_DIAL_REFUSED = 101,
338
+ OK = 200
339
+ }
340
+
341
+ export namespace ResponseStatus {
342
+ export const codec = (): Codec<ResponseStatus> => {
343
+ return enumeration<ResponseStatus>(__ResponseStatusValues)
344
+ }
345
+ }
346
+
347
+ let _codec: Codec<DialResponse>
348
+
349
+ export const codec = (): Codec<DialResponse> => {
350
+ if (_codec == null) {
351
+ _codec = message<DialResponse>((obj, w, opts = {}) => {
352
+ if (opts.lengthDelimited !== false) {
353
+ w.fork()
354
+ }
355
+
356
+ if (obj.status != null && __ResponseStatusValues[obj.status] !== 0) {
357
+ w.uint32(8)
358
+ DialResponse.ResponseStatus.codec().encode(obj.status, w)
359
+ }
360
+
361
+ if ((obj.addrIdx != null && obj.addrIdx !== 0)) {
362
+ w.uint32(16)
363
+ w.uint32(obj.addrIdx)
364
+ }
365
+
366
+ if (obj.dialStatus != null && __DialStatusValues[obj.dialStatus] !== 0) {
367
+ w.uint32(24)
368
+ DialStatus.codec().encode(obj.dialStatus, w)
369
+ }
370
+
371
+ if (opts.lengthDelimited !== false) {
372
+ w.ldelim()
373
+ }
374
+ }, (reader, length, opts = {}) => {
375
+ const obj: any = {
376
+ status: ResponseStatus.E_INTERNAL_ERROR,
377
+ addrIdx: 0,
378
+ dialStatus: DialStatus.UNUSED
379
+ }
380
+
381
+ const end = length == null ? reader.len : reader.pos + length
382
+
383
+ while (reader.pos < end) {
384
+ const tag = reader.uint32()
385
+
386
+ switch (tag >>> 3) {
387
+ case 1: {
388
+ obj.status = DialResponse.ResponseStatus.codec().decode(reader)
389
+ break
390
+ }
391
+ case 2: {
392
+ obj.addrIdx = reader.uint32()
393
+ break
394
+ }
395
+ case 3: {
396
+ obj.dialStatus = DialStatus.codec().decode(reader)
397
+ break
398
+ }
399
+ default: {
400
+ reader.skipType(tag & 7)
401
+ break
402
+ }
403
+ }
404
+ }
405
+
406
+ return obj
407
+ })
408
+ }
409
+
410
+ return _codec
411
+ }
412
+
413
+ export const encode = (obj: Partial<DialResponse>): Uint8Array => {
414
+ return encodeMessage(obj, DialResponse.codec())
415
+ }
416
+
417
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialResponse>): DialResponse => {
418
+ return decodeMessage(buf, DialResponse.codec(), opts)
419
+ }
420
+ }
421
+
422
+ export interface DialDataResponse {
423
+ data: Uint8Array
424
+ }
425
+
426
+ export namespace DialDataResponse {
427
+ let _codec: Codec<DialDataResponse>
428
+
429
+ export const codec = (): Codec<DialDataResponse> => {
430
+ if (_codec == null) {
431
+ _codec = message<DialDataResponse>((obj, w, opts = {}) => {
432
+ if (opts.lengthDelimited !== false) {
433
+ w.fork()
434
+ }
435
+
436
+ if ((obj.data != null && obj.data.byteLength > 0)) {
437
+ w.uint32(10)
438
+ w.bytes(obj.data)
439
+ }
440
+
441
+ if (opts.lengthDelimited !== false) {
442
+ w.ldelim()
443
+ }
444
+ }, (reader, length, opts = {}) => {
445
+ const obj: any = {
446
+ data: uint8ArrayAlloc(0)
447
+ }
448
+
449
+ const end = length == null ? reader.len : reader.pos + length
450
+
451
+ while (reader.pos < end) {
452
+ const tag = reader.uint32()
453
+
454
+ switch (tag >>> 3) {
455
+ case 1: {
456
+ obj.data = reader.bytes()
457
+ break
458
+ }
459
+ default: {
460
+ reader.skipType(tag & 7)
461
+ break
462
+ }
463
+ }
464
+ }
465
+
466
+ return obj
467
+ })
468
+ }
469
+
470
+ return _codec
471
+ }
472
+
473
+ export const encode = (obj: Partial<DialDataResponse>): Uint8Array => {
474
+ return encodeMessage(obj, DialDataResponse.codec())
475
+ }
476
+
477
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialDataResponse>): DialDataResponse => {
478
+ return decodeMessage(buf, DialDataResponse.codec(), opts)
479
+ }
480
+ }
481
+
482
+ export interface DialBack {
483
+ nonce: bigint
484
+ }
485
+
486
+ export namespace DialBack {
487
+ let _codec: Codec<DialBack>
488
+
489
+ export const codec = (): Codec<DialBack> => {
490
+ if (_codec == null) {
491
+ _codec = message<DialBack>((obj, w, opts = {}) => {
492
+ if (opts.lengthDelimited !== false) {
493
+ w.fork()
494
+ }
495
+
496
+ if ((obj.nonce != null && obj.nonce !== 0n)) {
497
+ w.uint32(9)
498
+ w.fixed64(obj.nonce)
499
+ }
500
+
501
+ if (opts.lengthDelimited !== false) {
502
+ w.ldelim()
503
+ }
504
+ }, (reader, length, opts = {}) => {
505
+ const obj: any = {
506
+ nonce: 0n
507
+ }
508
+
509
+ const end = length == null ? reader.len : reader.pos + length
510
+
511
+ while (reader.pos < end) {
512
+ const tag = reader.uint32()
513
+
514
+ switch (tag >>> 3) {
515
+ case 1: {
516
+ obj.nonce = reader.fixed64()
517
+ break
518
+ }
519
+ default: {
520
+ reader.skipType(tag & 7)
521
+ break
522
+ }
523
+ }
524
+ }
525
+
526
+ return obj
527
+ })
528
+ }
529
+
530
+ return _codec
531
+ }
532
+
533
+ export const encode = (obj: Partial<DialBack>): Uint8Array => {
534
+ return encodeMessage(obj, DialBack.codec())
535
+ }
536
+
537
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialBack>): DialBack => {
538
+ return decodeMessage(buf, DialBack.codec(), opts)
539
+ }
540
+ }
541
+
542
+ export interface DialBackResponse {
543
+ status: DialBackResponse.DialBackStatus
544
+ }
545
+
546
+ export namespace DialBackResponse {
547
+ export enum DialBackStatus {
548
+ OK = 'OK'
549
+ }
550
+
551
+ enum __DialBackStatusValues {
552
+ OK = 0
553
+ }
554
+
555
+ export namespace DialBackStatus {
556
+ export const codec = (): Codec<DialBackStatus> => {
557
+ return enumeration<DialBackStatus>(__DialBackStatusValues)
558
+ }
559
+ }
560
+
561
+ let _codec: Codec<DialBackResponse>
562
+
563
+ export const codec = (): Codec<DialBackResponse> => {
564
+ if (_codec == null) {
565
+ _codec = message<DialBackResponse>((obj, w, opts = {}) => {
566
+ if (opts.lengthDelimited !== false) {
567
+ w.fork()
568
+ }
569
+
570
+ if (obj.status != null && __DialBackStatusValues[obj.status] !== 0) {
571
+ w.uint32(8)
572
+ DialBackResponse.DialBackStatus.codec().encode(obj.status, w)
573
+ }
574
+
575
+ if (opts.lengthDelimited !== false) {
576
+ w.ldelim()
577
+ }
578
+ }, (reader, length, opts = {}) => {
579
+ const obj: any = {
580
+ status: DialBackStatus.OK
581
+ }
582
+
583
+ const end = length == null ? reader.len : reader.pos + length
584
+
585
+ while (reader.pos < end) {
586
+ const tag = reader.uint32()
587
+
588
+ switch (tag >>> 3) {
589
+ case 1: {
590
+ obj.status = DialBackResponse.DialBackStatus.codec().decode(reader)
591
+ break
592
+ }
593
+ default: {
594
+ reader.skipType(tag & 7)
595
+ break
596
+ }
597
+ }
598
+ }
599
+
600
+ return obj
601
+ })
602
+ }
603
+
604
+ return _codec
605
+ }
606
+
607
+ export const encode = (obj: Partial<DialBackResponse>): Uint8Array => {
608
+ return encodeMessage(obj, DialBackResponse.codec())
609
+ }
610
+
611
+ export const decode = (buf: Uint8Array | Uint8ArrayList, opts?: DecodeOptions<DialBackResponse>): DialBackResponse => {
612
+ return decodeMessage(buf, DialBackResponse.codec(), opts)
613
+ }
614
+ }