fatsify核心功能示例测试!!!
This commit is contained in:
24
node_modules/fast-json-stringify/lib/location.js
generated
vendored
Normal file
24
node_modules/fast-json-stringify/lib/location.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
class Location {
|
||||
constructor (schema, schemaId, jsonPointer = '#') {
|
||||
this.schema = schema
|
||||
this.schemaId = schemaId
|
||||
this.jsonPointer = jsonPointer
|
||||
}
|
||||
|
||||
getPropertyLocation (propertyName) {
|
||||
const propertyLocation = new Location(
|
||||
this.schema[propertyName],
|
||||
this.schemaId,
|
||||
this.jsonPointer + '/' + propertyName
|
||||
)
|
||||
return propertyLocation
|
||||
}
|
||||
|
||||
getSchemaRef () {
|
||||
return this.schemaId + this.jsonPointer
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Location
|
||||
9
node_modules/fast-json-stringify/lib/merge-schemas.js
generated
vendored
Normal file
9
node_modules/fast-json-stringify/lib/merge-schemas.js
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const { mergeSchemas: _mergeSchemas } = require('@fastify/merge-json-schemas')
|
||||
|
||||
function mergeSchemas (schemas) {
|
||||
return _mergeSchemas(schemas, { onConflict: 'skip' })
|
||||
}
|
||||
|
||||
module.exports = mergeSchemas
|
||||
1134
node_modules/fast-json-stringify/lib/schema-validator.js
generated
vendored
Normal file
1134
node_modules/fast-json-stringify/lib/schema-validator.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
139
node_modules/fast-json-stringify/lib/serializer.js
generated
vendored
Normal file
139
node_modules/fast-json-stringify/lib/serializer.js
generated
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
'use strict'
|
||||
|
||||
// eslint-disable-next-line
|
||||
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/
|
||||
|
||||
module.exports = class Serializer {
|
||||
constructor (options) {
|
||||
switch (options && options.rounding) {
|
||||
case 'floor':
|
||||
this.parseInteger = Math.floor
|
||||
break
|
||||
case 'ceil':
|
||||
this.parseInteger = Math.ceil
|
||||
break
|
||||
case 'round':
|
||||
this.parseInteger = Math.round
|
||||
break
|
||||
case 'trunc':
|
||||
default:
|
||||
this.parseInteger = Math.trunc
|
||||
break
|
||||
}
|
||||
this._options = options
|
||||
}
|
||||
|
||||
asInteger (i) {
|
||||
if (Number.isInteger(i)) {
|
||||
return '' + i
|
||||
} else if (typeof i === 'bigint') {
|
||||
return i.toString()
|
||||
}
|
||||
/* eslint no-undef: "off" */
|
||||
const integer = this.parseInteger(i)
|
||||
// check if number is Infinity or NaN
|
||||
// eslint-disable-next-line no-self-compare
|
||||
if (integer === Infinity || integer === -Infinity || integer !== integer) {
|
||||
throw new Error(`The value "${i}" cannot be converted to an integer.`)
|
||||
}
|
||||
return '' + integer
|
||||
}
|
||||
|
||||
asNumber (i) {
|
||||
// fast cast to number
|
||||
const num = Number(i)
|
||||
// check if number is NaN
|
||||
// eslint-disable-next-line no-self-compare
|
||||
if (num !== num) {
|
||||
throw new Error(`The value "${i}" cannot be converted to a number.`)
|
||||
} else if (num === Infinity || num === -Infinity) {
|
||||
return 'null'
|
||||
} else {
|
||||
return '' + num
|
||||
}
|
||||
}
|
||||
|
||||
asBoolean (bool) {
|
||||
return bool && 'true' || 'false' // eslint-disable-line
|
||||
}
|
||||
|
||||
asDateTime (date) {
|
||||
if (date === null) return '""'
|
||||
if (date instanceof Date) {
|
||||
return '"' + date.toISOString() + '"'
|
||||
}
|
||||
if (typeof date === 'string') {
|
||||
return '"' + date + '"'
|
||||
}
|
||||
throw new Error(`The value "${date}" cannot be converted to a date-time.`)
|
||||
}
|
||||
|
||||
asDate (date) {
|
||||
if (date === null) return '""'
|
||||
if (date instanceof Date) {
|
||||
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"'
|
||||
}
|
||||
if (typeof date === 'string') {
|
||||
return '"' + date + '"'
|
||||
}
|
||||
throw new Error(`The value "${date}" cannot be converted to a date.`)
|
||||
}
|
||||
|
||||
asTime (date) {
|
||||
if (date === null) return '""'
|
||||
if (date instanceof Date) {
|
||||
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"'
|
||||
}
|
||||
if (typeof date === 'string') {
|
||||
return '"' + date + '"'
|
||||
}
|
||||
throw new Error(`The value "${date}" cannot be converted to a time.`)
|
||||
}
|
||||
|
||||
asString (str) {
|
||||
const len = str.length
|
||||
if (len < 42) {
|
||||
// magically escape strings for json
|
||||
// relying on their charCodeAt
|
||||
// everything below 32 needs JSON.stringify()
|
||||
// every string that contain surrogate needs JSON.stringify()
|
||||
// 34 and 92 happens all the time, so we
|
||||
// have a fast case for them
|
||||
let result = ''
|
||||
let last = -1
|
||||
let point = 255
|
||||
for (let i = 0; i < len; i++) {
|
||||
point = str.charCodeAt(i)
|
||||
if (
|
||||
point === 0x22 || // '"'
|
||||
point === 0x5c // '\'
|
||||
) {
|
||||
last === -1 && (last = 0)
|
||||
result += str.slice(last, i) + '\\'
|
||||
last = i
|
||||
} else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
|
||||
// The current character is non-printable characters or a surrogate.
|
||||
return JSON.stringify(str)
|
||||
}
|
||||
}
|
||||
return (last === -1 && ('"' + str + '"')) || ('"' + result + str.slice(last) + '"')
|
||||
} else if (len < 5000 && STR_ESCAPE.test(str) === false) {
|
||||
// Only use the regular expression for shorter input. The overhead is otherwise too much.
|
||||
return '"' + str + '"'
|
||||
} else {
|
||||
return JSON.stringify(str)
|
||||
}
|
||||
}
|
||||
|
||||
asUnsafeString (str) {
|
||||
return '"' + str + '"'
|
||||
}
|
||||
|
||||
getState () {
|
||||
return this._options
|
||||
}
|
||||
|
||||
static restoreFromState (state) {
|
||||
return new Serializer(state)
|
||||
}
|
||||
}
|
||||
34
node_modules/fast-json-stringify/lib/standalone.js
generated
vendored
Normal file
34
node_modules/fast-json-stringify/lib/standalone.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
function buildStandaloneCode (contextFunc, context, serializer, validator) {
|
||||
let ajvDependencyCode = ''
|
||||
if (context.validatorSchemasIds.size > 0) {
|
||||
ajvDependencyCode += 'const Validator = require(\'fast-json-stringify/lib/validator\')\n'
|
||||
ajvDependencyCode += `const validatorState = ${JSON.stringify(validator.getState())}\n`
|
||||
ajvDependencyCode += 'const validator = Validator.restoreFromState(validatorState)\n'
|
||||
} else {
|
||||
ajvDependencyCode += 'const validator = null\n'
|
||||
}
|
||||
|
||||
// Don't need to keep external schemas once compiled
|
||||
// validatorState will hold external schemas if it needs them
|
||||
const { schema, ...serializerState } = serializer.getState()
|
||||
|
||||
return `
|
||||
'use strict'
|
||||
|
||||
const Serializer = require('fast-json-stringify/lib/serializer')
|
||||
const serializerState = ${JSON.stringify(serializerState)}
|
||||
const serializer = Serializer.restoreFromState(serializerState)
|
||||
|
||||
${ajvDependencyCode}
|
||||
|
||||
module.exports = ${contextFunc.toString()}(validator, serializer)`
|
||||
}
|
||||
|
||||
module.exports = buildStandaloneCode
|
||||
|
||||
module.exports.dependencies = {
|
||||
Serializer: require('./serializer'),
|
||||
Validator: require('./validator')
|
||||
}
|
||||
94
node_modules/fast-json-stringify/lib/validator.js
generated
vendored
Normal file
94
node_modules/fast-json-stringify/lib/validator.js
generated
vendored
Normal file
@@ -0,0 +1,94 @@
|
||||
'use strict'
|
||||
|
||||
const Ajv = require('ajv')
|
||||
const fastUri = require('fast-uri')
|
||||
const ajvFormats = require('ajv-formats')
|
||||
const clone = require('rfdc')({ proto: true })
|
||||
|
||||
class Validator {
|
||||
constructor (ajvOptions) {
|
||||
this.ajv = new Ajv({
|
||||
...ajvOptions,
|
||||
strictSchema: false,
|
||||
validateSchema: false,
|
||||
allowUnionTypes: true,
|
||||
uriResolver: fastUri
|
||||
})
|
||||
|
||||
ajvFormats(this.ajv)
|
||||
|
||||
this.ajv.addKeyword({
|
||||
keyword: 'fjs_type',
|
||||
type: 'object',
|
||||
errors: false,
|
||||
validate: (_type, date) => {
|
||||
return date instanceof Date
|
||||
}
|
||||
})
|
||||
|
||||
this._ajvSchemas = {}
|
||||
this._ajvOptions = ajvOptions || {}
|
||||
}
|
||||
|
||||
addSchema (schema, schemaName) {
|
||||
let schemaKey = schema.$id || schemaName
|
||||
if (schema.$id !== undefined && schema.$id[0] === '#') {
|
||||
schemaKey = schemaName + schema.$id // relative URI
|
||||
}
|
||||
|
||||
if (
|
||||
this.ajv.refs[schemaKey] === undefined &&
|
||||
this.ajv.schemas[schemaKey] === undefined
|
||||
) {
|
||||
const ajvSchema = clone(schema)
|
||||
this.convertSchemaToAjvFormat(ajvSchema)
|
||||
this.ajv.addSchema(ajvSchema, schemaKey)
|
||||
this._ajvSchemas[schemaKey] = schema
|
||||
}
|
||||
}
|
||||
|
||||
validate (schemaRef, data) {
|
||||
return this.ajv.validate(schemaRef, data)
|
||||
}
|
||||
|
||||
// Ajv does not support js date format. In order to properly validate objects containing a date,
|
||||
// it needs to replace all occurrences of the string date format with a custom keyword fjs_type.
|
||||
// (see https://github.com/fastify/fast-json-stringify/pull/441)
|
||||
convertSchemaToAjvFormat (schema) {
|
||||
if (schema === null) return
|
||||
|
||||
if (schema.type === 'string') {
|
||||
schema.fjs_type = 'string'
|
||||
schema.type = ['string', 'object']
|
||||
} else if (
|
||||
Array.isArray(schema.type) &&
|
||||
schema.type.includes('string') &&
|
||||
!schema.type.includes('object')
|
||||
) {
|
||||
schema.fjs_type = 'string'
|
||||
schema.type.push('object')
|
||||
}
|
||||
for (const property in schema) {
|
||||
if (typeof schema[property] === 'object') {
|
||||
this.convertSchemaToAjvFormat(schema[property])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getState () {
|
||||
return {
|
||||
ajvOptions: this._ajvOptions,
|
||||
ajvSchemas: this._ajvSchemas
|
||||
}
|
||||
}
|
||||
|
||||
static restoreFromState (state) {
|
||||
const validator = new Validator(state.ajvOptions)
|
||||
for (const [id, ajvSchema] of Object.entries(state.ajvSchemas)) {
|
||||
validator.ajv.addSchema(ajvSchema, id)
|
||||
}
|
||||
return validator
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Validator
|
||||
Reference in New Issue
Block a user