-
Notifications
You must be signed in to change notification settings - Fork 2
react-native: replace AlternatingFileWriter with WritableStream and ChunkifierSink for breadcrumbs #315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
react-native: replace AlternatingFileWriter with WritableStream and ChunkifierSink for breadcrumbs #315
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad1703c
react-native: add Stream
perf2711 0b785cc
react-native: replace AlternatingFileWriter with Chunkifier
perf2711 bb604a5
react-native: add Chunkifier tests
perf2711 7c49b28
react-native: make ChunkifierSink generic
perf2711 3ebf7c1
react-native: fix formatting issues
perf2711 ad9f004
react-native: import ponyfills in tests
perf2711 02d5fed
react-native: replace null assignments with ensure functions
perf2711 22d9778
react-native: rename variables in FileBreadcrumbsStorage
perf2711 fed7529
react-native: catch breadcrumb errors silently
perf2711 92a2439
react-native: add test case for chunkifier not calling splitter (NFC)
perf2711 414e2a9
react-native: add comments to ChunkifierSink.reset
perf2711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,13 @@ | ||
import crypto from 'crypto'; | ||
|
||
function getRandomSeed() { | ||
return crypto.randomBytes(16).toString('hex'); | ||
} | ||
|
||
export default function () { | ||
if (!process.env.TEST_SEED) { | ||
process.env.TEST_SEED = getRandomSeed(); | ||
} | ||
|
||
console.log(`\n=== Using random seed ${process.env.TEST_SEED} ===`); | ||
} |
This file contains hidden or 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
105 changes: 0 additions & 105 deletions
105
packages/react-native/src/breadcrumbs/AlternatingFileWriter.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,125 @@ | ||
export type ChunkSplitterFactory<W extends Chunk> = () => ChunkSplitter<W>; | ||
|
||
/** | ||
* Implementation of splitter should return either one or two `Buffer`s. | ||
* | ||
* The first `Buffer` will be written to the current chunk. | ||
* If the second `Buffer` is returned, `chunkifier` will create a new chunk and write the | ||
* second buffer to the new chunk. | ||
*/ | ||
export type ChunkSplitter<W extends Chunk> = (chunk: W) => [W, W?]; | ||
|
||
/** | ||
* Implementation of chunk sink should return each time a new writable stream. | ||
* | ||
* `n` determines which stream it is in sequence. | ||
*/ | ||
export type ChunkSink<W extends Chunk, S extends WritableStream<W> = WritableStream<W>> = (n: number) => S; | ||
|
||
export type Chunk = { readonly length: number }; | ||
|
||
export interface ChunkifierOptions<W extends Chunk> { | ||
/** | ||
* Chunk splitter factory. The factory will be called when creating a new chunk. | ||
*/ | ||
readonly splitter: ChunkSplitterFactory<W>; | ||
|
||
/** | ||
* Chunk sink. The sink will be called when creating a new chunk. | ||
*/ | ||
readonly sink: ChunkSink<W>; | ||
|
||
readonly allowEmptyChunks?: boolean; | ||
} | ||
|
||
interface StreamContext<W extends Chunk> { | ||
readonly stream: WritableStream<W>; | ||
readonly streamWriter: WritableStreamDefaultWriter<W>; | ||
isEmptyChunk: boolean; | ||
} | ||
|
||
export class ChunkifierSink<W extends Chunk> implements UnderlyingSink<W> { | ||
private _context?: StreamContext<W>; | ||
private _splitter?: ChunkSplitter<W>; | ||
private _chunkCount = 0; | ||
|
||
constructor(private readonly _options: ChunkifierOptions<W>) {} | ||
|
||
public async write(data: W): Promise<void> { | ||
// If data is empty from the start, forward the write directly to current stream | ||
if (this.isEmpty(data)) { | ||
return await this.ensureStreamContext().streamWriter.write(data); | ||
} | ||
|
||
while (data) { | ||
if (this.isEmpty(data)) { | ||
break; | ||
} | ||
|
||
const splitter = this.ensureSplitter(); | ||
const [currentChunk, nextChunk] = splitter(data); | ||
if (nextChunk === undefined) { | ||
const current = this.ensureStreamContext(); | ||
if (!this.isEmpty(currentChunk)) { | ||
current.isEmptyChunk = false; | ||
} | ||
|
||
return await current.streamWriter.write(currentChunk); | ||
} | ||
|
||
data = nextChunk; | ||
if ( | ||
this._context | ||
? this._context.isEmptyChunk | ||
: this.isEmpty(currentChunk) && !this._options.allowEmptyChunks | ||
) { | ||
continue; | ||
} | ||
|
||
const current = this.ensureStreamContext(); | ||
await current.streamWriter.write(currentChunk); | ||
current.streamWriter.releaseLock(); | ||
|
||
// On next loop iteration, or write, create new stream again | ||
this.reset(); | ||
} | ||
} | ||
|
||
public async close() { | ||
return await this._context?.streamWriter.close(); | ||
} | ||
|
||
private ensureStreamContext() { | ||
if (!this._context) { | ||
return (this._context = this.createStreamContext()); | ||
} | ||
return this._context; | ||
} | ||
|
||
private ensureSplitter() { | ||
if (!this._splitter) { | ||
return (this._splitter = this._options.splitter()); | ||
} | ||
return this._splitter; | ||
} | ||
|
||
private createStreamContext(): StreamContext<W> { | ||
const stream = this._options.sink(this._chunkCount++); | ||
const writer = stream.getWriter(); | ||
return { stream, streamWriter: writer, isEmptyChunk: true }; | ||
} | ||
|
||
/** | ||
* Resets the chunkifier to it's initial state. Use when switching streams. | ||
*/ | ||
private reset() { | ||
this._context = undefined; | ||
|
||
// Splitter may have an internal state which we need to recreate with new stream | ||
this._splitter = undefined; | ||
perf2711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private isEmpty(chunk: W) { | ||
return !chunk.length; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to run a write operation at all? Based on the stream writer implementation, we don't need to?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just that any write to this sink should end up in calling write on the underlying stream.