Socket
Socket
Sign inDemoInstall

micro

Package Overview
Dependencies
Maintainers
3
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro - npm Package Compare versions

Comparing version 9.3.5-canary.3 to 9.4.0

9

bin/micro.js

@@ -207,5 +207,8 @@ #!/usr/bin/env node

const details = server.address();
registerShutdown(() => {
console.log('micro: Gracefully shutting down. Please wait...');
server.close();
process.exit();
});
registerShutdown(() => server.close());
// `micro` is designed to run only in production, so

@@ -229,6 +232,4 @@ // this message is perfectly for prod

}
registerShutdown(() => console.log('micro: Gracefully shutting down. Please wait...'));
}
start();

@@ -117,3 +117,2 @@ // Native

// will be called later
// eslint-disable-next-line no-undefined
if (val !== undefined) {

@@ -142,3 +141,2 @@ send(res, res.statusCode || 200, val);

// eslint-disable-next-line no-undefined
if (encoding === undefined) {

@@ -145,0 +143,0 @@ encoding = contentType.parse(type).parameters.charset;

{
"name": "micro",
"version": "9.3.5-canary.3",
"version": "9.4.0",
"description": "Asynchronous HTTP microservices",

@@ -19,3 +19,3 @@ "license": "MIT",

},
"repository": "zeit/micro",
"repository": "vercel/micro",
"keywords": [

@@ -32,4 +32,3 @@ "micro",

"raw-body": "2.4.1"
},
"gitHead": "5d4b6748fa6e005579423a1d12cc838340117bc4"
}
}

@@ -1,23 +0,19 @@

