Skip to content

Commit

Permalink
add StandardMessageCodec extracted from Flutter SDK (flutter#2434)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Aug 9, 2022
1 parent d82c88e commit ee8c80d
Show file tree
Hide file tree
Showing 12 changed files with 993 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/standard_message_codec/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
6 changes: 6 additions & 0 deletions packages/standard_message_codec/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Flutter project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google Inc.
3 changes: 3 additions & 0 deletions packages/standard_message_codec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.1

* Initial release of standard message codec extracted from the Flutter SDK.
25 changes: 25 additions & 0 deletions packages/standard_message_codec/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright 2013 The Flutter Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 changes: 52 additions & 0 deletions packages/standard_message_codec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
An efficient and schemaless binary format used by the Flutter SDK.

## Features

### Efficiency

The standard message codec is a binary format, as opposed to text based formats
like JSON. Consider the following snippet of JSON:

```json
{
"data": [1, 2, 3, 4],
}
```

In order for this message to be decoded into a Dart map, a utf8 binary file must
first be parsed and validated into a Dart string. Then a second pass is performed
which looks for specific characters that indicate JSON structures - for example
"{" and "}". No sizes or lengths are known ahead of time while, parsing, so the
resulting Dart list created for the "data" key is append to as decoding happens.

In contrast, decoding the standard message codec version of this message avoids
utf8 decoding, instead operating on the bytes themselves. The only string constructed
will be for the "data" key. The length of the list in the data field is encoded in
the structure, meaning the correct length object can be allocated and filled in
as decoding happens.

### Schemaless

Using standard message codec does not require a schema (like protobuf) or any
generated code. This makes it easy to use for dynamic messages and simplifies
the integration into existing codebases.

The tradeoff for this ease of use is that it becomes the application's
responsibility to verify the structure of messages sent/received. There is also
no automatic backwards compatibility like protobuf.

## Getting started

standard_message_codec can be used to encode and decode messages in either Flutter
or pure Dart applications.

<?code-excerpt "readme_excerpts.dart (Encoding)"?>
```dart
void main() {
final ByteData? data = const StandardMessageCodec().encodeMessage(<Object, Object>{
'foo': true,
3: 'fizz',
});
print('The encoded message is $data');
}
```
3 changes: 3 additions & 0 deletions packages/standard_message_codec/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example_standard_message_codec

A sample app for demonstrating the StandardMessageCodec
20 changes: 20 additions & 0 deletions packages/standard_message_codec/example/lib/readme_excerpts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file exists solely to host compiled excerpts for README.md, and is not
// intended for use as an actual example application.

// #docregion Encoding
import 'dart:typed_data';
import 'package:standard_message_codec/standard_message_codec.dart';
// #enddocregion Encoding

void main() {
final ByteData? data =
const StandardMessageCodec().encodeMessage(<Object, Object>{
'foo': true,
3: 'fizz',
});
print('The encoded message is $data');
}
11 changes: 11 additions & 0 deletions packages/standard_message_codec/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: standard_message_codec_examples
description: Example code for standard message codec usage
version: 0.0.1
publish_to: none

environment:
sdk: ">=2.17.0 <3.0.0"

dependencies:
standard_message_codec:
path: ../
Loading

0 comments on commit ee8c80d

Please sign in to comment.