Skip to content
Snippets Groups Projects
Commit 847c9189 authored by Kevin Moore's avatar Kevin Moore
Browse files

Remove dependency on pkg/uuid

Use a home-rolled version
parent b7864409
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,6 @@ import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http_throttle/http_throttle.dart';
import 'package:stack_trace/stack_trace.dart';
import 'package:uuid/uuid.dart';
import 'command_runner.dart';
import 'io.dart';
......@@ -31,7 +30,7 @@ final _CENSORED_FIELDS = const ['refresh_token', 'authorization'];
final PUB_API_HEADERS = const {'Accept': 'application/vnd.pub.v2+json'};
/// A unique ID to identify this particular invocation of pub.
final _sessionId = new Uuid().v4() as String;
final _sessionId = createUuid();
/// An HTTP client that transforms 40* errors and socket exceptions into more
/// user-friendly error messages.
......
......@@ -824,3 +824,26 @@ void dataError(String message) => throw new DataException(message);
Iterable/*<T>*/ combineIterables/*<T>*/(
Iterable/*<T>*/ iter1, Iterable/*<T>*/ iter2) =>
iter1.toList()..addAll(iter2);
/// Returns a UUID in v4 format as a `String`.
///
/// If [bytes] is provided, it must be length 16 and have values between `0` and
/// `255` inclusive.
///
/// If [bytes] is not provided, it is generated using `Random.secure`.
String createUuid([List<int> bytes]) {
var rnd = new math.Random.secure();
// See http://www.cryptosys.net/pki/uuid-rfc4122.html for notes
bytes ??= new List<int>.generate(16, (_) => rnd.nextInt(256));
bytes[6] = (bytes[6] & 0x0F) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
var chars = bytes
.map((b) => b.toRadixString(16).padLeft(2, '0'))
.join()
.toUpperCase();
return '${chars.substring(0, 8)}-${chars.substring(8, 12)}-'
'${chars.substring(12, 16)}-${chars.substring(16, 20)}-${chars.substring(20, 32)}';
}
......@@ -31,7 +31,6 @@ dependencies:
stack_trace: "^1.0.0"
stream_channel: "^1.4.0"
string_scanner: "^1.0.0"
uuid: "^0.5.0"
watcher: "^0.9.2"
web_socket_channel: "^1.0.0"
yaml: "^2.0.0"
......
......@@ -102,4 +102,26 @@ b: {}"""));
expect(niceDuration(new Duration(minutes: 1)), equals("1:00.0s"));
});
});
group('uuid', () {
var uuidRegexp = new RegExp("^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-"
r"[8-9A-B][0-9A-F]{3}-[0-9A-F]{12}$");
test("min value is valid", () {
var uuid = createUuid(new List<int>.filled(16, 0));
expect(uuid, matches(uuidRegexp));
expect(uuid, "00000000-0000-4000-8000-000000000000");
});
test("max value is valid", () {
var uuid = createUuid(new List<int>.filled(16, 255));
expect(uuid, matches(uuidRegexp));
expect(uuid, "FFFFFFFF-FFFF-4FFF-BFFF-FFFFFFFFFFFF");
});
test("random values are valid", () {
for (var i = 0; i < 100; i++) {
var uuid = createUuid();
expect(uuid, matches(uuidRegexp));
}
});
});
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment