-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
13 changed files
with
1,785 additions
and
71 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 @@ | ||
{ | ||
"presets": [["env", { "modules": false }]], | ||
"env": { | ||
"test": { | ||
"plugins": [ | ||
"transform-es2015-modules-commonjs" | ||
] | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
coverage/ | ||
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
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
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,38 @@ | ||
// WARNING ONLY NODE??? | ||
const { Readable } = require('stream'); | ||
const splitArrayIntoBatches = require('./splitArrayIntoBatches'); | ||
|
||
|
||
module.exports = (tasks, concurrency, interval = 0, failFast = true) => { | ||
const stream = new Readable({ | ||
objectMode: true, | ||
read() {}, | ||
}); | ||
|
||
let hasErrored = false; | ||
const error = (err) => { | ||
hasErrored = true; | ||
stream.emit('error', err); | ||
}; | ||
|
||
const push = chunk => (!hasErrored && stream.readable && stream.push(chunk)); | ||
|
||
const processBatches = (batches) => { | ||
if (!batches.length) { | ||
return push(null); | ||
} | ||
|
||
const wrap = fn => fn() | ||
.then(result => push(result)) | ||
.catch(err => (failFast ? error(err) : push(err))); | ||
|
||
return Promise.all(batches[0].map(wrap)).then(() => setTimeout( | ||
() => (!hasErrored && stream.readable && processBatches(batches.slice(1))), | ||
interval, | ||
)); | ||
}; | ||
|
||
processBatches(splitArrayIntoBatches(tasks, concurrency)); | ||
|
||
return stream; | ||
}; |
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,7 @@ | ||
module.exports = (arr, limit) => arr.reduce((memo, item) => { | ||
if (memo.length && memo[memo.length - 1].length < limit) { | ||
memo[memo.length - 1].push(item); | ||
return memo; | ||
} | ||
return [...memo, [item]]; | ||
}, []); |
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 |
---|---|---|
@@ -1,18 +1,25 @@ | ||
{ | ||
"name": "pact", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"main": "./index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"pretest": "eslint .", | ||
"test": "jest --verbose --coverage", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls" | ||
"test": "jest --coverage", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"build:basic": "webpack --define process.env.PACT_NO_STREAMS=true --mode production --entry ./index.js -o ./dist/pact.basic.min.js --output-library pact --output-library-target umd", | ||
"build:full": "webpack --mode production --entry ./index.js -o ./dist/pact.full.min.js --output-library pact --output-library-target umd", | ||
"build": "yarn build:basic && yarn build:full" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-preset-env": "^1.7.0", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.5.0", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"jest": "^23.6.0" | ||
"jest": "^23.6.0", | ||
"webpack": "^4.18.0", | ||
"webpack-cli": "^3.1.0" | ||
} | ||
} |
File renamed without changes.
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,17 @@ | ||
describe('pact (basic - no streams)', () => { | ||
beforeAll(() => { | ||
process.env.PACT_NO_STREAMS = true; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env.PACT_NO_STREAMS = undefined; | ||
// done(); | ||
}); | ||
|
||
it('should be a function and NOT have `createStream`', () => { | ||
// eslint-disable-next-line global-require | ||
const pact = require('../'); | ||
expect(typeof pact).toBe('function'); | ||
expect(pact.createStream).toBe(undefined); | ||
}); | ||
}); |
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
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,59 @@ | ||
const { Readable } = require('stream'); | ||
const { createDelayedPromise, createFailedDelayedPromise } = require('../testUtils'); | ||
const createStream = require('../../lib/createStream'); | ||
|
||
|
||
describe('createStream', () => { | ||
it('should be a function', () => { | ||
expect(typeof createStream).toBe('function'); | ||
}); | ||
|
||
it('should return a readable stream', () => { | ||
expect(createStream([]) instanceof Readable).toBe(true); | ||
}); | ||
|
||
it('should process tasks in series and emit results as data events', (done) => { | ||
const results = []; | ||
createStream([ | ||
() => createDelayedPromise('xxx', 20), | ||
() => createDelayedPromise('yyy', 10), | ||
]) | ||
.on('data', result => results.push(result)) | ||
.on('end', () => { | ||
expect(results).toEqual(['xxx', 'yyy']); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should failFast by default', (done) => { | ||
createStream([ | ||
() => createFailedDelayedPromise('xxx', 10), | ||
() => createDelayedPromise('yyy', 10), | ||
() => createFailedDelayedPromise('zzz', 10), | ||
]) | ||
.on('error', (err) => { | ||
expect(err instanceof Error).toBe(true); | ||
expect(err.message).toBe('Error: xxx'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not bail out when failFast is false', (done) => { | ||
const onData = jest.fn(); | ||
createStream([ | ||
() => createFailedDelayedPromise('xxx', 10), | ||
() => createDelayedPromise('yyy', 10), | ||
() => createFailedDelayedPromise('zzz', 10), | ||
], 1, 0, false) | ||
.on('data', onData) | ||
.on('end', () => { | ||
expect(onData.mock.calls.length).toBe(3); | ||
expect(onData.mock.calls[0].length).toBe(1); | ||
expect(onData.mock.calls[1].length).toBe(1); | ||
expect(onData.mock.calls[0][0] instanceof Error).toBe(true); | ||
expect(onData.mock.calls[1][0]).toBe('yyy'); | ||
expect(onData.mock.calls[2][0] instanceof Error).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
File renamed without changes.
Oops, something went wrong.