Skip to content

Commit

Permalink
1. Fix #12 Sending buffer data is not working!
Browse files Browse the repository at this point in the history
2. Bump 3.0.0
  • Loading branch information
jumperchen committed Sep 29, 2024
1 parent 89f29a0 commit 30a9784
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.0.0

**Bug fix:**
* [socket_io_common#12](https://github.com/rikulo/socket_io_common/pull/12) Sending buffer data is not working!


## 3.0.0-beta.0

**Features:**
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Socket.io common parser library for Dart 2
`v3.*` | `v4.7.* ~ v4.*`

## Contributors
- Thanks [@Curvel](https://github.com/Curvel) for https://github.com/rikulo/socket_io_common/pull/4
- Thanks [@busslina](https://github.com/busslina) for https://github.com/rikulo/socket_io_common/pull/5
- Thanks [@claudius-kienle](https://github.com/claudius-kienle) for https://github.com/rikulo/socket_io_common/pull/8

<a href="https://github.com/rikulo/socket_io_common/graphs/contributors">
<img src="https://contrib.rocks/image?repo=rikulo/socket_io_common" />
</a>
3 changes: 1 addition & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ linter:
rules:
- cancel_subscriptions
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- collection_methods_unrelated_type
- test_types_in_equals
- unrelated_type_equality_checks
- valid_regexps
4 changes: 2 additions & 2 deletions lib/src/engine/parser/commons.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2024 Potix Corporation. All Rights Reserved
// Copyright (C) 2024 Potix Corporation. All Rights Reserved
// History: 2024/2/13 1:50 PM
// Author: jumperchen

Expand Down Expand Up @@ -29,4 +29,4 @@ Map<String, String> PACKET_TYPES_REVERSE = {
for (var entry in PacketTypeMap.entries) '${entry.value}': entry.key,
};

const ERROR_PACKET = const {'type': 'error', 'data': 'parser error'};
const ERROR_PACKET = const {'type': 'error', 'data': 'parser error'};
2 changes: 1 addition & 1 deletion lib/src/engine/parser/decodePacket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ decodePacket(dynamic encodedPacket, binaryType) {
} else {
return {'type': PACKET_TYPES_REVERSE[type]};
}
}
}
8 changes: 5 additions & 3 deletions lib/src/engine/parser/encodePacket.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2024 Potix Corporation. All Rights Reserved
// Copyright (C) 2024 Potix Corporation. All Rights Reserved
// History: 2024/2/13 1:46 PM
// Author: jumperchen
import 'dart:convert';
Expand All @@ -8,7 +8,9 @@ import 'commons.dart';

encodePacket(Packet packet, bool supportsBinary, callback(_)) {
if (packet.data is ByteBuffer || packet.data is Uint8List) {
return callback(supportsBinary ? packet.data : 'b' + base64Encode(toBuffer(packet.data, true)));
return callback(supportsBinary
? packet.data
: 'b' + base64Encode(toBuffer(packet.data, true)));
} else {
// plain string
return callback('${PacketTypeMap[packet.type]}' + (packet.data ?? ''));
Expand Down Expand Up @@ -49,4 +51,4 @@ class TextEncoder {
Uint8List encode(String input) {
return Uint8List.fromList(utf8.encode(input));
}
}
}
38 changes: 25 additions & 13 deletions lib/src/parser/is_binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,48 @@ bool isBinary(obj) {
}

bool hasBinary(obj, [bool toJSON = false]) {
if (obj == null || (obj is! Map && obj is! List)) {
if (obj == null) {
return false;
}
if (obj is List && obj is! ByteBuffer && obj is! Uint8List) {
for (var i = 0, l = obj.length; i < l; i++) {
if (hasBinary(obj[i])) {

if (obj is List && obj is! TypedData) {
for (var value in obj) {
if (hasBinary(value)) {
return true;
}
}
return false;
}

if (isBinary(obj)) {
return true;
}

if (obj['toJSON'] != null && obj['toJSON'] is Function && toJSON == false) {
return hasBinary(obj.toJSON(), true);
// Check if the object has a toJSON method, regardless of its type (Map, custom object, etc.)
var toJsonMethod = _getToJsonMethod(obj);
if (toJsonMethod != null && toJSON == false) {
return hasBinary(toJsonMethod(), true);
}

if (obj is Map) {
for (var entry in obj.entries) {
if (hasBinary(entry.value)) {
return true;
}
}
} else if (obj is List) {
for (var value in obj) {
if (hasBinary(value)) {
return true;
}
}
}

return false;
}

// Helper function to dynamically check if an object has a toJSON method
Function? _getToJsonMethod(obj) {
try {
var toJsonMethod = obj.toJSON;
if (toJsonMethod is Function) {
return toJsonMethod;
}
} catch (e) {
// Catch and ignore errors if the method is not present
}
return null;
}
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: socket_io_common
description: Socket.io common parser library.
version: 3.0.0-beta.0
version: 3.0.0
homepage: https://www.zkoss.org
repository: https://github.com/rikulo/socket_io_common
issue_tracker: https://github.com/rikulo/socket_io_common/issues
Expand All @@ -9,4 +9,8 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
logging: '^1.2.0'
logging: '^1.2.0'

dev_dependencies:
lints: ^4.0.0
test: ">=1.3.0 <2.0.0"
97 changes: 97 additions & 0 deletions test/isBinary.test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:typed_data';

import 'package:socket_io_common/src/parser/is_binary.dart';

createMap(TypedData? byteData) => {
"child": byteData,
};

createMapWithArray(TypedData? byteData) => {
"child": [byteData]
};

createDeepMap(TypedData? byteData) => {
"child": {
"deep": {"deep": byteData}
},
};

createDeepMapWithArray(TypedData? byteData) => {
"child": {
"deep": {
"deep": [byteData]
}
},
};

createArray(TypedData? byteData) => [byteData];

createArrayInArray(TypedData? byteData) => [
[byteData]
];

createArrayWithMap(TypedData? byteData) => [createMap(byteData)];

createArrayWithDeepMap(TypedData? byteData) => [createDeepMap(byteData)];

createMapWithToJson(TypedData? byteData) => {
"toJSON": () => {"child": byteData}
};

main() {
final byteData = ByteData(1);

// -------------
print("With ByteBuffer: ${hasBinary(byteData.buffer)}");
print("With ByteData: ${hasBinary(byteData)}");
print("-" * 30);
// -------------
print("With map and binary: ${hasBinary(createMap(byteData))}");
print("With map and null: ${hasBinary(createMap(null))}");
print("-" * 30);
// -------------
print(
"With map with array and binary: ${hasBinary(createMapWithArray(byteData))}");
print("With map with array and null: ${hasBinary(createMapWithArray(null))}");
print("-" * 30);
// -------------
print("With deep map and binary: ${hasBinary(createDeepMap(byteData))}");
print("With deep map and null: ${hasBinary(createDeepMap(null))}");
print("-" * 30);
// -------------
print(
"With deep map with array and binary: ${hasBinary(createDeepMapWithArray(byteData))}");
print(
"With deep map with array and null: ${hasBinary(createDeepMapWithArray(null))}");
print("-" * 30);
// -------------
print("With array and binary: ${hasBinary(createArray(byteData))}");
print("With array and null: ${hasBinary(createArray(null))}");
print("-" * 30);
// -------------
print(
"With array in array and binary: ${hasBinary(createArrayInArray(byteData))}");
print("With array in array and null: ${hasBinary(createArrayInArray(null))}");
print("-" * 30);
// -------------
print(
"With array with map and binary: ${hasBinary(createArrayWithMap(byteData))}");
print("With array with map and null: ${hasBinary(createArrayWithMap(null))}");
print("-" * 30);
// -------------
print(
"With array with deep map and binary: ${hasBinary(createArrayWithDeepMap(byteData))}");
print(
"With array with deep map and null: ${hasBinary(createArrayWithDeepMap(null))}");
print("-" * 30);
// -------------
print("With toJSON and binary: ${hasBinary(createMapWithToJson(byteData))}");
print("With toJSON and null: ${hasBinary(createMapWithToJson(null))}");
print("With toJSON from an Object: ${hasBinary(MyObject())}");
}

class MyObject {
Map toJSON() {
return {"child": ByteData(1)};
}
}

0 comments on commit 30a9784

Please sign in to comment.