@mark1russell7/market-sources 0.2.7 → 0.2.8
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 +31 -2
- package/build/index.d.ts +182 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +629 -15
- package/build/index.js.map +1 -1
- package/package.json +1 -1
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$
|
|
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$
|
|
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$
|
|
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$
|
|
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,427 @@ 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 value of the paging parameter of the page after an answer, or null when the answer is the
|
|
1995
|
+
* last page. `page` is the number of the page of the answer, from 1.
|
|
1996
|
+
*/
|
|
1997
|
+
function planNextPage(read, answer, page) {
|
|
1998
|
+
const { paging } = read;
|
|
1999
|
+
if (paging.kind === "none") return null;
|
|
2000
|
+
if (paging.kind === "page") {
|
|
2001
|
+
const pages = wholeNumber(dig(answer, paging.pages));
|
|
2002
|
+
return pages !== void 0 && page < pages ? String(page + 1) : null;
|
|
2003
|
+
}
|
|
2004
|
+
return text(dig(answer, paging.next)) ?? null;
|
|
2005
|
+
}
|
|
2006
|
+
function itemsOf(pages, read, what) {
|
|
2007
|
+
const items = [];
|
|
2008
|
+
for (const [index, page] of pages.entries()) {
|
|
2009
|
+
const found = list(dig(page, read.items));
|
|
2010
|
+
if (found === void 0) return {
|
|
2011
|
+
ok: false,
|
|
2012
|
+
why: `The page ${index + 1} of the ${what} has no list at ${show(read.items)}.`
|
|
2013
|
+
};
|
|
2014
|
+
items.push(...found);
|
|
2015
|
+
}
|
|
2016
|
+
return {
|
|
2017
|
+
ok: true,
|
|
2018
|
+
value: items
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
/** This function gives the available regions of each plan id from the rows of a list of regions. */
|
|
2022
|
+
function availability(rows, regions, notes) {
|
|
2023
|
+
const map = /* @__PURE__ */ new Map();
|
|
2024
|
+
let bad = 0;
|
|
2025
|
+
let off = 0;
|
|
2026
|
+
for (const row of rows) {
|
|
2027
|
+
const plan = text(dig(row, regions.plan));
|
|
2028
|
+
const region = text(dig(row, regions.region));
|
|
2029
|
+
const available = dig(row, regions.available);
|
|
2030
|
+
if (plan === void 0 || region === void 0 || typeof available !== "boolean") {
|
|
2031
|
+
bad += 1;
|
|
2032
|
+
continue;
|
|
2033
|
+
}
|
|
2034
|
+
if (!available) {
|
|
2035
|
+
off += 1;
|
|
2036
|
+
continue;
|
|
2037
|
+
}
|
|
2038
|
+
const found = map.get(plan);
|
|
2039
|
+
if (found === void 0) map.set(plan, [region]);
|
|
2040
|
+
else if (!found.includes(region)) found.push(region);
|
|
2041
|
+
}
|
|
2042
|
+
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.`);
|
|
2043
|
+
if (off > 0) notes.push(`${off} pairs of a plan and a region are not available.`);
|
|
2044
|
+
return map;
|
|
2045
|
+
}
|
|
2046
|
+
function regionsOfItem(item, at) {
|
|
2047
|
+
const ids = (list(dig(item, at)) ?? []).map(text).filter((id) => id !== void 0);
|
|
2048
|
+
return [...new Set(ids)];
|
|
2049
|
+
}
|
|
2050
|
+
/** This function gives the price object of one region of a plan, or undefined when the plan has no price for that region. */
|
|
2051
|
+
function regionPrice(item, prices, region) {
|
|
2052
|
+
if (prices.kind === "none") return void 0;
|
|
2053
|
+
const found = dig(item, prices.path);
|
|
2054
|
+
if (prices.kind === "map") return isRecord(found) ? field(found, region) : void 0;
|
|
2055
|
+
return (list(found) ?? []).find((row) => text(dig(row, prices.id)) === region);
|
|
2056
|
+
}
|
|
2057
|
+
/** 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. */
|
|
2058
|
+
function priceOf(item, region, itemPath, regionPath) {
|
|
2059
|
+
if (itemPath === void 0) return void 0;
|
|
2060
|
+
return (region === void 0 ? void 0 : dig(region, regionPath ?? itemPath)) ?? dig(item, itemPath);
|
|
2061
|
+
}
|
|
2062
|
+
/** This function gives the meter of an hourly price with a monthly cap. Above the cap, an hour has no price. */
|
|
2063
|
+
function computeMeter(currency, hourly, monthly) {
|
|
2064
|
+
const unit = canonicalUnit(`${currency}/h`);
|
|
2065
|
+
const tiers = [{
|
|
2066
|
+
from: {
|
|
2067
|
+
value: 0,
|
|
2068
|
+
unit: "h"
|
|
2069
|
+
},
|
|
2070
|
+
unitPrice: {
|
|
2071
|
+
value: hourly,
|
|
2072
|
+
unit
|
|
2073
|
+
}
|
|
2074
|
+
}];
|
|
2075
|
+
if (monthly !== void 0 && monthly > 0 && hourly > 0) {
|
|
2076
|
+
const cap = rationals.toNumber(rationals.div(rationals.fromNumber(monthly), rationals.fromNumber(hourly)));
|
|
2077
|
+
tiers.push({
|
|
2078
|
+
from: {
|
|
2079
|
+
value: cap,
|
|
2080
|
+
unit: "h"
|
|
2081
|
+
},
|
|
2082
|
+
unitPrice: {
|
|
2083
|
+
value: 0,
|
|
2084
|
+
unit
|
|
2085
|
+
}
|
|
2086
|
+
});
|
|
2087
|
+
}
|
|
2088
|
+
return {
|
|
2089
|
+
name: PLAN_METER,
|
|
2090
|
+
tiers
|
|
2091
|
+
};
|
|
2092
|
+
}
|
|
2093
|
+
function specsOf(item, provider, key) {
|
|
2094
|
+
const specs = [];
|
|
2095
|
+
for (const spec of provider.specs) {
|
|
2096
|
+
const value = dig(item, spec.path);
|
|
2097
|
+
const shown = typeof value === "number" && Number.isFinite(value) ? String(value) : text(value);
|
|
2098
|
+
if (shown === void 0) continue;
|
|
2099
|
+
specs.push({
|
|
2100
|
+
name: spec.name,
|
|
2101
|
+
value: spec.unit === void 0 ? shown : `${shown} ${spec.unit}`
|
|
2102
|
+
});
|
|
2103
|
+
}
|
|
2104
|
+
specs.push({
|
|
2105
|
+
name: PLAN_PRICING_SPEC,
|
|
2106
|
+
value: key
|
|
2107
|
+
});
|
|
2108
|
+
return specs;
|
|
2109
|
+
}
|
|
2110
|
+
/** This function reads one price term of one plan in one region. */
|
|
2111
|
+
function observationOf(provider, item, plan, term, region, at, notes) {
|
|
2112
|
+
const sourceId = `${plan.id}/${region}/${term.key}`;
|
|
2113
|
+
const id = offerId(provider.source, sourceId);
|
|
2114
|
+
const price = regionPrice(item, provider.regionPrices, region);
|
|
2115
|
+
const rawHourly = priceOf(item, price, term.hourly, term.regionHourly);
|
|
2116
|
+
const hourly = decimal(rawHourly);
|
|
2117
|
+
if (hourly === void 0) {
|
|
2118
|
+
notes.push(`The offer ${id} is skipped: its hourly price ${show(rawHourly)} is not a number of 0 or more.`);
|
|
2119
|
+
return;
|
|
2120
|
+
}
|
|
2121
|
+
const rawMonthly = priceOf(item, price, term.monthly, term.regionMonthly);
|
|
2122
|
+
const monthly = rawMonthly === void 0 || rawMonthly === null ? void 0 : decimal(rawMonthly);
|
|
2123
|
+
if (rawMonthly !== void 0 && rawMonthly !== null && monthly === void 0) {
|
|
2124
|
+
notes.push(`The offer ${id} is skipped: its monthly price ${show(rawMonthly)} is not a number of 0 or more.`);
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
return observeMetered({
|
|
2128
|
+
id,
|
|
2129
|
+
source: provider.source,
|
|
2130
|
+
sourceId,
|
|
2131
|
+
title: plan.title,
|
|
2132
|
+
location: {
|
|
2133
|
+
provider: provider.provider,
|
|
2134
|
+
region
|
|
2135
|
+
},
|
|
2136
|
+
term: term.term,
|
|
2137
|
+
specs: specsOf(item, provider, term.key)
|
|
2138
|
+
}, {
|
|
2139
|
+
offerId: id,
|
|
2140
|
+
at,
|
|
2141
|
+
period: {
|
|
2142
|
+
value: 1,
|
|
2143
|
+
unit: "mo"
|
|
2144
|
+
},
|
|
2145
|
+
meters: [computeMeter(provider.currency, hourly, monthly)]
|
|
2146
|
+
}, notes);
|
|
2147
|
+
}
|
|
2148
|
+
/**
|
|
2149
|
+
* This function reads the answers of a plan list at the instant `at`. It gives one metered offer for each plan,
|
|
2150
|
+
* available region and price term. A provider with regions of the kind `list` needs the pages of that list. The
|
|
2151
|
+
* result has no link of a next page: the client reads each page of a list before the parse.
|
|
2152
|
+
*/
|
|
2153
|
+
function parsePlanLists(provider, answers, at) {
|
|
2154
|
+
checkAt(at);
|
|
2155
|
+
const notes = [];
|
|
2156
|
+
const items = itemsOf(answers.plans, provider.plans, "plan list");
|
|
2157
|
+
if (!items.ok) return refused(items.why);
|
|
2158
|
+
const { regions } = provider;
|
|
2159
|
+
let available;
|
|
2160
|
+
if (regions.kind === "list") {
|
|
2161
|
+
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.`);
|
|
2162
|
+
const rows = itemsOf(answers.regions, regions.read, "list of regions");
|
|
2163
|
+
if (!rows.ok) return refused(rows.why);
|
|
2164
|
+
available = availability(rows.value, regions, notes);
|
|
2165
|
+
}
|
|
2166
|
+
const observations = [];
|
|
2167
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2168
|
+
for (const [index, item] of items.value.entries()) {
|
|
2169
|
+
const planId = text(dig(item, provider.id));
|
|
2170
|
+
if (planId === void 0) {
|
|
2171
|
+
notes.push(`The plan ${index + 1} is skipped: it has no text at ${show(provider.id)}.`);
|
|
2172
|
+
continue;
|
|
2173
|
+
}
|
|
2174
|
+
const label = provider.label === void 0 ? void 0 : text(dig(item, provider.label));
|
|
2175
|
+
const plan = {
|
|
2176
|
+
id: planId,
|
|
2177
|
+
title: `${provider.name} ${label ?? planId}`
|
|
2178
|
+
};
|
|
2179
|
+
const places = regions.kind === "field" ? regionsOfItem(item, regions.path) : available?.get(planId) ?? [];
|
|
2180
|
+
if (places.length === 0) {
|
|
2181
|
+
notes.push(`The plan ${planId} has no offer: it has no available region.`);
|
|
2182
|
+
continue;
|
|
2183
|
+
}
|
|
2184
|
+
for (const term of provider.prices) {
|
|
2185
|
+
if (term.when !== void 0 && dig(item, term.when) !== true) continue;
|
|
2186
|
+
for (const region of places) {
|
|
2187
|
+
const observation = observationOf(provider, item, plan, term, region, at, notes);
|
|
2188
|
+
if (observation === void 0 || seen.has(observation.offer.id)) continue;
|
|
2189
|
+
seen.add(observation.offer.id);
|
|
2190
|
+
observations.push(observation);
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
return {
|
|
2195
|
+
ok: true,
|
|
2196
|
+
observations,
|
|
2197
|
+
notes,
|
|
2198
|
+
next: null
|
|
2199
|
+
};
|
|
2200
|
+
}
|
|
2201
|
+
// src/plans-client.ts
|
|
2202
|
+
/** The most pages of one list in one read. */
|
|
2203
|
+
const PLAN_PAGE_LIMIT = 20;
|
|
2204
|
+
function checkPages(pages) {
|
|
2205
|
+
if (!Number.isInteger(pages) || pages < 1 || pages > 20) throw new RangeError(`The count of pages is not a whole number from 1 to 20.`);
|
|
2206
|
+
return pages;
|
|
2207
|
+
}
|
|
2208
|
+
/**
|
|
2209
|
+
* This function makes a client of the plan lists of providers. Each address comes from the provider record. An
|
|
2210
|
+
* answer gives only the number or the cursor of the next page. A read refuses a list with more pages than the limit,
|
|
2211
|
+
* because a part of a list gives wrong regions.
|
|
2212
|
+
*/
|
|
2213
|
+
function createPlanClient(options) {
|
|
2214
|
+
const { pace, transport } = options;
|
|
2215
|
+
const readList = async (provider, read, what, limit, signal) => {
|
|
2216
|
+
const pages = [];
|
|
2217
|
+
let after;
|
|
2218
|
+
while (pages.length < limit) {
|
|
2219
|
+
const page = pages.length + 1;
|
|
2220
|
+
const exchange = {
|
|
2221
|
+
method: "GET",
|
|
2222
|
+
url: planListUrl(provider, read, after),
|
|
2223
|
+
headers: { accept: "application/json" },
|
|
2224
|
+
body: null
|
|
2225
|
+
};
|
|
2226
|
+
const input = {
|
|
2227
|
+
source: provider.name,
|
|
2228
|
+
host: provider.host,
|
|
2229
|
+
minIntervalMs: provider.paceMs,
|
|
2230
|
+
what: page === 1 ? what : `${what}, page ${page}`,
|
|
2231
|
+
secrets: [],
|
|
2232
|
+
redact,
|
|
2233
|
+
exchange
|
|
2234
|
+
};
|
|
2235
|
+
const json = readJson(await send(pace, transport, input, signal), input);
|
|
2236
|
+
if (!json.ok) return json;
|
|
2237
|
+
pages.push(json.json);
|
|
2238
|
+
const next = planNextPage(read, json.json, page);
|
|
2239
|
+
if (next === null) return {
|
|
2240
|
+
ok: true,
|
|
2241
|
+
pages
|
|
2242
|
+
};
|
|
2243
|
+
if (next === after) return refused(`The read stops: ${what} gives the cursor of its page ${page} again.`);
|
|
2244
|
+
after = next;
|
|
2245
|
+
}
|
|
2246
|
+
return refused(`The read stops: ${what} has more pages than the limit of ${limit}.`);
|
|
2247
|
+
};
|
|
2248
|
+
return { async plans(provider, readOptions = {}, signal) {
|
|
2249
|
+
const checked = parsePlanProvider(provider);
|
|
2250
|
+
if (!checked.ok) throw new RangeError(`The provider record is not valid: ${checked.problems.join(" ")}`);
|
|
2251
|
+
const limit = checkPages(readOptions.pages ?? 20);
|
|
2252
|
+
const record = checked.provider;
|
|
2253
|
+
const plans = await readList(record, record.plans, "the plan list", limit, signal);
|
|
2254
|
+
if (!plans.ok) return plans;
|
|
2255
|
+
let regions;
|
|
2256
|
+
if (record.regions.kind === "list") {
|
|
2257
|
+
const read = await readList(record, record.regions.read, "the list of regions", limit, signal);
|
|
2258
|
+
if (!read.ok) return read;
|
|
2259
|
+
regions = read.pages;
|
|
2260
|
+
}
|
|
2261
|
+
return parsePlanLists(record, {
|
|
2262
|
+
plans: plans.pages,
|
|
2263
|
+
regions
|
|
2264
|
+
}, instantOf(pace.now()));
|
|
2265
|
+
} };
|
|
2266
|
+
}
|
|
1655
2267
|
// src/index.ts
|
|
1656
|
-
/** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure. */
|
|
2268
|
+
/** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure, then the plan lists. */
|
|
1657
2269
|
const SOURCE_TERMS = [
|
|
1658
2270
|
EBAY_TERMS,
|
|
1659
2271
|
BEST_BUY_TERMS,
|
|
1660
2272
|
DIGIKEY_TERMS,
|
|
1661
|
-
AZURE_TERMS
|
|
2273
|
+
AZURE_TERMS,
|
|
2274
|
+
LINODE_TERMS,
|
|
2275
|
+
VULTR_TERMS
|
|
1662
2276
|
];
|
|
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 };
|
|
2277
|
+
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
2278
|
|
|
1665
2279
|
//# sourceMappingURL=index.js.map
|