Skip to content

Commit e1c9c5e

Browse files
committed
version bump 0.4.1
- normalized crc iteration logic - added browser demo - added command line tool crc32 - fixed unicode baseline script (node 6 changed default array printing) - fixed performance tests (benchmark module changed behavior) - updated travis versions for test - miscellaneous adjustments to tooling
1 parent 79a265b commit e1c9c5e

31 files changed

+805
-266
lines changed

.flowconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
.*/misc/.*
1010
.*/perf/.*
1111

12+
.*/demo/browser.js
13+
1214
[include]
1315
crc32.flow.js
16+
.*/demo/browser.flow.js
1417

1518
[libs]
19+
bits/10_types.js
20+
misc/flow.js
1621

1722
[options]

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ test_files/*.py
33
test_files/*.js
44
test_files/baseline*
55
misc/coverage.html
6-
misc/*/

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
language: node_js
22
node_js:
3-
- "5.0"
4-
- "4.2"
3+
- "6"
4+
- "5"
5+
- "4"
56
- "0.12"
67
- "0.10"
78
- "0.8"
89
before_install:
910
- "npm install -g npm@next"
10-
- "npm install -g mocha crc-32 benchmark ansi"
11+
- "npm install -g mocha"
1112
- "npm install codepage"
1213
- "npm install blanket"
1314
- "npm install coveralls mocha-lcov-reporter"
1415
after_success:
1516
- "make coveralls-spin"
16-
- "make perf-all"
17+
# - "make perf-all"

Makefile

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,88 @@
11
LIB=crc32
22
REQS=
33
ADDONS=
4-
AUXTARGETS=
4+
AUXTARGETS=demo/browser.js
5+
HTMLLINT=index.html
56

