Skip to content

Instantly share code, notes, and snippets.

View jonasfj's full-sized avatar

Jonas Finnemann Jensen jonasfj

View GitHub Profile
@jonasfj
jonasfj / mkdirp.h
Created December 4, 2013 23:07
A quick and dirty implementation of `mkdir -p` for C++, enjoy.
#ifndef MKDIRP_H
#define MKDIRP_H
#include <sys/stat.h>
#include <errno.h>
#define DEFAULT_MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
/** Utility function to create directory tree */
bool mkdirp(const char* path, mode_t mode = DEFAULT_MODE) {
@jonasfj
jonasfj / entropy.dart
Created April 24, 2021 10:30
How compute shannon entropy of a string in Dart.
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
import 'dart:math';
double entropy(String s) {
final length = s.length.toDouble();
// TODO: Explore if this could be done faster without any allocations, since strings are often short.
final frequencies = <int, int>{};
@jonasfj
jonasfj / main.dart
Created May 28, 2020 10:39
Sample Dart program that handles `sigint`.
// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0
import 'dart:async';
import 'dart:io';
Future<void> main() async {
var count = 0;
ProcessSignal.sigint.watch().forEach((s) {
count++;
@jonasfj
jonasfj / query-all-pubspecs.md
Created January 20, 2020 10:47
Notes on how to query all pubspecs on `pub.dev`.

Query pubspec.yaml for all packages

When considering introduction of a new field in pubspec.yaml, or wondering how many packages use a certain feature, it can be useful to query all pubspecs from pub.dev. The following notes have some scripts to make this easy.

File yaml2json.sh

Helper script to convert YAML to JSON.

@jonasfj
jonasfj / count_mono_repo_pub_packages.dart
Created October 6, 2019 20:07
Count the number of packages on pub.dev that uses package:mono_repo.
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
import 'dart:async' show FutureOr;
import 'dart:async';
import 'dart:convert' show utf8, json;
import 'dart:io' show Directory, Process, IOException, File;
import 'package:http/http.dart' as http;
import 'package:retry/retry.dart' show retry;
import 'package:pool/pool.dart' show Pool;
import aws from 'aws-sdk-promise';
import url from 'url';
import https from 'https';
import fs from 'mz/fs';
import _ from 'lodash';
import path from 'path';
import crypto from 'crypto';
(async function() {

Two simple test files for dart.io.

  1. Run dart download.dart
  2. Run dart extract.dart

Have fun..

@jonasfj
jonasfj / grpc_googleapis_example.dart
Created January 23, 2019 09:45
Example of how to generate GRPC APIs from protobufs and use them in Dart.
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
// Experiments with dart, grpc and googleapis
// ==========================================
//
// 1. Install `protoc`
// 2. `pub global activate protoc_plugin`
// 3. `git clone https://github.com/googleapis/googleapis`
// 4. `protoc --dart_out=grpc:lib/src/generated -I googleapis/ -I /usr/local/include/ googleapis/google/cloud/vision/v1/image_annotator.proto`
@jonasfj
jonasfj / README.md
Last active July 13, 2018 01:06
Comparison of tweetnacl, tweetnacl-fast, node-sodium and js-nacl for node.js

Tests and Benchmarks for Node Ed25519 libraries and bindings

Mainly looking at using this for signing HTTP(S) requests, as a replacements for HMAC-SHA256, which requires symmetric keys.

Note, we want to this work in browser, python and node. But we only really care about performance in node. Clients that sign request, usually don't make hundreds of these per second. And python servers are slow anyways :)

So if it's fast server side on node, when deployed correctly (ie. without

import requests
from relengapi.lib.permissions import p
def scope_satisfied(req, scopes):
"""
Returns true, if scopes satisfies req
"""
return any((
s == req or (s.endswith('*') and req.startswith(s[:-1])) for s in scopes
))