@aws-blocks/core 0.1.13 → 0.1.18

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 (65) hide show
  1. package/README.md +180 -17
  2. package/dist/cdk/blocks-backend.d.ts +4 -0
  3. package/dist/cdk/blocks-backend.d.ts.map +1 -1
  4. package/dist/cdk/blocks-backend.js +23 -1
  5. package/dist/cdk/blocks-backend.test.js +71 -1
  6. package/dist/cdk/blocks-stack.test.js +32 -1
  7. package/dist/cdk/index.d.ts +13 -0
  8. package/dist/cdk/index.d.ts.map +1 -1
  9. package/dist/cdk/index.js +24 -0
  10. package/dist/cors.d.ts +27 -1
  11. package/dist/cors.d.ts.map +1 -1
  12. package/dist/cors.js +55 -2
  13. package/dist/cors.test.js +81 -2
  14. package/dist/errors.test.js +26 -1
  15. package/dist/hosting.d.ts.map +1 -1
  16. package/dist/hosting.js +26 -1
  17. package/dist/hosting.test.js +73 -0
  18. package/dist/lambda-handler.d.ts.map +1 -1
  19. package/dist/lambda-handler.js +4 -17
  20. package/dist/lambda-handler.test.js +59 -2
  21. package/dist/rpc.test.js +77 -1
  22. package/dist/scripts/console.d.ts.map +1 -1
  23. package/dist/scripts/console.js +30 -2
  24. package/dist/scripts/deploy-stream.d.ts +181 -0
  25. package/dist/scripts/deploy-stream.d.ts.map +1 -0
  26. package/dist/scripts/deploy-stream.js +332 -0
  27. package/dist/scripts/deploy-stream.test.d.ts +2 -0
  28. package/dist/scripts/deploy-stream.test.d.ts.map +1 -0
  29. package/dist/scripts/deploy-stream.test.js +845 -0
  30. package/dist/scripts/deploy.d.ts.map +1 -1
  31. package/dist/scripts/deploy.js +16 -9
  32. package/dist/scripts/dev-server-cors.test.js +19 -1
  33. package/dist/scripts/dev-server-rpc.test.js +50 -0
  34. package/dist/scripts/dev-server.d.ts +8 -0
  35. package/dist/scripts/dev-server.d.ts.map +1 -1
  36. package/dist/scripts/dev-server.js +35 -8
  37. package/dist/scripts/sandbox.js +1 -1
  38. package/dist/telemetry/client.js +4 -4
  39. package/dist/telemetry/telemetry-send-worker.js +4 -0
  40. package/dist/version.d.ts +1 -1
  41. package/dist/version.js +1 -1
  42. package/package.json +10 -1
  43. package/src/cdk/blocks-backend.test.ts +90 -1
  44. package/src/cdk/blocks-backend.ts +24 -1
  45. package/src/cdk/blocks-stack.test.ts +41 -1
  46. package/src/cdk/index.ts +25 -0
  47. package/src/cors.test.ts +96 -2
  48. package/src/cors.ts +59 -2
  49. package/src/errors.test.ts +29 -1
  50. package/src/hosting.test.ts +107 -0
  51. package/src/hosting.ts +27 -1
  52. package/src/lambda-handler.test.ts +71 -2
  53. package/src/lambda-handler.ts +4 -20
  54. package/src/rpc.test.ts +96 -1
  55. package/src/scripts/console.ts +29 -2
  56. package/src/scripts/deploy-stream.test.ts +1035 -0
  57. package/src/scripts/deploy-stream.ts +475 -0
  58. package/src/scripts/deploy.ts +18 -11
  59. package/src/scripts/dev-server-cors.test.ts +26 -1
  60. package/src/scripts/dev-server-rpc.test.ts +54 -0
  61. package/src/scripts/dev-server.ts +38 -8
  62. package/src/scripts/sandbox.ts +1 -1
  63. package/src/telemetry/client.ts +4 -4
  64. package/src/telemetry/telemetry-send-worker.ts +5 -0
  65. package/src/version.ts +1 -1
@@ -13,6 +13,7 @@ import { ApiError } from '../errors.js';
13
13
  import { BLOCKS_RPC_PREFIX, BLOCKS_SANDBOX_PREFIX } from '../constants.js';
14
14
  import { BLOCKS_SANDBOX_DIR } from '../common/constants.js';
15
15
  import { matchRoute, lockRouteRegistry } from '../raw-route.js';
16
+ import { CORS_MAX_AGE } from '../cors.js';
16
17
  import { registerBuiltinRoutes } from '../builtin-routes.js';
