Skip to content

Instantly share code, notes, and snippets.

@osher
osher / analyse-node_modules.sh
Last active August 29, 2024 11:16
A script that analyses node_modules for stats of CJS and ESM usage
{
CJS=0; ESM=0; DUAL=0; UN=0; UNP=''; TYPES=0;
# for each `package.json` in the `node_modules` first and second levels:
for P in $({
ls node_modules/*/package.json;
ls node_modules/*/*/package.json || true;
ls node_modules/**/node_modules/*/package.json || true;
ls node_modules/**/node_modules/*/*/package.json || true;
} | sort); do
echo "checking: $P";
# this gist is an appendix for the discussion in this article:
# https://medium.com/@osherel/yet-another-look-on-js-require-and-import-e2a6316a161d
#Some setup:
mkdir -p sandbox/packages/a sandbox/packages/c;
cd sandbox;
echo '{"name":"sandbox","version":"1.0.0","workspaces":["packages/*"]}' > package.json;
npm init -y -w -q packages/a;
npm init -y -w -q packages/c;
echo 'export default "a"' > packages/a/index.js;
import * as requireYaml from "require-yml";
import * as set from "lodash.set";
import * as merge from "deepmerge";
import { load as parseYaml } from "js-yaml";
import { readFileSync } from "fs";
type StringMap = { [key: string]: string };
let config: any, configRootPath: string;
_resetConfig();
@osher
osher / the.getting-started.improfessional.Dockerfile
Created May 8, 2023 14:58
the getting-started Dockerfile you get for nodejs docker images
FROM node:lts
WORKDIR /app
COPY . .
RUN npm i
EXPOSE 3000/tcp
CMD ["npm", "start"]
@osher
osher / node-service.multiphase.Dockerfile
Last active August 8, 2023 17:30
Example for the Dockerfile for node-services using an multi-phase build with optimized node-builder and node-runner.
ARG TAG_NODE_BUILDER=latest
FROM my-private-cr/node-builder:$TAG_NODE_BUILDER
COPY packge.json .
RUN npm i --production
ARG TAG_NODE_RUNNER=latest
FROM my-private-cr/node-runner:$TAG_NODE_RUNNER
COPY --from=0 /app /
COPY . /app/
RUN apk update --no-cache
@osher
osher / node-runner.Dockerfile
Created May 8, 2023 11:05
An example for a node-runner - a base image for the last step on a multi-phase build for node services
ARG TAG_ALPINE_BASE=latest
FROM alpine:$TAG_ALPINE_BASE
RUN apk update --no-cache \
&& apk upgrade --no-cache \
&& apk add --no-cache nodejs
WORKDIR /app
RUN addgroup -S service \
&& adduser -S service -G service
@osher
osher / example.for.node-builder.Dockerfile
Last active May 8, 2023 12:45
A dockerfile to create a base image for building npm packages
ARG TAG_NODE_BASE=lts
FROM node:$TAG_NODE_BASE
WORKDIR /app
RUN apk update --no-cache \
&& apk upgrade --no-cache \
&& apk add --no-cache \
build-base \
openssh \
FROM node:alpine-lts
WORKDIR /app
COPY packge.json .
RUN npm i --production
FROM alpine
RUN apk update \
&& apk upgrade --no-cache \
&& apk add --no-cache nodejs \
&& rm -r /**/apk /**/**/apk
@osher
osher / sops-with-age-encryption.demo.sh
Last active November 14, 2024 08:40
The simplest sops demo - sops using age encryption
# This demo uses an alpine sandbox in a docker container in
# interactive mode. ran with:
# docker run --rm -it alpine
#
# if you run it on your own system:
# 1. you should use your own package manager instead of `apk`
# 2. expect the following left overs:
# - installed binaries (age, age-keygen, sops)
# - $HOME/.config/sops/age/keys.txt
# - demo files: source.env, encrypted.env, decrypted.env
@osher
osher / separated-concerns-frameworkless-http-server.js
Last active September 14, 2022 18:03
A "frameworkless" http-server - with separation of concerns
//code concern
//-------------------------------------------------------------------
const conf = () => { //config
views: { //config
status: 'i\'m alive', //config
greet: 'hello, %s', //config
help: 'supports: /greet or /status', //config
}, //config
web: { port: 3000 }, //config
}; //config