Skip to content
Draft
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
8 changes: 3 additions & 5 deletions __tests__/test-GitPktLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,18 @@ describe('GitPktLine', () => {
it('encode string', async () => {
const foo = GitPktLine.encode('hello world\n')
expect(foo).toBeTruthy()
expect(Buffer.compare(foo, Buffer.from('0010hello world\n')) === 0).toBe(
true
)
expect(foo.equals(Buffer.from('0010hello world\n'))).toEqual(true)
})

it('encode empty string', async () => {
const foo = GitPktLine.encode('')
expect(foo).toBeTruthy()
expect(Buffer.compare(foo, Buffer.from('0004')) === 0).toBe(true)
expect(foo.equals(Buffer.from('0004'))).toEqual(true)
})

it('encode flush', async () => {
const foo = GitPktLine.flush()
expect(foo).toBeTruthy()
expect(Buffer.compare(foo, Buffer.from('0000')) === 0).toBe(true)
expect(foo.equals(Buffer.from('0000'))).toEqual(true)
})
})
4 changes: 2 additions & 2 deletions __tests__/test-hashBlob.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('hashBlob', () => {
})
expect(oid).toEqual('4551a1856279dde6ae9d65862a1dff59a5f199d8')
expect(format).toEqual('wrapped')
expect(Buffer.compare(object, wrapped) === 0).toBe(true)
expect(object.equals(wrapped)).toBe(true)
})

