Skip to content

Instantly share code, notes, and snippets.

View Thinkscape's full-sized avatar

Arthur Bodera Thinkscape

View GitHub Profile
@Thinkscape
Thinkscape / README.md
Last active May 7, 2024 14:20
Fix lodash edge compatibility (vercel, cloudflare workers)

Lodash and Edge Runtimes

See: lodash/lodash#5862

Lodash uses Function(String) which upsets Next.js and is incompatible with CloudFlare workers (Vercel Edge), resulting in errors like:

Failed to compile.
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
<?php
/*
* qtfaststart.php v0.1 by Valentin Schmidt
* based on qt-faststart.c v0.2 by Mike Melanson (melanson@pcisys.net)
*
* This file is placed in the public domain. Use the program however you
* see fit.
*
* This utility rearranges a Quicktime file such that the moov atom
* is in front of the data, thus facilitating network streaming.
@Thinkscape
Thinkscape / gist:1136563
Created August 10, 2011 11:05
Very simple performance comparison - arrays vs objects (PHP 5.3.6)
# php -v
PHP 5.3.6 (cli) (built: Mar 17 2011 20:58:15)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $s[] = array("name"=>"Adam","age"=>35); }; echo memory_get_peak_usage(); ' | php
655040
# echo '<?php $s = array(); for($x=0;$x<1000;$x++){ $o = new ArrayObject; $o->name = "Adam"; $o->age = 35; $s[] = $o;} echo memory_get_peak_usage(); ' | php
887984
@Thinkscape
Thinkscape / isAssocArrayBench.php
Created March 3, 2012 11:35
A benchmark of several methods for checking if PHP array is associative
<?php
if(!isset($argv[1])){
echo "Usage: ".$argv[0]." (number of iterations)\n";
exit(1);
}
/**
* Arrays to check
*/
$tests = array(
@Thinkscape
Thinkscape / flattenExceptionBacktrace.php
Last active December 28, 2023 12:28
Make any PHP Exception serializable by flattening complex values in backtrace.
<?php
function flattenExceptionBacktrace(\Exception $exception) {
$traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace');
$traceProperty->setAccessible(true);
$flatten = function(&$value, $key) {
if ($value instanceof \Closure) {
$closureReflection = new \ReflectionFunction($value);
$value = sprintf(
'(Closure at %s:%s)',
@Thinkscape
Thinkscape / zodErrorToString.ts
Last active November 21, 2023 23:12
Recursively convert ZodError to a string, or array of issue descriptions.
import { type ZodError, type ZodIssue } from "zod";
export function zodErrorToString(error: ZodError, separator = ", ") {
return zodIssuesToStrings(error.issues).join(separator);
}
function zodIssuesToStrings(obj: ZodIssue[]) {
let results: string[] = [];
function traverse(node: ZodIssue | ZodIssue[]) {
@Thinkscape
Thinkscape / disk-bench.sh
Last active November 8, 2023 02:24
disk benchmark using fio at different block sizes
#!/bin/env bash
for bs in 4k 16k 64k 256k 1M 4M; do
echo "Running burst speed test of $1 with block size $bs";
fio --name=burst_speed_test --ioengine=libaio --iodepth=1 --rw=rw --direct=1 --runtime=60 --time_based \
--verify=crc32c --do_verify=1 --verify_fatal=1 --verify_dump=1 --verify_backlog=1000 \
--filename=$1 --bs=$bs --numjobs=1 --group_reporting;
done
@Thinkscape
Thinkscape / xml-to-json-problem.php
Last active August 10, 2023 10:36
Problem with using json_encode() to transform XML into array.
<?php
$xml = new \SimpleXMLElement('<xml>
<node attr="bar">second</node>
</xml>');
$array = json_decode(json_encode($xml), true);
print_r($array);
/*
Array
@Thinkscape
Thinkscape / EagerQuoteStrategy.php
Last active March 14, 2023 11:58
Doctrine2 ORM strategy for quoting all identifiers by default.
<?php
namespace Doctrine\ORM\Mapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* A set of rules for determining the physical column, alias and table quotes
*/
class EagerQuoteStrategy implements QuoteStrategy
@Thinkscape
Thinkscape / EffectiveUrlMiddleware.php
Last active October 4, 2022 18:30
getEffectiveUrl() replacement for Guzzle 6.*
<?php
namespace Thinkscape\Guzzle;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class EffectiveUrlMiddleware
{
/**
* @var Callable