diff --git a/package.json b/package.json index b659cfd..2cfd6bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tf-docs", - "version": "0.0.1", + "version": "0.0.2", "description": "Terraform docs service (S3 static /docs) — initial skeleton", "main": "server.js", "scripts": { diff --git a/server.js b/server.js index d3e4c5a..62bb54b 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,7 @@ const path = require('path'); const { S3Client, GetObjectCommand, HeadBucketCommand } = require('@aws-sdk/client-s3'); const PORT = process.env.PORT || 3000; -const VERSION = process.env.APP_VERSION || '0.0.1'; +const VERSION = process.env.APP_VERSION || '0.0.2'; const SERVICE = 'tf-docs'; const S3_BUCKET = process.env.S3_BUCKET || ''; const DOCS_S3_PREFIX = (process.env.DOCS_S3_PREFIX || 'docs').replace(/^\/+|\/+$/g, ''); @@ -22,7 +22,7 @@ const s3 = new S3Client({ }); function getDocsCandidates(urlPath) { - const relativePath = decodeURIComponent(urlPath.replace(/^\/docs\/?/, '')); + const relativePath = decodeURIComponent(urlPath.replace(/^\/+/, '')); const normalized = path.posix.normalize(`/${relativePath}`).replace(/^\/+|\/+$/g, ''); if (!normalized || normalized === '.' || normalized.startsWith('../') || normalized.includes('/../')) { return null; @@ -106,23 +106,7 @@ const server = http.createServer((req, res) => { health(req, res); return; } - if (req.url.startsWith('/docs/')) { - docsHandler(req, res); - return; - } - res.statusCode = 200; - res.setHeader('Content-Type', 'text/html; charset=utf-8'); - res.end(` - - - - ${SERVICE} - - -

${SERVICE}

-

version: ${VERSION}

- -`); + docsHandler(req, res); }); if (require.main === module) { diff --git a/test/server.test.js b/test/server.test.js index 3fe1db2..663985b 100644 --- a/test/server.test.js +++ b/test/server.test.js @@ -22,21 +22,21 @@ async function withServer(fn) { test('candidates: trailing slash -> index.html', () => { assert.deepStrictEqual( - getDocsCandidates('/docs/nubes/nubes/1.0.0/'), + getDocsCandidates('/nubes/nubes/1.0.0/'), ['docs/nubes/nubes/1.0.0/index.html'] ); }); test('candidates: file with extension -> exact + root index fallback', () => { assert.deepStrictEqual( - getDocsCandidates('/docs/nubes/nubes/1.0.0/page.html'), + getDocsCandidates('/nubes/nubes/1.0.0/page.html'), ['docs/nubes/nubes/1.0.0/page.html', 'docs/nubes/nubes/1.0.0/index.html'] ); }); test('candidates: directory-style path -> exact + index.html + root index', () => { assert.deepStrictEqual( - getDocsCandidates('/docs/nubes/nubes/1.0.0/guides/getting-started'), + getDocsCandidates('/nubes/nubes/1.0.0/guides/getting-started'), [ 'docs/nubes/nubes/1.0.0/guides/getting-started', 'docs/nubes/nubes/1.0.0/guides/getting-started/index.html', @@ -91,7 +91,7 @@ test('GET /docs/... -> 200 streams body with content-type', async () => { return { $metadata: { httpStatusCode: 200 } }; }; await withServer(async (base) => { - const res = await fetch(`${base}/docs/nubes/nubes/1.0.0/`); + const res = await fetch(`${base}/nubes/nubes/1.0.0/`); assert.strictEqual(res.status, 200); assert.match(res.headers.get('content-type') || '', /text\/html/); assert.strictEqual(await res.text(), 'ok'); @@ -110,7 +110,7 @@ test('GET /docs/... -> 404 when no key found', async () => { return { $metadata: { httpStatusCode: 200 } }; }; await withServer(async (base) => { - const res = await fetch(`${base}/docs/nubes/nubes/1.0.0/missing`); + const res = await fetch(`${base}/nubes/nubes/1.0.0/missing`); assert.strictEqual(res.status, 404); }); s3.send = originalSend;