@dronedeploy/rocos-js-sdk 3.0.1-alpha.20 → 3.0.1-alpha.22
Sign up to get free protection for your applications and to get access to all the features.
- package/cjs/helpers/index.d.ts +1 -0
- package/cjs/helpers/index.js +1 -0
- package/cjs/helpers/kscript/Context.d.ts +34 -0
- package/cjs/helpers/kscript/Context.js +52 -0
- package/cjs/helpers/kscript/index.d.ts +1 -0
- package/cjs/helpers/kscript/index.js +27 -0
- package/cjs/helpers/kscript/kscript.d.ts +6 -0
- package/cjs/helpers/kscript/kscript.js +115 -0
- package/cjs/helpers/kscript/nodes/Identifier.d.ts +12 -0
- package/cjs/helpers/kscript/nodes/Identifier.js +20 -0
- package/cjs/helpers/kscript/nodes/Literal.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/Literal.js +22 -0
- package/cjs/helpers/kscript/nodes/Node.d.ts +9 -0
- package/cjs/helpers/kscript/nodes/Node.js +15 -0
- package/cjs/helpers/kscript/nodes/Program.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/Program.js +24 -0
- package/cjs/helpers/kscript/nodes/TemplateLiteral.d.ts +23 -0
- package/cjs/helpers/kscript/nodes/TemplateLiteral.js +35 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +7 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrayExpression.js +31 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +14 -0
- package/cjs/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +40 -0
- package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +16 -0
- package/cjs/helpers/kscript/nodes/expressions/BinaryExpression.js +104 -0
- package/cjs/helpers/kscript/nodes/expressions/CallExpression.d.ts +8 -0
- package/cjs/helpers/kscript/nodes/expressions/CallExpression.js +38 -0
- package/cjs/helpers/kscript/nodes/expressions/ChainExpression.d.ts +16 -0
- package/cjs/helpers/kscript/nodes/expressions/ChainExpression.js +25 -0
- package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +14 -0
- package/cjs/helpers/kscript/nodes/expressions/ConditionalExpression.js +30 -0
- package/cjs/helpers/kscript/nodes/expressions/ExpressionStatement.d.ts +7 -0
- package/cjs/helpers/kscript/nodes/expressions/ExpressionStatement.js +18 -0
- package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +10 -0
- package/cjs/helpers/kscript/nodes/expressions/LogicalExpression.js +47 -0
- package/cjs/helpers/kscript/nodes/expressions/MemberExpression.d.ts +44 -0
- package/cjs/helpers/kscript/nodes/expressions/MemberExpression.js +98 -0
- package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/expressions/ObjectExpression.js +59 -0
- package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +12 -0
- package/cjs/helpers/kscript/nodes/expressions/UnaryExpression.js +44 -0
- package/cjs/helpers/kscript/nodes/expressions/index.d.ts +11 -0
- package/cjs/helpers/kscript/nodes/expressions/index.js +28 -0
- package/cjs/helpers/kscript/nodes/index.d.ts +4 -0
- package/cjs/helpers/kscript/nodes/index.js +10 -0
- package/cjs/helpers/kscript/nodes/nodeTypes.d.ts +33 -0
- package/cjs/helpers/kscript/nodes/nodeTypes.js +29 -0
- package/cjs/helpers/kscript/utils.d.ts +4 -0
- package/cjs/helpers/kscript/utils.js +15 -0
- package/cjs/services/AuthService.d.ts +3 -3
- package/cjs/services/AuthService.js +13 -7
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/kscript/Context.d.ts +34 -0
- package/esm/helpers/kscript/Context.js +49 -0
- package/esm/helpers/kscript/index.d.ts +1 -0
- package/esm/helpers/kscript/index.js +1 -0
- package/esm/helpers/kscript/kscript.d.ts +6 -0
- package/esm/helpers/kscript/kscript.js +107 -0
- package/esm/helpers/kscript/nodes/Identifier.d.ts +12 -0
- package/esm/helpers/kscript/nodes/Identifier.js +14 -0
- package/esm/helpers/kscript/nodes/Literal.d.ts +11 -0
- package/esm/helpers/kscript/nodes/Literal.js +16 -0
- package/esm/helpers/kscript/nodes/Node.d.ts +9 -0
- package/esm/helpers/kscript/nodes/Node.js +12 -0
- package/esm/helpers/kscript/nodes/Program.d.ts +11 -0
- package/esm/helpers/kscript/nodes/Program.js +18 -0
- package/esm/helpers/kscript/nodes/TemplateLiteral.d.ts +23 -0
- package/esm/helpers/kscript/nodes/TemplateLiteral.js +29 -0
- package/esm/helpers/kscript/nodes/expressions/ArrayExpression.d.ts +7 -0
- package/esm/helpers/kscript/nodes/expressions/ArrayExpression.js +25 -0
- package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.d.ts +14 -0
- package/esm/helpers/kscript/nodes/expressions/ArrowFunctionExpression.js +34 -0
- package/esm/helpers/kscript/nodes/expressions/BinaryExpression.d.ts +16 -0
- package/esm/helpers/kscript/nodes/expressions/BinaryExpression.js +98 -0
- package/esm/helpers/kscript/nodes/expressions/CallExpression.d.ts +8 -0
- package/esm/helpers/kscript/nodes/expressions/CallExpression.js +32 -0
- package/esm/helpers/kscript/nodes/expressions/ChainExpression.d.ts +16 -0
- package/esm/helpers/kscript/nodes/expressions/ChainExpression.js +19 -0
- package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.d.ts +14 -0
- package/esm/helpers/kscript/nodes/expressions/ConditionalExpression.js +24 -0
- package/esm/helpers/kscript/nodes/expressions/ExpressionStatement.d.ts +7 -0
- package/esm/helpers/kscript/nodes/expressions/ExpressionStatement.js +12 -0
- package/esm/helpers/kscript/nodes/expressions/LogicalExpression.d.ts +10 -0
- package/esm/helpers/kscript/nodes/expressions/LogicalExpression.js +41 -0
- package/esm/helpers/kscript/nodes/expressions/MemberExpression.d.ts +44 -0
- package/esm/helpers/kscript/nodes/expressions/MemberExpression.js +92 -0
- package/esm/helpers/kscript/nodes/expressions/ObjectExpression.d.ts +11 -0
- package/esm/helpers/kscript/nodes/expressions/ObjectExpression.js +53 -0
- package/esm/helpers/kscript/nodes/expressions/UnaryExpression.d.ts +12 -0
- package/esm/helpers/kscript/nodes/expressions/UnaryExpression.js +38 -0
- package/esm/helpers/kscript/nodes/expressions/index.d.ts +11 -0
- package/esm/helpers/kscript/nodes/expressions/index.js +11 -0
- package/esm/helpers/kscript/nodes/index.d.ts +4 -0
- package/esm/helpers/kscript/nodes/index.js +3 -0
- package/esm/helpers/kscript/nodes/nodeTypes.d.ts +33 -0
- package/esm/helpers/kscript/nodes/nodeTypes.js +24 -0
- package/esm/helpers/kscript/utils.d.ts +4 -0
- package/esm/helpers/kscript/utils.js +11 -0
- package/esm/services/AuthService.d.ts +3 -3
- package/esm/services/AuthService.js +13 -7
- package/package.json +2 -1
@@ -0,0 +1,8 @@
|
|
1
|
+
import { AnyNode, CallExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class CallExpressionNode extends Node<CallExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
private spreadArgs;
|
8
|
+
}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
export default class CallExpressionNode extends Node {
|
4
|
+
constructor(node, context) {
|
5
|
+
super(node, context, 'CallExpression');
|
6
|
+
}
|
7
|
+
run() {
|
8
|
+
if (!this.context.isFunctionCallAllowed)
|
9
|
+
throw new Error('Function calls are not allowed');
|
10
|
+
const callee = construct(this.node.callee, this.context).run();
|
11
|
+
let thisArg = {};
|
12
|
+
if (this.node.callee.type === 'MemberExpression') {
|
13
|
+
thisArg = construct(this.node.callee.object, this.context).run();
|
14
|
+
}
|
15
|
+
if (typeof callee !== 'function') {
|
16
|
+
throw new Error('callee is not a function');
|
17
|
+
}
|
18
|
+
const args = this.spreadArgs(this.node.arguments);
|
19
|
+
return callee.call(thisArg, ...args);
|
20
|
+
}
|
21
|
+
spreadArgs(args) {
|
22
|
+
return args.reduce((acc, arg) => {
|
23
|
+
if (arg.type === 'SpreadElement') {
|
24
|
+
acc.push(...construct(arg.argument, this.context).run());
|
25
|
+
}
|
26
|
+
else {
|
27
|
+
acc.push(construct(arg, this.context).run());
|
28
|
+
}
|
29
|
+
return acc;
|
30
|
+
}, []);
|
31
|
+
}
|
32
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { AnyNode, ChainExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/**
|
5
|
+
* A ChainExpression node is created when the chain operator is used.
|
6
|
+
*
|
7
|
+
* e.g. `a?.b?.c`
|
8
|
+
*
|
9
|
+
* This node does not need to do anything special, as the chain operator is handled by the MemberExpressionNode.
|
10
|
+
*
|
11
|
+
* @see MemberExpressionNode
|
12
|
+
*/
|
13
|
+
export default class ExpressionStatementNode extends Node<ChainExpression> {
|
14
|
+
constructor(node: AnyNode, context: Context);
|
15
|
+
run(): unknown;
|
16
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
/**
|
4
|
+
* A ChainExpression node is created when the chain operator is used.
|
5
|
+
*
|
6
|
+
* e.g. `a?.b?.c`
|
7
|
+
*
|
8
|
+
* This node does not need to do anything special, as the chain operator is handled by the MemberExpressionNode.
|
9
|
+
*
|
10
|
+
* @see MemberExpressionNode
|
11
|
+
*/
|
12
|
+
export default class ExpressionStatementNode extends Node {
|
13
|
+
constructor(node, context) {
|
14
|
+
super(node, context, 'ChainExpression');
|
15
|
+
}
|
16
|
+
run() {
|
17
|
+
return construct(this.node.expression, this.context).run();
|
18
|
+
}
|
19
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { AnyNode, ConditionalExpression, IfStatement } from 'acorn';
|
2
|
+
import type Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/** Represents a ternary conditional expression (a ? b : c) or an if statement (if (a) b else c).
|
5
|
+
*
|
6
|
+
* If a (test) is truthy, b (consequent) is returned. Otherwise, c (alternate) is returned.
|
7
|
+
*
|
8
|
+
* Since the if statement and the conditional expression are so similar, they are both handled by this node,
|
9
|
+
* even though they are separate in the AST.
|
10
|
+
*/
|
11
|
+
export default class ConditionalExpressionNode extends Node<IfStatement | ConditionalExpression> {
|
12
|
+
constructor(node: AnyNode, context: Context);
|
13
|
+
run(): unknown;
|
14
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
/** Represents a ternary conditional expression (a ? b : c) or an if statement (if (a) b else c).
|
4
|
+
*
|
5
|
+
* If a (test) is truthy, b (consequent) is returned. Otherwise, c (alternate) is returned.
|
6
|
+
*
|
7
|
+
* Since the if statement and the conditional expression are so similar, they are both handled by this node,
|
8
|
+
* even though they are separate in the AST.
|
9
|
+
*/
|
10
|
+
export default class ConditionalExpressionNode extends Node {
|
11
|
+
constructor(node, context) {
|
12
|
+
super(node, context, ['ConditionalExpression', 'IfStatement']);
|
13
|
+
}
|
14
|
+
run() {
|
15
|
+
const test = construct(this.node.test, this.context).run();
|
16
|
+
if (test) {
|
17
|
+
return construct(this.node.consequent, this.context).run();
|
18
|
+
}
|
19
|
+
if (this.node.alternate === null || this.node.alternate === undefined) {
|
20
|
+
return undefined;
|
21
|
+
}
|
22
|
+
return construct(this.node.alternate, this.context).run();
|
23
|
+
}
|
24
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { AnyNode, ExpressionStatement } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class ExpressionStatementNode extends Node<ExpressionStatement> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
export default class ExpressionStatementNode extends Node {
|
4
|
+
constructor(node, context) {
|
5
|
+
super(node, context, 'ExpressionStatement');
|
6
|
+
}
|
7
|
+
run() {
|
8
|
+
if (this.node.directive)
|
9
|
+
throw new Error('Directives are not supported');
|
10
|
+
return construct(this.node.expression, this.context).run();
|
11
|
+
}
|
12
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { AnyNode, LogicalExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class BinaryExpressionNode extends Node<LogicalExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
/** Try and skip evaluating the right operand */
|
8
|
+
private shortCircuit;
|
9
|
+
private operate;
|
10
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
export default class BinaryExpressionNode extends Node {
|
4
|
+
constructor(node, context) {
|
5
|
+
super(node, context, 'LogicalExpression');
|
6
|
+
}
|
7
|
+
run() {
|
8
|
+
const left = construct(this.node.left, this.context).run();
|
9
|
+
const shortCircuit = this.shortCircuit(left);
|
10
|
+
if (shortCircuit !== null) {
|
11
|
+
return shortCircuit;
|
12
|
+
}
|
13
|
+
const right = construct(this.node.right, this.context).run();
|
14
|
+
return this.operate(left, right);
|
15
|
+
}
|
16
|
+
/** Try and skip evaluating the right operand */
|
17
|
+
shortCircuit(left) {
|
18
|
+
if (this.node.operator === '&&' && !left) {
|
19
|
+
return false;
|
20
|
+
}
|
21
|
+
if (this.node.operator === '||' && left) {
|
22
|
+
return true;
|
23
|
+
}
|
24
|
+
if (this.node.operator === '??' && left !== null && left !== undefined) {
|
25
|
+
return left;
|
26
|
+
}
|
27
|
+
return null;
|
28
|
+
}
|
29
|
+
operate(left, right) {
|
30
|
+
switch (this.node.operator) {
|
31
|
+
case '||':
|
32
|
+
return left || right;
|
33
|
+
case '&&':
|
34
|
+
return left && right;
|
35
|
+
case '??':
|
36
|
+
return left ?? right;
|
37
|
+
default:
|
38
|
+
throw new Error(`Unsupported operator: ${this.node.operator}`);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { AnyNode, MemberExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/**
|
5
|
+
* A member expression is an expression that accesses a property of an object.
|
6
|
+
*
|
7
|
+
* Examples of member expressions are:
|
8
|
+
* - `a.b`
|
9
|
+
* - `a['b']`
|
10
|
+
* - `a[0]`
|
11
|
+
* - `a?.b`
|
12
|
+
* - `a?.['b']`
|
13
|
+
* - `a?.[0]`
|
14
|
+
*
|
15
|
+
* A member access in the sandbox is considered secure if the following conditions are met:
|
16
|
+
* - The property is not named 'constructor'
|
17
|
+
* - Either:
|
18
|
+
* * The property is owned by the object and is *not* on the whitelist.
|
19
|
+
* * The prototype is whitelisted and the property list is null or contains the property.
|
20
|
+
*
|
21
|
+
* @see Context.isPrototypeAllowed
|
22
|
+
*/
|
23
|
+
export default class MemberExpressionNode extends Node<MemberExpression> {
|
24
|
+
constructor(node: AnyNode, context: Context);
|
25
|
+
run(): unknown;
|
26
|
+
/** Returns the name of the property being accessed.
|
27
|
+
*
|
28
|
+
* Handles both computed and non-computed properties.
|
29
|
+
*/
|
30
|
+
private getPropertyIdentifier;
|
31
|
+
/** Checks if the property can be accessed on the object.
|
32
|
+
*
|
33
|
+
* @throws {Error} If the property is not allowed to be accessed.
|
34
|
+
*/
|
35
|
+
private checkPropertyAccess;
|
36
|
+
/** Searches the prototype chain of the object for the prototype that defines the property. */
|
37
|
+
private getPropertyPrototype;
|
38
|
+
/**
|
39
|
+
* Returns true if the property exists on the object.
|
40
|
+
*
|
41
|
+
* i.e. the property is defined somewhere on the prototype chain.
|
42
|
+
*/
|
43
|
+
private propertyExists;
|
44
|
+
}
|
@@ -0,0 +1,92 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
/**
|
4
|
+
* A member expression is an expression that accesses a property of an object.
|
5
|
+
*
|
6
|
+
* Examples of member expressions are:
|
7
|
+
* - `a.b`
|
8
|
+
* - `a['b']`
|
9
|
+
* - `a[0]`
|
10
|
+
* - `a?.b`
|
11
|
+
* - `a?.['b']`
|
12
|
+
* - `a?.[0]`
|
13
|
+
*
|
14
|
+
* A member access in the sandbox is considered secure if the following conditions are met:
|
15
|
+
* - The property is not named 'constructor'
|
16
|
+
* - Either:
|
17
|
+
* * The property is owned by the object and is *not* on the whitelist.
|
18
|
+
* * The prototype is whitelisted and the property list is null or contains the property.
|
19
|
+
*
|
20
|
+
* @see Context.isPrototypeAllowed
|
21
|
+
*/
|
22
|
+
export default class MemberExpressionNode extends Node {
|
23
|
+
constructor(node, context) {
|
24
|
+
super(node, context, 'MemberExpression');
|
25
|
+
}
|
26
|
+
run() {
|
27
|
+
const object = construct(this.node.object, this.context).run();
|
28
|
+
if (object === null || object === undefined) {
|
29
|
+
if (this.node.optional)
|
30
|
+
return undefined;
|
31
|
+
throw new Error(`Cannot read property of ${object}`);
|
32
|
+
}
|
33
|
+
const propertyIdentifier = this.getPropertyIdentifier();
|
34
|
+
if (!this.propertyExists(object, propertyIdentifier)) {
|
35
|
+
return undefined;
|
36
|
+
}
|
37
|
+
this.checkPropertyAccess(object, propertyIdentifier);
|
38
|
+
return object[propertyIdentifier];
|
39
|
+
}
|
40
|
+
/** Returns the name of the property being accessed.
|
41
|
+
*
|
42
|
+
* Handles both computed and non-computed properties.
|
43
|
+
*/
|
44
|
+
getPropertyIdentifier() {
|
45
|
+
if (this.node.computed) {
|
46
|
+
const name = construct(this.node.property, this.context).run();
|
47
|
+
if (typeof name !== 'string' && typeof name !== 'number') {
|
48
|
+
throw new Error('Only string or number computed properties are supported');
|
49
|
+
}
|
50
|
+
return name;
|
51
|
+
}
|
52
|
+
if (this.node.property.type !== 'Identifier') {
|
53
|
+
throw new Error('Only identifiers are supported for non-computed member expression properties');
|
54
|
+
}
|
55
|
+
return this.node.property.name;
|
56
|
+
}
|
57
|
+
/** Checks if the property can be accessed on the object.
|
58
|
+
*
|
59
|
+
* @throws {Error} If the property is not allowed to be accessed.
|
60
|
+
*/
|
61
|
+
checkPropertyAccess(object, propertyIdentifier) {
|
62
|
+
if (typeof propertyIdentifier === 'number')
|
63
|
+
return;
|
64
|
+
if (propertyIdentifier === 'constructor') {
|
65
|
+
throw new Error('Cannot access constructor property');
|
66
|
+
}
|
67
|
+
const definingProto = this.getPropertyPrototype(object, propertyIdentifier);
|
68
|
+
const ownProperty = Object.prototype.hasOwnProperty.call(object, propertyIdentifier);
|
69
|
+
if (!this.context.isPrototypeAllowed(definingProto, propertyIdentifier, ownProperty)) {
|
70
|
+
throw new Error(`SecurityError: Access to property '${propertyIdentifier}' is not allowed`);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
/** Searches the prototype chain of the object for the prototype that defines the property. */
|
74
|
+
getPropertyPrototype(object, propertyIdentifier) {
|
75
|
+
let proto = object;
|
76
|
+
while (proto !== null) {
|
77
|
+
if (Object.prototype.hasOwnProperty.call(proto, propertyIdentifier)) {
|
78
|
+
return proto;
|
79
|
+
}
|
80
|
+
proto = Object.getPrototypeOf(proto);
|
81
|
+
}
|
82
|
+
return null;
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Returns true if the property exists on the object.
|
86
|
+
*
|
87
|
+
* i.e. the property is defined somewhere on the prototype chain.
|
88
|
+
*/
|
89
|
+
propertyExists(object, propertyIdentifier) {
|
90
|
+
return this.getPropertyPrototype(object, propertyIdentifier) !== null;
|
91
|
+
}
|
92
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { AnyNode, ObjectExpression } from 'acorn';
|
2
|
+
import type Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
export default class ObjectExpressionNode extends Node<ObjectExpression> {
|
5
|
+
constructor(node: AnyNode, context: Context);
|
6
|
+
run(): unknown;
|
7
|
+
private addProperty;
|
8
|
+
private spreadProperty;
|
9
|
+
private getKey;
|
10
|
+
private getStaticKey;
|
11
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
export default class ObjectExpressionNode extends Node {
|
4
|
+
constructor(node, context) {
|
5
|
+
super(node, context, 'ObjectExpression');
|
6
|
+
}
|
7
|
+
run() {
|
8
|
+
const object = {};
|
9
|
+
for (const property of this.node.properties) {
|
10
|
+
if (property.type === 'SpreadElement') {
|
11
|
+
this.spreadProperty(property, object);
|
12
|
+
continue;
|
13
|
+
}
|
14
|
+
this.addProperty(property, object);
|
15
|
+
}
|
16
|
+
return object;
|
17
|
+
}
|
18
|
+
addProperty(property, object) {
|
19
|
+
if (property.kind !== 'init')
|
20
|
+
throw new Error('Only init properties are supported');
|
21
|
+
const key = this.getKey(property);
|
22
|
+
object[key] = construct(property.value, this.context).run();
|
23
|
+
}
|
24
|
+
spreadProperty(property, object) {
|
25
|
+
const value = construct(property.argument, this.context).run();
|
26
|
+
if (!(typeof value === 'object' && value !== null)) {
|
27
|
+
throw new Error('Spread argument must be an object');
|
28
|
+
}
|
29
|
+
Object.assign(object, value);
|
30
|
+
}
|
31
|
+
getKey(property) {
|
32
|
+
let key;
|
33
|
+
if (property.computed) {
|
34
|
+
key = construct(property.key, this.context).run();
|
35
|
+
}
|
36
|
+
else {
|
37
|
+
key = this.getStaticKey(property.key);
|
38
|
+
}
|
39
|
+
if (key === null || key === undefined)
|
40
|
+
throw new Error('Key cannot be null or undefined');
|
41
|
+
if (typeof key !== 'string' && typeof key !== 'number' && typeof key !== 'symbol') {
|
42
|
+
throw new Error(`Unsupported key type: ${typeof key}`);
|
43
|
+
}
|
44
|
+
return key;
|
45
|
+
}
|
46
|
+
getStaticKey(key) {
|
47
|
+
if (key.type === 'Identifier')
|
48
|
+
return key.name;
|
49
|
+
if (key.type === 'Literal')
|
50
|
+
return key.value;
|
51
|
+
throw new Error('Only identifier and literal keys are supported');
|
52
|
+
}
|
53
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { AnyNode, UnaryExpression } from 'acorn';
|
2
|
+
import Context from '../../Context';
|
3
|
+
import Node from '../Node';
|
4
|
+
/** A unary expression is an expression that consists of a single operand and an operator
|
5
|
+
*
|
6
|
+
* e.g. `+1`, `-2`, `!true`, `typeof x`
|
7
|
+
*/
|
8
|
+
export default class UnaryExpressionNode extends Node<UnaryExpression> {
|
9
|
+
constructor(node: AnyNode, context: Context);
|
10
|
+
run(): unknown;
|
11
|
+
private arithmetic;
|
12
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
import Node from '../Node';
|
2
|
+
import { construct } from '../../utils';
|
3
|
+
/** A unary expression is an expression that consists of a single operand and an operator
|
4
|
+
*
|
5
|
+
* e.g. `+1`, `-2`, `!true`, `typeof x`
|
6
|
+
*/
|
7
|
+
export default class UnaryExpressionNode extends Node {
|
8
|
+
constructor(node, context) {
|
9
|
+
super(node, context, 'UnaryExpression');
|
10
|
+
}
|
11
|
+
run() {
|
12
|
+
const argument = construct(this.node.argument, this.context).run();
|
13
|
+
switch (this.node.operator) {
|
14
|
+
case '+':
|
15
|
+
case '-':
|
16
|
+
return this.arithmetic(this.node.operator, argument);
|
17
|
+
case '!':
|
18
|
+
return !argument;
|
19
|
+
case 'typeof':
|
20
|
+
return typeof argument;
|
21
|
+
default:
|
22
|
+
throw new Error(`Unsupported operator: ${this.node.operator}`);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
arithmetic(operator, argument) {
|
26
|
+
if (typeof argument !== 'number') {
|
27
|
+
throw new Error('Unary arithmetic operators are only supported for numbers');
|
28
|
+
}
|
29
|
+
switch (operator) {
|
30
|
+
case '+':
|
31
|
+
return +argument;
|
32
|
+
case '-':
|
33
|
+
return -argument;
|
34
|
+
default:
|
35
|
+
throw new Error(`Unsupported operator: ${operator}`);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export { default as ExpressionStatement } from './ExpressionStatement';
|
2
|
+
export { default as ArrayExpression } from './ArrayExpression';
|
3
|
+
export { default as ObjectExpression } from './ObjectExpression';
|
4
|
+
export { default as BinaryExpression } from './BinaryExpression';
|
5
|
+
export { default as UnaryExpression } from './UnaryExpression';
|
6
|
+
export { default as LogicalExpression } from './LogicalExpression';
|
7
|
+
export { default as ConditionalExpression } from './ConditionalExpression';
|
8
|
+
export { default as CallExpression } from './CallExpression';
|
9
|
+
export { default as ArrowFunctionExpression } from './ArrowFunctionExpression';
|
10
|
+
export { default as MemberExpression } from './MemberExpression';
|
11
|
+
export { default as ChainExpression } from './ChainExpression';
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export { default as ExpressionStatement } from './ExpressionStatement';
|
2
|
+
export { default as ArrayExpression } from './ArrayExpression';
|
3
|
+
export { default as ObjectExpression } from './ObjectExpression';
|
4
|
+
export { default as BinaryExpression } from './BinaryExpression';
|
5
|
+
export { default as UnaryExpression } from './UnaryExpression';
|
6
|
+
export { default as LogicalExpression } from './LogicalExpression';
|
7
|
+
export { default as ConditionalExpression } from './ConditionalExpression';
|
8
|
+
export { default as CallExpression } from './CallExpression';
|
9
|
+
export { default as ArrowFunctionExpression } from './ArrowFunctionExpression';
|
10
|
+
export { default as MemberExpression } from './MemberExpression';
|
11
|
+
export { default as ChainExpression } from './ChainExpression';
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { ArrayExpression, ArrowFunctionExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, ExpressionStatement, LogicalExpression, MemberExpression, ObjectExpression, UnaryExpression } from './expressions';
|
2
|
+
import { AnyNode } from 'acorn';
|
3
|
+
import type Context from '../Context';
|
4
|
+
import Identifier from './Identifier';
|
5
|
+
import Literal from './Literal';
|
6
|
+
import Node from './Node';
|
7
|
+
import Program from './Program';
|
8
|
+
import TemplateLiteral from './TemplateLiteral';
|
9
|
+
type NodeConstructor = {
|
10
|
+
new (node: AnyNode, scope: Context): Node<AnyNode>;
|
11
|
+
};
|
12
|
+
export type NodeMap = Map<AnyNode['type'], NodeConstructor>;
|
13
|
+
declare const nodes: {
|
14
|
+
readonly Program: typeof Program;
|
15
|
+
readonly Literal: typeof Literal;
|
16
|
+
readonly ExpressionStatement: typeof ExpressionStatement;
|
17
|
+
readonly ArrayExpression: typeof ArrayExpression;
|
18
|
+
readonly ObjectExpression: typeof ObjectExpression;
|
19
|
+
readonly Identifier: typeof Identifier;
|
20
|
+
readonly BinaryExpression: typeof BinaryExpression;
|
21
|
+
readonly UnaryExpression: typeof UnaryExpression;
|
22
|
+
readonly LogicalExpression: typeof LogicalExpression;
|
23
|
+
readonly ConditionalExpression: typeof ConditionalExpression;
|
24
|
+
readonly IfStatement: typeof ConditionalExpression;
|
25
|
+
readonly TemplateLiteral: typeof TemplateLiteral;
|
26
|
+
readonly ArrowFunctionExpression: typeof ArrowFunctionExpression;
|
27
|
+
readonly CallExpression: typeof CallExpression;
|
28
|
+
readonly MemberExpression: typeof MemberExpression;
|
29
|
+
readonly ChainExpression: typeof ChainExpression;
|
30
|
+
};
|
31
|
+
export type NodeType = keyof typeof nodes;
|
32
|
+
declare const _default: NodeMap;
|
33
|
+
export default _default;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import { ArrayExpression, ArrowFunctionExpression, BinaryExpression, CallExpression, ChainExpression, ConditionalExpression, ExpressionStatement, LogicalExpression, MemberExpression, ObjectExpression, UnaryExpression, } from './expressions';
|
2
|
+
import Identifier from './Identifier';
|
3
|
+
import Literal from './Literal';
|
4
|
+
import Program from './Program';
|
5
|
+
import TemplateLiteral from './TemplateLiteral';
|
6
|
+
const nodes = {
|
7
|
+
Program,
|
8
|
+
Literal,
|
9
|
+
ExpressionStatement,
|
10
|
+
ArrayExpression,
|
11
|
+
ObjectExpression,
|
12
|
+
Identifier,
|
13
|
+
BinaryExpression,
|
14
|
+
UnaryExpression,
|
15
|
+
LogicalExpression,
|
16
|
+
ConditionalExpression,
|
17
|
+
IfStatement: ConditionalExpression,
|
18
|
+
TemplateLiteral,
|
19
|
+
ArrowFunctionExpression,
|
20
|
+
CallExpression,
|
21
|
+
MemberExpression,
|
22
|
+
ChainExpression,
|
23
|
+
};
|
24
|
+
export default new Map(Object.entries(nodes));
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { nodes } from './nodes';
|
2
|
+
export function construct(node, context) {
|
3
|
+
if (!context.isNodeAllowed(node.type)) {
|
4
|
+
throw new Error(`Blacklisted Operation: ${node.type}`);
|
5
|
+
}
|
6
|
+
const nodeClass = nodes.get(node.type);
|
7
|
+
if (!nodeClass) {
|
8
|
+
throw new Error(`Unknown node type: ${node.type}`);
|
9
|
+
}
|
10
|
+
return Reflect.construct(nodeClass, [node, context]);
|
11
|
+
}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { IBaseService, IRocosSDKConfig, RocosError, Token } from '../models';
|
2
1
|
import { Observable } from 'rxjs';
|
2
|
+
import { IBaseService, IRocosSDKConfig, RocosError, Token } from '../models';
|
3
3
|
import { BaseServiceAbstract } from './BaseServiceAbstract';
|
4
4
|
export declare class AuthService extends BaseServiceAbstract implements IBaseService {
|
5
5
|
protected config: IRocosSDKConfig;
|
@@ -12,9 +12,9 @@ export declare class AuthService extends BaseServiceAbstract implements IBaseSer
|
|
12
12
|
* This is useful for getting notified when the token changes.
|
13
13
|
* i.e. when the token is refreshed from the token refresh checker.
|
14
14
|
*
|
15
|
-
*
|
15
|
+
* It will emit the current token when you subscribe and then emit new tokens when they are set.
|
16
16
|
*/
|
17
|
-
get
|
17
|
+
get token$(): Observable<Token>;
|
18
18
|
getStatus(): boolean;
|
19
19
|
teardown(): void;
|
20
20
|
protected getError(e: RocosError): RocosError;
|
@@ -1,22 +1,23 @@
|
|
1
1
|
import { API_APPLICATION_AUTH_URL, API_OTP_AUTH_URL, API_OTP_URL, API_USER_TOKEN_URL } from '../constants/api';
|
2
|
+
import { BehaviorSubject } from 'rxjs';
|
2
3
|
import { RocosError, Token, errorCodes } from '../models';
|
3
|
-
import { Subject } from 'rxjs';
|
4
4
|
import { BaseServiceAbstract } from './BaseServiceAbstract';
|
5
5
|
import { RocosLogger } from '../logger/RocosLogger';
|
6
6
|
import { RocosStore } from '../store/RocosStore';
|
7
|
+
import { filter } from 'rxjs/operators';
|
7
8
|
import { formatServiceUrl } from '../helpers/formatServiceUrl';
|
8
9
|
import { issuer } from '../constants/auth';
|
9
10
|
export class AuthService extends BaseServiceAbstract {
|
10
11
|
constructor(config) {
|
11
12
|
super(config);
|
12
13
|
this.config = config;
|
13
|
-
this.tokenSubject$ = new
|
14
|
+
this.tokenSubject$ = new BehaviorSubject(null);
|
14
15
|
this.config = config;
|
15
16
|
this.logger = RocosLogger.getInstance(`AuthService(${this.config.url})`);
|
16
17
|
if (this.config.token) {
|
17
18
|
this.setToken(this.config.token);
|
18
19
|
}
|
19
|
-
this.
|
20
|
+
this.token$.subscribe((token) => {
|
20
21
|
RocosStore.getChangeSubject().next({ type: 'token', url: this.config.url, data: token.value });
|
21
22
|
});
|
22
23
|
}
|
@@ -25,10 +26,10 @@ export class AuthService extends BaseServiceAbstract {
|
|
25
26
|
* This is useful for getting notified when the token changes.
|
26
27
|
* i.e. when the token is refreshed from the token refresh checker.
|
27
28
|
*
|
28
|
-
*
|
29
|
+
* It will emit the current token when you subscribe and then emit new tokens when they are set.
|
29
30
|
*/
|
30
|
-
get
|
31
|
-
return this.tokenSubject$.asObservable();
|
31
|
+
get token$() {
|
32
|
+
return this.tokenSubject$.asObservable().pipe(filter(Boolean));
|
32
33
|
}
|
33
34
|
getStatus() {
|
34
35
|
return true;
|
@@ -54,7 +55,12 @@ export class AuthService extends BaseServiceAbstract {
|
|
54
55
|
}
|
55
56
|
this.config.token = this.token.value;
|
56
57
|
// output a message for token change
|
57
|
-
this.
|
58
|
+
if (this.isTokenValid(this.token)) {
|
59
|
+
this.tokenSubject$.next(this.token);
|
60
|
+
}
|
61
|
+
else {
|
62
|
+
void this.getToken();
|
63
|
+
}
|
58
64
|
}
|
59
65
|
/**
|
60
66
|
* Clear token
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@dronedeploy/rocos-js-sdk",
|
3
|
-
"version": "3.0.1-alpha.
|
3
|
+
"version": "3.0.1-alpha.22",
|
4
4
|
"description": "Javascript SDK for rocos",
|
5
5
|
"main": "cjs/index.js",
|
6
6
|
"module": "esm/index.js",
|
@@ -27,6 +27,7 @@
|
|
27
27
|
"@protobuf-ts/plugin": "^2.9.1",
|
28
28
|
"@protobuf-ts/runtime": "^2.9.1",
|
29
29
|
"@types/json-schema": "^7.0.14",
|
30
|
+
"acorn": "^8.11.3",
|
30
31
|
"buffer": "^6.0.3",
|
31
32
|
"loglevel": "^1.7.1",
|
32
33
|
"loglevel-plugin-prefix": "^0.8.4",
|