Skip to content

Commit

Permalink
Merge pull request #56 from UmamiAppearance/support-node-Buffer
Browse files Browse the repository at this point in the history
Support node buffer
  • Loading branch information
UmamiAppearance committed Jun 6, 2023
2 parents 2ad46c3 + ac5245b commit 53ad0ae
Show file tree
Hide file tree
Showing 69 changed files with 695 additions and 333 deletions.
75 changes: 55 additions & 20 deletions cjs/base-ex.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
*/
class BytesInput {
static toBytes(input) {
if (ArrayBuffer.isView(input)) {
if (ArrayBuffer.isView(input) && !(typeof Buffer !== "undefined" && input instanceof Buffer)) {
input = input.buffer;
}
}

return [new Uint8Array(input), false, "bytes"];
}
}
Expand Down Expand Up @@ -224,14 +225,18 @@ class SmartInput {
let negative = false;
let type = "bytes";

// Buffer:
// ArrayBuffer:
if (input instanceof ArrayBuffer) {
inputUint8 = new Uint8Array(input.slice());
}

// TypedArray or DataView:
// TypedArray/DataView or node Buffer:
else if (ArrayBuffer.isView(input)) {
inputUint8 = new Uint8Array(input.buffer.slice());
if (typeof Buffer !== "undefined" && input instanceof Buffer) {
inputUint8 = new Uint8Array(input);
} else {
inputUint8 = new Uint8Array(input.buffer.slice());
}
}

// String:
Expand Down Expand Up @@ -1337,6 +1342,7 @@ class BaseTemplate {
this.padding = false;
this.padCharAmount = 0;
this.padChars = {};
this.nonASCII = false;
this.signed = false;
this.upper = null;
if (appendUtils) this.utils = new Utils(this);
Expand Down Expand Up @@ -2100,6 +2106,9 @@ class UUencode extends BaseTemplate {

// predefined settings
this.padding = true;
this.buffering = false;
this.utils.converterArgs.buffering = ["nobuffering", "buffering"];
this.isMutable.buffering = true;
this.header = false;
this.utils.converterArgs.header = ["noheader", "header"];
this.isMutable.header = true;
Expand All @@ -2122,42 +2131,47 @@ class UUencode extends BaseTemplate {

const charset = this.charsets[settings.version];
const outArray = [...output];
const outLen = outArray.length;
settings.options.lineWrap = 0;


if (settings.header) {
if (settings.header && !settings.buffering) {
const permissions = settings.options.permissions || een();
const fileName = settings.options.file || ees();
output = `begin ${permissions} ${fileName}\n`;
} else {
output = "";
}

// repeatedly take 60 chars from the output until it is empty
for (;;) {
const lArray = outArray.splice(0, 60);
// repeatedly take 60 chars from the output
for (let start=0; start<outLen; start+=60) {
const end = start+60;
const lArray = outArray.slice(start, end);

// if all chars are taken, remove eventually added pad zeros
if (!outArray.length) {
if (end >= outLen) {
const byteCount = this.converter.padChars(lArray.length) - zeroPadding;

// add the the current chars plus the leading
// count char
output += `${charset.at(byteCount)}${lArray.join("")}\n`;
break;
}

// add the the current chars plus the leading
// count char ("M" for default charsets)
output += `${charset.at(45)}${lArray.join("")}\n`;
else {
output += `${charset.at(45)}${lArray.join("")}\n`;
}
}

output += `${charset.at(0)}\n`;

if (settings.header) {
output += "\nend";
if (!settings.buffering) {
output += `${charset.at(0)}\n`;

if (settings.header) {
output += "end\n";
}
}


return output;
};

Expand Down Expand Up @@ -2195,8 +2209,19 @@ class UUencode extends BaseTemplate {

inArray.push(...lArray);

if (byteCount !== 45) {
padChars = this.converter.padChars(lArray.length) - byteCount;
if (byteCount !== 45) {
let len = lArray.length;

// fix probably missing spaces for original charset
if (settings.version === "original") {
const expectedLen = calcUUStrLen(byteCount);
while (len < expectedLen) {
len++;
inArray.push(" ");
}
}

padChars = this.converter.padChars(len) - byteCount;
break;
}

Expand Down Expand Up @@ -2262,6 +2287,14 @@ const ees = () => {
return `${pick(name)}.${pick(ext)}`;
};

const calcUUStrLen = byteCount => {
const len = byteCount / 3 * 4;
if (len % 4) {
return Math.floor(len/4) * 4 + 4;
}
return len;
};

/**
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
*
Expand Down Expand Up @@ -2839,6 +2872,7 @@ class Ecoji extends BaseTemplate {
// predefined settings
this.padding = true;
this.padCharAmount = 5;
this.nonASCII = true;
this.version = "emojis_v2";

// mutable extra args
Expand Down Expand Up @@ -3001,7 +3035,7 @@ class Ecoji extends BaseTemplate {
const lastChar = inArray.at(-1);
let skipLast = false;

for (let i=0; i<this.padChars[version].length-1; i++) {
for (let i=0, l=this.padChars[version].length-1; i<l; i++) {
if (lastChar === this.padChars[version].at(i)) {
inArray.splice(-1, 1, charset.at(i << 8));
input = inArray.join("");
Expand Down Expand Up @@ -3172,6 +3206,7 @@ class Base2048 extends BaseTemplate {
this.padCharAmount = 8;
this.hasSignedMode = true;
this.littleEndian = false;
this.nonASCII = true;

// apply user settings
this.utils.validateArgs(args, true);
Expand Down
2 changes: 1 addition & 1 deletion cjs/base-ex.cjs.map

Large diffs are not rendered by default.

75 changes: 55 additions & 20 deletions dist/base-ex.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/
class BytesInput {
static toBytes(input) {
if (ArrayBuffer.isView(input)) {
if (ArrayBuffer.isView(input) && !(typeof Buffer !== "undefined" && input instanceof Buffer)) {
input = input.buffer;
}
}

return [new Uint8Array(input), false, "bytes"];
}
}
Expand Down Expand Up @@ -222,14 +223,18 @@ class SmartInput {
let negative = false;
let type = "bytes";

// Buffer:
// ArrayBuffer:
if (input instanceof ArrayBuffer) {
inputUint8 = new Uint8Array(input.slice());
}

// TypedArray or DataView:
// TypedArray/DataView or node Buffer:
else if (ArrayBuffer.isView(input)) {
inputUint8 = new Uint8Array(input.buffer.slice());
if (typeof Buffer !== "undefined" && input instanceof Buffer) {
inputUint8 = new Uint8Array(input);
} else {
inputUint8 = new Uint8Array(input.buffer.slice());
}
}

// String:
Expand Down Expand Up @@ -1335,6 +1340,7 @@ class BaseTemplate {
this.padding = false;
this.padCharAmount = 0;
this.padChars = {};
this.nonASCII = false;
this.signed = false;
this.upper = null;
if (appendUtils) this.utils = new Utils(this);
Expand Down Expand Up @@ -2098,6 +2104,9 @@ class UUencode extends BaseTemplate {

// predefined settings
this.padding = true;
this.buffering = false;
this.utils.converterArgs.buffering = ["nobuffering", "buffering"];
this.isMutable.buffering = true;
this.header = false;
this.utils.converterArgs.header = ["noheader", "header"];
this.isMutable.header = true;
Expand All @@ -2120,42 +2129,47 @@ class UUencode extends BaseTemplate {

const charset = this.charsets[settings.version];
const outArray = [...output];
const outLen = outArray.length;
settings.options.lineWrap = 0;


if (settings.header) {
if (settings.header && !settings.buffering) {
const permissions = settings.options.permissions || een();
const fileName = settings.options.file || ees();
output = `begin ${permissions} ${fileName}\n`;
} else {
output = "";
}

// repeatedly take 60 chars from the output until it is empty
for (;;) {
const lArray = outArray.splice(0, 60);
// repeatedly take 60 chars from the output
for (let start=0; start<outLen; start+=60) {
const end = start+60;
const lArray = outArray.slice(start, end);

// if all chars are taken, remove eventually added pad zeros
if (!outArray.length) {
if (end >= outLen) {
const byteCount = this.converter.padChars(lArray.length) - zeroPadding;

// add the the current chars plus the leading
// count char
output += `${charset.at(byteCount)}${lArray.join("")}\n`;
break;
}

// add the the current chars plus the leading
// count char ("M" for default charsets)
output += `${charset.at(45)}${lArray.join("")}\n`;
else {
output += `${charset.at(45)}${lArray.join("")}\n`;
}
}

output += `${charset.at(0)}\n`;

if (settings.header) {
output += "\nend";
if (!settings.buffering) {
output += `${charset.at(0)}\n`;

if (settings.header) {
output += "end\n";
}
}


return output;
};

Expand Down Expand Up @@ -2193,8 +2207,19 @@ class UUencode extends BaseTemplate {

inArray.push(...lArray);

if (byteCount !== 45) {
padChars = this.converter.padChars(lArray.length) - byteCount;
if (byteCount !== 45) {
let len = lArray.length;

// fix probably missing spaces for original charset
if (settings.version === "original") {
const expectedLen = calcUUStrLen(byteCount);
while (len < expectedLen) {
len++;
inArray.push(" ");
}
}

padChars = this.converter.padChars(len) - byteCount;
break;
}

Expand Down Expand Up @@ -2260,6 +2285,14 @@ const ees = () => {
return `${pick(name)}.${pick(ext)}`;
};

const calcUUStrLen = byteCount => {
const len = byteCount / 3 * 4;
if (len % 4) {
return Math.floor(len/4) * 4 + 4;
}
return len;
};

/**
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
*
Expand Down Expand Up @@ -2837,6 +2870,7 @@ class Ecoji extends BaseTemplate {
// predefined settings
this.padding = true;
this.padCharAmount = 5;
this.nonASCII = true;
this.version = "emojis_v2";

// mutable extra args
Expand Down Expand Up @@ -2999,7 +3033,7 @@ class Ecoji extends BaseTemplate {
const lastChar = inArray.at(-1);
let skipLast = false;

for (let i=0; i<this.padChars[version].length-1; i++) {
for (let i=0, l=this.padChars[version].length-1; i<l; i++) {
if (lastChar === this.padChars[version].at(i)) {
inArray.splice(-1, 1, charset.at(i << 8));
input = inArray.join("");
Expand Down Expand Up @@ -3170,6 +3204,7 @@ class Base2048 extends BaseTemplate {
this.padCharAmount = 8;
this.hasSignedMode = true;
this.littleEndian = false;
this.nonASCII = true;

// apply user settings
this.utils.validateArgs(args, true);
Expand Down
4 changes: 2 additions & 2 deletions dist/base-ex.esm.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 53ad0ae

Please sign in to comment.