Skip to content

Commit 102c8f1

Browse files
committed
refactor!: move fetch into seperate package (libp2p#1913)
1 parent 66fbdb9 commit 102c8f1

17 files changed

+135
-95
lines changed

packages/libp2p/src/errors.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,3 @@ export enum messages {
77
ERR_PROTECTOR_REQUIRED = 'Private network is enforced, but no protector was provided',
88
NOT_FOUND = 'Not found'
99
}
10-
11-
export enum codes {
12-
DHT_DISABLED = 'ERR_DHT_DISABLED',
13-
ERR_PUBSUB_DISABLED = 'ERR_PUBSUB_DISABLED',
14-
PUBSUB_NOT_STARTED = 'ERR_PUBSUB_NOT_STARTED',
15-
DHT_NOT_STARTED = 'ERR_DHT_NOT_STARTED',
16-
CONN_ENCRYPTION_REQUIRED = 'ERR_CONN_ENCRYPTION_REQUIRED',
17-
ERR_TRANSPORTS_REQUIRED = 'ERR_TRANSPORTS_REQUIRED',
18-
ERR_PROTECTOR_REQUIRED = 'ERR_PROTECTOR_REQUIRED',
19-
ERR_PEER_DIAL_INTERCEPTED = 'ERR_PEER_DIAL_INTERCEPTED',
20-
ERR_CONNECTION_INTERCEPTED = 'ERR_CONNECTION_INTERCEPTED',
21-
ERR_INVALID_PROTOCOLS_FOR_STREAM = 'ERR_INVALID_PROTOCOLS_FOR_STREAM',
22-
ERR_CONNECTION_ENDED = 'ERR_CONNECTION_ENDED',
23-
ERR_CONNECTION_FAILED = 'ERR_CONNECTION_FAILED',
24-
ERR_NODE_NOT_STARTED = 'ERR_NODE_NOT_STARTED',
25-
ERR_ALREADY_ABORTED = 'ERR_ALREADY_ABORTED',
26-
ERR_TOO_MANY_ADDRESSES = 'ERR_TOO_MANY_ADDRESSES',
27-
ERR_NO_VALID_ADDRESSES = 'ERR_NO_VALID_ADDRESSES',
28-
ERR_RELAYED_DIAL = 'ERR_RELAYED_DIAL',
29-
ERR_DIALED_SELF = 'ERR_DIALED_SELF',
30-
ERR_DISCOVERED_SELF = 'ERR_DISCOVERED_SELF',
31-
ERR_DUPLICATE_TRANSPORT = 'ERR_DUPLICATE_TRANSPORT',
32-
ERR_ENCRYPTION_FAILED = 'ERR_ENCRYPTION_FAILED',
33-
ERR_HOP_REQUEST_FAILED = 'ERR_HOP_REQUEST_FAILED',
34-
ERR_INVALID_KEY = 'ERR_INVALID_KEY',
35-
ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE',
36-
ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS',
37-
ERR_INVALID_PEER = 'ERR_INVALID_PEER',
38-
ERR_MUXER_UNAVAILABLE = 'ERR_MUXER_UNAVAILABLE',
39-
ERR_NOT_FOUND = 'ERR_NOT_FOUND',
40-
ERR_TRANSPORT_UNAVAILABLE = 'ERR_TRANSPORT_UNAVAILABLE',
41-
ERR_TRANSPORT_DIAL_FAILED = 'ERR_TRANSPORT_DIAL_FAILED',
42-
ERR_UNSUPPORTED_PROTOCOL = 'ERR_UNSUPPORTED_PROTOCOL',
43-
ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED = 'ERR_PROTOCOL_HANDLER_ALREADY_REGISTERED',
44-
ERR_INVALID_MULTIADDR = 'ERR_INVALID_MULTIADDR',
45-
ERR_SIGNATURE_NOT_VALID = 'ERR_SIGNATURE_NOT_VALID',
46-
ERR_FIND_SELF = 'ERR_FIND_SELF',
47-
ERR_NO_ROUTERS_AVAILABLE = 'ERR_NO_ROUTERS_AVAILABLE',
48-
ERR_CONNECTION_NOT_MULTIPLEXED = 'ERR_CONNECTION_NOT_MULTIPLEXED',
49-
ERR_NO_DIAL_TOKENS = 'ERR_NO_DIAL_TOKENS',
50-
ERR_INVALID_CMS = 'ERR_INVALID_CMS',
51-
ERR_MISSING_KEYS = 'ERR_MISSING_KEYS',
52-
ERR_NO_KEY = 'ERR_NO_KEY',
53-
ERR_INVALID_KEY_NAME = 'ERR_INVALID_KEY_NAME',
54-
ERR_INVALID_KEY_TYPE = 'ERR_INVALID_KEY_TYPE',
55-
ERR_KEY_ALREADY_EXISTS = 'ERR_KEY_ALREADY_EXISTS',
56-
ERR_INVALID_KEY_SIZE = 'ERR_INVALID_KEY_SIZE',
57-
ERR_KEY_NOT_FOUND = 'ERR_KEY_NOT_FOUND',
58-
ERR_OLD_KEY_NAME_INVALID = 'ERR_OLD_KEY_NAME_INVALID',
59-
ERR_NEW_KEY_NAME_INVALID = 'ERR_NEW_KEY_NAME_INVALID',
60-
ERR_PASSWORD_REQUIRED = 'ERR_PASSWORD_REQUIRED',
61-
ERR_PEM_REQUIRED = 'ERR_PEM_REQUIRED',
62-
ERR_CANNOT_READ_KEY = 'ERR_CANNOT_READ_KEY',
63-
ERR_MISSING_PRIVATE_KEY = 'ERR_MISSING_PRIVATE_KEY',
64-
ERR_MISSING_PUBLIC_KEY = 'ERR_MISSING_PUBLIC_KEY',
65-
ERR_INVALID_OLD_PASS_TYPE = 'ERR_INVALID_OLD_PASS_TYPE',
66-
ERR_INVALID_NEW_PASS_TYPE = 'ERR_INVALID_NEW_PASS_TYPE',
67-
ERR_INVALID_PASS_LENGTH = 'ERR_INVALID_PASS_LENGTH',
68-
ERR_NOT_IMPLEMENTED = 'ERR_NOT_IMPLEMENTED',
69-
ERR_WRONG_PING_ACK = 'ERR_WRONG_PING_ACK',
70-
ERR_INVALID_RECORD = 'ERR_INVALID_RECORD',
71-
ERR_ALREADY_SUCCEEDED = 'ERR_ALREADY_SUCCEEDED',
72-
ERR_NO_HANDLER_FOR_PROTOCOL = 'ERR_NO_HANDLER_FOR_PROTOCOL',
73-
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
74-
ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS',
75-
ERR_CONNECTION_DENIED = 'ERR_CONNECTION_DENIED',
76-
ERR_TRANSFER_LIMIT_EXCEEDED = 'ERR_TRANSFER_LIMIT_EXCEEDED'
77-
}

packages/libp2p/test/configuration/protocol-prefix.node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-env mocha */
22

3+
import { type FetchService, fetchService } from '@libp2p/fetch'
34
import { expect } from 'aegir/chai'
45
import { pEvent } from 'p-event'
5-
import { type FetchService, fetchService } from '../../src/fetch/index.js'
66
import { identifyService } from '../../src/identify/index.js'
77
import { createLibp2p } from '../../src/index.js'
88
import { type PingService, pingService } from '../../src/ping/index.js'

packages/libp2p/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"include": [
77
"src",
88
"test"
9-
],
9+
, "../protocol-fetch/test/fetch.node.ts", "../protocol-fetch/test/index.spec.ts" ],
1010
"references": [
1111
{
1212
"path": "../crypto"

packages/protocol-autonat/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
}
3333
},
3434
"scripts": {
35-
"start": "node dist/src/main.js",
3635
"build": "aegir build",
3736
"test": "aegir test",
3837
"clean": "aegir clean",
@@ -48,8 +47,8 @@
4847
"dependencies": {
4948
"@libp2p/interface": "^0.1.2",
5049
"@libp2p/interface-internal": "^0.1.5",
51-
"@libp2p/peer-id": "^3.0.2" ,
52-
"@libp2p/peer-id-factory": "^3.0.4" ,
50+
"@libp2p/peer-id": "^3.0.2",
51+
"@libp2p/peer-id-factory": "^3.0.4",
5352
"@libp2p/logger": "^3.0.2",
5453
"@multiformats/multiaddr": "^12.1.5",
5554
"it-first": "^3.0.1",

packages/protocol-fetch/LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/protocol-fetch/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

packages/libp2p/src/fetch/README.md renamed to packages/protocol-fetch/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The fetch protocol is a simple protocol for requesting a value corresponding to
1616

1717
## Usage
1818

19-
```javascript
19+
```ts
2020
import { createLibp2p } from 'libp2p'
2121

2222
/**

packages/protocol-fetch/package.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "@libp2p/fetch",
3+
"version": "1.0.0",
4+
"description": "Implementation of the Fetch Protocol",
5+
"license": "Apache-2.0 OR MIT",
6+
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/protocol-fetch#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/libp2p/js-libp2p.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/libp2p/js-libp2p/issues"
13+
},
14+
"type": "module",
15+
"types": "./dist/src/index.d.ts",
16+
"files": [
17+
"src",
18+
"dist",
19+
"!dist/test",
20+
"!**/*.tsbuildinfo"
21+
],
22+
"exports": {
23+
".": {
24+
"types": "./dist/src/index.d.ts",
25+
"import": "./dist/src/index.js"
26+
}
27+
},
28+
"eslintConfig": {
29+
"extends": "ipfs",
30+
"parserOptions": {
31+
"sourceType": "module"
32+
}
33+
},
34+
"scripts": {
35+
"build": "aegir build",
36+
"test": "aegir test",
37+
"clean": "aegir clean",
38+
"generate": "protons ./src/pb/index.proto",
39+
"lint": "aegir lint",
40+
"test:chrome": "aegir test -t browser --cov",
41+
"test:chrome-webworker": "aegir test -t webworker",
42+
"test:firefox": "aegir test -t browser -- --browser firefox",
43+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
44+
"test:node": "aegir test -t node --cov",
45+
"dep-check": "aegir dep-check"
46+
},
47+
"dependencies": {
48+
"@libp2p/interface": "^0.1.4",
49+
"@libp2p/interface-internal": "^0.1.7",
50+
"@libp2p/logger": "^3.0.4",
51+
"it-first": "^3.0.3",
52+
"it-length-prefixed": "^9.0.3",
53+
"it-pipe": "^3.0.1",
54+
"protons-runtime": "^5.2.0",
55+
"uint8arraylist": "^2.4.3",
56+
"uint8arrays": "^4.0.6"
57+
},
58+
"devDependencies": {
59+
"@chainsafe/libp2p-noise": "^13.0.2",
60+
"@chainsafe/libp2p-yamux": "^5.0.0",
61+
"@libp2p/interface-compliance-tests": "^4.1.2",
62+
"@libp2p/peer-id-factory": "^3.0.6",
63+
"@libp2p/peer-store": "^9.0.7",
64+
"@libp2p/tcp": "^8.0.10",
65+
"aegir": "^40.0.8",
66+
"datastore-core": "^9.2.3",
67+
"delay": "^6.0.0",
68+
"libp2p": "^0.46.16",
69+
"sinon": "^17.0.1",
70+
"sinon-ts": "^2.0.0"
71+
}
72+
}

