bitski-provider
Advanced tools
Comparing version 0.6.1-alpha.3 to 0.6.1-alpha.4
@@ -8,3 +8,2 @@ import { BlockCacheSubprovider, default as Web3ProviderEngine, DefaultFixtureSubprovider, InflightCacheSubprovider, SanitizerSubprovider, SubscriptionSubprovider, } from '@bitski/provider-engine'; | ||
options = options || {}; | ||
this.subscriptionEventName = options.subscriptionEventName || 'notification'; | ||
// Handles static responses | ||
@@ -39,14 +38,25 @@ this.addProvider(new DefaultFixtureSubprovider()); | ||
} | ||
// This property will cause web3 to wrap this provider in Web3EthereumProvider. | ||
get isEIP1193() { | ||
supportsSubscriptions() { | ||
return true; | ||
} | ||
subscribe(subscribeMethod = 'eth_subscribe', subscriptionMethod, parameters) { | ||
parameters.unshift(subscriptionMethod); | ||
return this.send(subscribeMethod, parameters); | ||
} | ||
unsubscribe(subscriptionId, unsubscribeMethod = 'eth_unsubscribe') { | ||
return this.send(unsubscribeMethod, [subscriptionId]).then((response) => { | ||
if (response) { | ||
this.removeAllListeners(subscriptionId); | ||
} | ||
return response; | ||
}); | ||
} | ||
onMessage(notification) { | ||
// Re-emit (previous behavior ~ web3 1.0.0-beta.37) | ||
this.emit('data', null, notification); | ||
if (notification && notification.params) { | ||
// EIP-1193 standard | ||
this.emit(this.subscriptionEventName, notification.params); | ||
if (notification && notification.params && notification.params.subscription) { | ||
// Current web3 behavior - emit subscription id | ||
this.emit(notification.params.subscription, notification); | ||
} | ||
} | ||
} |
@@ -6,9 +6,9 @@ import { default as Web3ProviderEngine } from '@bitski/provider-engine'; | ||
disableValidation?: boolean; | ||
subscriptionEventName?: string; | ||
} | ||
export declare class BitskiEngine extends Web3ProviderEngine { | ||
protected subscriptionEventName: string; | ||
constructor(options?: BitskiEngineOptions); | ||
readonly isEIP1193: boolean; | ||
supportsSubscriptions(): boolean; | ||
subscribe(subscribeMethod: string | undefined, subscriptionMethod: string, parameters: any[]): Promise<string>; | ||
unsubscribe(subscriptionId: string, unsubscribeMethod?: string): Promise<boolean>; | ||
protected onMessage(notification: any): void; | ||
} |
@@ -26,3 +26,2 @@ var __importStar = (this && this.__importStar) || function (mod) { | ||
options = options || {}; | ||
this.subscriptionEventName = options.subscriptionEventName || 'notification'; | ||
// Handles static responses | ||
@@ -57,12 +56,23 @@ this.addProvider(new provider_engine_1.DefaultFixtureSubprovider()); | ||
} | ||
// This property will cause web3 to wrap this provider in Web3EthereumProvider. | ||
get isEIP1193() { | ||
supportsSubscriptions() { | ||
return true; | ||
} | ||
subscribe(subscribeMethod = 'eth_subscribe', subscriptionMethod, parameters) { | ||
parameters.unshift(subscriptionMethod); | ||
return this.send(subscribeMethod, parameters); | ||
} | ||
unsubscribe(subscriptionId, unsubscribeMethod = 'eth_unsubscribe') { | ||
return this.send(unsubscribeMethod, [subscriptionId]).then((response) => { | ||
if (response) { | ||
this.removeAllListeners(subscriptionId); | ||
} | ||
return response; | ||
}); | ||
} | ||
onMessage(notification) { | ||
// Re-emit (previous behavior ~ web3 1.0.0-beta.37) | ||
this.emit('data', null, notification); | ||
if (notification && notification.params) { | ||
// EIP-1193 standard | ||
this.emit(this.subscriptionEventName, notification.params); | ||
if (notification && notification.params && notification.params.subscription) { | ||
// Current web3 behavior - emit subscription id | ||
this.emit(notification.params.subscription, notification); | ||
} | ||
@@ -69,0 +79,0 @@ } |
@@ -12,3 +12,3 @@ { | ||
}, | ||
"version": "0.6.1-alpha.3", | ||
"version": "0.6.1-alpha.4", | ||
"scripts": { | ||
@@ -21,3 +21,3 @@ "test": "jest", | ||
"dependencies": { | ||
"@bitski/provider-engine": "^0.3.0", | ||
"@bitski/provider-engine": "^0.4.1", | ||
"@types/ethereum-protocol": "^1.0.0", | ||
@@ -29,3 +29,3 @@ "json-rpc-error": "^2.0.0" | ||
}, | ||
"gitHead": "a5e3a263e8f0f84bf10422a3aafcf4efb38294e2" | ||
"gitHead": "7996e8da44ae5aac9ede86861e91524198d8e3e2" | ||
} |
@@ -20,4 +20,2 @@ import { | ||
disableValidation?: boolean; | ||
// Subscription event name, defaults to 'notification' | ||
subscriptionEventName?: string; | ||
} | ||
@@ -27,5 +25,2 @@ | ||
// Currently web3 expects 'notification' but the spec currently calls for 'eth_subscription' | ||
protected subscriptionEventName: string; | ||
constructor(options?: BitskiEngineOptions) { | ||
@@ -35,4 +30,2 @@ super(options); | ||
this.subscriptionEventName = options.subscriptionEventName || 'notification'; | ||
// Handles static responses | ||
@@ -76,13 +69,26 @@ this.addProvider(new DefaultFixtureSubprovider()); | ||
// This property will cause web3 to wrap this provider in Web3EthereumProvider. | ||
public get isEIP1193() { | ||
public supportsSubscriptions(): boolean { | ||
return true; | ||
} | ||
public subscribe(subscribeMethod: string = 'eth_subscribe', subscriptionMethod: string, parameters: any[]): Promise<string> { | ||
parameters.unshift(subscriptionMethod); | ||
return this.send(subscribeMethod, parameters); | ||
} | ||
public unsubscribe(subscriptionId: string, unsubscribeMethod: string = 'eth_unsubscribe'): Promise<boolean> { | ||
return this.send(unsubscribeMethod, [subscriptionId]).then((response) => { | ||
if (response) { | ||
this.removeAllListeners(subscriptionId); | ||
} | ||
return response; | ||
}); | ||
} | ||
protected onMessage(notification) { | ||
// Re-emit (previous behavior ~ web3 1.0.0-beta.37) | ||
this.emit('data', null, notification); | ||
if (notification && notification.params) { | ||
// EIP-1193 standard | ||
this.emit(this.subscriptionEventName, notification.params); | ||
if (notification && notification.params && notification.params.subscription) { | ||
// Current web3 behavior - emit subscription id | ||
this.emit(notification.params.subscription, notification); | ||
} | ||
@@ -89,0 +95,0 @@ } |
@@ -53,6 +53,3 @@ import { AuthenticatedFetchSubprovider } from '../src/index'; | ||
const sendRequestSpy = jest.spyOn(provider, 'sendRequest'); | ||
const request = createRequest('eth_accounts', []); | ||
provider.originHttpHeaderKey = 'Origin'; | ||
request.origin = 'http://foo.bar'; | ||
return engine.sendAsync(request, (error, value) => { | ||
engine.send('eth_accounts', []).then((value) => { | ||
expect(sendRequestSpy).toHaveBeenCalled(); | ||
@@ -62,4 +59,3 @@ const params = sendRequestSpy.mock.calls[0][0]; | ||
expect(params.headers['X-API-KEY']).toBe('test-client-id'); | ||
expect(error).toBeNull(); | ||
expect(value.result).toBe('foo'); | ||
expect(value).toBe('foo'); | ||
done(); | ||
@@ -75,6 +71,3 @@ }); | ||
// @ts-ignore | ||
const request = createRequest('eth_accounts', []); | ||
return engine.sendAsync(request, (error) => { | ||
engine.send('eth_accounts', []).catch((error) => { | ||
expect(error.message).toMatch(/Not logged in/); | ||
@@ -92,9 +85,5 @@ done(); | ||
// @ts-ignore | ||
const request = createRequest('eth_peerCount', []); | ||
return engine.sendAsync(request, (error, value) => { | ||
return engine.send('eth_peerCount', []).then((value) => { | ||
expect(fetch.mock.calls.length).toBe(3); | ||
expect(error).toBeNull(); | ||
expect(value.result).toBe('foo'); | ||
expect(value).toBe('foo'); | ||
done(); | ||
@@ -111,9 +100,5 @@ }); | ||
// @ts-ignore | ||
const request = createRequest('eth_peerCount', []); | ||
return engine.sendAsync(request, (error, value) => { | ||
engine.send('eth_peerCount', []).catch((error) => { | ||
expect(fetch.mock.calls.length).toBe(1); | ||
expect(error.message).toMatch(/Not Authorized/); | ||
expect(value.result).toBeUndefined(); | ||
done(); | ||
@@ -132,5 +117,4 @@ }); | ||
return engine.sendAsync(request, (error, value) => { | ||
return engine.send('eth_peerCount', []).catch((error) => { | ||
expect(error.message).toMatch(/All retries exhausted/); | ||
expect(value.result).toBeUndefined(); | ||
expect(fetch.mock.calls.length).toBe(5); | ||
@@ -154,5 +138,4 @@ done(); | ||
const sendRequestSpy = jest.spyOn(provider, 'sendRequest'); | ||
const request = createRequest('eth_peerCount', []); | ||
return engine.sendAsync(request, (error, value) => { | ||
engine.send('eth_peerCount', []).then((value) => { | ||
expect(sendRequestSpy).toHaveBeenCalled(); | ||
@@ -162,4 +145,3 @@ const params = sendRequestSpy.mock.calls[0][0]; | ||
expect(params.headers['X-API-KEY']).toBe('test-client-id'); | ||
expect(error).toBeNull(); | ||
expect(value.result).toBe('foo'); | ||
expect(value).toBe('foo'); | ||
done(); | ||
@@ -181,7 +163,7 @@ }); | ||
})); | ||
// @ts-ignore | ||
const invalidateTokenSpy = jest.spyOn(provider.accessTokenProvider, 'invalidateToken'); | ||
const request = createRequest('eth_peerCount', []); | ||
return engine.sendAsync(request, (error, value) => { | ||
engine.send('eth_peerCount', []).catch((error) => { | ||
expect(invalidateTokenSpy).toHaveBeenCalled(); | ||
@@ -188,0 +170,0 @@ expect(error).toBeDefined(); |
@@ -15,8 +15,2 @@ import { FixtureSubprovider } from '@bitski/provider-engine'; | ||
describe('initialization', () => { | ||
test('it signals support for EIP-1193', () => { | ||
expect.assertions(1); | ||
const engine = createEngine(); | ||
expect(engine.isEIP1193).toBe(true); | ||
}); | ||
test('it exists', () => { | ||
@@ -37,6 +31,30 @@ expect.assertions(2); | ||
describe('when handling subscriptions', () => { | ||
test('it has support for subscriptions', () => { | ||
const engine = createEngine(); | ||
expect(engine.supportsSubscriptions()).toBe(true); | ||
}); | ||
test('it emits EIP-1193 events', (done) => { | ||
test('it can subscribe to events', (done) => { | ||
const engine = createEngine(); | ||
const spy = jest.spyOn(engine, 'send').mockResolvedValue('0x1'); | ||
engine.subscribe('eth_subscribe', 'logs', []).then((result) => { | ||
expect(result).toBe('0x1'); | ||
expect(spy).toBeCalled(); | ||
done(); | ||
}); | ||
}); | ||
test('it can unsubscribe from events', (done) => { | ||
const engine = createEngine(); | ||
const spy = jest.spyOn(engine, 'send').mockResolvedValue(true); | ||
engine.unsubscribe('0x1').then((result) => { | ||
expect(result).toBe(true); | ||
expect(spy).toBeCalled(); | ||
done(); | ||
}); | ||
}); | ||
test('it emits events with subscription id', (done) => { | ||
expect.assertions(1); | ||
const engine = createEngine({ subscriptionEventName: 'notification' }); | ||
const engine = createEngine(); | ||
const subscriptionSubprovider = engine._providers[5]; | ||
@@ -53,4 +71,4 @@ const notification = { | ||
}; | ||
engine.on('notification', (result) => { | ||
expect(result).toEqual(notification.params); | ||
engine.on('0x1', (result) => { | ||
expect(result).toEqual(notification); | ||
done(); | ||
@@ -84,6 +102,6 @@ }); | ||
expect.assertions(1); | ||
const engine = createEngine({ subscriptionEventName: 'notification' }); | ||
const engine = createEngine(); | ||
const subscriptionSubprovider = engine._providers[5]; | ||
let called = false; | ||
engine.on('notification', () => { | ||
engine.on('0x1', () => { | ||
called = true; | ||
@@ -90,0 +108,0 @@ }); |
Sorry, the diff of this file is not supported yet
263684
2656
+ Added@bitski/provider-engine@0.4.2(transitive)
- Removed@bitski/provider-engine@0.3.0(transitive)