Skip to content

Instantly share code, notes, and snippets.

View intech's full-sized avatar
🌍
The idea is not to live forever, it is to create something that will

Ivan Zhuravlev intech

🌍
The idea is not to live forever, it is to create something that will
View GitHub Profile
@intech
intech / uuid_serializer.js
Created February 3, 2024 16:04
UUIDv5 serialization and deserialization
/* Code compression - compresses given [A-Za-z0-9]+ string to hex, and/or to
* UUIDv5, and decopresses it back.
* Used by Rehau piece code compression, to generate unique product UUID based
* on code value.
* Can't use ordinary hex, as "16-bytes string".toString("hex") would result in
* 32 symbol hex string, which is too long to be encoded into UUID.
*/
const char0 = '0'.charCodeAt(0);
const char9 = '9'.charCodeAt(0);
import FSM from "./fsm";
const instance = new FSM([
// Start state, default state
{ state: "Green", from: "Red", to: ["Yellow", "Error"] },
// from is allowed array list states
{ state: "Yellow", from: "Green", to: ["Red", "Error"] },
// to is single, auto change state to Green
{ state: "Red", from: "Yellow", to: ["Green", "Error"] },
// End state
@intech
intech / TracingMethods.js
Last active September 6, 2022 15:04
Moleculer middleware tracing methods
// file: middlewares/tracingMethods.js
const { AsyncLocalStorage } = require("async_hooks");
const { Utils } = require("moleculer");
const storage = new AsyncLocalStorage();
module.exports = {
name: "tracingMethods",
localAction(next) {
return ctx => {
if (this.isTracingEnabled()) {
const { span } = ctx;
const _ = require("lodash");
const { ServiceSchemaError } = require("moleculer").Errors;
const { PrismaClient } = require("@prisma/client");
class PrismaDbAdapter {
/**
* Creates an instance of PrismaDbAdapter.
* @param {any} opts
*/
@intech
intech / lock.mixin.js
Last active July 1, 2022 23:23
Moleculer implementation of the redlock algorithm for distributed Redis locks
module.exports = {
name: "lock",
settings: {
lock: {
ttl: 10e3
}
},
methods: {
/**
* Try lock key
const StateMachine = require("fsm-async");
const chalk = require("chalk");
class Semaphore extends StateMachine {
constructor() {
const transitionTable = {
initial: "init",
transitions: [
{ ev: "init", from: "init", to: "red" },
{ ev: "_red", from: "red", to: "fromRed" },
@intech
intech / perfhook.js
Last active February 23, 2022 18:17
Moleculer middleware for detect event loop delay
const { monitorEventLoopDelay } = require("perf_hooks");
const { humanize } = require("moleculer/src/utils");
module.exports = {
name: "perfhook",
created() {
this.perfhook = monitorEventLoopDelay({ resolution: 20 });
this.perfhook.enable();
},
stopped() {
@intech
intech / test.service.js
Last active December 1, 2021 16:53
Moleculer Socket.IO mixin with auto-alias
module.exports = {
name: "test",
version: 1,
actions: {
test: {
ws: {
name: "test"
},
async handler(ctx) {
const { user } = ctx.meta;
@intech
intech / Base.js
Last active July 27, 2022 12:51
Moleculer ORM Objection.js (https://github.com/vincit/objection.js) middleware by pattern Shared database (https://microservices.io/patterns/data/shared-database.html)
// file: models/Base.js
const { Model } = require("objection");
module.exports = class BaseModel extends Model {
static get modelPaths() {
return [__dirname];
}
static get useLimitInFirst() {
return true;
@intech
intech / queue.js
Created May 2, 2021 15:33
Moleculer Middleware redis-smq
const { callbackify } = require("util");
const { GracefulStopTimeoutError } = require("moleculer").Errors;
const { Message, Producer, Consumer } = require("redis-smq");
const events = require("redis-smq/src/events");
module.exports = function QueueMiddleware() {
const producers = new Map();
const consumers = new Map();
function gracefulShutdown(broker, items) {