| 1 | //! DeepSeek Harness plugin market index, format 1. |
| 2 | //! |
| 3 | //! Schema source: `reference/dsh` `packages/util/plugin-market-index` |
| 4 | //! (`types.ts` + `index.ts`, the format's executable rule set) and its |
| 5 | //! README ("format 1 … the single JSON file that lists installable DSH |
| 6 | //! bundles with their display metadata"). |
| 7 | //! |
| 8 | //! ```json |
| 9 | //! { |
| 10 | //! "format": 1, |
| 11 | //! "title": "My market", |
| 12 | //! "metadata": { "categories": { "tools": { "title": "Tools" } } }, |
| 13 | //! "plugins": [{ |
| 14 | //! "name": "@scope/plugin", |
| 15 | //! "title": { "en": "Plugin" }, |
| 16 | //! "icon": "./icon.png", |
| 17 | //! "metadata": { "category": "tools", "keywords": ["x"] }, |
| 18 | //! "versions": [{ |
| 19 | //! "version": "1.2.3", |
| 20 | //! "publishTimestamp": 1760000000, |
| 21 | //! "source": { "type": "npm" }, |
| 22 | //! "engines": { "dsh": ">=0.2.0 <0.3.0" } |
| 23 | //! }] |
| 24 | //! }] |
| 25 | //! } |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! Parsed as the format documents it: `format` must be the number `1`, |
| 29 | //! `name` is the plugin's npm package name, display text is a string or a |
| 30 | //! language-keyed object whose `en` member is authoritative, and relative |
| 31 | //! `icon` references resolve against the index URL when that URL is known. |
| 32 | //! `plugins[].versions` is ordered by descending SemVer precedence, and the |
| 33 | //! highest version decides the row's version and install source. |
| 34 | //! |
| 35 | //! # Install reality, stated honestly |
| 36 | //! |
| 37 | //! DSH installs `name@version` through npm or a commit-pinned `git+https` |
| 38 | //! URL. Codewhale does not execute npm, and its installer fetches whole |
| 39 | //! GitHub repositories (archive tarballs) rather than a package |
| 40 | //! subdirectory. So: |
| 41 | //! |
| 42 | //! - `npm` sources parse into [`MarketplaceSourceSpec::Npm`] and plan the |
| 43 | //! reviewed installer's fetch of that exact version's registry tarball |
| 44 | //! (`npm:name@version`, or the tarball under a declared https |
| 45 | //! `registry`). Nothing runs npm and no dependency is installed; the |
| 46 | //! bundle still lands disabled and untrusted behind the content review. |
| 47 | //! A name, version or registry the installer cannot fetch exactly stays |
| 48 | //! [`MarketplaceInstallPlan::Unsupported`] with the reason. |
| 49 | //! - `git` sources on `github.com` without a `path` become a pinned |
| 50 | //! `github:` install plan. A `path` (package inside a subdirectory) or a |
| 51 | //! non-GitHub host stays unsupported. |
| 52 | //! - A highest version whose `engines.dsh` range admits none of the DSH |
| 53 | //! versions this host targets is reported as unsupported with that range, |
| 54 | //! before any download. |
| 55 | |
| 56 | use serde_json::Value; |
| 57 | |
| 58 | use super::super::types::{ |
| 59 | CatalogProvenance, CatalogTier, MarketplaceCandidate, MarketplaceCandidateId, |
| 60 | MarketplaceCatalog, MarketplaceCatalogId, MarketplaceDiagnostic, MarketplaceEntryKind, |
| 61 | MarketplaceFormat, MarketplaceInstallPlan, MarketplaceSourceSpec, |
| 62 | }; |
| 63 | use super::{MarketplaceDocument, str_field, unknown_fields_warning}; |
| 64 | |
| 65 | const TOP_LEVEL_FIELDS: &[&str] = &["format", "title", "description", "metadata", "plugins"]; |
| 66 | const ENTRY_FIELDS: &[&str] = &[ |
| 67 | "name", |
| 68 | "title", |
| 69 | "description", |
| 70 | "icon", |
| 71 | "author", |
| 72 | "homepage", |
| 73 | "repository", |
| 74 | "metadata", |
| 75 | "versions", |
| 76 | ]; |
| 77 | const VERSION_FIELDS: &[&str] = &["version", "publishTimestamp", "source", "engines"]; |
| 78 | |
| 79 | pub fn parse_dsh_catalog(document: MarketplaceDocument) -> MarketplaceCatalog { |
| 80 | let MarketplaceDocument { |
| 81 | catalog_id, |
| 82 | root, |
| 83 | base, |
| 84 | .. |
| 85 | } = document; |
| 86 | let mut diagnostics = Vec::new(); |
| 87 | |
| 88 | let Some(obj) = root.as_object() else { |
| 89 | return empty_catalog( |
| 90 | catalog_id, |
| 91 | base, |
| 92 | MarketplaceDiagnostic::error( |
| 93 | "NOT_AN_OBJECT", |
| 94 | "a DSH market index must be a JSON object", |
| 95 | None, |
| 96 | None, |
| 97 | ), |
| 98 | ); |
| 99 | }; |
| 100 | |
| 101 | // The format's own discriminator: `format` must be the number 1. |
| 102 | if obj.get("format").and_then(Value::as_f64) != Some(1.0) { |
| 103 | diagnostics.push(MarketplaceDiagnostic::error( |
| 104 | "UNDECLARED_FORMAT", |
| 105 | "a DSH market index must declare `\"format\": 1`", |
| 106 | None, |
| 107 | None, |
| 108 | )); |
| 109 | return MarketplaceCatalog { |
| 110 | id: catalog_id, |
| 111 | format: MarketplaceFormat::Dsh, |
| 112 | name: String::new(), |
| 113 | display_name: None, |
| 114 | description: None, |
| 115 | version: None, |
| 116 | base, |
| 117 | provenance: CatalogProvenance::default(), |
| 118 | candidates: Vec::new(), |
| 119 | diagnostics, |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | if let Some(diag) = unknown_fields_warning(obj, TOP_LEVEL_FIELDS) { |
| 124 | diagnostics.push(diag); |
| 125 | } |
| 126 | |
| 127 | let title = localized_field(obj, "title", &mut diagnostics); |
| 128 | let description = localized_field(obj, "description", &mut diagnostics); |
| 129 | let declarations = index_declarations(obj.get("metadata")); |
| 130 | |
| 131 | let Some(entries) = obj.get("plugins").and_then(Value::as_array) else { |
| 132 | diagnostics.push(MarketplaceDiagnostic::error( |
| 133 | "MISSING_PLUGINS", |
| 134 | "a DSH market index must contain a `plugins` array", |
| 135 | None, |
| 136 | None, |
| 137 | )); |
| 138 | let name = title |
| 139 | .clone() |
| 140 | .unwrap_or_else(|| catalog_id.as_str().to_string()); |
| 141 | return MarketplaceCatalog { |
| 142 | id: catalog_id, |
| 143 | format: MarketplaceFormat::Dsh, |
| 144 | name, |
| 145 | display_name: title, |
| 146 | description, |
| 147 | version: None, |
| 148 | base, |
| 149 | provenance: CatalogProvenance::default(), |
| 150 | candidates: Vec::new(), |
| 151 | diagnostics, |
| 152 | }; |
| 153 | }; |
| 154 | |
| 155 | let mut candidates = Vec::new(); |
| 156 | for (index, entry) in entries.iter().enumerate() { |
| 157 | if let Some(candidate) = parse_entry( |
| 158 | &catalog_id, |
| 159 | index, |
| 160 | entry, |
| 161 | base.as_deref(), |
| 162 | &declarations, |
| 163 | &mut diagnostics, |
| 164 | ) { |
| 165 | candidates.push(candidate); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | MarketplaceCatalog { |
| 170 | id: catalog_id, |
| 171 | format: MarketplaceFormat::Dsh, |
| 172 | name: title |
| 173 | .clone() |
| 174 | .unwrap_or_else(|| "DeepSeek Harness market".to_string()), |
| 175 | display_name: title, |
| 176 | description, |
| 177 | version: None, |
| 178 | base, |
| 179 | provenance: CatalogProvenance { |
| 180 | tier: CatalogTier::Community, |
| 181 | publisher: None, |
| 182 | source_url: None, |
| 183 | }, |
| 184 | candidates, |
| 185 | diagnostics, |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | fn empty_catalog( |
| 190 | catalog_id: MarketplaceCatalogId, |
| 191 | base: Option<String>, |
| 192 | diagnostic: MarketplaceDiagnostic, |
| 193 | ) -> MarketplaceCatalog { |
| 194 | MarketplaceCatalog { |
| 195 | id: catalog_id, |
| 196 | format: MarketplaceFormat::Dsh, |
| 197 | name: String::new(), |
| 198 | display_name: None, |
| 199 | description: None, |
| 200 | version: None, |
| 201 | base, |
| 202 | provenance: CatalogProvenance::default(), |
| 203 | candidates: Vec::new(), |
| 204 | diagnostics: vec![diagnostic], |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /// Category/label ids the index declares. An entry may only reference a |
| 209 | /// declared id; anything else is dropped with a warning, as the validator |
| 210 | /// does. |
| 211 | #[derive(Default)] |
| 212 | struct IndexDeclarations { |
| 213 | categories: Vec<String>, |
| 214 | labels: Vec<String>, |
| 215 | } |
| 216 | |
| 217 | fn index_declarations(metadata: Option<&Value>) -> IndexDeclarations { |
| 218 | let mut declarations = IndexDeclarations::default(); |
| 219 | let Some(groups) = metadata.and_then(Value::as_object) else { |
| 220 | return declarations; |
| 221 | }; |
| 222 | declarations.categories = declared_ids(groups.get("categories")); |
| 223 | declarations.labels = declared_ids(groups.get("labels")); |
| 224 | declarations |
| 225 | } |
| 226 | |
| 227 | fn declared_ids(group: Option<&Value>) -> Vec<String> { |
| 228 | group |
| 229 | .and_then(Value::as_object) |
| 230 | .map(|record| record.keys().cloned().collect()) |
| 231 | .unwrap_or_default() |
| 232 | } |
| 233 | |
| 234 | /// `title`/`description` are either a string or a language-keyed object. The |
| 235 | /// format requires an `en` member on the object form, so `en` is the value |
| 236 | /// shown; a first member is only a degraded fallback carrying a warning. |
| 237 | fn localized_field( |
| 238 | obj: &serde_json::Map<String, Value>, |
| 239 | field: &str, |
| 240 | diagnostics: &mut Vec<MarketplaceDiagnostic>, |
| 241 | ) -> Option<String> { |
| 242 | match obj.get(field) { |
| 243 | None | Some(Value::Null) => None, |
| 244 | Some(Value::String(text)) => Some(text.clone()), |
| 245 | Some(Value::Object(languages)) => { |
| 246 | if let Some(en) = languages.get("en").and_then(Value::as_str) { |
| 247 | return Some(en.to_string()); |
| 248 | } |
| 249 | let first = languages.values().find_map(Value::as_str); |
| 250 | if first.is_some() { |
| 251 | diagnostics.push(MarketplaceDiagnostic::warning( |
| 252 | "MISSING_EN", |
| 253 | format!("`{field}` is language-keyed without `en`; showing another locale"), |
| 254 | None, |
| 255 | None, |
| 256 | )); |
| 257 | } |
| 258 | first.map(str::to_string) |
| 259 | } |
| 260 | Some(_) => { |
| 261 | diagnostics.push(MarketplaceDiagnostic::warning( |
| 262 | "FIELD_TYPE", |
| 263 | format!("field `{field}` must be a string or a language-keyed object"), |
| 264 | None, |
| 265 | None, |
| 266 | )); |
| 267 | None |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | fn parse_entry( |
| 273 | catalog_id: &MarketplaceCatalogId, |
| 274 | index: usize, |
| 275 | value: &Value, |
| 276 | base: Option<&str>, |
| 277 | declarations: &IndexDeclarations, |
| 278 | diagnostics: &mut Vec<MarketplaceDiagnostic>, |
| 279 | ) -> Option<MarketplaceCandidate> { |
| 280 | let Some(obj) = value.as_object() else { |
| 281 | diagnostics.push(MarketplaceDiagnostic::error( |
| 282 | "ENTRY_NOT_OBJECT", |
| 283 | "each DSH market entry must be an object", |
| 284 | None, |
| 285 | Some(index), |
| 286 | )); |
| 287 | return None; |
| 288 | }; |
| 289 | let mut entry_diags = Vec::new(); |
| 290 | if let Some(diag) = unknown_fields_warning(obj, ENTRY_FIELDS) { |
| 291 | entry_diags.push(diag); |
| 292 | } |
| 293 | |
| 294 | let (name, bad_name) = str_field(obj, "name"); |
| 295 | if let Some(diag) = bad_name { |
| 296 | entry_diags.push(diag); |
| 297 | } |
| 298 | let Some(name) = name.map(str::trim).filter(|name| !name.is_empty()) else { |
| 299 | diagnostics.push(MarketplaceDiagnostic::error( |
| 300 | "MISSING_NAME", |
| 301 | "a DSH market entry must declare its npm package `name`", |
| 302 | None, |
| 303 | Some(index), |
| 304 | )); |
| 305 | return None; |
| 306 | }; |
| 307 | |
| 308 | let title = localized_field(obj, "title", &mut entry_diags); |
| 309 | let description = localized_field(obj, "description", &mut entry_diags); |
| 310 | let (author, bad_author) = str_field(obj, "author"); |
| 311 | if let Some(diag) = bad_author { |
| 312 | entry_diags.push(diag); |
| 313 | } |
| 314 | let (homepage, bad_homepage) = str_field(obj, "homepage"); |
| 315 | if let Some(diag) = bad_homepage { |
| 316 | entry_diags.push(diag); |
| 317 | } |
| 318 | let (icon, bad_icon) = str_field(obj, "icon"); |
| 319 | if let Some(diag) = bad_icon { |
| 320 | entry_diags.push(diag); |
| 321 | } |
| 322 | let icon = icon.and_then(|icon| resolve_icon(icon, base, &mut entry_diags)); |
| 323 | let repository = obj |
| 324 | .get("repository") |
| 325 | .and_then(Value::as_object) |
| 326 | .and_then(|repo| repo.get("url")) |
| 327 | .and_then(Value::as_str) |
| 328 | .map(str::to_string); |
| 329 | |
| 330 | let (keywords, categories) = |
| 331 | entry_metadata(obj.get("metadata"), declarations, &mut entry_diags); |
| 332 | |
| 333 | let Some(version) = pick_version(obj.get("versions"), &mut entry_diags) else { |
| 334 | diagnostics.push(MarketplaceDiagnostic::error( |
| 335 | "MISSING_VERSIONS", |
| 336 | format!("DSH market entry `{name}` must carry a non-empty `versions` array"), |
| 337 | Some(name.to_string()), |
| 338 | Some(index), |
| 339 | )); |
| 340 | return None; |
| 341 | }; |
| 342 | |
| 343 | let (source, install_plan) = normalize_version_source(name, &version); |
| 344 | |
| 345 | Some(MarketplaceCandidate { |
| 346 | id: MarketplaceCandidateId::new(catalog_id, name), |
| 347 | catalog_id: catalog_id.clone(), |
| 348 | kind: MarketplaceEntryKind::Plugin, |
| 349 | name: name.to_string(), |
| 350 | display_name: title, |
| 351 | icon, |
| 352 | description, |
| 353 | version: Some(version.version.clone()), |
| 354 | author: author.map(str::to_string), |
| 355 | homepage: homepage.map(str::to_string), |
| 356 | repository, |
| 357 | license: None, |
| 358 | keywords, |
| 359 | categories, |
| 360 | source, |
| 361 | install_plan, |
| 362 | // The format lists packages, not their components; the DSH bundle's |
| 363 | // own manifest decides activation at install review. |
| 364 | declared_components: None, |
| 365 | compatibility: None, |
| 366 | provenance: CatalogProvenance { |
| 367 | tier: CatalogTier::Community, |
| 368 | publisher: author.map(str::to_string), |
| 369 | source_url: base.map(str::to_string), |
| 370 | }, |
| 371 | when: None, |
| 372 | diagnostics: entry_diags, |
| 373 | }) |
| 374 | } |
| 375 | |
| 376 | /// One version's install-relevant facts, validated as the format documents |
| 377 | /// them: exact SemVer, a tagged `npm`/`git` source, and an optional |
| 378 | /// `engines.dsh` SemVer range that must be a range, never `workspace:`. |
| 379 | struct DshVersion { |
| 380 | version: String, |
| 381 | engines: Option<String>, |
| 382 | source_type: String, |
| 383 | registry: Option<String>, |
| 384 | git_url: Option<String>, |
| 385 | git_commit: Option<String>, |
| 386 | git_path: Option<String>, |
| 387 | } |
| 388 | |
| 389 | fn pick_version( |
| 390 | versions: Option<&Value>, |
| 391 | entry_diags: &mut Vec<MarketplaceDiagnostic>, |
| 392 | ) -> Option<DshVersion> { |
| 393 | let entries = versions?.as_array()?; |
| 394 | if entries.is_empty() { |
| 395 | return None; |
| 396 | } |
| 397 | let mut parsed: Vec<(semver::Version, DshVersion)> = Vec::new(); |
| 398 | for (position, entry) in entries.iter().enumerate() { |
| 399 | let Some(obj) = entry.as_object() else { |
| 400 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 401 | "VERSION_NOT_OBJECT", |
| 402 | format!("`versions[{position}]` is not an object and was ignored"), |
| 403 | None, |
| 404 | None, |
| 405 | )); |
| 406 | continue; |
| 407 | }; |
| 408 | if let Some(diag) = unknown_fields_warning(obj, VERSION_FIELDS) { |
| 409 | entry_diags.push(diag); |
| 410 | } |
| 411 | let (raw_version, bad_version) = str_field(obj, "version"); |
| 412 | if let Some(diag) = bad_version { |
| 413 | entry_diags.push(diag); |
| 414 | } |
| 415 | let Some(raw_version) = raw_version else { |
| 416 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 417 | "MISSING_VERSION", |
| 418 | format!("`versions[{position}]` declares no `version` and was ignored"), |
| 419 | None, |
| 420 | None, |
| 421 | )); |
| 422 | continue; |
| 423 | }; |
| 424 | let Ok(precedence) = semver::Version::parse(raw_version) else { |
| 425 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 426 | "INVALID_VERSION", |
| 427 | format!("`versions[{position}].version` `{raw_version}` is not exact SemVer"), |
| 428 | None, |
| 429 | None, |
| 430 | )); |
| 431 | continue; |
| 432 | }; |
| 433 | let engines = obj |
| 434 | .get("engines") |
| 435 | .and_then(Value::as_object) |
| 436 | .and_then(|engines| engines.get("dsh")) |
| 437 | .and_then(Value::as_str); |
| 438 | if let Some(range) = engines |
| 439 | && !range_is_dsh_range(range) |
| 440 | { |
| 441 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 442 | "INVALID_ENGINES", |
| 443 | format!( |
| 444 | "`versions[{position}].engines.dsh` `{range}` is not a SemVer range; the version was ignored" |
| 445 | ), |
| 446 | None, |
| 447 | None, |
| 448 | )); |
| 449 | continue; |
| 450 | } |
| 451 | let Some(source) = obj.get("source").and_then(Value::as_object) else { |
| 452 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 453 | "MISSING_SOURCE", |
| 454 | format!("`versions[{position}]` declares no `source` object and was ignored"), |
| 455 | None, |
| 456 | None, |
| 457 | )); |
| 458 | continue; |
| 459 | }; |
| 460 | let source_type = source |
| 461 | .get("type") |
| 462 | .and_then(Value::as_str) |
| 463 | .unwrap_or_default(); |
| 464 | if !matches!(source_type, "npm" | "git") { |
| 465 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 466 | "UNKNOWN_SOURCE", |
| 467 | format!( |
| 468 | "`versions[{position}].source.type` must be \"npm\" or \"git\"; the version was ignored" |
| 469 | ), |
| 470 | None, |
| 471 | None, |
| 472 | )); |
| 473 | continue; |
| 474 | } |
| 475 | let registry = source.get("registry"); |
| 476 | if source_type == "npm" && registry.is_some_and(|value| !value.is_string()) { |
| 477 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 478 | "INVALID_REGISTRY", |
| 479 | format!( |
| 480 | "`versions[{position}].source.registry` must be a URL string; the version was ignored" |
| 481 | ), |
| 482 | None, |
| 483 | None, |
| 484 | )); |
| 485 | continue; |
| 486 | } |
| 487 | parsed.push(( |
| 488 | precedence, |
| 489 | DshVersion { |
| 490 | version: raw_version.to_string(), |
| 491 | engines: engines.map(str::to_string), |
| 492 | source_type: source_type.to_string(), |
| 493 | registry: registry.and_then(Value::as_str).map(str::to_string), |
| 494 | git_url: source |
| 495 | .get("url") |
| 496 | .and_then(Value::as_str) |
| 497 | .map(str::to_string), |
| 498 | git_commit: source |
| 499 | .get("commit") |
| 500 | .and_then(Value::as_str) |
| 501 | .map(str::to_string), |
| 502 | git_path: source |
| 503 | .get("path") |
| 504 | .and_then(Value::as_str) |
| 505 | .map(str::to_string), |
| 506 | }, |
| 507 | )); |
| 508 | } |
| 509 | parsed.sort_by(|left, right| right.0.cmp(&left.0)); |
| 510 | parsed.into_iter().next().map(|(_, version)| version) |
| 511 | } |
| 512 | |
| 513 | fn entry_metadata( |
| 514 | metadata: Option<&Value>, |
| 515 | declarations: &IndexDeclarations, |
| 516 | entry_diags: &mut Vec<MarketplaceDiagnostic>, |
| 517 | ) -> (Vec<String>, Vec<String>) { |
| 518 | let Some(metadata) = metadata.and_then(Value::as_object) else { |
| 519 | return (Vec::new(), Vec::new()); |
| 520 | }; |
| 521 | let keywords = string_list(metadata.get("keywords")); |
| 522 | let mut categories = Vec::new(); |
| 523 | if let Some(category) = metadata.get("category").and_then(Value::as_str) { |
| 524 | if declarations.categories.iter().any(|id| id == category) { |
| 525 | categories.push(category.to_string()); |
| 526 | } else { |
| 527 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 528 | "UNDECLARED_CATEGORY", |
| 529 | format!("entry category `{category}` is not declared by the index"), |
| 530 | None, |
| 531 | None, |
| 532 | )); |
| 533 | } |
| 534 | } |
| 535 | for label in string_list(metadata.get("labels")) { |
| 536 | if declarations.labels.contains(&label) { |
| 537 | categories.push(label); |
| 538 | } else { |
| 539 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 540 | "UNDECLARED_LABEL", |
| 541 | format!("entry label `{label}` is not declared by the index"), |
| 542 | None, |
| 543 | None, |
| 544 | )); |
| 545 | } |
| 546 | } |
| 547 | (keywords, categories) |
| 548 | } |
| 549 | |
| 550 | /// The DSH validator accepts what node-semver's `validRange` accepts: bare |
| 551 | /// exact versions, space-separated comparators and `||` clauses. The npm |
| 552 | /// range parser already used for `engines.dsh` admission is the authority |
| 553 | /// here, so a range the installer would refuse is never accepted as valid. |
| 554 | fn range_is_dsh_range(range: &str) -> bool { |
| 555 | !range.starts_with("workspace:") |
| 556 | && crate::install::dsh::match_supported_dsh_range(range).is_ok() |
| 557 | } |
| 558 | |
| 559 | fn string_list(value: Option<&Value>) -> Vec<String> { |
| 560 | value |
| 561 | .and_then(Value::as_array) |
| 562 | .map(|items| { |
| 563 | items |
| 564 | .iter() |
| 565 | .filter_map(Value::as_str) |
| 566 | .map(str::to_string) |
| 567 | .collect() |
| 568 | }) |
| 569 | .unwrap_or_default() |
| 570 | } |
| 571 | |
| 572 | fn resolve_icon( |
| 573 | icon: &str, |
| 574 | base: Option<&str>, |
| 575 | entry_diags: &mut Vec<MarketplaceDiagnostic>, |
| 576 | ) -> Option<String> { |
| 577 | if icon.starts_with("data:") || icon.starts_with("https://") || icon.starts_with("http://") { |
| 578 | return Some(icon.to_string()); |
| 579 | } |
| 580 | let Some(base) = base else { |
| 581 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 582 | "UNRESOLVED_ICON", |
| 583 | format!("relative icon `{icon}` has no index URL to resolve against; ignored"), |
| 584 | None, |
| 585 | None, |
| 586 | )); |
| 587 | return None; |
| 588 | }; |
| 589 | let Some((prefix, _)) = base.rsplit_once('/') else { |
| 590 | entry_diags.push(MarketplaceDiagnostic::warning( |
| 591 | "UNRESOLVED_ICON", |
| 592 | format!("relative icon `{icon}` cannot be resolved against `{base}`; ignored"), |
| 593 | None, |
| 594 | None, |
| 595 | )); |
| 596 | return None; |
| 597 | }; |
| 598 | // Resolve the reference the way a URL parser would: `.` segments drop |
| 599 | // and `..` pops one segment, but never above the origin's first path |
| 600 | // segment (an index URL is absolute, so it always has one). |
| 601 | let scheme_end = prefix.find("://").map_or(0, |at| at + 3); |
| 602 | let (origin, path) = prefix.split_at(scheme_end); |
| 603 | let mut segments: Vec<&str> = path.split('/').collect(); |
| 604 | for part in icon.split('/') { |
| 605 | match part { |
| 606 | "" | "." => {} |
| 607 | ".." => { |
| 608 | if segments.len() > 1 { |
| 609 | segments.pop(); |
| 610 | } |
| 611 | } |
| 612 | other => segments.push(other), |
| 613 | } |
| 614 | } |
| 615 | Some(format!("{origin}{}", segments.join("/"))) |
| 616 | } |
| 617 | |
| 618 | fn normalize_version_source( |
| 619 | name: &str, |
| 620 | version: &DshVersion, |
| 621 | ) -> (MarketplaceSourceSpec, MarketplaceInstallPlan) { |
| 622 | let incompatible = version.engines.as_deref().and_then(|range| { |
| 623 | match crate::install::dsh::match_supported_dsh_range(range) { |
| 624 | Ok(Some(_)) => None, |
| 625 | Ok(None) => Some(format!( |
| 626 | "incompatible-version: the bundle requires DSH `{range}`, but this host targets DSH 0.2.1-rc.5 / 0.2.2-rc.1..rc.5" |
| 627 | )), |
| 628 | Err(_) => Some(format!( |
| 629 | "incompatible-version: `engines.dsh` value `{range}` is not a SemVer range" |
| 630 | )), |
| 631 | } |
| 632 | }); |
| 633 | if let Some(reason) = incompatible { |
| 634 | return ( |
| 635 | MarketplaceSourceSpec::Npm { |
| 636 | package: name.to_string(), |
| 637 | }, |
| 638 | MarketplaceInstallPlan::Unsupported { |
| 639 | reason, |
| 640 | raw: version.version.clone(), |
| 641 | }, |
| 642 | ); |
| 643 | } |
| 644 | |
| 645 | if version.source_type == "npm" { |
| 646 | let plan = match crate::install::dsh::plan_npm_install( |
| 647 | name, |
| 648 | &version.version, |
| 649 | version.registry.as_deref(), |
| 650 | ) { |
| 651 | Ok(plan) => MarketplaceInstallPlan::Supported { |
| 652 | spec: plan.spec, |
| 653 | source_kind: "npm registry tarball".to_string(), |
| 654 | }, |
| 655 | Err(reason) => MarketplaceInstallPlan::Unsupported { |
| 656 | reason, |
| 657 | raw: version.version.clone(), |
| 658 | }, |
| 659 | }; |
| 660 | return ( |
| 661 | MarketplaceSourceSpec::Npm { |
| 662 | package: name.to_string(), |
| 663 | }, |
| 664 | plan, |
| 665 | ); |
| 666 | } |
| 667 | |
| 668 | let url = version.git_url.clone().unwrap_or_default(); |
| 669 | let Some(commit) = version.git_commit.as_deref() else { |
| 670 | return ( |
| 671 | MarketplaceSourceSpec::GitUrl { url }, |
| 672 | MarketplaceInstallPlan::Unsupported { |
| 673 | reason: "a DSH git source must pin an exact 40-hex commit SHA".to_string(), |
| 674 | raw: version.version.clone(), |
| 675 | }, |
| 676 | ); |
| 677 | }; |
| 678 | let Some((owner, repo)) = super::github_url_parts(&url) else { |
| 679 | return ( |
| 680 | MarketplaceSourceSpec::GitUrl { url }, |
| 681 | MarketplaceInstallPlan::Unsupported { |
| 682 | reason: "Codewhale installs DSH bundles from GitHub repositories; this source is not a GitHub repository" |
| 683 | .to_string(), |
| 684 | raw: version.version.clone(), |
| 685 | }, |
| 686 | ); |
| 687 | }; |
| 688 | let sha_valid = commit.len() == 40 && commit.bytes().all(|b| b.is_ascii_hexdigit()); |
| 689 | let spec = format!("git:github.com/{owner}/{repo}@{commit}"); |
| 690 | if version.git_path.is_some() |
| 691 | || !sha_valid |
| 692 | || crate::install::PluginInstallSource::parse(&spec).is_err() |
| 693 | { |
| 694 | return ( |
| 695 | MarketplaceSourceSpec::GitHub { |
| 696 | owner, |
| 697 | repo, |
| 698 | git_ref: None, |
| 699 | sha: Some(commit.to_string()), |
| 700 | }, |
| 701 | MarketplaceInstallPlan::Unsupported { |
| 702 | reason: if version.git_path.is_some() { |
| 703 | "this version lives in a repository subdirectory; Codewhale installs a whole repository and cannot select the package directory" |
| 704 | .to_string() |
| 705 | } else { |
| 706 | "the git source cannot be installed exactly as declared (the commit must be a 40-hex SHA)".to_string() |
| 707 | }, |
| 708 | raw: version.version.clone(), |
| 709 | }, |
| 710 | ); |
| 711 | } |
| 712 | ( |
| 713 | MarketplaceSourceSpec::GitHub { |
| 714 | owner, |
| 715 | repo, |
| 716 | git_ref: None, |
| 717 | sha: Some(commit.to_string()), |
| 718 | }, |
| 719 | MarketplaceInstallPlan::Supported { |
| 720 | spec, |
| 721 | source_kind: "GitHub repository".to_string(), |
| 722 | }, |
| 723 | ) |
| 724 | } |
| 725 |