@hadoku/service-kernel 0.1.230 → 0.1.231

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.
@@ -44,7 +44,15 @@ export declare function creditsIn(text: string): string[];
44
44
  export declare function creditMark(name: string): string;
45
45
  /** Notes text with every `@name` credit shown as `creditMark(name)`: nobody is pinged. */
46
46
  export declare function showCredits(text: string): string;
47
- export declare function releaseEmbeds(notes: ReleaseNotes, appUrl: string): Embed[];
47
+ /**
48
+ * A post as the Discord messages it takes, in order, each a list of embeds: the text cut into
49
+ * messages of at most 6000 characters with the title, and each message into embeds of at most 4096,
50
+ * both by `splitBody`, so between whole sections where they fit and otherwise between lines (a
51
+ * bullet is one line by then), never inside one. The title heads the first message only. A text
52
+ * that fits one message comes out exactly as it did when every post was one message.
53
+ */
54
+ export declare function postMessages(title: string, body: string, url: string): Embed[][];
55
+ export declare function releaseMessages(notes: ReleaseNotes, appUrl: string): Embed[][];
48
56
  export interface CreditCount {
49
57
  name: string;
50
58
  count: number;
@@ -84,10 +92,13 @@ export interface AnnounceOptions {
84
92
  now?: () => Date;
85
93
  }
86
94
  /**
87
- * At boot, in this order: delete the credits post of every older release (404, none on record, is
88
- * the usual answer); post the current release's notes; re-send every release's notes as edits, so a
89
- * corrected notes file (a credit added, a typo) changes its post; then post the current release's
90
- * credits, or edit them when already posted. ArchiveBot edits only a post whose shown text differs
91
- * and never posts from an edit. Resolves on every outcome; each is logged, never thrown.
95
+ * At boot, in this order: delete every message of every older release's credits post (404, none on
96
+ * record, is the usual answer); post the current release's notes, each message ArchiveBot has no
97
+ * record of; re-send every release's notes as edits, so a corrected notes file (a credit added, a
98
+ * typo) changes its post, and a shorter one drops the messages it no longer needs; then post the
99
+ * current release's credits, or edit them when already posted. When this boot posted a message of
100
+ * notes whose credits may already stand (some of the notes were posted before), the credits are
101
+ * deleted and posted again, so they stay under the last message. Resolves on every outcome; each is
102
+ * logged, never thrown.
92
103
  */
93
104
  export declare function announceRelease(options: AnnounceOptions): Promise<void>;
@@ -2,9 +2,11 @@
2
2
  * Prod posts its release's patch notes to a Discord server's #announcements at boot, through
3
3
  * ArchiveBot's signed `/api/messages/send` (contract: docs/requests/ARCHIVEBOT_DISCORD_INTEGRATION_REPLY.md).
4
4
  *
5
- * The release is the newest `vX.Y.Z.md` release notes file in the tree. Every boot sends; the
6
- * idempotency key `<service>-release-<version>` is deduped by ArchiveBot for 7 days, so a restart
7
- * posts nothing new.
5
+ * The release is the newest `vX.Y.Z.md` release notes file in the tree. Discord takes at most 6000
6
+ * characters a message, so notes longer than that go out as several messages, in order, cut between
7
+ * sections or bullets (`postMessages`). Every boot sends each of them; ArchiveBot keeps a record per
8
+ * idempotency key (`<service>-release-<version>` for the first, `…-part2` and on for the rest) and
9
+ * answers a key it has as already posted, so a restart posts only a part it lacks.
8
10
  *
9
11
  * Whoever a bullet credits is named in it as `@name` (docs/RELEASE.md § "Attribution"). The post
10
12
  * shows each as ***__name__*** (bold, italic, underlined) and pings nobody: the operator, 2026-11-09,
@@ -28,8 +30,11 @@
28
30
  import { readdir, readFile } from 'node:fs/promises';
29
31
  import { join } from 'node:path';
30
32
  import { archivebotPost } from "./archivebot.js";
31
- /** ArchiveBot's per-embed description cap. */
33
+ /** Discord's (and ArchiveBot's) per-embed description cap. */
32
34
  const DESCRIPTION_MAX = 4096;
35
+ /** Discord's cap on one message's embeds: their titles and descriptions together. */
36
+ const MESSAGE_MAX = 6000;
37
+ const COLOR = 0x2f855a;
33
38
  /** The newest release notes in `dir`, by `ordering`, or null when there are none. */
34
39
  export async function latestRelease(dir, ordering) {
35
40
  const newest = ordering.newest(await readdir(dir));
@@ -135,13 +140,18 @@ export function creditMark(name) {
135
140
  export function showCredits(text) {
136
141
  return text.replace(CREDIT, (_, name) => creditMark(name));
137
142
  }
138
- export function releaseEmbeds(notes, appUrl) {
139
- return splitBody(showCredits(forDiscord(notes.body))).map((description, i) => ({
140
- ...(i === 0 ? { title: notes.title } : {}),
141
- description,
142
- url: appUrl,
143
- color: 0x2f855a,
144
- }));
143
+ /**
144
+ * A post as the Discord messages it takes, in order, each a list of embeds: the text cut into
145
+ * messages of at most 6000 characters with the title, and each message into embeds of at most 4096,
146
+ * both by `splitBody`, so between whole sections where they fit and otherwise between lines (a
147
+ * bullet is one line by then), never inside one. The title heads the first message only. A text
148
+ * that fits one message comes out exactly as it did when every post was one message.
149
+ */
150
+ export function postMessages(title, body, url) {
151
+ return splitBody(body, MESSAGE_MAX - title.length).map((text, m) => splitBody(text).map((description, i) => ({ ...(m === 0 && i === 0 ? { title } : {}), description, url, color: COLOR })));
152
+ }
153
+ export function releaseMessages(notes, appUrl) {
154
+ return postMessages(notes.title, showCredits(forDiscord(notes.body)), appUrl);
145
155
  }
146
156
  /**
147
157
  * How many things each person is credited with across `releases`: one per line of notes (a
@@ -186,23 +196,109 @@ export function creditsBody(releases, earlier) {
186
196
  parts.push(`Extra thanks to ${listed(earlier)}, who contributed many things before the release notes started crediting anyone.`);
187
197
  return parts.join('\n\n');
188
198
  }
199
+ const partKey = (key, i) => (i === 0 ? key : `${key}-part${i + 1}`);
189
200
  /**
190
- * The signed ArchiveBot request for one release's notes: its idempotency key is the release's own,
191
- * its record is kept for good (`retain`), and an edit with no record finds the post by its title's
192
- * version (`find`), so a release of any age can still be corrected.
201
+ * A release's notes as a post: its record is kept for good (`retain`), and an edit with no record
202
+ * finds the post by its title's version (`find`), so a release of any age can still be corrected.
193
203
  */
194
- function notesPayload(service, notes, appUrl, now) {
204
+ function notesPost(service, notes, appUrl) {
195
205
  const titlePrefix = notes.title.startsWith(`${notes.version} `) ? `${notes.version} ` : notes.title;
196
- return JSON.stringify({
197
- idempotencyKey: `${service}-release-${notes.version}`,
198
- timestamp: now.getTime(),
206
+ return { key: `${service}-release-${notes.version}`, version: notes.version, messages: releaseMessages(notes, appUrl), find: { titlePrefix } };
207
+ }
208
+ const creditsKey = (service, version) => `${service}-credits-${version}`;
209
+ /** A log line's part: which of how many, and nothing for a post of one message, as before there were parts. */
210
+ const partOf = (i, n) => (n === 1 ? {} : { part: i + 1, parts: n });
211
+ const refused = (res) => ({ status: res.status, reply: res.raw.slice(0, 500) });
212
+ const thrown = (err) => ({ error: err instanceof Error ? err.message : String(err) });
213
+ /** The requests one post's messages go out as, through ArchiveBot, each outcome logged under `event`. */
214
+ function poster(options) {
215
+ const { log, service } = options;
216
+ const now = () => (options.now ?? (() => new Date()))();
217
+ const request = (path, post, i) => archivebotPost(options, path, JSON.stringify({
218
+ idempotencyKey: partKey(post.key, i),
219
+ timestamp: now().getTime(),
199
220
  source: service,
200
221
  retain: true,
201
- find: { titlePrefix },
202
- embeds: releaseEmbeds(notes, appUrl),
203
- });
222
+ ...(i === 0 && post.find ? { find: post.find } : {}),
223
+ embeds: post.messages[i],
224
+ }));
225
+ /**
226
+ * Send every message in order; ArchiveBot posts the ones it has no record of and answers the
227
+ * rest as deduped. Stops at the first failure, so the posted messages are always the first
228
+ * ones and the next boot carries on from there. Null on a failure, else how many it posted.
229
+ */
230
+ async function send(event, post) {
231
+ const n = post.messages.length;
232
+ let posted = 0;
233
+ for (let i = 0; i < n; i++) {
234
+ const at = { event, version: post.version, ...partOf(i, n) };
235
+ try {
236
+ const res = await request('/api/messages/send', post, i);
237
+ if (!res.ok) {
238
+ log({ ...at, outcome: 'failed', ...refused(res) });
239
+ return null;
240
+ }
241
+ const deduped = res.answer.deduped === true;
242
+ if (!deduped)
243
+ posted += 1;
244
+ log({ ...at, outcome: deduped ? 'already-posted' : 'posted' });
245
+ }
246
+ catch (err) {
247
+ log({ ...at, outcome: 'failed', ...thrown(err) });
248
+ return null;
249
+ }
250
+ }
251
+ return { posted, had: n - posted };
252
+ }
253
+ /**
254
+ * Re-send every message as an edit, which ArchiveBot makes only when the shown text differs and
255
+ * which never posts; then delete the messages past the last, left by a longer text. A message
256
+ * with no post stops it, since those after it have none either: the first is the post never
257
+ * made (nothing to say), a later one is logged as `missing`, never posted here, where it would
258
+ * land under newer posts.
259
+ */
260
+ async function edit(event, post) {
261
+ const n = post.messages.length;
262
+ for (let i = 0; i < n; i++) {
263
+ const at = { event, version: post.version, ...partOf(i, n) };
264
+ try {
265
+ const res = await request('/api/messages/edit', post, i);
266
+ if (res.status === 404) {
267
+ if (i > 0)
268
+ log({ ...at, outcome: 'missing' });
269
+ return;
270
+ }
271
+ if (!res.ok)
272
+ log({ ...at, outcome: 'failed', ...refused(res) });
273
+ else if (res.answer.edited === true)
274
+ log({ ...at, outcome: 'edited' });
275
+ }
276
+ catch (err) {
277
+ log({ ...at, outcome: 'failed', ...thrown(err) });
278
+ }
279
+ }
280
+ await remove(event, post.key, post.version, n);
281
+ }
282
+ /** Delete a post's messages from the `from`-th on, until one has no record (404, the usual answer). */
283
+ async function remove(event, key, version, from = 0) {
284
+ for (let i = from;; i++) {
285
+ const at = { event, version, ...(i === 0 ? {} : { part: i + 1 }) };
286
+ try {
287
+ const res = await archivebotPost(options, '/api/messages/delete', JSON.stringify({ idempotencyKey: partKey(key, i), timestamp: now().getTime(), source: service }));
288
+ if (res.status === 404)
289
+ return;
290
+ log({ ...at, outcome: res.ok ? 'deleted' : 'failed', ...(res.ok ? {} : refused(res)) });
291
+ if (!res.ok)
292
+ return;
293
+ }
294
+ catch (err) {
295
+ log({ ...at, outcome: 'failed', ...thrown(err) });
296
+ return;
297
+ }
298
+ }
299
+ }
300
+ return { send, edit, remove };
204
301
  }
205
- const creditsKey = (service, version) => `${service}-credits-${version}`;
206
302
  async function readEarlierCredits(file) {
207
303
  if (file === undefined)
208
304
  return [];
@@ -216,11 +312,14 @@ async function readEarlierCredits(file) {
216
312
  }
217
313
  }
218
314
  /**
219
- * At boot, in this order: delete the credits post of every older release (404, none on record, is
220
- * the usual answer); post the current release's notes; re-send every release's notes as edits, so a
221
- * corrected notes file (a credit added, a typo) changes its post; then post the current release's
222
- * credits, or edit them when already posted. ArchiveBot edits only a post whose shown text differs
223
- * and never posts from an edit. Resolves on every outcome; each is logged, never thrown.
315
+ * At boot, in this order: delete every message of every older release's credits post (404, none on
316
+ * record, is the usual answer); post the current release's notes, each message ArchiveBot has no
317
+ * record of; re-send every release's notes as edits, so a corrected notes file (a credit added, a
318
+ * typo) changes its post, and a shorter one drops the messages it no longer needs; then post the
319
+ * current release's credits, or edit them when already posted. When this boot posted a message of
320
+ * notes whose credits may already stand (some of the notes were posted before), the credits are
321
+ * deleted and posted again, so they stay under the last message. Resolves on every outcome; each is
322
+ * logged, never thrown.
224
323
  */
225
324
  export async function announceRelease(options) {
226
325
  const { log, service, ordering } = options;
@@ -228,13 +327,12 @@ export async function announceRelease(options) {
228
327
  log({ event: 'release-announce', outcome: 'skipped', reason: `no ${options.secretEnvName}` });
229
328
  return;
230
329
  }
231
- const now = () => (options.now ?? (() => new Date()))();
232
330
  let releases;
233
331
  try {
234
332
  releases = await allReleases(options.releasesDir, ordering);
235
333
  }
236
334
  catch (err) {
237
- log({ event: 'release-announce', outcome: 'failed', error: err instanceof Error ? err.message : String(err) });
335
+ log({ event: 'release-announce', outcome: 'failed', ...thrown(err) });
238
336
  return;
239
337
  }
240
338
  const notes = releases[0];
@@ -242,73 +340,28 @@ export async function announceRelease(options) {
242
340
  log({ event: 'release-announce', outcome: 'skipped', reason: `no release notes in ${options.releasesDir}` });
243
341
  return;
244
342
  }
245
- for (const older of releases.slice(1)) {
246
- try {
247
- const res = await archivebotPost(options, '/api/messages/delete', JSON.stringify({ idempotencyKey: creditsKey(service, older.version), timestamp: now().getTime(), source: service }));
248
- if (res.status === 404)
249
- continue;
250
- log({ event: 'release-credits', outcome: res.ok ? 'deleted' : 'failed', version: older.version, ...(res.ok ? {} : { status: res.status, reply: res.raw.slice(0, 500) }) });
251
- }
252
- catch (err) {
253
- log({ event: 'release-credits', outcome: 'failed', version: older.version, error: err instanceof Error ? err.message : String(err) });
254
- }
255
- }
256
- try {
257
- const res = await archivebotPost(options, '/api/messages/send', notesPayload(service, notes, options.appUrl, now()));
258
- if (!res.ok) {
259
- log({ event: 'release-announce', outcome: 'failed', version: notes.version, status: res.status, reply: res.raw.slice(0, 500) });
260
- return;
261
- }
262
- log({ event: 'release-announce', outcome: res.answer.deduped === true ? 'already-posted' : 'posted', version: notes.version });
263
- }
264
- catch (err) {
265
- log({ event: 'release-announce', outcome: 'failed', version: notes.version, error: err instanceof Error ? err.message : String(err) });
343
+ const post = poster(options);
344
+ for (const older of releases.slice(1))
345
+ await post.remove('release-credits', creditsKey(service, older.version), older.version);
346
+ const sent = await post.send('release-announce', notesPost(service, notes, options.appUrl));
347
+ if (sent === null)
266
348
  return;
267
- }
268
- for (const release of releases) {
269
- try {
270
- const res = await archivebotPost(options, '/api/messages/edit', notesPayload(service, release, options.appUrl, now()));
271
- // 404: no post for it, on record or in the channel's history: nothing to change.
272
- if (res.status === 404)
273
- continue;
274
- if (!res.ok) {
275
- log({ event: 'release-edit', outcome: 'failed', version: release.version, status: res.status, reply: res.raw.slice(0, 500) });
276
- continue;
277
- }
278
- if (res.answer.edited === true)
279
- log({ event: 'release-edit', outcome: 'edited', version: release.version });
280
- }
281
- catch (err) {
282
- log({ event: 'release-edit', outcome: 'failed', version: release.version, error: err instanceof Error ? err.message : String(err) });
283
- }
284
- }
349
+ for (const release of releases)
350
+ await post.edit('release-edit', notesPost(service, release, options.appUrl));
351
+ let body;
285
352
  try {
286
- const body = creditsBody(releases, await readEarlierCredits(options.earlierCreditsFile));
287
- if (body === null)
288
- return;
289
- const payload = (at) => JSON.stringify({
290
- idempotencyKey: creditsKey(service, notes.version),
291
- timestamp: at.getTime(),
292
- source: service,
293
- retain: true,
294
- embeds: splitBody(body).map((description, i) => ({ ...(i === 0 ? { title: CREDITS_TITLE } : {}), description, url: options.appUrl, color: 0x2f855a })),
295
- });
296
- const sent = await archivebotPost(options, '/api/messages/send', payload(now()));
297
- if (!sent.ok) {
298
- log({ event: 'release-credits', outcome: 'failed', version: notes.version, status: sent.status, reply: sent.raw.slice(0, 500) });
299
- return;
300
- }
301
- if (sent.answer.deduped !== true) {
302
- log({ event: 'release-credits', outcome: 'posted', version: notes.version });
303
- return;
304
- }
305
- const edited = await archivebotPost(options, '/api/messages/edit', payload(now()));
306
- if (!edited.ok)
307
- log({ event: 'release-credits', outcome: 'failed', version: notes.version, status: edited.status, reply: edited.raw.slice(0, 500) });
308
- else if (edited.answer.edited === true)
309
- log({ event: 'release-credits', outcome: 'edited', version: notes.version });
353
+ body = creditsBody(releases, await readEarlierCredits(options.earlierCreditsFile));
310
354
  }
311
355
  catch (err) {
312
- log({ event: 'release-credits', outcome: 'failed', version: notes.version, error: err instanceof Error ? err.message : String(err) });
356
+ log({ event: 'release-credits', outcome: 'failed', version: notes.version, ...thrown(err) });
357
+ return;
313
358
  }
359
+ if (body === null)
360
+ return;
361
+ const credits = { key: creditsKey(service, notes.version), version: notes.version, messages: postMessages(CREDITS_TITLE, body, options.appUrl) };
362
+ if (sent.posted > 0 && sent.had > 0)
363
+ await post.remove('release-credits', credits.key, notes.version);
364
+ const creditsSent = await post.send('release-credits', credits);
365
+ if (creditsSent !== null && creditsSent.had > 0)
366
+ await post.edit('release-credits', credits);
314
367
  }
@@ -2,5 +2,5 @@ export { arrivedVia, busyHandler, isLocalProbe, type BusyReport, type BusyRun }
2
2
  export { bootClock } from './bootClock.js';
3
3
  export { closeConnections, sendThenFlush, type Closable, type Flushable } from './flush-sockets.js';
4
4
  export { toggleInspector } from './inspector.js';
5
- export { allReleases, announceRelease, creditCounts, creditMark, creditsBody, creditsIn, CREDITS_TITLE, forDiscord, latestRelease, parseNotes, releaseEmbeds, showCredits, splitBody, type AnnounceOptions, type CreditCount, type Embed, type ReleaseNotes, type ReleaseOrdering, } from './announce.js';
5
+ export { allReleases, announceRelease, creditCounts, creditMark, creditsBody, creditsIn, CREDITS_TITLE, forDiscord, latestRelease, parseNotes, postMessages, releaseMessages, showCredits, splitBody, type AnnounceOptions, type CreditCount, type Embed, type ReleaseNotes, type ReleaseOrdering, } from './announce.js';
6
6
  export { archivebotPost, type ArchivebotReply, type ArchivebotTarget } from './archivebot.js';
@@ -2,5 +2,5 @@ export { arrivedVia, busyHandler, isLocalProbe } from "./busy.js";
2
2
  export { bootClock } from "./bootClock.js";
3
3
  export { closeConnections, sendThenFlush } from "./flush-sockets.js";
4
4
  export { toggleInspector } from "./inspector.js";
5
- export { allReleases, announceRelease, creditCounts, creditMark, creditsBody, creditsIn, CREDITS_TITLE, forDiscord, latestRelease, parseNotes, releaseEmbeds, showCredits, splitBody, } from "./announce.js";
5
+ export { allReleases, announceRelease, creditCounts, creditMark, creditsBody, creditsIn, CREDITS_TITLE, forDiscord, latestRelease, parseNotes, postMessages, releaseMessages, showCredits, splitBody, } from "./announce.js";
6
6
  export { archivebotPost } from "./archivebot.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/service-kernel",
3
- "version": "0.1.230",
4
- "gitHead": "20683bcbf5bb6a1f9afba2273dae40f2964aaec9",
3
+ "version": "0.1.231",
4
+ "gitHead": "0962b3184641c70fa0ebdf6ace69eff2016b985b",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any game keeps: edge auth and the tier gate, base path and bundle check, health and the busy probe, lifecycle, the JSON logger, the state directory, telemetry and capacity, per-caller limits, the browser relays, the erase registry, and createGameServer, which boots a game from its config and plugins. Knows no game; the app binds its service id, prefix and env names. MIT; imports only node built-ins, express, socket.io, platform-wire and @wolffm/*.",
@@ -56,7 +56,7 @@
56
56
  }
57
57
  },
58
58
  "dependencies": {
59
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.197",
59
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.198",
60
60
  "express": "^5.1.0",
61
61
  "socket.io": "^4.8.3"
62
62
  },