@nocobase/plugin-field-sort 1.5.0-alpha.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.
Files changed (142) hide show
  1. package/LICENSE.txt +159 -0
  2. package/README.md +1 -0
  3. package/client.d.ts +2 -0
  4. package/client.js +1 -0
  5. package/dist/client/index.d.ts +15 -0
  6. package/dist/client/index.js +10 -0
  7. package/dist/client/locale.d.ts +10 -0
  8. package/dist/client/sort-interface.d.ts +131 -0
  9. package/dist/externalVersion.js +18 -0
  10. package/dist/index.d.ts +10 -0
  11. package/dist/index.js +48 -0
  12. package/dist/locale/en-US.json +1 -0
  13. package/dist/locale/zh-CN.json +1 -0
  14. package/dist/node_modules/@nocobase/lock-manager/LICENSE +661 -0
  15. package/dist/node_modules/@nocobase/lock-manager/lib/index.d.ts +10 -0
  16. package/dist/node_modules/@nocobase/lock-manager/lib/index.js +1 -0
  17. package/dist/node_modules/@nocobase/lock-manager/lib/lock-manager.d.ts +46 -0
  18. package/dist/node_modules/@nocobase/lock-manager/lib/lock-manager.js +171 -0
  19. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/client.js +12 -0
  20. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/assign.d.ts +17 -0
  21. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/assign.js +143 -0
  22. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/client.d.ts +30 -0
  23. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/client.js +88 -0
  24. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/collections-graph.d.ts +24 -0
  25. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/collections-graph.js +107 -0
  26. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/common.d.ts +14 -0
  27. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/common.js +87 -0
  28. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/date.d.ts +38 -0
  29. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/date.js +214 -0
  30. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/dayjs.d.ts +10 -0
  31. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/dayjs.js +72 -0
  32. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/forEach.d.ts +9 -0
  33. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/forEach.js +45 -0
  34. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/fs-exists.d.ts +9 -0
  35. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/fs-exists.js +46 -0
  36. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/getValuesByPath.d.ts +9 -0
  37. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/getValuesByPath.js +71 -0
  38. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/handlebars.d.ts +10 -0
  39. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/handlebars.js +73 -0
  40. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/i18n.d.ts +10 -0
  41. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/i18n.js +43 -0
  42. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/index.d.ts +38 -0
  43. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/index.js +105 -0
  44. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/isPortalInBody.d.ts +13 -0
  45. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/isPortalInBody.js +54 -0
  46. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/isValidFilter.d.ts +9 -0
  47. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/isValidFilter.js +63 -0
  48. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/json-templates.d.ts +9 -0
  49. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/json-templates.js +141 -0
  50. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/koa-multer.d.ts +15 -0
  51. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/koa-multer.js +92 -0
  52. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/log.d.ts +9 -0
  53. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/log.js +39 -0
  54. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/measure-execution-time.d.ts +9 -0
  55. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/measure-execution-time.js +44 -0
  56. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/merge.d.ts +9 -0
  57. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/merge.js +55 -0
  58. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/mixin/AsyncEmitter.d.ts +11 -0
  59. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/mixin/AsyncEmitter.js +80 -0
  60. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/mixin/index.d.ts +9 -0
  61. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/mixin/index.js +48 -0
  62. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/notification.d.ts +9 -0
  63. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/notification.js +43 -0
  64. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/number.d.ts +9 -0
  65. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/number.js +45 -0
  66. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parse-date.d.ts +15 -0
  67. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parse-date.js +262 -0
  68. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parse-filter.d.ts +67 -0
  69. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parse-filter.js +320 -0
  70. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parseHTML.d.ts +15 -0
  71. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/parseHTML.js +42 -0
  72. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/perf-hooks.d.ts +14 -0
  73. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/perf-hooks.js +69 -0
  74. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/registry.d.ts +22 -0
  75. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/registry.js +69 -0
  76. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/requireModule.d.ts +12 -0
  77. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/requireModule.js +90 -0
  78. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/server.d.ts +19 -0
  79. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/server.js +50 -0
  80. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/toposort.d.ts +18 -0
  81. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/toposort.js +83 -0
  82. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/uid.d.ts +9 -0
  83. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/uid.js +44 -0
  84. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/url.d.ts +14 -0
  85. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/url.js +46 -0
  86. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/wrap-middleware.d.ts +1 -0
  87. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/lib/wrap-middleware.js +63 -0
  88. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/package.json +20 -0
  89. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/plugin-symlink.d.ts +6 -0
  90. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/plugin-symlink.js +102 -0
  91. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/server.d.ts +2 -0
  92. package/dist/node_modules/@nocobase/lock-manager/node_modules/@nocobase/utils/server.js +12 -0
  93. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/.eslintrc.js +5 -0
  94. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/.github/workflows/ci.yml +24 -0
  95. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/.github/workflows/lint.yml +23 -0
  96. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-multipart-fields-100mb-big.js +149 -0
  97. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-multipart-fields-100mb-small.js +143 -0
  98. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-multipart-files-100mb-big.js +154 -0
  99. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-multipart-files-100mb-small.js +148 -0
  100. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-urlencoded-fields-100pairs-small.js +101 -0
  101. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/bench/bench-urlencoded-fields-900pairs-small-alt.js +84 -0
  102. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/lib/index.js +57 -0
  103. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/lib/types/multipart.js +653 -0
  104. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/lib/types/urlencoded.js +350 -0
  105. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/lib/utils.js +596 -0
  106. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/package.json +22 -0
  107. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/common.js +109 -0
  108. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/test-types-multipart-charsets.js +94 -0
  109. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/test-types-multipart-stream-pause.js +102 -0
  110. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/test-types-multipart.js +1053 -0
  111. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/test-types-urlencoded.js +488 -0
  112. package/dist/node_modules/@nocobase/lock-manager/node_modules/busboy/test/test.js +20 -0
  113. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/index.js +104 -0
  114. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/lib/counter.js +28 -0
  115. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/lib/file-appender.js +67 -0
  116. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/lib/make-middleware.js +173 -0
  117. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/lib/multer-error.js +24 -0
  118. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/lib/remove-uploaded-files.js +28 -0
  119. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/package.json +52 -0
  120. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/storage/disk.js +66 -0
  121. package/dist/node_modules/@nocobase/lock-manager/node_modules/multer/storage/memory.js +21 -0
  122. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/.eslintrc.js +5 -0
  123. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/.github/workflows/ci.yml +24 -0
  124. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/.github/workflows/lint.yml +23 -0
  125. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/lib/sbmh.js +267 -0
  126. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/package.json +34 -0
  127. package/dist/node_modules/@nocobase/lock-manager/node_modules/streamsearch/test/test.js +70 -0
  128. package/dist/node_modules/@nocobase/lock-manager/package.json +1 -0
  129. package/dist/node_modules/@nocobase/lock-manager/src/__tests__/lock-manager.test.ts +169 -0
  130. package/dist/node_modules/@nocobase/lock-manager/src/index.ts +11 -0
  131. package/dist/node_modules/@nocobase/lock-manager/src/lock-manager.ts +169 -0
  132. package/dist/server/action.d.ts +26 -0
  133. package/dist/server/action.js +168 -0
  134. package/dist/server/index.d.ts +10 -0
  135. package/dist/server/index.js +48 -0
  136. package/dist/server/plugin.d.ts +19 -0
  137. package/dist/server/plugin.js +69 -0
  138. package/dist/server/sort-field.d.ts +24 -0
  139. package/dist/server/sort-field.js +197 -0
  140. package/package.json +16 -0
  141. package/server.d.ts +2 -0
  142. package/server.js +1 -0
