Skip to content

Commit a0f6d87

Browse files
committed
remove large files streaming and update main changelog
1 parent 6120a03 commit a0f6d87

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

NpgsqlRestClient/AppStaticFileMiddleware.cs

+18-52
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public class AppStaticFileMiddleware
2525

2626
private static readonly ConcurrentDictionary<string, bool> _pathInParsePattern = new();
2727

28-
const long StreamingThreshold = 1024 * 1024 * 10; // 10MB
29-
const int MaxBufferSize = 8192; // 8KB
30-
3128
public static void ConfigureStaticFileMiddleware(
3229
bool parse,
3330
string[]? parsePatterns,
@@ -143,69 +140,38 @@ public async Task InvokeAsync(HttpContext context)
143140
customClaims: _customClaimTags,
144141
customParameters: null);
145142

146-
147-
if (fileInfo.Length < StreamingThreshold)
148-
{
149-
byte[] buffer = new byte[(int)fileInfo.Length];
150-
await fileStream.ReadExactlyAsync(buffer, context.RequestAborted);
151-
int charCount = Encoding.UTF8.GetCharCount(buffer);
152-
char[] chars = ArrayPool<char>.Shared.Rent(charCount);
153-
try
154-
{
155-
Encoding.UTF8.GetChars(buffer, 0, buffer.Length, chars, 0);
156-
ReadOnlySpan<char> result = parser.Parse(new ReadOnlySpan<char>(chars, 0, charCount), context);
157-
var writer = PipeWriter.Create(context.Response.Body);
158-
try
159-
{
160-
int maxBytesNeeded = Encoding.UTF8.GetMaxByteCount(result.Length);
161-
Memory<byte> memory = writer.GetMemory(maxBytesNeeded);
162-
int actualBytesWritten = Encoding.UTF8.GetBytes(result, memory.Span);
163-
writer.Advance(actualBytesWritten);
164-
context.Response.ContentLength = actualBytesWritten;
165-
await writer.FlushAsync(context.RequestAborted);
166-
}
167-
finally
168-
{
169-
await writer.CompleteAsync();
170-
}
171-
}
172-
finally
173-
{
174-
ArrayPool<char>.Shared.Return(chars);
175-
}
176-
}
177-
else
143+
byte[] buffer = new byte[(int)fileInfo.Length];
144+
await fileStream.ReadExactlyAsync(buffer, context.RequestAborted);
145+
int charCount = Encoding.UTF8.GetCharCount(buffer);
146+
char[] chars = ArrayPool<char>.Shared.Rent(charCount);
147+
try
178148
{
149+
Encoding.UTF8.GetChars(buffer, 0, buffer.Length, chars, 0);
150+
ReadOnlySpan<char> result = parser.Parse(new ReadOnlySpan<char>(chars, 0, charCount), context);
179151
var writer = PipeWriter.Create(context.Response.Body);
180152
try
181153
{
182-
using var reader = new StreamReader(fileStream, Encoding.UTF8, leaveOpen: true);
183-
char[] chars = ArrayPool<char>.Shared.Rent(MaxBufferSize);
184-
try
185-
{
186-
int charsRead;
187-
while ((charsRead = await reader.ReadAsync(chars, context.RequestAborted)) > 0)
188-
{
189-
var result = parser.Parse(chars.AsSpan(0, charsRead), context);
190-
int bytesWritten = Encoding.UTF8.GetBytes(result, writer.GetSpan(result.Length));
191-
writer.Advance(bytesWritten);
192-
await writer.FlushAsync(context.RequestAborted);
193-
}
194-
}
195-
finally
196-
{
197-
ArrayPool<char>.Shared.Return(chars);
198-
}
154+
int maxBytesNeeded = Encoding.UTF8.GetMaxByteCount(result.Length);
155+
Memory<byte> memory = writer.GetMemory(maxBytesNeeded);
156+
int actualBytesWritten = Encoding.UTF8.GetBytes(result, memory.Span);
157+
writer.Advance(actualBytesWritten);
158+
context.Response.ContentLength = actualBytesWritten;
159+
await writer.FlushAsync(context.RequestAborted);
199160
}
200161
finally
201162
{
202163
await writer.CompleteAsync();
203164
}
204165
}
166+
finally
167+
{
168+
ArrayPool<char>.Shared.Return(chars);
169+
}
205170
}
206171
catch (IOException ex)
207172
{
208173
_logger?.Error(ex, "Failed to serve static file {Path}", path.ToString());
174+
209175
context.Response.Clear();
210176
await _next(context);
211177
return;

changelog.md

+35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ Note: For a changelog for a client application [see the client application page
44

55
---
66

7+
## Version [2.22.0](https://github.com/vb-consulting/NpgsqlRest/tree/2.22.0 (2025-04-07)
8+
9+
[Full Changelog](https://github.com/vb-consulting/NpgsqlRest/compare/2.22.0...2.21.0)
10+
11+
## Improved Logging
12+
13+
Improved Logging for Endpoint creation to include routine and endpoint:
14+
15+
```console
16+
[14:56:17.477 INF] Function auth.login mapped to POST /api/auth/login has set ALLOW ANONYMOUS by the comment annotation. [NpgsqlRest]
17+
```
18+
19+
## Fixed CRUD Plugin
20+
21+
CRUD Endpoints are finally getting some love:
22+
- Fixed issue with connection as data source.
23+
- Added new tags: `onconflict`, `on_conflict` and `on-conflict` to generated endpoints handling with on conflict resoluions.
24+
25+
Now it's possible to enable or disable those endpoints explicitly with comment annotations:
26+
27+
```sql
28+
create table test.crud_table (
29+
id bigint generated always as identity primary key,
30+
name text not null,
31+
description text,
32+
created_at timestamp default now()
33+
);
34+
35+
comment on table test.crud_table is '
36+
HTTP
37+
for on-conflict
38+
disabled
39+
';
40+
```
41+
742
## Version [2.21.0](https://github.com/vb-consulting/NpgsqlRest/tree/2.21.0 (2025-03-24)
843

944
[Full Changelog](https://github.com/vb-consulting/NpgsqlRest/compare/2.21.0...2.20.0)

client.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ Example: override Auth:CookieName config npgsqlrest --auth:cookiename=Test
9191

9292
## Changelog
9393

94-
## 2.16.0
94+
## 2.17.0
95+
9596

9697

98+
## 2.16.0
99+
97100
### New option `ConnectionSettings.LogConnectionNoticeEventsMode`
98101

99102
```json

0 commit comments

Comments
 (0)