Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/37737-own-door-501-ja-vary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/runtime': patch
'@objectstack/spec': patch
---

fix(runtime): a 501 answered in the request's language says it varies by Accept-Language, and the inbound automation hooks' Japanese 501 says 自動化

Clause-②: no
Metadata-protocol: none

The `501 NOT_IMPLEMENTED` answers that are localized from the request's `Accept-Language` (the print, personal-view and attachment doors' bridge, the comment doors, the webhook redelivery door, the approval action-page door and the inbound automation hooks) now carry `Vary: Accept-Language`. A `501` is cacheable by default, so a shared cache in front of the API could otherwise hand one reader's language to the next; nothing else about these answers changes.

The two `ja-JP` sentences of the inbound automation hooks say `受信自動化フック`, the term the platform's Japanese bundles use, instead of `受信オートメーションフック`. The operation-message catalog's doc comments now say how a request's language is matched (`es-MX` reads `es-ES`, `zh` and `zh-TW` read `zh-CN`, `ja` reads `ja-JP`; only a language with no catalog locale answers English).
2 changes: 2 additions & 0 deletions packages/runtime/src/domains/approvals-act.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ describe('[#37662] the action-page door\'s 501 follows the request\'s language',
const label = JSON.stringify(headers);
const result = await dispatcher.dispatch('GET', ACT, undefined, {}, ctx(request));
expect(envelope(result), label).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, label).toBe('Accept-Language');
expect(result.response?.body?.error?.message, label).toBe(site.sentences[locale]);
expect(result.response?.body?.error?.userMessage, label).toBeUndefined();
}
Expand Down
19 changes: 3 additions & 16 deletions packages/runtime/src/domains/approvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@
*/

import { INTERNAL_ERROR_MESSAGE } from '@objectstack/types';
import { operationUserMessage } from '@objectstack/core';
import type { IApprovalService } from '@objectstack/spec/contracts';
import type { HttpProtocolContext, HttpDispatcherResult } from '../http-dispatcher.js';
import type { DomainHandlerDeps, DomainRoute } from '../domain-handler-registry.js';
import { requestLocaleOf } from './own-door-bridge.js';
import { catalogNotImplemented } from './own-door-bridge.js';

