Updated body_parser

This commit is contained in:
thomashii 2021-06-21 09:27:19 +08:00
parent 3dda3cf844
commit 1a63e8f6bd
6 changed files with 74 additions and 50 deletions

View file

@ -44,6 +44,34 @@
* Migrated angel_auth_cache to 4.0.0 (0/7 tests passed) * Migrated angel_auth_cache to 4.0.0 (0/7 tests passed)
* Migrated angel_auth_cors to 4.0.0 (10/15 tests passed) * Migrated angel_auth_cors to 4.0.0 (10/15 tests passed)
* Migrated angel_oauth2 to 4.0.0 (17/25 tests passed) * Migrated angel_oauth2 to 4.0.0 (17/25 tests passed)
* Migrated angel_proxy to 4.0.0 (5/7 tests passed)
* Migrated angel_file_service to 4.0.0 (6/6 tests passed)
* Migrated graphql_parser to 2.0.0 (55/55 tests passed)
* Migrated graphql_schema to 2.0.0 (34/35 tests passed)
* Migrated graphql_server to 2.0.0 (9/10 tests passed)
* Migrated graphql_generator to 2.0.0 (0/0 tests passed)
* Migrated data_loader to 2.0.0 (7/7 tests passed)
* Migrated angel_graphql to 2.0.0 (0/0 tests passed)
* Migrated angel_mongo to 3.0.0 (0/0 tests passed)
* Migrated angel_orm_mysql to 2.0.0 (0/0 tests passed)
* Migrated angel_orm_service to 2.0.0 (0/0 tests passed)
* Migrated body_parser to 2.0.0 (11/11 tests passed)
* Migrated angel_markdown to 3.0.0 (0/0 tests passed)
* Migrated angel_jinja to 2.0.0 (0/0 tests passed)
* Migrated angel_html to 3.0.0 (1/3 tests passed)
* Migrated angel_mustache to 2.0.0 (3/3 tests passed)
* Migrated angel_paginate to 3.0.0 (18/18 tests passed)
* Migrated angel_poll to 2.0.0 (0/5 tests passed)
* Migrated angel_redis to 2.0.0 (0/8 tests passed)
* Migrated angel_seeder to 2.0.0 (0/0 tests passed)
* Migrated angel_relations to 2.0.0 (0/0 tests passed)
* Migrated angel_rethink to 2.0.0 (0/0 tests passed)
* Migrated angel_security to 2.0.0 (0/1 tests passed)
* Migrated angel_sembast to 2.0.0 (10/10 tests passed)
* Migrated angel_sync to 3.0.0 (0/1 tests passed)
* Migrated angel_typed_service to 3.0.0 (4/4 tests passed)
* Migrated angel_shelf to 2.0.0 (0/1 tests passed)
* Migrated angel_user_agent to 2.0.0 (0/0 tests passed)
# 3.0.0 (Non NNBD) # 3.0.0 (Non NNBD)
* Changed Dart SDK requirements for all packages to ">=2.10.0 <3.0.0" * Changed Dart SDK requirements for all packages to ">=2.10.0 <3.0.0"

View file

@ -1,8 +1,18 @@
# 2.0.0 # Change Log
## 2.0.1
* Fixed source code formating warning
* Updated README
## 2.0.0
* Migrated to support Dart SDK 2.12.x NNBD * Migrated to support Dart SDK 2.12.x NNBD
# 1.1.1 ## 1.1.1
* Dart 2 updates; should fix Angel in Travis. * Dart 2 updates; should fix Angel in Travis.
# 1.1.0 ## 1.1.0
* Add `parseBodyFromStream` * Add `parseBodyFromStream`

View file

