@opensumi/ide-express-file-server 2.21.6 → 2.21.7-rc-1669780664.0

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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@opensumi/ide-express-file-server",
3
- "version": "2.21.6",
3
+ "version": "2.21.7-rc-1669780664.0",
4
4
  "files": [
5
- "lib"
5
+ "lib",
6
+ "src"
6
7
  ],
7
8
  "license": "MIT",
8
9
  "main": "lib/index.js",
@@ -16,16 +17,16 @@
16
17
  "url": "git@github.com:opensumi/core.git"
17
18
  },
18
19
  "dependencies": {
19
- "@opensumi/ide-core-common": "2.21.6",
20
- "@opensumi/ide-core-node": "2.21.6",
21
- "@opensumi/ide-static-resource": "2.21.6",
20
+ "@opensumi/ide-core-common": "2.21.7-rc-1669780664.0",
21
+ "@opensumi/ide-core-node": "2.21.7-rc-1669780664.0",
22
+ "@opensumi/ide-static-resource": "2.21.7-rc-1669780664.0",
22
23
  "koa-mount": "^4.0.0"
23
24
  },
24
25
  "devDependencies": {
25
- "@opensumi/ide-core-browser": "2.21.6",
26
+ "@opensumi/ide-core-browser": "2.21.7-rc-1669780664.0",
26
27
  "@opensumi/ide-dev-tool": "^1.3.1",
27
28
  "@types/koa-mount": "^4.0.1",
28
29
  "superagent": "^5.1.0"
29
30
  },
30
- "gitHead": "a34917dfa6d851e067a431ad700812e58e8cc8a8"
31
+ "gitHead": "a997e49dfa53179486d2148dfe336112acbb3a09"
31
32
  }
@@ -0,0 +1,32 @@
1
+ import { Autowired } from '@opensumi/di';
2
+ import { Domain, URI, AppConfig, Schemes } from '@opensumi/ide-core-browser';
3
+ import {
4
+ StaticResourceContribution,
5
+ StaticResourceService,
6
+ } from '@opensumi/ide-static-resource/lib/browser/static.definition';
7
+
8
+ import { EXPRESS_SERVER_PATH } from '../common';
9
+
10
+ @Domain(StaticResourceContribution)
11
+ export class ExpressFileServerContribution implements StaticResourceContribution {
12
+ @Autowired(AppConfig)
13
+ appConfig: AppConfig;
14
+
15
+ registerStaticResolver(service: StaticResourceService): void {
16
+ service.registerStaticResourceProvider({
17
+ scheme: Schemes.file,
18
+ resolveStaticResource: (uri: URI) => {
19
+ // file 协议统一走静态服务
20
+ // http://${HOST}:8000/assets/${path}
21
+ const assetsUri = new URI(this.appConfig.staticServicePath || EXPRESS_SERVER_PATH);
22
+ /**
23
+ * uri.path 在 Windows 下会被解析为 /c:/Path/to/file
24
+ * fsPath C:\\Path\\to\\file
25
+ */
26
+ const [resourceUrl, query] = uri.codeUri.path.split('?');
27
+ return assetsUri.resolve(`assets${decodeURIComponent(resourceUrl)}`).withQuery(query);
28
+ },
29
+ roots: [this.appConfig.staticServicePath || EXPRESS_SERVER_PATH],
30
+ });
31
+ }
32
+ }
@@ -0,0 +1,9 @@
1
+ import { Provider, Injectable } from '@opensumi/di';
2
+ import { BrowserModule } from '@opensumi/ide-core-browser';
3
+
4
+ import { ExpressFileServerContribution } from './file-server.contribution';
5
+
6
+ @Injectable()
7
+ export class ExpressFileServerModule extends BrowserModule {
8
+ providers: Provider[] = [ExpressFileServerContribution];
9
+ }
@@ -0,0 +1,20 @@
1
+ export const EXPRESS_SERVER_PORT = 8000;
2
+ export const EXPRESS_SERVER_PATH = process.env.STATIC_SERVER_PATH || `http://127.0.0.1:${EXPRESS_SERVER_PORT}/`;
3
+ // 静态服务资源白名单
4
+ export const ALLOW_MIME = {
5
+ gif: 'image/gif',
6
+ jpg: 'image/jpeg',
7
+ png: 'image/png',
8
+ webp: 'image/webp',
9
+ svg: 'image/svg+xml',
10
+ ttf: 'font/ttf',
11
+ eot: 'font/eot',
12
+ otf: 'font/otf',
13
+ woff: 'font/woff',
14
+ woff2: 'font/woff2',
15
+ js: 'application/javascript',
16
+ css: 'text/css',
17
+ mp4: 'video/mp4',
18
+ ogg: 'video/ogg',
19
+ webm: 'video/webm',
20
+ };
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './common';
@@ -0,0 +1,59 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ import mount from 'koa-mount';
5
+
6
+ import { Autowired } from '@opensumi/di';
7
+ import { ServerAppContribution, Domain, IServerApp, AppConfig, URI } from '@opensumi/ide-core-node';
8
+
9
+ import { ALLOW_MIME } from '../common';
10
+
11
+ @Domain(ServerAppContribution)
12
+ export class ExpressFileServerContribution implements ServerAppContribution {
13
+ @Autowired(AppConfig)
14
+ private appConfig: AppConfig;
15
+
16
+ getWhiteList() {
17
+ return [
18
+ // 插件市场安装目录
19
+ this.appConfig.marketplace.extensionDir,
20
+ ...(this.appConfig.staticAllowPath || []),
21
+ ];
22
+ }
23
+
24
+ initialize(app: IServerApp) {
25
+ app.use(
26
+ mount('/assets', async (ctx) => {
27
+ const uriPath = decodeURI(ctx.path.replace(/^\/assets/, ''));
28
+ if (!uriPath) {
29
+ ctx.status = 404;
30
+ return;
31
+ }
32
+
33
+ const filePath = URI.parse(`file://${uriPath}`).codeUri.fsPath;
34
+ const whitelist = this.getWhiteList();
35
+ const contentType = ALLOW_MIME[path.extname(filePath).slice(1)];
36
+ if (
37
+ /**
38
+ * 地址在白名单内
39
+ * Windows 下 C:\\Path\\to\\file 会被转换成 c:\\Path\\to\\file
40
+ */
41
+ whitelist.some(
42
+ (whitelistPath) => whitelistPath && filePath.toLowerCase().startsWith(whitelistPath.toLowerCase()),
43
+ ) &&
44
+ // 在允许的 contentType
45
+ contentType
46
+ ) {
47
+ ctx.set('Content-Type', contentType);
48
+ if (this.appConfig.staticAllowOrigin) {
49
+ ctx.set('Access-Control-Allow-Origin', this.appConfig.staticAllowOrigin);
50
+ }
51
+
52
+ ctx.body = fs.createReadStream(filePath);
53
+ } else {
54
+ ctx.status = 403;
55
+ }
56
+ }),
57
+ );
58
+ }
59
+ }
@@ -0,0 +1,9 @@
1
+ import { Provider, Injectable } from '@opensumi/di';
2
+ import { NodeModule } from '@opensumi/ide-core-node';
3
+
4
+ import { ExpressFileServerContribution } from './express-file-server.contribution';
5
+
6
+ @Injectable()
7
+ export class ExpressFileServerModule extends NodeModule {
8
+ providers: Provider[] = [ExpressFileServerContribution];
9
+ }