@spica-devkit/testing 0.20.6 → 0.20.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/index.mjs CHANGED
@@ -260,6 +260,85 @@ var ChangeKind;
260
260
  })(ChangeKind || (ChangeKind = {}));
261
261
  const DEFAULT_CONCURRENCY = 10;
262
262
 
263
+ const REQUEST_METHODS = ["get", "delete", "head", "post", "patch", "put"];
264
+ const limitedClients = new WeakMap();
265
+ function limitConcurrency(http, limit = DEFAULT_CONCURRENCY, signal) {
266
+ const existing = limitedClients.get(http);
267
+ if (existing && !signal)
268
+ return http;
269
+ const { semaphore, target } = existing ?? {
270
+ semaphore: new Semaphore(normalizeLimit(limit)),
271
+ target: http
272
+ };
273
+ const limited = { ...target };
274
+ for (const method of REQUEST_METHODS) {
275
+ limited[method] = (...args) => semaphore.run(() => {
276
+ if (signal?.aborted)
277
+ throw signal.reason;
278
+ return target[method](...args);
279
+ });
280
+ }
281
+ limitedClients.set(limited, { semaphore, target });
282
+ return limited;
283
+ }
284
+ async function mapWithConcurrency(items, limit, fn) {
285
+ const results = new Array(items.length);
286
+ let next = 0;
287
+ let failure;
288
+ const worker = async () => {
289
+ while (!failure && next < items.length) {
290
+ const index = next++;
291
+ try {
292
+ results[index] = await fn(items[index], index);
293
+ }
294
+ catch (error) {
295
+ if (!failure)
296
+ failure = { error };
297
+ }
298
+ }
299
+ };
300
+ const workerCount = Math.min(normalizeLimit(limit), items.length);
301
+ await Promise.all(Array.from({ length: workerCount }, worker));
302
+ if (failure)
303
+ throw failure.error;
304
+ return results;
305
+ }
306
+ function normalizeLimit(limit) {
307
+ return Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : DEFAULT_CONCURRENCY;
308
+ }
309
+ class Semaphore {
310
+ constructor(limit) {
311
+ this.limit = limit;
312
+ this.active = 0;
313
+ this.waiting = [];
314
+ }
315
+ async run(task) {
316
+ await this.acquire();
317
+ try {
318
+ return await task();
319
+ }
320
+ finally {
321
+ this.release();
322
+ }
323
+ }
324
+ acquire() {
325
+ if (this.active < this.limit) {
326
+ this.active++;
327
+ return Promise.resolve();
328
+ }
329
+ return new Promise(resolve => this.waiting.push(resolve));
330
+ }
331
+ release() {
332
+ const next = this.waiting.shift();
333
+ if (next) {
334
+ next();
335
+ }
336
+ else {
337
+ this.active--;
338
+ }
339
+ }
340
+ }
341
+
263
342
  const noopProgress = {
264
343
  update() { },
265
344
  succeed() { },
@@ -12927,13 +13006,14 @@ function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader
12927
13006
  return ret.join('\n') + '\n';
12928
13007
  }
12929
13008
 
12930
- async function buildPlan(modules, http, rootDir, options = {}) {
13009
+ async function buildPlan(modules, unlimitedHttp, rootDir, options = {}) {
12931
13010
  const { detailed = false, detectRenames = true, reporter = silentReporter, concurrency = DEFAULT_CONCURRENCY, source } = options;
13011
+ const http = limitConcurrency(unlimitedHttp, concurrency);
12932
13012
  const modulePlans = [];
12933
13013
  for (const mod of modules) {
12934
13014
  const [locals, remotes] = await Promise.all([
12935
13015
  reporter.task(`Reading local ${mod.displayName} files`, () => mod.readLocal(rootDir, source)),
12936
- reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http, { concurrency }))
13016
+ reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http))
12937
13017
  ]);
12938
13018
  const localBySlug = new Map(locals.map(l => [l.slug, l]));
12939
13019
  const remoteBySlug = new Map(remotes.map(r => [r.slug, r]));
@@ -13012,29 +13092,30 @@ async function buildPlan(modules, http, rootDir, options = {}) {
13012
13092
  }
13013
13093
  return { modules: modulePlans };
13014
13094
  }
