@fastify/fast-json-stringify-compiler
Advanced tools
Comparing version
19
index.js
'use strict' | ||
const fastJsonStringify = require('fast-json-stringify') | ||
const { SerializerSelector, StandaloneSerializer } = require('./standalone') | ||
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) | ||
} | ||
module.exports = SerializerSelector | ||
module.exports.default = SerializerSelector | ||
module.exports.SerializerSelector = SerializerSelector | ||
module.exports.StandaloneSerializer = require('./standalone') | ||
module.exports.StandaloneSerializer = StandaloneSerializer |
{ | ||
"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", | ||
@@ -11,3 +11,3 @@ "type": "commonjs", | ||
"lint:fix": "eslint --fix", | ||
"unit": "tap test/**/*.test.js", | ||
"unit": "c8 --100 node --test", | ||
"test": "npm run unit && npm run test:typescript", | ||
@@ -58,2 +58,3 @@ "test:typescript": "tsd" | ||
"@fastify/pre-commit": "^2.1.0", | ||
"c8": "^10.1.3", | ||
"eslint": "^9.17.0", | ||
@@ -63,3 +64,2 @@ "fastify": "^5.0.0", | ||
"sanitize-filename": "^1.6.3", | ||
"tap": "^18.7.2", | ||
"tsd": "^0.31.0" | ||
@@ -66,0 +66,0 @@ }, |
@@ -17,3 +17,3 @@ # @fastify/fast-json-stringify-compiler | ||
| v3.x | v4.x | ^4.x | | ||
| v4.x | v5.x | ^4.x | | ||
| v4.x | v5.x | ^5.x | | ||
@@ -20,0 +20,0 @@ ### fast-json-stringify Configuration |
'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 }) { | ||
@@ -41,3 +56,4 @@ if (options.readMode === true && typeof options.restoreFunction !== 'function') { | ||
module.exports = StandaloneSerializer | ||
module.exports.SerializerSelector = SerializerSelector | ||
module.exports.StandaloneSerializer = StandaloneSerializer | ||
module.exports.default = StandaloneSerializer |
'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) | ||
@@ -25,3 +25,3 @@ const externalSchemas = { | ||
t.pass() | ||
t.assert.ok(true) | ||
}) |
'use strict' | ||
const t = require('tap') | ||
const { test } = require('node:test') | ||
const fastify = require('fastify') | ||
@@ -30,3 +30,3 @@ const FjsCompiler = require('../index') | ||
t.test('basic usage', t => { | ||
test('basic usage', t => { | ||
t.plan(1) | ||
@@ -37,6 +37,6 @@ const factory = FjsCompiler() | ||
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() | ||
@@ -78,4 +78,4 @@ | ||
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' }) | ||
}) |
@@ -5,3 +5,3 @@ 'use strict' | ||
const path = require('node:path') | ||
const t = require('tap') | ||
const { test } = require('node:test') | ||
const fastify = require('fastify') | ||
@@ -20,6 +20,6 @@ const sanitize = require('sanitize-filename') | ||
t.test('standalone', t => { | ||
test('standalone', async t => { | ||
t.plan(5) | ||
t.teardown(async () => { | ||
t.after(async () => { | ||
for (const fileName of generatedFileNames) { | ||
@@ -34,6 +34,6 @@ try { | ||
t.plan(2) | ||
t.throws(() => { | ||
t.assert.throws(() => { | ||
FjsStandaloneCompiler() | ||
}, 'missing restoreFunction') | ||
t.throws(() => { | ||
t.assert.throws(() => { | ||
FjsStandaloneCompiler({ readMode: false }) | ||
@@ -80,7 +80,7 @@ }, 'missing storeFunction') | ||
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') | ||
} | ||
@@ -91,3 +91,3 @@ }) | ||
compiler(endpointSchema) | ||
t.pass('compiled the endpoint schema') | ||
t.assert.ok('compiled the endpoint schema') | ||
@@ -97,9 +97,9 @@ t.test('usage standalone code', t => { | ||
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":""}') | ||
}) | ||
@@ -115,5 +115,5 @@ }) | ||
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}`) | ||
}, | ||
@@ -129,3 +129,3 @@ restoreFunction () { | ||
t.test('fastify integration - writeMode forces standalone', async t => { | ||
await t.test('fastify integration - writeMode forces standalone', async t => { | ||
t.plan(4) | ||
@@ -137,5 +137,5 @@ | ||
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}`) | ||
}, | ||
@@ -155,3 +155,3 @@ restoreFunction () { | ||
t.test('fastify integration - readMode', async t => { | ||
await t.test('fastify integration - readMode', async t => { | ||
t.plan(6) | ||
@@ -166,3 +166,3 @@ | ||
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)) | ||
@@ -179,4 +179,4 @@ } | ||
}) | ||
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' })) | ||
@@ -187,4 +187,4 @@ res = await app.inject({ | ||
}) | ||
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' })) | ||
}) | ||
@@ -191,0 +191,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24278
1.8%495
0.2%1
-50%