-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add flow annotations #27
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"env": { | ||
"development": { | ||
"sourceMaps": "inline", | ||
"comments": false, | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
], | ||
"flow-node" | ||
] | ||
}, | ||
"umd": { | ||
"comments": false, | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"modules": false, | ||
"targets": { | ||
"browsers": "last 2 versions" | ||
} | ||
} | ||
], | ||
"flow-node" | ||
] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[ignore] | ||
.*/node_modules/documentation/* | ||
|
||
[libs] | ||
|
||
[include] | ||
|
||
[options] | ||
suppress_comment= \\(.\\|\n\\)*\\@FlowIgnore |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,32 @@ | |
"description": "multiple hash functions", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"lint": "aegir lint", | ||
"build": "aegir build", | ||
"test": "aegir test -t node -t browser -t webworker", | ||
"lint": "npm run type-check && aegir lint", | ||
"type-check": "flow check", | ||
"build": "npm run build:node && BABEL_ENV=umd aegir build", | ||
"test": "npm run build:node && aegir test -t node -t browser -t webworker", | ||
"test:node": "aegir test -t node", | ||
"test:browser": "aegir test -t browser", | ||
"test:webworker": "aegir test -t webworker", | ||
"release": "aegir release -t node -t browser", | ||
"release-minor": "aegir release --type minor -t node -t browser", | ||
"release-major": "aegir release --type major -t node -t browser", | ||
"coverage": "aegir coverage", | ||
"coverage-publish": "aegir coverage --provider coveralls" | ||
"coverage-publish": "aegir coverage --provider coveralls", | ||
"build:types": "flow-copy-source --verbose src lib", | ||
"build:lib": "babel --out-dir lib src", | ||
"build:node": "npm run build:types && npm run build:lib", | ||
"start": "flow-copy-source --watch --verbose src lib & babel --watch --out-dir lib src" | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
], | ||
"standard": { | ||
"ignore": [ | ||
"dist" | ||
] | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/multiformats/js-multihashing.git" | ||
|
@@ -33,16 +43,25 @@ | |
"url": "https://github.com/multiformats/js-multihashing/issues" | ||
}, | ||
"dependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-loader": "^7.1.4", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those 3 are |
||
"blakejs": "^1.1.0", | ||
"js-sha3": "^0.7.0", | ||
"multihashes": "~0.4.12", | ||
"webcrypto": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^12.3.0", | ||
"aegir": "git+https://github.com/ipfs/aegir.git#flow", | ||
"babel-preset-flow-node": "^2.0.1", | ||
"chai": "^4.1.2", | ||
"dirty-chai": "^2.0.1", | ||
"pre-commit": "^1.2.2" | ||
"flow-bin": "^0.69.0", | ||
"flow-copy-source": "^1.3.0", | ||
"lint-staged": "^7.0.2", | ||
"pre-commit": "^1.2.2", | ||
"rollup.config.flow": "^1.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as multiformats/js-multihash#47 (review). No need for rollup I guess. |
||
"source-map-support": "^0.5.4" | ||
}, | ||
"homepage": "https://github.com/multiformats/js-multihashing", | ||
"contributors": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,79 @@ | ||
'use strict' | ||
// @flow | ||
import * as blake from 'blakejs' | ||
import type { Hash, HashUpdate, HashTable, HashBuilder } from './types' | ||
import type { Code } from 'multihashes/lib/constants' | ||
|
||
const blake = require('blakejs') | ||
const minB = 0xb201 | ||
const minS = 0xb241 | ||
const minB: Code = 0xb201 | ||
const minS: Code = 0xb241 | ||
|
||
var blake2b = { | ||
type BlakeCtx = { | ||
b: Uint8Array, | ||
h: Uint32Array, | ||
t: number, | ||
c: number, | ||
outlen: number | ||
} | ||
type Blake2Hash = Buffer | ||
type BlakeHasher = { | ||
init(size: number, key: ?number): BlakeCtx, | ||
update(ctx: BlakeCtx, input: Uint8Array): void, | ||
digest(ctx: BlakeCtx): Uint8Array | ||
} | ||
|
||
const blake2b: BlakeHasher = { | ||
init: blake.blake2bInit, | ||
update: blake.blake2bUpdate, | ||
digest: blake.blake2bFinal | ||
} | ||
|
||
var blake2s = { | ||
const blake2s: BlakeHasher = { | ||
init: blake.blake2sInit, | ||
update: blake.blake2sUpdate, | ||
digest: blake.blake2sFinal | ||
} | ||
|
||
class B2Hash { | ||
class B2Hash implements Hash { | ||
ctx: BlakeCtx | null | ||
hf: BlakeHasher | ||
|
||
constructor (size, hashFunc) { | ||
this.hf = hashFunc | ||
this.ctx = this.hf.init(size, null) | ||
} | ||
|
||
update (buf) { | ||
static new (size, hashFunc): HashUpdate { | ||
return new B2Hash(size, hashFunc) | ||
} | ||
|
||
update (buf: Buffer): Hash { | ||
if (this.ctx === null) { | ||
throw new Error('blake2 context is null. (already called digest?)') | ||
} | ||
this.hf.update(this.ctx, buf) | ||
return this | ||
} | ||
|
||
digest () { | ||
digest (): Blake2Hash { | ||
const ctx = this.ctx | ||
this.ctx = null | ||
if (ctx === null) { | ||
throw Error('blake2 context is null. (already called digest?)') | ||
} | ||
return Buffer.from(this.hf.digest(ctx)) | ||
} | ||
} | ||
|
||
function addFuncs (table) { | ||
function mkFunc (size, hashFunc) { | ||
return () => new B2Hash(size, hashFunc) | ||
export const addFuncs = (table: HashTable) => { | ||
const mkFunc = (size: number, hashFunc: BlakeHasher): HashBuilder => { | ||
return (): HashUpdate => B2Hash.new(size, hashFunc) | ||
} | ||
|
||
var i | ||
// I don't like using any here but the only way I could get the types to work here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More out of curiosity - what was the problem? Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They were opaque type aliases which behave like nominal types. In that module context any number can be treated as Code but outside of that context Code is a subtype of number and only way to get hold of value of that tipe is by getting it from that module either from exposed constant annotated as Code or via function that returns Code. This gives you a way to say have |
||
let i | ||
for (i = 0; i < 64; i++) { | ||
table[minB + i] = mkFunc(i + 1, blake2b) | ||
table[(minB + i: any)] = mkFunc(i + 1, blake2b) | ||
} | ||
for (i = 0; i < 32; i++) { | ||
table[minS + i] = mkFunc(i + 1, blake2s) | ||
table[(minS + i: any)] = mkFunc(i + 1, blake2s) | ||
} | ||
} | ||
|
||
module.exports = { | ||
addFuncs: addFuncs | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than having this file (
.babelrc
) and.flowconfig
, could we have these in a separate repository and import here? Would be a lot better, otherwise this is gonna be duplicated everywhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ideally we should move these into AEgir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly
.flowconfig
isn't js or json so you can't really pull it from elsewhere (unless you're thinking of git submodules which aren't worth the hassle IMO).As of
.babelrc
indeed this probably should end up in AEgir.