forked from trufflesecurity/trufflehog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run chunker in pipeline (trufflesecurity#859)
* Run chunker in pipeline * Move ChunkSize and PeekSize to source package. * Use new Chunk and Peek size location
- Loading branch information
Showing
7 changed files
with
150 additions
and
128 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
package sources | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"io" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
// ChunkSize is the maximum size of a chunk. | ||
ChunkSize = 10 * 1024 | ||
// PeekSize is the size of the peek into the previous chunk. | ||
PeekSize = 3 * 1024 | ||
) | ||
|
||
// Chunker takes a chunk and splits it into chunks of ChunkSize. | ||
func Chunker(originalChunk *Chunk) chan *Chunk { | ||
chunkChan := make(chan *Chunk) | ||
go func() { | ||
defer close(chunkChan) | ||
if len(originalChunk.Data) <= ChunkSize+PeekSize { | ||
chunkChan <- originalChunk | ||
return | ||
} | ||
r := bytes.NewReader(originalChunk.Data) | ||
reader := bufio.NewReaderSize(bufio.NewReader(r), ChunkSize) | ||
for { | ||
chunkBytes := make([]byte, ChunkSize) | ||
chunk := *originalChunk | ||
n, err := reader.Read(chunkBytes) | ||
if err != nil && !errors.Is(err, io.EOF) { | ||
logrus.WithError(err).Error("Error chunking reader.") | ||
break | ||
} | ||
peekData, _ := reader.Peek(PeekSize) | ||
chunk.Data = append(chunkBytes[:n], peekData...) | ||
chunkChan <- &chunk | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
} | ||
}() | ||
return chunkChan | ||
} |
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
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