@malloy-publisher/sdk 0.12.5 → 0.12.6

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@malloy-publisher/sdk",
3
3
  "description": "Malloy Publisher SDK",
4
- "version": "0.12.5",
4
+ "version": "0.12.6",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",
7
7
  "module": "dist/index.es.js",
@@ -259,7 +259,13 @@ describe("createDocument names", () => {
259
259
  expect(result.slug).toBe("sales-2");
260
260
  });
261
261
 
262
- it("on the package route, moves to the next name when the server answers 409", async () => {
262
+ it("on the package route, moves to the next name when a server from before the reason says the file exists", async () => {
263
+ // Both wordings an older server answered a create collision with, as a
264
+ // 409 with no `reason`.
265
+ const said = [
266
+ "`sales.malloy` already exists in the package. Send the `expectedHash` of the text you opened to replace it.",
267
+ "`sales-2.malloy` already exists in the package, so nothing was written. Choose another path, or, to replace a report, send the `expectedHash` of the text you opened.",
268
+ ];
263
269
  const tried: string[] = [];
264
270
  const result = await createDocument({
265
271
  layout: "grid",
@@ -271,7 +277,10 @@ describe("createDocument names", () => {
271
277
  tried.push(path);
272
278
  if (tried.length < 3)
273
279
  throw Object.assign(new Error("exists"), {
274
- response: { status: 409 },
280
+ response: {
281
+ status: 409,
282
+ data: { code: 409, message: said[tried.length - 1] },
283
+ },
275
284
  });
276
285
  },
277
286
  },
@@ -284,6 +293,119 @@ describe("createDocument names", () => {
284
293
  expect(result.slug).toBe("sales-3");
285
294
  });
286
295
 
296
+ it.each([
297
+ ["a package loading", "Package pkg is loading; try again shortly."],
298
+ [
299
+ "a materialization conflict",
300
+ "A materialization build of pkg is already running.",
301
+ ],
302
+ ["no message at all", undefined],
303
+ ])(
304
+ "on the package route, a reasonless 409 for %s is thrown at once, not read as a taken name",
305
+ async (_what, message) => {
306
+ let writes = 0;
307
+ const error = (await createDocument({
308
+ layout: "grid",
309
+ document: DOC,
310
+ target: {
311
+ route: "package",
312
+ existing: [],
313
+ write: async () => {
314
+ writes++;
315
+ throw Object.assign(new Error("Request failed with 409"), {
316
+ response: { status: 409, data: { code: 409, message } },
317
+ });
318
+ },
319
+ },
320
+ }).catch((e: unknown) => e)) as {
321
+ response?: { data?: { message?: string } };
322
+ };
323
+ expect(error.response?.data?.message).toBe(message);
324
+ expect(writes).toBe(1);
325
+ },
326
+ );
327
+
328
+ it("on the package route, moves to the next name only when the 409 says the file exists", async () => {
329
+ const tried: string[] = [];
330
+ const result = await createDocument({
331
+ layout: "grid",
332
+ document: DOC,
333
+ target: {
334
+ route: "package",
335
+ existing: [],
336
+ write: async (path) => {
337
+ tried.push(path);
338
+ if (tried.length < 2)
339
+ throw Object.assign(new Error("exists"), {
340
+ response: {
341
+ status: 409,
342
+ data: { code: 409, reason: "FILE_EXISTS" },
343
+ },
344
+ });
345
+ },
346
+ },
347
+ });
348
+ expect(tried).toEqual(["sales.malloy", "sales-2.malloy"]);
349
+ expect(result.slug).toBe("sales-2");
350
+ });
351
+
352
+ it.each(["PACKAGE_INSTALLED_FROM_LOCATION", "PACKAGE_IS_VERSIONED"])(
353
+ "on the package route, a 409 with reason %s is thrown at once with the server's message",
354
+ async (reason) => {
355
+ const message = `Package pkg cannot take this write (${reason}).`;
356
+ let writes = 0;
357
+ const events: unknown[] = [];
358
+ const refusal = createDocument({
359
+ layout: "grid",
360
+ document: DOC,
361
+ target: {
362
+ route: "package",
363
+ existing: [],
364
+ write: async () => {
365
+ writes++;
366
+ throw Object.assign(new Error("Request failed with 409"), {
367
+ response: {
368
+ status: 409,
369
+ data: { code: 409, message, reason },
370
+ },
371
+ });
372
+ },
373
+ },
374
+ onEvent: (e) => events.push(e),
375
+ });
376
+ // The error itself, so the dialog reads the server's message from it.
377
+ const error = (await refusal.catch((e: unknown) => e)) as {
378
+ response?: { data?: { message?: string } };
379
+ };
380
+ expect(error.response?.data?.message).toBe(message);
381
+ expect(writes).toBe(1);
382
+ expect(events).toHaveLength(1);
383
+ expect(events[0]).toMatchObject({ type: "report.create_refused" });
384
+ },
385
+ );
386
+
387
+ it("on the package route, a path given outright that a 409 refuses for another reason is not reported as taken", async () => {
388
+ await expect(
389
+ createDocument({
390
+ layout: "grid",
391
+ path: "team/sales.malloy",
392
+ document: DOC,
393
+ target: {
394
+ route: "package",
395
+ existing: [],
396
+ write: async () => {
397
+ throw Object.assign(new Error("installed from a location"), {
398
+ response: {
399
+ status: 409,
400
+ data: { reason: "PACKAGE_INSTALLED_FROM_LOCATION" },
401
+ },
402
+ });
403
+ },
404
+ },
405
+ }),
406
+ ).rejects.toThrow("installed from a location");
407
+ });
408
+
287
409
  it("on the package route, any other failure is thrown, not retried", async () => {
288
410
  let writes = 0;
289
411
  await expect(
@@ -400,7 +522,7 @@ describe("createDocument at a path", () => {
400
522
  expect(writes).toEqual([]);
401
523
  });
402
524
 
403
- it("refuses a path the server answers 409 for, once", async () => {
525
+ it("refuses a path the server answers 409 FILE_EXISTS for, once", async () => {
404
526
  const tried: string[] = [];
405
527
  await expect(
406
528
  createDocument({
@@ -412,7 +534,7 @@ describe("createDocument at a path", () => {
412
534
  write: async (path) => {
413
535
  tried.push(path);
414
536
  throw Object.assign(new Error("exists"), {
415
- response: { status: 409 },
537
+ response: { status: 409, data: { reason: "FILE_EXISTS" } },
416
538
  });
417
539
  },
418
540
  },
@@ -181,8 +181,10 @@ async function writeNewReport(
181
181
  path = candidate;
182
182
  break;
183
183
  } catch (error) {
184
- // A create-only write refused as a conflict means the listing was stale; the next name is free to try.
185
- if (!isConflict(error)) throw error;
184
+ // A file already at the path means the listing was stale, and the
185
+ // next name is free to try. Any other refusal (a package that takes
186
+ // no write in place) fails every name alike, so it is thrown as is.
187
+ if (!isFileExists(error)) throw error;
186
188
  }
187
189
  }
188
190
  if (path === undefined) throw noFreeName(document.title, options.path);
@@ -228,8 +230,40 @@ async function writeNewReport(
228
230
  return found;
229
231
  }
230
232
 
231
- const isConflict = (error: unknown) =>
232
- (error as { response?: { status?: number } })?.response?.status === 409;
233
+ /**
234
+ * What a server from before `reason` FILE_EXISTS said when a create found a
235
+ * file at its path: "`<path>` already exists in the package. Send the …" in
236
+ * the first release that had the write, then "`<path>` already exists in the
237
+ * package, so nothing was written. …". It answers other 409s on the same
238
+ * route with no `reason` too (a package loading or unloading, a
239
+ * materialization conflict), and those are not a taken name.
240
+ */
241
+ const OLD_SERVER_FILE_EXISTS = /^`[^`]+` already exists in the package\b/;
242
+
243
+ /**
244
+ * Whether a create-only write was refused because a file is already at the
245
+ * path: 409 with `reason` FILE_EXISTS, or, from a server that predates the
246
+ * reason, a reasonless 409 whose message is that server's for a file at the
247
+ * path (OLD_SERVER_FILE_EXISTS). Any other 409, with another reason
248
+ * (PACKAGE_IS_VERSIONED, PACKAGE_INSTALLED_FROM_LOCATION) or none, is not:
249
+ * it would refuse every name alike, so it is shown at once.
250
+ */
251
+ function isFileExists(error: unknown): boolean {
252
+ const response = (
253
+ error as {
254
+ response?: {
255
+ status?: number;
256
+ data?: { reason?: unknown; message?: unknown };
257
+ };
258
+ }
259
+ )?.response;
260
+ if (response?.status !== 409) return false;
261
+ const reason = response.data?.reason;
262
+ if (reason === "FILE_EXISTS") return true;
263
+ if (reason !== undefined && reason !== null) return false;
264
+ const message = response.data?.message;
265
+ return typeof message === "string" && OLD_SERVER_FILE_EXISTS.test(message);
266
+ }
233
267
 
234
268
  const noFreeName = (title: string, path?: string) =>
235
269
  new Error(