Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Layout] Add IPFS interfaces and github workflow #2

Merged
merged 5 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: PR
on: [pull_request]

jobs:
pr:
strategy:
matrix:
step: ['lint', 'build']
name: ${{ matrix.step }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: ${{ matrix.step }}
run: |
yarn
yarn ${{ matrix.step }}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
"posttest": "yarn run lint"
},
"dependencies": {
"@polkadot/api": "^2.2.1",
"@polkadot/api": "^2.5.1",
"bignumber.js": "^9.0.1",
"ipfs-http-client": "^48.0.0",
"lodash": "^4.17.20"
},
"devDependencies": {
"@types/bignumber.js": "^5.0.0",
"@types/lodash": "^4.14.164",
"@types/node": "^14.14.6",
"gts": "^3.0.2",
Expand Down
73 changes: 72 additions & 1 deletion src/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,74 @@
import {BigNumber} from 'bignumber.js';
const IpfsHttpClient = require('ipfs-http-client');
const {CID} = require('ipfs-http-client');

export class IPFS {}
export default class Ipfs {
private ipfs: any;

constructor(ipfsAddr: string, mto: number) {
this.ipfs = IpfsHttpClient({
address: ipfsAddr,
timeout: mto,
});
}

/// WRITE methods
/**
* Pin add file by a given cid asyncly
* @param c ipfs cid value
* @throws illegal cid | timeout
*/
async pin(c: string): Promise<boolean> {
const cid = new CID(c);
const pin = await this.ipfs.pin.add(new CID(cid));
return cid.equals(pin);
}

/**
* Pin remove file by a given cid
* @param c ipfs cid value
* @throws illegal cid | unpinned `c` | timeout
*/
async unpin(c: string): Promise<boolean> {
const cid = new CID(c);
const pin = await this.ipfs.pin.rm(cid);
return cid.equals(pin);
}

/// READONLY methods
/**
* Get file size by a given cid
* @param cid ipfs cid value
* @returns file size (bytes)
* @throws illegal cid | timeout
*/
async size(cid: string): Promise<number> {
const objInfo = await this.ipfs.object.stat(new CID(cid));
return objInfo.CumulativeSize;
}

/**
* Query if a given cid(recursive type) exist
* @param c ipfs cid value
* @throws illegal cid | timeout
*/
async exist(c: string): Promise<boolean> {
const cid = new CID(c);
for await (const pin of this.ipfs.pin.ls({
paths: cid,
type: 'recursive',
})) {
if (cid.equals(pin.cid)) return true;
}
return false;
}

/**
* @returns ipfs remaining storage
* @throws timeout
*/
async free(): Promise<BigNumber> {
const repoStat = await this.ipfs.repo.stat();
return repoStat.storageMax.minus(repoStat.repoSize);
}
}
2 changes: 2 additions & 0 deletions src/queue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as _ from 'lodash';
export interface Task {
// The ipfs cid value
cid: string;
// Object size
size: number;
// Current block number
bn: number;
}
Expand Down
8 changes: 6 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
"target": "es6",
"module": "commonjs",
"rootDir": "./",
"outDir": "build",
"skipLibCheck": true,
"strict": true
},
"include": [
"src/**/*.ts",
Expand Down
Loading