Skip to content

Commit 53ad0ae

Browse files
Merge pull request #56 from UmamiAppearance/support-node-Buffer
Support node buffer
2 parents 2ad46c3 + ac5245b commit 53ad0ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+695
-333
lines changed

cjs/base-ex.cjs

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
*/
1010
class BytesInput {
1111
static toBytes(input) {
12-
if (ArrayBuffer.isView(input)) {
12+
if (ArrayBuffer.isView(input) && !(typeof Buffer !== "undefined" && input instanceof Buffer)) {
1313
input = input.buffer;
14-
}
14+
}
15+
1516
return [new Uint8Array(input), false, "bytes"];
1617
}
1718
}
@@ -224,14 +225,18 @@ class SmartInput {
224225
let negative = false;
225226
let type = "bytes";
226227

227-
// Buffer:
228+
// ArrayBuffer:
228229
if (input instanceof ArrayBuffer) {
229230
inputUint8 = new Uint8Array(input.slice());
230231
}
231232

232-
// TypedArray or DataView:
233+
// TypedArray/DataView or node Buffer:
233234
else if (ArrayBuffer.isView(input)) {
234-
inputUint8 = new Uint8Array(input.buffer.slice());
235+
if (typeof Buffer !== "undefined" && input instanceof Buffer) {
236+
inputUint8 = new Uint8Array(input);
237+
} else {
238+
inputUint8 = new Uint8Array(input.buffer.slice());
239+
}
235240
}
236241

237242
// String:
@@ -1337,6 +1342,7 @@ class BaseTemplate {
13371342
this.padding = false;
13381343
this.padCharAmount = 0;
13391344
this.padChars = {};
1345+
this.nonASCII = false;
13401346
this.signed = false;
13411347
this.upper = null;
13421348
if (appendUtils) this.utils = new Utils(this);
@@ -2100,6 +2106,9 @@ class UUencode extends BaseTemplate {
21002106

21012107
// predefined settings
21022108
this.padding = true;
2109+
this.buffering = false;
2110+
this.utils.converterArgs.buffering = ["nobuffering", "buffering"];
2111+
this.isMutable.buffering = true;
21032112
this.header = false;
21042113
this.utils.converterArgs.header = ["noheader", "header"];
21052114
this.isMutable.header = true;
@@ -2122,42 +2131,47 @@ class UUencode extends BaseTemplate {
21222131

21232132
const charset = this.charsets[settings.version];
21242133
const outArray = [...output];
2134+
const outLen = outArray.length;
2135+
settings.options.lineWrap = 0;
21252136

21262137

2127-
if (settings.header) {
2138+
if (settings.header && !settings.buffering) {
21282139
const permissions = settings.options.permissions || een();
21292140
const fileName = settings.options.file || ees();
21302141
output = `begin ${permissions} ${fileName}\n`;
21312142
} else {
21322143
output = "";
21332144
}
21342145

2135-
// repeatedly take 60 chars from the output until it is empty
2136-
for (;;) {
2137-
const lArray = outArray.splice(0, 60);
2146+
// repeatedly take 60 chars from the output
2147+
for (let start=0; start<outLen; start+=60) {
2148+
const end = start+60;
2149+
const lArray = outArray.slice(start, end);
21382150

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

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

21492160
// add the the current chars plus the leading
21502161
// count char ("M" for default charsets)
2151-
output += `${charset.at(45)}${lArray.join("")}\n`;
2162+
else {
2163+
output += `${charset.at(45)}${lArray.join("")}\n`;
2164+
}
21522165
}
21532166

2154-
output += `${charset.at(0)}\n`;
2155-
2156-
if (settings.header) {
2157-
output += "\nend";
2167+
if (!settings.buffering) {
2168+
output += `${charset.at(0)}\n`;
2169+
2170+
if (settings.header) {
2171+
output += "end\n";
2172+
}
21582173
}
21592174

2160-
21612175
return output;
21622176
};
21632177

@@ -2195,8 +2209,19 @@ class UUencode extends BaseTemplate {
21952209

21962210
inArray.push(...lArray);
21972211

2198-
if (byteCount !== 45) {
2199-
padChars = this.converter.padChars(lArray.length) - byteCount;
2212+
if (byteCount !== 45) {
2213+
let len = lArray.length;
2214+
2215+
// fix probably missing spaces for original charset
2216+
if (settings.version === "original") {
2217+
const expectedLen = calcUUStrLen(byteCount);
2218+
while (len < expectedLen) {
2219+
len++;
2220+
inArray.push(" ");
2221+
}
2222+
}
2223+
2224+
padChars = this.converter.padChars(len) - byteCount;
22002225
break;
22012226
}
22022227

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

2290+
const calcUUStrLen = byteCount => {
2291+
const len = byteCount / 3 * 4;
2292+
if (len % 4) {
2293+
return Math.floor(len/4) * 4 + 4;
2294+
}
2295+
return len;
2296+
};
2297+
22652298
/**
22662299
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
22672300
*
@@ -2839,6 +2872,7 @@ class Ecoji extends BaseTemplate {
28392872
// predefined settings
28402873
this.padding = true;
28412874
this.padCharAmount = 5;
2875+
this.nonASCII = true;
28422876
this.version = "emojis_v2";
28432877

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

3004-
for (let i=0; i<this.padChars[version].length-1; i++) {
3038+
for (let i=0, l=this.padChars[version].length-1; i<l; i++) {
30053039
if (lastChar === this.padChars[version].at(i)) {
30063040
inArray.splice(-1, 1, charset.at(i << 8));
30073041
input = inArray.join("");
@@ -3172,6 +3206,7 @@ class Base2048 extends BaseTemplate {
31723206
this.padCharAmount = 8;
31733207
this.hasSignedMode = true;
31743208
this.littleEndian = false;
3209+
this.nonASCII = true;
31753210

31763211
// apply user settings
31773212
this.utils.validateArgs(args, true);

cjs/base-ex.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/base-ex.esm.js

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
class BytesInput {
99
static toBytes(input) {
10-
if (ArrayBuffer.isView(input)) {
10+
if (ArrayBuffer.isView(input) && !(typeof Buffer !== "undefined" && input instanceof Buffer)) {
1111
input = input.buffer;
12-
}
12+
}
13+
1314
return [new Uint8Array(input), false, "bytes"];
1415
}
1516
}
@@ -222,14 +223,18 @@ class SmartInput {
222223
let negative = false;
223224
let type = "bytes";
224225

225-
// Buffer:
226+
// ArrayBuffer:
226227
if (input instanceof ArrayBuffer) {
227228
inputUint8 = new Uint8Array(input.slice());
228229
}
229230

230-
// TypedArray or DataView:
231+
// TypedArray/DataView or node Buffer:
231232
else if (ArrayBuffer.isView(input)) {
232-
inputUint8 = new Uint8Array(input.buffer.slice());
233+
if (typeof Buffer !== "undefined" && input instanceof Buffer) {
234+
inputUint8 = new Uint8Array(input);
235+
} else {
236+
inputUint8 = new Uint8Array(input.buffer.slice());
237+
}
233238
}
234239

235240
// String:
@@ -1335,6 +1340,7 @@ class BaseTemplate {
13351340
this.padding = false;
13361341
this.padCharAmount = 0;
13371342
this.padChars = {};
1343+
this.nonASCII = false;
13381344
this.signed = false;
13391345
this.upper = null;
13401346
if (appendUtils) this.utils = new Utils(this);
@@ -2098,6 +2104,9 @@ class UUencode extends BaseTemplate {
20982104

20992105
// predefined settings
21002106
this.padding = true;
2107+
this.buffering = false;
2108+
this.utils.converterArgs.buffering = ["nobuffering", "buffering"];
2109+
this.isMutable.buffering = true;
21012110
this.header = false;
21022111
this.utils.converterArgs.header = ["noheader", "header"];
21032112
this.isMutable.header = true;
@@ -2120,42 +2129,47 @@ class UUencode extends BaseTemplate {
21202129

21212130
const charset = this.charsets[settings.version];
21222131
const outArray = [...output];
2132+
const outLen = outArray.length;
2133+
settings.options.lineWrap = 0;
21232134

21242135

2125-
if (settings.header) {
2136+
if (settings.header && !settings.buffering) {
21262137
const permissions = settings.options.permissions || een();
21272138
const fileName = settings.options.file || ees();
21282139
output = `begin ${permissions} ${fileName}\n`;
21292140
} else {
21302141
output = "";
21312142
}
21322143

2133-
// repeatedly take 60 chars from the output until it is empty
2134-
for (;;) {
2135-
const lArray = outArray.splice(0, 60);
2144+
// repeatedly take 60 chars from the output
2145+
for (let start=0; start<outLen; start+=60) {
2146+
const end = start+60;
2147+
const lArray = outArray.slice(start, end);
21362148

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

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

21472158
// add the the current chars plus the leading
21482159
// count char ("M" for default charsets)
2149-
output += `${charset.at(45)}${lArray.join("")}\n`;
2160+
else {
2161+
output += `${charset.at(45)}${lArray.join("")}\n`;
2162+
}
21502163
}
21512164

2152-
output += `${charset.at(0)}\n`;
2153-
2154-
if (settings.header) {
2155-
output += "\nend";
2165+
if (!settings.buffering) {
2166+
output += `${charset.at(0)}\n`;
2167+
2168+
if (settings.header) {
2169+
output += "end\n";
2170+
}
21562171
}
21572172

2158-
21592173
return output;
21602174
};
21612175

@@ -2193,8 +2207,19 @@ class UUencode extends BaseTemplate {
21932207

21942208
inArray.push(...lArray);
21952209

2196-
if (byteCount !== 45) {
2197-
padChars = this.converter.padChars(lArray.length) - byteCount;
2210+
if (byteCount !== 45) {
2211+
let len = lArray.length;
2212+
2213+
// fix probably missing spaces for original charset
2214+
if (settings.version === "original") {
2215+
const expectedLen = calcUUStrLen(byteCount);
2216+
while (len < expectedLen) {
2217+
len++;
2218+
inArray.push(" ");
2219+
}
2220+
}
2221+
2222+
padChars = this.converter.padChars(len) - byteCount;
21982223
break;
21992224
}
22002225

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

2288+
const calcUUStrLen = byteCount => {
2289+
const len = byteCount / 3 * 4;
2290+
if (len % 4) {
2291+
return Math.floor(len/4) * 4 + 4;
2292+
}
2293+
return len;
2294+
};
2295+
22632296
/**
22642297
* [BaseEx|Base85 Converter]{@link https://github.com/UmamiAppearance/BaseExJS/blob/main/src/converters/base-85.js}
22652298
*
@@ -2837,6 +2870,7 @@ class Ecoji extends BaseTemplate {
28372870
// predefined settings
28382871
this.padding = true;
28392872
this.padCharAmount = 5;
2873+
this.nonASCII = true;
28402874
this.version = "emojis_v2";
28412875

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

3002-
for (let i=0; i<this.padChars[version].length-1; i++) {
3036+
for (let i=0, l=this.padChars[version].length-1; i<l; i++) {
30033037
if (lastChar === this.padChars[version].at(i)) {
30043038
inArray.splice(-1, 1, charset.at(i << 8));
30053039
input = inArray.join("");
@@ -3170,6 +3204,7 @@ class Base2048 extends BaseTemplate {
31703204
this.padCharAmount = 8;
31713205
this.hasSignedMode = true;
31723206
this.littleEndian = false;
3207+
this.nonASCII = true;
31733208

31743209
// apply user settings
31753210
this.utils.validateArgs(args, true);

dist/base-ex.esm.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)