Skip to content

Commit

Permalink
fix(benchmark): Fix edge case in log in (no-changelog)
Browse files Browse the repository at this point in the history
Apparently n8n can respond with 200 OK to a login request but with a body
containing the string 'n8n is starting up. Please wait' during startup. This
adds retry in the case where the n8n instance is still starting up.
  • Loading branch information
tomi committed Aug 29, 2024
1 parent 1491cbd commit 1f664e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { strict as assert } from 'node:assert';
import type { AxiosRequestConfig } from 'axios';
import { N8nApiClient } from './n8nApiClient';

Expand All @@ -16,15 +15,33 @@ export class AuthenticatedN8nApiClient extends N8nApiClient {
email: string;
password: string;
},
) {
): Promise<AuthenticatedN8nApiClient> {
const response = await apiClient.restApiRequest('/login', {
method: 'POST',
data: loginDetails,
});

if (response.data === 'n8n is starting up. Please wait') {
await apiClient.delay(1000);
return await this.createUsingUsernameAndPassword(apiClient, loginDetails);
}

const cookieHeader = response.headers['set-cookie'];
const authCookie = Array.isArray(cookieHeader) ? cookieHeader.join('; ') : cookieHeader;
assert(authCookie);
if (!authCookie) {
throw new Error(
'Did not receive authentication cookie even tho login succeeded: ' +
JSON.stringify(
{
status: response.status,
headers: response.headers,
data: response.data,
},
null,
2,
),
);
}

return new AuthenticatedN8nApiClient(apiClient.apiBaseUrl, authCookie);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/@n8n/benchmark/src/n8nApiClient/n8nApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export class N8nApiClient {
}
}

protected getRestEndpointUrl(endpoint: string) {
return `${this.apiBaseUrl}/rest${endpoint}`;
async delay(ms: number): Promise<void> {
return await new Promise((resolve) => setTimeout(resolve, ms));
}

private async delay(ms: number): Promise<void> {
return await new Promise((resolve) => setTimeout(resolve, ms));
protected getRestEndpointUrl(endpoint: string) {
return `${this.apiBaseUrl}/rest${endpoint}`;
}
}

0 comments on commit 1f664e1

Please sign in to comment.