From 73748a90681787039cc2b85d81969bcd7ddc353d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 11 Jan 2025 17:00:39 +0800 Subject: [PATCH 1/2] feat: support cjs and esm both by tshy BREAKING CHANGE: drop Node.js < 18.19.0 support part of https://github.com/eggjs/egg/issues/3644 https://github.com/eggjs/egg/issues/5257 --- .eslintignore | 2 + .eslintrc | 6 ++ .github/workflows/nodejs.yml | 17 ++++++ .github/workflows/pkg.pr.new.yml | 23 ++++++++ .github/workflows/release.yml | 13 +++++ .gitignore | 23 +++----- .jshintignore | 4 -- .jshintrc | 95 -------------------------------- .npmignore | 8 --- .travis.yml | 11 ---- AUTHORS | 6 -- History.md => CHANGELOG.md | 0 README.md | 28 ++++++---- index.d.ts | 18 ------ package.json | 82 ++++++++++++++++++--------- index.js => src/index.ts | 32 ++++------- test/index.test.js | 70 ----------------------- test/index.test.ts | 58 +++++++++++++++++++ tsconfig.json | 10 ++++ 19 files changed, 224 insertions(+), 282 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .github/workflows/nodejs.yml create mode 100644 .github/workflows/pkg.pr.new.yml create mode 100644 .github/workflows/release.yml delete mode 100644 .jshintignore delete mode 100644 .jshintrc delete mode 100644 .npmignore delete mode 100644 .travis.yml delete mode 100644 AUTHORS rename History.md => CHANGELOG.md (100%) delete mode 100644 index.d.ts rename index.js => src/index.ts (63%) delete mode 100644 test/index.test.js create mode 100644 test/index.test.ts create mode 100644 tsconfig.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..a24e501 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +test/fixtures +coverage diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..9bcdb46 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,6 @@ +{ + "extends": [ + "eslint-config-egg/typescript", + "eslint-config-egg/lib/rules/enforce-node-prefix" + ] +} diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..9eafb37 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,17 @@ +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: + os: 'macos-latest' + version: '18.19.0, 20, 22' + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml new file mode 100644 index 0000000..bac3fac --- /dev/null +++ b/.github/workflows/pkg.pr.new.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1c6cbb1 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,13 @@ +name: Release + +on: + push: + branches: [ master ] + +jobs: + release: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-release.yml@master + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/.gitignore b/.gitignore index c0a6490..c010914 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,11 @@ -coverage.html -*.seed -*.log -*.csv -*.dat -*.out -*.pid -*.gz - -pids -logs -results - -node_modules +logs/ npm-debug.log +node_modules/ coverage/ +test/fixtures/**/run +.DS_Store +.tshy* +.eslintcache +dist +package-lock.json +.package-lock.json diff --git a/.jshintignore b/.jshintignore deleted file mode 100644 index 63c6333..0000000 --- a/.jshintignore +++ /dev/null @@ -1,4 +0,0 @@ -node_modules/ -coverage/ -.tmp/ -.git/ diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 5988707..0000000 --- a/.jshintrc +++ /dev/null @@ -1,95 +0,0 @@ -{ - // JSHint Default Configuration File (as on JSHint website) - // See http://jshint.com/docs/ for more details - - "maxerr" : 50, // {int} Maximum error before stopping - - // Enforcing - "bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.) - "camelcase" : false, // true: Identifiers must be in camelCase - "curly" : true, // true: Require {} for every new block or scope - "eqeqeq" : true, // true: Require triple equals (===) for comparison - "forin" : false, // true: Require filtering for..in loops with obj.hasOwnProperty() - "immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());` - "indent" : false, // {int} Number of spaces to use for indentation - "latedef" : false, // true: Require variables/functions to be defined before being used - "newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()` - "noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee` - "noempty" : true, // true: Prohibit use of empty blocks - "nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment) - "plusplus" : false, // true: Prohibit use of `++` & `--` - "quotmark" : false, // Quotation mark consistency: - // false : do nothing (default) - // true : ensure whatever is used is consistent - // "single" : require single quotes - // "double" : require double quotes - "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) - "unused" : false, // true: Require all defined variables be used - "strict" : true, // true: Requires all functions run in ES5 Strict Mode - "trailing" : false, // true: Prohibit trailing whitespaces - "maxparams" : false, // {int} Max number of formal params allowed per function - "maxdepth" : false, // {int} Max depth of nested blocks (within functions) - "maxstatements" : false, // {int} Max number statements per function - "maxcomplexity" : false, // {int} Max cyclomatic complexity per function - "maxlen" : false, // {int} Max number of characters per line - - // Relaxing - "asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons) - "boss" : true, // true: Tolerate assignments where comparisons would be expected - "debug" : false, // true: Allow debugger statements e.g. browser breakpoints. - "eqnull" : false, // true: Tolerate use of `== null` - "es5" : false, // true: Allow ES5 syntax (ex: getters and setters) - "esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`) - "moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features) - // (ex: `for each`, multiple try/catch, function expression…) - "evil" : false, // true: Tolerate use of `eval` and `new Function()` - "expr" : true, // true: Tolerate `ExpressionStatement` as Programs - "funcscope" : false, // true: Tolerate defining variables inside control statements" - "globalstrict" : false, // true: Allow global "use strict" (also enables 'strict') - "iterator" : false, // true: Tolerate using the `__iterator__` property - "lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block - "laxbreak" : true, // true: Tolerate possibly unsafe line breakings - "laxcomma" : false, // true: Tolerate comma-first style coding - "loopfunc" : false, // true: Tolerate functions being defined in loops - "multistr" : true, // true: Tolerate multi-line strings - "proto" : false, // true: Tolerate using the `__proto__` property - "scripturl" : false, // true: Tolerate script-targeted URLs - "smarttabs" : false, // true: Tolerate mixed tabs/spaces when used for alignment - "shadow" : true, // true: Allows re-define variables later in code e.g. `var x=1; x=2;` - "sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation - "supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;` - "validthis" : false, // true: Tolerate using this in a non-constructor function - - // Environments - "browser" : true, // Web Browser (window, document, etc) - "couch" : false, // CouchDB - "devel" : true, // Development/debugging (alert, confirm, etc) - "dojo" : false, // Dojo Toolkit - "jquery" : false, // jQuery - "mootools" : false, // MooTools - "node" : true, // Node.js - "nonstandard" : false, // Widely adopted globals (escape, unescape, etc) - "prototypejs" : false, // Prototype and Scriptaculous - "rhino" : false, // Rhino - "worker" : false, // Web Workers - "wsh" : false, // Windows Scripting Host - "yui" : false, // Yahoo User Interface - "noyield" : true, // allow generators without a yield - - // Legacy - "nomen" : false, // true: Prohibit dangling `_` in variables - "onevar" : false, // true: Allow only one `var` statement per function - "passfail" : false, // true: Stop on first error - "white" : false, // true: Check against strict whitespace and indentation rules - - // Custom Globals - "globals" : { // additional predefined global variables - // mocha - "describe": true, - "it": true, - "before": true, - "afterEach": true, - "beforeEach": true, - "after": true - } -} diff --git a/.npmignore b/.npmignore deleted file mode 100644 index c12e803..0000000 --- a/.npmignore +++ /dev/null @@ -1,8 +0,0 @@ -Makefile -.travis.yml -logo.png -.jshintignore -.jshintrc -.gitingore -coverage/ -test/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index cf19cab..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -sudo: false -language: node_js -node_js: - - '5' - - '4' - - '0.12' - - '0.10' -script: - - 'npm run test-cov' -after_script: - - 'npm i codecov.io && cat ./coverage/coverage.json | ./node_modules/codecov.io/bin/codecov.io.js' diff --git a/AUTHORS b/AUTHORS deleted file mode 100644 index 4324702..0000000 --- a/AUTHORS +++ /dev/null @@ -1,6 +0,0 @@ -# Ordered by date of first contribution. -# Auto-generated by 'contributors' on Tue, 19 Aug 2014 09:33:56 GMT. -# https://github.com/xingrz/node-contributors - -fengmk2 (https://github.com/fengmk2) -dead-horse (https://github.com/dead-horse) diff --git a/History.md b/CHANGELOG.md similarity index 100% rename from History.md rename to CHANGELOG.md diff --git a/README.md b/README.md index d4b8e34..3e9ac6f 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,38 @@ # jsonp-body [![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] -[![Test coverage][coveralls-image]][coveralls-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 -[travis-image]: https://img.shields.io/travis/node-modules/jsonp-body.svg?style=flat-square -[travis-url]: https://travis-ci.org/node-modules/jsonp-body -[coveralls-image]: https://img.shields.io/coveralls/node-modules/jsonp-body.svg?style=flat-square -[coveralls-url]: https://coveralls.io/r/node-modules/jsonp-body?branch=master +[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 +npm install jsonp-body --save ``` ## Usage ```js -var koa = require('koa'); -var jsonp = require('jsonp-body'); +const koa = require('koa'); +const { jsonp } = require('jsonp-body'); var app = koa(); -app.use(function* () { +app.use(async function () { this.set('X-Content-Type-Options', 'nosniff'); if (this.query.callback) { this.set('Content-Type', 'text/javascript'); @@ -39,7 +45,7 @@ app.use(function* () { ## API Reference -### #jsonp(obj, callback, options) +### `jsonp(obj, callback, options)` Get `obj` jsonp string response with `callback`. diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index 1301d94..0000000 --- a/index.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -declare namespace jsonp { - interface Options { - /** length limit for callback function name, default to `512` */ - limit?: number; - /** replacer in `JSON.stringify(obj, [replacer, [space]])` */ - replacer?: Parameters[1]; - /** space in `JSON.stringify(obj, [replacer, [space]])` */ - space?: Parameters[2]; - } -} - -declare function jsonp( - obj: any, - callback?: string | string[], - options?: jsonp.Options -): string; - -export = jsonp; diff --git a/package.json b/package.json index e7ef961..502ebe3 100644 --- a/package.json +++ b/package.json @@ -1,33 +1,14 @@ { "name": "jsonp-body", "version": "1.1.0", - "description": "Helper to create more safe jsonp response body for koa and other web framework.", - "main": "index.js", - "types": "index.d.ts", - "scripts": { - "test": "mocha test/*.test.js", - "test-cov": "istanbul cover _mocha -- test/*.test.js" - }, - "dependencies": { - - }, - "devDependencies": { - "autod": "*", - "contributors": "*", - "istanbul": "*", - "jshint": "*", - "mocha": "*", - "should": "7" - }, + "description": "Helper to create more safe jsonp response body for koa and other web framework", "homepage": "https://github.com/node-modules/jsonp-body", "repository": { "type": "git", - "url": "git://github.com/node-modules/jsonp-body.git", - "web": "https://github.com/node-modules/jsonp-body" + "url": "git://github.com/node-modules/jsonp-body.git" }, "bugs": { - "url": "https://github.com/node-modules/jsonp-body/issues", - "email": "fengmk2@gmail.com" + "url": "https://github.com/node-modules/jsonp-body/issues" }, "keywords": [ "jsonp", @@ -36,9 +17,60 @@ "CVE-2014-4671", "abusing-jsonp-with-rosetta-flash" ], + "author": "fengmk2 (https://github.com/fengmk2)", + "license": "MIT", "engines": { - "node": ">= 0.10.0" + "node": ">= 18.19.0" }, - "author": "fengmk2 (https://github.com/fengmk2)", - "license": "MIT" + "dependencies": {}, + "devDependencies": { + "@arethetypeswrong/cli": "^0.17.1", + "@eggjs/tsconfig": "1", + "@types/node": "22", + "@types/mocha": "10", + "@eggjs/bin": "7", + "eslint": "8", + "eslint-config-egg": "14", + "rimraf": "6", + "tshy": "3", + "tshy-after": "1", + "typescript": "5" + }, + "scripts": { + "lint": "eslint --cache src test --ext .ts", + "pretest": "npm run clean && npm run lint -- --fix", + "test": "egg-bin test", + "preci": "npm run clean && npm run lint", + "ci": "egg-bin cov", + "postci": "npm run prepublishOnly && npm run clean", + "clean": "rimraf dist", + "prepublishOnly": "tshy && tshy-after && attw --pack" + }, + "type": "module", + "tshy": { + "exports": { + ".": "./src/index.ts", + "./package.json": "./package.json" + } + }, + "exports": { + ".": { + "import": { + "types": "./dist/esm/index.d.ts", + "default": "./dist/esm/index.js" + }, + "require": { + "types": "./dist/commonjs/index.d.ts", + "default": "./dist/commonjs/index.js" + } + }, + "./package.json": "./package.json" + }, + "files": [ + "dist", + "src" + ], + "types": "./dist/commonjs/index.d.ts", + "main": "./dist/commonjs/index.js", + "module": "./dist/esm/index.js" } diff --git a/index.js b/src/index.ts similarity index 63% rename from index.js rename to src/index.ts index cb019b5..6bbe654 100644 --- a/index.js +++ b/src/index.ts @@ -1,31 +1,23 @@ -/** - * Copyright(c) node-modules and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - -'use strict'; - -/** - * Module dependencies. - */ - -module.exports = jsonp; +interface Options { + /** length limit for callback function name, default to `512` */ + limit?: number; + /** replacer in `JSON.stringify(obj, [replacer, [space]])` */ + replacer?: Parameters[1]; + /** space in `JSON.stringify(obj, [replacer, [space]])` */ + space?: Parameters[2]; +} -function jsonp(obj, callback, options) { +export function jsonp(obj: any, callback?: string | string[], options: Options = {}): string { // fixup callback when `this.query.callback` return Array if (Array.isArray(callback)) { callback = callback[0]; } - options = options || {}; - var limit = options.limit || 512; + const limit = options.limit ?? 512; // replace chars not allowed in JavaScript that are in JSON // JSON parse vs eval fix. @see https://github.com/rack/rack-contrib/pull/37 - var body = JSON.stringify(obj, options.replacer, options.space) + const body = JSON.stringify(obj, options.replacer, options.space) .replace(/\u2028/g, '\\u2028') .replace(/\u2029/g, '\\u2029'); @@ -39,7 +31,7 @@ function jsonp(obj, callback, options) { } // Only allow "[","]","a-zA-Z0123456789_", "$" and "." characters. - var cb = callback.replace(/[^\[\]\w\$\.]+/g, ''); + const cb = callback.replace(/[^\[\]\w\$\.]+/g, ''); // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse" // @see https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-4671 diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index a15b855..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright(c) node-modules and other contributors. - * MIT Licensed - * - * Authors: - * fengmk2 (http://fengmk2.com) - */ - -"use strict"; - -/** - * Module dependencies. - */ - -var should = require('should'); -var jsonp = require('../'); - -describe('index.test.js', function () { - it('should return with padding', function () { - jsonp({foo: 'bar'}, 'f.f[1]$') - .should.equal('/**/ typeof f.f[1]$ === \'function\' && f.f[1]$({"foo":"bar"});'); - jsonp({foo: 'bar'}, 'fn') - .should.equal('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); - jsonp({foo: 'bar'}, ['fn']) - .should.equal('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); - jsonp({foo: 'bar'}, ['fn', 'fn2']) - .should.equal('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); - }); - - it('should return without padding', function () { - jsonp({foo: 'bar'}, '').should.equal('{"foo":"bar"}'); - jsonp({foo: 'bar'}).should.equal('{"foo":"bar"}'); - jsonp({foo: 'bar'}, null).should.equal('{"foo":"bar"}'); - jsonp({foo: 'bar'}, 1).should.equal('{"foo":"bar"}'); - jsonp({foo: 'bar'}, []).should.equal('{"foo":"bar"}'); - jsonp({foo: 'bar'}, [null]).should.equal('{"foo":"bar"}'); - }); - - it('should replace unsafe characters', function () { - jsonp({foo: 'bar'}, '~~~```fn---中文\u1231') - .should.equal('/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); - jsonp({foo: 'bar'}, ['fn哈哈\\!@#%^&*(){},?/ \tok']) - .should.equal('/**/ typeof fnok === \'function\' && fnok({"foo":"bar"});'); - }); - - it('should handle \\u2028 and \\u2029', function () { - jsonp({foo: 'bar\u2029-ok\u2028'}, 'fn2') - .should.equal('/**/ typeof fn2 === \'function\' && fn2({"foo":"bar\\u2029-ok\\u2028"});'); - }); - - it('should callback work', function (done) { - function fn2(data) { - data.should.eql({foo: 'bar\u2029-ok\u2028'}); - done(); - } - /* jshint ignore:start */ - eval(jsonp({foo: 'bar\u2029-ok\u2028'}, 'fn2')); - /* jshint ignore:end */ - }); - - it('should handle buffer', function () { - jsonp(new Buffer('foo'), 'fn') - .should.equal('/**/ typeof fn === \'function\' && fn(' + JSON.stringify(new Buffer('foo')) + ');'); - }); - - it('should limit callback length', function () { - jsonp(new Buffer('foo'), 'fnfn', {limit: 2}) - .should.equal('/**/ typeof fn === \'function\' && fn(' + JSON.stringify(new Buffer('foo')) + ');'); - }); -}); diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..90f1495 --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,58 @@ +import { strict as assert } from 'node:assert'; +import { jsonp } from '../src/index.js'; + +describe('index.test.ts', () => { + it('should return with padding', () => { + assert.equal(jsonp({ foo: 'bar' }, 'f.f[1]$'), + '/**/ typeof f.f[1]$ === \'function\' && f.f[1]$({"foo":"bar"});'); + assert.equal(jsonp({ foo: 'bar' }, 'fn'), + '/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); + assert.equal(jsonp({ foo: 'bar' }, [ 'fn' ]), + '/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); + assert.equal(jsonp({ foo: 'bar' }, [ 'fn', 'fn2' ]), + '/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); + }); + + it('should return without padding', () => { + assert.equal(jsonp({ foo: 'bar' }, ''), '{"foo":"bar"}'); + assert.equal(jsonp({ foo: 'bar' }), '{"foo":"bar"}'); + assert.equal((jsonp as any)({ foo: 'bar' }, null), '{"foo":"bar"}'); + assert.equal((jsonp as any)({ foo: 'bar' }, 1), '{"foo":"bar"}'); + assert.equal(jsonp({ foo: 'bar' }, []), '{"foo":"bar"}'); + assert.equal((jsonp as any)({ foo: 'bar' }, [ null ]), '{"foo":"bar"}'); + }); + + it('should replace unsafe characters', () => { + assert.equal(jsonp({ foo: 'bar' }, '~~~```fn---中文\u1231'), + '/**/ typeof fn === \'function\' && fn({"foo":"bar"});'); + assert.equal(jsonp({ foo: 'bar' }, [ 'fn哈哈\\!@#%^&*(){},?/ \tok' ]), + '/**/ typeof fnok === \'function\' && fnok({"foo":"bar"});'); + }); + + it('should handle \\u2028 and \\u2029', () => { + assert.equal(jsonp({ foo: 'bar\u2029-ok\u2028' }, 'fn2'), + '/**/ typeof fn2 === \'function\' && fn2({"foo":"bar\\u2029-ok\\u2028"});'); + }); + + it('should callback work', done => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + // eslint-disable-next-line @typescript-eslint/no-unused-vars + function fn2(data: any) { + assert.deepEqual(data, { foo: 'bar\u2029-ok\u2028' }); + done(); + } + // eslint-disable-next-line no-eval + eval(jsonp({ foo: 'bar\u2029-ok\u2028' }, 'fn2')); + }); + + it('should handle buffer', function() { + assert.equal(jsonp(Buffer.from('foo'), 'fn'), + '/**/ typeof fn === \'function\' && fn(' + JSON.stringify(Buffer.from('foo')) + ');'); + }); + + it('should limit callback length', function() { + assert.equal(jsonp(Buffer.from('foo'), 'fnfn', { limit: 2 }), + '/**/ typeof fn === \'function\' && fn(' + JSON.stringify(Buffer.from('foo')) + ');'); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..ff41b73 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "strict": true, + "noImplicitAny": true, + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +} From bb6d6c1095884ce2ae2fc15f56323c20f5f563e3 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sat, 11 Jan 2025 17:02:54 +0800 Subject: [PATCH 2/2] f --- .github/workflows/nodejs.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 9eafb37..fd73aac 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -11,7 +11,6 @@ jobs: name: Node.js uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: - os: 'macos-latest' version: '18.19.0, 20, 22' secrets: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}