deno 2.9.7

Provides the deno executable
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2018-2026 the Deno authors. MIT license.

use deno_core::anyhow::Context;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_runtime::deno_fetch;
use deno_semver::jsr::JsrPackageReqReference;
use serde::de::DeserializeOwned;

use crate::http_util;
use crate::http_util::HttpClient;
use crate::util::console::escape_terminal_control_chars;

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateAuthorizationResponse {
  pub verification_url: String,
  pub code: String,
  pub exchange_token: String,
  pub poll_interval: u64,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExchangeAuthorizationResponse {
  pub token: String,
  pub user: User,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
  pub name: String,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OidcTokenResponse {
  pub value: String,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublishingTaskError {
  #[allow(dead_code, reason = "currently unused")]
  pub code: String,
  pub message: String,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublishingTask {
  pub id: String,
  pub status: String,
  pub error: Option<PublishingTaskError>,
}

#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiError {
  pub code: String,
  pub message: String,
  #[serde(flatten)]
  pub data: serde_json::Value,
  #[serde(skip)]
  pub x_deno_ray: Option<String>,
}

impl std::fmt::Display for ApiError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "{} ({})",
      escape_terminal_control_chars(&self.message),
      escape_terminal_control_chars(&self.code)
    )?;
    if let Some(x_deno_ray) = &self.x_deno_ray {
      write!(
        f,
        "[x-deno-ray: {}]",
        escape_terminal_control_chars(x_deno_ray)
      )?;
    }
    Ok(())
  }
}

impl std::fmt::Debug for ApiError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    std::fmt::Display::fmt(self, f)
  }
}

impl std::error::Error for ApiError {}

pub async fn parse_response<T: DeserializeOwned>(
  response: http::Response<deno_fetch::ResBody>,
) -> Result<T, ApiError> {
  let status = response.status();
  let x_deno_ray = response
    .headers()
    .get("x-deno-ray")
    .and_then(|value| value.to_str().ok())
    .map(|s| s.to_string());
  let text = http_util::body_to_string(response).await.unwrap();

  if !status.is_success() {
    match serde_json::from_str::<ApiError>(&text) {
      Ok(mut err) => {
        err.x_deno_ray = x_deno_ray;
        return Err(err);
      }
      Err(_) => {
        let err = ApiError {
          code: "unknown".to_string(),
          message: format!("{}: {}", status, text),
          x_deno_ray,
          data: serde_json::json!({}),
        };
        return Err(err);
      }
    }
  }

  serde_json::from_str(&text).map_err(|err| ApiError {
    code: "unknown".to_string(),
    message: format!("Failed to parse response: {}, response: '{}'", err, text),
    x_deno_ray,
    data: serde_json::json!({}),
  })
}

pub fn get_package_api_url(
  registry_api_url: &Url,
  scope: &str,
  package: &str,
) -> Result<Url, AnyError> {
  append_path_segments(
    registry_api_url,
    &["scopes", scope, "packages", package],
  )
}

pub fn get_package_version_api_url(
  registry_api_url: &Url,
  scope: &str,
  package: &str,
  version: &str,
  config_path: Option<&str>,
) -> Result<Url, AnyError> {
  let mut url = append_path_segments(
    registry_api_url,
    &["scopes", scope, "packages", package, "versions", version],
  )?;
  if let Some(config_path) = config_path {
    url.query_pairs_mut().append_pair("config", config_path);
  }
  Ok(url)
}

pub fn get_package_version_provenance_api_url(
  registry_api_url: &Url,
  scope: &str,
  package: &str,
  version: &str,
) -> Result<Url, AnyError> {
  append_path_segments(
    registry_api_url,
    &[
      "scopes",
      scope,
      "packages",
      package,
      "versions",
      version,
      "provenance",
    ],
  )
}

