@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.js CHANGED
@@ -281,6 +281,85 @@ var ChangeKind;
281
281
  })(ChangeKind || (ChangeKind = {}));
282
282
  const DEFAULT_CONCURRENCY = 10;
283
283
 
284
+ const REQUEST_METHODS = ["get", "delete", "head", "post", "patch", "put"];
285
+ const limitedClients = new WeakMap();
286
+ function limitConcurrency(http, limit = DEFAULT_CONCURRENCY, signal) {
287
+ const existing = limitedClients.get(http);
288
+ if (existing && !signal)
289
+ return http;
290
+ const { semaphore, target } = existing ?? {
291
+ semaphore: new Semaphore(normalizeLimit(limit)),
292
+ target: http
293
+ };
294
+ const limited = { ...target };
295
+ for (const method of REQUEST_METHODS) {
296
+ limited[method] = (...args) => semaphore.run(() => {
297
+ if (signal?.aborted)
298
+ throw signal.reason;
299
+ return target[method](...args);
300
+ });
301
+ }
302
+ limitedClients.set(limited, { semaphore, target });
303
+ return limited;
304
+ }
305
+ async function mapWithConcurrency(items, limit, fn) {
306
+ const results = new Array(items.length);
307
+ let next = 0;
308
+ let failure;
309
+ const worker = async () => {
310
+ while (!failure && next < items.length) {
311
+ const index = next++;
312
+ try {
313
+ results[index] = await fn(items[index], index);
314
+ }
315
+ catch (error) {
316
+ if (!failure)
317
+ failure = { error };
318
+ }
319
+ }
320
+ };
321
+ const workerCount = Math.min(normalizeLimit(limit), items.length);
322
+ await Promise.all(Array.from({ length: workerCount }, worker));
323
+ if (failure)
324
+ throw failure.error;
325
+ return results;
326
+ }
327
+ function normalizeLimit(limit) {
328
+ return Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : DEFAULT_CONCURRENCY;
329
+ }
330
+ class Semaphore {
331
+ constructor(limit) {
332
+ this.limit = limit;
333
+ this.active = 0;
334
+ this.waiting = [];
335
+ }
336
+ async run(task) {
337
+ await this.acquire();
338
+ try {
339
+ return await task();
340
+ }
341
+ finally {
342
+ this.release();
343
+ }
344
+ }
345
+ acquire() {
346
+ if (this.active < this.limit) {
347
+ this.active++;
348
+ return Promise.resolve();
349
+ }
350
+ return new Promise(resolve => this.waiting.push(resolve));
351
+ }
352
+ release() {
353
+ const next = this.waiting.shift();
354
+ if (next) {
355
+ next();
356
+ }
357
+ else {
358
+ this.active--;
359
+ }
360
+ }
361
+ }
362
+
284
363
  const noopProgress = {
285
364
  update() { },
286
365
  succeed() { },
@@ -12948,13 +13027,14 @@ function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, oldHeader
12948
13027
  return ret.join('\n') + '\n';
12949
13028
  }
12950
13029
 
12951
- async function buildPlan(modules, http, rootDir, options = {}) {
13030
+ async function buildPlan(modules, unlimitedHttp, rootDir, options = {}) {
12952
13031
  const { detailed = false, detectRenames = true, reporter = silentReporter, concurrency = DEFAULT_CONCURRENCY, source } = options;
13032
+ const http = limitConcurrency(unlimitedHttp, concurrency);
12953
13033
  const modulePlans = [];
12954
13034
  for (const mod of modules) {
12955
13035
  const [locals, remotes] = await Promise.all([
12956
13036
  reporter.task(`Reading local ${mod.displayName} files`, () => mod.readLocal(rootDir, source)),
12957
- reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http, { concurrency }))
13037
+ reporter.task(`Fetching remote ${mod.displayName}`, () => mod.readRemote(http))
12958
13038
  ]);
12959
13039
  const localBySlug = new Map(locals.map(l => [l.slug, l]));
12960
13040
  const remoteBySlug = new Map(remotes.map(r => [r.slug, r]));
@@ -13033,29 +13113,30 @@ async function buildPlan(modules, http, rootDir, options = {}) {
13033
13113
  }
13034
13114
  return { modules: modulePlans };
13035
13115
  }
