Skip to content

Commit 61f8be3

Browse files
authored
feat: re-write @koa/send with typescript (#147)
* chore: sync history.md * feat: re-write with ts * setup: setup build and linter * feat: improve code structure * feat: add github actions over travis ci * chore: fix linter * fix: fix typo any in the code
1 parent 527048b commit 61f8be3

File tree

21 files changed

+1271
-1136
lines changed

21 files changed

+1271
-1136
lines changed

.eslintrc

Lines changed: 0 additions & 2 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Continuous Integration
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
ci:
9+
runs-on: ubuntu-latest
10+
strategy:
11+
matrix:
12+
node-version:
13+
- 16
14+
- 18
15+
- 20
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v3
20+
- name: Install dependencies
21+
run: yarn install
22+
- name: Check linter
23+
run: yarn lint
24+
- name: Run tests
25+
run: yarn test-ci

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package-lock.json
1414
npm-debug.log
1515
yarn-debug.log
1616
yarn-error.log
17+
yarn.lock
1718

1819

1920
# NYC #

.npmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.xo-config.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"prettier": true,
3+
"space": true,
4+
"extends": ["xo-lass"],
5+
"rules": {
6+
"no-await-in-loop": "off",
7+
"node/no-deprecated-api": "off",
8+
"no-unused-vars": "off",
9+
"no-prototype-builtins": "off",
10+
"prefer-rest-params": "off",
11+
"n/prefer-global/process": "off",
12+
"unicorn/prefer-math-trunc": "off",
13+
"no-bitwise": "off",
14+
"@typescript-eslint/restrict-template-expressions": "off",
15+
"@typescript-eslint/naming-convention": "off",
16+
"@typescript-eslint/prefer-nullish-coalescing": "off",
17+
"@typescript-eslint/no-unsafe-assignment": "off",
18+
"@typescript-eslint/no-unsafe-call": "off",
19+
"@typescript-eslint/no-unsafe-argument": "off",
20+
"@typescript-eslint/no-throw-literal": "off",
21+
"unicorn/no-array-reduce": "off"
22+
},
23+
"ignores": ["test/**", "examples/**"]
24+
}

