Skip to content

Commit

Permalink
feat: send return promise & tests/doc refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashenry committed Jan 21, 2021
1 parent 125430b commit db1e287
Show file tree
Hide file tree
Showing 45 changed files with 3,713 additions and 3,167 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2.3.0
==================

* Feature: send method is now returning a promise and is not anymore an event emitter
* Feature: send method have a new option to ignore premature close errors (true by default)
* Test: test all frameworks instead of only koa
* Docs: update/fix examples

2.2.0
==================

Expand Down
80 changes: 41 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,6 @@ Serve all files from a directory (also serve index.html from directories on trai

See [examples](#examples) for more advanced usages.

Using Express (v4.x.x):

```js
const path = require("path");
const express = require("express");
const { FileSystemStorage } = require('send-stream');

const app = express();
const storage = new FileSystemStorage(path.join(__dirname, 'assets'), { onDirectory: 'serve-index' });

app.get('*', async (req, res, next) => {
try {
let result = await storage.prepareResponse(req.url, req);
if (result.statusCode === 404) {
next(); // let express call next handlers
return;
}
result.send(res);
} catch (err) {
next(err);
}
});
app.listen(3000, () => {
console.info('listening on http://localhost:3000');
});
```

Using Fastify (v3.x.x):

```js
Expand All @@ -72,10 +45,12 @@ app.route({
handler: async (request, reply) => {
const result = await storage.prepareResponse(request.url, request.raw);
if (result.statusCode === 404) {
reply.callNotFound(); // let fastify call next handlers
reply.callNotFound(); // let fastify handle 404
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand All @@ -98,11 +73,11 @@ const storage = new FileSystemStorage(path.join(__dirname, 'assets'), { onDirect
app.use(async (ctx, next) => {
let result = await storage.prepareResponse(ctx.request.path, ctx.req);
if (result.statusCode === 404) {
await next(); // let koa call next handlers
await next(); // let koa handle 404
return;
}
ctx.response.status = result.statusCode;
ctx.response.set(result.headers);
ctx.status = result.statusCode;
ctx.set(result.headers);
ctx.body = result.stream;
});

Expand All @@ -111,6 +86,33 @@ app.listen(3000, () => {
});
```

Using Express (v4.x.x):

```js
const path = require("path");
const express = require("express");
const { FileSystemStorage } = require('send-stream');

const app = express();
const storage = new FileSystemStorage(path.join(__dirname, 'assets'), { onDirectory: 'serve-index' });

app.get('*', async (req, res, next) => {
try {
let result = await storage.prepareResponse(req.url, req);
if (result.statusCode === 404) {
next(); // let express handle 404
return;
}
await result.send(res);
} catch (err) {
next(err);
}
});
app.listen(3000, () => {
console.info('listening on http://localhost:3000');
});
```

## API

### `new FileSystemStorage(root, [options])`
Expand Down Expand Up @@ -630,7 +632,7 @@ const storage = new FileSystemStorage(directory);
...

let result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
```

### Serve files with directory index.html
Expand All @@ -641,7 +643,7 @@ const storage = new FileSystemStorage(directory, { onDirectory: 'serve-index' })
...

let result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
```

### Serve files with directory listing
Expand All @@ -652,7 +654,7 @@ const storage = new FileSystemStorage(directory, { onDirectory: 'list-files' });
...

let result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
```

### Serve files and add CORS headers
Expand All @@ -665,7 +667,7 @@ const storage = new FileSystemStorage(directory);
let result = await storage.prepareResponse(req.url, req);
result.headers['Access-Control-Allow-Origin'] = '*';
result.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
result.send(res);
await result.send(res);
```

### Serve one file specifically
Expand All @@ -676,7 +678,7 @@ const storage = new FileSystemStorage(directory);
...

let result = await storage.prepareResponse('/index.html', req);
result.send(res);
await result.send(res);
```

### Serve index.html instead of 404 for history.pushState applications
Expand All @@ -691,7 +693,7 @@ let result = await storage.prepareResponse(req.url, req);
if (result.error instanceof FileSystemStorageError) {
result = await storage.prepareResponse('/index.html', req);
}
result.send(res);
await result.send(res);
```

### Serve files and add CSP (Content-Security-Policy) header when content is html
Expand All @@ -709,7 +711,7 @@ if (result.storageInfo?.mimeType === 'text/html') {
// result.headers['Referrer-Policy'] = "no-referrer";
// result.headers['Feature-Policy'] = "...";
}
result.send(res);
await result.send(res);
```
## License
Expand Down
4 changes: 3 additions & 1 deletion examples/dynamic-compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
4 changes: 3 additions & 1 deletion examples/etag-hash-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
2 changes: 1 addition & 1 deletion examples/express-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ app.get('*', async (req, res, next) => {
next();
return;
}
result.send(res);
await result.send(res);
} catch (err: unknown) {
// eslint-disable-next-line node/callback-return
next(err);
Expand Down
2 changes: 1 addition & 1 deletion examples/express-https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ app.get('*', async (req, res, next) => {
next();
return;
}
result.send(res);
await result.send(res);
} catch (err: unknown) {
// eslint-disable-next-line node/callback-return
next(err);
Expand Down
4 changes: 3 additions & 1 deletion examples/fastify-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
4 changes: 3 additions & 1 deletion examples/fastify-http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
4 changes: 3 additions & 1 deletion examples/fastify-https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
2 changes: 1 addition & 1 deletion examples/http-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const app = http.createServer((req, res) => {
throw new Error('url not set');
}
const result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
})().catch(err => {
console.error(err);
if (res.headersSent) {
Expand Down
2 changes: 1 addition & 1 deletion examples/http2-module-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const options = {
const app = http2.createSecureServer(options, (req, res) => {
(async () => {
const result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
})().catch(err => {
console.error(err);
if (res.headersSent) {
Expand Down
2 changes: 1 addition & 1 deletion examples/http2-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ app.on('stream', (stream, headers) => {
throw new Error('path not set');
}
const result = await storage.prepareResponse(headers[':path'], headers);
result.send(stream);
await result.send(stream);
})().catch(err => {
console.error(err);
if (stream.headersSent) {
Expand Down
2 changes: 1 addition & 1 deletion examples/https-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const app = https.createServer(options, (req, res) => {
throw new Error('url not set');
}
const result = await storage.prepareResponse(req.url, req);
result.send(res);
await result.send(res);
})().catch(err => {
console.error(err);
if (res.headersSent) {
Expand Down
4 changes: 2 additions & 2 deletions examples/koa-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ app.use(async (ctx, next) => {
await next();
return;
}
ctx.response.status = result.statusCode;
ctx.response.set(<Record<string, string>> result.headers);
ctx.status = result.statusCode;
ctx.set(<Record<string, string>> result.headers);
ctx.body = result.stream;
});

Expand Down
4 changes: 2 additions & 2 deletions examples/koa-http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ app.use(async (ctx, next) => {
await next();
return;
}
ctx.response.status = result.statusCode;
ctx.response.set(<Record<string, string>> result.headers);
ctx.status = result.statusCode;
ctx.set(<Record<string, string>> result.headers);
ctx.body = result.stream;
});

Expand Down
4 changes: 2 additions & 2 deletions examples/koa-https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ app.use(async (ctx, next) => {
await next();
return;
}
ctx.response.status = result.statusCode;
ctx.response.set(<Record<string, string>> result.headers);
ctx.status = result.statusCode;
ctx.set(<Record<string, string>> result.headers);
ctx.body = result.stream;
});

Expand Down
4 changes: 3 additions & 1 deletion examples/listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
4 changes: 3 additions & 1 deletion examples/mongodb-gridfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ client.connect(error => {
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
4 changes: 3 additions & 1 deletion examples/pre-compressed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ app.route({
reply.callNotFound();
return;
}
result.send(reply.raw);
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

Expand Down
25 changes: 13 additions & 12 deletions examples/pushstate-server.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@

import { join } from 'path';

import express from 'express';
import { fastify } from 'fastify';

import { FileSystemStorage, FileSystemStorageError } from '../src/send-stream';

const app = express();
const app = fastify();

const storage = new FileSystemStorage(join(__dirname, 'assets'));

app.use(async (req, res, next) => {
try {
let result = await storage.prepareResponse(req.url, req);
app.route({
method: ['HEAD', 'GET'],
url: '*',
handler: async (request, reply) => {
let result = await storage.prepareResponse(request.url, request.raw);
// if path is not found then rewrite to root index.html
if (result.error instanceof FileSystemStorageError) {
result.stream.destroy();
const { error: { pathParts } } = result;
result = await storage.prepareResponse(
['', 'index.html'],
req,
request.raw,
// if the mime type can be determined from path then this is probably an error so add 404 status
storage.mimeTypeDefaultCharset(pathParts[pathParts.length - 1])
storage.mimeTypeLookup(pathParts[pathParts.length - 1])
? { statusCode: 404 }
: {},
);
}
result.send(res);
} catch (err: unknown) {
// eslint-disable-next-line node/callback-return
next(err);
}
await reply.code(result.statusCode)
.headers(result.headers)
.send(result.stream);
},
});

app.listen(3000, () => {
Expand Down
Loading

0 comments on commit db1e287

Please sign in to comment.