@maiyunnet/kebab 7.4.0 → 7.4.1

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "7.4.0";
8
+ export declare const VER = "7.4.1";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '7.4.0';
9
+ export const VER = '7.4.1';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "7.4.0",
3
+ "version": "7.4.1",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/route.d.ts CHANGED
@@ -63,8 +63,13 @@ export declare function getPost(req: http2.Http2ServerRequest | http.IncomingMes
63
63
  * @param events 文件处理情况
64
64
  */
65
65
  export declare function getFormData(req: http2.Http2ServerRequest | http.IncomingMessage, events?: {
66
+ /**
67
+ * --- 文件开始上传时触发,返回 true 则跳过该文件的保存 ---
68
+ */
66
69
  onfilestart?: (name: string) => boolean | undefined;
70
+ /** --- 文件上传时触发,仅 start 返回 true 时触发 --- */
67
71
  onfiledata?: (chunk: Buffer) => void;
72
+ /** --- 文件上传结束时触发,仅 start 返回 true 时触发 --- */
68
73
  onfileend?: () => void;
69
74
  }): Promise<{
70
75
  'post': Record<string, kebab.Json>;
package/sys/route.js CHANGED
@@ -867,6 +867,10 @@ export function getPost(req) {
867
867
  */
868
868
  export function getFormData(req, events = {}) {
869
869
  return new Promise(function (resolve) {
870
+ if (req.readableEnded) {
871
+ resolve({ 'post': {}, 'files': {} });
872
+ return;
873
+ }
870
874
  const ct = req.headers['content-type'] ?? '';
871
875
  if (!ct) {
872
876
  resolve({ 'post': {}, 'files': {} });
@@ -991,22 +995,20 @@ export function getFormData(req, events = {}) {
991
995
  if (io === -1) {
992
996
  // --- 没找到结束标语,将预留 boundary 长度之前的写入到文件 ---
993
997
  const writeBuffer = buffer.subarray(0, -boundary.length - 4);
994
- if (ftmpName === '') {
995
- events.onfiledata?.(writeBuffer);
996
- }
997
- else {
998
+ if (ftmpName) {
998
999
  ftmpStream.write(writeBuffer);
999
1000
  ftmpSize += Buffer.byteLength(writeBuffer);
1000
1001
  }
1002
+ else {
1003
+ // --- 跳过该文件 ---
1004
+ events.onfiledata?.(writeBuffer);
1005
+ }
1001
1006
  buffer = buffer.subarray(-boundary.length - 4);
1002
1007
  return;
1003
1008
  }
1004
1009
  // --- 找到结束标语,结束标语之前的写入文件,之后的重新放回 buffer ---
1005
1010
  const writeBuffer = buffer.subarray(0, io);
1006
- if (ftmpName === '') {
1007
- events.onfileend?.();
1008
- }
1009
- else {
1011
+ if (ftmpName) {
1010
1012
  ftmpStream.write(writeBuffer);
1011
1013
  ftmpSize += Buffer.byteLength(writeBuffer);
1012
1014
  ftmpStream.end(() => {
@@ -1046,6 +1048,10 @@ export function getFormData(req, events = {}) {
1046
1048
  rtn.files[name] = val;
1047
1049
  }
1048
1050
  }
1051
+ else {
1052
+ // --- 跳过该文件 ---
1053
+ events.onfileend?.();
1054
+ }
1049
1055
  // --- 重置状态机 ---
1050
1056
  state = EState.WAIT;
1051
1057
  buffer = buffer.subarray(io + 4 + boundary.length);
@@ -1054,11 +1060,31 @@ export function getFormData(req, events = {}) {
1054
1060
  }
1055
1061
  }
1056
1062
  });
1057
- req.on('error', function () {
1063
+ req.on('error', function (e) {
1064
+ if ((state === EState.FILE) && ftmpName) {
1065
+ ftmpStream.destroy();
1066
+ }
1067
+ lCore.debug('[ROUTE][GETFORMDATA] request error before getFormData: ' + e.message);
1068
+ lCore.log({}, '[ROUTE][GETFORMDATA] request error before getFormData: ' + (e.stack ?? ''), '-error');
1058
1069
  resolve(false);
1059
1070
  });
1060
1071
  req.on('end', function () {
1061
1072
  readEnd = true;
1073
+ if (state === EState.FILE) {
1074
+ if (ftmpName) {
1075
+ ftmpStream.end(() => {
1076
+ --writeFileLength;
1077
+ if (!writeFileLength) {
1078
+ // --- 文件也写完了 ---
1079
+ resolve(rtn);
1080
+ }
1081
+ });
1082
+ return;
1083
+ }
1084
+ // --- 跳过该文件 ---
1085
+ events.onfileend?.();
1086
+ --writeFileLength;
1087
+ }
1062
1088
  if (writeFileLength) {
1063
1089
  // --- 文件没写完 ---
1064
1090
  return;