-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #485 from zazuko/sp-destroyable-transform
Fix DestroyableTransform issues
- Loading branch information
Showing
11 changed files
with
588 additions
and
327 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,5 @@ | ||
--- | ||
"@zazuko/trifid-plugin-sparql-proxy": patch | ||
--- | ||
|
||
Fix issues in case of DestroyableTransform |
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,5 @@ | ||
--- | ||
"trifid-handler-fetch": patch | ||
--- | ||
|
||
Upgrade Oxigraph to 0.4.0-rc.1. |
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Transform } from 'node:stream' | ||
|
||
class ReplaceStream extends Transform { | ||
constructor(searchStr, replaceStr, options = {}) { | ||
super(options) | ||
this.searchStr = searchStr | ||
this.replaceStr = replaceStr | ||
this.tailPiece = '' // Holds trailing data from previous chunk | ||
this.searchStrLen = searchStr.length | ||
} | ||
|
||
_transform (chunk, encoding, callback) { | ||
const data = this.tailPiece + chunk.toString() // Combine previous tail with new chunk | ||
let lastIndex = 0 | ||
let index | ||
|
||
const pieces = [] | ||
|
||
// Search for occurrences of searchStr | ||
while ((index = data.indexOf(this.searchStr, lastIndex)) !== -1) { | ||
pieces.push(data.slice(lastIndex, index)) // Push the data before the match | ||
pieces.push(this.replaceStr) // Push the replacement string | ||
lastIndex = index + this.searchStrLen // Move the index past the match | ||
} | ||
|
||
// Save the remaining data after the last match as tailPiece | ||
this.tailPiece = data.slice(lastIndex) | ||
|
||
// Push the processed data | ||
this.push(pieces.join('')) | ||
|
||
callback() | ||
} | ||
|
||
_flush (callback) { | ||
// Push out any remaining data in tailPiece, processing any matches in it | ||
this.push(this.tailPiece.replace(new RegExp(this.searchStr, 'g'), this.replaceStr)) | ||
callback() | ||
} | ||
} | ||
|
||
export default ReplaceStream |
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,137 @@ | ||
import { PassThrough } from 'node:stream' | ||
import { expect } from 'chai' | ||
import { describe, it } from 'mocha' | ||
import ReplaceStream from '../lib/ReplaceStream.js' | ||
|
||
describe('ReplaceStream', () => { | ||
it('should replace a single occurrence of a string', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
const receivedData = [] | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
receivedData.push(data) | ||
}) | ||
|
||
output.on('end', () => { | ||
expect(receivedData.join('')).to.equal('new string') | ||
done() | ||
}) | ||
|
||
input.write('old string') | ||
input.end() | ||
}) | ||
|
||
it('should replace multiple occurrences of a string', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
const receivedData = [] | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
receivedData.push(data) | ||
}) | ||
|
||
output.on('end', () => { | ||
expect(receivedData.join('')).to.equal('new string with new value') | ||
done() | ||
}) | ||
|
||
input.write('old string with old value') | ||
input.end() | ||
}) | ||
|
||
it('should replace strings that span across chunks', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
const receivedData = [] | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
receivedData.push(data) | ||
}) | ||
|
||
output.on('end', () => { | ||
expect(receivedData.join('')).to.equal('new and new') | ||
done() | ||
}) | ||
|
||
input.write('ol') | ||
input.write('d and ol') | ||
input.write('d') | ||
input.end() | ||
}) | ||
|
||
it('should handle no occurrences of the string', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
expect(data).to.equal('no match here') | ||
}) | ||
|
||
output.on('end', done) | ||
|
||
input.write('no match here') | ||
input.end() | ||
}) | ||
|
||
it('should handle empty input', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
expect(data).to.equal('') | ||
}) | ||
|
||
output.on('end', done) | ||
|
||
input.end() | ||
}) | ||
|
||
it('should handle large input with multiple replacements', (done) => { | ||
const replaceStream = new ReplaceStream('old', 'new') | ||
const input = new PassThrough() | ||
const output = new PassThrough() | ||
|
||
const largeInput = 'old '.repeat(1000) | ||
const expectedOutput = 'new '.repeat(1000) | ||
|
||
input.pipe(replaceStream).pipe(output) | ||
|
||
let result = '' | ||
output.setEncoding('utf8') | ||
output.on('data', (data) => { | ||
result += data | ||
}) | ||
|
||
output.on('end', () => { | ||
expect(result).to.equal(expectedOutput) | ||
done() | ||
}) | ||
|
||
input.write(largeInput) | ||
input.end() | ||
}) | ||
}) |