-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Qu4k
committed
Sep 12, 2020
0 parents
commit 1acbdd5
Showing
13 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
open_collective: denosaurs | ||
github: denosaurs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: check | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup latest deno version | ||
uses: denolib/setup-deno@v2 | ||
with: | ||
deno-version: v1.x | ||
|
||
- name: Run deno fmt | ||
run: deno fmt --check | ||
|
||
- name: Run deno lint | ||
run: deno lint --unstable | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout sources | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup latest deno version | ||
uses: denolib/setup-deno@v2 | ||
with: | ||
deno-version: v1.x | ||
|
||
- name: Run deno test | ||
run: deno test --allow-none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: depsbot | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
schedule: | ||
- cron: "0 0 */2 * *" | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Run depsbot | ||
uses: denosaurs/depsbot@master | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# OS files | ||
.DS_Store | ||
.cache | ||
|
||
# IDE | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020-present the denosaurs team | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# debug | ||
|
||
[![Tags](https://img.shields.io/github/release/denosaurs/debug)](https://github.com/denosaurs/debug/releases) | ||
[![CI Status](https://img.shields.io/github/workflow/status/denosaurs/debug/check)](https://github.com/denosaurs/debug/actions) | ||
[![Dependencies](https://img.shields.io/github/workflow/status/denosaurs/debug/depsbot?label=dependencies)](https://github.com/denosaurs/depsbot) | ||
[![License](https://img.shields.io/github/license/denosaurs/debug)](https://github.com/denosaurs/debug/blob/master/LICENSE) | ||
|
||
```typescript | ||
import { debug } from "https://deno.land/x/debug/mod.ts"; | ||
|
||
const log = debug("worker"); | ||
|
||
for (let i = 0; i < 5; i++) { | ||
log("Hello World"); | ||
} | ||
``` | ||
|
||
## Maintainers | ||
|
||
- Filippo Rossi ([@qu4k](https://github.com/qu4k)) | ||
|
||
## Other | ||
|
||
### Related | ||
|
||
- [debug](https://github.com/visionmedia/debug) - A tiny JavaScript debugging utility modelled after Node.js core's debugging technique. | ||
|
||
### Contribution | ||
|
||
Pull request, issues and feedback are very welcome. Code style is formatted with `deno fmt` and commit messages are done following Conventional Commits spec. | ||
|
||
### Licence | ||
|
||
Copyright 2020-present, the denosaurs team. All rights reserved. MIT license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { createHash } from "https://deno.land/std@0.68.0/hash/mod.ts"; | ||
import { colors } from "./deps.ts"; | ||
|
||
export type ColorFunction = (message: string) => string; | ||
export const colorFunctions: ColorFunction[] = [ | ||
colors.red, | ||
colors.green, | ||
colors.yellow, | ||
colors.blue, | ||
colors.magenta, | ||
colors.cyan, | ||
]; | ||
|
||
function hashCode(s: string): number { | ||
let h = 0; | ||
let l = s.length; | ||
let i = 0; | ||
if (l > 0) while (i < l) h = ((h << 5) - h + s.charCodeAt(i++)) | 0; | ||
return h; | ||
} | ||
|
||
export function generateColor(message: string): ColorFunction { | ||
const hash = hashCode(message); | ||
return colorFunctions[hash % colorFunctions.length]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { generateColor, ColorFunction } from "./colors.ts"; | ||
import { encode } from "./deps.ts"; | ||
import { format } from "./format.ts"; | ||
|
||
export interface Debug { | ||
(fmt: string, ...args: unknown[]): void; | ||
self: Debugger; | ||
} | ||
|
||
export class Debugger { | ||
manager: DebugManager; | ||
ns: string; | ||
color: ColorFunction; | ||
last: number; | ||
enabled: boolean; | ||
|
||
constructor(manager: DebugManager, namespace: string) { | ||
this.manager = manager; | ||
this.ns = namespace; | ||
this.color = generateColor(namespace); | ||
this.last = 0; | ||
this.enabled = manager.enabled.some((r) => r.test(namespace)); | ||
} | ||
|
||
log(fmt: string, ...args: unknown[]): void { | ||
if (!this.enabled) return; | ||
const diff = Date.now() - (this.last || Date.now()); | ||
fmt = format(fmt, ...args); | ||
const msg = `${this.color(this.ns)} ${fmt} ${this.color(`+${diff}ms`)}\n`; | ||
Deno.stderr.writeSync(encode(msg)); | ||
this.last = Date.now(); | ||
} | ||
} | ||
|
||
class DebugManager { | ||
debuggers: Map<string, Debugger>; | ||
enabled: RegExp[]; | ||
|
||
constructor(enabled?: RegExp[]) { | ||
this.debuggers = new Map(); | ||
this.enabled = enabled ?? []; | ||
} | ||
} | ||
|
||
function extract(opts?: string): RegExp[] { | ||
if (!opts || opts.length === 0) return []; | ||
opts = opts.replace(/\s/g, "").replace(/\*/g, ".+"); | ||
return opts.split(",").map((rule) => new RegExp(`^${rule}$`)); | ||
} | ||
|
||
let manager: DebugManager; | ||
|
||
export function withoutEnv(enabled?: RegExp[] | string) { | ||
if (!enabled) enabled = [/.+/]; | ||
if (typeof enabled === "string") enabled = extract(enabled); | ||
manager = new DebugManager(enabled); | ||
} | ||
|
||
export function debug(namespace: string): Debug { | ||
if (!manager) manager = new DebugManager(extract(Deno.env.get("DEBUG"))); | ||
|
||
const dbg = new Debugger(manager, namespace); | ||
manager.debuggers.set(namespace, dbg); | ||
const de: Debug = Object.assign(dbg.log.bind(dbg), { | ||
self: dbg, | ||
}); | ||
return de; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { createHash } from "https://deno.land/std@0.68.0/hash/mod.ts"; | ||
export { encode } from "https://deno.land/std@0.68.0/encoding/utf8.ts"; | ||
export * as colors from "https://deno.land/std@0.68.0/fmt/colors.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { debug, withoutEnv } from "../mod.ts"; | ||
|
||
withoutEnv([/worker:(a|b)/]); | ||
|
||
const logA = debug("worker:a"); | ||
const logB = debug("worker:b"); | ||
const logC = debug("service"); | ||
|
||
// logC.self.enabled = true; | ||
|
||
for (let i = 0; i < 5; i++) { | ||
logA("Hello World"); | ||
logB("Hello World"); | ||
logC("Hello World"); | ||
await new Promise((resolve) => setTimeout(resolve, 200)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export function format(f: string, ...args: unknown[]) { | ||
let i = 0; | ||
let len = args.length; | ||
let str = String(f).replace(/%[sdjoO%]/g, (x: string): string => { | ||
if (x === "%%") return "%"; | ||
if (i >= len) return x; | ||
switch (x) { | ||
case "%s": | ||
return String(args[i++]); | ||
case "%d": | ||
return Number(args[i++]).toString(); | ||
case "%o": | ||
return Deno.inspect(args[i++]).replace("\n", " "); | ||
case "%O": | ||
return Deno.inspect(args[i++]); | ||
case "%j": | ||
try { | ||
return JSON.stringify(args[i++]); | ||
} catch { | ||
return "[Circular]"; | ||
} | ||
default: | ||
return x; | ||
} | ||
}); | ||
for (const x of args.splice(i)) { | ||
if (x === null || !(typeof x === "object" && x !== null)) { | ||
str += " " + x; | ||
} else { | ||
str += " " + Deno.inspect(x); | ||
} | ||
} | ||
return str; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./debug.ts"; |