@sanity/runtime-cli 4.3.4 → 4.3.5-bundle.0

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 (42) hide show
  1. package/README.md +17 -17
  2. package/dist/actions/blueprints/assets.d.ts +1 -0
  3. package/dist/actions/blueprints/assets.js +21 -4
  4. package/dist/actions/functions/test.d.ts +2 -2
  5. package/dist/actions/functions/test.js +2 -2
  6. package/dist/commands/blueprints/deploy.js +3 -4
  7. package/dist/commands/functions/test.js +3 -6
  8. package/dist/server/app.js +80 -12
  9. package/dist/server/static/api.js +24 -3
  10. package/dist/server/static/components/app.css +74 -23
  11. package/dist/server/static/components/filters.js +1 -1
  12. package/dist/server/static/components/function-list.js +5 -5
  13. package/dist/server/static/components/payload-panel.js +4 -3
  14. package/dist/server/static/components/response-panel.js +37 -35
  15. package/dist/server/static/index.html +5 -3
  16. package/dist/server/static/vendor/vendor.bundle.d.ts +2 -2
  17. package/dist/utils/build-payload.d.ts +1 -1
  18. package/dist/utils/build-payload.js +3 -3
  19. package/dist/utils/bundle/bundle-function.d.ts +8 -0
  20. package/dist/utils/bundle/bundle-function.js +125 -0
  21. package/dist/utils/bundle/cleanup-source-maps.d.ts +10 -0
  22. package/dist/utils/bundle/cleanup-source-maps.js +53 -0
  23. package/dist/utils/bundle/find-up.d.ts +16 -0
  24. package/dist/utils/bundle/find-up.js +39 -0
  25. package/dist/utils/bundle/verify-handler.d.ts +2 -0
  26. package/dist/utils/bundle/verify-handler.js +13 -0
  27. package/dist/utils/child-process-wrapper.js +8 -6
  28. package/dist/utils/functions/find-entry-point.d.ts +11 -0
  29. package/dist/utils/functions/find-entry-point.js +75 -0
  30. package/dist/utils/functions/should-bundle.d.ts +2 -0
  31. package/dist/utils/functions/should-bundle.js +23 -0
  32. package/dist/utils/invoke-local.d.ts +2 -2
  33. package/dist/utils/invoke-local.js +48 -7
  34. package/dist/utils/is-record.d.ts +1 -0
  35. package/dist/utils/is-record.js +3 -0
  36. package/dist/utils/parse-json-object.d.ts +1 -0
  37. package/dist/utils/parse-json-object.js +10 -0
  38. package/dist/utils/types.d.ts +3 -1
  39. package/oclif.manifest.json +1 -1
  40. package/package.json +4 -1
  41. package/dist/utils/is-json.d.ts +0 -1
  42. package/dist/utils/is-json.js +0 -12
@@ -1,42 +1,80 @@
1
1
  import { spawn } from 'node:child_process';
2
+ import { performance } from 'node:perf_hooks';
2
3
  import { cwd } from 'node:process';
3
4
  import { setTimeout } from 'node:timers';
4
5
  import config from '../config.js';
6
+ import { bundleFunction } from './bundle/bundle-function.js';
7
+ import { findFunctionEntryPoint } from './functions/find-entry-point.js';
8
+ import { shouldBundleFunction } from './functions/should-bundle.js';
5
9
  function getChildProcessWrapperPath() {
6
10
  return new URL('./child-process-wrapper.js', import.meta.url).pathname;
7
11
  }
8
12
  export function sanitizeLogs(logs) {
9
13
  return logs.replace(/([a-zA-Z0-9]{10})[a-zA-Z0-9]{65,}/g, '$1**********');
10
14
  }
11
- export default async function invoke(srcPath, data, context, timeout = 5) {
15
+ export default async function invoke(resource, data, context, timeout = 5) {
16
+ if (!resource.src) {
17
+ throw new Error(`Function resource "${resource.name}" is missing the 'src' property.`);
18
+ }
19
+ let cleanupBundle = async () => { };
20
+ let functionPath = '';
21
+ let bundleTimings = undefined;
22
+ if (await shouldBundleFunction(resource)) {
23
+ const bundleResult = await bundleFunction(resource);
24
+ functionPath = await findFunctionEntryPoint(bundleResult.outputDir);
25
+ bundleTimings = bundleResult.timings;
26
+ cleanupBundle = bundleResult.cleanup;
27
+ }
28
+ else {
29
+ functionPath = await findFunctionEntryPoint(resource.src, resource.displayName ?? resource.name);
30
+ }
12
31
  return new Promise((resolve, reject) => {
13
32
  let child;
14
33
  let timer;
34
+ let executionStart;
15
35
  function start() {
36
+ executionStart = performance.now();
16
37
  child = spawn('node', [getChildProcessWrapperPath()], {
17
38
  cwd: cwd(),
18
39
  stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
19
40
  });
20
- // Note: start a timeout so child process doesn't run forever
21
41
  child.on('message', (data) => {
42
+ const executionTimeMs = performance.now() - executionStart;
22
43
  const { json, logs } = JSON.parse(data.toString());
23
44
  shutdown();
24
- resolve({ json, logs: sanitizeLogs(logs), error: '' });
45
+ resolve({
46
+ json,
47
+ logs: sanitizeLogs(logs),
48
+ error: undefined,
49
+ timings: {
50
+ ...bundleTimings,
51
+ execute: executionTimeMs,
52
+ },
53
+ });
25
54
  });
26
55
  child.on('error', (error) => {
56
+ shutdown();
27
57
  reject(new Error(`encountered error ${error.message}`));
28
58
  });
29
59
  child.on('exit', (code) => {
60
+ const executionTimeMs = performance.now() - executionStart;
30
61
  shutdown();
31
62
  if (code !== 0) {
32
63
  reject(new Error(`exited with code ${code}`));
33
64
  }
34
65
  else {
35
- resolve({ json: {}, logs: '', error: '' });
66
+ resolve({
67
+ json: {},
68
+ logs: '',
69
+ error: undefined,
70
+ timings: {
71
+ ...bundleTimings,
72
+ execute: executionTimeMs,
73
+ },
74
+ });
36
75
  }
37
76
  });
38
77
  timer = setTimeout(() => {
39
- // timedOut = true
40
78
  shutdown();
41
79
  reject(new Error(`Timed out after hitting its ${timeout}s timeout!`));
42
80
  }, timeout * 1000);
@@ -51,11 +89,14 @@ export default async function invoke(srcPath, data, context, timeout = 5) {
51
89
  },
52
90
  },
53
91
  };
54
- child.send(JSON.stringify({ srcPath, payload }, null, 2));
92
+ child.send(JSON.stringify({ srcPath: functionPath, payload }, null, 2));
55
93
  }
56
94
  function shutdown() {
57
95
  clearTimeout(timer);
58
- child.kill();
96
+ if (child && !child.killed) {
97
+ child.kill();
98
+ }
99
+ cleanupBundle().catch((err) => console.warn('Bundle cleanup failed:', err));
59
100
  }
60
101
  start();
61
102
  });
