Created
March 2, 2014 10:38
-
-
Save roman-neuhauser/9304735 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crypto = require 'crypto' | |
fs = require 'fs' | |
path = require 'path' | |
digest = crypto.createHash 'sha1' | |
str = 'hello world!' | |
digest.update str | |
hex = digest.digest 'hex' | |
segs = (hex) -> | |
rv = hex[i..i+1] for _, i in hex.substr 0, 20 by 2 | |
mkseg = (parts, i, done) -> | |
if i >= parts.length | |
return do done | |
fs.mkdir (path.join parts[0..i]...), (err) -> | |
return done err if err and err.code isnt 'EEXIST' | |
mkseg parts, i+1, done | |
mkpath = (parts, done) -> | |
mkseg parts, 0, done | |
store = (payload, hex, done) -> | |
parts = segs hex | |
mkpath parts, (err, args...) -> | |
return done err if err | |
fpath = path.join parts..., hex | |
fs.writeFile fpath, payload, (err, args...) -> | |
done err, args... | |
load = (hex, done) -> | |
parts = segs hex | |
fpath = path.join parts..., hex | |
fs.readFile fpath, 'utf8', done | |
store str, hex, (args...) -> | |
console.dir args | |
load hex, (args...) -> | |
console.dir args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment