@sanity/runtime-cli 10.6.1 → 10.6.3

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.
package/README.md CHANGED
@@ -20,7 +20,7 @@ $ npm install -g @sanity/runtime-cli
20
20
  $ sanity-run COMMAND
21
21
  running command...
22
22
  $ sanity-run (--version)
23
- @sanity/runtime-cli/10.6.1 linux-x64 node-v22.19.0
23
+ @sanity/runtime-cli/10.6.3 linux-x64 node-v22.19.0
24
24
  $ sanity-run --help [COMMAND]
25
25
  USAGE
26
26
  $ sanity-run COMMAND
@@ -88,7 +88,7 @@ EXAMPLES
88
88
  $ sanity-run blueprints add function --name my-function --fn-type document-create --fn-type document-update --lang js
89
89
  ```
90
90
 
91
- _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/add.ts)_
91
+ _See code: [src/commands/blueprints/add.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/add.ts)_
92
92
 
93
93
  ## `sanity-run blueprints config`
94
94
 
@@ -119,7 +119,7 @@ EXAMPLES
119
119
  $ sanity-run blueprints config --edit --project-id <projectId> --stack-id <stackId>
120
120
  ```
121
121
 
122
- _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/config.ts)_
122
+ _See code: [src/commands/blueprints/config.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/config.ts)_
123
123
 
124
124
  ## `sanity-run blueprints deploy`
125
125
 
@@ -141,7 +141,7 @@ EXAMPLES
141
141
  $ sanity-run blueprints deploy --no-wait
142
142
  ```
143
143
 
144
- _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/deploy.ts)_
144
+ _See code: [src/commands/blueprints/deploy.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/deploy.ts)_
145
145
 
146
146
  ## `sanity-run blueprints destroy`
147
147
 
@@ -166,7 +166,7 @@ EXAMPLES
166
166
  $ sanity-run blueprints destroy --stack-id <stackId> --project-id <projectId> --force --no-wait
167
167
  ```
168
168
 
169
- _See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/destroy.ts)_
169
+ _See code: [src/commands/blueprints/destroy.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/destroy.ts)_
170
170
 
171
171
  ## `sanity-run blueprints info`
172
172
 
@@ -188,7 +188,7 @@ EXAMPLES
188
188
  $ sanity-run blueprints info --stack-id <stackId>
189
189
  ```
190
190
 
191
- _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/info.ts)_
191
+ _See code: [src/commands/blueprints/info.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/info.ts)_
192
192
 
193
193
  ## `sanity-run blueprints init [DIR]`
194
194
 
@@ -226,7 +226,7 @@ EXAMPLES
226
226
  $ sanity-run blueprints init --blueprint-type <json|js|ts> --stack-name <stackName>
227
227
  ```
228
228
 
229
- _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/init.ts)_
229
+ _See code: [src/commands/blueprints/init.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/init.ts)_
230
230
 
231
231
  ## `sanity-run blueprints logs`
232
232
 
@@ -248,7 +248,7 @@ EXAMPLES
248
248
  $ sanity-run blueprints logs --watch
249
249
  ```
250
250
 
251
- _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/logs.ts)_
251
+ _See code: [src/commands/blueprints/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/logs.ts)_
252
252
 
253
253
  ## `sanity-run blueprints plan`
254
254
 
@@ -265,7 +265,7 @@ EXAMPLES
265
265
  $ sanity-run blueprints plan
266
266
  ```
267
267
 
268
- _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/plan.ts)_
268
+ _See code: [src/commands/blueprints/plan.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/plan.ts)_
269
269
 
270
270
  ## `sanity-run blueprints stacks`
271
271
 
@@ -287,7 +287,7 @@ EXAMPLES
287
287
  $ sanity-run blueprints stacks --project-id <projectId>
288
288
  ```
289
289
 
290
- _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/blueprints/stacks.ts)_
290
+ _See code: [src/commands/blueprints/stacks.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/blueprints/stacks.ts)_
291
291
 
292
292
  ## `sanity-run functions dev`
293
293
 
@@ -307,7 +307,7 @@ EXAMPLES
307
307
  $ sanity-run functions dev --port 8974
308
308
  ```
309
309
 
310
- _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/dev.ts)_
310
+ _See code: [src/commands/functions/dev.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/dev.ts)_
311
311
 
312
312
  ## `sanity-run functions env add NAME KEY VALUE`
313
313
 
@@ -329,7 +329,7 @@ EXAMPLES
329
329
  $ sanity-run functions env add MyFunction API_URL https://api.example.com/
