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 9, 2023
1 parent 133ca2d commit 0e044db
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 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 @@ -1731,6 +1748,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 @@ -1823,6 +1841,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 @@ -1852,6 +1871,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 @@ -110,7 +110,9 @@ const EXECUTION_ENVIRONMENT_METHODS = {

type Methods = typeof EXECUTION_ENVIRONMENT_METHODS;

export type NotifyFunction = (notification: Omit<JsonRpcNotification, 'jsonrpc'>) => Promise<void>;
export type NotifyFunction = (
notification: Omit<JsonRpcNotification, 'jsonrpc'>,
) => Promise<void>;

export class BaseSnapExecutor {
private readonly snapData: Map<string, SnapData>;
Expand Down Expand Up @@ -457,11 +459,17 @@ export class BaseSnapExecutor {
assertSnapOutboundRequest(sanitizedArgs);
return await withTeardown(
(async () => {
await this.#notify({ method: 'OutboundRequest' });
await this.#notify({
method: 'OutboundRequest',
params: { source: 'snap.request' },
});
try {
return await originalRequest(sanitizedArgs);
} finally {
await this.#notify({ method: 'OutboundResponse' });
await this.#notify({
method: 'OutboundResponse',
params: { source: 'snap.request' },
});
}
})(),
this as any,
Expand Down Expand Up @@ -503,11 +511,17 @@ export class BaseSnapExecutor {
assertEthereumOutboundRequest(sanitizedArgs);
return await withTeardown(
(async () => {
await this.#notify({ method: 'OutboundRequest' });
await this.#notify({
method: 'OutboundRequest',
params: { source: 'ethereum.request' },
});
try {
return await originalRequest(sanitizedArgs);
} finally {
await this.#notify({ method: 'OutboundResponse' });
await this.#notify({
method: 'OutboundResponse',
params: { source: 'ethereum.request' },
});
}
})(),
this as any,
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 @@ -12,15 +12,15 @@ class ResponseWrapper implements Response {

#ogResponse: Response;

#onStart: () => void;
#onStart: () => Promise<void>;

#onFinish: () => void;
#onFinish: () => Promise<void>;

constructor(
ogResponse: Response,
teardownRef: { lastTeardown: number },
onStart: () => void,
onFinish: () => void,
onStart: () => Promise<void>,
onFinish: () => Promise<void>,
) {
this.#ogResponse = ogResponse;
this.#teardownRef = teardownRef;
Expand Down Expand Up @@ -175,22 +175,28 @@ const createNetwork = ({ notify }: EndowmentFactoryOptions = {}) => {
const onStart = async () => {
if (!started) {
started = true;
await notify({ method: 'OutboundRequest' });
await notify({
method: 'OutboundRequest',
params: { source: 'fetch' },
});
}
};

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

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

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

0 comments on commit 0e044db

Please sign in to comment.