Skip to content

Commit

Permalink
Add source parameter to OutboundRequest and OutboundResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 2, 2023
1 parent 2ea6431 commit a0bccd3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const blockNumRequest = await executor.readRpc();
Expand All @@ -217,6 +218,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -255,6 +257,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'snap.request' },
});

const walletRequest = await executor.readRpc();
Expand All @@ -281,6 +284,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'snap.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -416,6 +420,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const blockNumRequest = await executor.readRpc();
Expand All @@ -442,6 +447,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -482,6 +488,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'snap.request' },
});

const getSnapsRequest = await executor.readRpc();
Expand Down Expand Up @@ -516,6 +523,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'snap.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -898,6 +906,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'snap.request' },
});

const request = await executor.readRpc();
Expand Down Expand Up @@ -930,6 +939,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'snap.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -973,6 +983,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const request = await executor.readRpc();
Expand Down Expand Up @@ -1007,6 +1018,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -1051,21 +1063,25 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'fetch' },
});

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'fetch' },
});

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'fetch' },
});

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'fetch' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down Expand Up @@ -1624,6 +1640,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const blockNumRequest = await executor.readRpc();
Expand Down Expand Up @@ -1726,6 +1743,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const blockNumRequest = await executor.readRpc();
Expand Down Expand Up @@ -1813,6 +1831,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});

const blockNumRequest = await executor.readRpc();
Expand Down Expand Up @@ -1842,6 +1861,7 @@ describe('BaseSnapExecutor', () => {
expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});

expect(await executor.readCommand()).toStrictEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,17 @@ export class BaseSnapExecutor {
const request = async (args: RequestArguments) => {
const sanitizedArgs = sanitizeRequestArguments(args);
assertSnapOutboundRequest(sanitizedArgs);
this.notify({ method: 'OutboundRequest' });
this.notify({
method: 'OutboundRequest',
params: { source: 'snap.request' },
});
try {
return await withTeardown(originalRequest(sanitizedArgs), this as any);
} finally {
this.notify({ method: 'OutboundResponse' });
this.notify({
method: 'OutboundResponse',
params: { source: 'snap.request' },
});
}
};

Expand Down Expand Up @@ -479,11 +485,17 @@ export class BaseSnapExecutor {
const request = async (args: RequestArguments) => {
const sanitizedArgs = sanitizeRequestArguments(args);
assertEthereumOutboundRequest(sanitizedArgs);
this.notify({ method: 'OutboundRequest' });
this.notify({
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});
try {
return await withTeardown(originalRequest(sanitizedArgs), this as any);
} finally {
this.notify({ method: 'OutboundResponse' });
this.notify({
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ describe('Network endowments', () => {
const { fetch } = network.factory({ notify });
const result = await (await fetch('foo.com')).text();
expect(result).toStrictEqual(RESULT);
expect(notify).toHaveBeenCalledWith({
expect(notify).toHaveBeenNthCalledWith(1, {
method: 'OutboundRequest',
params: { source: 'fetch' },
});
expect(notify).toHaveBeenCalledWith({
expect(notify).toHaveBeenNthCalledWith(2, {
method: 'OutboundResponse',
params: { source: 'fetch' },
});
expect(notify).toHaveBeenNthCalledWith(3, {
method: 'OutboundRequest',
params: { source: 'fetch' },
});
expect(notify).toHaveBeenNthCalledWith(4, {
method: 'OutboundResponse',
params: { source: 'fetch' },
});
expect(notify).toHaveBeenCalledTimes(4);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,22 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
const onStart = () => {
if (!started) {
started = true;
notify({ method: 'OutboundRequest' });
notify({ method: 'OutboundRequest', params: { source: 'fetch' } });
}
};

let finished = false;
const onFinish = () => {
if (!finished) {
finished = true;
notify({ method: 'OutboundResponse' });
notify({ method: 'OutboundResponse', params: { source: 'fetch' } });
}
};

let res: Response;
let openFetchConnection: { cancel: () => Promise<void> } | undefined;
try {
notify({ method: 'OutboundRequest' });
notify({ method: 'OutboundRequest', params: { source: 'fetch' } });
const fetchPromise = fetch(input, {
...init,
signal: abortController.signal,
Expand Down Expand Up @@ -218,7 +218,7 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
if (openFetchConnection !== undefined) {
openConnections.delete(openFetchConnection);
}
notify({ method: 'OutboundResponse' });
notify({ method: 'OutboundResponse', params: { source: 'fetch' } });
}

if (res.body !== null) {
Expand Down

0 comments on commit a0bccd3

Please sign in to comment.