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