<commit_before>builder  = require("builder").init()
coffee   = require("coffee-script")
crypto   = require("crypto")
express  = require("express")
fs       = require("fs")
manifest = require("manifest")
storage  = require("storage").init()
util     = require("util")

app = express.createServer(
  express.logger(),
  express.cookieParser(),
  express.bodyParser())

app.post "/build", builder.build_request

app.get "/cache/:id.tgz", (req, res) ->
  storage.get "/cache/#{req.params.id}.tgz", (err, get) ->
    get.on "data", (chunk) -> res.write chunk
    get.on "end",          -> res.end()

app.put "/cache/:id.tgz", (req, res) ->
  storage.create_stream "/cache/#{req.params.id}.tgz", fs.createReadStream(req.files.data.path), (err) ->
    res.send("ok")

app.get "/file/:hash", (req, res) ->
  storage.get "/hash/#{req.params.hash}", (err, get) ->
    res.writeHead get.statusCode,
      "Content-Length": get.headers["content-length"]
    get.on "data", (chunk) -> res.write chunk
    get.on "end",          -> res.end()

app.post "/file/:hash", (req, res) ->
  storage.verify_hash req.files.data.path, req.params.hash, (err) ->
    return res.send(err, 403) if err
    storage.create_stream "/hash/#{req.params.hash}", fs.createReadStream(req.files.data.path), (err) ->
      res.send "ok"

app.post "/manifest", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
    res.header "Location", manifest_url
    res.send "ok"

app.post "/manifest/build", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
    delete req.body.manifest
    req.body.source = manifest_url
    builder.build_request req, res

app.post "/manifest/diff", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).missing_hashes (hashes) ->
    res.contentType "application/json"
    res.send JSON.stringify(hashes)<commit_msg>make sure to initialize builder for each request<commit_after>builder  = require("builder")
coffee   = require("coffee-script")
crypto   = require("crypto")
express  = require("express")
fs       = require("fs")
manifest = require("manifest")
storage  = require("storage").init()
util     = require("util")

app = express.createServer(
  express.logger(),
  express.cookieParser(),
  express.bodyParser())

app.post "/build", (req, res) ->
  builder.init().build_request req, res

app.get "/cache/:id.tgz", (req, res) ->
  storage.get "/cache/#{req.params.id}.tgz", (err, get) ->
    get.on "data", (chunk) -> res.write chunk
    get.on "end",          -> res.end()

app.put "/cache/:id.tgz", (req, res) ->
  storage.create_stream "/cache/#{req.params.id}.tgz", fs.createReadStream(req.files.data.path), (err) ->
    res.send("ok")

app.get "/file/:hash", (req, res) ->
  storage.get "/hash/#{req.params.hash}", (err, get) ->
    res.writeHead get.statusCode,
      "Content-Length": get.headers["content-length"]
    get.on "data", (chunk) -> res.write chunk
    get.on "end",          -> res.end()

app.post "/file/:hash", (req, res) ->
  storage.verify_hash req.files.data.path, req.params.hash, (err) ->
    return res.send(err, 403) if err
    storage.create_stream "/hash/#{req.params.hash}", fs.createReadStream(req.files.data.path), (err) ->
      res.send "ok"

app.post "/manifest", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
    res.header "Location", manifest_url
    res.send "ok"

app.post "/manifest/build", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).save (err, manifest_url) ->
    delete req.body.manifest
    req.body.source = manifest_url
    builder.init().build_request req, res

app.post "/manifest/diff", (req, res) ->
  manifest.init(JSON.parse(req.body.manifest)).missing_hashes (hashes) ->
    res.contentType "application/json"
    res.send JSON.stringify(hashes)
{'fraction_non_alphanumeric': 0.19343920853944285, 'fraction_numerical': 0.0015620932048945586, 'mean_word_length': 3.5146886016451235, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5339514', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1299, 'n_words': 258}