History.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
5.0.1 / 2020-07-08
2+
==================
3+
4+
* Replace mz with custom promisified wrappers (#140)
5+
* fix(jsdoc): send return type (#141)
6+
* fix extensions options on windows machines (#79) (#122)
7+
* Revert "Using RegExp replace loop in isPathHidden (#109)" (#138)
8+
* Using RegExp replace loop in isPathHidden (#109)
9+
* Clean up / Pipeline update ... (#135)
10+
* Update .travis.yml
11+
* Fix Tests & coverage (#132)
12+
* Fix license (#134)
13+
* travis: add node@11
114

215
5.0.0 / 2018-06-19
316
==================

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2020 Koa contributors
3+
Copyright (c) 2020 Koa.js contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

README.md

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,101 @@
1-
# [**koa-send**](https://github.com/koajs/send)
1+
# [**@koa/send**](https://github.com/koajs/send)
22

3-
> Static file serving middleware.
43

54
[![NPM version][npm-image]][npm-url]
6-
[![Build status][travis-image]][travis-url]
5+
![Build Status][github-action-image]
76
[![Test coverage][coveralls-image]][coveralls-url]
8-
[![Dependency Status][david-image]][david-url]
97
[![License][license-image]][license-url]
8+
[![Node Version][node-image]][node-url]
109
[![Downloads][downloads-image]][downloads-url]
1110

11+
[npm-image]: https://img.shields.io/npm/v/@koa/send.svg?style=flat-square
12+
[npm-url]: https://npmjs.org/package/@koa/send
13+
[github-action-image]: https://github.com/koajs/send/actions/workflows/ci.yml/badge.svg?style=flat-square
14+
[coveralls-image]: https://img.shields.io/coveralls/koajs/send.svg?style=flat-square
15+
[coveralls-url]: https://coveralls.io/r/koajs/send?branch=master
16+
[license-image]: http://img.shields.io/npm/l/koa-send.svg?style=flat-square
17+
[license-url]: LICENSE
18+
[downloads-image]: http://img.shields.io/npm/dm/koa-send.svg?style=flat-square
19+
[downloads-url]: https://npmjs.org/package/koa-send
20+
21+
Koa static file serving middleware.
22+
23+
## Install
1224

13-
## Installation
25+
[![NPM](https://nodei.co/npm/@koa/send.png?downloads=true)](https://nodei.co/npm/@koa/send)
1426

1527
```js
16-
$ npm install koa-send
28+
$ npm i @koa/send
1729
```
1830

19-
2031
## Options
2132

22-
- `maxage` Browser cache max-age in milliseconds. (defaults to `0`).
23-
- `immutable` Tell the browser the resource is immutable and can be cached indefinitely. (defaults to `false`).
24-
- `hidden` Allow transfer of hidden files. (defaults to `false`).
25-
- [`root`](#root-path) Root directory to restrict file access.
26-
- `index` Name of the index file to serve automatically when visiting the root location. (defaults to none).
27-
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. (defaults to `true`).
28-
- `brotli` Try to serve the brotli version of a file automatically when `brotli` is supported by a client and if the requested file with `.br` extension exists. (defaults to `true`).
29-
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`.
30-
- [`setHeaders`](#setheaders) Function to set custom headers on response.
31-
- `extensions` Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults to `false`)
33+
- `maxage` Browser cache max-age in milliseconds. (defaults to `0`).
34+
- `immutable` Tell the browser the resource is immutable and can be cached indefinitely. (defaults to `false`).
35+
- `hidden` Allow transfer of hidden files. (defaults to `false`).
36+
- [`root`](#root-path) Root directory to restrict file access.
37+
- `index` Name of the index file to serve automatically when visiting the root location. (defaults to none).
38+
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. (defaults to `true`).
39+
- `brotli` Try to serve the brotli version of a file automatically when `brotli` is supported by a client and if the requested file with `.br` extension exists. (defaults to `true`).
40+
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/`.
41+
- [`setHeaders`](#setheaders) Function to set custom headers on response.
42+
- `extensions` Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults to `false`)
3243

3344
### Root path
3445

35-
Note that `root` is required, defaults to `''` and will be resolved,
36-
removing the leading `/` to make the path relative and this
37-
path must not contain "..", protecting developers from
38-
concatenating user input. If you plan on serving files based on
39-
user input supply a `root` directory from which to serve from.
46+
Note that `root` is required, defaults to `''` and will be resolved,
47+
removing the leading `/` to make the path relative and this
48+
path must not contain "..", protecting developers from
49+
concatenating user input. If you plan on serving files based on
50+
user input supply a `root` directory from which to serve from.
4051

41-
For example to serve files from `./public`:
52+
For example to serve files from `./public`:
4253

4354
```js
4455
app.use(async (ctx) => {
45-
await send(ctx, ctx.path, { root: __dirname + '/public' });
46-
})
56+
await send(ctx, ctx.path, { root: __dirname + "/public" });
57+
});
4758
```
4859

49-
To serve developer specified files:
60+
To serve developer specified files:
5061

5162
```js
5263
app.use(async (ctx) => {
53-
await send(ctx, 'path/to/my.js');
54-
})
64+
await send(ctx, "path/to/my.js");
65+
});
5566
```
5667

5768
### setHeaders
5869

5970
The function is called as `fn(res, path, stats)`, where the arguments are:
60-
* `res`: the response object.
61-
* `path`: the resolved file path that is being sent.
62-
* `stats`: the stats object of the file that is being sent.
71+
72+
- `res`: the response object.
73+
- `path`: the resolved file path that is being sent.
74+
- `stats`: the stats object of the file that is being sent.
6375

6476
You should only use the `setHeaders` option when you wish to edit the `Cache-Control` or `Last-Modified` headers, because doing it before is useless (it's overwritten by `send`), and doing it after is too late because the headers are already sent.
6577

6678
If you want to edit any other header, simply set them before calling `send`.
6779

68-
6980
## Example
7081

7182
```js
72-
const send = require('koa-send');
73-
const Koa = require('koa');
83+
const send = require("koa-send");
84+
const Koa = require("koa");
7485
const app = new Koa();
7586

7687
// $ GET /package.json
7788
// $ GET /
7889

7990
app.use(async (ctx) => {
80-
if ('/' == ctx.path) return ctx.body = 'Try GET /package.json';
91+
if ("/" == ctx.path) return (ctx.body = "Try GET /package.json");
8192
await send(ctx, ctx.path);
82-
})
93+
});
8394

8495
app.listen(3000);
85-
console.log('listening on port 3000');
96+
console.log("listening on port 3000");
8697
```
8798

88-
8999
## License
90100

91101
[MIT](/LICENSE)
92-
93-
94-
[npm-image]: https://img.shields.io/npm/v/koa-send.svg?style=flat-square
95-
[npm-url]: https://npmjs.org/package/koa-send
96-
[github-tag]: http://img.shields.io/github/tag/koajs/send.svg?style=flat-square
97-
[github-url]: https://github.com/koajs/send/tags
98-
[travis-image]: https://img.shields.io/travis/koajs/send.svg?style=flat-square
99-
[travis-url]: https://travis-ci.org/koajs/send
100-
[coveralls-image]: https://img.shields.io/coveralls/koajs/send.svg?style=flat-square
101-
[coveralls-url]: https://coveralls.io/r/koajs/send?branch=master
102-
[david-image]: http://img.shields.io/david/koajs/send.svg?style=flat-square
103-
[david-url]: https://david-dm.org/koajs/send
104-
[license-image]: http://img.shields.io/npm/l/koa-send.svg?style=flat-square
105-
[license-url]: LICENSE
106-
[downloads-image]: http://img.shields.io/npm/dm/koa-send.svg?style=flat-square
107-
[downloads-url]: https://npmjs.org/package/koa-send
108-
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
109-
[gittip-url]: https://www.gittip.com/jonathanong/

0 commit comments

Comments
 (0)