@libp2p/fetch 3.0.22 → 4.0.0-55b7e5fea

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.
@@ -1,5 +1,5 @@
1
1
  import type { Fetch as FetchInterface, FetchComponents, FetchInit, LookupFunction } from './index.js';
2
- import type { AbortOptions, PeerId, Startable, IncomingStreamData } from '@libp2p/interface';
2
+ import type { AbortOptions, Stream, PeerId, Startable } from '@libp2p/interface';
3
3
  /**
4
4
  * A simple libp2p protocol for requesting a value corresponding to a key from a peer.
5
5
  * Developers can register one or more lookup function for retrieving the value corresponding to
@@ -26,7 +26,7 @@ export declare class Fetch implements Startable, FetchInterface {
26
26
  * responds based on looking up the key in the request via the lookup callback that corresponds
27
27
  * to the key's prefix.
28
28
  */
29
- handleMessage(data: IncomingStreamData): Promise<void>;
29
+ handleMessage(stream: Stream): Promise<void>;
30
30
  /**
31
31
  * Given a key, finds the appropriate function for looking up its corresponding value, based on
32
32
  * the key's prefix.
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,IAAI,cAAc,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACrG,OAAO,KAAK,EAAE,YAAY,EAAU,MAAM,EAAE,SAAS,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAIpG;;;;;GAKG;AACH,qBAAa,KAAM,YAAW,SAAS,EAAE,cAAc;IACrD,SAAgB,QAAQ,EAAE,MAAM,CAAA;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA6B;IAC7D,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAW;gBAEnB,UAAU,EAAE,eAAe,EAAE,IAAI,GAAE,SAAc;IAS9D,QAAQ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IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAW;gBAEnB,UAAU,EAAE,eAAe,EAAE,IAAI,GAAE,SAAc;IAU9D,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,mBAAkB;IAEzC,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAQvB,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAK5B,SAAS,IAAK,OAAO;IAIrB;;OAEG;IACG,KAAK,CAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,EAAE,OAAO,GAAE,YAAiB,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC;IA4DjH;;;;OAIG;IACG,aAAa,CAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+CnD;;;OAGG;IACH,kBAAkB,CAAE,GAAG,EAAE,MAAM,GAAG;QAAE,EAAE,EAAE,cAAc,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS;IAepF;;;;;;;;;;OAUG;IACH,sBAAsB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,IAAI;IAQrE;;;;;;;;;;OAUG;IACH,wBAAwB,CAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,cAAc,GAAG,IAAI;CAWzE"}
package/dist/src/fetch.js CHANGED
@@ -1,5 +1,5 @@
1
- import { AbortError, InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface';
2
- import { pbStream } from 'it-protobuf-stream';
1
+ import { InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface';
2
+ import { pbStream } from '@libp2p/utils';
3
3
  import { setMaxListeners } from 'main-event';
4
4
  import { fromString as uint8arrayFromString } from 'uint8arrays/from-string';
5
5
  import { toString as uint8arrayToString } from 'uint8arrays/to-string';
@@ -23,21 +23,12 @@ export class Fetch {
23
23
  this.components = components;
24
24
  this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`;
25
25
  this.lookupFunctions = new Map(); // Maps key prefix to value lookup function
26
- this.handleMessage = this.handleMessage.bind(this);
27
26
  this.init = init;
27
+ this.handleMessage = this.handleMessage.bind(this);
28
28
  }
29
29
  [Symbol.toStringTag] = '@libp2p/fetch';
30
30
  async start() {
31
- await this.components.registrar.handle(this.protocol, (data) => {
32
- const log = data.connection.log.newScope('fetch');
33
- void this.handleMessage(data)
34
- .then(async () => {
35
- await data.stream.close();
36
- })
37
- .catch(err => {
38
- log.error('error handling message - %e', err);
39
- });
40
- }, {
31
+ await this.components.registrar.handle(this.protocol, this.handleMessage, {
41
32
  maxInboundStreams: this.init.maxInboundStreams,
42
33
  maxOutboundStreams: this.init.maxOutboundStreams
43
34
  });
@@ -57,34 +48,28 @@ export class Fetch {
57
48
  if (typeof key === 'string') {
58
49
  key = uint8arrayFromString(key);
59
50
  }
60
- const connection = await this.components.connectionManager.openConnection(peer, options);
61
- const log = connection.log.newScope('fetch');
62
- let signal = options.signal;
63
- let stream;
64
- let onAbort = () => { };
65
51
  // create a timeout if no abort signal passed
66
- if (signal == null) {
52
+ if (options.signal == null) {
67
53
  const timeout = this.init.timeout ?? DEFAULT_TIMEOUT;
68
- log.trace('using default timeout of %d ms', timeout);
69
- signal = AbortSignal.timeout(timeout);
54
+ const signal = AbortSignal.timeout(timeout);
70
55
  setMaxListeners(Infinity, signal);
71
- }
72
- try {
73
- stream = await connection.newStream(this.protocol, {
56
+ options = {
57
+ ...options,
74
58
  signal
75
- });
76
- onAbort = () => {
77
- stream?.abort(new AbortError());
78
59
  };
79
- // make stream abortable
80
- signal.addEventListener('abort', onAbort, { once: true });
60
+ }
61
+ let stream;
62
+ try {
63
+ const connection = await this.components.connectionManager.openConnection(peer, options);
64
+ stream = await connection.newStream(this.protocol, options);
65
+ const log = stream.log.newScope('fetch');
81
66
  log.trace('fetch %m', key);
82
67
  const pb = pbStream(stream);
83
68
  await pb.write({
84
69
  identifier: key
85
70
  }, FetchRequest, options);
86
71
  const response = await pb.read(FetchResponse, options);
87
- await pb.unwrap().close(options);
72
+ await stream.close(options);
88
73
  switch (response.status) {
89
74
  case (FetchResponse.StatusCode.OK): {
90
75
  log.trace('received status OK for %m', key);
@@ -109,65 +94,52 @@ export class Fetch {
109
94
  stream?.abort(err);
110
95
  throw err;
111
96
  }
112
- finally {
113
- signal.removeEventListener('abort', onAbort);
114
- if (stream != null) {
115
- await stream.close();
116
- }
117
- }
118
97
  }
119
98
  /**
120
99
  * Invoked when a fetch request is received. Reads the request message off the given stream and
121
100
  * responds based on looking up the key in the request via the lookup callback that corresponds
122
101
  * to the key's prefix.
123
102
  */
