@lcap/nasl 2.18.0-beta.3 → 2.18.0-beta.5

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 (54) hide show
  1. package/out/bak/translator.js +1 -1
  2. package/out/bak/translator.js.map +1 -1
  3. package/out/concepts/LogicItem__.js.map +1 -1
  4. package/out/concepts/Logic__.js +4 -4
  5. package/out/concepts/Logic__.js.map +1 -1
  6. package/out/concepts/Match__.js +1 -1
  7. package/out/concepts/Match__.js.map +1 -1
  8. package/out/concepts/MemberExpression__.js.map +1 -1
  9. package/out/concepts/OqlQueryComponent__.js.map +1 -1
  10. package/out/concepts/Param__.js +2 -2
  11. package/out/concepts/Param__.js.map +1 -1
  12. package/out/concepts/Return__.js +1 -1
  13. package/out/concepts/Return__.js.map +1 -1
  14. package/out/concepts/StringInterpolation__.d.ts +16 -16
  15. package/out/concepts/StringInterpolation__.js +12 -12
  16. package/out/concepts/StringInterpolation__.js.map +1 -1
  17. package/out/concepts/ValidationRule__.js.map +1 -1
  18. package/out/concepts/Variable__.js +1 -1
  19. package/out/concepts/Variable__.js.map +1 -1
  20. package/out/concepts/View__.js +2 -2
  21. package/out/concepts/View__.js.map +1 -1
  22. package/out/generator/genReleaseBody.js +1 -1
  23. package/out/generator/genReleaseBody.js.map +1 -1
  24. package/out/server/naslServer.d.ts +2 -1
  25. package/out/server/naslServer.js +92 -40
  26. package/out/server/naslServer.js.map +1 -1
  27. package/out/templator/genQueryComponent.d.ts +2 -2
  28. package/out/templator/genQueryComponent.js +4 -1
  29. package/out/templator/genQueryComponent.js.map +1 -1
  30. package/package.json +2 -3
  31. package/src/bak/translator.js +1 -1
  32. package/src/concepts/CallInterface__.ts +9 -9
  33. package/src/concepts/Destination__.ts +9 -9
  34. package/src/concepts/ExternalDestination__.ts +5 -5
  35. package/src/concepts/LogicItem__.ts +4 -0
  36. package/src/concepts/Logic__.ts +42 -5
  37. package/src/concepts/Match__.ts +21 -1
  38. package/src/concepts/MemberExpression__.ts +11 -0
  39. package/src/concepts/OqlQueryComponent__.ts +7 -0
  40. package/src/concepts/Param__.ts +3 -2
  41. package/src/concepts/Return__.ts +5 -1
  42. package/src/concepts/StringInterpolation__.ts +26 -26
  43. package/src/concepts/UnaryExpression__.ts +4 -4
  44. package/src/concepts/ValidationRule__.ts +1 -1
  45. package/src/concepts/Variable__.ts +1 -1
  46. package/src/concepts/ViewElement__.ts +1 -1
  47. package/src/concepts/View__.ts +2 -2
  48. package/src/generator/genReleaseBody.ts +1 -1
  49. package/src/server/naslServer.ts +91 -41
  50. package/src/templator/genQueryComponent.ts +8 -3
  51. package/ts-worker/lib/cancellationToken.js +67 -0
  52. package/ts-worker/lib/harness.js +1 -1
  53. package/ts-worker/src/start.js +1 -1
  54. package/ts-worker/webpack.config.js +3 -1
@@ -291,7 +291,7 @@ export class Variable extends BaseNode {
291
291
  return code;
292
292
  }
293
293
  isEnum() {
294
- return this.typeAnnotation?.typeNamespace.endsWith('.enums');
294
+ return this.typeAnnotation?.typeNamespace?.endsWith('.enums');
295
295
  }
