Skip to content

Instantly share code, notes, and snippets.

@jdanthinne
jdanthinne / AsyncSequence+onSubscribed.swift
Created November 17, 2025 15:44
Extension for AsyncSequence to launch an operation when stream starts
import Foundation
package extension AsyncSequence where Element: Sendable, Self: Sendable {
func onSubscribed(perform: @Sendable @escaping () async throws -> Void) -> AsyncThrowingStream<Element, Error> {
.init { continuation in
let task = Task {
do {
// Use a task group to run both operations concurrently
try await withThrowingTaskGroup(of: Void.self) { group in
// Launch the iteration task
@jdanthinne
jdanthinne / PemToP12.swift
Last active July 27, 2023 21:31
Convert PEM certificate to P12 (PKCS#12)
import Foundation
import OpenSSL
static func pkcs12(fromPem pemCertificate: String,
withPrivateKey pemPrivateKey: String,
p12Password: String = "",
certificateAuthorityFileURL: URL? = nil) throws -> NSData {
// Create sec certificates from PEM string
let modifiedCert = pemCertificate
.replacingOccurrences(of: "-----BEGIN CERTIFICATE-----", with: "")