17
18
  import {
18
19
  parseRpcRequest,
@@ -45,6 +46,24 @@ export function resolveDevCorsOrigin(origin: string): string {
45
46
  return LOCALHOST_PATTERN.test(origin) ? origin : 'http://localhost:3000';
46
47
  }
47
48
 
49
+ /**
50
+ * Build the CORS response headers the dev server sets on every request.
51
+ *
52
+ * Mirrors the Lambda path's cache posture — same {@link CORS_MAX_AGE} and the
53
+ * same `Vary: Origin` — so the reflected `Access-Control-Allow-Origin` can't be
54
+ * served across origins by a shared cache, and so the two sites can't drift.
55
+ */
56
+ export function buildDevCorsHeaders(requestOrigin: string): Record<string, string> {
57
+ return {
58
+ 'Access-Control-Allow-Origin': resolveDevCorsOrigin(requestOrigin),
59
+ 'Vary': 'Origin',
60
+ 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS',
61
+ 'Access-Control-Allow-Headers': 'Content-Type',
62
+ 'Access-Control-Allow-Credentials': 'true',
63
+ 'Access-Control-Max-Age': CORS_MAX_AGE,
64
+ };
65
+ }
66
+
48
67
  /** Shape of the client runtime config the browser fetches to discover the API URL. */
49
68
  export interface BlocksRuntimeConfig {
50
69
  apiUrl: string;
@@ -854,12 +873,9 @@ export async function startDevServer(options: DevServerOptions) {
854
873
 
855
874
  // CORS headers
856
875
  const requestOrigin = req.headers.origin || '';
857
- const allowedOrigin = resolveDevCorsOrigin(requestOrigin);
858
- res.setHeader('Access-Control-Allow-Origin', allowedOrigin);
859
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS');
860
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
861
- res.setHeader('Access-Control-Allow-Credentials', 'true');
862
- res.setHeader('Access-Control-Max-Age', '86400');
876
+ for (const [name, value] of Object.entries(buildDevCorsHeaders(requestOrigin))) {
877
+ res.setHeader(name, value);
878
+ }
863
879
 
864
880
  if (method === 'OPTIONS') {
865
881
  res.writeHead(200);
@@ -1244,6 +1260,20 @@ function handleApiRequest(
1244
1260
  return;
1245
1261
  }
1246
1262
 
1247
- res.writeHead(404);
1248
- res.end();
1263
+ // Anything that reaches here didn't match `POST /aws-blocks/api`. Returning a
1264
+ // bare empty 404 leaves a developer poking at the endpoint (e.g. opening it in
1265
+ // a browser, which sends a GET, or trying a REST-style URL) with no feedback.
1266
+ // Reply with a small JSON hint pointing at the one supported shape. When the
1267
+ // path was right but the method wasn't, say so explicitly.
1268
+ const wrongMethodOnApi = url.pathname === BLOCKS_RPC_PREFIX;
1269
+ const message = wrongMethodOnApi
1270
+ ? `The AWS Blocks JSON-RPC API expects POST, not ${method}.`
1271
+ : 'Not found. The AWS Blocks JSON-RPC API is served at POST /aws-blocks/api.';
1272
+ res.writeHead(404, { 'Content-Type': 'application/json' });
1273
+ res.end(
1274
+ JSON.stringify({
1275
+ error: message,
1276
+ expected: { method: 'POST', path: BLOCKS_RPC_PREFIX },
1277
+ }),
1278
+ );
1249
1279
  }
@@ -150,7 +150,7 @@ export async function startSandbox(options: SandboxOptions) {
150
150
  console.log(`\n Open http://localhost:${clientPort}\n`);
151
151
 
152
152
  const cdkWatch = spawnCommand("npx", [
153
- "cdk", "watch", "--hotswap",
153
+ "cdk", "watch",
154
154
  `--outputs-file`, `${outDir}/outputs.json`,
155
155
  `--context`, `projectRoot=${process.cwd()}`,
156
156
  `--context`, `sandboxMode=true`,
@@ -199,16 +199,16 @@ export function sendEvent(event: BlocksTelemetryEvent): void {
199
199
  const workerPath = path.join(dir, 'telemetry-send-worker.js');
200
200
  const child = spawn(process.execPath, [workerPath, endpoint], {
201
201
  detached: true,
202
- stdio: ['pipe', 'ignore', 'ignore'],
202
+ stdio: ['pipe', 'ignore', process.env.NODE_DEBUG?.includes('blocks-telemetry') ? 'inherit' : 'ignore'],
203
203
  // Clear NODE_OPTIONS so inherited flags (e.g. --conditions=cdk) don't interfere
204
204
  env: { ...process.env, NODE_OPTIONS: '' },
205
205
  });
206
206
 
207
- child.stdin!.on('error', (err) => { debug('stdin write failed: %s', err.message); });
207
+ child.stdin?.on('error', (err) => { debug('stdin write failed: %s', err.message); });
208
208
  // Payload is small (<1KB JSON) so it fits in the kernel pipe buffer (~64KB)
209
209
  // and survives the parent closing its fd on exit.
210
- child.stdin!.write(payload);
211
- child.stdin!.end();
210
+ child.stdin?.write(payload);
211
+ child.stdin?.end();
212
212
  child.on('error', (err) => { debug('spawn failed: %s', err.message); });
213
213
  child.unref();
214
214
  debug('spawned telemetry subprocess (pid=%d)', child.pid);
@@ -1,6 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ // NOTE: This worker is an intentional near-duplicate of
5
+ // packages/create-blocks-app/src/telemetry-send-worker.ts. It is kept separate
6
+ // because the worker must run with zero project/package imports. Any change
7
+ // here should be mirrored in both files.
8
+
4
9
  /**
5
10
  * Telemetry send worker — spawned as a detached subprocess.
6
11
  * Reads JSON payload from stdin, POSTs it to the endpoint (argv[2]).
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Auto-generated by scripts/generate-version.mjs — do not edit manually
2
- export const CORE_VERSION = '0.1.13';
2
+ export const CORE_VERSION = '0.1.18';