respond method
Respond to a response.
Implementation
@override
Future<web.Response> respond(
Event event, web.Request request, Response response) async {
final init = web.ResponseInit(
status: response.status,
statusText: response.statusText,
headers: response.headers.toWebHeaders(),
);
if (response.body == null) {
return web.Response(null, init);
}
late final StreamSubscription<Uint8List> subscription;
final underlyingSource = web.UnderlyingSource(
type: "bytes",
start: (_ReadableByteStreamController controller) {
subscription = response.body!.listen(
(event) {
controller.enqueue(event.toJS);
if (controller.desiredSize == null ||
controller.desiredSize == -1 ||
controller.desiredSize == 0) {
subscription.pause();
}
},
onError: (error) {
final e = switch (error) {
String(toJS: final toJS) => toJS,
Exception exception => Error.safeToString(exception).toJS,
Error error => Error.safeToString(error).toJS,
JSAny any => any,
_ => error.jsify(),
};
controller.error(e);
},
onDone: () => controller.close(),
);
}.toJS,
pull: ((JSAny _) {
subscription.resume();
}).toJS,
cancel: (JSAny reason) {
subscription.cancel();
}.toJS,
);
final readableStream = web.ReadableStream(underlyingSource);
return web.Response(readableStream, init);
}