Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix_externalcontent
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Ellison committed Aug 7, 2023
2 parents 34c7ddc + ed3cace commit 7c27e5a
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 169 deletions.
5 changes: 2 additions & 3 deletions lib/etherpad/fetchPad.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export async function fetchPadDetails(cacheKey, rev = 0) {
const contentResponse = await fetch(`/api/etherpad/pad?pad=${padData.padID}&rev=${newRev}`);
const contentData = await contentResponse.json();

if (contentData.content && contentData.content.text) {
// console.log('fetchPadDetails:text: ', contentData.content.text);
if (contentData.content) {

let { content, data: frontmatter } = matter(contentData.content.text);
let { content, data: frontmatter } = matter(contentData.content);
frontmatter.padID = padData.padID;
frontmatterObj = frontmatter;
rawContent = content;
Expand Down
308 changes: 145 additions & 163 deletions lib/redis/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Redis = require('ioredis');
const Redis = require("ioredis");

let redisInstance;

Expand Down Expand Up @@ -36,189 +36,171 @@ let redisInstance;
// // console.log(await redis.ttl("foo")); // -1

function getRedisConfiguration() {
return {
host: process.env.REDIS_HOST || '172.17.0.1',
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT,
}
return {
host: process.env.REDIS_HOST || "172.17.0.1",
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT,
};
}

export async function createRedisInstance(config = getRedisConfiguration()) {
try {
const options = {
enableReadyCheck: true,
scaleReads: "all",
redisOptions: {
host: config.host,
lazyConnect: true,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
retryStrategy: times => {
if (times > 3) {
throw new Error(`[Redis] Could not connect after ${times} attempts`);
}

return Math.min(times * 200, 1000);
}
},
}

if (config.port) {
options.redisOptions.port = config.port;
}

if (config.password) {
options.redisOptions.password = config.password;
}

let redis;

if (process.env.REDIS_CLUSTER_MODE) {
redis = new Redis.Cluster([
{
host: config.host,
port: config.port
}],
options
try {
const options = {
enableReadyCheck: true,
scaleReads: "all",
redisOptions: {
host: config.host,
lazyConnect: true,
showFriendlyErrorStack: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 0,
retryStrategy: (times) => {
if (times > 3) {
throw new Error(
`[Redis] Could not connect after ${times} attempts`,
);
} else {
// For local development or non-clustered environment
redis = new Redis(options.redisOptions);
}

// Wrap the Redis instance creation in a promise to handle any connection errors.
return new Promise((resolve, reject) => {
redis.on('error', error => {
// console.warn('[Redis] Error connecting', error);
// If an error occurs during the Redis connection, we'll reject the promise.
reject(error);
});

// Now we'll try to connect the Redis instance.
redis.connect(err => {
if (err) {
// console.warn('[Redis] Error connecting', err);
// If an error occurs during the Redis connection, we'll reject the promise.
reject(err);
} else {
// If the connection is successful, resolve the promise with the Redis instance.
resolve(redis);
}
});
});
} catch (e) {
// throw new Error(`[Redis] Could not create a Redis instance`);
}

return Math.min(times * 200, 1000);
},
},
};

if (config.port) {
options.redisOptions.port = config.port;
}
}

if (config.password) {
options.redisOptions.password = config.password;
}

let redis;

if (process.env.REDIS_CLUSTER_MODE) {
redis = new Redis.Cluster(
[
{
host: config.host,
port: config.port,
},
],
options,
);
} else {
// For local development or non-clustered environment
redis = new Redis(options.redisOptions);
}

return redis;
} catch (e) {
// throw new Error(`[Redis] Could not create a Redis instance`);
}
}

// Function to read data from the cache
export async function cacheRead(key) {
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.get(key);
if (!value) return null;
const parsedValue = JSON.parse(value);
return parsedValue.buffer ? Buffer.from(parsedValue.buffer) : parsedValue;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return null;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.get(key);
if (!value) return null;
const parsedValue = JSON.parse(value);
return parsedValue.buffer ? Buffer.from(parsedValue.buffer) : parsedValue;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return null;
}
}


// Function to write data to the cache
export async function cacheWrite(key, value, ttl = null) {
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const isBuffer = Buffer.isBuffer(value);
const stringifiedValue = isBuffer ? JSON.stringify({ buffer: [...value] }) : JSON.stringify(value);

if (ttl) {
try {
await redisInstance.set(key, stringifiedValue, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.set(key, stringifiedValue);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return null;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const isBuffer = Buffer.isBuffer(value);
const stringifiedValue = isBuffer
? JSON.stringify({ buffer: [...value] })
: JSON.stringify(value);

if (ttl) {
try {
await redisInstance.set(key, stringifiedValue, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.set(key, stringifiedValue);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return null;
}
}

// Function to read data from the cache
export async function cacheSearch(key) {
try {

if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.keys(key, (err, result) => {
if (err) {
console.error('Error fetching keys:', err);
} else {
return result;
}
});

if (!value) return null;
return value;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return [];
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
const value = await redisInstance.keys(key, (err, result) => {
if (err) {
console.error("Error fetching keys:", err);
} else {
return result;
}
});

if (!value) return null;
return value;
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return [];
}
}

export async function hset(key, obj, ttl = null) {

try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}
if (ttl) {
try {
await redisInstance.hset(key, obj, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.hset(key, obj);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error('Error during Redis setup:', error);
return false;
try {
if (!redisInstance) {
redisInstance = await createRedisInstance();
}

}
if (ttl) {
try {
await redisInstance.hset(key, obj, "EX", ttl);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
} else {
try {
await redisInstance.hset(key, obj);
return true; // or return 'Data set successfully';
} catch (error) {
console.error(`Error setting data: ${error}`);
throw error;
}
}
} catch (error) {
// Handle the error here if Redis is unavailable or there was a connection error.
// For example, you may use a fallback cache mechanism or default values.
console.error("Error during Redis setup:", error);
return false;
}
}
6 changes: 5 additions & 1 deletion pages/api/content/sharepoint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import mime from 'mime-types';


// configure your node options (only once in your application)
const buffer = readFileSync(process.env.SHAREPOINT_PRIVATE_KEY_FILE);
let buffer = process.env.SHAREPOINT_PRIVATE_KEY;
if (!buffer) {
const privateSharepointKeyPath = process.env.SHAREPOINT_PRIVATE_KEY_FILE;
buffer = readFileSync(privateSharepointKeyPath);
}

const config = {
auth: {
Expand Down
Loading

0 comments on commit 7c27e5a

Please sign in to comment.