Skip to content

Commit

Permalink
change exports
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Dec 19, 2022
1 parent fe11545 commit 73ada30
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
## Quickstart

```js
import Substreams from "substreams";
import { Substreams, download } from "substreams";

// User input
const host = "eos.firehose.eosnation.io:9001";
Expand All @@ -52,11 +52,10 @@ const substreams = new Substreams(host, {
});

(async () => {
// download Substream from IPFS
const modules = await Substreams.downloadSubstream(substream);
// download Substream & Protobuf from IPFS
const [modules, root] = await download(substream, proto);

// download Protobuf from IPFS
const root = await Substreams.downloadProto(proto);
// Protobuf types
const Action = root.lookupType("Action");

substreams.on("block", block => {
Expand Down
9 changes: 4 additions & 5 deletions examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Substreams from "../";
import { Substreams, download } from "../";

// User input
const host = "eos.firehose.eosnation.io:9001";
Expand All @@ -16,11 +16,10 @@ const substreams = new Substreams(host, {
});

(async () => {
// download Substream from IPFS
const modules = await Substreams.downloadSubstream(substream);
// download Substream & Protobuf from IPFS
const [modules, root] = await download(substream, proto);

// download protobuf from IPFS
const root = await Substreams.downloadProto(proto);
// Protobuf types
const Action = root.lookupType("Action");

substreams.on("block", block => {
Expand Down
22 changes: 3 additions & 19 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { GrpcTransport } from '@protobuf-ts/grpc-transport';
// Substream generated code
// $ buf generate buf.build/fubhy/substreams
import { StreamClient } from './src/generated/sf/substreams/v1/substreams.client';
import { Package } from './src/generated/sf/substreams/v1/package';
import { Modules } from './src/generated/sf/substreams/v1/modules';
import { BlockScopedData, ForkStep, Request } from './src/generated/sf/substreams/v1/substreams';
import { StoreDeltas } from "./src/generated/sf/substreams/v1/substreams";
Expand All @@ -20,7 +19,7 @@ export * from "./src/generated/sf/substreams/v1/substreams"
export * from "./src/utils";

// Utils
import { downloadPackage, downloadProto, parseBlockData } from './src/utils';
import { parseBlockData } from './src/utils';

// Create Substream
export function createStream(client: StreamClient, modules?: Modules, options: {
Expand All @@ -43,13 +42,6 @@ export function createStream(client: StreamClient, modules?: Modules, options: {
}));
}

async function downloadSubstream( ipfs: string ) {
const binary = await downloadPackage(ipfs);
const { modules } = Package.fromBinary(binary);
if ( !modules ) throw new Error(`No modules found in Substream: ${ipfs}`);
return modules;
}

interface ModuleOutput {
name: string;
logs: string[];
Expand All @@ -76,7 +68,7 @@ type MessageEvents = {
storeDeltas: (output: StoreDelta) => void;
}

export default class Substreams extends (EventEmitter as new () => TypedEmitter<MessageEvents>) {
export class Substreams extends (EventEmitter as new () => TypedEmitter<MessageEvents>) {
// internal
public client: StreamClient;

Expand Down Expand Up @@ -113,14 +105,6 @@ export default class Substreams extends (EventEmitter as new () => TypedEmitter<
);
}

static async downloadSubstream( ipfs: string ) {
return downloadSubstream(ipfs);
}

static async downloadProto( ipfs: string ) {
return downloadProto(ipfs);
}

public async start(modules: Modules) {
// Setup Substream
const stream = createStream(this.client, modules, {
Expand Down Expand Up @@ -150,4 +134,4 @@ export default class Substreams extends (EventEmitter as new () => TypedEmitter<
}
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "substreams",
"description": "Substreams Node.js consumer",
"version": "0.1.1",
"version": "0.1.2",
"main": "dist/index.js",
"types": "dist/index.d.js",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/generated/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @ts-nocheck */
/* eslint-disable */
// @generated by protobuf-ts 2.8.2 with parameter generate_dependencies,long_type_string,eslint_disable,client_generic
// @generated from protobuf file "google/protobuf/timestamp.proto" (package "google.protobuf", syntax proto3)
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { importer } from 'ipfs-unixfs-importer';
import { MemoryBlockstore } from 'blockstore-core/memory';

// Substream auto-generated
import { Package } from './generated/sf/substreams/v1/package';
import { Clock } from "./generated/sf/substreams/v1/clock";
import { BlockScopedData, Response } from "./generated/sf/substreams/v1/substreams";

Expand Down Expand Up @@ -41,11 +42,22 @@ export function tmpFilepath(ipfs: string) {
return path.join(os.tmpdir(), ipfs);
}

export async function download(substream: string, proto: string) {
return Promise.all([downloadSubstream(substream), downloadProto(proto)]);
}

export async function downloadProto(ipfs: string) {
await downloadToFile(ipfs);
return protobuf.loadSync(tmpFilepath(ipfs));
}

export async function downloadSubstream( ipfs: string ) {
const binary = await downloadPackage(ipfs);
const { modules } = Package.fromBinary(binary);
if ( !modules ) throw new Error(`No modules found in Substream: ${ipfs}`);
return modules;
}

export function readFileToBuffer(ipfs: string) {
console.log(`Reading from file system: ${ipfs}`);
const filepath = path.isAbsolute(ipfs) ? ipfs : path.resolve(process.cwd(), ipfs);
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

/* Module Resolution Options */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

Expand Down

0 comments on commit 73ada30

Please sign in to comment.