Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser support #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const wrapCallback = (func: { (callback: any): void }) => {
};

describe('git', () => {
// the default branch was harcoded as 'master'
let defautBranch = 'master';
// version ^2.28 now has the default branch in the config
exec('git config --get init.defaultBranch', (err, stdout) => {
if (err == null && stdout !== '') defautBranch = stdout.trim();
});

test('create, push to, and clone a repo', async () => {
expect.assertions(11);

Expand Down Expand Up @@ -40,7 +47,7 @@ describe('git', () => {
repos.on('push', (push) => {
expect(push.repo).toBe('xyz/doom');
expect(push.commit).toBe(lastCommit);
expect(push.branch).toBe('master');
expect(push.branch).toBe(defautBranch);

expect(push.headers.host).toBe('localhost:' + port);
expect(push.method).toBe('POST');
Expand Down Expand Up @@ -87,10 +94,9 @@ describe('git', () => {
});
});
await wrapCallback((callback: () => void) => {
spawn(
'git',
['push', 'http://localhost:' + port + '/xyz/doom', 'master'],
{ cwd: srcDir }
spawn('git',['push', 'http://localhost:' + port + '/xyz/doom', defautBranch], { // eslint-disable-line
cwd: srcDir,
}
).on('exit', (code) => {
expect(code).toBe(0);
callback();
Expand Down Expand Up @@ -164,9 +170,10 @@ describe('git', () => {
);
});
await wrapCallback((callback: () => void) => {
spawn('git', ['push', 'http://localhost:' + port + '/doom', 'master'], {
cwd: srcDir,
}).on('exit', (code) => {
spawn('git', ['push', 'http://localhost:' + port + '/doom', defautBranch], { // eslint-disable-line
cwd: srcDir,
}
).on('exit', (code) => {
expect(code).toBe(0);
callback();
});
Expand Down Expand Up @@ -251,7 +258,7 @@ describe('git', () => {
await wrapCallback((callback: () => void) => {
spawn(
'git',
['push', 'http://localhost:' + port + '/doom.git', 'master'],
['push', 'http://localhost:' + port + '/doom.git', defautBranch],
{
cwd: srcDir,
}
Expand Down Expand Up @@ -310,7 +317,7 @@ describe('git', () => {
repos.on('push', (push) => {
expect(push.repo).toBe('doom');
expect(push.commit).toBe(lastCommit);
expect(push.branch).toBe('master');
expect(push.branch).toBe(defautBranch);

expect(push.headers.host).toBe('localhost:' + port);
expect(push.method).toBe('POST');
Expand Down Expand Up @@ -399,7 +406,7 @@ describe('git', () => {
await wrapCallback((callback: () => void) => {
spawn(
'git',
['push', '--tags', 'http://localhost:' + port + '/doom', 'master'],
['push', '--tags', 'http://localhost:' + port + '/doom', defautBranch],
{ cwd: srcDir }
).on('exit', (code) => {
expect(code).toBe(0);
Expand Down Expand Up @@ -514,7 +521,7 @@ describe('git', () => {
repos.on('push', (push) => {
expect(push.repo).toBe('doom');
expect(push.commit).toBe(lastCommit);
expect(push.branch).toBe('master');
expect(push.branch).toBe(defautBranch);

expect(push.headers.host).toBe('localhost:' + port);
expect(push.method).toBe('POST');
Expand Down Expand Up @@ -558,9 +565,10 @@ describe('git', () => {
);
});
await wrapCallback((callback: () => void) => {
_spawn('git', ['push', 'http://localhost:' + port + '/doom', 'master'], {
cwd: srcDir,
}).on('exit', (code) => {
_spawn('git', ['push', 'http://localhost:' + port + '/doom', defautBranch], { // eslint-disable-line
cwd: srcDir,
}
).on('exit', (code) => {
expect(code).not.toBe(0);
callback();
});
Expand All @@ -578,9 +586,9 @@ describe('git', () => {
glog.stderr.on('end', () => {
const res =
/fatal: bad default revision 'HEAD'/.test(data) ||
/fatal: your current branch 'master' does not have any commits yet/.test(
data
);
/fatal: your current branch / + defautBranch + / does not have any commits yet/.test( // eslint-disable-line
data
);
expect(res).toBeTruthy();
});
});
Expand Down Expand Up @@ -893,7 +901,7 @@ describe('git', () => {
const logs: any[] = [];
const push = spawn(
'git',
['push', 'http://localhost:' + port + '/doom.git', 'master'],
['push', 'http://localhost:' + port + '/doom.git', defautBranch],
{ cwd: srcDir }
);

Expand Down Expand Up @@ -981,7 +989,7 @@ describe('git', () => {
const logs: any[] = [];
const push = spawn(
'git',
['push', 'http://localhost:' + port + '/doom.git', 'master'],
['push', 'http://localhost:' + port + '/doom.git', defautBranch],
{ cwd: srcDir }
);

Expand Down
27 changes: 22 additions & 5 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,21 @@ export class Git extends EventEmitter implements GitEvents {
});
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method !== 'GET' && req.method !== 'POST') {
if (req.method !== 'OPTIONS') return false;

res.statusCode = 200;
res.setHeader('Allow', 'OPTIONS, GET, POST');
// preflight
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.end();
},
(req: http.IncomingMessage, res: http.ServerResponse) => {
if (
req.method !== 'GET' &&
req.method !== 'POST' &&
req.method !== 'OPTIONS'
) {
res.statusCode = 405;
res.end('method not supported');
} else {
Expand All @@ -499,6 +513,7 @@ export class Git extends EventEmitter implements GitEvents {
res.end('not found');
},
];
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('connection', 'close');

(function next(ix) {
Expand All @@ -522,12 +537,14 @@ export class Git extends EventEmitter implements GitEvents {

const createServer =
options.type == 'http'
? http.createServer
? http.createServer.bind(this, options)
: https.createServer.bind(this, options);

this.server = createServer((req, res) => {
this.handle(req, res);
});
this.server = createServer(
(req: http.IncomingMessage, res: http.ServerResponse) => {
this.handle(req, res);
}
);

this.server.listen(port, callback);

Expand Down
6 changes: 3 additions & 3 deletions website/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ repos.on('fetch', (fetch) => {
fetch.accept();
});

repos.listen(port, () => {
repos.listen(port, null, () => {
console.log(`node-git-server running at http://localhost:${port}`);
});
```
Expand Down Expand Up @@ -91,7 +91,7 @@ repos.on('fetch', (fetch) => {
fetch.accept();
});

repos.listen(port, () => {
repos.listen(port, null, () => {
console.log(`node-git-server running at http://localhost:${port}`);
});
```
Expand Down Expand Up @@ -153,7 +153,7 @@ repos.on('fetch', (fetch) => {
fetch.accept();
});

repos.listen(port, () => {
repos.listen(port, null, () => {
console.log(`node-git-server running at http://localhost:${port}`);
});
```
Expand Down