@bleedingdev/modern-js-app-tools 3.2.0-ultramodern.41 → 3.2.0-ultramodern.43

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.
@@ -97,7 +97,8 @@ const createWorkerManifest = async (outputDirectory, modernConfig)=>{
97
97
  },
98
98
  assets: {
99
99
  binding: ASSETS_BINDING,
100
- directory: `./${PUBLIC_ASSETS_DIRECTORY}`
100
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
101
+ runWorkerFirst: true
101
102
  },
102
103
  routeSpec: {
103
104
  file: ROUTE_SPEC_OUTPUT,
@@ -190,7 +191,8 @@ const createCloudflarePreset = ({ appContext, modernConfig })=>{
190
191
  ],
191
192
  assets: {
192
193
  directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
193
- binding: ASSETS_BINDING
194
+ binding: ASSETS_BINDING,
195
+ run_worker_first: true
194
196
  }
195
197
  }, {
196
198
  spaces: 2
@@ -2,14 +2,35 @@ const ASSETS_BINDING = 'ASSETS';
2
2
  const MODERN_WORKER_MANIFEST = p_workerManifest;
3
3
  const WORKER_MODULE_LOADERS = p_workerModuleLoaders;
4
4
  const workerModulePromises = new Map();
5
+ const CORS_HEADERS = {
6
+ 'access-control-allow-headers': '*',
7
+ 'access-control-allow-methods': 'GET, HEAD, OPTIONS',
8
+ 'access-control-allow-origin': '*'
9
+ };
5
10
  globalThis.__dirname ??= '/';
6
11
  globalThis.__filename ??= '/index.js';
12
+ function withCorsHeaders(response) {
13
+ const headers = new Headers(response.headers);
14
+ for (const [name, value] of Object.entries(CORS_HEADERS))if (!headers.has(name)) headers.set(name, value);
15
+ return new Response(response.body, {
16
+ headers,
17
+ status: response.status,
18
+ statusText: response.statusText
19
+ });
20
+ }
21
+ function createCorsPreflightResponse(request) {
22
+ if ('OPTIONS' !== request.method) return null;
23
+ return new Response(null, {
24
+ headers: CORS_HEADERS,
25
+ status: 204
26
+ });
27
+ }
7
28
  async function fetchAsset(request, env) {
8
29
  const assets = env?.[ASSETS_BINDING];
9
30
  if (!assets || 'function' != typeof assets.fetch) return null;
10
31
  const response = await assets.fetch(request);
11
32
  if (404 === response.status) return null;
12
- return response;
33
+ return withCorsHeaders(response);
13
34
  }
14
35
  async function fetchAssetByPath(pathname, request, env) {
15
36
  const url = new URL(request.url);
@@ -220,16 +241,18 @@ async function dispatchBffRequest(request, env) {
220
241
  }
221
242
  export default {
222
243
  async fetch (request, env, ctx) {
244
+ const corsPreflightResponse = createCorsPreflightResponse(request);
245
+ if (corsPreflightResponse) return corsPreflightResponse;
223
246
  const assetResponse = await fetchAsset(request, env);
224
247
  if (assetResponse) return assetResponse;
225
248
  const bffResponse = await dispatchBffRequest(request, env);
226
- if (bffResponse) return bffResponse;
249
+ if (bffResponse) return withCorsHeaders(bffResponse);
227
250
  const route = findRoute(request);
228
- if (route?.worker) return dispatchRouteWorker(route, request, env, ctx);
251
+ if (route?.worker) return withCorsHeaders(await dispatchRouteWorker(route, request, env, ctx));
229
252
  const htmlResponse = await fetchRouteHtml(route, request, env);
230
253
  if (htmlResponse) return htmlResponse;
231
- return new Response('Not found', {
254
+ return withCorsHeaders(new Response('Not found', {
232
255
  status: 404
233
- });
256
+ }));
234
257
  }
235
258
  };
@@ -59,7 +59,8 @@ const createWorkerManifest = async (outputDirectory, modernConfig)=>{
59
59
  },
60
60
  assets: {
61
61
  binding: ASSETS_BINDING,
62
- directory: `./${PUBLIC_ASSETS_DIRECTORY}`
62
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
63
+ runWorkerFirst: true
63
64
  },
64
65
  routeSpec: {
65
66
  file: ROUTE_SPEC_OUTPUT,
@@ -152,7 +153,8 @@ const createCloudflarePreset = ({ appContext, modernConfig })=>{
152
153
  ],
153
154
  assets: {
154
155
  directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
155
- binding: ASSETS_BINDING
156
+ binding: ASSETS_BINDING,
157
+ run_worker_first: true
156
158
  }
157
159
  }, {
158
160
  spaces: 2
@@ -2,14 +2,35 @@ const ASSETS_BINDING = 'ASSETS';
2
2
  const MODERN_WORKER_MANIFEST = p_workerManifest;
3
3
  const WORKER_MODULE_LOADERS = p_workerModuleLoaders;
4
4
  const workerModulePromises = new Map();
5
+ const CORS_HEADERS = {
6
+ 'access-control-allow-headers': '*',
7
+ 'access-control-allow-methods': 'GET, HEAD, OPTIONS',
8
+ 'access-control-allow-origin': '*'
9
+ };
5
10
  globalThis.__dirname ??= '/';
6
11
  globalThis.__filename ??= '/index.js';
12
+ function withCorsHeaders(response) {
13
+ const headers = new Headers(response.headers);
14
+ for (const [name, value] of Object.entries(CORS_HEADERS))if (!headers.has(name)) headers.set(name, value);
15
+ return new Response(response.body, {
16
+ headers,
17
+ status: response.status,
18
+ statusText: response.statusText
19
+ });
20
+ }
21
+ function createCorsPreflightResponse(request) {
22
+ if ('OPTIONS' !== request.method) return null;
23
+ return new Response(null, {
24
+ headers: CORS_HEADERS,
25
+ status: 204
26
+ });
27
+ }
7
28
  async function fetchAsset(request, env) {
8
29
  const assets = env?.[ASSETS_BINDING];
9
30
  if (!assets || 'function' != typeof assets.fetch) return null;
10
31
  const response = await assets.fetch(request);
11
32
  if (404 === response.status) return null;
12
- return response;
33
+ return withCorsHeaders(response);
13
34
  }
14
35
  async function fetchAssetByPath(pathname, request, env) {
15
36
  const url = new URL(request.url);
@@ -220,16 +241,18 @@ async function dispatchBffRequest(request, env) {
220
241
  }
221
242
  export default {
222
243
  async fetch (request, env, ctx) {
244
+ const corsPreflightResponse = createCorsPreflightResponse(request);
245
+ if (corsPreflightResponse) return corsPreflightResponse;
223
246
  const assetResponse = await fetchAsset(request, env);
224
247
  if (assetResponse) return assetResponse;
225
248
  const bffResponse = await dispatchBffRequest(request, env);
226
- if (bffResponse) return bffResponse;
249
+ if (bffResponse) return withCorsHeaders(bffResponse);
227
250
  const route = findRoute(request);
228
- if (route?.worker) return dispatchRouteWorker(route, request, env, ctx);
251
+ if (route?.worker) return withCorsHeaders(await dispatchRouteWorker(route, request, env, ctx));
229
252
  const htmlResponse = await fetchRouteHtml(route, request, env);
230
253
  if (htmlResponse) return htmlResponse;
231
- return new Response('Not found', {
254
+ return withCorsHeaders(new Response('Not found', {
232
255
  status: 404
233
- });
256
+ }));
234
257
  }
235
258
  };
@@ -60,7 +60,8 @@ const createWorkerManifest = async (outputDirectory, modernConfig)=>{
60
60
  },
61
61
  assets: {
62
62
  binding: ASSETS_BINDING,
63
- directory: `./${PUBLIC_ASSETS_DIRECTORY}`
63
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
64
+ runWorkerFirst: true
64
65
  },
65
66
  routeSpec: {
66
67
  file: ROUTE_SPEC_OUTPUT,
@@ -153,7 +154,8 @@ const createCloudflarePreset = ({ appContext, modernConfig })=>{
153
154
  ],
154
155
  assets: {
155
156
  directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
156
- binding: ASSETS_BINDING
157
+ binding: ASSETS_BINDING,
158
+ run_worker_first: true
157
159
  }
158
160
  }, {
159
161
  spaces: 2
@@ -2,14 +2,35 @@ const ASSETS_BINDING = 'ASSETS';
2
2
  const MODERN_WORKER_MANIFEST = p_workerManifest;
3
3
  const WORKER_MODULE_LOADERS = p_workerModuleLoaders;
4
4
  const workerModulePromises = new Map();
5
+ const CORS_HEADERS = {
6
+ 'access-control-allow-headers': '*',
7
+ 'access-control-allow-methods': 'GET, HEAD, OPTIONS',
8
+ 'access-control-allow-origin': '*'
9
+ };
5
10
  globalThis.__dirname ??= '/';
6
11
  globalThis.__filename ??= '/index.js';
12
+ function withCorsHeaders(response) {
13
+ const headers = new Headers(response.headers);
14
+ for (const [name, value] of Object.entries(CORS_HEADERS))if (!headers.has(name)) headers.set(name, value);
15
+ return new Response(response.body, {
16
+ headers,
17
+ status: response.status,
18
+ statusText: response.statusText
19
+ });
20
+ }
21
+ function createCorsPreflightResponse(request) {
22
+ if ('OPTIONS' !== request.method) return null;
23
+ return new Response(null, {
24
+ headers: CORS_HEADERS,
25
+ status: 204
26
+ });
27
+ }
7
28
  async function fetchAsset(request, env) {
8
29
  const assets = env?.[ASSETS_BINDING];
9
30
  if (!assets || 'function' != typeof assets.fetch) return null;
10
31
  const response = await assets.fetch(request);
11
32
  if (404 === response.status) return null;
12
- return response;
33
+ return withCorsHeaders(response);
13
34
  }
14
35
  async function fetchAssetByPath(pathname, request, env) {
15
36
  const url = new URL(request.url);
@@ -220,16 +241,18 @@ async function dispatchBffRequest(request, env) {
220
241
  }
221
242
  export default {
222
243
  async fetch (request, env, ctx) {
244
+ const corsPreflightResponse = createCorsPreflightResponse(request);
245
+ if (corsPreflightResponse) return corsPreflightResponse;
223
246
  const assetResponse = await fetchAsset(request, env);
224
247
  if (assetResponse) return assetResponse;
225
248
  const bffResponse = await dispatchBffRequest(request, env);
226
- if (bffResponse) return bffResponse;
249
+ if (bffResponse) return withCorsHeaders(bffResponse);
227
250
  const route = findRoute(request);
228
- if (route?.worker) return dispatchRouteWorker(route, request, env, ctx);
251
+ if (route?.worker) return withCorsHeaders(await dispatchRouteWorker(route, request, env, ctx));
229
252
  const htmlResponse = await fetchRouteHtml(route, request, env);
230
253
  if (htmlResponse) return htmlResponse;
231
- return new Response('Not found', {
254
+ return withCorsHeaders(new Response('Not found', {
232
255
  status: 404
233
- });
256
+ }));
234
257
  }
235
258
  };
@@ -1,4 +1,4 @@
1
1
  declare const _default: {
2
- fetch(request: any, env: any, ctx: any): Promise<any>;
2
+ fetch(request: any, env: any, ctx: any): Promise<Response>;
3
3
  };
4
4
  export default _default;
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "modern",
18
18
  "modern.js"
19
19
  ],
20
- "version": "3.2.0-ultramodern.41",
20
+ "version": "3.2.0-ultramodern.43",
21
21
  "types": "./dist/types/index.d.ts",
22
22
  "main": "./dist/cjs/index.js",
23
23
  "exports": {
@@ -98,16 +98,16 @@
98
98
  "ndepe": "^0.1.13",
99
99
  "pkg-types": "^2.3.1",
100
100
  "std-env": "4.1.0",
101
- "@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.2.0-ultramodern.41",
102
- "@modern-js/i18n-utils": "npm:@bleedingdev/modern-js-i18n-utils@3.2.0-ultramodern.41",
103
- "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.2.0-ultramodern.41",
104
- "@modern-js/plugin-data-loader": "npm:@bleedingdev/modern-js-plugin-data-loader@3.2.0-ultramodern.41",
105
- "@modern-js/server": "npm:@bleedingdev/modern-js-server@3.2.0-ultramodern.41",
106
- "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.41",
107
- "@modern-js/prod-server": "npm:@bleedingdev/modern-js-prod-server@3.2.0-ultramodern.41",
108
- "@modern-js/server-utils": "npm:@bleedingdev/modern-js-server-utils@3.2.0-ultramodern.41",
109
- "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.41",
110
- "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.41"
101
+ "@modern-js/builder": "npm:@bleedingdev/modern-js-builder@3.2.0-ultramodern.43",
102
+ "@modern-js/i18n-utils": "npm:@bleedingdev/modern-js-i18n-utils@3.2.0-ultramodern.43",
103
+ "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.2.0-ultramodern.43",
104
+ "@modern-js/plugin-data-loader": "npm:@bleedingdev/modern-js-plugin-data-loader@3.2.0-ultramodern.43",
105
+ "@modern-js/prod-server": "npm:@bleedingdev/modern-js-prod-server@3.2.0-ultramodern.43",
106
+ "@modern-js/server": "npm:@bleedingdev/modern-js-server@3.2.0-ultramodern.43",
107
+ "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.43",
108
+ "@modern-js/server-utils": "npm:@bleedingdev/modern-js-server-utils@3.2.0-ultramodern.43",
109
+ "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.43",
110
+ "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.43"
111
111
  },
112
112
  "devDependencies": {
113
113
  "@rslib/core": "0.21.5",