koa-files 3.2.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/cjs/ReadStream.cjs +1 -1
- package/cjs/ReadStream.d.cts +84 -0
- package/cjs/Service.cjs +8 -14
- package/cjs/Service.d.cts +62 -0
- package/cjs/index.cjs +6 -5
- package/cjs/index.d.cts +15 -0
- package/cjs/utils/fs.cjs +1 -1
- package/cjs/utils/hash.cjs +1 -1
- package/cjs/utils/http.cjs +2 -2
- package/cjs/utils/path.cjs +1 -1
- package/cjs/utils/type.cjs +24 -0
- package/cjs/utils/type.d.cts +9 -0
- package/{types → esm}/ReadStream.d.ts +2 -2
- package/esm/ReadStream.js +1 -1
- package/{types → esm}/Service.d.ts +5 -5
- package/esm/Service.js +7 -13
- package/esm/index.d.ts +15 -0
- package/esm/index.js +6 -5
- package/esm/utils/fs.d.ts +17 -0
- package/esm/utils/fs.js +1 -1
- package/esm/utils/hash.d.ts +9 -0
- package/esm/utils/hash.js +1 -1
- package/esm/utils/http.d.ts +37 -0
- package/esm/utils/http.js +2 -2
- package/esm/utils/path.d.ts +22 -0
- package/esm/utils/path.js +1 -1
- package/esm/utils/type.d.ts +9 -0
- package/esm/utils/type.js +22 -0
- package/package.json +17 -17
- package/types/index.d.ts +0 -14
- /package/{types/utils/fs.d.ts → cjs/utils/fs.d.cts} +0 -0
- /package/{types/utils/hash.d.ts → cjs/utils/hash.d.cts} +0 -0
- /package/{types/utils/http.d.ts → cjs/utils/http.d.cts} +0 -0
- /package/{types/utils/path.d.ts → cjs/utils/path.d.cts} +0 -0
package/README.md
CHANGED
|
@@ -20,14 +20,14 @@ $ npm install koa-files
|
|
|
20
20
|
import { Middleware } from 'koa';
|
|
21
21
|
import fs, { Stats } from 'node:fs';
|
|
22
22
|
|
|
23
|
-
interface IgnoreFunction {
|
|
24
|
-
(path: string): boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
23
|
interface Headers {
|
|
28
24
|
[key: string]: string | string[];
|
|
29
25
|
}
|
|
30
26
|
|
|
27
|
+
interface IgnoreFunction {
|
|
28
|
+
(path: string): boolean | Promise<boolean>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
31
|
interface HeadersFunction {
|
|
32
32
|
(path: string, stats: Stats): Promise<Headers | void> | Headers | void;
|
|
33
33
|
}
|
|
@@ -50,7 +50,7 @@ export interface Options {
|
|
|
50
50
|
headers?: Headers | HeadersFunction;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
export
|
|
53
|
+
export function server(root: string, options?: Options): Middleware;
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
### root
|
|
@@ -118,14 +118,14 @@ export default function server(root: string, options?: Options): Middleware;
|
|
|
118
118
|
*/
|
|
119
119
|
|
|
120
120
|
import Koa from 'koa';
|
|
121
|
-
import
|
|
121
|
+
import { server } from 'koa-files';
|
|
122
122
|
|
|
123
123
|
const app = new Koa();
|
|
124
124
|
const port = process.env.PORT || 80;
|
|
125
125
|
|
|
126
126
|
// Static files server
|
|
127
127
|
app.use(
|
|
128
|
-
|
|
128
|
+
server('tests', {
|
|
129
129
|
headers: {
|
|
130
130
|
'Cache-Control': 'public, max-age=31557600'
|
|
131
131
|
}
|
package/cjs/ReadStream.cjs
CHANGED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module ReadStream
|
|
3
|
+
*/
|
|
4
|
+
import { PathLike } from 'fs';
|
|
5
|
+
import { Range } from './utils/http.cjs';
|
|
6
|
+
import { FileSystem } from './utils/fs.cjs';
|
|
7
|
+
import { Readable, ReadableOptions } from 'stream';
|
|
8
|
+
export interface Options extends Pick<ReadableOptions, 'highWaterMark'> {
|
|
9
|
+
fs: FileSystem;
|
|
10
|
+
}
|
|
11
|
+
interface Callback {
|
|
12
|
+
(error?: Error | null): void;
|
|
13
|
+
}
|
|
14
|
+
export declare class ReadStream extends Readable {
|
|
15
|
+
private readonly fs;
|
|
16
|
+
private readonly path;
|
|
17
|
+
private readonly ranges;
|
|
18
|
+
private bytesRead;
|
|
19
|
+
private fd;
|
|
20
|
+
private reading;
|
|
21
|
+
private currentRangeIndex;
|
|
22
|
+
private readState;
|
|
23
|
+
/**
|
|
24
|
+
* @constructor
|
|
25
|
+
* @param path The file path.
|
|
26
|
+
* @param ranges The ranges to read.
|
|
27
|
+
* @param options The stream options.
|
|
28
|
+
*/
|
|
29
|
+
constructor(path: PathLike, ranges: Range[], options: Options);
|
|
30
|
+
/**
|
|
31
|
+
* @override
|
|
32
|
+
* @method _construct
|
|
33
|
+
* @param callback The callback.
|
|
34
|
+
*/
|
|
35
|
+
_construct(callback: Callback): void;
|
|
36
|
+
/**
|
|
37
|
+
* @private
|
|
38
|
+
* @method getRange
|
|
39
|
+
*/
|
|
40
|
+
private getRange;
|
|
41
|
+
/**
|
|
42
|
+
* @private
|
|
43
|
+
* @method getPadding
|
|
44
|
+
* @param range The current range.
|
|
45
|
+
*/
|
|
46
|
+
private getPadding;
|
|
47
|
+
/**
|
|
48
|
+
* @private
|
|
49
|
+
* @method readFilePadding
|
|
50
|
+
* @param fd The file descriptor.
|
|
51
|
+
* @param range The current range.
|
|
52
|
+
* @param size The number of bytes to read.
|
|
53
|
+
*/
|
|
54
|
+
private readFilePadding;
|
|
55
|
+
/**
|
|
56
|
+
* @private
|
|
57
|
+
* @method readFileRange
|
|
58
|
+
* @param fd The file descriptor.
|
|
59
|
+
* @param range The current range.
|
|
60
|
+
* @param size The number of bytes to read.
|
|
61
|
+
*/
|
|
62
|
+
private readFileRange;
|
|
63
|
+
/**
|
|
64
|
+
* @override
|
|
65
|
+
* @method _read
|
|
66
|
+
* @param size The number of bytes to read.
|
|
67
|
+
*/
|
|
68
|
+
_read(size: number): void;
|
|
69
|
+
/**
|
|
70
|
+
* @private
|
|
71
|
+
* @method dispose
|
|
72
|
+
* @param error The error.
|
|
73
|
+
* @param callback The callback.
|
|
74
|
+
*/
|
|
75
|
+
private dispose;
|
|
76
|
+
/**
|
|
77
|
+
* @override
|
|
78
|
+
* @method _destroy
|
|
79
|
+
* @param error The error.
|
|
80
|
+
* @param callback The callback.
|
|
81
|
+
*/
|
|
82
|
+
_destroy(error: Error | null, callback: Callback): void;
|
|
83
|
+
}
|
|
84
|
+
export {};
|
package/cjs/Service.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
const createETag = require('etag');
|
|
13
13
|
const fs = require('fs');
|
|
14
14
|
const ReadStream = require('./ReadStream.cjs');
|
|
15
|
+
const type = require('./utils/type.cjs');
|
|
15
16
|
const fs$1 = require('./utils/fs.cjs');
|
|
16
17
|
const path$1 = require('path');
|
|
17
18
|
const path = require('./utils/path.cjs');
|
|
@@ -27,14 +28,6 @@ const fs__default = /*#__PURE__*/ _interopDefault(fs);
|
|
|
27
28
|
/**
|
|
28
29
|
* @module Service
|
|
29
30
|
*/
|
|
30
|
-
/**
|
|
31
|
-
* @function isFunction
|
|
32
|
-
* @description Check if value is function.
|
|
33
|
-
* @param value The value to check.
|
|
34
|
-
*/
|
|
35
|
-
function isFunction(value) {
|
|
36
|
-
return typeof value === 'function';
|
|
37
|
-
}
|
|
38
31
|
/**
|
|
39
32
|
* @class Service
|
|
40
33
|
*/
|
|
@@ -55,9 +48,10 @@ class Service {
|
|
|
55
48
|
* @description Check if path is ignore.
|
|
56
49
|
* @param path The path to check.
|
|
57
50
|
*/
|
|
58
|
-
isIgnore(path) {
|
|
51
|
+
async isIgnore(path) {
|
|
59
52
|
const { ignore } = this.options;
|
|
60
|
-
|
|
53
|
+
const isIgnored = type.isFunction(ignore) ? await ignore(path) : false;
|
|
54
|
+
return isIgnored === true;
|
|
61
55
|
}
|
|
62
56
|
/**
|
|
63
57
|
* @private
|
|
@@ -76,7 +70,7 @@ class Service {
|
|
|
76
70
|
response.type = path$1.extname(path);
|
|
77
71
|
// Set headers.
|
|
78
72
|
if (headers) {
|
|
79
|
-
if (isFunction(headers)) {
|
|
73
|
+
if (type.isFunction(headers)) {
|
|
80
74
|
const fields = await headers(path, stats);
|
|
81
75
|
if (fields) {
|
|
82
76
|
response.set(fields);
|
|
@@ -138,7 +132,7 @@ class Service {
|
|
|
138
132
|
return false;
|
|
139
133
|
}
|
|
140
134
|
// Is ignore path or file (403).
|
|
141
|
-
if (this.isIgnore(path$2)) {
|
|
135
|
+
if (await this.isIgnore(path$2)) {
|
|
142
136
|
return false;
|
|
143
137
|
}
|
|
144
138
|
// Get options.
|
|
@@ -205,4 +199,4 @@ class Service {
|
|
|
205
199
|
}
|
|
206
200
|
}
|
|
207
201
|
|
|
208
|
-
|
|
202
|
+
exports.Service = Service;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module Service
|
|
3
|
+
*/
|
|
4
|
+
import { Context } from 'koa';
|
|
5
|
+
import { Stats } from 'fs';
|
|
6
|
+
import { FileSystem } from './utils/fs.cjs';
|
|
7
|
+
interface Headers {
|
|
8
|
+
[key: string]: string | string[];
|
|
9
|
+
}
|
|
10
|
+
interface IgnoreFunction {
|
|
11
|
+
(path: string): boolean | Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
interface HeadersFunction {
|
|
14
|
+
(path: string, stats: Stats): Promise<Headers | void> | Headers | void;
|
|
15
|
+
}
|
|
16
|
+
export interface Options {
|
|
17
|
+
fs?: FileSystem;
|
|
18
|
+
etag?: boolean;
|
|
19
|
+
acceptRanges?: boolean;
|
|
20
|
+
lastModified?: boolean;
|
|
21
|
+
highWaterMark?: number;
|
|
22
|
+
ignore?: IgnoreFunction;
|
|
23
|
+
headers?: Headers | HeadersFunction;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* @class Service
|
|
27
|
+
*/
|
|
28
|
+
export declare class Service {
|
|
29
|
+
private readonly root;
|
|
30
|
+
private readonly options;
|
|
31
|
+
/**
|
|
32
|
+
* @constructor
|
|
33
|
+
* @description Create file service.
|
|
34
|
+
* @param root The file service root.
|
|
35
|
+
* @param options The file service options.
|
|
36
|
+
*/
|
|
37
|
+
constructor(root: string, options?: Options);
|
|
38
|
+
/**
|
|
39
|
+
* @private
|
|
40
|
+
* @method isIgnore
|
|
41
|
+
* @description Check if path is ignore.
|
|
42
|
+
* @param path The path to check.
|
|
43
|
+
*/
|
|
44
|
+
private isIgnore;
|
|
45
|
+
/**
|
|
46
|
+
* @private
|
|
47
|
+
* @method setupHeaders
|
|
48
|
+
* @description Setup headers.
|
|
49
|
+
* @param context The koa context.
|
|
50
|
+
* @param path The file path.
|
|
51
|
+
* @param stats The file stats.
|
|
52
|
+
*/
|
|
53
|
+
private setupHeaders;
|
|
54
|
+
/**
|
|
55
|
+
* @public
|
|
56
|
+
* @method respond
|
|
57
|
+
* @description Respond file.
|
|
58
|
+
* @param context The koa context.
|
|
59
|
+
*/
|
|
60
|
+
respond(context: Context): Promise<boolean>;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
package/cjs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
@@ -16,11 +16,12 @@ const Service = require('./Service.cjs');
|
|
|
16
16
|
*/
|
|
17
17
|
/**
|
|
18
18
|
* @function server
|
|
19
|
-
* @
|
|
20
|
-
* @param {
|
|
19
|
+
* @description Create files server.
|
|
20
|
+
* @param {string} root The files server root.
|
|
21
|
+
* @param {Options} options The files server options.
|
|
21
22
|
*/
|
|
22
23
|
function server(root, options) {
|
|
23
|
-
const service = new Service(root, options);
|
|
24
|
+
const service = new Service.Service(root, options);
|
|
24
25
|
if (options === null || options === void 0 ? void 0 : options.defer) {
|
|
25
26
|
return async (context, next) => {
|
|
26
27
|
await next();
|
|
@@ -34,4 +35,4 @@ function server(root, options) {
|
|
|
34
35
|
};
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
|
|
38
|
+
exports.server = server;
|
package/cjs/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module index
|
|
3
|
+
*/
|
|
4
|
+
import { Middleware } from 'koa';
|
|
5
|
+
import { Options as ServiceOptions } from './Service.cjs';
|
|
6
|
+
export interface Options extends ServiceOptions {
|
|
7
|
+
defer?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @function server
|
|
11
|
+
* @description Create files server.
|
|
12
|
+
* @param {string} root The files server root.
|
|
13
|
+
* @param {Options} options The files server options.
|
|
14
|
+
*/
|
|
15
|
+
export declare function server(root: string, options?: Options): Middleware;
|
package/cjs/utils/fs.cjs
CHANGED
package/cjs/utils/hash.cjs
CHANGED
package/cjs/utils/http.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
@@ -166,7 +166,7 @@ function parseRanges(context, stats) {
|
|
|
166
166
|
const contentRange = `Content-Range: bytes ${start}-${end}/${size}`;
|
|
167
167
|
const prefix = buffer.Buffer.from(`${head}--${boundary}\r\n${contentType}\r\n${contentRange}\r\n\r\n`);
|
|
168
168
|
// Compute Content-Length
|
|
169
|
-
contentLength += length +
|
|
169
|
+
contentLength += prefix.length + length;
|
|
170
170
|
// Cache range.
|
|
171
171
|
ranges.push({ offset: start, length, prefix });
|
|
172
172
|
}
|
package/cjs/utils/path.cjs
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package koa-files
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 4.0.0
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A static files serving middleware for koa.
|
|
7
|
+
* @see https://github.com/nuintun/koa-files#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @module type
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @function isFunction
|
|
17
|
+
* @description Check if value is function.
|
|
18
|
+
* @param value The value to check.
|
|
19
|
+
*/
|
|
20
|
+
function isFunction(value) {
|
|
21
|
+
return typeof value === 'function';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
exports.isFunction = isFunction;
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
* @module ReadStream
|
|
3
3
|
*/
|
|
4
4
|
import { PathLike } from 'fs';
|
|
5
|
-
import { Range } from './utils/http';
|
|
6
|
-
import { FileSystem } from './utils/fs';
|
|
5
|
+
import { Range } from './utils/http.js';
|
|
6
|
+
import { FileSystem } from './utils/fs.js';
|
|
7
7
|
import { Readable, ReadableOptions } from 'stream';
|
|
8
8
|
export interface Options extends Pick<ReadableOptions, 'highWaterMark'> {
|
|
9
9
|
fs: FileSystem;
|
package/esm/ReadStream.js
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { Context } from 'koa';
|
|
5
5
|
import { Stats } from 'fs';
|
|
6
|
-
import { FileSystem } from './utils/fs';
|
|
7
|
-
interface IgnoreFunction {
|
|
8
|
-
(path: string): boolean;
|
|
9
|
-
}
|
|
6
|
+
import { FileSystem } from './utils/fs.js';
|
|
10
7
|
interface Headers {
|
|
11
8
|
[key: string]: string | string[];
|
|
12
9
|
}
|
|
10
|
+
interface IgnoreFunction {
|
|
11
|
+
(path: string): boolean | Promise<boolean>;
|
|
12
|
+
}
|
|
13
13
|
interface HeadersFunction {
|
|
14
14
|
(path: string, stats: Stats): Promise<Headers | void> | Headers | void;
|
|
15
15
|
}
|
|
@@ -25,7 +25,7 @@ export interface Options {
|
|
|
25
25
|
/**
|
|
26
26
|
* @class Service
|
|
27
27
|
*/
|
|
28
|
-
export
|
|
28
|
+
export declare class Service {
|
|
29
29
|
private readonly root;
|
|
30
30
|
private readonly options;
|
|
31
31
|
/**
|
package/esm/Service.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
import createETag from 'etag';
|
|
11
11
|
import fs from 'fs';
|
|
12
12
|
import { ReadStream } from './ReadStream.js';
|
|
13
|
+
import { isFunction } from './utils/type.js';
|
|
13
14
|
import { stat } from './utils/fs.js';
|
|
14
15
|
import { resolve, extname, join } from 'path';
|
|
15
16
|
import { unixify, isOutRoot, hasTrailingSlash } from './utils/path.js';
|
|
@@ -18,14 +19,6 @@ import { decodeURI, isConditionalGET, isPreconditionFailed, parseRanges } from '
|
|
|
18
19
|
/**
|
|
19
20
|
* @module Service
|
|
20
21
|
*/
|
|
21
|
-
/**
|
|
22
|
-
* @function isFunction
|
|
23
|
-
* @description Check if value is function.
|
|
24
|
-
* @param value The value to check.
|
|
25
|
-
*/
|
|
26
|
-
function isFunction(value) {
|
|
27
|
-
return typeof value === 'function';
|
|
28
|
-
}
|
|
29
22
|
/**
|
|
30
23
|
* @class Service
|
|
31
24
|
*/
|
|
@@ -46,9 +39,10 @@ class Service {
|
|
|
46
39
|
* @description Check if path is ignore.
|
|
47
40
|
* @param path The path to check.
|
|
48
41
|
*/
|
|
49
|
-
isIgnore(path) {
|
|
42
|
+
async isIgnore(path) {
|
|
50
43
|
const { ignore } = this.options;
|
|
51
|
-
|
|
44
|
+
const isIgnored = isFunction(ignore) ? await ignore(path) : false;
|
|
45
|
+
return isIgnored === true;
|
|
52
46
|
}
|
|
53
47
|
/**
|
|
54
48
|
* @private
|
|
@@ -129,7 +123,7 @@ class Service {
|
|
|
129
123
|
return false;
|
|
130
124
|
}
|
|
131
125
|
// Is ignore path or file (403).
|
|
132
|
-
if (this.isIgnore(path)) {
|
|
126
|
+
if (await this.isIgnore(path)) {
|
|
133
127
|
return false;
|
|
134
128
|
}
|
|
135
129
|
// Get options.
|
|
@@ -196,4 +190,4 @@ class Service {
|
|
|
196
190
|
}
|
|
197
191
|
}
|
|
198
192
|
|
|
199
|
-
export { Service
|
|
193
|
+
export { Service };
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module index
|
|
3
|
+
*/
|
|
4
|
+
import { Middleware } from 'koa';
|
|
5
|
+
import { Options as ServiceOptions } from './Service.js';
|
|
6
|
+
export interface Options extends ServiceOptions {
|
|
7
|
+
defer?: boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @function server
|
|
11
|
+
* @description Create files server.
|
|
12
|
+
* @param {string} root The files server root.
|
|
13
|
+
* @param {Options} options The files server options.
|
|
14
|
+
*/
|
|
15
|
+
export declare function server(root: string, options?: Options): Middleware;
|
package/esm/index.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import Service from './Service.js';
|
|
10
|
+
import { Service } from './Service.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @module index
|
|
14
14
|
*/
|
|
15
15
|
/**
|
|
16
16
|
* @function server
|
|
17
|
-
* @
|
|
18
|
-
* @param {
|
|
17
|
+
* @description Create files server.
|
|
18
|
+
* @param {string} root The files server root.
|
|
19
|
+
* @param {Options} options The files server options.
|
|
19
20
|
*/
|
|
20
21
|
function server(root, options) {
|
|
21
22
|
const service = new Service(root, options);
|
|
@@ -32,4 +33,4 @@ function server(root, options) {
|
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
export { server
|
|
36
|
+
export { server };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module fs
|
|
3
|
+
*/
|
|
4
|
+
import fs, { Stats } from 'fs';
|
|
5
|
+
export interface FileSystem {
|
|
6
|
+
stat: typeof fs.stat;
|
|
7
|
+
open: typeof fs.open;
|
|
8
|
+
read: typeof fs.read;
|
|
9
|
+
close: typeof fs.close;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @function stat
|
|
13
|
+
* @description Get file stats.
|
|
14
|
+
* @param fs The file system to used.
|
|
15
|
+
* @param path The file path.
|
|
16
|
+
*/
|
|
17
|
+
export declare function stat(fs: FileSystem, path: string): Promise<Stats | null>;
|
package/esm/utils/fs.js
CHANGED
package/esm/utils/hash.js
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module http
|
|
3
|
+
*/
|
|
4
|
+
import { Stats } from 'fs';
|
|
5
|
+
import { Context } from 'koa';
|
|
6
|
+
import { Buffer } from 'buffer';
|
|
7
|
+
export interface Range {
|
|
8
|
+
offset: number;
|
|
9
|
+
length: number;
|
|
10
|
+
prefix?: Buffer;
|
|
11
|
+
suffix?: Buffer;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @function decodeURI
|
|
15
|
+
* @description Decode URI component.
|
|
16
|
+
* @param URI The URI to decode.
|
|
17
|
+
*/
|
|
18
|
+
export declare function decodeURI(URI: string): string | -1;
|
|
19
|
+
/**
|
|
20
|
+
* @function isConditionalGET
|
|
21
|
+
* @description Check if request is conditional GET.
|
|
22
|
+
* @param context The koa context.
|
|
23
|
+
*/
|
|
24
|
+
export declare function isConditionalGET({ request }: Context): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* @function isPreconditionFailed
|
|
27
|
+
* @description Check if request precondition failed.
|
|
28
|
+
* @param context The koa context.
|
|
29
|
+
*/
|
|
30
|
+
export declare function isPreconditionFailed({ request, response }: Context): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* @function parseRanges
|
|
33
|
+
* @description Parse ranges.
|
|
34
|
+
* @param context The koa context.
|
|
35
|
+
* @param stats The file stats.
|
|
36
|
+
*/
|
|
37
|
+
export declare function parseRanges(context: Context, stats: Stats): Range[] | -1 | -2;
|
package/esm/utils/http.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package koa-files
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version
|
|
4
|
+
* @version 4.0.0
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A static files serving middleware for koa.
|
|
7
7
|
* @see https://github.com/nuintun/koa-files#readme
|
|
@@ -158,7 +158,7 @@ function parseRanges(context, stats) {
|
|
|
158
158
|
const contentRange = `Content-Range: bytes ${start}-${end}/${size}`;
|
|
159
159
|
const prefix = Buffer.from(`${head}--${boundary}\r\n${contentType}\r\n${contentRange}\r\n\r\n`);
|
|
160
160
|
// Compute Content-Length
|
|
161
|
-
contentLength += length +
|
|
161
|
+
contentLength += prefix.length + length;
|
|
162
162
|
// Cache range.
|
|
163
163
|
ranges.push({ offset: start, length, prefix });
|
|
164
164
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module path
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @function unixify
|
|
6
|
+
* @description Convert path to unix style.
|
|
7
|
+
* @param path The path to convert.
|
|
8
|
+
*/
|
|
9
|
+
export declare function unixify(path: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* @function hasTrailingSlash
|
|
12
|
+
* @description Check if path has trailing slash.
|
|
13
|
+
* @param path The path to check.
|
|
14
|
+
*/
|
|
15
|
+
export declare function hasTrailingSlash(path: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* @function isOutRoot
|
|
18
|
+
* @description Check if path is out of root.
|
|
19
|
+
* @param path The path to check.
|
|
20
|
+
* @param root The root path.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isOutRoot(path: string, root: string): boolean;
|
package/esm/utils/path.js
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package koa-files
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 4.0.0
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A static files serving middleware for koa.
|
|
7
|
+
* @see https://github.com/nuintun/koa-files#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @module type
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @function isFunction
|
|
15
|
+
* @description Check if value is function.
|
|
16
|
+
* @param value The value to check.
|
|
17
|
+
*/
|
|
18
|
+
function isFunction(value) {
|
|
19
|
+
return typeof value === 'function';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { isFunction };
|
package/package.json
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-files",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "A static files serving middleware for koa.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "cjs/index.cjs",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
|
-
"types": "types/index.d.ts",
|
|
9
8
|
"exports": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./esm/index.js",
|
|
11
|
+
"require": "./cjs/index.cjs"
|
|
12
|
+
}
|
|
13
13
|
},
|
|
14
14
|
"files": [
|
|
15
15
|
"cjs",
|
|
16
|
-
"esm"
|
|
17
|
-
"types"
|
|
16
|
+
"esm"
|
|
18
17
|
],
|
|
19
18
|
"repository": {
|
|
20
19
|
"type": "git",
|
|
@@ -43,34 +42,35 @@
|
|
|
43
42
|
},
|
|
44
43
|
"homepage": "https://github.com/nuintun/koa-files#readme",
|
|
45
44
|
"scripts": {
|
|
46
|
-
"test": "node tests",
|
|
47
45
|
"lint": "tsc --noEmit",
|
|
46
|
+
"test": "node tests/index.js",
|
|
48
47
|
"clean": "node tools/clean.js",
|
|
49
48
|
"prepublishOnly": "npm run build",
|
|
50
49
|
"build:cjs": "rollup -c tools/rollup.cjs.js",
|
|
51
50
|
"build:esm": "rollup -c tools/rollup.esm.js",
|
|
51
|
+
"build:fix-types": "node tools/fix-types.js",
|
|
52
52
|
"format": "prettier --write . --ignore-path .prettierignore",
|
|
53
53
|
"build": "npm run clean && npm run build:main && npm run format",
|
|
54
|
-
"build:main": "npm run build:cjs && npm run build:esm && npm run build:types"
|
|
55
|
-
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir types && node tools/fix-types.js"
|
|
54
|
+
"build:main": "npm run build:cjs && npm run build:esm && npm run build:fix-types"
|
|
56
55
|
},
|
|
57
56
|
"dependencies": {
|
|
58
57
|
"etag": "^1.8.1",
|
|
59
58
|
"range-parser": "^1.2.1",
|
|
60
|
-
"tslib": "^2.8.
|
|
59
|
+
"tslib": "^2.8.1"
|
|
61
60
|
},
|
|
62
61
|
"devDependencies": {
|
|
63
|
-
"@rollup/plugin-typescript": "^12.1.
|
|
62
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
64
63
|
"@types/etag": "^1.8.3",
|
|
65
64
|
"@types/koa": "^2.15.0",
|
|
65
|
+
"@types/node": "^22.10.2",
|
|
66
66
|
"@types/range-parser": "^1.2.7",
|
|
67
|
+
"dts-paths": "^1.0.1",
|
|
67
68
|
"koa": "^2.15.3",
|
|
68
|
-
"magic-string": "^0.30.
|
|
69
|
-
"prettier": "^3.
|
|
69
|
+
"magic-string": "^0.30.17",
|
|
70
|
+
"prettier": "^3.4.2",
|
|
70
71
|
"rimraf": "^6.0.1",
|
|
71
|
-
"rollup": "^4.
|
|
72
|
-
"
|
|
73
|
-
"typescript": "^5.6.3"
|
|
72
|
+
"rollup": "^4.28.1",
|
|
73
|
+
"typescript": "^5.7.2"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
76
|
"koa": "^2.0.0"
|
package/types/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module index
|
|
3
|
-
*/
|
|
4
|
-
import { Middleware } from 'koa';
|
|
5
|
-
import { Options as ServiceOptions } from './Service';
|
|
6
|
-
export interface Options extends ServiceOptions {
|
|
7
|
-
defer?: boolean;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* @function server
|
|
11
|
-
* @param {string} root
|
|
12
|
-
* @param {Options} options
|
|
13
|
-
*/
|
|
14
|
-
export default function server(root: string, options?: Options): Middleware;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|