@prisma/debug
Advanced tools
Comparing version 2.0.0-alpha.1205 to 2.0.0-alpha.1210
import DebugLib from 'debug'; | ||
declare function Debug(namespace: string): DebugLib.Debugger; | ||
declare namespace Debug { | ||
var enable: (namespace: any) => void; | ||
var enabled: (namespace: any) => boolean; | ||
var enable: (namespace: string) => void; | ||
var enabled: (namespace: string) => boolean; | ||
} | ||
export default Debug; | ||
export declare type Debugger = DebugLib.Debugger; | ||
export declare function getLogs(numLines?: number): string; | ||
export declare function getLogs(numChars?: number): string; |
@@ -12,6 +12,39 @@ "use strict"; | ||
const enabledNamespaces = new Map(); | ||
// parse the enabled namespaces that come from process.env.DEBUG | ||
const envDebug = process.env.DEBUG ? process.env.DEBUG + ',' : ''; | ||
const skips = debug_1.default.skips.slice(); | ||
const names = debug_1.default.names.slice(); | ||
// same algorithm as original `debug` library: | ||
function isEnabledByEnvVar(name) { | ||
if (name[name.length - 1] === '*') { | ||
return true; | ||
} | ||
for (const skip of skips) { | ||
if (skip.test(name)) { | ||
return false; | ||
} | ||
} | ||
for (const nameRegex of names) { | ||
if (nameRegex.test(name)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function Debug(namespace) { | ||
const debug = debug_1.default(namespace); | ||
namespaces.push(namespace); | ||
debug_1.default.enable(namespaces.join(',')); | ||
debug_1.default.enable(envDebug + namespaces.join(',')); | ||
if (isEnabledByEnvVar(namespace)) { | ||
enabledNamespaces.set(namespace, true); | ||
} | ||
const newDebug = (formatter, ...args) => { | ||
return debug(formatter, ...args); | ||
}; | ||
newDebug.log = console.error.bind(console); | ||
newDebug.color = debug.color; | ||
newDebug.namespace = debug.namespace; | ||
newDebug.enabled = debug.enabled; | ||
newDebug.destroy = debug.destroy; | ||
newDebug.extend = debug.extend; | ||
debug.log = (...args) => { | ||
@@ -24,6 +57,6 @@ cache.push(args); | ||
if (enabledNamespaces.has(namespace)) { | ||
console.error(...args); | ||
newDebug.log(...args); | ||
} | ||
}; | ||
return debug; | ||
return newDebug; | ||
} | ||
@@ -35,14 +68,13 @@ exports.default = Debug; | ||
Debug.enabled = (namespace) => enabledNamespaces.has(namespace); | ||
function getLogs(numLines = 100) { | ||
// https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers | ||
// we need some space for other characters, so we go for 30k here | ||
function getLogs(numChars = 30000) { | ||
// flatmap on text level | ||
const lines = cache | ||
.map((c) => c.join(' ')) | ||
.join('\n') | ||
.split('\n'); // no this is not a no-op | ||
if (lines.length <= numLines) { | ||
return lines.join('\n'); | ||
let output = cache.map((c) => c.join(' ')).join('\n'); | ||
if (output.length < numChars) { | ||
return output; | ||
} | ||
return lines.slice(-numLines).join('\n'); | ||
return output.slice(-numChars); | ||
} | ||
exports.getLogs = getLogs; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@prisma/debug", | ||
"version": "2.0.0-alpha.1205", | ||
"version": "2.0.0-alpha.1210", | ||
"main": "dist/index.js", | ||
@@ -21,2 +21,3 @@ "types": "dist/index.d.ts", | ||
"lint-staged": "^10.1.4", | ||
"strip-ansi": "^6.0.0", | ||
"ts-jest": "^25.3.1", | ||
@@ -23,0 +24,0 @@ "typescript": "^3.8.3" |
@@ -1,4 +0,4 @@ | ||
# @prisma/get-platform | ||
# @prisma/debug | ||
Platform detection used for Prisma 2 binaries. | ||
A cached [`debug`](https://github.com/visionmedia/debug/). | ||
@@ -8,5 +8,16 @@ ## Usage | ||
```ts | ||
import { getPlatform } from '@prisma/get-platform' | ||
import Debug, { getLogs } from '@prisma/debug' | ||
const platform = await getPlatform() | ||
const debug = Debug('my-namespace') | ||
try { | ||
debug('hello') | ||
debug(process.env) | ||
throw new Error('oops') | ||
} catch (e) { | ||
console.error(e) | ||
console.error(`We just crashed. But no worries, here are the debug logs:`) | ||
// get 10 last lines of debug logs | ||
console.error(getLogs(10)) | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16090
17
236
23
15
5
1