Skip to content

Instantly share code, notes, and snippets.

@bsitruk
bsitruk / purge-queues.sh
Last active July 23, 2024 07:25
Purge RabbitMQ Queues with the Management Plugin
#!/bin/bash
# Function to purge a queue
purge_queue() {
queue_name="$1"
echo "Purging queue: $queue_name"
docker exec CONTAINER_NAME rabbitmqadmin -u USER -p PWD -H 0.0.0.0 -P MANAGEMENT_PORT purge queue name="$queue_name" > /dev/null
}
# Fetch all queues
https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container
docker run -it --rm alpine nslookup host.docker.internal
@bsitruk
bsitruk / keys-to-union.mjs
Last active August 25, 2022 08:09
ZX Script to build TypeScript Pick type.
#!/usr/bin/env zx
# usage
#./keys-to-enum.mjs "{ 220ms  Thu Aug 25 11:04:52 2022
# attributeName,
# fieldName,
# fullyQualifiedName,
# attributeType,
# scannerTypeGroup,
# }: GetRecordFindingsDto"
Constrained Identity Function
- Use Case: We want to create a Record with a limited set of Keys, and a defined type of Values.
- We also don't want to Type the Set of Keys, but leverage TypeScript inference instead (DRY)
- If we create a new inline object literal, TypeScript will prevent any new key from being added, by won't enforce the type of the Values.
- So Narrow Keys, Wide Value Type
- If we specify the type Record<string, number>, we'll be able to add any new Key after the object creation
- So Wide Keys, Narrow Value Type
- By using a Types Identity Function, we can build an object with enforced Value Type, and Narrow set of Keys.
// Sort List of Objects By Two Keys
type SortKey<T> = {
key: keyof T;
order: 1 | -1;
};
export function sortByKeys<T>(list: T[], keys: SortKey<T> | [SortKey<T>, SortKey<T>]) {
if (!Array.isArray(keys)) {
const { key: sortKey, order } = keys;
return list.sort((v1, v2) => (v1[sortKey] < v2[sortKey] ? order * 1 : -1 * order));
}
const keys = ['k1', 'k2', 'k3']
const Keys = typeof keys[number]
type Object = {
k1: number,
k2: boolean
}
type KeyValue = {
[k in Keys]: k extends keyof Object ? Object[k] : unknown
import * as cluster from 'cluster';
import * as os from 'os';
import { Logger } from '@nestjs/common';
export class Cluster {
static register(workers: number, logger: Logger, callback: CallableFunction): void {
const cpuCount = os.cpus().length;
if (cluster.isMaster) {
logger.log(`master process started on ${process.pid}`);
Cypress.Commands.add('interceptOnce', (method, path, alias, status) => {
cy.intercept({
method,
path,
times: 1,
}).as(alias);
if (status) {
return cy.wrap(() =>
cy
function getCurrentDateP(mongoConnection) {
const query = { _id: 1 };
const update = { $currentDate: { updated_at: true } };
const queryOptions = { upsert: true, returnOriginal: false };
return mongoConnection
.collection('current_date')
.findOneAndUpdate(query, update, queryOptions)
.then(mongoResult => mongoResult.value.updated_at)
}
.catch((err: Error | AxiosError) {
if (axios.isAxiosError(error)) {
// Access to config, request, and response
} else {
// Just a stock error
}
})