Skip to content

Commit

Permalink
basic functionality. no tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpgeeganage committed Jan 19, 2019
1 parent 6bf9b78 commit 7a24e56
Show file tree
Hide file tree
Showing 14 changed files with 2,760 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
*.vscode/
logs
*.log
node_modules
.idea
.DS_Store
dist/
.nyc_output/
coverage
run.sh
12 changes: 12 additions & 0 deletions .npmignore
@@ -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
2 changes: 2 additions & 0 deletions .prettierignore
@@ -0,0 +1,2 @@
dist
*.d.ts
11 changes: 11 additions & 0 deletions .prettierrc
@@ -0,0 +1,11 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 80,
"proseWrap": "preserve",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
}
10 changes: 10 additions & 0 deletions .travis.yml
@@ -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
4 changes: 4 additions & 0 deletions _config.yml
@@ -0,0 +1,4 @@
theme: jekyll-theme-modernist
include:
- '_*_.html'
- '_*_.*.html'
81 changes: 81 additions & 0 deletions lib/ifto.ts
@@ -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);
});
}
}
}
5 changes: 5 additions & 0 deletions lib/index.ts
@@ -0,0 +1,5 @@
import { Ifto } from './ifto';
export function IftoProcess() {
const ifTo = Ifto.getInstance();
ifTo.attachToGlobal(global);
}

0 comments on commit 7a24e56

Please sign in to comment.