Skip to content

Commit

Permalink
Enhance network activity logging in configCache function
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] committed May 18, 2024
1 parent b61f4ac commit 622039e
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,40 @@ async function configCache(page) {

page.on('request', async (request) => {
const url = request.url();
const method = request.method();
const headers = request.headers();
console.log(`Request made with method: ${method}, URL: ${url}, Headers:`, headers); // Log the request method, URL, and headers
const resolveLock = await acquireCacheLock(url);
try {
if (cache[url] && cache[url].expires > Date.now()) {
await request.respond(cache[url]);
} else {
request.continue();
}
} catch (error) {
console.error(`Error handling request for URL: ${url}`, error); // Log any errors that occur during request handling
} finally {
resolveLock();
}
});

page.on('response', async (response) => {
const url = response.url();
const status = response.status();
console.log(`Response received from URL: ${url}, Status: ${status}`); // Log the response URL and status
const resolveLock = await acquireCacheLock(url);
try {
const headers = response.headers();
console.log(`Response headers for URL: ${url}`, headers); // Log the response headers
const cacheControl = headers['cache-control'] || '';
const maxAgeMatch = cacheControl.match(/max-age=(\d+)/);
const maxAge = maxAgeMatch && maxAgeMatch.length > 1 ? parseInt(maxAgeMatch[1], 10) : 0;
if (maxAge) {
let buffer;
try {
buffer = await response.buffer();
} catch {
// some responses do not contain buffer and do not need to be cached
} catch (error) {
console.error(`Error getting buffer for response from URL: ${url}`, error); // Log any errors that occur during response buffering
return;
}
// Check if the cache entry is still valid before assigning
Expand All @@ -169,24 +177,36 @@ async function configCache(page) {

// Validation function to check if a value is a valid GeoJSON object or string
function isValidGeojson(value) {
console.log('isValidGeojson called with value:', value); // Log the input value
console.log('Type of value:', typeof value); // Log the type of the value
if (typeof value === 'string') {
try {
const parsed = JSON.parse(value);
return isValidGeojsonObject(parsed);
} catch {
console.log('Parsed GeoJSON object:', parsed); // Log the parsed object
const isValid = isValidGeojsonObject(parsed);
console.log('Is parsed object valid GeoJSON:', isValid); // Log the result of the validation
return isValid;
} catch (e) {
console.log('Failed to parse value as JSON:', value); // Log the failed parsing
console.log('Parsing error:', e.message); // Log the parsing error message
return false; // Not a valid JSON string
}
} else if (typeof value === 'object' && value !== null) {
return isValidGeojsonObject(value);
const isValid = isValidGeojsonObject(value);
console.log('Is object valid GeoJSON:', isValid, 'Object:', value); // Log the result of the validation
return isValid;
}
console.log('Value is not a valid type for GeoJSON:', value); // Log the invalid type
return false; // Not a valid type for GeoJSON
}

// Helper function to check if an object is a valid GeoJSON structure
function isValidGeojsonObject(obj) {
// Basic check for type and features properties
return Object.prototype.hasOwnProperty.call(obj, 'type') && Object.prototype.hasOwnProperty.call(obj, 'features') &&
obj.type === 'FeatureCollection' && Array.isArray(obj.features);
const isValid = Object.prototype.hasOwnProperty.call(obj, 'type') &&
Object.prototype.hasOwnProperty.call(obj, 'features') &&
obj.type === 'FeatureCollection' && Array.isArray(obj.features);
console.log('GeoJSON object is valid:', isValid, 'Object:', obj); // Log the validation result
return isValid;
}

// Validation helper functions
Expand Down Expand Up @@ -243,13 +263,14 @@ module.exports = function(options) {
if (typeof config.default === 'function') {
options[key] = config.default(options);
}
if (!config.validate(options[key])) {
throw new Error(`Invalid ${key} parameter: must be a valid GeoJSON object or a string that can be parsed into a valid GeoJSON object`);
// Validate the geojson parameter separately to provide a more detailed error message
if (key === 'geojson' && !config.validate(options[key])) {
throw new Error(`Invalid ${key} parameter: the provided value is not a valid GeoJSON object or string.`);
} else if (key !== 'geojson' && !config.validate(options[key])) {
throw new Error(`Invalid ${key} parameter: the provided value does not meet the expected type or format.`);
}
});

// Removed redundant validation call

return new Promise(function(resolve, reject) {
(async () => {

Expand Down

0 comments on commit 622039e

Please sign in to comment.