@aiot-toolkit/aiotpack 2.0.2-dev.8 → 2.0.3-beta.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 (83) hide show
  1. package/lib/{utils/ux/UxFollowWorks.d.ts → afterCompile/ux/UxAfterCompile.d.ts} +11 -4
  2. package/lib/{utils/ux/UxFollowWorks.js → afterCompile/ux/UxAfterCompile.js} +95 -44
  3. package/lib/{followWorks → afterCompile}/xts/entryTemplate.d.ts +1 -1
  4. package/lib/{followWorks → afterCompile}/xts/entryTemplate.js +14 -13
  5. package/lib/{followWorks → afterCompile}/xts/generateRpk.d.ts +1 -1
  6. package/lib/{followWorks → afterCompile}/xts/generateRpk.js +5 -4
  7. package/lib/{followWorks → afterCompile}/xts/ts2wasm.d.ts +1 -1
  8. package/lib/{followWorks → afterCompile}/xts/ts2wasm.js +7 -6
  9. package/lib/afterWorks/ux/UxAfterWorks.d.ts +5 -0
  10. package/lib/afterWorks/ux/UxAfterWorks.js +22 -0
  11. package/lib/{utils/ux/UxPreWorks.d.ts → beforeCompile/ux/UxBeforeCompile.d.ts} +4 -4
  12. package/lib/{utils/ux/UxPreWorks.js → beforeCompile/ux/UxBeforeCompile.js} +11 -8
  13. package/lib/{preWorks → beforeCompile}/xts/preInstall.d.ts +1 -1
  14. package/lib/{preWorks → beforeCompile}/xts/preInstall.js +6 -5
  15. package/lib/beforeWorks/ux/UxBeforeWorks.d.ts +5 -0
  16. package/lib/beforeWorks/ux/UxBeforeWorks.js +22 -0
  17. package/lib/compiler/interface/ICompileParam.d.ts +1 -1
  18. package/lib/compiler/interface/ISignConfig.d.ts +6 -1
  19. package/lib/compiler/javascript/JavascriptCompiler.js +45 -12
  20. package/lib/compiler/javascript/JavascriptDefaultCompileOption.js +1 -2
  21. package/lib/compiler/javascript/android/AndroidWebpackConfigurator.d.ts +9 -0
  22. package/lib/compiler/javascript/android/AndroidWebpackConfigurator.js +16 -0
  23. package/lib/compiler/javascript/android/plugin/WrapPlugin.d.ts +20 -0
  24. package/lib/compiler/javascript/android/plugin/WrapPlugin.js +91 -0
  25. package/lib/compiler/javascript/interface/IJavascriptCompileOption.d.ts +16 -1
  26. package/lib/compiler/javascript/interface/IWebpackConfigurator.d.ts +4 -4
  27. package/lib/compiler/javascript/vela/VelaWebpackConfigurator.d.ts +4 -2
  28. package/lib/compiler/javascript/vela/VelaWebpackConfigurator.js +18 -5
  29. package/lib/compiler/javascript/vela/interface/IChunk.d.ts +6 -1
  30. package/lib/compiler/javascript/vela/interface/IManifest.d.ts +1 -1
  31. package/lib/compiler/javascript/vela/interface/IQuickAppConfig.d.ts +1 -1
  32. package/lib/compiler/javascript/vela/model/Package.d.ts +2 -2
  33. package/lib/compiler/javascript/vela/model/Package.js +5 -5
  34. package/lib/compiler/javascript/vela/plugin/WrapPlugin.d.ts +1 -1
  35. package/lib/compiler/javascript/vela/plugin/WrapPlugin.js +16 -2
  36. package/lib/compiler/javascript/vela/utils/Jsc.d.ts +1 -1
  37. package/lib/compiler/javascript/vela/utils/Jsc.js +19 -11
  38. package/lib/compiler/javascript/vela/utils/UxCompileUtil.d.ts +1 -1
  39. package/lib/compiler/javascript/vela/utils/UxCompileUtil.js +4 -4
  40. package/lib/compiler/javascript/vela/utils/ZipUtil.d.ts +29 -5
  41. package/lib/compiler/javascript/vela/utils/ZipUtil.js +45 -31
  42. package/lib/compiler/javascript/vela/utils/signature/Base64.d.ts +0 -1
  43. package/lib/compiler/javascript/vela/utils/signature/SignUtil.d.ts +23 -7
  44. package/lib/compiler/javascript/vela/utils/signature/SignUtil.js +94 -45
  45. package/lib/compiler/javascript/vela/utils/signature/Signer.d.ts +0 -1
  46. package/lib/compiler/javascript/vela/utils/webpackLoader/addColSourceMap.d.ts +3 -0
  47. package/lib/compiler/javascript/vela/utils/webpackLoader/addColSourceMap.js +56 -0
  48. package/lib/compiler/javascript/vela/utils/webpackLoader/extractMapData.d.ts +3 -0
  49. package/lib/compiler/javascript/vela/utils/webpackLoader/{splitMap.js → extractMapData.js} +2 -2
  50. package/lib/config/UxConfig.d.ts +45 -7
  51. package/lib/config/UxConfig.js +156 -40
  52. package/lib/config/XtsConfig.d.ts +8 -4
  53. package/lib/config/XtsConfig.js +24 -8
  54. package/lib/index.d.ts +9 -1
  55. package/lib/index.js +35 -1
  56. package/lib/loader/ux/JsLoader.d.ts +2 -0
  57. package/lib/loader/ux/JsLoader.js +6 -9
  58. package/lib/loader/ux/PngLoader.js +4 -5
  59. package/lib/loader/ux/android/UxLoader.d.ts +13 -0
  60. package/lib/loader/ux/android/UxLoader.js +38 -0
  61. package/lib/loader/ux/{AppUxLoader.d.ts → vela/AppUxLoader.d.ts} +3 -1
  62. package/lib/loader/ux/{AppUxLoader.js → vela/AppUxLoader.js} +5 -3
  63. package/lib/loader/ux/vela/HmlLoader.d.ts +24 -0
  64. package/lib/loader/ux/vela/HmlLoader.js +63 -0
  65. package/lib/loader/ux/{UxLoader.d.ts → vela/UxLoader.d.ts} +3 -1
  66. package/lib/loader/ux/{UxLoader.js → vela/UxLoader.js} +11 -14
  67. package/lib/loader/xts/XtsLoader.js +6 -7
  68. package/lib/utils/BeforeCompileUtils.d.ts +21 -0
  69. package/lib/utils/BeforeCompileUtils.js +118 -0
  70. package/lib/utils/ux/ManifestSchema.d.ts +1 -1
  71. package/lib/utils/ux/UxFileUtils.d.ts +16 -3
  72. package/lib/utils/ux/UxFileUtils.js +53 -17
  73. package/lib/utils/ux/UxLoaderUtils.d.ts +7 -4
  74. package/lib/utils/ux/UxLoaderUtils.js +43 -25
  75. package/lib/utils/ux/android/AndroidUx.d.ts +27 -0
  76. package/lib/utils/ux/android/AndroidUx.js +98 -0
  77. package/lib/utils/xts/XtsFileLaneUtils.d.ts +10 -0
  78. package/lib/utils/xts/XtsFileLaneUtils.js +68 -0
  79. package/lib/utils/xts/XtsFollowWorks.js +8 -8
  80. package/package.json +15 -8
  81. package/lib/compiler/javascript/vela/utils/webpackLoader/splitMap.d.ts +0 -3
  82. package/lib/utils/PreWorkUtils.d.ts +0 -22
  83. package/lib/utils/PreWorkUtils.js +0 -106
