SPB Git

Repository Public License

Satellite.swift 5.6 KB 101 lines swift

1import Foundation2import PrismComponentSDK3import PrismCore4import PrismObjectModel56public enum PrismRuntimeInfo {7    /// Version of the embedded renderer/runtime. A satellite states the minimum it needs.8    public static let version = "1.1.0"9    public static let kind = "prism-satellite"1011    public static func isCompatible(minimum: String, current: String = version) -> Bool {12        func parts(_ v: String) -> [Int] { v.split(separator: ".").map { Int($0) ?? 0 } + [0, 0, 0] }13        let a = parts(minimum), b = parts(current)14        for i in 0..<3 where a[i] != b[i] { return a[i] < b[i] }15        return true16    }17}1819public struct PublishError: Error, LocalizedError, Sendable, Equatable {20    public let message: String21    public init(_ message: String) { self.message = message }22    public var errorDescription: String? { message }23}2425/// The manifest inside a published satellite (spec §10).26public struct SatelliteManifest: Sendable, Equatable {27    public static let schemaVersion = 12829    public var appId: String30    public var displayName: String31    public var version: String32    public var minimumRuntime: String33    public var entrypoint: String34    public var permissions: [String]35    public var components: [String]36    public var blueprintHash: String37    public var projectID: String3839    public init(blueprint: AppBlueprint, appId: String, version: String, projectID: String) {40        self.appId = appId; displayName = blueprint.name; self.version = version; minimumRuntime = PrismRuntimeInfo.version41        entrypoint = "pages/" + blueprint.entrypoint; permissions = blueprint.permissions; components = blueprint.componentTypes42        blueprintHash = blueprint.contentHash; self.projectID = projectID43    }4445    public var json: JSONValue {46        [47            "schemaVersion": .int(Self.schemaVersion), "appId": .string(appId), "displayName": .string(displayName), "version": .string(version),48            "runtime": ["kind": .string(PrismRuntimeInfo.kind), "minimumVersion": .string(minimumRuntime)], "entrypoint": .string(entrypoint),49            "permissions": .array(permissions.map(JSONValue.string)), "components": .array(components.map(JSONValue.string)),50            "data": ["store": "sqlite", "schemaVersion": 1], "blueprintHash": .string(blueprintHash), "projectId": .string(projectID),51        ]52    }5354    public init(json: JSONValue) throws {55        guard json["schemaVersion"]?.intValue == Self.schemaVersion else { throw PublishError("The app's manifest uses an unsupported format.") }56        guard let appId = json["appId"]?.stringValue, let name = json["displayName"]?.stringValue, let version = json["version"]?.stringValue,57              json["runtime"]?["kind"]?.stringValue == PrismRuntimeInfo.kind, let minimum = json["runtime"]?["minimumVersion"]?.stringValue,58              let hash = json["blueprintHash"]?.stringValue else { throw PublishError("The app's manifest is incomplete.") }59        self.appId = appId; displayName = name; self.version = version; minimumRuntime = minimum60        entrypoint = json["entrypoint"]?.stringValue ?? ""61        permissions = (json["permissions"]?.arrayValue ?? []).compactMap(\.stringValue)62        components = (json["components"]?.arrayValue ?? []).compactMap(\.stringValue)63        blueprintHash = hash64        projectID = json["projectId"]?.stringValue ?? ""65    }66}6768/// What the satellite host does at launch: read, verify, and either produce a runnable app or a clear reason why not.69public enum SatelliteLoader {70    public struct Loaded: Sendable {71        public var manifest: SatelliteManifest72        public var blueprint: AppBlueprint73    }7475    public static func load(resources: URL, registry: ComponentRegistry, runtimeVersion: String = PrismRuntimeInfo.version) throws -> Loaded {76        guard let manifestData = FileManager.default.contents(atPath: resources.appendingPathComponent("satellite.json").path),77              let blueprintData = FileManager.default.contents(atPath: resources.appendingPathComponent("blueprint.json").path) else {78            throw PublishError("This app is incomplete: its definition is missing. Publish it again from PRISM.")79        }80        let manifest: SatelliteManifest81        do { manifest = try SatelliteManifest(json: JSONValue.parse(manifestData)) } catch { throw PublishError("This app's manifest cannot be read. Publish it again from PRISM.") }82        guard PrismRuntimeInfo.isCompatible(minimum: manifest.minimumRuntime, current: runtimeVersion) else {83            throw PublishError("“\(manifest.displayName)” needs PRISM runtime \(manifest.minimumRuntime) or later; this copy contains runtime \(runtimeVersion). Publish it again from a current PRISM.")84        }85        let blueprint: AppBlueprint86        do { blueprint = try AppBlueprint.load(blueprintData) } catch { throw PublishError("This app's definition cannot be read: \(error.localizedDescription)") }87        guard blueprint.contentHash == manifest.blueprintHash else {88            throw PublishError("This app's definition was changed after it was published, so it will not run. Publish it again from PRISM.")89        }90        if let problem = BlueprintValidator.validate(blueprint, registry: registry).errors.first {91            throw PublishError("This app cannot run with this runtime: \(problem.message)")92        }93        return Loaded(manifest: manifest, blueprint: blueprint)94    }9596    /// Where a satellite keeps its data: its own Application Support folder, outside the signed bundle.97    public static func dataDirectory(appId: String, base: URL? = nil) -> URL {98        (base ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]).appendingPathComponent(appId, isDirectory: true)99    }100}101
Files main
Menu

Theme