Skip to content

Instantly share code, notes, and snippets.

Avatar

Dag-Inge Aas dagingaa

View GitHub Profile
@dagingaa
dagingaa / prototype-to-class.js
Last active Jun 25, 2020
Codemod to transform well-written function prototype style classes into the new ES2015 syntax. Requires jscodeshift.
View prototype-to-class.js
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const root = j(file.source);
// We have to add "use strict" for node to play nice
// Taken from https://github.com/cpojer/js-codemod/blob/master/transforms/use-strict.js
const hasStrictMode = body =>
body.some(
statement => j.match(statement, {
type: 'ExpressionStatement',
@dagingaa
dagingaa / backdateStripeSubscription.js
Created Feb 5, 2020
Backdates a Stripe subscription without triggering proration. Probably the easiest way to undo a mistake if you ended up screwing something up royally and need to reset.
View backdateStripeSubscription.js
const stripe = require("stripe")("api_key");
const CUSTOMER = "";
const PLAN = "";
const quantity = 1;
const startTimeInSecondsSinceEpoch = 0;
const billingCycleAnchorInSecondsSinceEpoch = 0;
async function foo() {
const subscription = await stripe.subscriptions.create({
@dagingaa
dagingaa / oauth-client.rb
Created Jul 9, 2012
A minimal Sinatra OAuth 2.0 client and resource server.
View oauth-client.rb
require 'sinatra'
require 'open-uri'
require 'json'
# This application is the bare minimum to authorize with OAuth 2.0
# using the authorization grant scheme. No error handling included.
# The application is both a client and a resource server.
# Start it by using 'ruby <file>' and navigate to http://localhost:4567
#
# The application also needs the oauth-server written in Java.
View mediasteam_average_lightness.js
// run in console on a https page
stream = await navigator.mediaDevices.getUserMedia({ video: true, audio:true })
video = document.createElement("video");
video.muted = true;
video.srcObject = stream;
await video.play();
canvas = document.createElement("canvas")
ctx = canvas.getContext("2d")
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
imageData = ctx.getImageData(0,0,video.videoWidth, video.videoHeight)
View analyze_image_color_data.js
src = "url_to_imgur"
img = new Image();
img.crossOrigin = "Anonymous";
img.addEventListener("load", analyze, false);
img.src = src;
function analyze() {
canvas = document.createElement("canvas")
ctx = canvas.getContext("2d")
View stun-binding-request-test.js
const dgram = require('dgram');
// STUN binding request
const bind = Buffer.from('000300002112a4425f73a4b270529f877091d580', 'hex');
const socket = dgram.createSocket('udp4');
socket.on('message', (data) => {
console.log('STUN BINDING RESPONSE', data.toString());
});
View media-capabilities-video.js
if ('mediaCapabilities' in navigator && 'encodingInfo' in navigator.mediaCapabilities) {
const videoFileConfiguration = {
type : 'record',
video : {
contentType: "video/webm;codecs=vp8",
width: 1920,
height: 1080,
bitrate: 2500000,
framerate: 30,
}
View webrtcsupportgate.js
function WebRtcSupportGate({
children,
forceUnsupported,
forceUnsupportedIos,
forceUnsupportedFacebook,
forceUnsupportedInApp,
}) {
const isWebRtcSupported =
!!global.RTCPeerConnection &&
!!global.navigator.mediaDevices &&
@dagingaa
dagingaa / passport-grean.js
Created Sep 5, 2017
A small wrapper around openid-client passport for integrating with Grean EasyID
View passport-grean.js
"use strict";
const { Issuer, Strategy } = require("openid-client");
Issuer.defaultHttpOptions = { timeout: 5000 };
function createUser(userinfo) {
const name = userinfo.name.split(",");
return {
id: userinfo.uniqueuserid,
View maximally-minimal-es6-classes.js
// Source: http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
class Monster {
// A method named "constructor" defines the class’s constructor function.
constructor(name, health) {
// public name object
this.name = name;
// private name object
this[pHealth] = health;
}
You can’t perform that action at this time.