Skip to content

Commit

Permalink
AC - reading and writting chunks is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cleveland committed Nov 14, 2023
1 parent 5020733 commit 4096f4f
Show file tree
Hide file tree
Showing 9 changed files with 100,217 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

# Please, add your custom content below!
node_modules
output/*
output/*
input/*
215 changes: 121 additions & 94 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import log from "loglevel";
import { Agent } from "https";
import { readFileSync } from "fs";
import { readFileSync, writeFile } from "fs";
import handShake from "./src/get/handshake.js";
import getMessageCount from "./src/get/message_count.js";
import readMessage from "./src/get/read_message.js";
Expand All @@ -9,13 +9,13 @@ import sendMessage from "./src/post/send_message.js";
import sendMessageChunks from "./src/post/send_message_chunks.js";

// defaults to the local url for the sandbox environment
const url = process.env.MESH_URL || "https://localhost:8700";
let url = process.env.MESH_URL || "https://localhost:8700";

// This is the shared key for all mailboxes within an account
const sharedKey = process.env.MESH_sharedKey;
let sharedKey = process.env.MESH_SHARED_KEY;

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

// Setup the https agents for tls, you can ignore this for sandbox
let senderAgent = new Agent({
Expand All @@ -30,18 +30,19 @@ let receiverAgent = new Agent({
rejectUnauthorized: false,
});

const logLevel = process.env.LOG_LEVEL || "SILENT";
let logLevel = process.env.LOG_LEVEL || "SILENT";
log.setLevel(log.levels[logLevel]);

// The 'sender' is the mailbox we will be sending the message from
const senderMailboxID = process.env.MESH_SENDER_MAILBOX_ID;
const senderMailboxPassword = process.env.MESH_SENDER_MAILBOX_PASSWORD;

const messageContent = process.env.MESH_MESSAGE || "This is a test";
let senderMailboxID = process.env.MESH_SENDER_MAILBOX_ID;
let senderMailboxPassword = process.env.MESH_SENDER_MAILBOX_PASSWORD;

let messageContent = process.env.MESH_MESSAGE || "This is a test";
let messageFile =
process.env.MESH_DATA_FILE || "./tests/testdata-organizations-100000.csv";
// The 'receiver' is the mailbox we will be checking for new message
const receiverMailboxID = process.env.MESH_RECEIVER_MAILBOX_ID;
const receiverMailboxPassword = process.env.MESH_RECEIVER_MAILBOX_PASSWORD;
let receiverMailboxID = process.env.MESH_RECEIVER_MAILBOX_ID;
let receiverMailboxPassword = process.env.MESH_RECEIVER_MAILBOX_PASSWORD;

// A 30 second wait timer to allow the messages to be received and processed by mesh
async function waitThirtySeconds() {
Expand All @@ -55,103 +56,127 @@ async function waitThirtySeconds() {
async function createMessages() {
// Check connection to mailbox
log.debug("\nChecking connection to mailbox with handshake");
await handShake(
url,
senderMailboxID,
senderMailboxPassword,
sharedKey,
tlsEnabled,
senderAgent
);
await handShake({
url: url,
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});

log.debug("\nCreating new message");
// Create new messages
const newMessage = await sendMessage(
url,
senderMailboxID,
senderMailboxPassword,
messageContent,
receiverMailboxID,
tlsEnabled,
senderAgent
);
let newMessage = await sendMessage({
url: url,
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
message: messageContent,
mailboxTarget: receiverMailboxID,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});
log.debug("New message created with an ID: " + newMessage.data["message_id"]);
log.debug("Message content is: " + messageContent);
}

async function createMessageChunks() {
// Check connection to mailbox
log.debug("\nChecking connection to mailbox with handshake");
await handShake(
url,
senderMailboxID,
senderMailboxPassword,
sharedKey,
tlsEnabled,
senderAgent
);

// log.debug("\nCreating new message chunks");
// // Create new messages
// const newMessage = await sendMessageChunks({
// url: url,
// mailboxID: senderMailboxID,
// mailboxPassword: senderMailboxPassword,
// mailboxTarget: receiverMailboxID,
// tlsEnabled: tlsEnabled,
// agent: senderAgent,
// });
// log.debug("New message created with an ID: " + newMessage.data["message_id"]);
// log.debug("Message content is: " + messageContent);
await handShake({
url: url,
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});

await sendMessageChunks({
url: url,
mailboxID: senderMailboxID,
mailboxPassword: senderMailboxPassword,
mailboxTarget: receiverMailboxID,
messageFile: messageFile,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});
}

async function receiveMessage() {
// Check connection to mailbox
log.debug("\nChecking connection to mailbox with handshake");
await handShake(
url,
receiverMailboxID,
receiverMailboxPassword,
sharedKey,
tlsEnabled,
receiverAgent
);
await handShake({
url: url,
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});

// Get the number of messages in mailbox before we add any new ones.
log.debug("\nchecking number of messages in mailbox");
const inboxCount = await getMessageCount(
url,
receiverMailboxID,
receiverMailboxPassword,
sharedKey,
tlsEnabled,
receiverAgent
);
let inboxCount = await getMessageCount({
url: url,
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});

// Loop through the message and read them. so they don't interfere with tests
if (inboxCount.data["approx_inboxCount"] > 0) {
if (inboxCount.data["approx_inbox_count"] > 0) {
log.info(
"There are " +
inboxCount.data["approx_inboxCount"] +
inboxCount.data["approx_inbox_count"] +
" Messages in the mailbox"
);
log.debug("\nLooping through messages to read their content");
for (const message of inboxCount.data["messages"]) {
const messageResponse = await readMessage(
url,
receiverMailboxID,
receiverMailboxPassword,
sharedKey,
message,
tlsEnabled,
receiverAgent
);
log.debug("\nLooping through messages to read their content\n");
for (let message of inboxCount.data["messages"]) {
let messageResponse = await readMessage({
url: url,
mailboxID: receiverMailboxID,
mailboxPassword: receiverMailboxPassword,
sharedKey: sharedKey,
messageID: message,
tlsEnabled: tlsEnabled,
agent: receiverAgent,
});
try {
if (messageResponse.data === "") {
log.warn("WARNING: No data for message " + message);
} else {
console.debug("Message ID is: " + message);
console.debug("Message content is: " + messageResponse.data["data"]);
} else if (messageResponse.status === 200) {
log.debug("Message ID is: " + message);
log.debug(`Writing message to 'input/${message}.csv`);
writeFile(
`./input/${message}.csv`,
JSON.stringify(messageResponse.data, null, 2),
"utf8",
(err) => {
if (err) {
log.error(
`ERROR: an error occurred while trying to write chunk data: ${err}`
);
}
}
);
} else if (messageResponse.status === 206) {
log.debug("Message ID is: " + message);
log.debug(`Writing chunked message to 'input/${message}.csv`);
writeFile(
`./input/${message}.csv`,
JSON.stringify(messageResponse.data, null, 2),
"utf8",
(err) => {
if (err) {
log.error(
`ERROR: an error occurred while trying to write chunk data: ${err}`
);
}
}
);
}
} catch {
console.error("ERROR: Failure reading message" + message);
Expand All @@ -178,16 +203,18 @@ async function receiveMessage() {
}
}

// await createMessages();
// 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,
tlsEnabled: tlsEnabled,
agent: senderAgent,
});
await createMessages();
await createMessageChunks();
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,
// });
16 changes: 8 additions & 8 deletions src/get/generate_headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ import { v4 as uuid } from "uuid";

// Generates the token, this will be generated fresh for each call, as required by MESH
async function generateToken(
mailbox_id,
mailboxID,
password,
shared_key,
sharedKey,
nonce = uuid(),
nonce_count = 0
nonceCount = 0
) {
// Make sure to leave a space at the end of the schema.
const auth_schema_name = "NHSMESH ";
const timestamp = new Date()
.toISOString()
.replace(/[-:.TZ]/g, "")
.slice(0, 12);
const hmac_msg = `${mailbox_id}:${nonce}:${nonce_count}:${password}:${timestamp}`;
const hmac_msg = `${mailboxID}:${nonce}:${nonceCount}:${password}:${timestamp}`;

const hmac = crypto
.createHmac("sha256", shared_key)
.createHmac("sha256", sharedKey)
.update(hmac_msg)
.digest("hex");

return `${auth_schema_name}${mailbox_id}:${nonce}:${nonce_count}:${timestamp}:${hmac}`;
return `${auth_schema_name}${mailboxID}:${nonce}:${nonceCount}:${timestamp}:${hmac}`;
}

async function generateHeaders(mailbox_id, mailbox_password, shared_key) {
const token = await generateToken(mailbox_id, mailbox_password, shared_key);
async function generateHeaders(mailboxID, mailboxPassword, sharedKey) {
const token = await generateToken(mailboxID, mailboxPassword, sharedKey);
const header = {
accept: "application/vnd.mesh.v2+json",
authorization: token,
Expand Down
26 changes: 11 additions & 15 deletions src/get/handshake.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@ import axios from "axios";
import generateHeaders from "./generate_headers.js";
import log from "loglevel";

async function handShake(
async function handShake({
url,
mailbox_id,
mailbox_password,
shared_key,
ssl_enabled,
agent
) {
const full_url = `${url}/messageexchange/${mailbox_id}`;
const headers = await generateHeaders(
mailbox_id,
mailbox_password,
shared_key
);
mailboxID,
mailboxPassword,
sharedKey,
tlsEnabled,
agent,
}) {
const full_url = `${url}/messageexchange/${mailboxID}`;
const headers = await generateHeaders(mailboxID, mailboxPassword, sharedKey);

let config = { headers: headers };
if (ssl_enabled) {
if (tlsEnabled) {
config.httpsAgent = agent;
}
const response = await axios.get(full_url, config);
try {
if (response.status === 200) {
log.info("Handshake successful, status " + response.status);
log.info(`Handshake successful, status ${response.status}\n`);
return response;
} else {
console.error(
Expand Down
Loading

0 comments on commit 4096f4f

Please sign in to comment.