Skip to content

Instantly share code, notes, and snippets.

@bakoushin
bakoushin / sw-await.js
Created February 1, 2018 08:12
Service Worker: Promises vs Async/Await
const version = 1;
const appPrefix = 'myApp-';
const staticCacheName = appPrefix + 'static-v' + version;
const imagesCacheName = appPrefix + 'content-imgs';
var allCaches = [
staticCacheName,
imagesCacheName
];
self.addEventListener('message', event => {
@bakoushin
bakoushin / sw.js
Created February 1, 2018 08:14
ServiceWorker Fetch Examples — Hijacking Requests
self.addEventListener('fetch', event => {
/*
HIJACKING RESPONSE EXAMPLES
Uncomment examples one by one to see how it works.
Don't forget to enable 'Update on reload' in Application - Service Workers.
*/
// Example 1: respond with arbitrary HTML
@bakoushin
bakoushin / 90-monitor-hotplug.rules
Last active May 30, 2023 02:51
Lubuntu 18.04 external HDMI display hotplug
ACTION=="change", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/USER/.Xauthority", RUN+="/bin/bash /home/USER/bin/monitorhotplug.sh"
@bakoushin
bakoushin / verify.apple.attestation.js
Created February 21, 2022 19:34 — forked from herrjemand/verify.apple.attestation.js
Snippet code to verify apple anonymous attestation
const crypto = require('crypto');
const base64url = require('base64url');
const cbor = require('cbor');
const asn1 = require('@lapo/asn1js');
const jsrsasign = require('jsrsasign');
/* Apple Webauthn Root
* Original is here https://www.apple.com/certificateauthority/Apple_WebAuthn_Root_CA.pem
*/
let appleWebAuthnRoot = 'MIICEjCCAZmgAwIBAgIQaB0BbHo84wIlpQGUKEdXcTAKBggqhkjOPQQDAzBLMR8wHQYDVQQDDBZBcHBsZSBXZWJBdXRobiBSb290IENBMRMwEQYDVQQKDApBcHBsZSBJbmMuMRMwEQYDVQQIDApDYWxpZm9ybmlhMB4XDTIwMDMxODE4MjEzMloXDTQ1MDMxNTAwMDAwMFowSzEfMB0GA1UEAwwWQXBwbGUgV2ViQXV0aG4gUm9vdCBDQTETMBEGA1UECgwKQXBwbGUgSW5jLjETMBEGA1UECAwKQ2FsaWZvcm5pYTB2MBAGByqGSM49AgEGBSuBBAAiA2IABCJCQ2pTVhzjl4Wo6IhHtMSAzO2cv+H9DQKev3//fG59G11kxu9eI0/7o6V5uShBpe1u6l6mS19S1FEh6yGljnZAJ+2GNP1mi/YK2kSXIuTHjxA/pcoRf7XkOtO4o1qlcaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUJtdk2cV4wlpn0afeaxLQG2PxxtcwDgYDVR0PAQH/BAQDAgEGMAoGCCqGSM49BAMDA2cAMGQCMFrZ+9DsJ1PW9hfNdBywZDsWDbWFp28it1d/5w2RPkRX3Bbn/UbDTNLx7Jr3jAGGiQIwHFj+dJZYUJR786osByBelJYsVZd2GbHQu209b5
async function* dataGenerator() {
while (true) {
const csvStream = fs.createReadStream(pathToCSV).pipe(csv({
headers: ['center', 'left', 'right', 'steering', 'throttle', 'brake', 'speed'],
mapValues: ({ value }) => value.trim()
}));
for await (const { center, left, right, steering } of csvStream) {
const centerImageBuffer = fs.promises.readFile(center);
const leftImageBuffer = fs.promises.readFile(left);
const parseArgs = require('minimist');
const io = require('socket.io')();
const tf = require('@tensorflow/tfjs-node');
const { model: modelDir = 'model', speed: maxSpeed = 30 } = parseArgs(
process.argv.slice(2)
);
tf.loadLayersModel(`file://${modelDir}/model.json`).then((model) => {
io.on('connection', async (socket) => {
@bakoushin
bakoushin / README-Template.md
Created July 16, 2019 05:02 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@bakoushin
bakoushin / promise-chain.js
Created December 10, 2018 16:55
Start downloading files simultaneously, but display them in order
const files = [fetch('file1'), fetch('file2'), fetch('file3')];
let chain = Promise.resolve();
for (const file of files) {
chain = chain
.then(() => {
return file;
})
.then(file => console.log(file));
@bakoushin
bakoushin / @ SimpleDB.md
Created November 13, 2018 14:30
Simple DB

SimpleDB - Like Indexed DB, but Simple

A simple asynchronous data store.

STATUS: This is a thought experiment, not a serious proposal. Would basic async storage like this be useful? With this plus some locking primitive, could you build Indexed DB?

Like Indexed DB:

  • rich value types - store anything you can structured clone
  • rich key types - Number, String, Date, Array (of other key types)
@bakoushin
bakoushin / tweak.html
Last active October 3, 2018 04:55
Tweak to make InVision prototype work with Google Analytics
<!-- Tweak for Google Analytics to catch SPA navigation -->
<script>
var currentPage = window.location.href;
window.onload = function() {
setInterval(function() {
if (currentPage !== window.location.href) {
currentPage = window.location.href;
var newPage = window.location.pathname + window.location.hash
ga('set', 'page', newPage);
ga('send', 'pageview');