296
296
  getNamespace() {
297
297
  if (this.parentNode && (this.parentNode as Namespace).getNamespace) {
@@ -1071,7 +1071,7 @@ export class ViewElement extends BaseNode {
1071
1071
  }
1072
1072
 
1073
1073
 
1074
-
1074
+
1075
1075
 
1076
1076
  /**
1077
1077
  * 删除元素绑定属性
@@ -2106,14 +2106,14 @@ export class View extends BaseNode {
2106
2106
  // 调用接口,参数替换成 null
2107
2107
  if (rightNode instanceof CallInterface) {
2108
2108
  const { _interface } = rightNode.getModuleInterface();
2109
- return `${rightNode.tsCalleeKey}(${_interface.params.map(() => 'null').join(',')})`;
2109
+ return _interface ? `${rightNode.tsCalleeKey}(${_interface.params.map(() => 'null').join(',')})` : null;
2110
2110
  }
2111
2111
 
2112
2112
  // 调用全局逻辑,参数替换成 null
2113
2113
  if (rightNode instanceof CallLogic && rightNode.calleeNamespace === 'app.logics') {
2114
2114
  const calleeName = rightNode?.calleeName;
2115
2115
  const calleeLogic = this.app.logics.find((l) => l.name === calleeName);
2116
- return `app.logics.${calleeName}(${calleeLogic.params.map(() => 'null').join(',')})`;
2116
+ return calleeLogic ? `app.logics.${calleeName}(${calleeLogic.params.map(() => 'null').join(',')})` : null;
2117
2117
  }
2118
2118
 
2119
2119
  // view 局部变量,申明提升前增加判断,使用 logic 内的 result 直接替换成对应 logic,logic 内部变量替换成 null
@@ -59,7 +59,7 @@ export async function genReleaseBody(app: App, {
59
59
  const jsonString = JSON.stringify(NaslAnnotatedJSON);
60
60
 
61
61
  // 打印传给服务端的nasl,为了服务端排查问题方便
62
- // console.warn(NaslAnnotatedJSON, '标注后的nasl');
62
+ console.log(NaslAnnotatedJSON, '标注后的nasl');
63
63
  files.push({ name: 'nasl-annotated.json', content: jsonString });
64
64
 
65
65
  body.files = files;
@@ -614,6 +614,48 @@ export class NaslServer {
614
614
  }
615
615
  return '';
616
616
  }
617
+ async getDataSchemaStructureOrTypeAnnotation(node: ViewElement) {
618
+ if (!(node instanceof ViewElement)) return;
619
+
620
+ const { currentSource, fileNode } = this.getCurrentSource(node);
621
+ const quickInfo = await this._getTypeQuickinfo({
622
+ file: (fileNode as FileNode).getEmbeddedFilePath(),
623
+ line: lsp2tspNumber(currentSource.range.start.line),
624
+ offset: lsp2tspNumber(currentSource.range.start.character) + `new nasl.ui.`.length,
625
+ });
626
+ if (quickInfo.responseRequired) {
627
+ const displayString = quickInfo?.response?.displayString || '';
628
+ const flag = displayString.includes('<') && displayString.includes('>');
629
+ if(!flag) return;
630
+
631
+ const types = /\<([^()]+)\>/g.exec(displayString);
632
+ let typeStr = types && types[1];
633
+ const app = node.app;
634
+ if (typeStr.includes('__name: "AStructure_')) {
635
+ const properties: StructureProperty[] = [];
636
+ typeStr
637
+ .replace(/([^:\s]+):\s+([^;]+);/g, ($1, name, typeKey) => {
638
+ if(name === '__name') return;
639
+
640
+ typeKey = `app.${typeKey}`;
641
+ const keys = typeKey.split('.');
642
+ const typeName = keys.pop();
643
+ const typeNamespace = keys.join('.');
644
+ properties.push(StructureProperty.from({
645
+ name,
646
+ typeAnnotation: TypeAnnotation.from({
647
+ typeKind: 'reference',
648
+ typeName,
649
+ typeNamespace,
650
+ })
651
+ }))
652
+ return '';
653
+ });
654
+ return TypeAnnotation.createTypeAnonymousStructure(properties);
655
+ } else if(typeStr.startsWith('structures'))
656
+ return app.findNodeByCompleteName(`app.${typeStr}`);
657
+ }
658
+ }
617
659
 
618
660
  /**
619
661
  * ts的 quickInfo方法,查询指定位置的详情
@@ -997,40 +1039,37 @@ export class NaslServer {
997
1039
  const jsCode = nodeIn.toJS();
998
1040
  if (!jsCode.includes(`${node.name} = `))
999
1041
  return;
1000
- // 逻辑内局部变量、输出参数、输入参数过滤
1001
- if (nodeIn.logic?.variables.find((lVar) => jsCode.includes(`${lVar.name} = `))
1002
- || nodeIn.logic?.returns.find((lRet) => jsCode.includes(`${lRet.name} = `))
1003
- || nodeIn.logic?.params.find((lPar) => jsCode.includes(`${lPar.name} = `))
1004
- || nodeIn.logic?.virtualParams.find((vParam) => jsCode.includes(`${vParam.name} = `)))
1005
- return;
1006
- if (node instanceof Variable
1007
- && node.parentNode instanceof View
1008
- && (nodeIn.logic?.params.find((param) => jsCode.includes(param.name))
1009
- || nodeIn.logic?.virtualParams.find((vParam) => jsCode.includes(vParam.name))
1010
- || nodeIn.logic?.variables.find((variable) => jsCode.includes(variable.name)))) {
1042
+ // 页面局部变量
1043
+ if (node instanceof Variable && node.parentNode instanceof View) {
1044
+ // 跟页面局部变量无关的赋值过滤
1045
+ if (!jsCode.includes(`this.${node.name} = `))
1046
+ return;
1011
1047
  // 直接赋值 logic 内局部变量、输入参数直接提示系统无法推断类型
1012
- if (!nodeIn.tsErrorDetail) {
1013
- const diagnostic = {
1014
- node: nodeIn,
1015
- severity: 'error',
1016
- message: `页面${nodeTypeName} ${node.name} 赋值页面逻辑内局部变量或输入参数时,系统无法推导类型。`,
1017
- };
1018
- nodeIn.tsErrorDetail = diagnostic;
1019
- diagnostics.push(diagnostic);
1020
- }
1021
- msg = '系统无法推断类型。';
1022
- } else {
1023
- if (!nodeIn.tsErrorDetail) {
1024
- const diagnostic = {
1025
- node: nodeIn,
1026
- severity: 'error',
1027
- message: `${nodeIn.label}左边 ${node.name} 未设置类型,右边必须为有返回值的内容。`,
1028
- };
1029
- nodeIn.tsErrorDetail = diagnostic;
1030
- diagnostics.push(diagnostic);
1048
+ if (nodeIn.logic?.params.find((param) => jsCode.includes(param.name))
1049
+ || nodeIn.logic?.virtualParams.find((vParam) => jsCode.includes(vParam.name))
1050
+ || nodeIn.logic?.variables.find((variable) => jsCode.includes(variable.name))) {
1051
+ if (!nodeIn.tsErrorDetail) {
1052
+ const diagnostic = {
1053
+ node: nodeIn,
1054
+ severity: 'error',
1055
+ message: `页面${nodeTypeName} ${node.name} 赋值页面逻辑内局部变量或输入参数时,系统无法推导类型。`,
1056
+ };
1057
+ nodeIn.tsErrorDetail = diagnostic;
1058
+ diagnostics.push(diagnostic);
1059
+ }
1060
+ msg = '系统无法推断类型。';
1031
1061
  }
1032
- msg = '必须赋值有返回值的内容。';
1033
1062
  }
1063
+ if (!nodeIn.tsErrorDetail) {
1064
+ const diagnostic = {
1065
+ node: nodeIn,
1066
+ severity: 'error',
1067
+ message: `${nodeIn.label}左边 ${node.name} 未设置类型,右边必须为有返回值的内容。`,
1068
+ };
1069
+ nodeIn.tsErrorDetail = diagnostic;
1070
+ diagnostics.push(diagnostic);
1071
+ }
1072
+ msg = '必须赋值有返回值的内容。';
1034
1073
  }
1035
1074
  });
1036
1075
  if (!msg)
@@ -2330,7 +2369,6 @@ export class NaslServer {
2330
2369
  const nodeTypeAnnotation = this.getCurrentNodeKnownTypeAnnotation(node);
2331
2370
  if (nodeTypeAnnotation) {
2332
2371
  types.set(node, nodeTypeAnnotation);
2333
- node.__TypeAnnotation = nodeTypeAnnotation;
2334
2372
  return;
2335
2373
  }
2336
2374
  // 要通过自己或者依赖关系拿的,二次遍历
@@ -2344,8 +2382,8 @@ export class NaslServer {
2344
2382
  if ([
2345
2383
  'Return',
2346
2384
  'Variable',
2347
- ].includes(node.concept) && (node as Return | Variable).typeAnnotation) {
2348
- node.__TypeAnnotation = (node as Return | Variable).typeAnnotation;
2385
+ ].includes(node.concept) && (node as Variable).typeAnnotation) {
2386
+ types.set(node, (node as Variable).typeAnnotation);
2349
2387
  return;
2350
2388
  }
2351
2389
  if (![
@@ -2408,7 +2446,9 @@ export class NaslServer {
2408
2446
  }
2409
2447
  args.push(fileDetail);
2410
2448
  });
2449
+ console.time('请求')
2411
2450
  const resultMap: any = (await this.getNaslNodeTypeFull(args)).response;
2451
+ console.timeEnd('请求')
2412
2452
 
2413
2453
  // console.log(resultMap);
2414
2454
 
@@ -2424,8 +2464,7 @@ export class NaslServer {
2424
2464
  const nodeTypeAnnotation = type2TypeAnnotation(item?.[0]?.nodeType);
2425
2465
  // console.log(newQuickInfoNodes[index].node, nodeTypeAnnotation, nodeTypeAnnotation?.typeKey);
2426
2466
  types.set(newQuickInfoNodes[index].node, nodeTypeAnnotation);
2427
- newQuickInfoNodes[index].node.__TypeAnnotation = nodeTypeAnnotation;
2428
- (newQuickInfoNodes[index].node as any).__nodeType = item?.[0]?.nodeType;
2467
+ (newQuickInfoNodes[index].node as any).__nodeType = Object.freeze(item?.[0]?.nodeType);
2429
2468
 
2430
2469
  // console.log('方案1:', displayString1);
2431
2470
  });
@@ -2433,13 +2472,11 @@ export class NaslServer {
2433
2472
  if (node instanceof Assignment) {
2434
2473
  if (!types.get(node.left)) {
2435
2474
  types.set(node.left, types.get(node.right));
2436
- node.left.__TypeAnnotation = (types.get(node.right) as TypeAnnotation);
2437
2475
  }
2438
2476
  } else if (node instanceof Argument) {
2439
2477
  // 如果Argument,但是没可以用的类型,就用原来logic的参数类型
2440
2478
  const argType = this.getArgumentTypeAnnotation(node, newQuickInfoNodes, (types as any));
2441
2479
  types.set(node, argType);
2442
- node.__TypeAnnotation = argType.typeAnnotation;
2443
2480
  } else if (node instanceof MatchCase) {
2444
2481
  // matchCase的类型
2445
2482
  // 直接从最后一项的返回值取,有就有没有就没有
@@ -2447,15 +2484,26 @@ export class NaslServer {
2447
2484
  const last = node.body[node.body.length - 1];
2448
2485
  if (last.__TypeAnnotation) {
2449
2486
  types.set(node, last.__TypeAnnotation);
2450
- node.__TypeAnnotation = last.__TypeAnnotation;
2451
2487
  }
2452
2488
  }
2453
2489
  }
2454
2490
  });
2455
2491
 
2456
- // 重置类型状态
2492
+ // 重置类型状态, 并且赋值
2457
2493
  types.forEach((value, node) => {
2458
- node.__isCorrectTypeAnnotation = true;
2494
+ try {
2495
+ // 因为node可能是经过处理的TypeAnnotation
2496
+ node.__isCorrectTypeAnnotation = true;
2497
+ if (value) {
2498
+ if (value instanceof TypeAnnotation) {
2499
+ node.__TypeAnnotation = (Object.freeze(value) as TypeAnnotation);
2500
+ } else if (value.typeAnnotation) {
2501
+ node.__TypeAnnotation = (Object.freeze(value.typeAnnotation) as TypeAnnotation);
2502
+ }
2503
+ }
2504
+ } catch (err) {
2505
+ console.log(err);
2506
+ }
2459
2507
  });
2460
2508
  // newQuickInfoNodes.forEach((itemDetail, index) => {
2461
2509
  // const { node, filePath } = itemDetail;
@@ -2561,6 +2609,7 @@ export class NaslServer {
2561
2609
 
2562
2610
  // 增量标注
2563
2611
  async IncrementalAnnotationJSON(records: DiagnosticRecord[]) {
2612
+ console.time('增量标注');
2564
2613
  const nodes: QuickInfoNodes = [];
2565
2614
  records.forEach((record) => {
2566
2615
  const fileNode = this.file2NodeMap.get(record.filePath);
@@ -2580,6 +2629,7 @@ export class NaslServer {
2580
2629
  });
2581
2630
 
2582
2631
  await this.getQuickInfoNodesTypeMap(nodes);
2632
+ console.timeEnd('增量标注');
2583
2633
  }
2584
2634
 
2585
2635
  annotationToJson(typesMap: Map<BaseNode, TypeAnnotation | {
@@ -1,5 +1,5 @@
1
1
  import * as utils from '../utils';
2
- import { App, Module, Namespace, Entity, Structure, QueryJoinExpression, CallQueryComponent, StructureProperty, DataSource } from '../concepts';
2
+ import { App, Module, Namespace, Entity, Structure, QueryJoinExpression, CallQueryComponent, StructureProperty, DataSource, TypeAnnotation } from '../concepts';
3
3
  import { NameGroup, NaslCoreTypeAnnotation, NaslCollectionTypeAnnotation, NaslTypeAnnotation, NaslQueryExpression, NaslLogicItem, NaslNode } from './utils';
4
4
 
5
5
  export function genQueryStructure(allEntities: Array<Entity>, nameGroup: NameGroup) {
@@ -205,7 +205,7 @@ export function genQueryLogic(allEntities: Array<Entity>, nameGroup: NameGroup,
205
205
  return logic;
206
206
  }
207
207
 
208
- export async function joinEntity(callQueryComponent: CallQueryComponent, entity: Entity, recordStructure: Structure) {
208
+ export async function joinEntity(callQueryComponent: CallQueryComponent, entity: Entity, recordStructure: Structure | TypeAnnotation) {
209
209
  const dataSource = entity.parentNode as DataSource;
210
210
  const from = callQueryComponent.from;
211
211
  let joinInfo;
@@ -276,7 +276,12 @@ export async function joinEntity(callQueryComponent: CallQueryComponent, entity:
276
276
  typeName: entity.name,
277
277
  }),
278
278
  }), recordStructure, 'properties');
279
- recordStructure.addProperty(structureProperty);
279
+
280
+ if(recordStructure instanceof Structure)
281
+ recordStructure.addProperty(structureProperty);
282
+ else if(recordStructure instanceof TypeAnnotation)
283
+ recordStructure.properties.push(structureProperty);
284
+
280
285
  const newJoinPartLogicItem = QueryJoinExpression.from(queryJoinExpression, callQueryComponent, 'joinParts');
281
286
  from.addJoinPart(newJoinPartLogicItem);
282
287
  callQueryComponent.saveStructure();
@@ -0,0 +1,67 @@
1
+ /*! *****************************************************************************
2
+ Copyright (c) Microsoft Corporation. All rights reserved.
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
+ this file except in compliance with the License. You may obtain a copy of the
5
+ License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8
+ KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9
+ WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10
+ MERCHANTABLITY OR NON-INFRINGEMENT.
11
+
12
+ See the Apache Version 2.0 License for specific language governing permissions
13
+ and limitations under the License.
14
+ ***************************************************************************** */
15
+
16
+
17
+ "use strict";
18
+ var fs = require("fs");
19
+ function pipeExists(name) {
20
+ return fs.existsSync(name);
21
+ }
22
+ function createCancellationToken(args) {
23
+ var cancellationPipeName;
24
+ for (var i = 0; i < args.length - 1; i++) {
25
+ if (args[i] === "--cancellationPipeName") {
26
+ cancellationPipeName = args[i + 1];
27
+ break;
28
+ }
29
+ }
30
+ if (!cancellationPipeName) {
31
+ return {
32
+ isCancellationRequested: function () { return false; },
33
+ setRequest: function (_requestId) { return void 0; },
34
+ resetRequest: function (_requestId) { return void 0; }
35
+ };
36
+ }
37
+ if (cancellationPipeName.charAt(cancellationPipeName.length - 1) === "*") {
38
+ var namePrefix_1 = cancellationPipeName.slice(0, -1);
39
+ if (namePrefix_1.length === 0 || namePrefix_1.indexOf("*") >= 0) {
40
+ throw new Error("Invalid name for template cancellation pipe: it should have length greater than 2 characters and contain only one '*'.");
41
+ }
42
+ var perRequestPipeName_1;
43
+ var currentRequestId_1;
44
+ return {
45
+ isCancellationRequested: function () { return perRequestPipeName_1 !== undefined && pipeExists(perRequestPipeName_1); },
46
+ setRequest: function (requestId) {
47
+ currentRequestId_1 = requestId;
48
+ perRequestPipeName_1 = namePrefix_1 + requestId;
49
+ },
50
+ resetRequest: function (requestId) {
51
+ if (currentRequestId_1 !== requestId) {
52
+ throw new Error("Mismatched request id, expected ".concat(currentRequestId_1, ", actual ").concat(requestId));
53
+ }
54
+ perRequestPipeName_1 = undefined;
55
+ }
56
+ };
57
+ }
58
+ else {
59
+ return {
60
+ isCancellationRequested: function () { return pipeExists(cancellationPipeName); },
61
+ setRequest: function (_requestId) { return void 0; },
62
+ resetRequest: function (_requestId) { return void 0; }
63
+ };
64
+ }
65
+ }
66
+ module.exports = createCancellationToken;
67
+ //# sourceMappingURL=cancellationToken.js.map
@@ -1,4 +1,4 @@
1
- const ts = require('typescript/lib/tsserver');
1
+ const ts = require('../lib/tsserver');
2
2
 
3
3
  'use strict';
4
4
  const __generator = (this && this.__generator) || function (thisArg, body) {
@@ -1,4 +1,4 @@
1
- const ts = require('typescript/lib/tsserver');
1
+ const ts = require('../lib/tsserver');
2
2
  require('../lib/harness');
3
3
 
4
4
  const tsconfig = require('../../sandbox/tsconfig.json');
@@ -27,10 +27,12 @@ module.exports = {
27
27
  // DISABLE Webpack's built-in process and Buffer polyfills!
28
28
  fallback: {
29
29
  fs: false,
30
+ os: false,
31
+ inspector: false,
30
32
  // fs: 'browserfs/dist/shims/fs.js',
31
33
  crypto: require.resolve('crypto-browserify'),
32
34
  stream: require.resolve('stream-browserify'),
33
- // path: require.resolve('path-browserify'),
35
+ path: require.resolve('path-browserify'),
34
36
  // processGlobal: 'browserfs/dist/shims/process.js',
35
37
  // bufferGlobal: 'browserfs/dist/shims/bufferGlobal.js',
36
38
  // bfsGlobal: require.resolve('browserfs'),