124
- async handleMessage(data) {
125
- const { stream, connection } = data;
126
- const log = connection.log.newScope('fetch');
103
+ async handleMessage(stream) {
104
+ const log = stream.log.newScope('fetch');
127
105
  const signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT);
128
- try {
129
- const pb = pbStream(stream);
130
- const request = await pb.read(FetchRequest, {
131
- signal
132
- });
133
- let response;
134
- const key = uint8arrayToString(request.identifier);
135
- const lookup = this._getLookupFunction(key);
136
- if (lookup == null) {
137
- log.trace('sending status ERROR for %m', request.identifier);
138
- const errMsg = uint8arrayFromString('No lookup function registered for key');
139
- response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
140
- }
141
- else {
142
- log.trace('lookup data with identifier %s', lookup.prefix);
143
- try {
144
- const data = await lookup.fn(request.identifier);
145
- if (data == null) {
146
- log.trace('sending status NOT_FOUND for %m', request.identifier);
147
- response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) };
148
- }
149
- else {
150
- log.trace('sending status OK for %m', request.identifier);
151
- response = { status: FetchResponse.StatusCode.OK, data };
152
- }
106
+ const pb = pbStream(stream);
107
+ const request = await pb.read(FetchRequest, {
108
+ signal
109
+ });
110
+ let response;
111
+ const key = uint8arrayToString(request.identifier);
112
+ const lookup = this._getLookupFunction(key);
113
+ if (lookup == null) {
114
+ log.trace('sending status ERROR for %m', request.identifier);
115
+ const errMsg = uint8arrayFromString('No lookup function registered for key');
116
+ response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
117
+ }
118
+ else {
119
+ log.trace('lookup data with identifier %s', lookup.prefix);
120
+ try {
121
+ const data = await lookup.fn(request.identifier);
122
+ if (data == null) {
123
+ log.trace('sending status NOT_FOUND for %m', request.identifier);
124
+ response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) };
153
125
  }
154
- catch (err) {
155
- log.error('error during lookup of %m - %e', request.identifier, err);
156
- const errMsg = uint8arrayFromString(err.message);
157
- response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
126
+ else {
127
+ log.trace('sending status OK for %m', request.identifier);
128
+ response = { status: FetchResponse.StatusCode.OK, data };
158
129
  }
159
130
  }
160
- await pb.write(response, FetchResponse, {
161
- signal
162
- });
163
- await pb.unwrap().close({
164
- signal
165
- });
166
- }
167
- catch (err) {
168
- log.error('error answering fetch request - %e', err);
169
- stream.abort(err);
131
+ catch (err) {
132
+ log.error('error during lookup of %m - %e', request.identifier, err);
133
+ const errMsg = uint8arrayFromString(err.message);
134
+ response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
135
+ }
170
136
  }
137
+ await pb.write(response, FetchResponse, {
138
+ signal
139
+ });
140
+ await stream.close({
141
+ signal
142
+ });
171
143
  }
172
144
  /**
173
145
  * Given a key, finds the appropriate function for looking up its corresponding value, based on
@@ -1 +1 @@
1
- {"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAC1G,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC5C,OAAO,EAAE,UAAU,IAAI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",
3
+ "version": "4.0.0-55b7e5fea",
4
4
  "description": "Implementation of the Fetch Protocol",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/protocol-fetch#readme",
@@ -45,22 +45,20 @@
45
45
  "doc-check": "aegir doc-check"
46
46
  },
47
47
  "dependencies": {
48
- "@libp2p/interface": "^2.11.0",
49
- "@libp2p/interface-internal": "^2.3.19",
50
- "it-protobuf-stream": "^2.0.2",
48
+ "@libp2p/interface": "3.0.0-55b7e5fea",
49
+ "@libp2p/interface-internal": "3.0.0-55b7e5fea",
50
+ "@libp2p/utils": "7.0.0-55b7e5fea",
51
51
  "main-event": "^1.0.1",
52
- "protons-runtime": "^5.5.0",
52
+ "protons-runtime": "^5.6.0",
53
53
  "uint8arraylist": "^2.4.8",
54
54
  "uint8arrays": "^5.1.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@libp2p/crypto": "^5.1.8",
58
- "@libp2p/logger": "^5.2.0",
59
- "@libp2p/peer-id": "^5.1.9",
60
- "aegir": "^47.0.14",
61
- "it-pair": "^2.0.6",
62
- "protons": "^7.6.1",
63
- "sinon": "^20.0.0",
57
+ "@libp2p/crypto": "5.1.9-55b7e5fea",
58
+ "@libp2p/peer-id": "6.0.0-55b7e5fea",
59
+ "aegir": "^47.0.22",
60
+ "protons": "^7.7.0",
61
+ "sinon": "^21.0.0",
64
62
  "sinon-ts": "^2.0.0"
65
63
  },
66
64
  "sideEffects": false
package/src/fetch.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { AbortError, InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface'
2
- import { pbStream } from 'it-protobuf-stream'
1
+ import { InvalidMessageError, InvalidParametersError, ProtocolError } from '@libp2p/interface'
2
+ import { pbStream } from '@libp2p/utils'
3
3
  import { setMaxListeners } from 'main-event'
4
4
  import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
5
5
  import { toString as uint8arrayToString } from 'uint8arrays/to-string'
6
6
  import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
7
7
  import { FetchRequest, FetchResponse } from './pb/proto.js'
8
8
  import type { Fetch as FetchInterface, FetchComponents, FetchInit, LookupFunction } from './index.js'
9
- import type { AbortOptions, Stream, PeerId, Startable, IncomingStreamData } from '@libp2p/interface'
9
+ import type { AbortOptions, Stream, PeerId, Startable } from '@libp2p/interface'
10
10
 
11
11
  const DEFAULT_TIMEOUT = 10_000
12
12
 
@@ -28,24 +28,15 @@ export class Fetch implements Startable, FetchInterface {
28
28
  this.components = components
29
29
  this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
30
30
  this.lookupFunctions = new Map() // Maps key prefix to value lookup function
31
- this.handleMessage = this.handleMessage.bind(this)
32
31
  this.init = init
32
+
33
+ this.handleMessage = this.handleMessage.bind(this)
33
34
  }
34
35
 
35
36
  readonly [Symbol.toStringTag] = '@libp2p/fetch'
36
37
 
37
38
  async start (): Promise<void> {
38
- await this.components.registrar.handle(this.protocol, (data) => {
39
- const log = data.connection.log.newScope('fetch')
40
-
41
- void this.handleMessage(data)
42
- .then(async () => {
43
- await data.stream.close()
44
- })
45
- .catch(err => {
46
- log.error('error handling message - %e', err)
47
- })
48
- }, {
39
+ await this.components.registrar.handle(this.protocol, this.handleMessage, {
49
40
  maxInboundStreams: this.init.maxInboundStreams,
50
41
  maxOutboundStreams: this.init.maxOutboundStreams
51
42
  })
@@ -69,33 +60,25 @@ export class Fetch implements Startable, FetchInterface {
69
60
  key = uint8arrayFromString(key)
70
61
  }
71
62
 
72
- const connection = await this.components.connectionManager.openConnection(peer, options)
73
- const log = connection.log.newScope('fetch')
74
- let signal = options.signal
75
- let stream: Stream | undefined
76
- let onAbort = (): void => {}
77
-
78
63
  // create a timeout if no abort signal passed
79
- if (signal == null) {
64
+ if (options.signal == null) {
80
65
  const timeout = this.init.timeout ?? DEFAULT_TIMEOUT
81
- log.trace('using default timeout of %d ms', timeout)
82
- signal = AbortSignal.timeout(timeout)
83
-
66
+ const signal = AbortSignal.timeout(timeout)
84
67
  setMaxListeners(Infinity, signal)
85
- }
86
68
 
87
- try {
88
- stream = await connection.newStream(this.protocol, {
69
+ options = {
70
+ ...options,
89
71
  signal
90
- })
91
-
92
- onAbort = () => {
93
- stream?.abort(new AbortError())
94
72
  }
73
+ }
74
+
75
+ let stream: Stream | undefined
95
76
 
96
- // make stream abortable
97
- signal.addEventListener('abort', onAbort, { once: true })
77
+ try {
78
+ const connection = await this.components.connectionManager.openConnection(peer, options)
79
+ stream = await connection.newStream(this.protocol, options)
98
80
 
81
+ const log = stream.log.newScope('fetch')
99
82
  log.trace('fetch %m', key)
100
83
 
101
84
  const pb = pbStream(stream)
@@ -104,7 +87,8 @@ export class Fetch implements Startable, FetchInterface {
104
87
  }, FetchRequest, options)
105
88
 
106
89
  const response = await pb.read(FetchResponse, options)
107
- await pb.unwrap().close(options)
90
+
91
+ await stream.close(options)
108
92
 
109
93
  switch (response.status) {
110
94
  case (FetchResponse.StatusCode.OK): {
@@ -128,11 +112,6 @@ export class Fetch implements Startable, FetchInterface {
128
112
  } catch (err: any) {
129
113
  stream?.abort(err)
130
114
  throw err
131
- } finally {
132
- signal.removeEventListener('abort', onAbort)
133
- if (stream != null) {
134
- await stream.close()
135
- }
136
115
  }
137
116
  }
138
117
 
@@ -141,57 +120,51 @@ export class Fetch implements Startable, FetchInterface {
141
120
  * responds based on looking up the key in the request via the lookup callback that corresponds
142
121
  * to the key's prefix.
143
122
  */
