Remove HijackCallback and OnHijackCallback (#82)
Closes #81
These have been deprecated, remove them completely.
Change type of OnHijackCallback to also use StreamChannel.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dc3c6cd..94efabe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,9 @@
* Give a return type to the `Handler` typedef. This may cause static warnings
where there previously were none, but all handlers should have already been
returning a `Response` or `Future<Response>`.
+* Remove `HijackCallback` and `OnHijackCallback` typedefs.
+* **Breaking**: Change type of `onHijack` in the `Request` constructor to take
+ an argument of `StreamChannel`.
## 0.6.8
diff --git a/lib/shelf_io.dart b/lib/shelf_io.dart
index 31ca95b..d091c76 100644
--- a/lib/shelf_io.dart
+++ b/lib/shelf_io.dart
@@ -21,6 +21,7 @@
import 'package:collection/collection.dart';
import 'package:http_parser/http_parser.dart';
import 'package:stack_trace/stack_trace.dart';
+import 'package:stream_channel/stream_channel.dart';
import 'shelf.dart';
import 'src/util.dart';
@@ -122,10 +123,10 @@
// Remove the Transfer-Encoding header per the adapter requirements.
headers.remove(HttpHeaders.TRANSFER_ENCODING);
- void onHijack(callback) {
+ void onHijack(void callback(StreamChannel<List<int>> channel)) {
request.response
.detachSocket(writeHeaders: false)
- .then((socket) => callback(socket, socket));
+ .then((socket) => callback(new StreamChannel(socket, socket)));
}
return new Request(request.method, request.requestedUri,
diff --git a/lib/src/request.dart b/lib/src/request.dart
index f208cf6..cbb2602 100644
--- a/lib/src/request.dart
+++ b/lib/src/request.dart
@@ -2,7 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-import 'dart:async';
import 'dart:convert';
import 'package:http_parser/http_parser.dart';
@@ -12,15 +11,9 @@
import 'message.dart';
import 'util.dart';
-/// A callback provided by a Shelf handler that's passed to [Request.hijack].
-@Deprecated("Will be removed in shelf 0.7.0.")
-typedef void HijackCallback(
- Stream<List<int>> stream, StreamSink<List<int>> sink);
-
/// A callback provided by a Shelf adapter that's used by [Request.hijack] to
/// provide a [HijackCallback] with a socket.
-@Deprecated("Will be removed in shelf 0.7.0.")
-typedef void OnHijackCallback(HijackCallback callback);
+typedef void _OnHijackCallback(void callback(StreamChannel<List<int>> channel));
/// Represents an HTTP request to be processed by a Shelf application.
class Request extends Message {
@@ -115,11 +108,9 @@
/// the bidirectional socket underlying the HTTP connection stream.
///
/// The [onHijack] callback will only be called once per request. It will be
- /// passed another callback which takes a byte stream and a byte sink.
- /// [onHijack] must pass the stream and sink for the connection stream to this
- /// callback, although it may do so asynchronously. Both parameters may be the
- /// same object. If the user closes the sink, the adapter should ensure that
- /// the stream is closed as well.
+ /// passed another callback which takes a byte StreamChannel. [onHijack] must
+ /// pass the channel for the connection stream to this callback, although it
+ /// may do so asynchronously.
///
/// If a request is hijacked, the adapter should expect to receive a
/// [HijackException] from the handler. This is a special exception used to
@@ -142,8 +133,7 @@
body,
Encoding encoding,
Map<String, Object> context,
- void onHijack(
- void hijack(Stream<List<int>> stream, StreamSink<List<int>> sink))})
+ void onHijack(void hijack(StreamChannel<List<int>> channel))})
: this._(method, requestedUri,
protocolVersion: protocolVersion,
headers: headers,
@@ -247,26 +237,16 @@
/// [callback] is called with a [StreamChannel<List<int>>] that provides
/// access to the underlying request socket.
///
- /// For backwards compatibility, if the callback takes two arguments, it will
- /// be passed a `Stream<List<int>>` and a `StreamSink<List<int>>` separately,
- /// but this behavior is deprecated.
- ///
/// This may only be called when using a Shelf adapter that supports
/// hijacking, such as the `dart:io` adapter. In addition, a given request may
/// only be hijacked once. [canHijack] can be used to detect whether this
/// request can be hijacked.
- void hijack(Function callback) {
+ void hijack(void callback(StreamChannel<List<int>> channel)) {
if (_onHijack == null) {
throw new StateError("This request can't be hijacked.");
}
- if (callback is HijackCallback) {
- _onHijack.run(callback);
- } else {
- _onHijack.run((Stream<List<int>> stream, StreamSink<List<int>> sink) {
- callback(new StreamChannel<List<int>>(stream, sink));
- });
- }
+ _onHijack.run(callback);
throw const HijackException();
}
@@ -276,7 +256,7 @@
/// the callback has been called.
class _OnHijack {
/// The callback.
- final OnHijackCallback _callback;
+ final _OnHijackCallback _callback;
/// Whether [this] has been called.
bool called = false;
@@ -286,7 +266,7 @@
/// Calls [this].
///
/// Throws a [StateError] if [this] has already been called.
- void run(HijackCallback callback) {
+ void run(void callback(StreamChannel<List<int>> channel)) {
if (called) throw new StateError("This request has already been hijacked.");
called = true;
newFuture(() => _callback(callback));
diff --git a/test/hijack_test.dart b/test/hijack_test.dart
index 47ae4ba..49815fb 100644
--- a/test/hijack_test.dart
+++ b/test/hijack_test.dart
@@ -4,8 +4,9 @@
import 'dart:async';
-import 'package:test/test.dart';
import 'package:shelf/shelf.dart';
+import 'package:stream_channel/stream_channel.dart';
+import 'package:test/test.dart';
import 'test_util.dart';
@@ -19,7 +20,7 @@
'hijacking a hijackable request throws a HijackException and calls '
'onHijack', () {
var request = new Request('GET', localhostUri,
- onHijack: expectAsync1((void callback(a, b)) {
+ onHijack: expectAsync1((void callback(channel)) {
var streamController = new StreamController<List<int>>();
streamController.add([1, 2, 3]);
streamController.close();
@@ -27,7 +28,7 @@
var sinkController = new StreamController();
expect(sinkController.stream.first, completion(equals([4, 5, 6])));
- callback(streamController.stream, sinkController);
+ callback(new StreamChannel(streamController.stream, sinkController));
}));
expect(
@@ -61,7 +62,7 @@
'hijacking a hijackable request throws a HijackException and calls '
'onHijack', () {
var request = new Request('GET', localhostUri,
- onHijack: expectAsync1((callback(a, b)) {
+ onHijack: expectAsync1((callback(channel)) {
var streamController = new StreamController<List<int>>();
streamController.add([1, 2, 3]);
streamController.close();
@@ -69,7 +70,7 @@
var sinkController = new StreamController();
expect(sinkController.stream.first, completion(equals([4, 5, 6])));
- callback(streamController.stream, sinkController);
+ callback(new StreamChannel(streamController.stream, sinkController));
}));
var newRequest = request.change();
diff --git a/test/shelf_io_test.dart b/test/shelf_io_test.dart
index 67baa04..5150873 100644
--- a/test/shelf_io_test.dart
+++ b/test/shelf_io_test.dart
@@ -214,16 +214,16 @@
_scheduleServer((request) {
expect(request.method, 'POST');
- request.hijack(expectAsync2((stream, sink) {
- expect(stream.first, completion(equals("Hello".codeUnits)));
+ request.hijack(expectAsync1((channel) {
+ expect(channel.stream.first, completion(equals("Hello".codeUnits)));
- sink.add(("HTTP/1.1 404 Not Found\r\n"
+ channel.sink.add(("HTTP/1.1 404 Not Found\r\n"
"Date: Mon, 23 May 2005 22:38:34 GMT\r\n"
"Content-Length: 13\r\n"
"\r\n"
"Hello, world!")
.codeUnits);
- sink.close();
+ channel.sink.close();
}));
});