67
ULIB=$(shell echo $(LIB) | tr a-z A-Z)
78
DEPS=$(sort $(wildcard bits/*.js))
89
TARGET=$(LIB).js
10+
FLOWTARGET=$(LIB).flow.js
11+
12+
## Main Targets
913

1014
.PHONY: all
11-
all: $(TARGET) $(AUXTARGETS)
15+
all: $(TARGET) $(AUXTARGETS) ## Build library and auxiliary scripts
1216

1317
$(TARGET) $(AUXTARGETS): %.js : %.flow.js
14-
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^\s*\/\*:[^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*:[^*]*\*\//gm,""))' > $@
18+
node -e 'process.stdout.write(require("fs").readFileSync("$<","utf8").replace(/^[ \t]*\/\*[:#][^*]*\*\/\s*(\n)?/gm,"").replace(/\/\*[:#][^*]*\*\//gm,""))' > $@
1519

16-
$(LIB).flow.js: $(DEPS)
20+
$(FLOWTARGET): $(DEPS)
1721
cat $^ | tr -d '\15\32' > $@
1822

1923
bits/01_version.js: package.json
2024
echo "CRC32.version = '"`grep version package.json | awk '{gsub(/[^0-9a-z\.-]/,"",$$2); print $$2}'`"';" > $@
2125

2226
.PHONY: clean
23-
clean: clean-baseline
24-
rm -f $(TARGET)
27+
clean: clean-baseline ## Remove targets and build artifacts
28+
rm -f $(TARGET) $(FLOWTARGET)
29+
30+
## Testing
2531

2632
.PHONY: test mocha
27-
test mocha: test.js $(TARGET) baseline
33+
test mocha: test.js $(TARGET) baseline ## Run test suite
2834
mocha -R spec -t 20000
2935

3036
.PHONY: ctest
31-
ctest:
37+
ctest: ## Build browser test (into ctest/ subdirectory)
3238
cat misc/*.js > ctest/fixtures.js
3339
cp -f test.js ctest/test.js
3440
cp -f $(TARGET) ctest/
3541

42+
.PHONY: ctestserv
43+
ctestserv: ## Start a test server on port 8000
44+
@cd ctest && python -mSimpleHTTPServer
45+
46+
.PHONY: baseline
47+
baseline: ## Build test baselines
48+
@bash ./misc/make_baseline.sh
49+
50+
.PHONY: clean-baseline
51+
clean-baseline: ## Remove test baselines
52+
@bash ./misc/make_baseline.sh clean
53+
54+
## Code Checking
55+
3656
.PHONY: lint
37-
lint: $(TARGET) $(AUXTARGETS)
38-
jshint --show-non-errors $(TARGET) $(AUXTARGETS)
39-
jshint --show-non-errors package.json
40-
jscs $(TARGET) $(AUXTARGETS)
57+
lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
58+
@jshint --show-non-errors $(TARGET) $(AUXTARGETS)
59+
@jshint --show-non-errors package.json
60+
@jshint --show-non-errors --extract=always $(HTMLLINT)
61+
@jscs $(TARGET) $(AUXTARGETS)
4162

4263
.PHONY: flow
43-
flow: lint
44-
flow check --all --show-all-errors
64+
flow: lint ## Run flow checker
65+
@flow check --all --show-all-errors
4566

46-
.PHONY: cov cov-spin
47-
cov: misc/coverage.html
48-
cov-spin:
49-
make cov & bash misc/spin.sh $$!
50-
51-
COVFMT=$(patsubst %,cov_%,$(FMT))
52-
.PHONY: $(COVFMT)
53-
$(COVFMT): cov_%:
54-
FMTS=$* make cov
67+
.PHONY: cov
68+
cov: misc/coverage.html ## Run coverage test
5569

5670
misc/coverage.html: $(TARGET) test.js
5771
mocha --require blanket -R html-cov -t 20000 > $@
5872

59-
.PHONY: coveralls coveralls-spin
60-
coveralls:
61-
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | ./node_modules/coveralls/bin/coveralls.js
62-
63-
coveralls-spin:
64-
make coveralls & bash misc/spin.sh $$!
73+
.PHONY: coveralls
74+
coveralls: ## Coverage Test + Send to coveralls.io
75+
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js
6576

6677
.PHONY: perf
67-
perf:
68-
bash perf/perf.sh
78+
perf: ## Run Performance Tests
79+
@bash perf/perf.sh
6980

70-
.PHONY: perf-all
71-
perf-all:
72-
bash misc/perf.sh
7381

74-
.PHONY: baseline clean-baseline
75-
baseline:
76-
./misc/make_baseline.sh
82+
.PHONY: help
83+
help:
84+
@grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST) | bash misc/help.sh
7785

78-
clean-baseline:
79-
rm -f test_files/*.*
86+
#* To show a spinner, append "-spin" to any target e.g. cov-spin
87+
%-spin:
88+
@make $* & bash misc/spin.sh $$!

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ In the browser:
1313

1414
<script src="crc32.js"></script>
1515

16+
The browser exposes a variable ADLER32
17+
18+
When installed globally, npm installs a script `crc32` that computes the
19+
checksum for a specified file or standard input.
20+
1621
The script will manipulate `module.exports` if available (e.g. in a CommonJS
1722
`require` context). This is not always desirable. To prevent the behavior,
1823
define `DO_NOT_EXPORT_CRC`
@@ -46,13 +51,47 @@ For example:
4651

4752
## Testing
4853

49-
`make test` will run the node-based tests.
54+
`make test` will run the nodejs-based test.
5055

5156
To run the in-browser tests, run a local server and go to the `ctest` directory.
57+
`make ctestserv` will start a python `SimpleHTTPServer` server on port 8000.
58+
5259
To update the browser artifacts, run `make ctest`.
5360

54-
`make baseline` will generate baseline files based on the unicode mapping at
55-
<http://mathias.html5.org>
61+
To generate the bits file, use the `crc32` function from python zlib:
62+
63+
```python
64+
>>> from zlib import crc32
65+
>>> x="foo bar baz٪☃🍣"
66+
>>> crc32(x)
67+
1531648243
68+
>>> crc32(x+x)
69+
-218791105
70+
>>> crc32(x+x+x)
71+
1834240887
72+
```
73+
74+
The included `crc32.njs` script can process files or stdin:
75+
76+
```
77+
$ echo "this is a test" > t.txt
78+
$ bin/crc32.njs t.txt
79+
1912935186
80+
```
81+
82+
For comparison, the included `crc32.py` script uses python zlib:
83+
84+
```
85+
$ bin/crc32.py t.txt
86+
1912935186
87+
```
88+
89+
## Performance
90+
91+
`make perf` will run algorithmic performance tests (which should justify certain
92+
decisions in the code).
93+
94+
[js-adler32](http://git.io/adler32) has more performance notes
5695

5796
## License
5897

bin/crc32.njs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
3+
/* vim: set ts=2 ft=javascript: */
4+
5+
var X;
6+
try { X = require('../'); } catch(e) { X = require('crc-32'); }
7+
var fs = require('fs');
8+
require('exit-on-epipe');
9+
10+
var args = process.argv.slice(2);
11+
12+
var filename;
13+
if(args[0]) filename = args[0];
14+
15+
if(!process.stdin.isTTY) filename = filename || "-";
16+
17+
if(!filename) {
18+
console.error("crc32: must specify a filename ('-' for stdin)");
19+
process.exit(1);
20+
}
21+
22+
if(filename === "-h" || filename === "--help") {
23+
console.log("usage: " + process.argv[0] + " [filename]");
24+
process.exit(0);
25+
}
26+
27+
if(filename !== "-" && !fs.existsSync(filename)) {
28+
console.error("crc32: " + filename + ": No such file or directory");
29+
process.exit(2);
30+
}
31+
32+
if(filename === "-") process.stdin.pipe(require('concat-stream')(process_data));
33+
else process_data(fs.readFileSync(filename));
34+
35+
function process_data(data) {
36+
console.log(X.buf(data));
37+
}

bin/crc32.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# crc32.py -- calculate crc32 checksum of data
3+
# Copyright (C) 2016-present SheetJS
4+
from zlib import crc32
5+
from sys import argv, stdin
6+
7+
args=argv[1:]
8+
payload=""
9+
if len(args) == 0 or args[0] == "-":
10+
payload = stdin.read()
11+
else:
12+
payload = open(args[0],"rb").read()
13+
14+
# NOTE: python 2 returns a signed value; python3 is unsigned
15+
print crc32(payload)

bits/00_header.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
22
/* vim: set ts=2: */
3+
/*exported CRC32 */
34
var CRC32;
45
/*:: declare var DO_NOT_EXPORT_CRC: any; */
56
/*:: declare var define: any; */
67
(function (factory) {
8+
/*jshint ignore:start */
79
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
810
if('object' === typeof exports) {
911
factory(exports);
@@ -19,4 +21,5 @@ var CRC32;
1921
} else {
2022
factory(CRC32 = {});
2123
}
24+
/*jshint ignore:end */
2225
}(function(CRC32) {

bits/01_version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CRC32.version = '0.4.0';
1+
CRC32.version = '0.4.1';

bits/20_crctable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* see perf/crc32table.js */
2+
/*global Int32Array */
23
function signed_crc_table()/*:CRC32TableType*/ {
34
var c = 0, table/*:Array<number>*/ = new Array(256);
45

@@ -18,4 +19,4 @@ function signed_crc_table()/*:CRC32TableType*/ {
1819
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
1920
}
2021

21-
var table = signed_crc_table();
22+
var T = signed_crc_table();

0 commit comments

Comments
 (0)