@workbasehq/core 0.2.0 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workbasehq/core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
package/src/config.ts CHANGED
@@ -13,7 +13,7 @@ import { detailDisplaySchema, validateDetailDisplay } from './detail-display'
13
13
 
14
14
  export { Q, Fn, Cond }
15
15
 
16
- export const CORE_VERSION = '0.2.0'
16
+ export const CORE_VERSION = '0.2.1'
17
17
  const navigationIconSchema = z.enum(['LayoutDashboard', 'Users', 'MapPin', 'Coins', 'Wallet', 'ArrowLeftRight', 'Settings', 'Building2', 'Activity', 'Workflow', 'Database', 'Folder'])
18
18
  const identifier = z.string().regex(/^[a-z][a-z0-9_]*$/)
19
19
  export const fieldSchema = z.object({
package/src/index.ts CHANGED
@@ -16,7 +16,7 @@ export * from './authorization-api'
16
16
  export * from './identity'
17
17
 
18
18
  export * from "./authorized-execution"
19
- export * from './execution'
19
+ export * from './execution/index'
20
20
  export * from './identity-providers'
21
21
 
22
22
  export * from './workflow-runtime'
@@ -18,11 +18,19 @@ Install the tested deployment tools explicitly (Node 22+, TypeScript 5.7+):
18
18
 
19
19
  ```sh
20
20
  npm install @workbasehq/core
21
- npm install -D sst@4.17.1 @pulumi/command@1.2.1 @pulumi/pulumi@3.215.0 tsx@4.23.15
21
+ npm install -D sst@4.17.1 tsx@4.23.15
22
22
  ```
23
23
 
24
24
  Use Cloudflare provider `6.19.0` in `sst.config.ts`. SST's pinned Pulumi version
25
25
  supports TypeScript <6; the packed consumer check uses TypeScript 5.7.2.
26
+ With all three SDK packages installed, mcp supplies `tsx` transitively, so only
27
+ `sst` is needed as an additional deployment dependency. Do not install Pulumi or
28
+ the command provider for provisioning: resources and secrets use SST's injected
29
+ globals, and migrations use the command provider resolved from `$cli.paths.platform`.
30
+ SST 4.17.1 supplies command 1.0.1 and Pulumi 3.215.0 there. The provisioner verifies
31
+ that command resolves the same Pulumi module as the platform before loading it;
32
+ it does not force a separate command provider version. Optional Pulumi peers are
33
+ only for consumers that need their types directly.
26
34
 
27
35
  ```ts
28
36
  /// <reference path="./.sst/platform/config.d.ts" />
@@ -188,3 +196,11 @@ also typechecks a migration-only consumer before installing deployment peers,
188
196
  imports and dry-plans provisioning without SST/Pulumi, typechecks a consumer
189
197
  `provision()` call, runs the emitted migration command and executes an SST-style
190
198
  bundle from the consumer's `.sst/platform` directory.
199
+
200
+ `bash scripts/sdk-pack.sh && bun run test:sdk-sst` installs the three packed SDK
201
+ packages plus SST in a clean directory and runs real `sst diff` previews with
202
+ local state, dummy credentials and Cloudflare API URLs pointed at loopback. It
203
+ checks every resource (including the Worker/custom domain/migration/consumer/cron)
204
+ and verifies that loaded Pulumi/command modules belong to SST's platform, then
205
+ repeats with conflicting consumer Pulumi/command dependencies installed. CI `check`
206
+ runs this regression alongside `test:infra`.
@@ -1,15 +1,9 @@
1
- import { createRequire } from 'node:module'
2
1
  import { dirname } from 'node:path'
3
- import { fileURLToPath, pathToFileURL } from 'node:url'
2
+ import { fileURLToPath } from 'node:url'
4
3
 
5
4
  // A bundled SST program has a different import.meta.url. Resolve the installed
6
- // export first, then resolve optional tools from that package, not .sst/platform
7
- // (which carries its own, potentially different versions of Pulumi tools).
5
+ // export first so config evaluation and migrations use the installed SDK bytes.
6
+ // Deployment providers must instead use SST's platform runtime (sst-runtime.ts).
8
7
  export function installedInfraDirectory(): string {
9
8
  return dirname(fileURLToPath(import.meta.resolve('@workbasehq/core/infra')))
10
9
  }
11
-
12
- export function packageToolUrl(specifier: string): string {
13
- const require = createRequire(import.meta.resolve('@workbasehq/core/infra'))
14
- return pathToFileURL(require.resolve(specifier)).href
15
- }
@@ -0,0 +1,29 @@
1
+ // Executed only in a fresh Node process. Never import this entry into SST's bundle.
2
+ import { pathToFileURL } from 'node:url'
3
+
4
+ const finish = result => process.send(result, () => process.exit(0))
5
+ const [configPath, migrationsDirectory] = process.argv.slice(2)
6
+ async function main() {
7
+ let config
8
+ try {
9
+ const { tsImport } = await import('tsx/esm/api')
10
+ config = (await tsImport(pathToFileURL(configPath).href, import.meta.url)).default
11
+ } catch (error) {
12
+ return finish({ kind: 'load-error', message: error instanceof Error ? error.message : String(error) })
13
+ }
14
+ if (!config || typeof config.id !== 'string' || !Number.isInteger(config.revision) || config.revision < 1 || !config.entities) {
15
+ finish({ kind: 'invalid-config' })
16
+ } else {
17
+ try {
18
+ // Keep plugin functions, schemas and identity brands in this process. Only
19
+ // the resulting schema fingerprints cross the IPC boundary.
20
+ const { tsImport } = await import('tsx/esm/api')
21
+ const { expectedSchema } = await tsImport(new URL('./schema.ts', import.meta.url).href, import.meta.url)
22
+ const schema = await expectedSchema({ migrationsDirectory, revision: config.revision, config })
23
+ finish({ kind: 'schema', schema })
24
+ } catch {
25
+ finish({ kind: 'schema-error' })
26
+ }
27
+ }
28
+ }
29
+ await main()
@@ -0,0 +1,33 @@
1
+ import { spawn } from 'node:child_process'
2
+ import { join } from 'node:path'
3
+ import type { SchemaRevision } from '../schema-compatibility'
4
+ import { installedInfraDirectory } from './package-tools'
5
+ import { ProvisionDiagnostic } from './provision-diagnostics'
6
+
7
+ type Result = { kind: 'schema'; schema: SchemaRevision }
8
+ | { kind: 'load-error'; message: string }
9
+ | { kind: 'invalid-config' | 'schema-error' }
10
+
11
+ /** SST registers process-wide ESM hooks. Evaluate trusted project code outside
12
+ * that process, without inheriting execArgv or NODE_OPTIONS loader registrations.
13
+ * stdout/stderr are discarded: config logs must not expose secrets or break IPC. */
14
+ export async function provisionConfigSchema(root: string, configPath: string, migrationsDirectory: string, secrets: string[]): Promise<SchemaRevision> {
15
+ const result = await new Promise<Result>((resolve, reject) => {
16
+ const child = spawn(process.execPath, [join(installedInfraDirectory(), 'provision-config-main.mjs'), configPath, migrationsDirectory], {
17
+ cwd: root, env: { ...process.env, NODE_OPTIONS: '' }, stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
18
+ })
19
+ let result: Result | undefined
20
+ const timeout = setTimeout(() => { child.kill('SIGKILL') }, 30000)
21
+ child.on('message', message => { result = message as Result })
22
+ child.on('error', error => { clearTimeout(timeout); reject(error) })
23
+ child.on('close', code => {
24
+ clearTimeout(timeout)
25
+ if (code === 0 && result) resolve(result)
26
+ else reject(new Error(code === null ? 'Config evaluation timed out or was terminated' : `Config evaluation process exited with code ${code}`))
27
+ })
28
+ }).catch(error => { throw new ProvisionDiagnostic('provision_config_load_failed', { message: error instanceof Error ? error.message : String(error), secrets }) })
29
+ if (result.kind === 'schema') return result.schema
30
+ if (result.kind === 'invalid-config') throw new ProvisionDiagnostic('provision_invalid_config')
31
+ if (result.kind === 'load-error') throw new ProvisionDiagnostic('provision_config_load_failed', { message: result.message, secrets })
32
+ throw new ProvisionDiagnostic('provision_failed')
33
+ }
@@ -1,3 +1,5 @@
1
+ import { redactCliOutput } from './diagnostics'
2
+
1
3
  const codes = [
2
4
  'provision_invalid_stage', 'provision_invalid_prefix', 'provision_invalid_handler',
3
5
  'provision_invalid_config_path', 'provision_invalid_migrations_dir', 'provision_invalid_assets_dir',
@@ -8,25 +10,36 @@ const codes = [
8
10
  ] as const
9
11
  export type ProvisionDiagnosticCode = typeof codes[number]
10
12
 
11
- const diagnostics = new WeakMap<object, ProvisionDiagnosticCode>()
13
+ const diagnostics = new WeakMap<object, { code: ProvisionDiagnosticCode; message: string }>()
12
14
 
13
- /** Fixed, branded diagnostics never interpolate application paths, config or secrets. */
15
+ /** Branded diagnostics; config loader messages redact deployment/environment secrets. */
14
16
  export class ProvisionDiagnostic extends Error {
15
17
  readonly code: ProvisionDiagnosticCode
16
- constructor(code: ProvisionDiagnosticCode) {
18
+ constructor(code: ProvisionDiagnosticCode, detail?: { message: string; secrets: string[] }) {
17
19
  const safeCode = codes.includes(code) ? code : 'provision_failed'
18
- super(safeCode)
20
+ let message: string = safeCode
21
+ if (safeCode === 'provision_config_load_failed' && detail) {
22
+ const secrets = Object.fromEntries(detail.secrets.map((value, index) => [`SECRET_${index}`, value]))
23
+ const cause = redactCliOutput(detail.message, { ...process.env, ...secrets })
24
+ .replace(/(https?:\/\/)[^\s/@]+:[^\s/@]+@/g, '$1[redacted]@')
25
+ .replace(/Bearer\s+[^\s'";,]+/gi, 'Bearer [redacted]')
26
+ .replace(/[\r\n\x00-\x1f\x7f]+/g, ' ').slice(0, 2000)
27
+ message += `: ${cause}`
28
+ }
29
+ super(message)
19
30
  this.name = 'ProvisionDiagnostic'
20
31
  this.code = safeCode
21
- diagnostics.set(this, safeCode)
32
+ diagnostics.set(this, { code: safeCode, message })
22
33
  }
23
34
  }
24
35
 
25
36
  export function provisionErrorMessage(error: unknown): string {
26
- return typeof error === 'object' && error !== null ? diagnostics.get(error) ?? 'provision_failed' : 'provision_failed'
37
+ return typeof error === 'object' && error !== null ? diagnostics.get(error)?.message ?? 'provision_failed' : 'provision_failed'
27
38
  }
28
39
 
29
40
  export function provisionFailure(error: unknown): ProvisionDiagnostic {
30
- const code = typeof error === 'object' && error !== null ? diagnostics.get(error) : undefined
31
- return new ProvisionDiagnostic(code ?? 'provision_failed')
41
+ const saved = typeof error === 'object' && error !== null ? diagnostics.get(error) : undefined
42
+ const failure = new ProvisionDiagnostic(saved?.code ?? 'provision_failed')
43
+ if (saved) { failure.message = saved.message; diagnostics.set(failure, saved) }
44
+ return failure
32
45
  }
@@ -1,13 +1,11 @@
1
1
  import type { Input } from '@pulumi/pulumi'
2
2
  import { readFile, readdir, stat } from 'node:fs/promises'
3
3
  import { dirname, join, resolve } from 'node:path'
4
- import { pathToFileURL } from 'node:url'
5
- import type { InstanceConfig } from '../config'
6
4
  import type { SchemaRevision } from '../schema-compatibility'
7
5
  import { migrationHash, readMigrationFiles, sha256 } from './migration-files'
8
- import { expectedSchema } from './schema'
9
6
  import { ProvisionDiagnostic, provisionFailure } from './provision-diagnostics'
10
- import { installedInfraDirectory, packageToolUrl } from './package-tools'
7
+ import { installedInfraDirectory } from './package-tools'
8
+ import { provisionConfigSchema } from './provision-config'
11
9
 
12
10
  export interface ProvisionOptions {
13
11
  /** Trusted application module, default-exporting an InstanceConfig. Paths resolve from projectRoot. */
@@ -104,7 +102,7 @@ async function packageTools() {
104
102
  }
105
103
 
106
104
  /** Build a secret-free resource specification, without SST, Pulumi execution, preview or network access.
107
- * Config modules are trusted code and are evaluated with the optional tsx loader. */
105
+ * Config modules are trusted code, evaluated in an isolated Node process with tsx. */
108
106
  export async function createProvisionPlan(options: ProvisionOptions): Promise<ProvisionPlan> {
109
107
  validateProvisionOptions(options)
110
108
  const root = resolve(options.projectRoot ?? process.cwd())
@@ -114,16 +112,10 @@ export async function createProvisionPlan(options: ProvisionOptions): Promise<Pr
114
112
  try {
115
113
  if (!(await stat(handler)).isFile() || !(await stat(configPath)).isFile() || !(await stat(migrationsDir)).isDirectory() || (assetsDir && !(await stat(assetsDir)).isDirectory())) throw 0
116
114
  } catch { throw new ProvisionDiagnostic('provision_inputs_unreadable') }
117
- let config: InstanceConfig
115
+ const secrets = [options.authSecret, options.internalToken, options.webhook?.url, options.webhook?.allowedHosts, options.webhook?.service]
116
+ .filter((value): value is string => typeof value === 'string' && value.length > 0)
117
+ const schema = await provisionConfigSchema(root, configPath, migrationsDir, secrets)
118
118
  try {
119
- // Leave the loader external to SST's bundle: tsx resolves files relative to
120
- // its installed module, and must not inherit the SST bundle's import.meta.url.
121
- const { tsImport } = await import(packageToolUrl('tsx/esm/api')) as typeof import('tsx/esm/api')
122
- config = (await tsImport(pathToFileURL(configPath).href, import.meta.url)).default
123
- } catch { throw new ProvisionDiagnostic('provision_config_load_failed') }
124
- if (!config || typeof config.id !== 'string' || !Number.isInteger(config.revision) || config.revision < 1 || !config.entities) throw new ProvisionDiagnostic('provision_invalid_config')
125
- try {
126
- const schema = await expectedSchema({ migrationsDirectory: migrationsDir, revision: config.revision, config })
127
119
  const files = await readMigrationFiles(migrationsDir)
128
120
  const tools = await packageTools()
129
121
  const name = (suffix: string) => resourceName(options.resourcePrefix, options.stage, suffix)
@@ -2,7 +2,7 @@ import type { ComponentResourceOptions, Input, Output } from '@pulumi/pulumi'
2
2
  import type { local } from '@pulumi/command'
3
3
  import { createProvisionPlan, type ProvisionOptions } from './provision-plan'
4
4
  import { ProvisionDiagnostic, provisionFailure } from './provision-diagnostics'
5
- import { packageToolUrl } from './package-tools'
5
+ import { sstCommand } from './sst-runtime'
6
6
  import type { CloudflareRuntime, DatabaseResource, KvResource, NativeBinding, QueueResource, SstRuntime, WorkerResource, CronResource } from './sst-runtime'
7
7
 
8
8
  export { createProvisionPlan, validateProvisionOptions, type ProvisionOptions, type ProvisionPlan } from './provision-plan'
@@ -13,6 +13,7 @@ export { ProvisionDiagnostic, provisionErrorMessage, type ProvisionDiagnosticCod
13
13
  declare const sst: SstRuntime
14
14
  declare const cloudflare: CloudflareRuntime
15
15
  declare const $util: Pick<typeof import('@pulumi/pulumi'), 'output' | 'secret'>
16
+ declare const $cli: { paths: { platform: string } }
16
17
 
17
18
  export interface ProvisionResult {
18
19
  stage: string
@@ -31,12 +32,10 @@ export interface ProvisionResult {
31
32
  * No resources are registered until all local inputs and expected schema are prepared. */
32
33
  export async function provision(options: ProvisionOptions): Promise<ProvisionResult> {
33
34
  const plan = await createProvisionPlan(options)
34
- if (typeof sst === 'undefined' || typeof cloudflare === 'undefined' || typeof $util === 'undefined') throw new ProvisionDiagnostic('provision_requires_sst')
35
+ if (typeof sst === 'undefined' || typeof cloudflare === 'undefined' || typeof $util === 'undefined' || typeof $cli === 'undefined') throw new ProvisionDiagnostic('provision_requires_sst')
35
36
  const accountId = options.accountId ?? process.env.CLOUDFLARE_DEFAULT_ACCOUNT_ID
36
37
  if (!accountId || !/^[a-f0-9]{32}$/.test(accountId)) throw new ProvisionDiagnostic('provision_invalid_account_id')
37
- let command: typeof import('@pulumi/command')
38
- try { command = await import(packageToolUrl('@pulumi/command')) }
39
- catch { throw new ProvisionDiagnostic('provision_requires_command') }
38
+ const command = sstCommand($cli.paths.platform)
40
39
  try {
41
40
  const protect = options.protect ?? options.stage === 'production'
42
41
  const protection = { protect, retainOnDelete: protect }
@@ -60,7 +59,7 @@ export async function provision(options: ProvisionOptions): Promise<ProvisionRes
60
59
  WB_CONFIG_MODULE: plan.configPath, CLOUDFLARE_DEFAULT_ACCOUNT_ID: accountId,
61
60
  },
62
61
  triggers: [...plan.migration.triggers, ...(options.migrationTriggers ?? [])],
63
- }, { protect: false, version: '1.2.1', dependsOn: [db, db.nodes.database] })
62
+ }, { protect: false, dependsOn: [db, db.nodes.database] })
64
63
 
65
64
  function secret(value: Input<string>, auth = false) {
66
65
  return $util.secret($util.output(value).apply(text => {
@@ -1,4 +1,19 @@
1
1
  import type { ComponentResourceOptions, Input, Output, Resource } from '@pulumi/pulumi'
2
+ import { createRequire } from 'node:module'
3
+ import { join } from 'node:path'
4
+ import { ProvisionDiagnostic } from './provision-diagnostics'
5
+
6
+ /** Load SST's built-in command provider, never a consumer's optional peer. Pulumi
7
+ * shares process-wide state, but protobuf messages must use the monitor's SDK. */
8
+ export function sstCommand(platform: string): typeof import('@pulumi/command') {
9
+ try {
10
+ const require = createRequire(join(platform, 'package.json'))
11
+ const entry = require.resolve('@pulumi/command')
12
+ // Fail before loading a provider with a nested, incompatible Pulumi runtime.
13
+ if (createRequire(entry).resolve('@pulumi/pulumi') !== require.resolve('@pulumi/pulumi')) throw new Error('duplicate_pulumi')
14
+ return require('@pulumi/command')
15
+ } catch { throw new ProvisionDiagnostic('provision_requires_command') }
16
+ }
2
17
 
3
18
  // SST's infrastructure constructors are injected by its config bundler; they are
4
19
  // not exports of the npm `sst` runtime. Keep this small structural boundary here so