@platformatic/foundation 3.0.0-alpha.6 → 3.0.0-alpha.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.
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
- // Configuration types
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
@@ -10,3 +10,4 @@ export * from './lib/node.js'
10
10
  export * from './lib/object.js'
11
11
  export * from './lib/schema.js'
12
12
  export * from './lib/string.js'
13
+ export * from './lib/symbols.js'
@@ -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')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/foundation",
3
- "version": "3.0.0-alpha.6",
3
+ "version": "3.0.0-alpha.7",
4
4
  "description": "Platformatic Foundation",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",