|
| 1 | +import Route from '@ember/routing/route'; |
| 2 | + |
| 3 | +import { didCancel } from 'ember-concurrency'; |
| 4 | + |
| 5 | +import { AjaxError } from '../../utils/ajax'; |
| 6 | + |
| 7 | +async function fetchAdvisories(crateId) { |
| 8 | + let url = `https://rustsec.org/packages/${crateId}.json`; |
| 9 | + let response = await fetch(url); |
| 10 | + if (response.status === 404) { |
| 11 | + return []; |
| 12 | + } else if (response.ok) { |
| 13 | + return await response.json(); |
| 14 | + } else { |
| 15 | + throw new Error(`HTTP error! status: ${response}`); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export default class SecurityRoute extends Route { |
| 20 | + queryParams = { |
| 21 | + sort: { refreshModel: true }, |
| 22 | + }; |
| 23 | + |
| 24 | + async model() { |
| 25 | + let crate = this.modelFor('crate'); |
| 26 | + try { |
| 27 | + let [advisories, micromarkModule, gfmModule] = await Promise.all([ |
| 28 | + fetchAdvisories(crate.id), |
| 29 | + import('micromark'), |
| 30 | + import('micromark-extension-gfm'), |
| 31 | + ]); |
| 32 | + |
| 33 | + const convertMarkdown = markdown => { |
| 34 | + return micromarkModule.micromark(markdown, { |
| 35 | + extensions: [gfmModule.gfm()], |
| 36 | + htmlExtensions: [gfmModule.gfmHtml()], |
| 37 | + }); |
| 38 | + }; |
| 39 | + |
| 40 | + return { crate, advisories, convertMarkdown }; |
| 41 | + } catch (error) { |
| 42 | + // report unexpected errors to Sentry and ignore `ajax()` errors |
| 43 | + if (!didCancel(error) && !(error instanceof AjaxError)) { |
| 44 | + this.sentry.captureException(error); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + setupController(controller, { crate, advisories, convertMarkdown }) { |
| 50 | + super.setupController(...arguments); |
| 51 | + // reset when crate changes |
| 52 | + if (crate && crate !== controller.crate) { |
| 53 | + controller.reset(); |
| 54 | + } |
| 55 | + controller.crate = crate; |
| 56 | + controller.advisories = advisories; |
| 57 | + controller.convertMarkdown = convertMarkdown; |
| 58 | + } |
| 59 | +} |
0 commit comments