it('object as String', async () => {
Expand All @@ -56,6 +56,6 @@ describe('hashBlob', () => {
})
expect(oid).toEqual('4551a1856279dde6ae9d65862a1dff59a5f199d8')
expect(format).toEqual('wrapped')
expect(Buffer.compare(object, wrapped) === 0).toBe(true)
expect(object.equals(wrapped)).toBe(true)
})
})
3 changes: 1 addition & 2 deletions __tests__/test-wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ access it.
expect(error.code).toEqual(E.AssertServerResponseFail)
expect(error.data).toEqual({
expected: `Two strings separated by '\\x00'`,
actual:
`ERR Repository not found
actual: `ERR Repository not found
The requested repository does not exist, or you do not have permission to
access it.
`
Expand Down
30 changes: 22 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@
"src/index.d.ts"
],
"dependencies": {
"array-buffer-to-hex": "^1.0.0",
"arraybuffer-equal": "^1.0.4",
"async-lock": "^1.1.0",
"base64-js": "^1.3.1",
"clean-git-ref": "^2.0.1",
"crc-32": "^1.2.0",
"diff3": "0.0.3",
"git-apply-delta": "0.0.7",
"globalyzer": "^0.1.4",
"globrex": "^0.1.2",
"hex-to-array-buffer": "^1.1.0",
"ignore": "^5.1.4",
"isomorphic-textencoder": "^1.0.1",
"marky": "^1.2.1",
"minimisted": "^2.0.0",
"pako": "^1.0.10",
Expand Down
3 changes: 2 additions & 1 deletion src/commands/hashBlob.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
import { hashObject } from '../storage/hashObject.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'

/**
*
Expand Down Expand Up @@ -36,7 +37,7 @@ export async function hashBlob ({ core = 'default', object }) {
try {
// Convert object to buffer
if (typeof object === 'string') {
object = Buffer.from(object, 'utf8')
object = TinyBuffer.from(object, 'utf8')
}

const type = 'blob'
Expand Down
5 changes: 3 additions & 2 deletions src/commands/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Hash from 'sha.js/sha1'

import { FileSystem } from '../models/FileSystem.js'
import { readObject } from '../storage/readObject.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'
import { join } from '../utils/join.js'
import { padHex } from '../utils/padHex.js'
import { cores } from '../utils/plugins.js'
Expand All @@ -28,7 +29,7 @@ export async function pack ({
const hash = new Hash()
const outputStream = []
function write (chunk, enc) {
const buff = Buffer.from(chunk, enc)
const buff = TinyBuffer.from(chunk, enc)
outputStream.push(buff)
hash.update(buff)
}
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function pack ({
length = length >>> 7
}
// Lastly, we can compress and write the object.
write(Buffer.from(pako.deflate(object)))
write(TinyBuffer.from(pako.deflate(object)))
}
write('PACK')
write('00000002', 'hex')
Expand Down
3 changes: 2 additions & 1 deletion src/commands/writeObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GitCommit } from '../models/GitCommit.js'
import { E, GitError } from '../models/GitError.js'
import { GitTree } from '../models/GitTree.js'
import { writeObject as _writeObject } from '../storage/writeObject.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'
import { join } from '../utils/join.js'
import { cores } from '../utils/plugins.js'

Expand Down Expand Up @@ -86,7 +87,7 @@ export async function writeObject ({
object = GitTree.from(object.entries).toObject()
break
case 'blob':
object = Buffer.from(object, encoding)
object = TinyBuffer.from(object, encoding)
break
case 'tag':
object = GitAnnotatedTag.from(object).toObject()
Expand Down
12 changes: 4 additions & 8 deletions src/models/GitAnnotatedTag.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { E, GitError } from '../models/GitError.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'
import { formatAuthor } from '../utils/formatAuthor.js'
import { normalizeNewlines } from '../utils/normalizeNewlines.js'
import { parseAuthor } from '../utils/parseAuthor.js'
Expand All @@ -7,14 +7,10 @@ export class GitAnnotatedTag {
constructor (tag) {
if (typeof tag === 'string') {
this._tag = tag
} else if (Buffer.isBuffer(tag)) {
this._tag = tag.toString('utf8')
} else if (typeof tag === 'object') {
} else if (typeof tag.object === 'string') {
this._tag = GitAnnotatedTag.render(tag)
} else {
throw new GitError(E.InternalFail, {
message: 'invalid type passed to GitAnnotatedTag constructor'
})
this._tag = tag.toString('utf8')
}
}

Expand Down Expand Up @@ -98,7 +94,7 @@ ${obj.signature ? obj.signature : ''}`
}

toObject () {
return Buffer.from(this._tag, 'utf8')
return TinyBuffer.from(this._tag, 'utf8')
}

static async sign (tag, pgp, secretKey) {
Expand Down
11 changes: 4 additions & 7 deletions src/models/GitCommit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { E, GitError } from '../models/GitError.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'
import { formatAuthor } from '../utils/formatAuthor.js'
import { indent } from '../utils/indent.js'
import { normalizeNewlines } from '../utils/normalizeNewlines.js'
Expand All @@ -9,14 +10,10 @@ export class GitCommit {
constructor (commit) {
if (typeof commit === 'string') {
this._commit = commit
} else if (Buffer.isBuffer(commit)) {
this._commit = commit.toString('utf8')
} else if (typeof commit === 'object') {
} else if (typeof commit.author === 'object') {
this._commit = GitCommit.render(commit)
} else {
throw new GitError(E.InternalFail, {
message: 'invalid type passed to GitCommit constructor'
})
this._commit = commit.toString('utf8')
}
}

Expand All @@ -34,7 +31,7 @@ export class GitCommit {
}

toObject () {
return Buffer.from(this._commit, 'utf8')
return TinyBuffer.from(this._commit, 'utf8')
}

// Todo: allow setting the headers and message
Expand Down
25 changes: 11 additions & 14 deletions src/models/GitIndex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferCursor } from '../utils/BufferCursor.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'
import { comparePath } from '../utils/comparePath.js'
import { normalizeStats } from '../utils/normalizeStats.js'
import { shasum } from '../utils/shasum.js'
Expand All @@ -21,7 +22,7 @@ function renderCacheEntryFlags (entry) {
flags.extended = false
// 12-bit name length if the length is less than 0xFFF; otherwise 0xFFF
// is stored in this field.
flags.nameLength = Math.min(Buffer.from(entry.path).length, 0xfff)
flags.nameLength = Math.min(TinyBuffer.from(entry.path).length, 0xfff)
return (
(flags.assumeValid ? 0b1000000000000000 : 0) +
(flags.extended ? 0b0100000000000000 : 0) +
Expand All @@ -41,14 +42,10 @@ export class GitIndex {
}

static async from (buffer) {
if (Buffer.isBuffer(buffer)) {
return GitIndex.fromBuffer(buffer)
} else if (buffer === null) {
if (buffer === null) {
return new GitIndex(null)
} else {
throw new GitError(E.InternalFail, {
message: 'invalid type passed to GitIndex.from'
})
return GitIndex.fromBuffer(buffer)
}
}

Expand Down Expand Up @@ -141,7 +138,7 @@ export class GitIndex {

insert ({ filepath, stats, oid }) {
stats = normalizeStats(stats)
const bfilepath = Buffer.from(filepath)
const bfilepath = TinyBuffer.from(filepath)
const entry = {
ctimeSeconds: stats.ctimeSeconds,
ctimeNanoseconds: stats.ctimeNanoseconds,
Expand Down Expand Up @@ -194,17 +191,17 @@ export class GitIndex {
}

async toObject () {
const header = Buffer.alloc(12)
const header = TinyBuffer.alloc(12)
const writer = new BufferCursor(header)
writer.write('DIRC', 4, 'utf8')
writer.writeUInt32BE(2)
writer.writeUInt32BE(this.entries.length)
const body = Buffer.concat(
const body = TinyBuffer.concat(
this.entries.map(entry => {
const bpath = Buffer.from(entry.path)
const bpath = TinyBuffer.from(entry.path)
// the fixed length + the filename + at least one null char => align by 8
const length = Math.ceil((62 + bpath.length + 1) / 8) * 8
const written = Buffer.alloc(length)
const written = TinyBuffer.alloc(length)
const writer = new BufferCursor(written)
const stat = normalizeStats(entry)
writer.writeUInt32BE(stat.ctimeSeconds)
Expand All @@ -223,8 +220,8 @@ export class GitIndex {
return written
})
)
const main = Buffer.concat([header, body])
const main = TinyBuffer.concat([header, body])
const sum = await shasum(main)
return Buffer.concat([main, Buffer.from(sum, 'hex')])
return TinyBuffer.concat([main, TinyBuffer.from(sum, 'hex')])
}
}
9 changes: 5 additions & 4 deletions src/models/GitObject.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { E, GitError } from '../models/GitError.js'
import { TinyBuffer } from '../utils/TinyBuffer.js'

export class GitObject {
static wrap ({ type, object }) {
return Buffer.concat([
Buffer.from(`${type} ${object.byteLength.toString()}\x00`),
Buffer.from(object)
return TinyBuffer.concat([
TinyBuffer.from(`${type} ${object.byteLength.toString()}\x00`),
TinyBuffer.from(object)
])
}

Expand All @@ -22,7 +23,7 @@ export class GitObject {
}
return {
type,
object: Buffer.from(buffer.slice(i + 1))
object: TinyBuffer.from(buffer.slice(i + 1))
}
}
}
Loading