Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created January 26, 2021 01:40
Show Gist options
  • Save ishiduca/b82bc19b501c8cacf15df575f11b22c3 to your computer and use it in GitHub Desktop.
Save ishiduca/b82bc19b501c8cacf15df575f11b22c3 to your computer and use it in GitHub Desktop.

Revisions

  1. ishiduca created this gist Jan 26, 2021.
    3 changes: 3 additions & 0 deletions bin.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #!/usr/bin/env node
    var find = require('./find-rss')
    process.stdin(pipe(find(JSON.stringify)).pipe(process.stdout)
    72 changes: 72 additions & 0 deletions find-rss.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    var trumpet = require('trumpet')
    var { through, duplex, concat, pipe } = require('mississippi')

    var mimeTypes = {
    'application/atom': 'atom',
    'application/atom+xml': 'atom',
    'text/atom': 'atom',
    'text/atom+xml': 'atom',
    'application/rss': 'rss',
    'application/rss+xml': 'rss',
    'text/rss': 'rss',
    'text/rss+xml': 'rss',
    'application/rdf': 'rdf',
    'application/rdf+xml': 'rdf',
    'text/rdf': 'rdf',
    'text/rdf+xml': 'rdf',
    'application/feed+json': 'json-feed',
    'application/json': 'json-feed'
    }

    module.exports = function find (f) {
    var tr = trumpet()
    var snk = through.obj((data, _, done) => {
    try {
    done(null, f(data))
    } catch (error) {
    done(error)
    }
    })
    var i = 0
    var mid = through.obj()
    mid.setMaxListeners(0)
    mid.on('pipe', () => (i += 1))
    mid.on('unpipe', () => {
    if ((i -= 1) === 0 && tr._readableState.ended) mid.end()
    })
    tr.once('end', () => {
    if (i === 0 && !mid._readableState.ended) mid.end()
    })

    pipe(
    mid,
    through.obj((data, _, done) => {
    if (mimeTypes[data.type] == null) return done()
    done(null, data)
    }),
    concat(list => snk.end(list)),
    error => (error != null) && snk.emit('error', error)
    )

    tr.selectAll('link', link => {
    var data = {}
    var src = through.obj()
    link.getAttribute('href', href => (data.href = href))
    link.getAttribute('rel', rel => (data.rel = rel))
    link.getAttribute('type', type => (data.type = type))
    link.getAttribute('title', title => (data.title = title))

    pipe(
    link.createReadStream(),
    concat(() => src.end(data)),
    error => {
    if (error != null) mid.emit('error', error)
    // else src.end(data)
    }
    )

    src.pipe(mid, { end: false })
    })

    return duplex.obj(tr, snk)
    }
    1 change: 1 addition & 0 deletions use
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    curl -- https://jsonfeed.org/version/1.1 | bin.js | JSONStream