This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ipfs api): extend connector with add and cat data
- Loading branch information
Showing
16 changed files
with
977 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.