-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.coffee
41 lines (34 loc) · 1.05 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
crypto = Npm.require 'crypto'
Crypto =
SHA256: class extends Crypto.SHA256
constructor: (params) ->
super
@_total = 0
@_hash = crypto.createHash 'sha256'
update: (data, callback) =>
throw new Error "No data given" if not data
try
@_hash.update data
@_total += data.length
catch error
# If callback is given we use it for reporting
# the error, otherwise we rethrow
return callback error if callback
throw error
# We ignore the result from onProgress because we are processing all data
# in update in one chunk anyway and there is nothing to prematurely abort
if @size?
@onProgress @_total / @size
else
@onProgress()
callback? null
finalize: (callback) =>
try
sha256 = @_hash.digest 'hex'
catch error
# If callback is given we use it for reporting
# the error, otherwise we rethrow
return callback error if callback
throw error
callback? null, sha256
sha256