@libp2p/fetch 3.0.21-cfe2be4c9 → 3.0.22
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/index.min.js +1 -1
- package/dist/index.min.js.map +3 -3
- package/dist/src/fetch.d.ts +0 -1
- package/dist/src/fetch.d.ts.map +1 -1
- package/dist/src/fetch.js +18 -18
- package/dist/src/fetch.js.map +1 -1
- package/dist/src/index.d.ts +1 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/typedoc-urls.json +12 -0
- package/package.json +6 -6
- package/src/fetch.ts +20 -20
- package/src/index.ts +1 -2
package/dist/src/fetch.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export declare class Fetch implements Startable, FetchInterface {
|
|
|
12
12
|
private readonly lookupFunctions;
|
|
13
13
|
private started;
|
|
14
14
|
private readonly init;
|
|
15
|
-
private readonly log;
|
|
16
15
|
constructor(components: FetchComponents, init?: FetchInit);
|
|
17
16
|
readonly [Symbol.toStringTag] = "@libp2p/fetch";
|
|
18
17
|
start(): Promise<void>;
|
package/dist/src/fetch.d.ts.map
CHANGED
|
@@ -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,
|
|
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,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,mBAAkB;IAEzC,KAAK,IAAK,OAAO,CAAC,IAAI,CAAC;IAkBvB,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;IAwEjH;;;;OAIG;IACG,aAAa,CAAE,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;IAqD7D;;;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
|
@@ -5,7 +5,7 @@ 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
|
-
const DEFAULT_TIMEOUT =
|
|
8
|
+
const DEFAULT_TIMEOUT = 10_000;
|
|
9
9
|
/**
|
|
10
10
|
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
11
11
|
* Developers can register one or more lookup function for retrieving the value corresponding to
|
|
@@ -18,9 +18,7 @@ export class Fetch {
|
|
|
18
18
|
lookupFunctions;
|
|
19
19
|
started;
|
|
20
20
|
init;
|
|
21
|
-
log;
|
|
22
21
|
constructor(components, init = {}) {
|
|
23
|
-
this.log = components.logger.forComponent('libp2p:fetch');
|
|
24
22
|
this.started = false;
|
|
25
23
|
this.components = components;
|
|
26
24
|
this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`;
|
|
@@ -31,12 +29,13 @@ export class Fetch {
|
|
|
31
29
|
[Symbol.toStringTag] = '@libp2p/fetch';
|
|
32
30
|
async start() {
|
|
33
31
|
await this.components.registrar.handle(this.protocol, (data) => {
|
|
32
|
+
const log = data.connection.log.newScope('fetch');
|
|
34
33
|
void this.handleMessage(data)
|
|
35
34
|
.then(async () => {
|
|
36
35
|
await data.stream.close();
|
|
37
36
|
})
|
|
38
37
|
.catch(err => {
|
|
39
|
-
|
|
38
|
+
log.error('error handling message - %e', err);
|
|
40
39
|
});
|
|
41
40
|
}, {
|
|
42
41
|
maxInboundStreams: this.init.maxInboundStreams,
|
|
@@ -58,15 +57,15 @@ export class Fetch {
|
|
|
58
57
|
if (typeof key === 'string') {
|
|
59
58
|
key = uint8arrayFromString(key);
|
|
60
59
|
}
|
|
61
|
-
this.log.trace('dialing %s to %p', this.protocol, peer);
|
|
62
60
|
const connection = await this.components.connectionManager.openConnection(peer, options);
|
|
61
|
+
const log = connection.log.newScope('fetch');
|
|
63
62
|
let signal = options.signal;
|
|
64
63
|
let stream;
|
|
65
64
|
let onAbort = () => { };
|
|
66
65
|
// create a timeout if no abort signal passed
|
|
67
66
|
if (signal == null) {
|
|
68
67
|
const timeout = this.init.timeout ?? DEFAULT_TIMEOUT;
|
|
69
|
-
|
|
68
|
+
log.trace('using default timeout of %d ms', timeout);
|
|
70
69
|
signal = AbortSignal.timeout(timeout);
|
|
71
70
|
setMaxListeners(Infinity, signal);
|
|
72
71
|
}
|
|
@@ -79,7 +78,7 @@ export class Fetch {
|
|
|
79
78
|
};
|
|
80
79
|
// make stream abortable
|
|
81
80
|
signal.addEventListener('abort', onAbort, { once: true });
|
|
82
|
-
|
|
81
|
+
log.trace('fetch %m', key);
|
|
83
82
|
const pb = pbStream(stream);
|
|
84
83
|
await pb.write({
|
|
85
84
|
identifier: key
|
|
@@ -88,20 +87,20 @@ export class Fetch {
|
|
|
88
87
|
await pb.unwrap().close(options);
|
|
89
88
|
switch (response.status) {
|
|
90
89
|
case (FetchResponse.StatusCode.OK): {
|
|
91
|
-
|
|
90
|
+
log.trace('received status OK for %m', key);
|
|
92
91
|
return response.data;
|
|
93
92
|
}
|
|
94
93
|
case (FetchResponse.StatusCode.NOT_FOUND): {
|
|
95
|
-
|
|
94
|
+
log('received status NOT_FOUND for %m', key);
|
|
96
95
|
return;
|
|
97
96
|
}
|
|
98
97
|
case (FetchResponse.StatusCode.ERROR): {
|
|
99
|
-
|
|
98
|
+
log('received status ERROR for %m', key);
|
|
100
99
|
const errMsg = uint8arrayToString(response.data);
|
|
101
100
|
throw new ProtocolError('Error in fetch protocol response: ' + errMsg);
|
|
102
101
|
}
|
|
103
102
|
default: {
|
|
104
|
-
|
|
103
|
+
log('received status unknown for %m', key);
|
|
105
104
|
throw new InvalidMessageError('Unknown response status');
|
|
106
105
|
}
|
|
107
106
|
}
|
|
@@ -123,7 +122,8 @@ export class Fetch {
|
|
|
123
122
|
* to the key's prefix.
|
|
124
123
|
*/
|
|
125
124
|
async handleMessage(data) {
|
|
126
|
-
const { stream } = data;
|
|
125
|
+
const { stream, connection } = data;
|
|
126
|
+
const log = connection.log.newScope('fetch');
|
|
127
127
|
const signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT);
|
|
128
128
|
try {
|
|
129
129
|
const pb = pbStream(stream);
|
|
@@ -134,25 +134,25 @@ export class Fetch {
|
|
|
134
134
|
const key = uint8arrayToString(request.identifier);
|
|
135
135
|
const lookup = this._getLookupFunction(key);
|
|
136
136
|
if (lookup == null) {
|
|
137
|
-
|
|
137
|
+
log.trace('sending status ERROR for %m', request.identifier);
|
|
138
138
|
const errMsg = uint8arrayFromString('No lookup function registered for key');
|
|
139
139
|
response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
|
|
140
140
|
}
|
|
141
141
|
else {
|
|
142
|
-
|
|
142
|
+
log.trace('lookup data with identifier %s', lookup.prefix);
|
|
143
143
|
try {
|
|
144
144
|
const data = await lookup.fn(request.identifier);
|
|
145
145
|
if (data == null) {
|
|
146
|
-
|
|
146
|
+
log.trace('sending status NOT_FOUND for %m', request.identifier);
|
|
147
147
|
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) };
|
|
148
148
|
}
|
|
149
149
|
else {
|
|
150
|
-
|
|
150
|
+
log.trace('sending status OK for %m', request.identifier);
|
|
151
151
|
response = { status: FetchResponse.StatusCode.OK, data };
|
|
152
152
|
}
|
|
153
153
|
}
|
|
154
154
|
catch (err) {
|
|
155
|
-
|
|
155
|
+
log.error('error during lookup of %m - %e', request.identifier, err);
|
|
156
156
|
const errMsg = uint8arrayFromString(err.message);
|
|
157
157
|
response = { status: FetchResponse.StatusCode.ERROR, data: errMsg };
|
|
158
158
|
}
|
|
@@ -165,7 +165,7 @@ export class Fetch {
|
|
|
165
165
|
});
|
|
166
166
|
}
|
|
167
167
|
catch (err) {
|
|
168
|
-
|
|
168
|
+
log.error('error answering fetch request - %e', err);
|
|
169
169
|
stream.abort(err);
|
|
170
170
|
}
|
|
171
171
|
}
|
package/dist/src/fetch.js.map
CHANGED
|
@@ -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,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAChE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAI3D,MAAM,eAAe,GAAG,
|
|
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,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AACtE,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAChE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAI3D,MAAM,eAAe,GAAG,MAAM,CAAA;AAE9B;;;;;GAKG;AACH,MAAM,OAAO,KAAK;IACA,QAAQ,CAAQ;IACf,UAAU,CAAiB;IAC3B,eAAe,CAA6B;IACrD,O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AM,CAAC,MAAM,CAAC,CAAA;IACrC,CAAC;CACF"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
* })
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
|
-
import type { AbortOptions,
|
|
44
|
+
import type { AbortOptions, PeerId } from '@libp2p/interface';
|
|
45
45
|
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal';
|
|
46
46
|
export interface FetchInit {
|
|
47
47
|
protocolPrefix?: string;
|
|
@@ -65,7 +65,6 @@ export interface LookupFunction {
|
|
|
65
65
|
export interface FetchComponents {
|
|
66
66
|
registrar: Registrar;
|
|
67
67
|
connectionManager: ConnectionManager;
|
|
68
|
-
logger: ComponentLogger;
|
|
69
68
|
}
|
|
70
69
|
export interface Fetch {
|
|
71
70
|
/**
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAA;AAE9E,MAAM,WAAW,SAAS;IACxB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAE3B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,CAAC,GAAG,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAA;CACnD;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,SAAS,CAAA;IACpB,iBAAiB,EAAE,iBAAiB,CAAA;CACrC;AAED,MAAM,WAAW,KAAK;IACpB;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CAAA;IAEtG;;;;;;;;;;OAUG;IACH,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,IAAI,CAAA;IAEpE;;;;;;;;;;OAUG;IACH,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,cAAc,GAAG,IAAI,CAAA;CACxE;AAED,wBAAgB,KAAK,CAAE,IAAI,GAAE,SAAc,GAAG,CAAC,UAAU,EAAE,eAAe,KAAK,KAAK,CAEnF"}
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,EAAE,KAAK,IAAI,UAAU,EAAE,MAAM,YAAY,CAAA;AAgEhD,MAAM,UAAU,KAAK,CAAE,OAAkB,EAAE;IACzC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,UAAU,CAAC,UAAU,EAAE,IAAI,CAAC,CAAA;AACzD,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/fetch",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.22",
|
|
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,8 +45,8 @@
|
|
|
45
45
|
"doc-check": "aegir doc-check"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@libp2p/interface": "2.
|
|
49
|
-
"@libp2p/interface-internal": "2.3.
|
|
48
|
+
"@libp2p/interface": "^2.11.0",
|
|
49
|
+
"@libp2p/interface-internal": "^2.3.19",
|
|
50
50
|
"it-protobuf-stream": "^2.0.2",
|
|
51
51
|
"main-event": "^1.0.1",
|
|
52
52
|
"protons-runtime": "^5.5.0",
|
|
@@ -54,9 +54,9 @@
|
|
|
54
54
|
"uint8arrays": "^5.1.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@libp2p/crypto": "5.1.
|
|
58
|
-
"@libp2p/logger": "5.
|
|
59
|
-
"@libp2p/peer-id": "5.1.
|
|
57
|
+
"@libp2p/crypto": "^5.1.8",
|
|
58
|
+
"@libp2p/logger": "^5.2.0",
|
|
59
|
+
"@libp2p/peer-id": "^5.1.9",
|
|
60
60
|
"aegir": "^47.0.14",
|
|
61
61
|
"it-pair": "^2.0.6",
|
|
62
62
|
"protons": "^7.6.1",
|
package/src/fetch.ts
CHANGED
|
@@ -6,9 +6,9 @@ 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,
|
|
9
|
+
import type { AbortOptions, Stream, PeerId, Startable, IncomingStreamData } from '@libp2p/interface'
|
|
10
10
|
|
|
11
|
-
const DEFAULT_TIMEOUT =
|
|
11
|
+
const DEFAULT_TIMEOUT = 10_000
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A simple libp2p protocol for requesting a value corresponding to a key from a peer.
|
|
@@ -22,10 +22,8 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
22
22
|
private readonly lookupFunctions: Map<string, LookupFunction>
|
|
23
23
|
private started: boolean
|
|
24
24
|
private readonly init: FetchInit
|
|
25
|
-
private readonly log: Logger
|
|
26
25
|
|
|
27
26
|
constructor (components: FetchComponents, init: FetchInit = {}) {
|
|
28
|
-
this.log = components.logger.forComponent('libp2p:fetch')
|
|
29
27
|
this.started = false
|
|
30
28
|
this.components = components
|
|
31
29
|
this.protocol = `/${init.protocolPrefix ?? 'libp2p'}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
|
|
@@ -38,12 +36,14 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
38
36
|
|
|
39
37
|
async start (): Promise<void> {
|
|
40
38
|
await this.components.registrar.handle(this.protocol, (data) => {
|
|
39
|
+
const log = data.connection.log.newScope('fetch')
|
|
40
|
+
|
|
41
41
|
void this.handleMessage(data)
|
|
42
42
|
.then(async () => {
|
|
43
43
|
await data.stream.close()
|
|
44
44
|
})
|
|
45
45
|
.catch(err => {
|
|
46
|
-
|
|
46
|
+
log.error('error handling message - %e', err)
|
|
47
47
|
})
|
|
48
48
|
}, {
|
|
49
49
|
maxInboundStreams: this.init.maxInboundStreams,
|
|
@@ -69,9 +69,8 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
69
69
|
key = uint8arrayFromString(key)
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
this.log.trace('dialing %s to %p', this.protocol, peer)
|
|
73
|
-
|
|
74
72
|
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
73
|
+
const log = connection.log.newScope('fetch')
|
|
75
74
|
let signal = options.signal
|
|
76
75
|
let stream: Stream | undefined
|
|
77
76
|
let onAbort = (): void => {}
|
|
@@ -79,7 +78,7 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
79
78
|
// create a timeout if no abort signal passed
|
|
80
79
|
if (signal == null) {
|
|
81
80
|
const timeout = this.init.timeout ?? DEFAULT_TIMEOUT
|
|
82
|
-
|
|
81
|
+
log.trace('using default timeout of %d ms', timeout)
|
|
83
82
|
signal = AbortSignal.timeout(timeout)
|
|
84
83
|
|
|
85
84
|
setMaxListeners(Infinity, signal)
|
|
@@ -97,7 +96,7 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
97
96
|
// make stream abortable
|
|
98
97
|
signal.addEventListener('abort', onAbort, { once: true })
|
|
99
98
|
|
|
100
|
-
|
|
99
|
+
log.trace('fetch %m', key)
|
|
101
100
|
|
|
102
101
|
const pb = pbStream(stream)
|
|
103
102
|
await pb.write({
|
|
@@ -109,20 +108,20 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
109
108
|
|
|
110
109
|
switch (response.status) {
|
|
111
110
|
case (FetchResponse.StatusCode.OK): {
|
|
112
|
-
|
|
111
|
+
log.trace('received status OK for %m', key)
|
|
113
112
|
return response.data
|
|
114
113
|
}
|
|
115
114
|
case (FetchResponse.StatusCode.NOT_FOUND): {
|
|
116
|
-
|
|
115
|
+
log('received status NOT_FOUND for %m', key)
|
|
117
116
|
return
|
|
118
117
|
}
|
|
119
118
|
case (FetchResponse.StatusCode.ERROR): {
|
|
120
|
-
|
|
119
|
+
log('received status ERROR for %m', key)
|
|
121
120
|
const errMsg = uint8arrayToString(response.data)
|
|
122
121
|
throw new ProtocolError('Error in fetch protocol response: ' + errMsg)
|
|
123
122
|
}
|
|
124
123
|
default: {
|
|
125
|
-
|
|
124
|
+
log('received status unknown for %m', key)
|
|
126
125
|
throw new InvalidMessageError('Unknown response status')
|
|
127
126
|
}
|
|
128
127
|
}
|
|
@@ -143,7 +142,8 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
143
142
|
* to the key's prefix.
|
|
144
143
|
*/
|
|
145
144
|
async handleMessage (data: IncomingStreamData): Promise<void> {
|
|
146
|
-
const { stream } = data
|
|
145
|
+
const { stream, connection } = data
|
|
146
|
+
const log = connection.log.newScope('fetch')
|
|
147
147
|
const signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
|
|
148
148
|
|
|
149
149
|
try {
|
|
@@ -158,24 +158,24 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
158
158
|
const lookup = this._getLookupFunction(key)
|
|
159
159
|
|
|
160
160
|
if (lookup == null) {
|
|
161
|
-
|
|
161
|
+
log.trace('sending status ERROR for %m', request.identifier)
|
|
162
162
|
const errMsg = uint8arrayFromString('No lookup function registered for key')
|
|
163
163
|
response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
|
|
164
164
|
} else {
|
|
165
|
-
|
|
165
|
+
log.trace('lookup data with identifier %s', lookup.prefix)
|
|
166
166
|
|
|
167
167
|
try {
|
|
168
168
|
const data = await lookup.fn(request.identifier)
|
|
169
169
|
|
|
170
170
|
if (data == null) {
|
|
171
|
-
|
|
171
|
+
log.trace('sending status NOT_FOUND for %m', request.identifier)
|
|
172
172
|
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
|
|
173
173
|
} else {
|
|
174
|
-
|
|
174
|
+
log.trace('sending status OK for %m', request.identifier)
|
|
175
175
|
response = { status: FetchResponse.StatusCode.OK, data }
|
|
176
176
|
}
|
|
177
177
|
} catch (err: any) {
|
|
178
|
-
|
|
178
|
+
log.error('error during lookup of %m - %e', request.identifier, err)
|
|
179
179
|
const errMsg = uint8arrayFromString(err.message)
|
|
180
180
|
response = { status: FetchResponse.StatusCode.ERROR, data: errMsg }
|
|
181
181
|
}
|
|
@@ -189,7 +189,7 @@ export class Fetch implements Startable, FetchInterface {
|
|
|
189
189
|
signal
|
|
190
190
|
})
|
|
191
191
|
} catch (err: any) {
|
|
192
|
-
|
|
192
|
+
log.error('error answering fetch request - %e', err)
|
|
193
193
|
stream.abort(err)
|
|
194
194
|
}
|
|
195
195
|
}
|
package/src/index.ts
CHANGED
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
*/
|
|
44
44
|
|
|
45
45
|
import { Fetch as FetchClass } from './fetch.js'
|
|
46
|
-
import type { AbortOptions,
|
|
46
|
+
import type { AbortOptions, PeerId } from '@libp2p/interface'
|
|
47
47
|
import type { ConnectionManager, Registrar } from '@libp2p/interface-internal'
|
|
48
48
|
|
|
49
49
|
export interface FetchInit {
|
|
@@ -71,7 +71,6 @@ export interface LookupFunction {
|
|
|
71
71
|
export interface FetchComponents {
|
|
72
72
|
registrar: Registrar
|
|
73
73
|
connectionManager: ConnectionManager
|
|
74
|
-
logger: ComponentLogger
|
|
75
74
|
}
|
|
76
75
|
|
|
77
76
|
export interface Fetch {
|