-
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
1 parent
6bf9b78
commit 7a24e56
Showing
14 changed files
with
2,760 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,10 @@ | ||
*.vscode/ | ||
logs | ||
*.log | ||
node_modules | ||
.idea | ||
.DS_Store | ||
dist/ | ||
.nyc_output/ | ||
coverage | ||
run.sh |
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,12 @@ | ||
doc | ||
lib | ||
test | ||
examples | ||
_config.yml | ||
tsconfig.json | ||
tslint.json | ||
.prettierrc | ||
.prettierignore | ||
.travis.yml | ||
yarn-error.log | ||
CODE_OF_CONDUCT.md |
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 @@ | ||
dist | ||
*.d.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,11 @@ | ||
{ | ||
"arrowParens": "always", | ||
"bracketSpacing": true, | ||
"printWidth": 80, | ||
"proseWrap": "preserve", | ||
"semi": true, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false | ||
} |
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,10 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
- "10" | ||
script: | ||
- npm test | ||
- npm run lint | ||
- npm install codecov -g | ||
after_success: | ||
- npm run test-coverage-report |
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,4 @@ | ||
theme: jekyll-theme-modernist | ||
include: | ||
- '_*_.html' | ||
- '_*_.*.html' |
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,81 @@ | ||
import { Context } from 'aws-lambda'; | ||
|
||
type OutputFunction = (...arg: any[]) => void; | ||
export const ENV_VAR_NAME = 'ifto'; | ||
export const ENV_VAR_EXPECTED_VALUE = '1'; | ||
|
||
export interface ExtendedNodeJsGlobal extends NodeJS.Global { | ||
Ifto?: Ifto; | ||
} | ||
|
||
export interface Options { | ||
flushLogsWhenDifferenceIs: number; | ||
output: OutputFunction; | ||
} | ||
|
||
const defaults: Options = { | ||
flushLogsWhenDifferenceIs: 50, | ||
output: console.log | ||
}; | ||
|
||
export class Ifto { | ||
private logEntries: string[] = []; | ||
private flushLogsWhenDifferenceIs: number; | ||
private output: OutputFunction; | ||
private lambdaContext?: Context; | ||
|
||
constructor() { | ||
this.flushLogsWhenDifferenceIs = defaults.flushLogsWhenDifferenceIs; | ||
this.output = defaults.output; | ||
} | ||
|
||
static getInstance() { | ||
return new Ifto(); | ||
} | ||
|
||
addContext(lambdaContext: Context) { | ||
this.lambdaContext = lambdaContext; | ||
} | ||
|
||
updateConfigs(options: Partial<Options>) { | ||
if (options.flushLogsWhenDifferenceIs) { | ||
this.flushLogsWhenDifferenceIs = options.flushLogsWhenDifferenceIs; | ||
} | ||
|
||
if (options.output) { | ||
this.output = options.output; | ||
} | ||
} | ||
|
||
attachToGlobal(globalObject: ExtendedNodeJsGlobal) { | ||
globalObject.Ifto = this; | ||
} | ||
|
||
log(entry: string) { | ||
this.logEntries.push(entry); | ||
} | ||
|
||
start(currentProcess: NodeJS.Process) { | ||
const envValue = currentProcess.env[ENV_VAR_NAME]; | ||
if (envValue && envValue.toLocaleLowerCase() === ENV_VAR_EXPECTED_VALUE) { | ||
this.hookIntoProcess(currentProcess); | ||
} | ||
} | ||
|
||
private hookIntoProcess(currentProcess: NodeJS.Process) { | ||
if (!this.lambdaContext) { | ||
throw new Error('Lambda context is not set'); | ||
} | ||
|
||
if ( | ||
this.lambdaContext.getRemainingTimeInMillis() <= | ||
this.flushLogsWhenDifferenceIs | ||
) { | ||
this.output(this.logEntries.join(',')); | ||
} else { | ||
currentProcess.nextTick(() => { | ||
this.hookIntoProcess(currentProcess); | ||
}); | ||
} | ||
} | ||
} |
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,5 @@ | ||
import { Ifto } from './ifto'; | ||
export function IftoProcess() { | ||
const ifTo = Ifto.getInstance(); | ||
ifTo.attachToGlobal(global); | ||
} |
Oops, something went wrong.