Skip to content

Commit

Permalink
feat: migration to the new IPFS API with async (for) await (ipfs#1569)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored Aug 10, 2020
1 parent d6ef7fd commit 16e15a2
Show file tree
Hide file tree
Showing 40 changed files with 10,619 additions and 31,590 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/validate_change.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Validate Changes
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
with:
persist-credentials: false

- name: Install 🔧
run: |
npm install
- name: Type Check 🔬
run: |
npm run check
219 changes: 219 additions & 0 deletions @types/ipfs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
declare module "ipfs" {
import CID from "cids"
import Multiaddr from 'multiaddr'
import { Buffer } from "buffer"

export interface IPFSService extends CoreService {
pin: PinService
files: FileService
name: NameService
object: ObjectService
}

export interface CoreService {
cat(pathOrCID: string | CID, options?: CatOptions): AsyncIterable<Buffer>
ls(pathOrCID: string | CID, options?: ListOptions): AsyncIterable<ListEntry>
add(file: FileContent | FileObject, options?: AddOptions): Promise<UnixFSEntry>
addAll(files: Iterable<FileContent | FileObject> | AsyncIterable<FileContent | FileObject> | ReadableStream<FileContent | FileObject>, options?: AddOptions): AsyncIterable<UnixFSEntry>
}

export interface PinService {
add(cid: CID, options?: PinAddOptions): Promise<Pin[]>
ls(options?: PinListOptions): AsyncIterable<PinEntry>
rm(cid: CID, options?: PinRemoveOptions): Promise<Pin[]>
}

export interface FileService {
stat(path: string, options?: FSStatOptions): Promise<FileStat>
cp(from: string, to: string, options?: FSCopyOptions): Promise<void>
mv(from: string, to: string, options?: FSMoveOptions): Promise<void>
rm(path: string, options: FSRemoveOptions): Promise<void>
mkdir(path: string, options: FSMakDirectoryOptions): Promise<void>
}

export interface NameService {
resolve(value: string, options?: NameResloveOptions): AsyncIterable<string>
}

export interface SwarmService {
connect(addr: Multiaddr, options?: TimeoutOptions): Promise<void>
}

export interface ObjectService {
new: (options?: ObjectNewOptions) => Promise<CID>
patch: ObjectPatchService
}

export interface ObjectPatchService {
addLink(cid: CID, link: DAGLink, options?: TimeoutOptions): Promise<CID>
}

export type DAGLink = {
name: string,
size: number,
cid: CID
}

export type Pin = { cid: CID }

export type TimeoutOptions = {
timeout?: number,
signal?: AbortSignal
}

export type PinAddOptions = TimeoutOptions & {
recursive?: boolean,
}

export type PinType =
| "recursive"
| "direct"
| "indirect"

export type PinEntry = {
cid: CID,
typ: PinType
}

export type PinListOptions = TimeoutOptions & {
paths?: string | CID | string[] | CID[],
type?: PinType
}

export type PinRemoveOptions = TimeoutOptions & {
recursive?: boolean
}



export type FSStatOptions = TimeoutOptions & {
hash?: boolean,
size?: boolean,
withLocal?: boolean
}



export type FSCopyOptions = TimeoutOptions & {
parents?: boolean,
flush?: boolean,
hashAlg?: string,
cidVersion?: number
}

export type FSMoveOptions = TimeoutOptions & {
parents?: boolean,
flush?: boolean,
hashAlg?: string,
cidVersion?: number
}



export type FSRemoveOptions = TimeoutOptions & {
recursive?: boolean,
flush?: boolean,
hashAlg?: string,
cidVersion?: number
}

export type FSMakDirectoryOptions = TimeoutOptions & {
parents?: boolean,
mode?: number,
mtime?: UnixFSTime | Date | [number, number],
flush?: boolean,
hashAlg?: string,
cidVersion?: number
}

export type FileType =
| 'file'
| 'dir'

export interface FileStat {
cid: CID
size: number
cumulativeSize: number
type: FileType
blocks: number
withLocality: boolean
local: boolean
sizeLocal: number
}


export type NameResloveOptions = TimeoutOptions & {
recursive?: boolean,
nocache?: boolean
}

export type ObjectNewOptions = TimeoutOptions & {
template?: string,
recursive?: boolean,
nocache?: boolean
}

export type CatOptions = TimeoutOptions & {
offset?: number
length?: number
}

export type ListOptions = TimeoutOptions & {

}

export type ListEntry = {
depth: number,
name: string,
path: string,
size: number,
cid: CID,
type: FileType,
mode: number,
mtime: { secs: number, nsecs?: number }
}


type FileContent =
| Uint8Array
| Blob
| String
| AsyncIterable<Uint8Array>
| ReadableStream<Uint8Array>

type FileObject = {
path?: string,
content?: FileContent,
mode?: number | string,
mtime?: Date | UnixFSTime | [number, number]
}

export type AddOptions = TimeoutOptions & {
chunker?: string,
cidVersion?: number,
hashAlg?: number,
onlyHash?: boolean,
pin?: boolean,
progress?: (bytes: number) => void,
rawLeaves?: boolean,
trickle?: boolean,
wrapWithDirectory?: boolean
}

export type UnixFSEntry = {
path: string,
cid: CID,
mode: number,
mtime: UnixFSTime,
size: number
}


export type UnixFSTime = {
secs: number,
nsecs: number
}


export var IPFS: IPFSService
}
5 changes: 5 additions & 0 deletions @types/it-all/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "it-all" {
function all<T>(source: AsyncIterable<T>): Promise<T[]>

export default all
}
5 changes: 5 additions & 0 deletions @types/it-last/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module "it-last" {
function last<T>(input: AsyncIterable<T>): Promise<T>

export default last
}
4 changes: 4 additions & 0 deletions @types/it-map/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "it-map" {
function map<I, O>(input: AsyncIterable<I>, f: (input: I) => O): AsyncIterable<O>
export default map
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,17 @@ CI setup of ipfs-webui repo runs tests against both JS and GO implementations:
> E2E_IPFSD_TYPE=js npm run test:e2e
```

##### Overriding versions

It is possible to test against arbitrary versions by tweaking `ipfs` (js-ipfs)
and `go-ipfs-dep` (go-ipfs) in `devDependencies` section of `package.json` and applying the change via `npm i`
and `go-ipfs` in `devDependencies` section of `package.json` and applying the change via `npm i`

One can also override the binary used in e2e tests by providing a path to an alternative one via `IPFS_GO_EXEC` (or `IPFS_JS_EXEC`):

```sh
> IPFS_GO_EXEC=$GOPATH/bin/ipfs npm run test:e2e
> E2E_IPFSD_TYPE=js IPFS_JS_EXEC=/path/to/jsipfs npm run test:e2e
```

#### `E2E_API_URL`

Expand Down
Loading

0 comments on commit 16e15a2

Please sign in to comment.