open-index/open-npm huggingface dataset:
npm package: @m59/compose-middleware
Description: Generic promise-based middleware composer that is friendly to pure functions.
Author: Johnny Hauser
Latest version: 1.0.0

# @m59/compose-middleware

Generic promise-based middleware composer that is friendly to pure functions.

The primary difference from other middleware composing functions is that data is passed as arguments so that middleware is not forced to mutate it. Middleware can modify and pass on the same reference it is given, or it can make a new object and pass it on to the next middleware.

## install

```sh
$ npm install @m59/compose-middleware
```

## example

```js
const composeMiddleware = require('@m59/compose-middleware')

const addSlashBarToUrl = (request, next) => {
  /* you can mutate if you want (but it's not cool)
  request.url += '/bar'
  return next(request)
  */
  return next(Object.assign({}, request, { url: request.url + '/bar' }))
}

const changeMethodToGet = (request, next) => next(Object.assign({}, request, { method: 'GET' }))

// this middleware only deals with the response, but it could change the request too, if it wanted
const trollTheResponse = (request, next) => next(request)
  .then(response => '¯\_(ツ)_/¯')

const makeTheRequest = request => someAsyncRequest(request)

// middleware can modify the request object in the order they are given
composeMiddleware([
  addSlashBarToUrl
  changeMethodToGet,
  trollTheResponse,
  // the main thing goes last and makes the response data, which is handed up the middleware stack (reverse of given order)
  makeTheRequest
])

doTheThing({ url: '/foo' })
```

## api

### `composeMiddleware(arrayOfMiddleware)`

### `middleware`

```js
(context, next) => next(context).then(result => result)
```

#### `context`

`context` is whatever data that is passed into the composed middleware function (1st and only argument). It is passed to middleware in the order the middleware was given. The value of `context` for each middleware is the value that the previous middleware passed into `next(context)`.

#### `result`

`result` is the value resolved from the promise of the last middleware. Keep in mind that results are handled in reverse order of how the middleware are given. It's intuitive if you read the middleware:

```js
next(context).then( // call the next given middleware, then do something with its result
  result => // result resolved from the next given middleware
    'foo' // result from this middleware, which can be handled by the middleware given above this one
)
```
npm package: @m59/error-cereal
Description: Serialize a JavaScript error to a regular, json stringifiable object, and deserialize an object into an error.
Author: Johnny Hauser
Latest version: 1.0.0

# @m59/error-cereal

Serialize a JavaScript error to a regular, json stringifiable object, and deserialize an object into an error.

## api

```js
const { serialize_error, deserialize_error } = require('@m59/error-cereal')
```

### `serialize_error(error)`

Takes an error instance and returns a regular object that you can stringify with `JSON.stringify`.
Circular references are replaced with `'[Circular]'`.

```js
serialize_error(Object.assign(new Error('an error'), { foo: 'bar' }))
// => { name: 'Error', message: 'an error', stack: 'etc', foo: 'bar' }
```

### `deserialize_error(object, { custom_error_constructors = {} })`

Takes an object and returns an instance of the error referenced by `{ name }`. This operation is isomorphic with `serialize_error` for error instances as they were originally constructed or with added properties when those properties have values that can be converted back to their original.

Custom error constructors can be passed in and will be used to construct the error for objects with a matching { name }.

```js
deserialize_error(
	{ name: 'My_Custom_Error', message: 'error message' },
	{
		custom_error_constructors: {
			My_Custom_Error
		}
	}
)
// => new My_Custom_Error('error message')
```
npm package: @m59/fetch
Description: Fetch API with conveniences I like.
Author: Johnny Hauser
Latest version: 0.0.0

# @m59/fetch

Fetch API with conveniences I like.

