st 1.2.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.travis.yml CHANGED
@@ -1,4 +1,13 @@
1
+ sudo: false
1
2
  language: node_js
2
3
  node_js:
3
- - node
4
- sudo: false
4
+ - '10'
5
+ - '12'
6
+ - lts/*
7
+ - current
8
+ branches:
9
+ only:
10
+ - master
11
+ notifications:
12
+ email:
13
+ - rod@vagg.org
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # st
2
2
 
3
+ [![Travis Status](https://api.travis-ci.org/isaacs/st.svg?branch=master)](https://travis-ci.org/isaacs/st)
4
+
3
5
  A module for serving static files. Does etags, caching, etc.
4
6
 
5
7
  ## USAGE
@@ -9,8 +11,8 @@ Here are some very simple usage examples.
9
11
  Just serve the files in the cwd at the root of the http server url:
10
12
 
11
13
  ```javascript
12
- var st = require('st')
13
- var http = require('http')
14
+ const st = require('st')
15
+ const http = require('http')
14
16
 
15
17
  http.createServer(
16
18
  st(process.cwd())
@@ -22,9 +24,11 @@ Serve the files in static under the /static url. Otherwise do a
22
24
  different thing:
23
25
 
24
26
  ```javascript
25
- var mount = st({ path: __dirname + '/static', url: '/static' })
26
- http.createServer(function(req, res) {
27
- var stHandled = mount(req, res);
27
+ const path = require('path')
28
+ const mount = st({ path: path.join(__dirname, '/static'), url: '/static' })
29
+
30
+ http.createServer((req, res) => {
31
+ const stHandled = mount(req, res)
28
32
  if (stHandled)
29
33
  return
30
34
  else
@@ -35,11 +39,11 @@ http.createServer(function(req, res) {
35
39
  The same sort of thing, but using an express middleware style:
36
40
 
37
41
  ```javascript
38
- var mount = st({ path: __dirname + '/static', url: '/static' })
39
- http.createServer(function(req, res) {
40
- mount(req, res, function() {
41
- res.end('this is not a static file')
42
- })
42
+ const path = require('path')
43
+ const mount = st({ path: path.join(__dirname, '/static'), url: '/static' })
44
+
45
+ http.createServer((req, res) => {
46
+ mount(req, res, () => res.end('this is not a static file'))
43
47
  }).listen(1339)
44
48
  ```
45
49
 
@@ -48,8 +52,10 @@ Serve the files in static under the / url, but only if not some doing
48
52
  other thing:
49
53
 
50
54
  ```javascript
51
- var mount = st({ path: __dirname + '/static', url: '/' })
52
- http.createServer(function(req, res) {
55
+ const path = require('path')
56
+ const mount = st({ path: path.join(__dirname, '/static'), url: '/' })
57
+
58
+ http.createServer((req, res) => {
53
59
  if (shouldDoThing(req)) {
54
60
  doTheThing(req, res)
55
61
  } else {
@@ -62,12 +68,12 @@ Serve the files in static under the / url, but don't serve a 404 if
62
68
  the file isn't found, so that the rest of the app can handle it:
63
69
 
64
70
  ```javascript
65
- var mount = st({ path: __dirname + '/static', url: '/', passthrough: true })
66
- http.createServer(function(req, res) {
67
- mount(req, res, function() {
68
- res.end('this is not a static file');
69
- });
70
- }).listen(1341);
71
+ const path = require('path')
72
+ const mount = st({ path: path.join(__dirname, '/static'), url: '/', passthrough: true})
73
+
74
+ http.createServer((req, res) => {
75
+ mount(req, res, () => res.end('this is not a static file'))
76
+ }).listen(1341)
71
77
  ```
72
78
 
73
79
  Serve the files with
@@ -106,8 +112,8 @@ Here are all the options described with their defaults values and a
106
112
  few possible settings you might choose to use:
107
113
 
108
114
  ```javascript
109
- var st = require('st')
110
- var mount = st({
115
+ const st = require('st')
116
+ const mount = st({
111
117
  path: 'resources/static/', // resolved against the process cwd
112
118
  url: 'static/', // defaults to '/'
113
119
 
@@ -160,7 +166,7 @@ var mount = st({
160
166
  })
161
167
 
162
168
  // with bare node.js
163
- http.createServer(function (req, res) {
169
+ http.createServer((req, res) => {
164
170
  if (mount(req, res)) return // serving a static file
165
171
  myCustomLogic(req, res)
166
172
  }).listen(PORT)
@@ -168,7 +174,7 @@ http.createServer(function (req, res) {
168
174
  // with express
169
175
  app.use(mount)
170
176
  // or
171
- app.route('/static/:fooblz', function (req, res, next) {
177
+ app.route('/static/:fooblz', (req, res, next) => {
172
178
  mount(req, res, next) // will call next() if it doesn't do anything
173
179
  })
174
180
  ```
package/bin/server.js CHANGED
@@ -1,18 +1,17 @@
1
1
  #!/usr/bin/env node
2
- var st = require('../st.js')
3
- var http = require('http')
4
- var port = +(process.env.PORT || 1337)
5
- var host = undefined
6
- var dir = ''
7
- var url = '/'
8
- var cacheSize = 0
9
- var dot = false
10
- var index = true
11
- var cache = true
12
- var age = null
13
- var cors = false
14
-
15
- for (var i = 2; i < process.argv.length; i++) {
2
+ const st = require('../st.js')
3
+ const http = require('http')
4
+ let port = +(process.env.PORT || 1337)
5
+ let host
6
+ let dir = ''
7
+ let url = '/'
8
+ let dot = false
9
+ let index = true
10
+ let cache = true
11
+ let age = null
12
+ let cors = false
13
+
14
+ for (let i = 2; i < process.argv.length; i++) {
16
15
  switch (process.argv[i]) {
17
16
  case '-p':
18
17
  case '--port':
@@ -45,9 +44,11 @@ for (var i = 2; i < process.argv.length; i++) {
45
44
  case '-.':
46
45
  case '--dot':
47
46
  dot = process.argv[++i]
48
- if (dot === undefined || dot === 'true') dot = true
49
- else if (dot === 'false') dot = false
50
- else if (dot.charAt(0) === '-') {
47
+ if (dot === undefined || dot === 'true') {
48
+ dot = true
49
+ } else if (dot === 'false') {
50
+ dot = false
51
+ } else if (dot.charAt(0) === '-') {
51
52
  --i
52
53
  dot = true
53
54
  }
@@ -61,9 +62,11 @@ for (var i = 2; i < process.argv.length; i++) {
61
62
  case '-i':
62
63
  case '--index':
63
64
  index = process.argv[++i]
64
- if (index === undefined || index === 'true') index = true
65
- if (index === 'false') index = false
66
- if (index.charAt(0) === '-') {
65
+ if (index === undefined || index === 'true') {
66
+ index = true
67
+ } else if (index === 'false') {
68
+ index = false
69
+ } else if (index.charAt(0) === '-') {
67
70
  --i
68
71
  index = true
69
72
  }
@@ -95,54 +98,56 @@ for (var i = 2; i < process.argv.length; i++) {
95
98
 
96
99
  case '-co':
97
100
  case '--cors':
98
- cors = true;
101
+ cors = true
99
102
  break
100
103
  }
101
104
  }
102
105
 
103
106
  function help () {
104
107
  console.log(
105
- ['st'
106
- ,'Static file server in node'
107
- ,''
108
- ,'Options:'
109
- ,''
110
- ,'-h --help Show this help'
111
- ,''
112
- ,'-p --port PORT Listen on PORT (default=1337)'
113
- ,''
114
- ,'-H --host HOST Bind address HOST (default=*)'
115
- ,''
116
- ,'-l --localhost Same as "--host localhost"'
117
- ,''
118
- ,'-d --dir DIRECTORY Serve the contents of DIRECTORY (default=cwd)'
119
- ,''
120
- ,'-u --url /url Serve at this mount url (default=/)'
121
- ,''
122
- ,'-i --index [INDEX] Use the specified INDEX filename as the result'
123
- ,' when a directory is requested. Set to "true"'
124
- ,' to turn autoindexing on, or "false" to turn it'
125
- ,' off. If no INDEX is provided, then it will turn'
126
- ,' autoindexing on. (default=true)'
127
- ,''
128
- ,'-ni --no-index Same as "--index false"'
129
- ,''
130
- ,'-. --dot [DOT] Allow .files to be served. Set to "false" to'
131
- ,' disable.'
132
- ,''
133
- ,'-n. --no-dot Same as "--dot false"'
134
- ,''
135
- ,'-co --cors Enable CORS to serve files to any domain.'
136
- ,''
137
- ,'-nc --no-cache Turn off all caching.'
138
- ,''
139
- ,'-a --age AGE Max age (in ms) of cache entries.'
140
- ].join('\n'))
108
+ ['st',
109
+ 'Static file server in node',
110
+ '',
111
+ 'Options:',
112
+ '',
113
+ '-h --help Show this help',
114
+ '',
115
+ '-p --port PORT Listen on PORT (default=1337)',
116
+ '',
117
+ '-H --host HOST Bind address HOST (default=*)',
118
+ '',
119
+ '-l --localhost Same as "--host localhost"',
120
+ '',
121
+ '-d --dir DIRECTORY Serve the contents of DIRECTORY (default=cwd)',
122
+ '',
123
+ '-u --url /url Serve at this mount url (default=/)',
124
+ '',
125
+ '-i --index [INDEX] Use the specified INDEX filename as the result',
126
+ ' when a directory is requested. Set to "true"',
127
+ ' to turn autoindexing on, or "false" to turn it',
128
+ ' off. If no INDEX is provided, then it will turn',
129
+ ' autoindexing on. (default=true)',
130
+ '',
131
+ '-ni --no-index Same as "--index false"',
132
+ '',
133
+ '-. --dot [DOT] Allow .files to be served. Set to "false" to',
134
+ ' disable.',
135
+ '',
136
+ '-n. --no-dot Same as "--dot false"',
137
+ '',
138
+ '-co --cors Enable CORS to serve files to any domain.',
139
+ '',
140
+ '-nc --no-cache Turn off all caching.',
141
+ '',
142
+ '-a --age AGE Max age (in ms) of cache entries.'
143
+ ].join('\n'))
141
144
  }
142
145
 
143
- if (isNaN(port)) throw new Error('invalid port: '+port)
146
+ if (isNaN(port)) {
147
+ throw new Error('invalid port: ' + port)
148
+ }
144
149
 
145
- var opt = {
150
+ const opt = {
146
151
  path: dir,
147
152
  url: url,
148
153
  index: index,
@@ -161,27 +166,31 @@ if (cache === false) {
161
166
  opt.cache = false
162
167
  } else {
163
168
  if (age) {
164
- Object.keys(opt.cache).forEach(function (k) {
169
+ for (const k in opt.cache) {
165
170
  opt.cache[k].maxAge = age
166
- })
171
+ }
167
172
  }
168
173
  // maybe other cache-manipulating CLI flags?
169
174
  }
170
175
 
171
- var mount = st(opt)
176
+ const mount = st(opt)
172
177
 
173
178
  http.createServer(function (q, s) {
174
- if (mount(q, s)) return
179
+ if (mount(q, s)) {
180
+ return
181
+ }
175
182
  s.statusCode = 404
176
183
  s.end('not found')
177
- }).listen(port, host, function() {
178
- var addr = this.address()
179
- var port = addr.port
184
+ }).listen(port, host, function () {
185
+ const addr = this.address()
186
+ const port = addr.port
187
+
180
188
  if (!host) {
181
189
  host = addr.address
182
190
  }
183
191
  if (/:/.test(host)) {
184
192
  host = '[' + host + ']'
185
193
  }
194
+
186
195
  console.log('listening at http://' + host + ':' + port)
187
196
  })
package/package.json CHANGED
@@ -1,26 +1,28 @@
1
1
  {
2
2
  "name": "st",
3
- "version": "1.2.2",
3
+ "version": "2.0.0",
4
4
  "description": "A module for serving static files. Does etags, caching, etc.",
5
5
  "main": "st.js",
6
6
  "bin": "bin/server.js",
7
7
  "dependencies": {
8
- "async-cache": "~1.1.0",
9
- "bl": "~1.2.1",
8
+ "async-cache": "^1.1.0",
9
+ "bl": "^4.0.0",
10
10
  "fd": "~0.0.2",
11
- "mime": "~1.4.1",
12
- "negotiator": "~0.6.1"
11
+ "mime": "^2.4.4",
12
+ "negotiator": "~0.6.2"
13
13
  },
14
14
  "optionalDependencies": {
15
- "graceful-fs": "~4.1.11"
15
+ "graceful-fs": "^4.2.3"
16
16
  },
17
17
  "devDependencies": {
18
- "request": "~2.83.0",
19
- "rimraf": "~2.6.2",
20
- "tap": "~10.7.2"
18
+ "request": "^2.88.0",
19
+ "rimraf": "^3.0.0",
20
+ "standard": "^14.3.1",
21
+ "tap": "^14.9.2"
21
22
  },
22
23
  "scripts": {
23
- "test": "tap test/*.js test/cli/*-test.js"
24
+ "lint": "standard",
25
+ "test": "npm run lint && tap test/*.js test/cli/*-test.js"
24
26
  },
25
27
  "repository": {
26
28
  "type": "git",