From 2eb748d24d3ee0d13f990d2b7eab167a1b41b930 Mon Sep 17 00:00:00 2001 From: cel Date: Wed, 20 Sep 2017 10:43:25 -1000 Subject: Render npm package readmes --- lib/app.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'lib/app.js') diff --git a/lib/app.js b/lib/app.js index 65d404a..1705d1c 100644 --- a/lib/app.js +++ b/lib/app.js @@ -12,6 +12,8 @@ var Serve = require('./serve') var Render = require('./render') var Git = require('./git') var cat = require('pull-cat') +var proc = require('child_process') +var toPull = require('stream-to-pull-stream') module.exports = App @@ -40,11 +42,15 @@ function App(sbot, config) { this.unboxContent = memo({cache: lru(100)}, sbot.private.unbox) this.reverseNameCache = lru(500) this.reverseEmojiNameCache = lru(500) + this.getBlobSize = memo({cache: this.blobSizeCache = lru(100)}, + sbot.blobs.size.bind(sbot.blobs)) this.unboxMsg = this.unboxMsg.bind(this) this.render = new Render(this, this.opts) this.git = new Git(this) + + this.monitorBlobWants() } App.prototype.go = function () { @@ -179,9 +185,12 @@ App.prototype.publish = function (content, cb) { } App.prototype.wantSizeBlob = function (id, cb) { + // only want() the blob if we don't already have it + var self = this var blobs = this.sbot.blobs blobs.size(id, function (err, size) { if (size != null) return cb(null, size) + self.blobWants[id] = true blobs.want(id, function (err) { if (err) return cb(err) blobs.size(id, cb) @@ -565,3 +574,50 @@ App.prototype.blobMentions = function (opts) { ] }) } + +App.prototype.monitorBlobWants = function () { + var self = this + self.blobWants = {} + pull( + this.sbot.blobs.createWants(), + pull.drain(function (wants) { + for (var id in wants) { + if (wants[id] < 0) self.blobWants[id] = true + else delete self.blobWants[id] + self.blobSizeCache.remove(id) + } + }, function (err) { + if (err) console.trace(err) + }) + ) +} + +App.prototype.getBlobState = function (id, cb) { + var self = this + if (self.blobWants[id]) return cb(null, 'wanted') + self.getBlobSize(id, function (err, size) { + if (err) return cb(err) + cb(null, size != null) + }) +} + +App.prototype.getNpmReadme = function (tarballId, cb) { + var self = this + // TODO: make this portable, and handle plaintext readmes + var tar = proc.spawn('tar', ['--ignore-case', '-Oxz', + 'package/README.md', 'package/readme.markdown', 'package/readme.mkd']) + var done = multicb({pluck: 1, spread: true}) + pull( + self.sbot.blobs.get(tarballId), + toPull.sink(tar.stdin, done()) + ) + pull( + toPull.source(tar.stdout), + pull.collect(done()) + ) + done(function (err, _, bufs) { + if (err) return cb(err) + var text = Buffer.concat(bufs).toString('utf8') + cb(null, text, true) + }) +} -- cgit v1.2.3