Publish range_header

This commit is contained in:
thomashii 2021-05-15 19:58:09 +08:00
parent 4603bfcc69
commit 3f55c1df67
6 changed files with 25 additions and 22 deletions

View file

@ -1,7 +1,9 @@
# range_header # angel3_range_header
[![version](https://img.shields.io/badge/pub-v3.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_range_header)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
[![Pub](https://img.shields.io/pub/v/range_header.svg)](https://pub.dartlang.org/packages/range_header) [![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/range_header/LICENSE)
[![build status](https://travis-ci.org/thosakwe/range_header.svg)](https://travis-ci.org/thosakwe/range_header)
Range header parser for Dart. Range header parser for Dart.
@ -10,7 +12,7 @@ In your `pubspec.yaml`:
```yaml ```yaml
dependencies: dependencies:
range_header: ^2.0.0 angel3_range_header: ^3.0.0
``` ```
# Usage # Usage
@ -18,11 +20,11 @@ dependencies:
```dart ```dart
handleRequest(HttpRequest request) async { handleRequest(HttpRequest request) async {
// Parse the header // Parse the header
var header = new RangeHeader.parse(request.headers.value(HttpHeaders.rangeHeader)); var header = RangeHeader.parse(request.headers.value(HttpHeaders.rangeHeader));
// Optimize/canonicalize it // Optimize/canonicalize it
var items = RangeHeader.foldItems(header.items); var items = RangeHeader.foldItems(header.items);
header = new RangeHeader(items); header = RangeHeader(items);
// Get info // Get info
header.items; header.items;
@ -30,7 +32,7 @@ handleRequest(HttpRequest request) async {
print(header.items[0].toContentRange(fileSize)); print(header.items[0].toContentRange(fileSize));
// Serve the file // Serve the file
var transformer = new RangeHeaderTransformer(header); var transformer = RangeHeaderTransformer(header);
await file.openRead().transform(transformer).pipe(request.response); await file.openRead().transform(transformer).pipe(request.response);
} }
``` ```

View file

@ -1,16 +1,16 @@
import 'dart:io'; import 'dart:io';
import 'package:range_header/range_header.dart'; import 'package:angel3_range_header/angel3_range_header.dart';
var file = new File('some_video.mp4'); var file = File('some_video.mp4');
handleRequest(HttpRequest request) async { handleRequest(HttpRequest request) async {
// Parse the header // Parse the header
var header = var header =
new RangeHeader.parse(request.headers.value(HttpHeaders.rangeHeader)!); RangeHeader.parse(request.headers.value(HttpHeaders.rangeHeader)!);
// Optimize/canonicalize it // Optimize/canonicalize it
var items = RangeHeader.foldItems(header.items); var items = RangeHeader.foldItems(header.items);
header = new RangeHeader(items); header = RangeHeader(items);
// Get info // Get info
header.items; header.items;
@ -19,7 +19,7 @@ handleRequest(HttpRequest request) async {
// Serve the file // Serve the file
var transformer = var transformer =
new RangeHeaderTransformer(header, 'video/mp4', await file.length()); RangeHeaderTransformer(header, 'video/mp4', await file.length());
await file await file
.openRead() .openRead()
.cast<List<int>>() .cast<List<int>>()

View file

@ -1,5 +1,6 @@
import 'dart:math'; import 'dart:math';
import 'package:quiver_hashcode/hashcode.dart';
import 'package:quiver/core.dart';
/// Represents an individual range, with an optional start index and optional end index. /// Represents an individual range, with an optional start index and optional end index.
class RangeHeaderItem implements Comparable<RangeHeaderItem> { class RangeHeaderItem implements Comparable<RangeHeaderItem> {
@ -14,8 +15,8 @@ class RangeHeaderItem implements Comparable<RangeHeaderItem> {
/// Joins two items together into the largest possible range. /// Joins two items together into the largest possible range.
RangeHeaderItem consolidate(RangeHeaderItem other) { RangeHeaderItem consolidate(RangeHeaderItem other) {
if (!(other.overlaps(this))) if (!(other.overlaps(this)))
throw new ArgumentError('The two ranges do not overlap.'); throw ArgumentError('The two ranges do not overlap.');
return new RangeHeaderItem(min(start, other.start), max(end, other.end)); return RangeHeaderItem(min(start, other.start), max(end, other.end));
} }
@override @override
@ -77,7 +78,7 @@ class RangeHeaderItem implements Comparable<RangeHeaderItem> {
if (end == -1) { if (end == -1) {
if (totalSize == null) { if (totalSize == null) {
throw new UnsupportedError( throw UnsupportedError(
'If the end of this range is unknown, `totalSize` must not be null.'); 'If the end of this range is unknown, `totalSize` must not be null.');
} else { } else {
// if (end == totalSize - 1) { // if (end == totalSize - 1) {

View file

@ -1,13 +1,13 @@
name: range_header name: angel3_range_header
version: 3.0.0 version: 3.0.0
description: Range header parser for Dart. Beyond parsing, a stream transformer is included. description: Range header parser for Dart. Beyond parsing, a stream transformer is included.
homepage: https://github.com/thosakwe/range_header homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/range_header
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
async: ^2.6.0 async: ^2.6.0
charcode: ^1.2.0 charcode: ^1.2.0
quiver_hashcode: ^3.0.0+1 quiver: ^3.0.1
source_span: ^1.8.1 source_span: ^1.8.1
string_scanner: ^1.1.0 string_scanner: ^1.1.0
dev_dependencies: dev_dependencies:
@ -16,4 +16,4 @@ dev_dependencies:
file: ^6.1.0 file: ^6.1.0
http_parser: ^4.0.0 http_parser: ^4.0.0
logging: ^1.0.1 logging: ^1.0.1
test: ^1.17.3 test: ^1.17.4

View file

@ -1,6 +1,6 @@
import 'package:test/test.dart'; import 'package:test/test.dart';
import '../lib/range_header.dart'; import '../lib/angel3_range_header.dart';
import '../lib/src/range_header.dart'; import '../lib/src/range_header.dart';
final Matcher throwsRangeParseException = final Matcher throwsRangeParseException =
@ -9,7 +9,7 @@ final Matcher throwsRangeParseException =
final Matcher throwsInvalidRangeHeaderException = final Matcher throwsInvalidRangeHeaderException =
throwsA(const TypeMatcher<InvalidRangeHeaderException>()); throwsA(const TypeMatcher<InvalidRangeHeaderException>());
main() { void main() {
group('one item', () { group('one item', () {
test('start and end', () { test('start and end', () {
var r = RangeHeader.parse('bytes 1-200'); var r = RangeHeader.parse('bytes 1-200');