@owlmeans/server-api 0.1.18-rc.5 → 0.1.18-rc.50

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.
Files changed (103) hide show
  1. package/README.md +202 -46
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/server-api/SKILL.md +128 -22
  4. package/build/consts.local.d.ts +2 -0
  5. package/build/consts.local.d.ts.map +1 -0
  6. package/build/consts.local.js +2 -0
  7. package/build/consts.local.js.map +1 -0
  8. package/build/errors.d.ts +12 -0
  9. package/build/errors.d.ts.map +1 -1
  10. package/build/errors.js +16 -0
  11. package/build/errors.js.map +1 -1
  12. package/build/helper.d.ts +3 -6
  13. package/build/helper.d.ts.map +1 -1
  14. package/build/helper.js +10 -1
  15. package/build/helper.js.map +1 -1
  16. package/build/hold.d.ts +35 -0
  17. package/build/hold.d.ts.map +1 -0
  18. package/build/hold.js +57 -0
  19. package/build/hold.js.map +1 -0
  20. package/build/index.d.ts +2 -0
  21. package/build/index.d.ts.map +1 -1
  22. package/build/index.js +2 -0
  23. package/build/index.js.map +1 -1
  24. package/build/protocol.d.ts +18 -0
  25. package/build/protocol.d.ts.map +1 -0
  26. package/build/protocol.js +87 -0
  27. package/build/protocol.js.map +1 -0
  28. package/build/server.d.ts +2 -3
  29. package/build/server.d.ts.map +1 -1
  30. package/build/server.js +37 -32
  31. package/build/server.js.map +1 -1
  32. package/build/types.d.ts +31 -2
  33. package/build/types.d.ts.map +1 -1
  34. package/build/types.local.d.ts +38 -0
  35. package/build/types.local.d.ts.map +1 -0
  36. package/build/types.local.js +2 -0
  37. package/build/types.local.js.map +1 -0
  38. package/build/utils/consts.d.ts +2 -0
  39. package/build/utils/consts.d.ts.map +1 -0
  40. package/build/utils/consts.js +2 -0
  41. package/build/utils/consts.js.map +1 -0
  42. package/build/utils/consts.local.d.ts +5 -0
  43. package/build/utils/consts.local.d.ts.map +1 -0
  44. package/build/utils/consts.local.js +5 -0
  45. package/build/utils/consts.local.js.map +1 -0
  46. package/build/utils/error.d.ts +47 -1
  47. package/build/utils/error.d.ts.map +1 -1
  48. package/build/utils/error.js +192 -9
  49. package/build/utils/error.js.map +1 -1
  50. package/build/utils/guards.d.ts +1 -5
  51. package/build/utils/guards.d.ts.map +1 -1
  52. package/build/utils/guards.js +11 -15
  53. package/build/utils/guards.js.map +1 -1
  54. package/build/utils/index.d.ts +2 -0
  55. package/build/utils/index.d.ts.map +1 -1
  56. package/build/utils/index.js +1 -0
  57. package/build/utils/index.js.map +1 -1
  58. package/build/utils/log.d.ts +11 -0
  59. package/build/utils/log.d.ts.map +1 -0
  60. package/build/utils/log.js +57 -0
  61. package/build/utils/log.js.map +1 -0
  62. package/build/utils/payload.d.ts.map +1 -1
  63. package/build/utils/payload.js +6 -2
  64. package/build/utils/payload.js.map +1 -1
  65. package/build/utils/server.d.ts +3 -6
  66. package/build/utils/server.d.ts.map +1 -1
  67. package/build/utils/server.js +8 -9
  68. package/build/utils/server.js.map +1 -1
  69. package/build/utils/types.d.ts +18 -0
  70. package/build/utils/types.d.ts.map +1 -0
  71. package/build/utils/types.js +2 -0
  72. package/build/utils/types.js.map +1 -0
  73. package/build/utils/types.local.d.ts +9 -0
  74. package/build/utils/types.local.d.ts.map +1 -0
  75. package/build/utils/types.local.js +2 -0
  76. package/build/utils/types.local.js.map +1 -0
  77. package/package.json +22 -18
  78. package/src/consts.local.ts +1 -0
  79. package/src/errors.ts +18 -0
  80. package/src/helper.ts +13 -8
  81. package/src/hold.ts +66 -0
  82. package/src/index.ts +2 -0
  83. package/src/protocol.ts +149 -0
  84. package/src/server.ts +67 -67
  85. package/src/types.local.ts +44 -0
  86. package/src/types.ts +38 -4
  87. package/src/utils/consts.local.ts +7 -0
  88. package/src/utils/consts.ts +1 -0
  89. package/src/utils/error.ts +213 -11
  90. package/src/utils/guards.ts +12 -22
  91. package/src/utils/index.ts +2 -0
  92. package/src/utils/log.ts +62 -0
  93. package/src/utils/payload.ts +6 -2
  94. package/src/utils/server.ts +12 -17
  95. package/src/utils/types.local.ts +12 -0
  96. package/src/utils/types.ts +18 -0
  97. package/tests/error-status.spec.ts +349 -0
  98. package/tests/handlers.spec.ts +83 -0
  99. package/tests/json-body.spec.ts +72 -0
  100. package/tests/log-adapter.spec.ts +52 -0
  101. package/tests/tsconfig.json +12 -0
  102. package/tsconfig.json +1 -0
  103. package/build/.gitkeep +0 -0
