PHPackages                             cleong/sixbeermk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cleong/sixbeermk

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cleong/sixbeermk
================

Random haiku generator

04PHP

Since Apr 4Pushed 3y agoCompare

[ Source](https://github.com/6bmk/sixbeermk)[ Packagist](https://packagist.org/packages/cleong/sixbeermk)[ RSS](/packages/cleong-sixbeermk/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

6bmk [![ci](https://camo.githubusercontent.com/dbff72aa76e9275188c3d954a53768531ecbdc549ee326203cb59373f56782bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368756e672d6c656f6e672f36626d6b2f6e6f64652e6a732e796d6c3f6272616e63683d6d61696e266c6162656c3d4e6f64652e6a732532304349266c6f676f3d676974687562)](https://camo.githubusercontent.com/dbff72aa76e9275188c3d954a53768531ecbdc549ee326203cb59373f56782bc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368756e672d6c656f6e672f36626d6b2f6e6f64652e6a732e796d6c3f6272616e63683d6d61696e266c6162656c3d4e6f64652e6a732532304349266c6f676f3d676974687562) [![nycrc config on GitHub](https://camo.githubusercontent.com/5baeba06f3f1f424c345fe9a9046916cee11588460a76658d242efb435c5f926/68747470733a2f2f696d672e736869656c64732e696f2f6e796372632f6368756e672d6c656f6e672f36626d6b)](https://camo.githubusercontent.com/5baeba06f3f1f424c345fe9a9046916cee11588460a76658d242efb435c5f926/68747470733a2f2f696d672e736869656c64732e696f2f6e796372632f6368756e672d6c656f6e672f36626d6b) [![ci](https://camo.githubusercontent.com/30592e20260f9797c1dfdac885e5b4b99c2703d4bb0d3353f04ebffc8e42353e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368756e672d6c656f6e672f36626d6b2f7068702e796d6c3f6272616e63683d6d61696e266c6162656c3d5048502532304349266c6f676f3d676974687562)](https://camo.githubusercontent.com/30592e20260f9797c1dfdac885e5b4b99c2703d4bb0d3353f04ebffc8e42353e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6368756e672d6c656f6e672f36626d6b2f7068702e796d6c3f6272616e63683d6d61696e266c6162656c3d5048502532304349266c6f676f3d676974687562)
================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#6bmk---)

[![Monkey](./doc/images/infinite-monkey.svg)](./doc/images/infinite-monkey.svg)

6bmk is an access control mechanism for Internet discussion boards. It consists of two parts: a random haiku generator and a PowerPoint flyer generator. The haiku serve as a form of one-time code. Only people possessing a strip of paper torn from the flyer can join the group.

[![Flyer](./doc/images/photo-1.jpg)](./doc/images/photo-1.jpg)

[![Strip](./doc/images/photo-2.jpg)](./doc/images/photo-2.jpg)

This project is inspired by bulletin board systems (BBSes) of yesteryears. Users of such systems generally all live in the same area code. This geographic proximity meant people could easily meet up in the real world. And they did frequently, either at group events or visiting each other's homes. "Like a family" is not an uncommon description when people speak of their BBS experience. It's hope that by imposing a geographic limit on membership, we could recreate the old social dynamic on the Internet.

Installation
------------

[](#installation)

Node.js:

```
npm install 6bmk
```

PHP

```
composer require cleong/sixbeermk
```

Usage - Node.js
---------------

[](#usage---nodejs)

### Generating haiku

[](#generating-haiku)

```
import { generateHaiku, normalizeHaiku } from '6bmk';
import { createHash } from 'crypto';

async function *getAccessHaiku(db, flyerId, locale) {
  // load existing haiku first
  const rows = await db.query(`SELECT text FROM haiku WHERE flyer_id = ?`, [ flyerId ]);
  for (const { text } of rows) {
    yield text;
  }
  // generate new ones if there aren't enough
  for await (const text of generateHaiku({ locale })) {
    // generate hash
    const sha1 = createHash('sha1');
    sha1.update(normalizeHaiku(text));
    const hash = sha1.digest('hex');
    // save to database
    db.query(`INSERT INTO haiku (flyer_id, text, hash) VALUES(?, ?, ?)`, [ flyerId, text, hash ]);
    yield text;
  }
}
```

[`generateHaiku`](./doc/generateHaiku.md#readme) returns an async generator. Generally you would save its output to a database so you can verify later that a user has entered a correct haiku. [`normalizeHaiku`](./doc/normalizeHaiku.md#readme) removes punctuations, normalizes whitespaces, and converts characters to lowercase. These steps ensure we would get the same hash despite minor differences.

### Generating flyer

[](#generating-flyer)

```
import { createFlyer } from '6bmk';

async function createDownload(db, flyerId) {
  const [ row ] = await db.query(`SELECT address, instructions, options FROM flyer WHERE id = ?`, [ flyerId ]);
  const { address, instructions, options } = row;
  const { paper, orientation, mode, locale } = JSON.parse(options);
  const haiku = getAccessHaiku(db, flyerId, locale);
  return createFlyer({ paper, orientation, mode, address, instructions, haiku });
}
```

[`createFlyer`](./doc/createFlyer.md#readme) returns a Node [Readable Stream](https://nodejs.org/api/stream.html#readable-streams). If you're using [Fastify](https://www.fastify.io/), you can simply return the stream in your handler. If you're using [Express](https://expressjs.com/), you would need to pipe the stream into `res`. In both cases you should set the appriopriate HTTP headers so the response is handled as a download by the browser.

For a working example, check out the code in [examples/fastify](./examples/fastify#readme)or [examples/express](./examples/express#readme).

### CommonJS

[](#commonjs)

```
const { createFlyer, generateHaiku } = require('6bmk/cjs');
```

Usage - PHP
-----------

[](#usage---php)

### Generating haiku

[](#generating-haiku-1)

```
