@tinycloud/operations 0.2.1-beta.0 → 0.3.0-beta.0

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/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { I as InvocationTarget, a as OperationResult } from './contract-OnWT3OXa.cjs';
1
+ import { I as InvocationTarget, a as OperationResult } from './contract-xMIIMtpF.cjs';
2
2
 
3
3
  /** The sole projection-facing execution API. */
4
4
  declare function invokeOperation(operationId: string, operationVersion: number, invocationTarget: InvocationTarget, unknownInput: unknown): Promise<OperationResult<unknown>>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { I as InvocationTarget, a as OperationResult } from './contract-OnWT3OXa.js';
1
+ import { I as InvocationTarget, a as OperationResult } from './contract-xMIIMtpF.js';
2
2
 
3
3
  /** The sole projection-facing execution API. */
4
4
  declare function invokeOperation(operationId: string, operationVersion: number, invocationTarget: InvocationTarget, unknownInput: unknown): Promise<OperationResult<unknown>>;
package/dist/index.js CHANGED
@@ -6752,6 +6752,7 @@ var init_authority = __esm({
6752
6752
 
6753
6753
  // src/state.ts
6754
6754
  import { randomUUID } from "crypto";
6755
+ import { AsyncLocalStorage } from "async_hooks";
6755
6756
  import {
6756
6757
  mkdir,
6757
6758
  readFile,
@@ -6761,11 +6762,18 @@ import {
6761
6762
  writeFile
6762
6763
  } from "fs/promises";
6763
6764
  import { homedir } from "os";
6764
- import { basename, dirname, join, relative, resolve } from "path";
6765
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "path";
6765
6766
  function tinycloudHomePath() {
6766
- const home = process.env.TC_HOME ?? process.env.HOME ?? process.env.USERPROFILE ?? homedir();
6767
+ const home = invocationStateRoot.getStore() ?? process.env.TC_HOME ?? process.env.HOME ?? process.env.USERPROFILE ?? homedir();
6767
6768
  return join(home, ".tinycloud");
6768
6769
  }
6770
+ function withTinyCloudStateRoot(stateRoot, action) {
6771
+ if (stateRoot === void 0) return action();
6772
+ if (!isAbsolute(stateRoot) || stateRoot.includes("\0")) {
6773
+ throw new TypeError("The TinyCloud state root must be an absolute path.");
6774
+ }
6775
+ return invocationStateRoot.run(resolve(stateRoot), action);
6776
+ }
6769
6777
  function tinycloudConfigPath() {
6770
6778
  return join(tinycloudHomePath(), "config.json");
6771
6779
  }
@@ -7017,7 +7025,7 @@ function isErrno(error, code) {
7017
7025
  function sleep(milliseconds) {
7018
7026
  return new Promise((resolve2) => setTimeout(resolve2, milliseconds));
7019
7027
  }
7020
- var DEFAULT_PROFILE, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_LOCK_RETRY_MS, DEFAULT_STALE_LOCK_MS, TEST_LOCK_CONTENTION_SIGNAL_PATH, TEST_LOCK_RECOVERY_BARRIER_DIR, ProfileLockTimeoutError;
7028
+ var DEFAULT_PROFILE, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_LOCK_RETRY_MS, DEFAULT_STALE_LOCK_MS, TEST_LOCK_CONTENTION_SIGNAL_PATH, TEST_LOCK_RECOVERY_BARRIER_DIR, invocationStateRoot, ProfileLockTimeoutError;
7021
7029
  var init_state = __esm({
7022
7030
  "src/state.ts"() {
7023
7031
  "use strict";
@@ -7027,6 +7035,7 @@ var init_state = __esm({
7027
7035
  DEFAULT_STALE_LOCK_MS = 3e4;
7028
7036
  TEST_LOCK_CONTENTION_SIGNAL_PATH = "TC_TEST_PROFILE_LOCK_CONTENTION_SIGNAL_PATH";
7029
7037
  TEST_LOCK_RECOVERY_BARRIER_DIR = "TC_TEST_PROFILE_LOCK_RECOVERY_BARRIER_DIR";
7038
+ invocationStateRoot = new AsyncLocalStorage();
7030
7039
  ProfileLockTimeoutError = class extends Error {
7031
7040
  constructor(profile, timeoutMs) {
7032
7041
  super(`Timed out waiting for the profile lock for "${profile}" after ${timeoutMs}ms.`);
@@ -9910,6 +9919,7 @@ var init_runtime = __esm({
9910
9919
 
9911
9920
  // src/invoke.ts
9912
9921
  import { createHash } from "crypto";
9922
+ import { isAbsolute as isAbsolute2 } from "path";
9913
9923
  import {
9914
9924
  makePkhSpaceId as makePkhSpaceId3,
9915
9925
  parsePkhDid as parsePkhDid3,
@@ -10253,16 +10263,27 @@ function isRecord(value) {
10253
10263
  }
10254
10264
 
10255
10265
  // src/invoke.ts
10266
+ init_state();
10256
10267
  init_secrets();
10257
10268
  async function invokeOperation(operationId, operationVersion, invocationTarget, unknownInput) {
10258
- const prepared = await prepareInvocation(
10259
- operationId,
10260
- operationVersion,
10261
- invocationTarget,
10262
- unknownInput
10263
- );
10264
- if (isPreparedError(prepared)) return prepared.result;
10265
- return executePreparedInvocation(prepared);
10269
+ const target = normalizeInvocationTarget(invocationTarget);
10270
+ if (target === void 0) {
10271
+ return errorResult(
10272
+ { operationId, operationVersion },
10273
+ unresolvedContextSummary(),
10274
+ operationError("INPUT_INVALID", "The invocation target is invalid.")
10275
+ );
10276
+ }
10277
+ return withTinyCloudStateRoot(target.stateRoot, async () => {
10278
+ const prepared = await prepareInvocation(
10279
+ operationId,
10280
+ operationVersion,
10281
+ target,
10282
+ unknownInput
10283
+ );
10284
+ if (isPreparedError(prepared)) return prepared.result;
10285
+ return executePreparedInvocation(prepared);
10286
+ });
10266
10287
  }
10267
10288
  function isPreparedError(prepared) {
10268
10289
  return "status" in prepared;
@@ -10605,14 +10626,16 @@ function normalizeInvocationTarget(target) {
10605
10626
  const candidate = target;
10606
10627
  const profile = candidate.profile;
10607
10628
  const host = candidate.host;
10629
+ const stateRoot = candidate.stateRoot;
10608
10630
  const allowOwnerProfile = candidate.allowOwnerProfile;
10609
10631
  const privateKey = candidate.privateKey;
10610
- if (profile !== void 0 && typeof profile !== "string" || host !== void 0 && typeof host !== "string" || allowOwnerProfile !== void 0 && typeof allowOwnerProfile !== "boolean" || privateKey !== void 0 && typeof privateKey !== "string") {
10632
+ if (profile !== void 0 && typeof profile !== "string" || host !== void 0 && typeof host !== "string" || stateRoot !== void 0 && (typeof stateRoot !== "string" || !isAbsolute2(stateRoot)) || allowOwnerProfile !== void 0 && typeof allowOwnerProfile !== "boolean" || privateKey !== void 0 && typeof privateKey !== "string") {
10611
10633
  return void 0;
10612
10634
  }
10613
10635
  return {
10614
10636
  ...profile === void 0 ? {} : { profile },
10615
10637
  ...host === void 0 ? {} : { host },
10638
+ ...stateRoot === void 0 ? {} : { stateRoot },
10616
10639
  ...allowOwnerProfile === true ? { allowOwnerProfile: true } : {},
10617
10640
  ...privateKey === void 0 ? {} : { privateKey }
10618
10641
  };