|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftContainerPlugin open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import struct Foundation.Data |
| 16 | +import class Foundation.JSONDecoder |
| 17 | +import class Foundation.JSONEncoder |
| 18 | + |
| 19 | +/// ScratchImage is a special-purpose ImageSource which represents the scratch image. |
| 20 | +public struct ScratchImage { |
| 21 | + var architecture: String |
| 22 | + var os: String |
| 23 | + |
| 24 | + var decoder: JSONDecoder |
| 25 | + var encoder: JSONEncoder |
| 26 | + |
| 27 | + public init(architecture: String, os: String) { |
| 28 | + self.architecture = architecture |
| 29 | + self.os = os |
| 30 | + self.decoder = JSONDecoder() |
| 31 | + self.encoder = JSONEncoder() |
| 32 | + self.encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes] |
| 33 | + self.encoder.dateEncodingStrategy = .iso8601 |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +extension ScratchImage: ImageSource { |
| 38 | + /// Fetches an unstructured blob of data from the registry. |
| 39 | + /// |
| 40 | + /// - Parameters: |
| 41 | + /// - repository: Name of the repository containing the blob. |
| 42 | + /// - digest: Digest of the blob. |
| 43 | + /// - Returns: The downloaded data. |
| 44 | + /// - Throws: If the blob download fails. |
| 45 | + public func getBlob(repository: ImageReference.Repository, digest: ImageReference.Digest) async throws -> Data { |
| 46 | + Data() // fatalError? |
| 47 | + } |
| 48 | + |
| 49 | + /// Fetches an image manifest. |
| 50 | + /// |
| 51 | + /// - Parameters: |
| 52 | + /// - repository: Name of the source repository. |
| 53 | + /// - reference: Tag or digest of the manifest to fetch. |
| 54 | + /// - Returns: The downloaded manifest. |
| 55 | + /// - Throws: If the download fails or the manifest cannot be decoded. |
| 56 | + public func getManifest( |
| 57 | + repository: ImageReference.Repository, |
| 58 | + reference: any ImageReference.Reference |
| 59 | + ) async throws -> (ImageManifest, ContentDescriptor) { |
| 60 | + let config = ImageConfiguration( |
| 61 | + architecture: architecture, |
| 62 | + os: os, |
| 63 | + rootfs: .init(_type: "layers", diff_ids: []) |
| 64 | + ) |
| 65 | + let encodedConfig = try encoder.encode(config) |
| 66 | + |
| 67 | + let manifest = ImageManifest( |
| 68 | + schemaVersion: 2, |
| 69 | + config: ContentDescriptor( |
| 70 | + mediaType: "application/vnd.oci.image.config.v1+json", |
| 71 | + digest: "\(ImageReference.Digest(of: encodedConfig))", |
| 72 | + size: Int64(encodedConfig.count) |
| 73 | + ), |
| 74 | + layers: [] |
| 75 | + ) |
| 76 | + let encodedManifest = try encoder.encode(manifest) |
| 77 | + |
| 78 | + return ( |
| 79 | + manifest, |
| 80 | + ContentDescriptor( |
| 81 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 82 | + digest: "\(ImageReference.Digest(of: encodedManifest))", |
| 83 | + size: Int64(encodedManifest.count) |
| 84 | + ) |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + /// Fetches an image index. |
| 89 | + /// |
| 90 | + /// - Parameters: |
| 91 | + /// - repository: Name of the source repository. |
| 92 | + /// - reference: Tag or digest of the index to fetch. |
| 93 | + /// - Returns: The downloaded index. |
| 94 | + /// - Throws: If the download fails or the index cannot be decoded. |
| 95 | + public func getIndex( |
| 96 | + repository: ImageReference.Repository, |
| 97 | + reference: any ImageReference.Reference |
| 98 | + ) async throws -> ImageIndex { |
| 99 | + fatalError() |
| 100 | + } |
| 101 | + |
| 102 | + /// Get an image configuration record from the registry. |
| 103 | + /// - Parameters: |
| 104 | + /// - image: Reference to the image containing the record. |
| 105 | + /// - digest: Digest of the record. |
| 106 | + /// - Returns: The image confguration record stored in `repository` with digest `digest`. |
| 107 | + /// - Throws: If the blob cannot be decoded as an `ImageConfiguration`. |
| 108 | + /// |
| 109 | + /// Image configuration records are stored as blobs in the registry. This function retrieves the requested blob and tries to decode it as a configuration record. |
| 110 | + public func getImageConfiguration( |
| 111 | + forImage image: ImageReference, |
| 112 | + digest: ImageReference.Digest |
| 113 | + ) async throws -> ImageConfiguration { |
| 114 | + ImageConfiguration( |
| 115 | + architecture: architecture, |
| 116 | + os: os, |
| 117 | + rootfs: .init(_type: "layers", diff_ids: []) |
| 118 | + ) |
| 119 | + } |
| 120 | +} |
0 commit comments