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

feat: prevent incorrect signal usage #488

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Changes from 2 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
32 changes: 32 additions & 0 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
) => {
const payload = p ?? {};
debug(`Calling ${method}`);
if (signal !== undefined) validateSignal(method, payload, signal);
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
// General config
const opts = this.options;
const formDataRequired = requiresFormDataUpload(payload);
Expand Down Expand Up @@ -417,3 +418,34 @@
else sig.addEventListener("abort", abort);
return { abort, signal: abortController.signal };
}

function validateSignal(
method: string,
payload: Record<string, unknown>,
signal: AbortSignal,

Check warning on line 425 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L422-L425

Added lines #L422 - L425 were not covered by tests
) {
// We use a very simple heuristic to check for AbortSignal instances
// in order to avoid doing a runtime-specific version of `instanceof`.
if (typeof signal.addEventListener === "function") {
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Check warning on line 431 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L427-L431

Added lines #L427 - L431 were not covered by tests

let payload0 = JSON.stringify(payload);
if (payload0.length > 20) {
payload0 = payload0.substring(0, 16) + " ...";
}
let payload1 = JSON.stringify(signal);
if (payload1.length > 20) {
payload1 = payload1.substring(0, 16) + " ...";
}
throw new Error(
`Incorrect abort signal instance found! \
You passed two payloads to '${method}' but you should merge \
the second one containing '${payload1}' into the first one \
containing '${payload0}'! If you are using context shortcuts, \

Check warning on line 445 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L433-L445

Added lines #L433 - L445 were not covered by tests
you may want to use a method on 'ctx.api' instead.

If you want to prevent such mistakes in the future, \
consider using TypeScript. https://www.typescriptlang.org/`,

Check warning on line 449 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L449

Added line #L449 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Good try 😀

Copy link
Member Author

Choose a reason for hiding this comment

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

I hope it will help someone

);
}

Check warning on line 451 in src/core/client.ts

View check run for this annotation

Codecov / codecov/patch

src/core/client.ts#L451

Added line #L451 was not covered by tests
Loading