@lcap/nasl 3.8.0-beta.6 → 3.8.0-beta.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.
Files changed (206) hide show
  1. package/.mocharc.js +1 -0
  2. package/out/common/BaseNode.d.ts +19 -3
  3. package/out/common/BaseNode.js +85 -7
  4. package/out/common/BaseNode.js.map +1 -1
  5. package/out/concepts/AbstractInterface__.js.map +1 -1
  6. package/out/concepts/Annotation__.js +1 -1
  7. package/out/concepts/Annotation__.js.map +1 -1
  8. package/out/concepts/App__.d.ts +3 -0
  9. package/out/concepts/App__.js +964 -4
  10. package/out/concepts/App__.js.map +1 -1
  11. package/out/concepts/Argument__.js +3 -3
  12. package/out/concepts/Argument__.js.map +1 -1
  13. package/out/concepts/BatchAssignment__.d.ts +1 -1
  14. package/out/concepts/BatchAssignment__.js +2 -2
  15. package/out/concepts/BatchAssignment__.js.map +1 -1
  16. package/out/concepts/BindAttribute__.js +3 -4
  17. package/out/concepts/BindAttribute__.js.map +1 -1
  18. package/out/concepts/BindDirective__.js +3 -4
  19. package/out/concepts/BindDirective__.js.map +1 -1
  20. package/out/concepts/BusinessComponent__.d.ts +2 -0
  21. package/out/concepts/BusinessComponent__.js +48 -0
  22. package/out/concepts/BusinessComponent__.js.map +1 -1
  23. package/out/concepts/BusinessLogic__.d.ts +1 -0
  24. package/out/concepts/BusinessLogic__.js +10 -0
  25. package/out/concepts/BusinessLogic__.js.map +1 -1
  26. package/out/concepts/CallEvent__.d.ts +2 -0
  27. package/out/concepts/CallEvent__.js +75 -0
  28. package/out/concepts/CallEvent__.js.map +1 -1
  29. package/out/concepts/CallInterface__.d.ts +1 -0
  30. package/out/concepts/CallInterface__.js +26 -15
  31. package/out/concepts/CallInterface__.js.map +1 -1
  32. package/out/concepts/CallLogic__.d.ts +4 -0
  33. package/out/concepts/CallLogic__.js +59 -3
  34. package/out/concepts/CallLogic__.js.map +1 -1
  35. package/out/concepts/Entity__.d.ts +3 -3
  36. package/out/concepts/Entity__.js +4 -0
  37. package/out/concepts/Entity__.js.map +1 -1
  38. package/out/concepts/Event__.d.ts +2 -0
  39. package/out/concepts/Event__.js +30 -0
  40. package/out/concepts/Event__.js.map +1 -1
  41. package/out/concepts/FrontendType__.d.ts +2 -0
  42. package/out/concepts/FrontendType__.js +32 -0
  43. package/out/concepts/FrontendType__.js.map +1 -1
  44. package/out/concepts/Frontend__.d.ts +2 -0
  45. package/out/concepts/Frontend__.js +48 -0
  46. package/out/concepts/Frontend__.js.map +1 -1
  47. package/out/concepts/IfStatement__.js +6 -6
  48. package/out/concepts/IfStatement__.js.map +1 -1
  49. package/out/concepts/InterfaceParam__.d.ts +1 -0
  50. package/out/concepts/InterfaceParam__.js +23 -4
  51. package/out/concepts/InterfaceParam__.js.map +1 -1
  52. package/out/concepts/Interface__.d.ts +2 -1
  53. package/out/concepts/Interface__.js +29 -5
  54. package/out/concepts/Interface__.js.map +1 -1
  55. package/out/concepts/JSBlock__.d.ts +1 -0
  56. package/out/concepts/JSBlock__.js +6 -0
  57. package/out/concepts/JSBlock__.js.map +1 -1
  58. package/out/concepts/LogicDeclaration__.d.ts +1 -0
  59. package/out/concepts/LogicDeclaration__.js +58 -7
  60. package/out/concepts/LogicDeclaration__.js.map +1 -1
  61. package/out/concepts/Logic__.js +32 -22
  62. package/out/concepts/Logic__.js.map +1 -1
  63. package/out/concepts/MatchCase__.js +1 -2
  64. package/out/concepts/MatchCase__.js.map +1 -1
  65. package/out/concepts/Match__.js +3 -18
  66. package/out/concepts/Match__.js.map +1 -1
  67. package/out/concepts/MemberExpression__.js +1 -1
  68. package/out/concepts/MemberExpression__.js.map +1 -1
  69. package/out/concepts/Module__.d.ts +2 -0
  70. package/out/concepts/Module__.js +64 -0
  71. package/out/concepts/Module__.js.map +1 -1
  72. package/out/concepts/NewComposite__.d.ts +1 -1
  73. package/out/concepts/NewComposite__.js +2 -2
  74. package/out/concepts/NewComposite__.js.map +1 -1
  75. package/out/concepts/Paginate__.d.ts +1 -0
  76. package/out/concepts/Paginate__.js +15 -7
  77. package/out/concepts/Paginate__.js.map +1 -1
  78. package/out/concepts/QueryFromExpression__.js +2 -2
  79. package/out/concepts/QueryFromExpression__.js.map +1 -1
  80. package/out/concepts/StringLiteral__.js +3 -4
  81. package/out/concepts/StringLiteral__.js.map +1 -1
  82. package/out/concepts/TypeAnnotation__.js +1 -0
  83. package/out/concepts/TypeAnnotation__.js.map +1 -1
  84. package/out/concepts/ViewElement__.d.ts +2 -0
  85. package/out/concepts/ViewElement__.js +8 -5
  86. package/out/concepts/ViewElement__.js.map +1 -1
  87. package/out/concepts/View__.d.ts +1 -0
  88. package/out/concepts/View__.js +55 -1
  89. package/out/concepts/View__.js.map +1 -1
  90. package/out/generator/genBundleFiles.js +14 -11
  91. package/out/generator/genBundleFiles.js.map +1 -1
  92. package/out/generator/genHash.d.ts +2 -2
  93. package/out/generator/genHash.js +30 -30
  94. package/out/generator/genHash.js.map +1 -1
  95. package/out/generator/permission.d.ts +8 -1
  96. package/out/generator/permission.js +321 -23
  97. package/out/generator/permission.js.map +1 -1
  98. package/out/generator/release-body/body.js +18 -4
  99. package/out/generator/release-body/body.js.map +1 -1
  100. package/out/generator/release-body/data.js +6 -2
  101. package/out/generator/release-body/data.js.map +1 -1
  102. package/out/generator/release-body/utils.js +3 -3
  103. package/out/generator/release-body/utils.js.map +1 -1
  104. package/out/generator/release-body/validation.js +2 -2
  105. package/out/generator/release-body/validation.js.map +1 -1
  106. package/out/index.d.ts +1 -1
  107. package/out/index.js.map +1 -1
  108. package/out/natural/genNaturalTS.d.ts +2 -2
  109. package/out/natural/genNaturalTS.js +7 -7
  110. package/out/natural/genNaturalTS.js.map +1 -1
  111. package/out/natural/getContext/index.d.ts +1 -1
  112. package/out/natural/getContext/index.js +15 -9
  113. package/out/natural/getContext/index.js.map +1 -1
  114. package/out/natural/transformTS2UI.js +30 -11
  115. package/out/natural/transformTS2UI.js.map +1 -1
  116. package/out/natural/transformTSCode.js +30 -11
  117. package/out/natural/transformTSCode.js.map +1 -1
  118. package/out/server/entity2LogicNamespace.d.ts +2 -2
  119. package/out/server/entity2LogicNamespace.js +321 -310
  120. package/out/server/entity2LogicNamespace.js.map +1 -1
  121. package/out/server/extendBaseNode.js +25 -0
  122. package/out/server/extendBaseNode.js.map +1 -1
  123. package/out/server/naslServer.d.ts +1 -1
  124. package/out/server/naslServer.js +23 -16
  125. package/out/server/naslServer.js.map +1 -1
  126. package/out/server/translator.js +12 -0
  127. package/out/server/translator.js.map +1 -1
  128. package/out/templator/genCreateBlock.js +3 -3
  129. package/out/templator/genCreateBlock.js.map +1 -1
  130. package/out/templator/genCurdEditMultipleKeyBlock.js +2 -2
  131. package/out/templator/genCurdEditMultipleKeyBlock.js.map +1 -1
  132. package/out/templator/genCurdMultipleKeyBlock.js +4 -4
  133. package/out/templator/genCurdMultipleKeyBlock.js.map +1 -1
  134. package/out/templator/genEditTableBlock.js +1 -1
  135. package/out/templator/genEditTableBlock.js.map +1 -1
  136. package/out/templator/genGetBlock.js.map +1 -1
  137. package/out/templator/genGridViewBlock.js.map +1 -1
  138. package/out/templator/genSelectBlock.js.map +1 -1
  139. package/out/templator/genTableBlock.js.map +1 -1
  140. package/out/templator/genUpdateBlock.js +3 -3
  141. package/out/templator/genUpdateBlock.js.map +1 -1
  142. package/out/templator/utils.js +1 -1
  143. package/out/templator/utils.js.map +1 -1
  144. package/package.json +5 -5
  145. package/src/common/BaseNode.ts +91 -11
  146. package/src/concepts/AbstractInterface__.ts +1 -0
  147. package/src/concepts/Annotation__.ts +1 -1
  148. package/src/concepts/App__.ts +104 -4
  149. package/src/concepts/Argument__.ts +3 -3
  150. package/src/concepts/BatchAssignment__.ts +2 -2
  151. package/src/concepts/BindAttribute__.ts +3 -4
  152. package/src/concepts/BindDirective__.ts +3 -4
  153. package/src/concepts/BusinessComponent__.ts +54 -1
  154. package/src/concepts/BusinessLogic__.ts +10 -1
  155. package/src/concepts/CallEvent__.ts +66 -0
  156. package/src/concepts/CallInterface__.ts +21 -9
  157. package/src/concepts/CallLogic__.ts +59 -3
  158. package/src/concepts/Entity__.ts +7 -1
  159. package/src/concepts/Event__.ts +32 -0
  160. package/src/concepts/FrontendType__.ts +34 -1
  161. package/src/concepts/Frontend__.ts +51 -0
  162. package/src/concepts/IfStatement__.ts +6 -6
  163. package/src/concepts/InterfaceParam__.ts +23 -4
  164. package/src/concepts/Interface__.ts +29 -5
  165. package/src/concepts/JSBlock__.ts +5 -0
  166. package/src/concepts/LogicDeclaration__.ts +65 -7
  167. package/src/concepts/Logic__.ts +34 -25
  168. package/src/concepts/MatchCase__.ts +1 -2
  169. package/src/concepts/Match__.ts +3 -21
  170. package/src/concepts/MemberExpression__.ts +1 -1
  171. package/src/concepts/Module__.ts +75 -0
  172. package/src/concepts/NewComposite__.ts +2 -2
  173. package/src/concepts/Paginate__.ts +14 -7
  174. package/src/concepts/QueryFromExpression__.ts +2 -2
  175. package/src/concepts/StringLiteral__.ts +3 -4
  176. package/src/concepts/TypeAnnotation__.ts +1 -0
  177. package/src/concepts/ViewElement__.ts +9 -5
  178. package/src/concepts/View__.ts +56 -1
  179. package/src/generator/genBundleFiles.ts +14 -11
  180. package/src/generator/genHash.ts +32 -6
  181. package/src/generator/permission.ts +333 -23
  182. package/src/generator/release-body/body.ts +24 -5
  183. package/src/generator/release-body/data.ts +7 -2
  184. package/src/generator/release-body/utils.ts +3 -3
  185. package/src/generator/release-body/validation.ts +1 -1
  186. package/src/index.ts +13 -1
  187. package/src/natural/genNaturalTS.ts +9 -7
  188. package/src/natural/getContext/index.ts +17 -11
  189. package/src/natural/transformTS2UI.ts +28 -11
  190. package/src/natural/transformTSCode.ts +28 -11
  191. package/src/server/entity2LogicNamespace.ts +13 -1
  192. package/src/server/extendBaseNode.ts +31 -0
  193. package/src/server/naslServer.ts +32 -23
  194. package/src/server/translator.ts +15 -0
  195. package/src/templator/genCreateBlock.ts +4 -4
  196. package/src/templator/genCurdEditMultipleKeyBlock.ts +2 -2
  197. package/src/templator/genCurdMultipleKeyBlock.ts +7 -7
  198. package/src/templator/genEditTableBlock.ts +4 -4
  199. package/src/templator/genGetBlock.ts +1 -1
  200. package/src/templator/genGridViewBlock.ts +2 -2
  201. package/src/templator/genSelectBlock.ts +3 -3
  202. package/src/templator/genTableBlock.ts +2 -2
  203. package/src/templator/genUpdateBlock.ts +6 -6
  204. package/src/templator/utils.ts +1 -1
  205. package/test/concepts/string-literal/__snapshots__/toVue.spec.ts.snap +1 -1
  206. package/test/concepts/view-element/__snapshots__/toVue.spec.ts.snap +6 -2
