aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@shellscape/koa-static/index.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/@shellscape/koa-static/index.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/@shellscape/koa-static/index.js')
-rw-r--r--node_modules/@shellscape/koa-static/index.js73
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
+ }
+ }
+ }
+}