encodeConfiguration static method

Map<String, Object?> encodeConfiguration(
  1. JackfieldConfiguration config
)

Encodes initialization, rejecting unsafe callback destinations and bounds.

Implementation

static Map<String, Object?> encodeConfiguration(
  JackfieldConfiguration config,
) {
  final callbacks = config.callbacks;
  if (callbacks == null) return encodeQuery();
  final endpoint = callbacks.endpoint;
  if (endpoint.scheme != 'https' ||
      endpoint.host.isEmpty ||
      endpoint.userInfo.isNotEmpty ||
      endpoint.hasFragment ||
      callbacks.timeToLive.inMilliseconds <= 0 ||
      callbacks.maxPendingEvents <= 0 ||
      callbacks.auth.token.trim().isEmpty ||
      callbacks.auth.token.contains(RegExp(r'[\r\n]'))) {
    throw const JackfieldProtocolException('Invalid callback configuration');
  }
  return {
    'version': version,
    'callbacks': {
      'endpoint': endpoint.toString(),
      'auth': {'type': 'bearer', 'token': callbacks.auth.token},
      'timeToLiveMs': callbacks.timeToLive.inMilliseconds,
      'maxPendingEvents': callbacks.maxPendingEvents,
    },
  };
}