|
| 1 | +/** |
| 2 | + * OCI Annotations support for WebAssembly components |
| 3 | + * |
| 4 | + * This module implements the WebAssembly tool-conventions for OCI annotations |
| 5 | + * as defined in: https://github.com/WebAssembly/tool-conventions/pull/248 |
| 6 | + * |
| 7 | + * The following custom sections are supported: |
| 8 | + * - version: Version of the packaged software |
| 9 | + * - description: Human-readable description of the binary |
| 10 | + * - authors: Contact details of the authors |
| 11 | + * - licenses: SPDX license expression |
| 12 | + * - homepage: URL to find more information about the package |
| 13 | + * - source: URL to get source code for building the package |
| 14 | + * - revision: Hash of the commit used to build the package |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Encode a number as LEB128 unsigned integer |
| 19 | + * @param {number} value |
| 20 | + * @returns {Uint8Array} |
| 21 | + */ |
| 22 | +function encodeLEB128(value) { |
| 23 | + const bytes = []; |
| 24 | + do { |
| 25 | + let byte = value & 0x7f; |
| 26 | + value >>= 7; |
| 27 | + if (value !== 0) { |
| 28 | + byte |= 0x80; |
| 29 | + } |
| 30 | + bytes.push(byte); |
| 31 | + } while (value !== 0); |
| 32 | + return new Uint8Array(bytes); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Encode a custom section with the given name and data |
| 37 | + * @param {string} name - Section name |
| 38 | + * @param {string} data - Section data (UTF-8 string) |
| 39 | + * @returns {Uint8Array} |
| 40 | + */ |
| 41 | +function encodeCustomSection(name, data) { |
| 42 | + const nameBytes = new TextEncoder().encode(name); |
| 43 | + const dataBytes = new TextEncoder().encode(data); |
| 44 | + |
| 45 | + const nameLengthBytes = encodeLEB128(nameBytes.length); |
| 46 | + const payloadSize = nameLengthBytes.length + nameBytes.length + dataBytes.length; |
| 47 | + const sectionSizeBytes = encodeLEB128(payloadSize); |
| 48 | + |
| 49 | + // Custom section ID is 0 |
| 50 | + const sectionId = new Uint8Array([0]); |
| 51 | + |
| 52 | + // Concatenate all parts |
| 53 | + const section = new Uint8Array( |
| 54 | + sectionId.length + |
| 55 | + sectionSizeBytes.length + |
| 56 | + nameLengthBytes.length + |
| 57 | + nameBytes.length + |
| 58 | + dataBytes.length |
| 59 | + ); |
| 60 | + |
| 61 | + let offset = 0; |
| 62 | + section.set(sectionId, offset); |
| 63 | + offset += sectionId.length; |
| 64 | + section.set(sectionSizeBytes, offset); |
| 65 | + offset += sectionSizeBytes.length; |
| 66 | + section.set(nameLengthBytes, offset); |
| 67 | + offset += nameLengthBytes.length; |
| 68 | + section.set(nameBytes, offset); |
| 69 | + offset += nameBytes.length; |
| 70 | + section.set(dataBytes, offset); |
| 71 | + |
| 72 | + return section; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Add OCI annotation custom sections to a WebAssembly component |
| 77 | + * @param {Uint8Array} component - The WebAssembly component binary |
| 78 | + * @param {Object} annotations - OCI annotations to add |
| 79 | + * @param {string} [annotations.version] - Package version |
| 80 | + * @param {string} [annotations.description] - Package description |
| 81 | + * @param {string} [annotations.authors] - Package authors (freeform contact details) |
| 82 | + * @param {string} [annotations.licenses] - SPDX license expression |
| 83 | + * @param {string} [annotations.homepage] - Package homepage URL |
| 84 | + * @param {string} [annotations.source] - Source repository URL |
| 85 | + * @param {string} [annotations.revision] - Source revision/commit hash |
| 86 | + * @returns {Uint8Array} - Component with OCI annotations added |
| 87 | + */ |
| 88 | +export function addOCIAnnotations(component, annotations) { |
| 89 | + if (!annotations || Object.keys(annotations).length === 0) { |
| 90 | + return component; |
| 91 | + } |
| 92 | + |
| 93 | + const sections = []; |
| 94 | + |
| 95 | + // Add each annotation as a custom section |
| 96 | + // Order matters: add in a consistent order for reproducibility |
| 97 | + const fields = ['version', 'description', 'authors', 'licenses', 'homepage', 'source', 'revision']; |
| 98 | + |
| 99 | + for (const field of fields) { |
| 100 | + if (annotations[field]) { |
| 101 | + sections.push(encodeCustomSection(field, annotations[field])); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (sections.length === 0) { |
| 106 | + return component; |
| 107 | + } |
| 108 | + |
| 109 | + // Calculate total size needed |
| 110 | + let totalSize = component.length; |
| 111 | + for (const section of sections) { |
| 112 | + totalSize += section.length; |
| 113 | + } |
| 114 | + |
| 115 | + // The WebAssembly module/component starts with a magic number and version |
| 116 | + // Magic: 0x00 0x61 0x73 0x6d (for modules) |
| 117 | + // Component magic: 0x00 0x61 0x73 0x6d (same magic, different layer encoding) |
| 118 | + // Version: 4 bytes |
| 119 | + // We want to insert custom sections after the header (8 bytes) |
| 120 | + |
| 121 | + const WASM_HEADER_SIZE = 8; |
| 122 | + const result = new Uint8Array(totalSize); |
| 123 | + |
| 124 | + // Copy header |
| 125 | + result.set(component.subarray(0, WASM_HEADER_SIZE), 0); |
| 126 | + |
| 127 | + let offset = WASM_HEADER_SIZE; |
| 128 | + |
| 129 | + // Add custom sections |
| 130 | + for (const section of sections) { |
| 131 | + result.set(section, offset); |
| 132 | + offset += section.length; |
| 133 | + } |
| 134 | + |
| 135 | + // Copy rest of component |
| 136 | + result.set(component.subarray(WASM_HEADER_SIZE), offset); |
| 137 | + |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Extract OCI annotations from package.json metadata |
| 143 | + * @param {Object} packageJson - Parsed package.json content |
| 144 | + * @returns {Object} OCI annotations |
| 145 | + */ |
| 146 | +export function extractAnnotationsFromPackageJson(packageJson) { |
| 147 | + const annotations = {}; |
| 148 | + |
| 149 | + if (packageJson.version) { |
| 150 | + annotations.version = packageJson.version; |
| 151 | + } |
| 152 | + |
| 153 | + if (packageJson.description) { |
| 154 | + annotations.description = packageJson.description; |
| 155 | + } |
| 156 | + |
| 157 | + // Authors can be a string or an array of objects/strings |
| 158 | + if (packageJson.author) { |
| 159 | + if (typeof packageJson.author === 'string') { |
| 160 | + annotations.authors = packageJson.author; |
| 161 | + } else if (packageJson.author.name) { |
| 162 | + const author = packageJson.author; |
| 163 | + let authorStr = author.name; |
| 164 | + if (author.email) authorStr += ` <${author.email}>`; |
| 165 | + annotations.authors = authorStr; |
| 166 | + } |
| 167 | + } else if (packageJson.authors && Array.isArray(packageJson.authors)) { |
| 168 | + const authorStrs = packageJson.authors.map(a => { |
| 169 | + if (typeof a === 'string') return a; |
| 170 | + let str = a.name || ''; |
| 171 | + if (a.email) str += ` <${a.email}>`; |
| 172 | + return str; |
| 173 | + }).filter(s => s); |
| 174 | + if (authorStrs.length > 0) { |
| 175 | + annotations.authors = authorStrs.join(', '); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (packageJson.license) { |
| 180 | + annotations.licenses = packageJson.license; |
| 181 | + } |
| 182 | + |
| 183 | + if (packageJson.homepage) { |
| 184 | + annotations.homepage = packageJson.homepage; |
| 185 | + } |
| 186 | + |
| 187 | + // Extract source URL from repository field |
| 188 | + if (packageJson.repository) { |
| 189 | + if (typeof packageJson.repository === 'string') { |
| 190 | + annotations.source = packageJson.repository; |
| 191 | + } else if (packageJson.repository.url) { |
| 192 | + annotations.source = packageJson.repository.url; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return annotations; |
| 197 | +} |
0 commit comments