Skip to content

Commit

Permalink
Initial commit - define interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dimadeveatii committed Jan 21, 2019
0 parents commit 42fc1f1
Show file tree
Hide file tree
Showing 21 changed files with 496 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Editor configuration, see http://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
max_line_length = off
trim_trailing_whitespace = true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
build
package-lock.json
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
node_js:
- "lts/*"
- "node"
cache:
directories:
- "node_modules"
install:
- "npm install"
- "gulp tslint"
- "gulp test"
script: "npm run coverage"
after_script: "npm install coveralls@3.0.0 && cat ./coverage/lcov.info | coveralls"
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"explorer.confirmDragAndDrop": false,
"editor.tabSize": 2,
"editor.renderWhitespace": "all",
"editor.formatOnSave": true,
"explorer.confirmDelete": false,
"files.exclude": {
"**/node_modules": true,
"**/build": true,
"**/coverage": true
}
}
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.ex.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
]
}
]
}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2019 Labs42 <hello@labs42.io>

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.

29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# micro-decorators

Microservice pattern decorators.

```ts
// provide example
```

TODO: under development...

Description...

## Installation

Using npm:

```javascript
$ npm install 'micro-decorators' --save
```

Importing:

```javascript
import { query } from 'micro-decorators';
```

## License

[MIT](LICENSE)
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
12 changes: 12 additions & 0 deletions examples/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { timeout } from '../lib';

class Test {
@timeout(10)
do(): Promise<number> {
return new Promise((res, rej) => setTimeout(res, 1000));
}
}

console.log('Hello world.');

const t = new Test().do().catch(err => console.log('failed'));
151 changes: 151 additions & 0 deletions gulpfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { Gulpclass, MergedTask, SequenceTask, Task } from 'gulpclass';

import * as gulp from 'gulp';
import * as del from 'del';
import * as replace from 'gulp-replace';
import * as shell from 'gulp-shell';
import * as mocha from 'gulp-mocha';
import * as ts from 'gulp-typescript';
import * as sourcemaps from 'gulp-sourcemaps';
const tslint = require('gulp-tslint');

@Gulpclass()
export class Gulpfile {

/**
* Cleans build folder.
*/
@Task()
clean(cb: Function) {
return del(['./build/**', './coverage/**'], cb);
}

/**
* Runs typescript files compilation.
*/
@Task()
compile() {
return gulp.src('./package.json', { read: false })
.pipe(shell(['tsc']));
}

/**
* Runs unit-tests.
*/
@Task()
unit() {
return gulp.src('./build/compiled/test/**/*.js')
.pipe(mocha());
}

/**
* Compiles the code and runs tests.
*/
@SequenceTask()
test() {
return ['clean', 'compile', 'unit'];
}

/**
* Runs the tslint.
*/
@Task()
tslint() {
return gulp.src(['./lib/**/*.ts', './test/**/*.ts', './examples/**/*.ts'])
.pipe(tslint({ formatter: 'stylish' }))
.pipe(tslint.report({
emitError: true,
summarizeFailureOutput: true,
sort: true,
bell: true,
}));
}

/**
* Copies all sources to the package directory.
*/
@MergedTask()
packageCompile() {
const tsProject = ts.createProject('tsconfig.json');
const tsResult = gulp.src(['lib/**/*.ts'])
.pipe(sourcemaps.init())
.pipe(tsProject());

return [
tsResult.dts.pipe(gulp.dest('build/package')),
tsResult.js
.pipe(sourcemaps.write('.', { sourceRoot: '', includeContent: true }))
.pipe(gulp.dest('build/package')),
];
}

/**
* Moves all compiled files to the final package directory.
*/
@Task()
packageMoveCompiledFiles() {
return gulp.src('./build/package/lib/**/*')
.pipe(gulp.dest('./build/package'));
}

/**
* Clears the directory with compiled files.
*/
@Task()
packageClearCompileDirectory(cb: Function) {
return del(['build/package/lib/**'], cb);
}

/**
* Change the "private" state of the packaged package.json file to public.
*/
@Task()
packagePreparePackageFile() {
return gulp.src('./package.json')
.pipe(replace('\"private\": true,', '\"private\": false,'))
.pipe(gulp.dest('./build/package'));
}

/**
* This task will replace all typescript code blocks in the README
* (since npm does not support typescript syntax highlighting)
* and copy this README file into the package folder.
*/
@Task()
packageReadmeFile() {
return gulp.src('./README.md')
.pipe(replace(/```ts([\s\S]*?)```/g, '```javascript$1```'))
.pipe(gulp.dest('./build/package'));
}

/**
* Creates a package that can be published to npm.
*/
@SequenceTask()
package() {
return [
'clean',
'packageCompile',
'packageMoveCompiledFiles',
'packageClearCompileDirectory',
['packagePreparePackageFile', 'packageReadmeFile'],
];
}

/**
* Publishes a package to npm from ./build/package directory.
*/
@Task()
npmPublish() {
return gulp.src('./package.json', { read: false })
.pipe(shell(['cd ./build/package && npm publish --access public']));
}

/**
* Creates a package and publishes it to npm.
*/
@SequenceTask()
publish() {
return ['test', 'tslint', 'package', 'npmPublish'];
}
}
8 changes: 8 additions & 0 deletions lib/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Caches the result of a method.
* @param timeout cache timeout in milliseconds.
* @todo implement `varyBy`
*/
export function cache(timeout: number) {
throw new Error('Not implemented.');
}
11 changes: 11 additions & 0 deletions lib/circuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* A circuit breaker.
* After the method fails `maxFailures` count it enter the closed state and
* rejects with `Circuit closed.` error. Once in closed state, the circuit fails
* for the provided `timeout` milliseconds.
* @param maxFailures the max number of failures until the circuit gets closed.
* @param timeout timeout in milliseconds to keep the circuit in closed state.
*/
export function circuit(maxFailures: number, timeout: number) {
throw new Error('Not implemented.');
}
8 changes: 8 additions & 0 deletions lib/concurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Limits the number of concurrent executions of a method.
* When the limit is hit, the execution is delayed and queued.
* @param degree the max number of concurrent executions.
*/
export function concurrency(degree: number) {
throw new Error('Not implemented.');
}
6 changes: 6 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { cache } from './cache';
export { circuit } from './circuit';
export { concurrency } from './concurrency';
export { retry } from './retry';
export { throttle } from './throttle';
export { timeout } from './timeout';
25 changes: 25 additions & 0 deletions lib/retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

/**
* Retries the execution of a method for a given number of attempts.
* If the method fails to succeed after `attempts` retries, it fails
* with error `Retry failed.`
* @param attempts max number of attempts to retry execution
* @param timeout (optional) the number of milliseconds to wait before making new attempt.
* When no value is provided, retries immediately.
*/
export function retry(attempts: number, timeout?: number);

/**
* Retries the execution of a method for a given number of attempts.
* If the method fails to succeed after `attempts` retries, it fails
* with error `Retry failed.`
* @param attempts max number of attempts to retry execution
* @param pattern an array of timeouts in milliseconds to wait between each attempt.
* When the length of the array is less then the number of attempts,
* then last array value is used for the remaining attempts.
*/
export function retry(attempts: number, pattern?: number[]);

export function retry(attempts: number, timeoutOrPattern?: number | number[]): any {
throw new Error('Not implemented.');
}
7 changes: 7 additions & 0 deletions lib/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Limits the number of concurrent executions of a method.
* @param rate the rate of concurrent connections per second.
*/
export function throttle(rate: number) {
throw new Error('Not implemented.');
}
Loading

0 comments on commit 42fc1f1

Please sign in to comment.