Skip to content

Fix stream return value getting disposed #73

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 3 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ This project uses [semantic versioning](http://semver.org/spec/v2.0.0.html). Ref
*[Semantic Versioning in Practice](https://www.jering.tech/articles/semantic-versioning-in-practice)*
for an overview of semantic versioning.

## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/5.3.1...HEAD)
## [Unreleased](https://github.com/JeringTech/Javascript.NodeJS/compare/5.3.2...HEAD)

## [5.3.2](https://github.com/JeringTech/Javascript.NodeJS/compare/5.3.1...5.3.2) - Feb 22, 2020
### Fixes
- `HttpNodeJSService` no longer disposes `Stream`s before returning them. ([#73](https://github.com/JeringTech/Javascript.NodeJS/pull/73)).

## [5.3.1](https://github.com/JeringTech/Javascript.NodeJS/compare/5.3.0...5.3.1) - Feb 12, 2020
### Fixes
Expand Down
2 changes: 1 addition & 1 deletion perf/NodeJS/Jering.Javascript.NodeJS.Performance.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="Microsoft.AspNetCore.NodeServices" Version="3.0.0" />
<PackageReference Include="Yarn.MSBuild" Version="1.16.0" />
<PackageReference Include="Yarn.MSBuild" Version="1.22.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions perf/NodeJS/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
},
"Yarn.MSBuild": {
"type": "Direct",
"requested": "[1.16.0, )",
"resolved": "1.16.0",
"contentHash": "JzrCeiEMGjkw+z4JvOeZMWmhJF7YI7KDPXglg7PRtFqqBarLFiwGXsMPmiJQ+tXxFNfdBX8KmfDX0KT8htZjZw=="
"requested": "[1.22.0, )",
"resolved": "1.22.0",
"contentHash": "+uECXuuZ+8kjNR8bF6Y8wyKiDbrI/MSX9VTuE0q1GcRm13vU7IZl4WUfXSb/3aHmxsDsWGZxktfEnwe2YWgugg=="
},
"BenchmarkDotNet.Annotations": {
"type": "Transitive",
Expand Down
6 changes: 3 additions & 3 deletions src/NodeJS/Jering.Javascript.NodeJS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<PackageReference Include="Roslynator.CodeFixes" Version="2.0.0" PrivateAssets="all" ExcludeAssets="Runtime">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Yarn.MSBuild" Version="1.16.0" PrivateAssets="all" ExcludeAssets="Runtime">
<PackageReference Include="Yarn.MSBuild" Version="1.22.0" PrivateAssets="all" ExcludeAssets="Runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="4.6.0" />
Expand Down Expand Up @@ -86,11 +86,11 @@
So we must specify both. This way if we are multi-targeting, JavascriptBuild runs once, before DispatchToInnerBuilds after which inputs == outputs.
If we aren't multi-targeting, JavascriptBuild runs before PreBuildEvent.
-->
<Target Name="JavascriptBuildNonWindows" BeforeTargets="PreBuildEvent" Inputs="@(JavascriptInputs)" Outputs="@(JavascriptOutputs)" Condition="'$(OS)' != 'Windows_NT'">
<Target Name="JavascriptBuildNonWindows" BeforeTargets="PreBuildEvent" Inputs="@(JavascriptInputs)" Outputs="@(JavascriptOutputs)" Condition="'$(OS)' != 'Windows_NT'">
<Yarn WorkingDirectory=".\Javascript" Command="run build --env.mode=$(Configuration) --env.entry=.\Servers\OutOfProcess\Http\HttpServer.ts" />
</Target>

<Target Name="JavascriptBuildWindows" BeforeTargets="DispatchToInnerBuilds" Inputs="@(JavascriptInputs)" Outputs="@(JavascriptOutputs)" Condition="'$(OS)' == 'Windows_NT'">
<Target Name="JavascriptBuildWindows" BeforeTargets="DispatchToInnerBuilds" Inputs="@(JavascriptInputs)" Outputs="@(JavascriptOutputs)" Condition="'$(OS)' == 'Windows_NT'">
<Yarn WorkingDirectory=".\Javascript" Command="run build --env.mode=$(Configuration) --env.entry=.\Servers\OutOfProcess\Http\HttpServer.ts" />
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ public HttpNodeJSService(IOptions<OutOfProcessNodeJSServiceOptions> outOfProcess
return (true, (T)(object)result);
}

using (Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
if (typeof(T) == typeof(Stream))
{
if (typeof(T) == typeof(Stream))
{
return (true, (T)(object)stream);
}
Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
return (true, (T)(object)stream); // User's reponsibility to handle disposal
}

using (Stream stream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
T result = await _jsonService.DeserializeAsync<T>(stream, cancellationToken).ConfigureAwait(false);

return (true, result);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/NodeJS/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@
},
"Yarn.MSBuild": {
"type": "Direct",
"requested": "[1.16.0, )",
"resolved": "1.16.0",
"contentHash": "JzrCeiEMGjkw+z4JvOeZMWmhJF7YI7KDPXglg7PRtFqqBarLFiwGXsMPmiJQ+tXxFNfdBX8KmfDX0KT8htZjZw=="
"requested": "[1.22.0, )",
"resolved": "1.22.0",
"contentHash": "+uECXuuZ+8kjNR8bF6Y8wyKiDbrI/MSX9VTuE0q1GcRm13vU7IZl4WUfXSb/3aHmxsDsWGZxktfEnwe2YWgugg=="
},
"Microsoft.AspNetCore.Hosting.Server.Abstractions": {
"type": "Transitive",
Expand Down Expand Up @@ -640,9 +640,9 @@
},
"Yarn.MSBuild": {
"type": "Direct",
"requested": "[1.16.0, )",
"resolved": "1.16.0",
"contentHash": "JzrCeiEMGjkw+z4JvOeZMWmhJF7YI7KDPXglg7PRtFqqBarLFiwGXsMPmiJQ+tXxFNfdBX8KmfDX0KT8htZjZw=="
"requested": "[1.22.0, )",
"resolved": "1.22.0",
"contentHash": "+uECXuuZ+8kjNR8bF6Y8wyKiDbrI/MSX9VTuE0q1GcRm13vU7IZl4WUfXSb/3aHmxsDsWGZxktfEnwe2YWgugg=="
},
"Microsoft.AspNetCore.Hosting.Server.Abstractions": {
"type": "Transitive",
Expand Down
25 changes: 25 additions & 0 deletions test/NodeJS/HttpNodeJSServiceIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,31 @@ public async void AllInvokeMethods_HandleHttpClientErrorsSuchAsMalformedRequests
}
}

[Fact]
public async void AllInvokeMethodsThatReturnAValue_HandleStreamReturnType()
{
// Arrange
const string dummyData = "dummyData";
string dummyModule = $@"var Readable = require('stream').Readable;
module.exports = (callback) => {{
var stream = new Readable();
stream.push('{dummyData}');
stream.push(null);

callback(null, stream);
}}";
HttpNodeJSService testSubject = CreateHttpNodeJSService();

// Act
using (Stream resultStream = await testSubject.InvokeFromStringAsync<Stream>(dummyModule).ConfigureAwait(false))
using (var streamReader = new StreamReader(resultStream))
{
// Assert
string result = streamReader.ReadToEnd();
Assert.Equal(dummyData, result);
}
}

/// <summary>
/// Specify <paramref name="loggerStringBuilder"/> for access to all logging output.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion test/NodeJS/Jering.Javascript.NodeJS.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Yarn.MSBuild" Version="1.16.0" />
<PackageReference Include="Yarn.MSBuild" Version="1.22.0" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions test/NodeJS/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@
},
"Yarn.MSBuild": {
"type": "Direct",
"requested": "[1.16.0, )",
"resolved": "1.16.0",
"contentHash": "JzrCeiEMGjkw+z4JvOeZMWmhJF7YI7KDPXglg7PRtFqqBarLFiwGXsMPmiJQ+tXxFNfdBX8KmfDX0KT8htZjZw=="
"requested": "[1.22.0, )",
"resolved": "1.22.0",
"contentHash": "+uECXuuZ+8kjNR8bF6Y8wyKiDbrI/MSX9VTuE0q1GcRm13vU7IZl4WUfXSb/3aHmxsDsWGZxktfEnwe2YWgugg=="
},
"Castle.Core": {
"type": "Transitive",
Expand Down Expand Up @@ -1023,9 +1023,9 @@
},
"Yarn.MSBuild": {
"type": "Direct",
"requested": "[1.16.0, )",
"resolved": "1.16.0",
"contentHash": "JzrCeiEMGjkw+z4JvOeZMWmhJF7YI7KDPXglg7PRtFqqBarLFiwGXsMPmiJQ+tXxFNfdBX8KmfDX0KT8htZjZw=="
"requested": "[1.22.0, )",
"resolved": "1.22.0",
"contentHash": "+uECXuuZ+8kjNR8bF6Y8wyKiDbrI/MSX9VTuE0q1GcRm13vU7IZl4WUfXSb/3aHmxsDsWGZxktfEnwe2YWgugg=="
},
"Castle.Core": {
"type": "Transitive",
Expand Down