opticedge-cloud-utils 1.1.24 → 1.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/parser.d.ts +2 -1
- package/dist/parser.js +12 -1
- package/dist/task.js +1 -3
- package/package.json +1 -1
- package/src/env.ts +1 -1
- package/src/parser.ts +13 -1
- package/src/task.ts +1 -3
- package/tests/env.test.ts +3 -7
- package/tests/parser.test.ts +37 -1
package/dist/parser.d.ts
CHANGED
package/dist/parser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toNumber = void 0;
|
|
3
|
+
exports.parsePositiveInt = exports.toNumber = void 0;
|
|
4
4
|
// Helper: accept number or numeric-string, return number or null
|
|
5
5
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
6
|
const toNumber = (v) => {
|
|
@@ -11,3 +11,14 @@ const toNumber = (v) => {
|
|
|
11
11
|
return null;
|
|
12
12
|
};
|
|
13
13
|
exports.toNumber = toNumber;
|
|
14
|
+
const parsePositiveInt = (value, name) => {
|
|
15
|
+
if (!/^\d+$/.test(value)) {
|
|
16
|
+
throw new Error(`${name} must be a positive integer`);
|
|
17
|
+
}
|
|
18
|
+
const n = Number.parseInt(value, 10);
|
|
19
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
20
|
+
throw new Error(`${name} must be a positive integer`);
|
|
21
|
+
}
|
|
22
|
+
return n;
|
|
23
|
+
};
|
|
24
|
+
exports.parsePositiveInt = parsePositiveInt;
|
package/dist/task.js
CHANGED
|
@@ -81,9 +81,7 @@ async function createTask(projectId, region, queueId, data, serviceAccount, audi
|
|
|
81
81
|
return response.name;
|
|
82
82
|
};
|
|
83
83
|
try {
|
|
84
|
-
|
|
85
|
-
console.log(`✅ Created Cloud Task: ${name}`);
|
|
86
|
-
return name;
|
|
84
|
+
return await (0, retry_1.retry)(() => doCreate(), retryOpts);
|
|
87
85
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
86
|
}
|
|
89
87
|
catch (err) {
|
package/package.json
CHANGED
package/src/env.ts
CHANGED
package/src/parser.ts
CHANGED
|
@@ -6,4 +6,16 @@ const toNumber = (v: any): number | null => {
|
|
|
6
6
|
return null
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const parsePositiveInt = (value: string, name: string): number => {
|
|
10
|
+
if (!/^\d+$/.test(value)) {
|
|
11
|
+
throw new Error(`${name} must be a positive integer`)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const n = Number.parseInt(value, 10)
|
|
15
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
16
|
+
throw new Error(`${name} must be a positive integer`)
|
|
17
|
+
}
|
|
18
|
+
return n
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { toNumber, parsePositiveInt }
|
package/src/task.ts
CHANGED
|
@@ -106,9 +106,7 @@ export async function createTask(
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
try {
|
|
109
|
-
|
|
110
|
-
console.log(`✅ Created Cloud Task: ${name}`)
|
|
111
|
-
return name
|
|
109
|
+
return await retry(() => doCreate(), retryOpts)
|
|
112
110
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
111
|
} catch (err: any) {
|
|
114
112
|
// If ALREADY_EXISTS happened (and was not considered retryable), you may prefer to
|
package/tests/env.test.ts
CHANGED
|
@@ -51,15 +51,11 @@ describe('requireEnv', () => {
|
|
|
51
51
|
|
|
52
52
|
it('throws when env is undefined and no default provided', () => {
|
|
53
53
|
delete process.env.TEST_VAR
|
|
54
|
-
expect(() => requireEnv('TEST_VAR')).toThrow(
|
|
55
|
-
'TEST_VAR env var must be set'
|
|
56
|
-
)
|
|
54
|
+
expect(() => requireEnv('TEST_VAR')).toThrow('TEST_VAR env var must be set')
|
|
57
55
|
})
|
|
58
56
|
|
|
59
57
|
it('throws when env is empty string and no default provided', () => {
|
|
60
58
|
process.env.TEST_VAR = ''
|
|
61
|
-
expect(() => requireEnv('TEST_VAR')).toThrow(
|
|
62
|
-
'TEST_VAR env var must be set'
|
|
63
|
-
)
|
|
59
|
+
expect(() => requireEnv('TEST_VAR')).toThrow('TEST_VAR env var must be set')
|
|
64
60
|
})
|
|
65
|
-
})
|
|
61
|
+
})
|
package/tests/parser.test.ts
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import { toNumber } from '../src'
|
|
1
|
+
import { toNumber, parsePositiveInt } from '../src'
|
|
2
2
|
|
|
3
3
|
describe('toNumber', () => {
|
|
4
4
|
it('returns the same number when input is a finite number', () => {
|
|
5
5
|
expect(toNumber(123)).toBe(123)
|
|
6
6
|
expect(toNumber(0)).toBe(0)
|
|
7
|
+
expect(toNumber(-10)).toBe(-10)
|
|
7
8
|
})
|
|
8
9
|
|
|
9
10
|
it('returns number when input is a numeric string', () => {
|
|
10
11
|
expect(toNumber('456')).toBe(456)
|
|
12
|
+
expect(toNumber('0')).toBe(0)
|
|
11
13
|
})
|
|
12
14
|
|
|
13
15
|
it('returns null for non-numeric strings', () => {
|
|
14
16
|
expect(toNumber('abc')).toBeNull()
|
|
15
17
|
expect(toNumber('123abc')).toBeNull()
|
|
16
18
|
expect(toNumber('')).toBeNull()
|
|
19
|
+
expect(toNumber(' ')).toBeNull()
|
|
20
|
+
expect(toNumber('12.3')).toBeNull() // decimal not allowed by regex
|
|
21
|
+
expect(toNumber('-123')).toBeNull() // negative string not allowed
|
|
17
22
|
})
|
|
18
23
|
|
|
19
24
|
it('returns null for non-numbers', () => {
|
|
@@ -22,5 +27,36 @@ describe('toNumber', () => {
|
|
|
22
27
|
expect(toNumber(undefined)).toBeNull()
|
|
23
28
|
expect(toNumber(null)).toBeNull()
|
|
24
29
|
expect(toNumber({})).toBeNull()
|
|
30
|
+
expect(toNumber([])).toBeNull()
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('parsePositiveInt', () => {
|
|
35
|
+
it('parses valid positive integers', () => {
|
|
36
|
+
expect(parsePositiveInt('1', 'TEST')).toBe(1)
|
|
37
|
+
expect(parsePositiveInt('10', 'TEST')).toBe(10)
|
|
38
|
+
expect(parsePositiveInt('001', 'TEST')).toBe(1)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('throws for zero', () => {
|
|
42
|
+
expect(() => parsePositiveInt('0', 'TEST')).toThrow('TEST must be a positive integer')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('throws for negative numbers', () => {
|
|
46
|
+
expect(() => parsePositiveInt('-1', 'TEST')).toThrow('TEST must be a positive integer')
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('throws for non-numeric values', () => {
|
|
50
|
+
expect(() => parsePositiveInt('abc', 'TEST')).toThrow('TEST must be a positive integer')
|
|
51
|
+
expect(() => parsePositiveInt('', 'TEST')).toThrow('TEST must be a positive integer')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('throws for decimals', () => {
|
|
55
|
+
expect(() => parsePositiveInt('1.5', 'TEST')).toThrow('TEST must be a positive integer')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('throws for NaN-like values', () => {
|
|
59
|
+
expect(() => parsePositiveInt('NaN', 'TEST')).toThrow('TEST must be a positive integer')
|
|
60
|
+
expect(() => parsePositiveInt('Infinity', 'TEST')).toThrow('TEST must be a positive integer')
|
|
25
61
|
})
|
|
26
62
|
})
|