Comparing version 1.0.0 to 1.1.0
29
cli.js
@@ -7,3 +7,3 @@ const minimist = require('minimist'); | ||
Options: | ||
Base options: | ||
-p, --port <num> Server port [default: 3000] | ||
@@ -13,5 +13,11 @@ -h, --host <host> Server host [default: "localhost"] | ||
-n, --not-found <glob> Mocks for 404 errors [default: "404.*"] | ||
-g, --ignore <glob> Files to ignore [default: none] | ||
-l, --logs Enable server logs | ||
-v, --version Show version | ||
--help Show help | ||
Mock recording: | ||
-r, --record <host> Proxy & record requests if no mock found | ||
-d, --depth <N> Folder depth for mocks [default: 1] | ||
-a, --save-headers Save response headers | ||
`; | ||
@@ -21,4 +27,4 @@ | ||
const options = minimist(args, { | ||
number: ['port'], | ||
string: ['host', 'set', 'not-found'], | ||
number: ['port', 'depth'], | ||
string: ['host', 'set', 'not-found', 'record', 'ignore'], | ||
boolean: ['help', 'version', 'logs'], | ||
@@ -30,10 +36,11 @@ alias: { | ||
n: 'not-found', | ||
v: 'version' | ||
}, | ||
default: { | ||
port: 3000 | ||
v: 'version', | ||
r: 'record', | ||
d: 'depth', | ||
a: 'save-headers', | ||
g: 'ignore' | ||
} | ||
}); | ||
const app = createServer({ | ||
basePath: args[0], | ||
basePath: options._[0], | ||
port: options.port, | ||
@@ -43,3 +50,7 @@ host: options.host, | ||
notFound: options['not-found'], | ||
logs: options.logs | ||
ignore: options.ignore, | ||
logs: options.logs, | ||
record: options.record, | ||
depth: options.depth, | ||
saveHeaders: options['save-headers'] | ||
}); | ||
@@ -46,0 +57,0 @@ |
@@ -19,2 +19,6 @@ const path = require('path'); | ||
details.body = response.body === undefined ? null : response.body; | ||
if (response.buffer && details.body !== null) { | ||
details.body = Buffer.from(details.body, 'base64'); | ||
} | ||
} else { | ||
@@ -21,0 +25,0 @@ details.body = response; |
{ | ||
"name": "smoke", | ||
"version": "1.0.0", | ||
"description": "Simple yet powerful file-based mock server", | ||
"version": "1.1.0", | ||
"description": "Simple yet powerful file-based mock server with recording abilities", | ||
"main": "smoke.js", | ||
@@ -31,2 +31,3 @@ "bin": { | ||
"express": "^4.16.4", | ||
"express-http-proxy": "^1.5.0", | ||
"fs-extra": "^7.0.1", | ||
@@ -36,2 +37,3 @@ "globby": "^8.0.1", | ||
"lodash.template": "^4.4.0", | ||
"mime-types": "^2.1.21", | ||
"minimist": "^1.2.0", | ||
@@ -38,0 +40,0 @@ "morgan": "^1.9.1", |
@@ -9,3 +9,3 @@ # :dash: smoke | ||
> Simple yet powerful file-based mock server | ||
> Simple yet powerful file-based mock server with recording abilities | ||
@@ -37,2 +37,3 @@ ![demo](https://user-images.githubusercontent.com/593151/49312821-9f2cc680-f4e5-11e8-900a-117120c38422.gif) | ||
- Customize headers and status code if needed, automatically detect content-type if not specified | ||
- Generate mocks quickly by recording from an existing server | ||
@@ -53,3 +54,3 @@ ## Installation | ||
Options: | ||
Base options: | ||
-p, --port <num> Server port [default: 3000] | ||
@@ -59,5 +60,11 @@ -h, --host <host> Server host [default: "localhost"] | ||
-n, --not-found <glob> Mocks for 404 errors [default: "404.*"] | ||
-g, --ignore <glob> Files to ignore [default: none] | ||
-l, --logs Enable server logs | ||
-v, --version Show version | ||
--help Show help | ||
Mock recording: | ||
-r, --record <host> Proxy & record requests if no mock found | ||
-d, --depth <N> Folder depth for mocks [default: 1] | ||
-a, --save-headers Save response headers | ||
``` | ||
@@ -182,2 +189,15 @@ | ||
You can also use non-string content type if you encode the content as a base64 string in the `body` property and add | ||
the property `"buffer": true` to the mock: | ||
```js | ||
{ | ||
"statusCode": 200, | ||
"body": "U21va2Ugcm9ja3Mh", | ||
"buffer": true, | ||
"headers": { | ||
"Content-Type": "application/octet-stream" | ||
} | ||
} | ||
``` | ||
### Mock formats | ||
@@ -219,5 +239,18 @@ | ||
### Mock recording | ||
To quickly create a mock set of an existing server (to allow working offline for example), you can use the | ||
`--record <host>` option. This will proxy every request for which a mock does not exist to the specified host, and | ||
record the resulting response as a mock file. | ||
You can change the maximum folder depth for mock files created this way using the `--depth` option. | ||
The recorded mock set can also be changed using the `--set` option. | ||
Note that by default response headers are not saved and simple mocks are generated. To change this behavior, you can | ||
enable the `--save-headers` option. | ||
## Other mock servers | ||
If you cannot find what you need here, you might want to check out one of these other mock servers: | ||
If you cannot find what you need here, you might want to check out one of these other NodeJS mock servers: | ||
@@ -224,0 +257,0 @@ - [JSON Server](https://github.com/typicode/json-server) |
25
smoke.js
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const multer = require('multer'); | ||
const proxy = require('express-http-proxy'); | ||
const {getMocks} = require('./lib/mock'); | ||
const {respondMock} = require('./lib/response'); | ||
const {record} = require('./lib/recorder'); | ||
@@ -16,3 +18,7 @@ function createServer(options) { | ||
notFound: options.notFound || '404.*', | ||
logs: options.logs || false | ||
ignore: ['!' + options.ignore] || [], | ||
logs: options.logs || false, | ||
record: options.record || null, | ||
depth: typeof options.depth === 'number' ? options.depth : 1, | ||
saveHeaders: options.saveHeaders || false | ||
}; | ||
@@ -33,3 +39,3 @@ | ||
return app.all('*', async (req, res) => { | ||
return app.all('*', async (req, res, next) => { | ||
const {query, headers, body, files} = req; | ||
@@ -39,3 +45,3 @@ const reqPath = req.path.substring(1); | ||
const data = {method, query, params: {}, headers, body, files}; | ||
const mocks = await getMocks(options.basePath, ['**/*', `!${options.notFound}`]); | ||
const mocks = await getMocks(options.basePath, ['**/*', `!${options.notFound}`].concat(options.ignore)); | ||
const matches = mocks.reduce((allMatches, mock) => { | ||
@@ -57,4 +63,15 @@ const match = reqPath.match(mock.regexp); | ||
if (matches.length === 0) { | ||
if (options.record) { | ||
console.info(`No mock found for ${req.path}, proxying request to ${options.record}`); | ||
return proxy(options.record, { | ||
limit: '10mb', | ||
userResDecorator: async (proxyRes, proxyResData, userReq) => { | ||
await record(userReq, proxyRes, proxyResData, options); | ||
return proxyResData; | ||
} | ||
})(req, res, next); | ||
} | ||
// Search for 404 mocks, matching accept header | ||
const notFoundMocks = await getMocks(options.basePath, [options.notFound]); | ||
const notFoundMocks = await getMocks(options.basePath, [options.notFound].concat(options.ignore)); | ||
const types = notFoundMocks.length > 0 ? notFoundMocks.map(mock => mock.type) : null; | ||
@@ -61,0 +78,0 @@ const accept = types && req.accepts(types); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
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
24337
12
364
256
0
12
3
+ Addedexpress-http-proxy@^1.5.0
+ Addedmime-types@^2.1.21
+ Addeddebug@3.2.7(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedexpress-http-proxy@1.6.3(transitive)