@umijs/preset-umi 4.0.6 → 4.0.7

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.
@@ -78,6 +78,17 @@ umi preview --port [port]
78
78
  }));
79
79
  // history fallback
80
80
  app.use(require('@umijs/bundler-webpack/compiled/connect-history-api-fallback')());
81
+ // 如果是 browser,并且配置了非 / base,访问 / 时 /index.html redirect 到 base 路径
82
+ app.use((_req, res, next) => {
83
+ var _a;
84
+ const historyType = ((_a = api.config.history) === null || _a === void 0 ? void 0 : _a.type) || 'browser';
85
+ if (historyType === 'browser' &&
86
+ api.config.base !== '/' &&
87
+ (_req.path === '/' || _req.path === '/index.html')) {
88
+ return res.redirect(api.config.base);
89
+ }
90
+ next();
91
+ });
81
92
  // https 复用用户配置
82
93
  const server = api.userConfig.https
83
94
  ? await (0, bundler_utils_1.createHttpsServer)(app, api.userConfig.https)
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import type { IncomingMessage } from 'http';
3
4
  import type { IRoute } from '../../types';
4
5
  declare class UmiApiRequest {
@@ -22,4 +23,10 @@ declare class UmiApiRequest {
22
23
  get pathName(): string | undefined;
23
24
  readBody(): Promise<void>;
24
25
  }
26
+ export declare function parseMultipart(body: Buffer, boundary: string): {
27
+ [key: string]: any;
28
+ };
29
+ export declare function parseUrlEncoded(body: string): {
30
+ [key: string]: any;
31
+ };
25
32
  export default UmiApiRequest;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseUrlEncoded = exports.parseMultipart = void 0;
3
4
  const utils_1 = require("./utils");
4
5
  class UmiApiRequest {
5
6
  constructor(req, apiRoutes) {
@@ -61,21 +62,33 @@ class UmiApiRequest {
61
62
  return Promise.resolve();
62
63
  }
63
64
  return new Promise((resolve, reject) => {
64
- let body = '';
65
+ let body = [];
65
66
  this._req.on('data', (chunk) => {
66
- body += chunk;
67
+ body.push(chunk);
67
68
  });
68
69
  this._req.on('end', () => {
69
- // TODO: handle other content types
70
- switch (this._req.headers['content-type']) {
70
+ var _a, _b;
71
+ const bodyBuffer = Buffer.concat(body);
72
+ switch ((_a = this._req.headers['content-type']) === null || _a === void 0 ? void 0 : _a.split(';')[0]) {
71
73
  case 'application/json':
72
74
  try {
73
- this._body = JSON.parse(body);
75
+ this._body = JSON.parse(bodyBuffer.toString());
74
76
  }
75
77
  catch (e) {
76
78
  this._body = body;
77
79
  }
78
80
  break;
81
+ case 'multipart/form-data':
82
+ const boundary = (_b = this.headers['content-type']) === null || _b === void 0 ? void 0 : _b.split('boundary=')[1];
83
+ if (!boundary) {
84
+ this._body = body;
85
+ break;
86
+ }
87
+ this._body = parseMultipart(bodyBuffer, boundary);
88
+ break;
89
+ case 'application/x-www-form-urlencoded':
90
+ this._body = parseUrlEncoded(bodyBuffer.toString());
91
+ break;
79
92
  default:
80
93
  this._body = body;
81
94
  break;
@@ -86,4 +99,46 @@ class UmiApiRequest {
86
99
  });
87
100
  }
88
101
  }
102
+ function parseMultipart(body, boundary) {
103
+ const hexBoundary = Buffer.from(`--${boundary}`, 'utf-8').toString('hex');
104
+ return body
105
+ .toString('hex')
106
+ .split(hexBoundary)
107
+ .reduce((acc, cur) => {
108
+ var _a, _b;
109
+ const [hexMeta, hexValue] = cur.split(Buffer.from('\r\n\r\n').toString('hex'));
110
+ const meta = Buffer.from(hexMeta, 'hex').toString('utf-8');
111
+ const name = (_a = meta.split('name="')[1]) === null || _a === void 0 ? void 0 : _a.split('"')[0];
112
+ // if this part doesn't have name, skip it
113
+ if (!name)
114
+ return acc;
115
+ // if there is filename, this field is file, save as buffer
116
+ const fileName = (_b = meta.split('filename="')[1]) === null || _b === void 0 ? void 0 : _b.split('"')[0];
117
+ if (fileName) {
118
+ const fileBufferBeforeTrim = Buffer.from(hexValue, 'hex');
119
+ const fileBuffer = fileBufferBeforeTrim.slice(0, fileBufferBeforeTrim.byteLength - 2);
120
+ const contentType = meta.split('Content-Type: ')[1];
121
+ acc[name] = {
122
+ fileName,
123
+ data: fileBuffer,
124
+ contentType,
125
+ };
126
+ return acc;
127
+ }
128
+ // if there is no filename, this field is string, save as string
129
+ const valueBufferBeforeTrim = Buffer.from(hexValue, 'hex');
130
+ const valueBuffer = valueBufferBeforeTrim.slice(0, valueBufferBeforeTrim.byteLength - 2);
131
+ acc[name] = valueBuffer.toString('utf-8');
132
+ return acc;
133
+ }, {});
134
+ }
135
+ exports.parseMultipart = parseMultipart;
136
+ function parseUrlEncoded(body) {
137
+ return body.split('&').reduce((acc, cur) => {
138
+ const [key, value] = cur.split('=');
139
+ acc[key] = decodeURI(value);
140
+ return acc;
141
+ }, {});
142
+ }
143
+ exports.parseUrlEncoded = parseUrlEncoded;
89
144
  exports.default = UmiApiRequest;
@@ -6,6 +6,7 @@ declare class UmiApiResponse {
6
6
  status(statusCode: number): this;
7
7
  header(key: string, value: string): this;
8
8
  setCookie(key: string, value: string): this;
9
+ end(data: any): this;
9
10
  text(data: string): this;
10
11
  html(data: string): this;
11
12
  json(data: any): this;
@@ -16,6 +16,10 @@ class UmiApiResponse {
16
16
  this._res.setHeader('Set-Cookie', `${key}=${value}; path=/`);
17
17
  return this;
18
18
  }
19
+ end(data) {
20
+ this._res.end(data);
21
+ return this;
22
+ }
19
23
  text(data) {
20
24
  this._res.setHeader('Content-Type', 'text/plain; charset=utf-8');
21
25
  this._res.end(data);
@@ -29,6 +29,7 @@ function getMockData(opts) {
29
29
  const mockFile = `${opts.cwd}/${file}`;
30
30
  let m;
31
31
  try {
32
+ delete require.cache[mockFile];
32
33
  m = require(mockFile);
33
34
  }
34
35
  catch (e) {
@@ -52,9 +53,6 @@ function getMockData(opts) {
52
53
  }
53
54
  return memo;
54
55
  }, {});
55
- for (const file of utils_1.register.getFiles()) {
56
- delete require.cache[file];
57
- }
58
56
  utils_1.register.restore();
59
57
  return ret;
60
58
  }
@@ -128,6 +128,14 @@ declare module '*.gif' {
128
128
  export default src
129
129
  }
130
130
  declare module '*.svg' {
131
+ ${api.config.svgr
132
+ ? `
133
+ import * as React from 'react';
134
+ export const ReactComponent: React.FunctionComponent<React.SVGProps<
135
+ SVGSVGElement
136
+ > & { title?: string }>;
137
+ `.trimStart()
138
+ : ''}
131
139
  const src: string
132
140
  export default src
133
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/preset-umi",
3
- "version": "4.0.6",
3
+ "version": "4.0.7",
4
4
  "description": "@umijs/preset-umi",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/preset-umi#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",
@@ -26,15 +26,15 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@types/multer": "1.4.7",
29
- "@umijs/ast": "4.0.6",
30
- "@umijs/babel-preset-umi": "4.0.6",
31
- "@umijs/bundler-utils": "4.0.6",
32
- "@umijs/bundler-vite": "4.0.6",
33
- "@umijs/bundler-webpack": "4.0.6",
34
- "@umijs/core": "4.0.6",
35
- "@umijs/renderer-react": "4.0.6",
36
- "@umijs/server": "4.0.6",
37
- "@umijs/utils": "4.0.6",
29
+ "@umijs/ast": "4.0.7",
30
+ "@umijs/babel-preset-umi": "4.0.7",
31
+ "@umijs/bundler-utils": "4.0.7",
32
+ "@umijs/bundler-vite": "4.0.7",
33
+ "@umijs/bundler-webpack": "4.0.7",
34
+ "@umijs/core": "4.0.7",
35
+ "@umijs/renderer-react": "4.0.7",
36
+ "@umijs/server": "4.0.7",
37
+ "@umijs/utils": "4.0.7",
38
38
  "click-to-react-component": "^1.0.8",
39
39
  "core-js": "3.22.4",
40
40
  "current-script-polyfill": "1.0.0",
@@ -7,7 +7,7 @@ export function createHistory(opts: any) {
7
7
  if (opts.type === 'hash') {
8
8
  h = createHashHistory();
9
9
  } else if (opts.type === 'memory') {
10
- h = createMemoryHistory();
10
+ h = createMemoryHistory(opts);
11
11
  } else {
12
12
  h = createBrowserHistory();
13
13
  }
package/templates/umi.tpl CHANGED
@@ -51,8 +51,9 @@ async function render() {
51
51
  publicPath,
52
52
  runtimePublicPath,
53
53
  history: createHistory({
54
- type: '{{{ historyType }}}',
54
+ type: contextOpts.historyType || '{{{ historyType }}}',
55
55
  basename,
56
+ ...contextOpts.historyOpts,
56
57
  }),
57
58
  basename,
58
59
  };