
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
middleman-proxy
Advanced tools
HTTP proxy with content caching.
$ npm install --save middleman-proxy
const Middleman = require('middleman-proxy')
const proxy = new Middleman({
target: 'http://some.api.com',
maxAge: 3600000,
maxSize: '1MB'
})
.createKey((req, url) => `${req.method}:${req.session.id}:${url.path}`)
.listen(3000, () => {
console.log('Proxing "http://some.api.com" on port 3000')
})
By default, Middleman's cache is a Least-Recently-Used managed, in-memory cache, but it can just as easily work with any persistent store.
The Cache really manages an index of "keys" that are associated with "entries" in the store. The keys also contain the size in bytes of the associated entry, which allows the LRU work properly. Basically, when a key is evicted, a call is made to the store to deleted that entry. This allows the LRU to work even with out-of-memory stores. See Implementing a Store for more details.
The "store" is really just an interface, and a simple one at that.
Promise
, resolves the cache value if it exists, and null
if not.Promise
, resolves value
Promise
, resolves true
More than that, it's perfectly fine to resolve JSON strings; Middleman will automatically take care of parsing.
Recently, I have been working on a project that depended upon a very slow api; up to 40+ seconds in some instances. Due to circumstances, a standalone proxy was not an option. While making a hand-rolled solution, I thought it would be useful to have an in-application caching proxy solution for small apps. So I made Middleman!
const Middleman = require('middleman-proxy')
const app = require('express')()
const proxy = new Middleman({
target: 'http://some.api.com'
})
app.use(proxy.handler())
// OR
app.use((req, res) => {
// do some stuff ...
proxy.http(req, res)
});
app.get('/nameSpace', (req, res) => {
proxy.http(req, res, {
stripPrefix: '/nameSpace',
basePath: '/someBasePath'
})
// GET /nameSpace/path?foo=bar#baz
// => (Proxy) GET http://some.api.com/someBasePath/path?foo=bar#baz
})
// OR
app.get('/nameSpace', proxy.handler({
stripPrefix: '/nameSpace',
basePath: '/someBasePath'
}))
const Middleman = require('middleman-proxy')
const proxy = new Middleman({
target: 'http://some.api.com',
setHeaders: {
'X-API-Key': `${API_KEY}`,
'Authorization': `Bearer ${getAccessToken()}`
}
})
.listen(3000)
const Middleman = require('middleman-proxy')
const proxy = new Middleman({
target: 'http://some.api.com',
ignoreHeaders: [
'X-Some-Header'
]
})
const Middleman = require('middleman-proxy')
const proxy = new Middleman({
target: 'http://some.api.com'
})
.on('request', (req, res) => {
// For every request
res.setHeader('X-Always', 'true')
})
.on('proxy request', (req, res) => {
// For requests being proxied
res.setHeader('X-Proxied', 'true')
})
.on('cache request', (req, res) => {
// For requests with cached responses
res.setHeader('X-Cached', 'true')
})
{}
[]
'any'
Infinity
Infinity
. Note: If it is a string, it is parsed by the bytes
library, hence
values like '1KB'
or '13MB'
are perfectly acceptable.true
MemoryStore
. See
Implementing a Store for more details.true
.res
(instance of http.IncomingMessage)
which is a response from the proxied host, and returns a boolean; true
and the response is
not cached, false
and the response is cached. Default () => true
.req
(http.IncomingMessage)
and url
(Object) and returns a key
for the cache entry. Default
(req, url) => req.method + ':' + url.path
''
''
Handles a "request" event.const proxy = new Middleman({target: 'http://some.api.com'})
// ...
proxy.http(req, res, {
stripPrefix: '/namespace',
basePath: '/someBasePath'
})
// GET /namespace/path?foo=bar#baz => http://some.api.com/someBasePath/path?foo=bar#baz
Returns Middleman#http() bound with the instances context. See Middleman#http.
Populates the instances server
property with an instance of http.Server
, and
binds to the port
.
instance.listen(3000, () => {
console.log('Middleman instance is now serving on port 3000')
})
createKey
instance
.createKey((req, url) => {
return `${req.method}:${req.session.id}:${url.path}`
})
.listen(3000)
bypass
instance
.bypass((res) => {
if (res.statusCode < 300) {
return false // this response is cached
} else {
return true // not caching this one
}
})
.listen(3000)
httpError
instance
.httpError((req, res) => {
res.statusCode = 500
res.end('Whoops! Something blew up...')
})
.listen(3000)
MIT
FAQs
HTTP Proxy with content caching.
We found that middleman-proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.