Skip to content

Commit

Permalink
AC - refactored to work with sandbox and integration
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cleveland committed Nov 14, 2023
1 parent 4096f4f commit dccf27b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 84 deletions.
72 changes: 35 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,35 @@ let url = process.env.MESH_URL || "https://localhost:8700";
let sharedKey = process.env.MESH_SHARED_KEY;

// This should be disabled for sandbox use, but enabled for integration and prod
let tlsEnabled = process.env.MESH_TLS_ENABLED;
let sandbox = process.env.MESH_SANDBOX;

// Setup the https agents for tls, you can ignore this for sandbox
let senderAgent = new Agent({
cert: readFileSync(process.env.MESH_SENDER_CERT_LOCATION),
key: readFileSync(process.env.MESH_SENDER_KEY_LOCATION),
rejectUnauthorized: false,
});
console.log(sandbox);

let receiverAgent = new Agent({
cert: readFileSync(process.env.MESH_RECEIVER_CERT_LOCATION),
key: readFileSync(process.env.MESH_RECEIVER_KEY_LOCATION),
rejectUnauthorized: false,
});
let senderAgent;
let receiverAgent;

if (sandbox === "true") {
// just setup to ignore self-signed certs
senderAgent = new Agent({
rejectUnauthorized: false,
});

receiverAgent = new Agent({
rejectUnauthorized: false,
});
} else {
// Setup the https agents for integration, you can ignore this for sandbox
senderAgent = new Agent({
cert: readFileSync(process.env.MESH_SENDER_CERT_LOCATION),
key: readFileSync(process.env.MESH_SENDER_KEY_LOCATION),
rejectUnauthorized: false,
});
receiverAgent = new Agent({
cert: readFileSync(process.env.MESH_RECEIVER_CERT_LOCATION),
key: readFileSync(process.env.MESH_RECEIVER_KEY_LOCATION),
rejectUnauthorized: false,
});
}

let logLevel = process.env.LOG_LEVEL || "SILENT";
log.setLevel(log.levels[logLevel]);
Expand Down Expand Up @@ -61,7 +76,6 @@ async function createMessages() {
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});

Expand All @@ -73,7 +87,6 @@ async function createMessages() {
mailboxPassword: senderMailboxPassword,
message: messageContent,
mailboxTarget: receiverMailboxID,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});
log.debug("New message created with an ID: " + newMessage.data["message_id"]);
Expand All @@ -88,7 +101,6 @@ async function createMessageChunks() {
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});

Expand All @@ -98,7 +110,6 @@ async function createMessageChunks() {
mailboxPassword: senderMailboxPassword,
mailboxTarget: receiverMailboxID,
messageFile: messageFile,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});
}
Expand All @@ -111,7 +122,6 @@ async function receiveMessage() {
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});

Expand All @@ -122,7 +132,6 @@ async function receiveMessage() {
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});

