Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
822 changes: 794 additions & 28 deletions reposilite-frontend/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion reposilite-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"dev": "vite",
"eslint": "eslint \"src/**\"",
"test": "vitest run",
"build": "vite build",
"serve": "vite preview",
"fake-api": "nodemon fake-api/fake-ingot-backend.js",
Expand All @@ -24,7 +25,6 @@
"@unhead/vue": "^3.3.1",
"@vueform/toggle": "^2.1.4",
"@vueuse/core": "^14.3.0",
"ansi-to-html": "^0.7.2",
"axios": "^1.18.0",
"downloadjs": "^1.4.7",
"echarts": "^6.1.0",
Expand Down Expand Up @@ -54,12 +54,14 @@
"express-ws": "^5.0.2",
"globals": "^17.6.0",
"jsdoc": "^4.0.5",
"jsdom": "^30.1.2",
"json-server": "^0.17.4",
"license-checker-rseidelsohn": "^5.0.1",
"nodemon": "^3.1.14",
"rollup-plugin-visualizer": "^7.0.1",
"tailwindcss": "^4.3.3",
"vite": "^8.0.16",
"vitest": "^5.0.4",
"ws": "^8.21.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import XmlTag from './XmlTag.vue'
import CodeString from './CodeString.vue'
import CodeBrackets from "./CodeBrackets.vue"
import { computed, ref } from "vue";
import { computed, ref } from "vue"

