Gathering detailed insights and metrics for connect-static-transform
Gathering detailed insights and metrics for connect-static-transform
Gathering detailed insights and metrics for connect-static-transform
Gathering detailed insights and metrics for connect-static-transform
A connect middleware which allows transformation of static files before serving them.
npm install connect-static-transform
Typescript
Module System
Node Version
NPM Version
70.9
Supply Chain
98.3
Quality
75.6
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
195,256
Last Day
1
Last Week
49
Last Month
285
Last Year
4,044
MIT License
15 Stars
78 Commits
4 Forks
3 Watchers
2 Branches
7 Contributors
Updated on Oct 08, 2023
Minified
Minified + Gzipped
Latest Version
0.9.2
Package Id
connect-static-transform@0.9.2
Unpacked Size
21.38 kB
Size
6.50 kB
File Count
10
NPM Version
6.7.0
Node Version
11.10.1
Cumulative downloads
Total Downloads
Last Day
-83.3%
1
Compared to previous day
Last Week
-39.5%
49
Compared to previous week
Last Month
-20.4%
285
Compared to previous month
Last Year
24.9%
4,044
Compared to previous year
connect-static-transform
is a middleware for Connect and systems based on Connect such as Express. It allows you to serve static files but gives you an opportunity to transform the content of the files before they are sent to the client (e.g., compiling .coffee
files or compiling .styl
files).
There are a few examples available in example/app.js
. Consider the following simple example:
1// Dependencies 2var http = require('http'), 3 connect = require('connect'), 4 st = require('connect-static-transform'); 5 6// Middleware which serves .txt files in all uppercase 7var toUpperCase = st({ 8 root: __dirname, 9 match: /.+\.txt/, 10 transform: function (path, text, send) { 11 send(text.toUpperCase(), {'Content-Type': 'text/plain'}); 12 } 13}); 14 15// Create application which uses middleware 16var app = connect().use(toUpperCase); 17 18// Create server and listen 19http.createServer(app).listen(3000);
This shows the basic usage. st
acts as a factory which creates middleware for use in a Connect-like system. The above example will serve all files in __dirname
matching /.+\.txt/
in all uppercase letters.
By running node examples/standardExample.js
you can access http://localhost:3000/file.txt
which serves examples/file.txt
in all uppercase or you can access http://localhost:3000/file.css
which serves examples/file.styl
compiled into compressed css.
By running node examples/factoriesExample.js
you can access http://localhost:3000/css/file.css
which serves examples/file.styl
compiled into compressed css or you can access http://localhost:3000/js/file.js
which serves examples/file.coffee
compiled into compressed JavaScript. See documentation below for examples on how to use the built-in middleware factories.
st
takes a single argument: an object containing all configuration options.
root
This option specifies which directory the static files should come from.
match
This option should be a regular expression which matches the full path of a file as given to the server from the client.
transform
This option should be a function which may operate asynchronously. Three arguments are passed to transform
. The first argument is the path to the file which was opened. The second argument is the data from the file which was opened. The third argument is a callback to which the transformed data should be passed. If the argument to the callback is false
or otherwise untruthy then the next middleware in the Connect application is invoked. The callback function also accepts a second parameter of headers to use in the response -- it is highly recommended (but not required) that this argument is set (see example usage above).
normalize
This option, if presented, will alter the path to the file being opened before it is opened. For example, if the client requested script.js
and you wanted to open script.coffee
for compilation then there are two things you can do with normalize
.
First, you can pass a string which follows regular expression replacement syntax. If match
were set to /(.+)\.js
, you set normalize to '$1.coffee'
. In this case, a request to script.js
will result in script.coffee
being opened for transformation. match
must have capture groups in order for this to work.
Second, you can pass a function which given the path returns the path of the file to open. For example, to accomplish the same as the first example normalize
could be set as such:
1var compileCoffee = st({ 2 // other options 3 normalize: function (path) { 4 return path.substring(0, path.length - 2) + 'coffee'; 5 }, 6 // other options 7});
pathOnly
If true
then the signature for the transformation function becomes function (path, send) { }
, i.e., the file is not opened by connect-static-transform
.
cache
If set to true
or an otherwise truthy value, the transformed data for each path will be cached in memory. Appropriate cache headers will also be set on the HTTP response.
maxage
Used in conjunction with cache
, this indicates the maximum age in seconds a client should keep the file cached for. This will not expire the local in-memory cache.
expires
Optionally you can set the Expires
response header. Defaults to 1 year.
encoding
The encoding of the files which are opened for transformation. Defaults to 'utf-8'
. If set to 'buffer'
then the transformation function will receive a raw data buffer (see fs.readFile(...)
).
connect-static-transform
includes factory functions for common use-cases. Instead of manually creating middleware using the st
function as above, you can simply use the factories outlined in this section.
To use the Stylus middleware factory you must have Stylus installed in your project. If you have nib installed in your project it will be automatically included so you can use it. From there, you can create a Stylus middleware using st.stylus(options)
. See the following example:
1// If you have a file `foo.styl` in `__dirname` then you can access the compiled css at the url `/css/foo.css`: 2app.use(st.stylus({ 3 root: __dirname, // where to open the styl files from 4 path: '/css', // optional, sets where to serve from 5 compress: true, // optional 6 cache: true, // optional, caches in memory as well as on the client 7 maxage: 3600 // optional, sets the maximum number of seconds a client should keep the compiled file (defaults to one year) 8}));
To use the Less middleware factory you must have LESS installed in your project. From there, you can create a LESS middleware using st.less(options)
. See the following example:
1// If you have a file `foo.less` in `__dirname` then you can access the compiled css at the url `/css/foo.css`: 2app.use(st.less({ 3 root: __dirname, // where to open the less files from 4 path: '/css', // optional, sets where to serve from 5 cache: true, // optional, caches in memory as well as on the client 6 maxage: 3600, // optional, sets the maximum number of seconds a client should keep the compiled file (defaults to one year) 7 options: { // optional, options object to send directly to the LESS compiler 8 compress: true 9 } 10}));
To use the CoffeeScript middleware factory you must have Snockets and Coffee-Script installed locally. Compilation is handled by Snockets. You can create a CoffeeScript middleware using st.coffee(options)
. See the following examples:
1// If you have a file `foo.coffee` in `__dirname` then you can access the compiles javascript at the url `/js/foo.js`: 2app.use(st.coffee({ 3 root: __dirname, // where to open the coffee files from 4 path: '/js', // optional, sets where to serve from 5 cache: true, // optional, caches in memory as well as on the client 6 maxage: 3600, // optional, sets the maximum number of seconds a client should keep the compiled file (defaults to one year) 7 // these options are passed to Snockets: 8 options: { 9 minify: true 10 } 11}));
The MIT License
Copyright (c) 2013 Kenneth Powers
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
29 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More