This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'http'; | |
import url from 'url'; | |
// Validation function to perform security checks | |
const validateRequest = (req) => { | |
const apiKey = req.headers['x-api-key']; | |
if (!apiKey || apiKey !== 'your-secure-api-key') { | |
console.log('Invalid API Key'); | |
return false; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const validator = { | |
set(obj, prop, value, receiver) { | |
if (prop === "age") { | |
if (!Number.isInteger(value)) { | |
throw new TypeError("The age is not an integer"); | |
} | |
if (value > 200) { | |
throw new RangeError("The age seems invalid"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to create a reactive store | |
function createStore(initialState) { | |
// Store the initial state in a private variable | |
let state = initialState; | |
// Array to hold subscribers (callbacks) | |
const subscribers = []; | |
// Create a proxy for the state object | |
const stateProxy = new Proxy(state, { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { | |
name: 'Alice', | |
age: 30, | |
} | |
const handler = { | |
get: function(target, prop, receiver) { | |
console.log(`Getting property ${prop}`); | |
return Reflect.get(target, prop, receiver); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { name: 'Alice', age: 30 }; | |
const handler = { | |
ownKeys: function(target) { | |
console.log('Getting property keys.'); | |
return Reflect.ownKeys(target); | |
} | |
}; | |
const proxy = new Proxy(target, handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = function(a, b) { | |
return a + b; | |
}; | |
const handler = { | |
apply: function(target, thisArg, argumentsList) { | |
console.log(`Called with arguments: ${argumentsList}`); | |
return target.apply(thisArg, argumentsList); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { name: 'Alice', age: 30 }; | |
const handler = { | |
deleteProperty: function(target, prop) { | |
console.log(`Deleting property ${prop}.`); | |
delete target[prop]; | |
return true; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { name: 'Alice', age: 30 }; | |
const handler = { | |
has: function(target, prop) { | |
console.log(`Checking if property ${prop} is in target.`); | |
return prop in target; | |
} | |
}; | |
const proxy = new Proxy(target, handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { message: "Hello, World!" }; | |
const handler = { | |
set: function(target, prop, value, receiver) { | |
console.log(`Setting property ${prop} to ${value}.`); | |
target[prop] = value; | |
return true; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const target = { message: "Hello, World!" }; | |
const handler = { | |
get: function(target, prop, receiver) { | |
console.log(`Property ${prop} was accessed.`); | |
return Reflect.get(target, prop, receiver); | |
} | |
}; | |
const proxy = new Proxy(target, handler); |
NewerOlder