webpack-dev-service 0.12.0 → 0.12.2
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/cjs/client/client.cjs +1 -1
- package/cjs/client/events.cjs +1 -1
- package/cjs/client/hot.cjs +1 -1
- package/cjs/client/index.cjs +1 -1
- package/cjs/client/main.cjs +1 -1
- package/cjs/client/ui/Overlay.cjs +1 -1
- package/cjs/client/ui/Progress.cjs +1 -1
- package/cjs/client/ui/utils.cjs +1 -1
- package/cjs/server/compose.cjs +1 -1
- package/cjs/server/dev/Service.cjs +31 -113
- package/cjs/server/dev/index.cjs +1 -1
- package/cjs/server/dev/middleware.cjs +8 -8
- package/cjs/server/dev/utils/fs.cjs +1 -1
- package/cjs/server/dev/utils/hash.cjs +3 -3
- package/cjs/server/dev/utils/http.cjs +22 -19
- package/cjs/server/dev/utils/path.cjs +5 -5
- package/cjs/server/dev/utils/paths.cjs +3 -3
- package/cjs/server/dev/utils/ready.cjs +1 -1
- package/cjs/server/dev/utils/setupHooks.cjs +1 -1
- package/cjs/server/dev/utils/setupOutputFileSystem.cjs +1 -1
- package/cjs/server/dev/utils/setupWatching.cjs +1 -1
- package/cjs/server/dev/utils/setupWriteToDisk.cjs +6 -6
- package/cjs/server/dev/utils/stream.cjs +189 -0
- package/cjs/server/hot/Socket.cjs +8 -7
- package/cjs/server/hot/index.cjs +1 -1
- package/cjs/server/hot/utils.cjs +1 -1
- package/cjs/server/index.cjs +1 -1
- package/cjs/server/schema.cjs +1 -1
- package/cjs/server/utils.cjs +1 -1
- package/esm/client/client.js +1 -1
- package/esm/client/events.js +1 -1
- package/esm/client/hot.js +1 -1
- package/esm/client/index.js +1 -1
- package/esm/client/main.js +1 -1
- package/esm/client/ui/Overlay.js +1 -1
- package/esm/client/ui/Progress.js +1 -1
- package/esm/client/ui/utils.js +1 -1
- package/esm/server/compose.js +1 -1
- package/esm/server/dev/Service.js +22 -103
- package/esm/server/dev/index.js +1 -1
- package/esm/server/dev/middleware.js +8 -8
- package/esm/server/dev/utils/fs.js +1 -1
- package/esm/server/dev/utils/hash.js +2 -2
- package/esm/server/dev/utils/http.js +22 -19
- package/esm/server/dev/utils/path.js +2 -2
- package/esm/server/dev/utils/paths.js +2 -2
- package/esm/server/dev/utils/ready.js +1 -1
- package/esm/server/dev/utils/setupHooks.js +1 -1
- package/esm/server/dev/utils/setupOutputFileSystem.js +1 -1
- package/esm/server/dev/utils/setupWatching.js +1 -1
- package/esm/server/dev/utils/setupWriteToDisk.js +3 -3
- package/esm/server/dev/utils/stream.js +187 -0
- package/esm/server/hot/Socket.js +4 -3
- package/esm/server/hot/index.js +1 -1
- package/esm/server/hot/utils.js +1 -1
- package/esm/server/index.js +1 -1
- package/esm/server/schema.js +1 -1
- package/esm/server/utils.js +1 -1
- package/package.json +10 -11
- package/types/server/dev/Service.d.ts +15 -32
- package/types/server/dev/utils/fs.d.ts +7 -12
- package/types/server/dev/utils/http.d.ts +6 -11
- package/types/server/dev/utils/stream.d.ts +83 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
12
|
+
const node_path = require('node:path');
|
|
13
|
+
const node_fs = require('node:fs');
|
|
14
14
|
const utils = require('../../utils.cjs');
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -31,14 +31,14 @@ function setupWriteToDisk(context) {
|
|
|
31
31
|
if (!isAllowWrite(targetPath, filter)) {
|
|
32
32
|
return callback();
|
|
33
33
|
}
|
|
34
|
-
const dir =
|
|
34
|
+
const dir = node_path.dirname(targetPath);
|
|
35
35
|
const name = getCompilerName(compiler);
|
|
36
|
-
return
|
|
36
|
+
return node_fs.mkdir(dir, { recursive: true }, error => {
|
|
37
37
|
if (error) {
|
|
38
38
|
logger.error(`${name}unable to write "${dir}" directory to disk:\n${error}`);
|
|
39
39
|
return callback(error);
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
node_fs.writeFile(targetPath, content, error => {
|
|
42
42
|
if (error) {
|
|
43
43
|
logger.error(`${name}unable to write "${targetPath}" asset to disk:\n${error}`);
|
|
44
44
|
return callback(error);
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @package webpack-dev-service
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @version 0.12.2
|
|
5
|
+
* @author nuintun <nuintun@qq.com>
|
|
6
|
+
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
|
+
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
const node_buffer = require('node:buffer');
|
|
13
|
+
const node_stream = require('node:stream');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @module stream
|
|
17
|
+
*/
|
|
18
|
+
class FileReadStream extends node_stream.Readable {
|
|
19
|
+
fd;
|
|
20
|
+
fs;
|
|
21
|
+
path;
|
|
22
|
+
ranges;
|
|
23
|
+
bytesRead = 0;
|
|
24
|
+
reading = false;
|
|
25
|
+
currentRangeIndex = 0;
|
|
26
|
+
readState = 0 /* ReadState.PREFIX */;
|
|
27
|
+
/**
|
|
28
|
+
* @constructor
|
|
29
|
+
* @param path The file path.
|
|
30
|
+
* @param ranges The ranges to read.
|
|
31
|
+
* @param options The stream options.
|
|
32
|
+
*/
|
|
33
|
+
constructor(path, ranges, options) {
|
|
34
|
+
const { fs, encoding, highWaterMark } = options;
|
|
35
|
+
super({ encoding, highWaterMark });
|
|
36
|
+
this.fs = fs;
|
|
37
|
+
this.path = path;
|
|
38
|
+
this.ranges = ranges;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @override
|
|
42
|
+
* @method _construct
|
|
43
|
+
* @param callback The callback.
|
|
44
|
+
*/
|
|
45
|
+
_construct(callback) {
|
|
46
|
+
this.fs.open(this.path, 'r', (openError, fd) => {
|
|
47
|
+
if (openError === null) {
|
|
48
|
+
this.fd = fd;
|
|
49
|
+
}
|
|
50
|
+
callback(openError);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @private
|
|
55
|
+
* @method getRange
|
|
56
|
+
*/
|
|
57
|
+
getRange() {
|
|
58
|
+
return this.ranges[this.currentRangeIndex];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @private
|
|
62
|
+
* @method getPadding
|
|
63
|
+
* @param range The current range.
|
|
64
|
+
*/
|
|
65
|
+
getPadding(range) {
|
|
66
|
+
switch (this.readState) {
|
|
67
|
+
case 0 /* ReadState.PREFIX */:
|
|
68
|
+
return range.prefix;
|
|
69
|
+
case 2 /* ReadState.SUFFIX */:
|
|
70
|
+
return range.suffix;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @private
|
|
75
|
+
* @method readFilePadding
|
|
76
|
+
* @param fd The file descriptor.
|
|
77
|
+
* @param range The current range.
|
|
78
|
+
* @param size The number of bytes to read.
|
|
79
|
+
*/
|
|
80
|
+
readFilePadding(fd, range, size) {
|
|
81
|
+
let bytesRead = 0;
|
|
82
|
+
const padding = this.getPadding(range);
|
|
83
|
+
const hasRangePadding = padding != null;
|
|
84
|
+
const length = hasRangePadding ? padding.length : 0;
|
|
85
|
+
if (hasRangePadding && length > 0) {
|
|
86
|
+
const begin = this.bytesRead;
|
|
87
|
+
bytesRead = Math.min(size, length - begin);
|
|
88
|
+
const end = begin + bytesRead;
|
|
89
|
+
this.push(padding.subarray(begin, end));
|
|
90
|
+
this.bytesRead = end;
|
|
91
|
+
}
|
|
92
|
+
if (!hasRangePadding || this.bytesRead >= length) {
|
|
93
|
+
this.bytesRead = 0;
|
|
94
|
+
const hasBytesRead = bytesRead > 0;
|
|
95
|
+
switch (this.readState) {
|
|
96
|
+
case 0 /* ReadState.PREFIX */:
|
|
97
|
+
this.readState = 1 /* ReadState.RANGE */;
|
|
98
|
+
if (!hasBytesRead) {
|
|
99
|
+
this.readFileRange(fd, range, size);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
case 2 /* ReadState.SUFFIX */:
|
|
103
|
+
this.currentRangeIndex++;
|
|
104
|
+
this.readState = 0 /* ReadState.PREFIX */;
|
|
105
|
+
if (!hasBytesRead) {
|
|
106
|
+
const range = this.getRange();
|
|
107
|
+
if (range == null) {
|
|
108
|
+
this.push(null);
|
|
109
|
+
} else {
|
|
110
|
+
this.readFilePadding(fd, range, size);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @private
|
|
119
|
+
* @method readFileRange
|
|
120
|
+
* @param fd The file descriptor.
|
|
121
|
+
* @param range The current range.
|
|
122
|
+
* @param size The number of bytes to read.
|
|
123
|
+
*/
|
|
124
|
+
readFileRange(fd, range, size) {
|
|
125
|
+
this.reading = true;
|
|
126
|
+
const { bytesRead } = this;
|
|
127
|
+
const position = range.offset + bytesRead;
|
|
128
|
+
const buffer = node_buffer.Buffer.alloc(Math.min(size, range.length - bytesRead));
|
|
129
|
+
this.fs.read(fd, buffer, 0, buffer.length, position, (readError, bytesRead, buffer) => {
|
|
130
|
+
if (readError === null) {
|
|
131
|
+
if (bytesRead > 0) {
|
|
132
|
+
this.push(buffer.subarray(0, bytesRead));
|
|
133
|
+
this.bytesRead += bytesRead;
|
|
134
|
+
} else {
|
|
135
|
+
this.bytesRead = 0;
|
|
136
|
+
this.readState = 2 /* ReadState.SUFFIX */;
|
|
137
|
+
this.readFilePadding(fd, range, size);
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
this.destroy(readError);
|
|
141
|
+
}
|
|
142
|
+
this.reading = false;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @override
|
|
147
|
+
* @method _read
|
|
148
|
+
* @param size The number of bytes to read.
|
|
149
|
+
*/
|
|
150
|
+
_read(size) {
|
|
151
|
+
if (!this.reading) {
|
|
152
|
+
const { fd } = this;
|
|
153
|
+
const range = this.getRange();
|
|
154
|
+
if (fd != null && range != null) {
|
|
155
|
+
switch (this.readState) {
|
|
156
|
+
case 0 /* ReadState.PREFIX */:
|
|
157
|
+
case 2 /* ReadState.SUFFIX */:
|
|
158
|
+
this.readFilePadding(fd, range, size);
|
|
159
|
+
break;
|
|
160
|
+
case 1 /* ReadState.RANGE */:
|
|
161
|
+
this.readFileRange(fd, range, size);
|
|
162
|
+
break;
|
|
163
|
+
default:
|
|
164
|
+
throw new Error(`invalid read state: ${this.readState}`);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
this.push(null);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* @override
|
|
173
|
+
* @method _destroy
|
|
174
|
+
* @param error The error.
|
|
175
|
+
* @param callback The callback.
|
|
176
|
+
*/
|
|
177
|
+
_destroy(error, callback) {
|
|
178
|
+
const { fd } = this;
|
|
179
|
+
if (fd != null) {
|
|
180
|
+
this.fs.close(fd, closeError => {
|
|
181
|
+
callback(error ?? closeError);
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
callback(error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
exports.FileReadStream = FileReadStream;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
12
|
const webpack = require('webpack');
|
|
13
|
-
const
|
|
14
|
-
const
|
|
13
|
+
const node_buffer = require('node:buffer');
|
|
14
|
+
const node_url = require('node:url');
|
|
15
|
+
const node_path = require('node:path');
|
|
15
16
|
const WebSocket = require('ws');
|
|
16
17
|
const utils$1 = require('../utils.cjs');
|
|
17
18
|
const utils = require('./utils.cjs');
|
|
@@ -29,12 +30,12 @@ const WebSocket__default = /*#__PURE__*/ _interopDefault(WebSocket);
|
|
|
29
30
|
function entrypoint() {
|
|
30
31
|
const filename = require.main.filename;
|
|
31
32
|
try {
|
|
32
|
-
return
|
|
33
|
+
return node_path.dirname(node_url.fileURLToPath(filename));
|
|
33
34
|
} catch {
|
|
34
|
-
return
|
|
35
|
+
return node_path.dirname(filename);
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
|
-
const client =
|
|
38
|
+
const client = node_path.resolve(entrypoint(), '../../client/main.cjs');
|
|
38
39
|
class Socket {
|
|
39
40
|
logger;
|
|
40
41
|
compiler;
|
|
@@ -144,7 +145,7 @@ class Socket {
|
|
|
144
145
|
if (utils.isUpgradable(context) && server.shouldHandle(request)) {
|
|
145
146
|
context.respond = false;
|
|
146
147
|
const { socket } = context;
|
|
147
|
-
const head = Buffer.alloc(0);
|
|
148
|
+
const head = node_buffer.Buffer.alloc(0);
|
|
148
149
|
server.handleUpgrade(request, socket, head, client => {
|
|
149
150
|
server.emit('connection', client, request);
|
|
150
151
|
});
|
package/cjs/server/hot/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/cjs/server/hot/utils.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/cjs/server/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/cjs/server/schema.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/cjs/server/utils.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/events.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/hot.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/main.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/ui/Overlay.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/client/ui/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
package/esm/server/compose.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import
|
|
11
|
-
import destroy from 'destroy';
|
|
12
|
-
import { PassThrough } from 'stream';
|
|
10
|
+
import createETag from 'etag';
|
|
13
11
|
import { isFunction } from '../utils.js';
|
|
14
12
|
import { stat } from './utils/fs.js';
|
|
15
|
-
import {
|
|
13
|
+
import { FileReadStream } from './utils/stream.js';
|
|
14
|
+
import { resolve, extname, join } from 'node:path';
|
|
16
15
|
import { unixify, isOutRoot, hasTrailingSlash } from './utils/path.js';
|
|
17
16
|
import { isConditionalGET, isPreconditionFailure, parseRanges } from './utils/http.js';
|
|
18
17
|
|
|
@@ -28,8 +27,8 @@ class Service {
|
|
|
28
27
|
/**
|
|
29
28
|
* @constructor
|
|
30
29
|
* @description Create file service.
|
|
31
|
-
* @param root
|
|
32
|
-
* @param options
|
|
30
|
+
* @param root The file service root.
|
|
31
|
+
* @param options The file service options.
|
|
33
32
|
*/
|
|
34
33
|
constructor(root, options) {
|
|
35
34
|
this.options = options;
|
|
@@ -39,7 +38,7 @@ class Service {
|
|
|
39
38
|
* @private
|
|
40
39
|
* @method isIgnore
|
|
41
40
|
* @description Check if path is ignore.
|
|
42
|
-
* @param path
|
|
41
|
+
* @param path The path to check.
|
|
43
42
|
*/
|
|
44
43
|
isIgnore(path) {
|
|
45
44
|
const { ignore } = this.options;
|
|
@@ -48,10 +47,10 @@ class Service {
|
|
|
48
47
|
/**
|
|
49
48
|
* @private
|
|
50
49
|
* @method setupHeaders
|
|
51
|
-
* @description Setup headers
|
|
52
|
-
* @param context
|
|
53
|
-
* @param path
|
|
54
|
-
* @param stats
|
|
50
|
+
* @description Setup headers.
|
|
51
|
+
* @param context The koa context.
|
|
52
|
+
* @param path The file path.
|
|
53
|
+
* @param stats The file stats.
|
|
55
54
|
*/
|
|
56
55
|
setupHeaders(context, path, stats) {
|
|
57
56
|
const { options } = this;
|
|
@@ -74,7 +73,7 @@ class Service {
|
|
|
74
73
|
context.remove('ETag');
|
|
75
74
|
} else {
|
|
76
75
|
// Set ETag.
|
|
77
|
-
context.set('ETag',
|
|
76
|
+
context.set('ETag', createETag(stats));
|
|
78
77
|
}
|
|
79
78
|
// Last-Modified.
|
|
80
79
|
if (options.lastModified === false) {
|
|
@@ -96,97 +95,15 @@ class Service {
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
|
-
/**
|
|
100
|
-
* @private
|
|
101
|
-
* @method write
|
|
102
|
-
* @description Write file to stream.
|
|
103
|
-
* @param stream Destination stream.
|
|
104
|
-
* @param path The file path to read.
|
|
105
|
-
* @param range The range to read.
|
|
106
|
-
* @param end Is destory destination stream after read complete.
|
|
107
|
-
*/
|
|
108
|
-
write(stream, path, range, end) {
|
|
109
|
-
const { fs } = this.options;
|
|
110
|
-
return new Promise(resolve => {
|
|
111
|
-
// Range prefix and suffix.
|
|
112
|
-
const { prefix, suffix } = range;
|
|
113
|
-
// Create file stream reader.
|
|
114
|
-
const reader = fs.createReadStream(path, range);
|
|
115
|
-
// File read stream open.
|
|
116
|
-
if (prefix) {
|
|
117
|
-
reader.once('open', () => {
|
|
118
|
-
// Write prefix boundary.
|
|
119
|
-
stream.write(prefix);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
// File read stream end.
|
|
123
|
-
if (suffix) {
|
|
124
|
-
reader.once('end', () => {
|
|
125
|
-
// Push suffix boundary.
|
|
126
|
-
stream.write(suffix);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
// File read stream error.
|
|
130
|
-
reader.once('error', () => {
|
|
131
|
-
// End stream.
|
|
132
|
-
stream.end();
|
|
133
|
-
// Unpipe.
|
|
134
|
-
reader.unpipe();
|
|
135
|
-
// Destroy.
|
|
136
|
-
destroy(reader);
|
|
137
|
-
// Resolve.
|
|
138
|
-
resolve(false);
|
|
139
|
-
});
|
|
140
|
-
// File read stream close.
|
|
141
|
-
reader.once('close', () => {
|
|
142
|
-
// Unpipe.
|
|
143
|
-
reader.unpipe();
|
|
144
|
-
// Destroy.
|
|
145
|
-
destroy(reader);
|
|
146
|
-
// Resolve.
|
|
147
|
-
resolve(true);
|
|
148
|
-
});
|
|
149
|
-
// Write data to buffer.
|
|
150
|
-
reader.pipe(stream, { end });
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* @private
|
|
155
|
-
* @method send
|
|
156
|
-
* @description Send file.
|
|
157
|
-
* @param context Koa context.
|
|
158
|
-
* @param path File path.
|
|
159
|
-
* @param ranges Read ranges.
|
|
160
|
-
*/
|
|
161
|
-
async send(context, path, ranges) {
|
|
162
|
-
// Set stream body, highWaterMark 64kb.
|
|
163
|
-
const stream = new PassThrough({
|
|
164
|
-
highWaterMark: 65536
|
|
165
|
-
});
|
|
166
|
-
// Set response body.
|
|
167
|
-
context.body = stream;
|
|
168
|
-
// Ranges length.
|
|
169
|
-
let { length } = ranges;
|
|
170
|
-
// Write ranges to stream.
|
|
171
|
-
for (const range of ranges) {
|
|
172
|
-
// Write range.
|
|
173
|
-
const passed = await this.write(stream, path, range, --length === 0);
|
|
174
|
-
// If not passed, break.
|
|
175
|
-
if (!passed) {
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
98
|
/**
|
|
181
99
|
* @public
|
|
182
|
-
* @method
|
|
183
|
-
* @description
|
|
184
|
-
* @param
|
|
185
|
-
* @param
|
|
100
|
+
* @method respond
|
|
101
|
+
* @description Respond file.
|
|
102
|
+
* @param pathname The pathname.
|
|
103
|
+
* @param context The koa context.
|
|
104
|
+
* @param publicPath The public path.
|
|
186
105
|
*/
|
|
187
|
-
async
|
|
188
|
-
// Get request pathname.
|
|
189
|
-
const pathname = context.path || '/';
|
|
106
|
+
async respond(pathname, context, publicPath) {
|
|
190
107
|
// Check public path.
|
|
191
108
|
if (!pathname.startsWith(publicPath)) {
|
|
192
109
|
return false;
|
|
@@ -260,8 +177,10 @@ class Service {
|
|
|
260
177
|
if (ranges === -2) {
|
|
261
178
|
return context.throw(400);
|
|
262
179
|
}
|
|
263
|
-
//
|
|
264
|
-
|
|
180
|
+
// Ranges length.
|
|
181
|
+
const { fs, highWaterMark } = this.options;
|
|
182
|
+
// Set response body.
|
|
183
|
+
context.body = new FileReadStream(path, ranges, { fs, highWaterMark });
|
|
265
184
|
// File found.
|
|
266
185
|
return true;
|
|
267
186
|
}
|
package/esm/server/dev/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -44,7 +44,7 @@ function getFileServices(context, stats) {
|
|
|
44
44
|
// Return services.
|
|
45
45
|
return services;
|
|
46
46
|
}
|
|
47
|
-
function getFileServicesAsync(context,
|
|
47
|
+
function getFileServicesAsync(context, pathname) {
|
|
48
48
|
return new Promise(resolve => {
|
|
49
49
|
const { stats } = context;
|
|
50
50
|
// If stats exists, resolve immediately.
|
|
@@ -52,7 +52,7 @@ function getFileServicesAsync(context, path) {
|
|
|
52
52
|
resolve(getFileServices(context, stats));
|
|
53
53
|
} else {
|
|
54
54
|
// Log waiting info.
|
|
55
|
-
context.logger.info(`wait until bundle finished: ${
|
|
55
|
+
context.logger.info(`wait until bundle finished: ${pathname}`);
|
|
56
56
|
// Otherwise, wait until bundle finished.
|
|
57
57
|
ready(context, stats => {
|
|
58
58
|
resolve(getFileServices(context, stats));
|
|
@@ -63,18 +63,18 @@ function getFileServicesAsync(context, path) {
|
|
|
63
63
|
function middleware(context) {
|
|
64
64
|
// Middleware.
|
|
65
65
|
return async (ctx, next) => {
|
|
66
|
-
const
|
|
67
|
-
//
|
|
68
|
-
if (
|
|
66
|
+
const pathname = decodeURI(ctx.path);
|
|
67
|
+
// Pathname decode failed or includes null byte(s).
|
|
68
|
+
if (pathname === -1 || pathname.includes('\0')) {
|
|
69
69
|
return ctx.throw(400);
|
|
70
70
|
}
|
|
71
71
|
// Only support GET and HEAD (405).
|
|
72
72
|
if (ctx.method === 'GET' || ctx.method === 'HEAD') {
|
|
73
73
|
// Get the file services.
|
|
74
|
-
const services = await getFileServicesAsync(context,
|
|
74
|
+
const services = await getFileServicesAsync(context, pathname);
|
|
75
75
|
// Try to respond.
|
|
76
76
|
for (const [publicPath, service] of services) {
|
|
77
|
-
if (await service.
|
|
77
|
+
if (await service.respond(pathname, ctx, publicPath)) {
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @package webpack-dev-service
|
|
3
3
|
* @license MIT
|
|
4
|
-
* @version 0.12.
|
|
4
|
+
* @version 0.12.2
|
|
5
5
|
* @author nuintun <nuintun@qq.com>
|
|
6
6
|
* @description A koa 2 middleware for webpack development and hot reloading.
|
|
7
7
|
* @see https://github.com/nuintun/webpack-dev-service#readme
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { getRandomValues } from 'crypto';
|
|
10
|
+
import { getRandomValues } from 'node:crypto';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @module hash
|