@@ -0,0 +1 @@
1
+ export declare function isRecord(value: unknown): value is Record<string, unknown>;
@@ -0,0 +1,3 @@
1
+ export function isRecord(value) {
2
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
3
+ }
@@ -0,0 +1 @@
1
+ export declare function parseJsonObject(jsonString: string): null | Record<string, unknown>;
@@ -0,0 +1,10 @@
1
+ import { isRecord } from './is-record.js';
2
+ export function parseJsonObject(jsonString) {
3
+ try {
4
+ const o = JSON.parse(jsonString);
5
+ return isRecord(o) ? o : null;
6
+ }
7
+ catch {
8
+ return null;
9
+ }
10
+ }
@@ -12,7 +12,7 @@ export interface LocalBlueprint {
12
12
  /** @link https://github.com/sanity-io/blueprints-rfc/blob/main/readme.md#outputs */
13
13
  outputs?: Array<Record<string, unknown>>;
14
14
  /** @link https://github.com/sanity-io/blueprints-rfc/blob/main/readme.md#resources */
15
- resources?: Array<LocalResource>;
15
+ resources?: Array<LocalResource | LocalFunctionResource>;
16
16
  /** @link https://github.com/sanity-io/blueprints-rfc/blob/main/readme.md#parameters */
17
17
  parameters?: Array<{
18
18
  name: string;
@@ -51,6 +51,7 @@ export declare function isLocalFunctionResource(r: LocalResource): r is LocalFun
51
51
  /** @internal */
52
52
  export interface LocalFunctionResource extends LocalResource {
53
53
  src?: string;
54
+ bundle?: boolean;
54
55
  memory?: number;
55
56
  timeout?: number;
56
57
  env?: Record<string, string>;
@@ -115,6 +116,7 @@ export interface InvocationResponse {
115
116
  error: undefined | unknown;
116
117
  json: object | undefined;
117
118
  logs: string | undefined;
119
+ timings?: Record<string, number>;
118
120
  }
119
121
  /** @internal */
120
122
  export interface BlueprintLog {
@@ -769,5 +769,5 @@
769
769
  ]
770
770
  }
771
771
  },
772
- "version": "4.3.4"
772
+ "version": "4.3.5-bundle.0"
773
773
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sanity/runtime-cli",
3
3
  "description": "Sanity's Runtime CLI for Blueprints and Functions",
4
- "version": "4.3.4",
4
+ "version": "4.3.5-bundle.0",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -47,6 +47,7 @@
47
47
  "postbuild": "oclif manifest && oclif readme",
48
48
  "build:ts": "shx rm -rf dist *.tsbuildinfo && tsc -b",
49
49
  "build:static": "npm run copy:wrapper && npm run copy:server",
50
+ "clean": "shx rm -rf dist oclif.manifest.json",
50
51
  "copy:server": "shx cp -r ./src/server/static ./dist/server",
51
52
  "copy:wrapper": "shx cp ./src/utils/child-process-wrapper.js ./dist/utils/child-process-wrapper.js",
52
53
  "lint": "biome ci",
@@ -68,6 +69,8 @@
68
69
  "eventsource": "^3.0.6",
69
70
  "inquirer": "^12.5.2",
70
71
  "mime-types": "^3.0.1",
72
+ "vite": "^6.3.3",
73
+ "vite-tsconfig-paths": "^5.1.4",
71
74
  "xdg-basedir": "^5.1.0",
72
75
  "yocto-spinner": "^0.2.1"
73
76
  },
@@ -1 +0,0 @@
1
- export default function isJson(jsonString: string): null | object;
@@ -1,12 +0,0 @@
1
- export default function isJson(jsonString) {
2
- try {
3
- const o = JSON.parse(jsonString);
4
- if (o && typeof o === 'object') {
5
- return o;
6
- }
7
- }
8
- catch {
9
- return null;
10
- }
11
- return null;
12
- }