@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.9 → 3.9.3-ultramodern.2

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 (88) hide show
  1. package/README.md +3 -1
  2. package/dist/cjs/backend-federation-security/index.js +73 -152
  3. package/dist/cjs/bff-policy/index.js +7 -0
  4. package/dist/cjs/bff-policy/node.js +20 -4
  5. package/dist/cjs/bff-policy/operationContracts.js +150 -135
  6. package/dist/cjs/bff-policy/operationIdentity.js +81 -0
  7. package/dist/cjs/bff-policy/producer.js +79 -0
  8. package/dist/cjs/localisedLoader.js +65 -0
  9. package/dist/cjs/localisedLoaderPlugin.js +2 -2
  10. package/dist/cjs/module-federation-css/remote.js +2 -1
  11. package/dist/cjs/static-serving/staticModuleFederation.js +49 -12
  12. package/dist/esm/backend-federation-security/index.mjs +73 -152
  13. package/dist/esm/bff-policy/index.mjs +1 -0
  14. package/dist/esm/bff-policy/operationIdentity.mjs +30 -0
  15. package/dist/esm/bff-policy/producer.mjs +31 -0
  16. package/dist/esm/localisedLoader.mjs +24 -0
  17. package/dist/esm/localisedLoaderPlugin.mjs +2 -2
  18. package/dist/esm/module-federation-css/remote.mjs +2 -1
  19. package/dist/esm-node/backend-federation-security/index.mjs +73 -152
  20. package/dist/esm-node/bff-policy/index.mjs +1 -0
  21. package/dist/esm-node/bff-policy/node.mjs +1 -0
  22. package/dist/esm-node/bff-policy/operationContracts.mjs +5 -27
  23. package/dist/esm-node/bff-policy/operationIdentity.mjs +31 -0
  24. package/dist/esm-node/bff-policy/producer.mjs +32 -0
  25. package/dist/esm-node/localisedLoader.mjs +25 -0
  26. package/dist/esm-node/localisedLoaderPlugin.mjs +2 -2
  27. package/dist/esm-node/module-federation-css/remote.mjs +2 -1
  28. package/dist/esm-node/static-serving/staticModuleFederation.mjs +49 -12
  29. package/dist/types/backend-federation-security/node.d.ts +1 -1
  30. package/dist/types/bff-policy/index.d.ts +1 -0
  31. package/dist/types/bff-policy/node.d.ts +1 -0
  32. package/dist/types/bff-policy/operationContracts.d.ts +2 -41
  33. package/dist/types/bff-policy/operationIdentity.d.ts +34 -0
  34. package/dist/types/bff-policy/producer.d.ts +10 -0
  35. package/dist/types/localisedLoader.d.ts +15 -0
  36. package/dist/types/module-federation-css/remote.d.ts +2 -0
  37. package/dist/types/serverConfig.d.ts +3 -1
  38. package/dist/types-esm/backend-federation-security/node.d.mts +1 -1
  39. package/dist/types-esm/bff-policy/index.d.mts +1 -0
  40. package/dist/types-esm/bff-policy/node.d.mts +1 -0
  41. package/dist/types-esm/bff-policy/operationContracts.d.mts +2 -41
  42. package/dist/types-esm/bff-policy/operationIdentity.d.mts +34 -0
  43. package/dist/types-esm/bff-policy/producer.d.mts +10 -0
  44. package/dist/types-esm/localisedLoader.d.mts +15 -0
  45. package/dist/types-esm/module-federation-css/remote.d.mts +2 -0
  46. package/dist/types-esm/serverConfig.d.mts +3 -1
  47. package/package.json +47 -19
  48. package/src/backend-federation-security/index.ts +151 -318
  49. package/src/backend-federation-security/node.ts +24 -22
  50. package/src/bff-policy/index.ts +1 -0
  51. package/src/bff-policy/node.ts +1 -0
  52. package/src/bff-policy/operationContracts.ts +13 -86
  53. package/src/bff-policy/operationIdentity.ts +90 -0
  54. package/src/bff-policy/producer.ts +40 -0
  55. package/src/contract-gate-snapshot-store/resolve.ts +0 -1
  56. package/src/localisedLoader.ts +50 -0
  57. package/src/localisedLoaderPlugin.ts +6 -7
  58. package/src/module-federation-css/remote.ts +3 -1
  59. package/src/serverConfig.ts +2 -0
  60. package/src/static-serving/staticModuleFederation.ts +127 -22
  61. package/rslib.config.mts +0 -38
  62. package/rstest.config.mts +0 -18
  63. package/tests/backendFederationSecurity.test.ts +0 -416
  64. package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +0 -450
  65. package/tests/bff-policy/crossProjectPolicy.test.ts +0 -197
  66. package/tests/bff-policy/operationContracts.test.ts +0 -221
  67. package/tests/bff-policy/optionalZodPeer.test.ts +0 -30
  68. package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +0 -76
  69. package/tests/contractGateSnapshotObserver.test.ts +0 -347
  70. package/tests/contractGateSnapshotStore.test.ts +0 -225
  71. package/tests/env.test.ts +0 -54
  72. package/tests/helpers.ts +0 -20
  73. package/tests/localisedLoader.test.ts +0 -120
  74. package/tests/mfCache.test.ts +0 -63
  75. package/tests/moduleFederationCss.test.ts +0 -348
  76. package/tests/nativeErrorResponse.test.ts +0 -118
  77. package/tests/nativeServerAssembly.test.ts +0 -385
  78. package/tests/runtimeLifecycle.test.ts +0 -159
  79. package/tests/static-serving/ordering.test.ts +0 -214
  80. package/tests/static-serving/static.test.ts +0 -411
  81. package/tests/telemetry.test.ts +0 -248
  82. package/tests/telemetryHealthMonitor.test.ts +0 -183
  83. package/tests/telemetryHealthObservation.test.ts +0 -302
  84. package/tests/telemetryLifecycle.test.ts +0 -123
  85. package/tests/telemetryRegistryQueueMatrix.test.ts +0 -130
  86. package/tests/telemetryTraceparent.test.ts +0 -167
  87. package/tests/tsconfig.json +0 -11
  88. package/tsconfig.json +0 -10
