Skip to content

Commit

Permalink
Schopp/fetch empty (#367)
Browse files Browse the repository at this point in the history
* fix(fetch): deleted empty objects in fetch Request

* handle blob data
  • Loading branch information
Florian-Schopp authored Aug 31, 2023
1 parent 5156ad8 commit f3c4c44
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
4 changes: 1 addition & 3 deletions src/2_jsonrpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ export class JsonRPC extends EventEmitter {
} else {
this._dispatchSingleMessage(decoded)
}
this.send().catch((err) => {
this.logger.error(err)
})
this.send()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
const decodedId = (decoded && decoded.id) || ''
Expand Down
2 changes: 1 addition & 1 deletion src/3_jet/peer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class Peer extends EventEmitter {
return this.#jsonrpc
.sendRequest('unfetch', param)
.then(() => delete this.#fetcher[id])
.then(() => Promise.resolve([]))
.then(() => Promise.resolve())
} else {
delete this.#fetcher[id]
return Promise.resolve()
Expand Down
69 changes: 66 additions & 3 deletions test/jsonrpc/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
INVALID_PARAMS_CODE,
methodNotFoundError
} from '../../src/jet'
import { NotFound } from '../../src/jet'

describe('Testing JsonRpc', () => {
it('Should test basic functionality', (done) => {
const sock = sockMock()
Expand Down Expand Up @@ -316,6 +316,36 @@ describe('Testing JsonRpc', () => {
.then(() => done())
sock.emit('open')
})
class MockDecoder {
decode = () =>
JSON.stringify({
id: '1',
method: 'add',
params: { event: 'Add', path: 'foo', value: 1 }
})
}
it('Should test incoming blob request', (done) => {
const sock = sockMock()
jest.spyOn(Sock, 'Socket').mockImplementation(() => sock)

const jsonrpc = new JsonRPC(new Logger())
jsonrpc.connect(new AbortController()).then(() => {
jsonrpc.addListener('add', (_peer, id, msg) => {
expect(id).toEqual('1')
expect(msg).toEqual({ event: 'Add', path: 'foo', value: 1 })
done()
})
const blob = new Blob([], {
type: 'text/plain'
})
blob.arrayBuffer = () => Promise.resolve(new ArrayBuffer(10))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
global.TextDecoder = MockDecoder as any
sock.emit('message', { data: blob })
})
sock.emit('open')
})

it('Should test incoming request', (done) => {
const sock = sockMock()
jest.spyOn(Sock, 'Socket').mockImplementation(() => sock)
Expand Down Expand Up @@ -427,7 +457,12 @@ describe('Testing JsonRpc', () => {
jsonrpc.connect().then(() => {
sock.emit('message', { data: json })
expect(jsonrpc.sendRequest('add', { path: 'foo' }, true)).rejects.toEqual(
new NotFound()
{
code: -32602,
data: {
pathNotExists: 'Foo'
}
}
)
done()
})
Expand All @@ -449,7 +484,6 @@ describe('Testing JsonRpc', () => {
])
jest.spyOn(Sock, 'Socket').mockImplementation(() => sock)
jest.spyOn(sock, 'send').mockImplementationOnce((msg) => {
console.log('Triggered send')
expect(msg).toEqual(
JSON.stringify([
{ id: '1', method: 'add', params: { path: 'foo', value: 3 } },
Expand All @@ -474,4 +508,33 @@ describe('Testing JsonRpc', () => {

sock.emit('open')
})

it('Should test batch invalid method', (done) => {
const sock = sockMock()
jest.spyOn(Sock, 'Socket').mockImplementation(() => sock)

const msgMock = jest.fn().mockImplementation((msg) => {
expect(msg).toEqual(
JSON.stringify({
id: '1',
error: new methodNotFoundError('foo')
})
)
done()
})
sock.send = msgMock
const message = [
{
id: '1',
method: 'foo',
params: { event: 'Add', path: 'foo', value: 1 }
}
]
const jsonrpc = new JsonRPC(new Logger(), { batches: true })
jsonrpc.connect(new AbortController()).then(() => {
sock.emit('message', { data: JSON.stringify(message) })
})

sock.emit('open')
})
})
20 changes: 20 additions & 0 deletions test/peer/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,24 @@ describe('Testing Fetcher', () => {

expect(fetch.matches('test', { user: { id: 3 } })).toEqual(false)
})
it('Should create differential Fetcher', () => {
const fetch = new Fetcher().path('equals', 'test').differential()
expect(fetch.matches('test', { user: { id: 3 } })).toEqual(true)
})
it('Should create ascending Fetcher', () => {
const fetch = new Fetcher().path('equals', 'test').ascending()
expect(fetch.matches('test', { user: { id: 3 } })).toEqual(true)
})
it('Should create descending Fetcher', () => {
const fetch = new Fetcher().path('equals', 'test').descending()
expect(fetch.matches('test', { user: { id: 3 } })).toEqual(true)
})
it('Should create range Fetcher', () => {
const fetch = new Fetcher().path('equals', 'test').range(0, 2)
expect(fetch.matches('test', { user: { id: 3 } })).toEqual(true)
})
it('Should create sorted Fetcher', () => {
const fetch = new Fetcher().path('equals', 'test').sortByPath()
expect(fetch.matches('test', { user: { id: 3 } })).toEqual(true)
})
})
17 changes: 3 additions & 14 deletions test/peer/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,7 @@ describe('Testing Peer', () => {
jest.spyOn(JsonRPC, 'default').mockImplementation(() => jsonrpc)
const peer = new Peer()
peer.fetch(new Fetcher()).catch((ex) => {
expect(sendSpy).toBeCalledWith(
'fetch',
expect.objectContaining({
path: {},
value: {},
sort: {}
})
)
expect(sendSpy).toBeCalledWith('fetch', expect.objectContaining({}))
expect(ex).toBe('invalid path')
done()
})
Expand All @@ -479,9 +472,7 @@ describe('Testing Peer', () => {
expect(sendSpy).toBeCalledWith(
'fetch',
expect.objectContaining({
path: { startsWith: 'a' },
value: {},
sort: {}
path: { startsWith: 'a' }
})
)
})
Expand All @@ -490,9 +481,7 @@ describe('Testing Peer', () => {
expect(sendSpy).toBeCalledWith(
'fetch',
expect.objectContaining({
path: { equals: 'b' },
value: {},
sort: {}
path: { equals: 'b' }
})
)

Expand Down

0 comments on commit f3c4c44

Please sign in to comment.