Repository: nteract/nteract (language: python)

Issue:
outputToJS is expensive when there is a large portion of "stream" output
**Application or Package Used**
@nteract/core@15.1.4

**Describe the bug**
Saving a notebook with a large data payload of "stream" output takes a long time.

**To Reproduce**
Steps to reproduce the behavior:
load a notebook with a display output contains a large data payload of a stream output
save the notebook
Then the UI will be frozen for like 10~ seconds

**Expected behavior**
This is due to calling `remultiline()` at line 
https://github.com/nteract/nteract/blob/08221c3064a6c71b1af5f424d4a440ac490f4de2/packages/commutable/src/v4.ts#L235
in `outputToJS` when we are serializing notebook content.

It is similar to issue #5145 which was about mediaBundle, I think we can skip applying `remultiline` for the "stream" output as well.




Discussion / hints:

Fix patch:
diff --git a/changelogs/current_changelog.md b/changelogs/current_changelog.md
index 52014b361e..5c314c0bec 100644
--- a/changelogs/current_changelog.md
+++ b/changelogs/current_changelog.md
@@ -29,6 +29,7 @@ Provide a bulleted list of bug fixes and a reference to the PR(s) containing the
 
 - Fixed mythic-configuration, allowing it to be imported in a browser setting ([Issue #5445](https://github.com/nteract/nteract/issues/5445))
 - Upgraded react-syntax-highlighter v^12.0.0 -> v^13.0.0 ([PR#5523](https://github.com/nteract/nteract/pull/5523))
+- Fixed outputToJS, no reformatting text in serializing stream output ([#5596](https://github.com/nteract/nteract/pull/5596))
 
 ## Core SDK Packages
 
diff --git a/packages/commutable/src/v4.ts b/packages/commutable/src/v4.ts
index b3f92b63dc..5048d04267 100644
--- a/packages/commutable/src/v4.ts
+++ b/packages/commutable/src/v4.ts
@@ -232,7 +232,10 @@ export function outputToJS(output: ImmutableOutput): OnDiskOutput {
       return {
         output_type: output.output_type,
         name: output.name,
-        text: remultiline(output.text)
+        // Note: similar to createOnDiskMediaBundle, steam output could be very big e.g. stdout dumps a large chunk of text
+        //       calling remultiline on that will cause performance issue.
+        // TODO: figure out a configurable way to reformat the string when needed.
+        text: output.text
       };
     case "error":
       return {


Test patch:
diff --git a/packages/commutable/__tests__/v4.spec.ts b/packages/commutable/__tests__/v4.spec.ts
index 59a1b5a5e1..5dc4c2aa37 100644
--- a/packages/commutable/__tests__/v4.spec.ts
+++ b/packages/commutable/__tests__/v4.spec.ts
@@ -4,7 +4,8 @@ import {
   makeDisplayData,
   makeErrorOutput,
   makeExecuteResult,
-  makeStreamOutput
+  makeStreamOutput,
+  OnDiskStreamOutput
 } from "../src/outputs";
 import {
   createCodeCell,
@@ -56,6 +57,13 @@ describe("outputToJS", () => {
     expect(outputToJS(streamOutput).output_type).toEqual("stream");
     expect(outputToJS(errorOutput).output_type).toEqual("error");
   });
+
+  it("stream output does not reformat multiline string", () => {
+    const text = "line1\r\nline2\rline3\nline4\n\n\r\n\r\r\n";
+    const streamOutput = makeStreamOutput().set("text", text)
+    const outputJS = outputToJS(streamOutput) as OnDiskStreamOutput;
+    expect(outputJS.text).toEqual(text);
+  });
 });
 
 describe("cell ids", () => {