-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19751a4
commit d3d923e
Showing
5 changed files
with
114 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Readable } from 'stream' | ||
import { escapeRegex, readFromStream } from './utils' | ||
import path from 'path' | ||
import axios from 'axios' | ||
import { Exception } from '@adonisjs/core/build/standalone' | ||
|
||
export default class GithubLib { | ||
public static async getRefHeadCommit(remoteurl, ref): Promise<string> { | ||
const remote = new URL(remoteurl) | ||
remote.pathname = path.join(remote.pathname, '/info/refs') | ||
remote.searchParams.set('service', 'git-upload-pack') | ||
|
||
const reader = await this.getReaderFromHTTP(remote) | ||
|
||
const regref = escapeRegex(ref) | ||
const lines = await this.parsePktLines(reader, new RegExp(`^([0-9A-Za-z]+) refs\\/${regref}\n`)) | ||
if (lines.length < 2 || !lines[1]) { | ||
throw new Exception("Cannot find this branch") | ||
} | ||
|
||
return lines[1] | ||
} | ||
|
||
private static async getReaderFromHTTP(url: URL): Promise<Readable> { | ||
const response = await axios({ | ||
method: 'get', | ||
url: url.toString(), | ||
responseType: 'stream', // Axios will return the response body as a stream. | ||
}); | ||
|
||
return response.data | ||
} | ||
|
||
private static async parsePktLines(reader: Readable, pattern?: RegExp): Promise<(string | null)[]> { | ||
const lines = new Array<string | null>() | ||
|
||
while (reader.readable) { | ||
const lenraw = await readFromStream(reader, 4) // Read the length of the pkt-line | ||
console.log('lenraw', lenraw) | ||
if (!lenraw) continue | ||
|
||
const len = parseInt(lenraw.toString('ascii'), 16) // Convert the length to a number | ||
console.log('len', len) | ||
|
||
if (len < 4) { | ||
lines.push(null) // Push null for flush-pkt | ||
continue | ||
} | ||
|
||
const lineraw = await readFromStream(reader, len - 4) // Read the pkt-line | ||
console.log('lineraw', lineraw) | ||
if (!lineraw) continue | ||
|
||
const line = lineraw.toString('ascii') | ||
console.log('line', line) | ||
|
||
if (pattern) { | ||
const match = pattern.exec(line) | ||
if (match) return match | ||
} | ||
|
||
lines.push(line) | ||
} | ||
|
||
return lines | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Readable } from 'stream' | ||
|
||
export function escapeRegex(str: string): string { | ||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
} | ||
|
||
export async function readFromStream(stream: Readable, size: number): Promise<Buffer | null> { | ||
return new Promise((resolve, reject) => { | ||
const onReadable = () => { | ||
const chunk = stream.read(size); | ||
if (chunk !== null) { | ||
cleanup(); | ||
resolve(chunk); | ||
} | ||
}; | ||
|
||
const onEnd = () => { | ||
cleanup(); | ||
resolve(null); // Stream has ended. | ||
}; | ||
|
||
const onError = (error: Error) => { | ||
cleanup(); | ||
reject(error); // Stream encountered an error. | ||
}; | ||
|
||
const cleanup = () => { | ||
stream.removeListener('readable', onReadable); | ||
stream.removeListener('end', onEnd); | ||
stream.removeListener('error', onError); | ||
}; | ||
|
||
// Attempt to read immediately; otherwise, wait for events. | ||
const chunk = stream.read(size); | ||
if (chunk !== null) { | ||
resolve(chunk); | ||
} else { | ||
stream.on('readable', onReadable); | ||
stream.on('end', onEnd); | ||
stream.on('error', onError); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters