Skip to content

Commit

Permalink
version bump 0.4.1
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
SheetJSDev committed Jun 16, 2016
1 parent 79a265b commit e1c9c5e
Show file tree
Hide file tree
Showing 31 changed files with 805 additions and 266 deletions.
5 changes: 5 additions & 0 deletions .flowconfig
Expand Up @@ -9,9 +9,14 @@
.*/misc/.*
.*/perf/.*

.*/demo/browser.js

[include]
crc32.flow.js
.*/demo/browser.flow.js

[libs]
bits/10_types.js
misc/flow.js

[options]
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,3 @@ test_files/*.py
test_files/*.js
test_files/baseline*
misc/coverage.html
misc/*/
9 changes: 5 additions & 4 deletions .travis.yml
@@ -1,16 +1,17 @@
language: node_js
node_js:
- "5.0"
- "4.2"
- "6"
- "5"
- "4"
- "0.12"
- "0.10"
- "0.8"
before_install:
- "npm install -g npm@next"
- "npm install -g mocha crc-32 benchmark ansi"
- "npm install -g mocha"
- "npm install codepage"
- "npm install blanket"
- "npm install coveralls mocha-lcov-reporter"
after_success:
- "make coveralls-spin"
- "make perf-all"
# - "make perf-all"
87 changes: 48 additions & 39 deletions Makefile
@@ -1,79 +1,88 @@
LIB=crc32
REQS=
ADDONS=
AUXTARGETS=
AUXTARGETS=demo/browser.js
HTMLLINT=index.html

