1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Replacing node-fetch with cross-fetch (#687)

* installing cross-fetch

* implementing cross-fetch

* Correcting web3 dependancy

* importing types from cross-fetch

* fixing dependancies

* adding imports from cross/fetch/lib

* changing .buffer() to .arrayBufer()

* implementing timeout-signal

* chaning require statements to import statements

* fixing require/import issues

* using default import for timeoutSignal

* fixing linting issues

* fixing dependancy issue

* lint fix

* fix timeout

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* add abort controller compatible with browser and node

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
Jamie Hewitt 2021-03-26 20:17:50 +03:00 committed by GitHub
parent c957e8e846
commit 2a4510aa0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 77 additions and 38 deletions

61
package-lock.json generated
View File

@ -1852,6 +1852,16 @@
"json-rpc-engine": "^5.1.3" "json-rpc-engine": "^5.1.3"
}, },
"dependencies": { "dependencies": {
"cross-fetch": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-2.2.3.tgz",
"integrity": "sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw==",
"dev": true,
"requires": {
"node-fetch": "2.1.2",
"whatwg-fetch": "2.0.4"
}
},
"eth-json-rpc-errors": { "eth-json-rpc-errors": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/eth-json-rpc-errors/-/eth-json-rpc-errors-1.1.1.tgz", "resolved": "https://registry.npmjs.org/eth-json-rpc-errors/-/eth-json-rpc-errors-1.1.1.tgz",
@ -1860,6 +1870,12 @@
"requires": { "requires": {
"fast-safe-stringify": "^2.0.6" "fast-safe-stringify": "^2.0.6"
} }
},
"node-fetch": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz",
"integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=",
"dev": true
} }
} }
}, },
@ -2002,6 +2018,16 @@
"integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=", "integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=",
"dev": true "dev": true
}, },
"cross-fetch": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-2.2.3.tgz",
"integrity": "sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw==",
"dev": true,
"requires": {
"node-fetch": "2.1.2",
"whatwg-fetch": "2.0.4"
}
},
"ethereum-common": { "ethereum-common": {
"version": "0.0.18", "version": "0.0.18",
"resolved": "https://registry.npmjs.org/ethereum-common/-/ethereum-common-0.0.18.tgz", "resolved": "https://registry.npmjs.org/ethereum-common/-/ethereum-common-0.0.18.tgz",
@ -2033,6 +2059,12 @@
"safe-buffer": "^5.1.1" "safe-buffer": "^5.1.1"
} }
}, },
"node-fetch": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz",
"integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=",
"dev": true
},
"readable-stream": { "readable-stream": {
"version": "2.3.7", "version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
@ -2157,9 +2189,9 @@
}, },
"dependencies": { "dependencies": {
"form-data": { "form-data": {
"version": "3.0.0", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.0.tgz", "resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==", "integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dev": true, "dev": true,
"requires": { "requires": {
"asynckit": "^0.4.0", "asynckit": "^0.4.0",
@ -3746,21 +3778,11 @@
} }
}, },
"cross-fetch": { "cross-fetch": {
"version": "2.2.3", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-2.2.3.tgz", "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.2.tgz",
"integrity": "sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw==", "integrity": "sha512-+JhD65rDNqLbGmB3Gzs3HrEKC0aQnD+XA3SY6RjgkF88jV2q5cTc5+CwxlS3sdmLk98gpPt5CF9XRnPdlxZe6w==",
"dev": true,
"requires": { "requires": {
"node-fetch": "2.1.2", "node-fetch": "2.6.1"
"whatwg-fetch": "2.0.4"
},
"dependencies": {
"node-fetch": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz",
"integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=",
"dev": true
}
} }
}, },
"cross-spawn": { "cross-spawn": {
@ -8278,6 +8300,11 @@
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==",
"dev": true "dev": true
}, },
"node-abort-controller": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-1.2.0.tgz",
"integrity": "sha512-x6Pv6ACfOUmYGDW/SRjSKBJs7waJRxRPQ9FeXFqfBtEtvJQBfuPl5P74p0Ow+vl0w6WURvXwn+xq/3S95Z7e5Q=="
},
"node-addon-api": { "node-addon-api": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.2.tgz",

View File

@ -38,17 +38,18 @@
}, },
"homepage": "https://github.com/oceanprotocol/ocean.js#readme", "homepage": "https://github.com/oceanprotocol/ocean.js#readme",
"peerDependencies": { "peerDependencies": {
"web3": "^1.2.3" "web3": "^1.3.4"
}, },
"dependencies": { "dependencies": {
"@ethereum-navigator/navigator": "^0.5.2", "@ethereum-navigator/navigator": "^0.5.2",
"@oceanprotocol/contracts": "^0.5.10", "@oceanprotocol/contracts": "^0.5.10",
"@types/crypto-js": "^4.0.1", "@types/crypto-js": "^4.0.1",
"cross-fetch": "^3.1.2",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"decimal.js": "^10.2.1", "decimal.js": "^10.2.1",
"fs": "0.0.1-security", "fs": "0.0.1-security",
"lzma": "^2.3.2", "lzma": "^2.3.2",
"node-fetch": "^2.6.1", "node-abort-controller": "^1.2.0",
"save-file": "^2.3.1", "save-file": "^2.3.1",
"uuid": "^8.3.2", "uuid": "^8.3.2",
"web3": "^1.3.4", "web3": "^1.3.4",
@ -60,8 +61,8 @@
"@types/chai": "^4.2.11", "@types/chai": "^4.2.11",
"@types/chai-spies": "^1.0.1", "@types/chai-spies": "^1.0.1",
"@types/mocha": "^8.0.3", "@types/mocha": "^8.0.3",
"@types/node": "^14.14.20", "@types/node": "^14.14.35",
"@types/node-fetch": "^2.5.5", "@types/node-fetch": "^2.5.8",
"@typescript-eslint/eslint-plugin": "^4.13.0", "@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0", "@typescript-eslint/parser": "^4.13.0",
"auto-changelog": "^2.2.1", "auto-changelog": "^2.2.1",

View File

@ -1,11 +1,9 @@
import { BodyInit, RequestInit, Response } from 'node-fetch' import { Response } from 'node-fetch'
import fs from 'fs' import fs from 'fs'
import { Logger } from '../../utils' import { Logger } from '../../utils'
import save from 'save-file' import save from 'save-file'
// import { createWriteStream } from 'streamsaver' import timeoutSignal from '../../utils/Timeout'
const fetch = require('cross-fetch')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetch = require('node-fetch')
/** /**
* Provides a common interface to web services. * Provides a common interface to web services.
@ -40,12 +38,12 @@ export class WebServiceConnector {
method: 'POST', method: 'POST',
body: payload, body: payload,
headers, headers,
timeout: 5000 signal: timeoutSignal(5000)
}) })
} else { } else {
return this.fetch(url, { return this.fetch(url, {
method: 'POST', method: 'POST',
timeout: 5000 signal: timeoutSignal(5000)
}) })
} }
} }
@ -56,7 +54,7 @@ export class WebServiceConnector {
headers: { headers: {
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
timeout: 5000 signal: timeoutSignal(5000)
}) })
} }
@ -68,7 +66,7 @@ export class WebServiceConnector {
headers: { headers: {
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
timeout: 5000 signal: timeoutSignal(5000)
}) })
} else { } else {
return this.fetch(url, { return this.fetch(url, {
@ -76,7 +74,7 @@ export class WebServiceConnector {
headers: { headers: {
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
timeout: 5000 signal: timeoutSignal(5000)
}) })
} }
} }
@ -89,7 +87,7 @@ export class WebServiceConnector {
headers: { headers: {
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
timeout: 5000 signal: timeoutSignal(5000)
}) })
} else { } else {
return this.fetch(url, { return this.fetch(url, {
@ -97,7 +95,7 @@ export class WebServiceConnector {
headers: { headers: {
'Content-type': 'application/json' 'Content-type': 'application/json'
}, },
timeout: 5000 signal: timeoutSignal(5000)
}) })
} }
} }

View File

@ -7,13 +7,9 @@ import { ComputeInput } from '../ocean/interfaces/ComputeInput'
import { Output } from '../ocean/interfaces/ComputeOutput' import { Output } from '../ocean/interfaces/ComputeOutput'
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm' import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
import { Versions } from '../ocean/Versions' import { Versions } from '../ocean/Versions'
import { Response } from 'node-fetch'
import { DDO } from '../ddo/DDO' import { DDO } from '../ddo/DDO'
import DID from '../ocean/DID' import DID from '../ocean/DID'
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetch = require('node-fetch')
export interface ServiceEndpoint { export interface ServiceEndpoint {
serviceName: string serviceName: string
method: string method: string

17
src/utils/Timeout.ts Normal file
View File

@ -0,0 +1,17 @@
import AbortController from 'node-abort-controller'
export default function timeoutSignal(timeout: number) {
if (!Number.isInteger(timeout)) {
throw new TypeError(`Expected an integer, got ${typeof timeout}`)
}
const signalMap = new WeakMap()
const controller = new AbortController()
const timeoutId = setTimeout(() => {
controller.abort()
}, timeout)
signalMap.set(controller.signal, timeoutId)
// any is needed due to some type incompatibility
return controller.signal as any
}

View File

@ -1,5 +1,5 @@
import { WebServiceConnector } from '../../../src/ocean/utils/WebServiceConnector' import { WebServiceConnector } from '../../../src/ocean/utils/WebServiceConnector'
import { RequestInit } from 'node-fetch' import { RequestInit } from 'cross-fetch/lib.fetch'
export default class WebServiceConnectorMock extends (WebServiceConnector as any) { export default class WebServiceConnectorMock extends (WebServiceConnector as any) {
constructor(private returnData: any) { constructor(private returnData: any) {