diff options
Diffstat (limited to 'node_modules/@shellscape/koa-static/index.js')
| -rw-r--r-- | node_modules/@shellscape/koa-static/index.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/node_modules/@shellscape/koa-static/index.js b/node_modules/@shellscape/koa-static/index.js new file mode 100644 index 00000000..02355ef0 --- /dev/null +++ b/node_modules/@shellscape/koa-static/index.js @@ -0,0 +1,73 @@ + +'use strict' + +/** + * Module dependencies. + */ + +const debug = require('debug')('koa-static') +const { resolve } = require('path') +const assert = require('assert') +const send = require('koa-send') + +/** + * Expose `serve()`. + */ + +module.exports = serve + +/** + * Serve static files from `root`. + * + * @param {String} root + * @param {Object} [opts] + * @return {Function} + * @api public + */ + +function serve (root, opts) { + opts = Object.assign({}, opts) + + assert(root, 'root directory is required to serve files') + + // options + debug('static "%s" %j', root, opts) + opts.root = resolve(root) + if (opts.index !== false) opts.index = opts.index || 'index.html' + + if (!opts.defer) { + return async function serve (ctx, next) { + let done = false + + if (ctx.method === 'HEAD' || ctx.method === 'GET') { + try { + done = await send(ctx, ctx.path, opts) + } catch (err) { + if (err.status !== 404) { + throw err + } + } + } + + if (!done) { + await next() + } + } + } + + return async function serve (ctx, next) { + await next() + + if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return + // response is already handled + if (ctx.body != null || ctx.status !== 404) return // eslint-disable-line + + try { + await send(ctx, ctx.path, opts) + } catch (err) { + if (err.status !== 404) { + throw err + } + } + } +} |
