Add Strictly Typed NIO Channel Builders for Swift 5.7

Related tags

Database Omnibus
Overview

⚠️ Requires Swift 5.7

Omnibus is a set of helpers for SwiftNIO that allow you to leverage Swift's generics type system to create NIO Channels.

It depends on Swift 5.7's new ResultBuilder feature, Partial Blocks, and leverages this to enable type-checked channel building.

Channels are written in the same order as they're used in NIO normally. Inbound data comes in from the top, using the channel's read (Inbound) types. For official NIO channels that's IOData, and official channels also write (Outbound) IOData.

Each handler transforms either the Inboud, Outbound or both types using ChannelInboundHandler, ChannelOutboundHandler or ChannelDuplexHandler respectively.

Omnibus' channel builder type checks each handler, so that the InboundOut of one handler must match the InboundIn of the next. Likewise, it checks if the OutboundIn and OutboundOut match up as well.

When writing channels using this system, or NIO in general, the input (read data) comes in at the first handler. However, outbound data comes in at the bottom of the chain, and works its way back to the front.

Example

let myPrivateKey: Insecure.RSA.PrivateKey = ..

// Create a TCP Server that accepts clients on localhost:8082
let server = try await TCPServer.buildServer(host: "127.0.0.1", port: 8082) {
  // InboundHandler that maps NIO's IOData to ByteBuffer
  IODataInboundDecoder()
  // OutboundHandler that encodes HTTPServerResponsePart to IOData
  HTTPResponseEncoder()
  // InboundHandler that decodes ByteBuffer
  ByteToMessageHandler(HTTPRequestDecoder(leftOverBytesStrategy: .dropBytes))
  // InboundHandler that maps HTTPServerRequestPart to HTTPClientRequestPart
  HTTPProxyServerRequestMapper()
  // OutboundHandler that maps HTTPClientResponsePart to HTTPServerResponsePart
  HTTPProxyServerResponseMapper()
  
  // DuplexHandler that consumes all Inbound data
  ProxyChannelHandler { baseChannel in
    // baseChannel is the client connected to TCPServer
    // 1. Connect to an SSHServer and start an SSH tunnel
    // 2. Configure that tunnel's channel
    // 3. Return the configured (typed) channel
    return try await SSHClient.connect(
        host: "orlandos.nl",
        authenticationMethod: .rsa(username: "joannis", privateKey: myPrivateKey),
        hostKeyValidator: .acceptAnything()
    ).buildTunnel(host: "example.com", port: 80) {
      // OutboundHandler that maps IOData to ByteBuffer. SSHTunnel reads/writes ByteBuffer, not IOData.
      IODataOutboundDecoder()
      // OutboundHandler that Encodes Ooutbound HTTPClientRequestPart to IOData
      HTTPRequestEncoder()
      // InboundHandler that decodes incoming ByteBuffer into HTTPClientResponsePart
      HTTPResponseDecoder(leftOverBytesStrategy: .dropBytes)
      // InboundHandler that consumes HTTPClientResponsePart, and writes it to the TCPServer's client
      OutputToChannelHandler(channel: baseChannel, payloadType: HTTPClientResponsePart.self)
    }
  }
}
You might also like...
Ios-App-ication-Swift - A simple iOS application made in Xcode using Swift
Ios-App-ication-Swift - A simple iOS application made in Xcode using Swift

📱 iPhone Calculator A simple iOS application made in Xcode using Swift. This ap

Save-the-dot-project-swift - Save the dot project with swift
Save-the-dot-project-swift - Save the dot project with swift

Save the Dot Apple introduced UIViewPropertyAnimator for iOS 10. We can use this

The Swift Package Index is the place to find Swift packages!
The Swift Package Index is the place to find Swift packages!

The Swift Package Index helps you make better decisions about the dependencies you use in your apps. The Swift Package Index is a search engine for pa

A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.
A stand-alone Swift wrapper around the mongo-c client library, enabling access to MongoDB servers.

This package is deprecated in favour of the official Mongo Swift Driver. We advise users to switch to that pack

Elegant library to manage the interactions between view and model in Swift
Elegant library to manage the interactions between view and model in Swift

An assistant to manage the interactions between view and model ModelAssistant is a mediator between the view and model. This framework is tailored to

CRUD is an object-relational mapping (ORM) system for Swift 4+.

CRUD is an object-relational mapping (ORM) system for Swift 4+. CRUD takes Swift 4 Codable types and maps them to SQL database tables. CRUD can create tables based on Codable types and perform inserts and updates of objects in those tables. CRUD can also perform selects and joins of tables, all in a type-safe manner.

CoreXLSX is a Excel spreadsheet (XLSX) format parser written in pure Swift

CoreXLSX Excel spreadsheet (XLSX) format parser written in pure Swift CoreXLSX is a library focused on representing the low-level structure of the XML

Solutions to LeetCode by Swift
Solutions to LeetCode by Swift

LeetCode by Swift LeetCode Online Judge is a website containing many algorithm questions. Most of them are real interview questions of Google, Faceboo

Super lightweight DB written in Swift.
Super lightweight DB written in Swift.

Use of value types is recommended and we define standard values, simple structured data, application state and etc. as struct or enum. Pencil makes us store these values more easily.

Owner
Orlandos
Orlandos
💾 Safe, statically-typed, store-agnostic key-value storage written in Swift!

Storez ?? Safe, statically-typed, store-agnostic key-value storage Highlights Fully Customizable: Customize the persistence store, the KeyType class,

Kitz 67 Aug 7, 2022
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
Simple, Strongly Typed UserDefaults for iOS, macOS and tvOS

简体中文 DefaultsKit leverages Swift 4's powerful Codable capabilities to provide a Simple and Strongly Typed wrapper on top of UserDefaults. It uses less

Nuno Dias 1.4k Dec 26, 2022
Typed key-value storage solution to store Codable types in various persistence layers with few lines of code!

?? Stores A typed key-value storage solution to store Codable types in various persistence layers like User Defaults, File System, Core Data, Keychain

Omar Albeik 94 Dec 31, 2022
The hassle-free way to add Segment analytics to your Swift app (iOS/tvOS/watchOS/macOS/Linux).

Analytics-Swift The hassle-free way to add Segment analytics to your Swift app (iOS/tvOS/watchOS/macOS/Linux/iPadOS). Analytics helps you measure your

Segment 53 Dec 16, 2022
Movies Information DataBase (Add - Delete - Edit - Search)

MoviesInformation Movies Information DataBase (Add - Delete - Edit - Search) This Code Provide Simple Program About Movies Information This Program Ca

Mohammad Jaha 2 Sep 15, 2021
SQLite.swift - A type-safe, Swift-language layer over SQLite3.

SQLite.swift provides compile-time confidence in SQL statement syntax and intent.

Stephen Celis 8.7k Jan 3, 2023
🧡 SQLiteOrm-Swift is an ORM library for SQLite3 built with Swift 5

?? Easy to use SQLite ORM library written with Swift

Yevgeniy Zakharov 25 Oct 6, 2022
ObjectBox Swift - persisting your Swift objects superfast and simple

ObjectBox Swift ObjectBox is a superfast, light-weight object persistence framework. This Swift API seamlessly persists objects on-device for iOS and

ObjectBox 380 Dec 19, 2022
Shows the issue with swift using an ObjC class which has a property from a swift package.

SwiftObjCSwiftTest Shows the issue with swift using an ObjC class which has a property from a swift package. The Swift class (created as @objc derived

Scott Little 0 Nov 8, 2021