Skip to content

Commit

Permalink
refactor!: move fetch into seperate package (libp2p#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad authored and achingbrain committed Nov 3, 2023
1 parent ce6ee74 commit 0cb7368
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 26 deletions.
17 changes: 17 additions & 0 deletions doc/migrations/v0.46-v1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A migration guide for refactoring your application code from libp2p `v0.46` to `
## Table of Contents <!-- omit in toc -->

- [AutoNAT](#autonat)
- [Fetch](#fetch)
- [KeyChain](#keychain)
- [Pnet](#pnet)
- [Metrics](#metrics)
Expand All @@ -26,6 +27,22 @@ import { autoNATService } from 'libp2p/autonat'
import { autoNATService } from '@libp2p/autonat'
```

## Fetch

The Fetch service is now publisehd as it's own package.

**Before**

```ts
import { autoNATService } from 'libp2p/fetch'
```

**After**

```ts
import { autoNATService } from '@libp2p/fetch'
```

## KeyChain

The KeyChain object is no longer included on Libp2p and must be instantiated explicitly if desired.
Expand Down
3 changes: 3 additions & 0 deletions packages/interface/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ export class InvalidCryptoTransmissionError extends Error {
// Error codes

export const ERR_TIMEOUT = 'ERR_TIMEOUT'
export const ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE'
export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'
export const ERR_KEY_ALREADY_EXISTS = 'ERR_KEY_ALREADY_EXISTS'
2 changes: 1 addition & 1 deletion packages/libp2p/test/configuration/protocol-prefix.node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-env mocha */

import { type FetchService, fetchService } from '@libp2p/fetch'
import { expect } from 'aegir/chai'
import { pEvent } from 'p-event'
import { type FetchService, fetchService } from '../../src/fetch/index.js'
import { identifyService } from '../../src/identify/index.js'
import { createLibp2p } from '../../src/index.js'
import { type PingService, pingService } from '../../src/ping/index.js'
Expand Down
5 changes: 2 additions & 3 deletions packages/protocol-autonat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
}
},
"scripts": {
"start": "node dist/src/main.js",
"build": "aegir build",
"test": "aegir test",
"clean": "aegir clean",
Expand All @@ -48,8 +47,8 @@
"dependencies": {
"@libp2p/interface": "^0.1.2",
"@libp2p/interface-internal": "^0.1.5",
"@libp2p/peer-id": "^3.0.2" ,
"@libp2p/peer-id-factory": "^3.0.4" ,
"@libp2p/peer-id": "^3.0.2",
"@libp2p/peer-id-factory": "^3.0.4",
"@libp2p/logger": "^3.0.2",
"@multiformats/multiaddr": "^12.1.5",
"it-first": "^3.0.1",
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol-fetch/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/protocol-fetch/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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

http://www.apache.org/licenses/LICENSE-2.0

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.
19 changes: 19 additions & 0 deletions packages/protocol-fetch/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The fetch protocol is a simple protocol for requesting a value corresponding to

## Usage

```javascript
```ts
import { createLibp2p } from 'libp2p'

/**
Expand Down
72 changes: 72 additions & 0 deletions packages/protocol-fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "@libp2p/fetch",
"version": "1.0.0",
"description": "Implementation of the Fetch Protocol",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/protocol-fetch#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p/issues"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"scripts": {
"build": "aegir build",
"test": "aegir test",
"clean": "aegir clean",
"generate": "protons ./src/pb/index.proto",
"lint": "aegir lint",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"test:node": "aegir test -t node --cov",
"dep-check": "aegir dep-check"
},
"dependencies": {
"@libp2p/interface": "^0.1.4",
"@libp2p/interface-internal": "^0.1.7",
"@libp2p/logger": "^3.0.4",
"it-first": "^3.0.3",
"it-length-prefixed": "^9.0.3",
"it-pipe": "^3.0.1",
"protons-runtime": "^5.2.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^4.0.6"
},
"devDependencies": {
"@chainsafe/libp2p-noise": "^13.0.2",
"@chainsafe/libp2p-yamux": "^5.0.0",
"@libp2p/interface-compliance-tests": "^4.1.2",
"@libp2p/peer-id-factory": "^3.0.6",
"@libp2p/peer-store": "^9.0.7",
"@libp2p/tcp": "^8.0.10",
"aegir": "^40.0.8",
"datastore-core": "^9.2.3",
"delay": "^6.0.0",
"libp2p": "^0.46.16",
"sinon": "^17.0.1",
"sinon-ts": "^2.0.0"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { CodeError, ERR_TIMEOUT } from '@libp2p/interface/errors'
import { setMaxListeners } from '@libp2p/interface/events'
import { CodeError, ERR_TIMEOUT, ERR_INVALID_MESSAGE, ERR_INVALID_PARAMETERS, ERR_KEY_ALREADY_EXISTS } from '@libp2p/interface/errors'
import { logger } from '@libp2p/logger'
import first from 'it-first'
import * as lp from 'it-length-prefixed'
import { pipe } from 'it-pipe'
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
import { codes } from '../errors.js'
import { PROTOCOL_NAME, PROTOCOL_VERSION } from './constants.js'
import { FetchRequest, FetchResponse } from './pb/proto.js'
import type { AbortOptions } from '@libp2p/interface'
Expand Down Expand Up @@ -172,7 +171,7 @@ class DefaultFetchService implements Startable, FetchService {
const buf = await first(source)

if (buf == null) {
throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
throw new CodeError('No data received', ERR_INVALID_MESSAGE)
}

const response = FetchResponse.decode(buf)
Expand All @@ -189,11 +188,11 @@ class DefaultFetchService implements Startable, FetchService {
case (FetchResponse.StatusCode.ERROR): {
log('received status for %s error', key)
const errmsg = uint8arrayToString(response.data)
throw new CodeError('Error in fetch protocol response: ' + errmsg, codes.ERR_INVALID_PARAMETERS)
throw new CodeError('Error in fetch protocol response: ' + errmsg, ERR_INVALID_PARAMETERS)
}
default: {
log('received status for %s unknown', key)
throw new CodeError('Unknown response status', codes.ERR_INVALID_MESSAGE)
throw new CodeError('Unknown response status', ERR_INVALID_MESSAGE)
}
}
}
Expand Down Expand Up @@ -224,7 +223,7 @@ class DefaultFetchService implements Startable, FetchService {
const buf = await first(source)

if (buf == null) {
throw new CodeError('No data received', codes.ERR_INVALID_MESSAGE)
throw new CodeError('No data received', ERR_INVALID_MESSAGE)
}

// for await (const buf of source) {
Expand Down Expand Up @@ -280,7 +279,7 @@ class DefaultFetchService implements Startable, FetchService {
*/
registerLookupFunction (prefix: string, lookup: LookupFunction): void {
if (this.lookupFunctions.has(prefix)) {
throw new CodeError(`Fetch protocol handler for key prefix '${prefix}' already registered`, codes.ERR_KEY_ALREADY_EXISTS)
throw new CodeError(`Fetch protocol handler for key prefix '${prefix}' already registered`, ERR_KEY_ALREADY_EXISTS)
}

this.lookupFunctions.set(prefix, lookup)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* eslint-env mocha */

import { yamux } from '@chainsafe/libp2p-yamux'
import { mplex } from '@libp2p/mplex'
import { type FetchService, fetchService } from '../src/index.js'
import { tcp } from '@libp2p/tcp'
import { expect } from 'aegir/chai'
import { codes } from '../../src/errors.js'
import { type FetchService, fetchService } from '../../src/fetch/index.js'
import { createLibp2p } from '../../src/index.js'
import { plaintext } from '../../src/insecure/index.js'
import { createPeerId } from '../fixtures/creators/peer.js'
import type { Libp2p } from '@libp2p/interface'
import type { PeerId } from '@libp2p/interface/peer-id'
import { createLibp2p } from 'libp2p'
import { noise } from '@chainsafe/libp2p-noise'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'

async function createNode (peerId: PeerId): Promise<Libp2p<{ fetch: FetchService }>> {
return createLibp2p({
Expand All @@ -24,11 +22,10 @@ async function createNode (peerId: PeerId): Promise<Libp2p<{ fetch: FetchService
tcp()
],
streamMuxers: [
yamux(),
mplex()
yamux()
],
connectionEncryption: [
plaintext()
noise()
],
services: {
fetch: fetchService()
Expand Down Expand Up @@ -56,8 +53,8 @@ describe('Fetch', () => {
}

beforeEach(async () => {
const peerIdA = await createPeerId()
const peerIdB = await createPeerId()
const peerIdA = await createEd25519PeerId()
const peerIdB = await createEd25519PeerId()
sender = await createNode(peerIdA)
receiver = await createNode(peerIdB)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env mocha */

import { fetchService, type FetchServiceInit } from '../src/index.js'
import { ERR_TIMEOUT } from '@libp2p/interface/errors'
import { TypedEventEmitter } from '@libp2p/interface/events'
import { start, stop } from '@libp2p/interface/startable'
Expand All @@ -12,9 +13,6 @@ import delay from 'delay'
import { pipe } from 'it-pipe'
import sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import { defaultComponents, type Components } from '../../src/components.js'
import { DefaultConnectionManager } from '../../src/connection-manager/index.js'
import { fetchService, type FetchServiceInit } from '../../src/fetch/index.js'
import type { ConnectionGater } from '@libp2p/interface/connection-gater'
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'

Expand Down
10 changes: 10 additions & 0 deletions packages/protocol-fetch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src",
"test"
]
}
5 changes: 5 additions & 0 deletions packages/protocol-fetch/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"entryPoints": [
"./src/index.ts"
]
}

0 comments on commit 0cb7368

Please sign in to comment.