Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar
💭
A

Rico Chen midnightcodr

💭
A
View GitHub Profile
@midnightcodr
midnightcodr / app.js
Created March 12, 2024 16:13
hapijs + pino + kafka
const pino = require('pino')
const pkafka = require('pino-kafka')
// this can be from .env, via JSON.parse
const kafkaLogConfig = {
"clientId":"some-producer1",
"defaultTopic": "some-log-topic",
"brokers":["some-host:9092"]
}
const streams = [{ stream: process.stdout, level: 'info' }]
@midnightcodr
midnightcodr / translate.php
Last active October 21, 2023 04:50
aws translate with php (curl) without sdk
<?php
function aws_translate($text, $sourceLang = 'zh-TW', $targetLang = 'en')
{
$aws_access_key_id = 'key_id';
$aws_secret_access_key = 'key';
// AWS region and Host Name (Host names are different for each AWS region)
// As an example these are set to us-east-1 (US Standard)
$aws_region = 'us-east-1';
$host_name = 'translate.us-east-1.amazonaws.com';
@midnightcodr
midnightcodr / rust_lambda_helpers.zsh
Created March 14, 2022 03:25
alternative al2build function using cross
RUST_TARGET="aarch64-unknown-linux-gnu"
RUST_VERSION="latest"
PROJECT_NAME=${PWD##*/}
zipRustLambda() {
cp ./target/${RUST_TARGET}/release/${PROJECT_NAME} ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
}
al2build() {
cross build --release --target ${RUST_TARGET}
@midnightcodr
midnightcodr / cat_copycat_with_rust.rs
Created April 9, 2021 01:19
a simple cat implementation with rust, plus some customized error handling
// main.rs
use std::env;
use std::error::Error;
use std::fs;
use std::fmt;
#[derive(Debug)]
struct MyError(String);
const Hapi = require('@hapi/hapi')
;(async () => {
const server = Hapi.server({
port: 3000
})
server.events.on('log', logObject => {
console.log(JSON.stringify(logObject))
})
@midnightcodr
midnightcodr / pass-the-fun-node-v14-and-under.js
Created December 30, 2020 01:58
Use stream.PassThrough to count file size
// node pass-the-fun-node-v14-and-under.js inputfile
// this will create a clone of inputfile with inputfile.bak
// and count the bytes written using stream.PassThrough
// note the simplified contructor form (as illustrated in
// pass-the-fun-node-v15.js does not work as expected in
// node v14
const fs = require('fs')
const { pipeline, PassThrough } = require('stream')
@midnightcodr
midnightcodr / .env.example
Created December 26, 2020 18:17
ssh2-sftp-client reconnect on error demostration
SFTP_USER=
SFTP_HOST=127.0.0.1
SFTP_PORT=22
SFTP_PASSWORD=
@midnightcodr
midnightcodr / Hapijs-uploads-basic-and-advanced-examples.md
Last active May 23, 2020 17:51
hapijs uploads: basic and advanced (with virus scanning) examples

Setup

npm i

Use this docker image to setup the scanner

 docker run --name clamav -d -p 3310:3310 mkodockx/docker-clamav
@midnightcodr
midnightcodr / hapi-js-file-upload-with-content-type-detection-demo.js
Created September 14, 2019 18:50
hapi-js-file-upload-with-content-type-detection-demo.js
const Hapi = require('@hapi/hapi')
const Joi = require('@hapi/joi')
const fileType = require('file-type')
const stream = require('stream')
const Readable = stream.Readable
const port = 5000
const server = Hapi.server({
port
})
@midnightcodr
midnightcodr / write-two-millio-rows-of-csv-data-with-node-cluster.js
Last active September 3, 2023 03:55
How to write two million rows of csv data using faker.js and node cluster
const faker = require('faker')
// const N = 30
const N = 2000000
const fs = require('fs')
const record = () => {
// faker.fake(
// '{{name.lastName}},{{name.lastName}},{{address.city}},{{address.county}},{{address.zipCode}},{{hacker.adjective}}\n'
return [
faker.name.firstName(),
faker.name.lastName(),