@@ -1,3 +1,49 @@
1
1
  import type { FastifyReply } from 'fastify';
2
- export declare const handleError: (error: Error, reply: FastifyReply) => void;
2
+ import type { Config, HttpErrorExposure } from '../types.js';
3
+ import type { SerializedHttpError } from './types.js';
4
+ /**
5
+ * The HTTP status an error's class declares, or `null` when it declares none that may be honoured.
6
+ *
7
+ * Read through the constructor chain — a static property is inherited, so a subclass answers what
8
+ * its nearest declaring ancestor says, and redeclares to change it. An integer 4xx is honoured
9
+ * directly. A 5xx is honoured only with `allowServerErrorStatus = true`, so a domain class must
10
+ * explicitly opt in to exposing a precise availability response. The read is structural, so a
11
+ * class from a duplicate module copy declares exactly what the imported one does.
12
+ */
13
+ export declare const declaredErrorStatus: (error: unknown) => number | null;
14
+ /**
15
+ * The HTTP status an error is answered with, read off the error exactly as given.
16
+ *
17
+ * Two families reach here and both must be recognised. `AuthFailedError` / `AccessError` are this
18
+ * package's own, raised by the request pipeline itself. `AuthorizationError` / `AuthForbidden`
19
+ * come from `@owlmeans/auth` and are what the guards and gates actually throw — an unrecognised
20
+ * one answers 500, which reports a refused request as a crashed server: the client cannot tell
21
+ * "sign in again" from "the service is broken", and a consumer watching statuses concludes the
22
+ * application fell over. A family member is recognised by class OR by registered type name
23
+ * ({@link typeNamesOf}), so a class from a duplicate module copy answers the same status.
24
+ *
25
+ * **Order matters.** `AuthForbidden extends AuthorizationError`, so the 403 branch has to be
26
+ * tested first or every refusal of a permission is reported as a failure to authenticate. Both
27
+ * auth branches come before a class's own declaration, so an auth refusal can never be restated
28
+ * as something else. Everything that is neither an auth error nor a class declaring an allowed
29
+ * {@link declaredErrorStatus} answers 500.
30
+ */
31
+ export declare const errorStatus: (error: unknown) => number;
32
+ /** Invalid or absent configuration always resolves to the production-safe policy. */
33
+ export declare const errorExposure: (config?: Pick<Config, 'http'>) => HttpErrorExposure;
34
+ /** One central wire shape for every server error. */
35
+ export declare const serializeError: (error: Error, exposure?: HttpErrorExposure) => SerializedHttpError;
36
+ /** Apply safe response metadata carried by an error without coupling its package to Fastify. */
37
+ export declare const applyErrorHeaders: (error: unknown, reply: FastifyReply) => void;
38
+ /**
39
+ * Answer a thrown error: a status, and the marshalled `ResilientError` as the body.
40
+ *
41
+ * The status is resolved in two steps. The error AS THROWN comes first: it is the one object whose
42
+ * class is certainly the class that was raised, while `ResilientError.ensure` rebuilds through the
43
+ * registry of whichever `@owlmeans/error` copy this module loaded — under duplicate module copies
44
+ * that turns a perfectly recognisable auth refusal into a plain `ResilientError` answering 500.
45
+ * Only when the thrown error answers 500 is the ENSURED error asked, because the rebuild is what
46
+ * gives a status to a marshalled error that crossed a hop as a plain `Error`.
47
+ */
48
+ export declare const handleError: (error: Error, reply: FastifyReply, exposure?: HttpErrorExposure) => void;
3
49
  //# sourceMappingURL=error.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAI3C,eAAO,MAAM,WAAW,UAAW,KAAK,SAAS,YAAY,SAY5D,CAAA"}
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAM3C,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAG5D,OAAO,KAAK,EAAyB,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAG5E;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,UAAW,OAAO,KAAG,MAAM,GAAG,IAc7D,CAAA;AAkCD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,WAAW,UAAW,OAAO,KAAG,MAkB5C,CAAA;AAED,qFAAqF;AACrF,eAAO,MAAM,aAAa,YAAa,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,iBACmB,CAAA;AAsBjF,qDAAqD;AACrD,eAAO,MAAM,cAAc,UAClB,KAAK,aAAY,iBAAiB,KACxC,mBAcF,CAAA;AAED,gGAAgG;AAChG,eAAO,MAAM,iBAAiB,UAAW,OAAO,SAAS,YAAY,KAAG,IAcvE,CAAA;AA+BD;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,UACf,KAAK,SAAS,YAAY,aAAY,iBAAiB,SAU/D,CAAA"}
@@ -1,16 +1,199 @@
1
- import { FORBIDDEN_ERROR, SERVER_ERROR, UNAUTHORIZED_ERROR } from '@owlmeans/api';
1
+ import { FORBIDDEN_ERROR, SERVER_ERROR, UNAUTHORIZED_ERROR, DENIAL_KIND_HEADER, ACCESS_DENIED_KIND, } from '@owlmeans/api';
2
2
  import { AccessError, AuthFailedError } from '../errors.js';
