-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 1f1776a
Showing
10 changed files
with
179 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,3 @@ | ||
dist | ||
node_modules | ||
*.log |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
const path = require('path') | ||
const { build } = require('esbuild') | ||
|
||
const buildFile = (input, output) => | ||
build({ | ||
entryPoints: [input], | ||
outfile: output, | ||
bundle: true, | ||
platform: 'node', | ||
target: 'node14', | ||
plugins: [ | ||
{ | ||
name: 'make-all-packages-external', | ||
setup(build) { | ||
let filter = /^[^./]|^\.[^./]|^\.\.[^/]/ | ||
build.onResolve({ filter }, (args) => ({ | ||
path: args.path, | ||
external: true, | ||
})) | ||
}, | ||
}, | ||
], | ||
watch: process.argv.includes('-w'), | ||
}) | ||
|
||
buildFile(`./src/index.ts`, `./dist/index.js`).catch((error) => { | ||
if (!error.errors) { | ||
console.error(error) | ||
} | ||
process.exit(1) | ||
}) |
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,18 @@ | ||
{ | ||
"name": "@karimsa/context", | ||
"version": "0.0.0", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"start": "node ./dist/index.js", | ||
"build": "tsc && node esbuild" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^16.7.13", | ||
"@types/uuid": "^8.3.1", | ||
"esbuild": "^0.12.25", | ||
"typescript": "^4.4.2" | ||
}, | ||
"dependencies": { | ||
"uuid": "^8.3.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,52 @@ | ||
import * as uuid from 'uuid'; | ||
|
||
class OperationContextEntry { | ||
private readonly trace: Error; | ||
private readonly contextValues: Record<string, any> = {}; | ||
|
||
constructor(private readonly context: OperationContext) { | ||
this.trace = new Error('-----'); | ||
} | ||
|
||
next() { | ||
return this.context.next() | ||
} | ||
|
||
addContext(key: string, value: any): OperationContextEntry { | ||
this.contextValues[key] = value | ||
return this | ||
} | ||
|
||
addHttpRequest(request: { method: string; url: string; body: any }, response: { status: number; body: any }): OperationContextEntry { | ||
this.addContext('request', { method: request.method, url: request.method, body: request.body }); | ||
this.addContext('response', { status: response.status, body: response.body }) | ||
return this | ||
} | ||
|
||
toJSON() { | ||
const stacktrace = String(this.trace.stack || this.trace).split('\n'); | ||
return { | ||
values: this.contextValues, | ||
// Remove the first line, it has an empty error message | ||
stacktrace: stacktrace.slice(1).map(line => line.trim()), | ||
}; | ||
} | ||
} | ||
|
||
export class OperationContext { | ||
private readonly id: string = uuid.v4(); | ||
private trace: OperationContextEntry[] = []; | ||
|
||
next(): OperationContextEntry { | ||
const entry = new OperationContextEntry(this); | ||
this.trace.push(entry); | ||
return entry; | ||
} | ||
|
||
toJSON() { | ||
return { | ||
id: this.id, | ||
trace: this.trace.map(entry => entry.toJSON()), | ||
} | ||
} | ||
} |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["esnext"], | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"emitDeclarationOnly": true, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"declaration": true, | ||
"declarationDir": "./dist" | ||
} | ||
} |
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,28 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
"@types/node@^16.7.13": | ||
version "16.7.13" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.7.13.tgz#86fae356b03b5a12f2506c6cf6cd9287b205973f" | ||
integrity sha512-pLUPDn+YG3FYEt/pHI74HmnJOWzeR+tOIQzUx93pi9M7D8OE7PSLr97HboXwk5F+JS+TLtWuzCOW97AHjmOXXA== | ||
|
||
"@types/uuid@^8.3.1": | ||
version "8.3.1" | ||
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.1.tgz#1a32969cf8f0364b3d8c8af9cc3555b7805df14f" | ||
integrity sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg== | ||
|
||
esbuild@^0.12.25: | ||
version "0.12.25" | ||
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.25.tgz#c2131cef022cf9fe94aaa5e00110b27fc976221a" | ||
integrity sha512-woie0PosbRSoN8gQytrdCzUbS2ByKgO8nD1xCZkEup3D9q92miCze4PqEI9TZDYAuwn6CruEnQpJxgTRWdooAg== | ||
|
||
typescript@^4.4.2: | ||
version "4.4.2" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86" | ||
integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ== | ||
|
||
uuid@^8.3.2: | ||
version "8.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" | ||
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== |