@hyperfrontend/workspace/executors/service

service

Builds a service written on @hyperfrontend/server into a directory that runs with node alone: one CommonJS main with every package inlined, the manifest of its immutable section, its assets, and a release manifest that lists every file by size and SHA-256. The main is sealed by the kernel the service installs, so the built service refuses to boot beside bytes the build did not write.

{
  "build": {
    "executor": "@hyperfrontend/workspace:service",
    "options": {
      "assets": ["assets/**"]
    }
  }
}
OptionWhat it does
--outputPathWhere the service is written, relative to the root; defaults to dist/<project root>
--tsConfigThe TypeScript configuration, relative to the root; defaults to tsconfig.lib.json in the project
--assetsGlobs relative to src/, the directory of the entry module; each match is copied to the same path beside the main. A list, so it is set in project.json
--verbosePrints the build's diagnostics

The entry is always src/main.ts, and a project also needs service.config.ts, the TypeScript configuration and a package.json that states a name and a version. hf generate service writes all of them. An option the executor does not take is refused, and every problem of the options is reported at once.

What it writes

dist/apps/hello/
  main.cjs                 the bundle, executable
  service.manifest.json    the immutable section and its digest
  hf-release.json          every other file, with its size and SHA-256
  assets/notice.txt        each asset, at its path relative to src/

Nothing else: no package.json and no installed packages. Start it with node dist/apps/hello/main.cjs, with nothing between the platform's signal and the process. The build prints one line, built hello release=<sha256> immutable=<sha256>, and the running service prints the same release digest in its started line.

The output directory is emptied first when it is absent, empty or a previous build of the same service. A directory that holds anything else is refused. A build that fails once it has started writing removes what it wrote, and a build refused before that leaves the previous build as it was. A built service checks its own directory at boot, so it must not write beside its main: data lives elsewhere.

The immutable section

service.config.ts default-exports a plain object with one member, immutable, holding the structural settings fixed when the service is built. It must be JSON values only, nested no deeper than 32 levels, and the file is read twice: by the build and by the service itself, which hands the section to the runner. So the file has to give the same value every time. A section built from the clock or the environment gets another digest at boot, and the service refuses to start.

// service.config.ts
export default { immutable: { greeting: 'Hello', limits: { names: 3 } } }

Assets

In the built main every module has the main's URL, so a file is addressed the same way from source and built only when the URL is formed in a module directly in src/. Form the base once there and import it everywhere else:

// src/assets.ts
export const assetsUrl = new URL('./assets/', import.meta.url)

// src/routes/notice.ts
readFileSync(fileURLToPath(new URL('notice.txt', assetsUrl)), 'utf8')

The build reads the service's own sources and refuses the ones that would resolve differently once built: import.meta.url in a module below src/ or in service.config.ts, and import.meta.dirname or import.meta.filename anywhere, since a built service has neither. A glob that matches no file is refused, and so is one that matches main.cjs, service.manifest.json or hf-release.json.

Limits

  • Every package is inlined, so a native addon cannot be built in, and there is no source map.
  • The build checks that the main reaches the runner, not that it always starts through it, so keep src/main.ts as generated.
  • A service that imports a workspace library through a path alias needs rootDir stated in tsconfig.base.json, which hf generate workspace writes.
  • The source check reads the service's own files, service.config.ts included, and sees a member read from import.meta directly. It does not follow one handed to a variable first, and it does not read a workspace library, which is inlined with the main's URL.
  • The release check catches a partial or changed deployment. It is not a defence against someone who can write to the directory.

API Reference§

ƒ Functions

§function

checkServiceConfig(value: unknown): unknown

Checks what a service.config.ts exports by default, and reports every problem at once.
Parameters
NameTypeDescription
§value
unknownThe default export
Returns
unknown
The problems in document order; none for a configuration the build can read
Example

A configuration with two problems

import { checkServiceConfig } from '@hyperfrontend/workspace/executors/service'

checkServiceConfig({ immutable: { limits: { names: 3 } } }) // => []
checkServiceConfig({ immutable: { since: new Date(0) }, routes: [] }).map((issue) => issue.message)
// => [
//   'immutable.since must be a JSON value: a string, a finite number, a boolean, null, an array or a plain object.',
//   'the configuration has an unknown member "routes": it takes immutable.',
// ]

◈ Interfaces

§interface

ServiceConfig

What service.config.ts default-exports.
Properties
§readonly immutable?:Readonly<Record<string, JsonValue>>The immutable section: structural settings fixed when the service is built. An empty object when omitted.
§interface

ServiceConfigIssue

One problem of a service configuration.
Properties
§readonly code:ServiceConfigIssueCodeWhat kind of problem it is.
§readonly path:stringWhere it is, such as immutable.origins[2]; empty for the export itself.
§readonly message:stringThe problem in a sentence, holding member names and paths and never a value.
§interface

ServiceExecutorOptions

Options for the service executor.
Properties
§outputPath?:stringOutput directory, relative to the workspace root. Defaults to dist/<project root>.
§tsConfig?:stringPath to tsconfig, relative to the workspace root. Defaults to <project root>/tsconfig.lib.json.
§assets?:string[]Globs relative to the entry module's directory; each match is copied to the same relative path beside the main.
§verbose?:booleanEnable verbose / debug logging.
§interface

ServiceManifest

What service.manifest.json holds, schema 1.
Properties
§readonly schema:1The schema of the file.
§readonly immutable:ServiceManifestSectionThe immutable section of the build.
§interface

ServiceManifestSection

The immutable section of a build, as service.manifest.json states it.
Properties
§readonly digest:stringThe SHA-256 of the section's compact JSON, in 64 lowercase hexadecimal characters.
§readonly value:Readonly<Record<string, JsonValue>>The section, in the author's key order.

◆ Types

§type

JsonValue

A JSON value.
type JsonValue = string | number | boolean | null | unknown | { }
§type

ServiceConfigIssueCode

What is wrong with a service configuration.
type ServiceConfigIssueCode = "config-not-object" | "config-unknown-member" | "immutable-not-object" | "immutable-not-json" | "immutable-too-deep"

● Variables

§type

serviceExecutor

Builds a service: one CommonJS main, service.manifest.json, the assets and hf-release.json.
Example

A `build` target that copies a directory of assets beside the main

{
  "targets": {
    "build": {
      "executor": "@hyperfrontend/workspace:service",
      "options": { "assets": ["assets/**"] }
    }
  }
}
```
Run with `npx hf run hello:build`; the service lands in `dist/apps/hello` and starts with `node dist/apps/hello/main.cjs`.