144
- async handleMessage (data: IncomingStreamData): Promise<void> {
145
- const { stream, connection } = data
146
- const log = connection.log.newScope('fetch')
123
+ async handleMessage (stream: Stream): Promise<void> {
124
+ const log = stream.log.newScope('fetch')
147
125
  const signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
148
126
 
149
- try {
150
- const pb = pbStream(stream)
151
- const request = await pb.read(FetchRequest, {
152
- signal
153
- })
127
+ const pb = pbStream(stream)
128
+ const request = await pb.read(FetchRequest, {
129
+ signal
130
+ })
154
131
 
155
- let response: FetchResponse
156
- const key = uint8arrayToString(request.identifier)
132
+ let response: FetchResponse
133
+ const key = uint8arrayToString(request.identifier)
157
134
 
158
- const lookup = this._getLookupFunction(key)
135
+ const lookup = this._getLookupFunction(key)
159
136
 
160
- if (lookup == null) {
161
- log.trace('sending status ERROR for %m', request.identifier)
162
- const errMsg = uint8arrayFromString('No lookup function registered for key')
163
- response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
164
- } else {
165
- log.trace('lookup data with identifier %s', lookup.prefix)
166
-
167
- try {
168
- const data = await lookup.fn(request.identifier)
169
-
170
- if (data == null) {
171
- log.trace('sending status NOT_FOUND for %m', request.identifier)
172
- response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
173
- } else {
174
- log.trace('sending status OK for %m', request.identifier)
175
- response = { status: FetchResponse.StatusCode.OK, data }
176
- }
177
- } catch (err: any) {
178
- log.error('error during lookup of %m - %e', request.identifier, err)
179
- const errMsg = uint8arrayFromString(err.message)
180
- response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
137
+ if (lookup == null) {
138
+ log.trace('sending status ERROR for %m', request.identifier)
139
+ const errMsg = uint8arrayFromString('No lookup function registered for key')
140
+ response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
141
+ } else {
142
+ log.trace('lookup data with identifier %s', lookup.prefix)
143
+
144
+ try {
145
+ const data = await lookup.fn(request.identifier)
146
+
147
+ if (data == null) {
148
+ log.trace('sending status NOT_FOUND for %m', request.identifier)
149
+ response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
150
+ } else {
151
+ log.trace('sending status OK for %m', request.identifier)
152
+ response = { status: FetchResponse.StatusCode.OK, data }
181
153
  }
154
+ } catch (err: any) {
155
+ log.error('error during lookup of %m - %e', request.identifier, err)
156
+ const errMsg = uint8arrayFromString(err.message)
157
+ response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
182
158
  }
159
+ }
183
160
 
184
- await pb.write(response, FetchResponse, {
185
- signal
186
- })
161
+ await pb.write(response, FetchResponse, {
162
+ signal
163
+ })
187
164
 
188
- await pb.unwrap().close({
189
- signal
190
- })
191
- } catch (err: any) {
192
- log.error('error answering fetch request - %e', err)
193
- stream.abort(err)
194
- }
165
+ await stream.close({
166
+ signal
167
+ })
195
168
  }
196
169
 
197
170
  /**
@@ -1,12 +0,0 @@
1
- {
2
- "Fetch": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.Fetch.html",
3
- ".:Fetch": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.Fetch.html",
4
- "FetchComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.FetchComponents.html",
5
- ".:FetchComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.FetchComponents.html",
6
- "FetchInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.FetchInit.html",
7
- ".:FetchInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.FetchInit.html",
8
- "LookupFunction": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.LookupFunction.html",
9
- ".:LookupFunction": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_fetch.LookupFunction.html",
10
- "fetch": "https://libp2p.github.io/js-libp2p/functions/_libp2p_fetch.fetch.html",
11
- ".:fetch": "https://libp2p.github.io/js-libp2p/functions/_libp2p_fetch.fetch.html"
12
- }