Skip to content

Commit

Permalink
fix: fail starting Snap if no exports found (#2357)
Browse files Browse the repository at this point in the history
If no valid exports were found during initial eval of the Snap, fail
early instead of potentially throwing misleading errors later.

---------

Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
  • Loading branch information
FrederikBolding and Mrtenz committed Apr 24, 2024
1 parent da52434 commit 8cc7ad9
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ describe('AbstractExecutionService', () => {
await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

const { streams } = service.getJobs().values().next().value;
Expand All @@ -61,9 +61,9 @@ describe('AbstractExecutionService', () => {
await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

const { streams } = service.getJobs().values().next().value;
Expand Down Expand Up @@ -111,9 +111,9 @@ describe('AbstractExecutionService', () => {
await service.executeSnap({
snapId: MOCK_SNAP_ID,
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

await expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe('IframeExecutionService', () => {
const response = await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

expect(response).toBe('OK');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('NodeProcessExecutionService', () => {
const response = await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});
expect(response).toBe('OK');
await service.terminateAllSnaps();
Expand Down Expand Up @@ -205,6 +205,7 @@ describe('NodeProcessExecutionService', () => {
const result = await service.executeSnap({
snapId,
sourceCode: `
module.exports.onRpcRequest = () => null;
console.log('foo');
console.error('bar');
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('NodeThreadExecutionService', () => {
const response = await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});
expect(response).toBe('OK');
await service.terminateAllSnaps();
Expand Down Expand Up @@ -206,6 +206,7 @@ describe('NodeThreadExecutionService', () => {
const result = await service.executeSnap({
snapId,
sourceCode: `
module.exports.onRpcRequest = () => null;
console.log('foo');
console.error('bar');
`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ describe('WebWorkerExecutionService', () => {
await service.executeSnap({
snapId: MOCK_SNAP_ID,
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

await service.executeSnap({
snapId: MOCK_LOCAL_SNAP_ID,
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

expect(document.getElementById(WORKER_POOL_ID)).not.toBeNull();
Expand All @@ -64,9 +64,9 @@ describe('WebWorkerExecutionService', () => {
const response = await service.executeSnap({
snapId: 'TestSnap',
sourceCode: `
console.log('foo');
module.exports.onRpcRequest = () => null;
`,
endowments: ['console'],
endowments: [],
});

expect(response).toBe('OK');
Expand Down
4 changes: 2 additions & 2 deletions packages/snaps-execution-environments/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 80,
"functions": 90.06,
"lines": 90.75,
"statements": 90.12
"lines": 90.76,
"statements": 90.13
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ describe('BaseSnapExecutor', () => {
const CODE = `
setTimeout(() => { throw new Error('setTimeout executed'); }, 10);
setInterval(() => { throw new Error('setInterval executed'); }, 10);
exports.onRpcRequest = () => null;
`;

const executor = new TestSnapExecutor();
Expand Down Expand Up @@ -1242,6 +1244,28 @@ describe('BaseSnapExecutor', () => {
});
});

it("throws if the Snap doesn't export anything", async () => {
const CODE = ``;

const executor = new TestSnapExecutor();
await executor.executeSnap(1, MOCK_SNAP_ID, CODE, []);

expect(await executor.readCommand()).toStrictEqual({
jsonrpc: '2.0',
id: 1,
error: expect.objectContaining({
code: -32603,
message: `Error while running snap '${MOCK_SNAP_ID}': Snap has no valid exports.`,
data: {
cause: expect.objectContaining({
code: -32603,
message: 'Snap has no valid exports.',
}),
},
}),
});
});

it('supports onTransaction export', async () => {
const CODE = `
module.exports.onTransaction = ({ transaction, chainId, transactionOrigin }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ export class BaseSnapExecutor {
}
return acc;
}, {});

// If the Snap has no valid exports after this, fail.
assert(Object.keys(data.exports).length > 0, 'Snap has no valid exports.');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-jest/src/internals/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('startServer', () => {
"registry": "https://registry.npmjs.org",
},
},
"shasum": "D3ANeNZ7C1Ynx0GTP07afj72Jq06Srlq49QZkhICY+E=",
"shasum": "uaLwMO39qzKbshqPM6W2Ju7gkO/czuwgNKpjzXRXJj0=",
},
"version": "1.0.0",
}
Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-jest/src/test-utils/snap/snap.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// eslint-disable-next-line no-console
console.log('Hello, world!');

module.exports.onRpcRequest = () => null;
2 changes: 1 addition & 1 deletion packages/snaps-jest/src/test-utils/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "baz",
"version": "1.0.0",
"source": {
"shasum": "D3ANeNZ7C1Ynx0GTP07afj72Jq06Srlq49QZkhICY+E=",
"shasum": "uaLwMO39qzKbshqPM6W2Ju7gkO/czuwgNKpjzXRXJj0=",
"location": {
"npm": {
"filePath": "snap.js",
Expand Down

0 comments on commit 8cc7ad9

Please sign in to comment.