3
- import { ResilientError } from '@owlmeans/error';
4
- export const handleError = (error, reply) => {
5
- let code = SERVER_ERROR;
6
- if (error instanceof AuthFailedError) {
7
- code = UNAUTHORIZED_ERROR;
3
+ import { AuthForbidden, AuthorizationError } from '@owlmeans/auth';
4
+ import { isResilientError, ResilientError, SEPARATOR } from '@owlmeans/error';
5
+ import { randomUUID } from 'node:crypto';
6
+ import { logger } from '@owlmeans/log';
7
+ import { CLIENT_ERROR_FIRST, CLIENT_ERROR_LAST, SERVER_ERROR_FIRST, SERVER_ERROR_LAST } from './consts.local.js';
8
+ import { INCIDENT_ID_HEADER } from './consts.js';
9
+ /**
10
+ * The HTTP status an error's class declares, or `null` when it declares none that may be honoured.
11
+ *
12
+ * Read through the constructor chain — a static property is inherited, so a subclass answers what
13
+ * its nearest declaring ancestor says, and redeclares to change it. An integer 4xx is honoured
14
+ * directly. A 5xx is honoured only with `allowServerErrorStatus = true`, so a domain class must
15
+ * explicitly opt in to exposing a precise availability response. The read is structural, so a
16
+ * class from a duplicate module copy declares exactly what the imported one does.
17
+ */
18
+ export const declaredErrorStatus = (error) => {
19
+ if (error == null || typeof error !== 'object') {
20
+ return null;
8
21
  }
9
- else if (error instanceof AccessError) {
10
- code = FORBIDDEN_ERROR;
22
+ const declaration = error.constructor;
23
+ const status = declaration?.httpStatus;
24
+ if (typeof status !== 'number' || !Number.isInteger(status)) {
25
+ return null;
11
26
  }
27
+ if (status >= CLIENT_ERROR_FIRST && status <= CLIENT_ERROR_LAST)
28
+ return status;
29
+ if (status >= SERVER_ERROR_FIRST && status <= SERVER_ERROR_LAST
30
+ && declaration?.allowServerErrorStatus === true)
31
+ return status;
32
+ return null;
33
+ };
34
+ /**
35
+ * The registered type names an error carries: its own `type`, and the static `typeName` of every
36
+ * class on its constructor chain.
37
+ *
38
+ * This is how a class is recognised when `instanceof` cannot see it. A process that loads a package
39
+ * twice — `bun --preserve-symlinks` over a linked workspace resolves one package through several
40
+ * `node_modules` paths — holds several copies of every error class, and an error built by one copy
41
+ * is not an instance of another. The registered names are the same in every copy. A subclass's
42
+ * `typeName` does not reliably embed its parent's (`EntitlementRefusal` extends `AuthForbidden`), so
43
+ * a family is recognised by walking the chain and matching names exactly, never by a substring.
44
+ */
45
+ const typeNamesOf = (error) => {
46
+ const names = new Set();
47
+ const type = error.type;
48
+ if (typeof type === 'string') {
49
+ names.add(type);
50
+ }
51
+ let ctor = error.constructor;
52
+ while (typeof ctor === 'function') {
53
+ const name = ctor.typeName;
54
+ if (typeof name === 'string') {
55
+ names.add(name);
56
+ }
57
+ ctor = Object.getPrototypeOf(ctor);
58
+ }
59
+ return names;
60
+ };
61
+ const isOfFamily = (names, family) => family.some(typeName => names.has(typeName));
62
+ /**
63
+ * The HTTP status an error is answered with, read off the error exactly as given.
64
+ *
65
+ * Two families reach here and both must be recognised. `AuthFailedError` / `AccessError` are this
66
+ * package's own, raised by the request pipeline itself. `AuthorizationError` / `AuthForbidden`
67
+ * come from `@owlmeans/auth` and are what the guards and gates actually throw — an unrecognised
68
+ * one answers 500, which reports a refused request as a crashed server: the client cannot tell
69
+ * "sign in again" from "the service is broken", and a consumer watching statuses concludes the
70
+ * application fell over. A family member is recognised by class OR by registered type name
71
+ * ({@link typeNamesOf}), so a class from a duplicate module copy answers the same status.
72
+ *
73
+ * **Order matters.** `AuthForbidden extends AuthorizationError`, so the 403 branch has to be
74
+ * tested first or every refusal of a permission is reported as a failure to authenticate. Both
75
+ * auth branches come before a class's own declaration, so an auth refusal can never be restated
76
+ * as something else. Everything that is neither an auth error nor a class declaring an allowed
77
+ * {@link declaredErrorStatus} answers 500.
78
+ */
79
+ export const errorStatus = (error) => {
80
+ if (error == null || typeof error !== 'object') {
81
+ return SERVER_ERROR;
82
+ }
83
+ const names = typeNamesOf(error);
84
+ if (error instanceof AuthForbidden || error instanceof AccessError
85
+ || isOfFamily(names, [AuthForbidden.typeName, AccessError.typeName])) {
86
+ // An established identity that lacks the permission. Re-authenticating changes nothing.
87
+ return FORBIDDEN_ERROR;
88
+ }
89
+ if (error instanceof AuthorizationError || error instanceof AuthFailedError
90
+ || isOfFamily(names, [AuthorizationError.typeName, AuthFailedError.typeName])) {
91
+ // No usable credential — absent, expired, revoked, or naming a session this server no longer
92
+ // holds. The caller is told to sign in, not that signing in would not help.
93
+ return UNAUTHORIZED_ERROR;
94
+ }
95
+ return declaredErrorStatus(error) ?? SERVER_ERROR;
96
+ };
97
+ /** Invalid or absent configuration always resolves to the production-safe policy. */
98
+ export const errorExposure = (config) => config?.http?.errors?.exposure === 'development' ? 'development' : 'production';
99
+ /**
100
+ * Preserve a typed/marshalled error when possible without asking `ensure` to convert a plain
101
+ * error. In particular, `ensure` deliberately rethrows SyntaxError; an HTTP boundary still has
102
+ * to serialize it safely and let the incident log retain the original object.
103
+ */
104
+ const normalizeForWire = (error) => {
105
+ if (error instanceof SyntaxError || isResilientError(error)) {
106
+ return error;
107
+ }
108
+ if (error.message.includes(SEPARATOR)) {
109
+ const ensured = ResilientError.ensure(error);
110
+ // The catch-all converter's legacy constructor shape shifts an ordinary error's fields.
111
+ // Only keep `ensure`'s result when a registered wire prefix actually rebuilt something.
112
+ if (ensured.type !== error.message || ensured.message !== error.stack) {
113
+ return ensured;
114
+ }
115
+ }
116
+ return error;
117
+ };
118
+ /** One central wire shape for every server error. */
119
+ export const serializeError = (error, exposure = 'production') => {
120
+ const normalized = normalizeForWire(error);
121
+ const thrown = errorStatus(error);
122
+ const status = thrown !== SERVER_ERROR ? thrown : errorStatus(normalized);
123
+ const incidentId = randomUUID();
124
+ // The server log owns every diagnostic. Production callers receive only its correlation key;
125
+ // development keeps the typed wire form so local clients can retain their usual error details.
126
+ Object.assign(error, { incidentId });
127
+ if (isResilientError(normalized))
128
+ normalized.incidentId = incidentId;
129
+ const body = exposure === 'production'
130
+ ? incidentId
131
+ : ResilientError.marshal(normalized, { includeStack: true, incidentId }).message;
132
+ return { status, body, incidentId };
133
+ };
134
+ /** Apply safe response metadata carried by an error without coupling its package to Fastify. */
135
+ export const applyErrorHeaders = (error, reply) => {
136
+ if (error == null || typeof error !== 'object') {
137
+ return;
138
+ }
139
+ const retryAfter = error.retryAfter;
140
+ if (typeof retryAfter === 'number' && Number.isFinite(retryAfter) && retryAfter > 0) {
141
+ reply.header('Retry-After', String(Math.ceil(retryAfter)));
142
+ }
143
+ // Match exact registered types. EntitlementRefusal also extends AuthForbidden but is a plan
144
+ // decision, not a person's IAM permission denial.
145
+ const type = error.type;
146
+ if (type === AuthForbidden.typeName || type === AccessError.typeName) {
147
+ reply.header(DENIAL_KIND_HEADER, ACCESS_DENIED_KIND);
148
+ }
149
+ };
150
+ const http = logger('http');
151
+ /**
152
+ * The one log entry of a failed request, by what the failure means:
153
+ *
154
+ * | Status | Level | Why |
155
+ * |---|---|---|
156
+ * | 5xx | error | a fault — the one entry that owns type, message and stack, keyed by the incident id |
157
+ * | 403 | warn, `access.forbidden` | an established identity was refused; worth an operator's eye |
158
+ * | 401 | debug, `auth.refused` | an absent or expired credential is the ordinary shape of a sign-in |
159
+ * | other 4xx | debug | the caller's mistake, not the server's |
160
+ *
161
+ * The method and path are logged; the query string is not — it can carry a token.
162
+ */
163
+ const logFailure = (error, reply, status, incidentId) => {
164
+ const request = reply.request;
165
+ const where = { method: request?.method, path: request?.url?.split('?')[0], status, incidentId };
166
+ if (status >= SERVER_ERROR) {
167
+ http.error('Request failed', { err: error, ...where });
168
+ }
169
+ else if (status === FORBIDDEN_ERROR) {
170
+ http.warn('Access forbidden', { type: error.type, message: error.message, ...where }, { event: 'access.forbidden' });
171
+ }
172
+ else if (status === UNAUTHORIZED_ERROR) {
173
+ http.debug('Authentication refused', { message: error.message, ...where }, { event: 'auth.refused' });
174
+ }
175
+ else {
176
+ http.debug('Request refused', { message: error.message, ...where });
177
+ }
178
+ };
179
+ /**
180
+ * Answer a thrown error: a status, and the marshalled `ResilientError` as the body.
181
+ *
182
+ * The status is resolved in two steps. The error AS THROWN comes first: it is the one object whose
183
+ * class is certainly the class that was raised, while `ResilientError.ensure` rebuilds through the
184
+ * registry of whichever `@owlmeans/error` copy this module loaded — under duplicate module copies
185
+ * that turns a perfectly recognisable auth refusal into a plain `ResilientError` answering 500.
186
+ * Only when the thrown error answers 500 is the ENSURED error asked, because the rebuild is what
187
+ * gives a status to a marshalled error that crossed a hop as a plain `Error`.
188
+ */
189
+ export const handleError = (error, reply, exposure = 'production') => {
12
190
  if (!reply.sent) {
13
- reply.code(code).send(ResilientError.marshal(ResilientError.ensure(error)).message);
191
+ const serialized = serializeError(error, exposure);
192
+ applyErrorHeaders(error, reply);
193
+ logFailure(error, reply, serialized.status, serialized.incidentId);
194
+ reply.header(INCIDENT_ID_HEADER, serialized.incidentId)
195
+ .code(serialized.status)
196
+ .send(serialized.body);
14
197
  }
15
198
  };
16
199
  //# sourceMappingURL=error.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEjF,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAEhD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAY,EAAE,KAAmB,EAAE,EAAE;IAC/D,IAAI,IAAI,GAAW,YAAY,CAAA;IAC/B,IAAI,KAAK,YAAY,eAAe,EAAE,CAAC;QACrC,IAAI,GAAG,kBAAkB,CAAA;IAC3B,CAAC;SAAM,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;QACxC,IAAI,GAAG,eAAe,CAAA;IACxB,CAAC;IACD,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAChB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CACnB,cAAc,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,KAAc,CAAC,CAAC,CAAC,OAAO,CACtE,CAAA;IACH,CAAC;AACH,CAAC,CAAA"}
1
+ {"version":3,"file":"error.js","sourceRoot":"","sources":["../../src/utils/error.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,kBAAkB,GAC1F,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC3D,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAClE,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,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AK,CAAC,CAAA;QAC/B,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,UAAU,CAAC,CAAA;QAClE,KAAK,CAAC,MAAM,CAAC,kBAAkB,EAAE,UAAU,CAAC,UAAU,CAAC;aACpD,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC;aACvB,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;AACH,CAAC,CAAA"}
@@ -1,9 +1,5 @@
1
1
  import type { FastifyReply, FastifyRequest } from 'fastify';
2
2
  import type { ServerEntrypoint } from '@owlmeans/server-entrypoint';
3
- import type { ServerConfig, ServerContext } from '@owlmeans/server-context';
4
- type Config = ServerConfig;
5
- interface Context<C extends Config = Config> extends ServerContext<C> {
6
- }
3
+ import type { Config, Context } from './types.local.js';
7
4
  export declare const authorize: <C extends Config, T extends Context<C>>(context: T, module: ServerEntrypoint<FastifyRequest>, req: FastifyRequest, reply: FastifyReply) => Promise<[T, ServerEntrypoint<FastifyRequest>]>;
8
- export {};
9
5
  //# sourceMappingURL=guards.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAGnE,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAK3E,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,UAAU,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;CAAI;AAEzE,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,WAC3D,CAAC,UAAU,gBAAgB,CAAC,cAAc,CAAC,OAC/C,cAAc,SAAS,YAAY,KACvC,OAAO,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC,CA0D/C,CAAA"}
1
+ {"version":3,"file":"guards.d.ts","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AAMnE,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAGvD,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO,CAAC,CAAC,CAAC,WAC3D,CAAC,UAAU,gBAAgB,CAAC,cAAc,CAAC,OAC/C,cAAc,SAAS,YAAY,KACvC,OAAO,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC,CAmD/C,CAAA"}
@@ -1,7 +1,7 @@
1
- import { isContextWithoutIds, Layer } from '@owlmeans/context';
2
1
  import { provideResponse } from '@owlmeans/entrypoint';
3
2
  import { executeResponse, provideRequest } from './payload.js';
4
3
  import { AuthFailedError } from '../errors.js';
4
+ import { attachEntity } from '@owlmeans/auth-common';
5
5
  export const authorize = async (context, module, req, reply) => {
6
6
  const guards = module.getGuards();
7
7
  if (guards.length > 0) {
@@ -34,20 +34,16 @@ export const authorize = async (context, module, req, reply) => {
34
34
  if (request.auth != null) {
35
35
  req._auth = request.auth;
36
36
  }
37
- if (request.auth?.entityId != null) {
38
- // @TODO Probably we need to downgrade context in this case
39
- if (!isContextWithoutIds(context)) {
40
- throw SyntaxError(`Context should be without ids during authorization ${context.cfg.layer}:${context.cfg.layerId}`);
41
- }
42
- if (isContextWithoutIds(context) && context.cfg.layer !== Layer.Service) {
43
- context = await context.updateContext(undefined, Layer.Service);
44
- await context.waitForInitialized();
45
- }
46
- context = await context.updateContext(request.auth.entityId, Layer.Entity);
47
- await context.waitForInitialized();
48
- // We elevate module to the context level if it was changed
49
- module = context.entrypoint(module.alias);
50
- await module.resolve();
37
+ // The token names the organization by slug; everything downstream — grants, records, minted
38
+ // infrastructure names — keys on the entity's stable id. Resolving here, once, is what keeps a
39
+ // rename from being a sweep of every handler: the id arrives on the request, and a slug that
40
+ // has since been retired still resolves to the entity that retired it.
41
+ const entity = await attachEntity(context, request);
42
+ if (entity != null) {
43
+ // Carried on the raw request the same way `_auth` is, so the request the handler is given
44
+ // later — a different object, built after this runs — sees the same resolution.
45
+ ;
46
+ req._entity = entity;
51
47
  }
52
48
  }
53
49
  // Update context in request object
@@ -1 +1 @@
1
- {"version":3,"file":"guards.js","sourceRoot":"","sources":["../../src/utils/guards.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAI9D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEtD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAM9C,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAC5B,OAAU,EAAE,MAAwC,EACpD,GAAmB,EAAE,KAAmB,EACQ,EAAE;IAClD,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,EAAE,CAAA;IACjC,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,QAAQ,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;QACvC,MAAM,OAAO,GAAG,cAAc,CAAC,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAEjD,IAAI,KAAK,GAA6B,SAAS,CAAA;QAC/C,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,MAAM,MAAM,GAAiB,OAAO,CAAC,OAAO,CAAC,KAAK,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,GAAW,CAAC,OAAO,GAAG,MAAM,CAAA;QAChC,CAAC;IACH,CAAC;IACD,mCAAmC;IAClC,GAAW,CAAC,IAAI,GAAG,OAAO,CAAA;IAE3B,OAAO,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;AAC1B,CAAC,CAAA"}
@@ -1,4 +1,6 @@
1
1
  export * from './error.js';
2
+ export * from './consts.js';
3
+ export type * from './types.js';
2
4
  export * from './server.js';
3
5
  export * from './payload.js';
4
6
  export * from './server.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
@@ -1,4 +1,5 @@
1
1
  export * from './error.js';
2
+ export * from './consts.js';
2
3
  export * from './server.js';
3
4
  export * from './payload.js';
4
5
  export * from './server.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AACA,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAE3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA"}
@@ -0,0 +1,11 @@
1
+ import type { FastifyBaseLogger } from 'fastify';
2
+ import { type Logger } from '@owlmeans/log';
3
+ /**
4
+ * A pino-shaped logger over `@owlmeans/log`, handed to Fastify as its `loggerInstance`.
5
+ *
6
+ * Fastify then logs through the process's own level, format and plugins instead of a second pino
7
+ * instance that ignores them. `(object, message)` and `(message)` are the two shapes Fastify and
8
+ * its plugins call with; an `err` in the object travels as the record's error.
9
+ */
10
+ export declare const fastifyLogger: (log?: Logger, bindings?: Record<string, unknown>) => FastifyBaseLogger;
11
+ //# sourceMappingURL=log.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAChD,OAAO,EAAyB,KAAK,MAAM,EAAE,MAAM,eAAe,CAAA;AA0BlE;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,SAAS,MAAM,aAA8B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,iBAsBhG,CAAA"}
@@ -0,0 +1,57 @@
1
+ import { logger } from '@owlmeans/log';
2
+ /**
3
+ * What Fastify says about itself, mapped to what an operator needs at each level.
4
+ *
5
+ * Fastify writes a pair of `info` lines for EVERY request and one for each unknown route, and
6
+ * announces its own listening address. The server writes one `debug` record per request itself
7
+ * (`onResponse`), a failed request is classified once in `handleError`, and the listening line is
8
+ * `server.listening` — so these are dropped or lowered here, by their fixed wording, rather than by
9
+ * a Fastify option (`disableRequestLogging` is deprecated from 5.12, `logController` does not exist
10
+ * before it).
11
+ */
12
+ const levelOf = (level, message) => {
13
+ if (typeof message !== 'string' || level !== 'info') {
14
+ return level;
15
+ }
16
+ if (message === 'incoming request' || message === 'request completed') {
17
+ return undefined;
18
+ }
19
+ if (message.startsWith('Server listening at') || /^Route .* not found$/.test(message)) {
20
+ return 'debug';
21
+ }
22
+ return level;
23
+ };
24
+ /**
25
+ * A pino-shaped logger over `@owlmeans/log`, handed to Fastify as its `loggerInstance`.
26
+ *
27
+ * Fastify then logs through the process's own level, format and plugins instead of a second pino
28
+ * instance that ignores them. `(object, message)` and `(message)` are the two shapes Fastify and
29
+ * its plugins call with; an `err` in the object travels as the record's error.
30
+ */
31
+ export const fastifyLogger = (log = logger('http'), bindings) => {
32
+ const call = (requested) => (first, second) => {
33
+ const [object, message] = typeof first === 'string' || first instanceof Error
34
+ ? [undefined, first] : [first, second];
35
+ const level = levelOf(requested, message);
36
+ if (level == null) {
37
+ return;
38
+ }
39
+ const data = bindings != null && Object.keys(bindings).length > 0
40
+ ? { ...bindings, ...(object != null && typeof object === 'object' ? object : {}) } : object;
41
+ log[level](message == null ? '' : message instanceof Error ? message : String(message), data);
42
+ };
43
+ const adapter = {
44
+ get level() { return levelName(log); },
45
+ set level(_value) { },
46
+ fatal: call('error'), error: call('error'), warn: call('warn'),
47
+ info: call('info'), debug: call('debug'), trace: call('debug'),
48
+ silent: () => undefined,
49
+ child: (extra) => fastifyLogger(log, { ...bindings, ...extra }),
50
+ };
51
+ return adapter;
52
+ };
53
+ const levelName = (log) => {
54
+ const order = ['debug', 'info', 'warn', 'error'];
55
+ return order.find(level => log.enabled(level)) ?? 'silent';
56
+ };
57
+ //# sourceMappingURL=log.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.js","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAA8B,MAAM,eAAe,CAAA;AAGlE;;;;;;;;;GASG;AACH,MAAM,OAAO,GAAG,CAAC,KAAe,EAAE,OAAgB,EAAwB,EAAE;IAC1E,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,K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B,CAAA;AAC7E,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,KAAK,CAAA;AAE1C,eAAO,MAAM,cAAc,UAAW,MAAM,OAAO,OAAO,cAAc,OAAO,KAAG,eAYjF,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,YAAY,gBAAgB,CAAC,CAAC,CAAC,SAAS,QAAQ,iBAAiB,OAAO,KAAG,OAuB3G,CAAA;AAGD,eAAO,MAAM,cAAc,WAAY,eAAe,oBAgBrD,CAAA"}
1
+ {"version":3,"file":"payload.d.ts","sourceRoot":"","sources":["../../src/utils/payload.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC7E,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAEpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,KAAK,CAAA;AAG1C,eAAO,MAAM,cAAc,UAAW,MAAM,OAAO,OAAO,cAAc,OAAO,KAAG,eAejF,CAAA;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,YAAY,gBAAgB,CAAC,CAAC,CAAC,SAAS,QAAQ,iBAAiB,OAAO,KAAG,OAuB3G,CAAA;AAGD,eAAO,MAAM,cAAc,WAAY,eAAe,oBAgBrD,CAAA"}
@@ -1,10 +1,14 @@
1
1
  import { EntrypointOutcome } from '@owlmeans/entrypoint';
2
- import { ACCEPTED, CREATED, OK, SERVER_ERROR } from '@owlmeans/api';
2
+ import { ACCEPTED, CREATED, OK } from '@owlmeans/api';
3
+ import { handleError } from './error.js';
3
4
  export const provideRequest = (alias, req, provision) => {
4
5
  provision = provision ?? false;
5
6
  return {
6
7
  alias,
7
8
  auth: req._auth ?? undefined,
9
+ // Resolved once by `authorize` and carried by reference, exactly like `_auth`, so a handler
10
+ // reached through any path sees the same entity the guard admitted.
11
+ entity: req._entity ?? undefined,
8
12
  params: req.params,
9
13
  body: req.body,
10
14
  headers: req.headers,
@@ -27,7 +31,7 @@ export const executeResponse = (response, reply, throwOnError) => {
27
31
  if (throwOnError ?? false) {
28
32
  throw response.error;
29
33
  }
30
- reply.code(SERVER_ERROR).send(response.error.message);
34
+ handleError(response.error, reply);
31
35
  return true;
32
36
  }
33
37
  else if (response.outcome != null) {
@@ -1 +1 @@
1
- {"version":3,"file":"payload.js","sourceRoot":"","sources":["../../src/utils/payload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAGxD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAGnE,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAE,GAAY,EAAE,SAAmB,EAAmB,EAAE;IAClG,SAAS,GAAG,SAAS,IAAI,KAAK,CAAA;IAC9B,OAAO;QACL,KAAK;QACL,IAAI,EAAG,GAAW,CAAC,KAAK,IAAI,SAAS;QACrC,MAAM,EAAE,GAAG,CAAC,MAA4D;QACxE,IAAI,EAAE,GAAG,CAAC,IAA2B;QACrC,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,KAAK,EAAE,GAAG,CAAC,KAA2D;QACtE,IAAI,EAAE,GAAG,CAAC,GAAG;QACb,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS;KACtC,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAI,QAA6B,EAAE,KAAe,EAAE,YAAsB,EAAW,EAAE;IACpH,IAAI,QAAQ,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;QAC3B,IAAI,YAAY,IAAI,KAAK,EAAE,CAAC;YAC1B,MAAM,QAAQ,CA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,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,WAAW,CACpC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,cAAc,CAAC,KAAwB,CAAC,CAAC,CAAC,CACzG,CAAA;QACH,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
@@ -1,10 +1,7 @@
1
1
  import type { FastifyReply, FastifyRequest } from 'fastify';
2
2
  import type { ServerEntrypoint } from '@owlmeans/server-entrypoint';
3
- import type { CommonEntrypoint } from '@owlmeans/entrypoint';
4
- import type { ServerContext, ServerConfig } from '@owlmeans/server-context';
5
- type Config = ServerConfig;
6
- type Context = ServerContext<Config>;
7
- export declare const canServeModule: (context: Context, module: CommonEntrypoint) => module is ServerEntrypoint<unknown>;
3
+ import { type CommonEntrypoint } from '@owlmeans/entrypoint';
4
+ import type { ServerContext2 } from './types.local.js';
5
+ export declare const canServeModule: (context: ServerContext2, module: CommonEntrypoint) => module is ServerEntrypoint<unknown>;
8
6
  export declare const createServerHandler: (module: ServerEntrypoint<FastifyRequest>, location: string) => (req: FastifyRequest, reply: FastifyReply) => Promise<void>;
9
- export {};
10
7
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/utils/server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE3D,OAAO,KAAK,EAAgB,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACjF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAG5D,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAQ3E,KAAK,MAAM,GAAG,YAAY,CAAA;AAC1B,KAAK,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;AAEpC,eAAO,MAAM,cAAc,YAAa,OAAO,UAAU,gBAAgB,KAAG,MAAM,IAAI,gBAAgB,CAAC,OAAO,CAY7G,CAAA;AAED,eAAO,MAAM,mBAAmB,WAAY,gBAAgB,CAAC,cAAc,CAAC,YAAY,MAAM,WAChF,cAAc,SAAS,YAAY,kBA0C9C,CAAA"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/utils/server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC3D,OAAO,KAAK,EAAgB,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACjF,OAAO,EAAE,KAAK,gBAAgB,EAAqC,MAAM,sBAAsB,CAAA;AAQ/F,OAAO,KAAK,EAAiB,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAGrE,eAAO,MAAM,cAAc,YAAa,cAAc,UAAU,gBAAgB,KAAG,MAAM,IAAI,gBAAgB,CAAC,OAAO,CAcpH,CAAA;AAED,eAAO,MAAM,mBAAmB,WAAY,gBAAgB,CAAC,cAAc,CAAC,YAAY,MAAM,WAChF,cAAc,SAAS,YAAY,kBAuC9C,CAAA"}
@@ -1,9 +1,9 @@
1
- import { AppType } from '@owlmeans/context';
2
- import { assertContext } from '@owlmeans/context';
1
+ import { AppType, assertContext } from '@owlmeans/context';
3
2
  import { provideResponse } from '@owlmeans/entrypoint';
4
3
  import { ResilientError } from '@owlmeans/error';
5
4
  import { OK } from '@owlmeans/api';
6
- import { handleError } from './error.js';
5
+ import { logger } from '@owlmeans/log';
6
+ import { errorExposure, handleError } from './error.js';
7
7
  import { executeResponse, provideRequest } from './payload.js';
8
8
  import { authorize } from './guards.js';
9
9
  import { RouteProtocols } from '@owlmeans/route';
@@ -14,7 +14,9 @@ export const canServeModule = (context, module) => {
14
14
  if (module.route.route.service != null && module.route.route.service !== context.cfg.service) {
15
15
  return false;
16
16
  }
17
- if (module.route.route.protocol === RouteProtocols.SOCKET) {
17
+ // HTTP serves only its own protocol. Custom transports are handled by their owning packages.
18
+ if (module.route.route.protocol != null
19
+ && module.route.route.protocol !== RouteProtocols.WEB) {
18
20
  return false;
19
21
  }
20
22
  return 'isIntermediate' in module.route;
@@ -43,20 +45,17 @@ export const createServerHandler = (module, location) => async (req, reply) => {
43
45
  // neither `executeResponse` nor the handler itself (hijack) replied.
44
46
  const responded = executeResponse(response, reply, true);
45
47
  if (!responded && !reply.sent) {
46
- console.warn(`SENDS DEFAULT RESPONSE: ${module.alias}`);
48
+ logger('http').warn(`SENDS DEFAULT RESPONSE: ${module.alias}`);
47
49
  reply.code(OK).send(response.value);
48
50
  }
49
51
  }
50
52
  catch (error) {
51
- console.error(`Error in ${module.alias} (${location})`);
52
- console.error(JSON.stringify(error, null, 2));
53
- console.error(error);
54
53
  if (module.fixer != null) {
55
54
  const fixer = context.service(module.fixer);
56
55
  fixer.handle(reply, ResilientError.ensure(error));
57
56
  return;
58
57
  }
59
- handleError(error, reply);
58
+ handleError(error, reply, errorExposure(context.cfg));
60
59
  }
61
60
  };
62
61
  //# sourceMappingURL=server.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/utils/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAE3C,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAIjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAChD,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,O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ACpE,oEAAoE;QACpE,qEAAqE;QACrE,MAAM,SAAS,GAAG,eAAe,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,CAAA;QACxD,IAAI,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAC9B,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,2BAA2B,MAAM,CAAC,KAAK,EAAE,CAAC,CAAA;YAC9D,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QACrC,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;YACzB,MAAM,KAAK,GAAiB,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YACzD,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,cAAc,CAAC,MAAM,CAAC,KAAc,CAAC,CAAC,CAAA;YAC1D,OAAM;QACR,CAAC;QACD,WAAW,CAAC,KAAc,EAAE,KAAK,EAAE,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;IAChE,CAAC;AACH,CAAC,CAAA"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * What an error class may declare about the HTTP status it is answered with.
3
+ *
4
+ * Structural on purpose: the classes that declare it live in packages that must not depend on an
5
+ * HTTP server (`@owlmeans/payment`, a product's contract package), so they write a plain
6
+ * `public static httpStatus = 409` and nothing imports this type.
7
+ */
8
+ export interface HttpStatusDeclaration {
9
+ httpStatus?: unknown;
10
+ /** Explicit opt-in for a declared 5xx response. Client-error declarations need no opt-in. */
11
+ allowServerErrorStatus?: unknown;
12
+ }
13
+ export interface SerializedHttpError {
14
+ status: number;
15
+ body: string;
16
+ incidentId: string;
17
+ }
18
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,WAAW,qBAAqB;IACpC,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,6FAA6F;IAC7F,sBAAsB,CAAC,EAAE,OAAO,CAAA;CACjC;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;CACnB"}