2020-06-30 19:36:15 +00:00
|
|
|
import * as db from './database';
|
|
|
|
import { types as modelTypes } from '../models';
|
|
|
|
import { send } from '../network/network';
|
|
|
|
import { getBodyBuffer } from '../models/response';
|
|
|
|
|
2020-06-30 22:40:15 +00:00
|
|
|
export async function getSendRequestCallbackMemDb(environmentId, memDB) {
|
|
|
|
// Initialize the DB in-memory and fill it with data if we're given one
|
2020-07-06 22:59:56 +00:00
|
|
|
await db.init(modelTypes(), { inMemoryOnly: true }, true, () => {});
|
2020-06-30 19:36:15 +00:00
|
|
|
|
|
|
|
const docs = [];
|
|
|
|
for (const type of Object.keys(memDB)) {
|
|
|
|
for (const doc of memDB[type]) {
|
|
|
|
docs.push(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.batchModifyDocs({ upsert: docs, remove: [] });
|
|
|
|
|
|
|
|
// Return callback helper to send requests
|
|
|
|
return async function sendRequest(requestId) {
|
2020-06-30 22:40:15 +00:00
|
|
|
return sendAndTransform(requestId, environmentId);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSendRequestCallback(environmentId) {
|
|
|
|
return async function sendRequest(requestId) {
|
|
|
|
return sendAndTransform(requestId, environmentId);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendAndTransform(requestId, environmentId) {
|
|
|
|
const res = await send(requestId, environmentId);
|
|
|
|
const headersObj = {};
|
|
|
|
for (const h of res.headers || []) {
|
|
|
|
const name = h.name || '';
|
|
|
|
headersObj[name.toLowerCase()] = h.value || '';
|
|
|
|
}
|
2020-06-30 19:36:15 +00:00
|
|
|
|
2020-06-30 22:40:15 +00:00
|
|
|
const bodyBuffer = await getBodyBuffer(res);
|
2020-06-30 19:36:15 +00:00
|
|
|
|
2020-06-30 22:40:15 +00:00
|
|
|
return {
|
|
|
|
status: res.statusCode,
|
|
|
|
statusMessage: res.statusMessage,
|
|
|
|
data: bodyBuffer ? bodyBuffer.toString('utf8') : undefined,
|
|
|
|
headers: headersObj,
|
2020-06-30 19:36:15 +00:00
|
|
|
};
|
|
|
|
}
|