Skip to content

Commit

Permalink
feat: Remove undici (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan authored Jul 26, 2024
1 parent 6a80a48 commit c67fe9d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 46 deletions.
1 change: 0 additions & 1 deletion packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"subleveldown": "4.1.4",
"tslib": "2.6.3",
"typeorm": "0.3.17",
"undici": "5.28.4",
"uuid": "8.3.2"
},
"devDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/utils/ckb-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
PayloadInBatchException,
IdNotMatchedInBatchException,
} from '@ckb-lumos/rpc/lib/exceptions'
import { request } from 'undici'
import CommonUtils from './common'
import { NetworkType } from '../models/network'
import type { RPCConfig } from '@ckb-lumos/rpc/lib/types/common'
Expand Down Expand Up @@ -335,12 +334,12 @@ export class LightRPC extends Base {
return []
}

const res = await request(node.url, {
const res = await fetch(node.url, {
method: 'POST',
body: JSON.stringify(payload),
headers: { 'content-type': 'application/json' },
})
const batchRes = await res.body.json()
const batchRes = await res.json()

if (!Array.isArray(batchRes)) {
return []
Expand Down
18 changes: 8 additions & 10 deletions packages/neuron-wallet/src/utils/rpc-request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { request } from 'undici'

export const rpcRequest = async <T = any>(
url: string,
options: {
method: string
params?: any
}
): Promise<T> => {
const res = await request(url, {
const res = await fetch(url, {
method: 'POST',
body: JSON.stringify({
id: 0,
Expand All @@ -19,10 +17,10 @@ export const rpcRequest = async <T = any>(
'content-type': 'application/json',
},
})
if (res.statusCode !== 200) {
throw new Error(`indexer request failed with HTTP code ${res.statusCode}`)
if (res.status !== 200) {
throw new Error(`indexer request failed with HTTP code ${res.status}`)
}
const body = await res.body.json()
const body = await res.json()
if (body !== null && typeof body === 'object' && 'result' in body) {
return body?.result as T
}
Expand All @@ -36,7 +34,7 @@ export const rpcBatchRequest = async (
params?: any
}[]
): Promise<any[]> => {
const res = await request(url, {
const res = await fetch(url, {
headers: {
'content-type': 'application/json',
},
Expand All @@ -50,10 +48,10 @@ export const rpcBatchRequest = async (
}))
),
})
if (res.statusCode !== 200) {
throw new Error(`indexer request failed with HTTP code ${res.statusCode}`)
if (res.status !== 200) {
throw new Error(`indexer request failed with HTTP code ${res.status}`)
}
const responseBody = await res.body.json()
const responseBody = await res.json()
if (Array.isArray(responseBody) && responseBody.every(i => 'id' in i)) {
return responseBody.sort((a, b) => a.id - b.id)
}
Expand Down
3 changes: 0 additions & 3 deletions packages/neuron-wallet/tests/controllers/sync-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ jest.doMock('models/subjects/networks', () => {
},
}
})
jest.mock('undici', () => ({
request: () => jest.fn()(),
}))
jest.mock('services/multisig', () => ({
syncMultisigOutput: () => jest.fn(),
}))
Expand Down
38 changes: 21 additions & 17 deletions packages/neuron-wallet/tests/utils/rpc-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { rpcBatchRequest, rpcRequest } from '../../src/utils/rpc-request'

const requestMock = jest.fn()
jest.mock('undici', () => ({
request: () => requestMock(),
}))

describe('rpc-batch-request', () => {
const options = [
{
Expand All @@ -17,15 +12,19 @@ describe('rpc-batch-request', () => {
},
]
it('fetch error', async () => {
requestMock.mockResolvedValueOnce({ statusCode: 500 })
global.fetch = jest.fn(() =>
Promise.resolve({
status: 500,
})
) as jest.Mock
await expect(rpcBatchRequest('url', options)).rejects.toThrow(
new Error(`indexer request failed with HTTP code 500`)
)
})
it('result is order by id', async () => {
requestMock.mockResolvedValueOnce({
statusCode: 200,
body: {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
json() {
return Promise.resolve([
{
Expand All @@ -38,8 +37,9 @@ describe('rpc-batch-request', () => {
},
])
},
},
})
})
) as jest.Mock

const res = await rpcBatchRequest('url', options)
expect(res).toEqual([
{
Expand All @@ -60,21 +60,25 @@ describe('rpc-request', () => {
params: 1,
}
it('fetch error', async () => {
requestMock.mockResolvedValueOnce({ statusCode: 500 })
global.fetch = jest.fn(() =>
Promise.resolve({
status: 500,
})
) as jest.Mock
await expect(rpcRequest('url', option)).rejects.toThrow(new Error(`indexer request failed with HTTP code 500`))
})
it('fetch success', async () => {
requestMock.mockResolvedValueOnce({
statusCode: 200,
body: {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 200,
json() {
return Promise.resolve({
id: 2,
result: 2,
})
},
},
})
})
) as jest.Mock
const res = await rpcRequest('url', option)
expect(res).toEqual(2)
})
Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2658,11 +2658,6 @@
resolved "https://registry.yarnpkg.com/@fal-works/esbuild-plugin-global-externals/-/esbuild-plugin-global-externals-2.1.2.tgz#c05ed35ad82df8e6ac616c68b92c2282bd083ba4"
integrity sha512-cEee/Z+I12mZcFJshKcCqC8tuX5hG3s+d+9nZ3LabqKF1vKdF41B92pJVCBggjAGORAeOzyyDDKrZwIkLffeOQ==

"@fastify/busboy@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.0.0.tgz#f22824caff3ae506b18207bad4126dbc6ccdb6b8"
integrity sha512-JUFJad5lv7jxj926GPgymrWQxxjPYuJNiNjNMzqT+HiuP6Vl3dk5xzG+8sTX96np0ZAluvaMzPsjhHZ5rNuNQQ==

"@floating-ui/core@^1.4.2":
version "1.5.2"
resolved "https://registry.yarnpkg.com/@floating-ui/core/-/core-1.5.2.tgz#53a0f7a98c550e63134d504f26804f6b83dbc071"
Expand Down Expand Up @@ -19981,13 +19976,6 @@ undici-types@~5.26.4:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

undici@5.28.4:
version "5.28.4"
resolved "https://registry.yarnpkg.com/undici/-/undici-5.28.4.tgz#6b280408edb6a1a604a9b20340f45b422e373068"
integrity sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g==
dependencies:
"@fastify/busboy" "^2.0.0"

unicode-canonical-property-names-ecmascript@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/unicode-canonical-property-names-ecmascript/-/unicode-canonical-property-names-ecmascript-2.0.0.tgz#301acdc525631670d39f6146e0e77ff6bbdebddc"
Expand Down

2 comments on commit c67fe9d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10110238302

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10110240362

Please sign in to comment.