update
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import getSideChannelList from 'side-channel-list';
|
||||
import getSideChannelMap from 'side-channel-map';
|
||||
import getSideChannelWeakMap from 'side-channel-weakmap';
|
||||
|
||||
declare namespace getSideChannel {
|
||||
type Channel<K, V> =
|
||||
| getSideChannelList.Channel<K, V>
|
||||
| ReturnType<Exclude<typeof getSideChannelMap<K, V>, false>>
|
||||
| ReturnType<Exclude<typeof getSideChannelWeakMap<K, V>, false>>;
|
||||
}
|
||||
|
||||
declare function getSideChannel<K, V>(): getSideChannel.Channel<K, V>;
|
||||
|
||||
export = getSideChannel;
|
||||
@@ -0,0 +1,43 @@
|
||||
// TypeScript Version: 3.2
|
||||
|
||||
/// <reference types="node" lib="esnext" />
|
||||
|
||||
import * as fs from 'fs';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
declare namespace readdir {
|
||||
interface EntryInfo {
|
||||
path: string;
|
||||
fullPath: string;
|
||||
basename: string;
|
||||
stats?: fs.Stats;
|
||||
dirent?: fs.Dirent;
|
||||
}
|
||||
|
||||
interface ReaddirpOptions {
|
||||
root?: string;
|
||||
fileFilter?: string | string[] | ((entry: EntryInfo) => boolean);
|
||||
directoryFilter?: string | string[] | ((entry: EntryInfo) => boolean);
|
||||
type?: 'files' | 'directories' | 'files_directories' | 'all';
|
||||
lstat?: boolean;
|
||||
depth?: number;
|
||||
alwaysStat?: boolean;
|
||||
}
|
||||
|
||||
interface ReaddirpStream extends Readable, AsyncIterable<EntryInfo> {
|
||||
read(): EntryInfo;
|
||||
[Symbol.asyncIterator](): AsyncIterableIterator<EntryInfo>;
|
||||
}
|
||||
|
||||
function promise(
|
||||
root: string,
|
||||
options?: ReaddirpOptions
|
||||
): Promise<EntryInfo[]>;
|
||||
}
|
||||
|
||||
declare function readdir(
|
||||
root: string,
|
||||
options?: readdir.ReaddirpOptions
|
||||
): readdir.ReaddirpStream;
|
||||
|
||||
export = readdir;
|
||||
@@ -0,0 +1,84 @@
|
||||
'use strict';
|
||||
|
||||
var GetIntrinsic = require('get-intrinsic');
|
||||
var callBound = require('call-bound');
|
||||
var inspect = require('object-inspect');
|
||||
var getSideChannelMap = require('side-channel-map');
|
||||
|
||||
var $TypeError = require('es-errors/type');
|
||||
var $WeakMap = GetIntrinsic('%WeakMap%', true);
|
||||
|
||||
/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => V} */
|
||||
var $weakMapGet = callBound('WeakMap.prototype.get', true);
|
||||
/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K, value: V) => void} */
|
||||
var $weakMapSet = callBound('WeakMap.prototype.set', true);
|
||||
/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => boolean} */
|
||||
var $weakMapHas = callBound('WeakMap.prototype.has', true);
|
||||
/** @type {<K extends object, V>(thisArg: WeakMap<K, V>, key: K) => boolean} */
|
||||
var $weakMapDelete = callBound('WeakMap.prototype.delete', true);
|
||||
|
||||
/** @type {import('.')} */
|
||||
module.exports = $WeakMap
|
||||
? /** @type {Exclude<import('.'), false>} */ function getSideChannelWeakMap() {
|
||||
/** @typedef {ReturnType<typeof getSideChannelWeakMap>} Channel */
|
||||
/** @typedef {Parameters<Channel['get']>[0]} K */
|
||||
/** @typedef {Parameters<Channel['set']>[1]} V */
|
||||
|
||||
/** @type {WeakMap<K & object, V> | undefined} */ var $wm;
|
||||
/** @type {Channel | undefined} */ var $m;
|
||||
|
||||
/** @type {Channel} */
|
||||
var channel = {
|
||||
assert: function (key) {
|
||||
if (!channel.has(key)) {
|
||||
throw new $TypeError('Side channel does not contain ' + inspect(key));
|
||||
}
|
||||
},
|
||||
'delete': function (key) {
|
||||
if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
|
||||
if ($wm) {
|
||||
return $weakMapDelete($wm, key);
|
||||
}
|
||||
} else if (getSideChannelMap) {
|
||||
if ($m) {
|
||||
return $m['delete'](key);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
get: function (key) {
|
||||
if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
|
||||
if ($wm) {
|
||||
return $weakMapGet($wm, key);
|
||||
}
|
||||
}
|
||||
return $m && $m.get(key);
|
||||
},
|
||||
has: function (key) {
|
||||
if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
|
||||
if ($wm) {
|
||||
return $weakMapHas($wm, key);
|
||||
}
|
||||
}
|
||||
return !!$m && $m.has(key);
|
||||
},
|
||||
set: function (key, value) {
|
||||
if ($WeakMap && key && (typeof key === 'object' || typeof key === 'function')) {
|
||||
if (!$wm) {
|
||||
$wm = new $WeakMap();
|
||||
}
|
||||
$weakMapSet($wm, key, value);
|
||||
} else if (getSideChannelMap) {
|
||||
if (!$m) {
|
||||
$m = getSideChannelMap();
|
||||
}
|
||||
// eslint-disable-next-line no-extra-parens
|
||||
/** @type {NonNullable<typeof $m>} */ ($m).set(key, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// @ts-expect-error TODO: figure out why this is erroring
|
||||
return channel;
|
||||
}
|
||||
: getSideChannelMap;
|
||||
@@ -0,0 +1,139 @@
|
||||
# proxy-addr
|
||||
|
||||
[![NPM Version][npm-version-image]][npm-url]
|
||||
[![NPM Downloads][npm-downloads-image]][npm-url]
|
||||
[![Node.js Version][node-image]][node-url]
|
||||
[![Build Status][ci-image]][ci-url]
|
||||
[![Test Coverage][coveralls-image]][coveralls-url]
|
||||
|
||||
Determine address of proxied request
|
||||
|
||||
## Install
|
||||
|
||||
This is a [Node.js](https://nodejs.org/en/) module available through the
|
||||
[npm registry](https://www.npmjs.com/). Installation is done using the
|
||||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
||||
|
||||
```sh
|
||||
$ npm install proxy-addr
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
```js
|
||||
var proxyaddr = require('proxy-addr')
|
||||
```
|
||||
|
||||
### proxyaddr(req, trust)
|
||||
|
||||
Return the address of the request, using the given `trust` parameter.
|
||||
|
||||
The `trust` argument is a function that returns `true` if you trust
|
||||
the address, `false` if you don't. The closest untrusted address is
|
||||
returned.
|
||||
|
||||
```js
|
||||
proxyaddr(req, function (addr) { return addr === '127.0.0.1' })
|
||||
proxyaddr(req, function (addr, i) { return i < 1 })
|
||||
```
|
||||
|
||||
The `trust` arugment may also be a single IP address string or an
|
||||
array of trusted addresses, as plain IP addresses, CIDR-formatted
|
||||
strings, or IP/netmask strings.
|
||||
|
||||
```js
|
||||
proxyaddr(req, '127.0.0.1')
|
||||
proxyaddr(req, ['127.0.0.0/8', '10.0.0.0/8'])
|
||||
proxyaddr(req, ['127.0.0.0/255.0.0.0', '192.168.0.0/255.255.0.0'])
|
||||
```
|
||||
|
||||
This module also supports IPv6. Your IPv6 addresses will be normalized
|
||||
automatically (i.e. `fe80::00ed:1` equals `fe80:0:0:0:0:0:ed:1`).
|
||||
|
||||
```js
|
||||
proxyaddr(req, '::1')
|
||||
proxyaddr(req, ['::1/128', 'fe80::/10'])
|
||||
```
|
||||
|
||||
This module will automatically work with IPv4-mapped IPv6 addresses
|
||||
as well to support node.js in IPv6-only mode. This means that you do
|
||||
not have to specify both `::ffff:a00:1` and `10.0.0.1`.
|
||||
|
||||
As a convenience, this module also takes certain pre-defined names
|
||||
in addition to IP addresses, which expand into IP addresses:
|
||||
|
||||
```js
|
||||
proxyaddr(req, 'loopback')
|
||||
proxyaddr(req, ['loopback', 'fc00:ac:1ab5:fff::1/64'])
|
||||
```
|
||||
|
||||
* `loopback`: IPv4 and IPv6 loopback addresses (like `::1` and
|
||||
`127.0.0.1`).
|
||||
* `linklocal`: IPv4 and IPv6 link-local addresses (like
|
||||
`fe80::1:1:1:1` and `169.254.0.1`).
|
||||
* `uniquelocal`: IPv4 private addresses and IPv6 unique-local
|
||||
addresses (like `fc00:ac:1ab5:fff::1` and `192.168.0.1`).
|
||||
|
||||
When `trust` is specified as a function, it will be called for each
|
||||
address to determine if it is a trusted address. The function is
|
||||
given two arguments: `addr` and `i`, where `addr` is a string of
|
||||
the address to check and `i` is a number that represents the distance
|
||||
from the socket address.
|
||||
|
||||
### proxyaddr.all(req, [trust])
|
||||
|
||||
Return all the addresses of the request, optionally stopping at the
|
||||
first untrusted. This array is ordered from closest to furthest
|
||||
(i.e. `arr[0] === req.connection.remoteAddress`).
|
||||
|
||||
```js
|
||||
proxyaddr.all(req)
|
||||
```
|
||||
|
||||
The optional `trust` argument takes the same arguments as `trust`
|
||||
does in `proxyaddr(req, trust)`.
|
||||
|
||||
```js
|
||||
proxyaddr.all(req, 'loopback')
|
||||
```
|
||||
|
||||
### proxyaddr.compile(val)
|
||||
|
||||
Compiles argument `val` into a `trust` function. This function takes
|
||||
the same arguments as `trust` does in `proxyaddr(req, trust)` and
|
||||
returns a function suitable for `proxyaddr(req, trust)`.
|
||||
|
||||
```js
|
||||
var trust = proxyaddr.compile('loopback')
|
||||
var addr = proxyaddr(req, trust)
|
||||
```
|
||||
|
||||
This function is meant to be optimized for use against every request.
|
||||
It is recommend to compile a trust function up-front for the trusted
|
||||
configuration and pass that to `proxyaddr(req, trust)` for each request.
|
||||
|
||||
## Testing
|
||||
|
||||
```sh
|
||||
$ npm test
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
```sh
|
||||
$ npm run-script bench
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE)
|
||||
|
||||
[ci-image]: https://badgen.net/github/checks/jshttp/proxy-addr/master?label=ci
|
||||
[ci-url]: https://github.com/jshttp/proxy-addr/actions?query=workflow%3Aci
|
||||
[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/proxy-addr/master
|
||||
[coveralls-url]: https://coveralls.io/r/jshttp/proxy-addr?branch=master
|
||||
[node-image]: https://badgen.net/npm/node/proxy-addr
|
||||
[node-url]: https://nodejs.org/en/download
|
||||
[npm-downloads-image]: https://badgen.net/npm/dm/proxy-addr
|
||||
[npm-url]: https://npmjs.org/package/proxy-addr
|
||||
[npm-version-image]: https://badgen.net/npm/v/proxy-addr
|
||||
Reference in New Issue
Block a user