Skip to content

Commit

Permalink
fix: undoing fix #1
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
addievo committed Sep 15, 2023
1 parent 54e7d41 commit 5e65aef
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
10 changes: 4 additions & 6 deletions src/utils/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function binaryToJsonMessageStream<T extends JSONRPCMessage>(
let bytesWritten: number = 0;

return new TransformStream<Uint8Array, T>({
flush: async (controller) => {
flush: async () => {
// Avoid potential race conditions by allowing parser to end first
const waitP = promise();
parser.onEnd = () => waitP.resolveP();
parser.end();
Expand All @@ -41,14 +42,11 @@ function binaryToJsonMessageStream<T extends JSONRPCMessage>(
start: (controller) => {
parser.onValue = (value) => {
const jsonMessage = messageParser(value.value);
const jsonMessageString = JSON.stringify(jsonMessage);
const jsonMessageWithNewline = jsonMessageString + '\n';
const jsonMessageObject: T = JSON.parse(jsonMessageWithNewline) as T;
controller.enqueue(jsonMessageObject);
controller.enqueue(jsonMessage);
bytesWritten = 0;
};
},
transform: (chunk, controller) => {
transform: (chunk) => {
try {
bytesWritten += chunk.byteLength;
parser.write(chunk);
Expand Down
21 changes: 2 additions & 19 deletions tests/utils/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,19 @@ describe('Middleware tests', () => {
.noShrink();

testProp(
'can parse json stream and are human readable',
'can parse json stream',
[rpcTestUtils.jsonMessagesArb],
async (messages) => {
let captured = '';

const captureStream = new TransformStream<
JSONRPCMessage<JSONValue>,
JSONRPCMessage<JSONValue>
>({
transform(chunk, controller) {
captured += JSON.stringify(chunk) + '\n'; // Assuming chunk can be serialized to JSON
controller.enqueue(chunk);
},
});

const parsedStream = rpcTestUtils
.messagesToReadableStream(messages)
.pipeThrough(
rpcUtilsMiddleware.binaryToJsonMessageStream(
rpcUtils.parseJSONRPCMessage,
),
)
.pipeThrough(captureStream); // Converting back.
); // Converting back.

const asd = await AsyncIterable.as(parsedStream).toArray();
expect(asd).toEqual(messages);

// Check readibility

expect(captured).toContain('\n');
},
{ numRuns: 1000 },
);
Expand Down

0 comments on commit 5e65aef

Please sign in to comment.