Skip to content

Commit

Permalink
Replacing node-fetch with cross-fetch (#687)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jamiehewitt15 and mihaisc authored Mar 26, 2021
1 parent c957e8e commit 2a4510a
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 38 deletions.
61 changes: 44 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@
},
"homepage": "https://github.com/oceanprotocol/ocean.js#readme",
"peerDependencies": {
"web3": "^1.2.3"
"web3": "^1.3.4"
},
"dependencies": {
"@ethereum-navigator/navigator": "^0.5.2",
"@oceanprotocol/contracts": "^0.5.10",
"@types/crypto-js": "^4.0.1",
"cross-fetch": "^3.1.2",
"crypto-js": "^4.0.0",
"decimal.js": "^10.2.1",
"fs": "0.0.1-security",
"lzma": "^2.3.2",
"node-fetch": "^2.6.1",
"node-abort-controller": "^1.2.0",
"save-file": "^2.3.1",
"uuid": "^8.3.2",
"web3": "^1.3.4",
Expand All @@ -60,8 +61,8 @@
"@types/chai": "^4.2.11",
"@types/chai-spies": "^1.0.1",
"@types/mocha": "^8.0.3",
"@types/node": "^14.14.20",
"@types/node-fetch": "^2.5.5",
"@types/node": "^14.14.35",
"@types/node-fetch": "^2.5.8",
"@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0",
"auto-changelog": "^2.2.1",
Expand Down
22 changes: 10 additions & 12 deletions src/ocean/utils/WebServiceConnector.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { BodyInit, RequestInit, Response } from 'node-fetch'
import { Response } from 'node-fetch'
import fs from 'fs'
import { Logger } from '../../utils'
import save from 'save-file'
// import { createWriteStream } from 'streamsaver'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetch = require('node-fetch')
import timeoutSignal from '../../utils/Timeout'
const fetch = require('cross-fetch')

/**
* Provides a common interface to web services.
Expand Down Expand Up @@ -40,12 +38,12 @@ export class WebServiceConnector {
method: 'POST',
body: payload,
headers,
timeout: 5000
signal: timeoutSignal(5000)
})
} else {
return this.fetch(url, {
method: 'POST',
timeout: 5000
signal: timeoutSignal(5000)
})
}
}
Expand All @@ -56,7 +54,7 @@ export class WebServiceConnector {
headers: {
'Content-type': 'application/json'
},
timeout: 5000
signal: timeoutSignal(5000)
})
}

Expand All @@ -68,15 +66,15 @@ export class WebServiceConnector {
headers: {
'Content-type': 'application/json'
},
timeout: 5000
signal: timeoutSignal(5000)
})
} else {
return this.fetch(url, {
method: 'PUT',
headers: {
'Content-type': 'application/json'
},
timeout: 5000
signal: timeoutSignal(5000)
})
}
}
Expand All @@ -89,15 +87,15 @@ export class WebServiceConnector {
headers: {
'Content-type': 'application/json'
},
timeout: 5000
signal: timeoutSignal(5000)
})
} else {
return this.fetch(url, {
method: 'DELETE',
headers: {
'Content-type': 'application/json'
},
timeout: 5000
signal: timeoutSignal(5000)
})
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/provider/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import { ComputeInput } from '../ocean/interfaces/ComputeInput'
import { Output } from '../ocean/interfaces/ComputeOutput'
import { MetadataAlgorithm } from '../ddo/interfaces/MetadataAlgorithm'
import { Versions } from '../ocean/Versions'
import { Response } from 'node-fetch'
import { DDO } from '../ddo/DDO'
import DID from '../ocean/DID'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const fetch = require('node-fetch')

export interface ServiceEndpoint {
serviceName: string
method: string
Expand Down
17 changes: 17 additions & 0 deletions src/utils/Timeout.ts
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion test/unit/__mocks__/WebServiceConnector.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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) {
constructor(private returnData: any) {
Expand Down

0 comments on commit 2a4510a

Please sign in to comment.