Skip to content

Instantly share code, notes, and snippets.

@franzwong
franzwong / main.js
Created January 23, 2019 14:41
HowTo: Grant IAM role to Cognito authenticated user
let response = await login(email, password)
const idToken = response.AuthenticationResult.IdToken
const { identityId, credentials } = await getUserCredentials(idToken)
const s3 = new AWS.S3({ credentials })
response = await s3.putObject({
Body: 'Hello world',
Bucket: process.env.S3_BUCKET,
Key: `${identityId}/foo.txt`,
ContentType: 'text/plain'
@franzwong
franzwong / stack.tf
Created February 4, 2019 10:12
Sample Terraform stack for EC2 and VPC
variable "aws_region" {
default = "us-east-1"
}
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
variable "webSubnetCidr" {
default = "10.0.1.0/24"
@franzwong
franzwong / app.component.ts
Created March 6, 2019 14:15
Integrate Google OAuth with Angular 7
import { Component, OnInit, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
import { environment } from './../environments/environment';
const clientId = environment.clientId;
const scope = 'https://www.googleapis.com/auth/youtube.readonly';
@franzwong
franzwong / index.js
Created June 16, 2019 04:49
S3 POST policy example
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
const uuidv4 = require('uuid/v4');
const bucketName = process.env.bucketName;
const kmsKeyId = process.env.kmsKeyId;
const MAX_FILE_SIZE = 5 * 1024 * 1024;
const EXPIRATION = 900;
@franzwong
franzwong / tsconfig.json
Created July 1, 2019 01:51
Typescript config example
{
"compilerOptions": {
"outDir": "./built",
"allowJs": true,
"target": "ES2018",
"moduleResolution": "node",
"module": "commonjs"
},
"include": [
"./src/**/*"
@franzwong
franzwong / index.ts
Created July 1, 2019 02:40
Screenshot capture script
import * as puppeteer from 'puppeteer';
interface PageInfoMap {
[key: string]: string;
}
const pageInfoMap: PageInfoMap = {
'はちま起稿': 'http://blog.esuteru.com/',
}
@franzwong
franzwong / index.js
Last active November 10, 2022 08:28
Synology DownloadStation Task
const axios = require('axios');
const url = require('url');
if (process.argv && process.argv.length < 3) {
console.error('Invalid number of parameters');
process.exit(1);
}
const downloadLink = process.argv[2];
const domain = process.env.domain;
@franzwong
franzwong / index.js
Created July 20, 2019 07:48
Mac OS Keychain wrapper
const util = require('util');
const child_process = require('child_process');
const exec = util.promisify(child_process.exec);
function addPassword(serviceName, userName, password) {
return exec(`security add-generic-password -s ${serviceName} -a ${userName} -w ${password}`);
}
async function getPassword(serviceName, userName) {
@franzwong
franzwong / gulpfile.js
Created July 27, 2019 05:43
Prepare lambda installation package with Gulp
const { src, dest, series, parallel } = require('gulp');
const install = require('gulp-install');
const del = require('del');
const zip = require('gulp-zip');
const BUILD_DIR = 'build';
const packageInfo = require('./package.json');
function clean() {
@franzwong
franzwong / README.md
Last active August 19, 2019 02:02
YAML cheatsheet

Array of objects

Multiple properties

Yaml

- id: 123456
  name: Luke
 title: Jedi Knight