<img src="https://raw.githubusercontent.com/zeit/art/6451bc300e00312d970527274f316f9b2c07a27e/micro/logo.png" width="50"/>
# Micro — Asynchronous HTTP microservices
_**Micro** — Asynchronous HTTP microservices_
## Features
[![CircleCI](https://circleci.com/gh/zeit/micro/tree/master.svg?style=shield)](https://circleci.com/gh/zeit/micro/tree/master)
[![Install Size](https://packagephobia.now.sh/badge?p=micro)](https://packagephobia.now.sh/result?p=micro)
[![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/zeit)
- **Easy**: Designed for usage with `async` and `await`
- **Fast**: Ultra-high performance (even JSON parsing is opt-in)
- **Micro**: The whole project is ~260 lines of code
- **Agile**: Super easy deployment and containerization
- **Simple**: Oriented for single purpose modules (function)
- **Standard**: Just HTTP!
- **Explicit**: No middleware - modules declare all [dependencies](https://github.com/amio/awesome-micro)
- **Lightweight**: With all dependencies, the package weighs less than a megabyte
## Features
**Disclaimer:** Micro was created for use within containers and is not intended for use in serverless environments. For those using Vercel, this means that there is no requirement to use Micro in your projects as the benefits it provides are not applicable to the platform. Utility features provided by Micro, such as `json`, are readily available in the form of [Serverless Function helpers](https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-request-and-response-objects).
* **Easy**: Designed for usage with `async` and `await` ([more](https://zeit.co/blog/async-and-await))
* **Fast**: Ultra-high performance (even JSON parsing is opt-in)
* **Micro**: The whole project is ~260 lines of code
* **Agile**: Super easy deployment and containerization
* **Simple**: Oriented for single purpose modules (function)
* **Standard**: Just HTTP!
* **Explicit**: No middleware - modules declare all [dependencies](https://github.com/amio/awesome-micro)
* **Lightweight**: With all dependencies, the package weighs less than a megabyte
## Installation
**Important:** Micro is only meant to be used in production. In development, you should use [micro-dev](https://github.com/zeit/micro-dev), which provides you with a tool belt specifically tailored for developing microservices.
**Important:** Micro is only meant to be used in production. In development, you should use [micro-dev](https://github.com/vercel/micro-dev), which provides you with a tool belt specifically tailored for developing microservices.

@@ -36,10 +32,10 @@ To prepare your microservice for running in the production environment, firstly install `micro`:

module.exports = (req, res) => {
res.end('Welcome to Micro')
}
res.end('Welcome to Micro');
};
```
Micro provides [useful helpers](https://github.com/zeit/micro#body-parsing) but also handles return values – so you can write it even shorter!
Micro provides [useful helpers](https://github.com/vercel/micro#body-parsing) but also handles return values – so you can write it even shorter!
```js
module.exports = () => 'Welcome to Micro'
module.exports = () => 'Welcome to Micro';
```

@@ -117,11 +113,11 @@

Micro is built for usage with async/await. You can read more about async / await [here](https://zeit.co/blog/async-and-await)
Micro is built for usage with async/await.
```js
const sleep = require('then-sleep')
const sleep = require('then-sleep');
module.exports = async (req, res) => {
await sleep(500)
return 'Ready!'
}
await sleep(500);
return 'Ready!';
};
```

@@ -160,16 +156,16 @@

```js
const {buffer, text, json} = require('micro')
const { buffer, text, json } = require('micro');
module.exports = async (req, res) => {
const buf = await buffer(req)
console.log(buf)
const buf = await buffer(req);
console.log(buf);
// <Buffer 7b 22 70 72 69 63 65 22 3a 20 39 2e 39 39 7d>
const txt = await text(req)
console.log(txt)
const txt = await text(req);
console.log(txt);
// '{"price": 9.99}'
const js = await json(req)
console.log(js.price)
const js = await json(req);
console.log(js.price);
// 9.99
return ''
}
return '';
};
```

@@ -180,7 +176,9 @@

##### `buffer(req, { limit = '1mb', encoding = 'utf8' })`
##### `text(req, { limit = '1mb', encoding = 'utf8' })`
##### `json(req, { limit = '1mb', encoding = 'utf8' })`
- Buffers and parses the incoming body and returns it.
- Exposes an `async` function that can be run with `await`.
- Exposes an `async` function that can be run with `await`.
- Can be called multiple times, as it caches the raw request body the first time.

@@ -197,10 +195,10 @@ - `limit` is how much data is aggregated before parsing at max. Otherwise, an `Error` is thrown with `statusCode` set to `413` (see [Error Handling](#error-handling)). It can be a `Number` of bytes or [a string](https://www.npmjs.com/package/bytes) like `'1mb'`.

```js
const {send} = require('micro')
const { send } = require('micro');
module.exports = async (req, res) => {
const statusCode = 400
const data = { error: 'Custom error message' }
const statusCode = 400;
const data = { error: 'Custom error message' };
send(res, statusCode, data)
}
send(res, statusCode, data);
};
```

@@ -224,12 +222,14 @@

```js
const http = require('http')
const micro = require('micro')
const sleep = require('then-sleep')
const http = require('http');
const micro = require('micro');
const sleep = require('then-sleep');
const server = new http.Server(micro(async (req, res) => {
await sleep(500)
return 'Hello world'
}))
const server = new http.Server(
micro(async (req, res) => {
await sleep(500);
return 'Hello world';
})
);
server.listen(3000)
server.listen(3000);
```

@@ -269,15 +269,15 @@

```js
const rateLimit = require('my-rate-limit')
const rateLimit = require('my-rate-limit');
module.exports = async (req, res) => {
await rateLimit(req)
await rateLimit(req);
// ... your code
}
};
```
If the API endpoint is abused, it can throw an error with ``createError`` like so:
If the API endpoint is abused, it can throw an error with `createError` like so:
```js
if (tooMany) {
throw createError(429, 'Rate limit exceeded')
throw createError(429, 'Rate limit exceeded');
}

@@ -290,5 +290,5 @@ ```

if (tooMany) {
const err = new Error('Rate limit exceeded')
err.statusCode = 429
throw err
const err = new Error('Rate limit exceeded');
err.statusCode = 429;
throw err;
}

@@ -301,7 +301,7 @@ ```

try {
await rateLimit(req)
await rateLimit(req);
} catch (err) {
if (429 == err.statusCode) {
// perhaps send 500 instead?
send(res, 500)
send(res, 500);
}

@@ -316,16 +316,16 @@ }

```js
const {send} = require('micro')
const { send } = require('micro');
const handleErrors = fn => async (req, res) => {
const handleErrors = (fn) => async (req, res) => {
try {
return await fn(req, res)
return await fn(req, res);
} catch (err) {
console.log(err.stack)
send(res, 500, 'My custom error!')
console.log(err.stack);
send(res, 500, 'My custom error!');
}
}
};
module.exports = handleErrors(async (req, res) => {
throw new Error('What happened here?')
})
throw new Error('What happened here?');
});
```

@@ -339,24 +339,27 @@

```js
const http = require('http')
const micro = require('micro')
const test = require('ava')
const listen = require('test-listen')
const request = require('request-promise')
const http = require('http');
const micro = require('micro');
const test = require('ava');
const listen = require('test-listen');
const fetch = require('node-fetch');
test('my endpoint', async t => {
const service = new http.Server(micro(async (req, res) => {
micro.send(res, 200, {
test: 'woot'
test('my endpoint', async (t) => {
const service = new http.Server(
micro(async (req, res) => {
micro.send(res, 200, {
test: 'woot',
});
})
}))
);
const url = await listen(service)
const body = await request(url)
const url = await listen(service);
const response = await fetch(url);
const body = await response.json();
t.deepEqual(JSON.parse(body).test, 'woot')
service.close()
})
t.deepEqual(body.test, 'woot');
service.close();
});
```
Look at [test-listen](https://github.com/zeit/test-listen) for a
Look at [test-listen](https://github.com/vercel/test-listen) for a
function that returns a URL with an ephemeral port every time it's called.

@@ -370,3 +373,3 @@

As always, you can run the [AVA](https://github.com/sindresorhus/ava) and [ESLint](http://eslint.org) tests using: `npm test`
You can run the [AVA](https://github.com/sindresorhus/ava) tests using: `npm test`

@@ -379,4 +382,4 @@ ## Credits

- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [ZEIT](https://zeit.co)
- Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [ZEIT](https://zeit.co)
- Tim Neutkens ([@timneutkens](https://twitter.com/timneutkens)) - [ZEIT](https://zeit.co)
- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [Vercel](https://vercel.com)
- Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [Vercel](https://vercel.com)
- Tim Neutkens ([@timneutkens](https://twitter.com/timneutkens)) - [Vercel](https://vercel.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc