@restatedev/restate-sdk 1.17.4 → 1.18.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/dist/common_api.d.cts +1 -1
- package/dist/common_api.d.cts.map +1 -1
- package/dist/common_api.d.ts +1 -1
- package/dist/common_api.d.ts.map +1 -1
- package/dist/common_api.js.map +1 -1
- package/dist/context.d.cts +3 -0
- package/dist/context.d.cts.map +1 -1
- package/dist/context.d.ts +3 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js.map +1 -1
- package/dist/context_impl.cjs +8 -3
- package/dist/context_impl.js +8 -3
- package/dist/context_impl.js.map +1 -1
- package/dist/endpoint/components.cjs +28 -6
- package/dist/endpoint/components.js +28 -6
- package/dist/endpoint/components.js.map +1 -1
- package/dist/endpoint/connect/env.cjs +206 -0
- package/dist/endpoint/connect/env.js +199 -0
- package/dist/endpoint/connect/env.js.map +1 -0
- package/dist/endpoint/connect/index.cjs +113 -0
- package/dist/endpoint/connect/index.d.cts +41 -0
- package/dist/endpoint/connect/index.d.cts.map +1 -0
- package/dist/endpoint/connect/index.d.ts +41 -0
- package/dist/endpoint/connect/index.d.ts.map +1 -0
- package/dist/endpoint/connect/index.js +114 -0
- package/dist/endpoint/connect/index.js.map +1 -0
- package/dist/endpoint/connect/key.cjs +53 -0
- package/dist/endpoint/connect/key.js +53 -0
- package/dist/endpoint/connect/key.js.map +1 -0
- package/dist/endpoint/handlers/discovery.cjs +12 -2
- package/dist/endpoint/handlers/discovery.js +12 -2
- package/dist/endpoint/handlers/discovery.js.map +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.cjs +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js +1 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js.map +1 -1
- package/dist/endpoint/tunnel/backoff.cjs +37 -0
- package/dist/endpoint/tunnel/backoff.js +36 -0
- package/dist/endpoint/tunnel/backoff.js.map +1 -0
- package/dist/endpoint/tunnel/connection.cjs +604 -0
- package/dist/endpoint/tunnel/connection.js +600 -0
- package/dist/endpoint/tunnel/connection.js.map +1 -0
- package/dist/endpoint/tunnel/draining.cjs +40 -0
- package/dist/endpoint/tunnel/draining.js +40 -0
- package/dist/endpoint/tunnel/draining.js.map +1 -0
- package/dist/endpoint/tunnel/engine.cjs +264 -0
- package/dist/endpoint/tunnel/engine.d.cts +1 -0
- package/dist/endpoint/tunnel/engine.d.ts +1 -0
- package/dist/endpoint/tunnel/engine.js +265 -0
- package/dist/endpoint/tunnel/engine.js.map +1 -0
- package/dist/endpoint/tunnel/forwarded.cjs +34 -0
- package/dist/endpoint/tunnel/forwarded.js +34 -0
- package/dist/endpoint/tunnel/forwarded.js.map +1 -0
- package/dist/endpoint/tunnel/handshake.cjs +98 -0
- package/dist/endpoint/tunnel/handshake.js +97 -0
- package/dist/endpoint/tunnel/handshake.js.map +1 -0
- package/dist/endpoint/tunnel/index.d.cts +3 -0
- package/dist/endpoint/tunnel/index.d.ts +3 -0
- package/dist/endpoint/tunnel/options.cjs +168 -0
- package/dist/endpoint/tunnel/options.d.cts +2 -0
- package/dist/endpoint/tunnel/options.d.ts +2 -0
- package/dist/endpoint/tunnel/options.js +161 -0
- package/dist/endpoint/tunnel/options.js.map +1 -0
- package/dist/endpoint/tunnel/supervisor.cjs +180 -0
- package/dist/endpoint/tunnel/supervisor.js +181 -0
- package/dist/endpoint/tunnel/supervisor.js.map +1 -0
- package/dist/endpoint/tunnel/targets.cjs +110 -0
- package/dist/endpoint/tunnel/targets.js +107 -0
- package/dist/endpoint/tunnel/targets.js.map +1 -0
- package/dist/endpoint/tunnel/types.d.cts +270 -0
- package/dist/endpoint/tunnel/types.d.cts.map +1 -0
- package/dist/endpoint/tunnel/types.d.ts +270 -0
- package/dist/endpoint/tunnel/types.d.ts.map +1 -0
- package/dist/endpoint/tunnel/util.cjs +67 -0
- package/dist/endpoint/tunnel/util.js +65 -0
- package/dist/endpoint/tunnel/util.js.map +1 -0
- package/dist/fetch.d.cts +2 -2
- package/dist/fetch.d.ts +2 -2
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/lambda.d.cts +2 -2
- package/dist/lambda.d.ts +2 -2
- package/dist/node.cjs +2 -0
- package/dist/node.d.cts +5 -2
- package/dist/node.d.cts.map +1 -1
- package/dist/node.d.ts +5 -2
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2 -1
- package/dist/node.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/dist/types/errors.cjs +4 -2
- package/dist/types/errors.d.cts.map +1 -1
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +4 -2
- package/dist/types/errors.js.map +1 -1
- package/dist/types/rpc.d.cts +42 -17
- package/dist/types/rpc.d.cts.map +1 -1
- package/dist/types/rpc.d.ts +42 -17
- package/dist/types/rpc.d.ts.map +1 -1
- package/dist/types/rpc.js.map +1 -1
- package/package.json +2 -2
- package/dist/context_impl.d.ts.map +0 -1
- package/dist/endpoint/components.d.ts.map +0 -1
- package/dist/endpoint/discovery.d.ts +0 -184
- package/dist/endpoint/discovery.d.ts.map +0 -1
- package/dist/endpoint/discovery.js +0 -7
- package/dist/endpoint/discovery.js.map +0 -1
- package/dist/endpoint/endpoint.d.ts.map +0 -1
- package/dist/endpoint/handlers/core_logging.d.ts +0 -14
- package/dist/endpoint/handlers/core_logging.d.ts.map +0 -1
- package/dist/endpoint/handlers/discovery.d.ts +0 -5
- package/dist/endpoint/handlers/discovery.d.ts.map +0 -1
- package/dist/endpoint/handlers/fetch.d.ts +0 -5
- package/dist/endpoint/handlers/fetch.d.ts.map +0 -1
- package/dist/endpoint/handlers/generic.d.ts +0 -5
- package/dist/endpoint/handlers/generic.d.ts.map +0 -1
- package/dist/endpoint/handlers/lambda.d.ts +0 -10
- package/dist/endpoint/handlers/lambda.d.ts.map +0 -1
- package/dist/endpoint/handlers/preview.d.ts +0 -5
- package/dist/endpoint/handlers/preview.d.ts.map +0 -1
- package/dist/endpoint/handlers/types.d.ts.map +0 -1
- package/dist/endpoint/handlers/types.js +0 -2
- package/dist/endpoint/handlers/types.js.map +0 -1
- package/dist/endpoint/handlers/utils.d.ts +0 -23
- package/dist/endpoint/handlers/utils.d.ts.map +0 -1
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +0 -454
- package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts.map +0 -1
- package/dist/endpoint/node_endpoint.d.ts +0 -28
- package/dist/endpoint/node_endpoint.d.ts.map +0 -1
- package/dist/endpoint/types.js +0 -2
- package/dist/endpoint/types.js.map +0 -1
- package/dist/endpoint/withOptions.d.ts +0 -4
- package/dist/endpoint/withOptions.d.ts.map +0 -1
- package/dist/endpoint.js +0 -12
- package/dist/endpoint.js.map +0 -1
- package/dist/error_sanitization.d.ts +0 -13
- package/dist/error_sanitization.d.ts.map +0 -1
- package/dist/hooks.js +0 -2
- package/dist/hooks.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/io.d.ts.map +0 -1
- package/dist/logging/console_logger_transport.d.ts +0 -5
- package/dist/logging/console_logger_transport.d.ts.map +0 -1
- package/dist/logging/logger.d.ts.map +0 -1
- package/dist/user_agent.d.ts +0 -2
- package/dist/user_agent.d.ts.map +0 -1
- package/dist/utils/external_progress_channel.d.ts +0 -19
- package/dist/utils/external_progress_channel.d.ts.map +0 -1
- package/dist/utils/rand.d.ts +0 -13
- package/dist/utils/rand.d.ts.map +0 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_options = require('../tunnel/options.cjs');
|
|
3
|
+
const require_key = require('./key.cjs');
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
node_fs = require_rolldown_runtime.__toESM(node_fs);
|
|
6
|
+
|
|
7
|
+
//#region src/endpoint/connect/env.ts
|
|
8
|
+
/** The Cloud UI, for the deep links below. */
|
|
9
|
+
const CLOUD_UI_URL = "https://cloud.restate.dev";
|
|
10
|
+
/** Where to get a connect key (Development → Configure SDK), linked in every error. */
|
|
11
|
+
const CONNECT_KEY_URL = `${CLOUD_UI_URL}/to/developers/integration#configure-sdk`;
|
|
12
|
+
/** The deployment name, in resolution order. */
|
|
13
|
+
const CONNECT_NAME_ENVS = [
|
|
14
|
+
"RESTATE_CONNECT_NAME",
|
|
15
|
+
"RESTATE_INPROC_TUNNEL_NAME",
|
|
16
|
+
"RESTATE_TUNNEL_NAME"
|
|
17
|
+
];
|
|
18
|
+
/** The connect key. */
|
|
19
|
+
const CONNECT_KEY_ENV = "RESTATE_CONNECT_KEY";
|
|
20
|
+
const ENVIRONMENT_ID_ENVS = ["RESTATE_ENVIRONMENT_ID", "RESTATE_INPROC_ENVIRONMENT_ID"];
|
|
21
|
+
const AUTH_TOKEN_ENVS = ["RESTATE_AUTH_TOKEN"];
|
|
22
|
+
/** A file holding the token, re-read on every reconnect (a mounted Secret). */
|
|
23
|
+
const AUTH_TOKEN_FILE_ENVS = ["RESTATE_INPROC_AUTH_TOKEN_FILE"];
|
|
24
|
+
const CLOUD_REGION_ENVS = ["RESTATE_CLOUD_REGION", "RESTATE_INPROC_CLOUD_REGION"];
|
|
25
|
+
const TUNNEL_SERVERS_SRV_ENVS = ["RESTATE_TUNNEL_SERVERS_SRV"];
|
|
26
|
+
const TUNNEL_SERVERS_ENVS = ["RESTATE_TUNNEL_SERVERS"];
|
|
27
|
+
const SIGNING_PUBLIC_KEY_ENVS = ["RESTATE_SIGNING_PUBLIC_KEY", "RESTATE_INPROC_SIGNING_PUBLIC_KEY"];
|
|
28
|
+
/** The query parameters a connect key may carry; anything else is ignored with a warning. */
|
|
29
|
+
const KNOWN_KEY_PARAMS = [
|
|
30
|
+
"signing-key",
|
|
31
|
+
"tunnel-srv",
|
|
32
|
+
"tunnel-servers"
|
|
33
|
+
];
|
|
34
|
+
/** An env var set to the empty string is treated as unset. */
|
|
35
|
+
function readEnv(name) {
|
|
36
|
+
const value = process.env[name];
|
|
37
|
+
return value === void 0 || value === "" ? void 0 : value;
|
|
38
|
+
}
|
|
39
|
+
/** The first set variable among `names`, with its name. */
|
|
40
|
+
function readFirstEnv(names) {
|
|
41
|
+
for (const name of names) {
|
|
42
|
+
const value = readEnv(name);
|
|
43
|
+
if (value !== void 0) return {
|
|
44
|
+
value,
|
|
45
|
+
name
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function given(value) {
|
|
50
|
+
return value !== void 0 && value !== "";
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The deployment name: option → `RESTATE_CONNECT_NAME` →
|
|
54
|
+
* `RESTATE_INPROC_TUNNEL_NAME` → `RESTATE_TUNNEL_NAME` → error. No inference:
|
|
55
|
+
* an unnamed deployment is a mistake, and the error says what to set.
|
|
56
|
+
*/
|
|
57
|
+
function resolveName(option) {
|
|
58
|
+
const name = given(option) ? option : readFirstEnv(CONNECT_NAME_ENVS)?.value;
|
|
59
|
+
if (name === void 0) throw new Error(`connect(): the deployment needs a name — pass { name: "greeter-v1" } or set ${CONNECT_NAME_ENVS[0]}. Replicas of one deployment share the name; it is what you register (connect://<name>).`);
|
|
60
|
+
if (!/^[A-Za-z0-9._-]+$/.test(name)) throw new Error(`connect(): invalid deployment name ${JSON.stringify(name)} — use letters, digits, '.', '_' or '-'`);
|
|
61
|
+
return name;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The coordinates for `connect()`: `key` option → `RESTATE_CONNECT_KEY` →
|
|
65
|
+
* the expanded variables → error naming the variable and the Cloud UI page.
|
|
66
|
+
* Explicit `identityKeys` and discovery options win over the key's.
|
|
67
|
+
*/
|
|
68
|
+
function resolveCoordinates(options, warn) {
|
|
69
|
+
if (given(options.key)) return coordinatesFromKey(require_key.parseCloudKey(options.key, "the `key` option", CONNECT_KEY_URL), options, warn);
|
|
70
|
+
const fromEnv = readEnv(CONNECT_KEY_ENV);
|
|
71
|
+
if (fromEnv !== void 0) return coordinatesFromKey(require_key.parseCloudKey(fromEnv, CONNECT_KEY_ENV, CONNECT_KEY_URL), options, warn);
|
|
72
|
+
if (readFirstEnv(ENVIRONMENT_ID_ENVS) === void 0) throw new Error(`connect(): no Restate Cloud connect key — set ${CONNECT_KEY_ENV} (create one at ${CONNECT_KEY_URL}) or pass { key }`);
|
|
73
|
+
return coordinatesFromExpanded(options);
|
|
74
|
+
}
|
|
75
|
+
function coordinatesFromKey(key, options, warn) {
|
|
76
|
+
const params = /* @__PURE__ */ new Map();
|
|
77
|
+
const unknown = [];
|
|
78
|
+
for (const [name, value] of key.params) if (KNOWN_KEY_PARAMS.includes(name)) params.set(name, value);
|
|
79
|
+
else unknown.push(name);
|
|
80
|
+
if (unknown.length > 0) warn(`connect(): ignoring unknown connect key parameter(s) ${unknown.map((n) => JSON.stringify(n)).join(", ")} — this SDK version knows ${KNOWN_KEY_PARAMS.join(", ")}`);
|
|
81
|
+
const injected = readFirstEnv(ENVIRONMENT_ID_ENVS);
|
|
82
|
+
if (injected !== void 0 && injected.value !== key.environmentId) warn(`connect(): the connect key is for environment ${key.environmentId} but ${injected.name} is set to ${injected.value}; the key wins`);
|
|
83
|
+
const identityKeys = resolveIdentityKeys(options.identityKeys, params.get("signing-key"), `the connect key has no signing-key parameter and no identityKeys were given — copy the key again from ${CONNECT_KEY_URL}`);
|
|
84
|
+
const authToken = key.apiKey !== void 0 ? constantToken(key.apiKey, "the connect key's API key") : tokenFromEnv() ?? fail(`the connect key carries no API key (restate://<api key>@…) and no token is configured in ${AUTH_TOKEN_ENVS[0]} or a file in ${AUTH_TOKEN_FILE_ENVS[0]}`);
|
|
85
|
+
const keyServers = params.get("tunnel-servers");
|
|
86
|
+
if (keyServers !== void 0 && splitList(keyServers).length === 0) fail("the connect key's tunnel-servers parameter is empty");
|
|
87
|
+
const keySrv = params.get("tunnel-srv");
|
|
88
|
+
if (keySrv === "") fail("the connect key's tunnel-srv parameter is empty");
|
|
89
|
+
if (keySrv !== void 0 && keyServers !== void 0) fail("the connect key carries both tunnel-srv and tunnel-servers; it may carry one");
|
|
90
|
+
const keyDiscovery = {
|
|
91
|
+
serversSrv: keySrv,
|
|
92
|
+
servers: keyServers === void 0 ? void 0 : splitList(keyServers)
|
|
93
|
+
};
|
|
94
|
+
const override = pickDiscovery(options);
|
|
95
|
+
const discovery = override ?? pickDiscovery(keyDiscovery) ?? { srvName: require_options.srvNameForRegionDomain(key.regionDomain) };
|
|
96
|
+
return {
|
|
97
|
+
environmentId: key.environmentId,
|
|
98
|
+
authToken,
|
|
99
|
+
identityKeys,
|
|
100
|
+
discovery,
|
|
101
|
+
regionLabel: override === void 0 && pickDiscovery(keyDiscovery) === void 0 ? key.regionDomain : discoveryLabel(discovery)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The coordinates from the expanded variables (`RESTATE_<X>`, then
|
|
106
|
+
* `RESTATE_INPROC_<X>`), each missing piece an error naming its variable.
|
|
107
|
+
* Explicit `identityKeys` and discovery options win over the environment.
|
|
108
|
+
*/
|
|
109
|
+
function coordinatesFromExpanded(o) {
|
|
110
|
+
const environmentId = require_options.requireHeaderSafe(readFirstEnv(ENVIRONMENT_ID_ENVS)?.value ?? fail(`environmentId is required — set ${ENVIRONMENT_ID_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`), "environmentId");
|
|
111
|
+
if (!/^env_[A-Za-z0-9_-]+$/.test(environmentId)) throw new Error(`connect(): environmentId must be \`env_\` followed by alphanumerics (e.g. env_201k0yd4...), got ${JSON.stringify(environmentId)}`);
|
|
112
|
+
const authToken = tokenFromEnv() ?? fail(`authToken is required — set ${AUTH_TOKEN_ENVS[0]} or a token file in ${AUTH_TOKEN_FILE_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
113
|
+
const identityKeys = resolveIdentityKeys(o.identityKeys, readFirstEnv(SIGNING_PUBLIC_KEY_ENVS)?.value, `signingPublicKey is required — set ${SIGNING_PUBLIC_KEY_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
114
|
+
let discovery = pickDiscovery(o);
|
|
115
|
+
let regionLabel;
|
|
116
|
+
if (discovery === void 0) {
|
|
117
|
+
const region = readEnv(CLOUD_REGION_ENVS[0]);
|
|
118
|
+
const srv = readFirstEnv(TUNNEL_SERVERS_SRV_ENVS)?.value;
|
|
119
|
+
const servers = readFirstEnv(TUNNEL_SERVERS_ENVS)?.value;
|
|
120
|
+
const inproc = readEnv(CLOUD_REGION_ENVS[1]);
|
|
121
|
+
if (region !== void 0 || srv === void 0 && servers === void 0) {
|
|
122
|
+
const value = region ?? inproc;
|
|
123
|
+
if (value === void 0) throw new Error(`connect(): no Restate Cloud region — set ${CLOUD_REGION_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
124
|
+
const domain = regionDomainForRegion(value);
|
|
125
|
+
discovery = { srvName: require_options.srvNameForRegionDomain(domain) };
|
|
126
|
+
regionLabel = domain;
|
|
127
|
+
} else if (srv !== void 0) discovery = { srvName: srv };
|
|
128
|
+
else discovery = { servers: splitList(servers) };
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
environmentId,
|
|
132
|
+
authToken,
|
|
133
|
+
identityKeys,
|
|
134
|
+
discovery,
|
|
135
|
+
regionLabel: regionLabel ?? discoveryLabel(discovery)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function fail(message) {
|
|
139
|
+
throw new Error(`connect(): ${message}`);
|
|
140
|
+
}
|
|
141
|
+
function resolveIdentityKeys(option, signingKey, missing) {
|
|
142
|
+
if (option !== void 0 && option.length > 0) return option;
|
|
143
|
+
if (signingKey === void 0) fail(missing);
|
|
144
|
+
if (!signingKey.startsWith("publickeyv1_")) fail(`the signing key must be a request-identity public key (publickeyv1_...), got ${JSON.stringify(signingKey)}`);
|
|
145
|
+
return [signingKey];
|
|
146
|
+
}
|
|
147
|
+
function constantToken(token, what) {
|
|
148
|
+
require_options.requireHeaderSafe(token, what);
|
|
149
|
+
return () => token;
|
|
150
|
+
}
|
|
151
|
+
/** `RESTATE_AUTH_TOKEN`, then the file named by `RESTATE_INPROC_AUTH_TOKEN_FILE`. */
|
|
152
|
+
function tokenFromEnv() {
|
|
153
|
+
const token = readFirstEnv(AUTH_TOKEN_ENVS);
|
|
154
|
+
if (token !== void 0) return constantToken(token.value, token.name);
|
|
155
|
+
const file = readFirstEnv(AUTH_TOKEN_FILE_ENVS);
|
|
156
|
+
if (file === void 0) return void 0;
|
|
157
|
+
const tokenFile = file.value;
|
|
158
|
+
const readToken = () => {
|
|
159
|
+
const stat = node_fs.statSync(tokenFile);
|
|
160
|
+
if (!stat.isFile()) throw new Error(`connect(): token file ${tokenFile} (${file.name}) is not a regular file`);
|
|
161
|
+
if (stat.size > 64 * 1024) throw new Error(`connect(): token file ${tokenFile} (${file.name}) is implausibly large for a token (${stat.size} bytes)`);
|
|
162
|
+
const token$1 = node_fs.readFileSync(tokenFile, "utf8").trim();
|
|
163
|
+
if (token$1 === "") throw new Error(`connect(): token file ${tokenFile} (${file.name}) is empty`);
|
|
164
|
+
return require_options.requireHeaderSafe(token$1, `token file ${tokenFile}`);
|
|
165
|
+
};
|
|
166
|
+
readToken();
|
|
167
|
+
return readToken;
|
|
168
|
+
}
|
|
169
|
+
function pickDiscovery(o) {
|
|
170
|
+
if (given(o.serversSrv) && o.servers !== void 0) throw new Error("connect(): specify at most one of `serversSrv` and `servers`");
|
|
171
|
+
if (given(o.serversSrv)) return { srvName: o.serversSrv };
|
|
172
|
+
if (o.servers !== void 0) {
|
|
173
|
+
if (o.servers.length === 0) throw new Error("connect(): `servers` must not be empty");
|
|
174
|
+
return { servers: o.servers };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** A region (`us`, `eu`, `<slug>.byoc`) is the labels before `.restate.cloud`. */
|
|
178
|
+
function regionDomainForRegion(region) {
|
|
179
|
+
if (!/^[a-z0-9-]+(\.[a-z0-9-]+)*$/.test(region)) throw new Error(`connect(): invalid region ${JSON.stringify(region)}`);
|
|
180
|
+
return `${region}.restate.cloud`;
|
|
181
|
+
}
|
|
182
|
+
/** Human-readable discovery target for the banner. */
|
|
183
|
+
function discoveryLabel(discovery) {
|
|
184
|
+
if ("srvName" in discovery) return discovery.srvName.replace(/^tunnel\./, "");
|
|
185
|
+
return discovery.servers.join(",");
|
|
186
|
+
}
|
|
187
|
+
function splitList(value) {
|
|
188
|
+
return value.split(",").map((s) => s.trim()).filter((s) => s !== "");
|
|
189
|
+
}
|
|
190
|
+
/** The registration identifier the CLI and the Cloud UI accept. */
|
|
191
|
+
function registrationIdentifier(name) {
|
|
192
|
+
return `connect://${name}`;
|
|
193
|
+
}
|
|
194
|
+
/** The Cloud UI deep link that opens the register dialog pre-filled. */
|
|
195
|
+
function registrationDeepLink(name) {
|
|
196
|
+
return `${CLOUD_UI_URL}/to/overview?register-deployment=${registrationIdentifier(name)}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
exports.CONNECT_KEY_ENV = CONNECT_KEY_ENV;
|
|
201
|
+
exports.CONNECT_KEY_URL = CONNECT_KEY_URL;
|
|
202
|
+
exports.CONNECT_NAME_ENVS = CONNECT_NAME_ENVS;
|
|
203
|
+
exports.registrationDeepLink = registrationDeepLink;
|
|
204
|
+
exports.registrationIdentifier = registrationIdentifier;
|
|
205
|
+
exports.resolveCoordinates = resolveCoordinates;
|
|
206
|
+
exports.resolveName = resolveName;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { requireHeaderSafe, srvNameForRegionDomain } from "../tunnel/options.js";
|
|
2
|
+
import { parseCloudKey } from "./key.js";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
|
|
5
|
+
//#region src/endpoint/connect/env.ts
|
|
6
|
+
/** The Cloud UI, for the deep links below. */
|
|
7
|
+
const CLOUD_UI_URL = "https://cloud.restate.dev";
|
|
8
|
+
/** Where to get a connect key (Development → Configure SDK), linked in every error. */
|
|
9
|
+
const CONNECT_KEY_URL = `${CLOUD_UI_URL}/to/developers/integration#configure-sdk`;
|
|
10
|
+
/** The deployment name, in resolution order. */
|
|
11
|
+
const CONNECT_NAME_ENVS = [
|
|
12
|
+
"RESTATE_CONNECT_NAME",
|
|
13
|
+
"RESTATE_INPROC_TUNNEL_NAME",
|
|
14
|
+
"RESTATE_TUNNEL_NAME"
|
|
15
|
+
];
|
|
16
|
+
/** The connect key. */
|
|
17
|
+
const CONNECT_KEY_ENV = "RESTATE_CONNECT_KEY";
|
|
18
|
+
const ENVIRONMENT_ID_ENVS = ["RESTATE_ENVIRONMENT_ID", "RESTATE_INPROC_ENVIRONMENT_ID"];
|
|
19
|
+
const AUTH_TOKEN_ENVS = ["RESTATE_AUTH_TOKEN"];
|
|
20
|
+
/** A file holding the token, re-read on every reconnect (a mounted Secret). */
|
|
21
|
+
const AUTH_TOKEN_FILE_ENVS = ["RESTATE_INPROC_AUTH_TOKEN_FILE"];
|
|
22
|
+
const CLOUD_REGION_ENVS = ["RESTATE_CLOUD_REGION", "RESTATE_INPROC_CLOUD_REGION"];
|
|
23
|
+
const TUNNEL_SERVERS_SRV_ENVS = ["RESTATE_TUNNEL_SERVERS_SRV"];
|
|
24
|
+
const TUNNEL_SERVERS_ENVS = ["RESTATE_TUNNEL_SERVERS"];
|
|
25
|
+
const SIGNING_PUBLIC_KEY_ENVS = ["RESTATE_SIGNING_PUBLIC_KEY", "RESTATE_INPROC_SIGNING_PUBLIC_KEY"];
|
|
26
|
+
/** The query parameters a connect key may carry; anything else is ignored with a warning. */
|
|
27
|
+
const KNOWN_KEY_PARAMS = [
|
|
28
|
+
"signing-key",
|
|
29
|
+
"tunnel-srv",
|
|
30
|
+
"tunnel-servers"
|
|
31
|
+
];
|
|
32
|
+
/** An env var set to the empty string is treated as unset. */
|
|
33
|
+
function readEnv(name) {
|
|
34
|
+
const value = process.env[name];
|
|
35
|
+
return value === void 0 || value === "" ? void 0 : value;
|
|
36
|
+
}
|
|
37
|
+
/** The first set variable among `names`, with its name. */
|
|
38
|
+
function readFirstEnv(names) {
|
|
39
|
+
for (const name of names) {
|
|
40
|
+
const value = readEnv(name);
|
|
41
|
+
if (value !== void 0) return {
|
|
42
|
+
value,
|
|
43
|
+
name
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function given(value) {
|
|
48
|
+
return value !== void 0 && value !== "";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The deployment name: option → `RESTATE_CONNECT_NAME` →
|
|
52
|
+
* `RESTATE_INPROC_TUNNEL_NAME` → `RESTATE_TUNNEL_NAME` → error. No inference:
|
|
53
|
+
* an unnamed deployment is a mistake, and the error says what to set.
|
|
54
|
+
*/
|
|
55
|
+
function resolveName(option) {
|
|
56
|
+
const name = given(option) ? option : readFirstEnv(CONNECT_NAME_ENVS)?.value;
|
|
57
|
+
if (name === void 0) throw new Error(`connect(): the deployment needs a name — pass { name: "greeter-v1" } or set ${CONNECT_NAME_ENVS[0]}. Replicas of one deployment share the name; it is what you register (connect://<name>).`);
|
|
58
|
+
if (!/^[A-Za-z0-9._-]+$/.test(name)) throw new Error(`connect(): invalid deployment name ${JSON.stringify(name)} — use letters, digits, '.', '_' or '-'`);
|
|
59
|
+
return name;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The coordinates for `connect()`: `key` option → `RESTATE_CONNECT_KEY` →
|
|
63
|
+
* the expanded variables → error naming the variable and the Cloud UI page.
|
|
64
|
+
* Explicit `identityKeys` and discovery options win over the key's.
|
|
65
|
+
*/
|
|
66
|
+
function resolveCoordinates(options, warn) {
|
|
67
|
+
if (given(options.key)) return coordinatesFromKey(parseCloudKey(options.key, "the `key` option", CONNECT_KEY_URL), options, warn);
|
|
68
|
+
const fromEnv = readEnv(CONNECT_KEY_ENV);
|
|
69
|
+
if (fromEnv !== void 0) return coordinatesFromKey(parseCloudKey(fromEnv, CONNECT_KEY_ENV, CONNECT_KEY_URL), options, warn);
|
|
70
|
+
if (readFirstEnv(ENVIRONMENT_ID_ENVS) === void 0) throw new Error(`connect(): no Restate Cloud connect key — set ${CONNECT_KEY_ENV} (create one at ${CONNECT_KEY_URL}) or pass { key }`);
|
|
71
|
+
return coordinatesFromExpanded(options);
|
|
72
|
+
}
|
|
73
|
+
function coordinatesFromKey(key, options, warn) {
|
|
74
|
+
const params = /* @__PURE__ */ new Map();
|
|
75
|
+
const unknown = [];
|
|
76
|
+
for (const [name, value] of key.params) if (KNOWN_KEY_PARAMS.includes(name)) params.set(name, value);
|
|
77
|
+
else unknown.push(name);
|
|
78
|
+
if (unknown.length > 0) warn(`connect(): ignoring unknown connect key parameter(s) ${unknown.map((n) => JSON.stringify(n)).join(", ")} — this SDK version knows ${KNOWN_KEY_PARAMS.join(", ")}`);
|
|
79
|
+
const injected = readFirstEnv(ENVIRONMENT_ID_ENVS);
|
|
80
|
+
if (injected !== void 0 && injected.value !== key.environmentId) warn(`connect(): the connect key is for environment ${key.environmentId} but ${injected.name} is set to ${injected.value}; the key wins`);
|
|
81
|
+
const identityKeys = resolveIdentityKeys(options.identityKeys, params.get("signing-key"), `the connect key has no signing-key parameter and no identityKeys were given — copy the key again from ${CONNECT_KEY_URL}`);
|
|
82
|
+
const authToken = key.apiKey !== void 0 ? constantToken(key.apiKey, "the connect key's API key") : tokenFromEnv() ?? fail(`the connect key carries no API key (restate://<api key>@…) and no token is configured in ${AUTH_TOKEN_ENVS[0]} or a file in ${AUTH_TOKEN_FILE_ENVS[0]}`);
|
|
83
|
+
const keyServers = params.get("tunnel-servers");
|
|
84
|
+
if (keyServers !== void 0 && splitList(keyServers).length === 0) fail("the connect key's tunnel-servers parameter is empty");
|
|
85
|
+
const keySrv = params.get("tunnel-srv");
|
|
86
|
+
if (keySrv === "") fail("the connect key's tunnel-srv parameter is empty");
|
|
87
|
+
if (keySrv !== void 0 && keyServers !== void 0) fail("the connect key carries both tunnel-srv and tunnel-servers; it may carry one");
|
|
88
|
+
const keyDiscovery = {
|
|
89
|
+
serversSrv: keySrv,
|
|
90
|
+
servers: keyServers === void 0 ? void 0 : splitList(keyServers)
|
|
91
|
+
};
|
|
92
|
+
const override = pickDiscovery(options);
|
|
93
|
+
const discovery = override ?? pickDiscovery(keyDiscovery) ?? { srvName: srvNameForRegionDomain(key.regionDomain) };
|
|
94
|
+
return {
|
|
95
|
+
environmentId: key.environmentId,
|
|
96
|
+
authToken,
|
|
97
|
+
identityKeys,
|
|
98
|
+
discovery,
|
|
99
|
+
regionLabel: override === void 0 && pickDiscovery(keyDiscovery) === void 0 ? key.regionDomain : discoveryLabel(discovery)
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The coordinates from the expanded variables (`RESTATE_<X>`, then
|
|
104
|
+
* `RESTATE_INPROC_<X>`), each missing piece an error naming its variable.
|
|
105
|
+
* Explicit `identityKeys` and discovery options win over the environment.
|
|
106
|
+
*/
|
|
107
|
+
function coordinatesFromExpanded(o) {
|
|
108
|
+
const environmentId = requireHeaderSafe(readFirstEnv(ENVIRONMENT_ID_ENVS)?.value ?? fail(`environmentId is required — set ${ENVIRONMENT_ID_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`), "environmentId");
|
|
109
|
+
if (!/^env_[A-Za-z0-9_-]+$/.test(environmentId)) throw new Error(`connect(): environmentId must be \`env_\` followed by alphanumerics (e.g. env_201k0yd4...), got ${JSON.stringify(environmentId)}`);
|
|
110
|
+
const authToken = tokenFromEnv() ?? fail(`authToken is required — set ${AUTH_TOKEN_ENVS[0]} or a token file in ${AUTH_TOKEN_FILE_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
111
|
+
const identityKeys = resolveIdentityKeys(o.identityKeys, readFirstEnv(SIGNING_PUBLIC_KEY_ENVS)?.value, `signingPublicKey is required — set ${SIGNING_PUBLIC_KEY_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
112
|
+
let discovery = pickDiscovery(o);
|
|
113
|
+
let regionLabel;
|
|
114
|
+
if (discovery === void 0) {
|
|
115
|
+
const region = readEnv(CLOUD_REGION_ENVS[0]);
|
|
116
|
+
const srv = readFirstEnv(TUNNEL_SERVERS_SRV_ENVS)?.value;
|
|
117
|
+
const servers = readFirstEnv(TUNNEL_SERVERS_ENVS)?.value;
|
|
118
|
+
const inproc = readEnv(CLOUD_REGION_ENVS[1]);
|
|
119
|
+
if (region !== void 0 || srv === void 0 && servers === void 0) {
|
|
120
|
+
const value = region ?? inproc;
|
|
121
|
+
if (value === void 0) throw new Error(`connect(): no Restate Cloud region — set ${CLOUD_REGION_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`);
|
|
122
|
+
const domain = regionDomainForRegion(value);
|
|
123
|
+
discovery = { srvName: srvNameForRegionDomain(domain) };
|
|
124
|
+
regionLabel = domain;
|
|
125
|
+
} else if (srv !== void 0) discovery = { srvName: srv };
|
|
126
|
+
else discovery = { servers: splitList(servers) };
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
environmentId,
|
|
130
|
+
authToken,
|
|
131
|
+
identityKeys,
|
|
132
|
+
discovery,
|
|
133
|
+
regionLabel: regionLabel ?? discoveryLabel(discovery)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function fail(message) {
|
|
137
|
+
throw new Error(`connect(): ${message}`);
|
|
138
|
+
}
|
|
139
|
+
function resolveIdentityKeys(option, signingKey, missing) {
|
|
140
|
+
if (option !== void 0 && option.length > 0) return option;
|
|
141
|
+
if (signingKey === void 0) fail(missing);
|
|
142
|
+
if (!signingKey.startsWith("publickeyv1_")) fail(`the signing key must be a request-identity public key (publickeyv1_...), got ${JSON.stringify(signingKey)}`);
|
|
143
|
+
return [signingKey];
|
|
144
|
+
}
|
|
145
|
+
function constantToken(token, what) {
|
|
146
|
+
requireHeaderSafe(token, what);
|
|
147
|
+
return () => token;
|
|
148
|
+
}
|
|
149
|
+
/** `RESTATE_AUTH_TOKEN`, then the file named by `RESTATE_INPROC_AUTH_TOKEN_FILE`. */
|
|
150
|
+
function tokenFromEnv() {
|
|
151
|
+
const token = readFirstEnv(AUTH_TOKEN_ENVS);
|
|
152
|
+
if (token !== void 0) return constantToken(token.value, token.name);
|
|
153
|
+
const file = readFirstEnv(AUTH_TOKEN_FILE_ENVS);
|
|
154
|
+
if (file === void 0) return void 0;
|
|
155
|
+
const tokenFile = file.value;
|
|
156
|
+
const readToken = () => {
|
|
157
|
+
const stat = fs.statSync(tokenFile);
|
|
158
|
+
if (!stat.isFile()) throw new Error(`connect(): token file ${tokenFile} (${file.name}) is not a regular file`);
|
|
159
|
+
if (stat.size > 64 * 1024) throw new Error(`connect(): token file ${tokenFile} (${file.name}) is implausibly large for a token (${stat.size} bytes)`);
|
|
160
|
+
const token$1 = fs.readFileSync(tokenFile, "utf8").trim();
|
|
161
|
+
if (token$1 === "") throw new Error(`connect(): token file ${tokenFile} (${file.name}) is empty`);
|
|
162
|
+
return requireHeaderSafe(token$1, `token file ${tokenFile}`);
|
|
163
|
+
};
|
|
164
|
+
readToken();
|
|
165
|
+
return readToken;
|
|
166
|
+
}
|
|
167
|
+
function pickDiscovery(o) {
|
|
168
|
+
if (given(o.serversSrv) && o.servers !== void 0) throw new Error("connect(): specify at most one of `serversSrv` and `servers`");
|
|
169
|
+
if (given(o.serversSrv)) return { srvName: o.serversSrv };
|
|
170
|
+
if (o.servers !== void 0) {
|
|
171
|
+
if (o.servers.length === 0) throw new Error("connect(): `servers` must not be empty");
|
|
172
|
+
return { servers: o.servers };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** A region (`us`, `eu`, `<slug>.byoc`) is the labels before `.restate.cloud`. */
|
|
176
|
+
function regionDomainForRegion(region) {
|
|
177
|
+
if (!/^[a-z0-9-]+(\.[a-z0-9-]+)*$/.test(region)) throw new Error(`connect(): invalid region ${JSON.stringify(region)}`);
|
|
178
|
+
return `${region}.restate.cloud`;
|
|
179
|
+
}
|
|
180
|
+
/** Human-readable discovery target for the banner. */
|
|
181
|
+
function discoveryLabel(discovery) {
|
|
182
|
+
if ("srvName" in discovery) return discovery.srvName.replace(/^tunnel\./, "");
|
|
183
|
+
return discovery.servers.join(",");
|
|
184
|
+
}
|
|
185
|
+
function splitList(value) {
|
|
186
|
+
return value.split(",").map((s) => s.trim()).filter((s) => s !== "");
|
|
187
|
+
}
|
|
188
|
+
/** The registration identifier the CLI and the Cloud UI accept. */
|
|
189
|
+
function registrationIdentifier(name) {
|
|
190
|
+
return `connect://${name}`;
|
|
191
|
+
}
|
|
192
|
+
/** The Cloud UI deep link that opens the register dialog pre-filled. */
|
|
193
|
+
function registrationDeepLink(name) {
|
|
194
|
+
return `${CLOUD_UI_URL}/to/overview?register-deployment=${registrationIdentifier(name)}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
//#endregion
|
|
198
|
+
export { CONNECT_KEY_ENV, CONNECT_KEY_URL, CONNECT_NAME_ENVS, registrationDeepLink, registrationIdentifier, resolveCoordinates, resolveName };
|
|
199
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","names":["unknown: string[]","keyDiscovery: DiscoveryOverrides","regionLabel: string | undefined","token"],"sources":["../../../src/endpoint/connect/env.ts"],"sourcesContent":["/*\n * Copyright (c) 2023-2026 - Restate Software, Inc., Restate GmbH\n *\n * This file is part of the Restate SDK for Node.js/TypeScript,\n * which is released under the MIT license.\n *\n * You can find a copy of the license in file LICENSE in the root\n * directory of this repository or package, or at\n * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE\n */\n\n// Resolving the identity of a connected deployment.\n// =============================================================================\n//\n// Two inputs, read from the options and then the process environment (never a\n// file; the SDK reads `process.env` only):\n//\n// name option → RESTATE_CONNECT_NAME → RESTATE_INPROC_TUNNEL_NAME\n// → RESTATE_TUNNEL_NAME → error\n// key option → RESTATE_CONNECT_KEY → the expanded variables → error\n//\n// The key is one URL-shaped string from the Cloud UI (Development → Configure\n// SDK), see `parseCloudKey` in restate-sdk-core. The expanded variables are\n// the older spelling of the same coordinates, kept for the restate-operator\n// (`RESTATE_INPROC_*`) and the standalone tunnel container (`RESTATE_*`):\n// environment id, token or token file, region or SRV / servers, signing key.\n\nimport * as fs from \"node:fs\";\nimport { parseCloudKey, type CloudKey } from \"./key.js\";\nimport {\n requireHeaderSafe,\n srvNameForRegionDomain,\n} from \"../tunnel/options.js\";\nimport type { EngineOptions } from \"../tunnel/types.js\";\n\n/** The Cloud UI, for the deep links below. */\nexport const CLOUD_UI_URL = \"https://cloud.restate.dev\";\n/** Where to get a connect key (Development → Configure SDK), linked in every error. */\nexport const CONNECT_KEY_URL = `${CLOUD_UI_URL}/to/developers/integration#configure-sdk`;\n\n/** The deployment name, in resolution order. */\nexport const CONNECT_NAME_ENVS = [\n \"RESTATE_CONNECT_NAME\",\n \"RESTATE_INPROC_TUNNEL_NAME\",\n \"RESTATE_TUNNEL_NAME\",\n] as const;\n/** The connect key. */\nexport const CONNECT_KEY_ENV = \"RESTATE_CONNECT_KEY\";\n\n// The expanded variables, each in resolution order.\nexport const ENVIRONMENT_ID_ENVS = [\n \"RESTATE_ENVIRONMENT_ID\",\n \"RESTATE_INPROC_ENVIRONMENT_ID\",\n] as const;\nexport const AUTH_TOKEN_ENVS = [\"RESTATE_AUTH_TOKEN\"] as const;\n/** A file holding the token, re-read on every reconnect (a mounted Secret). */\nexport const AUTH_TOKEN_FILE_ENVS = [\"RESTATE_INPROC_AUTH_TOKEN_FILE\"] as const;\nexport const CLOUD_REGION_ENVS = [\n \"RESTATE_CLOUD_REGION\",\n \"RESTATE_INPROC_CLOUD_REGION\",\n] as const;\nexport const TUNNEL_SERVERS_SRV_ENVS = [\"RESTATE_TUNNEL_SERVERS_SRV\"] as const;\nexport const TUNNEL_SERVERS_ENVS = [\"RESTATE_TUNNEL_SERVERS\"] as const;\nexport const SIGNING_PUBLIC_KEY_ENVS = [\n \"RESTATE_SIGNING_PUBLIC_KEY\",\n \"RESTATE_INPROC_SIGNING_PUBLIC_KEY\",\n] as const;\n\n/** The query parameters a connect key may carry; anything else is ignored with a warning. */\nconst KNOWN_KEY_PARAMS = [\"signing-key\", \"tunnel-srv\", \"tunnel-servers\"];\n\n/** An env var set to the empty string is treated as unset. */\nfunction readEnv(name: string): string | undefined {\n const value = process.env[name];\n return value === undefined || value === \"\" ? undefined : value;\n}\n\n/** The first set variable among `names`, with its name. */\nfunction readFirstEnv(\n names: readonly string[]\n): { value: string; name: string } | undefined {\n for (const name of names) {\n const value = readEnv(name);\n if (value !== undefined) return { value, name };\n }\n return undefined;\n}\n\nfunction given(value: string | undefined): value is string {\n return value !== undefined && value !== \"\";\n}\n\n/**\n * The deployment name: option → `RESTATE_CONNECT_NAME` →\n * `RESTATE_INPROC_TUNNEL_NAME` → `RESTATE_TUNNEL_NAME` → error. No inference:\n * an unnamed deployment is a mistake, and the error says what to set.\n */\nexport function resolveName(option: string | undefined): string {\n const name = given(option) ? option : readFirstEnv(CONNECT_NAME_ENVS)?.value;\n if (name === undefined) {\n throw new Error(\n `connect(): the deployment needs a name — pass { name: \"greeter-v1\" } or set ${CONNECT_NAME_ENVS[0]}. Replicas of one deployment share the name; it is what you register (connect://<name>).`\n );\n }\n if (!/^[A-Za-z0-9._-]+$/.test(name)) {\n throw new Error(\n `connect(): invalid deployment name ${JSON.stringify(name)} — use letters, digits, '.', '_' or '-'`\n );\n }\n return name;\n}\n\n/** The resolved coordinates: everything but the name and the tuning. */\nexport type Coordinates = Pick<\n EngineOptions,\n \"environmentId\" | \"authToken\" | \"identityKeys\" | \"discovery\"\n> & {\n /** For the banner: the region domain, or what stands in for it. */\n regionLabel: string;\n};\n\n/** Discovery overrides from the options (at most one). */\nexport interface DiscoveryOverrides {\n serversSrv?: string;\n servers?: string[];\n}\n\n/**\n * The coordinates for `connect()`: `key` option → `RESTATE_CONNECT_KEY` →\n * the expanded variables → error naming the variable and the Cloud UI page.\n * Explicit `identityKeys` and discovery options win over the key's.\n */\nexport function resolveCoordinates(\n options: {\n key?: string;\n identityKeys?: string[];\n } & DiscoveryOverrides,\n warn: (message: string) => void\n): Coordinates {\n if (given(options.key)) {\n return coordinatesFromKey(\n parseCloudKey(options.key, \"the `key` option\", CONNECT_KEY_URL),\n options,\n warn\n );\n }\n const fromEnv = readEnv(CONNECT_KEY_ENV);\n if (fromEnv !== undefined) {\n return coordinatesFromKey(\n parseCloudKey(fromEnv, CONNECT_KEY_ENV, CONNECT_KEY_URL),\n options,\n warn\n );\n }\n if (readFirstEnv(ENVIRONMENT_ID_ENVS) === undefined) {\n throw new Error(\n `connect(): no Restate Cloud connect key — set ${CONNECT_KEY_ENV} (create one at ${CONNECT_KEY_URL}) or pass { key }`\n );\n }\n return coordinatesFromExpanded(options);\n}\n\nfunction coordinatesFromKey(\n key: CloudKey,\n options: { identityKeys?: string[] } & DiscoveryOverrides,\n warn: (message: string) => void\n): Coordinates {\n const params = new Map<string, string>();\n const unknown: string[] = [];\n for (const [name, value] of key.params) {\n if (KNOWN_KEY_PARAMS.includes(name)) params.set(name, value);\n else unknown.push(name);\n }\n if (unknown.length > 0) {\n warn(\n `connect(): ignoring unknown connect key parameter(s) ${unknown.map((n) => JSON.stringify(n)).join(\", \")} — this SDK version knows ${KNOWN_KEY_PARAMS.join(\", \")}`\n );\n }\n\n // A key for one environment next to an operator's variables for another\n // is almost certainly a stray developer key: say so.\n const injected = readFirstEnv(ENVIRONMENT_ID_ENVS);\n if (injected !== undefined && injected.value !== key.environmentId) {\n warn(\n `connect(): the connect key is for environment ${key.environmentId} but ${injected.name} is set to ${injected.value}; the key wins`\n );\n }\n\n const identityKeys = resolveIdentityKeys(\n options.identityKeys,\n params.get(\"signing-key\"),\n `the connect key has no signing-key parameter and no identityKeys were given — copy the key again from ${CONNECT_KEY_URL}`\n );\n\n const authToken =\n key.apiKey !== undefined\n ? constantToken(key.apiKey, \"the connect key's API key\")\n : (tokenFromEnv() ??\n fail(\n `the connect key carries no API key (restate://<api key>@…) and no token is configured in ${AUTH_TOKEN_ENVS[0]} or a file in ${AUTH_TOKEN_FILE_ENVS[0]}`\n ));\n\n const keyServers = params.get(\"tunnel-servers\");\n if (keyServers !== undefined && splitList(keyServers).length === 0) {\n fail(\"the connect key's tunnel-servers parameter is empty\");\n }\n const keySrv = params.get(\"tunnel-srv\");\n if (keySrv === \"\") fail(\"the connect key's tunnel-srv parameter is empty\");\n if (keySrv !== undefined && keyServers !== undefined) {\n fail(\n \"the connect key carries both tunnel-srv and tunnel-servers; it may carry one\"\n );\n }\n const keyDiscovery: DiscoveryOverrides = {\n serversSrv: keySrv,\n servers: keyServers === undefined ? undefined : splitList(keyServers),\n };\n const override = pickDiscovery(options);\n const discovery =\n override ??\n pickDiscovery(keyDiscovery) ??\n ({ srvName: srvNameForRegionDomain(key.regionDomain) } as const);\n\n return {\n environmentId: key.environmentId,\n authToken,\n identityKeys,\n discovery,\n regionLabel:\n override === undefined && pickDiscovery(keyDiscovery) === undefined\n ? key.regionDomain\n : discoveryLabel(discovery),\n };\n}\n\n/**\n * The coordinates from the expanded variables (`RESTATE_<X>`, then\n * `RESTATE_INPROC_<X>`), each missing piece an error naming its variable.\n * Explicit `identityKeys` and discovery options win over the environment.\n */\nexport function coordinatesFromExpanded(\n o: DiscoveryOverrides & { identityKeys?: string[] }\n): Coordinates {\n const environmentId = requireHeaderSafe(\n readFirstEnv(ENVIRONMENT_ID_ENVS)?.value ??\n fail(\n `environmentId is required — set ${ENVIRONMENT_ID_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`\n ),\n \"environmentId\"\n );\n if (!/^env_[A-Za-z0-9_-]+$/.test(environmentId)) {\n throw new Error(\n `connect(): environmentId must be \\`env_\\` followed by alphanumerics (e.g. env_201k0yd4...), got ${JSON.stringify(environmentId)}`\n );\n }\n\n const authToken =\n tokenFromEnv() ??\n fail(\n `authToken is required — set ${AUTH_TOKEN_ENVS[0]} or a token file in ${AUTH_TOKEN_FILE_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`\n );\n\n const identityKeys = resolveIdentityKeys(\n o.identityKeys,\n readFirstEnv(SIGNING_PUBLIC_KEY_ENVS)?.value,\n `signingPublicKey is required — set ${SIGNING_PUBLIC_KEY_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`\n );\n\n // Discovery: the options, else the environment: RESTATE_CLOUD_REGION →\n // RESTATE_TUNNEL_SERVERS_SRV → RESTATE_TUNNEL_SERVERS →\n // RESTATE_INPROC_CLOUD_REGION.\n let discovery = pickDiscovery(o);\n let regionLabel: string | undefined;\n if (discovery === undefined) {\n const region = readEnv(CLOUD_REGION_ENVS[0]);\n const srv = readFirstEnv(TUNNEL_SERVERS_SRV_ENVS)?.value;\n const servers = readFirstEnv(TUNNEL_SERVERS_ENVS)?.value;\n const inproc = readEnv(CLOUD_REGION_ENVS[1]);\n if (region !== undefined || (srv === undefined && servers === undefined)) {\n const value = region ?? inproc;\n if (value === undefined) {\n throw new Error(\n `connect(): no Restate Cloud region — set ${CLOUD_REGION_ENVS[0]} (or ${CONNECT_KEY_ENV} from ${CONNECT_KEY_URL})`\n );\n }\n const domain = regionDomainForRegion(value);\n discovery = { srvName: srvNameForRegionDomain(domain) };\n regionLabel = domain;\n } else if (srv !== undefined) {\n discovery = { srvName: srv };\n } else {\n discovery = { servers: splitList(servers!) };\n }\n }\n\n return {\n environmentId,\n authToken,\n identityKeys,\n discovery,\n regionLabel: regionLabel ?? discoveryLabel(discovery),\n };\n}\n\nfunction fail(message: string): never {\n throw new Error(`connect(): ${message}`);\n}\n\nfunction resolveIdentityKeys(\n option: string[] | undefined,\n signingKey: string | undefined,\n missing: string\n): string[] {\n if (option !== undefined && option.length > 0) return option;\n if (signingKey === undefined) fail(missing);\n if (!signingKey.startsWith(\"publickeyv1_\")) {\n fail(\n `the signing key must be a request-identity public key (publickeyv1_...), got ${JSON.stringify(signingKey)}`\n );\n }\n return [signingKey];\n}\n\nfunction constantToken(token: string, what: string): () => string {\n requireHeaderSafe(token, what);\n return () => token;\n}\n\n/** `RESTATE_AUTH_TOKEN`, then the file named by `RESTATE_INPROC_AUTH_TOKEN_FILE`. */\nfunction tokenFromEnv(): (() => string) | undefined {\n const token = readFirstEnv(AUTH_TOKEN_ENVS);\n if (token !== undefined) return constantToken(token.value, token.name);\n const file = readFirstEnv(AUTH_TOKEN_FILE_ENVS);\n if (file === undefined) return undefined;\n const tokenFile = file.value;\n const readToken = () => {\n // Guard before reading: this runs synchronously on the redial path, so a\n // FIFO (blocks forever) or an unbounded device file (reads forever) would\n // freeze the event loop — and with it every other live connection.\n const stat = fs.statSync(tokenFile);\n if (!stat.isFile()) {\n throw new Error(\n `connect(): token file ${tokenFile} (${file.name}) is not a regular file`\n );\n }\n if (stat.size > 64 * 1024) {\n throw new Error(\n `connect(): token file ${tokenFile} (${file.name}) is implausibly large for a token (${stat.size} bytes)`\n );\n }\n // Trimmed because mounted secrets routinely carry a trailing newline.\n const token = fs.readFileSync(tokenFile, \"utf8\").trim();\n if (token === \"\") {\n throw new Error(\n `connect(): token file ${tokenFile} (${file.name}) is empty`\n );\n }\n return requireHeaderSafe(token, `token file ${tokenFile}`);\n };\n // A bad path or token must throw at configuration time like every other\n // misconfiguration, not look like a transient failure mid-redial.\n readToken();\n return readToken;\n}\n\nfunction pickDiscovery(\n o: DiscoveryOverrides\n): EngineOptions[\"discovery\"] | undefined {\n if (given(o.serversSrv) && o.servers !== undefined) {\n throw new Error(\n \"connect(): specify at most one of `serversSrv` and `servers`\"\n );\n }\n if (given(o.serversSrv)) return { srvName: o.serversSrv };\n if (o.servers !== undefined) {\n if (o.servers.length === 0) {\n throw new Error(\"connect(): `servers` must not be empty\");\n }\n return { servers: o.servers };\n }\n return undefined;\n}\n\n/** A region (`us`, `eu`, `<slug>.byoc`) is the labels before `.restate.cloud`. */\nfunction regionDomainForRegion(region: string): string {\n // Each label lowercase [a-z0-9-], dot-separated, no empty labels.\n if (!/^[a-z0-9-]+(\\.[a-z0-9-]+)*$/.test(region)) {\n throw new Error(`connect(): invalid region ${JSON.stringify(region)}`);\n }\n return `${region}.restate.cloud`;\n}\n\n/** Human-readable discovery target for the banner. */\nfunction discoveryLabel(discovery: EngineOptions[\"discovery\"]): string {\n if (\"srvName\" in discovery) {\n return discovery.srvName.replace(/^tunnel\\./, \"\");\n }\n return discovery.servers.join(\",\");\n}\n\nfunction splitList(value: string): string[] {\n return value\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\");\n}\n\n/** The registration identifier the CLI and the Cloud UI accept. */\nexport function registrationIdentifier(name: string): string {\n return `connect://${name}`;\n}\n\n/** The Cloud UI deep link that opens the register dialog pre-filled. */\nexport function registrationDeepLink(name: string): string {\n return `${CLOUD_UI_URL}/to/overview?register-deployment=${registrationIdentifier(name)}`;\n}\n"],"mappings":";;;;;;AAoCA,MAAa,eAAe;;AAE5B,MAAa,kBAAkB,GAAG,aAAa;;AAG/C,MAAa,oBAAoB;CAC/B;CACA;CACA;CACD;;AAED,MAAa,kBAAkB;AAG/B,MAAa,sBAAsB,CACjC,0BACA,gCACD;AACD,MAAa,kBAAkB,CAAC,qBAAqB;;AAErD,MAAa,uBAAuB,CAAC,iCAAiC;AACtE,MAAa,oBAAoB,CAC/B,wBACA,8BACD;AACD,MAAa,0BAA0B,CAAC,6BAA6B;AACrE,MAAa,sBAAsB,CAAC,yBAAyB;AAC7D,MAAa,0BAA0B,CACrC,8BACA,oCACD;;AAGD,MAAM,mBAAmB;CAAC;CAAe;CAAc;CAAiB;;AAGxE,SAAS,QAAQ,MAAkC;CACjD,MAAM,QAAQ,QAA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KAAK,IAAI;;AAGpC,SAAS,UAAU,OAAyB;AAC1C,QAAO,MACJ,MAAM,IAAI,CACV,KAAK,MAAM,EAAE,MAAM,CAAC,CACpB,QAAQ,MAAM,MAAM,GAAG;;;AAI5B,SAAgB,uBAAuB,MAAsB;AAC3D,QAAO,aAAa;;;AAItB,SAAgB,qBAAqB,MAAsB;AACzD,QAAO,GAAG,aAAa,mCAAmC,uBAAuB,KAAK"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
const require_logger_transport = require('../../logging/logger_transport.cjs');
|
|
2
|
+
const require_console_logger_transport = require('../../logging/console_logger_transport.cjs');
|
|
3
|
+
const require_logger = require('../../logging/logger.cjs');
|
|
4
|
+
const require_engine = require('../tunnel/engine.cjs');
|
|
5
|
+
const require_env = require('./env.cjs');
|
|
6
|
+
|
|
7
|
+
//#region src/endpoint/connect/index.ts
|
|
8
|
+
/**
|
|
9
|
+
* Connect your services to Restate Cloud and serve them over the connection.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import * as restate from "@restatedev/restate-sdk";
|
|
13
|
+
*
|
|
14
|
+
* restate.connect({ services: [greeter], name: "greeter-v1" });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* The connection needs two things:
|
|
18
|
+
*
|
|
19
|
+
* - a **name** for the deployment (`name` option, or `RESTATE_CONNECT_NAME`):
|
|
20
|
+
* the identity you register, shared by replicas of the same deployment;
|
|
21
|
+
* - a **connect key** (`RESTATE_CONNECT_KEY`): one string you can obtain from
|
|
22
|
+
* Cloud UI -> Development → Configure SDK.
|
|
23
|
+
*
|
|
24
|
+
* Both are read from the process environment; the SDK never reads a file.
|
|
25
|
+
*
|
|
26
|
+
* Once connected, the SDK logs how to register the deployment, once:
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* Connecting to Restate Cloud environment env_201k… (eu.restate.cloud)
|
|
30
|
+
* Connected as "greeter-v1"
|
|
31
|
+
* Register it once:
|
|
32
|
+
* restate deployments register connect://greeter-v1
|
|
33
|
+
* or open https://cloud.restate.dev/to/overview?register-deployment=connect://greeter-v1
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* The returned {@link Connection} reconnects in the background until closed.
|
|
37
|
+
*
|
|
38
|
+
* @param options the services, the name and the tuning, see {@link ConnectOptions}
|
|
39
|
+
* @returns the connection handle
|
|
40
|
+
*/
|
|
41
|
+
function connect(options) {
|
|
42
|
+
const handle = connectEngine(options);
|
|
43
|
+
const settled = async (done) => {
|
|
44
|
+
await done;
|
|
45
|
+
if (handle.error !== void 0) throw handle.error;
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
ready: handle.ready,
|
|
49
|
+
name: handle.name,
|
|
50
|
+
close: () => settled(handle.close()),
|
|
51
|
+
shutdown: (opts) => settled(handle.shutdown(opts))
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* `connect()` without the projection: the engine handle, with the connection
|
|
56
|
+
* count and the fatal error. Internal (tests).
|
|
57
|
+
*/
|
|
58
|
+
function connectEngine(options) {
|
|
59
|
+
const { name: nameOption, key, identityKeys, connection = {},...endpointOptions } = options;
|
|
60
|
+
const { serversSrv, servers,...tuning } = connection;
|
|
61
|
+
const rlog = require_logger.createLogger(options.logger ?? require_console_logger_transport.defaultLoggerTransport, require_logger_transport.LogSource.SYSTEM);
|
|
62
|
+
const name = require_env.resolveName(nameOption);
|
|
63
|
+
const coordinates = require_env.resolveCoordinates({
|
|
64
|
+
key,
|
|
65
|
+
identityKeys,
|
|
66
|
+
serversSrv,
|
|
67
|
+
servers
|
|
68
|
+
}, (message) => rlog.warn(message));
|
|
69
|
+
return start({
|
|
70
|
+
...endpointOptions,
|
|
71
|
+
...tuning
|
|
72
|
+
}, name, coordinates, rlog);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Start the engine with the resolved identity and log the banner on ready.
|
|
76
|
+
*/
|
|
77
|
+
function start(rest, name, coordinates, rlog) {
|
|
78
|
+
const { regionLabel,...engineCoordinates } = coordinates;
|
|
79
|
+
const connection = require_engine.startEngine({
|
|
80
|
+
...rest,
|
|
81
|
+
name,
|
|
82
|
+
...engineCoordinates,
|
|
83
|
+
onFatal: (error) => {
|
|
84
|
+
for (const line of explainFatal(error, name, coordinates.environmentId)) rlog.error(line);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
rlog.info(`Connecting to Restate Cloud environment ${coordinates.environmentId} (${regionLabel})`);
|
|
88
|
+
connection.ready.then(() => {
|
|
89
|
+
rlog.debug(`Deployment URL: ${connection.deploymentUrl ?? "?"}`);
|
|
90
|
+
rlog.info(`Connected as "${name}"`);
|
|
91
|
+
rlog.info("Register it once:");
|
|
92
|
+
rlog.info(` restate deployments register ${require_env.registrationIdentifier(name)}`);
|
|
93
|
+
rlog.info(` or open ${require_env.registrationDeepLink(name)}`);
|
|
94
|
+
}, () => {});
|
|
95
|
+
return connection;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A non-retryable stop, in plain words: what Restate Cloud said, what it
|
|
99
|
+
* means, what to do. The engine's own `tunnel: FATAL — …` line stays at debug.
|
|
100
|
+
*/
|
|
101
|
+
function explainFatal(error, name, environmentId) {
|
|
102
|
+
const reason = error.message.replace(/^tunnel: /, "");
|
|
103
|
+
const lines = [];
|
|
104
|
+
if (reason.includes("tunnel-status: unauthorized")) lines.push(`Restate Cloud rejected the connect key for environment ${environmentId}: it is invalid, revoked, or for another environment.`, `Create a new key at ${require_env.CONNECT_KEY_URL} and set ${require_env.CONNECT_KEY_ENV}.`);
|
|
105
|
+
else if (reason.includes("tunnel-status: bad-tunnel-name") || reason.startsWith("tunnel-name mismatch")) lines.push(`Restate Cloud rejected the deployment name "${name}" (${reason}).`, `Use letters, digits, '.', '_' or '-' — ${require_env.CONNECT_NAME_ENVS[0]} or the name option.`);
|
|
106
|
+
else if (reason.startsWith("gave up reconnecting")) lines.push(`Could not reach Restate Cloud for environment ${environmentId}: ${reason}.`, `Check the network, and that the key's region is right (${require_env.CONNECT_KEY_ENV}).`);
|
|
107
|
+
else lines.push(`The connection to Restate Cloud stopped: ${reason}`);
|
|
108
|
+
lines.push("Not retrying. Fix the configuration and restart.");
|
|
109
|
+
return lines;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
exports.connect = connect;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ConnectOptions, Connection } from "../tunnel/types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/endpoint/connect/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Connect your services to Restate Cloud and serve them over the connection.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import * as restate from "@restatedev/restate-sdk";
|
|
10
|
+
*
|
|
11
|
+
* restate.connect({ services: [greeter], name: "greeter-v1" });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The connection needs two things:
|
|
15
|
+
*
|
|
16
|
+
* - a **name** for the deployment (`name` option, or `RESTATE_CONNECT_NAME`):
|
|
17
|
+
* the identity you register, shared by replicas of the same deployment;
|
|
18
|
+
* - a **connect key** (`RESTATE_CONNECT_KEY`): one string you can obtain from
|
|
19
|
+
* Cloud UI -> Development → Configure SDK.
|
|
20
|
+
*
|
|
21
|
+
* Both are read from the process environment; the SDK never reads a file.
|
|
22
|
+
*
|
|
23
|
+
* Once connected, the SDK logs how to register the deployment, once:
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
* Connecting to Restate Cloud environment env_201k… (eu.restate.cloud)
|
|
27
|
+
* Connected as "greeter-v1"
|
|
28
|
+
* Register it once:
|
|
29
|
+
* restate deployments register connect://greeter-v1
|
|
30
|
+
* or open https://cloud.restate.dev/to/overview?register-deployment=connect://greeter-v1
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The returned {@link Connection} reconnects in the background until closed.
|
|
34
|
+
*
|
|
35
|
+
* @param options the services, the name and the tuning, see {@link ConnectOptions}
|
|
36
|
+
* @returns the connection handle
|
|
37
|
+
*/
|
|
38
|
+
declare function connect(options: ConnectOptions): Connection;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { connect };
|
|
41
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../../src/endpoint/connect/index.ts"],"sourcesContent":[],"mappings":";;;;;;AAiEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,OAAA,UAAiB,iBAAiB"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ConnectOptions, Connection } from "../tunnel/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/endpoint/connect/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Connect your services to Restate Cloud and serve them over the connection.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import * as restate from "@restatedev/restate-sdk";
|
|
10
|
+
*
|
|
11
|
+
* restate.connect({ services: [greeter], name: "greeter-v1" });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The connection needs two things:
|
|
15
|
+
*
|
|
16
|
+
* - a **name** for the deployment (`name` option, or `RESTATE_CONNECT_NAME`):
|
|
17
|
+
* the identity you register, shared by replicas of the same deployment;
|
|
18
|
+
* - a **connect key** (`RESTATE_CONNECT_KEY`): one string you can obtain from
|
|
19
|
+
* Cloud UI -> Development → Configure SDK.
|
|
20
|
+
*
|
|
21
|
+
* Both are read from the process environment; the SDK never reads a file.
|
|
22
|
+
*
|
|
23
|
+
* Once connected, the SDK logs how to register the deployment, once:
|
|
24
|
+
*
|
|
25
|
+
* ```
|
|
26
|
+
* Connecting to Restate Cloud environment env_201k… (eu.restate.cloud)
|
|
27
|
+
* Connected as "greeter-v1"
|
|
28
|
+
* Register it once:
|
|
29
|
+
* restate deployments register connect://greeter-v1
|
|
30
|
+
* or open https://cloud.restate.dev/to/overview?register-deployment=connect://greeter-v1
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* The returned {@link Connection} reconnects in the background until closed.
|
|
34
|
+
*
|
|
35
|
+
* @param options the services, the name and the tuning, see {@link ConnectOptions}
|
|
36
|
+
* @returns the connection handle
|
|
37
|
+
*/
|
|
38
|
+
declare function connect(options: ConnectOptions): Connection;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { connect };
|
|
41
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../../src/endpoint/connect/index.ts"],"sourcesContent":[],"mappings":";;;;;;AAiEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,OAAA,UAAiB,iBAAiB"}
|