Skip to content

Instantly share code, notes, and snippets.

View blackbing's full-sized avatar
💭
I want play a game

Bingo Yang blackbing

💭
I want play a game
View GitHub Profile
@blackbing
blackbing / fetchStream.js
Last active September 1, 2024 08:05
Server Sent Event with fetch stream
const url = 'https://api.example.com/v1/sse';
const accessToken = 'test';
fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
.then(response => {
if (response.ok && response.body) {
reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
'use strict';
const http = require('http');
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;
if (response.status == 404) {
const domainHash = request.headers['x-domain-hash'] && request.headers['x-domain-hash'][0].value;
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const host = headers.host[0].value;
const hash = /([0-9a-z-]+)\.m\.verybuy\.io/.exec(host)
if (hash && hash[1]) {
headers['X-Domain-Hash'] = [{
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const domainHash = headers['x-domain-hash'] && headers['x-domain-hash'][0].value;
if (domainHash) {
request.uri = '/' + domainHash + request.uri;
}
return callback(null, request);
@blackbing
blackbing / viewer-request.js
Created July 30, 2020 16:22
prerender lambda@edge
'use strict';
const customUserAgentHeaderName = 'x-is-bot';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const userAgent = headers['user-agent'][0].value;
headers[customUserAgentHeaderName] = [{
@blackbing
blackbing / origin-response.js
Last active July 30, 2020 16:28
prerender lambda@edge
'use strict';
const https = require('https');
const BASE_URL_RENDERER = 'YOUR_PRERENDER_SERVER?url=';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;
const headers = response.headers;
@blackbing
blackbing / origin-request.js
Last active July 30, 2020 16:23
prerender lambda@edge
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
let SHOULD_PRERENDER = false;
let IsBot = request.headers['x-is-bot'][0].value === 'true';
SHOULD_PRERENDER = IsBot;
@blackbing
blackbing / S3-Static-Sites.md
Created June 3, 2020 02:28 — forked from bradwestfall/S3-Static-Sites.md
Use S3 and CloudFront to host Static Single Page Apps (SPAs) with HTTPs and www-redirects. Also covers deployments.

S3 Static Sites

What this will cover

  • Host a static website at S3
  • Redirect www.website.com to website.com
  • Website can be an SPA (requiring all requests to return index.html)
  • Free AWS SSL certs
  • Deployment with CDN invalidation

Resources

@blackbing
blackbing / sticker.js
Created May 25, 2018 10:13
get link sticker from line store
// > node index.js 'https://store.line.me/stickershop/product/11383/zh-Hant'
const puppeteer = require('puppeteer') ;
const fs = require('fs');
const http = require('https');
const process = require('process');
var url = process.argv[2];
(async () => {
const timeout = 30000;
@blackbing
blackbing / Checkbox.jsx
Created February 15, 2017 01:42
react checkbox support indeterminate
import React, { PropTypes, Component } from 'react';
class Checkbox extends Component {
static propTypes = {
indeterminate: PropTypes.bool,
};
componentDidUpdate() {
if (typeof this.props.indeterminate !== 'undefined') {
this.checkbox.indeterminate = this.props.indeterminate;