@platformatic/foundation 3.0.0-alpha.6 → 3.0.0-alpha.8
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/index.d.ts +13 -3
- package/index.js +1 -0
- package/lib/configuration.js +1 -1
- package/lib/execution.js +60 -2
- package/lib/module.js +1 -2
- package/lib/schema.js +13 -0
- package/lib/symbols.js +5 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -3,8 +3,14 @@ import { JSONSchemaType } from 'ajv'
|
|
|
3
3
|
import { EventEmitter } from 'node:events'
|
|
4
4
|
import { Logger } from 'pino'
|
|
5
5
|
|
|
6
|
-
//
|
|
6
|
+
// Symbols
|
|
7
|
+
export declare const kCanceled: unique symbol
|
|
8
|
+
export declare const kFailedImport: unique symbol
|
|
9
|
+
export declare const kHandledError: unique symbol
|
|
7
10
|
export declare const kMetadata: unique symbol
|
|
11
|
+
export declare const kTimeout: unique symbol
|
|
12
|
+
|
|
13
|
+
// Configuration types
|
|
8
14
|
export declare const envVariablePattern: RegExp
|
|
9
15
|
export declare const knownConfigurationFilesExtensions: string[]
|
|
10
16
|
export declare const knownConfigurationFilesSchemas: RegExp[]
|
|
@@ -117,8 +123,13 @@ export declare const SchemaMustBeDefinedError: FastifyError
|
|
|
117
123
|
export declare const ConfigurationDoesNotValidateAgainstSchemaError: FastifyError
|
|
118
124
|
|
|
119
125
|
// Execution types
|
|
120
|
-
export declare const kTimeout: unique symbol
|
|
121
126
|
export declare function executeWithTimeout<T> (promise: Promise<T>, timeout: number, timeoutValue?: any): Promise<T>
|
|
127
|
+
export declare function executeInParallel<T, Args extends any[]> (
|
|
128
|
+
fn: (...args: Args) => Promise<T>,
|
|
129
|
+
args: Args[],
|
|
130
|
+
concurrency?: number,
|
|
131
|
+
throwOnRejections?: boolean
|
|
132
|
+
): Promise<T[]>
|
|
122
133
|
|
|
123
134
|
// File system types
|
|
124
135
|
export declare function removeDotSlash (path: string): string
|
|
@@ -185,7 +196,6 @@ export declare const stdTimeFunctions: {
|
|
|
185
196
|
}
|
|
186
197
|
|
|
187
198
|
// Module types
|
|
188
|
-
export declare const kFailedImport: unique symbol
|
|
189
199
|
export declare const defaultPackageManager: string
|
|
190
200
|
|
|
191
201
|
export declare function getLatestNpmVersion (pkg: string): Promise<string | null>
|
package/index.js
CHANGED
package/lib/configuration.js
CHANGED
|
@@ -17,13 +17,13 @@ import {
|
|
|
17
17
|
} from './errors.js'
|
|
18
18
|
import { isFileAccessible } from './file-system.js'
|
|
19
19
|
import { loadModule, splitModuleFromVersion } from './module.js'
|
|
20
|
+
import { kMetadata } from './symbols.js'
|
|
20
21
|
|
|
21
22
|
const { parse: parseJSON5, stringify: rawStringifyJSON5 } = JSON5
|
|
22
23
|
const { parse: parseTOML, stringify: stringifyTOML } = toml
|
|
23
24
|
|
|
24
25
|
const kReplaceEnvIgnore = Symbol('plt.foundation.replaceEnvIgnore')
|
|
25
26
|
|
|
26
|
-
export const kMetadata = Symbol('plt.foundation.metadata')
|
|
27
27
|
export const envVariablePattern = /(?:\{{1,2})([a-z0-9_]+)(?:\}{1,2})/i
|
|
28
28
|
|
|
29
29
|
export const knownConfigurationFilesExtensions = ['json', 'json5', 'yaml', 'yml', 'toml', 'tml']
|
package/lib/execution.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Unpromise } from '@watchable/unpromise'
|
|
2
2
|
import { setTimeout as sleep } from 'node:timers/promises'
|
|
3
|
-
|
|
4
|
-
export const kTimeout = Symbol('plt.utils.timeout')
|
|
3
|
+
import { kCanceled, kTimeout } from './symbols.js'
|
|
5
4
|
|
|
6
5
|
export async function executeWithTimeout (promise, timeout, timeoutValue = kTimeout) {
|
|
7
6
|
const ac = new AbortController()
|
|
@@ -11,3 +10,62 @@ export async function executeWithTimeout (promise, timeout, timeoutValue = kTime
|
|
|
11
10
|
return value
|
|
12
11
|
})
|
|
13
12
|
}
|
|
13
|
+
|
|
14
|
+
export async function executeInParallel (fn, args, concurrency = 5, throwOnRejections = true, throwAllErrors = false) {
|
|
15
|
+
const { promise, resolve } = Promise.withResolvers()
|
|
16
|
+
const results = new Map()
|
|
17
|
+
let current = 0
|
|
18
|
+
let pending = 0
|
|
19
|
+
let firstError
|
|
20
|
+
let terminated = false
|
|
21
|
+
|
|
22
|
+
if (args.length === 0) {
|
|
23
|
+
return []
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function scheduleNext () {
|
|
27
|
+
// While we have capacity and there are still items to process
|
|
28
|
+
while (current < args.length && pending < concurrency) {
|
|
29
|
+
const i = current++
|
|
30
|
+
pending++
|
|
31
|
+
|
|
32
|
+
// Perform the async operation
|
|
33
|
+
fn(...args[i])
|
|
34
|
+
.then(result => {
|
|
35
|
+
results.set(i, result)
|
|
36
|
+
})
|
|
37
|
+
.catch(err => {
|
|
38
|
+
results.set(i, err)
|
|
39
|
+
firstError = err
|
|
40
|
+
})
|
|
41
|
+
.finally(() => {
|
|
42
|
+
pending--
|
|
43
|
+
|
|
44
|
+
if (terminated) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if ((current === args.length && pending === 0) || (firstError && throwOnRejections)) {
|
|
49
|
+
terminated = true
|
|
50
|
+
resolve()
|
|
51
|
+
} else {
|
|
52
|
+
scheduleNext()
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
scheduleNext()
|
|
59
|
+
await promise
|
|
60
|
+
|
|
61
|
+
const returnedValues = []
|
|
62
|
+
for (let j = 0; j < args.length; j++) {
|
|
63
|
+
returnedValues.push(results.has(j) ? results.get(j) : kCanceled)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (firstError && throwOnRejections) {
|
|
67
|
+
throw throwAllErrors ? AggregateError(returnedValues, 'One or more operations failed.') : firstError
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return returnedValues
|
|
71
|
+
}
|
package/lib/module.js
CHANGED
|
@@ -4,11 +4,10 @@ import { resolve } from 'node:path'
|
|
|
4
4
|
import { fileURLToPath } from 'node:url'
|
|
5
5
|
import { request } from 'undici'
|
|
6
6
|
import { hasJavascriptFiles } from './file-system.js'
|
|
7
|
+
import { kFailedImport } from './symbols.js'
|
|
7
8
|
|
|
8
9
|
let platformaticPackageVersion
|
|
9
10
|
|
|
10
|
-
export const kFailedImport = Symbol('plt.utils.failedImport')
|
|
11
|
-
|
|
12
11
|
export const defaultPackageManager = 'npm'
|
|
13
12
|
|
|
14
13
|
export async function getLatestNpmVersion (pkg) {
|
package/lib/schema.js
CHANGED
|
@@ -689,6 +689,13 @@ export const applications = {
|
|
|
689
689
|
},
|
|
690
690
|
workers,
|
|
691
691
|
health: { ...healthWithoutDefaults },
|
|
692
|
+
dependencies: {
|
|
693
|
+
type: 'array',
|
|
694
|
+
items: {
|
|
695
|
+
type: 'string'
|
|
696
|
+
},
|
|
697
|
+
default: []
|
|
698
|
+
},
|
|
692
699
|
arguments: {
|
|
693
700
|
type: 'array',
|
|
694
701
|
items: {
|
|
@@ -803,6 +810,12 @@ export const runtimeProperties = {
|
|
|
803
810
|
workers,
|
|
804
811
|
health: { ...healthWithoutDefaults },
|
|
805
812
|
preload,
|
|
813
|
+
dependencies: {
|
|
814
|
+
type: 'array',
|
|
815
|
+
items: {
|
|
816
|
+
type: 'string'
|
|
817
|
+
}
|
|
818
|
+
},
|
|
806
819
|
arguments: {
|
|
807
820
|
type: 'array',
|
|
808
821
|
items: {
|
package/lib/symbols.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export const kCanceled = Symbol('plt.foundation.canceled')
|
|
2
|
+
export const kFailedImport = Symbol('plt.foundation.failedImport')
|
|
3
|
+
export const kHandledError = Symbol('plt.foundation.handledError')
|
|
4
|
+
export const kMetadata = Symbol('plt.foundation.metadata')
|
|
5
|
+
export const kTimeout = Symbol('plt.foundation.timeout')
|