* Wraps a raw object with a Git header. * * @param {Object} params - The parameters for wrapping. * @param {string} params.type - The type of the Git object (e.g., 'blob', 'tree', 'commit'). * @param {Uint8Array} params.object - The raw object data to wrap. * @returns {Uint8Array} The
({ type, object })
| 14 | * @returns {Uint8Array} The wrapped Git object as a single buffer. |
| 15 | */ |
| 16 | static wrap({ type, object }) { |
| 17 | const header = `${type} ${object.length}\x00` |
| 18 | const headerLen = header.length |
| 19 | const totalLength = headerLen + object.length |
| 20 | |
| 21 | // Allocate a single buffer for the header and object, rather than create multiple buffers |
| 22 | const wrappedObject = new Uint8Array(totalLength) |
| 23 | for (let i = 0; i < headerLen; i++) { |
| 24 | wrappedObject[i] = header.charCodeAt(i) |
| 25 | } |
| 26 | wrappedObject.set(object, headerLen) |
| 27 | |
| 28 | return wrappedObject |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Unwraps a Git object buffer into its type and raw object data. |
no test coverage detected