diff --git a/.github/dependabot.yml b/.github/dependabot.yml index dfa7fa6..35d66ca 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -9,5 +9,5 @@ updates: - package-ecosystem: "npm" directory: "/" schedule: - interval: "weekly" + interval: "monthly" open-pull-requests-limit: 10 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8ef7a89..fd45202 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,6 @@ on: push: branches: - main - - master - next - 'v*' paths-ignore: @@ -15,9 +14,15 @@ on: - 'docs/**' - '*.md' +permissions: + contents: read + jobs: test: + permissions: + contents: write + pull-requests: write uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5 with: - lint: true license-check: true + lint: true diff --git a/README.md b/README.md index a9bf8eb..dcc1ecd 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This package is responsible for compiling the application's `response` JSON sche | v1.x | v3.x | ^3.x | | v2.x | v3.x | ^4.x | | v3.x | v4.x | ^4.x | -| v4.x | v5.x | ^4.x | +| v4.x | v5.x | ^5.x | ### fast-json-stringify Configuration diff --git a/index.js b/index.js index a021ea1..9323c97 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,8 @@ 'use strict' -const fastJsonStringify = require('fast-json-stringify') - -function SerializerSelector () { - return function buildSerializerFactory (externalSchemas, serializerOpts) { - const fjsOpts = Object.assign({}, serializerOpts, { schema: externalSchemas }) - return responseSchemaCompiler.bind(null, fjsOpts) - } -} - -function responseSchemaCompiler (fjsOpts, { schema /* method, url, httpStatus */ }) { - if (fjsOpts.schema && schema.$id && fjsOpts.schema[schema.$id]) { - fjsOpts.schema = { ...fjsOpts.schema } - delete fjsOpts.schema[schema.$id] - } - return fastJsonStringify(schema, fjsOpts) -} +const { SerializerSelector, StandaloneSerializer } = require('./standalone') module.exports = SerializerSelector module.exports.default = SerializerSelector module.exports.SerializerSelector = SerializerSelector -module.exports.StandaloneSerializer = require('./standalone') +module.exports.StandaloneSerializer = StandaloneSerializer diff --git a/package.json b/package.json index c409304..cc104b7 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "@fastify/fast-json-stringify-compiler", "description": "Build and manage the fast-json-stringify instances for the fastify framework", - "version": "5.0.2", + "version": "5.0.3", "main": "index.js", "type": "commonjs", "types": "types/index.d.ts", "scripts": { "lint": "eslint", "lint:fix": "eslint --fix", - "unit": "tap test/**/*.test.js", + "unit": "c8 --100 node --test", "test": "npm run unit && npm run test:typescript", "test:typescript": "tsd" }, @@ -54,11 +54,11 @@ ], "devDependencies": { "@fastify/pre-commit": "^2.1.0", + "c8": "^10.1.3", "eslint": "^9.17.0", "fastify": "^5.0.0", "neostandard": "^0.12.0", "sanitize-filename": "^1.6.3", - "tap": "^18.7.2", "tsd": "^0.31.0" }, "pre-commit": [ diff --git a/standalone.js b/standalone.js index 39b4937..7f0d40a 100644 --- a/standalone.js +++ b/standalone.js @@ -1,6 +1,21 @@ 'use strict' -const SerializerSelector = require('./index') +const fastJsonStringify = require('fast-json-stringify') + +function SerializerSelector () { + return function buildSerializerFactory (externalSchemas, serializerOpts) { + const fjsOpts = Object.assign({}, serializerOpts, { schema: externalSchemas }) + return responseSchemaCompiler.bind(null, fjsOpts) + } +} + +function responseSchemaCompiler (fjsOpts, { schema /* method, url, httpStatus */ }) { + if (fjsOpts.schema && schema.$id && fjsOpts.schema[schema.$id]) { + fjsOpts.schema = { ...fjsOpts.schema } + delete fjsOpts.schema[schema.$id] + } + return fastJsonStringify(schema, fjsOpts) +} function StandaloneSerializer (options = { readMode: true }) { if (options.readMode === true && typeof options.restoreFunction !== 'function') { @@ -38,5 +53,6 @@ function StandaloneSerializer (options = { readMode: true }) { } } -module.exports = StandaloneSerializer +module.exports.SerializerSelector = SerializerSelector +module.exports.StandaloneSerializer = StandaloneSerializer module.exports.default = StandaloneSerializer diff --git a/test/duplicate-schema.test.js b/test/duplicate-schema.test.js index d6904ed..2acaa24 100644 --- a/test/duplicate-schema.test.js +++ b/test/duplicate-schema.test.js @@ -1,9 +1,9 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const FjsCompiler = require('../index') -t.test('Use input schema duplicate in the externalSchemas', async t => { +test('Use input schema duplicate in the externalSchemas', async t => { t.plan(1) const externalSchemas = { schema1: { @@ -22,5 +22,5 @@ t.test('Use input schema duplicate in the externalSchemas', async t => { compiler({ schema: externalSchemas.schema1 }) compiler({ schema: externalSchemas.schema2 }) - t.pass() + t.assert.ok(true) }) diff --git a/test/plugin.test.js b/test/plugin.test.js index 11e34d8..26ca0f5 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -1,6 +1,6 @@ 'use strict' -const t = require('tap') +const { test } = require('node:test') const fastify = require('fastify') const FjsCompiler = require('../index') @@ -27,16 +27,16 @@ const externalSchemas2 = Object.freeze({ const fastifyFjsOptionsDefault = Object.freeze({}) -t.test('basic usage', t => { +test('basic usage', t => { t.plan(1) const factory = FjsCompiler() const compiler = factory(externalSchemas1, fastifyFjsOptionsDefault) const serializeFunc = compiler({ schema: sampleSchema }) const result = serializeFunc({ name: 'hello' }) - t.equal(result, '{"name":"hello"}') + t.assert.equal(result, '{"name":"hello"}') }) -t.test('fastify integration', async t => { +test('fastify integration', async t => { const factory = FjsCompiler() const app = fastify({ @@ -73,6 +73,6 @@ t.test('fastify integration', async t => { } }) - t.equal(res.statusCode, 200) - t.same(res.json(), { name: 'serialize me' }) + t.assert.equal(res.statusCode, 200) + t.assert.deepStrictEqual(res.json(), { name: 'serialize me' }) }) diff --git a/test/standalone.test.js b/test/standalone.test.js index ba11330..6ecdc17 100644 --- a/test/standalone.test.js +++ b/test/standalone.test.js @@ -2,7 +2,7 @@ const fs = require('node:fs') const path = require('node:path') -const t = require('tap') +const { test } = require('node:test') const fastify = require('fastify') const sanitize = require('sanitize-filename') @@ -16,10 +16,10 @@ function generateFileName (routeOpts) { return fileName } -t.test('standalone', t => { +test('standalone', async t => { t.plan(5) - t.teardown(async () => { + t.after(async () => { for (const fileName of generatedFileNames) { try { await fs.promises.unlink(path.join(__dirname, fileName)) @@ -29,10 +29,10 @@ t.test('standalone', t => { t.test('errors', t => { t.plan(2) - t.throws(() => { + t.assert.throws(() => { FjsStandaloneCompiler() }, 'missing restoreFunction') - t.throws(() => { + t.assert.throws(() => { FjsStandaloneCompiler({ readMode: false }) }, 'missing storeFunction') }) @@ -74,28 +74,28 @@ t.test('standalone', t => { const factory = FjsStandaloneCompiler({ readMode: false, storeFunction (routeOpts, schemaSerializerCode) { - t.same(routeOpts, endpointSchema) - t.type(schemaSerializerCode, 'string') + t.assert.deepStrictEqual(routeOpts, endpointSchema) + t.assert.ok(typeof schemaSerializerCode === 'string') fs.writeFileSync(path.join(__dirname, '/fjs-generated.js'), schemaSerializerCode) generatedFileNames.push('/fjs-generated.js') - t.pass('stored the serializer function') + t.assert.ok('stored the serializer function') } }) const compiler = factory(schemaMap) compiler(endpointSchema) - t.pass('compiled the endpoint schema') + t.assert.ok('compiled the endpoint schema') t.test('usage standalone code', t => { t.plan(3) const standaloneSerializer = require('./fjs-generated') - t.ok(standaloneSerializer) + t.assert.ok(standaloneSerializer) const valid = standaloneSerializer({ hello: 'world' }) - t.same(valid, JSON.stringify({ hello: 'world' })) + t.assert.deepStrictEqual(valid, JSON.stringify({ hello: 'world' })) const invalid = standaloneSerializer({ hello: [] }) - t.same(invalid, '{"hello":""}') + t.assert.deepStrictEqual(invalid, '{"hello":""}') }) }) @@ -106,9 +106,9 @@ t.test('standalone', t => { readMode: false, storeFunction (routeOpts, schemaSerializationCode) { const fileName = generateFileName(routeOpts) - t.ok(routeOpts) + t.assert.ok(routeOpts) fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode) - t.pass(`stored the serializer function ${fileName}`) + t.assert.ok(`stored the serializer function ${fileName}`) }, restoreFunction () { t.fail('write mode ON') @@ -119,16 +119,16 @@ t.test('standalone', t => { await app.ready() }) - t.test('fastify integration - writeMode forces standalone', async t => { + await t.test('fastify integration - writeMode forces standalone', async t => { t.plan(4) const factory = FjsStandaloneCompiler({ readMode: false, storeFunction (routeOpts, schemaSerializationCode) { const fileName = generateFileName(routeOpts) - t.ok(routeOpts) + t.assert.ok(routeOpts) fs.writeFileSync(path.join(__dirname, fileName), schemaSerializationCode) - t.pass(`stored the serializer function ${fileName}`) + t.assert.ok(`stored the serializer function ${fileName}`) }, restoreFunction () { t.fail('write mode ON') @@ -143,7 +143,7 @@ t.test('standalone', t => { await app.ready() }) - t.test('fastify integration - readMode', async t => { + await t.test('fastify integration - readMode', async t => { t.plan(6) const factory = FjsStandaloneCompiler({ @@ -153,7 +153,7 @@ t.test('standalone', t => { }, restoreFunction (routeOpts) { const fileName = generateFileName(routeOpts) - t.pass(`restore the serializer function ${fileName}}`) + t.assert.ok(`restore the serializer function ${fileName}}`) return require(path.join(__dirname, fileName)) } }) @@ -165,15 +165,15 @@ t.test('standalone', t => { url: '/foo', method: 'POST' }) - t.equal(res.statusCode, 200) - t.equal(res.payload, JSON.stringify({ hello: 'world' })) + t.assert.equal(res.statusCode, 200) + t.assert.equal(res.payload, JSON.stringify({ hello: 'world' })) res = await app.inject({ url: '/bar?lang=it', method: 'GET' }) - t.equal(res.statusCode, 200) - t.equal(res.payload, JSON.stringify({ lang: 'en' })) + t.assert.equal(res.statusCode, 200) + t.assert.equal(res.payload, JSON.stringify({ lang: 'en' })) }) function buildApp (factory, serializerOpts) {