Skip to content

Instantly share code, notes, and snippets.

@clinyong
clinyong / route.ts
Created September 24, 2023 01:46
Using Next.js to fetch an audio file and respond it to the client
import { NextResponse } from "next/server";
async function GET(
_request: Request,
{ params }: { params: { url: string } }
) {
// An audio url, for example, https://xxx/xxx.mp3
const audioUrl = decodeURIComponent(params.url);
const headers = new Headers();
const res = await fetch(audioUrl).then((res) => {
@clinyong
clinyong / tree-printer.js
Last active May 8, 2022 04:16
A gist to print binary tree in Node.js
class TreePrinter {
constructor() {
this._maxDigits = 0;
}
// BFS: https://en.wikipedia.org/wiki/Breadth-first_search
// empty node will be null in list
_getBreadthFirstList(rootNode, maxHeight) {
const queue = [rootNode];
const list = [];
@clinyong
clinyong / Transaction.js
Created August 19, 2017 08:58
react transaction demo
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
@clinyong
clinyong / config.yaml
Created September 25, 2020 16:56
clash custom
payload:
# company ban
- DOMAIN-SUFFIX,yinxiang.com,Proxy
- DOMAIN-SUFFIX,yuque.com,Proxy
- DOMAIN-SUFFIX,shimo.im,Proxy
- DOMAIN-SUFFIX,docs.qq.com,Proxy
# custom
- DOMAIN-SUFFIX,setapp.com,Proxy
- DOMAIN-SUFFIX,ankiweb.net,Proxy
[rulesets]
ruleset=🎯 全球直连,clash-classic:https://gist.githubusercontent.com/clinyong/5d734fc72d0390e831c4e5279a69a9f3/raw/5a3eaa83e1c637a2356df744fb9b48bcd941e5d9/clash-pdd,86400
@clinyong
clinyong / build.js
Created March 11, 2016 15:38
webpack build script.
import webpack from 'webpack'
import config from './webpack'
import swig from 'swig'
import fs from 'fs'
function writeTemplate (name) {
let sourceTemplate = config.templatePath + '/prod.tmpl'
let targetTemplate = config.buildTemplatePath + '/index.tmpl'
let content = swig.renderFile(sourceTemplate, {name})
fs.writeFile(targetTemplate, content, err => {
@clinyong
clinyong / Promise.ts
Created July 23, 2018 02:50
Minimal promise implementation for JavaScript
class Promise() {
constructor() {
}
}
@clinyong
clinyong / event-pool.js
Last active July 5, 2018 03:48
Minimum react event pool
const eventPool = [];
const EVENT_POOL_SIZE = 10;
function SyntheticEvent(nativeEvent) {
this.nativeEvent = nativeEvent;
}
SyntheticEvent.prototype.persist = function persist() {
this.isPersistent = true;
};
@clinyong
clinyong / delBom.php
Created August 27, 2014 01:21
I use this script to detect bom file recursively, and delete bom with sublime text.
<?php
/**
* 用法:复制以下代码至新建的php文件中,将该php文件放置项目目录,运行即可。代码来源于网络。
* chenwei 注。
*/
header('content-Type: text/html; charset=utf-8');
$auto=0;/* 设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除 */
$basedir='.';
$loop=true;
echo '当前查找的目录为:'.$basedir.'当前的设置是:';
@clinyong
clinyong / walkdir.js
Created February 24, 2017 14:52
walk directory
function walkDir(root) {
const stat = fs.statSync(root);
if (stat.isDirectory()) {
const dirs = fs.readdirSync(root).filter(item => !item.startsWith('.'));
let results = dirs.map(sub => walkDir(`${root}/${sub}`));
return [].concat(...results);
} else {
return root;
}