# jsonp-body

> Helper to create more safe jsonp response body for koa and other web framework

Latest version **2.0.0** (published 2025-01-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install jsonp-body
pnpm add jsonp-body
yarn add jsonp-body
bun add jsonp-body
```

## Health

**Score 45/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-01-11 |
| First published | 2014-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.19.0 |
| Dependencies | 0 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 10 |
| Author | fengmk2 |
| Maintainers | fengmk2 |
| Keywords | jsonp, koa, json, CVE-2014-4671, abusing-jsonp-with-rosetta-flash |

## Links

- npm: https://www.npmjs.com/package/jsonp-body
- Repository: https://github.com/node-modules/jsonp-body
- Issues: https://github.com/node-modules/jsonp-body/issues
- npm.io page: https://npm.io/package/jsonp-body

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.0 (latest) — 2025-01-11
- 1.1.0 — 2025-01-11
- 1.0.0 — 2014-08-19
- 0.2.0 — 2014-07-22
- 0.1.0 — 2014-07-17

## README

# jsonp-body

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/node-modules/jsonp-body/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/jsonp-body/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/jsonp-body.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)

[npm-image]: https://img.shields.io/npm/v/jsonp-body.svg?style=flat-square
[npm-url]: https://npmjs.org/package/jsonp-body
[codecov-image]: https://img.shields.io/codecov/c/github/node-modules/jsonp-body.svg?style=flat-square
[codecov-url]: https://codecov.io/github/node-modules/jsonp-body?branch=master
[snyk-image]: https://snyk.io/test/npm/jsonp-body/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/jsonp-body
[download-image]: https://img.shields.io/npm/dm/jsonp-body.svg?style=flat-square
[download-url]: https://npmjs.org/package/jsonp-body

Helper to create more safe jsonp response body for [koa](http://koajs.com/) and other web framework.

## Install

```bash
npm install jsonp-body --save
```

## Usage

```js
const koa = require('koa');
const { jsonp } = require('jsonp-body');

var app = koa();
app.use(async function () {
  this.set('X-Content-Type-Options', 'nosniff');
  if (this.query.callback) {
    this.set('Content-Type', 'text/javascript');
  } else {
    this.set('Content-Type', 'application/json');
  }
  this.body = jsonp({foo: 'bar'}, this.query.callback);
});
```

## API Reference

### `jsonp(obj, callback, options)`

Get `obj` jsonp string response with `callback`.

- __obj__: object convert to JSON string
- __callback__: callback function name
- __options__: optional for `JSON.stringify`
  - __limit__: length limit for `callback`, default to `512`
  - __replacer__: replacer in `JSON.stringify(obj, [replacer, [space]])`
  - __space__: space in `JSON.stringify(obj, [replacer, [space]])`

## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=node-modules/jsonp-body)](https://github.com/node-modules/jsonp-body/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).

---
_Source: https://npm.io/package/jsonp-body · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
