Skip to content

Instantly share code, notes, and snippets.

Avatar
🦆
Keep it simple and stupid.

ba bacloud22

🦆
Keep it simple and stupid.
View GitHub Profile
View docker-compose.yml
version: '3'
services:
vaultwarden:
image: vaultwarden/server:latest
container_name: vaultwarden
restart: always
environment:
- SIGNUPS_ALLOWED=false
- INVITATIONS_ALLOWED=false
@bacloud22
bacloud22 / client_chat.js
Last active Jul 4, 2022
Who said chat with websockets is easy ? These are most of the parts: client and server side. Server side is relying on Fastify and Fastify websockets.. You need to glow parts together at your convenience.
View client_chat.js
import { getChannels, newSocket } from './sockets/refresh.js'
recoverState()
if (newSocket()) getChannels()
@bacloud22
bacloud22 / _IPLookup.js
Last active Jun 21, 2022
Efficient storage of IPs v4. Memory/Search/Add. Can hold any prefixed Strings (IP, Path, Routes...) with the following assumption: very very big array with more changes of part(i) than part(i-1) -- Also see Radix Trie for more
View _IPLookup.js
// This is an implementation in JavaScript of the most simple yet efficient map suggested here https://stackoverflow.com/a/44950027/1951298
// Nothing special, I just like my JavaScript implementation.
const ips = ["100.1.108.246", "101.0.80.218", "101.108.122.200"]
const bucket = {}
function pushToBucket(bucket, ip) {
const intIp = ip.split('.').map(Number)
var part1, part2, part3, part4
;[part1, part2, part3, part4] = intIp
View offline_translator.js
// I WILL TRY TO CLEAN UP CODE !!!
const path = require('path')
const __filePath = path.join(__dirname, './wiki.multi.en.vec')
const filePath__ = path.join(__dirname, './wiki.multi.ar.vec')
const cachePath = path.join(__dirname, './cache.model')
if (typeof String.prototype.trim === 'undefined') {
String.prototype.trim = function () {
@bacloud22
bacloud22 / one-to-many-cache-mechanism.js
Last active Apr 23, 2022
This is my attempt to cache a post (like in blogs, called Listing here) and also to cache pages (like first, second, .. page that contains 9 Listings). The thing to mention here is that whenever one Listing is updated anywhere in the app, this modification must propagate through caches !!! DB is MongoDB, Cache is Redis, and my attempt is kinda m…
View one-to-many-cache-mechanism.js
// FIRST, it is to mention this doesn't handle race conditions all over the app per requests !!!
// ... that's another story ! A user could update a document and at the same time, another user requests that document (from cache)
// Check https://blog.theodo.com/2019/09/handle-race-conditions-in-nodejs-using-mutex/
// At first someone could argue it is just simple, whenever a Listing is modified,
// just delete it from Listing cache and iterate through all pages and remove pages containing the Listing.
// Well! this is TRUE ! and I didn't thought about that (or thought it would be performance degrading)
// So what I'm doing, is to keep a list of updated documents (with up = 3)
// When a user first visits a Listing, update up 3 -> 1, 2 -> nil
@bacloud22
bacloud22 / Constraints.js
Last active Apr 15, 2022
Super stupid 😝
View Constraints.js
// Constraints to ease code complexity. These constraints reflect
// which operations to run on some endpoint on some environment
const S = require('fluent-json-schema')
const { illustrations, fontFamilies } = require('./hallux.js')
const config = require('config')
const TAG_SIZE = config.get('TAG_SIZE')
const login = S.object()
.prop('username', S.string().format(S.FORMATS.EMAIL))
.prop('password', S.string().minLength(3).maxLength(40))
@bacloud22
bacloud22 / Pipeline.js
Last active Apr 15, 2022
This is a JavaScript pipeline with side effects implementation. I coded this for a web-app on Node side. It helped me to have a clear and robust pattern to ease complexity at scale. Practically, it helps "Post handlers" because they all get data from `request.body`, modify it, add to it,... while alwayse maintaining two values: `pipeline.result`…
View Pipeline.js
// author: b@cloud14[at]gmail.com
// This file is not self contained, I'm using it for my web-app on back-end.
// This gist is an illustration of a simple pattern that could help dealing each time on same data (req.body here) with different logics.
const geoEncoder = require('../../data/geo/geoJSONEncoder')
const { constraints } = require('../constraints/constraints')
const { html, reb, rew } = require('../constraints/regex')
const sanitizeHtml = require('sanitize-html')
const nlp = require('wink-nlp-utils');
const coordinates = geoEncoder.getBorders()
View magicMiddleware.js
const { constraints } = require('./constraints.js')
const { obj, ops } = require('./helpers.js')
const borders = require('../../data/geo/country').borders
const ConnectSequence = require('connect-sequence')
const { celebrate, Joi, errors, Segments } = require('celebrate')
const formidable = require('formidable')
// Chain wrapper for Strings
function stringTransformer(s) {
var internal = String(s)
@bacloud22
bacloud22 / main.js
Last active May 23, 2021
One JS file for multiple pages
View main.js
var htmlNode_1 = document.querySelector('#foo')
var htmlNode_2 = document.querySelector('#bar')
function funcc1(node) {
node.value = node.value.toUpperCase()
return node
}
function funcc2(node) {
node.value = node.value.repeat(2)
return node
}
@bacloud22
bacloud22 / projecthoneypot_nodejs_check.js
Last active Jan 17, 2021
With variables in '.env' set (NODE_ENV and HONEYPOT_KEY); This is an example of how to benefit projecthoneypot API. It just blocks visitors from score 1 and above. You can use plain NodeJS but I am using Express here.
View projecthoneypot_nodejs_check.js
var router = express.Router();
var dns = require('dns');
var visitor_type = {
0: 'Search Engine Bot',
1: 'Suspicious',
2: 'Harvester',
3: 'Suspicious, Harvester',
4: 'Comment Spammer',
5: 'Suspicious, Comment Spammer',
6: 'Harvester, Comment Spammer',