@n8n/n8n-benchmark
Version:
Cli for running benchmark tests for n8n
83 lines • 3.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.N8nApiClient = void 0;
const axios_1 = __importDefault(require("axios"));
class N8nApiClient {
constructor(apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}
async waitForInstanceToBecomeOnline() {
const HEALTH_ENDPOINT = 'healthz';
const START_TIME = Date.now();
const INTERVAL_MS = 1000;
const TIMEOUT_MS = 60000;
while (Date.now() - START_TIME < TIMEOUT_MS) {
try {
const response = await axios_1.default.request({
url: `${this.apiBaseUrl}/${HEALTH_ENDPOINT}`,
method: 'GET',
});
if (response.status === 200 && response.data.status === 'ok') {
return;
}
}
catch { }
console.log(`n8n instance not online yet, retrying in ${INTERVAL_MS / 1000} seconds...`);
await this.delay(INTERVAL_MS);
}
throw new Error(`n8n instance did not come online within ${TIMEOUT_MS / 1000} seconds`);
}
async setupOwnerIfNeeded(loginDetails) {
const response = await this.restApiRequest('/owner/setup', {
method: 'POST',
data: {
email: loginDetails.email,
password: loginDetails.password,
firstName: 'Test',
lastName: 'User',
},
validateStatus: () => true,
});
const responsePayload = response.data;
if (response.status === 200) {
console.log('Owner setup successful');
}
else if (response.status === 400) {
if (responsePayload.message === 'Instance owner already setup')
console.log('Owner already set up');
}
else if (response.status === 404) {
console.log('Owner setup endpoint not available yet, retrying in 1s...');
await this.delay(1000);
await this.setupOwnerIfNeeded(loginDetails);
}
else {
throw new Error(`Owner setup failed with status ${response.status}: ${responsePayload.message}`);
}
}
async restApiRequest(endpoint, init) {
var _a;
try {
return await axios_1.default.request({
...init,
url: this.getRestEndpointUrl(endpoint),
});
}
catch (e) {
const error = e;
console.error(`[ERROR] Request failed ${init.method} ${endpoint}`, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data);
throw error;
}
}
getRestEndpointUrl(endpoint) {
return `${this.apiBaseUrl}/rest${endpoint}`;
}
async delay(ms) {
return await new Promise((resolve) => setTimeout(resolve, ms));
}
}
exports.N8nApiClient = N8nApiClient;
//# sourceMappingURL=n8nApiClient.js.map