base64DecodeHook function

HiHook base64DecodeHook({
  1. String uid = 'base64:decode',
  2. List<String> events = const ['read', 'get'],
  3. HiPhase phase = HiPhase.post,
  4. int priority = 0,
  5. bool urlSafe = false,
  6. bool asString = true,
})

Creates a Base64 decode hook for read operations.

Decodes Base64 string from storage to original value.

Parameters

  • uid: Unique hook identifier, defaults to 'base64:decode'
  • events: Events to listen for, defaults to 'read', 'get'
  • phase: Hook phase, defaults to HiPhase.post (decode after read)
  • urlSafe: Expect URL-safe Base64 encoding
  • asString: Decode to String (true) or Uint8List (false)

Payload Transformation

  • Base64 string → String (if asString=true)
  • Base64 string → Uint8List (if asString=false)
  • Non-string values → passed through unchanged

Example

engine.register(base64DecodeHook(asString: true));

// Input: HiPayload(key: 'data', value: 'aGVsbG8=')
// Output: HiPayload(key: 'data', value: 'hello')

Implementation

HiHook<dynamic, dynamic> base64DecodeHook({
  String uid = 'base64:decode',
  List<String> events = const ['read', 'get'],
  HiPhase phase = HiPhase.post,
  int priority = 0,
  bool urlSafe = false,
  bool asString = true,
}) {
  return HiHook<dynamic, dynamic>(
    uid: uid,
    events: events,
    phase: phase,
    priority: priority,
    handler: (payload, ctx) {
      final value = payload.value;

      if (value is String) {
        try {
          final bytes = urlSafe ? base64Url.decode(value) : base64.decode(value);
          final decoded = asString ? utf8.decode(bytes) : bytes;

          return HiContinue(
            payload: HiPayload(
              key: payload.key,
              value: decoded,
              metadata: payload.metadata,
            ),
          );
        } catch (_) {
          // Invalid Base64, pass through unchanged
          return const HiContinue();
        }
      }

      // Pass through unchanged for non-string values
      return const HiContinue();
    },
  );
}