Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit f7a56dd

Browse files
committed
init
1 parent ca55356 commit f7a56dd

File tree

12 files changed

+232
-6
lines changed

12 files changed

+232
-6
lines changed

packages/core/lib/console-child.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const TruffleError = require("@truffle/error");
33
const Config = require("@truffle/config");
44
const Web3 = require("web3");
55
const yargs = require("yargs");
6+
const StreamingWeb3HttpProvider = require("stream-provider");
67

78
const crypto = require("crypto");
89
global.crypto = crypto;
@@ -32,7 +33,7 @@ detectedConfig.networks.develop = {
3233
port: customConfig.port || 9545,
3334
network_id: customConfig.network_id || 5777,
3435
provider: function () {
35-
return new Web3.providers.HttpProvider(url, { keepAlive: false });
36+
return new StreamingWeb3HttpProvider(url, { keepAlive: false });
3637
}
3738
};
3839

packages/core/lib/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ const TruffleError = require("@truffle/error");
33
const Networks = require("./networks");
44
const EthPM = require("ethpm");
55
const EthPMRegistry = require("ethpm-registry");
6-
const Web3 = require("web3");
76
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
87
const path = require("path");
98
const fs = require("fs");
109
const OS = require("os");
10+
const StreamingWeb3HttpProvider = require("stream-provider");
1111

1212
const Package = {
1313
install: async function(options, callback) {
@@ -22,7 +22,7 @@ const Package = {
2222

2323
const provider =
2424
options.ethpm.provider ||
25-
new Web3.providers.HttpProvider(options.ethpm.install_provider_uri, {
25+
new StreamingWeb3HttpProvider(options.ethpm.install_provider_uri, {
2626
keepAlive: false
2727
});
2828
let host = options.ethpm.ipfs_host;

packages/debugger/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"test:debug": "NODE_ENV=testing node --inspect ./node_modules/.bin/mocha-webpack --webpack-config webpack/webpack.config-test.js --recursive"
2525
},
2626
"dependencies": {
27+
"JSONStream": "1.3.5",
2728
"@truffle/abi-utils": "^0.2.4",
2829
"@truffle/codec": "^0.11.17",
2930
"@truffle/source-map-utils": "^1.3.61",

packages/environment/environment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Resolver = require("@truffle/resolver");
66
const Artifactor = require("@truffle/artifactor");
77
const Ganache = require("ganache-core/public-exports");
88
const Provider = require("@truffle/provider");
9+
const StreamingWeb3HttpProvider = require("stream-provider");
910

1011
const Environment = {
1112
// It's important config is a Config object and not a vanilla object
@@ -73,7 +74,7 @@ const Environment = {
7374
config.networks[network] = {
7475
network_id: ganacheOptions.network_id,
7576
provider: function () {
76-
return new Web3.providers.HttpProvider(url, { keepAlive: false });
77+
return new StreamingWeb3HttpProvider(url, { keepAlive: false });
7778
}
7879
};
7980

packages/provider/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Web3 = require("web3");
33
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
44
const wrapper = require("./wrapper");
55
const DEFAULT_NETWORK_CHECK_TIMEOUT = 5000;
6+
const StreamingWeb3HttpProvider = require("stream-provider");
67

78
module.exports = {
89
wrap: function (provider, options) {
@@ -25,7 +26,7 @@ module.exports = {
2526
options.url || "ws://" + options.host + ":" + options.port
2627
);
2728
} else {
28-
provider = new Web3.providers.HttpProvider(
29+
provider = new StreamingWeb3HttpProvider(
2930
options.url || `http://${options.host}:${options.port}`,
3031
{ keepAlive: false }
3132
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

packages/stream-provider/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 ConsenSys, Inc.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

packages/stream-provider/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# @truffle/stream-provider
2+
3+
A Web3.providers.HttpProvider that can handle JSON blobs greater than 1 GB.
4+
5+
Large blob handling is currently only used for `debug_traceTransaction`
6+
results.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import axios, { AxiosRequestConfig } from "axios";
2+
import type { JsonRpcResponse } from "web3-core-helpers";
3+
import { errors } from "web3-core-helpers";
4+
import _Web3HttpProvider, { HttpProvider } from "web3-providers-http";
5+
const JSONStream = require("JSONStream");
6+
7+
// they export types, but in the wrong place
8+
const Web3HttpProvider = (_Web3HttpProvider as any) as typeof HttpProvider;
9+
10+
export class StreamingWeb3HttpProvider extends Web3HttpProvider {
11+
/**
12+
* Should be used to make async request
13+
*
14+
* @method send
15+
* @param {Object} payload
16+
* @param {Function} callback triggered on end with (err, result)
17+
*/
18+
send(
19+
payload: any,
20+
callback: (error: Error | null, result: JsonRpcResponse | undefined) => void
21+
) {
22+
if (
23+
typeof payload === "object" &&
24+
payload.method === "debug_traceTransaction"
25+
) {
26+
const requestOptions: AxiosRequestConfig = {
27+
method: "post",
28+
url: this.host,
29+
responseType: "stream",
30+
data: payload,
31+
timeout: this.timeout,
32+
withCredentials: this.withCredentials,
33+
headers: this.headers
34+
? this.headers.reduce((acc, header) => {
35+
acc[header.name] = header.value;
36+
return acc;
37+
}, {} as Record<string, string>)
38+
: undefined
39+
};
40+
// transitional.clarifyTimeoutError is required so we can detect and emit
41+
// a timeout error the way web3 already does
42+
(requestOptions as any).transitional = {
43+
clarifyTimeoutError: true
44+
};
45+
const agents = {
46+
httpsAgent: (this as any).httpsAgent,
47+
httpAgent: (this as any).httpAgent,
48+
baseUrl: (this as any).baseUrl
49+
};
50+
if (this.agent) {
51+
agents.httpsAgent = this.agent.https;
52+
agents.httpAgent = this.agent.http;
53+
agents.baseUrl = this.agent.baseUrl;
54+
}
55+
requestOptions.httpAgent = agents.httpAgent;
56+
requestOptions.httpsAgent = agents.httpsAgent;
57+
requestOptions.baseURL = agents.baseUrl;
58+
59+
axios(requestOptions)
60+
.then(async response => {
61+
let error = null;
62+
let result: any = {};
63+
try {
64+
for await (const { key, value } of response.data.pipe(
65+
JSONStream.parse([true, { emitKey: true }])
66+
)) {
67+
result[key] = value;
68+
}
69+
} catch (e) {
70+
error = errors.InvalidResponse(e);
71+
}
72+
this.connected = true;
73+
// process.nextTick so an exception thrown in the callback doesn't
74+
// bubble back up to here
75+
process.nextTick(callback, error, result);
76+
})
77+
.catch(error => {
78+
this.connected = false;
79+
if (error.code === "ETIMEDOUT") {
80+
// web3 passes timeout as a number to ConnectionTimeout, despite the
81+
// type requiring a string
82+
callback(errors.ConnectionTimeout(this.timeout as any), undefined);
83+
} else {
84+
callback(errors.InvalidConnection(this.host), undefined);
85+
}
86+
});
87+
} else {
88+
return super.send(payload, callback);
89+
}
90+
}
91+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@truffle/stream-provider",
3+
"description": "Enables streaming results into memory from debug_traceTransaction",
4+
"license": "MIT",
5+
"author": "David Murdoch <[email protected]>",
6+
"homepage": "https://github.com/trufflesuite/truffle/tree/master/packages/stream-provider#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/trufflesuite/truffle.git",
10+
"directory": "packages/stream-provider"
11+
},
12+
"bugs": {
13+
"url": "https://github.com/trufflesuite/truffle/issues"
14+
},
15+
"version": "0.0.1",
16+
"main": "dist/index.js",
17+
"files": [
18+
"dist"
19+
],
20+
"directories": {
21+
"lib": "lib"
22+
},
23+
"scripts": {
24+
"build": "tsc",
25+
"prepare": "yarn build"
26+
},
27+
"types": "dist/index.d.ts",
28+
"dependencies": {
29+
"web3-providers-http": "^1.6.0",
30+
"web3-core-helpers": "1.5.3",
31+
"JSONStream": "1.3.5",
32+
"axios": "^0.21.1"
33+
},
34+
"keywords": [
35+
"ethereum",
36+
"etherscan",
37+
"ipfs",
38+
"solidity",
39+
"web3"
40+
],
41+
"publishConfig": {
42+
"access": "public"
43+
}
44+
}

0 commit comments

Comments
 (0)