@@ -1,348 +0,0 @@
1
- import { mkdtemp, rm, writeFile } from 'node:fs/promises';
2
- import os from 'node:os';
3
- import path from 'node:path';
4
- import {
5
- collectDirectRemoteModuleFederationCssWithMeta,
6
- collectModuleFederationManifestCss,
7
- createModuleFederationCssCollector,
8
- } from '../src/module-federation-css';
9
-
10
- const tempDirs: string[] = [];
11
-
12
- const createTempDir = async () => {
13
- const dir = await mkdtemp(path.join(os.tmpdir(), 'modern-mf-css-'));
14
- tempDirs.push(dir);
15
- return dir;
16
- };
17
-
18
- afterEach(async () => {
19
- await Promise.all(
20
- tempDirs.splice(0).map(dir =>
21
- rm(dir, {
22
- recursive: true,
23
- force: true,
24
- }),
25
- ),
26
- );
27
- });
28
-
29
- describe('module federation css collection', () => {
30
- it('resolves manifest css against publicPath and dedupes assets', () => {
31
- const css = collectModuleFederationManifestCss(
32
- {
33
- metaData: {
34
- publicPath: 'https://cdn.example.com/remote',
35
- },
36
- shared: [
37
- {
38
- assets: {
39
- css: {
40
- sync: ['static/css/shared.css'],
41
- async: ['static/css/shared.css', '/root.css'],
42
- },
43
- },
44
- },
45
- ],
46
- exposes: [
47
- {
48
- assets: {
49
- css: {
50
- sync: ['static/css/expose.css'],
51
- async: ['https://assets.example.com/async.css'],
52
- },
53
- },
54
- },
55
- ],
56
- remotes: [
57
- {
58
- assets: {
59
- css: {
60
- sync: ['static/css/nested-remote.css'],
61
- },
62
- },
63
- },
64
- ],
65
- },
66
- 'https://origin.example.com/mf-manifest.json',
67
- );
68
-
69
- expect(css).toEqual([
70
- 'https://cdn.example.com/remote/static/css/shared.css',
71
- 'https://cdn.example.com/root.css',
72
- 'https://cdn.example.com/remote/static/css/expose.css',
73
- 'https://assets.example.com/async.css',
74
- ]);
75
- });
76
-
77
- it('resolves publicPath variants without localhost or doubled static prefixes', () => {
78
- const cases = [
79
- {
80
- publicPath: '/',
81
- expected: 'https://remote.example.com/static/css/main.css',
82
- },
83
- {
84
- publicPath: '/static-base',
85
- expected: 'https://remote.example.com/static-base/static/css/main.css',
86
- },
87
- {
88
- publicPath: '/static-base/',
89
- expected: 'https://remote.example.com/static-base/static/css/main.css',
90
- },
91
- {
92
- publicPath: 'https://cdn.example.com/assets',
93
- expected: 'https://cdn.example.com/assets/static/css/main.css',
94
- },
95
- {
96
- publicPath: 'https://cdn.example.com/assets/',
97
- expected: 'https://cdn.example.com/assets/static/css/main.css',
98
- },
99
- ];
100
-
101
- for (const { publicPath, expected } of cases) {
102
- const css = collectModuleFederationManifestCss(
103
- {
104
- metaData: {
105
- publicPath,
106
- },
107
- exposes: [
108
- {
109
- assets: {
110
- css: {
111
- sync: ['static/css/main.css'],
112
- },
113
- },
114
- },
115
- ],
116
- },
117
- 'https://remote.example.com/nested/mf-manifest.json',
118
- );
119
-
120
- expect(css).toEqual([expected]);
121
- expect(css[0]).not.toContain('localhost');
122
- expect(css[0]).not.toContain('//static');
123
- }
124
- });
125
-
126
- it('falls back to the remote manifest URL when publicPath is absent', () => {
127
- const css = collectModuleFederationManifestCss(
128
- {
129
- exposes: [
130
- {
131
- assets: {
132
- css: {
133
- sync: ['static/css/expose.css', '/root.css'],
134
- },
135
- },
136
- },
137
- ],
138
- },
139
- 'http://localhost:3010/nested/mf-manifest.json',
140
- );
141
-
142
- expect(css).toEqual([
143
- 'http://localhost:3010/nested/static/css/expose.css',
144
- 'http://localhost:3010/root.css',
145
- ]);
146
- });
147
-
148
- it('loads available remote CSS and marks partial remote failures', async () => {
149
- const pwd = await createTempDir();
150
- await writeFile(
151
- path.join(pwd, 'mf-manifest.json'),
152
- JSON.stringify({
153
- remotes: [
154
- {
155
- entry: 'remoteA@https://remote-a.example.com/mf-manifest.json',
156
- },
157
- {
158
- entry: 'https://remote-b.example.com/mf-manifest.json',
159
- },
160
- ],
161
- }),
162
- );
163
-
164
- const warn = rs.fn();
165
- const fetcher = rs.fn(async (url: string) => {
166
- if (url.includes('remote-b')) {
167
- return new Response('missing', { status: 404 });
168
- }
169
-
170
- return Response.json({
171
- metaData: {
172
- publicPath: 'https://cdn.example.com/a/',
173
- },
174
- shared: [
175
- {
176
- assets: {
177
- css: {
178
- sync: ['static/css/shared.css'],
179
- },
180
- },
181
- },
182
- ],
183
- exposes: [
184
- {
185
- assets: {
186
- css: {
187
- sync: ['static/css/expose.css'],
188
- async: ['static/css/shared.css'],
189
- },
190
- },
191
- },
192
- ],
193
- });
194
- });
195
-
196
- const result = await collectDirectRemoteModuleFederationCssWithMeta(pwd, {
197
- fetcher,
198
- monitors: {
199
- warn,
200
- } as any,
201
- });
202
-
203
- expect(result.assets).toEqual([
204
- 'https://cdn.example.com/a/static/css/shared.css',
205
- 'https://cdn.example.com/a/static/css/expose.css',
206
- ]);
207
- expect(result.errored).toBe(true);
208
- expect(fetcher).toHaveBeenCalledTimes(2);
209
- expect(warn).toHaveBeenCalledTimes(1);
210
- });
211
- });
212
-
213
- describe('module federation css collector cache', () => {
214
- const writeHostManifest = async () => {
215
- const pwd = await createTempDir();
216
- await writeFile(
217
- path.join(pwd, 'mf-manifest.json'),
218
- JSON.stringify({
219
- remotes: [
220
- {
221
- entry: 'https://remote-a.example.com/mf-manifest.json',
222
- },
223
- ],
224
- }),
225
- );
226
- return pwd;
227
- };
228
-
229
- const remoteManifestWithCss = (cssAsset: string) =>
230
- Response.json({
231
- metaData: {
232
- publicPath: 'https://cdn.example.com/a/',
233
- },
234
- exposes: [
235
- {
236
- assets: {
237
- css: {
238
- sync: [cssAsset],
239
- },
240
- },
241
- },
242
- ],
243
- });
244
-
245
- it('serves cached assets within the ttl and refetches after expiry', async () => {
246
- const pwd = await writeHostManifest();
247
-
248
- let nowMs = 10_000;
249
- let cssAsset = 'static/css/one.css';
250
- const fetcher = rs.fn(async () => remoteManifestWithCss(cssAsset));
251
-
252
- const collector = createModuleFederationCssCollector(pwd, {
253
- fetcher,
254
- ttlMs: 5_000,
255
- now: () => nowMs,
256
- });
257
-
258
- expect(await collector.collect()).toEqual([
259
- 'https://cdn.example.com/a/static/css/one.css',
260
- ]);
261
- expect(fetcher).toHaveBeenCalledTimes(1);
262
-
263
- // The remote redeployed, but the cache is still fresh.
264
- cssAsset = 'static/css/two.css';
265
- nowMs += 4_999;
266
- expect(await collector.collect()).toEqual([
267
- 'https://cdn.example.com/a/static/css/one.css',
268
- ]);
269
- expect(fetcher).toHaveBeenCalledTimes(1);
270
-
271
- // Past the ttl the collection is refreshed and picks up the redeploy.
272
- nowMs += 2;
273
- expect(await collector.collect()).toEqual([
274
- 'https://cdn.example.com/a/static/css/two.css',
275
- ]);
276
- expect(fetcher).toHaveBeenCalledTimes(2);
277
- });
278
-
279
- it('does not cache errored collections and serves the last good list', async () => {
280
- const pwd = await writeHostManifest();
281
-
282
- let nowMs = 10_000;
283
- let failing = false;
284
- const fetcher = rs.fn(async () => {
285
- if (failing) {
286
- return new Response('boom', { status: 500 });
287
- }
288
- return remoteManifestWithCss('static/css/one.css');
289
- });
290
-
291
- const collector = createModuleFederationCssCollector(pwd, {
292
- fetcher,
293
- ttlMs: 5_000,
294
- now: () => nowMs,
295
- monitors: { warn: rs.fn() } as any,
296
- });
297
-
298
- expect(await collector.collect()).toEqual([
299
- 'https://cdn.example.com/a/static/css/one.css',
300
- ]);
301
- expect(fetcher).toHaveBeenCalledTimes(1);
302
-
303
- // Cache expired and the remote is now failing: serve last-good...
304
- nowMs += 5_001;
305
- failing = true;
306
- expect(await collector.collect()).toEqual([
307
- 'https://cdn.example.com/a/static/css/one.css',
308
- ]);
309
- expect(fetcher).toHaveBeenCalledTimes(2);
310
-
311
- // ...and do NOT cache the failure: the next request retries immediately.
312
- expect(await collector.collect()).toEqual([
313
- 'https://cdn.example.com/a/static/css/one.css',
314
- ]);
315
- expect(fetcher).toHaveBeenCalledTimes(3);
316
-
317
- failing = false;
318
- expect(await collector.collect()).toEqual([
319
- 'https://cdn.example.com/a/static/css/one.css',
320
- ]);
321
- expect(fetcher).toHaveBeenCalledTimes(4);
322
- });
323
-
324
- it('coalesces concurrent collections into a single in-flight fetch', async () => {
325
- const pwd = await writeHostManifest();
326
-
327
- const fetcher = rs.fn(async () =>
328
- remoteManifestWithCss('static/css/one.css'),
329
- );
330
- const collector = createModuleFederationCssCollector(pwd, {
331
- fetcher,
332
- ttlMs: 0,
333
- });
334
-
335
- const [first, second] = await Promise.all([
336
- collector.collect(),
337
- collector.collect(),
338
- ]);
339
-
340
- expect(first).toEqual(['https://cdn.example.com/a/static/css/one.css']);
341
- expect(second).toEqual(first);
342
- expect(fetcher).toHaveBeenCalledTimes(1);
343
-
344
- // ttlMs 0 keeps the previous per-request freshness for dev.
345
- await collector.collect();
346
- expect(fetcher).toHaveBeenCalledTimes(2);
347
- });
348
- });
@@ -1,118 +0,0 @@
1
- import { applyPlugins, type ProdServerOptions } from '@modern-js/prod-server';
2
- import { createServerBase } from '@modern-js/server-core';
3
- import { ultramodernServerPlugin } from '@modern-js/server-runtime-extensions/server-plugin';
4
- import fs from 'fs';
5
- import os from 'os';
6
- import path from 'path';
7
-
8
- const makeTempDir = () =>
9
- fs.mkdtempSync(path.join(os.tmpdir(), 'modern-prod-apply-plugins-'));
10
-
11
- describe('fork error response selection', () => {
12
- let directory: string;
13
- let server: ReturnType<typeof createServerBase> | undefined;
14
-
15
- beforeEach(() => {
16
- directory = makeTempDir();
17
- });
18
- afterEach(async () => {
19
- await server?.dispose();
20
- server = undefined;
21
- fs.rmSync(directory, { recursive: true, force: true });
22
- });
23
-
24
- const initialize = async (
25
- onError?: NonNullable<ProdServerOptions['serverConfig']>['onError'],
26
- ) => {
27
- const options: ProdServerOptions = {
28
- pwd: directory,
29
- serverConfigPath: path.join(directory, 'modern.server.js'),
30
- appContext: {
31
- appDirectory: directory,
32
- apiDirectory: '',
33
- lambdaDirectory: '',
34
- },
35
- config: {
36
- html: {},
37
- output: {},
38
- source: {},
39
- tools: {},
40
- server: { logger: false },
41
- bff: { prefix: ['/rpc', '/service'] },
42
- dev: {},
43
- security: {},
44
- },
45
- plugins: [ultramodernServerPlugin()],
46
- serverConfig: {
47
- onError,
48
- middlewares: [
49
- {
50
- name: 'request-failure',
51
- order: 'pre',
52
- handler: () => {
53
- throw Object.assign(new Error('private maintenance detail'), {
54
- status: 503,
55
- retryAfterSeconds: 15,
56
- });
57
- },
58
- },
59
- ],
60
- },
61
- };
62
- server = createServerBase(options);
63
- await applyPlugins(server, options);
64
- await server.init();
65
- return server;
66
- };
67
-
68
- test('applies the owner envelope to every configured prefix and leaves HTML outside them', async () => {
69
- const instance = await initialize();
70
- for (const url of ['/rpc/failure', '/service/failure']) {
71
- const response = await instance.request(url, {}, {});
72
- expect(response.status).toBe(503);
73
- expect(response.headers.get('Retry-After')).toBe('15');
74
- await expect(response.json()).resolves.toEqual({
75
- success: false,
76
- error: {
77
- code: 'SERVICE_UNAVAILABLE',
78
- message: 'Service Unavailable',
79
- status: 503,
80
- },
81
- });
82
- }
83
- const page = await instance.request('/api/page', {}, {});
84
- expect(page.status).toBe(500);
85
- expect(page.headers.get('content-type')).toContain('text/html');
86
- expect(await page.text()).not.toContain('private maintenance detail');
87
- });
88
-
89
- test('preserves a user error response before applying the fork policy', async () => {
90
- const instance = await initialize(
91
- () => new Response('user unavailable', { status: 409 }),
92
- );
93
- const response = await instance.request('/rpc/failure', {}, {});
94
- expect(response.status).toBe(409);
95
- expect(await response.text()).toBe('user unavailable');
96
- expect(response.headers.get('Retry-After')).toBeNull();
97
- });
98
-
99
- test.each([
100
- 'declines',
101
- 'throws',
102
- ])('applies the fork policy when the user handler %s', async outcome => {
103
- const instance = await initialize(() => {
104
- if (outcome === 'throws') throw new Error('custom handler detail');
105
- });
106
- const response = await instance.request('/rpc/failure', {}, {});
107
- expect(response.status).toBe(503);
108
- expect(response.headers.get('Retry-After')).toBe('15');
109
- await expect(response.json()).resolves.toMatchObject({
110
- success: false,
111
- error: {
112
- code: 'SERVICE_UNAVAILABLE',
113
- message: 'Service Unavailable',
114
- status: 503,
115
- },
116
- });
117
- });
118
- });