Skip to content
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

SSE response #90

Merged
merged 4 commits into from
Jul 3, 2024
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
8 changes: 3 additions & 5 deletions lib/src/database/migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class Migration {
: '';
query.write(
'''DROP TABLE IF EXISTS `$name`; CREATE TABLE `$name` (${_queries.join(',')}$primary$index$foreig)''');

String sqlQuery = query.toString();
if (MigrationConnection().database?.driver == 'Postgresql') {
sqlQuery = _mysqlToPosgresqlMapper(sqlQuery);
Expand Down Expand Up @@ -762,17 +761,16 @@ class Migration {
void uuid(
String name, {
bool nullable = false,
String? defaultValue,
int length = 36,
String? comment,
String? collation,
String? expression,
String? virtuality,
}) {
addColumn(
char(
name,
'UUID',
nullable: nullable,
defaultValue: defaultValue,
length: length,
comment: comment,
collation: collation,
expression: expression,
Expand Down
35 changes: 35 additions & 0 deletions lib/src/http/response/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum ResponseType {
json,
none,
html,
sse,
streamFile,
download,
}
Expand All @@ -29,6 +30,25 @@ class Response {
this.headers = const {},
});

@protected
Future<void> sseHandler(HttpResponse res) async {
res.headers.contentType = ContentType.parse('text/event-stream');
res.headers.add(HttpHeaders.cacheControlHeader, 'no-cache');
res.headers.add(HttpHeaders.connectionHeader, 'keep-alive');
res.headers.add(HttpHeaders.transferEncodingHeader, 'chunked');

void writeSSE(String data) {
res.add(utf8.encode('data: $data\n\n'));
}

await for (var event in data) {
writeSSE(jsonEncode(event));
await res.flush();
}

await res.close();
}

void makeResponse(HttpResponse res) async {
res.statusCode = httpStatusCode;
if (headers.isNotEmpty) {
Expand All @@ -51,6 +71,9 @@ class Response {
res.write(data);
await res.close();
break;
case ResponseType.sse:
await sseHandler(res);
break;
case ResponseType.streamFile:
StreamFile? stream = StreamFile(
fileName: data['fileName'],
Expand Down Expand Up @@ -134,6 +157,18 @@ class Response {
headers: headers,
);

static sse(
Stream<dynamic> eventStream, {
int statusCode = HttpStatus.ok,
Map<String, String> headers = const {},
}) =>
Response(
data: eventStream,
responseType: ResponseType.sse,
httpStatusCode: statusCode,
headers: headers,
);

static download(
String fileName,
Uint8List bytes, {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/route/route_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ RouteData? _getMatchRoute(String inputRoute, String method, String? domain) {
.trim()
.replaceFirst(RegExp(r'^/'), '')
.replaceAll('//', '/')
.replaceAll(RegExp(r'/$'), '')
.replaceFirst(RegExp(r'/$'), '/');
inputRoute = inputRoute
.replaceFirst(RegExp(r'^/'), '')
.replaceAll('//', '/')
.replaceAll(RegExp(r'/$'), '')
.replaceFirst(RegExp(r'/$'), '');

if (route.prefix != null) {
Expand Down
Loading