Skip to content

Commit

Permalink
feat(ref-imp): changed IPFS CAS adaptor to use IPFS HTTP API directly
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Aug 7, 2020
1 parent 0dbf2c8 commit a84f079
Show file tree
Hide file tree
Showing 27 changed files with 465 additions and 1,121 deletions.
27 changes: 24 additions & 3 deletions lib/common/ReadableStream.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import SidetreeError from './SidetreeError';
import ErrorCode from '../common/SharedErrorCode';

/**
* ReadableStream utilities
*/
export default class ReadableStream {

/**
* Given a readable stream, reads all data until end or error
* @param stream Fetch readable stream to read
* Given a readable stream, reads all data only if the content does not exceed given max size.
* Throws error if content exceeds give max size.
* @param stream Readable stream to read.
* @param maxAllowedSizeInBytes The maximum allowed size limit of the content.
* @returns a Buffer of the readable stream data
*/
public static async readAll (stream: NodeJS.ReadableStream): Promise<Buffer> {
public static async readAll (stream: NodeJS.ReadableStream, maxAllowedSizeInBytes?: number): Promise<Buffer> {
// Set callback for the 'readable' event to concatenate chunks of the readable stream.
let content: Buffer = Buffer.alloc(0);
let currentSizeInBytes = 0;

stream.on('readable', () => {
// NOTE: Cast to any is to work-around incorrect TS definition for read() where
// `null` should be a possible return type but is not defined in @types/node: 10.12.18.
let chunk = stream.read() as any;
while (chunk !== null) {
currentSizeInBytes += chunk.length;

// Monitor on read size only if `maxAllowedSizeInBytes` is set.
if (maxAllowedSizeInBytes !== undefined &&
currentSizeInBytes > maxAllowedSizeInBytes) {

const error = new SidetreeError(
ErrorCode.ReadableStreamMaxAllowedDataSizeExceeded,
`Max data size allowed: ${maxAllowedSizeInBytes} bytes, aborted reading at ${currentSizeInBytes} bytes.`
);

// NOTE: Cast to any is to work-around incorrect TS definition where `destroy()` is missing.
(stream as any).destroy(error);
}

content = Buffer.concat([content, chunk]);
chunk = stream.read();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/common/SharedErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
export default {
BlockchainTimeOutOfRange: 'blockchain_time_out_of_range',
InvalidTransactionNumberOrTimeHash: 'invalid_transaction_number_or_time_hash',
NotEnoughBalanceForWrite: 'not_enough_balace_for_write',
NotEnoughBalanceForWrite: 'not_enough_balance_for_write',
ReadableStreamMaxAllowedDataSizeExceeded: 'readable_stream_max_allowed_data_size_exceeded',
SpendingCapPerPeriodReached: 'spending_cap_per_period_reached',
ValueTimeLockNotFound: 'value_time_lock_not_found'
};
1 change: 0 additions & 1 deletion lib/common/enums/FetchResultCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ enum FetchResultCode {
CasNotReachable = 'cas_not_reachable',
InvalidHash = 'content_hash_invalid',
MaxSizeExceeded = 'content_exceeds_maximum_allowed_size',
MaxSizeNotSpecified = 'content_max_size_not_specified',
NotAFile = 'content_not_a_file',
NotFound = 'content_not_found',
Success = 'success'
Expand Down
94 changes: 0 additions & 94 deletions lib/core/Cas.ts

This file was deleted.

11 changes: 4 additions & 7 deletions lib/core/Core.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BatchScheduler from './BatchScheduler';
import Blockchain from './Blockchain';
import Cas from './Cas';
import DownloadManager from './DownloadManager';
import Config from './models/Config';
import DownloadManager from './DownloadManager';
import ICas from './interfaces/ICas';
import MongoDbOperationStore from './MongoDbOperationStore';
import MongoDbTransactionStore from '../common/MongoDbTransactionStore';
import MongoDbUnresolvableTransactionStore from './MongoDbUnresolvableTransactionStore';
Expand All @@ -23,7 +23,6 @@ export default class Core {
private operationStore: MongoDbOperationStore;
private versionManager: VersionManager;
private blockchain: Blockchain;
private cas: Cas;
private downloadManager: DownloadManager;
private observer: Observer;
private batchScheduler: BatchScheduler;
Expand All @@ -33,12 +32,11 @@ export default class Core {
/**
* Core constructor.
*/
public constructor (config: Config, versionModels: VersionModel[]) {
public constructor (config: Config, versionModels: VersionModel[], private cas: ICas) {
// Component dependency construction & injection.
this.versionManager = new VersionManager(config, versionModels); // `VersionManager` is first constructed component.
this.operationStore = new MongoDbOperationStore(config.mongoDbConnectionString, config.databaseName);
this.blockchain = new Blockchain(config.blockchainServiceUri);
this.cas = new Cas(config.contentAddressableStoreServiceUri);
this.downloadManager = new DownloadManager(config.maxConcurrentDownloads, this.cas);
this.resolver = new Resolver(this.versionManager, this.operationStore);
this.batchScheduler = new BatchScheduler(this.versionManager, this.blockchain, config.batchingIntervalInSeconds);
Expand Down Expand Up @@ -112,8 +110,7 @@ export default class Core {
public async handleGetVersionRequest (): Promise<ResponseModel> {
const responses = [
this.serviceInfo.getServiceVersion(),
await this.blockchain.getServiceVersion(),
await this.cas.getServiceVersion()
await this.blockchain.getServiceVersion()
];

return {
Expand Down
1 change: 0 additions & 1 deletion lib/core/models/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
export default interface Config {
batchingIntervalInSeconds: number;
blockchainServiceUri: string;
contentAddressableStoreServiceUri: string;
didMethodName: string;
maxConcurrentDownloads: number;
observingIntervalInSeconds: number;
Expand Down
9 changes: 2 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// NOTE: Aliases to classes and interfaces are used for external consumption.

// Core service exports.
import ISidetreeCas from './core/interfaces/ICas';
import SidetreeCore from './core/Core';
import SidetreeConfig from './core/models/Config';
import SidetreeResponse from './common/Response';
import SidetreeResponseModel from './common/models/ResponseModel';
import SidetreeVersionModel from './common/models/VersionModel';

export {
ISidetreeCas,
SidetreeConfig,
SidetreeCore,
SidetreeResponse,
Expand All @@ -25,10 +27,3 @@ export {
ISidetreeBitcoinWallet,
SidetreeBitcoinProcessor
};

// IPFS service exports.
import SidetreeIpfsService from './ipfs/RequestHandler';

export {
SidetreeIpfsService
};
Loading

0 comments on commit a84f079

Please sign in to comment.