@@ -0,0 +1,653 @@
1
+ 'use strict';
2
+
3
+ const { Readable, Writable } = require('stream');
4
+
5
+ const StreamSearch = require('streamsearch');
6
+
7
+ const {
8
+ basename,
9
+ convertToUTF8,
10
+ getDecoder,
11
+ parseContentType,
12
+ parseDisposition,
13
+ } = require('../utils.js');
14
+
15
+ const BUF_CRLF = Buffer.from('\r\n');
16
+ const BUF_CR = Buffer.from('\r');
17
+ const BUF_DASH = Buffer.from('-');
18
+
19
+ function noop() {}
20
+
21
+ const MAX_HEADER_PAIRS = 2000; // From node
22
+ const MAX_HEADER_SIZE = 16 * 1024; // From node (its default value)
23
+
24
+ const HPARSER_NAME = 0;
25
+ const HPARSER_PRE_OWS = 1;
26
+ const HPARSER_VALUE = 2;
27
+ class HeaderParser {
28
+ constructor(cb) {
29
+ this.header = Object.create(null);
30
+ this.pairCount = 0;
31
+ this.byteCount = 0;
32
+ this.state = HPARSER_NAME;
33
+ this.name = '';
34
+ this.value = '';
35
+ this.crlf = 0;
36
+ this.cb = cb;
37
+ }
38
+
39
+ reset() {
40
+ this.header = Object.create(null);
41
+ this.pairCount = 0;
42
+ this.byteCount = 0;
43
+ this.state = HPARSER_NAME;
44
+ this.name = '';
45
+ this.value = '';
46
+ this.crlf = 0;
47
+ }
48
+
49
+ push(chunk, pos, end) {
50
+ let start = pos;
51
+ while (pos < end) {
52
+ switch (this.state) {
53
+ case HPARSER_NAME: {
54
+ let done = false;
55
+ for (; pos < end; ++pos) {
56
+ if (this.byteCount === MAX_HEADER_SIZE)
57
+ return -1;
58
+ ++this.byteCount;
59
+ const code = chunk[pos];
60
+ if (TOKEN[code] !== 1) {
61
+ if (code !== 58/* ':' */)
62
+ return -1;
63
+ this.name += chunk.latin1Slice(start, pos);
64
+ if (this.name.length === 0)
65
+ return -1;
66
+ ++pos;
67
+ done = true;
68
+ this.state = HPARSER_PRE_OWS;
69
+ break;
70
+ }
71
+ }
72
+ if (!done) {
73
+ this.name += chunk.latin1Slice(start, pos);
74
+ break;
75
+ }
76
+ // FALLTHROUGH
77
+ }
78
+ case HPARSER_PRE_OWS: {
79
+ // Skip optional whitespace
80
+ let done = false;
81
+ for (; pos < end; ++pos) {
82
+ if (this.byteCount === MAX_HEADER_SIZE)
83
+ return -1;
84
+ ++this.byteCount;
85
+ const code = chunk[pos];
86
+ if (code !== 32/* ' ' */ && code !== 9/* '\t' */) {
87
+ start = pos;
88
+ done = true;
89
+ this.state = HPARSER_VALUE;
90
+ break;
91
+ }
92
+ }
93
+ if (!done)
94
+ break;
95
+ // FALLTHROUGH
96
+ }
97
+ case HPARSER_VALUE:
98
+ switch (this.crlf) {
99
+ case 0: // Nothing yet
100
+ for (; pos < end; ++pos) {
101
+ if (this.byteCount === MAX_HEADER_SIZE)
102
+ return -1;
103
+ ++this.byteCount;
104
+ const code = chunk[pos];
105
+ if (FIELD_VCHAR[code] !== 1) {
106
+ if (code !== 13/* '\r' */)
107
+ return -1;
108
+ ++this.crlf;
109
+ break;
110
+ }
111
+ }
112
+ this.value += chunk.latin1Slice(start, pos++);
113
+ break;
114
+ case 1: // Received CR
115
+ if (this.byteCount === MAX_HEADER_SIZE)
116
+ return -1;
117
+ ++this.byteCount;
118
+ if (chunk[pos++] !== 10/* '\n' */)
119
+ return -1;
120
+ ++this.crlf;
121
+ break;
122
+ case 2: { // Received CR LF
123
+ if (this.byteCount === MAX_HEADER_SIZE)
124
+ return -1;
125
+ ++this.byteCount;
126
+ const code = chunk[pos];
127
+ if (code === 32/* ' ' */ || code === 9/* '\t' */) {
128
+ // Folded value
129
+ start = pos;
130
+ this.crlf = 0;
131
+ } else {
132
+ if (++this.pairCount < MAX_HEADER_PAIRS) {
133
+ this.name = this.name.toLowerCase();
134
+ if (this.header[this.name] === undefined)
135
+ this.header[this.name] = [this.value];
136
+ else
137
+ this.header[this.name].push(this.value);
138
+ }
139
+ if (code === 13/* '\r' */) {
140
+ ++this.crlf;
141
+ ++pos;
142
+ } else {
143
+ // Assume start of next header field name
144
+ start = pos;
145
+ this.crlf = 0;
146
+ this.state = HPARSER_NAME;
147
+ this.name = '';
148
+ this.value = '';
149
+ }
150
+ }
151
+ break;
152
+ }
153
+ case 3: { // Received CR LF CR
154
+ if (this.byteCount === MAX_HEADER_SIZE)
155
+ return -1;
156
+ ++this.byteCount;
157
+ if (chunk[pos++] !== 10/* '\n' */)
158
+ return -1;
159
+ // End of header
160
+ const header = this.header;
161
+ this.reset();
162
+ this.cb(header);
163
+ return pos;
164
+ }
165
+ }
166
+ break;
167
+ }
168
+ }
169
+
170
+ return pos;
171
+ }
172
+ }
173
+
174
+ class FileStream extends Readable {
175
+ constructor(opts, owner) {
176
+ super(opts);
177
+ this.truncated = false;
178
+ this._readcb = null;
179
+ this.once('end', () => {
180
+ // We need to make sure that we call any outstanding _writecb() that is
181
+ // associated with this file so that processing of the rest of the form
182
+ // can continue. This may not happen if the file stream ends right after
183
+ // backpressure kicks in, so we force it here.
184
+ this._read();
185
+ if (--owner._fileEndsLeft === 0 && owner._finalcb) {
186
+ const cb = owner._finalcb;
187
+ owner._finalcb = null;
188
+ // Make sure other 'end' event handlers get a chance to be executed
189
+ // before busboy's 'finish' event is emitted
190
+ process.nextTick(cb);
191
+ }
192
+ });
193
+ }
194
+ _read(n) {
195
+ const cb = this._readcb;
196
+ if (cb) {
197
+ this._readcb = null;
198
+ cb();
199
+ }
200
+ }
201
+ }
202
+
203
+ const ignoreData = {
204
+ push: (chunk, pos) => {},
205
+ destroy: () => {},
206
+ };
207
+
208
+ function callAndUnsetCb(self, err) {
209
+ const cb = self._writecb;
210
+ self._writecb = null;
211
+ if (err)
212
+ self.destroy(err);
213
+ else if (cb)
214
+ cb();
215
+ }
216
+
217
+ function nullDecoder(val, hint) {
218
+ return val;
219
+ }
220
+
221
+ class Multipart extends Writable {
222
+ constructor(cfg) {
223
+ const streamOpts = {
224
+ autoDestroy: true,
225
+ emitClose: true,
226
+ highWaterMark: (typeof cfg.highWaterMark === 'number'
227
+ ? cfg.highWaterMark
228
+ : undefined),
229
+ };
230
+ super(streamOpts);
231
+
232
+ if (!cfg.conType.params || typeof cfg.conType.params.boundary !== 'string')
233
+ throw new Error('Multipart: Boundary not found');
234
+
235
+ const boundary = cfg.conType.params.boundary;
236
+ const paramDecoder = (typeof cfg.defParamCharset === 'string'
237
+ && cfg.defParamCharset
238
+ ? getDecoder(cfg.defParamCharset)
239
+ : nullDecoder);
240
+ const defCharset = (cfg.defCharset || 'utf8');
241
+ const preservePath = cfg.preservePath;
242
+ const fileOpts = {
243
+ autoDestroy: true,
244
+ emitClose: true,
245
+ highWaterMark: (typeof cfg.fileHwm === 'number'
246
+ ? cfg.fileHwm
247
+ : undefined),
248
+ };
249
+
250
+ const limits = cfg.limits;
251
+ const fieldSizeLimit = (limits && typeof limits.fieldSize === 'number'
252
+ ? limits.fieldSize
253
+ : 1 * 1024 * 1024);
254
+ const fileSizeLimit = (limits && typeof limits.fileSize === 'number'
255
+ ? limits.fileSize
256
+ : Infinity);
257
+ const filesLimit = (limits && typeof limits.files === 'number'
258
+ ? limits.files
259
+ : Infinity);
260
+ const fieldsLimit = (limits && typeof limits.fields === 'number'
261
+ ? limits.fields
262
+ : Infinity);
263
+ const partsLimit = (limits && typeof limits.parts === 'number'
264
+ ? limits.parts
265
+ : Infinity);
266
+
267
+ let parts = -1; // Account for initial boundary
268
+ let fields = 0;
269
+ let files = 0;
270
+ let skipPart = false;
271
+
272
+ this._fileEndsLeft = 0;
273
+ this._fileStream = undefined;
274
+ this._complete = false;
275
+ let fileSize = 0;
276
+
277
+ let field;
278
+ let fieldSize = 0;
279
+ let partCharset;
280
+ let partEncoding;
281
+ let partType;
282
+ let partName;
283
+ let partTruncated = false;
284
+
285
+ let hitFilesLimit = false;
286
+ let hitFieldsLimit = false;
287
+
288
+ this._hparser = null;
289
+ const hparser = new HeaderParser((header) => {
290
+ this._hparser = null;
291
+ skipPart = false;
292
+
293
+ partType = 'text/plain';
294
+ partCharset = defCharset;
295
+ partEncoding = '7bit';
296
+ partName = undefined;
297
+ partTruncated = false;
298
+
299
+ let filename;
300
+ if (!header['content-disposition']) {
301
+ skipPart = true;
302
+ return;
303
+ }
304
+
305
+ const disp = parseDisposition(header['content-disposition'][0],
306
+ paramDecoder);
307
+ if (!disp || disp.type !== 'form-data') {
308
+ skipPart = true;
309
+ return;
310
+ }
311
+
312
+ if (disp.params) {
313
+ if (disp.params.name)
314
+ partName = disp.params.name;
315
+
316
+ if (disp.params['filename*'])
317
+ filename = disp.params['filename*'];
318
+ else if (disp.params.filename)
319
+ filename = disp.params.filename;
320
+
321
+ if (filename !== undefined && !preservePath)
322
+ filename = basename(filename);
323
+ }
324
+
325
+ if (header['content-type']) {
326
+ const conType = parseContentType(header['content-type'][0]);
327
+ if (conType) {
328
+ partType = `${conType.type}/${conType.subtype}`;
329
+ if (conType.params && typeof conType.params.charset === 'string')
330
+ partCharset = conType.params.charset.toLowerCase();
331
+ }
332
+ }
333
+
334
+ if (header['content-transfer-encoding'])
335
+ partEncoding = header['content-transfer-encoding'][0].toLowerCase();
336
+
337
+ if (partType === 'application/octet-stream' || filename !== undefined) {
338
+ // File
339
+
340
+ if (files === filesLimit) {
341
+ if (!hitFilesLimit) {
342
+ hitFilesLimit = true;
343
+ this.emit('filesLimit');
344
+ }
345
+ skipPart = true;
346
+ return;
347
+ }
348
+ ++files;
349
+
350
+ if (this.listenerCount('file') === 0) {
351
+ skipPart = true;
352
+ return;
353
+ }
354
+
355
+ fileSize = 0;
356
+ this._fileStream = new FileStream(fileOpts, this);
357
+ ++this._fileEndsLeft;
358
+ this.emit(
359
+ 'file',
360
+ partName,
361
+ this._fileStream,
362
+ { filename,
363
+ encoding: partEncoding,
364
+ mimeType: partType }
365
+ );
366
+ } else {
367
+ // Non-file
368
+
369
+ if (fields === fieldsLimit) {
370
+ if (!hitFieldsLimit) {
371
+ hitFieldsLimit = true;
372
+ this.emit('fieldsLimit');
373
+ }
374
+ skipPart = true;
375
+ return;
376
+ }
377
+ ++fields;
378
+
379
+ if (this.listenerCount('field') === 0) {
380
+ skipPart = true;
381
+ return;
382
+ }
383
+
384
+ field = [];
385
+ fieldSize = 0;
386
+ }
387
+ });
388
+
389
+ let matchPostBoundary = 0;
390
+ const ssCb = (isMatch, data, start, end, isDataSafe) => {
391
+ retrydata:
392
+ while (data) {
393
+ if (this._hparser !== null) {
394
+ const ret = this._hparser.push(data, start, end);
395
+ if (ret === -1) {
396
+ this._hparser = null;
397
+ hparser.reset();
398
+ this.emit('error', new Error('Malformed part header'));
399
+ break;
400
+ }
401
+ start = ret;
402
+ }
403
+
404
+ if (start === end)
405
+ break;
406
+
407
+ if (matchPostBoundary !== 0) {
408
+ if (matchPostBoundary === 1) {
409
+ switch (data[start]) {
410
+ case 45: // '-'
411
+ // Try matching '--' after boundary
412
+ matchPostBoundary = 2;
413
+ ++start;
414
+ break;
415
+ case 13: // '\r'
416
+ // Try matching CR LF before header
417
+ matchPostBoundary = 3;
418
+ ++start;
419
+ break;
420
+ default:
421
+ matchPostBoundary = 0;
422
+ }
423
+ if (start === end)
424
+ return;
425
+ }
426
+
427
+ if (matchPostBoundary === 2) {
428
+ matchPostBoundary = 0;
429
+ if (data[start] === 45/* '-' */) {
430
+ // End of multipart data
431
+ this._complete = true;
432
+ this._bparser = ignoreData;
433
+ return;
434
+ }
435
+ // We saw something other than '-', so put the dash we consumed
436
+ // "back"
437
+ const writecb = this._writecb;
438
+ this._writecb = noop;
439
+ ssCb(false, BUF_DASH, 0, 1, false);
440
+ this._writecb = writecb;
441
+ } else if (matchPostBoundary === 3) {
442
+ matchPostBoundary = 0;
443
+ if (data[start] === 10/* '\n' */) {
444
+ ++start;
445
+ if (parts >= partsLimit)
446
+ break;
447
+ // Prepare the header parser
448
+ this._hparser = hparser;
449
+ if (start === end)
450
+ break;
451
+ // Process the remaining data as a header
452
+ continue retrydata;
453
+ } else {
454
+ // We saw something other than LF, so put the CR we consumed
455
+ // "back"
456
+ const writecb = this._writecb;
457
+ this._writecb = noop;
458
+ ssCb(false, BUF_CR, 0, 1, false);
459
+ this._writecb = writecb;
460
+ }
461
+ }
462
+ }
463
+
464
+ if (!skipPart) {
465
+ if (this._fileStream) {
466
+ let chunk;
467
+ const actualLen = Math.min(end - start, fileSizeLimit - fileSize);
468
+ if (!isDataSafe) {
469
+ chunk = Buffer.allocUnsafe(actualLen);
470
+ data.copy(chunk, 0, start, start + actualLen);
471
+ } else {
472
+ chunk = data.slice(start, start + actualLen);
473
+ }
474
+
475
+ fileSize += chunk.length;
476
+ if (fileSize === fileSizeLimit) {
477
+ if (chunk.length > 0)
478
+ this._fileStream.push(chunk);
479
+ this._fileStream.emit('limit');
480
+ this._fileStream.truncated = true;
481
+ skipPart = true;
482
+ } else if (!this._fileStream.push(chunk)) {
483
+ if (this._writecb)
484
+ this._fileStream._readcb = this._writecb;
485
+ this._writecb = null;
486
+ }
487
+ } else if (field !== undefined) {
488
+ let chunk;
489
+ const actualLen = Math.min(
490
+ end - start,
491
+ fieldSizeLimit - fieldSize
492
+ );
493
+ if (!isDataSafe) {
494
+ chunk = Buffer.allocUnsafe(actualLen);
495
+ data.copy(chunk, 0, start, start + actualLen);
496
+ } else {
497
+ chunk = data.slice(start, start + actualLen);
498
+ }
499
+
500
+ fieldSize += actualLen;
501
+ field.push(chunk);
502
+ if (fieldSize === fieldSizeLimit) {
503
+ skipPart = true;
504
+ partTruncated = true;
505
+ }
506
+ }
507
+ }
508
+
509
+ break;
510
+ }
511
+
512
+ if (isMatch) {
513
+ matchPostBoundary = 1;
514
+
515
+ if (this._fileStream) {
516
+ // End the active file stream if the previous part was a file
517
+ this._fileStream.push(null);
518
+ this._fileStream = null;
519
+ } else if (field !== undefined) {
520
+ let data;
521
+ switch (field.length) {
522
+ case 0:
523
+ data = '';
524
+ break;
525
+ case 1:
526
+ data = convertToUTF8(field[0], partCharset, 0);
527
+ break;
528
+ default:
529
+ data = convertToUTF8(
530
+ Buffer.concat(field, fieldSize),
531
+ partCharset,
532
+ 0
533
+ );
534
+ }
535
+ field = undefined;
536
+ fieldSize = 0;
537
+ this.emit(
538
+ 'field',
539
+ partName,
540
+ data,
541
+ { nameTruncated: false,
542
+ valueTruncated: partTruncated,
543
+ encoding: partEncoding,
544
+ mimeType: partType }
545
+ );
546
+ }
547
+
548
+ if (++parts === partsLimit)
549
+ this.emit('partsLimit');
550
+ }
551
+ };
552
+ this._bparser = new StreamSearch(`\r\n--${boundary}`, ssCb);
553
+
554
+ this._writecb = null;
555
+ this._finalcb = null;
556
+
557
+ // Just in case there is no preamble
558
+ this.write(BUF_CRLF);
559
+ }
560
+
561
+ static detect(conType) {
562
+ return (conType.type === 'multipart' && conType.subtype === 'form-data');
563
+ }
564
+
565
+ _write(chunk, enc, cb) {
566
+ this._writecb = cb;
567
+ this._bparser.push(chunk, 0);
568
+ if (this._writecb)
569
+ callAndUnsetCb(this);
570
+ }
571
+
572
+ _destroy(err, cb) {
573
+ this._hparser = null;
574
+ this._bparser = ignoreData;
575
+ if (!err)
576
+ err = checkEndState(this);
577
+ const fileStream = this._fileStream;
578
+ if (fileStream) {
579
+ this._fileStream = null;
580
+ fileStream.destroy(err);
581
+ }
582
+ cb(err);
583
+ }
584
+
585
+ _final(cb) {
586
+ this._bparser.destroy();
587
+ if (!this._complete)
588
+ return cb(new Error('Unexpected end of form'));
589
+ if (this._fileEndsLeft)
590
+ this._finalcb = finalcb.bind(null, this, cb);
591
+ else
592
+ finalcb(this, cb);
593
+ }
594
+ }
595
+
596
+ function finalcb(self, cb, err) {
597
+ if (err)
598
+ return cb(err);
599
+ err = checkEndState(self);
600
+ cb(err);
601
+ }
602
+
603
+ function checkEndState(self) {
604
+ if (self._hparser)
605
+ return new Error('Malformed part header');
606
+ const fileStream = self._fileStream;
607
+ if (fileStream) {
608
+ self._fileStream = null;
609
+ fileStream.destroy(new Error('Unexpected end of file'));
610
+ }
611
+ if (!self._complete)
612
+ return new Error('Unexpected end of form');
613
+ }
614
+
615
+ const TOKEN = [
616
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
617
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
618
+ 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0,
619
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
620
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
621
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
622
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
623
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
624
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
625
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
626
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
627
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
628
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
629
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
630
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
631
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
632
+ ];
633
+
634
+ const FIELD_VCHAR = [
635
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
636
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
637
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
638
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
639
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
640
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
641
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
642
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
643
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
644
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
645
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
646
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
647
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
648
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
649
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
650
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
651
+ ];
652
+
653
+ module.exports = Multipart;