@ -1,5 +1,6 @@
# Angel3 Body Parser # Angel3 Body Parser
[![version](https://img.shields.io/badge/pub-v2.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_body_parser)
[![version](https://img.shields.io/badge/pub-v2.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_body_parser)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![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) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
@ -11,35 +12,31 @@ Parse request bodies and query strings in Dart, as well multipart/form-data uplo
This is the request body parser powering the [Angel3](https://github.com/dukefirehawk/angel) framework. If you are looking for a server-side solution with dependency injection, WebSockets, and more, then I highly recommend it as your first choice. Bam! This is the request body parser powering the [Angel3](https://github.com/dukefirehawk/angel) framework. If you are looking for a server-side solution with dependency injection, WebSockets, and more, then I highly recommend it as your first choice. Bam!
### Contents ## Contents
* [Body Parser](#body-parser) - [Angel3 Body Parser](#angel3-body-parser)
* [About](#about) - [Contents](#contents)
* [Installation](#installation) - [About](#about)
* [Usage](#usage) - [Installation](#installation)
* [Thanks](#thank-you-for-using-body-parser) - [Usage](#usage)
- [Custom Body Parsing](#custom-body-parsing)
# About ### About
I needed something like Express.js's `body-parser` module, so I made it here. It fully supports JSON requests. I needed something like Express.js's `body-parser` module, so I made it here. It fully supports JSON requests. x-www-form-urlencoded fully supported, as well as query strings. You can also include arrays in your query, in the same way you would for a PHP application. Full file upload support will also be present by the production 1.0.0 release.
x-www-form-urlencoded fully supported, as well as query strings. You can also include arrays in your query,
in the same way you would for a PHP application. Full file upload support will also be present by the production 1.0.0 release.
A benefit of this is that primitive types are automatically deserialized correctly. As in, if you have a `hello=1.5` request, then A benefit of this is that primitive types are automatically deserialized correctly. As in, if you have a `hello=1.5` request, then `body['hello']` will equal `1.5` and not `'1.5'`. A very semantic difference, yes, but it relieves stress in my head.
`body['hello']` will equal `1.5` and not `'1.5'`. A very semantic difference, yes, but it relieves stress in my head.
# Installation ### Installation
To install Body Parser for your Dart project, simply add body_parser to your To install Body Parser for your Dart project, simply add body_parser to your pub dependencies.
pub dependencies.
dependencies: dependencies:
angel3_body_parser: ^2.0.0 angel3_body_parser: ^2.0.0
# Usage ### Usage
Body Parser exposes a simple class called `BodyParseResult`. Body Parser exposes a simple class called `BodyParseResult`. You can easily parse the query string and request body for a request by calling `Future<BodyParseResult> parseBody`.
You can easily parse the query string and request body for a request by calling `Future<BodyParseResult> parseBody`.
```dart ```dart
import 'dart:convert'; import 'dart:convert';
@ -64,10 +61,9 @@ MyClass create(HttpRequest request) async {
} }
``` ```
## Custom Body Parsing ### Custom Body Parsing
In cases where you need to parse unrecognized content types, `body_parser` won't be of any help to you
on its own. However, you can use the `originalBuffer` property of a `BodyParseResult` to see the original In cases where you need to parse unrecognized content types, `body_parser` won't be of any help to you on its own. However, you can use the `originalBuffer` property of a `BodyParseResult` to see the original request buffer. To get this functionality, pass `storeOriginalBuffer` as `true` when calling `parseBody`.
request buffer. To get this functionality, pass `storeOriginalBuffer` as `true` when calling `parseBody`.
For example, if you wanted to [parse GraphQL queries within your server](https://github.com/dukefirehawk/graphql_dart)... For example, if you wanted to [parse GraphQL queries within your server](https://github.com/dukefirehawk/graphql_dart)...

View file

@ -69,7 +69,8 @@ Future<BodyParseResult> parseBodyFromStream(
stream = data; stream = data;
} }
var parts = MimeMultipartTransformer(contentType.parameters['boundary']!) var parts =
MimeMultipartTransformer(contentType.parameters['boundary']!)
.bind(stream) .bind(stream)
.map((part) => .map((part) =>
HttpMultipartFormData.parse(part, defaultEncoding: utf8)); HttpMultipartFormData.parse(part, defaultEncoding: utf8));
@ -79,10 +80,8 @@ Future<BodyParseResult> parseBodyFromStream(
part.contentDisposition.parameters.containsKey('filename')) { part.contentDisposition.parameters.containsKey('filename')) {
var builder = await part.fold( var builder = await part.fold(
BytesBuilder(copy: false), BytesBuilder(copy: false),
(BytesBuilder b, d) => b (BytesBuilder b, d) =>
..add(d is! String b..add(d is! String ? (d as List<int>?)! : d.codeUnits));
? (d as List<int>?)!
: d.codeUnits));
var upload = FileUploadInfo( var upload = FileUploadInfo(
mimeType: part.contentType!.mimeType, mimeType: part.contentType!.mimeType,
name: part.contentDisposition.parameters['name'], name: part.contentDisposition.parameters['name'],
@ -97,8 +96,8 @@ Future<BodyParseResult> parseBodyFromStream(
} }
} }
} else if (contentType.mimeType == 'application/json') { } else if (contentType.mimeType == 'application/json') {
result.body result.body.addAll(
.addAll(_foldToStringDynamic(json.decode(await getBody()) as Map?)!); _foldToStringDynamic(json.decode(await getBody()) as Map?)!);
} else if (contentType.mimeType == 'application/x-www-form-urlencoded') { } else if (contentType.mimeType == 'application/x-www-form-urlencoded') {
var body = await getBody(); var body = await getBody();
buildMapFromUri(result.body, body); buildMapFromUri(result.body, body);

View file

@ -1,5 +1,5 @@
name: angel3_body_parser name: angel3_body_parser
version: 2.0.0 version: 2.0.1
description: Parse request bodies and query strings in Dart. Supports JSON, URL-encoded, and multi-part bodies. description: Parse request bodies and query strings in Dart. Supports JSON, URL-encoded, and multi-part bodies.
homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/body_parser homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/body_parser
environment: environment:

View file

@ -1,21 +1,12 @@
name: angel_redis name: angel3_redis
version: 2.0.0 version: 2.0.0
description: An Angel service provider for Redis. Works well for caching volatile data. description: An Angel service provider for Redis. Works well for caching volatile data.
homepage: https://github.com/angel-dart/redis homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/redis
publish_to: none
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
angel_framework: angel_framework:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/framework
angel_http_exception: angel_http_exception:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/http_exception
resp_client: ^1.2.0 resp_client: ^1.2.0
dev_dependencies: dev_dependencies:
test: ^1.17.8 test: ^1.17.8