@platformatic/basic 3.4.1 → 3.5.1

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/config.d.ts CHANGED
@@ -5,7 +5,327 @@
5
5
  * and run json-schema-to-typescript to regenerate this file.
6
6
  */
7
7
 
8
- export interface PlatformaticStackable {
8
+ export interface PlatformaticBasicConfig {
9
9
  $schema?: string;
10
+ runtime?: {
11
+ preload?: string | string[];
12
+ basePath?: string;
13
+ services?: {
14
+ [k: string]: unknown;
15
+ }[];
16
+ workers?: number | string;
17
+ logger?: {
18
+ level: (
19
+ | ("fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent")
20
+ | {
21
+ [k: string]: unknown;
22
+ }
23
+ ) &
24
+ string;
25
+ transport?:
26
+ | {
27
+ target?: string;
28
+ options?: {
29
+ [k: string]: unknown;
30
+ };
31
+ }
32
+ | {
33
+ targets?: {
34
+ target?: string;
35
+ options?: {
36
+ [k: string]: unknown;
37
+ };
38
+ level?: string;
39
+ }[];
40
+ options?: {
41
+ [k: string]: unknown;
42
+ };
43
+ };
44
+ pipeline?: {
45
+ target?: string;
46
+ options?: {
47
+ [k: string]: unknown;
48
+ };
49
+ };
50
+ formatters?: {
51
+ path: string;
52
+ };
53
+ timestamp?: "epochTime" | "unixTime" | "nullTime" | "isoTime";
54
+ redact?: {
55
+ paths: string[];
56
+ censor?: string;
57
+ };
58
+ base?: {
59
+ [k: string]: unknown;
60
+ } | null;
61
+ messageKey?: string;
62
+ customLevels?: {
63
+ [k: string]: unknown;
64
+ };
65
+ [k: string]: unknown;
66
+ };
67
+ server?: {
68
+ hostname?: string;
69
+ port?: number | string;
70
+ http2?: boolean;
71
+ https?: {
72
+ allowHTTP1?: boolean;
73
+ key:
74
+ | string
75
+ | {
76
+ path?: string;
77
+ }
78
+ | (
79
+ | string
80
+ | {
81
+ path?: string;
82
+ }
83
+ )[];
84
+ cert:
85
+ | string
86
+ | {
87
+ path?: string;
88
+ }
89
+ | (
90
+ | string
91
+ | {
92
+ path?: string;
93
+ }
94
+ )[];
95
+ requestCert?: boolean;
96
+ rejectUnauthorized?: boolean;
97
+ };
98
+ };
99
+ startTimeout?: number;
100
+ restartOnError?: boolean | number;
101
+ exitOnUnhandledErrors?: boolean;
102
+ gracefulShutdown?: {
103
+ runtime: number | string;
104
+ application: number | string;
105
+ };
106
+ health?: {
107
+ enabled?: boolean | string;
108
+ interval?: number | string;
109
+ gracePeriod?: number | string;
110
+ maxUnhealthyChecks?: number | string;
111
+ maxELU?: number | string;
112
+ maxHeapUsed?: number | string;
113
+ maxHeapTotal?: number | string;
114
+ maxYoungGeneration?: number | string;
115
+ };
116
+ undici?: {
117
+ agentOptions?: {
118
+ [k: string]: unknown;
119
+ };
120
+ interceptors?:
121
+ | {
122
+ module: string;
123
+ options: {
124
+ [k: string]: unknown;
125
+ };
126
+ [k: string]: unknown;
127
+ }[]
128
+ | {
129
+ Client?: {
130
+ module: string;
131
+ options: {
132
+ [k: string]: unknown;
133
+ };
134
+ [k: string]: unknown;
135
+ }[];
136
+ Pool?: {
137
+ module: string;
138
+ options: {
139
+ [k: string]: unknown;
140
+ };
141
+ [k: string]: unknown;
142
+ }[];
143
+ Agent?: {
144
+ module: string;
145
+ options: {
146
+ [k: string]: unknown;
147
+ };
148
+ [k: string]: unknown;
149
+ }[];
150
+ [k: string]: unknown;
151
+ };
152
+ [k: string]: unknown;
153
+ };
154
+ httpCache?:
155
+ | boolean
156
+ | {
157
+ store?: string;
158
+ /**
159
+ * @minItems 1
160
+ */
161
+ methods?: [string, ...string[]];
162
+ cacheTagsHeader?: string;
163
+ maxSize?: number;
164
+ maxEntrySize?: number;
165
+ maxCount?: number;
166
+ [k: string]: unknown;
167
+ };
168
+ watch?: boolean | string;
169
+ managementApi?:
170
+ | boolean
171
+ | string
172
+ | {
173
+ logs?: {
174
+ maxSize?: number;
175
+ };
176
+ };
177
+ metrics?:
178
+ | boolean
179
+ | {
180
+ port?: number | string;
181
+ enabled?: boolean | string;
182
+ hostname?: string;
183
+ endpoint?: string;
184
+ auth?: {
185
+ username: string;
186
+ password: string;
187
+ };
188
+ labels?: {
189
+ [k: string]: string;
190
+ };
191
+ /**
192
+ * The label name to use for the application identifier in metrics (e.g., applicationId, serviceId)
193
+ */
194
+ applicationLabel?: string;
195
+ readiness?:
196
+ | boolean
197
+ | {
198
+ endpoint?: string;
199
+ success?: {
200
+ statusCode?: number;
201
+ body?: string;
202
+ };
203
+ fail?: {
204
+ statusCode?: number;
205
+ body?: string;
206
+ };
207
+ };
208
+ liveness?:
209
+ | boolean
210
+ | {
211
+ endpoint?: string;
212
+ success?: {
213
+ statusCode?: number;
214
+ body?: string;
215
+ };
216
+ fail?: {
217
+ statusCode?: number;
218
+ body?: string;
219
+ };
220
+ };
221
+ plugins?: string[];
222
+ };
223
+ telemetry?: {
224
+ enabled?: boolean | string;
225
+ /**
226
+ * The name of the application. Defaults to the folder name if not specified.
227
+ */
228
+ applicationName: string;
229
+ /**
230
+ * The version of the application (optional)
231
+ */
232
+ version?: string;
233
+ /**
234
+ * An array of paths to skip when creating spans. Useful for health checks and other endpoints that do not need to be traced.
235
+ */
236
+ skip?: {
237
+ /**
238
+ * The path to skip. Can be a string or a regex.
239
+ */
240
+ path?: string;
241
+ /**
242
+ * HTTP method to skip
243
+ */
244
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
245
+ [k: string]: unknown;
246
+ }[];
247
+ exporter?:
248
+ | {
249
+ type?: "console" | "otlp" | "zipkin" | "memory" | "file";
250
+ /**
251
+ * Options for the exporter. These are passed directly to the exporter.
252
+ */
253
+ options?: {
254
+ /**
255
+ * The URL to send the traces to. Not used for console or memory exporters.
256
+ */
257
+ url?: string;
258
+ /**
259
+ * Headers to send to the exporter. Not used for console or memory exporters.
260
+ */
261
+ headers?: {
262
+ [k: string]: unknown;
263
+ };
264
+ /**
265
+ * The path to write the traces to. Only for file exporter.
266
+ */
267
+ path?: string;
268
+ [k: string]: unknown;
269
+ };
270
+ additionalProperties?: never;
271
+ [k: string]: unknown;
272
+ }[]
273
+ | {
274
+ type?: "console" | "otlp" | "zipkin" | "memory" | "file";
275
+ /**
276
+ * Options for the exporter. These are passed directly to the exporter.
277
+ */
278
+ options?: {
279
+ /**
280
+ * The URL to send the traces to. Not used for console or memory exporters.
281
+ */
282
+ url?: string;
283
+ /**
284
+ * Headers to send to the exporter. Not used for console or memory exporters.
285
+ */
286
+ headers?: {
287
+ [k: string]: unknown;
288
+ };
289
+ /**
290
+ * The path to write the traces to. Only for file exporter.
291
+ */
292
+ path?: string;
293
+ [k: string]: unknown;
294
+ };
295
+ additionalProperties?: never;
296
+ [k: string]: unknown;
297
+ };
298
+ };
299
+ inspectorOptions?: {
300
+ host?: string;
301
+ port?: number;
302
+ breakFirstLine?: boolean;
303
+ watchDisabled?: boolean;
304
+ [k: string]: unknown;
305
+ };
306
+ applicationTimeout?: number | string;
307
+ messagingTimeout?: number | string;
308
+ env?: {
309
+ [k: string]: string;
310
+ };
311
+ sourceMaps?: boolean;
312
+ scheduler?: {
313
+ enabled?: boolean | string;
314
+ name: string;
315
+ cron: string;
316
+ callbackUrl: string;
317
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
318
+ headers?: {
319
+ [k: string]: string;
320
+ };
321
+ body?:
322
+ | string
323
+ | {
324
+ [k: string]: unknown;
325
+ };
326
+ maxRetries?: number;
327
+ [k: string]: unknown;
328
+ }[];
329
+ };
10
330
  [k: string]: unknown;
11
331
  }
package/eslint.config.js CHANGED
@@ -1,5 +1,3 @@
1
1
  import neostandard from 'neostandard'
2
2
 
3
- export default neostandard({
4
- ignores: ['**/.next'],
5
- })
3
+ export default neostandard()
package/index.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ export interface StartOptions {
2
+ listen?: boolean
3
+ }
4
+
5
+ export type BaseContext = Partial<{
6
+ applicationId: string
7
+ isEntrypoint: boolean
8
+ isProduction: boolean
9
+ isStandalone: boolean
10
+ directory: string
11
+ telemetryConfig: object
12
+ metricsConfig: object
13
+ serverConfig: object
14
+ hasManagementApi: boolean
15
+ }>
16
+
17
+ export interface BaseOptions<Context = BaseContext> {
18
+ context: Context
19
+ }
20
+
21
+ export declare const schemaOptions: Partial<Record<string, unknown>>
22
+
23
+ export class BaseCapability<Config = Record<string, any>, Options = BaseOptions> {
24
+ basePath: string
25
+ constructor (
26
+ type: string,
27
+ version: string,
28
+ root: string,
29
+ config: object,
30
+ standardStreams?: Record<string, NodeJS.WritableStream>
31
+ )
32
+
33
+ init (): Promise<void>
34
+ start (options: StartOptions): Promise<void>
35
+ stop (): Promise<void>
36
+ build (): Promise<void>
37
+ getUrl (): string
38
+ updateContext (context: Partial<BaseContext>): Promise<void>
39
+ getConfig (includeMeta?: boolean): Promise<object>
40
+ getInfo (): Promise<{ type: string; version: string }>
41
+ getDispatchFunc (): Promise<Function>
42
+ getDispatchTarget (): Promise<Function | string>
43
+ getOpenapiSchema (): Promise<object>
44
+ getGraphqlSchema (): Promise<string>
45
+ setConnectionStatus (status: string): Promise<void>
46
+ setOpenapiSchema (schema: object): Promise<void>
47
+ setGraphqlSchema (schema: string): Promise<void>
48
+ setCustomHealthCheck (
49
+ healthCheck: () =>
50
+ | boolean
51
+ | Promise<boolean>
52
+ | { status: boolean; statusCode?: number; body?: string }
53
+ | Promise<{ status: boolean; statusCode?: number; body?: string }>
54
+ ): Promise<void>
55
+ setCustomReadinessCheck (
56
+ readinessCheck: () =>
57
+ | boolean
58
+ | Promise<boolean>
59
+ | { status: boolean; statusCode?: number; body?: string }
60
+ | Promise<{ status: boolean; statusCode?: number; body?: string }>
61
+ ): Promise<void>
62
+ collectMetrics (): Promise<any>
63
+ getMetrics ({ format: string }): Promise<string | Array<object>>
64
+ getMeta (): Promise<object>
65
+ inject (injectParams: string | object): Promise<{
66
+ statusCode: number
67
+ statusMessage: string
68
+ headers: object
69
+ body: object
70
+ }>
71
+ log (options: { message: string; level: string }): Promise<void>
72
+ getWatchConfig (): Promise<{
73
+ enabled: boolean
74
+ path: string
75
+ allow?: string[]
76
+ ignore?: string[]
77
+ }>
78
+
79
+ _initializeLogger (options: object): Promise<void>
80
+ _collectMetrics (): Promise<void>
81
+ }
package/index.js CHANGED
@@ -1,159 +1,8 @@
1
- import { existsSync } from 'node:fs'
2
- import { readFile } from 'node:fs/promises'
3
- import { createRequire } from 'node:module'
4
- import { relative, resolve } from 'node:path'
5
- import { workerData } from 'node:worker_threads'
6
- import pino from 'pino'
7
- import { packageJson, schema } from './lib/schema.js'
8
- import { importFile } from './lib/utils.js'
9
-
10
- const importStackablePackageMarker = '__pltImportStackablePackage.js'
11
-
12
- export const configCandidates = [
13
- 'platformatic.application.json',
14
- 'platformatic.json',
15
- 'watt.json',
16
- 'platformatic.application.yaml',
17
- 'platformatic.yaml',
18
- 'watt.yaml',
19
- 'platformatic.application.yml',
20
- 'platformatic.yml',
21
- 'watt.yml',
22
- 'platformatic.application.toml',
23
- 'platformatic.toml',
24
- 'watt.toml',
25
- 'platformatic.application.tml',
26
- 'platformatic.tml',
27
- 'watt.tml'
28
- ]
29
-
30
- function isImportFailedError (error, pkg) {
31
- if (error.code !== 'ERR_MODULE_NOT_FOUND' && error.code !== 'MODULE_NOT_FOUND') {
32
- return false
33
- }
34
-
35
- const match = error.message.match(/Cannot find package '(.+)' imported from (.+)/)
36
-
37
- return match?.[1] === pkg || error.requireStack?.[0].endsWith(importStackablePackageMarker)
38
- }
39
-
40
- async function importStackablePackage (opts, pkg, autodetectDescription) {
41
- try {
42
- try {
43
- // Try regular import
44
- return await import(pkg)
45
- } catch (e) {
46
- if (!isImportFailedError(e, pkg)) {
47
- throw e
48
- }
49
-
50
- // Scope to the service
51
- const require = createRequire(resolve(opts.context.directory, importStackablePackageMarker))
52
- const imported = require.resolve(pkg)
53
- return await importFile(imported)
54
- }
55
- } catch (e) {
56
- if (!isImportFailedError(e, pkg)) {
57
- throw e
58
- }
59
-
60
- const serviceDirectory = relative(workerData.dirname, opts.context.directory)
61
- throw new Error(
62
- `Unable to import package '${pkg}'. Please add it as a dependency in the package.json file in the folder ${serviceDirectory}.`
63
- )
64
- }
65
- }
66
-
67
- async function buildStackable (opts) {
68
- const root = opts.context.directory
69
- let toImport = '@platformatic/node'
70
- let autodetectDescription = 'is using a generic Node.js application'
71
-
72
- let rootPackageJson
73
- try {
74
- rootPackageJson = JSON.parse(await readFile(resolve(root, 'package.json'), 'utf-8'))
75
- } catch {
76
- rootPackageJson = {}
77
- }
78
-
79
- const hadConfig = opts.config
80
-
81
- if (!hadConfig) {
82
- for (const candidate of configCandidates) {
83
- const candidatePath = resolve(root, candidate)
84
-
85
- if (existsSync(candidatePath)) {
86
- opts.config = candidatePath
87
- break
88
- }
89
- }
90
- }
91
-
92
- const { dependencies, devDependencies } = rootPackageJson
93
-
94
- if (dependencies?.next || devDependencies?.next) {
95
- autodetectDescription = 'is using Next.js'
96
- toImport = '@platformatic/next'
97
- } else if (dependencies?.['@remix-run/dev'] || devDependencies?.['@remix-run/dev']) {
98
- autodetectDescription = 'is using Remix'
99
- toImport = '@platformatic/remix'
100
- } else if (dependencies?.vite || devDependencies?.vite) {
101
- autodetectDescription = 'is using Vite'
102
- toImport = '@platformatic/vite'
103
- } else if (dependencies?.astro || devDependencies?.astro) {
104
- autodetectDescription = 'is using Astro'
105
- toImport = '@platformatic/astro'
106
- }
107
-
108
- const imported = await importStackablePackage(opts, toImport, autodetectDescription)
109
-
110
- const serviceRoot = relative(process.cwd(), opts.context.directory)
111
- if (
112
- !hadConfig &&
113
- !existsSync(resolve(serviceRoot, 'platformatic.json') || existsSync(resolve(serviceRoot, 'watt.json')))
114
- ) {
115
- const logger = pino({
116
- level: opts.context.serverConfig?.logger?.level ?? 'warn',
117
- name: opts.context.serviceId
118
- })
119
-
120
- logger.warn(
121
- [
122
- `Platformatic has auto-detected that service ${opts.context.serviceId} ${autodetectDescription}.\n`,
123
- `We suggest you create a platformatic.json or watt.json file in the folder ${serviceRoot} with the "$schema" `,
124
- `property set to "https://schemas.platformatic.dev/${toImport}/${packageJson.version}.json".`
125
- ].join('')
126
- )
127
- }
128
-
129
- return imported.buildStackable(opts)
130
- }
131
-
132
- /* c8 ignore next 3 */
133
- export function transformConfig () {
134
- // This is currently empty but it left as a placeholder for the future
135
- }
136
-
137
- export const schemaOptions = {
138
- useDefaults: true,
139
- coerceTypes: true,
140
- allErrors: true,
141
- strict: false
142
- }
143
-
144
- export default {
145
- configType: 'nodejs',
146
- configManagerConfig: {
147
- schemaOptions,
148
- transformConfig
149
- },
150
- buildStackable,
151
- schema,
152
- version: packageJson.version
153
- }
154
-
155
- export * from './lib/base.js'
1
+ export * from './lib/capability.js'
2
+ export * from './lib/config.js'
3
+ export * from './lib/creation.js'
156
4
  export * as errors from './lib/errors.js'
5
+ export * from './lib/modules.js'
157
6
  export { schema, schemaComponents } from './lib/schema.js'
158
7
  export * from './lib/utils.js'
159
8
  export * from './lib/worker/child-manager.js'