Skip to content

Commit

Permalink
fix wrong route path for /static/json (#12)
Browse files Browse the repository at this point in the history
* change back to old behaviour

* synchronize .gitignore with .npmignore
  • Loading branch information
Uzlopak authored Oct 11, 2022
1 parent 2ee14f2 commit 399eb52
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# @fastify/swagger-ui specific
/static

# Logs
logs
*.log
Expand Down
122 changes: 110 additions & 12 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,50 +1,148 @@
# @fastify/swagger-ui specific
# /static

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency files and directories
node_modules
jspm_packages
yarn.lock
# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# mac files
.DS_Store
# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# vim swap files
# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Vim swap files
*.swp

# macOS files
.DS_Store

# lock files
package-lock.json
pnpm-lock.yaml
yarn.lock

# project specific
*.tgz
.idea
# editor files
.vscode
.idea
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fp = require('fastify-plugin')

function fastifySwaggerUi (fastify, opts, next) {
fastify.decorate('swaggerCSP', require('./dist/csp.json'))
fastify.decorate('swaggerCSP', require('./static/csp.json'))

const baseDir = opts.baseDir
const prefix = opts.routePrefix || '/documentation'
Expand Down
4 changes: 2 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getRedirectPathForTheRootRoute (url) {
function fastifySwagger (fastify, opts, done) {
let staticCSP = false
if (opts.staticCSP === true) {
const csp = require('../dist/csp.json')
const csp = require('../static/csp.json')
staticCSP = `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`
}
if (typeof opts.staticCSP === 'string') {
Expand Down Expand Up @@ -116,7 +116,7 @@ function fastifySwagger (fastify, opts, done) {

// serve swagger-ui with the help of @fastify/static
fastify.register(fastifyStatic, {
root: path.join(__dirname, '..', 'dist'),
root: path.join(__dirname, '..', 'static'),
prefix: staticPrefix,
decorateReply: false
})
Expand Down
20 changes: 11 additions & 9 deletions scripts/prepare-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const crypto = require('crypto')
const swaggerUiAssetPath = require('swagger-ui-dist').getAbsoluteFSPath()
const resolve = require('path').resolve

fse.emptyDirSync(resolve('./dist'))
const folderName = 'static'

fse.emptyDirSync(resolve(`./${folderName}`))

// since the original swagger-ui-dist folder contains non UI files
const filesToCopy = [
Expand All @@ -24,10 +26,10 @@ const filesToCopy = [
'swagger-ui.js.map'
]
filesToCopy.forEach(filename => {
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./dist/${filename}`))
fse.copySync(`${swaggerUiAssetPath}/${filename}`, resolve(`./static/${filename}`))
})

fse.writeFileSync(resolve('./dist/swagger-initializer.js'), `window.onload = function () {
fse.writeFileSync(resolve(`./${folderName}/swagger-initializer.js`), `window.onload = function () {
function resolveUrl (url) {
const anchor = document.createElement('a')
anchor.href = url
Expand All @@ -36,7 +38,7 @@ fse.writeFileSync(resolve('./dist/swagger-initializer.js'), `window.onload = fun
function resolveConfig (cb) {
return fetch(
resolveUrl('./uiConfig').replace('dist/uiConfig', 'uiConfig')
resolveUrl('./uiConfig').replace('${folderName}/uiConfig', 'uiConfig')
)
.then(res => res.json())
.then((config) => {
Expand All @@ -52,7 +54,7 @@ fse.writeFileSync(resolve('./dist/swagger-initializer.js'), `window.onload = fun
],
layout: "StandaloneLayout"
}, config, {
url: resolveUrl('./json').replace('dist/json', 'json'),
url: resolveUrl('./json').replace('${folderName}/json', 'json'),
oauth2RedirectUrl: resolveUrl('./oauth2-redirect.html')
});
return cb(resConfig);
Expand All @@ -64,7 +66,7 @@ fse.writeFileSync(resolve('./dist/swagger-initializer.js'), `window.onload = fun
const ui = SwaggerUIBundle(config)
window.ui = ui
fetch(resolveUrl('./initOAuth').replace('dist/initOAuth', 'initOAuth'))
fetch(resolveUrl('./initOAuth').replace('${folderName}/initOAuth', 'initOAuth'))
.then(res => res.json())
.then((config) => {
ui.initOAuth(config);
Expand Down Expand Up @@ -99,6 +101,6 @@ function computeCSPHashes (path) {
result = styleRegex.exec(indexSrc)
}
}
computeCSPHashes('./dist/index.html')
computeCSPHashes('./dist/oauth2-redirect.html')
fse.writeFileSync(resolve('./dist/csp.json'), JSON.stringify(sha))
computeCSPHashes(`./${folderName}/index.html`)
computeCSPHashes(`./${folderName}/oauth2-redirect.html`)
fse.writeFileSync(resolve(`./${folderName}/csp.json`), JSON.stringify(sha))
2 changes: 1 addition & 1 deletion test/csp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
schemaSecurity,
swaggerOption
} = require('../examples/options')
const csp = require('../dist/csp.json')
const csp = require('../static/csp.json')

test('staticCSP = undefined', async (t) => {
t.plan(3)
Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifyHelmet = require('@fastify/helmet')
const fastifySwaggerUi = require('../index')
const swaggerCSP = require('../dist/csp.json')
const swaggerCSP = require('../static/csp.json')

test('fastify will response swagger csp', async (t) => {
t.plan(1)
Expand Down
12 changes: 6 additions & 6 deletions test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'index.html'),
resolve(__dirname, '..', 'static', 'index.html'),
'utf8'
),
res.payload
Expand All @@ -363,7 +363,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'swagger-initializer.js'),
resolve(__dirname, '..', 'static', 'swagger-initializer.js'),
'utf8'
),
res.payload
Expand All @@ -380,7 +380,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'text/html; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'oauth2-redirect.html'),
resolve(__dirname, '..', 'static', 'oauth2-redirect.html'),
'utf8'
),
res.payload
Expand All @@ -396,7 +396,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'text/css; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'swagger-ui.css'),
resolve(__dirname, '..', 'static', 'swagger-ui.css'),
'utf8'
),
res.payload
Expand All @@ -412,7 +412,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'swagger-ui-bundle.js'),
resolve(__dirname, '..', 'static', 'swagger-ui-bundle.js'),
'utf8'
),
res.payload
Expand All @@ -428,7 +428,7 @@ test('/documentation/static/:file should send back the correct file', async (t)
t.equal(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.equal(
readFileSync(
resolve(__dirname, '..', 'dist', 'swagger-ui-standalone-preset.js'),
resolve(__dirname, '..', 'static', 'swagger-ui-standalone-preset.js'),
'utf8'
),
res.payload
Expand Down
10 changes: 5 additions & 5 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ test('/documentation/:file should be served from custom location', async (t) =>
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml',
baseDir: resolve(__dirname, '..', 'dist')
baseDir: resolve(__dirname, '..', 'static')
}
}

const uiConfig = {
baseDir: resolve(__dirname, '..', 'dist')
baseDir: resolve(__dirname, '..', 'static')
}

t.plan(2)
Expand All @@ -218,7 +218,7 @@ test('/documentation/:file should be served from custom location', async (t) =>
url: '/documentation/oauth2-redirect.html'
})

const fileContent = readFileSync(resolve(__dirname, '..', 'dist', 'oauth2-redirect.html'), 'utf8')
const fileContent = readFileSync(resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8')
t.equal(res.statusCode, 200)
t.equal(
fileContent,
Expand All @@ -235,7 +235,7 @@ test('/documentation/:file should be served from custom location with trailing s
}

const uiConfig = {
baseDir: resolve(__dirname, '..', 'dist') + '/'
baseDir: resolve(__dirname, '..', 'static') + '/'
}

t.plan(2)
Expand All @@ -250,7 +250,7 @@ test('/documentation/:file should be served from custom location with trailing s

t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', 'dist', 'oauth2-redirect.html'), 'utf8'),
readFileSync(resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
})
Expand Down

0 comments on commit 399eb52

Please sign in to comment.