@@ -1,8 +1,8 @@
1
- /// <reference types="node" />
2
- import { Dictionary } from '@aiot-toolkit/shared-utils/lib/type/Type';
1
+ import { Dictionary } from '@aiot-toolkit/shared-utils';
3
2
  import JSZip from 'jszip';
4
3
  import IJavascriptCompileOption from '../../interface/IJavascriptCompileOption';
5
4
  import IManifest from '../interface/IManifest';
5
+ import Package from '../model/Package';
6
6
  /**
7
7
  * Zip 用于将打包成功的 build 目录按特定规则压缩成 rpk
8
8
  *
@@ -29,12 +29,26 @@ declare class ZipUtil {
29
29
  * 3. 写入到硬盘
30
30
  * @param dist
31
31
  * @param param
32
- * @returns
32
+ * @returns 生成的 rpk 文件名
33
33
  */
34
- static createRpk(dist: string, param: IJavascriptCompileOption): Promise<void>;
34
+ static createRpk(dist: string, param: IJavascriptCompileOption): Promise<string | undefined>;
35
35
  private static getFileName;
36
36
  private static packageToZipBuffer;
37
+ /**
38
+ *
39
+ * @param buffer
40
+ * @param param
41
+ * @param fileName
42
+ *
43
+ * @returns 生成的文件名
44
+ */
37
45
  private static generateDistFile;
