Skip to content

Instantly share code, notes, and snippets.

View slavafomin's full-sized avatar
✌️
Let's make this World a better place!

Slava Fomin II slavafomin

✌️
Let's make this World a better place!
View GitHub Profile
@slavafomin
slavafomin / 00-typescript-esm.md
Last active May 5, 2024 16:36
Using TypeScript with native ESM

Using TypeScript Node.js with native ESM

This reference guide shows how to configure a TypeScript Node.js project to work and compile to to native ESM.

Rationale

CommonJS module system was introduced by the Node.js developers due to the lack of the notion of "modules" in the original JavaScript (ECMAScript) language specification at that time. However, nowadays, ECMAScript has a standard module system called ESM — ECMAScript Modules, which is a part of the accepted standard. This way CommonJS could be considered vendor-specific and obsolete/legacy. Hopefully, TypeScript ecosystem now supports the "new" standard.

So the key benefits are:

@slavafomin
slavafomin / git-submodules.md
Last active April 15, 2024 14:11
Git submodules best practices

Git submodules best practices

Useful commands

— Clone repository with submodules automatically:

git clone --recursive git@github.com:name/repo.git

— Initialize submodules after regular cloning:

@slavafomin
slavafomin / nodejs-custom-es6-errors.md
Last active March 9, 2024 12:03
Custom ES6 errors in Node.js

Here's how you could create custom error classes in Node.js using latest ES6 / ES2015 syntax.

I've tried to make it as lean and unobtrusive as possible.

Defining our own base class for errors

errors/AppError.js

@slavafomin
slavafomin / 0.md
Last active January 9, 2024 18:35
JavaScript bytes copy benchmark (Uint8Array)

The most optimal way to copy bytes in JavaScript (Uint8Array)

Please read my post in Telegram: Don't use spread operator with byte arrays

Short summary

  1. Use target.set(source, offset) MDN
  2. Pre-alocate the entire array once
  3. Don't use ...spread operator with bytes
@slavafomin
slavafomin / jest-swc-typescript-esm.md
Last active January 3, 2024 23:58
Using Jest with SWC, TypeScript, and ESM

Using Jest with SWC, TypeScript, and ESM

Install dependencies

pnpm add -D jest @jest/globals @types/jest @swc/jest

Create jest.config.js file

{
"5.9.10.47:19949": {
"errors": {
"LITE_SERVER_UNKNOWN: timeout(during last block synchronization)": 2
},
"results": {}
},
"5.9.10.15:48014": {
"errors": {
"LITE_SERVER_NOTREADY: block is not applied": 38,
/**
* This module implements ECMAScript (stage-3) proposal:
* Promise with resolvers:
* https://github.com/tc39/proposal-promise-with-resolvers
*
* Copyright 2023 Slava Fomin II
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the “Software”), to deal in the
@slavafomin
slavafomin / nginx.conf
Created January 22, 2019 17:35
How to enable CORS in nginx with origin matching
server {
listen 80 default_server;
root /var/www;
location / {
set $cors '';
set $cors_allowed_methods 'OPTIONS, HEAD, GET';
if ($http_origin ~ '^https?://(www\.)?example.com$') {
set $cors 'origin_matched';
const isValid$ = (
merge(
value$.pipe(
distinctUntilChanged(),
map(() => undefined),
),
value$.pipe(
debounceTime(500),
withLatestFrom(isComplete$),
filter(([_, isComplete]) => isComplete),
@slavafomin
slavafomin / is-twa.ts
Last active May 1, 2023 12:05
TWA Main Button Emulation
import { WebApp } from '@grammyjs/web-app';
export function isTwa(): boolean {
return (
Boolean(WebApp.initData) ||
(WebApp.platform !== 'unknown')
);