Expand All @@ -141,7 +150,6 @@ async function receiveMessage() {
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
messageID: message,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});
try {
Expand Down Expand Up @@ -184,15 +192,14 @@ async function receiveMessage() {

// mark the messages as read
log.debug("clearing the message from the mailbox");
await markAsRead(
url,
receiverMailboxID,
receiverMailboxPassword,
sharedKey,
message,
tlsEnabled,
receiverAgent
);
await markAsRead({
url: url,
mailbox_id: receiverMailboxID,
mailbox_password: receiverMailboxPassword,
shared_key: sharedKey,
message: message,
agent: receiverAgent,
});
try {
} catch {
console.error("ERROR: Failure marking message" + message + " as read");
Expand All @@ -209,12 +216,3 @@ log.debug("\nwaiting 30 seconds for mesh to process the message");
await waitThirtySeconds();
log.debug("\nchecking if the message has arrived");
await receiveMessage();
// await sendMessageChunks({
// url: url,
// mailboxID: senderMailboxID,
// mailboxPassword: senderMailboxPassword,
// mailboxTarget: receiverMailboxID,
// messageFile: messageFile,
// tlsEnabled: tlsEnabled,
// agent: senderAgent,
// });
9 changes: 4 additions & 5 deletions src/get/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ async function handShake({
mailboxID,
mailboxPassword,
sharedKey,
tlsEnabled,
agent,
}) {
const full_url = `${url}/messageexchange/${mailboxID}`;
const headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
const response = await axios.get(full_url, config);
// attach agent to headers
config.httpsAgent = agent;
// const response = await axios.get(full_url, config);
try {
const response = await axios.get(full_url, config);
if (response.status === 200) {
log.info(`Handshake successful, status ${response.status}\n`);
return response;
Expand Down
14 changes: 5 additions & 9 deletions src/get/message_count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import axios from "axios";
import { Agent } from "https";
import { readFileSync } from "fs";
import log from "loglevel";
import generateHeaders from "./generate_headers.js";

Expand All @@ -9,17 +7,15 @@ async function getMessageCount({
mailboxID,
mailboxPassword,
sharedKey,
tlsEnabled,
agent,
}) {
const fullUrl = `${url}/messageexchange/${mailboxID}/inbox`;
const headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);
let fullUrl = `${url}/messageexchange/${mailboxID}/inbox`;
let headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
const response = await axios.get(fullUrl, config);
// attach agent to headers
config.httpsAgent = agent;
let response = await axios.get(fullUrl, config);
try {
if (response.status === 200) {
return response;
Expand Down
25 changes: 14 additions & 11 deletions src/get/read_message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios from "axios";
import { Agent } from "https";
import { readFileSync, writeFile } from "fs";
import fs from "fs";
import log from "loglevel";
import generateHeaders from "./generate_headers.js";

Expand All @@ -10,17 +9,22 @@ async function readMessage({
mailboxPassword,
sharedKey,
messageID,
tlsEnabled,
agent,
}) {
let directoryPath = "input";
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
log.debug("Directory created:", directoryPath);
} else {
log.debug("Directory already exists:", directoryPath);
}
let chunkedMessage = "";
let fullUrl = `${url}/messageexchange/${mailboxID}/inbox/${messageID}`;
let headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
// attach agent to headers
config.httpsAgent = agent;

let response = await axios.get(fullUrl, config);

Expand All @@ -33,8 +37,8 @@ async function readMessage({
// If the message is chunked then loop through all the chunks and return the assembled message
do {
chunkedMessage += response.data;
const chunkRange = response.headers["mex-chunk-range"];
const [currentChunk, totalChunks] = chunkRange.split(":").map(Number);
let chunkRange = response.headers["mex-chunk-range"];
let [currentChunk, totalChunks] = chunkRange.split(":").map(Number);
log.debug(`chunk ${currentChunk} of ${totalChunks} downloaded`);
if (currentChunk < totalChunks) {
let headers = await generateHeaders(
Expand All @@ -44,9 +48,8 @@ async function readMessage({
);

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
// attach agent to headers
config.httpsAgent = agent;
// If there are more chunks to fetch, update the URL for the next request
fullUrl = `${url}/messageexchange/${mailboxID}/inbox/${messageID}/${
currentChunk + 1
Expand Down
6 changes: 2 additions & 4 deletions src/post/send_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ async function sendMessage({
mailboxPassword,
message,
mailboxTarget,
tlsEnabled,
agent,
}) {
const fullUrl = `${url}/messageexchange/${mailboxID}/outbox`;
Expand All @@ -19,9 +18,8 @@ async function sendMessage({
);

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
// attach agent to headers
config.httpsAgent = agent;
const response = await axios.post(fullUrl, { data: message }, config);
try {
if (response.status === 202) {
Expand Down
13 changes: 9 additions & 4 deletions src/post/send_message_chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class CsvChunker extends stream.Transform {
}

async function splitAndZipCsv(filePath) {
let directoryPath = "output";
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
console.log("Directory created:", directoryPath);
} else {
console.log("Directory already exists:", directoryPath);
}
let csvChunker = new CsvChunker();
let count = 0;

Expand Down Expand Up @@ -74,7 +81,6 @@ async function sendMessageChunks({
mailboxPassword,
mailboxTarget,
messageFile,
tlsEnabled,
agent,
}) {
let fileCount = 0;
Expand Down Expand Up @@ -108,9 +114,8 @@ async function sendMessageChunks({
headers["content-encoding"] = "gzip";

let config = { headers: headers };
if (tlsEnabled) {
config.httpsAgent = agent;
}
// attach agent to headers
config.httpsAgent = agent;

let chunkedFilePath = `output/chunk_${chunk}.gzip`;
let fileContent = fs.readFileSync(chunkedFilePath);
Expand Down
22 changes: 8 additions & 14 deletions src/put/mark_as_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@ import { readFileSync } from "fs";
import log from "loglevel";
import generateHeaders from "./generate_headers.js";

async function markAsRead(
async function markAsRead({
url,
mailbox_id,
mailbox_password,
shared_key,
message,
tls_enabled,
agent
) {
const full_url = `${url}/messageexchange/${mailbox_id}/inbox/${message}/status/acknowledged`;
const headers = await generateHeaders(
mailbox_id,
mailbox_password,
shared_key
);
agent,
}) {
let full_url = `${url}/messageexchange/${mailbox_id}/inbox/${message}/status/acknowledged`;
let headers = await generateHeaders(mailbox_id, mailbox_password, shared_key);

let config = { headers: headers };
if (tls_enabled) {
config.httpsAgent = agent;
}
const response = await axios.put(full_url, { messageId: message }, config);
// attach agent to headers
config.httpsAgent = agent;
let response = await axios.put(full_url, { messageId: message }, config);
try {
if (response.status === 200) {
log.info("message cleared\n");
Expand Down

0 comments on commit dccf27b

Please sign in to comment.