13036
- async function applyPlan(plan, http, opts = {}) {
13116
+ async function applyPlan(plan, unlimitedHttp, opts = {}) {
13037
13117
  const concurrency = opts.concurrency ?? DEFAULT_CONCURRENCY;
13038
13118
  const reporter = opts.reporter ?? silentReporter;
13039
13119
  const errors = [];
13120
+ const abortController = new AbortController();
13121
+ const http = limitConcurrency(unlimitedHttp, concurrency, abortController.signal);
13122
+ let abortError;
13040
13123
  const handleErr = (label, e) => {
13041
- const msg = formatError(e);
13042
- const full = `${label}: ${msg}`;
13043
- if (opts.abortOnError)
13044
- throw new Error(full);
13124
+ if (abortError)
13125
+ throw abortError;
13126
+ const full = `${label}: ${formatError(e)}`;
13127
+ if (opts.abortOnError) {
13128
+ abortError = new Error(full);
13129
+ abortController.abort(abortError);
13130
+ throw abortError;
13131
+ }
13045
13132
  reporter.warn(full);
13046
13133
  errors.push(full);
13047
13134
  };
13048
13135
  for (const mp of plan.modules) {
13049
13136
  const { module: mod, creates, updates, deletes } = mp;
13050
- if (deletes.length) {
13051
- await runConcurrently(deletes.map(e => ({ slug: e.slug, run: () => mod.delete(http, e.remote.id) })), concurrency, `Deleting ${mod.displayName}`, handleErr, reporter);
13052
- }
13053
- if (updates.length) {
13054
- await runConcurrently(updates.map(e => ({ slug: e.slug, run: () => mod.update(http, e.local, e.remote.id) })), concurrency, `Updating ${mod.displayName}`, handleErr, reporter);
13055
- }
13056
- if (creates.length) {
13057
- await runConcurrently(creates.map(e => ({ slug: e.slug, run: () => mod.create(http, e.local) })), concurrency, `Creating ${mod.displayName}`, handleErr, reporter);
13058
- }
13137
+ await runAllAtOnce(deletes.map(e => ({ slug: e.slug, run: () => mod.delete(http, e.remote.id) })), `Deleting ${mod.displayName}`, handleErr, reporter);
13138
+ await runAllAtOnce(updates.map(e => ({ slug: e.slug, run: () => mod.update(http, e.local, e.remote.id) })), `Updating ${mod.displayName}`, handleErr, reporter);
13139
+ await runAllAtOnce(creates.map(e => ({ slug: e.slug, run: () => mod.create(http, e.local) })), `Creating ${mod.displayName}`, handleErr, reporter);
13059
13140
  }
13060
13141
  return { errors };
13061
13142
  }
@@ -13082,6 +13163,9 @@ function renderSchemaDetail(localData, remoteData, ignoredFields) {
13082
13163
  const remoteYaml = YAML.stringify(omit(remoteData, ignoredFields));
13083
13164
  return { schema: buildUnifiedDiff(remoteYaml, localYaml, "schema.yaml") };
13084
13165
  }
13166
+ function runAllAtOnce(tasks, label, onError, reporter) {
13167
+ return runConcurrently(tasks, tasks.length, label, onError, reporter);
13168
+ }
13085
13169
  async function runConcurrently(tasks, concurrency, label, onError, reporter) {
13086
13170
  if (!tasks.length)
13087
13171
  return;
@@ -13089,21 +13173,13 @@ async function runConcurrently(tasks, concurrency, label, onError, reporter) {
13089
13173
  let done = 0;
13090
13174
  const progress = reporter.progress(label, total);
13091
13175
  try {
13092
- const limit = Math.max(1, Math.min(total, concurrency));
13093
- let batch = [];
13094
- for (let i = 0; i < tasks.length; i++) {
13095
- batch.push(tasks[i]);
13096
- if (batch.length === limit || i === tasks.length - 1) {
13097
- await Promise.all(batch.map(task => task
13098
- .run()
13099
- .catch((e) => onError(`${label}: ${task.slug}`, e))
13100
- .finally(() => {
13101
- done++;
13102
- progress.update(task.slug, done, total);
13103
- })));
13104
- batch = [];
13105
- }
13106
- }
13176
+ await mapWithConcurrency(tasks, concurrency, task => task
13177
+ .run()
13178
+ .catch((e) => onError(`${label}: ${task.slug}`, e))
13179
+ .finally(() => {
13180
+ done++;
13181
+ progress.update(task.slug, done, total);
13182
+ }));
13107
13183
  progress.succeed();
13108
13184
  }
13109
13185
  catch (e) {
@@ -15960,14 +16036,6 @@ function normalizeSchema(schema) {
15960
16036
  }
15961
16037
  return normalized;
15962
16038
  }
15963
- async function mapInBatches(items, size, fn) {
15964
- const limit = Math.max(1, size);
15965
- const results = [];
15966
- for (let i = 0; i < items.length; i += limit) {
15967
- results.push(...(await Promise.all(items.slice(i, i + limit).map(fn))));
15968
- }
15969
- return results;
15970
- }
15971
16039
  function isNotFoundError(error) {
15972
16040
  if (!error || typeof error !== "object")
15973
16041
  return false;
@@ -16031,10 +16099,10 @@ const functionModule = {
16031
16099
  }
16032
16100
  return results;
16033
16101
  },
16034
- async readRemote(http, options = {}) {
16102
+ async readRemote(http) {
16035
16103
  const res = await http.get("function");
16036
16104
  const fns = unwrapList(res);
16037
- return mapInBatches(fns, options.concurrency ?? DEFAULT_CONCURRENCY, async (fn) => {
16105
+ return Promise.all(fns.map(async (fn) => {
16038
16106
  const id = fn._id;
16039
16107
  const [indexRes, depsRes] = await Promise.all([
16040
16108
  http.get(`function/${id}/index`).catch(emptyIfMissing({ index: "" })),
@@ -16057,7 +16125,7 @@ const functionModule = {
16057
16125
  dependencies
16058
16126
  }
16059
16127
  };
16060
- });
16128
+ }));
16061
16129
  },
16062
16130
  async create(http, local) {
16063
16131
  const id = await this.createSchema(http, local.data.schema);