Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted lib/ to TypeScript #123

Closed
wants to merge 1 commit into from
Closed
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
59 changes: 59 additions & 0 deletions cust_typings/bodec/bodec.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// <reference path="../../typings/node/node.d.ts"/>

declare var bodec: bodec.bodec;

declare module bodec {
export interface Buffer extends NodeBuffer {
concat: any;
}

export interface bodec {
Binary: any;
// Utility functions
isBinary: (t: any) => boolean;
create: (length?: number) => Buffer;
join: any; //Buffer.concat;

// Binary input and output
copy: (source, binary, offset?: number) => typeof source;
slice: (binary, start: number, end?: number) => typeof binary;

// String input and output
toRaw: (binary, start: number, end?: number) => string;
fromRaw: (raw, binary?, offset?: number) => typeof binary | Buffer;
toUnicode: (binary, start?: number, end?: number) => string;
fromUnicode: (unicode, binary?, offset?: number) => typeof binary | Buffer;
toHex: (binary, start?: number, end?: number) => string;
fromHex: (hex, binary?, offset?: number) => typeof binary | Buffer;
toBase64: (binary, start: number, end?: number) => string;
fromBase64: (base64, binary, offset?: number) => typeof binary | Buffer;
toString: (binary, encoding) => string;
fromString: (string, encoding) => any;

// Array input and output
toArray: (binary, start: number, end?: number) => any[];
fromArray: (array: any[], binary?, offset?: number) => Buffer;

// Raw <-> Hex-encoded codec
decodeHex: (hex: string) => string;
encodeHex: (raw: string) => string;

decodeBase64: (base64: string) => string;
encodeBase64: (raw: string) => string;

// Unicode <-> Utf8-encoded-raw codec
encodeUtf8: (unicode: string) => string;
decodeUtf8: (utf8) => string;

// Hex <-> Nibble codec
nibbleToCode: (nibble: number) => number;
codeToNibble: (code: number) => number;

// More
subarray: (item, i: number) => any[];
}
}

declare module "bodec" {
export = bodec;
}
108 changes: 52 additions & 56 deletions lib/apply-delta.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,57 @@
var bodec = require('bodec');

module.exports = applyDelta;