46
+ /**
47
+ * 根据文件列表创建 zip 的 Buffer
48
+ * @param fileList 文件列表
49
+ * @param comment zip 的注释
50
+ * @returns
51
+ */
38
52
  static createZipBufferFromFileList(fileList: {
39
53
  path: string;
40
54
  content: string | Buffer;
@@ -46,7 +60,9 @@ declare class ZipUtil {
46
60
  * @param files
47
61
  * @returns
48
62
  */
49
- private static createPackagesDefinition;
63
+ static createPackagesDefinition(param: IJavascriptCompileOption, config: IManifest, files: string[]): Promise<{
64
+ fullPackage: Package;
65
+ }>;
50
66
  /**
51
67
  * 获取有序的文件列表
52
68
  *
@@ -70,6 +86,14 @@ declare class ZipUtil {
70
86
  * 创建打包的注释属性
71
87
  */
72
88
  static createComment(param: IJavascriptCompileOption): Dictionary<any>;
89
+ /**
90
+ * 生成rpk的最终的Buffer
91
+ *
92
+ * @description 使用证书对原始rpk包的 buffer 签名. 签名失败,返回原始包; 签名成功,返回新包
93
+ * @param fullPackage 原始包 buffer
94
+ * @param signConfig 证书信息
95
+ * @returns
96
+ */
73
97
  private static buildProjectAndOutput;
74
98
  }
75
99
  export default ZipUtil;
@@ -13,9 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  var _a;
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
17
- const CommonUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/CommonUtil"));
18
- const FileUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/FileUtil"));
16
+ const shared_utils_1 = require("@aiot-toolkit/shared-utils");
19
17
  const fs_extra_1 = __importDefault(require("fs-extra"));
20
18
  const jszip_1 = __importDefault(require("jszip"));
21
19
  const path_1 = __importDefault(require("path"));
@@ -43,7 +41,7 @@ class ZipUtil {
43
41
  * 3. 写入到硬盘
44
42
  * @param dist
45
43
  * @param param
46
- * @returns
44
+ * @returns 生成的 rpk 文件名
47
45
  */
48
46
  static createRpk(dist, param) {
49
47
  return __awaiter(this, void 0, void 0, function* () {
@@ -51,19 +49,16 @@ class ZipUtil {
51
49
  // 1
52
50
  const files = this.getSortedFiles(dist, param, config);
53
51
  if (!files.length) {
54
- ColorConsole_1.default.error(`The build file is missing, stop generating the application package, please check carefully`);
52
+ shared_utils_1.ColorConsole.error(`The build file is missing, stop generating the application package, please check carefully`);
55
53
  return;
56
54
  }
57
55
  // 2
58
56
  const { fullPackage } = yield this.createPackagesDefinition(param, config, files);
59
- const zipBuffer = yield this.packageToZipBuffer(fullPackage);
60
57
  // 生产出带签名的rpk文件buffer
61
58
  const signConfig = SignUtil_1.default.getProjectSignConfig(param);
62
- const { rpkBuffer } = yield ZipUtil.buildProjectAndOutput(zipBuffer, signConfig);
63
- // IDE 扫码预览打包 todo
64
- // IDE 重置包名
59
+ const rpkBuffer = yield _a.buildProjectAndOutput(fullPackage, signConfig);
65
60
  // 3
66
- this.generateDistFile(rpkBuffer, param, this.getFileName(param, config, 'rpk'));
61
+ return this.generateDistFile(rpkBuffer, param, this.getFileName(param, config, 'rpk'));
67
62
  });
68
63
  }
69
64
  static getFileName(param, config, ext) {
@@ -72,7 +67,7 @@ class ZipUtil {
72
67
  return [
73
68
  packageName,
74
69
  mode === CompileMode_1.default.DEVELOPMENT ? 'debug' : 'release',
75
- buildNameFormat === BuildNameFormatType_1.default.DEFAULT ? config.versionName : '',
70
+ buildNameFormat === BuildNameFormatType_1.default.ORIGINAL ? '' : config.versionName,
76
71
  ext
77
72
  ]
78
73
  .filter(Boolean)
@@ -90,14 +85,29 @@ class ZipUtil {
90
85
  return yield this.createZipBufferFromFileList(packageFileList, comment);
91
86
  });
92
87
  }
88
+ /**
89
+ *
90
+ * @param buffer
91
+ * @param param
92
+ * @param fileName
93
+ *
94
+ * @returns 生成的文件名
95
+ */
93
96
  static generateDistFile(buffer, param, fileName) {
94
97
  return __awaiter(this, void 0, void 0, function* () {
95
98
  const { releasePath, projectPath } = param;
96
99
  const filePath = path_1.default.join(projectPath, releasePath, fileName);
97
100
  fs_extra_1.default.outputFileSync(filePath, buffer);
98
- ColorConsole_1.default.success(`Project build and generate files:${fileName}`);
101
+ shared_utils_1.ColorConsole.success(`Project build and generate files:${fileName}`);
102
+ return fileName;
99
103
  });
100
104
  }
105
+ /**
106
+ * 根据文件列表创建 zip 的 Buffer
107
+ * @param fileList 文件列表
108
+ * @param comment zip 的注释
109
+ * @returns
110
+ */
101
111
  static createZipBufferFromFileList(fileList, comment) {
102
112
  return __awaiter(this, void 0, void 0, function* () {
103
113
  const zip = new jszip_1.default();
@@ -129,11 +139,11 @@ class ZipUtil {
129
139
  for (let fileBuildPath of files) {
130
140
  const absPath = path_1.default.join(projectPath, outputPath, fileBuildPath);
131
141
  let fileContentBuffer = fs_extra_1.default.readFileSync(absPath);
132
- if (fileBuildPath === ZipUtil.CERT_PATH) {
142
+ if (fileBuildPath === _a.CERT_PATH) {
133
143
  let metaZip = yield jszip_1.default.loadAsync(fileContentBuffer);
134
- fileContentBuffer = (yield metaZip.generateAsync(Object.assign(Object.assign({}, ZipUtil.ZIP_OPTION), { comment: null })));
144
+ fileContentBuffer = (yield metaZip.generateAsync(Object.assign(Object.assign({}, _a.ZIP_OPTION), { comment: null })));
135
145
  }
136
- const fileContentDigest = CommonUtil_1.default.calcDataDigest(fileContentBuffer);
146
+ const fileContentDigest = shared_utils_1.CommonUtil.calcDataDigest(fileContentBuffer);
137
147
  const resourceInfo = {
138
148
  fileBuildPath,
139
149
  fileContentBuffer,
@@ -174,13 +184,13 @@ class ZipUtil {
174
184
  // 1. 排除META-INF 目录下的文件
175
185
  // 2. 生产环境排除 .map 文件
176
186
  const mapReg = /\.map$/;
177
- let result = FileUtil_1.default.readAlldirSync(dist);
187
+ let result = shared_utils_1.FileUtil.readAlldirSync(dist);
178
188
  result = result
179
189
  .filter((item) => {
180
190
  return !(item.startsWith(UxCompileUtil_1.default.DIGEST_ZIP_DIR) ||
181
191
  (param.mode === CompileMode_1.default.PRODUCTION && mapReg.test(item)));
182
192
  })
183
- .map((item) => path_1.default.relative(dist, item));
193
+ .map((item) => path_1.default.relative(dist, item).split(path_1.default.sep).join(path_1.default.posix.sep));
184
194
  result.sort((a, b) => {
185
195
  const indexA = getFileIndex(a);
186
196
  const indexB = getFileIndex(b);
@@ -210,7 +220,7 @@ class ZipUtil {
210
220
  ...entrySkFiles,
211
221
  new RegExp(`^${entry}/$`),
212
222
  new RegExp(`^${entry}/.+`),
213
- /^common\//i,
223
+ /^common\//i, // 静态资源
214
224
  /.+\.js/,
215
225
  'META-INF/build.txt'
216
226
  ];
@@ -231,27 +241,31 @@ class ZipUtil {
231
241
  };
232
242
  return obj;
233
243
  }
234
- static buildProjectAndOutput(fullPackage, signConfig) {
244
+ /**
245
+ * 生成rpk的最终的Buffer
246
+ *
247
+ * @description 使用证书对原始rpk包的 buffer 签名. 签名失败,返回原始包; 签名成功,返回新包
248
+ * @param fullPackage 原始包 buffer
249
+ * @param signConfig 证书信息
250
+ * @returns
251
+ */
252
+ static buildProjectAndOutput(target, signConfig) {
235
253
  return __awaiter(this, void 0, void 0, function* () {
236
- // 1. 默认携带META,如果禁用流失打包,需要删除META
237
- // 2. 是否需要增加签名内容
254
+ let result;
238
255
  if (signConfig) {
239
256
  const { privatekey, certificate } = signConfig;
240
- // fullPackageBuffer
241
- const fullPackageBuffer = yield SignUtil_1.default.signZipBufferForPackage(fullPackage, privatekey, certificate);
257
+ const fullPackageBuffer = yield SignUtil_1.default.signPackage(target, privatekey, certificate);
242
258
  if (fullPackageBuffer === false) {
243
- ColorConsole_1.default.throw('Fullpackage signature failed');
259
+ shared_utils_1.ColorConsole.throw('Fullpackage signature failed');
244
260
  }
245
261
  else {
246
- fullPackage = fullPackageBuffer;
262
+ result = fullPackageBuffer;
247
263
  }
248
264
  }
249
- // 3. 校验体积等
250
- // 4. 生成rpks:不需要META和签名
251
- // 5. 返回
252
- return {
253
- rpkBuffer: fullPackage
254
- };
265
+ if (!result) {
266
+ result = yield this.packageToZipBuffer(target);
267
+ }
268
+ return result;
255
269
  });
256
270
  }
257
271
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * Base64
4
3
  */
@@ -1,29 +1,45 @@
1
- /// <reference types="node" />
2
- import ICompileParam from '../../../../interface/ICompileParam';
3
1
  import IJavascriptCompileOption from '../../../interface/IJavascriptCompileOption';
2
+ import Package from '../../model/Package';
4
3
  /**
5
4
  * SignUtil
6
5
  */
7
6
  declare class SignUtil {
8
7
  /**
9
8
  * 获取签名相关的配置内容
10
- * 1. ICompileParam参数中获取编译模式是否为release
11
- * 2. IJavascriptCompileOption参数中获取签名目录等内容
12
- * @param param
9
+ * @param param IJavascriptCompileOption参数中获取签名目录等内容
10
+ * @returns
13
11
  */
14
- static getProjectSignConfig(param: ICompileParam & IJavascriptCompileOption): {
12
+ static getProjectSignConfig(param: IJavascriptCompileOption): {
15
13
  privatekey: Buffer;
16
14
  certificate: Buffer;
17
15
  };
18
- static signZipBufferForPackage(zipBuffer: Buffer, privatekey: Buffer, certificate: Buffer): Promise<false | Buffer>;
16
+ /**
17
+ * 对二进制zipBuffer签名
18
+ *
19
+ * 1. 对`META-INF/CERT`做签名,并生成新的`META-INF/CERT` buffer
20
+ * 2. "新`META-INF/CERT` + 其它文件"生成新的 zipBuffer
21
+ * 3. 对“新zipBuffer”,做整体签名
22
+ * @param zipBuffer
23
+ * @param privatekey
24
+ * @param certificate
25
+ * @returns
26
+ */
27
+ static signPackage(target: Package, privatekey: Buffer, certificate: Buffer): Promise<false | Buffer>;
19
28
  /**
20
29
  * 根据ZIP流获取文件实例
30
+ *
31
+ * 1. zipbuffer转换为JSZip对象
32
+ * 2. 提取文件列表,并生成每个文件的 buffer
33
+ * 3. 创建获取文件buffer的函数
34
+ *
21
35
  * @param zipBuffer
22
36
  * @returns
23
37
  */
24
38
  private static createFileListFromZipBuffer;
25
39
  /**
26
40
  * 为 zip buffer 签名,返回签名后的buffer
41
+ *
42
+ * @description 根据zipBuffer + zip的文件列表 + 私钥 + 证书,对 zipbuffer 签名
27
43
  * @param fileBuffer
28
44
  * @param files
29
45
  * @param privatekey
@@ -12,9 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
16
- const CommonUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/CommonUtil"));
17
- const FileUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/FileUtil"));
15
+ const shared_utils_1 = require("@aiot-toolkit/shared-utils");
18
16
  const crypto_1 = __importDefault(require("crypto"));
19
17
  const fs_extra_1 = __importDefault(require("fs-extra"));
20
18
  const jsrsasign_1 = __importDefault(require("jsrsasign"));
@@ -31,64 +29,104 @@ const Signer_1 = __importDefault(require("./Signer"));
31
29
  class SignUtil {
32
30
  /**
33
31
  * 获取签名相关的配置内容
34
- * 1. ICompileParam参数中获取编译模式是否为release
35
- * 2. IJavascriptCompileOption参数中获取签名目录等内容
36
- * @param param
32
+ * @param param IJavascriptCompileOption参数中获取签名目录等内容
33
+ * @returns
37
34
  */
38
35
  static getProjectSignConfig(param) {
39
36
  const { mode, projectPath, signRoot } = param;
37
+ const signPathFolder = path_1.default.join(projectPath, signRoot || 'sign');
38
+ const signPathConfig = {
39
+ debug: {
40
+ privatekey: path_1.default.join(signPathFolder, 'debug', 'private.pem'),
41
+ certificate: path_1.default.join(signPathFolder, 'debug', 'certificate.pem')
42
+ },
43
+ sign: {
44
+ privatekey: path_1.default.join(signPathFolder, 'private.pem'),
45
+ certificate: path_1.default.join(signPathFolder, 'certificate.pem')
46
+ },
47
+ defaultDevelopment: {
48
+ privatekey: path_1.default.join(__dirname, 'pem', 'private.pem'),
49
+ certificate: path_1.default.join(__dirname, 'pem', 'certificate.pem')
50
+ },
51
+ oldRelease: {
52
+ privatekey: path_1.default.join(signPathFolder, 'release', 'private.pem'),
53
+ certificate: path_1.default.join(signPathFolder, 'release', 'certificate.pem')
54
+ }
55
+ };
56
+ // build模式 优先sign/debug/证书 其次sign/证书 最后toolkit中默认的证书
57
+ const developmentSignConfig = [
58
+ signPathConfig.debug,
59
+ signPathConfig.sign,
60
+ signPathConfig.defaultDevelopment
61
+ ];
62
+ // release模式 优先sign/release/证书 其次sign/证书
63
+ const releaseSignConfig = [signPathConfig.oldRelease, signPathConfig.sign];
64
+ let modeSignConfig = [];
40
65
  let signConfig;
41
- let privatekeyPath;
42
- let certificatePath;
43
66
  switch (mode) {
44
67
  case CompileMode_1.default.DEVELOPMENT:
45
- privatekeyPath = path_1.default.join(__dirname, 'pem', 'private.pem');
46
- certificatePath = path_1.default.join(__dirname, 'pem', 'certificate.pem');
47
- ColorConsole_1.default.info(`User debug signature is enabled by project build`);
68
+ modeSignConfig = developmentSignConfig;
48
69
  break;
49
70
  case CompileMode_1.default.PRODUCTION:
50
- privatekeyPath = path_1.default.join(projectPath, signRoot, 'private.pem');
51
- certificatePath = path_1.default.join(projectPath, signRoot, 'certificate.pem');
52
- ColorConsole_1.default.info(`release signature is enabled by project build`);
71
+ modeSignConfig = releaseSignConfig;
53
72
  break;
54
73
  default:
55
74
  // 打印信息, 模式未定义
56
- ColorConsole_1.default.error(`Error: mode ${mode} undefined`);
75
+ shared_utils_1.ColorConsole.error(`Error: mode ${mode} undefined`);
57
76
  break;
58
77
  }
59
- // 检查路径是否真实存在
60
- if (privatekeyPath &&
61
- certificatePath &&
62
- FileUtil_1.default.checkFilePath([privatekeyPath, certificatePath])) {
78
+ // 寻找有效的证书路径
79
+ const findPath = modeSignConfig.find(({ privatekey, certificate }) => shared_utils_1.FileUtil.checkFilePath([privatekey, certificate]));
80
+ if (findPath) {
81
+ let { privatekey: privatekeyPath, certificate: certificatePath } = findPath;
82
+ shared_utils_1.ColorConsole.info('privatekeyPath is ', {
83
+ word: privatekeyPath
84
+ }, `\ncertificatePath is `, {
85
+ word: certificatePath
86
+ });
87
+ // 读取证书内容
63
88
  signConfig = {
64
89
  privatekey: fs_extra_1.default.readFileSync(privatekeyPath),
65
90
  certificate: fs_extra_1.default.readFileSync(certificatePath)
66
91
  };
67
92
  }
68
93
  else {
69
- // 结束程序
70
- process.exit();
94
+ // 抛出错误,在指定模式下未找到证书
95
+ throw new Error(`The current mode is ${mode}, and there is a problem with the certification path`);
71
96
  }
72
97
  return signConfig;
73
98
  }
74
- static signZipBufferForPackage(zipBuffer, privatekey, certificate) {
99
+ /**
100
+ * 对二进制zipBuffer签名
101
+ *
102
+ * 1. 对`META-INF/CERT`做签名,并生成新的`META-INF/CERT` buffer
103
+ * 2. "新`META-INF/CERT` + 其它文件"生成新的 zipBuffer
104
+ * 3. 对“新zipBuffer”,做整体签名
105
+ * @param zipBuffer
106
+ * @param privatekey
107
+ * @param certificate
108
+ * @returns
109
+ */
110
+ static signPackage(target, privatekey, certificate) {
75
111
  return __awaiter(this, void 0, void 0, function* () {
76
- // 1. 解压得到文件列表
77
- const zipInstWrap = yield SignUtil.createFileListFromZipBuffer(zipBuffer);
78
- // 摘要map
112
+ var _a;
113
+ const getFileBuffer = (path) => {
114
+ return target.getResource(path);
115
+ };
116
+ // 摘要 和 文件列表
79
117
  const fileDigestHash = [];
80
118
  const fileList = [];
81
- // 2. 如果包含META-INF/CERT,则对其解压,增加签名
82
- const metaBuffer = zipInstWrap.getFileBuffer(ZipUtil_1.default.CERT_PATH);
119
+ // 1
120
+ const metaBuffer = (_a = getFileBuffer(ZipUtil_1.default.CERT_PATH)) === null || _a === void 0 ? void 0 : _a.fileContentBuffer;
83
121
  if (metaBuffer) {
84
122
  const metaHash = {
85
123
  name: ZipUtil_1.default.DIGEST_HASH_JSON,
86
- hash: CommonUtil_1.default.calcDataDigest(metaBuffer)
124
+ hash: shared_utils_1.CommonUtil.calcDataDigest(metaBuffer)
87
125
  };
88
126
  const signedMetaBuffer = SignUtil.doSign(metaBuffer, [metaHash], privatekey, certificate);
89
127
  if (signedMetaBuffer === false) {
90
128
  // META-INF/CERT签名失败
91
- ColorConsole_1.default.throw('META-INF/CERT signature failed');
129
+ shared_utils_1.ColorConsole.throw('META-INF/CERT signature failed');
92
130
  }
93
131
  else {
94
132
  fileList.push({
@@ -97,32 +135,45 @@ class SignUtil {
97
135
  });
98
136
  fileDigestHash.push({
99
137
  name: ZipUtil_1.default.CERT_PATH,
100
- hash: CommonUtil_1.default.calcDataDigest(signedMetaBuffer)
138
+ hash: shared_utils_1.CommonUtil.calcDataDigest(signedMetaBuffer)
101
139
  });
102
140
  }
103
141
  }
104
- // 3. 对整个zip做签名
105
- const files = zipInstWrap.fileList;
106
- files.filter(SignUtil.fileFilter).map((item) => {
142
+ // 2
143
+ const files = target.resourceList;
144
+ files
145
+ .map((item) => {
146
+ return {
147
+ path: item.fileBuildPath,
148
+ content: item.fileContentBuffer
149
+ };
150
+ })
151
+ .filter(SignUtil.fileFilter)
152
+ .map((item) => {
107
153
  fileList.push(item);
108
154
  fileDigestHash.push({
109
155
  name: item.path,
110
- hash: CommonUtil_1.default.calcDataDigest(item.content)
156
+ hash: shared_utils_1.CommonUtil.calcDataDigest(item.content)
111
157
  });
112
158
  });
113
159
  // 因为META变化,重新创建ZIP流
114
- const newZipBuffer = yield ZipUtil_1.default.createZipBufferFromFileList(fileList, zipInstWrap.comment);
115
- // 对新ZIP流重新签名
160
+ const newZipBuffer = yield ZipUtil_1.default.createZipBufferFromFileList(fileList, target.comment);
161
+ // 3
116
162
  const signedZipBuffer = SignUtil.doSign(newZipBuffer, fileDigestHash, privatekey, certificate);
117
163
  if (signedZipBuffer === false) {
118
164
  // 签名失败
119
- ColorConsole_1.default.throw('package signature failed');
165
+ shared_utils_1.ColorConsole.throw('package signature failed');
120
166
  }
121
167
  return signedZipBuffer;
122
168
  });
123
169
  }
124
170
  /**
125
171
  * 根据ZIP流获取文件实例
172
+ *
173
+ * 1. zipbuffer转换为JSZip对象
174
+ * 2. 提取文件列表,并生成每个文件的 buffer
175
+ * 3. 创建获取文件buffer的函数
176
+ *
126
177
  * @param zipBuffer
127
178
  * @returns
128
179
  */
@@ -144,17 +195,15 @@ class SignUtil {
144
195
  comment: zipInst.comment,
145
196
  getFileBuffer(path) {
146
197
  const buffer = fileList.find((item) => item.path === path);
147
- let content;
148
- if (buffer) {
149
- content = buffer.content;
150
- }
151
- return content;
198
+ return buffer === null || buffer === void 0 ? void 0 : buffer.content;
152
199
  }
153
200
  };
154
201
  });
155
202
  }
156
203
  /**
157
204
  * 为 zip buffer 签名,返回签名后的buffer
205
+ *
206
+ * @description 根据zipBuffer + zip的文件列表 + 私钥 + 证书,对 zipbuffer 签名
158
207
  * @param fileBuffer
159
208
  * @param files
160
209
  * @param privatekey
@@ -180,13 +229,13 @@ class SignUtil {
180
229
  static unZipFiles(fileBuffer, files) {
181
230
  // 1. 读取zip文件
182
231
  if (!fileBuffer || fileBuffer.length <= 4) {
183
- ColorConsole_1.default.error('Zip file open failed');
232
+ shared_utils_1.ColorConsole.error('Zip file open failed');
184
233
  return false;
185
234
  }
186
235
  // 2. 检查文件格式是否正确
187
236
  const fileMagic = fileBuffer.readInt32LE(0);
188
237
  if (fileMagic !== 0x4034b50) {
189
- ColorConsole_1.default.error('Zip file format is wrong');
238
+ shared_utils_1.ColorConsole.error('Zip file format is wrong');
190
239
  return false;
191
240
  }
192
241
  // 3. 解析数据块
@@ -386,7 +435,7 @@ class SignUtil {
386
435
  const cert = Buffer.from(Base64_1.default.unarmor(certificate));
387
436
  const c = new jsrsasign_1.default.X509();
388
437
  c.readCertPEM(certificate.toString());
389
- const pubKey = jsrsasign_1.default.KEYUTIL.getPEM(c.subjectPublicKeyRSA);
438
+ const pubKey = jsrsasign_1.default.KEYUTIL.getPEM(c.getPublicKey());
390
439
  // 摘要块
391
440
  const digestBuf = Buffer.alloc(sign.length + 12);
392
441
  digestBuf.writeInt32LE(sign.length + 8, 0);
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  /**
3
2
  * Signer
4
3
  */
@@ -0,0 +1,3 @@
1
+ import { LoaderContext } from '@rspack/core';
2
+ declare function addColSourceMap(this: LoaderContext<any>, source: string, map: any): Promise<void>;
3
+ export default addColSourceMap;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const parser_1 = require("@aiot-toolkit/parser");
13
+ function addColSourceMap(source, map) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ const callback = this.async();
16
+ if (!map) {
17
+ callback(null, source, map);
18
+ }
19
+ let { consumer, tempGenerator } = yield parser_1.SourceMapUtil.createConsumerAndGenerator(JSON.stringify(map));
20
+ let recordLine = -1;
21
+ consumer.eachMapping((mapping) => {
22
+ if (mapping.source === consumer.sources[consumer.sources.length - 1]) {
23
+ if (recordLine < mapping.generatedLine &&
24
+ (mapping.generatedColumn !== 0 || mapping.originalColumn !== 0)) {
25
+ tempGenerator.addMapping({
26
+ generated: {
27
+ line: mapping.generatedLine,
28
+ column: 0
29
+ },
30
+ original: {
31
+ line: mapping.originalLine,
32
+ column: 0
33
+ },
34
+ source: mapping.source,
35
+ name: ''
36
+ });
37
+ }
38
+ recordLine = mapping.generatedLine;
39
+ }
40
+ tempGenerator.addMapping({
41
+ generated: {
42
+ line: mapping.generatedLine,
43
+ column: mapping.generatedColumn
44
+ },
45
+ original: {
46
+ line: mapping.originalLine,
47
+ column: mapping.originalColumn
48
+ },
49
+ source: mapping.source,
50
+ name: mapping.name
51
+ });
52
+ });
53
+ callback(null, source, JSON.parse(tempGenerator.toString()));
54
+ });
55
+ }
56
+ exports.default = addColSourceMap;
@@ -0,0 +1,3 @@
1
+ import { LoaderContext } from '@rspack/core';
2
+ declare function extractMapData(this: LoaderContext<any>, source: string, map: any): void;
3
+ export default extractMapData;
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const path_1 = __importDefault(require("path"));
8
- function splitMap(source, map) {
8
+ function extractMapData(source, map) {
9
9
  const enterFilePath = this.resourcePath;
10
10
  // 获取当前文件夹和文件名
11
11
  const dirName = path_1.default.dirname(enterFilePath);
@@ -17,4 +17,4 @@ function splitMap(source, map) {
17
17
  }
18
18
  this.callback(null, source, map && JSON.parse(map));
19
19
  }
20
- exports.default = splitMap;
20
+ exports.default = extractMapData;