@le-space/core 0.1.33 → 0.1.35

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.
Files changed (3) hide show
  1. package/index.d.ts +48 -1
  2. package/index.js +56 -3
  3. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -31,6 +31,15 @@ declare function resolveRootfsReference(itemHash: string, options: {
31
31
  declare const DEFAULT_CRN_LIST_URL = "https://crns-list.aleph.sh/crns.json";
32
32
  declare const DEFAULT_COUNTRY_LOOKUP_BASE_URL = "https://api.country.is";
33
33
  declare const DEFAULT_DNS_RESOLVE_URL = "https://dns.google/resolve";
34
+ interface CrnCapacitySpec {
35
+ vcpus: number;
36
+ memoryMiB: number;
37
+ diskMiB: number;
38
+ }
39
+ declare function filterDeployableCrns(crns: ReadonlyArray<CrnRecord>, options?: {
40
+ excludedHashes?: ReadonlyArray<string>;
41
+ spec?: CrnCapacitySpec | null;
42
+ }): CrnRecord[];
34
43
  declare function fetchCrns(options: {
35
44
  url?: string;
36
45
  fetch: FetchLike;
@@ -381,6 +390,18 @@ declare function ensureInstancePortForwards(args: {
381
390
  }>;
382
391
 
383
392
  declare const SSH_PUBLIC_KEY_PATTERN: RegExp;
393
+ interface TierSpec {
394
+ vcpus: number;
395
+ memoryMiB: number;
396
+ diskMiB: number;
397
+ }
398
+ interface PaymentQuote {
399
+ required: number;
400
+ available: number;
401
+ computeUnits: number;
402
+ unitPrice: number;
403
+ label: 'credits';
404
+ }
384
405
  declare function normalizeSshPublicKey(value: string): string;
385
406
  declare function isValidSshPublicKey(value: string): boolean;
386
407
  declare function createReleaseMetadata(name: string, rootfsVersion: string, deployer?: string): {
@@ -388,6 +409,32 @@ declare function createReleaseMetadata(name: string, rootfsVersion: string, depl
388
409
  rootfs_version: string;
389
410
  deployer: string;
390
411
  };
412
+ declare function selectedTier<TTier extends {
413
+ id: string;
414
+ }>(pricing: {
415
+ tiers: TTier[];
416
+ } | null, tierId: string): TTier | null;
417
+ declare function tierSpec<TTier extends {
418
+ compute_units: number;
419
+ }>(pricing: {
420
+ compute_unit: {
421
+ vcpus: number;
422
+ memory_mib: number;
423
+ disk_mib: number;
424
+ };
425
+ }, tier: TTier): TierSpec;
426
+ declare function buildPaymentQuote<TTier extends {
427
+ compute_units: number;
428
+ }>(tier: TTier, pricing: {
429
+ price: {
430
+ compute_unit?: {
431
+ credit?: string | number | null;
432
+ } | null;
433
+ };
434
+ }, balance: {
435
+ credit_balance?: number | null;
436
+ }): PaymentQuote | null;
437
+ declare function quoteRequiredBudgetUnits(quote: PaymentQuote | null): bigint;
391
438
  declare function createInstanceContent(args: {
392
439
  address: string;
393
440
  name: string;
@@ -553,4 +600,4 @@ declare function verifyUcGoPeerReachability(args: {
553
600
  checks: Record<string, Record<string, unknown>>;
554
601
  }>;
555
602
 
556
- export { DEFAULT_ALEPH_API_HOST, DEFAULT_ALEPH_CHANNEL, DEFAULT_COUNTRY_LOOKUP_BASE_URL, DEFAULT_CRN_LIST_URL, DEFAULT_DNS_RESOLVE_URL, DEFAULT_INSTANCE_PORT_FORWARDS, DEFAULT_IPFS_GATEWAY_BASE_URL, DEFAULT_SCHEDULER_ALLOCATION_URL, DEFAULT_TWO_N_SIX_HASH_URL, type FetchLike, type FetchLikeResponse, type HttpProbeResult, ITEM_HASH_RE, type JsonFetchLike, type JsonFetchLikeResponse, type RetentionRecord, SSH_PUBLIC_KEY_PATTERN, SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY, type TcpProbeResult, broadcastAlephMessage, cleanupFailedDeployment, configureUcGoPeer, createDeploymentIntent, createInstanceContent, createPortForwardAggregateContent, createReleaseMetadata, createUnsignedAggregateMessage, createUnsignedForgetMessage, createUnsignedInstanceMessage, deployInstance, describeRejectedDeployment, describeRuntimeAvailability, enrichCrnsWithGeo, ensureInstancePortForwards, extractProxyUrl, extractReferenceHashes, fetch2n6WebAccessUrl, fetchAggregateKey, fetchCrnExecutionMap, fetchCrns, fetchMessageEnvelope, fetchPortForwardAggregate, fetchSchedulerAllocation, fetchUcGoPeerMetadata, fetchVmRuntime, findCrnByHash, forgetAlephMessages, inspectDeploymentResult, inspectMessageResult, isInvalidMessageFormatResponse, isRetryableBroadcastFailure, isValidSshPublicKey, listGeocodedCrns, mergePortFlagMaps, mergeRequiredPortForwards, messageTypeFromEnvelope, normalizeBroadcastStatus, normalizeExecution, normalizeExistingPortForwardEntry, normalizeMessageStatus, normalizeProxyUrl, normalizeSshPublicKey, notifyCrnAllocation, portForwardLabel, postBroadcastPayload, probeRootfsGateway, publishAggregateKey, rankCandidateCrns, requestedPortFlags, requiredInstancePortForwards, resolveRootfsReference, retainSuccessfulDeployments, selectPreferredCrn, signAlephMessage, signaturePayload, validateRootfsManifest, verifyRootfsExists, verifyUcGoPeerReachability, waitForDeploymentResult, waitForSetupEndpoint, waitForVmRuntime };
603
+ export { type CrnCapacitySpec, DEFAULT_ALEPH_API_HOST, DEFAULT_ALEPH_CHANNEL, DEFAULT_COUNTRY_LOOKUP_BASE_URL, DEFAULT_CRN_LIST_URL, DEFAULT_DNS_RESOLVE_URL, DEFAULT_INSTANCE_PORT_FORWARDS, DEFAULT_IPFS_GATEWAY_BASE_URL, DEFAULT_SCHEDULER_ALLOCATION_URL, DEFAULT_TWO_N_SIX_HASH_URL, type FetchLike, type FetchLikeResponse, type HttpProbeResult, ITEM_HASH_RE, type JsonFetchLike, type JsonFetchLikeResponse, type PaymentQuote, type RetentionRecord, SSH_PUBLIC_KEY_PATTERN, SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY, type TcpProbeResult, type TierSpec, broadcastAlephMessage, buildPaymentQuote, cleanupFailedDeployment, configureUcGoPeer, createDeploymentIntent, createInstanceContent, createPortForwardAggregateContent, createReleaseMetadata, createUnsignedAggregateMessage, createUnsignedForgetMessage, createUnsignedInstanceMessage, deployInstance, describeRejectedDeployment, describeRuntimeAvailability, enrichCrnsWithGeo, ensureInstancePortForwards, extractProxyUrl, extractReferenceHashes, fetch2n6WebAccessUrl, fetchAggregateKey, fetchCrnExecutionMap, fetchCrns, fetchMessageEnvelope, fetchPortForwardAggregate, fetchSchedulerAllocation, fetchUcGoPeerMetadata, fetchVmRuntime, filterDeployableCrns, findCrnByHash, forgetAlephMessages, inspectDeploymentResult, inspectMessageResult, isInvalidMessageFormatResponse, isRetryableBroadcastFailure, isValidSshPublicKey, listGeocodedCrns, mergePortFlagMaps, mergeRequiredPortForwards, messageTypeFromEnvelope, normalizeBroadcastStatus, normalizeExecution, normalizeExistingPortForwardEntry, normalizeMessageStatus, normalizeProxyUrl, normalizeSshPublicKey, notifyCrnAllocation, portForwardLabel, postBroadcastPayload, probeRootfsGateway, publishAggregateKey, quoteRequiredBudgetUnits, rankCandidateCrns, requestedPortFlags, requiredInstancePortForwards, resolveRootfsReference, retainSuccessfulDeployments, selectPreferredCrn, selectedTier, signAlephMessage, signaturePayload, tierSpec, validateRootfsManifest, verifyRootfsExists, verifyUcGoPeerReachability, waitForDeploymentResult, waitForSetupEndpoint, waitForVmRuntime };
package/index.js CHANGED
@@ -213,6 +213,15 @@ function compatibleCrns(crns, excludedHashes = []) {
213
213
  return true;
214
214
  });
215
215
  }
216
+ function hasCrnCapacity(crn, spec) {
217
+ if (!spec) return true;
218
+ const usage = crn.system_usage;
219
+ if (!usage) return true;
220
+ const cpuOk = usage.cpu?.count == null || usage.cpu.count >= spec.vcpus;
221
+ const memoryOk = usage.mem?.available_kB == null || usage.mem.available_kB >= spec.memoryMiB * 1024;
222
+ const diskOk = usage.disk?.available_kB == null || usage.disk.available_kB >= spec.diskMiB * 1024;
223
+ return cpuOk && memoryOk && diskOk;
224
+ }
216
225
  function scoreSortedCrns(crns) {
217
226
  return [...crns].sort((left, right) => {
218
227
  const rightScore = typeof right.score === "number" ? right.score : Number(right.score ?? Number.NEGATIVE_INFINITY);
@@ -223,6 +232,11 @@ function scoreSortedCrns(crns) {
223
232
  return leftName.localeCompare(rightName);
224
233
  });
225
234
  }
235
+ function filterDeployableCrns(crns, options = {}) {
236
+ return scoreSortedCrns(
237
+ compatibleCrns(crns, options.excludedHashes).filter((crn) => hasCrnCapacity(crn, options.spec))
238
+ );
239
+ }
226
240
  async function resolveHostIp(host, options) {
227
241
  if (!host) return null;
228
242
  if (isIpAddress(host)) return host;
@@ -309,10 +323,10 @@ async function enrichCrnsWithGeo(crns, options) {
309
323
  );
310
324
  }
311
325
  async function listGeocodedCrns(options) {
312
- const sortedCrns = scoreSortedCrns(compatibleCrns(await fetchCrns({
326
+ const sortedCrns = filterDeployableCrns(await fetchCrns({
313
327
  url: options.url,
314
328
  fetch: options.fetch
315
- })));
329
+ }));
316
330
  const geocodedCrns = await enrichCrnsWithGeo(
317
331
  sortedCrns.slice(0, Math.max(1, Number(options.limit) || 30)),
318
332
  options
@@ -326,7 +340,9 @@ async function listGeocodedCrns(options) {
326
340
  async function rankCandidateCrns(crns, options) {
327
341
  const preferredCountryCode = normalizeCountryCode(options.preferredCountryCode);
328
342
  const geoLimit = Math.max(1, Number(options.geoLimit) || 30);
329
- const sortedCrns = scoreSortedCrns(compatibleCrns(crns, options.excludedHashes));
343
+ const sortedCrns = filterDeployableCrns(crns, {
344
+ excludedHashes: options.excludedHashes
345
+ });
330
346
  if (!preferredCountryCode || sortedCrns.length === 0) {
331
347
  return sortedCrns;
332
348
  }
@@ -1210,6 +1226,11 @@ async function ensureInstancePortForwards(args) {
1210
1226
 
1211
1227
  // src/instance-deployment.ts
1212
1228
  var SSH_PUBLIC_KEY_PATTERN = /^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521|sk-ssh-ed25519@openssh\.com|sk-ecdsa-sha2-nistp256@openssh\.com)\s+[A-Za-z0-9+/]+={0,3}(?:\s+.+)?$/;
1229
+ function toFiniteNumber(value) {
1230
+ if (typeof value === "number") return value;
1231
+ if (typeof value === "string" && value.trim()) return Number(value);
1232
+ return Number.NaN;
1233
+ }
1213
1234
  function normalizeSshPublicKey(value) {
1214
1235
  return value.split(/\r?\n/g).map((line) => line.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim();
1215
1236
  }
@@ -1223,6 +1244,33 @@ function createReleaseMetadata(name, rootfsVersion, deployer = "shared-aleph-too
1223
1244
  deployer
1224
1245
  };
1225
1246
  }
1247
+ function selectedTier(pricing, tierId) {
1248
+ return pricing?.tiers.find((tier) => tier.id === tierId) ?? null;
1249
+ }
1250
+ function tierSpec(pricing, tier) {
1251
+ return {
1252
+ vcpus: pricing.compute_unit.vcpus * tier.compute_units,
1253
+ memoryMiB: pricing.compute_unit.memory_mib * tier.compute_units,
1254
+ diskMiB: pricing.compute_unit.disk_mib * tier.compute_units
1255
+ };
1256
+ }
1257
+ function buildPaymentQuote(tier, pricing, balance) {
1258
+ const computeUnitPrice = pricing.price.compute_unit;
1259
+ if (!computeUnitPrice) return null;
1260
+ const unitPrice = toFiniteNumber(computeUnitPrice.credit);
1261
+ if (!Number.isFinite(unitPrice)) return null;
1262
+ return {
1263
+ required: unitPrice * tier.compute_units,
1264
+ available: Number(balance.credit_balance ?? 0),
1265
+ computeUnits: tier.compute_units,
1266
+ unitPrice,
1267
+ label: "credits"
1268
+ };
1269
+ }
1270
+ function quoteRequiredBudgetUnits(quote) {
1271
+ if (!quote) return 0n;
1272
+ return BigInt(Math.ceil(quote.required * 1e18));
1273
+ }
1226
1274
  function createInstanceContent(args) {
1227
1275
  const sshKey = normalizeSshPublicKey(args.sshPublicKey);
1228
1276
  if (!sshKey) {
@@ -1785,6 +1833,7 @@ export {
1785
1833
  SSH_PUBLIC_KEY_PATTERN,
1786
1834
  SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY,
1787
1835
  broadcastAlephMessage,
1836
+ buildPaymentQuote,
1788
1837
  cleanupFailedDeployment,
1789
1838
  configureUcGoPeer,
1790
1839
  createDeploymentIntent,
@@ -1810,6 +1859,7 @@ export {
1810
1859
  fetchSchedulerAllocation,
1811
1860
  fetchUcGoPeerMetadata,
1812
1861
  fetchVmRuntime,
1862
+ filterDeployableCrns,
1813
1863
  findCrnByHash,
1814
1864
  forgetAlephMessages,
1815
1865
  inspectDeploymentResult,
@@ -1832,14 +1882,17 @@ export {
1832
1882
  postBroadcastPayload,
1833
1883
  probeRootfsGateway,
1834
1884
  publishAggregateKey,
1885
+ quoteRequiredBudgetUnits,
1835
1886
  rankCandidateCrns,
1836
1887
  requestedPortFlags,
1837
1888
  requiredInstancePortForwards,
1838
1889
  resolveRootfsReference,
1839
1890
  retainSuccessfulDeployments,
1840
1891
  selectPreferredCrn,
1892
+ selectedTier,
1841
1893
  signAlephMessage,
1842
1894
  signaturePayload,
1895
+ tierSpec,
1843
1896
  validateRootfsManifest,
1844
1897
  verifyRootfsExists,
1845
1898
  verifyUcGoPeerReachability,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/core",
3
- "version": "0.1.33",
3
+ "version": "0.1.35",
4
4
  "description": "Reusable Aleph deployment, runtime, and guest lifecycle logic.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,6 +16,6 @@
16
16
  "access": "public"
17
17
  },
18
18
  "dependencies": {
19
- "@le-space/shared-types": "0.1.33"
19
+ "@le-space/shared-types": "0.1.35"
20
20
  }
21
21
  }