ULIB=$(shell echo $(LIB) | tr a-z A-Z)
DEPS=$(sort $(wildcard bits/*.js))
TARGET=$(LIB).js
FLOWTARGET=$(LIB).flow.js

## Main Targets

.PHONY: all
all: $(TARGET) $(AUXTARGETS)
all: $(TARGET) $(AUXTARGETS) ## Build library and auxiliary scripts

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

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

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

.PHONY: clean
clean: clean-baseline
rm -f $(TARGET)
clean: clean-baseline ## Remove targets and build artifacts
rm -f $(TARGET) $(FLOWTARGET)

## Testing

.PHONY: test mocha
test mocha: test.js $(TARGET) baseline
test mocha: test.js $(TARGET) baseline ## Run test suite
mocha -R spec -t 20000

.PHONY: ctest
ctest:
ctest: ## Build browser test (into ctest/ subdirectory)
cat misc/*.js > ctest/fixtures.js
cp -f test.js ctest/test.js
cp -f $(TARGET) ctest/

.PHONY: ctestserv
ctestserv: ## Start a test server on port 8000
@cd ctest && python -mSimpleHTTPServer

.PHONY: baseline
baseline: ## Build test baselines
@bash ./misc/make_baseline.sh

.PHONY: clean-baseline
clean-baseline: ## Remove test baselines
@bash ./misc/make_baseline.sh clean

## Code Checking

.PHONY: lint
lint: $(TARGET) $(AUXTARGETS)
jshint --show-non-errors $(TARGET) $(AUXTARGETS)
jshint --show-non-errors package.json
jscs $(TARGET) $(AUXTARGETS)
lint: $(TARGET) $(AUXTARGETS) ## Run jshint and jscs checks
@jshint --show-non-errors $(TARGET) $(AUXTARGETS)
@jshint --show-non-errors package.json
@jshint --show-non-errors --extract=always $(HTMLLINT)
@jscs $(TARGET) $(AUXTARGETS)

.PHONY: flow
flow: lint
flow check --all --show-all-errors
flow: lint ## Run flow checker
@flow check --all --show-all-errors

.PHONY: cov cov-spin
cov: misc/coverage.html
cov-spin:
make cov & bash misc/spin.sh $$!

COVFMT=$(patsubst %,cov_%,$(FMT))
.PHONY: $(COVFMT)
$(COVFMT): cov_%:
FMTS=$* make cov
.PHONY: cov
cov: misc/coverage.html ## Run coverage test

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

.PHONY: coveralls coveralls-spin
coveralls:
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | ./node_modules/coveralls/bin/coveralls.js

coveralls-spin:
make coveralls & bash misc/spin.sh $$!
.PHONY: coveralls
coveralls: ## Coverage Test + Send to coveralls.io
mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js

.PHONY: perf
perf:
bash perf/perf.sh
perf: ## Run Performance Tests
@bash perf/perf.sh

.PHONY: perf-all
perf-all:
bash misc/perf.sh

.PHONY: baseline clean-baseline
baseline:
./misc/make_baseline.sh
.PHONY: help
help:
@grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST) | bash misc/help.sh

clean-baseline:
rm -f test_files/*.*
#* To show a spinner, append "-spin" to any target e.g. cov-spin
%-spin:
@make $* & bash misc/spin.sh $$!
45 changes: 42 additions & 3 deletions README.md
Expand Up @@ -13,6 +13,11 @@ In the browser:

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

The browser exposes a variable ADLER32

When installed globally, npm installs a script `crc32` that computes the
checksum for a specified file or standard input.

The script will manipulate `module.exports` if available (e.g. in a CommonJS
`require` context). This is not always desirable. To prevent the behavior,
define `DO_NOT_EXPORT_CRC`
Expand Down Expand Up @@ -46,13 +51,47 @@ For example:

## Testing

`make test` will run the node-based tests.
`make test` will run the nodejs-based test.

To run the in-browser tests, run a local server and go to the `ctest` directory.
`make ctestserv` will start a python `SimpleHTTPServer` server on port 8000.

To update the browser artifacts, run `make ctest`.

`make baseline` will generate baseline files based on the unicode mapping at
<http://mathias.html5.org>
To generate the bits file, use the `crc32` function from python zlib:

```python
>>> from zlib import crc32
>>> x="foo bar baz٪☃🍣"
>>> crc32(x)
1531648243
>>> crc32(x+x)
-218791105
>>> crc32(x+x+x)
1834240887
```

The included `crc32.njs` script can process files or stdin:

```
$ echo "this is a test" > t.txt
$ bin/crc32.njs t.txt
1912935186
```

For comparison, the included `crc32.py` script uses python zlib:

```
$ bin/crc32.py t.txt
1912935186
```

## Performance

`make perf` will run algorithmic performance tests (which should justify certain
decisions in the code).

[js-adler32](http://git.io/adler32) has more performance notes

## License

Expand Down
37 changes: 37 additions & 0 deletions bin/crc32.njs
@@ -0,0 +1,37 @@
#!/usr/bin/env node
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2 ft=javascript: */

var X;
try { X = require('../'); } catch(e) { X = require('crc-32'); }
var fs = require('fs');
require('exit-on-epipe');

var args = process.argv.slice(2);

var filename;
if(args[0]) filename = args[0];

if(!process.stdin.isTTY) filename = filename || "-";

if(!filename) {
console.error("crc32: must specify a filename ('-' for stdin)");
process.exit(1);
}

if(filename === "-h" || filename === "--help") {
console.log("usage: " + process.argv[0] + " [filename]");
process.exit(0);
}

if(filename !== "-" && !fs.existsSync(filename)) {
console.error("crc32: " + filename + ": No such file or directory");
process.exit(2);
}

if(filename === "-") process.stdin.pipe(require('concat-stream')(process_data));
else process_data(fs.readFileSync(filename));

function process_data(data) {
console.log(X.buf(data));
}
15 changes: 15 additions & 0 deletions bin/crc32.py
@@ -0,0 +1,15 @@
#!/usr/bin/env python
# crc32.py -- calculate crc32 checksum of data
# Copyright (C) 2016-present SheetJS
from zlib import crc32
from sys import argv, stdin

args=argv[1:]
payload=""
if len(args) == 0 or args[0] == "-":
payload = stdin.read()
else:
payload = open(args[0],"rb").read()

# NOTE: python 2 returns a signed value; python3 is unsigned
print crc32(payload)
3 changes: 3 additions & 0 deletions bits/00_header.js
@@ -1,9 +1,11 @@
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*exported CRC32 */
var CRC32;
/*:: declare var DO_NOT_EXPORT_CRC: any; */
/*:: declare var define: any; */
(function (factory) {
/*jshint ignore:start */
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
if('object' === typeof exports) {
factory(exports);
Expand All @@ -19,4 +21,5 @@ var CRC32;
} else {
factory(CRC32 = {});
}
/*jshint ignore:end */
}(function(CRC32) {
2 changes: 1 addition & 1 deletion bits/01_version.js
@@ -1 +1 @@
CRC32.version = '0.4.0';
CRC32.version = '0.4.1';
3 changes: 2 additions & 1 deletion bits/20_crctable.js
@@ -1,4 +1,5 @@
/* see perf/crc32table.js */
/*global Int32Array */
function signed_crc_table()/*:CRC32TableType*/ {
var c = 0, table/*:Array<number>*/ = new Array(256);

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

var table = signed_crc_table();
var T = signed_crc_table();

0 comments on commit e1c9c5e

Please sign in to comment.