fatsify核心功能示例测试!!!
This commit is contained in:
2
node_modules/process-warning/.gitattributes
generated
vendored
Normal file
2
node_modules/process-warning/.gitattributes
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Set default behavior to automatically convert line endings
|
||||
* text=auto eol=lf
|
||||
13
node_modules/process-warning/.github/dependabot.yml
generated
vendored
Normal file
13
node_modules/process-warning/.github/dependabot.yml
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
open-pull-requests-limit: 10
|
||||
22
node_modules/process-warning/.github/workflows/ci.yml
generated
vendored
Normal file
22
node_modules/process-warning/.github/workflows/ci.yml
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- next
|
||||
- 'v*'
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- 'docs/**'
|
||||
- '*.md'
|
||||
|
||||
jobs:
|
||||
test:
|
||||
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
|
||||
with:
|
||||
license-check: true
|
||||
lint: true
|
||||
21
node_modules/process-warning/LICENSE
generated
vendored
Normal file
21
node_modules/process-warning/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Fastify
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
118
node_modules/process-warning/README.md
generated
vendored
Normal file
118
node_modules/process-warning/README.md
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
# process-warning
|
||||
|
||||
[](https://github.com/fastify/process-warning/actions/workflows/ci.yml)
|
||||
[](https://www.npmjs.com/package/process-warning)
|
||||
[](https://github.com/neostandard/neostandard)
|
||||
|
||||
A small utility for generating consistent warning objects across your codebase.
|
||||
It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once (unless configured otherwise).
|
||||
|
||||
_This module is used by the [Fastify](https://fastify.dev) framework and it was called `fastify-warning` prior to version 1.0.0._
|
||||
|
||||
### Install
|
||||
|
||||
```
|
||||
npm i process-warning
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
The module exports two builder functions for creating warnings.
|
||||
|
||||
```js
|
||||
const {
|
||||
createWarning,
|
||||
createDeprecation
|
||||
} = require('process-warning')
|
||||
|
||||
const warning = createWarning({
|
||||
name: 'ExampleWarning',
|
||||
code: 'EXP_WRN_001',
|
||||
message: 'Hello %s',
|
||||
unlimited: true
|
||||
})
|
||||
warning('world')
|
||||
```
|
||||
|
||||
#### Methods
|
||||
|
||||
##### `createWarning({ name, code, message[, unlimited] })`
|
||||
|
||||
- `name` (`string`, required) - The error name, you can access it later with
|
||||
`error.name`. For consistency, we recommend prefixing module error names
|
||||
with `{YourModule}Warning`
|
||||
- `code` (`string`, required) - The warning code, you can access it later with
|
||||
`error.code`. For consistency, we recommend prefixing plugin error codes with
|
||||
`{ThreeLetterModuleName}_`, e.g. `FST_`. NOTE: codes should be all uppercase.
|
||||
- `message` (`string`, required) - The warning message. You can also use
|
||||
interpolated strings for formatting the message.
|
||||
- `options` (`object`, optional) - Optional options with the following
|
||||
properties:
|
||||
+ `unlimited` (`boolean`, optional) - Should the warning be emitted more than
|
||||
once? Defaults to `false`.
|
||||
|
||||
|
||||
##### `createDeprecation({code, message[, options]})`
|
||||
|
||||
This is a wrapper for `createWarning`. It is equivalent to invoking
|
||||
`createWarning` with the `name` parameter set to "DeprecationWarning".
|
||||
|
||||
Deprecation warnings have extended support for the Node.js CLI options:
|
||||
`--throw-deprecation`, `--no-deprecation`, and `--trace-deprecation`.
|
||||
|
||||
##### `warning([, a [, b [, c]]])`
|
||||
|
||||
The returned `warning` function can used for emitting warnings.
|
||||
A warning is guaranteed to be emitted at least once.
|
||||
|
||||
- `[, a [, b [, c]]]` (`any`, optional) - Parameters for string interpolation.
|
||||
|
||||
```js
|
||||
const { createWarning } = require('process-warning')
|
||||
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'message' })
|
||||
FST_ERROR_CODE()
|
||||
```
|
||||
|
||||
How to use an interpolated string:
|
||||
```js
|
||||
const { createWarning } = require('process-warning')
|
||||
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
|
||||
FST_ERROR_CODE('world')
|
||||
```
|
||||
|
||||
The `warning` object has methods and properties for managing the warning's state. Useful for testing.
|
||||
```js
|
||||
const { createWarning } = require('process-warning')
|
||||
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
|
||||
console.log(FST_ERROR_CODE.emitted) // false
|
||||
FST_ERROR_CODE('world')
|
||||
console.log(FST_ERROR_CODE.emitted) // true
|
||||
|
||||
const FST_ERROR_CODE_2 = createWarning('MyAppWarning', 'FST_ERROR_CODE_2', 'Hello %s')
|
||||
FST_ERROR_CODE_2.emitted = true
|
||||
FST_ERROR_CODE_2('world') // will not be emitted because it is not unlimited
|
||||
```
|
||||
|
||||
How to use an unlimited warning:
|
||||
```js
|
||||
const { createWarning } = require('process-warning')
|
||||
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s', unlimited: true })
|
||||
FST_ERROR_CODE('world') // will be emitted
|
||||
FST_ERROR_CODE('world') // will be emitted again
|
||||
```
|
||||
|
||||
#### Suppressing warnings
|
||||
|
||||
It is possible to suppress warnings by utilizing one of node's built-in warning suppression mechanisms.
|
||||
|
||||
Warnings can be suppressed:
|
||||
|
||||
- by setting the `NODE_NO_WARNINGS` environment variable to `1`
|
||||
- by passing the `--no-warnings` flag to the node process
|
||||
- by setting '--no-warnings' in the `NODE_OPTIONS` environment variable
|
||||
|
||||
For more information see [node's documentation](https://nodejs.org/api/cli.html).
|
||||
|
||||
## License
|
||||
|
||||
Licensed under [MIT](./LICENSE).
|
||||
25
node_modules/process-warning/benchmarks/warn.js
generated
vendored
Normal file
25
node_modules/process-warning/benchmarks/warn.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
const { Suite } = require('benchmark')
|
||||
const { createWarning } = require('..')
|
||||
|
||||
const err1 = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'TST_ERROR_CODE_1',
|
||||
message: 'message'
|
||||
})
|
||||
const err2 = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'TST_ERROR_CODE_2',
|
||||
message: 'message'
|
||||
})
|
||||
|
||||
new Suite()
|
||||
.add('warn', function () {
|
||||
err1()
|
||||
err2()
|
||||
})
|
||||
.on('cycle', function (event) {
|
||||
console.log(String(event.target))
|
||||
})
|
||||
.run()
|
||||
6
node_modules/process-warning/eslint.config.js
generated
vendored
Normal file
6
node_modules/process-warning/eslint.config.js
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = require('neostandard')({
|
||||
ignores: require('neostandard').resolveIgnoresFromGitignore(),
|
||||
ts: true
|
||||
})
|
||||
11
node_modules/process-warning/examples/example.js
generated
vendored
Normal file
11
node_modules/process-warning/examples/example.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const { createWarning } = require('..')
|
||||
|
||||
const CUSTDEP001 = createWarning({
|
||||
name: 'DeprecationWarning',
|
||||
code: 'CUSTDEP001',
|
||||
message: 'This is a deprecation warning'
|
||||
})
|
||||
|
||||
CUSTDEP001()
|
||||
124
node_modules/process-warning/index.js
generated
vendored
Normal file
124
node_modules/process-warning/index.js
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
'use strict'
|
||||
|
||||
const { format } = require('node:util')
|
||||
|
||||
/**
|
||||
* @namespace processWarning
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents a warning item with details.
|
||||
* @typedef {Function} WarningItem
|
||||
* @param {*} [a] Possible message interpolation value.
|
||||
* @param {*} [b] Possible message interpolation value.
|
||||
* @param {*} [c] Possible message interpolation value.
|
||||
* @property {string} name - The name of the warning.
|
||||
* @property {string} code - The code associated with the warning.
|
||||
* @property {string} message - The warning message.
|
||||
* @property {boolean} emitted - Indicates if the warning has been emitted.
|
||||
* @property {function} format - Formats the warning message.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Options for creating a process warning.
|
||||
* @typedef {Object} ProcessWarningOptions
|
||||
* @property {string} name - The name of the warning.
|
||||
* @property {string} code - The code associated with the warning.
|
||||
* @property {string} message - The warning message.
|
||||
* @property {boolean} [unlimited=false] - If true, allows unlimited emissions of the warning.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents the process warning functionality.
|
||||
* @typedef {Object} ProcessWarning
|
||||
* @property {function} createWarning - Creates a warning item.
|
||||
* @property {function} createDeprecation - Creates a deprecation warning item.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a deprecation warning item.
|
||||
* @function
|
||||
* @memberof processWarning
|
||||
* @param {ProcessWarningOptions} params - Options for creating the warning.
|
||||
* @returns {WarningItem} The created deprecation warning item.
|
||||
*/
|
||||
function createDeprecation (params) {
|
||||
return createWarning({ ...params, name: 'DeprecationWarning' })
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a warning item.
|
||||
* @function
|
||||
* @memberof processWarning
|
||||
* @param {ProcessWarningOptions} params - Options for creating the warning.
|
||||
* @returns {WarningItem} The created warning item.
|
||||
* @throws {Error} Throws an error if name, code, or message is empty, or if opts.unlimited is not a boolean.
|
||||
*/
|
||||
function createWarning ({ name, code, message, unlimited = false } = {}) {
|
||||
if (!name) throw new Error('Warning name must not be empty')
|
||||
if (!code) throw new Error('Warning code must not be empty')
|
||||
if (!message) throw new Error('Warning message must not be empty')
|
||||
if (typeof unlimited !== 'boolean') throw new Error('Warning opts.unlimited must be a boolean')
|
||||
|
||||
code = code.toUpperCase()
|
||||
|
||||
let warningContainer = {
|
||||
[name]: function (a, b, c) {
|
||||
if (warning.emitted === true && warning.unlimited !== true) {
|
||||
return
|
||||
}
|
||||
warning.emitted = true
|
||||
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
|
||||
}
|
||||
}
|
||||
if (unlimited) {
|
||||
warningContainer = {
|
||||
[name]: function (a, b, c) {
|
||||
warning.emitted = true
|
||||
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const warning = warningContainer[name]
|
||||
|
||||
warning.emitted = false
|
||||
warning.message = message
|
||||
warning.unlimited = unlimited
|
||||
warning.code = code
|
||||
|
||||
/**
|
||||
* Formats the warning message.
|
||||
* @param {*} [a] Possible message interpolation value.
|
||||
* @param {*} [b] Possible message interpolation value.
|
||||
* @param {*} [c] Possible message interpolation value.
|
||||
* @returns {string} The formatted warning message.
|
||||
*/
|
||||
warning.format = function (a, b, c) {
|
||||
let formatted
|
||||
if (a && b && c) {
|
||||
formatted = format(message, a, b, c)
|
||||
} else if (a && b) {
|
||||
formatted = format(message, a, b)
|
||||
} else if (a) {
|
||||
formatted = format(message, a)
|
||||
} else {
|
||||
formatted = message
|
||||
}
|
||||
return formatted
|
||||
}
|
||||
|
||||
return warning
|
||||
}
|
||||
|
||||
/**
|
||||
* Module exports containing the process warning functionality.
|
||||
* @namespace
|
||||
* @property {function} createWarning - Creates a warning item.
|
||||
* @property {function} createDeprecation - Creates a deprecation warning item.
|
||||
* @property {ProcessWarning} processWarning - Represents the process warning functionality.
|
||||
*/
|
||||
const out = { createWarning, createDeprecation }
|
||||
module.exports = out
|
||||
module.exports.default = out
|
||||
module.exports.processWarning = out
|
||||
73
node_modules/process-warning/package.json
generated
vendored
Normal file
73
node_modules/process-warning/package.json
generated
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
{
|
||||
"name": "process-warning",
|
||||
"version": "5.0.0",
|
||||
"description": "A small utility for creating warnings and emitting them.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"types": "types/index.d.ts",
|
||||
"scripts": {
|
||||
"lint": "eslint",
|
||||
"lint:fix": "eslint --fix",
|
||||
"test": "npm run test:unit && npm run test:jest && npm run test:typescript",
|
||||
"test:jest": "jest jest.test.js",
|
||||
"test:unit": "c8 --100 node --test",
|
||||
"test:typescript": "tsd"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/fastify/process-warning.git"
|
||||
},
|
||||
"keywords": [
|
||||
"fastify",
|
||||
"error",
|
||||
"warning",
|
||||
"utility",
|
||||
"plugin",
|
||||
"emit",
|
||||
"once"
|
||||
],
|
||||
"author": "Tomas Della Vedova",
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Matteo Collina",
|
||||
"email": "hello@matteocollina.com"
|
||||
},
|
||||
{
|
||||
"name": "Manuel Spigolon",
|
||||
"email": "behemoth89@gmail.com"
|
||||
},
|
||||
{
|
||||
"name": "James Sumners",
|
||||
"url": "https://james.sumners.info"
|
||||
},
|
||||
{
|
||||
"name": "Frazer Smith",
|
||||
"email": "frazer.dev@icloud.com",
|
||||
"url": "https://github.com/fdawgs"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/fastify/fastify-warning/issues"
|
||||
},
|
||||
"homepage": "https://github.com/fastify/fastify-warning#readme",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/fastify"
|
||||
},
|
||||
{
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/fastify"
|
||||
}
|
||||
],
|
||||
"devDependencies": {
|
||||
"@fastify/pre-commit": "^2.1.0",
|
||||
"benchmark": "^2.1.4",
|
||||
"c8": "^10.1.3",
|
||||
"eslint": "^9.17.0",
|
||||
"jest": "^29.7.0",
|
||||
"neostandard": "^0.12.0",
|
||||
"tsd": "^0.31.0"
|
||||
}
|
||||
}
|
||||
34
node_modules/process-warning/test/emit-interpolated-string.test.js
generated
vendored
Normal file
34
node_modules/process-warning/test/emit-interpolated-string.test.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('..')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('emit with interpolated string', t => {
|
||||
t.plan(4)
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
|
||||
process.on('warning', onWarning)
|
||||
function onWarning (warning) {
|
||||
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
|
||||
t.assert.deepStrictEqual(warning.code, 'CODE')
|
||||
t.assert.deepStrictEqual(warning.message, 'Hello world')
|
||||
t.assert.ok(codeWarning.emitted)
|
||||
}
|
||||
|
||||
const codeWarning = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello %s'
|
||||
})
|
||||
codeWarning('world')
|
||||
codeWarning('world')
|
||||
|
||||
setImmediate(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
33
node_modules/process-warning/test/emit-once-only.test.js
generated
vendored
Normal file
33
node_modules/process-warning/test/emit-once-only.test.js
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('..')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('emit should emit a given code only once', t => {
|
||||
t.plan(4)
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
|
||||
process.on('warning', onWarning)
|
||||
function onWarning (warning) {
|
||||
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
|
||||
t.assert.deepStrictEqual(warning.code, 'CODE')
|
||||
t.assert.deepStrictEqual(warning.message, 'Hello world')
|
||||
t.assert.ok(warn.emitted)
|
||||
}
|
||||
|
||||
const warn = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello world'
|
||||
})
|
||||
warn()
|
||||
warn()
|
||||
setImmediate(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
40
node_modules/process-warning/test/emit-reset.test.js
generated
vendored
Normal file
40
node_modules/process-warning/test/emit-reset.test.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('../')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('a limited warning can be re-set', t => {
|
||||
t.plan(4)
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
let count = 0
|
||||
process.on('warning', onWarning)
|
||||
function onWarning () {
|
||||
count++
|
||||
}
|
||||
|
||||
const warn = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello world'
|
||||
})
|
||||
|
||||
warn()
|
||||
t.assert.ok(warn.emitted)
|
||||
|
||||
warn()
|
||||
t.assert.ok(warn.emitted)
|
||||
|
||||
warn.emitted = false
|
||||
warn()
|
||||
t.assert.ok(warn.emitted)
|
||||
|
||||
setImmediate(() => {
|
||||
t.assert.deepStrictEqual(count, 2)
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
35
node_modules/process-warning/test/emit-set.test.js
generated
vendored
Normal file
35
node_modules/process-warning/test/emit-set.test.js
generated
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('../')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('emit should set the emitted state', t => {
|
||||
t.plan(3)
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
|
||||
process.on('warning', onWarning)
|
||||
function onWarning () {
|
||||
t.fail('should not be called')
|
||||
}
|
||||
|
||||
const warn = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello world'
|
||||
})
|
||||
t.assert.ok(!warn.emitted)
|
||||
warn.emitted = true
|
||||
t.assert.ok(warn.emitted)
|
||||
|
||||
warn()
|
||||
t.assert.ok(warn.emitted)
|
||||
|
||||
setImmediate(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
42
node_modules/process-warning/test/emit-unlimited.test.js
generated
vendored
Normal file
42
node_modules/process-warning/test/emit-unlimited.test.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('..')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('emit should emit a given code unlimited times', t => {
|
||||
t.plan(50)
|
||||
|
||||
let runs = 0
|
||||
const expectedRun = []
|
||||
const times = 10
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
|
||||
process.on('warning', onWarning)
|
||||
function onWarning (warning) {
|
||||
t.assert.deepStrictEqual(warning.name, 'TestDeprecation')
|
||||
t.assert.deepStrictEqual(warning.code, 'CODE')
|
||||
t.assert.deepStrictEqual(warning.message, 'Hello world')
|
||||
t.assert.ok(warn.emitted)
|
||||
t.assert.deepStrictEqual(runs++, expectedRun.shift())
|
||||
}
|
||||
|
||||
const warn = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello world',
|
||||
unlimited: true
|
||||
})
|
||||
|
||||
for (let i = 0; i < times; i++) {
|
||||
expectedRun.push(i)
|
||||
warn()
|
||||
}
|
||||
setImmediate(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
99
node_modules/process-warning/test/index.test.js
generated
vendored
Normal file
99
node_modules/process-warning/test/index.test.js
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning, createDeprecation } = require('..')
|
||||
|
||||
process.removeAllListeners('warning')
|
||||
|
||||
test('Create warning with zero parameter', t => {
|
||||
t.plan(3)
|
||||
|
||||
const warnItem = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE',
|
||||
message: 'Not available'
|
||||
})
|
||||
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
|
||||
t.assert.deepStrictEqual(warnItem.message, 'Not available')
|
||||
t.assert.deepStrictEqual(warnItem.code, 'CODE')
|
||||
})
|
||||
|
||||
test('Create error with 1 parameter', t => {
|
||||
t.plan(3)
|
||||
|
||||
const warnItem = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE',
|
||||
message: 'hey %s'
|
||||
})
|
||||
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
|
||||
t.assert.deepStrictEqual(warnItem.format('alice'), 'hey alice')
|
||||
t.assert.deepStrictEqual(warnItem.code, 'CODE')
|
||||
})
|
||||
|
||||
test('Create error with 2 parameters', t => {
|
||||
t.plan(3)
|
||||
|
||||
const warnItem = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE',
|
||||
message: 'hey %s, I like your %s'
|
||||
})
|
||||
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
|
||||
t.assert.deepStrictEqual(warnItem.format('alice', 'attitude'), 'hey alice, I like your attitude')
|
||||
t.assert.deepStrictEqual(warnItem.code, 'CODE')
|
||||
})
|
||||
|
||||
test('Create error with 3 parameters', t => {
|
||||
t.plan(3)
|
||||
|
||||
const warnItem = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE',
|
||||
message: 'hey %s, I like your %s %s'
|
||||
})
|
||||
t.assert.deepStrictEqual(warnItem.name, 'TestWarning')
|
||||
t.assert.deepStrictEqual(warnItem.format('alice', 'attitude', 'see you'), 'hey alice, I like your attitude see you')
|
||||
t.assert.deepStrictEqual(warnItem.code, 'CODE')
|
||||
})
|
||||
|
||||
test('Creates a deprecation warning', t => {
|
||||
t.plan(3)
|
||||
|
||||
const deprecationItem = createDeprecation({
|
||||
name: 'DeprecationWarning',
|
||||
code: 'CODE',
|
||||
message: 'hello %s'
|
||||
})
|
||||
t.assert.deepStrictEqual(deprecationItem.name, 'DeprecationWarning')
|
||||
t.assert.deepStrictEqual(deprecationItem.format('world'), 'hello world')
|
||||
t.assert.deepStrictEqual(deprecationItem.code, 'CODE')
|
||||
})
|
||||
|
||||
test('Should throw when error code has no name', t => {
|
||||
t.plan(1)
|
||||
t.assert.throws(() => createWarning(), new Error('Warning name must not be empty'))
|
||||
})
|
||||
|
||||
test('Should throw when error has no code', t => {
|
||||
t.plan(1)
|
||||
t.assert.throws(() => createWarning({ name: 'name' }), new Error('Warning code must not be empty'))
|
||||
})
|
||||
|
||||
test('Should throw when error has no message', t => {
|
||||
t.plan(1)
|
||||
t.assert.throws(() => createWarning({
|
||||
name: 'name',
|
||||
code: 'code'
|
||||
}), new Error('Warning message must not be empty'))
|
||||
})
|
||||
|
||||
test('Cannot set unlimited other than boolean', t => {
|
||||
t.plan(1)
|
||||
t.assert.throws(() => createWarning({
|
||||
name: 'name',
|
||||
code: 'code',
|
||||
message: 'message',
|
||||
unlimited: 'unlimited'
|
||||
}), new Error('Warning opts.unlimited must be a boolean'))
|
||||
})
|
||||
38
node_modules/process-warning/test/issue-88.test.js
generated
vendored
Normal file
38
node_modules/process-warning/test/issue-88.test.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { createWarning } = require('..')
|
||||
const { withResolvers } = require('./promise')
|
||||
|
||||
test('Must not overwrite config', t => {
|
||||
t.plan(1)
|
||||
|
||||
function onWarning (warning) {
|
||||
t.assert.deepStrictEqual(warning.code, 'CODE_1')
|
||||
}
|
||||
|
||||
const a = createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE_1',
|
||||
message: 'Msg'
|
||||
})
|
||||
createWarning({
|
||||
name: 'TestWarning',
|
||||
code: 'CODE_2',
|
||||
message: 'Msg',
|
||||
unlimited: true
|
||||
})
|
||||
|
||||
const { promise, resolve } = withResolvers()
|
||||
|
||||
process.on('warning', onWarning)
|
||||
a('CODE_1')
|
||||
a('CODE_1')
|
||||
|
||||
setImmediate(() => {
|
||||
process.removeListener('warning', onWarning)
|
||||
resolve()
|
||||
})
|
||||
|
||||
return promise
|
||||
})
|
||||
24
node_modules/process-warning/test/jest.test.js
generated
vendored
Normal file
24
node_modules/process-warning/test/jest.test.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
/* global test, expect */
|
||||
'use strict'
|
||||
|
||||
const { createWarning } = require('..')
|
||||
|
||||
if (globalThis.test) {
|
||||
test('works with jest', done => {
|
||||
const code = createWarning({
|
||||
name: 'TestDeprecation',
|
||||
code: 'CODE',
|
||||
message: 'Hello world'
|
||||
})
|
||||
code('world')
|
||||
|
||||
// we cannot actually listen to process warning event
|
||||
// because jest messes with it (that's the point of this test)
|
||||
// we can only test it was emitted indirectly
|
||||
// and test no exception is raised
|
||||
setImmediate(() => {
|
||||
expect(code.emitted).toBeTruthy()
|
||||
done()
|
||||
})
|
||||
})
|
||||
}
|
||||
80
node_modules/process-warning/test/no-warnings.test.js
generated
vendored
Normal file
80
node_modules/process-warning/test/no-warnings.test.js
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('node:test')
|
||||
const { spawnSync } = require('node:child_process')
|
||||
const { resolve } = require('node:path')
|
||||
|
||||
const entry = resolve(__dirname, '../examples', 'example.js')
|
||||
|
||||
test('--no-warnings is set in cli', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
'--no-warnings',
|
||||
entry
|
||||
])
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.deepStrictEqual(stderr, '')
|
||||
})
|
||||
|
||||
test('--no-warnings is not set in cli', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
entry
|
||||
])
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/)
|
||||
})
|
||||
|
||||
test('NODE_NO_WARNINGS is set to 1', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
entry
|
||||
], {
|
||||
env: {
|
||||
NODE_NO_WARNINGS: '1'
|
||||
}
|
||||
})
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.deepStrictEqual(stderr, '')
|
||||
})
|
||||
|
||||
test('NODE_NO_WARNINGS is set to 0', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
entry
|
||||
], {
|
||||
env: {
|
||||
NODE_NO_WARNINGS: '0'
|
||||
}
|
||||
})
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/)
|
||||
})
|
||||
|
||||
test('NODE_NO_WARNINGS is not set', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
entry
|
||||
])
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.match(stderr, /\[CUSTDEP001\] DeprecationWarning: This is a deprecation warning/)
|
||||
})
|
||||
|
||||
test('NODE_Options contains --no-warnings', t => {
|
||||
t.plan(1)
|
||||
const child = spawnSync(process.execPath, [
|
||||
entry
|
||||
], {
|
||||
env: {
|
||||
NODE_OPTIONS: '--no-warnings'
|
||||
}
|
||||
})
|
||||
|
||||
const stderr = child.stderr.toString()
|
||||
t.assert.deepStrictEqual(stderr, '')
|
||||
})
|
||||
10
node_modules/process-warning/test/promise.js
generated
vendored
Normal file
10
node_modules/process-warning/test/promise.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
withResolvers: function () {
|
||||
let promiseResolve, promiseReject
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
promiseResolve = resolve
|
||||
promiseReject = reject
|
||||
})
|
||||
return { promise, resolve: promiseResolve, reject: promiseReject }
|
||||
}
|
||||
}
|
||||
37
node_modules/process-warning/types/index.d.ts
generated
vendored
Normal file
37
node_modules/process-warning/types/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
declare namespace processWarning {
|
||||
export interface WarningItem {
|
||||
(a?: any, b?: any, c?: any): void;
|
||||
name: string;
|
||||
code: string;
|
||||
message: string;
|
||||
emitted: boolean;
|
||||
unlimited: boolean;
|
||||
format(a?: any, b?: any, c?: any): string;
|
||||
}
|
||||
|
||||
export type WarningOptions = {
|
||||
name: string;
|
||||
code: string;
|
||||
message: string;
|
||||
unlimited?: boolean;
|
||||
}
|
||||
|
||||
export type DeprecationOptions = Omit<WarningOptions, 'name'>
|
||||
|
||||
export type ProcessWarningOptions = {
|
||||
unlimited?: boolean;
|
||||
}
|
||||
|
||||
export type ProcessWarning = {
|
||||
createWarning(params: WarningOptions): WarningItem;
|
||||
createDeprecation(params: DeprecationOptions): WarningItem;
|
||||
}
|
||||
|
||||
export function createWarning (params: WarningOptions): WarningItem
|
||||
export function createDeprecation (params: DeprecationOptions): WarningItem
|
||||
|
||||
const processWarning: ProcessWarning
|
||||
export { processWarning as default }
|
||||
}
|
||||
|
||||
export = processWarning
|
||||
36
node_modules/process-warning/types/index.test-d.ts
generated
vendored
Normal file
36
node_modules/process-warning/types/index.test-d.ts
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
import { expectType } from 'tsd'
|
||||
import { createWarning, createDeprecation } from '..'
|
||||
|
||||
const WarnInstance = createWarning({
|
||||
name: 'TypeScriptWarning',
|
||||
code: 'CODE',
|
||||
message: 'message'
|
||||
})
|
||||
|
||||
expectType<string>(WarnInstance.code)
|
||||
expectType<string>(WarnInstance.message)
|
||||
expectType<string>(WarnInstance.name)
|
||||
expectType<boolean>(WarnInstance.emitted)
|
||||
expectType<boolean>(WarnInstance.unlimited)
|
||||
|
||||
expectType<void>(WarnInstance())
|
||||
expectType<void>(WarnInstance('foo'))
|
||||
expectType<void>(WarnInstance('foo', 'bar'))
|
||||
|
||||
const buildWarnUnlimited = createWarning({
|
||||
name: 'TypeScriptWarning',
|
||||
code: 'CODE',
|
||||
message: 'message',
|
||||
unlimited: true
|
||||
})
|
||||
expectType<boolean>(buildWarnUnlimited.unlimited)
|
||||
|
||||
const DeprecationInstance = createDeprecation({
|
||||
code: 'CODE',
|
||||
message: 'message'
|
||||
})
|
||||
expectType<string>(DeprecationInstance.code)
|
||||
|
||||
DeprecationInstance()
|
||||
DeprecationInstance('foo')
|
||||
DeprecationInstance('foo', 'bar')
|
||||
Reference in New Issue
Block a user