defineProps({
configuration: {
Expand Down
7 changes: 5 additions & 2 deletions reposilite-frontend/src/components/console/ConsoleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ const chipClasses = (level) =>
<p
v-for="entry in log"
:key="entry.id"
v-html="entry.message"
class="whitespace-nowrap"
/>
><span
v-for="(segment, index) in entry.segments"
:key="index"
:class="segment.classes"
>{{ segment.text }}</span></p>

<!--
whitespace-normal on each of these: the panel around them keeps pre-wrap so log
Expand Down
98 changes: 98 additions & 0 deletions reposilite-frontend/src/store/console/ansi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2023 dzikoysk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Turns one console line into segments of plain text, each with a list of style classes.
*
* No HTML is generated. The view renders the text with interpolation, which escapes & < > " '
* itself, and every class comes from the fixed table below, so nothing from the log can end up
* in markup or in an attribute.
*/

export const COLOUR_CLASSES = {
30: 'text-gray-900 dark:text-gray-500',
31: 'text-red-700 dark:text-red-400',
32: 'text-green-700 dark:text-green-400',
33: 'text-yellow-700 dark:text-yellow-400',
34: 'text-blue-700 dark:text-blue-400',
35: 'text-fuchsia-700 dark:text-fuchsia-400',
36: 'text-cyan-700 dark:text-cyan-400',
37: 'text-gray-700 dark:text-gray-300',
90: 'text-gray-500',
91: 'text-red-600 dark:text-red-300',
92: 'text-green-600 dark:text-green-300',
93: 'text-yellow-600 dark:text-yellow-300',
94: 'text-blue-600 dark:text-blue-300',
95: 'text-fuchsia-600 dark:text-fuchsia-300',
96: 'text-cyan-600 dark:text-cyan-300',
97: 'text-gray-900 dark:text-white'
}

const ESC = '\u001b'
const CONTROL_SEQUENCE = new RegExp(`${ESC}\\[([0-9;]*)([A-Za-z])`, 'g')

const applySgr = (style, params) => {
const codes = params === '' ? [0] : params.split(';').map(Number)
for (let i = 0; i < codes.length; i++) {
const code = codes[i]
if (code === 0) {
style.bold = false
style.underline = false
style.colour = null
} else if (code === 1) {
style.bold = true
} else if (code === 22) {
style.bold = false
} else if (code === 4) {
style.underline = true
} else if (code === 24) {
style.underline = false
} else if (code === 39) {
style.colour = null
} else if (code in COLOUR_CLASSES) {
style.colour = code
} else if (code === 38 || code === 48) {
// Extended colours (5;n or 2;r;g;b) are not supported; skip their parameters.
if (codes[i + 1] === 5) i += 2
else if (codes[i + 1] === 2) i += 4
}
}
}

const classesOf = style => [
...(style.bold ? ['font-bold'] : []),
...(style.underline ? ['underline'] : []),
...(style.colour === null ? [] : [COLOUR_CLASSES[style.colour]])
]

export function parseAnsi(message) {
const segments = []
const style = { bold: false, underline: false, colour: null }
let position = 0

const pushText = text => {
if (text.length > 0) segments.push({ text, classes: classesOf(style) })
}

for (const match of message.matchAll(CONTROL_SEQUENCE)) {
pushText(message.slice(position, match.index))
if (match[2] === 'm') applySgr(style, match[1])
position = match.index + match[0].length
}
pushText(message.slice(position))

return segments
}
80 changes: 80 additions & 0 deletions reposilite-frontend/src/store/console/ansi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2023 dzikoysk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from 'vitest'
import { COLOUR_CLASSES, parseAnsi } from './ansi.js'

const ESC = '\u001b'

describe('parseAnsi', () => {
it('keeps an img tag payload as literal text', () => {
const segments = parseAnsi('<img src=x onerror=alert(1)>')

expect(segments).toEqual([{ text: '<img src=x onerror=alert(1)>', classes: [] }])
})

it('keeps an already escaped entity as literal text', () => {
const segments = parseAnsi('&amp;')

expect(segments).toEqual([{ text: '&amp;', classes: [] }])
})

it('keeps a double quote that tries to close an attribute as text', () => {
const segments = parseAnsi('" onmouseover="alert(1)')

expect(segments).toEqual([{ text: '" onmouseover="alert(1)', classes: [] }])
})

it('keeps a single quote as text', () => {
const segments = parseAnsi("it's")

expect(segments).toEqual([{ text: "it's", classes: [] }])
})

it('splits a line into coloured and plain segments at an SGR colour code', () => {
const segments = parseAnsi(`${ESC}[31mERROR${ESC}[0m done`)

expect(segments).toEqual([
{ text: 'ERROR', classes: [COLOUR_CLASSES[31]] },
{ text: ' done', classes: [] }
])
})

it('combines bold and colour in a single segment', () => {
const segments = parseAnsi(`${ESC}[1;32mok`)

expect(segments).toEqual([{ text: 'ok', classes: ['font-bold', COLOUR_CLASSES[32]] }])
})

it('treats a bare reset code as ending the colour', () => {
const segments = parseAnsi(`${ESC}[34mA${ESC}[mB`)

expect(segments).toEqual([
{ text: 'A', classes: [COLOUR_CLASSES[34]] },
{ text: 'B', classes: [] }
])
})

it('drops escape sequences that are not colour or style codes', () => {
const segments = parseAnsi(`${ESC}[2Kx`)

expect(segments).toEqual([{ text: 'x', classes: [] }])
})

it('returns no segments for an empty line', () => {
expect(parseAnsi('')).toEqual([])
})
})
17 changes: 5 additions & 12 deletions reposilite-frontend/src/store/console/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,17 @@
*/

import { computed, ref, reactive } from 'vue'
import Convert from 'ansi-to-html'
import { parseAnsi } from './ansi'

const levelNames = ['Other', 'Trace', 'Debug', 'Info', 'Warn', 'Error']
const levels = reactive({})
const filter = ref('')
const id = ref(0)
const rawLog = reactive([])
const convert = new Convert()

const getLevel = message =>
levelNames.find(level => message.includes(`${level.toUpperCase()} | `)) ?? 'Other'

const sanitizeMessage = (message) => convert.toHtml(
message
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll(' ', '&nbsp;')
)

export default function useLog() {
levelNames.forEach(level => {
levels[level] = {
Expand All @@ -45,12 +37,14 @@ export default function useLog() {

const log = computed(() => {
return rawLog
.filter(entry => entry.message.toLowerCase().includes(filter.value.toLowerCase()))
.filter(entry => entry.text.toLowerCase().includes(filter.value.toLowerCase()))
.filter(entry => levels[entry.level].enabled)
})

const logMessage = value => {
rawLog.push({ id: id.value++, message: sanitizeMessage(value), level: getLevel(value) })
const segments = parseAnsi(value)
const text = segments.map(segment => segment.text).join('')
rawLog.push({ id: id.value++, segments, text, level: getLevel(value) })
}

const clearLog = () => {
Expand All @@ -61,7 +55,6 @@ export default function useLog() {
levels,
log,
filter,
sanitizeMessage,
logMessage,
clearLog
}
Expand Down
23 changes: 23 additions & 0 deletions reposilite-frontend/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 dzikoysk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
environment: 'jsdom'
}
})
Loading