packages/libp2p/src/fetch/index.ts renamed to packages/protocol-fetch/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { setMaxListeners } from 'events'
2-
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
2+
import { CodeError, ERR_TIMEOUT, ERR_INVALID_MESSAGE, ERR_INVALID_PARAMETERS, ERR_KEY_ALREADY_EXISTS } from '@libp2p/interface/errors'
33
import { logger } from '@libp2p/logger'
44
import first from 'it-first'
55
import * as lp from 'it-length-prefixed'
66
import { pipe } from 'it-pipe'
77
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
88
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
9-
import { codes } from '../errors.js'
109
import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
1110
import { FetchRequest, FetchResponse } from './pb/proto.js'
1211
import type { AbortOptions } from '@libp2p/interface'
@@ -175,7 +174,7 @@ class DefaultFetchService implements Startable, FetchService {
175174
const buf = await first(source)
176175

177176
if (buf == null) {
178-
throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
177+
throw new CodeError('No data received', ERR_INVALID_MESSAGE)
179178
}
180179

181180
const response = FetchResponse.decode(buf)
@@ -192,11 +191,11 @@ class DefaultFetchService implements Startable, FetchService {
192191
case (FetchResponse.StatusCode.ERROR): {
193192
log('received status for %s error', key)
194193
const errmsg = uint8arrayToString(response.data)
195-
throw new CodeError('Error in fetch protocol response: ' + errmsg, codes.ERR_INVALID_PARAMETERS)
194+
throw new CodeError('Error in fetch protocol response: ' + errmsg, ERR_INVALID_PARAMETERS)
196195
}
197196
default: {
198197
log('received status for %s unknown', key)
199-
throw new CodeError('Unknown response status', codes.ERR_INVALID_MESSAGE)
198+
throw new CodeError('Unknown response status', ERR_INVALID_MESSAGE)
200199
}
201200
}
202201
}
@@ -227,7 +226,7 @@ class DefaultFetchService implements Startable, FetchService {
227226
const buf = await first(source)
228227

229228
if (buf == null) {
230-
throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
229+
throw new CodeError('No data received', ERR_INVALID_MESSAGE)
231230
}
232231

233232
// for await (const buf of source) {
@@ -283,7 +282,7 @@ class DefaultFetchService implements Startable, FetchService {
283282
*/
284283
registerLookupFunction (prefix: string, lookup: LookupFunction): void {
285284
if (this.lookupFunctions.has(prefix)) {
286-
throw new CodeError(`Fetch protocol handler for key prefix '${prefix}' already registered`, codes.ERR_KEY_ALREADY_EXISTS)
285+
throw new CodeError(`Fetch protocol handler for key prefix '${prefix}' already registered`, ERR_KEY_ALREADY_EXISTS)
287286
}
288287

289288
this.lookupFunctions.set(prefix, lookup)

packages/libp2p/test/fetch/fetch.node.ts renamed to packages/protocol-fetch/test/fetch.node.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/* eslint-env mocha */
22

33
import { yamux } from '@chainsafe/libp2p-yamux'
4-
import { mplex } from '@libp2p/mplex'
4+
import { type FetchService, fetchService } from '../src/index.js'
55
import { tcp } from '@libp2p/tcp'
66
import { expect } from 'aegir/chai'
7-
import { codes } from '../../src/errors.js'
8-
import { type FetchService, fetchService } from '../../src/fetch/index.js'
9-
import { createLibp2p } from '../../src/index.js'
10-
import { plaintext } from '../../src/insecure/index.js'
11-
import { createPeerId } from '../fixtures/creators/peer.js'
127
import type { Libp2p } from '@libp2p/interface'
138
import type { PeerId } from '@libp2p/interface/peer-id'
9+
import { createLibp2p } from 'libp2p'
10+
import { noise } from '@chainsafe/libp2p-noise'
11+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
1412

1513
async function createNode (peerId: PeerId): Promise<Libp2p<{ fetch: FetchService }>> {
1614
return createLibp2p({
@@ -24,11 +22,10 @@ async function createNode (peerId: PeerId): Promise<Libp2p<{ fetch: FetchService
2422
tcp()
2523
],
2624
streamMuxers: [
27-
yamux(),
28-
mplex()
25+
yamux()
2926
],
3027
connectionEncryption: [
31-
plaintext()
28+
noise()
3229
],
3330
services: {
3431
fetch: fetchService()
@@ -56,8 +53,8 @@ describe('Fetch', () => {
5653
}
5754

5855
beforeEach(async () => {
59-
const peerIdA = await createPeerId()
60-
const peerIdB = await createPeerId()
56+
const peerIdA = await createEd25519PeerId()
57+
const peerIdB = await createEd25519PeerId()
6158
sender = await createNode(peerIdA)
6259
receiver = await createNode(peerIdB)
6360

packages/libp2p/test/fetch/index.spec.ts renamed to packages/protocol-fetch/test/index.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env mocha */
22

3+
import { fetchService, type FetchServiceInit } from '../src/index.js'
34
import { ERR_TIMEOUT } from '@libp2p/interface/errors'
45
import { TypedEventEmitter } from '@libp2p/interface/events'
56
import { start, stop } from '@libp2p/interface/startable'
@@ -12,9 +13,6 @@ import delay from 'delay'
1213
import { pipe } from 'it-pipe'
1314
import sinon from 'sinon'
1415
import { stubInterface } from 'sinon-ts'
15-
import { defaultComponents, type Components } from '../../src/components.js'
16-
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
17-
import { fetchService, type FetchServiceInit } from '../../src/fetch/index.js'
1816
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
1917
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
2018

packages/protocol-fetch/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
]
10+
}

packages/protocol-fetch/typedoc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"entryPoints": [
3+
"./src/index.ts"
4+
]
5+
}

0 commit comments

Comments
 (0)