13015
- async function applyPlan(plan, http, opts = {}) {
13095
+ async function applyPlan(plan, unlimitedHttp, opts = {}) {
13016
13096
  const concurrency = opts.concurrency ?? DEFAULT_CONCURRENCY;
13017
13097
  const reporter = opts.reporter ?? silentReporter;
13018
13098
  const errors = [];
13099
+ const abortController = new AbortController();
13100
+ const http = limitConcurrency(unlimitedHttp, concurrency, abortController.signal);
13101
+ let abortError;
13019
13102
  const handleErr = (label, e) => {
13020
- const msg = formatError(e);
13021
- const full = `${label}: ${msg}`;
13022
- if (opts.abortOnError)
13023
- throw new Error(full);
13103
+ if (abortError)
13104
+ throw abortError;
13105
+ const full = `${label}: ${formatError(e)}`;
13106
+ if (opts.abortOnError) {
13107
+ abortError = new Error(full);
13108
+ abortController.abort(abortError);
13109
+ throw abortError;
13110
+ }
13024
13111
  reporter.warn(full);
13025
13112
  errors.push(full);
13026
13113
  };
13027
13114
  for (const mp of plan.modules) {
13028
13115
  const { module: mod, creates, updates, deletes } = mp;
13029
- if (deletes.length) {
13030
- await runConcurrently(deletes.map(e => ({ slug: e.slug, run: () => mod.delete(http, e.remote.id) })), concurrency, `Deleting ${mod.displayName}`, handleErr, reporter);
13031
- }
13032
- if (updates.length) {
13033
- await runConcurrently(updates.map(e => ({ slug: e.slug, run: () => mod.update(http, e.local, e.remote.id) })), concurrency, `Updating ${mod.displayName}`, handleErr, reporter);
13034
- }
13035
- if (creates.length) {
13036
- await runConcurrently(creates.map(e => ({ slug: e.slug, run: () => mod.create(http, e.local) })), concurrency, `Creating ${mod.displayName}`, handleErr, reporter);
13037
- }
13116
+ await runAllAtOnce(deletes.map(e => ({ slug: e.slug, run: () => mod.delete(http, e.remote.id) })), `Deleting ${mod.displayName}`, handleErr, reporter);
13117
+ await runAllAtOnce(updates.map(e => ({ slug: e.slug, run: () => mod.update(http, e.local, e.remote.id) })), `Updating ${mod.displayName}`, handleErr, reporter);
13118
+ await runAllAtOnce(creates.map(e => ({ slug: e.slug, run: () => mod.create(http, e.local) })), `Creating ${mod.displayName}`, handleErr, reporter);
13038
13119
  }
13039
13120
  return { errors };
13040
13121
  }
@@ -13061,6 +13142,9 @@ function renderSchemaDetail(localData, remoteData, ignoredFields) {
13061
13142
  const remoteYaml = YAML.stringify(omit(remoteData, ignoredFields));
13062
13143
  return { schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml") };
13063
13144
  }
13145
+ function runAllAtOnce(tasks, label, onError, reporter) {
13146
+ return runConcurrently(tasks, tasks.length, label, onError, reporter);
13147
+ }
13064
13148
  async function runConcurrently(tasks, concurrency, label, onError, reporter) {
13065
13149
  if (!tasks.length)
13066
13150
  return;
@@ -13068,21 +13152,13 @@ async function runConcurrently(tasks, concurrency, label, onError, reporter) {
13068
13152
  let done = 0;
13069
13153
  const progress = reporter.progress(label, total);
13070
13154
  try {
13071
- const limit = Math.max(1, Math.min(total, concurrency));
13072
- let batch = [];
13073
- for (let i = 0; i < tasks.length; i++) {
13074
- batch.push(tasks[i]);
13075
- if (batch.length === limit || i === tasks.length - 1) {
13076
- await Promise.all(batch.map(task => task
13077
- .run()
13078
- .catch((e) => onError(`${label}: ${task.slug}`, e))
13079
- .finally(() => {
13080
- done++;
13081
- progress.update(task.slug, done, total);
13082
- })));
13083
- batch = [];
13084
- }
13085
- }
13155
+ await mapWithConcurrency(tasks, concurrency, task => task
13156
+ .run()
13157
+ .catch((e) => onError(`${label}: ${task.slug}`, e))
13158
+ .finally(() => {
13159
+ done++;
13160
+ progress.update(task.slug, done, total);
13161
+ }));
13086
13162
  progress.succeed();
13087
13163
  }
13088
13164
  catch (e) {
@@ -15939,14 +16015,6 @@ function normalizeSchema(schema) {
15939
16015
  }
15940
16016
  return normalized;
15941
16017
  }
15942
- async function mapInBatches(items, size, fn) {
15943
- const limit = Math.max(1, size);
15944
- const results = [];
15945
- for (let i = 0; i < items.length; i += limit) {
15946
- results.push(...(await Promise.all(items.slice(i, i + limit).map(fn))));
15947
- }
15948
- return results;
15949
- }
15950
16018
  function isNotFoundError(error) {
15951
16019
  if (!error || typeof error !== "object")
15952
16020
  return false;
@@ -16010,10 +16078,10 @@ const functionModule = {
16010
16078
  }
16011
16079
  return results;
16012
16080
  },
16013
- async readRemote(http, options = {}) {
16081
+ async readRemote(http) {
16014
16082
  const res = await http.get("function");
16015
16083
  const fns = unwrapList(res);
16016
- return mapInBatches(fns, options.concurrency ?? DEFAULT_CONCURRENCY, async (fn) => {
16084
+ return Promise.all(fns.map(async (fn) => {
16017
16085
  const id = fn._id;
16018
16086
  const [indexRes, depsRes] = await Promise.all([
16019
16087
  http.get(`function/${id}/index`).catch(emptyIfMissing({ index: "" })),
@@ -16036,7 +16104,7 @@ const functionModule = {
16036
16104
  dependencies
16037
16105
  }
16038
16106
  };
16039
- });
16107
+ }));
16040
16108
  },
16041
16109
  async create(http, local) {
16042
16110
  const id = await this.createSchema(http, local.data.schema);