web/src/pages/api/line-numbers.json.ts
96 lines · 3124 bytes
1import type { APIRoute } from 'astro';
2import yaml from 'js-yaml';
3
4import type { AwesomePrivacy, Service, Category } from '../../types/Service';
5
6interface LineNumberRange {
7 start: number;
8 end: number;
9}
10
11interface LineNumberData {
12 [category: string]: {
13 [section: string]: {
14 [service: string]: {
15 lineNumbers: LineNumberRange | null;
16 yaml: string;
17 }
18 };
19 };
20}
21
22const awesomePrivacyYamlPath = 'https://raw.githubusercontent.com/Lissy93/awesome-privacy/main/awesome-privacy.yml';
23
24/**
25 * Given a service object and an array of string lines from the raw YAML
26 * Find the starting and ending line number for that service
27 */
28const calculateServiceRange = (service: Service, category: Category, yamlLines: string[]): LineNumberRange | null => {
29 const lookFor = `- name: ${service.name}`;
30 const categoryStart = yamlLines.findIndex(line => line.includes(category.name));
31 const start = yamlLines.slice(categoryStart).findIndex(line => line.includes(lookFor)) + categoryStart + 1;
32 if (start === -1) return null;
33 const detectEnd = (line: string) => {
34 return line.trim().length === 0
35 || line.startsWith(' - ')
36 || line.includes('- name:')
37 || line.includes('notableMentions:')
38 || line.includes('furtherInfo:')
39 || line.includes('wordOfWarning:')
40 }
41 const remainingLines = yamlLines.slice(start);
42 const end = start + remainingLines.findIndex(detectEnd);
43
44 return { start, end };
45}
46
47/**
48 * Given a service object, convert it into a correctly formatted YAML string
49 */
50const convertJsonIntoYaml = (service: Service): string => {
51 return yaml.dump(service);
52};
53
54/**
55 * Given the object representation of the YAML and the array of lines from the raw YAML
56 * Organize the data into a format that can be returned as JSON
57 */
58const makeResults = (yamlObject: AwesomePrivacy, yamlLines: string[]): LineNumberData => {
59 const organizedData: LineNumberData = {};
60 (yamlObject.categories || []).forEach((category) => {
61 organizedData[category.name] = {};
62 (category.sections || []).forEach((section) => {
63 organizedData[category.name][section.name] = {};
64 (section.services || []).forEach((service) => {
65 organizedData[category.name][section.name][service.name] = {
66 lineNumbers: calculateServiceRange(service, category, yamlLines),
67 yaml: convertJsonIntoYaml(service),
68 };
69 });
70 });
71 });
72 return organizedData;
73}
74
75export const GET: APIRoute = async () => {
76
77 // Fetch the raw YAML from the awesome-privacy repository
78 const yamlContent = await fetch(awesomePrivacyYamlPath)
79 .then(response => response.text())
80 .catch(error => {
81 return JSON.stringify({ error: "Failed to fetch YAML file", details: error });
82 });
83
84 // Array of lines from the raw YAML
85 const yamlLines: string[] = yamlContent.split('\n');
86
87 // Object representation of the YAML
88 const yamlObject = yaml.load(yamlContent) as AwesomePrivacy;
89
90 // Make results
91 const results = makeResults(yamlObject, yamlLines);
92
93 return new Response(
94 JSON.stringify(results), { headers: { 'content-type': 'application/json' } }
95 )
96}