import * as bodec from 'bodec';
exports = applyDelta;
function applyDelta(delta, base) {
var deltaOffset = 0;

if (base.length !== readLength()) {
throw new Error("Base length mismatch");
}

// Create a new output buffer with length from header.
var outOffset = 0;
var out = bodec.create(readLength());

while (deltaOffset < delta.length) {
var byte = delta[deltaOffset++];
// Copy command. Tells us offset in base and length to copy.
if (byte & 0x80) {
var offset = 0;
var length = 0;
if (byte & 0x01) offset |= delta[deltaOffset++] << 0;
if (byte & 0x02) offset |= delta[deltaOffset++] << 8;
if (byte & 0x04) offset |= delta[deltaOffset++] << 16;
if (byte & 0x08) offset |= delta[deltaOffset++] << 24;
if (byte & 0x10) length |= delta[deltaOffset++] << 0;
if (byte & 0x20) length |= delta[deltaOffset++] << 8;
if (byte & 0x40) length |= delta[deltaOffset++] << 16;
if (length === 0) length = 0x10000;
// copy the data
bodec.copy(bodec.slice(base, offset, offset + length), out, outOffset);
outOffset += length;
let deltaOffset = 0;
if (base.length !== readLength()) {
throw new Error("Base length mismatch");
}
// Insert command, opcode byte is length itself
else if (byte) {
bodec.copy(bodec.slice(delta, deltaOffset, deltaOffset + byte), out, outOffset);
deltaOffset += byte;
outOffset += byte;
let outOffset = 0;
let out = bodec.create(readLength());
while (deltaOffset < delta.length) {
let byte = delta[deltaOffset++];
if (byte & 0x80) {
let offset = 0;
let length = 0;
if (byte & 0x01)
offset |= delta[deltaOffset++] << 0;
if (byte & 0x02)
offset |= delta[deltaOffset++] << 8;
if (byte & 0x04)
offset |= delta[deltaOffset++] << 16;
if (byte & 0x08)
offset |= delta[deltaOffset++] << 24;
if (byte & 0x10)
length |= delta[deltaOffset++] << 0;
if (byte & 0x20)
length |= delta[deltaOffset++] << 8;
if (byte & 0x40)
length |= delta[deltaOffset++] << 16;
if (length === 0)
length = 0x10000;
bodec.copy(bodec.slice(base, offset, offset + length), out, outOffset);
outOffset += length;
}
else if (byte) {
bodec.copy(bodec.slice(delta, deltaOffset, deltaOffset + byte), out, outOffset);
deltaOffset += byte;
outOffset += byte;
}
else
throw new Error('Invalid delta opcode');
}
else throw new Error('Invalid delta opcode');
}

if (outOffset !== out.length) {
throw new Error("Size mismatch in check");
}

return out;

// Read a variable length number our of delta and move the offset.
function readLength() {
var byte = delta[deltaOffset++];
var length = byte & 0x7f;
var shift = 7;
while (byte & 0x80) {
byte = delta[deltaOffset++];
length |= (byte & 0x7f) << shift;
shift += 7;
if (outOffset !== out.length) {
throw new Error("Size mismatch in check");
}
return out;
function readLength() {
let byte = delta[deltaOffset++];
let length = byte & 0x7f;
let shift = 7;
while (byte & 0x80) {
byte = delta[deltaOffset++];
length |= (byte & 0x7f) << shift;
shift += 7;
}
return length;
}
return length;
}
}
63 changes: 63 additions & 0 deletions lib/apply-delta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// <reference path="../cust_typings/bodec/bodec.d.ts"/>

import * as bodec from 'bodec';

exports = applyDelta;

function applyDelta(delta, base) {
let deltaOffset = 0;

if (base.length !== readLength()) {
throw new Error("Base length mismatch");
}

// Create a new output buffer with length from header.
let outOffset = 0;
let out = bodec.create(readLength());

while (deltaOffset < delta.length) {
let byte = delta[deltaOffset++];
// Copy command. Tells us offset in base and length to copy.
if (byte & 0x80) {
let offset = 0;
let length = 0;
if (byte & 0x01) offset |= delta[deltaOffset++] << 0;
if (byte & 0x02) offset |= delta[deltaOffset++] << 8;
if (byte & 0x04) offset |= delta[deltaOffset++] << 16;
if (byte & 0x08) offset |= delta[deltaOffset++] << 24;
if (byte & 0x10) length |= delta[deltaOffset++] << 0;
if (byte & 0x20) length |= delta[deltaOffset++] << 8;
if (byte & 0x40) length |= delta[deltaOffset++] << 16;
if (length === 0) length = 0x10000;
// copy the data
bodec.copy(bodec.slice(base, offset, offset + length), out, outOffset);
outOffset += length;
}
// Insert command, opcode byte is length itself
else if (byte) {
bodec.copy(bodec.slice(delta, deltaOffset, deltaOffset + byte), out, outOffset);
deltaOffset += byte;
outOffset += byte;
}
else throw new Error('Invalid delta opcode');
}

if (outOffset !== out.length) {
throw new Error("Size mismatch in check");
}

return out;

// Read a letiable length number our of delta and move the offset.
function readLength() {
let byte = delta[deltaOffset++];
let length = byte & 0x7f;
let shift = 7;
while (byte & 0x80) {
byte = delta[deltaOffset++];
length |= (byte & 0x7f) << shift;
shift += 7;
}
return length;
}
}
108 changes: 49 additions & 59 deletions lib/config-codec.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,57 @@
"use strict";

// This is for working with git config files like .git/config and .gitmodules.
// I believe this is just INI format.
module.exports = {
encode: encode,
decode: decode
exports = {
encode: encode,
decode: decode
};

function encode(config) {
var lines = [];
Object.keys(config).forEach(function (name) {
var obj = config[name];
var deep = {};
var values = {};
var hasValues = false;
Object.keys(obj).forEach(function (key) {
var value = obj[key];
if (typeof value === 'object') {
deep[key] = value;
}
else {
hasValues = true;
values[key] = value;
}
let lines = [];
Object.keys(config).forEach(function (name) {
const obj = config[name];
let deep = {};
let values = {};
let hasValues = false;
Object.keys(obj).forEach(function (key) {
let value = obj[key];
if (typeof value === 'object') {
deep[key] = value;
}
else {
hasValues = true;
values[key] = value;
}
});
if (hasValues) {
encodeBody('[' + name + ']', values);
}
Object.keys(deep).forEach(function (sub) {
let child = deep[sub];
encodeBody('[' + name + ' "' + sub + '"]', child);
});
});
if (hasValues) {
encodeBody('[' + name + ']', values);
return lines.join("\n") + "\n";
function encodeBody(header, obj) {
lines.push(header);
Object.keys(obj).forEach(function (name) {
lines.push("\t" + name + " = " + obj[name]);
});
}

Object.keys(deep).forEach(function (sub) {
var child = deep[sub];
encodeBody('[' + name + ' "' + sub + '"]', child);
});
});

return lines.join("\n") + "\n";

function encodeBody(header, obj) {
lines.push(header);
Object.keys(obj).forEach(function (name) {
lines.push( "\t" + name + " = " + obj[name]);
});
}

}


function decode(text) {
var config = {};
var section;
text.split(/[\r\n]+/).forEach(function (line) {
var match = line.match(/\[([^ \t"\]]+) *(?:"([^"]+)")?\]/);
if (match) {
section = config[match[1]] || (config[match[1]] = {});
if (match[2]) {
section = section[match[2]] = {};
}
return;
}
match = line.match(/([^ \t=]+)[ \t]*=[ \t]*(.+)/);
if (match) {
section[match[1]] = match[2];
}
});
return config;
let config = {};
let section;
text.split(/[\r\n]+/).forEach(function (line) {
let match = line.match(/\[([^ \t"\]]+) *(?:"([^"]+)")?\]/);
if (match) {
section = config[match[1]] || (config[match[1]] = {});
if (match[2]) {
section = section[match[2]] = {};
}
return;
}
match = line.match(/([^ \t=]+)[ \t]*=[ \t]*(.+)/);
if (match) {
section[match[1]] = match[2];
}
});
return config;
}
Loading