~samhh/bangin-server-node

417affe8b837b9d12de2505b37ea8c17aa455726 — Sam A. Horvath-Hunt 6 months ago 0.1.0
Initial
4 files changed, 100 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A index.js
A package.json
A  => LICENSE +22 -0
@@ 1,22 @@
MIT License

Copyright (c) 2021 Sam Ashley Horvath-Hunt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


A  => README.md +48 -0
@@ 1,48 @@
# bangin-server-node

A basic web server around [bangin](https://github.com/samhh/bangin) written in Node.js.

## Prerequisites

- Node.js 10+
- bangin (in your `$PATH`)

## Usage

Run the server with:

```
node index.js [port]
```

If no port is provided then it will default to `1234`.

Requests must include a non-empty value at querystring `q`. The string will first be routed to bangin before, should that fail, falling back to DuckDuckGo.

The response if successful will be a 303 redirect that browsers can handle.

Examples:

```
$ curl -I 'http://localhost:1234?q=x!aw'
HTTP/1.1 303 See Other
Location: https://wiki.archlinux.org/index.php?search=x
[...]

$ curl -I 'http://localhost:1234?q=x'
HTTP/1.1 303 See Other
Location: https://duckduckgo.com/?q=x
[...]
```

## Setting as the browser search engine

### Chrome/similar

1. Go to `chrome://settings/searchEngines`.
2. Add a new search engine with the URL pointing to `http://localhost:<port>/?q=%s`.

### Firefox

Firefox does not yet support fully custom search engines. You can track the relevant issue [here](https://bugzilla.mozilla.org/show_bug.cgi?id=1650874).


A  => index.js +23 -0
@@ 1,23 @@
const http = require('http')
const cp = require('child_process')

let port = Number(process.argv[2])
if (Number.isNaN(port)) port = 1234

const fallback = x => `https://duckduckgo.com/?q=${x}`

http.createServer((req, res) => {
    const q = new URL(req.url, `http://localhost:${port}`).searchParams.get('q')

    if (!q) return res.writeHead(400).end()

    const cmd = cp.spawn('bangin', [q])

    let loc
    cmd.stdout.on('data', x => { loc = String(x).trimEnd() })

    cmd.on('close', () => {
        res.writeHead(303, { 'Location': loc ? encodeURI(loc) : fallback(q) }).end()
    })
}).listen(port)


A  => package.json +7 -0
@@ 1,7 @@
{
  "name": "bangin-server-node",
  "version": "1.0.0",
  "description": "A Node.js web server for bangin.",
  "main": "index.js",
  "license": "MIT"
}