fn append_path_segments(
  base_url: &Url,
  segments: &[&str],
) -> Result<Url, AnyError> {
  let mut url = base_url.clone();
  url.set_query(None);
  url.set_fragment(None);
  url
    .path_segments_mut()
    .map_err(|_| {
      deno_core::anyhow::anyhow!(
        "Registry API URL cannot be used as a base URL"
      )
    })?
    .pop_if_empty()
    .extend(segments);
  Ok(url)
}

pub async fn get_package(
  client: &HttpClient,
  registry_api_url: &Url,
  scope: &str,
  package: &str,
  authorization: Option<&str>,
) -> Result<http::Response<deno_fetch::ResBody>, AnyError> {
  let package_url = get_package_api_url(registry_api_url, scope, package)?;
  let mut request = client.get(package_url)?;
  // The registry responds with a 404 for private packages unless the request
  // is authenticated as someone with access, so authenticate when we can.
  if let Some(authorization) = authorization {
    request = request.header(
      http::header::AUTHORIZATION,
      authorization
        .parse()
        .context("Failed to parse authorization header")?,
    );
  }
  let response = request.send().await?;
  Ok(response)
}

/// Splits a fully qualified JSR package name (e.g. `@scope/package`) into its
/// `(scope, package)` parts.
pub fn parse_package_name(name: &str) -> Result<(&str, &str), AnyError> {
  // Keep the explicit path-safety checks below even if the JSR grammar changes.
  let reference = JsrPackageReqReference::from_str(&format!("jsr:{name}@*"))
    .map_err(|_| {
      deno_core::anyhow::anyhow!(
        "package name must use the '@<scope>/<package>' format"
      )
    })?;
  if reference.sub_path().is_some() {
    bail!("package name must not contain additional path segments");
  }

  let Some((scope, package)) =
    name.strip_prefix('@').and_then(|name| name.split_once('/'))
  else {
    bail!("package name must use the '@<scope>/<package>' format");
  };
  for component in [scope, package] {
    if component == "." || component == ".." {
      bail!("package name must not contain dot path segments");
    }
    if component
      .chars()
      .any(|c| matches!(c, '/' | '\\' | '?' | '#' | '%'))
    {
      bail!("package name contains a URL path or delimiter character");
    }
  }
  Ok((scope, package))
}

/// Returns `true` if the given package version is already published to the
/// registry.
///
/// Only a `200 OK` response is treated as "already published". A `404` (and any
/// other non-success status) is treated as "not published" so that this
/// up-front optimization never blocks a legitimate publish because of a
/// transient registry error.
pub async fn check_version_exists(
  client: &HttpClient,
  registry_api_url: &Url,
  scope: &str,
  package: &str,
  version: &str,
) -> Result<bool, AnyError> {
  let url = get_package_version_api_url(
    registry_api_url,
    scope,
    package,
    version,
    None,
  )?;
  let response = client.get(url)?.send().await?;
  Ok(response.status() == 200)
}

