Skip to content

Commit cbdc948

Browse files
committed
version bump 1.14.0: Buffer.from node 4.x fix
1 parent 58ebd1f commit cbdc948

37 files changed

+203
-97
lines changed

codepage.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,28 @@ while the input format is automatically determined.
448448

449449
# Tests
450450

451-
The tests include JS validity tests (requiring or evaluating code):
452-
453451
```>test.js
454452
var fs = require('fs'), assert = require('assert'), vm = require('vm');
455453
var cptable, sbcs;
454+
455+
```
456+
457+
Due to a bug in `Buffer.from` in node `4.0 - 4.4`, a special check is needed:
458+
459+
```>test.js
460+
var Buffer_from = function(){};
461+
462+
if(typeof Buffer !== 'undefined') {
463+
var nbfs = !Buffer.from;
464+
if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
465+
Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
466+
}
467+
468+
```
469+
470+
The tests include JS validity tests (requiring or evaluating code):
471+
472+
```>test.js
456473
describe('source', function() {
457474
it('should load node', function() { cptable = require('./'); });
458475
it('should load sbcs', function() { sbcs = require('./sbcs'); });
@@ -565,7 +582,7 @@ describe('entry conditions', function() {
565582
var arr = cptable.utils.encode(cp,i.split(""),e);
566583
assert.deepEqual(str,arr);
567584
if(typeof Buffer === 'undefined') return;
568-
var buf = cptable.utils.encode(cp,Buffer.from(i),e);
585+
var buf = cptable.utils.encode(cp,Buffer_from(i),e);
569586
assert.deepEqual(str,buf);
570587
};
571588
cptable.utils.cache.encache();
@@ -593,7 +610,7 @@ describe('entry conditions', function() {
593610
var arr = cptable.utils.decode(cp,s.join?s.join(""):s);
594611
assert.deepEqual(str,arr);
595612
if(typeof Buffer === 'undefined') return;
596-
var buf = cptable.utils.decode(cp,Buffer.from(i));
613+
var buf = cptable.utils.decode(cp,Buffer_from(i));
597614
assert.deepEqual(str,buf);
598615
};
599616
cptable.utils.cache.encache();
@@ -603,7 +620,7 @@ describe('entry conditions', function() {
603620
};
604621
describe('decode', function() {
605622
it('CP 1252 : sbcs', function() { chkde(1252,[0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72]); }); /* "foobar" */
606-
if(typeof Buffer !== 'undefined') it('CP 708 : sbcs', function() { chkde(708, Buffer.from([0xca, 0x20, 0x61, 0x6e, 0x64, 0x20, 0xcb, 0x20, 0x73, 0x6d, 0x69, 0x6c, 0x65, 0x79, 0x20, 0x66, 0x61, 0x63, 0x65, 0x73])); }); /* ("ت and ث smiley faces") */
623+
if(typeof Buffer !== 'undefined') it('CP 708 : sbcs', function() { chkde(708, Buffer_from([0xca, 0x20, 0x61, 0x6e, 0x64, 0x20, 0xcb, 0x20, 0x73, 0x6d, 0x69, 0x6c, 0x65, 0x79, 0x20, 0x66, 0x61, 0x63, 0x65, 0x73])); }); /* ("ت and ث smiley faces") */
607624
it('CP 936 : dbcs', function() { chkde(936, [0xd5, 0xe2, 0xca, 0xc7, 0xd6, 0xd0, 0xce, 0xc4, 0xd7, 0xd6, 0xb7, 0xfb, 0xb2, 0xe2, 0xca, 0xd4]);}); /* "这是中文字符测试" */
608625
});
609626
});
@@ -754,7 +771,7 @@ describe('failures', function() {
754771
```json>package.json
755772
{
756773
"name": "codepage",
757-
"version": "1.13.0",
774+
"version": "1.14.0",
758775
"author": "SheetJS",
759776
"description": "pure-JS library to handle codepages",
760777
"keywords": [ "codepage", "iconv", "convert", "strings" ],
@@ -807,6 +824,7 @@ describe('failures', function() {
807824
"LICENSE",
808825
"README.md",
809826
"bin",
827+
"bits/*.js",
810828
"types/index.d.ts",
811829
"types/*.json",
812830
"cptable.js",
@@ -825,10 +843,12 @@ describe('failures', function() {
825843
```
826844

827845
```>.gitignore
846+
node_modules
847+
package-lock.json
848+
*.tgz
828849
.gitignore
829850
codepages/
830851
.vocrc
831-
node_modules/
832852
make.sh
833853
make.njs
834854
misc/coverage.html

cpexcel.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cptable.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cputils.flow.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ type DecoderMap = {[id:CPIndex]:Decoder};
4747
var cca = function cca(x/*:string*/)/*:number*/ { return x.charCodeAt(0); };
4848

4949
var has_buf/*:boolean*/ = (typeof Buffer !== 'undefined');
50+
var Buffer_from = function(){};
5051
if(has_buf) {
51-
// $FlowIgnore
52-
if(!Buffer.from) Buffer.from = function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); };
52+
var nbfs = !Buffer.from;
53+
if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
54+
Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
5355
// $FlowIgnore
5456
if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
5557

@@ -330,7 +332,7 @@ type DecoderMap = {[id:CPIndex]:Decoder};
330332
}
331333
else if((M=magic[cp])) switch(M) {
332334
case "utf8":
333-
if(has_buf && isstr/*:: && typeof data == 'string' */) { out = Buffer.from(data, M); j = out.length; break; }
335+
if(has_buf && isstr/*:: && typeof data == 'string' */) { out = Buffer_from(data, M); j = out.length; break; }
334336
for(i = 0; i < len; ++i, ++j) {
335337
w = isstr/*:: && typeof data == 'string' */ ? data.charCodeAt(i) : data[i].charCodeAt(0);
336338
if(w <= 0x007F) out[j] = w;
@@ -352,15 +354,15 @@ type DecoderMap = {[id:CPIndex]:Decoder};
352354
}
353355
break;
354356
case "ascii":
355-
if(has_buf && typeof data === "string") { out = Buffer.from(data, M); j = out.length; break; }
357+
if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
356358
for(i = 0; i < len; ++i, ++j) {
357359
w = isstr/*:: && typeof data == 'string' */ ? data.charCodeAt(i) : data[i].charCodeAt(0);
358360
if(w <= 0x007F) out[j] = w;
359361
else throw new Error("bad ascii " + w);
360362
}
361363
break;
362364
case "utf16le":
363-
if(has_buf && typeof data === "string") { out = Buffer.from(data, M); j = out.length; break; }
365+
if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
364366
for(i = 0; i < len; ++i) {
365367
w = isstr/*:: && typeof data == 'string' */ ? data.charCodeAt(i) : data[i].charCodeAt(0);
366368
out[j++] = w&255;

cputils.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
var cca = function cca(x) { return x.charCodeAt(0); };
4141

4242
var has_buf = (typeof Buffer !== 'undefined');
43+
var Buffer_from = function(){};
4344
if(has_buf) {
44-
// $FlowIgnore
45-
if(!Buffer.from) Buffer.from = function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); };
45+
var nbfs = !Buffer.from;
46+
if(!nbfs) try { Buffer.from("foo", "utf8"); } catch(e) { nbfs = true; }
47+
Buffer_from = nbfs ? function(buf, enc) { return (enc) ? new Buffer(buf, enc) : new Buffer(buf); } : Buffer.from.bind(Buffer);
4648
// $FlowIgnore
4749
if(!Buffer.allocUnsafe) Buffer.allocUnsafe = function(n) { return new Buffer(n); };
4850

@@ -315,7 +317,7 @@
315317
}
316318
else if((M=magic[cp])) switch(M) {
317319
case "utf8":
318-
if(has_buf && isstr) { out = Buffer.from(data, M); j = out.length; break; }
320+
if(has_buf && isstr) { out = Buffer_from(data, M); j = out.length; break; }
319321
for(i = 0; i < len; ++i, ++j) {
320322
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
321323
if(w <= 0x007F) out[j] = w;
@@ -337,15 +339,15 @@
337339
}
338340
break;
339341
case "ascii":
340-
if(has_buf && typeof data === "string") { out = Buffer.from(data, M); j = out.length; break; }
342+
if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
341343
for(i = 0; i < len; ++i, ++j) {
342344
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
343345
if(w <= 0x007F) out[j] = w;
344346
else throw new Error("bad ascii " + w);
345347
}
346348
break;
347349
case "utf16le":
348-
if(has_buf && typeof data === "string") { out = Buffer.from(data, M); j = out.length; break; }
350+
if(has_buf && typeof data === "string") { out = Buffer_from(data, M); j = out.length; break; }
349351
for(i = 0; i < len; ++i) {
350352
w = isstr ? data.charCodeAt(i) : data[i].charCodeAt(0);
351353
out[j++] = w&255;

ctest/fixtures.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cpexcel.full.js

+8-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cpexcel.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)