Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / ProxySecureAPIGateway.js
Created May 20, 2024 20:35
Proxy secure API Gateway
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;
}
@helabenkhalfallah
helabenkhalfallah / ProxyValidator.js
Created May 20, 2024 19:58
Proxy validation and sanitization
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");
}
}
// 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, {
const target = {
name: 'Alice',
age: 30,
}
const handler = {
get: function(target, prop, receiver) {
console.log(`Getting property ${prop}`);
return Reflect.get(target, prop, receiver);
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);
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);
}
};
const target = { name: 'Alice', age: 30 };
const handler = {
deleteProperty: function(target, prop) {
console.log(`Deleting property ${prop}.`);
delete target[prop];
return true;
}
};
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);
@helabenkhalfallah
helabenkhalfallah / BasicProxyAssignment.js
Last active May 20, 2024 16:20
Basic Proxy Assignment (set)
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;
}
};
@helabenkhalfallah
helabenkhalfallah / BasicProxy.js
Created May 20, 2024 15:57
Basic creation of a Proxy
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);