@libp2p/fetch 0.0.0-05b52d69c

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,153 @@
1
+ /* eslint-disable import/export */
2
+ /* eslint-disable complexity */
3
+ /* eslint-disable @typescript-eslint/no-namespace */
4
+ /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
6
+
7
+ import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
8
+ import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
10
+
11
+ export interface FetchRequest {
12
+ identifier: string
13
+ }
14
+
15
+ export namespace FetchRequest {
16
+ let _codec: Codec<FetchRequest>
17
+
18
+ export const codec = (): Codec<FetchRequest> => {
19
+ if (_codec == null) {
20
+ _codec = message<FetchRequest>((obj, w, opts = {}) => {
21
+ if (opts.lengthDelimited !== false) {
22
+ w.fork()
23
+ }
24
+
25
+ if ((obj.identifier != null && obj.identifier !== '')) {
26
+ w.uint32(10)
27
+ w.string(obj.identifier)
28
+ }
29
+
30
+ if (opts.lengthDelimited !== false) {
31
+ w.ldelim()
32
+ }
33
+ }, (reader, length) => {
34
+ const obj: any = {
35
+ identifier: ''
36
+ }
37
+
38
+ const end = length == null ? reader.len : reader.pos + length
39
+
40
+ while (reader.pos < end) {
41
+ const tag = reader.uint32()
42
+
43
+ switch (tag >>> 3) {
44
+ case 1:
45
+ obj.identifier = reader.string()
46
+ break
47
+ default:
48
+ reader.skipType(tag & 7)
49
+ break
50
+ }
51
+ }
52
+
53
+ return obj
54
+ })
55
+ }
56
+
57
+ return _codec
58
+ }
59
+
60
+ export const encode = (obj: Partial<FetchRequest>): Uint8Array => {
61
+ return encodeMessage(obj, FetchRequest.codec())
62
+ }
63
+
64
+ export const decode = (buf: Uint8Array | Uint8ArrayList): FetchRequest => {
65
+ return decodeMessage(buf, FetchRequest.codec())
66
+ }
67
+ }
68
+
69
+ export interface FetchResponse {
70
+ status: FetchResponse.StatusCode
71
+ data: Uint8Array
72
+ }
73
+
74
+ export namespace FetchResponse {
75
+ export enum StatusCode {
76
+ OK = 'OK',
77
+ NOT_FOUND = 'NOT_FOUND',
78
+ ERROR = 'ERROR'
79
+ }
80
+
81
+ enum __StatusCodeValues {
82
+ OK = 0,
83
+ NOT_FOUND = 1,
84
+ ERROR = 2
85
+ }
86
+
87
+ export namespace StatusCode {
88
+ export const codec = (): Codec<StatusCode> => {
89
+ return enumeration<StatusCode>(__StatusCodeValues)
90
+ }
91
+ }
92
+
93
+ let _codec: Codec<FetchResponse>
94
+
95
+ export const codec = (): Codec<FetchResponse> => {
96
+ if (_codec == null) {
97
+ _codec = message<FetchResponse>((obj, w, opts = {}) => {
98
+ if (opts.lengthDelimited !== false) {
99
+ w.fork()
100
+ }
101
+
102
+ if (obj.status != null && __StatusCodeValues[obj.status] !== 0) {
103
+ w.uint32(8)
104
+ FetchResponse.StatusCode.codec().encode(obj.status, w)
105
+ }
106
+
107
+ if ((obj.data != null && obj.data.byteLength > 0)) {
108
+ w.uint32(18)
109
+ w.bytes(obj.data)
110
+ }
111
+
112
+ if (opts.lengthDelimited !== false) {
113
+ w.ldelim()
114
+ }
115
+ }, (reader, length) => {
116
+ const obj: any = {
117
+ status: StatusCode.OK,
118
+ data: new Uint8Array(0)
119
+ }
120
+
121
+ const end = length == null ? reader.len : reader.pos + length
122
+
123
+ while (reader.pos < end) {
124
+ const tag = reader.uint32()
125
+
126
+ switch (tag >>> 3) {
127
+ case 1:
128
+ obj.status = FetchResponse.StatusCode.codec().decode(reader)
129
+ break
130
+ case 2:
131
+ obj.data = reader.bytes()
132
+ break
133
+ default:
134
+ reader.skipType(tag & 7)
135
+ break
136
+ }
137
+ }
138
+
139
+ return obj
140
+ })
141
+ }
142
+
143
+ return _codec
144
+ }
145
+
146
+ export const encode = (obj: Partial<FetchResponse>): Uint8Array => {
147
+ return encodeMessage(obj, FetchResponse.codec())
148
+ }
149
+
150
+ export const decode = (buf: Uint8Array | Uint8ArrayList): FetchResponse => {
151
+ return decodeMessage(buf, FetchResponse.codec())
152
+ }
153
+ }