330
330
  ```
331
331
 
332
- _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/env/add.ts)_
332
+ _See code: [src/commands/functions/env/add.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/env/add.ts)_
333
333
 
334
334
  ## `sanity-run functions env list NAME`
335
335
 
@@ -349,7 +349,7 @@ EXAMPLES
349
349
  $ sanity-run functions env list MyFunction
350
350
  ```
351
351
 
352
- _See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/env/list.ts)_
352
+ _See code: [src/commands/functions/env/list.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/env/list.ts)_
353
353
 
354
354
  ## `sanity-run functions env remove NAME KEY`
355
355
 
@@ -370,7 +370,7 @@ EXAMPLES
370
370
  $ sanity-run functions env remove MyFunction API_URL
371
371
  ```
372
372
 
373
- _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/env/remove.ts)_
373
+ _See code: [src/commands/functions/env/remove.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/env/remove.ts)_
374
374
 
375
375
  ## `sanity-run functions logs NAME`
376
376
 
@@ -404,7 +404,7 @@ EXAMPLES
404
404
  $ sanity-run functions logs <name> --delete
405
405
  ```
406
406
 
407
- _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/logs.ts)_
407
+ _See code: [src/commands/functions/logs.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/logs.ts)_
408
408
 
409
409
  ## `sanity-run functions test NAME`
410
410
 
@@ -447,9 +447,11 @@ EXAMPLES
447
447
  $ sanity-run functions test <name> --file 'payload.json'
448
448
 
449
449
  $ sanity-run functions test <name> --data '{ "id": 1 }' --timeout 60
