Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@es-git/object-mixin

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@es-git/object-mixin - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

58

es/decodeObject.js

@@ -1,4 +0,2 @@

import { Type } from '@es-git/core';
import { TextDecoder } from 'text-encoding';
const decoder = new TextDecoder();
import { Type, decode, unpackHash, fromDec, fromOct } from '@es-git/core';
export default function decodeObject(buffer) {

@@ -12,6 +10,6 @@ const space = buffer.indexOf(0x20);

const body = buffer.subarray(nil + 1);
const size = parseDec(buffer, space + 1, nil);
const size = fromDec(buffer, space + 1, nil);
if (size !== body.length)
throw new Error("Invalid body length.");
const type = decodeSubarray(buffer, 0, space);
const type = decode(buffer, 0, space);
switch (type) {

@@ -31,3 +29,3 @@ case Type.blob:

export function blobToText(blob) {
return decoder.decode(blob);
return decode(blob);
}

@@ -53,7 +51,7 @@ function decodeBlob(body) {

throw new SyntaxError("Missing space");
mode = parseOct(body, start, i++);
mode = fromOct(body, start, i++);
start = i;
i = body.indexOf(0x00, start);
name = decodeSubarray(body, start, i++);
hash = toHex(body, i, i += 20);
name = decode(body, start, i++);
hash = unpackHash(body, i, i += 20);
tree[name] = {

@@ -86,3 +84,3 @@ mode: mode,

throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++);
key = decode(body, start, i++);
start = i;

@@ -92,3 +90,3 @@ i = body.indexOf(0x0a, start);

throw new SyntaxError("Missing linefeed");
let value = decodeSubarray(body, start, i++);
let value = decode(body, start, i++);
if (key === "parent") {

@@ -105,3 +103,3 @@ parents.push(value);

i++;
commit.message = decodeSubarray(body, i, body.length);
commit.message = decode(body, i, body.length);
return {

@@ -122,3 +120,3 @@ type: Type.commit,

throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++);
key = decode(body, start, i++);
start = i;

@@ -128,3 +126,3 @@ i = body.indexOf(0x0a, start);

throw new SyntaxError("Missing linefeed");
let value = decodeSubarray(body, start, i++);
let value = decode(body, start, i++);
if (key === "tagger")

@@ -135,3 +133,3 @@ value = decodePerson(value);

i++;
tag.message = decodeSubarray(body, i, body.length);
tag.message = decode(body, i, body.length);
return {

@@ -155,32 +153,2 @@ type: Type.tag,

}
function parseOct(buffer, start, end) {
let val = 0;
while (start < end) {
val = (val << 3) + buffer[start++] - 0x30;
}
return val;
}
function parseDec(buffer, start, end) {
let val = 0;
while (start < end) {
val = val * 10 + buffer[start++] - 0x30;
}
return val;
}
function decodeSubarray(binary, start = 0, end = binary.length) {
return decoder.decode(binary.subarray(start, end));
}
function toHex(binary, start = 0, end = binary.length) {
var hex = "";
for (var i = start; i < end; i++) {
var byte = binary[i];
hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
String.fromCharCode(nibbleToCode(byte & 0xf));
}
return hex;
}
function nibbleToCode(nibble) {
nibble |= 0;
return (nibble + (nibble < 10 ? 0x30 : 0x57)) | 0;
}
//# sourceMappingURL=decodeObject.js.map

@@ -13,5 +13,5 @@ import { Mode } from '@es-git/core';

mode: Mode;
}): 0 | 1 | -1;
}): 1 | -1 | 0;
export declare function encodeTree(body: TreeBody): Uint8Array;
export declare function encodeTag(body: TagBody): Uint8Array;
export declare function encodeCommit(body: CommitBody): Uint8Array;

@@ -1,8 +0,5 @@

import { Type, Mode } from '@es-git/core';
import { TextEncoder } from 'text-encoding';
const NEWLINE = '\n'.charCodeAt(0);
const encoder = new TextEncoder();
import { Type, Mode, NEWLINE, encode, concat, flatten, packHash } from '@es-git/core';
export default function encodeObject(object) {
const bytes = getBytes(object);
return concatenate(encoder.encode(`${object.type} ${bytes.length}\0`), bytes);
return concat(encode(`${object.type} ${bytes.length}\0`), bytes);
}

