Skip to content

Commit 434628f

Browse files
@jotadevelopersergiohgz
authored andcommitted
feat: add stream library
1 parent 5de1f77 commit 434628f

File tree

11 files changed

+3143
-0
lines changed

11 files changed

+3143
-0
lines changed

core/streams/.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": ["es2015"],
3+
"env": {
4+
"test": {
5+
"plugins": [
6+
"istanbul"
7+
]
8+
}
9+
}
10+
}

core/streams/.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
# 2 space indentation
10+
[{.,}*.{js,yml,yaml}]
11+
indent_style = space
12+
indent_size = 2

core/streams/.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
coverage/
3+
lib/
4+
.nyc_output
5+
tests-report/

core/streams/.eslintrc.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# vim: syntax=yaml
2+
3+
#
4+
# List of very light restrictions designed to prevent obvious errors,
5+
# not impose our own code style upon other contributors.
6+
#
7+
# This is supposed to be used with `eslint --reset`
8+
#
9+
# Created to work with eslint@0.18.0
10+
#
11+
12+
extends: ["eslint:recommended", "google"]
13+
14+
env:
15+
node: true
16+
browser: true
17+
es6: true
18+
mocha: true
19+
20+
parserOptions:
21+
sourceType: "module"
22+
ecmaVersion: 8
23+
ecmaFeatures:
24+
modules: true
25+
26+
rules:
27+
# useful to have in node.js,
28+
# if you're sure you don't need to handle error, rename it to "_err"
29+
handle-callback-err: 2
30+
31+
padded-blocks: 0
32+
33+
# just to make sure we don't forget to remove them when releasing
34+
no-debugger: 2
35+
36+
# add "falls through" for those
37+
no-fallthrough: 2
38+
39+
# enforce use curly always
40+
# curly: 1
41+
42+
# just warnings about whitespace weirdness here
43+
eol-last: 1
44+
no-irregular-whitespace: 1
45+
no-mixed-spaces-and-tabs: [1, smart-tabs]
46+
no-trailing-spaces: 1
47+
48+
# probably always an error, tell me if it's not
49+
no-new-require: 2
50+
51+
# single most important rule here, without it linting won't even
52+
# make any sense
53+
no-undef: 2
54+
55+
# in practice, those are always errors
56+
no-unreachable: 2
57+
58+
# useful for code clean-up
59+
no-unused-vars: [2, {"vars": "all", "args": "none"}]
60+
61+
max-len: [1, 160]
62+
63+
# camelcase is standard, but this should be 1 and then 2 soon
64+
camelcase: 0
65+
66+
# jsdoc is mandatory
67+
require-jsdoc: 0
68+
valid-jsdoc: 0
69+
70+
# this feature is cool but not supported by Node 4, disable via comments
71+
prefer-spread: 1
72+
prefer-rest-params: 1
73+
74+
# encorage use es6
75+
no-var: 2
76+
77+
# configuration that should be upgraded progresivelly
78+
no-constant-condition: 2
79+
no-empty: 2
80+
81+
# loop over objects http://eslint.org/docs/rules/guard-for-in
82+
guard-for-in: 2
83+
84+
# this must be used within classes
85+
no-invalid-this: 2
86+
87+
# All object must be uppercase
88+
new-cap: 2
89+
90+
# readbility is important, no multiple inline declarations
91+
one-var: 2
92+
93+
# console not allowed unless for testing
94+
no-console: [2, {"allow": ["log", "warn"]}]

core/streams/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
npm-debug.log
2+
.DS_Store
3+
4+
lib/
5+
node_modules/
6+
7+
# Istanbul
8+
.nyc*
9+
tests-report
10+
11+
# IDE
12+
.vscode/*
13+
.idea/
14+
*.log
15+
*.tar
16+
*.gz
17+

core/streams/.npmignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
src/
2+
tests-report/
3+
.nyc_output
4+
.editorconfig
5+
.gitignore
6+
yarn-error.log
7+
yarn.lock
8+
.eslintrc
9+
.babelrc
10+
test/
11+
.eslintignore
12+
.eslintrc.yml
13+
.npmignore
14+
.travis.yml

core/streams/.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- '4'
4+
- '6'
5+
- '7'
6+
- '8'
7+
sudo: false
8+
script: npm install . && npm run cover

core/streams/package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@verdaccio/streams",
3+
"version": "0.0.1",
4+
"description": "helper to delay streams",
5+
"main": "lib/index.js",
6+
"scripts": {
7+
"test": "npm run lint && mocha --require babel-polyfill --compilers js:babel-core/register ./test/**/*.spec.js",
8+
"lint": "eslint .",
9+
"build": "babel src/ --out-dir lib/ --copy-files",
10+
"cover": "cross-env NODE_ENV=test nyc npm t"
11+
},
12+
"devDependencies": {
13+
"babel-cli": "6.24.1",
14+
"babel-core": "6.25.0",
15+
"babel-plugin-istanbul": "4.1.4",
16+
"babel-polyfill": "6.23.0",
17+
"babel-preset-es2015": "6.24.1",
18+
"cross-env": "5.0.1",
19+
"eslint": "4.1.1",
20+
"eslint-config-google": "0.9.1",
21+
"mocha": "3.4.2",
22+
"nyc": "11.0.3"
23+
},
24+
"nyc": {
25+
"include": [
26+
"src/**/*.js"
27+
],
28+
"all": true,
29+
"cache": true,
30+
"sourceMap": false,
31+
"instrument": false,
32+
"report-dir": "./tests-report",
33+
"reporter": [
34+
"text",
35+
"html"
36+
]
37+
},
38+
"publishConfig": {
39+
"registry": "https://registry.npmjs.org/"
40+
},
41+
"keywords": [
42+
"streams"
43+
],
44+
"author": "Juan Picado <juanpicado19@gmail.com>",
45+
"private": false,
46+
"license": "MIT"
47+
}

core/streams/src/index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const Stream = require('stream');
4+
5+
/**
6+
* This stream is used to read tarballs from repository.
7+
* @param {*} options
8+
* @return {Stream}
9+
*/
10+
class ReadTarball extends Stream.PassThrough {
11+
12+
/**
13+
*
14+
* @param {Object} options
15+
*/
16+
constructor(options) {
17+
super(options);
18+
// called when data is not needed anymore
19+
addAbstractMethods(this, 'abort');
20+
}
21+
}
22+
23+
/**
24+
* This stream is used to upload tarballs to a repository.
25+
* @param {*} options
26+
* @return {Stream}
27+
*/
28+
class UploadTarball extends Stream.PassThrough {
29+
30+
/**
31+
*
32+
* @param {Object} options
33+
*/
34+
constructor(options) {
35+
super(options);
36+
// called when user closes connection before upload finishes
37+
addAbstractMethods(this, 'abort');
38+
39+
// called when upload finishes successfully
40+
addAbstractMethods(this, 'done');
41+
}
42+
}
43+
44+
/**
45+
* This function intercepts abstract calls and replays them allowing.
46+
* us to attach those functions after we are ready to do so
47+
* @param {*} self
48+
* @param {*} name
49+
*/
50+
function addAbstractMethods(self, name) {
51+
self._called_methods = self._called_methods || {};
52+
self.__defineGetter__(name, function() {
53+
return function() {
54+
self._called_methods[name] = true;
55+
};
56+
});
57+
self.__defineSetter__(name, function(fn) {
58+
delete self[name];
59+
self[name] = fn;
60+
if (self._called_methods && self._called_methods[name]) {
61+
delete self._called_methods[name];
62+
self[name]();
63+
}
64+
});
65+
}
66+
67+
export {ReadTarball, UploadTarball};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
let ReadTarball = require('../src/index').ReadTarball;
4+
5+
describe('mystreams', function() {
6+
it('should delay events', function(cb) {
7+
let test = new ReadTarball();
8+
test.abort();
9+
setTimeout(function() {
10+
test.abort = function() {
11+
cb();
12+
};
13+
test.abort = function() {
14+
throw Error('fail');
15+
};
16+
}, 10);
17+
});
18+
});
19+

0 commit comments

Comments
 (0)