modal 0.5.0 → 0.5.2
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/README.md +29 -20
- package/dist/index.cjs +378 -60
- package/dist/index.d.cts +27 -2
- package/dist/index.d.ts +27 -2
- package/dist/index.js +378 -60
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -43702,6 +43702,15 @@ var FunctionService = class {
|
|
|
43702
43702
|
objectTag: name,
|
|
43703
43703
|
environmentName: this.#client.environmentName(params.environment)
|
|
43704
43704
|
});
|
|
43705
|
+
this.#client.logger.debug(
|
|
43706
|
+
"Retrieved Function",
|
|
43707
|
+
"function_id",
|
|
43708
|
+
resp.functionId,
|
|
43709
|
+
"app_name",
|
|
43710
|
+
appName,
|
|
43711
|
+
"function_name",
|
|
43712
|
+
name
|
|
43713
|
+
);
|
|
43705
43714
|
return new Function_(
|
|
43706
43715
|
this.#client,
|
|
43707
43716
|
resp.functionId,
|
|
@@ -43733,16 +43742,42 @@ var Function_ = class {
|
|
|
43733
43742
|
static async lookup(appName, name, params = {}) {
|
|
43734
43743
|
return await getDefaultClient().functions.fromName(appName, name, params);
|
|
43735
43744
|
}
|
|
43745
|
+
#checkNoWebUrl(fnName) {
|
|
43746
|
+
if (this.#handleMetadata?.webUrl) {
|
|
43747
|
+
throw new InvalidError(
|
|
43748
|
+
`A webhook Function cannot be invoked for remote execution with '.${fnName}'. Invoke this Function via its web url '${this.#handleMetadata.webUrl}' instead.`
|
|
43749
|
+
);
|
|
43750
|
+
}
|
|
43751
|
+
}
|
|
43736
43752
|
// Execute a single input into a remote Function.
|
|
43737
43753
|
async remote(args = [], kwargs = {}) {
|
|
43754
|
+
this.#client.logger.debug(
|
|
43755
|
+
"Executing function call",
|
|
43756
|
+
"function_id",
|
|
43757
|
+
this.functionId
|
|
43758
|
+
);
|
|
43759
|
+
this.#checkNoWebUrl("remote");
|
|
43738
43760
|
const input = await this.#createInput(args, kwargs);
|
|
43739
43761
|
const invocation = await this.#createRemoteInvocation(input);
|
|
43740
43762
|
let retryCount = 0;
|
|
43741
43763
|
while (true) {
|
|
43742
43764
|
try {
|
|
43743
|
-
|
|
43765
|
+
const result = await invocation.awaitOutput();
|
|
43766
|
+
this.#client.logger.debug(
|
|
43767
|
+
"Function call completed",
|
|
43768
|
+
"function_id",
|
|
43769
|
+
this.functionId
|
|
43770
|
+
);
|
|
43771
|
+
return result;
|
|
43744
43772
|
} catch (err) {
|
|
43745
43773
|
if (err instanceof InternalFailure && retryCount <= maxSystemRetries) {
|
|
43774
|
+
this.#client.logger.debug(
|
|
43775
|
+
"Retrying function call due to internal failure",
|
|
43776
|
+
"function_id",
|
|
43777
|
+
this.functionId,
|
|
43778
|
+
"retry_count",
|
|
43779
|
+
retryCount
|
|
43780
|
+
);
|
|
43746
43781
|
await invocation.retry(retryCount);
|
|
43747
43782
|
retryCount++;
|
|
43748
43783
|
} else {
|
|
@@ -43769,6 +43804,12 @@ var Function_ = class {
|
|
|
43769
43804
|
}
|
|
43770
43805
|
// Spawn a single input into a remote Function.
|
|
43771
43806
|
async spawn(args = [], kwargs = {}) {
|
|
43807
|
+
this.#client.logger.debug(
|
|
43808
|
+
"Spawning function call",
|
|
43809
|
+
"function_id",
|
|
43810
|
+
this.functionId
|
|
43811
|
+
);
|
|
43812
|
+
this.#checkNoWebUrl("spawn");
|
|
43772
43813
|
const input = await this.#createInput(args, kwargs);
|
|
43773
43814
|
const invocation = await ControlPlaneInvocation.create(
|
|
43774
43815
|
this.#client,
|
|
@@ -43776,6 +43817,13 @@ var Function_ = class {
|
|
|
43776
43817
|
input,
|
|
43777
43818
|
3 /* FUNCTION_CALL_INVOCATION_TYPE_ASYNC */
|
|
43778
43819
|
);
|
|
43820
|
+
this.#client.logger.debug(
|
|
43821
|
+
"Function call spawned",
|
|
43822
|
+
"function_id",
|
|
43823
|
+
this.functionId,
|
|
43824
|
+
"function_call_id",
|
|
43825
|
+
invocation.functionCallId
|
|
43826
|
+
);
|
|
43779
43827
|
return new FunctionCall(this.#client, invocation.functionCallId);
|
|
43780
43828
|
}
|
|
43781
43829
|
// Returns statistics about the Function.
|
|
@@ -43815,7 +43863,7 @@ var Function_ = class {
|
|
|
43815
43863
|
const supported_input_formats = this.#handleMetadata?.supportedInputFormats?.length ? this.#handleMetadata.supportedInputFormats : [1 /* DATA_FORMAT_PICKLE */];
|
|
43816
43864
|
if (!supported_input_formats.includes(4 /* DATA_FORMAT_CBOR */)) {
|
|
43817
43865
|
throw new InvalidError(
|
|
43818
|
-
"
|
|
43866
|
+
"cannot call Modal Function from JS SDK since it was deployed with an incompatible Python SDK version. Redeploy with Modal Python SDK >= 1.2"
|
|
43819
43867
|
);
|
|
43820
43868
|
}
|
|
43821
43869
|
const payload = cborEncode([args, kwargs]);
|
|
@@ -43878,6 +43926,13 @@ var SecretService = class {
|
|
|
43878
43926
|
environmentName: this.#client.environmentName(params?.environment),
|
|
43879
43927
|
requiredKeys: params?.requiredKeys ?? []
|
|
43880
43928
|
});
|
|
43929
|
+
this.#client.logger.debug(
|
|
43930
|
+
"Retrieved Secret",
|
|
43931
|
+
"secret_id",
|
|
43932
|
+
resp.secretId,
|
|
43933
|
+
"secret_name",
|
|
43934
|
+
name
|
|
43935
|
+
);
|
|
43881
43936
|
return new Secret(resp.secretId, name);
|
|
43882
43937
|
} catch (err) {
|
|
43883
43938
|
if (err instanceof import_nice_grpc2.ClientError && err.code === import_nice_grpc2.Status.NOT_FOUND)
|
|
@@ -43902,6 +43957,11 @@ var SecretService = class {
|
|
|
43902
43957
|
envDict: entries,
|
|
43903
43958
|
environmentName: this.#client.environmentName(params?.environment)
|
|
43904
43959
|
});
|
|
43960
|
+
this.#client.logger.debug(
|
|
43961
|
+
"Created ephemeral Secret",
|
|
43962
|
+
"secret_id",
|
|
43963
|
+
resp.secretId
|
|
43964
|
+
);
|
|
43905
43965
|
return new Secret(resp.secretId);
|
|
43906
43966
|
} catch (err) {
|
|
43907
43967
|
if (err instanceof import_nice_grpc2.ClientError && (err.code === import_nice_grpc2.Status.INVALID_ARGUMENT || err.code === import_nice_grpc2.Status.FAILED_PRECONDITION))
|
|
@@ -44022,6 +44082,15 @@ var ClsService = class {
|
|
|
44022
44082
|
`Unsupported parameter format: ${parameterInfo?.format}`
|
|
44023
44083
|
);
|
|
44024
44084
|
}
|
|
44085
|
+
this.#client.logger.debug(
|
|
44086
|
+
"Retrieved Cls",
|
|
44087
|
+
"function_id",
|
|
44088
|
+
serviceFunction.functionId,
|
|
44089
|
+
"app_name",
|
|
44090
|
+
appName,
|
|
44091
|
+
"cls_name",
|
|
44092
|
+
name
|
|
44093
|
+
);
|
|
44025
44094
|
return new Cls(
|
|
44026
44095
|
this.#client,
|
|
44027
44096
|
serviceFunction.functionId,
|
|
@@ -44400,8 +44469,15 @@ var ImageService = class {
|
|
|
44400
44469
|
* Delete an {@link Image} by ID. Warning: This removes an *entire Image*, and cannot be undone.
|
|
44401
44470
|
*/
|
|
44402
44471
|
async delete(imageId, _ = {}) {
|
|
44403
|
-
|
|
44404
|
-
|
|
44472
|
+
try {
|
|
44473
|
+
await this.#client.cpClient.imageDelete({ imageId });
|
|
44474
|
+
} catch (err) {
|
|
44475
|
+
if (err instanceof import_nice_grpc4.ClientError && err.code === import_nice_grpc5.Status.NOT_FOUND)
|
|
44476
|
+
throw new NotFoundError(err.details);
|
|
44477
|
+
if (err instanceof import_nice_grpc4.ClientError && err.code === import_nice_grpc5.Status.FAILED_PRECONDITION && err.details.includes("Could not find image with ID"))
|
|
44478
|
+
throw new NotFoundError(err.details);
|
|
44479
|
+
throw err;
|
|
44480
|
+
}
|
|
44405
44481
|
}
|
|
44406
44482
|
};
|
|
44407
44483
|
var Image2 = class _Image {
|
|
@@ -44499,6 +44575,7 @@ var Image2 = class _Image {
|
|
|
44499
44575
|
if (this.imageId !== "") {
|
|
44500
44576
|
return this;
|
|
44501
44577
|
}
|
|
44578
|
+
this.#client.logger.debug("Building image", "app_id", app.appId);
|
|
44502
44579
|
let baseImageId;
|
|
44503
44580
|
for (let i = 0; i < this.#layers.length; i++) {
|
|
44504
44581
|
const layer = this.#layers[i];
|
|
@@ -44573,6 +44650,7 @@ ${result.exception}`
|
|
|
44573
44650
|
baseImageId = resp.imageId;
|
|
44574
44651
|
}
|
|
44575
44652
|
this.#imageId = baseImageId;
|
|
44653
|
+
this.#client.logger.debug("Image build completed", "image_id", baseImageId);
|
|
44576
44654
|
return this;
|
|
44577
44655
|
}
|
|
44578
44656
|
/**
|
|
@@ -45036,6 +45114,11 @@ var QueueService = class {
|
|
|
45036
45114
|
objectCreationType: 5 /* OBJECT_CREATION_TYPE_EPHEMERAL */,
|
|
45037
45115
|
environmentName: this.#client.environmentName(params.environment)
|
|
45038
45116
|
});
|
|
45117
|
+
this.#client.logger.debug(
|
|
45118
|
+
"Created ephemeral Queue",
|
|
45119
|
+
"queue_id",
|
|
45120
|
+
resp.queueId
|
|
45121
|
+
);
|
|
45039
45122
|
const ephemeralHbManager = new EphemeralHeartbeatManager(
|
|
45040
45123
|
() => this.#client.cpClient.queueHeartbeat({ queueId: resp.queueId })
|
|
45041
45124
|
);
|
|
@@ -45050,6 +45133,13 @@ var QueueService = class {
|
|
|
45050
45133
|
objectCreationType: params.createIfMissing ? 1 /* OBJECT_CREATION_TYPE_CREATE_IF_MISSING */ : void 0,
|
|
45051
45134
|
environmentName: this.#client.environmentName(params.environment)
|
|
45052
45135
|
});
|
|
45136
|
+
this.#client.logger.debug(
|
|
45137
|
+
"Retrieved Queue",
|
|
45138
|
+
"queue_id",
|
|
45139
|
+
resp.queueId,
|
|
45140
|
+
"queue_name",
|
|
45141
|
+
name
|
|
45142
|
+
);
|
|
45053
45143
|
return new Queue(this.#client, resp.queueId, name);
|
|
45054
45144
|
}
|
|
45055
45145
|
/**
|
|
@@ -45761,6 +45851,11 @@ var SandboxService = class {
|
|
|
45761
45851
|
}
|
|
45762
45852
|
throw err;
|
|
45763
45853
|
}
|
|
45854
|
+
this.#client.logger.debug(
|
|
45855
|
+
"Created Sandbox",
|
|
45856
|
+
"sandbox_id",
|
|
45857
|
+
createResp.sandboxId
|
|
45858
|
+
);
|
|
45764
45859
|
return new Sandbox2(this.#client, createResp.sandboxId);
|
|
45765
45860
|
}
|
|
45766
45861
|
/** Returns a running {@link Sandbox} object from an ID.
|
|
@@ -46021,6 +46116,15 @@ var Sandbox2 = class _Sandbox {
|
|
|
46021
46116
|
mergedParams
|
|
46022
46117
|
);
|
|
46023
46118
|
const resp = await this.#client.cpClient.containerExec(req);
|
|
46119
|
+
this.#client.logger.debug(
|
|
46120
|
+
"Created ContainerProcess",
|
|
46121
|
+
"exec_id",
|
|
46122
|
+
resp.execId,
|
|
46123
|
+
"sandbox_id",
|
|
46124
|
+
this.sandboxId,
|
|
46125
|
+
"command",
|
|
46126
|
+
command
|
|
46127
|
+
);
|
|
46024
46128
|
return new ContainerProcess(this.#client, resp.execId, params);
|
|
46025
46129
|
}
|
|
46026
46130
|
async #getTaskId() {
|
|
@@ -46053,7 +46157,17 @@ var Sandbox2 = class _Sandbox {
|
|
|
46053
46157
|
timeout: 10
|
|
46054
46158
|
});
|
|
46055
46159
|
if (resp.result) {
|
|
46056
|
-
|
|
46160
|
+
const returnCode = _Sandbox.#getReturnCode(resp.result);
|
|
46161
|
+
this.#client.logger.debug(
|
|
46162
|
+
"Sandbox wait completed",
|
|
46163
|
+
"sandbox_id",
|
|
46164
|
+
this.sandboxId,
|
|
46165
|
+
"status",
|
|
46166
|
+
resp.result.status,
|
|
46167
|
+
"return_code",
|
|
46168
|
+
returnCode
|
|
46169
|
+
);
|
|
46170
|
+
return returnCode;
|
|
46057
46171
|
}
|
|
46058
46172
|
}
|
|
46059
46173
|
}
|
|
@@ -46318,6 +46432,13 @@ var VolumeService = class {
|
|
|
46318
46432
|
environmentName: this.#client.environmentName(params?.environment),
|
|
46319
46433
|
objectCreationType: params?.createIfMissing ? 1 /* OBJECT_CREATION_TYPE_CREATE_IF_MISSING */ : 0 /* OBJECT_CREATION_TYPE_UNSPECIFIED */
|
|
46320
46434
|
});
|
|
46435
|
+
this.#client.logger.debug(
|
|
46436
|
+
"Retrieved Volume",
|
|
46437
|
+
"volume_id",
|
|
46438
|
+
resp.volumeId,
|
|
46439
|
+
"volume_name",
|
|
46440
|
+
name
|
|
46441
|
+
);
|
|
46321
46442
|
return new Volume(resp.volumeId, name);
|
|
46322
46443
|
} catch (err) {
|
|
46323
46444
|
if (err instanceof import_nice_grpc9.ClientError && err.code === import_nice_grpc9.Status.NOT_FOUND)
|
|
@@ -46334,6 +46455,11 @@ var VolumeService = class {
|
|
|
46334
46455
|
objectCreationType: 5 /* OBJECT_CREATION_TYPE_EPHEMERAL */,
|
|
46335
46456
|
environmentName: this.#client.environmentName(params.environment)
|
|
46336
46457
|
});
|
|
46458
|
+
this.#client.logger.debug(
|
|
46459
|
+
"Created ephemeral Volume",
|
|
46460
|
+
"volume_id",
|
|
46461
|
+
resp.volumeId
|
|
46462
|
+
);
|
|
46337
46463
|
const ephemeralHbManager = new EphemeralHeartbeatManager(
|
|
46338
46464
|
() => this.#client.cpClient.volumeHeartbeat({ volumeId: resp.volumeId })
|
|
46339
46465
|
);
|
|
@@ -46386,9 +46512,17 @@ var import_node_fs = require("fs");
|
|
|
46386
46512
|
var import_node_os = require("os");
|
|
46387
46513
|
var import_node_path = __toESM(require("path"), 1);
|
|
46388
46514
|
var import_smol_toml = require("smol-toml");
|
|
46515
|
+
function configFilePath() {
|
|
46516
|
+
const configPath = process.env["MODAL_CONFIG_PATH"];
|
|
46517
|
+
if (configPath && configPath !== "") {
|
|
46518
|
+
return configPath;
|
|
46519
|
+
}
|
|
46520
|
+
return import_node_path.default.join((0, import_node_os.homedir)(), ".modal.toml");
|
|
46521
|
+
}
|
|
46389
46522
|
function readConfigFile() {
|
|
46390
46523
|
try {
|
|
46391
|
-
const
|
|
46524
|
+
const configPath = configFilePath();
|
|
46525
|
+
const configContent = (0, import_node_fs.readFileSync)(configPath, {
|
|
46392
46526
|
encoding: "utf-8"
|
|
46393
46527
|
});
|
|
46394
46528
|
return (0, import_smol_toml.parse)(configContent);
|
|
@@ -46415,7 +46549,8 @@ function getProfile(profileName) {
|
|
|
46415
46549
|
tokenId: process.env["MODAL_TOKEN_ID"] || profileData.token_id,
|
|
46416
46550
|
tokenSecret: process.env["MODAL_TOKEN_SECRET"] || profileData.token_secret,
|
|
46417
46551
|
environment: process.env["MODAL_ENVIRONMENT"] || profileData.environment,
|
|
46418
|
-
imageBuilderVersion: process.env["MODAL_IMAGE_BUILDER_VERSION"] || profileData.imageBuilderVersion
|
|
46552
|
+
imageBuilderVersion: process.env["MODAL_IMAGE_BUILDER_VERSION"] || profileData.imageBuilderVersion,
|
|
46553
|
+
logLevel: process.env["MODAL_LOGLEVEL"] || profileData.loglevel
|
|
46419
46554
|
};
|
|
46420
46555
|
return profile;
|
|
46421
46556
|
}
|
|
@@ -46425,13 +46560,15 @@ var REFRESH_WINDOW = 5 * 60;
|
|
|
46425
46560
|
var DEFAULT_EXPIRY_OFFSET = 20 * 60;
|
|
46426
46561
|
var AuthTokenManager = class {
|
|
46427
46562
|
client;
|
|
46563
|
+
logger;
|
|
46428
46564
|
currentToken = "";
|
|
46429
46565
|
tokenExpiry = 0;
|
|
46430
46566
|
stopped = false;
|
|
46431
46567
|
timeoutId = null;
|
|
46432
46568
|
initialTokenPromise = null;
|
|
46433
|
-
constructor(client2) {
|
|
46569
|
+
constructor(client2, logger) {
|
|
46434
46570
|
this.client = client2;
|
|
46571
|
+
this.logger = logger;
|
|
46435
46572
|
}
|
|
46436
46573
|
/**
|
|
46437
46574
|
* Returns the current cached token.
|
|
@@ -46462,9 +46599,19 @@ var AuthTokenManager = class {
|
|
|
46462
46599
|
if (exp > 0) {
|
|
46463
46600
|
this.tokenExpiry = exp;
|
|
46464
46601
|
} else {
|
|
46465
|
-
|
|
46602
|
+
this.logger.warn("x-modal-auth-token does not contain exp field");
|
|
46466
46603
|
this.tokenExpiry = Math.floor(Date.now() / 1e3) + DEFAULT_EXPIRY_OFFSET;
|
|
46467
46604
|
}
|
|
46605
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
46606
|
+
const expiresIn = this.tokenExpiry - now;
|
|
46607
|
+
const refreshIn = this.tokenExpiry - now - REFRESH_WINDOW;
|
|
46608
|
+
this.logger.debug(
|
|
46609
|
+
"Fetched auth token",
|
|
46610
|
+
"expires_in",
|
|
46611
|
+
`${expiresIn}s`,
|
|
46612
|
+
"refresh_in",
|
|
46613
|
+
`${refreshIn}s`
|
|
46614
|
+
);
|
|
46468
46615
|
}
|
|
46469
46616
|
/**
|
|
46470
46617
|
* Background loop that refreshes tokens REFRESH_WINDOW seconds before they expire.
|
|
@@ -46484,7 +46631,7 @@ var AuthTokenManager = class {
|
|
|
46484
46631
|
try {
|
|
46485
46632
|
await this.fetchToken();
|
|
46486
46633
|
} catch (error) {
|
|
46487
|
-
|
|
46634
|
+
this.logger.error("Failed to refresh auth token", "error", error);
|
|
46488
46635
|
await new Promise((resolve) => setTimeout(resolve, 5e3));
|
|
46489
46636
|
}
|
|
46490
46637
|
}
|
|
@@ -46548,7 +46695,113 @@ var AuthTokenManager = class {
|
|
|
46548
46695
|
|
|
46549
46696
|
// src/version.ts
|
|
46550
46697
|
function getSDKVersion() {
|
|
46551
|
-
return true ? "0.5.
|
|
46698
|
+
return true ? "0.5.2" : "0.0.0";
|
|
46699
|
+
}
|
|
46700
|
+
|
|
46701
|
+
// src/logger.ts
|
|
46702
|
+
var LOG_LEVELS = {
|
|
46703
|
+
debug: 0,
|
|
46704
|
+
info: 1,
|
|
46705
|
+
warn: 2,
|
|
46706
|
+
error: 3
|
|
46707
|
+
};
|
|
46708
|
+
function parseLogLevel(level) {
|
|
46709
|
+
if (!level) {
|
|
46710
|
+
return "warn";
|
|
46711
|
+
}
|
|
46712
|
+
const normalized = level.toLowerCase();
|
|
46713
|
+
if (normalized === "debug" || normalized === "info" || normalized === "warn" || normalized === "warning" || normalized === "error") {
|
|
46714
|
+
return normalized === "warning" ? "warn" : normalized;
|
|
46715
|
+
}
|
|
46716
|
+
throw new Error(
|
|
46717
|
+
`Invalid log level value: "${level}" (must be debug, info, warn, or error)`
|
|
46718
|
+
);
|
|
46719
|
+
}
|
|
46720
|
+
var DefaultLogger = class {
|
|
46721
|
+
levelValue;
|
|
46722
|
+
constructor(level = "warn") {
|
|
46723
|
+
this.levelValue = LOG_LEVELS[level];
|
|
46724
|
+
}
|
|
46725
|
+
debug(message, ...args) {
|
|
46726
|
+
if (this.levelValue <= LOG_LEVELS.debug) {
|
|
46727
|
+
console.log(this.formatMessage("DEBUG", message, args));
|
|
46728
|
+
}
|
|
46729
|
+
}
|
|
46730
|
+
info(message, ...args) {
|
|
46731
|
+
if (this.levelValue <= LOG_LEVELS.info) {
|
|
46732
|
+
console.log(this.formatMessage("INFO", message, args));
|
|
46733
|
+
}
|
|
46734
|
+
}
|
|
46735
|
+
warn(message, ...args) {
|
|
46736
|
+
if (this.levelValue <= LOG_LEVELS.warn) {
|
|
46737
|
+
console.warn(this.formatMessage("WARN", message, args));
|
|
46738
|
+
}
|
|
46739
|
+
}
|
|
46740
|
+
error(message, ...args) {
|
|
46741
|
+
if (this.levelValue <= LOG_LEVELS.error) {
|
|
46742
|
+
console.error(this.formatMessage("ERROR", message, args));
|
|
46743
|
+
}
|
|
46744
|
+
}
|
|
46745
|
+
formatMessage(level, message, args) {
|
|
46746
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
46747
|
+
let formatted = `time=${timestamp} level=${level} msg="${message}"`;
|
|
46748
|
+
if (args.length > 0) {
|
|
46749
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
46750
|
+
if (i + 1 < args.length) {
|
|
46751
|
+
const key = args[i];
|
|
46752
|
+
const value = args[i + 1];
|
|
46753
|
+
formatted += ` ${key}=${this.formatValue(value)}`;
|
|
46754
|
+
}
|
|
46755
|
+
}
|
|
46756
|
+
}
|
|
46757
|
+
return formatted;
|
|
46758
|
+
}
|
|
46759
|
+
formatValue(value) {
|
|
46760
|
+
if (typeof value === "string") {
|
|
46761
|
+
return value.includes(" ") ? `"${value}"` : value;
|
|
46762
|
+
}
|
|
46763
|
+
if (value instanceof Error) {
|
|
46764
|
+
return `"${value.message}"`;
|
|
46765
|
+
}
|
|
46766
|
+
if (Array.isArray(value)) {
|
|
46767
|
+
return `[${value.join(",")}]`;
|
|
46768
|
+
}
|
|
46769
|
+
return String(value);
|
|
46770
|
+
}
|
|
46771
|
+
};
|
|
46772
|
+
var FilteredLogger = class {
|
|
46773
|
+
constructor(logger, level) {
|
|
46774
|
+
this.logger = logger;
|
|
46775
|
+
this.levelValue = LOG_LEVELS[level];
|
|
46776
|
+
}
|
|
46777
|
+
levelValue;
|
|
46778
|
+
debug(message, ...args) {
|
|
46779
|
+
if (this.levelValue <= LOG_LEVELS.debug) {
|
|
46780
|
+
this.logger.debug(message, ...args);
|
|
46781
|
+
}
|
|
46782
|
+
}
|
|
46783
|
+
info(message, ...args) {
|
|
46784
|
+
if (this.levelValue <= LOG_LEVELS.info) {
|
|
46785
|
+
this.logger.info(message, ...args);
|
|
46786
|
+
}
|
|
46787
|
+
}
|
|
46788
|
+
warn(message, ...args) {
|
|
46789
|
+
if (this.levelValue <= LOG_LEVELS.warn) {
|
|
46790
|
+
this.logger.warn(message, ...args);
|
|
46791
|
+
}
|
|
46792
|
+
}
|
|
46793
|
+
error(message, ...args) {
|
|
46794
|
+
if (this.levelValue <= LOG_LEVELS.error) {
|
|
46795
|
+
this.logger.error(message, ...args);
|
|
46796
|
+
}
|
|
46797
|
+
}
|
|
46798
|
+
};
|
|
46799
|
+
function createLogger(logger, logLevel = "") {
|
|
46800
|
+
const level = parseLogLevel(logLevel);
|
|
46801
|
+
if (logger) {
|
|
46802
|
+
return new FilteredLogger(logger, level);
|
|
46803
|
+
}
|
|
46804
|
+
return new DefaultLogger(level);
|
|
46552
46805
|
}
|
|
46553
46806
|
|
|
46554
46807
|
// src/client.ts
|
|
@@ -46566,8 +46819,10 @@ var ModalClient = class {
|
|
|
46566
46819
|
/** @ignore */
|
|
46567
46820
|
cpClient;
|
|
46568
46821
|
profile;
|
|
46822
|
+
logger;
|
|
46569
46823
|
ipClients;
|
|
46570
46824
|
authTokenManager = null;
|
|
46825
|
+
customMiddleware;
|
|
46571
46826
|
constructor(params) {
|
|
46572
46827
|
checkForRenamedParams(params, { timeout: "timeoutMs" });
|
|
46573
46828
|
const baseProfile = getProfile(process.env["MODAL_PROFILE"]);
|
|
@@ -46577,8 +46832,19 @@ var ModalClient = class {
|
|
|
46577
46832
|
...params?.tokenSecret && { tokenSecret: params.tokenSecret },
|
|
46578
46833
|
...params?.environment && { environment: params.environment }
|
|
46579
46834
|
};
|
|
46835
|
+
const logLevelValue = params?.logLevel || this.profile.logLevel || "";
|
|
46836
|
+
this.logger = createLogger(params?.logger, logLevelValue);
|
|
46837
|
+
this.logger.debug(
|
|
46838
|
+
"Initializing Modal client",
|
|
46839
|
+
"version",
|
|
46840
|
+
getSDKVersion(),
|
|
46841
|
+
"server_url",
|
|
46842
|
+
this.profile.serverUrl
|
|
46843
|
+
);
|
|
46844
|
+
this.customMiddleware = params?.grpcMiddleware ?? [];
|
|
46580
46845
|
this.ipClients = /* @__PURE__ */ new Map();
|
|
46581
46846
|
this.cpClient = params?.cpClient ?? this.createClient(this.profile);
|
|
46847
|
+
this.logger.debug("Modal client initialized successfully");
|
|
46582
46848
|
this.apps = new AppService(this);
|
|
46583
46849
|
this.cls = new ClsService(this);
|
|
46584
46850
|
this.functions = new FunctionService(this);
|
|
@@ -46602,16 +46868,19 @@ var ModalClient = class {
|
|
|
46602
46868
|
if (existing) {
|
|
46603
46869
|
return existing;
|
|
46604
46870
|
}
|
|
46871
|
+
this.logger.debug("Creating input plane client", "server_url", serverUrl);
|
|
46605
46872
|
const profile = { ...this.profile, serverUrl };
|
|
46606
46873
|
const newClient = this.createClient(profile);
|
|
46607
46874
|
this.ipClients.set(serverUrl, newClient);
|
|
46608
46875
|
return newClient;
|
|
46609
46876
|
}
|
|
46610
46877
|
close() {
|
|
46878
|
+
this.logger.debug("Closing Modal client");
|
|
46611
46879
|
if (this.authTokenManager) {
|
|
46612
46880
|
this.authTokenManager.stop();
|
|
46613
46881
|
this.authTokenManager = null;
|
|
46614
46882
|
}
|
|
46883
|
+
this.logger.debug("Modal client closed");
|
|
46615
46884
|
}
|
|
46616
46885
|
version() {
|
|
46617
46886
|
return getSDKVersion();
|
|
@@ -46622,12 +46891,101 @@ var ModalClient = class {
|
|
|
46622
46891
|
"grpc.max_send_message_length": 100 * 1024 * 1024,
|
|
46623
46892
|
"grpc-node.flow_control_window": 64 * 1024 * 1024
|
|
46624
46893
|
});
|
|
46625
|
-
|
|
46894
|
+
let factory = (0, import_nice_grpc10.createClientFactory)().use(this.authMiddleware(profile)).use(this.retryMiddleware()).use(timeoutMiddleware);
|
|
46895
|
+
for (const middleware of this.customMiddleware) {
|
|
46896
|
+
factory = factory.use(middleware);
|
|
46897
|
+
}
|
|
46898
|
+
return factory.create(ModalClientDefinition, channel);
|
|
46899
|
+
}
|
|
46900
|
+
/** Middleware to retry transient errors and timeouts for unary requests. */
|
|
46901
|
+
retryMiddleware() {
|
|
46902
|
+
const logger = this.logger;
|
|
46903
|
+
return async function* retryMiddleware(call, options) {
|
|
46904
|
+
const {
|
|
46905
|
+
retries = 3,
|
|
46906
|
+
baseDelay = 100,
|
|
46907
|
+
maxDelay = 1e3,
|
|
46908
|
+
delayFactor = 2,
|
|
46909
|
+
additionalStatusCodes = [],
|
|
46910
|
+
signal,
|
|
46911
|
+
...restOptions
|
|
46912
|
+
} = options;
|
|
46913
|
+
if (call.requestStream || call.responseStream || !retries) {
|
|
46914
|
+
return yield* call.next(call.request, restOptions);
|
|
46915
|
+
}
|
|
46916
|
+
const retryableCodes = /* @__PURE__ */ new Set([
|
|
46917
|
+
...retryableGrpcStatusCodes,
|
|
46918
|
+
...additionalStatusCodes
|
|
46919
|
+
]);
|
|
46920
|
+
const idempotencyKey = (0, import_uuid.v4)();
|
|
46921
|
+
const startTime = Date.now();
|
|
46922
|
+
let attempt = 0;
|
|
46923
|
+
let delayMs = baseDelay;
|
|
46924
|
+
logger.debug("Sending gRPC request", "method", call.method.path);
|
|
46925
|
+
while (true) {
|
|
46926
|
+
const metadata = new import_nice_grpc10.Metadata(restOptions.metadata ?? {});
|
|
46927
|
+
metadata.set("x-idempotency-key", idempotencyKey);
|
|
46928
|
+
metadata.set("x-retry-attempt", String(attempt));
|
|
46929
|
+
if (attempt > 0) {
|
|
46930
|
+
metadata.set(
|
|
46931
|
+
"x-retry-delay",
|
|
46932
|
+
((Date.now() - startTime) / 1e3).toFixed(3)
|
|
46933
|
+
);
|
|
46934
|
+
}
|
|
46935
|
+
try {
|
|
46936
|
+
return yield* call.next(call.request, {
|
|
46937
|
+
...restOptions,
|
|
46938
|
+
metadata,
|
|
46939
|
+
signal
|
|
46940
|
+
});
|
|
46941
|
+
} catch (err) {
|
|
46942
|
+
if (!(err instanceof import_nice_grpc10.ClientError) || !retryableCodes.has(err.code) || attempt >= retries) {
|
|
46943
|
+
if (attempt === retries && attempt > 0) {
|
|
46944
|
+
logger.debug(
|
|
46945
|
+
"Final retry attempt failed",
|
|
46946
|
+
"error",
|
|
46947
|
+
err,
|
|
46948
|
+
"retries",
|
|
46949
|
+
attempt,
|
|
46950
|
+
"delay",
|
|
46951
|
+
delayMs,
|
|
46952
|
+
"method",
|
|
46953
|
+
call.method.path,
|
|
46954
|
+
"idempotency_key",
|
|
46955
|
+
idempotencyKey.substring(0, 8)
|
|
46956
|
+
);
|
|
46957
|
+
}
|
|
46958
|
+
throw err;
|
|
46959
|
+
}
|
|
46960
|
+
if (attempt > 0) {
|
|
46961
|
+
logger.debug(
|
|
46962
|
+
"Retryable failure",
|
|
46963
|
+
"error",
|
|
46964
|
+
err,
|
|
46965
|
+
"retries",
|
|
46966
|
+
attempt,
|
|
46967
|
+
"delay",
|
|
46968
|
+
delayMs,
|
|
46969
|
+
"method",
|
|
46970
|
+
call.method.path,
|
|
46971
|
+
"idempotency_key",
|
|
46972
|
+
idempotencyKey.substring(0, 8)
|
|
46973
|
+
);
|
|
46974
|
+
}
|
|
46975
|
+
await sleep(delayMs, signal);
|
|
46976
|
+
delayMs = Math.min(delayMs * delayFactor, maxDelay);
|
|
46977
|
+
attempt += 1;
|
|
46978
|
+
}
|
|
46979
|
+
}
|
|
46980
|
+
};
|
|
46626
46981
|
}
|
|
46627
46982
|
authMiddleware(profile) {
|
|
46628
46983
|
const getOrCreateAuthTokenManager = () => {
|
|
46629
46984
|
if (!this.authTokenManager) {
|
|
46630
|
-
this.authTokenManager = new AuthTokenManager(
|
|
46985
|
+
this.authTokenManager = new AuthTokenManager(
|
|
46986
|
+
this.cpClient,
|
|
46987
|
+
this.logger
|
|
46988
|
+
);
|
|
46631
46989
|
this.authTokenManager.start();
|
|
46632
46990
|
}
|
|
46633
46991
|
return this.authTokenManager;
|
|
@@ -46717,53 +47075,6 @@ var sleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
|
46717
47075
|
{ once: true }
|
|
46718
47076
|
);
|
|
46719
47077
|
});
|
|
46720
|
-
var retryMiddleware = async function* retryMiddleware2(call, options) {
|
|
46721
|
-
const {
|
|
46722
|
-
retries = 3,
|
|
46723
|
-
baseDelay = 100,
|
|
46724
|
-
maxDelay = 1e3,
|
|
46725
|
-
delayFactor = 2,
|
|
46726
|
-
additionalStatusCodes = [],
|
|
46727
|
-
signal,
|
|
46728
|
-
...restOptions
|
|
46729
|
-
} = options;
|
|
46730
|
-
if (call.requestStream || call.responseStream || !retries) {
|
|
46731
|
-
return yield* call.next(call.request, restOptions);
|
|
46732
|
-
}
|
|
46733
|
-
const retryableCodes = /* @__PURE__ */ new Set([
|
|
46734
|
-
...retryableGrpcStatusCodes,
|
|
46735
|
-
...additionalStatusCodes
|
|
46736
|
-
]);
|
|
46737
|
-
const idempotencyKey = (0, import_uuid.v4)();
|
|
46738
|
-
const startTime = Date.now();
|
|
46739
|
-
let attempt = 0;
|
|
46740
|
-
let delayMs = baseDelay;
|
|
46741
|
-
while (true) {
|
|
46742
|
-
const metadata = new import_nice_grpc10.Metadata(restOptions.metadata ?? {});
|
|
46743
|
-
metadata.set("x-idempotency-key", idempotencyKey);
|
|
46744
|
-
metadata.set("x-retry-attempt", String(attempt));
|
|
46745
|
-
if (attempt > 0) {
|
|
46746
|
-
metadata.set(
|
|
46747
|
-
"x-retry-delay",
|
|
46748
|
-
((Date.now() - startTime) / 1e3).toFixed(3)
|
|
46749
|
-
);
|
|
46750
|
-
}
|
|
46751
|
-
try {
|
|
46752
|
-
return yield* call.next(call.request, {
|
|
46753
|
-
...restOptions,
|
|
46754
|
-
metadata,
|
|
46755
|
-
signal
|
|
46756
|
-
});
|
|
46757
|
-
} catch (err) {
|
|
46758
|
-
if (!(err instanceof import_nice_grpc10.ClientError) || !retryableCodes.has(err.code) || attempt >= retries) {
|
|
46759
|
-
throw err;
|
|
46760
|
-
}
|
|
46761
|
-
await sleep(delayMs, signal);
|
|
46762
|
-
delayMs = Math.min(delayMs * delayFactor, maxDelay);
|
|
46763
|
-
attempt += 1;
|
|
46764
|
-
}
|
|
46765
|
-
}
|
|
46766
|
-
};
|
|
46767
47078
|
var defaultClient;
|
|
46768
47079
|
var defaultClientOptions;
|
|
46769
47080
|
function getDefaultClient() {
|
|
@@ -46807,6 +47118,13 @@ var AppService = class {
|
|
|
46807
47118
|
environmentName: this.#client.environmentName(params.environment),
|
|
46808
47119
|
objectCreationType: params.createIfMissing ? 1 /* OBJECT_CREATION_TYPE_CREATE_IF_MISSING */ : 0 /* OBJECT_CREATION_TYPE_UNSPECIFIED */
|
|
46809
47120
|
});
|
|
47121
|
+
this.#client.logger.debug(
|
|
47122
|
+
"Retrieved App",
|
|
47123
|
+
"app_id",
|
|
47124
|
+
resp.appId,
|
|
47125
|
+
"app_name",
|
|
47126
|
+
name
|
|
47127
|
+
);
|
|
46810
47128
|
return new App2(resp.appId, name);
|
|
46811
47129
|
} catch (err) {
|
|
46812
47130
|
if (err instanceof import_nice_grpc11.ClientError && err.code === import_nice_grpc11.Status.NOT_FOUND)
|