Skip to content

Commit

Permalink
Finished git lib
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkBrines committed Nov 15, 2024
1 parent 19751a4 commit d3d923e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 46 deletions.
68 changes: 68 additions & 0 deletions app/lib/git.ts
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
}

}
43 changes: 0 additions & 43 deletions app/lib/github.ts

This file was deleted.

43 changes: 43 additions & 0 deletions app/lib/utils.ts
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);
}
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@adonisjs/shield": "^7.0.0",
"@adonisjs/view": "^6.1.0",
"adm-zip": "^0.5.16",
"axios": "^1.6.2",
"axios": "^1.7.7",
"highlight.js": "^11.9.0",
"luxon": "^3.4.0",
"markdown-it": "^13.0.2",
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2721,9 +2721,9 @@ atomic-sleep@^1.0.0:
resolved "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz"
integrity sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==

axios@^1.6.2:
axios@^1.7.7:
version "1.7.7"
resolved "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
dependencies:
follow-redirects "^1.15.6"
Expand Down

0 comments on commit d3d923e

Please sign in to comment.