Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Code Node): Bind helper methods to the correct context #9380

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/nodes-base/nodes/Code/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ export interface SandboxContext extends IWorkflowDataProxyData {
export const REQUIRED_N8N_ITEM_KEYS = new Set(['json', 'binary', 'pairedItem', 'error']);

export function getSandboxContext(this: IExecuteFunctions, index: number): SandboxContext {
const helpers = {
...this.helpers,
httpRequestWithAuthentication: this.helpers.httpRequestWithAuthentication.bind(this),
requestWithAuthenticationPaginated: this.helpers.requestWithAuthenticationPaginated.bind(this),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great for these helpers, should we do it for all?

const helpers = mapValues(this.helpers, (helper) => helper.bind(this));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't requestWIthAuthentication still fail as I think it uses the passed in node parameters to work out which credential type to use which the code node doesn't have access to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elsmr I explicitly didn't want to bind any other helper because most helper methods don't need a bound this, and the others that do are deprecated.

@Joffcom this.helpers.requestWIthAuthentication does not take those additional parameters. They are picked from a bound outer scope. https://github.com/n8n-io/n8n/blob/master/packages/core/src/NodeExecuteFunctions.ts#L3129

};
return {
// from NodeExecuteFunctions
$getNodeParameter: this.getNodeParameter,
$getWorkflowStaticData: this.getWorkflowStaticData,
helpers: this.helpers,
helpers,

// to bring in all $-prefixed vars and methods from WorkflowDataProxy
// $node, $items(), $parameter, $json, $env, etc.
Expand Down
Loading