@mark1russell7/market-sources 0.2.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { instantSchema, meteredKindOf, meteredObservationSchema, moneySchema, offerId, offerSchema, parseGtin, priceReadingSchema } from "@mark1russell7/market";
1
+ import { instantSchema, meteredKindOf, meteredObservationSchema, moneySchema, offerId, offerSchema, parseGtin, priceReadingSchema, priceTermSchema, sourceSchema, sourceTermsSchema, textSchema } from "@mark1russell7/market";
2
2
  import { canonicalUnit, rationals } from "@mark1russell7/quantity";
3
3
  import { z } from "zod";
4
4
  import { redactSecrets } from "@mark1russell7/text/redact";
@@ -536,7 +536,7 @@ function meterOf(draft, meter, name, id, notes) {
536
536
  }))
537
537
  };
538
538
  }
539
- function observationOf(draft, at, notes) {
539
+ function observationOf$1(draft, at, notes) {
540
540
  const id = offerId(AZURE_SOURCE, draft.sourceId);
541
541
  const names = /* @__PURE__ */ new Map();
542
542
  for (const meter of draft.meters.values()) names.set(meter.meterName, (names.get(meter.meterName) ?? 0) + 1);
@@ -610,7 +610,7 @@ function parseAzureRetailPricePages(answers, at) {
610
610
  for (const row of rows) addRow(row, drafts, notes);
611
611
  const observations = [];
612
612
  for (const draft of drafts.values()) {
613
- const observation = observationOf(draft, at, notes);
613
+ const observation = observationOf$1(draft, at, notes);
614
614
  if (observation !== void 0) observations.push(observation);
615
615
  }
616
616
  const last = answers[answers.length - 1];
@@ -682,7 +682,7 @@ function availabilityOf(product) {
682
682
  if (orderable === "Available") return "in-stock";
683
683
  return orderable === void 0 && product["onlineAvailability"] === true ? "in-stock" : "unknown";
684
684
  }
685
- function specsOf$2(value) {
685
+ function specsOf$3(value) {
686
686
  const specs = [];
687
687
  for (const detail of list(value) ?? []) {
688
688
  const name = text(field(detail, "name"));
@@ -736,7 +736,7 @@ function mapProduct$1(product, where, at, notes) {
736
736
  format: "fixed-price",
737
737
  gtins: gtin === null ? [] : [gtin],
738
738
  partNumbers,
739
- specs: specsOf$2(product["details"])
739
+ specs: specsOf$3(product["details"])
740
740
  }, {
741
741
  offerId: id,
742
742
  at,
@@ -893,7 +893,7 @@ function sellerOf$1(variation) {
893
893
  ...name === void 0 ? {} : { name }
894
894
  };
895
895
  }
896
- function specsOf$1(product) {
896
+ function specsOf$2(product) {
897
897
  const specs = [];
898
898
  for (const parameter of list(product["Parameters"]) ?? []) {
899
899
  const name = text(field(parameter, "ParameterText"));
@@ -928,7 +928,7 @@ function mapProduct(product, where, currency, at, notes) {
928
928
  brand,
929
929
  partNumber
930
930
  }] : [];
931
- const specs = specsOf$1(product);
931
+ const specs = specsOf$2(product);
932
932
  const url = httpsUrl(product["ProductUrl"]);
933
933
  const observations = [];
934
934
  variations.forEach((variation, index) => {
@@ -1116,7 +1116,7 @@ function sellerOf(value) {
1116
1116
  rating
1117
1117
  });
1118
1118
  }
1119
- function specsOf(value) {
1119
+ function specsOf$1(value) {
1120
1120
  const specs = [];
1121
1121
  for (const aspect of list(value) ?? []) {
1122
1122
  const name = text(field(aspect, "name"));
@@ -1183,7 +1183,7 @@ function mapItem(item, where, at, notes) {
1183
1183
  endsAt,
1184
1184
  gtins: gtin === null ? [] : [gtin],
1185
1185
  partNumbers,
1186
- specs: specsOf(item["localizedAspects"])
1186
+ specs: specsOf$1(item["localizedAspects"])
1187
1187
  }, {
1188
1188
  offerId: id,
1189
1189
  at,
@@ -1228,6 +1228,207 @@ function parseEbaySearch(answer, at) {
1228
1228
  });
1229
1229
  return done(observations, notes);
1230
1230
  }
1231
+ // src/providers.ts
1232
+ /** The name of the Vultr source. */
1233
+ const VULTR_SOURCE = "vultr";
1234
+ /** The terms of the Vultr source. A reading serves one request. */
1235
+ const VULTR_TERMS = {
1236
+ source: VULTR_SOURCE,
1237
+ retention: { kind: "no-store" },
1238
+ deleteWhenEnded: false,
1239
+ priceModel: false,
1240
+ clause: "Not read: the terms pages of vultr.com refused an automated read on 2026-11-18. The strictest rule holds until a person reads the terms."
1241
+ };
1242
+ /**
1243
+ * The record of the Vultr plan list (`GET /v2/plans` of the API v2). The API gave the list with no key on
1244
+ * 2026-11-18. Each plan lists its regions, and the object `location_cost` has the prices that change with the region.
1245
+ * A plan has the on-demand term when `deploy_ondemand` is true, and the preemptible term when `deploy_preemptible` is true.
1246
+ */
1247
+ const VULTR_PLANS = {
1248
+ source: VULTR_SOURCE,
1249
+ provider: "vultr",
1250
+ name: "Vultr",
1251
+ host: "api.vultr.com",
1252
+ paceMs: 2e3,
1253
+ currency: "USD",
1254
+ terms: VULTR_TERMS,
1255
+ plans: {
1256
+ path: "/v2/plans",
1257
+ query: { per_page: "500" },
1258
+ items: "plans",
1259
+ paging: {
1260
+ kind: "cursor",
1261
+ param: "cursor",
1262
+ next: "meta.links.next"
1263
+ }
1264
+ },
1265
+ id: "id",
1266
+ regions: {
1267
+ kind: "field",
1268
+ path: "locations"
1269
+ },
1270
+ regionPrices: {
1271
+ kind: "map",
1272
+ path: "location_cost"
1273
+ },
1274
+ prices: [{
1275
+ key: "on-demand",
1276
+ term: { kind: "on-demand" },
1277
+ when: "deploy_ondemand",
1278
+ hourly: "hourly_cost",
1279
+ monthly: "monthly_cost"
1280
+ }, {
1281
+ key: "spot",
1282
+ term: { kind: "spot" },
1283
+ when: "deploy_preemptible",
1284
+ hourly: "hourly_cost_preemptible",
1285
+ monthly: "monthly_cost_preemptible"
1286
+ }],
1287
+ specs: [
1288
+ {
1289
+ name: "type",
1290
+ path: "type"
1291
+ },
1292
+ {
1293
+ name: "vcpu",
1294
+ path: "vcpu_count"
1295
+ },
1296
+ {
1297
+ name: "memory",
1298
+ path: "ram",
1299
+ unit: "MiB"
1300
+ },
1301
+ {
1302
+ name: "disk",
1303
+ path: "disk",
1304
+ unit: "GB"
1305
+ },
1306
+ {
1307
+ name: "disk-type",
1308
+ path: "disk_type"
1309
+ },
1310
+ {
1311
+ name: "transfer",
1312
+ path: "bandwidth",
1313
+ unit: "GB"
1314
+ },
1315
+ {
1316
+ name: "cpu-vendor",
1317
+ path: "cpu_vendor"
1318
+ },
1319
+ {
1320
+ name: "gpu",
1321
+ path: "gpu_type"
1322
+ },
1323
+ {
1324
+ name: "gpu-count",
1325
+ path: "gpu_count"
1326
+ },
1327
+ {
1328
+ name: "gpu-memory",
1329
+ path: "gpu_vram_gb",
1330
+ unit: "GB"
1331
+ }
1332
+ ]
1333
+ };
1334
+ /** The name of the Linode source of Akamai Cloud. */
1335
+ const LINODE_SOURCE = "linode";
1336
+ /** The terms of the Linode source. A reading serves one request. */
1337
+ const LINODE_TERMS = {
1338
+ source: LINODE_SOURCE,
1339
+ retention: { kind: "no-store" },
1340
+ deleteWhenEnded: false,
1341
+ priceModel: false,
1342
+ clause: "Not read: the legal pages of akamai.com refused an automated read on 2026-11-18. The API reference tells that these reads need no permissions. The strictest rule holds until a person reads the terms."
1343
+ };
1344
+ const LINODE_PAGES = {
1345
+ kind: "page",
1346
+ param: "page",
1347
+ pages: "pages"
1348
+ };
1349
+ /**
1350
+ * The record of the Linode plan list of Akamai Cloud (`GET /v4/linode/types` of the API v4). The API reference
1351
+ * tells that the read needs no permissions. The list of `GET /v4/regions/availability` tells the regions of each
1352
+ * plan. The list `region_prices` has the prices that change with the region.
1353
+ */
1354
+ const LINODE_PLANS = {
1355
+ source: LINODE_SOURCE,
1356
+ provider: "linode",
1357
+ name: "Linode",
1358
+ host: "api.linode.com",
1359
+ paceMs: 2e3,
1360
+ currency: "USD",
1361
+ terms: LINODE_TERMS,
1362
+ plans: {
1363
+ path: "/v4/linode/types",
1364
+ query: { page_size: "500" },
1365
+ items: "data",
1366
+ paging: LINODE_PAGES
1367
+ },
1368
+ id: "id",
1369
+ label: "label",
1370
+ regions: {
1371
+ kind: "list",
1372
+ read: {
1373
+ path: "/v4/regions/availability",
1374
+ query: { page_size: "500" },
1375
+ items: "data",
1376
+ paging: LINODE_PAGES
1377
+ },
1378
+ plan: "plan",
1379
+ region: "region",
1380
+ available: "available"
1381
+ },
1382
+ regionPrices: {
1383
+ kind: "list",
1384
+ path: "region_prices",
1385
+ id: "id"
1386
+ },
1387
+ prices: [{
1388
+ key: "on-demand",
1389
+ term: { kind: "on-demand" },
1390
+ hourly: "price.hourly",
1391
+ monthly: "price.monthly",
1392
+ regionHourly: "hourly",
1393
+ regionMonthly: "monthly"
1394
+ }],
1395
+ specs: [
1396
+ {
1397
+ name: "class",
1398
+ path: "class"
1399
+ },
1400
+ {
1401
+ name: "vcpu",
1402
+ path: "vcpus"
1403
+ },
1404
+ {
1405
+ name: "memory",
1406
+ path: "memory",
1407
+ unit: "MiB"
1408
+ },
1409
+ {
1410
+ name: "disk",
1411
+ path: "disk",
1412
+ unit: "MiB"
1413
+ },
1414
+ {
1415
+ name: "transfer",
1416
+ path: "transfer",
1417
+ unit: "GB"
1418
+ },
1419
+ {
1420
+ name: "gpu-count",
1421
+ path: "gpus"
1422
+ },
1423
+ {
1424
+ name: "network-out",
1425
+ path: "network_out",
1426
+ unit: "Mbps"
1427
+ }
1428
+ ]
1429
+ };
1430
+ /** The record of each provider with a plan list, in the order of the names. */
1431
+ const PLAN_PROVIDERS = [LINODE_PLANS, VULTR_PLANS];
1231
1432
  // src/client.ts
1232
1433
  /**
1233
1434
  * The shared parts of the clients of the seller APIs. Each client sends its exchanges with the polite send of
@@ -1532,7 +1733,7 @@ const AZURE_PACE_MS = 6e3;
1532
1733
  /** The most pages of one read. Each page has 1,000 prices at most. */
1533
1734
  const AZURE_PAGE_LIMIT = 10;
1534
1735
  const SOURCE$1 = "Azure Retail Prices";
1535
- function checkPages(pages) {
1736
+ function checkPages$1(pages) {
1536
1737
  if (!Number.isInteger(pages) || pages < 1 || pages > 10) throw new RangeError(`The count of pages is not a whole number from 1 to 10.`);
1537
1738
  return pages;
1538
1739
  }
@@ -1572,11 +1773,11 @@ function createAzureClient(options) {
1572
1773
  return {
1573
1774
  async prices(filter, readOptions = {}, signal) {
1574
1775
  const url = azurePricesUrl(filter, readOptions.currency);
1575
- return read(url, checkPages(readOptions.pages ?? 1), "the prices of a filter", signal);
1776
+ return read(url, checkPages$1(readOptions.pages ?? 1), "the prices of a filter", signal);
1576
1777
  },
1577
1778
  async more(next, pages = 1, signal) {
1578
1779
  if (typeof next !== "string" || !isAzurePricesUrl(next)) throw new RangeError("The link is not a link of the Azure Retail Prices API.");
1579
- return read(next, checkPages(pages), "the next page of a read", signal);
1780
+ return read(next, checkPages$1(pages), "the next page of a read", signal);
1580
1781
  }
1581
1782
  };
1582
1783
  }
@@ -1652,14 +1853,451 @@ function createBestBuyClient(options) {
1652
1853
  }
1653
1854
  };
1654
1855
  }
1856
+ // src/plans.ts
1857
+ /**
1858
+ * The generic parser of the plan lists of cloud providers. A plan list is an API that gives each plan of virtual
1859
+ * machines with its hourly price, its monthly cap and its regions. A `PlanProvider` record tells where the API keeps
1860
+ * each value. Thus a new provider is a record of data, and not new code.
1861
+ *
1862
+ * The parser gives one metered offer for each plan, region and price term. The meter `compute` counts the hours of
1863
+ * a month: the hourly price up to the monthly cap, then no price.
1864
+ */
1865
+ /** The name of the meter of each plan offer. It counts the hours of use in one month. */
1866
+ const PLAN_METER = "compute";
1867
+ /** The name of the spec that the parser adds to each offer. Its value is the key of the price term. */
1868
+ const PLAN_PRICING_SPEC = "pricing";
1869
+ const dottedSchema = z.string().regex(/^[A-Za-z0-9_$-]+(\.[A-Za-z0-9_$-]+)*$/, "The path has names of letters, digits, _, $ and -, with a dot between two names.");
1870
+ const paramSchema = z.string().regex(/^[A-Za-z0-9_.-]+$/, "The name of a query parameter has letters, digits, _, . and -.");
1871
+ const pagingSchema = z.discriminatedUnion("kind", [
1872
+ z.strictObject({ kind: z.literal("none") }),
1873
+ z.strictObject({
1874
+ kind: z.literal("page"),
1875
+ param: paramSchema,
1876
+ pages: dottedSchema
1877
+ }),
1878
+ z.strictObject({
1879
+ kind: z.literal("cursor"),
1880
+ param: paramSchema,
1881
+ next: dottedSchema
1882
+ })
1883
+ ]);
1884
+ const listReadSchema = z.strictObject({
1885
+ path: z.string().regex(/^\/[A-Za-z0-9._~/-]*$/, "The path of a list starts with a slash, and it has no query."),
1886
+ query: z.record(paramSchema, z.string()),
1887
+ items: dottedSchema,
1888
+ paging: pagingSchema
1889
+ });
1890
+ const termSchema = z.strictObject({
1891
+ key: sourceSchema,
1892
+ term: priceTermSchema,
1893
+ when: dottedSchema.optional(),
1894
+ hourly: dottedSchema,
1895
+ monthly: dottedSchema.optional(),
1896
+ regionHourly: dottedSchema.optional(),
1897
+ regionMonthly: dottedSchema.optional()
1898
+ });
1899
+ /**
1900
+ * This schema accepts the record of a provider with a plan list. The terms are the terms of the source of the
1901
+ * record. Each price term has its own key, and each spec has its own name. The name `pricing` is for the parser.
1902
+ */
1903
+ const planProviderSchema = z.strictObject({
1904
+ source: sourceSchema,
1905
+ provider: textSchema,
1906
+ name: textSchema,
1907
+ host: z.string().regex(/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/, "The host is a DNS name with small letters, digits, hyphens and dots."),
1908
+ paceMs: z.number().int().min(1),
1909
+ currency: z.string().refine(isCurrency, "The currency is not a money unit of @mark1russell7/quantity."),
1910
+ terms: sourceTermsSchema,
1911
+ plans: listReadSchema,
1912
+ id: dottedSchema,
1913
+ label: dottedSchema.optional(),
1914
+ regions: z.discriminatedUnion("kind", [z.strictObject({
1915
+ kind: z.literal("field"),
1916
+ path: dottedSchema
1917
+ }), z.strictObject({
1918
+ kind: z.literal("list"),
1919
+ read: listReadSchema,
1920
+ plan: dottedSchema,
1921
+ region: dottedSchema,
1922
+ available: dottedSchema
1923
+ })]),
1924
+ regionPrices: z.discriminatedUnion("kind", [
1925
+ z.strictObject({ kind: z.literal("none") }),
1926
+ z.strictObject({
1927
+ kind: z.literal("map"),
1928
+ path: dottedSchema
1929
+ }),
1930
+ z.strictObject({
1931
+ kind: z.literal("list"),
1932
+ path: dottedSchema,
1933
+ id: dottedSchema
1934
+ })
1935
+ ]),
1936
+ prices: z.array(termSchema).min(1),
1937
+ specs: z.array(z.strictObject({
1938
+ name: textSchema,
1939
+ path: dottedSchema,
1940
+ unit: textSchema.optional()
1941
+ }))
1942
+ }).superRefine((record, context) => {
1943
+ if (record.terms.source !== record.source) context.addIssue({
1944
+ code: "custom",
1945
+ message: `The terms are the terms of ${record.terms.source}, and not of ${record.source}.`,
1946
+ path: ["terms", "source"]
1947
+ });
1948
+ const keys = record.prices.map((price) => price.key);
1949
+ if (new Set(keys).size !== keys.length) context.addIssue({
1950
+ code: "custom",
1951
+ message: "Two price terms have one key.",
1952
+ path: ["prices"]
1953
+ });
1954
+ const names = record.specs.map((spec) => spec.name);
1955
+ if (new Set(names).size !== names.length) context.addIssue({
1956
+ code: "custom",
1957
+ message: "Two specs have one name.",
1958
+ path: ["specs"]
1959
+ });
1960
+ if (names.includes("pricing")) context.addIssue({
1961
+ code: "custom",
1962
+ message: `The spec name ${PLAN_PRICING_SPEC} is for the parser.`,
1963
+ path: ["specs"]
1964
+ });
1965
+ });
1966
+ /** This function examines a provider record, for example a record from a JSON file. */
1967
+ function parsePlanProvider(value) {
1968
+ const checked = planProviderSchema.safeParse(value);
1969
+ if (checked.success) return {
1970
+ ok: true,
1971
+ provider: checked.data
1972
+ };
1973
+ return {
1974
+ ok: false,
1975
+ problems: checked.error.issues.map((issue) => `${issue.path.join(".") || "record"}: ${issue.message}`)
1976
+ };
1977
+ }
1978
+ /** This function gives the value at a path of names with dots, or undefined. */
1979
+ function dig(value, dotted) {
1980
+ return path(value, ...dotted.split("."));
1981
+ }
1982
+ /**
1983
+ * This function gives the address of one page of a list of a provider. `after` is the value of the paging parameter
1984
+ * of the page. Without it, the address is the address of the first page.
1985
+ */
1986
+ function planListUrl(provider, read, after) {
1987
+ const { paging } = read;
1988
+ const pairs = Object.entries(read.query).filter(([name]) => paging.kind === "none" || after === void 0 || name !== paging.param);
1989
+ if (after !== void 0 && paging.kind !== "none") pairs.push([paging.param, after]);
1990
+ const search = pairs.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join("&");
1991
+ return `https://${provider.host}${read.path}${search === "" ? "" : `?${search}`}`;
1992
+ }
1993
+ /**
1994
+ * This function gives the page after an answer. `page` is the number of the page of the answer, from 1. A paging
1995
+ * field that is missing or of a wrong type gives a fault, not the end. A part of a list gives wrong offers. A cursor
1996
+ * of empty text or null is the end.
1997
+ */
1998
+ function planNextPage(read, answer, page) {
1999
+ const { paging } = read;
2000
+ if (paging.kind === "none") return { kind: "end" };
2001
+ if (paging.kind === "page") {
2002
+ const raw = dig(answer, paging.pages);
2003
+ const pages = wholeNumber(raw);
2004
+ if (pages === void 0) return {
2005
+ kind: "fault",
2006
+ why: `The count of pages ${show(raw)} at ${show(paging.pages)} is not a whole number.`
2007
+ };
2008
+ return page < pages ? {
2009
+ kind: "next",
2010
+ after: String(page + 1)
2011
+ } : { kind: "end" };
2012
+ }
2013
+ const raw = dig(answer, paging.next);
2014
+ if (raw === null || typeof raw === "string" && raw.trim() === "") return { kind: "end" };
2015
+ if (typeof raw !== "string") return {
2016
+ kind: "fault",
2017
+ why: `The cursor ${show(raw)} at ${show(paging.next)} is not text or null.`
2018
+ };
2019
+ return {
2020
+ kind: "next",
2021
+ after: raw
2022
+ };
2023
+ }
2024
+ function itemsOf(pages, read, what) {
2025
+ const items = [];
2026
+ for (const [index, page] of pages.entries()) {
2027
+ const found = list(dig(page, read.items));
2028
+ if (found === void 0) return {
2029
+ ok: false,
2030
+ why: `The page ${index + 1} of the ${what} has no list at ${show(read.items)}.`
2031
+ };
2032
+ items.push(...found);
2033
+ }
2034
+ return {
2035
+ ok: true,
2036
+ value: items
2037
+ };
2038
+ }
2039
+ /** This function gives the available regions of each plan id from the rows of a list of regions. */
2040
+ function availability(rows, regions, notes) {
2041
+ const map = /* @__PURE__ */ new Map();
2042
+ let bad = 0;
2043
+ let off = 0;
2044
+ for (const row of rows) {
2045
+ const plan = text(dig(row, regions.plan));
2046
+ const region = text(dig(row, regions.region));
2047
+ const available = dig(row, regions.available);
2048
+ if (plan === void 0 || region === void 0 || typeof available !== "boolean") {
2049
+ bad += 1;
2050
+ continue;
2051
+ }
2052
+ if (!available) {
2053
+ off += 1;
2054
+ continue;
2055
+ }
2056
+ const found = map.get(plan);
2057
+ if (found === void 0) map.set(plan, [region]);
2058
+ else if (!found.includes(region)) found.push(region);
2059
+ }
2060
+ if (bad > 0) notes.push(`${bad} rows of the list of regions are skipped: each has no plan, no region or no ${show(regions.available)} of true or false.`);
2061
+ if (off > 0) notes.push(`${off} pairs of a plan and a region are not available.`);
2062
+ return map;
2063
+ }
2064
+ function regionsOfItem(item, at) {
2065
+ const ids = (list(dig(item, at)) ?? []).map(text).filter((id) => id !== void 0);
2066
+ return [...new Set(ids)];
2067
+ }
2068
+ /** This function gives the price object of one region of a plan, or undefined when the plan has no price for that region. */
2069
+ function regionPrice(item, prices, region) {
2070
+ if (prices.kind === "none") return void 0;
2071
+ const found = dig(item, prices.path);
2072
+ if (prices.kind === "map") return isRecord(found) ? field(found, region) : void 0;
2073
+ return (list(found) ?? []).find((row) => text(dig(row, prices.id)) === region);
2074
+ }
2075
+ /** This function reads a price: the value at the path of the price of the region, else the value at the path of the plan. */
2076
+ function priceOf(item, region, itemPath, regionPath) {
2077
+ if (itemPath === void 0) return void 0;
2078
+ return (region === void 0 ? void 0 : dig(region, regionPath ?? itemPath)) ?? dig(item, itemPath);
2079
+ }
2080
+ /** This function gives the meter of an hourly price with a monthly cap. Above the cap, an hour has no price. */
2081
+ function computeMeter(currency, hourly, monthly) {
2082
+ const unit = canonicalUnit(`${currency}/h`);
2083
+ const tiers = [{
2084
+ from: {
2085
+ value: 0,
2086
+ unit: "h"
2087
+ },
2088
+ unitPrice: {
2089
+ value: hourly,
2090
+ unit
2091
+ }
2092
+ }];
2093
+ if (monthly !== void 0 && monthly > 0 && hourly > 0) {
2094
+ const cap = rationals.toNumber(rationals.div(rationals.fromNumber(monthly), rationals.fromNumber(hourly)));
2095
+ tiers.push({
2096
+ from: {
2097
+ value: cap,
2098
+ unit: "h"
2099
+ },
2100
+ unitPrice: {
2101
+ value: 0,
2102
+ unit
2103
+ }
2104
+ });
2105
+ }
2106
+ return {
2107
+ name: PLAN_METER,
2108
+ tiers
2109
+ };
2110
+ }
2111
+ function specsOf(item, provider, key) {
2112
+ const specs = [];
2113
+ for (const spec of provider.specs) {
2114
+ const value = dig(item, spec.path);
2115
+ const shown = typeof value === "number" && Number.isFinite(value) ? String(value) : text(value);
2116
+ if (shown === void 0) continue;
2117
+ specs.push({
2118
+ name: spec.name,
2119
+ value: spec.unit === void 0 ? shown : `${shown} ${spec.unit}`
2120
+ });
2121
+ }
2122
+ specs.push({
2123
+ name: PLAN_PRICING_SPEC,
2124
+ value: key
2125
+ });
2126
+ return specs;
2127
+ }
2128
+ /** This function reads one price term of one plan in one region. */
2129
+ function observationOf(provider, item, plan, term, region, at, notes) {
2130
+ const sourceId = `${plan.id}/${region}/${term.key}`;
2131
+ const id = offerId(provider.source, sourceId);
2132
+ const price = regionPrice(item, provider.regionPrices, region);
2133
+ const rawHourly = priceOf(item, price, term.hourly, term.regionHourly);
2134
+ const hourly = decimal(rawHourly);
2135
+ if (hourly === void 0) {
2136
+ notes.push(`The offer ${id} is skipped: its hourly price ${show(rawHourly)} is not a number of 0 or more.`);
2137
+ return;
2138
+ }
2139
+ const rawMonthly = priceOf(item, price, term.monthly, term.regionMonthly);
2140
+ const monthly = rawMonthly === void 0 || rawMonthly === null ? void 0 : decimal(rawMonthly);
2141
+ if (rawMonthly !== void 0 && rawMonthly !== null && monthly === void 0) {
2142
+ notes.push(`The offer ${id} is skipped: its monthly price ${show(rawMonthly)} is not a number of 0 or more.`);
2143
+ return;
2144
+ }
2145
+ if (hourly === 0 && monthly !== void 0 && monthly > 0) {
2146
+ notes.push(`The offer ${id} is skipped: its hourly price is 0 and its monthly price is ${String(monthly)}.`);
2147
+ return;
2148
+ }
2149
+ if (hourly > 0 && monthly === 0) notes.push(`The offer ${id} has no monthly cap: its monthly price is 0 and its hourly price is ${String(hourly)}.`);
2150
+ return observeMetered({
2151
+ id,
2152
+ source: provider.source,
2153
+ sourceId,
2154
+ title: plan.title,
2155
+ location: {
2156
+ provider: provider.provider,
2157
+ region
2158
+ },
2159
+ term: term.term,
2160
+ specs: specsOf(item, provider, term.key)
2161
+ }, {
2162
+ offerId: id,
2163
+ at,
2164
+ period: {
2165
+ value: 1,
2166
+ unit: "mo"
2167
+ },
2168
+ meters: [computeMeter(provider.currency, hourly, monthly)]
2169
+ }, notes);
2170
+ }
2171
+ /**
2172
+ * This function reads the answers of a plan list at the instant `at`. It gives one metered offer for each plan,
2173
+ * available region and price term. A provider with regions of the kind `list` needs the pages of that list. The
2174
+ * result has no link of a next page: the client reads each page of a list before the parse.
2175
+ */
2176
+ function parsePlanLists(provider, answers, at) {
2177
+ checkAt(at);
2178
+ const notes = [];
2179
+ const items = itemsOf(answers.plans, provider.plans, "plan list");
2180
+ if (!items.ok) return refused(items.why);
2181
+ const { regions } = provider;
2182
+ let available;
2183
+ if (regions.kind === "list") {
2184
+ if (answers.regions === void 0 || answers.regions.length === 0) return refused(`The provider ${provider.source} reads its regions from a list, and the answers have no page of that list.`);
2185
+ const rows = itemsOf(answers.regions, regions.read, "list of regions");
2186
+ if (!rows.ok) return refused(rows.why);
2187
+ available = availability(rows.value, regions, notes);
2188
+ }
2189
+ const observations = [];
2190
+ const seen = /* @__PURE__ */ new Set();
2191
+ for (const [index, item] of items.value.entries()) {
2192
+ const planId = text(dig(item, provider.id));
2193
+ if (planId === void 0) {
2194
+ notes.push(`The plan ${index + 1} is skipped: it has no text at ${show(provider.id)}.`);
2195
+ continue;
2196
+ }
2197
+ const label = provider.label === void 0 ? void 0 : text(dig(item, provider.label));
2198
+ const plan = {
2199
+ id: planId,
2200
+ title: `${provider.name} ${label ?? planId}`
2201
+ };
2202
+ const places = regions.kind === "field" ? regionsOfItem(item, regions.path) : available?.get(planId) ?? [];
2203
+ if (places.length === 0) {
2204
+ notes.push(`The plan ${planId} has no offer: it has no available region.`);
2205
+ continue;
2206
+ }
2207
+ for (const term of provider.prices) {
2208
+ if (term.when !== void 0 && dig(item, term.when) !== true) continue;
2209
+ for (const region of places) {
2210
+ const observation = observationOf(provider, item, plan, term, region, at, notes);
2211
+ if (observation === void 0 || seen.has(observation.offer.id)) continue;
2212
+ seen.add(observation.offer.id);
2213
+ observations.push(observation);
2214
+ }
2215
+ }
2216
+ }
2217
+ return {
2218
+ ok: true,
2219
+ observations,
2220
+ notes,
2221
+ next: null
2222
+ };
2223
+ }
2224
+ // src/plans-client.ts
2225
+ /** The most pages of one list in one read. */
2226
+ const PLAN_PAGE_LIMIT = 20;
2227
+ function checkPages(pages) {
2228
+ if (!Number.isInteger(pages) || pages < 1 || pages > 20) throw new RangeError(`The count of pages is not a whole number from 1 to 20.`);
2229
+ return pages;
2230
+ }
2231
+ /**
2232
+ * This function makes a client of the plan lists of providers. Each address comes from the provider record. An
2233
+ * answer gives only the number or the cursor of the next page. A read refuses a list with more pages than the limit,
2234
+ * because a part of a list gives wrong regions.
2235
+ */
2236
+ function createPlanClient(options) {
2237
+ const { pace, transport } = options;
2238
+ const readList = async (provider, read, what, limit, signal) => {
2239
+ const pages = [];
2240
+ let after;
2241
+ while (pages.length < limit) {
2242
+ const page = pages.length + 1;
2243
+ const exchange = {
2244
+ method: "GET",
2245
+ url: planListUrl(provider, read, after),
2246
+ headers: { accept: "application/json" },
2247
+ body: null
2248
+ };
2249
+ const input = {
2250
+ source: provider.name,
2251
+ host: provider.host,
2252
+ minIntervalMs: provider.paceMs,
2253
+ what: page === 1 ? what : `${what}, page ${page}`,
2254
+ secrets: [],
2255
+ redact,
2256
+ exchange
2257
+ };
2258
+ const json = readJson(await send(pace, transport, input, signal), input);
2259
+ if (!json.ok) return json;
2260
+ pages.push(json.json);
2261
+ const next = planNextPage(read, json.json, page);
2262
+ if (next.kind === "end") return {
2263
+ ok: true,
2264
+ pages
2265
+ };
2266
+ if (next.kind === "fault") return refused(`The read stops at the page ${page} of ${what}. ${next.why}`);
2267
+ if (next.after === after) return refused(`The read stops: ${what} gives the cursor of its page ${page} again.`);
2268
+ after = next.after;
2269
+ }
2270
+ return refused(`The read stops: ${what} has more pages than the limit of ${limit}.`);
2271
+ };
2272
+ return { async plans(provider, readOptions = {}, signal) {
2273
+ const checked = parsePlanProvider(provider);
2274
+ if (!checked.ok) throw new RangeError(`The provider record is not valid: ${checked.problems.join(" ")}`);
2275
+ const limit = checkPages(readOptions.pages ?? 20);
2276
+ const record = checked.provider;
2277
+ const plans = await readList(record, record.plans, "the plan list", limit, signal);
2278
+ if (!plans.ok) return plans;
2279
+ let regions;
2280
+ if (record.regions.kind === "list") {
2281
+ const read = await readList(record, record.regions.read, "the list of regions", limit, signal);
2282
+ if (!read.ok) return read;
2283
+ regions = read.pages;
2284
+ }
2285
+ return parsePlanLists(record, {
2286
+ plans: plans.pages,
2287
+ regions
2288
+ }, instantOf(pace.now()));
2289
+ } };
2290
+ }
1655
2291
  // src/index.ts
1656
- /** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure. */
2292
+ /** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure, then the plan lists. */
1657
2293
  const SOURCE_TERMS = [
1658
2294
  EBAY_TERMS,
1659
2295
  BEST_BUY_TERMS,
1660
2296
  DIGIKEY_TERMS,
1661
- AZURE_TERMS
2297
+ AZURE_TERMS,
2298
+ LINODE_TERMS,
2299
+ VULTR_TERMS
1662
2300
  ];
1663
- export { AZURE_API_VERSION, AZURE_FILTER_FIELDS, AZURE_HOST, AZURE_PACE_MS, AZURE_PAGE_LIMIT, AZURE_PRICES_URL, AZURE_PROVIDER, AZURE_SOURCE, AZURE_TERMS, ApiError, BEST_BUY_HOST, BEST_BUY_PACE_MS, BEST_BUY_PAGE_LIMIT, BEST_BUY_SOURCE, BEST_BUY_TERMS, DIGIKEY_HOST, DIGIKEY_PACE_MS, DIGIKEY_SANDBOX_HOST, DIGIKEY_SEARCH_LIMIT, DIGIKEY_SELLER, DIGIKEY_SOURCE, DIGIKEY_TERMS, DIGIKEY_US, EBAY_HOST, EBAY_PACE_MS, EBAY_SANDBOX_HOST, EBAY_SCOPE, EBAY_SEARCH_LIMIT, EBAY_SOURCE, EBAY_TERMS, EBAY_US, SECRET_MARK, SOURCE_TERMS, azureNextPage, azurePricesUrl, azureUnit, bestBuyCondition, createAzureClient, createBestBuyClient, createDigiKeyClient, createEbayClient, ebayCondition, isAzurePricesUrl, parseAzureRetailPricePages, parseAzureRetailPrices, parseBestBuyOpenBox, parseBestBuyProduct, parseBestBuyProducts, parseDigiKeyKeywordSearch, parseDigiKeyProductDetails, parseEbayItem, parseEbaySearch, redact };
2301
+ export { AZURE_API_VERSION, AZURE_FILTER_FIELDS, AZURE_HOST, AZURE_PACE_MS, AZURE_PAGE_LIMIT, AZURE_PRICES_URL, AZURE_PROVIDER, AZURE_SOURCE, AZURE_TERMS, ApiError, BEST_BUY_HOST, BEST_BUY_PACE_MS, BEST_BUY_PAGE_LIMIT, BEST_BUY_SOURCE, BEST_BUY_TERMS, DIGIKEY_HOST, DIGIKEY_PACE_MS, DIGIKEY_SANDBOX_HOST, DIGIKEY_SEARCH_LIMIT, DIGIKEY_SELLER, DIGIKEY_SOURCE, DIGIKEY_TERMS, DIGIKEY_US, EBAY_HOST, EBAY_PACE_MS, EBAY_SANDBOX_HOST, EBAY_SCOPE, EBAY_SEARCH_LIMIT, EBAY_SOURCE, EBAY_TERMS, EBAY_US, LINODE_PLANS, LINODE_SOURCE, LINODE_TERMS, PLAN_METER, PLAN_PAGE_LIMIT, PLAN_PRICING_SPEC, PLAN_PROVIDERS, SECRET_MARK, SOURCE_TERMS, VULTR_PLANS, VULTR_SOURCE, VULTR_TERMS, azureNextPage, azurePricesUrl, azureUnit, bestBuyCondition, createAzureClient, createBestBuyClient, createDigiKeyClient, createEbayClient, createPlanClient, ebayCondition, isAzurePricesUrl, parseAzureRetailPricePages, parseAzureRetailPrices, parseBestBuyOpenBox, parseBestBuyProduct, parseBestBuyProducts, parseDigiKeyKeywordSearch, parseDigiKeyProductDetails, parseEbayItem, parseEbaySearch, parsePlanLists, parsePlanProvider, planListUrl, planNextPage, planProviderSchema, redact };
1664
2302
 
1665
2303
  //# sourceMappingURL=index.js.map