mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-18 21:54:34 +01:00
firebase_auth_rest.dart 333 -> 238 + firebase_credentials 71
+ firebase_auth_error 31
firebase_client.dart 283 -> 207 + firebase_keys 60 + firebase_errors 19
sync.dart 259 -> 141 + sync_state 121
Mirrors the split just made on the Python side, module for module, so the two
implementations stay legible against each other: credentials with their store,
key escaping with its escape table, the error type with the response parser
that builds its message.
Every new file is added to lib/crdt_sync.dart. A src/ file that is not in the
barrel compiles and tests green while silently dropping the symbol from every
consumer's import, so the export list is part of the split, not an afterthought.
280 tests pass and `dart analyze` is clean — including the four consumers
(firebase_config, secure_credential_store) whose imports had to follow the
moved types.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CFSGeWxHPY5rHUGrPqhcmG
63 lines
2.1 KiB
Dart
63 lines
2.1 KiB
Dart
/// Characters Realtime Database forbids in a key, plus `~` itself because it
|
|
/// is this module's escape character.
|
|
///
|
|
/// `/` is absent deliberately: it is the path separator, so it is handled by
|
|
/// splitting into segments before any segment is escaped.
|
|
const _escapedChars = {
|
|
'~': '~7E',
|
|
'.': '~2E',
|
|
r'$': '~24',
|
|
'#': '~23',
|
|
'[': '~5B',
|
|
']': '~5D',
|
|
};
|
|
|
|
/// Escapes one path segment into a legal Realtime Database key.
|
|
///
|
|
/// RTDB rejects `. $ # [ ] /` in keys, and the REST API's trailing `.json`
|
|
/// is a *format suffix* rather than part of the path -- so a filename like
|
|
/// `log.json` cannot be stored verbatim. The mapping is a reversible `~XX`
|
|
/// hex escape (`log.json` -> `log~2Ejson`) rather than a lossy "strip the
|
|
/// extension", because callers see these names again: todo-app's
|
|
/// `listDirectory('todo-sync/notes')` returns *filenames*, not device
|
|
/// directories, and must keep getting `<uuid>.json` back.
|
|
///
|
|
/// `~` is used as the escape character because it is legal in RTDB keys and
|
|
/// unreserved in URLs, so the escaped form needs no percent-encoding -- which
|
|
/// would otherwise be decoded back into the illegal character by the server.
|
|
String encodeKey(String segment) {
|
|
final buffer = StringBuffer();
|
|
for (final rune in segment.split('')) {
|
|
buffer.write(_escapedChars[rune] ?? rune);
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
/// Reverses [encodeKey].
|
|
String decodeKey(String key) {
|
|
final buffer = StringBuffer();
|
|
var index = 0;
|
|
while (index < key.length) {
|
|
final escape = key.length - index >= 3
|
|
? key.substring(index, index + 3)
|
|
: '';
|
|
final decoded = _escapeToChar[escape];
|
|
if (decoded == null) {
|
|
buffer.write(key[index]);
|
|
index += 1;
|
|
} else {
|
|
buffer.write(decoded);
|
|
index += 3;
|
|
}
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
final Map<String, String> _escapeToChar = {
|
|
for (final entry in _escapedChars.entries) entry.value: entry.key,
|
|
};
|
|
|
|
/// Escapes every segment of a `/`-separated logical path.
|
|
String encodePath(String path) =>
|
|
path.split('/').where((s) => s.isNotEmpty).map(encodeKey).join('/');
|