@mark1russell7/market-sources 0.2.6 → 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 +68 -2
- package/build/index.d.ts +292 -5
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1178 -22
- package/build/index.js.map +1 -1
- package/package.json +5 -3
package/build/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { instantSchema, 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
|
+
import { canonicalUnit, rationals } from "@mark1russell7/quantity";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import { redactSecrets } from "@mark1russell7/text/redact";
|
|
4
5
|
import { ApiError, readGrant, send, sendWithToken, statusLine as statusLine$1, tokenStore } from "polite-crawler/api";
|
|
@@ -132,6 +133,21 @@ function observe(offer, reading, notes) {
|
|
|
132
133
|
reading: checkedReading.data
|
|
133
134
|
};
|
|
134
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* This function examines a metered offer and its reading with the schemas of `@mark1russell7/market`. It gives
|
|
138
|
+
* the observation, or it adds a note and gives undefined.
|
|
139
|
+
*/
|
|
140
|
+
function observeMetered(offer, reading, notes) {
|
|
141
|
+
const checked = meteredObservationSchema.safeParse({
|
|
142
|
+
offer: compact(offer),
|
|
143
|
+
reading: compact(reading)
|
|
144
|
+
});
|
|
145
|
+
if (!checked.success) {
|
|
146
|
+
notes.push(`The offer ${offer.id} is skipped: the schema refuses it (${issues(checked.error)}).`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
return checked.data;
|
|
150
|
+
}
|
|
135
151
|
/** This function gives a result with the observations. An offer that the answer repeats counts once. */
|
|
136
152
|
function done(observations, notes) {
|
|
137
153
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -152,6 +168,462 @@ function refused(...problems) {
|
|
|
152
168
|
problems
|
|
153
169
|
};
|
|
154
170
|
}
|
|
171
|
+
// src/azure.ts
|
|
172
|
+
/** The name of the Azure Retail Prices API as a source of metered offers. */
|
|
173
|
+
const AZURE_SOURCE = "azure";
|
|
174
|
+
/** The provider of the location of each offer of the Azure Retail Prices API. */
|
|
175
|
+
const AZURE_PROVIDER = "azure";
|
|
176
|
+
/**
|
|
177
|
+
* The terms of the Azure Retail Prices API. The API needs no key, and its documentation names tools for internal
|
|
178
|
+
* analysis and price comparison. The Microsoft APIs Terms of Use permit copies only for the use of the
|
|
179
|
+
* application, and they forbid redistribution and resale of the data.
|
|
180
|
+
*/
|
|
181
|
+
const AZURE_TERMS = {
|
|
182
|
+
source: AZURE_SOURCE,
|
|
183
|
+
retention: { kind: "keep" },
|
|
184
|
+
deleteWhenEnded: false,
|
|
185
|
+
priceModel: true,
|
|
186
|
+
clause: "Azure Retail Prices REST API overview, read on 2026-11-18, and Microsoft APIs Terms of Use of October 2025, sections 3(b)(4) and 3(b)(14): copies only as the application needs them, and no redistribution or resale"
|
|
187
|
+
};
|
|
188
|
+
/** The address of the Azure Retail Prices API. */
|
|
189
|
+
const AZURE_PRICES_URL = "https://prices.azure.com/api/retail/prices";
|
|
190
|
+
/** The version of the API that the request builder asks for. Only this version gives the prices of savings plans. */
|
|
191
|
+
const AZURE_API_VERSION = "2023-01-01-preview";
|
|
192
|
+
/** The fields of a filter of the Azure Retail Prices API, from its documentation. */
|
|
193
|
+
const AZURE_FILTER_FIELDS = [
|
|
194
|
+
"armRegionName",
|
|
195
|
+
"location",
|
|
196
|
+
"meterId",
|
|
197
|
+
"meterName",
|
|
198
|
+
"productId",
|
|
199
|
+
"skuId",
|
|
200
|
+
"productName",
|
|
201
|
+
"skuName",
|
|
202
|
+
"serviceName",
|
|
203
|
+
"serviceId",
|
|
204
|
+
"serviceFamily",
|
|
205
|
+
"priceType",
|
|
206
|
+
"armSkuName"
|
|
207
|
+
];
|
|
208
|
+
/** The longest value of a field of a filter, in code units. */
|
|
209
|
+
const FILTER_VALUE_LIMIT = 200;
|
|
210
|
+
/**
|
|
211
|
+
* This function gives the URL of a read of the Azure Retail Prices API with a filter. Each field of the filter
|
|
212
|
+
* gives the term `field eq 'value'`, and the terms join with `and`. Without a currency, the API gives US dollars.
|
|
213
|
+
*
|
|
214
|
+
* @throws RangeError when the filter has no field, or a field that the API does not filter. It also throws when a
|
|
215
|
+
* value is empty, has a space at an end, has a control character or has more than 200 characters. A currency that
|
|
216
|
+
* `@mark1russell7/quantity` does not have also gives a `RangeError`.
|
|
217
|
+
*/
|
|
218
|
+
function azurePricesUrl(filter, currency) {
|
|
219
|
+
const fields = AZURE_FILTER_FIELDS;
|
|
220
|
+
const other = Object.keys(filter).find((name) => !fields.includes(name));
|
|
221
|
+
if (other !== void 0) throw new RangeError(`The API does not filter the field ${show(other)}.`);
|
|
222
|
+
const terms = [];
|
|
223
|
+
for (const name of AZURE_FILTER_FIELDS) {
|
|
224
|
+
const value = filter[name];
|
|
225
|
+
if (value === void 0) continue;
|
|
226
|
+
if (typeof value !== "string" || value === "" || value !== value.trim() || value.length > FILTER_VALUE_LIMIT || /[\u0000-\u001f\u007f]/.test(value)) throw new RangeError(`The value of the field ${name} is not a text of 1 to ${FILTER_VALUE_LIMIT} characters with no control character and no space at an end.`);
|
|
227
|
+
terms.push(`${name} eq '${value.replaceAll("'", "''")}'`);
|
|
228
|
+
}
|
|
229
|
+
if (terms.length === 0) throw new RangeError("The filter has no field. A read of each price of Azure is a large crawl.");
|
|
230
|
+
const query = [`api-version=${AZURE_API_VERSION}`];
|
|
231
|
+
if (currency !== void 0) {
|
|
232
|
+
if (!/^[A-Z]{3}$/.test(currency) || !isCurrency(currency)) throw new RangeError(`The currency ${show(currency)} is not a money unit of @mark1russell7/quantity.`);
|
|
233
|
+
query.push(`currencyCode=${encodeURIComponent(`'${currency}'`)}`);
|
|
234
|
+
}
|
|
235
|
+
query.push(`$filter=${encodeURIComponent(terms.join(" and "))}`);
|
|
236
|
+
return `${AZURE_PRICES_URL}?${query.join("&")}`;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The pattern of a link to the Azure Retail Prices API: `https`, the host `prices.azure.com`, no port or the port 443,
|
|
240
|
+
* the path `/api/retail/prices` and an optional query. The pattern refuses a user name, a password and a fragment.
|
|
241
|
+
*/
|
|
242
|
+
const AZURE_PRICES_LINK = /^https:\/\/prices\.azure\.com(?::443)?\/api\/retail\/prices(?:\?[^#\s]*)?$/;
|
|
243
|
+
/** This function tells if a link goes to the Azure Retail Prices API (`AZURE_PRICES_LINK`). */
|
|
244
|
+
function isAzurePricesUrl(link) {
|
|
245
|
+
return AZURE_PRICES_LINK.test(link);
|
|
246
|
+
}
|
|
247
|
+
/** This function gives the link of the next page of an answer, or null. A link to another address gives null. */
|
|
248
|
+
function azureNextPage(answer) {
|
|
249
|
+
const link = text(field(answer, "NextPageLink"));
|
|
250
|
+
return link !== void 0 && isAzurePricesUrl(link) ? link : null;
|
|
251
|
+
}
|
|
252
|
+
/** The words of an amount in a unit of measure, and their units. A GB of Azure is 2^30 bytes, thus a GiB. */
|
|
253
|
+
const AMOUNT_WORDS = {
|
|
254
|
+
Second: "s",
|
|
255
|
+
Seconds: "s",
|
|
256
|
+
Minute: "min",
|
|
257
|
+
Minutes: "min",
|
|
258
|
+
Hour: "h",
|
|
259
|
+
Hours: "h",
|
|
260
|
+
Day: "d",
|
|
261
|
+
Days: "d",
|
|
262
|
+
Month: "mo",
|
|
263
|
+
Months: "mo",
|
|
264
|
+
Year: "yr",
|
|
265
|
+
Years: "yr",
|
|
266
|
+
KB: "KiB",
|
|
267
|
+
KiB: "KiB",
|
|
268
|
+
MB: "MiB",
|
|
269
|
+
MiB: "MiB",
|
|
270
|
+
GB: "GiB",
|
|
271
|
+
GiB: "GiB",
|
|
272
|
+
TB: "TiB",
|
|
273
|
+
TiB: "TiB"
|
|
274
|
+
};
|
|
275
|
+
/** The words of a time after the slash of a unit of measure, and their units. */
|
|
276
|
+
const PER_WORDS = {
|
|
277
|
+
Second: "s",
|
|
278
|
+
Minute: "min",
|
|
279
|
+
Hour: "h",
|
|
280
|
+
Day: "d",
|
|
281
|
+
Month: "mo",
|
|
282
|
+
Year: "yr"
|
|
283
|
+
};
|
|
284
|
+
/** A unit of measure: a count with `K` or `M`, a word of an amount, and `/` and a time. Only the count is necessary. */
|
|
285
|
+
const UNIT_OF_MEASURE = /^([1-9][0-9]{0,8})([KM]?)(?: ([A-Za-z]+))?(?:\/([A-Za-z]+))?$/;
|
|
286
|
+
/**
|
|
287
|
+
* This function gives the unit of a unit of measure of the API, or undefined when the parser does not know it. A
|
|
288
|
+
* count with no word gives `{count}`, for example `10K` gives 10,000 of `{count}`. A word of data or time gives
|
|
289
|
+
* its unit, and `/Month` multiplies it by a month. Only `1/Hour` and `1/Month` give a time alone.
|
|
290
|
+
*/
|
|
291
|
+
function azureUnit(unitOfMeasure) {
|
|
292
|
+
const match = UNIT_OF_MEASURE.exec(unitOfMeasure);
|
|
293
|
+
if (match === null) return void 0;
|
|
294
|
+
const [, digits = "", multiplier = "", word, per] = match;
|
|
295
|
+
const count = Number(digits) * (multiplier === "K" ? 1e3 : multiplier === "M" ? 1e6 : 1);
|
|
296
|
+
const amount = word === void 0 ? void 0 : AMOUNT_WORDS[word];
|
|
297
|
+
const time = per === void 0 ? void 0 : PER_WORDS[per];
|
|
298
|
+
if (word !== void 0 && amount === void 0 || per !== void 0 && time === void 0) return void 0;
|
|
299
|
+
let unit;
|
|
300
|
+
if (amount !== void 0) unit = time === void 0 ? amount : `${amount}·${time}`;
|
|
301
|
+
else if (time !== void 0 && count === 1) unit = time;
|
|
302
|
+
else unit = time === void 0 ? "{count}" : `{count}·${time}`;
|
|
303
|
+
try {
|
|
304
|
+
meteredKindOf(priceUnit("USD", unit));
|
|
305
|
+
} catch {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
count,
|
|
310
|
+
unit
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
function priceUnit(currency, unit) {
|
|
314
|
+
return canonicalUnit(unit.includes("·") ? `${currency}/(${unit})` : `${currency}/${unit}`);
|
|
315
|
+
}
|
|
316
|
+
/** This function gives the exact price of one unit, from the price of `count` units, as the nearest number. */
|
|
317
|
+
function perUnit(price, count) {
|
|
318
|
+
return rationals.toNumber(rationals.div(rationals.fromNumber(price), rationals.fromNumber(count)));
|
|
319
|
+
}
|
|
320
|
+
/** The text of a spot term at the end of a SKU name: `Spot`, or `Low Priority` of the older VMs that Azure can end. */
|
|
321
|
+
const SPOT = /(?:^|\s)(?:Spot|Low Priority)$/;
|
|
322
|
+
/** A term of a reservation or of a savings plan: `1 Year`, `3 Years` or `10 Years`. */
|
|
323
|
+
const YEARS = /^([1-9][0-9]?) Years?$/;
|
|
324
|
+
function yearsOf(value) {
|
|
325
|
+
const match = YEARS.exec(text(value) ?? "");
|
|
326
|
+
return match?.[1] === void 0 ? void 0 : Number(match[1]);
|
|
327
|
+
}
|
|
328
|
+
/** The fields of a row that each offer keeps as specs. The spec `pricing` adds the key of the term of the offer. */
|
|
329
|
+
const SPEC_FIELDS = [
|
|
330
|
+
"serviceName",
|
|
331
|
+
"serviceFamily",
|
|
332
|
+
"productName",
|
|
333
|
+
"skuName",
|
|
334
|
+
"armSkuName",
|
|
335
|
+
"location"
|
|
336
|
+
];
|
|
337
|
+
function readRow(item, where, notes) {
|
|
338
|
+
if (!isRecord(item)) {
|
|
339
|
+
notes.push(`The row ${where} is skipped: it is not an object.`);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const skuId = text(item["skuId"]);
|
|
343
|
+
const meterId = text(item["meterId"]);
|
|
344
|
+
const meterName = text(item["meterName"]);
|
|
345
|
+
const region = text(item["armRegionName"]) ?? text(item["location"]);
|
|
346
|
+
const product = text(item["productName"]);
|
|
347
|
+
const sku = text(item["skuName"]);
|
|
348
|
+
const type = text(item["type"]);
|
|
349
|
+
if (skuId === void 0 || meterId === void 0 || meterName === void 0 || region === void 0 || product === void 0 || sku === void 0 || type === void 0) {
|
|
350
|
+
const missing = Object.entries({
|
|
351
|
+
skuId,
|
|
352
|
+
meterId,
|
|
353
|
+
meterName,
|
|
354
|
+
armRegionName: region,
|
|
355
|
+
productName: product,
|
|
356
|
+
skuName: sku,
|
|
357
|
+
type
|
|
358
|
+
}).filter(([, value]) => value === void 0);
|
|
359
|
+
notes.push(`The row ${where} is skipped: it has no ${missing.map(([name]) => name).join(", ")}.`);
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const unitOfMeasure = text(item["unitOfMeasure"]) ?? "";
|
|
363
|
+
const unit = azureUnit(unitOfMeasure);
|
|
364
|
+
if (unit === void 0) {
|
|
365
|
+
notes.push(`The row ${where} is skipped: the parser does not know the unit of measure ${show(unitOfMeasure)}.`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const currency = text(item["currencyCode"]);
|
|
369
|
+
if (currency === void 0 || !isCurrency(currency)) {
|
|
370
|
+
notes.push(`The row ${where} is skipped: the currency ${show(item["currencyCode"])} is not a money unit of @mark1russell7/quantity.`);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const start = decimal(item["tierMinimumUnits"]);
|
|
374
|
+
const price = decimal(item["retailPrice"]);
|
|
375
|
+
if (start === void 0 || price === void 0) {
|
|
376
|
+
const name = start === void 0 ? "tierMinimumUnits" : "retailPrice";
|
|
377
|
+
notes.push(`The row ${where} is skipped: its ${name} ${show(item[name])} is not a decimal of zero or more.`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
const specs = [];
|
|
381
|
+
for (const name of SPEC_FIELDS) {
|
|
382
|
+
const value = text(item[name]);
|
|
383
|
+
if (value !== void 0) specs.push({
|
|
384
|
+
name,
|
|
385
|
+
value
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const spot = SPOT.test(sku);
|
|
389
|
+
return {
|
|
390
|
+
where,
|
|
391
|
+
region,
|
|
392
|
+
skuId,
|
|
393
|
+
meterId,
|
|
394
|
+
meterName,
|
|
395
|
+
unitOfMeasure,
|
|
396
|
+
unit,
|
|
397
|
+
currency,
|
|
398
|
+
start,
|
|
399
|
+
price,
|
|
400
|
+
type,
|
|
401
|
+
title: `${product}, ${sku}`,
|
|
402
|
+
spot,
|
|
403
|
+
specs,
|
|
404
|
+
item
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function variantsOf(row, notes) {
|
|
408
|
+
const base = row.spot ? { kind: "spot" } : { kind: "on-demand" };
|
|
409
|
+
if (row.type === "DevTestConsumption") return [{
|
|
410
|
+
key: "dev-test",
|
|
411
|
+
term: base,
|
|
412
|
+
title: `${row.title}, Dev/Test`,
|
|
413
|
+
price: row.price
|
|
414
|
+
}];
|
|
415
|
+
if (row.type === "Reservation") {
|
|
416
|
+
const years = yearsOf(row.item["reservationTerm"]);
|
|
417
|
+
if (years === void 0) {
|
|
418
|
+
notes.push(`The row ${row.where} is skipped: its reservationTerm ${show(row.item["reservationTerm"])} is not a count of years.`);
|
|
419
|
+
return [];
|
|
420
|
+
}
|
|
421
|
+
const term = {
|
|
422
|
+
kind: "committed",
|
|
423
|
+
length: {
|
|
424
|
+
value: years,
|
|
425
|
+
unit: "yr"
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
return [{
|
|
429
|
+
key: `reservation-${years}y`,
|
|
430
|
+
term,
|
|
431
|
+
title: `${row.title}, ${years}-year reservation`,
|
|
432
|
+
price: 0,
|
|
433
|
+
upfront: row.price
|
|
434
|
+
}];
|
|
435
|
+
}
|
|
436
|
+
if (row.type !== "Consumption") {
|
|
437
|
+
notes.push(`The row ${row.where} is skipped: its type ${show(row.type)} is not a type of the documentation.`);
|
|
438
|
+
return [];
|
|
439
|
+
}
|
|
440
|
+
const variants = [{
|
|
441
|
+
key: "consumption",
|
|
442
|
+
term: base,
|
|
443
|
+
title: row.title,
|
|
444
|
+
price: row.price
|
|
445
|
+
}];
|
|
446
|
+
const plans = row.item["savingsPlan"];
|
|
447
|
+
(list(plans) ?? []).forEach((plan, index) => {
|
|
448
|
+
const years = yearsOf(field(plan, "term"));
|
|
449
|
+
const price = decimal(field(plan, "retailPrice"));
|
|
450
|
+
if (years === void 0 || price === void 0) {
|
|
451
|
+
notes.push(`The savings plan ${row.where}.savingsPlan[${index}] is skipped: it has no term in years or no retailPrice.`);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const term = {
|
|
455
|
+
kind: "committed",
|
|
456
|
+
length: {
|
|
457
|
+
value: years,
|
|
458
|
+
unit: "yr"
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
variants.push({
|
|
462
|
+
key: `savings-plan-${years}y`,
|
|
463
|
+
term,
|
|
464
|
+
title: `${row.title}, ${years}-year savings plan`,
|
|
465
|
+
price
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
if (plans !== void 0 && plans !== null && list(plans) === void 0) notes.push(`The field savingsPlan of the row ${row.where} is not a list, and the parser does not read it.`);
|
|
469
|
+
return variants;
|
|
470
|
+
}
|
|
471
|
+
function addRow(row, drafts, notes) {
|
|
472
|
+
for (const variant of variantsOf(row, notes)) {
|
|
473
|
+
const sourceId = `${row.region}/${row.skuId}/${variant.key}`;
|
|
474
|
+
let draft = drafts.get(sourceId);
|
|
475
|
+
if (draft === void 0) {
|
|
476
|
+
draft = {
|
|
477
|
+
sourceId,
|
|
478
|
+
region: row.region,
|
|
479
|
+
title: variant.title,
|
|
480
|
+
term: variant.term,
|
|
481
|
+
specs: [...row.specs, {
|
|
482
|
+
name: "pricing",
|
|
483
|
+
value: variant.key
|
|
484
|
+
}],
|
|
485
|
+
currency: row.currency,
|
|
486
|
+
upfront: variant.upfront,
|
|
487
|
+
meters: /* @__PURE__ */ new Map()
|
|
488
|
+
};
|
|
489
|
+
drafts.set(sourceId, draft);
|
|
490
|
+
} else if (draft.currency !== row.currency) {
|
|
491
|
+
notes.push(`The row ${row.where} is skipped: its currency ${row.currency} is not the currency of the offer ${offerId(AZURE_SOURCE, sourceId)}.`);
|
|
492
|
+
continue;
|
|
493
|
+
} else if (variant.upfront !== void 0) {
|
|
494
|
+
notes.push(`The row ${row.where} is skipped: it repeats the reservation ${offerId(AZURE_SOURCE, sourceId)}.`);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const key = `${row.meterId}\u0000${row.meterName}\u0000${row.unitOfMeasure}`;
|
|
498
|
+
let meter = draft.meters.get(key);
|
|
499
|
+
if (meter === void 0) {
|
|
500
|
+
meter = {
|
|
501
|
+
meterId: row.meterId,
|
|
502
|
+
meterName: row.meterName,
|
|
503
|
+
unit: row.unit,
|
|
504
|
+
tiers: /* @__PURE__ */ new Map()
|
|
505
|
+
};
|
|
506
|
+
draft.meters.set(key, meter);
|
|
507
|
+
}
|
|
508
|
+
if (row.start !== 0 && row.unit.count !== 1) meter.problem = `a tier starts at ${row.start} of the unit of measure ${row.unitOfMeasure}, and the documentation does not tell the unit of the start`;
|
|
509
|
+
const known = meter.tiers.get(row.start);
|
|
510
|
+
if (known !== void 0 && known !== variant.price) meter.problem = `two rows give the tier at ${row.start} the prices ${known} and ${variant.price}`;
|
|
511
|
+
meter.tiers.set(row.start, variant.price);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function meterOf(draft, meter, name, id, notes) {
|
|
515
|
+
if (meter.problem !== void 0) {
|
|
516
|
+
notes.push(`The meter ${show(meter.meterName)} of the offer ${id} is skipped: ${meter.problem}.`);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const starts = [...meter.tiers.keys()].sort((a, b) => a - b);
|
|
520
|
+
if (starts[0] !== 0) {
|
|
521
|
+
notes.push(`The meter ${show(meter.meterName)} of the offer ${id} is skipped: it has no tier that starts at 0.`);
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
const unitPrice = priceUnit(draft.currency, meter.unit.unit);
|
|
525
|
+
return {
|
|
526
|
+
name,
|
|
527
|
+
tiers: starts.map((start) => ({
|
|
528
|
+
from: {
|
|
529
|
+
value: start,
|
|
530
|
+
unit: meter.unit.unit
|
|
531
|
+
},
|
|
532
|
+
unitPrice: {
|
|
533
|
+
value: perUnit(meter.tiers.get(start) ?? 0, meter.unit.count),
|
|
534
|
+
unit: unitPrice
|
|
535
|
+
}
|
|
536
|
+
}))
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
function observationOf$1(draft, at, notes) {
|
|
540
|
+
const id = offerId(AZURE_SOURCE, draft.sourceId);
|
|
541
|
+
const names = /* @__PURE__ */ new Map();
|
|
542
|
+
for (const meter of draft.meters.values()) names.set(meter.meterName, (names.get(meter.meterName) ?? 0) + 1);
|
|
543
|
+
const meters = [];
|
|
544
|
+
for (const meter of draft.meters.values()) {
|
|
545
|
+
const read = meterOf(draft, meter, (names.get(meter.meterName) ?? 0) > 1 ? `${meter.meterName} (${meter.meterId})` : meter.meterName, id, notes);
|
|
546
|
+
if (read !== void 0) meters.push(read);
|
|
547
|
+
}
|
|
548
|
+
if (meters.length === 0) {
|
|
549
|
+
notes.push(`The offer ${id} is skipped: it has no meter with a valid price.`);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
return observeMetered({
|
|
553
|
+
id,
|
|
554
|
+
source: AZURE_SOURCE,
|
|
555
|
+
sourceId: draft.sourceId,
|
|
556
|
+
title: draft.title,
|
|
557
|
+
location: {
|
|
558
|
+
provider: AZURE_PROVIDER,
|
|
559
|
+
region: draft.region
|
|
560
|
+
},
|
|
561
|
+
term: draft.term,
|
|
562
|
+
specs: [...draft.specs]
|
|
563
|
+
}, {
|
|
564
|
+
offerId: id,
|
|
565
|
+
at,
|
|
566
|
+
period: {
|
|
567
|
+
value: 1,
|
|
568
|
+
unit: "mo"
|
|
569
|
+
},
|
|
570
|
+
meters,
|
|
571
|
+
upfront: draft.upfront === void 0 ? void 0 : {
|
|
572
|
+
value: draft.upfront,
|
|
573
|
+
unit: draft.currency
|
|
574
|
+
}
|
|
575
|
+
}, notes);
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* This function reads one page of the Azure Retail Prices API (`GET /api/retail/prices`) at the instant `at`.
|
|
579
|
+
* The prices of one SKU can continue on the next page: `parseAzureRetailPricePages` reads all the pages together.
|
|
580
|
+
*
|
|
581
|
+
* @throws RangeError when `at` is not an instant in UTC in the ISO 8601 form.
|
|
582
|
+
*/
|
|
583
|
+
function parseAzureRetailPrices(answer, at) {
|
|
584
|
+
return parseAzureRetailPricePages([answer], at);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* This function reads the pages of one read of the Azure Retail Prices API at the instant `at`. Each SKU in a
|
|
588
|
+
* region on a term gives one offer, with a meter for each meter of the SKU. The `next` of the result is the link
|
|
589
|
+
* of the last page.
|
|
590
|
+
*
|
|
591
|
+
* @throws RangeError when `at` is not an instant in UTC in the ISO 8601 form.
|
|
592
|
+
*/
|
|
593
|
+
function parseAzureRetailPricePages(answers, at) {
|
|
594
|
+
checkAt(at);
|
|
595
|
+
if (answers.length === 0) return refused("The read has no page.");
|
|
596
|
+
const notes = [];
|
|
597
|
+
const drafts = /* @__PURE__ */ new Map();
|
|
598
|
+
const rows = [];
|
|
599
|
+
for (const [index, answer] of answers.entries()) {
|
|
600
|
+
const label = answers.length === 1 ? "The answer" : `The page ${index + 1}`;
|
|
601
|
+
if (!isRecord(answer)) return refused(`${label} is not an object.`);
|
|
602
|
+
const items = list(answer["Items"]);
|
|
603
|
+
if (items === void 0) return refused(`The field Items of ${label.toLowerCase()} is not a list.`);
|
|
604
|
+
const prefix = answers.length === 1 ? "" : `page ${index + 1}, `;
|
|
605
|
+
items.forEach((item, row) => {
|
|
606
|
+
const read = readRow(item, `${prefix}Items[${row}]`, notes);
|
|
607
|
+
if (read !== void 0) rows.push(read);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
for (const row of rows) addRow(row, drafts, notes);
|
|
611
|
+
const observations = [];
|
|
612
|
+
for (const draft of drafts.values()) {
|
|
613
|
+
const observation = observationOf$1(draft, at, notes);
|
|
614
|
+
if (observation !== void 0) observations.push(observation);
|
|
615
|
+
}
|
|
616
|
+
const last = answers[answers.length - 1];
|
|
617
|
+
const next = azureNextPage(last);
|
|
618
|
+
const link = field(last, "NextPageLink");
|
|
619
|
+
if (next === null && text(link) !== void 0) notes.push(`The link of the next page ${show(link)} does not go to the Azure Retail Prices API, and the result has no link.`);
|
|
620
|
+
return {
|
|
621
|
+
ok: true,
|
|
622
|
+
observations,
|
|
623
|
+
notes,
|
|
624
|
+
next
|
|
625
|
+
};
|
|
626
|
+
}
|
|
155
627
|
// src/bestbuy.ts
|
|
156
628
|
/** The name of the Best Buy Products API and Open Box API as a source of offers. */
|
|
157
629
|
const BEST_BUY_SOURCE = "bestbuy";
|
|
@@ -210,7 +682,7 @@ function availabilityOf(product) {
|
|
|
210
682
|
if (orderable === "Available") return "in-stock";
|
|
211
683
|
return orderable === void 0 && product["onlineAvailability"] === true ? "in-stock" : "unknown";
|
|
212
684
|
}
|
|
213
|
-
function specsOf$
|
|
685
|
+
function specsOf$3(value) {
|
|
214
686
|
const specs = [];
|
|
215
687
|
for (const detail of list(value) ?? []) {
|
|
216
688
|
const name = text(field(detail, "name"));
|
|
@@ -264,7 +736,7 @@ function mapProduct$1(product, where, at, notes) {
|
|
|
264
736
|
format: "fixed-price",
|
|
265
737
|
gtins: gtin === null ? [] : [gtin],
|
|
266
738
|
partNumbers,
|
|
267
|
-
specs: specsOf$
|
|
739
|
+
specs: specsOf$3(product["details"])
|
|
268
740
|
}, {
|
|
269
741
|
offerId: id,
|
|
270
742
|
at,
|
|
@@ -421,7 +893,7 @@ function sellerOf$1(variation) {
|
|
|
421
893
|
...name === void 0 ? {} : { name }
|
|
422
894
|
};
|
|
423
895
|
}
|
|
424
|
-
function specsOf$
|
|
896
|
+
function specsOf$2(product) {
|
|
425
897
|
const specs = [];
|
|
426
898
|
for (const parameter of list(product["Parameters"]) ?? []) {
|
|
427
899
|
const name = text(field(parameter, "ParameterText"));
|
|
@@ -456,7 +928,7 @@ function mapProduct(product, where, currency, at, notes) {
|
|
|
456
928
|
brand,
|
|
457
929
|
partNumber
|
|
458
930
|
}] : [];
|
|
459
|
-
const specs = specsOf$
|
|
931
|
+
const specs = specsOf$2(product);
|
|
460
932
|
const url = httpsUrl(product["ProductUrl"]);
|
|
461
933
|
const observations = [];
|
|
462
934
|
variations.forEach((variation, index) => {
|
|
@@ -644,7 +1116,7 @@ function sellerOf(value) {
|
|
|
644
1116
|
rating
|
|
645
1117
|
});
|
|
646
1118
|
}
|
|
647
|
-
function specsOf(value) {
|
|
1119
|
+
function specsOf$1(value) {
|
|
648
1120
|
const specs = [];
|
|
649
1121
|
for (const aspect of list(value) ?? []) {
|
|
650
1122
|
const name = text(field(aspect, "name"));
|
|
@@ -711,7 +1183,7 @@ function mapItem(item, where, at, notes) {
|
|
|
711
1183
|
endsAt,
|
|
712
1184
|
gtins: gtin === null ? [] : [gtin],
|
|
713
1185
|
partNumbers,
|
|
714
|
-
specs: specsOf(item["localizedAspects"])
|
|
1186
|
+
specs: specsOf$1(item["localizedAspects"])
|
|
715
1187
|
}, {
|
|
716
1188
|
offerId: id,
|
|
717
1189
|
at,
|
|
@@ -756,6 +1228,207 @@ function parseEbaySearch(answer, at) {
|
|
|
756
1228
|
});
|
|
757
1229
|
return done(observations, notes);
|
|
758
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];
|
|
759
1432
|
// src/client.ts
|
|
760
1433
|
/**
|
|
761
1434
|
* The shared parts of the clients of the seller APIs. Each client sends its exchanges with the polite send of
|
|
@@ -801,19 +1474,28 @@ function formBody(fields) {
|
|
|
801
1474
|
return Object.entries(fields).map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join("&");
|
|
802
1475
|
}
|
|
803
1476
|
/**
|
|
804
|
-
* This function reads
|
|
805
|
-
*
|
|
1477
|
+
* This function reads the JSON of an answer. An answer that is not a 2xx gives a problem with its status line,
|
|
1478
|
+
* and no part of its body.
|
|
806
1479
|
*/
|
|
807
|
-
function
|
|
1480
|
+
function readJson(answer, input) {
|
|
808
1481
|
const { reply } = answer;
|
|
809
1482
|
if (reply.status < 200 || reply.status > 299) return refused(redact(`${input.source} answered ${statusLine$1(reply)} to ${input.what}.`, input.secrets ?? []));
|
|
810
|
-
let json;
|
|
811
1483
|
try {
|
|
812
|
-
|
|
1484
|
+
return {
|
|
1485
|
+
ok: true,
|
|
1486
|
+
json: JSON.parse(reply.body)
|
|
1487
|
+
};
|
|
813
1488
|
} catch {
|
|
814
1489
|
return refused(`The answer of ${input.source} to ${input.what} is not JSON.`);
|
|
815
1490
|
}
|
|
816
|
-
|
|
1491
|
+
}
|
|
1492
|
+
/**
|
|
1493
|
+
* This function reads an answer with a parser of this package at the instant `at`. An answer that is not a 2xx
|
|
1494
|
+
* gives a problem with its status line, and no part of its body.
|
|
1495
|
+
*/
|
|
1496
|
+
function readAnswer(answer, input, parse, at) {
|
|
1497
|
+
const read = readJson(answer, input);
|
|
1498
|
+
return read.ok ? parse(read.json, at) : read;
|
|
817
1499
|
}
|
|
818
1500
|
/** This function gives the instant of a time in milliseconds since the epoch, in the ISO 8601 form in UTC. */
|
|
819
1501
|
function instantOf(ms) {
|
|
@@ -834,7 +1516,7 @@ const DIGIKEY_US = {
|
|
|
834
1516
|
language: "en",
|
|
835
1517
|
currency: "USD"
|
|
836
1518
|
};
|
|
837
|
-
const SOURCE$
|
|
1519
|
+
const SOURCE$3 = "Digi-Key";
|
|
838
1520
|
/**
|
|
839
1521
|
* This function makes a client of the Digi-Key Product Information API v4. The client gets a token with the
|
|
840
1522
|
* 2-legged OAuth flow (the client credentials grant), and it keeps the token until 1 minute before its end. Each
|
|
@@ -851,7 +1533,7 @@ function createDigiKeyClient(options) {
|
|
|
851
1533
|
const locale = options.locale ?? DIGIKEY_US;
|
|
852
1534
|
const secrets = [clientId, clientSecret];
|
|
853
1535
|
const context = {
|
|
854
|
-
source: SOURCE$
|
|
1536
|
+
source: SOURCE$3,
|
|
855
1537
|
host,
|
|
856
1538
|
minIntervalMs: 500,
|
|
857
1539
|
secrets,
|
|
@@ -877,7 +1559,7 @@ function createDigiKeyClient(options) {
|
|
|
877
1559
|
what: "the token",
|
|
878
1560
|
exchange
|
|
879
1561
|
}, signal);
|
|
880
|
-
return readGrant(reply, SOURCE$
|
|
1562
|
+
return readGrant(reply, SOURCE$3, issuedAt, secrets, redact);
|
|
881
1563
|
}, pace.now);
|
|
882
1564
|
const headers = (token) => ({
|
|
883
1565
|
authorization: `Bearer ${token}`,
|
|
@@ -949,7 +1631,7 @@ const EBAY_PACE_MS = 1e3;
|
|
|
949
1631
|
const EBAY_SEARCH_LIMIT = 200;
|
|
950
1632
|
/** The marketplace of the US. */
|
|
951
1633
|
const EBAY_US = "EBAY_US";
|
|
952
|
-
const SOURCE$
|
|
1634
|
+
const SOURCE$2 = "eBay";
|
|
953
1635
|
const ITEM_ID = /^v1\|\d{1,20}\|\d{1,20}$/;
|
|
954
1636
|
const MARKETPLACE = /^EBAY_[A-Z]{2,4}$/;
|
|
955
1637
|
/**
|
|
@@ -974,7 +1656,7 @@ function createEbayClient(options) {
|
|
|
974
1656
|
basic
|
|
975
1657
|
];
|
|
976
1658
|
const context = {
|
|
977
|
-
source: SOURCE$
|
|
1659
|
+
source: SOURCE$2,
|
|
978
1660
|
host,
|
|
979
1661
|
minIntervalMs: EBAY_PACE_MS,
|
|
980
1662
|
secrets,
|
|
@@ -1000,7 +1682,7 @@ function createEbayClient(options) {
|
|
|
1000
1682
|
what: "the token",
|
|
1001
1683
|
exchange
|
|
1002
1684
|
}, signal);
|
|
1003
|
-
return readGrant(reply, SOURCE$
|
|
1685
|
+
return readGrant(reply, SOURCE$2, issuedAt, secrets, redact);
|
|
1004
1686
|
}, pace.now);
|
|
1005
1687
|
const headers = (token) => ({
|
|
1006
1688
|
authorization: `Bearer ${token}`,
|
|
@@ -1039,6 +1721,66 @@ function createEbayClient(options) {
|
|
|
1039
1721
|
}
|
|
1040
1722
|
};
|
|
1041
1723
|
}
|
|
1724
|
+
// src/azure-client.ts
|
|
1725
|
+
/** The host of the Azure Retail Prices API. */
|
|
1726
|
+
const AZURE_HOST = "prices.azure.com";
|
|
1727
|
+
/**
|
|
1728
|
+
* The client sends one request each 6 seconds at most. The documentation gives no limit. But each answer has the
|
|
1729
|
+
* headers `x-ms-ratelimit-remaining-retailprices-requests` and `x-ms-ratelimit-retailprices-retry-after: 60`.
|
|
1730
|
+
* Thus the client sends 10 requests in 60 seconds at most.
|
|
1731
|
+
*/
|
|
1732
|
+
const AZURE_PACE_MS = 6e3;
|
|
1733
|
+
/** The most pages of one read. Each page has 1,000 prices at most. */
|
|
1734
|
+
const AZURE_PAGE_LIMIT = 10;
|
|
1735
|
+
const SOURCE$1 = "Azure Retail Prices";
|
|
1736
|
+
function checkPages$1(pages) {
|
|
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.`);
|
|
1738
|
+
return pages;
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* This function makes a client of the Azure Retail Prices API. A read follows the link of the next page of each
|
|
1742
|
+
* answer, only to the same API, up to its count of pages. It then reads all the pages together.
|
|
1743
|
+
*/
|
|
1744
|
+
function createAzureClient(options) {
|
|
1745
|
+
const { pace, transport } = options;
|
|
1746
|
+
const read = async (first, pages, what, signal) => {
|
|
1747
|
+
const answers = [];
|
|
1748
|
+
let link = first;
|
|
1749
|
+
while (link !== null && answers.length < pages) {
|
|
1750
|
+
const page = answers.length + 1;
|
|
1751
|
+
const exchange = {
|
|
1752
|
+
method: "GET",
|
|
1753
|
+
url: link,
|
|
1754
|
+
headers: { accept: "application/json" },
|
|
1755
|
+
body: null
|
|
1756
|
+
};
|
|
1757
|
+
const input = {
|
|
1758
|
+
source: SOURCE$1,
|
|
1759
|
+
host: AZURE_HOST,
|
|
1760
|
+
minIntervalMs: AZURE_PACE_MS,
|
|
1761
|
+
what: page === 1 ? what : `${what}, page ${page}`,
|
|
1762
|
+
secrets: [],
|
|
1763
|
+
redact,
|
|
1764
|
+
exchange
|
|
1765
|
+
};
|
|
1766
|
+
const json = readJson(await send(pace, transport, input, signal), input);
|
|
1767
|
+
if (!json.ok) return json;
|
|
1768
|
+
answers.push(json.json);
|
|
1769
|
+
link = azureNextPage(json.json);
|
|
1770
|
+
}
|
|
1771
|
+
return parseAzureRetailPricePages(answers, instantOf(pace.now()));
|
|
1772
|
+
};
|
|
1773
|
+
return {
|
|
1774
|
+
async prices(filter, readOptions = {}, signal) {
|
|
1775
|
+
const url = azurePricesUrl(filter, readOptions.currency);
|
|
1776
|
+
return read(url, checkPages$1(readOptions.pages ?? 1), "the prices of a filter", signal);
|
|
1777
|
+
},
|
|
1778
|
+
async more(next, pages = 1, signal) {
|
|
1779
|
+
if (typeof next !== "string" || !isAzurePricesUrl(next)) throw new RangeError("The link is not a link of the Azure Retail Prices API.");
|
|
1780
|
+
return read(next, checkPages$1(pages), "the next page of a read", signal);
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
1783
|
+
}
|
|
1042
1784
|
// src/bestbuy-client.ts
|
|
1043
1785
|
/** The host of the Best Buy APIs. */
|
|
1044
1786
|
const BEST_BUY_HOST = "api.bestbuy.com";
|
|
@@ -1111,13 +1853,427 @@ function createBestBuyClient(options) {
|
|
|
1111
1853
|
}
|
|
1112
1854
|
};
|
|
1113
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
|
+
}
|
|
1114
2267
|
// src/index.ts
|
|
1115
|
-
/** The terms of each source of this package
|
|
2268
|
+
/** The terms of each source of this package: the sellers in the order of the hand crawl, then Azure, then the plan lists. */
|
|
1116
2269
|
const SOURCE_TERMS = [
|
|
1117
2270
|
EBAY_TERMS,
|
|
1118
2271
|
BEST_BUY_TERMS,
|
|
1119
|
-
DIGIKEY_TERMS
|
|
2272
|
+
DIGIKEY_TERMS,
|
|
2273
|
+
AZURE_TERMS,
|
|
2274
|
+
LINODE_TERMS,
|
|
2275
|
+
VULTR_TERMS
|
|
1120
2276
|
];
|
|
1121
|
-
export { 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, bestBuyCondition, createBestBuyClient, createDigiKeyClient, createEbayClient, ebayCondition, 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 };
|
|
1122
2278
|
|
|
1123
2279
|
//# sourceMappingURL=index.js.map
|