Skip to content

Commit

Permalink
Fix top level return incompatibility (#818)
Browse files Browse the repository at this point in the history
* Run transformation functions at top level

* changelog
  • Loading branch information
Half-Shot authored Sep 26, 2023
1 parent 257c93e commit c3cb131
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/818.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix transformation scripts breaking if they include a `return` at the top level
2 changes: 1 addition & 1 deletion src/Connections/GenericHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection
ctx.runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + TRANSFORMATION_TIMEOUT_MS));
try {
ctx.setProp(ctx.global, 'HookshotApiVersion', ctx.newString('v2'));
const ctxResult = ctx.evalCode(`const data = ${JSON.stringify(data)};\n\n${this.state.transformationFunction}`);
const ctxResult = ctx.evalCode(`const data = ${JSON.stringify(data)};\n(() => { ${this.state.transformationFunction} })();`);

if (ctxResult.error) {
const e = Error(`Transformation failed to run: ${JSON.stringify(ctx.dump(ctxResult.error))}`);
Expand Down
24 changes: 24 additions & 0 deletions tests/connections/GenericHookTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ROOM_ID = "!foo:bar";

const V1TFFunction = "result = `The answer to '${data.question}' is ${data.answer}`;";
const V2TFFunction = "result = {plain: `The answer to '${data.question}' is ${data.answer}`, version: 'v2'}";
const V2TFFunctionWithReturn = "result = {plain: `The answer to '${data.question}' is ${data.answer}`, version: 'v2'}; return;";

async function testSimpleWebhook(connection: GenericHookConnection, mq: LocalMQ, testValue: string) {
const webhookData = {simple: testValue};
Expand Down Expand Up @@ -199,6 +200,29 @@ describe("GenericHookConnection", () => {
type: 'm.room.message',
});
});
it("will handle a hook event with a top-level return", async () => {
const webhookData = {question: 'What is the meaning of life?', answer: 42};
const [connection, mq] = createGenericHook({name: 'test', transformationFunction: V2TFFunctionWithReturn}, {
enabled: true,
urlPrefix: "https://example.com/webhookurl",
allowJsTransformationFunctions: true,
}
);
const messagePromise = handleMessage(mq);
await connection.onGenericHook(webhookData);
expect(await messagePromise).to.deep.equal({
roomId: ROOM_ID,
sender: connection.getUserId(),
content: {
body: "The answer to 'What is the meaning of life?' is 42",
format: "org.matrix.custom.html",
formatted_body: "<p>The answer to 'What is the meaning of life?' is 42</p>",
msgtype: "m.notice",
"uk.half-shot.hookshot.webhook_data": webhookData,
},
type: 'm.room.message',
});
});
it("will fail to handle a webhook with an invalid script", async () => {
const webhookData = {question: 'What is the meaning of life?', answer: 42};
const [connection, mq] = createGenericHook({name: 'test', transformationFunction: "bibble bobble"}, {
Expand Down

0 comments on commit c3cb131

Please sign in to comment.