Skip to content

Commit

Permalink
Weekly fixes for Prism (#1107)
Browse files Browse the repository at this point in the history
* fix: handle listening errors correctly

* server is an event emitter

* fix: correclty catch messages

* docs: changelog

* feat: remove PINO warning

* fix: rethrow exception
  • Loading branch information
XVincentX authored Apr 27, 2020
1 parent 2094fe3 commit 3401aa3
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 24 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

# Unreleased

## Fixed

- Correctly catch some exceptions and propagate them to the CLI [#1107](https://github.com/stoplightio/prism/pull/1107)

# 3.3.3 (2020-04-02)

## Fixed

- All the dependencies used by the various Prism packages have been explicitily declared avoiding some resolutions problems in case you are using Prism programmatically [#1072](https://github.com/stoplightio/prism/pull/1072)
- Prism's current options aren't overriden internally anymore because of the `Prefer` header set [#1074](https://github.com/stoplightio/prism/pull/1074)


# 3.3.2 (2020-03-16)

## Fixed

- Prism will not correctly consider that HTTP Security Schemes are case insensitive [#1044](https://github.com/stoplightio/prism/pull/1044)


# 3.3.1 (2020-03-13)

## Fixed
Expand Down
5 changes: 3 additions & 2 deletions docs/getting-started/01-installation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Installation

For many, the easiest way to install Prism is as a node module.
For many, the easiest way to install Prism is as a node module.

```bash
npm install -g @stoplight/prism-cli
Expand All @@ -17,6 +17,7 @@ curl -L https://raw.githack.com/stoplightio/prism/master/install | sh
```

<!-- theme: info -->

> The binaries do _not_ auto-update, so you will need to run it again to install new versions.
## Docker
Expand All @@ -37,6 +38,6 @@ If you want to start the proxy server, you can run a command like this:

```bash
docker run --init --rm -it -d --name myprism -p 4010:4010 -v $(pwd):/tmp -P stoplight/prism:3 proxy -h 0.0.0.0 "/tmp/file.yml" http://host.docker.internal:8080 --errors
```
```

Now everything is installed, let's look at some of the [concepts](./02-concepts.md).
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"chokidar": "^3.2.1",
"fp-ts": "^2.5.3",
"lodash": "^4.17.15",
"pino": "^6.2.0",
"pino": "^6.2.1",
"signale": "^1.4.0",
"split2": "^3.1.1",
"tslib": "^1.10.0",
Expand All @@ -39,7 +39,7 @@
"url": "https://github.com/stoplightio/prism.git"
},
"scripts": {
"cli": "node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts src/index.ts",
"cli": "node -r ts-node/register/transpile-only -r tsconfig-paths/register src/index.ts",
"cli:debug": "node -r ts-node/register/transpile-only -r tsconfig-paths/register --inspect-brk src/index.ts"
},
"types": "dist/index.d.ts"
Expand Down
21 changes: 11 additions & 10 deletions packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ signale.config({ displayTimestamp: true });

const cliSpecificLoggerOptions: LoggerOptions = {
customLevels: { start: 11 },
useLevelLabels: true,
level: 'start',
formatters: {
level: level => ({ level }),
},
};

const createMultiProcessPrism: CreatePrism = async options => {
Expand All @@ -37,27 +39,26 @@ const createMultiProcessPrism: CreatePrism = async options => {
return;
} else {
const logInstance = createLogger('CLI', cliSpecificLoggerOptions);
try {
return createPrismServerWithLogger(options, logInstance);
} catch (e) {

return createPrismServerWithLogger(options, logInstance).catch(e => {
logInstance.fatal(e.message);
cluster.worker.kill();
}
throw e;
});
}
};

const createSingleProcessPrism: CreatePrism = async options => {
const createSingleProcessPrism: CreatePrism = options => {
signale.await({ prefix: chalk.bgWhiteBright.black('[CLI]'), message: 'Starting Prism…' });

const logStream = new PassThrough();
const logInstance = createLogger('CLI', cliSpecificLoggerOptions, logStream);
pipeOutputToSignale(logStream);

try {
return createPrismServerWithLogger(options, logInstance);
} catch (e) {
return createPrismServerWithLogger(options, logInstance).catch(e => {
logInstance.fatal(e.message);
}
throw e;
});
};

async function createPrismServerWithLogger(options: CreateBaseServerOptions, logInstance: Logger) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/util/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { CreateMockServerOptions } from './createServer';
export type CreatePrism = (options: CreateMockServerOptions) => Promise<IPrismHttpServer | void>;

export function runPrismAndSetupWatcher(createPrism: CreatePrism, options: CreateMockServerOptions) {
return createPrism(options).then(possiblyServer => {
if (possiblyServer) {
let server: IPrismHttpServer = possiblyServer;
return createPrism(options).then(possibleServer => {
if (possibleServer) {
let server: IPrismHttpServer = possibleServer;

const watcher = chokidar.watch(options.document, {
// See https://github.com/paulmillr/chokidar#persistence
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"fp-ts": "^2.5.3",
"lodash": "^4.17.15",
"pino": "^6.2.0",
"pino": "^6.2.1",
"tslib": "^1.10.0"
},
"publishConfig": {
Expand Down
8 changes: 7 additions & 1 deletion packages/http-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ export const createServer = (operations: IHttpOperation[], opts: IPrismHttpServe
},

listen: (port: number, ...args: any[]) =>
new Promise(resolve => server.listen(port, ...args, () => resolve(addressInfoToString(server.address())))),
new Promise((resolve, reject) => {
server.once('error', e => reject(e.message));
server.listen(port, ...args, (err: unknown) => {
if (err) return reject(err);
return resolve(addressInfoToString(server.address()));
});
}),
};
};

Expand Down
2 changes: 1 addition & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"lodash": "^4.17.15",
"node-fetch": "^2.6.0",
"openapi-sampler": "^1.0.0-beta.15",
"pino": "^6.2.0",
"pino": "^6.2.1",
"tslib": "^1.10.0",
"type-is": "^1.6.18",
"uri-template-lite": "^19.4.0"
Expand Down
4 changes: 3 additions & 1 deletion packages/http/src/getHttpOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const httpAndFileResolver = new Resolver({

export async function getHttpOperationsFromResource(file: string): Promise<IHttpOperation[]> {
const isRemote = /^https?:\/\//i.test(file);
const fileContent = isRemote ? await fetch(file).then(d => d.text()) : fs.readFileSync(file, { encoding: 'utf8' });
const fileContent = await (isRemote
? fetch(file).then(d => d.text())
: fs.promises.readFile(file, { encoding: 'utf8' }));

return getHttpOperations(fileContent, isRemote ? file : resolve(file));
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7131,7 +7131,7 @@ pino-std-serializers@^2.4.2:
resolved "https://registry.yarnpkg.com/pino-std-serializers/-/pino-std-serializers-2.4.2.tgz#cb5e3e58c358b26f88969d7e619ae54bdfcc1ae1"
integrity sha512-WaL504dO8eGs+vrK+j4BuQQq6GLKeCCcHaMB2ItygzVURcL1CycwNEUHTD/lHFHs/NL5qAz2UKrjYWXKSf4aMQ==

pino@^6.2.0:
pino@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/pino/-/pino-6.2.1.tgz#d2b86306b3998e8f6bb33bdf23910d418ed696cf"
integrity sha512-5F5A+G25Ex2rMOBEe3XYGyLSF4dikQZsFvPojwsqnDBX+rfg7+kw9s5i7pHuVAJImekjwb+MR9jQyHWPLENlvQ==
Expand Down

0 comments on commit 3401aa3

Please sign in to comment.