@@ -24,3 +21,3 @@ export function getBytes(object) {

export function textToBlob(text) {
return encoder.encode(text);
return encode(text);
}

@@ -36,6 +33,6 @@ export function encodeBlob(body) {

export function encodeTree(body) {
return concatenate(...flatten(Object.keys(body)
return concat(...flatten(Object.keys(body)
.map(key => (Object.assign({ name: key }, body[key])))
.sort(treeSort)
.map(entry => [encoder.encode(`${entry.mode.toString(8)} ${entry.name}\0`), encodeHex(entry.hash)])));
.map(entry => [encode(`${entry.mode.toString(8)} ${entry.name}\0`), packHash(entry.hash)])));
}

@@ -80,12 +77,2 @@ export function encodeTag(body) {

}
function concatenate(...arrays) {
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
function joinWithNewline(...values) {

@@ -100,3 +87,3 @@ const sum = values.reduce((sum, x) => sum + x.length, 0);

if (typeof (arr) === 'string') {
result.set(encoder.encode(arr), offset);
result.set(encode(arr), offset);
}

@@ -110,18 +97,2 @@ else {

}
function encodeHex(hex) {
var raw = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length;) {
raw[i / 2] =
(codeToNibble(hex.charCodeAt(i++)) << 4)
| codeToNibble(hex.charCodeAt(i++));
}
return raw;
}
function codeToNibble(code) {
code |= 0;
return (code - ((code & 0x40) ? 0x57 : 0x30)) | 0;
}
function flatten(items) {
return items.reduce((result, list) => result.concat(list), []);
}
//# sourceMappingURL=encodeObject.js.map

@@ -1,2 +0,2 @@

'use strict';
"use strict";

@@ -9,7 +9,4 @@ Object.defineProperty(exports, "__esModule", {

var _core = require('@es-git/core');
var _core = require("@es-git/core");
var _textEncoding = require('text-encoding');
var decoder = new _textEncoding.TextDecoder();
function decodeObject(buffer) {

@@ -21,5 +18,5 @@ var space = buffer.indexOf(0x20);

var body = buffer.subarray(nil + 1);
var size = parseDec(buffer, space + 1, nil);
var size = (0, _core.fromDec)(buffer, space + 1, nil);
if (size !== body.length) throw new Error("Invalid body length.");
var type = decodeSubarray(buffer, 0, space);
var type = (0, _core.decode)(buffer, 0, space);
switch (type) {

@@ -39,3 +36,3 @@ case _core.Type.blob:

function blobToText(blob) {
return decoder.decode(blob);
return (0, _core.decode)(blob);
}

@@ -60,7 +57,7 @@ function decodeBlob(body) {

if (i < 0) throw new SyntaxError("Missing space");
mode = parseOct(body, start, i++);
mode = (0, _core.fromOct)(body, start, i++);
start = i;
i = body.indexOf(0x00, start);
name = decodeSubarray(body, start, i++);
hash = toHex(body, i, i += 20);
name = (0, _core.decode)(body, start, i++);
hash = (0, _core.unpackHash)(body, i, i += 20);
tree[name] = {

@@ -92,7 +89,7 @@ mode: mode,

if (i < 0) throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++);
key = (0, _core.decode)(body, start, i++);
start = i;
i = body.indexOf(0x0a, start);
if (i < 0) throw new SyntaxError("Missing linefeed");
var value = decodeSubarray(body, start, i++);
var value = (0, _core.decode)(body, start, i++);
if (key === "parent") {

@@ -107,3 +104,3 @@ parents.push(value);

i++;
commit.message = decodeSubarray(body, i, body.length);
commit.message = (0, _core.decode)(body, i, body.length);
return {

@@ -123,7 +120,7 @@ type: _core.Type.commit,

if (i < 0) throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++);
key = (0, _core.decode)(body, start, i++);
start = i;
i = body.indexOf(0x0a, start);
if (i < 0) throw new SyntaxError("Missing linefeed");
var value = decodeSubarray(body, start, i++);
var value = (0, _core.decode)(body, start, i++);
if (key === "tagger") value = decodePerson(value);

@@ -133,3 +130,3 @@ tag[key] = value;

i++;
tag.message = decodeSubarray(body, i, body.length);
tag.message = (0, _core.decode)(body, i, body.length);
return {

@@ -152,38 +149,3 @@ type: _core.Type.tag,

}
function parseOct(buffer, start, end) {
var val = 0;
while (start < end) {
val = (val << 3) + buffer[start++] - 0x30;
}
return val;
}
function parseDec(buffer, start, end) {
var val = 0;
while (start < end) {
val = val * 10 + buffer[start++] - 0x30;
}
return val;
}
function decodeSubarray(binary) {
var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : binary.length;
return decoder.decode(binary.subarray(start, end));
}
function toHex(binary) {
var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
var end = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : binary.length;
var hex = "";
for (var i = start; i < end; i++) {
var byte = binary[i];
hex += String.fromCharCode(nibbleToCode(byte >> 4)) + String.fromCharCode(nibbleToCode(byte & 0xf));
}
return hex;
}
function nibbleToCode(nibble) {
nibble |= 0;
return nibble + (nibble < 10 ? 0x30 : 0x57) | 0;
}
//# sourceMappingURL=decodeObject.js.map
//# sourceMappingURL=decodeObject.js.map

@@ -38,11 +38,7 @@ 'use strict';

var _textEncoding = require('text-encoding');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var NEWLINE = '\n'.charCodeAt(0);
var encoder = new _textEncoding.TextEncoder();
function encodeObject(object) {
var bytes = getBytes(object);
return concatenate(encoder.encode(String(object.type) + ' ' + String(bytes.length) + '\0'), bytes);
return (0, _core.concat)((0, _core.encode)(String(object.type) + ' ' + String(bytes.length) + '\0'), bytes);
}

@@ -64,3 +60,3 @@ function getBytes(object) {

function textToBlob(text) {
return encoder.encode(text);
return (0, _core.encode)(text);
}

@@ -78,3 +74,3 @@ function encodeBlob(body) {

return concatenate.apply(undefined, (0, _toConsumableArray3.default)(flatten((0, _keys2.default)(body).map(function (key) {
return _core.concat.apply(undefined, (0, _toConsumableArray3.default)((0, _core.flatten)((0, _keys2.default)(body).map(function (key) {
(0, _newArrowCheck3.default)(this, _this);

@@ -84,3 +80,3 @@ return (0, _assign2.default)({ name: key }, body[key]);

(0, _newArrowCheck3.default)(this, _this);
return [encoder.encode(String(entry.mode.toString(8)) + ' ' + String(entry.name) + '\0'), encodeHex(entry.hash)];
return [(0, _core.encode)(String(entry.mode.toString(8)) + ' ' + String(entry.name) + '\0'), (0, _core.packHash)(entry.hash)];
}.bind(this)))));

@@ -126,14 +122,14 @@ }

}
function concatenate() {
function joinWithNewline() {
var _this3 = this;
for (var _len = arguments.length, arrays = Array(_len), _key = 0; _key < _len; _key++) {
arrays[_key] = arguments[_key];
for (var _len = arguments.length, values = Array(_len), _key = 0; _key < _len; _key++) {
values[_key] = arguments[_key];
}
var totalLength = arrays.reduce(function (sum, arr) {
var sum = values.reduce(function (sum, x) {
(0, _newArrowCheck3.default)(this, _this3);
return sum + arr.length;
return sum + x.length;
}.bind(this), 0);
var result = new Uint8Array(totalLength);
var result = new Uint8Array(values.length - 1 + sum);
var offset = 0;

@@ -145,6 +141,13 @@ var _iteratorNormalCompletion = true;

try {
for (var _iterator = (0, _getIterator3.default)(arrays), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
for (var _iterator = (0, _getIterator3.default)(values), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var arr = _step.value;
result.set(arr, offset);
if (offset > 0) {
result.set([_core.NEWLINE], offset++);
}
if (typeof arr === 'string') {
result.set((0, _core.encode)(arr), offset);
} else {
result.set(arr, offset);
}
offset += arr.length;

@@ -169,70 +172,3 @@ }

}
function joinWithNewline() {
var _this4 = this;
for (var _len2 = arguments.length, values = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
values[_key2] = arguments[_key2];
}
var sum = values.reduce(function (sum, x) {
(0, _newArrowCheck3.default)(this, _this4);
return sum + x.length;
}.bind(this), 0);
var result = new Uint8Array(values.length - 1 + sum);
var offset = 0;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = (0, _getIterator3.default)(values), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var arr = _step2.value;
if (offset > 0) {
result.set([NEWLINE], offset++);
}
if (typeof arr === 'string') {
result.set(encoder.encode(arr), offset);
} else {
result.set(arr, offset);
}
offset += arr.length;
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return result;
}
function encodeHex(hex) {
var raw = new Uint8Array(hex.length / 2);
for (var i = 0; i < hex.length;) {
raw[i / 2] = codeToNibble(hex.charCodeAt(i++)) << 4 | codeToNibble(hex.charCodeAt(i++));
}
return raw;
}
function codeToNibble(code) {
code |= 0;
return code - (code & 0x40 ? 0x57 : 0x30) | 0;
}
function flatten(items) {
var _this5 = this;
return items.reduce(function (result, list) {
(0, _newArrowCheck3.default)(this, _this5);
return result.concat(list);
}.bind(this), []);
}
//# sourceMappingURL=encodeObject.js.map
//# sourceMappingURL=encodeObject.js.map
{
"name": "@es-git/object-mixin",
"version": "0.0.5",
"version": "0.1.0",
"description": "",

@@ -42,6 +42,5 @@ "main": "js/index.js",

"dependencies": {
"@es-git/core": "^0.0.1",
"git-sha1": "^0.1.2",
"text-encoding": "^0.6.4"
"@es-git/core": "^0.1.0",
"git-sha1": "^0.1.2"
}
}

@@ -1,33 +0,1 @@

declare interface TextEncoder {
new() : TextEncoder;
readonly encoding : string;
encode(input? : string) : Uint8Array;
}
declare type TextDecoderOptions = {
fatal : boolean;
ignoreBOM : boolean;
};
declare type TextDecodeOptions = {
stream : boolean;
};
declare interface TextDecoder
{
new(label? : string, options? : TextDecoderOptions) : TextDecoder;
readonly encoding : string;
readonly fatal : boolean;
readonly ignoreBOM : boolean;
decode(input? : BufferSource, options? : TextDecodeOptions) : string;
}
declare var TextEncoder : TextEncoder;
declare var TextDecoder : TextDecoder;
declare module 'text-encoding' {
export var TextEncoder : TextEncoder;
export var TextDecoder : TextDecoder;
}
declare module "git-sha1" {

@@ -34,0 +2,0 @@ export interface Sha1 {

import {
Type,
Mode
Mode,
decode,
unpackHash,
fromDec,
fromOct
} from '@es-git/core';
import { TextDecoder } from 'text-encoding';
import {

@@ -22,4 +24,2 @@ GitObject,

const decoder = new TextDecoder();
export default function decodeObject(buffer : Uint8Array) : GitObject {

@@ -31,5 +31,5 @@ const space = buffer.indexOf(0x20);

const body = buffer.subarray(nil + 1);
const size = parseDec(buffer, space + 1, nil);
const size = fromDec(buffer, space + 1, nil);
if (size !== body.length) throw new Error("Invalid body length.");
const type = decodeSubarray(buffer, 0, space);
const type = decode(buffer, 0, space);
switch(type){

@@ -50,3 +50,3 @@ case Type.blob:

export function blobToText(blob : Uint8Array) {
return decoder.decode(blob);
return decode(blob);
}

@@ -73,7 +73,7 @@

if (i < 0) throw new SyntaxError("Missing space");
mode = parseOct(body, start, i++);
mode = fromOct(body, start, i++);
start = i;
i = body.indexOf(0x00, start);
name = decodeSubarray(body, start, i++);
hash = toHex(body, i, i += 20);
name = decode(body, start, i++);
hash = unpackHash(body, i, i += 20);
tree[name] = {

@@ -107,7 +107,7 @@ mode: mode,

if (i < 0) throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++) as any;
key = decode(body, start, i++) as any;
start = i;
i = body.indexOf(0x0a, start);
if (i < 0) throw new SyntaxError("Missing linefeed");
let value = decodeSubarray(body, start, i++);
let value = decode(body, start, i++);
if (key === "parent") {

@@ -122,3 +122,3 @@ parents.push(value);

i++;
commit.message = decodeSubarray(body, i, body.length);
commit.message = decode(body, i, body.length);
return {

@@ -139,7 +139,7 @@ type: Type.commit,

if (i < 0) throw new SyntaxError("Missing space");
key = decodeSubarray(body, start, i++);
key = decode(body, start, i++);
start = i;
i = body.indexOf(0x0a, start);
if (i < 0) throw new SyntaxError("Missing linefeed");
let value : any = decodeSubarray(body, start, i++);
let value : any = decode(body, start, i++);
if (key === "tagger") value = decodePerson(value);

@@ -149,3 +149,3 @@ tag[key] = value;

i++;
tag.message = decodeSubarray(body, i, body.length);
tag.message = decode(body, i, body.length);
return {

@@ -169,36 +169,1 @@ type: Type.tag,

}
function parseOct(buffer : Uint8Array, start : number, end : number) {
let val = 0;
while (start < end) {
val = (val << 3) + buffer[start++] - 0x30;
}
return val;
}
function parseDec(buffer : Uint8Array, start : number, end : number) {
let val = 0;
while (start < end) {
val = val * 10 + buffer[start++] - 0x30;
}
return val;
}
function decodeSubarray(binary : Uint8Array, start = 0, end = binary.length) {
return decoder.decode(binary.subarray(start, end));
}
function toHex(binary : Uint8Array, start = 0, end = binary.length) {
var hex = "";
for (var i = start; i < end; i++) {
var byte = binary[i];
hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
String.fromCharCode(nibbleToCode(byte & 0xf));
}
return hex;
}
function nibbleToCode(nibble : number) {
nibble |= 0;
return (nibble + (nibble < 10 ? 0x30 : 0x57))|0;
}
import {
Type,
Mode
Mode,
NEWLINE,
encode,
concat,
flatten,
packHash
} from '@es-git/core';
import { TextEncoder } from 'text-encoding';
import {

@@ -18,8 +21,5 @@ GitObject,

const NEWLINE = '\n'.charCodeAt(0);
const encoder = new TextEncoder();
export default function encodeObject(object : GitObject) : Uint8Array {
const bytes = getBytes(object);
return concatenate(encoder.encode(`${object.type} ${bytes.length}\0`), bytes);
return concat(encode(`${object.type} ${bytes.length}\0`), bytes);
}

@@ -43,3 +43,3 @@

export function textToBlob(text : string) {
return encoder.encode(text);
return encode(text);
}

@@ -58,3 +58,3 @@

export function encodeTree(body : TreeBody) {
return concatenate(...flatten(Object.keys(body)
return concat(...flatten(Object.keys(body)
.map(key => ({

@@ -65,3 +65,3 @@ name: key,

.sort(treeSort)
.map(entry => [encoder.encode(`${entry.mode.toString(8)} ${entry.name}\0`), encodeHex(entry.hash)])));
.map(entry => [encode(`${entry.mode.toString(8)} ${entry.name}\0`), packHash(entry.hash)])));

@@ -126,13 +126,2 @@ }

function concatenate(...arrays : Uint8Array[]) {
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
const result = new Uint8Array(totalLength);
let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
function joinWithNewline(...values : (string | Uint8Array)[]){

@@ -147,3 +136,3 @@ const sum = values.reduce((sum, x) => sum + x.length, 0);

if(typeof(arr) === 'string'){
result.set(encoder.encode(arr), offset);
result.set(encode(arr), offset);
}else{

@@ -156,20 +145,1 @@ result.set(arr, offset);

}
function encodeHex(hex : string) {
var raw = new Uint8Array(hex.length/2);
for (let i=0; i < hex.length;) {
raw[i/2] =
(codeToNibble(hex.charCodeAt(i++)) << 4)
| codeToNibble(hex.charCodeAt(i++));
}
return raw;
}
function codeToNibble(code : number) {
code |= 0;
return (code - ((code & 0x40) ? 0x57 : 0x30))|0;
}
function flatten<T>(items : T[][]){
return items.reduce((result, list) => result.concat(list), []);
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc