Skip to content

Commit 20bc064

Browse files
james-prejcubic
authored andcommitted
chore: Update readme for new CLI/API, backward compatibility (#23)
* Update readme * Added small API section * Add back support for middleware * Revert readme changes
1 parent 8d5c055 commit 20bc064

File tree

2 files changed

+69
-63
lines changed

2 files changed

+69
-63
lines changed

README.md

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ npm install @isomorphic-git/cors-proxy
1818
Start proxy on default port 9999:
1919

2020
```sh
21-
cors-proxy start
21+
cors-proxy run
2222
```
2323

2424
Start proxy on a custom port:
2525

2626
```sh
27-
cors-proxy start -p 9889
27+
cors-proxy run -p 9889
2828
```
2929

30-
Start proxy in daemon mode. It will write the PID of the daemon process to `$PWD/cors-proxy.pid`:
30+
Start proxy in daemon mode.
3131

3232
```sh
33-
cors-proxy start -d
33+
cors-proxy start
3434
```
3535

36-
Kill the process with the PID specified in `$PWD/cors-proxy.pid`:
36+
Kill the daemon process:
3737

3838
```sh
3939
cors-proxy stop
@@ -42,67 +42,67 @@ cors-proxy stop
4242
### CLI configuration
4343

4444
Environment variables:
45+
4546
- `PORT` the port to listen to (if run with `npm start`)
4647
- `ALLOW_ORIGIN` the value for the 'Access-Control-Allow-Origin' CORS header
4748
- `INSECURE_HTTP_ORIGINS` comma separated list of origins for which HTTP should be used instead of HTTPS (added to make developing against locally running git servers easier)
4849

49-
5050
## Middleware usage
5151

5252
You can also use the `cors-proxy` as a middleware in your own server.
5353

5454
```js
55-
const express = require('express')
56-
const corsProxy = require('@isomorphic-git/cors-proxy/middleware.js')
55+
import express from 'express';
56+
import corsProxy from '@isomorphic-git/cors-proxy';
5757

58-
const app = express()
59-
const options = {}
60-
61-
app.use(corsProxy(options))
58+
const app = express();
59+
const options = {};
6260

61+
app.use(corsProxy(options));
6362
```
6463

6564
### Middleware configuration
6665

67-
*The middleware doesn't use the environment variables.* The options object supports the following properties:
66+
_The middleware doesn't use the environment variables._ The options object supports the following properties:
6867

6968
- `origin`: _string_. The value for the 'Access-Control-Allow-Origin' CORS header
7069
- `insecure_origins`: _string[]_. Array of origins for which HTTP should be used instead of HTTPS (added to make developing against locally running git servers easier)
7170
- `authorization`: _(req, res, next) => void_. A middleware function you can use to handle custom authorization. Is run after filtering for git-like requests and handling CORS but before the request is proxied.
7271

7372
_Example:_
73+
7474
```ts
7575
app.use(
76-
corsProxy({
77-
authorization: (req: Request, res: Response, next: NextFunction) => {
78-
// proxied git HTTP requests already use the Authorization header for git credentials,
79-
// so their [Company] credentials are inserted in the X-Authorization header instead.
80-
if (getAuthorizedUser(req, 'X-Authorization')) {
81-
return next();
82-
} else {
83-
return res.status(401).send("Unable to authenticate you with [Company]'s git proxy");
84-
}
85-
},
86-
})
76+
corsProxy({
77+
authorization: (req: Request, res: Response, next: NextFunction) => {
78+
// proxied git HTTP requests already use the Authorization header for git credentials,
79+
// so their [Company] credentials are inserted in the X-Authorization header instead.
80+
if (getAuthorizedUser(req, 'X-Authorization')) {
81+
return next();
82+
} else {
83+
return res.status(401).send("Unable to authenticate you with [Company]'s git proxy");
84+
}
85+
},
86+
}),
8787
);
8888

8989
// Only requests with a valid JSON Web Token will be proxied
9090
function getAuthorizedUser(req: Request, header: string = 'Authorization') {
91-
const Authorization = req.get(header);
92-
93-
if (Authorization) {
94-
const token = Authorization.replace('Bearer ', '');
95-
try {
96-
const verifiedToken = verify(token, env.APP_SECRET) as IToken;
97-
if (verifiedToken) {
98-
return {
99-
id: verifiedToken.userId,
100-
};
101-
}
102-
} catch (e) {
103-
// noop
104-
}
105-
}
91+
const Authorization = req.get(header);
92+
93+
if (Authorization) {
94+
const token = Authorization.replace('Bearer ', '');
95+
try {
96+
const verifiedToken = verify(token, env.APP_SECRET) as IToken;
97+
if (verifiedToken) {
98+
return {
99+
id: verifiedToken.userId,
100+
};
101+
}
102+
} catch (e) {
103+
// noop
104+
}
105+
}
106106
}
107107
```
108108

@@ -111,29 +111,30 @@ function getAuthorizedUser(req: Request, header: string = 'Authorization') {
111111
There is no official chart for this project, helm or otherwise. You can make your own, but keep in mind cors-proxy uses the Micro server, which will return a 403 error for any requests that do not have the user agent header.
112112

113113
_Example:_
114+
114115
```yaml
115-
containers:
116-
- name: cors-proxy
117-
image: node:lts-alpine
118-
env:
119-
- name: ALLOW_ORIGIN
120-
value: https://mydomain.com
121-
command:
122-
- npx
123-
args:
124-
- '@isomorphic-git/cors-proxy'
125-
- start
126-
ports:
127-
- containerPort: 9999
128-
hostPort: 9999
129-
name: proxy
130-
protocol: TCP
131-
livenessProbe:
132-
tcpSocket:
133-
port: proxy
134-
readinessProbe:
135-
tcpSocket:
136-
port: proxy
116+
containers:
117+
- name: cors-proxy
118+
image: node:lts-alpine
119+
env:
120+
- name: ALLOW_ORIGIN
121+
value: https://mydomain.com
122+
command:
123+
- npx
124+
args:
125+
- '@isomorphic-git/cors-proxy'
126+
- start
127+
ports:
128+
- containerPort: 9999
129+
hostPort: 9999
130+
name: proxy
131+
protocol: TCP
132+
livenessProbe:
133+
tcpSocket:
134+
port: proxy
135+
readinessProbe:
136+
tcpSocket:
137+
port: proxy
137138
```
138139
139140
## License

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ function isAllowed(req, u) {
9090
*
9191
* @param {import('http').IncomingMessage} req
9292
* @param {import('http').ServerResponse} res
93+
* @param {(() => any)?} next
9394
* @returns
9495
*/
95-
export default function handleRequest(req, res) {
96+
export default function handleRequest(req, res, next) {
9697
const u = new URL(req.url, `https://0.0.0.0:${req.socket.localPort}/`);
98+
const isMiddleware = typeof next === 'function';
9799

98100
// CORS
99101

@@ -120,13 +122,15 @@ export default function handleRequest(req, res) {
120122

121123
// Default landing page
122124
if (u.pathname === '/') {
125+
if (isMiddleware) return next();
123126
res.setHeader('content-type', 'text/html');
124127
res.statusCode = 400;
125128
res.end(landingPage);
126129
return;
127130
}
128131

129132
if (!isAllowed(req, u)) {
133+
if (isMiddleware) return next();
130134
res.statusCode = 403;
131135
res.end();
132136
return;
@@ -175,8 +179,9 @@ export default function handleRequest(req, res) {
175179
return f.body.pipeTo(Writable.toWeb(res));
176180
})
177181
.catch((e) => {
178-
res.statusCode = 502;
179182
console.error(e);
183+
if (isMiddleware) return next();
184+
res.statusCode = 502;
180185
res.end();
181186
});
182187
}

0 commit comments

Comments
 (0)