From 26105034da4fcce7ac883c899d781f016559310d Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 8 Nov 2018 00:38:48 +0800 Subject: switch to vuepress --- node_modules/@shellscape/koa-static/index.js | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 node_modules/@shellscape/koa-static/index.js (limited to 'node_modules/@shellscape/koa-static/index.js') 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 + } + } + } +} -- cgit v1.2.3