450
+
451
+ $ sanity-run functions test <name> --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'
450
452
  ```
451
453
 
452
- _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.1/src/commands/functions/test.ts)_
454
+ _See code: [src/commands/functions/test.ts](https://github.com/sanity-io/runtime-cli/blob/v10.6.3/src/commands/functions/test.ts)_
453
455
 
454
456
  ## `sanity-run help [COMMAND]`
455
457
 
@@ -1,5 +1,7 @@
1
+ import AdmZip from 'adm-zip';
1
2
  import type { AuthParams, FunctionResource } from '../../utils/types.js';
2
- export declare const stashUrl: string;
3
+ export declare const ASSET_CHECK_URL: string;
4
+ export declare const ASSET_STASH_URL: string;
3
5
  export declare function stashAsset({ resource, auth, }: {
4
6
  resource: FunctionResource;
5
7
  auth: AuthParams;
@@ -9,3 +11,11 @@ export declare function stashAsset({ resource, auth, }: {
9
11
  outputPath?: string;
10
12
  error?: string;
11
13
  }>;
14
+ export declare function hashBuffer(buffer: Buffer): string;
15
+ export declare function pathToZip(path: string): Promise<AdmZip>;
16
+ export declare function pathToB64ZipHash(path: string): Promise<{
17
+ b64: string;
18
+ buffer: Buffer;
19
+ hash: string;
20
+ zip: AdmZip;
21
+ }>;
@@ -1,3 +1,4 @@
1
+ import crypto from 'node:crypto';
1
2
  import fs from 'node:fs';
2
3
  import path from 'node:path';
3
4
  import { cwd } from 'node:process';
@@ -10,7 +11,9 @@ import { shouldTranspileFunction } from '../../utils/functions/should-transpile.
10
11
  import getHeaders from '../../utils/get-headers.js';
11
12
  import { transpileFunction } from '../../utils/transpile/transpile-function.js';
12
13
  const { apiUrl } = config;
13
- export const stashUrl = `${apiUrl}vX/blueprints/assets/stash`;
14
+ const ASSETS_URL = `${apiUrl}vX/blueprints/assets`;
15
+ export const ASSET_CHECK_URL = `${ASSETS_URL}/check`;
16
+ export const ASSET_STASH_URL = `${ASSETS_URL}/stash`;
14
17
  const MAX_ASSET_SIZE = 209_715_200; // 200 MB in bytes
15
18
  export async function stashAsset({ resource, auth, }) {
16
19
  if (!resource.src)
@@ -33,26 +36,29 @@ export async function stashAsset({ resource, auth, }) {
33
36
  await resolveResourceDependencies(resource, shouldTranspile);
34
37
  }
35
38
  try {
36
- const stats = await fs.promises.stat(functionPath);
37
39
  // This works for files and folders
38
40
  const size = await getFolderSize.loose(functionPath);
39
41
  if (size > MAX_ASSET_SIZE) {
40
42
  throw new Error('Resource is larger than max asset size of 200 MB.');
41
43
  }
42
- const zip = new AdmZip();
43
- if (stats.isDirectory()) {
44
- zip.addLocalFolder(functionPath);
45
- }
46
- else {
47
- zip.addLocalFile(functionPath, '', 'index.js');
44
+ const { b64, hash } = await pathToB64ZipHash(functionPath);
45
+ try {
46
+ const checkResponse = await fetch(`${ASSET_CHECK_URL}/${hash}`, {
47
+ method: 'GET',
48
+ headers: getHeaders(auth),
49
+ });
50
+ const checkJson = await checkResponse.json();
51
+ if (checkResponse.ok && checkJson.exists && checkJson.id) {
52
+ return { success: true, assetId: checkJson.id };
53
+ }
48
54
  }
49
- const zipBuffer = zip.toBuffer();
50
- const base64Zip = zipBuffer.toString('base64');
51
- const assetResponse = await fetch(stashUrl, {
55
+ catch { } // Continue to stash the Asset
56
+ // Asset doesn't exist, so stash it
57
+ const assetResponse = await fetch(ASSET_STASH_URL, {
52
58
  method: 'POST',
53
59
  headers: getHeaders(auth),
54
60
  body: JSON.stringify({
55
- file: base64Zip,
61
+ file: b64,
56
62
  filename: `${resource.name}.zip`,
57
63
  }),
58
64
  });
@@ -72,3 +78,24 @@ export async function stashAsset({ resource, auth, }) {
72
78
  await cleanup();
73
79
  }
74
80
  }
81
+ export function hashBuffer(buffer) {
82
+ const hash = crypto.createHash('sha256');
83
+ hash.update(buffer);
84
+ return hash.digest('hex');
85
+ }
86
+ export async function pathToZip(path) {
87
+ const stats = await fs.promises.stat(path);
88
+ const zip = new AdmZip();
89
+ if (stats.isDirectory())
90
+ zip.addLocalFolder(path);
91
+ else
92
+ zip.addLocalFile(path, '', 'index.js');
93
+ return zip;
94
+ }
95
+ export async function pathToB64ZipHash(path) {
96
+ const zip = await pathToZip(path);
97
+ const buffer = zip.toBuffer();
98
+ const b64 = buffer.toString('base64');
99
+ const hash = hashBuffer(buffer);
100
+ return { b64, buffer, hash, zip };
101
+ }
@@ -10,6 +10,7 @@ export default class TestCommand extends BlueprintCommand {
10
10
  `<%= config.bin %> <%= command.id %> <name> --data '{ "id": 1 }'`,
11
11
  `<%= config.bin %> <%= command.id %> <name> --file 'payload.json'`,
12
12
  `<%= config.bin %> <%= command.id %> <name> --data '{ "id": 1 }' --timeout 60`,
13
+ `<%= config.bin %> <%= command.id %> <name> --event update --data-before '{ "title": "before" }' --data-after '{ "title": "after" }'`,
13
14
  ];
14
15
  static flags = {
15
16
  data: Flags.string({
@@ -141,7 +142,7 @@ export default class TestCommand extends BlueprintCommand {
141
142
  async run() {
142
143
  if (this.flags.event === 'update') {
143
144
  const hasDataPair = this.flags['data-before'] && this.flags['data-after'];
144
- const hasFilePair = this.flags['file-before'] && this.flags['file-before'];
145
+ const hasFilePair = this.flags['file-before'] && this.flags['file-after'];
145
146
  const hasDocPair = this.flags['document-id-before'] && this.flags['document-id-after'];
146
147
  if (!(hasDataPair || hasFilePair || hasDocPair)) {
147
148
  this.error('When using --event=update, you must provide one of the following flag pairs:\n' +
@@ -696,7 +696,8 @@
696
696
  "examples": [
697
697
  "<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }'",
698
698
  "<%= config.bin %> <%= command.id %> <name> --file 'payload.json'",
699
- "<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }' --timeout 60"
699
+ "<%= config.bin %> <%= command.id %> <name> --data '{ \"id\": 1 }' --timeout 60",
700
+ "<%= config.bin %> <%= command.id %> <name> --event update --data-before '{ \"title\": \"before\" }' --data-after '{ \"title\": \"after\" }'"
700
701
  ],
701
702
  "flags": {
702
703
  "data": {
@@ -1023,5 +1024,5 @@
1023
1024
  ]
1024
1025
  }
1025
1026
  },
1026
- "version": "10.6.1"
1027
+ "version": "10.6.3"
1027
1028
  }
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": "10.6.1",
4
+ "version": "10.6.3",
5
5
  "author": "Sanity Runtime Team",
6
6
  "type": "module",
7
7
  "license": "MIT",