Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy (#12)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Release Notes

- **New Features**
  - Added TypeScript support for the JSONP plugin
  - Modernized project structure with ES module syntax
  - Enhanced type definitions and configuration
  - Introduced new GitHub Actions workflows for CI/CD
  - Added a new class for JSONP error handling

- **Breaking Changes**
  - Renamed package from `egg-jsonp` to `@eggjs/jsonp`
  - Dropped support for Node.js versions below 18.19.0
  - Refactored configuration and middleware approach

- **Improvements**
  - Updated GitHub Actions workflows for CI/CD
  - Improved security checks for JSONP requests
  - Added more robust error handling
  - Enhanced logging configuration

- **Dependency Updates**
  - Updated core dependencies
  - Migrated to modern TypeScript tooling
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jan 11, 2025
1 parent c60f540 commit 9136768
Show file tree
Hide file tree
Showing 28 changed files with 500 additions and 382 deletions.
25 changes: 0 additions & 25 deletions .autod.conf.js

This file was deleted.

5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
24 changes: 0 additions & 24 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
version: '18.19.0, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Release

on:
push:
branches: [ master ]

jobs:
release:
name: Node.js
uses: eggjs/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
test/fixtures/**/run
.DS_Store
*.swp

.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

File renamed without changes.
111 changes: 66 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,77 @@
# egg-jsonp
# @eggjs/jsonp

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Node.js CI](https://github.com/eggjs/jsonp/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/jsonp/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-jsonp.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-jsonp
[travis-image]: https://img.shields.io/travis/eggjs/egg-jsonp.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-jsonp
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-jsonp.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-jsonp?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-jsonp.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-jsonp
[snyk-image]: https://snyk.io/test/npm/egg-jsonp/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-jsonp
[download-image]: https://img.shields.io/npm/dm/egg-jsonp.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-jsonp
[![Node.js Version](https://img.shields.io/node/v/@eggjs/jsonp.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/@eggjs/jsonp.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/jsonp
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/jsonp.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/jsonp?branch=master
[snyk-image]: https://snyk.io/test/npm/@eggjs/jsonp/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/jsonp
[download-image]: https://img.shields.io/npm/dm/@eggjs/jsonp.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/jsonp

An egg plugin for jsonp support.

## Requirements

- egg >= 4.x

## Install

```bash
$ npm i egg-jsonp --save
npm i @eggjs/jsonp
```

## Usage

```js
// {app_root}/config/plugin.js
exports.jsonp = {
enable: true,
package: 'egg-jsonp',
```ts
// {app_root}/config/plugin.ts

export default {
jsonp: {
enable: true,
package: '@eggjs/jsonp',
},
};
```

## Configuration

* {String|Array} callback - jsonp callback method key, default to `[ '_callback', 'callback' ]`
* {Number} limit - callback method name's max length, default to `50`
* {Boolean} csrf - enable csrf check or not. default to false
* {String|RegExp|Array} whiteList - referrer white list
- {String|Array} callback - jsonp callback method key, default to `[ '_callback', 'callback' ]`
- {Number} limit - callback method name's max length, default to `50`
- {Boolean} csrf - enable csrf check or not. default to false
- {String|RegExp|Array} whiteList - referrer white list

if whiteList's type is `RegExp`, referrer must match `whiteList`, pay attention to the first `^` and last `/`.

```js
exports.jsonp = {
whiteList: /^https?:\/\/test.com\//,
}
```ts
export default {
jsonp: {
whiteList: /^https?:\/\/test.com\//,
},
};

// matchs referrer:
// https://test.com/hello
// http://test.com/
```

if whiteList's type is `String` and starts with `.`:

```js
exports.jsonp = {
whiteList: '.test.com',
```ts
export default {
jsonp: {
whiteList: '.test.com',
},
};

// matchs domain test.com:
// https://test.com/hello
// http://test.com/
Expand All @@ -73,40 +83,46 @@ exports.jsonp = {

if whiteList's type is `String` and not starts with `.`:

```js
exports.jsonp = {
whiteList: 'sub.test.com',
```ts
export default {
jsonp: {
whiteList: 'sub.test.com',
},
};

// only matchs domain sub.test.com:
// https://sub.test.com/hello
// http://sub.test.com/
```

whiteList also can be an array:

```js
exports.jsonp = {
whiteList: [ '.foo.com', '.bar.com' ],
```ts
export default {
jsonp: {
whiteList: [ '.foo.com', '.bar.com' ],
},
};
```

see [config/config.default.js](https://github.com/eggjs/egg-jsonp/blob/master/config/config.default.js) for more detail.
see [config/config.default.ts](https://github.com/eggjs/jsonp/blob/master/src/config/config.default.ts) for more detail.

## API

* ctx.acceptJSONP - detect if response should be jsonp, readonly
- ctx.acceptJSONP - detect if response should be jsonp, readonly

## Example

In `app/router.js`
In `app/router.ts`

```js
```ts
// Create once and use in any router you want to support jsonp.
const jsonp = app.jsonp();

app.get('/default', jsonp, 'jsonp.index');
app.get('/another', jsonp, 'jsonp.another');

// Customize by create another jsonp middleware with specific sonfigurations.
// Customize by create another jsonp middleware with specific configurations.
app.get('/customize', app.jsonp({ callback: 'fn' }), 'jsonp.customize');
```

Expand All @@ -116,5 +132,10 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](https://github.com/eggjs/egg-jsonp/blob/master/LICENSE)
[MIT](LICENSE)

## Contributors

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

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

0 comments on commit 9136768

Please sign in to comment.