This is shimmed for server side usage with [`node-fetch`](https://github.com/bitinn/node-fetch). The server code is ignored when bundling for the browser.

## install

```sh
$ npm install @m59/fetch
```

## example

```js
const fetch = require('@m59/fetch')

fetch.json({
  url: URL + '/posts',
  query: { userId: 1 }
}).then(({response, body}) => body[0].id) // 1
```

## api

### `fetch([url], options)`

The first argument can be a url or options object. If you pass only an options object, it should have a url property. Put your [normal `fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) in `options`.


#### `options.query`

`options.query` is an object that will be used to create a query string that will set or overwrite the query string in the given url

```js
{
  url: '/foo'
  query: { bar: 123 }
} // => /foo?bar=123
```

#### `options.body`

If you set `options.body` to an instance of `FormData`, the form data content type header will be set automatically. If you set `options.body` to a regular object, it will be JSON stringified and the json content type header will be set automatically.

#### helpers

If you want the response body in a certain format, save some code by using the corresponding helper. `json()` and `text()` will also set the appropriate `accept` header to ensure the server knows what format you want. The result will be in the format `{ response, body }`.

```js
fetch.arrayBuffer()
fetch.blob()
fetch.json()
fetch.text()
fetch.formData()
```
npm package: @m59/reload-client
Description: Client-side script to respond to signals from @m59/reload-server. Can reload the page and refresh css and images.
Author: Johnny Hauser
Latest version: 1.0.3

# Reload Client

Client-side script for triggering page and resource reload over WebSockets.

```sh
npm install @m59/reload-client
```

```javascript
import Client from '@m59/reload-client'
Client(reloadServerUrl)
```

## JS API

### Client(reloadServerUrl, options)

Makes a websocket connection to the given url to listen for reload messages.

- `options: object`
  - `quiet: boolean, false` disable logging to console
- **returns**: `object`
  - socket: the WebSocket
  - ...helpers

## WebSocket API

- `string JSON`
  - `type: string` type of reload
    - reload
    - refreshCSS
    - refreshImages
  - `path: string` path to a changed file (for logging)

## Helpers

### refreshCSS()
```
import refreshCSS from '@m59/reload-client/refresh-css'
refreshCSS()
```

### refreshImages()
```
import refreshImages from '@m59/reload-client/refresh-images'
refreshImages()
```
npm package: @m59/reload-server
Description: Simple SPA development server with reload API. Injects reload script.
Author: Johnny Hauser
Latest version: 1.0.6

# Reload Server

Simple SPA development server with reload API. Injects reload script.

## Install

```sh
npm install @m59/reload-server
```

## CLI

```sh
m59-reload-server [options]
```

```txt
  -h, --help          output usage information
  -V, --version       output the version number
  -d, --dir  [value]  directory of files to be served [cwd]
  -p, --port [value]  `port` argument for server.listen() [8080]
  -H, --host [value]  `host` argument for server.listen()
  -q, --quiet         disable logging
  --wsPath   [value]  path to the websocket server
```

## JS API

```javascript
const Server = require('@m59/reload-server')

const server = Server({
  dir: './www'
})
server.listen(8080)

watch(someFiles, function(event) {
  server.reload(event.path) // smart reload based on path
})
```

### Server(options)
- `options: object`
  - `dir: string, cwd`
  - `wsPath: string, random`
  - `quiet: boolean, false`
- **returns**: [server](https://nodejs.org/api/http.html#http_class_http_server)

### server.reload(path)
Signals connected clients to reload the page. If `path` is given, clients may be signaled to refresh CSS or images based on the file extension in the path, rather than reloading the page.

### server.refreshCSS()
Signals connected clients to refresh CSS without page reload.

### server.refreshImages()
Signals connected clients to refresh images without page reload.

### server.wsPath
The path to the websocket server.

### server.wss
The [ws](https://www.npmjs.com/package/ws) server.

## HTTP API

```
POST /reload
POST /reload/css
POST /reload/img
```

The `path` parameter of `server.reload` can be passed as JSON in the body or as a query parameter.

`{"path": "/path/here.css"}`

`/reload?path=/path/here.css`


```sh
curl -X POST http://localhost:8080/reload
```
npm package: @m59/remove-directory
Description: Simple function that can remove a non-empty directory.
Author: Johnny Hauser
Latest version: 1.0.0

# @m59/remove-directory

Simple function that can remove a non-empty directory.

```js
const { removeDirectory, removeDirectorySync } = require('@m59/remove-directory')

removeDirectorySync('/tmp/o')

;(async () => {
	await removeDirectory('/tmp/ani')
})()
```
npm package: @m59/yaml-to-json
Description: pipe yaml to stdin, get json from stdout, or pass a src directory of yaml files (recursive) and a dest directory for json files.
Author: Johnny Hauser
Latest version: 1.0.4

# yaml-to-json

> pipe yaml to stdin, get json from stdout, or pass a src directory of yaml files (recursive) and a dest directory for json files.

## install

```sh
$ npm install -g @m59/yaml-to-json
```

## example

```sh
cat my-file.yaml | yaml-to-json > my.file.json
```

### `yaml-to-json $src $dest`

`.yml` and `.yaml` in the `$src` directory and subdirectories (recursive) will be used to write json files. The json files are output with the same directory structure, but starting from `$dest`.

```sh
# process yaml files under current directory
# output json in the same directory as the corresponding yaml
yaml-to-json . .
```

```sh
yaml-to-json src /some/other/place
```