Skip to content
This repository has been archived by the owner on Oct 20, 2020. It is now read-only.

Commit

Permalink
feat(ipfs api): extend connector with add and cat data
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshyx committed May 16, 2016
1 parent dca23b1 commit 16da1ab
Show file tree
Hide file tree
Showing 16 changed files with 977 additions and 35 deletions.
62 changes: 62 additions & 0 deletions IpfsApiHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
const Promise = require('bluebird');
class IpfsApiHelper {
constructor(provider) {
this.apiClient = provider;
}
add(source) {
if (Array.isArray(source)) {
return this._addMultiple(source);
}
return this._add(source);
}
cat(hashSource) {
if (Array.isArray(hashSource)) {
return this._catMultiple(hashSource);
}
return this._cat(hashSource);
}
_add(source) {
const options = Object.assign({}, { isPath: false, recursive: false, followSymlinks: false }, source.options);
let contentBody = source.data;
if (!options.isPath) {
contentBody = new Buffer(contentBody);
}
return this.apiClient.add(contentBody, options);
}
_addMultiple(sources = []) {
let data = [];
sources.forEach((source) => {
data.push(this._add(source));
});
return Promise.all(data);
}
_cat(source) {
let buf = new Buffer(0);
return new Promise((resolve, reject) => {
return this.apiClient.cat(source.id).then((response) => {
if (response.readable) {
return response.on('error', (err) => {
return reject(err);
}).on('data', (data) => {
buf = Buffer.concat([buf, data]);
}).on('end', () => {
if (source.encoding) {
return resolve(buf.toString(source.encoding));
}
return resolve(buf);
});
}
return resolve(response);
}).catch((err) => reject(err));
});
}
_catMultiple(hashSources = []) {
let data = [];
hashSources.forEach((hash) => {
data.push(this._cat(hash));
});
return Promise.all(data);
}
}
exports.IpfsApiHelper = IpfsApiHelper;
118 changes: 118 additions & 0 deletions IpfsApiHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/// <reference path="typings/main.d.ts"/>
import * as Promise from 'bluebird';

export class IpfsApiHelper {
public apiClient: any;

constructor (provider: any) {
this.apiClient = provider;
}

/**
* Add data to ipfs
* @param source
* @returns {Bluebird}
*/
public add (source: {
data: any,
options?: { isPath: boolean, recursive: boolean, followSymlinks: boolean }
}[]): Promise<{} | {}[]> {
if (Array.isArray(source)) {
return this._addMultiple(source);
}
return this._add(source);
}

/**
* Read data from ipfs
* @param hashSource
* @returns {any}
*/
public cat (hashSource: {
id: string,
encoding?: string
}[]): Promise<{} | {}[]> {
if (Array.isArray(hashSource)) {
return this._catMultiple(hashSource);
}
return this._cat(hashSource);
}

/**
*
* @param source
* @returns {Bluebird}
* @private
*/
private _add (source: any): Promise<{}> {
const options = Object.assign({},
{ isPath: false, recursive: false, followSymlinks: false },
source.options);

let contentBody = source.data;

if (!options.isPath) {
contentBody = new Buffer(contentBody);
}

return this.apiClient.add(contentBody, options);
}

/**
*
* @param sources
* @returns {Bluebird<any>}
* @private
*/
private _addMultiple (sources: {}[] = []): Promise<{}[]> {

let data: Promise<{}>[] = [];

sources.forEach((source) => {
data.push(this._add(source));
});

return Promise.all(data);
}

/**
*
* @param source
* @returns {Bluebird}
* @private
*/
private _cat (source: any): Promise<{}> {
let buf = new Buffer(0);
return new Promise((resolve, reject) => {
return this.apiClient.cat(source.id).then((response: any) => {
if (response.readable) {
return response.on('error', (err: Error) => {
return reject(err);
}).on('data', (data: Buffer) => {
buf = Buffer.concat([buf, data]);
}).on('end', () => {
if (source.encoding) {
return resolve(buf.toString(source.encoding));
}
return resolve(buf);
});
}
return resolve(response);
}).catch((err: Error) => reject(err));
});
}

/**
*
* @param hashSources
* @returns {Bluebird<any>}
* @private
*/
private _catMultiple (hashSources: {}[] = []): Promise<{}[]> {
let data: Promise<{}>[] = [];
hashSources.forEach((hash) => {
data.push(this._cat(hash));
});
return Promise.all(data);
}
}
10 changes: 10 additions & 0 deletions IpfsConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const os_1 = require('os');
const Promise = require('bluebird');
const IpfsBin_1 = require('./IpfsBin');
const IpfsApiHelper_1 = require('./IpfsApiHelper');
const childProcess = require('child_process');
const path = require('path');
const ipfsApi = require('ipfs-api');
const symbolEnforcer = Symbol();
const symbol = Symbol();
class IpfsConnector {
Expand All @@ -30,6 +32,13 @@ class IpfsConnector {
}
return this[symbol];
}
get api() {
if (!this._api) {
let api = ipfsApi(this.options.apiAddress);
this._api = new IpfsApiHelper_1.IpfsApiHelper(api);
}
return this._api;
}
setLogger(logger) {
this.logger = logger;
}
Expand Down Expand Up @@ -58,6 +67,7 @@ class IpfsConnector {
stop(signal = 'SIGINT') {
this.process.kill(signal);
this.process = null;
this._api = null;
this.options.retry = true;
}
_init() {
Expand Down
17 changes: 16 additions & 1 deletion IpfsConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import { homedir } from 'os';
import * as Promise from 'bluebird';
import { IpfsBin } from './IpfsBin';
import { IpfsApiHelper } from './IpfsApiHelper';

import childProcess = require('child_process');
import path = require('path');
import ipfsApi = require('ipfs-api');
import * as ipfsApi from 'ipfs-api';

const symbolEnforcer = Symbol();
const symbol = Symbol();
Expand All @@ -15,6 +16,7 @@ export class IpfsConnector {
private process: childProcess.ChildProcess;
private downloadManager = new IpfsBin();
private logger: any = console;
private _api: IpfsApiHelper;
public options = {
retry: true,
apiAddress: '/ip4/127.0.0.1/tcp/5001',
Expand Down Expand Up @@ -46,6 +48,18 @@ export class IpfsConnector {
return this[symbol];
}

/**
*
* @returns {IpfsApiHelper}
*/
get api (): IpfsApiHelper {
if (!this._api) {
let api = ipfsApi(this.options.apiAddress);
this._api = new IpfsApiHelper(api);
}
return this._api;
}

/**
* Set logging object, winston works great
* @param logger
Expand Down Expand Up @@ -101,6 +115,7 @@ export class IpfsConnector {
public stop (signal = 'SIGINT'): void {
this.process.kill(signal);
this.process = null;
this._api = null;
this.options.retry = true;
}

Expand Down
2 changes: 1 addition & 1 deletion docs/assets/js/search.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 16da1ab

Please sign in to comment.