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
5 changes: 4 additions & 1 deletion src/server/plugin/PatchHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export class PatchHtml implements IPluginMiddleware {
private insertTags = (req: Request, html: string | Buffer): string => {
html = String(html)

if (!html.includes("__VERDACCIO_BASENAME_UI_OPTIONS")) {
if (
!html.includes("__VERDACCIO_BASENAME_UI_OPTIONS") &&
!html.includes("ui-options.js")
) {
return html
}

Expand Down
51 changes: 51 additions & 0 deletions test/server/plugin/PatchHtml/insertTags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from "vitest"
import { PatchHtml } from "src/server/plugin/PatchHtml"

const scriptPattern = /verdaccio-6\.js/

function createPatchHtml() {
const config = { url_prefix: "" } as any
return new PatchHtml(config)
}

function callInsertTags(html: string) {
const patchHtml = createPatchHtml()
const req = { headers: { host: "localhost:4873" }, protocol: "http" } as any
// Access the private method via bracket notation
return (patchHtml as any).insertTags(req, html)
}

// Verdaccio <=6.4 inlines the options in a <script> tag
const legacyHtml = `
<!DOCTYPE html>
<html><head>
<script>window.__VERDACCIO_BASENAME_UI_OPTIONS={"base":"/"}</script>
</head><body><div id="root"></div></body></html>
`

// Verdaccio >=6.5 loads options from an external script
const modernHtml = `
<!DOCTYPE html>
<html><head>
<script src="/-/static/ui-options.js"></script>
</head><body><div id="root"></div></body></html>
`

const jsonResponse = JSON.stringify({ name: "some-package", version: "1.0.0" })

describe("PatchHtml - insertTags", () => {
it("injects the client script into legacy Verdaccio HTML (<=6.4)", () => {
const result = callInsertTags(legacyHtml)
expect(result).toMatch(scriptPattern)
})

it("injects the client script into modern Verdaccio HTML (>=6.5)", () => {
const result = callInsertTags(modernHtml)
expect(result).toMatch(scriptPattern)
})

it("does not modify non-HTML responses", () => {
const result = callInsertTags(jsonResponse)
expect(result).toBe(jsonResponse)
})
})
Loading