@@ -80,6 +80,8 @@ function genArgumentMemberExpression(arr: string[]) {
80
80
  }
81
81
  export interface ElementToVueOptions {
82
82
  filterAttr?: Array<string>;
83
+ /** 生成器镜像的导出流程 */
84
+ isGeneratorExport?: boolean;
83
85
  isExport?: boolean;
84
86
  isRelease?: boolean;
85
87
  isRoot?: boolean;
@@ -287,7 +289,7 @@ function transAstNodeToNaslNode(astNode: any, namespace?: string): any {
287
289
  function transAstNodeToNaslTypeNode(astNode: any, namespace?: string): any {
288
290
  let node = null;
289
291
  // 单独特殊处理枚举因为他不是MemberExpression,而是Identifier_
290
- if (astNode?.value?.startsWith('__enumTypeAnnotation_')) {
292
+ if (astNode?.value?.startsWith?.('__enumTypeAnnotation_')) {
291
293
  const valueArr = astNode.value.replace('__enumTypeAnnotation_', '')?.split('.');
292
294
  const name = valueArr.pop();
293
295
  const namespace = valueArr.join('.');
@@ -1634,7 +1636,7 @@ export class ViewElement extends Annotatable {
1634
1636
  let levelIndex = parentLevel || 0;
1635
1637
  code += `${self.name}: () => {\n`;
1636
1638
 
1637
- const getMultipleDefaultValue = (name: string) => {
1639
+ const getPropInitialValue = (name: string) => {
1638
1640
  let v = 'new nasl.core.BooleanFalse()';
1639
1641
  const attr = self.bindAttrs?.find((attr) => attr.name === name);
1640
1642
  if (['static', 'string'].includes(attr?.type)) {
@@ -1783,18 +1785,19 @@ export class ViewElement extends Annotatable {
1783
1785
  // 分页
1784
1786
  let pageableDefaultValue: string;
1785
1787
  if (hasDataSource && P && !T1) {
1786
- pageableDefaultValue = getMultipleDefaultValue('pageable');
1788
+ pageableDefaultValue = getPropInitialValue('pageable');
1787
1789
  code += `let __pageable = ${pageableDefaultValue};\n`;
1788
1790
  }
1789
1791
  // 多选
1790
1792
  let multipleDefaultValue: string;
1791
1793
  if (hasDataSource && M && !T1) {
1792
- multipleDefaultValue = getMultipleDefaultValue('multiple');
1794
+ const name = config?.allNodesAPI?.[self.tag]?.props?.find((prop) => ['M'].includes(prop.tsType))?.name;
1795
+ multipleDefaultValue = getPropInitialValue(name || 'multiple');
1793
1796
  code += `let __multiple = ${multipleDefaultValue};\n`;
1794
1797
  }
1795
1798
  if (hasDataSource && V && !T1) {
1796
1799
  code += `// @ts-ignore
1797
- let __value: typeof __multiple extends nasl.core.BooleanTrue ? ${valueType}[] : ${valueType};\n`;
1800
+ let __value: typeof __multiple extends nasl.core.BooleanTrue ? nasl.collection.List<${valueType}> : ${valueType};\n`;
1798
1801
  code += `// @ts-ignore
1799
1802
  let __V: ${valueType};\n`;
1800
1803
  }
@@ -2914,6 +2917,7 @@ export class ViewElement extends Annotatable {
2914
2917
  return `:${name}="${value.replace(/"/g, "'")}"`;
2915
2918
  }
2916
2919
  } catch (e) {
2920
+ if (apiOfAttr && apiOfAttr.type && !apiOfAttr.type.includes('string')) return '';
2917
2921
  if (apiOfAttr && apiOfAttr.tsType && !apiOfAttr.tsType.includes('nasl.core.String')) return '';
2918
2922
  }
2919
2923
  value = value
@@ -15,6 +15,7 @@ import {
15
15
  withSourceMapGenerator,
16
16
  withGenerator,
17
17
  TranslatorGenerator,
18
+ createDecoratorCode,
18
19
  } from '../translator';
19
20
  import translator from '../bak/translator';
20
21
  import { ElementToVueOptions } from './ViewElement__';
@@ -2559,11 +2560,11 @@ export class View extends Annotatable {
2559
2560
  code += variable.toNaturalTS(shiftState(state, code, { inline: true }));
2560
2561
  code += ';\n';
2561
2562
  });
2563
+ if (this.variables.length) code += '\n';
2562
2564
 
2563
2565
  if (callback) {
2564
2566
  code += callback.call(this, state);
2565
2567
  } else {
2566
- code += '\n';
2567
2568
  code += this.elements[0].toNaturalTS(shiftState(state, code, { tabSize: state.tabSize + 1 }));
2568
2569
  }
2569
2570
 
@@ -2573,6 +2574,60 @@ export class View extends Annotatable {
2573
2574
  return code;
2574
2575
  }
2575
2576
 
2577
+ @withSourceMap()
2578
+ toTextualNASL(state = createCompilerState(), callback?: (this: View, state: TranslatorState) => string): string {
2579
+ const viewName = state.rename ?? this.tsName;
2580
+ let code = '';
2581
+ code += createDecoratorCode(state, this, ['pageTemplateId', 'uuid', 'title', 'crumb', 'auth', 'authDescription', 'isIndex']);
2582
+
2583
+ if (state.declaration) {
2584
+ code += `declare view ${viewName}`;
2585
+ // 有参数长度
2586
+ code += '(';
2587
+ this.params.forEach((param, index) => {
2588
+ code += param.toTextualNASL(shiftState(state, code, { tabSize: 0 }));
2589
+ if (index !== this.params.length - 1) code += ', ';
2590
+ });
2591
+ code += ') {';
2592
+ if (state.convertChildren && this.children?.length) {
2593
+ this.children.forEach((child) => {
2594
+ code += child.toTextualNASL(shiftState(state, code, { tabSize: state?.tabSize || 0 + 1 }), callback);
2595
+ code += '\n';
2596
+ });
2597
+ }
2598
+ code += '}';
2599
+ } else {
2600
+ code += `view ${viewName}`;
2601
+
2602
+ // 有参数长度
2603
+ code += '(';
2604
+ this.params.forEach((param, index) => {
2605
+ code += param.toTextualNASL(shiftState(state, code, { inline: true }));
2606
+ if (index !== this.params.length - 1) code += ', ';
2607
+ });
2608
+ code += ')';
2609
+
2610
+ code += ' {\n';
2611
+
2612
+ this.variables.forEach((variable) => {
2613
+ code += `${indent(state.tabSize + 1)}let `;
2614
+ code += variable.toTextualNASL(shiftState(state, code, { inline: true }));
2615
+ code += ';\n';
2616
+ });
2617
+ if (this.variables.length) code += '\n';
2618
+
2619
+ if (callback) {
2620
+ code += callback.call(this, state);
2621
+ } else {
2622
+ code += this.elements[0].toTextualNASL(shiftState(state, code, { tabSize: state.tabSize + 1 }));
2623
+ }
2624
+
2625
+ code += `\n${indent(state.tabSize)}}\n`;
2626
+ }
2627
+
2628
+ return code;
2629
+ }
2630
+
2576
2631
  *toEmbeddedTSFile(): EmbeddedTSFileGenerator {
2577
2632
  let code = `namespace ${this.getTsNamespace()} {\n`;
2578
2633
  const state = createCompilerState(code, {
@@ -176,7 +176,6 @@ function genRouteFiles(routes: Route[], defaultRoute: string,config?: any) {
176
176
  // 生成路由文件列表
177
177
  const routeFiles: any[] = [];
178
178
  function routeToFile(route: Route) {
179
- console.log(route, 'route');
180
179
  if (route?.component?.script) {
181
180
  const content = genExportComponetCode(route.component);
182
181
  const lazyPath = getCompletePath(null, content, config);
@@ -236,6 +235,7 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
236
235
  try {
237
236
  config?.debug && genBreakpoints(app);
238
237
  app.curDeployEnv = config.env
238
+ console.time('toVueOptions');
239
239
 
240
240
  utils.traverse(
241
241
  (current) => {
@@ -252,6 +252,7 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
252
252
  } as View,
253
253
  }
254
254
  );
255
+ console.timeEnd('toVueOptions');
255
256
 
256
257
  businessComponents.forEach((businessComponent) => {
257
258
  const frontendI18nEnabled = frontend?.i18nInfo?.enabled
@@ -543,8 +544,16 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
543
544
  } else if(window.parent.isGlobalScaleTop) {
544
545
  // 点击跳转到外部页面时,通知顶层页面
545
546
  document.body.addEventListener('click', (e) => {
546
- if(e.target.href){
547
- parent.location.href = e.target.href;
547
+ if(!e.target.href) return;
548
+ if(typeof e.target.href !== 'string') return;
549
+ if(e.target.tagName !== 'A') return;
550
+ try {
551
+ const url = new URL(e.target.href);
552
+ if(url.host !== location.host){
553
+ parent.location.href = e.target.href;
554
+ }
555
+ } catch(err) {
556
+ console.error(err);
548
557
  }
549
558
  });
550
559
  }
@@ -618,8 +627,8 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
618
627
  }
619
628
  return response.text();
620
629
  }).then(function(scriptText) {
621
- var func = new Function(scriptText);
622
- var result = func();
630
+ var func = new Function('importComponent', scriptText);
631
+ var result = func(window.importComponent);
623
632
  return result;
624
633
  })
625
634
  .catch(error => {
@@ -879,9 +888,7 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
879
888
 
880
889
 
881
890
 
882
- console.time('genHash')
883
891
  let bundleMinPath = completePath + getCompletePath(getBundleFileName(), content, config);
884
- console.timeEnd('genHash');
885
892
 
886
893
  const otherJsList = (frontend as any)?.appletsConfig?.enable ? ['//res.wx.qq.com/open/js/jweixin-1.3.2.js'] : [];
887
894
 
@@ -1056,7 +1063,6 @@ export function genBundleFiles(app: App, frontend: Frontend, config: Config) {
1056
1063
  output.content = replaceContentFilePath(output.content || '');
1057
1064
  })
1058
1065
  }
1059
- console.log('outputs', outputs)
1060
1066
  return outputs;
1061
1067
  }
1062
1068
 
@@ -1143,7 +1149,6 @@ export function getOneFiles(options: { view: View; action: string; cacheRouterDa
1143
1149
  }
1144
1150
  currentView = currentView.parentNode;
1145
1151
  }
1146
- console.log(currentRoute);
1147
1152
 
1148
1153
  function addOrUpdateRoute(routes: Route[], newRoute: Route) {
1149
1154
  const existingRoute = routes.find((route) => route.path === newRoute.path);
@@ -1181,7 +1186,6 @@ export function getOneFiles(options: { view: View; action: string; cacheRouterDa
1181
1186
  } else {
1182
1187
  addOrUpdateRoute(cacheRouters, currentRoute);
1183
1188
  }
1184
- console.log(cacheRouters, 111);
1185
1189
  const routerFiles = genRouteFiles(cacheRouters, null);
1186
1190
  const completePath = `${cacheRouterData.baseUrl}${frontend.basePath || ''}/`;
1187
1191
  const outputs = routerFiles;
@@ -1195,6 +1199,5 @@ export function getOneFiles(options: { view: View; action: string; cacheRouterDa
1195
1199
  item.ideVersion = cacheRouterData.ideVersion;
1196
1200
  }
1197
1201
  });
1198
- console.log('outputs: ', outputs);
1199
1202
  return outputs;
1200
1203
  }
@@ -1,13 +1,39 @@
1
- import * as crypto from 'crypto';
1
+ // cyrb53 (c) 2018 bryc (github.com/bryc). License: Public domain. Attribution appreciated.
2
+ // A fast and simple 64-bit (or 53-bit) string hash function with decent collision resistance.
3
+ // Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
4
+ // See https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript/52171480#52171480
5
+ // https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
6
+ const cyrb64 = (str : string, seed = 0) => {
7
+ let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
8
+ for(let i = 0, ch; i < str.length; i++) {
9
+ ch = str.charCodeAt(i);
10
+ h1 = Math.imul(h1 ^ ch, 2654435761);
11
+ h2 = Math.imul(h2 ^ ch, 1597334677);
12
+ }
13
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
14
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
15
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
16
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
17
+ // For a single 53-bit numeric return value we could return
18
+ // 4294967296 * (2097151 & h2) + (h1 >>> 0);
19
+ // but we instead return the full 64-bit value:
20
+ return [h2>>>0, h1>>>0];
21
+ };
22
+
23
+ // An improved, *insecure* 64-bit hash that's short, fast, and has no dependencies.
24
+ // Output is always 14 characters.
25
+ const cyrb64Hash = (str : string, seed = 0) => {
26
+ const [h2, h1] = cyrb64(str, seed);
27
+ return h2.toString(36).padStart(7, '0') + h1.toString(36).padStart(7, '0');
28
+ }
29
+
2
30
 
3
31
  /**
4
32
  *
5
33
  * @param 文件内容 content
6
- * @returns MD5 hash8位
34
+ * @returns cyrb64 hash,取前 8
7
35
  */
8
- export function genHash(content: string) {
9
- const hash = crypto.createHash('md5').update(content, 'utf8').digest('hex');
10
- return hash.slice(0, 8);
11
- }
36
+
37
+ export const genHash = (str : string) => cyrb64Hash(str, 0).slice(0, 8);
12
38
 
13
39
  export default genHash;