/**
* The one path this domain claims — exactly, with no sub-paths: the action
Expand Down Expand Up @@ -114,22 +113,10 @@ export async function handleApprovalsActRequest(
// [#37662] The `501` is the operation-message catalog's, in the request's
// top `Accept-Language` tag, as the own-door bridge answers its own.
if (!service) {
return {
handled: true,
response: deps.error(
operationUserMessage('approvals_act_service_unregistered', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'approvals_act_service_unregistered');
}
if (typeof service.handleActionPage !== 'function') {
return {
handled: true,
response: deps.error(
operationUserMessage('approvals_act_member_missing', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'approvals_act_member_missing');
}

const request = context.request as Request;
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime/src/domains/automation-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,13 @@ describe('[#37662] the inbound hook route\'s 501 follows the request\'s language
unregistered: {
en: 'Inbound automation hooks are not available: no trigger-api service is registered. The inbound-hook trigger (@objectstack/trigger-api) serves this route through that service; compose it on the environment that owns the flow.',
'zh-CN': '入站自动化钩子不可用:未注册 trigger-api 服务。入站钩子触发器(@objectstack/trigger-api)通过该服务提供此路由;请在拥有该流程的环境上组合它。',
'ja-JP': '受信オートメーションフックは利用できません。trigger-api サービスが登録されていません。受信フックトリガー(@objectstack/trigger-api)がこのサービスを通じてこのルートを提供します。フローを所有する環境に組み込んでください。',
'ja-JP': '受信自動化フックは利用できません。trigger-api サービスが登録されていません。受信フックトリガー(@objectstack/trigger-api)がこのサービスを通じてこのルートを提供します。フローを所有する環境に組み込んでください。',
'es-ES': 'Los ganchos de automatización entrantes no están disponibles: no hay ningún servicio trigger-api registrado. El disparador de ganchos entrantes (@objectstack/trigger-api) sirve esta ruta a través de ese servicio; compóngalo en el entorno al que pertenece el flujo.',
},
memberMissing: {
en: 'Inbound automation hooks are not available: the registered trigger-api service does not implement handleInboundHook.',
'zh-CN': '入站自动化钩子不可用:已注册的 trigger-api 服务未实现 handleInboundHook。',
'ja-JP': '受信オートメーションフックは利用できません。登録されている trigger-api サービスは handleInboundHook を実装していません。',
'ja-JP': '受信自動化フックは利用できません。登録されている trigger-api サービスは handleInboundHook を実装していません。',
'es-ES': 'Los ganchos de automatización entrantes no están disponibles: el servicio trigger-api registrado no implementa handleInboundHook.',
},
};
Expand All @@ -969,6 +969,8 @@ describe('[#37662] the inbound hook route\'s 501 follows the request\'s language
const label = JSON.stringify(headers);
const result = await dispatcher.dispatch('POST', ROUTE, {}, {}, ctx(request));
expect(envelope(result), label).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, label).toBe('Accept-Language');
expect(result.response?.body?.error?.message, label).toBe(site.sentences[locale]);
expect(result.response?.body?.error?.userMessage, label).toBeUndefined();
}
Expand Down
19 changes: 3 additions & 16 deletions packages/runtime/src/domains/automation-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ import { INTERNAL_ERROR_MESSAGE } from '@objectstack/types';
import {
admitInboundHookAddress,
inboundHookRefusalResponse,
operationUserMessage,
type InboundHookRouteMatch,
} from '@objectstack/core';
import { automationAddressRegistryOf } from './automation-address.js';
import type { ITriggerApiService } from '@objectstack/spec/contracts';
import type { HttpProtocolContext, HttpDispatcherResult } from '../http-dispatcher.js';
import type { DomainHandlerDeps } from '../domain-handler-registry.js';
import { requestLocaleOf } from './own-door-bridge.js';
import { catalogNotImplemented } from './own-door-bridge.js';

/** The slot the member lives on — the trigger package's own name (ADR-0041 §2). */
const TRIGGER_API_SLOT = 'trigger-api';
Expand Down Expand Up @@ -187,22 +186,10 @@ export async function handleInboundHookRequest(
// [#37662] The `501` is the operation-message catalog's, in the request's
// top `Accept-Language` tag, as the own-door bridge answers its own.
if (!service) {
return {
handled: true,
response: deps.error(
operationUserMessage('automation_hooks_service_unregistered', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'automation_hooks_service_unregistered');
}
if (typeof service.handleInboundHook !== 'function') {
return {
handled: true,
response: deps.error(
operationUserMessage('automation_hooks_member_missing', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'automation_hooks_member_missing');
}

const request = context.request as Request;
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/src/domains/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ describe('[#37610] the comment doors\' 501 follows the request\'s language', ()
const label = `${method} ${path} ${JSON.stringify(headers)}`;
const result = await dispatcher.dispatch(method, path, {}, {}, ctx(request));
expect(envelope(result), label).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, label).toBe('Accept-Language');
expect(result.response?.body?.error?.message, label).toBe(site.sentences[locale]);
expect(result.response?.body?.error?.userMessage, label).toBeUndefined();
}
Expand Down
19 changes: 3 additions & 16 deletions packages/runtime/src/domains/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@
*/

import { INTERNAL_ERROR_MESSAGE } from '@objectstack/types';
import { operationUserMessage } from '@objectstack/core';
import type { ICommentDoorsService } from '@objectstack/spec/contracts';
import type { HttpProtocolContext, HttpDispatcherResult } from '../http-dispatcher.js';
import type { DomainHandlerDeps, DomainRequest, DomainRoute } from '../domain-handler-registry.js';
import { requestLocaleOf } from './own-door-bridge.js';
import { catalogNotImplemented } from './own-door-bridge.js';

/** The prefix the domain claims (a segment claim, narrowed by the handler). */
export const COMMENTS_DOMAIN_PREFIX = '/comments';
Expand Down Expand Up @@ -163,22 +162,10 @@ export async function handleCommentsRequest(
// [#37610] The `501` is the operation-message catalog's, in the request's
// top `Accept-Language` tag, as the own-door bridge answers its own.
if (!service) {
return {
handled: true,
response: deps.error(
operationUserMessage('comment_doors_service_unregistered', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'comment_doors_service_unregistered');
}
if (typeof service.handleCommentDoor !== 'function') {
return {
handled: true,
response: deps.error(
operationUserMessage('comment_doors_member_missing', { locale: requestLocaleOf(context) }),
501,
),
};
return catalogNotImplemented(deps, context, 'comment_doors_member_missing');
}

const request = context.request as Request;
Expand Down
23 changes: 19 additions & 4 deletions packages/runtime/src/domains/own-door-bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ describe('[#37273] the print door\'s 501 follows the request\'s language', () =>
const { dispatcher } = makeDispatcher(site.services);
const result = await dispatcher.dispatch(method, path, {}, {}, withHeaders({ 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8' }));
expect(envelope(result)).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, 'Vary').toBe('Accept-Language');
expect(result.response?.body?.error?.message).toBe(site.zh);
expect(result.response?.body?.error?.userMessage).toBeUndefined();
});
Expand All @@ -415,6 +417,8 @@ describe('[#37273] the print door\'s 501 follows the request\'s language', () =>
const { dispatcher } = makeDispatcher(site.services);
const result = await dispatcher.dispatch(method, path, {}, {}, withHeaders(headers));
expect(envelope(result), JSON.stringify(headers)).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, JSON.stringify(headers)).toBe('Accept-Language');
expect(result.response?.body?.error?.message, JSON.stringify(headers)).toBe(site.en);
}
});
Expand Down Expand Up @@ -495,6 +499,8 @@ describe('[#37352] the personal-view and attachment doors\' 501s follow the requ
const label = `${request[0]} ${request[1]} ${JSON.stringify(headers)}`;
const result = await answer(site.services, request, headers);
expect(envelope(result), label).toEqual({ status: 501, code: 'NOT_IMPLEMENTED' });
// [#37737] The body is in the request's language, so the answer says it varies by it.
expect(result.response?.headers?.Vary, label).toBe('Accept-Language');
expect(result.response?.body?.error?.message, label).toBe(site.sentences[locale]);
expect(result.response?.body?.error?.userMessage, label).toBeUndefined();
}
Expand All @@ -514,8 +520,10 @@ describe('[#37352] the personal-view and attachment doors\' 501s follow the requ
/**
* [#37662] Every bridge's flags against the catalog, found rather than listed:
* each module of this directory that builds a bridge domain
* (`createOwnDoorBridgeDomain(`) is imported and its exported bridges walked,
* so a fourth bridge is held to the same rules the day it lands.
* (`createOwnDoorBridgeDomain(`) or answers through the bridge's handler
* directly (`handleOwnDoorBridgeRequest(`, #37737) is imported and its
* exported bridges walked, so a fourth bridge is held to the same rules the
* day it lands, whichever way it is mounted.
*/
describe('[#37662] every own-door bridge is tied to the catalog', () => {
const isBridge = (value: unknown): value is OwnDoorBridge =>
Expand All @@ -526,7 +534,10 @@ describe('[#37662] every own-door bridge is tied to the catalog', () => {
const here = dirname(fileURLToPath(import.meta.url));
const builders = readdirSync(here)
.filter((name) => name.endsWith('.ts') && !name.endsWith('.test.ts') && name !== 'own-door-bridge.ts')
.filter((name) => readFileSync(join(here, name), 'utf8').includes('createOwnDoorBridgeDomain('));
.filter((name) => {
const source = readFileSync(join(here, name), 'utf8');
return source.includes('createOwnDoorBridgeDomain(') || source.includes('handleOwnDoorBridgeRequest(');
});

it('finds the shipped bridges, each module exporting the bridge it builds', async () => {
const found: Array<[string, OwnDoorBridge]> = [];
Expand All @@ -546,8 +557,12 @@ describe('[#37662] every own-door bridge is tied to the catalog', () => {
expect(BUILTIN_OPERATION_MESSAGES[locale][bridge.nounKey], `${exported} ${locale}`).toBeTruthy();
}
expect(BUILTIN_OPERATION_MESSAGES.en[bridge.nounKey], exported).toBe(bridge.noun);
// [#37737] The English noun names its door(s) last, so whether it is
// plural is read off the word and never guessed (`the … endpoints`
// would read as plural with no flag to say so).
expect(bridge.noun, exported).toMatch(/\bdoors?$/);
// `nounPlural` is set exactly when the English noun is plural (`the … doors`).
expect(bridge.nounPlural === true, `${exported}: '${bridge.noun}'`).toBe(/\bdoors\b/.test(bridge.noun));
expect(bridge.nounPlural === true, `${exported}: '${bridge.noun}'`).toBe(/\bdoors$/.test(bridge.noun));
// The pair of sentences the bridge reads is in every locale.
const suffix = bridge.nounPlural ? '_plural' : '';
for (const key of ['own_door_service_unregistered', 'own_door_member_missing']) {
Expand Down
55 changes: 38 additions & 17 deletions packages/runtime/src/domains/own-door-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export interface OwnDoorBridge {
readonly member: string;
/** The package that registers the slot, named in the `501` for an empty slot. */
readonly producer: string;
/** The doors' noun for answers and log lines, e.g. `the personal view door`. */
/**
* The doors' noun for answers and log lines, e.g. `the personal view door`.
* It ends in `door` or `doors`, which is what says it is plural
* (`nounPlural`), pinned by `own-door-bridge.test.ts`.
*/
readonly noun: string;
/**
* [#37273] The operation-message catalog key of {@link noun} (its English
Expand Down Expand Up @@ -188,23 +192,11 @@ export async function handleOwnDoorBridgeRequest(
service = undefined;
}
if (!service) {
return {
handled: true,
response: deps.error(
notImplementedSentence(bridge, context, 'own_door_service_unregistered', { producer: bridge.producer }),
501,
),
};
return localizedNotImplemented(deps, notImplementedSentence(bridge, context, 'own_door_service_unregistered', { producer: bridge.producer }));
}
const member = service[bridge.member];
if (typeof member !== 'function') {
return {
handled: true,
response: deps.error(
notImplementedSentence(bridge, context, 'own_door_member_missing', { member: bridge.member }),
501,
),
};
return localizedNotImplemented(deps, notImplementedSentence(bridge, context, 'own_door_member_missing', { member: bridge.member }));
}

const request = context.request as Request;
Expand Down Expand Up @@ -236,6 +228,32 @@ export async function handleOwnDoorBridgeRequest(
}
}

/**
* [#37737] The answer for a `501` whose sentence is in the request's language
* (read by {@link requestLocaleOf}), with the `Vary: Accept-Language` such a
* body owes. A `501` is cacheable by default (RFC 9111 §4.2.2: a shared cache
* may store it with no explicit freshness), so without the header a cache in
* front of the API could hand one reader's language to the next. Every door
* that answers a localized `501` answers through this.
*/
export function localizedNotImplemented(deps: DomainHandlerDeps, message: string): HttpDispatcherResult {
const response = deps.error(message, 501) as NonNullable<HttpDispatcherResult['response']>;
return { handled: true, response: { ...response, headers: { ...(response.headers ?? {}), Vary: 'Accept-Language' } } };
}

/**
* [#37737] {@link localizedNotImplemented} for one sentence of the operation
* message catalog, in the request's top `Accept-Language` tag.
*/
export function catalogNotImplemented(
deps: DomainHandlerDeps,
context: HttpProtocolContext,
key: string,
params?: Record<string, unknown>,
): HttpDispatcherResult {
return localizedNotImplemented(deps, operationUserMessage(key, { locale: requestLocaleOf(context) }, params));
}

/** A `501` sentence of the catalog, in the request's top `Accept-Language` tag. */
function notImplementedSentence(
bridge: OwnDoorBridge,
Expand All @@ -251,8 +269,11 @@ function notImplementedSentence(

/**
* The request's top `Accept-Language` tag, from a fetch `Request` or a plain
* header record: the language of an own door's `501` (#37610: the comment and
* webhook redelivery doors read it too).
* header record: the language of an own door's `501`. The doors that are not
* bridges read it for their own `501`s too: the comment doors (`comments.ts`)
* and the webhook redelivery door (`webhooks.ts`, both #37610), the approval
* action-page door (`approvals.ts`) and the inbound automation hooks
* (`automation-hooks.ts`, both #37662).
*/
export function requestLocaleOf(context: HttpProtocolContext): string | undefined {
const headers = (context.request as { headers?: unknown } | undefined)?.headers as
Expand Down
Loading
Loading