Skip to content

Commit

Permalink
Use Ajv@7, remove dependency on Ajv (#115)
Browse files Browse the repository at this point in the history
* Use Ajv@7, remove dependency on Ajv

* automatically build
  • Loading branch information
mcollina authored Dec 17, 2020
1 parent bfffc36 commit a32d7cb
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ examples/typescript-server.js
test/type/index.js

.vscode

validate.js
65 changes: 65 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

# 0x
profile-*

# mac files
.DS_Store

# vim swap files
*.swp

# webstorm
.idea

# flamegraphs
profile*

# lock files
yarn.lock
package-lock.json

# generated code
examples/typescript-server.js
test/type/index.js

.vscode

.dependabot
.github
8 changes: 8 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
esm: false
ts: false
jsx: false
flow: false
coverage: true
100: true
nyc-arg:
- --exclude=validate.js
74 changes: 74 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

const Ajv = require('ajv').default
const standaloneCode = require('ajv/dist/standalone').default
const http = require('http')
const fs = require('fs')
const path = require('path')

const ajv = new Ajv({ code: { source: true } })

const urlSchema = {
oneOf: [
{ type: 'string' },
{
type: 'object',
properties: {
protocol: { type: 'string' },
hostname: { type: 'string' },
pathname: { type: 'string' }
// port type => any
// query type => any
},
additionalProperties: true,
required: ['pathname']
}
]
}

const schema = {
type: 'object',
properties: {
url: urlSchema,
path: urlSchema,
cookies: {
type: 'object',
additionalProperties: true
},
headers: {
type: 'object',
additionalProperties: true
},
query: {
type: 'object',
additionalProperties: true
},
simulate: {
type: 'object',
properties: {
end: { type: 'boolean' },
split: { type: 'boolean' },
error: { type: 'boolean' },
close: { type: 'boolean' }
}
},
authority: { type: 'string' },
remoteAddress: { type: 'string' },
method: { type: 'string', enum: http.METHODS.concat(http.METHODS.map(toLowerCase)) },
validate: { type: 'boolean' }
// payload type => any
},
additionalProperties: true,
oneOf: [
{ required: ['url'] },
{ required: ['path'] }
]
}

const validate = ajv.compile(schema)

const moduleCode = standaloneCode(ajv, validate)

fs.writeFileSync(path.join(__dirname, '/validate.js'), moduleCode)

function toLowerCase (m) { return m.toLowerCase() }
63 changes: 1 addition & 62 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,12 @@
'use strict'

const assert = require('assert')
const http = require('http')
const Ajv = require('ajv')
const Request = require('./lib/request')
const Response = require('./lib/response')

const errorMessage = 'The dispatch function has already been invoked'
const urlSchema = {
oneOf: [
{ type: 'string' },
{
type: 'object',
properties: {
protocol: { type: 'string' },
hostname: { type: 'string' },
pathname: { type: 'string' }
// port type => any
// query type => any
},
additionalProperties: true,
required: ['pathname']
}
]
}

const ajv = new Ajv()
const schema = {
type: 'object',
properties: {
url: urlSchema,
path: urlSchema,
cookies: {
type: 'object',
additionalProperties: true
},
headers: {
type: 'object',
additionalProperties: true
},
query: {
type: 'object',
additionalProperties: true
},
simulate: {
type: 'object',
properties: {
end: { type: 'boolean' },
split: { type: 'boolean' },
error: { type: 'boolean' },
close: { type: 'boolean' }
}
},
authority: { type: 'string' },
remoteAddress: { type: 'string' },
method: { type: 'string', enum: http.METHODS.concat(http.METHODS.map(toLowerCase)) },
validate: { type: 'boolean' }
// payload type => any
},
additionalProperties: true,
oneOf: [
{ required: ['url'] },
{ required: ['path'] }
]
}

const optsValidator = ajv.compile(schema)
const optsValidator = require('./validate')

function inject (dispatchFunc, options, callback) {
if (typeof callback === 'undefined') {
Expand Down Expand Up @@ -206,8 +147,6 @@ function isInjection (obj) {
return (obj instanceof Request || obj instanceof Response)
}

function toLowerCase (m) { return m.toLowerCase() }

module.exports = inject
module.exports.inject = inject
module.exports.isInjection = isInjection
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"description": "Fake HTTP injection library",
"main": "index.js",
"dependencies": {
"ajv": "^6.12.2",
"cookie": "^0.4.0",
"fastify-warning": "^0.2.0",
"readable-stream": "^3.6.0",
"set-cookie-parser": "^2.4.1"
},
"types": "index.d.ts",
"devDependencies": {
"ajv": "^7.0.0",
"@types/node": "^14.0.1",
"end-of-stream": "^1.4.4",
"form-auto-content": "^2.0.0",
Expand All @@ -22,9 +22,10 @@
"tsd": "^0.13.1"
},
"scripts": {
"prepare": "node build.js",
"test": "npm run lint && npm run unit && npm run tsd",
"lint": "standard",
"unit": "tap test/test.js test/*.test.js --100",
"unit": "tap test/test.js test/*.test.js",
"coverage": "npm run unit -- --cov --coverage-report=html",
"tsd": "tsd"
},
Expand All @@ -47,5 +48,10 @@
"homepage": "https://github.com/fastify/light-my-request/blob/master/README.md",
"tsd": {
"directory": "test"
},
"standard": {
"ignore": [
"validate.js"
]
}
}

0 comments on commit a32d7cb

Please sign in to comment.