@modern-js/plugin-express 1.0.0 → 1.1.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/.eslintrc.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ ignorePatterns: ['types.d.ts'],
7
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # @modern-js/plugin-express
2
2
 
3
+ ## 1.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 0fa83663: support more .env files
8
+ - Updated dependencies [6f7fe574]
9
+ - Updated dependencies [0fa83663]
10
+ - Updated dependencies [f594fbc8]
11
+ - @modern-js/core@1.1.2
12
+ - @modern-js/adapter-helpers@1.1.1
13
+ - @modern-js/bff-runtime@1.1.1
14
+ - @modern-js/bff-utils@1.1.1
15
+ - @modern-js/server-plugin@1.1.1
16
+ - @modern-js/utils@1.1.2
17
+
18
+ ## 1.1.1
19
+
20
+ ### Patch Changes
21
+
22
+ - 6ffd1a50: Refactor to base on farrow-pipeline
23
+ - Updated dependencies [687c92c7]
24
+ - Updated dependencies [c0fc0700]
25
+ - @modern-js/core@1.1.1
26
+ - @modern-js/utils@1.1.1
27
+
28
+ ## 1.1.0
29
+
30
+ ### Minor Changes
31
+
32
+ - 96119db2: Relese v1.1.0
33
+
34
+ ### Patch Changes
35
+
36
+ - Updated dependencies [96119db2]
37
+ - @modern-js/core@1.1.0
38
+ - @modern-js/adapter-helpers@1.1.0
39
+ - @modern-js/bff-runtime@1.1.0
40
+ - @modern-js/bff-utils@1.1.0
41
+ - @modern-js/server-plugin@1.1.0
42
+ - @modern-js/utils@1.1.0
43
+
3
44
  ## 1.0.0
4
45
 
5
46
  ### Patch Changes
package/README.md CHANGED
@@ -17,10 +17,7 @@
17
17
 
18
18
  > The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.
19
19
 
20
- - 介绍 Modern.js (即将上线)
21
- - [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
22
- - [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
23
- - [字节跳动是如何落地微前端的](https://mp.weixin.qq.com/s/L9wbfNG5fTXF5bx7dcgj4Q)
20
+ - [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)
24
21
 
25
22
  ## Getting Started
26
23
 
@@ -1,6 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import { useAppContext, createPlugin } from '@modern-js/core';
3
- import { createRuntimeExportsUtils, upath } from '@modern-js/utils';
3
+ import { createRuntimeExportsUtils } from '@modern-js/utils';
4
4
  export default createPlugin(() => {
5
5
  let bffExportsUtils;
6
6
  const runtimeModulePath = path.resolve(__dirname, '../runtime');
@@ -18,13 +18,16 @@ export default createPlugin(() => {
18
18
  };
19
19
  },
20
20
 
21
- modifyEntryImports() {
21
+ modifyEntryImports(input) {
22
+ // eslint-disable-next-line react-hooks/rules-of-hooks
22
23
  const {
23
24
  appDirectory
24
25
  } = useAppContext();
25
- const runtimePath = upath.normalizeSafe(require.resolve(`@modern-js/runtime`, {
26
+
27
+ const runtimePath = require.resolve(`@modern-js/runtime`, {
26
28
  paths: [appDirectory]
27
- }));
29
+ });
30
+
28
31
  const currentFile = bffExportsUtils.getPath();
29
32
  const runtimeDir = path.dirname(runtimePath);
30
33
  const relativeBffPath = path.relative(path.dirname(currentFile), path.join(runtimeDir, './exports/server'));
@@ -39,6 +42,7 @@ export default createPlugin(() => {
39
42
  ...pluginRuntime
40
43
  }
41
44
  `);
45
+ return input;
42
46
  }
43
47
 
44
48
  };
@@ -11,7 +11,7 @@ const debug = createDebugger('express');
11
11
 
12
12
  const findAppModule = async apiDir => {
13
13
  const exts = ['.ts', '.js'];
14
- const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
14
+ const paths = exts.map(ext => path.resolve(apiDir, `app${ext}`));
15
15
 
16
16
  for (const filename of paths) {
17
17
  if (await fs.pathExists(filename)) {
@@ -62,6 +62,7 @@ export default createPlugin(() => ({
62
62
  app = await findAppModule(apiDir);
63
63
 
64
64
  if (!app || !app.use) {
65
+ console.warn('There is not api/app.ts.');
65
66
  app = express();
66
67
  }
67
68
 
@@ -99,10 +100,10 @@ export default createPlugin(() => ({
99
100
  const handler = err => {
100
101
  if (err) {
101
102
  return reject(err);
102
- }
103
+ } // finalhanlder will trigger 'finish' event
103
104
 
104
- finalhandler(req, res, {})(null);
105
- return resolve();
105
+
106
+ return finalhandler(req, res, {})(null); // return resolve();
106
107
  };
107
108
 
108
109
  res.on('finish', err => {
@@ -23,7 +23,6 @@ const registerRoutes = app => {
23
23
  method,
24
24
  name
25
25
  }) => {
26
- // eslint-disable-next-line max-statements
27
26
  const wrapedHandler = async (req, res, next) => {
28
27
  const input = await getInputFromRequest(req);
29
28
 
@@ -48,11 +47,16 @@ const registerRoutes = app => {
48
47
  try {
49
48
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
50
49
  // @ts-expect-error
51
- const body = await handler(...args);
50
+ const body = await handler(...args); // this should never happen
51
+
52
+ if (res.headersSent) {
53
+ return await Promise.resolve();
54
+ }
55
+
52
56
  res.status(200);
53
57
  return res.json(body);
54
58
  } catch (e) {
55
- next(e);
59
+ return next(e);
56
60
  }
57
61
  }
58
62
  };
@@ -32,14 +32,15 @@ var _default = (0, _core.createPlugin)(() => {
32
32
  };
33
33
  },
34
34
 
35
- modifyEntryImports() {
35
+ modifyEntryImports(input) {
36
+ // eslint-disable-next-line react-hooks/rules-of-hooks
36
37
  const {
37
38
  appDirectory
38
39
  } = (0, _core.useAppContext)();
39
40
 
40
- const runtimePath = _utils.upath.normalizeSafe(require.resolve(`@modern-js/runtime`, {
41
+ const runtimePath = require.resolve(`@modern-js/runtime`, {
41
42
  paths: [appDirectory]
42
- }));
43
+ });
43
44
 
44
45
  const currentFile = bffExportsUtils.getPath();
45
46
  const runtimeDir = path.dirname(runtimePath);
@@ -55,6 +56,7 @@ var _default = (0, _core.createPlugin)(() => {
55
56
  ...pluginRuntime
56
57
  }
57
58
  `);
59
+ return input;
58
60
  }
59
61
 
60
62
  };
@@ -33,7 +33,7 @@ const debug = (0, _utils.createDebugger)('express');
33
33
 
34
34
  const findAppModule = async apiDir => {
35
35
  const exts = ['.ts', '.js'];
36
- const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
36
+ const paths = exts.map(ext => path.resolve(apiDir, `app${ext}`));
37
37
 
38
38
  for (const filename of paths) {
39
39
  if (await _utils.fs.pathExists(filename)) {
@@ -84,6 +84,7 @@ var _default = (0, _serverPlugin.createPlugin)(() => ({
84
84
  app = await findAppModule(apiDir);
85
85
 
86
86
  if (!app || !app.use) {
87
+ console.warn('There is not api/app.ts.');
87
88
  app = (0, _express.default)();
88
89
  }
89
90
 
@@ -121,10 +122,10 @@ var _default = (0, _serverPlugin.createPlugin)(() => ({
121
122
  const handler = err => {
122
123
  if (err) {
123
124
  return reject(err);
124
- }
125
+ } // finalhanlder will trigger 'finish' event
125
126
 
126
- (0, _finalhandler.default)(req, res, {})(null);
127
- return resolve();
127
+
128
+ return (0, _finalhandler.default)(req, res, {})(null); // return resolve();
128
129
  };
129
130
 
130
131
  res.on('finish', err => {
@@ -38,7 +38,6 @@ const registerRoutes = app => {
38
38
  method,
39
39
  name
40
40
  }) => {
41
- // eslint-disable-next-line max-statements
42
41
  const wrapedHandler = async (req, res, next) => {
43
42
  const input = await getInputFromRequest(req);
44
43
 
@@ -63,11 +62,16 @@ const registerRoutes = app => {
63
62
  try {
64
63
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
65
64
  // @ts-expect-error
66
- const body = await handler(...args);
65
+ const body = await handler(...args); // this should never happen
66
+
67
+ if (res.headersSent) {
68
+ return await Promise.resolve();
69
+ }
70
+
67
71
  res.status(200);
68
72
  return res.json(body);
69
73
  } catch (e) {
70
- next(e);
74
+ return next(e);
71
75
  }
72
76
  }
73
77
  };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.0.0",
14
+ "version": "1.1.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -29,8 +29,8 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@babel/runtime": "^7.15.3",
32
- "@modern-js/adapter-helpers": "^1.0.0",
33
- "@modern-js/utils": "^1.0.0",
32
+ "@modern-js/adapter-helpers": "^1.1.1",
33
+ "@modern-js/utils": "^1.1.2",
34
34
  "cookie-parser": "^1.4.5",
35
35
  "finalhandler": "^1.1.2",
36
36
  "formidable": "^1.2.2",
@@ -38,8 +38,8 @@
38
38
  },
39
39
  "devDependencies": {
40
40
  "express": "^4.17.1",
41
- "@modern-js/server-utils": "^1.0.0",
42
- "@modern-js/bff-runtime": "^1.0.0",
41
+ "@modern-js/server-utils": "^1.1.1",
42
+ "@modern-js/bff-runtime": "^1.1.1",
43
43
  "@types/cookie-parser": "^1.4.2",
44
44
  "@types/express": "^4.17.13",
45
45
  "@types/finalhandler": "^1.1.1",
@@ -50,11 +50,11 @@
50
50
  "@types/type-is": "^1.6.3",
51
51
  "supertest": "^6.1.6",
52
52
  "typescript": "^4",
53
- "@modern-js/bff-utils": "^1.0.0",
54
- "@modern-js/core": "^1.0.0",
55
- "@modern-js/server-plugin": "^1.0.0",
56
- "@modern-js/plugin-testing": "^1.0.0",
57
- "@modern-js/module-tools": "^1.0.0"
53
+ "@modern-js/bff-utils": "^1.1.1",
54
+ "@modern-js/core": "^1.1.2",
55
+ "@modern-js/server-plugin": "^1.1.1",
56
+ "@modern-js/plugin-testing": "^1.1.0",
57
+ "@modern-js/module-tools": "^1.1.0"
58
58
  },
59
59
  "modernConfig": {
60
60
  "output": {
@@ -62,10 +62,10 @@
62
62
  }
63
63
  },
64
64
  "peerDependencies": {
65
- "@modern-js/bff-utils": "^1.0.0",
66
- "@modern-js/core": "^1.0.0",
67
- "@modern-js/server-plugin": "^1.0.0",
68
- "@modern-js/bff-runtime": "^1.0.0",
65
+ "@modern-js/bff-utils": "^1.1.1",
66
+ "@modern-js/core": "^1.1.2",
67
+ "@modern-js/server-plugin": "^1.1.1",
68
+ "@modern-js/bff-runtime": "^1.1.1",
69
69
  "express": "^4.17.1"
70
70
  },
71
71
  "publishConfig": {
package/src/cli/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import { useAppContext, createPlugin } from '@modern-js/core';
3
- import { createRuntimeExportsUtils, upath } from '@modern-js/utils';
3
+ import { createRuntimeExportsUtils } from '@modern-js/utils';
4
4
 
5
5
  export default createPlugin(
6
6
  () => {
@@ -22,11 +22,12 @@ export default createPlugin(
22
22
  },
23
23
  };
24
24
  },
25
- modifyEntryImports() {
25
+ modifyEntryImports(input) {
26
+ // eslint-disable-next-line react-hooks/rules-of-hooks
26
27
  const { appDirectory } = useAppContext();
27
- const runtimePath = upath.normalizeSafe(require.resolve(`@modern-js/runtime`, {
28
+ const runtimePath = require.resolve(`@modern-js/runtime`, {
28
29
  paths: [appDirectory],
29
- }));
30
+ });
30
31
 
31
32
  const currentFile = bffExportsUtils.getPath();
32
33
 
@@ -55,6 +56,7 @@ export default createPlugin(
55
56
  ...pluginRuntime
56
57
  }
57
58
  `);
59
+ return input;
58
60
  },
59
61
  };
60
62
  },
package/src/plugin.ts CHANGED
@@ -4,7 +4,7 @@ import type { Request, Response } from 'express';
4
4
  import cookieParser from 'cookie-parser';
5
5
  import { createPlugin } from '@modern-js/server-plugin';
6
6
  import { requireModule } from '@modern-js/bff-utils';
7
- import { fs, createDebugger, logger } from '@modern-js/utils';
7
+ import { fs, createDebugger } from '@modern-js/utils';
8
8
  import finalhandler from 'finalhandler';
9
9
  import { run } from './context';
10
10
  import registerRoutes from './registerRoutes';
@@ -19,7 +19,7 @@ export type Mode = 'function' | 'framework';
19
19
 
20
20
  const findAppModule = async (apiDir: string) => {
21
21
  const exts = ['.ts', '.js'];
22
- const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
22
+ const paths = exts.map(ext => path.resolve(apiDir, `app${ext}`));
23
23
 
24
24
  for (const filename of paths) {
25
25
  if (await fs.pathExists(filename)) {
@@ -67,6 +67,7 @@ export default createPlugin(
67
67
  app = await findAppModule(apiDir);
68
68
 
69
69
  if (!app || !app.use) {
70
+ console.warn('There is not api/app.ts.');
70
71
  app = express();
71
72
  }
72
73
  initApp(app);
@@ -102,8 +103,9 @@ export default createPlugin(
102
103
  if (err) {
103
104
  return reject(err);
104
105
  }
105
- finalhandler(req, res, {})(null);
106
- return resolve();
106
+ // finalhanlder will trigger 'finish' event
107
+ return finalhandler(req, res, {})(null);
108
+ // return resolve();
107
109
  };
108
110
 
109
111
  res.on('finish', (err: Error) => {
@@ -21,7 +21,6 @@ const registerRoutes = (app: Express) => {
21
21
  debug('handlerInfos', handlerInfos);
22
22
 
23
23
  handlerInfos.forEach(({ path, handler, method, name }) => {
24
- // eslint-disable-next-line max-statements
25
24
  const wrapedHandler: RequestHandler = async (
26
25
  req: Request,
27
26
  res: Response,
@@ -49,10 +48,16 @@ const registerRoutes = (app: Express) => {
49
48
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
50
49
  // @ts-expect-error
51
50
  const body = await handler(...args);
51
+
52
+ // this should never happen
53
+ if (res.headersSent) {
54
+ return await Promise.resolve();
55
+ }
56
+
52
57
  res.status(200);
53
58
  return res.json(body);
54
59
  } catch (e) {
55
- next(e);
60
+ return next(e);
56
61
  }
57
62
  }
58
63
  };
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ extends: ['@modern-js'],
3
+ parserOptions: {
4
+ project: require.resolve('./tsconfig.json'),
5
+ },
6
+ };
@@ -1,2 +1 @@
1
- // eslint-disable-next-line @typescript-eslint/require-await
2
1
  export const get = async () => ({ message: 'hello' });
package/tests/helpers.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { path } from '@modern-js/utils';
1
+ import path from 'path';
2
2
  import { createPlugin } from '@modern-js/server-plugin';
3
3
  import { injectAPIHandlerInfos, API_DIR } from '@modern-js/bff-utils';
4
4
 
@@ -1,4 +1,4 @@
1
- import { path } from '@modern-js/utils';
1
+ import path from 'path';
2
2
  import express, { Request, Response } from 'express';
3
3
  import request from 'supertest';
4
4
  import { serverManager } from '@modern-js/server-plugin';
@@ -281,6 +281,7 @@ describe('support app.ts in lambda mode', () => {
281
281
  const app = mock_express();
282
282
  app.get('/hello', (req, res) => {
283
283
  res.send(`foo`);
284
+ res.end();
284
285
  });
285
286
 
286
287
  return app;