pub fn get_jsr_alternative(imported: &Url) -> Option<String> {
  if matches!(imported.host_str(), Some("esm.sh")) {
    let mut segments = imported.path_segments()?;
    match segments.next()? {
      "gh" => None,
      module => Some(format!("\"npm:{module}\"")),
    }
  } else if imported.as_str().starts_with("https://deno.land/") {
    let mut segments = imported.path_segments()?;
    let maybe_std = segments.next()?;
    if maybe_std != "std" && !maybe_std.starts_with("std@") {
      return None;
    }
    let module = segments.next()?;
    let export = segments
      .next()
      .filter(|s| *s != "mod.ts")
      .map(|s| s.strip_suffix(".ts").unwrap_or(s).replace("_", "-"));
    Some(format!(
      "\"jsr:@std/{}@1{}\"",
      module,
      export.map(|s| format!("/{}", s)).unwrap_or_default()
    ))
  } else {
    None
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  fn test_jsr_alternative() {
    #[track_caller]
    fn run_test(imported: &str, output: Option<&str>) {
      let imported = Url::parse(imported).unwrap();
      let output = output.map(|s| s.to_string());
      assert_eq!(get_jsr_alternative(&imported), output);
    }

    run_test("https://esm.sh/ts-morph", Some("\"npm:ts-morph\""));
    run_test(
      "https://deno.land/std/path/mod.ts",
      Some("\"jsr:@std/path@1\""),
    );
    run_test(
      "https://deno.land/std/path/join.ts",
      Some("\"jsr:@std/path@1/join\""),
    );
    run_test(
      "https://deno.land/std@0.229.0/path/join.ts",
      Some("\"jsr:@std/path@1/join\""),
    );
    run_test(
      "https://deno.land/std@0.229.0/path/something_underscore.ts",
      Some("\"jsr:@std/path@1/something-underscore\""),
    );
  }

  #[test]
  fn test_parse_package_name() {
    assert_eq!(parse_package_name("@deno/doc").unwrap(), ("deno", "doc"));
    assert_eq!(
      parse_package_name("@scope-1/package-2").unwrap(),
      ("scope-1", "package-2")
    );
    assert_eq!(parse_package_name("@a/b").unwrap(), ("a", "b"));

    for invalid_name in [
      "deno/doc",
      "@deno",
      "@deno/../doc",
      "@deno/doc/other",
      "@deno/doc?other",
      "@deno/doc#other",
      "@deno/doc\\other",
      "@deno/doc%2Fother",
    ] {
      assert!(
        parse_package_name(invalid_name).is_err(),
        "{invalid_name} should be invalid"
      );
    }
  }

  #[test]
  fn package_api_urls_use_path_segments() {
    let base = Url::parse("https://registry.example/custom/api/").unwrap();
    let package_url = get_package_api_url(&base, "scope", "package").unwrap();
    assert_eq!(
      package_url.as_str(),
      "https://registry.example/custom/api/scopes/scope/packages/package"
    );

    let version_url = get_package_version_api_url(
      &base,
      "scope",
      "package",
      "1.2.3+build.1",
      Some("/deno.json"),
    )
    .unwrap();
    assert_eq!(
      version_url.path(),
      "/custom/api/scopes/scope/packages/package/versions/1.2.3+build.1"
    );
    assert_eq!(
      version_url.query_pairs().collect::<Vec<_>>(),
      vec![("config".into(), "/deno.json".into())]
    );

    let provenance_url = get_package_version_provenance_api_url(
      &base, "scope", "package", "1.2.3",
    )
    .unwrap();
    assert_eq!(
      provenance_url.as_str(),
      "https://registry.example/custom/api/scopes/scope/packages/package/versions/1.2.3/provenance"
    );
  }

  #[test]
  fn package_api_url_components_cannot_change_the_endpoint() {
    let base = Url::parse("https://registry.example/custom/api/").unwrap();
    let url = get_package_version_api_url(
      &base,
      "scope/../../other",
      "package?mode=other#fragment",
      "1.0.0/../../other",
      None,
    )
    .unwrap();
    assert_eq!(
      url.origin().ascii_serialization(),
      "https://registry.example"
    );
    assert_eq!(
      url.as_str(),
      "https://registry.example/custom/api/scopes/scope%2F..%2F..%2Fother/packages/package%3Fmode=other%23fragment/versions/1.0.0%2F..%2F..%2Fother"
    );
  }

  #[test]
  fn api_error_display_escapes_terminal_controls() {
    let err = ApiError {
      code: "bad\u{202e}code".to_string(),
      message: "failed\x1b[2J\nagain".to_string(),
      data: serde_json::json!({}),
      x_deno_ray: Some("ray\u{009b}31m".to_string()),
    };

    let display = err.to_string();
    assert_eq!(
      display,
      r"failed\u{1b}[2J\nagain (bad\u{202e}code)[x-deno-ray: ray\u{9b}31m]"
    );
    assert_eq!(format!("{err:?}"), display);
    assert_eq!(err.code, "bad\u{202e}code");
  }
}