-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into index_pattern_class_refactor
- Loading branch information
Showing
316 changed files
with
6,444 additions
and
3,446 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
74 changes: 74 additions & 0 deletions
74
packages/kbn-es-archiver/src/lib/streams/concat_stream.test.js
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,74 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { createListStream, createPromiseFromStreams, createConcatStream } from './'; | ||
|
||
describe('concatStream', () => { | ||
test('accepts an initial value', async () => { | ||
const output = await createPromiseFromStreams([ | ||
createListStream([1, 2, 3]), | ||
createConcatStream([0]), | ||
]); | ||
|
||
expect(output).toEqual([0, 1, 2, 3]); | ||
}); | ||
|
||
describe(`combines using the previous value's concat method`, () => { | ||
test('works with strings', async () => { | ||
const output = await createPromiseFromStreams([ | ||
createListStream(['a', 'b', 'c']), | ||
createConcatStream(), | ||
]); | ||
expect(output).toEqual('abc'); | ||
}); | ||
|
||
test('works with arrays', async () => { | ||
const output = await createPromiseFromStreams([ | ||
createListStream([[1], [2, 3, 4], [10]]), | ||
createConcatStream(), | ||
]); | ||
expect(output).toEqual([1, 2, 3, 4, 10]); | ||
}); | ||
|
||
test('works with a mixture, starting with array', async () => { | ||
const output = await createPromiseFromStreams([ | ||
createListStream([[], 1, 2, 3, 4, [5, 6, 7]]), | ||
createConcatStream(), | ||
]); | ||
expect(output).toEqual([1, 2, 3, 4, 5, 6, 7]); | ||
}); | ||
|
||
test('fails when the value does not have a concat method', async () => { | ||
let promise; | ||
try { | ||
promise = createPromiseFromStreams([createListStream([1, '1']), createConcatStream()]); | ||
} catch (err) { | ||
throw new Error('createPromiseFromStreams() should not fail synchronously'); | ||
} | ||
|
||
try { | ||
await promise; | ||
throw new Error('Promise should have rejected'); | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(Error); | ||
expect(err.message).toContain('concat'); | ||
} | ||
}); | ||
}); | ||
}); |
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,41 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { createReduceStream } from './reduce_stream'; | ||
|
||
/** | ||
* Creates a Transform stream that consumes all provided | ||
* values and concatenates them using each values `concat` | ||
* method. | ||
* | ||
* Concatenate strings: | ||
* createListStream(['f', 'o', 'o']) | ||
* .pipe(createConcatStream()) | ||
* .on('data', console.log) | ||
* // logs "foo" | ||
* | ||
* Concatenate values into an array: | ||
* createListStream([1,2,3]) | ||
* .pipe(createConcatStream([])) | ||
* .on('data', console.log) | ||
* // logs "[1,2,3]" | ||
*/ | ||
export function createConcatStream(initial: any) { | ||
return createReduceStream((acc, chunk) => acc.concat(chunk), initial); | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/kbn-es-archiver/src/lib/streams/concat_stream_providers.test.js
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,66 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Readable } from 'stream'; | ||
|
||
import { concatStreamProviders } from './concat_stream_providers'; | ||
import { createListStream } from './list_stream'; | ||
import { createConcatStream } from './concat_stream'; | ||
import { createPromiseFromStreams } from './promise_from_streams'; | ||
|
||
describe('concatStreamProviders() helper', () => { | ||
test('writes the data from an array of stream providers into a destination stream in order', async () => { | ||
const results = await createPromiseFromStreams([ | ||
concatStreamProviders([ | ||
() => createListStream(['foo', 'bar']), | ||
() => createListStream(['baz']), | ||
() => createListStream(['bug']), | ||
]), | ||
createConcatStream(''), | ||
]); | ||
|
||
expect(results).toBe('foobarbazbug'); | ||
}); | ||
|
||
test('emits the errors from a sub-stream to the destination', async () => { | ||
const dest = concatStreamProviders([ | ||
() => createListStream(['foo', 'bar']), | ||
() => | ||
new Readable({ | ||
read() { | ||
this.emit('error', new Error('foo')); | ||
}, | ||
}), | ||
]); | ||
|
||
const errorListener = jest.fn(); | ||
dest.on('error', errorListener); | ||
|
||
await expect(createPromiseFromStreams([dest])).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"foo"` | ||
); | ||
expect(errorListener.mock.calls).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
[Error: foo], | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
packages/kbn-es-archiver/src/lib/streams/concat_stream_providers.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,60 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { PassThrough, TransformOptions } from 'stream'; | ||
|
||
/** | ||
* Write the data and errors from a list of stream providers | ||
* to a single stream in order. Stream providers are only | ||
* called right before they will be consumed, and only one | ||
* provider will be active at a time. | ||
*/ | ||
export function concatStreamProviders( | ||
sourceProviders: Array<() => NodeJS.ReadableStream>, | ||
options: TransformOptions = {} | ||
) { | ||
const destination = new PassThrough(options); | ||
const queue = sourceProviders.slice(); | ||
|
||
(function pipeNext() { | ||
const provider = queue.shift(); | ||
|
||
if (!provider) { | ||
return; | ||
} | ||
|
||
const source = provider(); | ||
const isLast = !queue.length; | ||
|
||
// if there are more sources to pipe, hook | ||
// into the source completion | ||
if (!isLast) { | ||
source.once('end', pipeNext); | ||
} | ||
|
||
source | ||
// proxy errors from the source to the destination | ||
.once('error', (error) => destination.emit('error', error)) | ||
// pipe the source to the destination but only proxy the | ||
// end event if this is the last source | ||
.pipe(destination, { end: isLast }); | ||
})(); | ||
|
||
return destination; | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/kbn-es-archiver/src/lib/streams/filter_stream.test.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,77 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { | ||
createConcatStream, | ||
createFilterStream, | ||
createListStream, | ||
createPromiseFromStreams, | ||
} from './'; | ||
|
||
describe('createFilterStream()', () => { | ||
test('calls the function with each item in the source stream', async () => { | ||
const filter = jest.fn().mockReturnValue(true); | ||
|
||
await createPromiseFromStreams([createListStream(['a', 'b', 'c']), createFilterStream(filter)]); | ||
|
||
expect(filter).toMatchInlineSnapshot(` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"a", | ||
], | ||
Array [ | ||
"b", | ||
], | ||
Array [ | ||
"c", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": true, | ||
}, | ||
Object { | ||
"type": "return", | ||
"value": true, | ||
}, | ||
Object { | ||
"type": "return", | ||
"value": true, | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
test('send the filtered values on the output stream', async () => { | ||
const result = await createPromiseFromStreams([ | ||
createListStream([1, 2, 3]), | ||
createFilterStream<number>((n) => n % 2 === 0), | ||
createConcatStream([]), | ||
]); | ||
|
||
expect(result).toMatchInlineSnapshot(` | ||
Array [ | ||
2, | ||
] | ||
`); | ||
}); | ||
}); |
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,29 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { concatStreamProviders } from './concat_stream_providers'; | ||
export { createIntersperseStream } from './intersperse_stream'; | ||
export { createSplitStream } from './split_stream'; | ||
export { createListStream } from './list_stream'; | ||
export { createReduceStream } from './reduce_stream'; | ||
export { createPromiseFromStreams } from './promise_from_streams'; | ||
export { createConcatStream } from './concat_stream'; | ||
export { createMapStream } from './map_stream'; | ||
export { createReplaceStream } from './replace_stream'; | ||
export { createFilterStream } from './filter_stream'; |
Oops, something went wrong.