buildonce 0.1.0 → 0.15.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.
package/README.md CHANGED
@@ -18,3 +18,7 @@ async function example() {
18
18
 
19
19
  The package looks for a bundled native library under `prebuilds/`. Set
20
20
  `ONCE_LIBRARY_PATH` to load a custom `libonce` build.
21
+
22
+ `putBlob` sends bytes to the native library through the JSON SDK ABI. This
23
+ keeps the package portable across platforms, but it means very large blobs are
24
+ materialized as JSON byte arrays before crossing into Rust.
package/lib/index.d.ts CHANGED
@@ -16,7 +16,17 @@ export class OnceError extends Error {}
16
16
 
17
17
  export class Cache {
18
18
  version(): string;
19
+ /**
20
+ * Strings are encoded as UTF-8 before hashing.
21
+ */
19
22
  digest(bytes: Buffer | Uint8Array | string): string;
23
+ /**
24
+ * Strings are encoded as UTF-8 before being stored.
25
+ *
26
+ * Blob bytes currently cross the native boundary as JSON arrays, so very
27
+ * large blobs should use the Rust SDK or CLI until the JavaScript SDK has a
28
+ * streaming native ABI.
29
+ */
20
30
  putBlob(bytes: Buffer | Uint8Array | string): Promise<string>;
21
31
  getBlob(digest: string): Promise<Buffer>;
22
32
  hasBlob(digest: string): Promise<boolean>;
@@ -26,4 +36,7 @@ export class Cache {
26
36
  stats(): Promise<CacheStats>;
27
37
  }
28
38
 
39
+ /**
40
+ * Strings are encoded as UTF-8 before hashing.
41
+ */
29
42
  export function digest(bytes: Buffer | Uint8Array | string): string;
package/lib/index.js CHANGED
@@ -12,6 +12,7 @@ class OnceError extends Error {
12
12
  }
13
13
 
14
14
  const native = loadNative();
15
+ const DIGEST_PATTERN = /^[0-9a-f]{64}$/;
15
16
 
16
17
  class Cache {
17
18
  version() {
@@ -19,13 +20,12 @@ class Cache {
19
20
  }
20
21
 
21
22
  digest(bytes) {
22
- const buffer = toBuffer(bytes);
23
- const pointer = buffer.length === 0 ? null : buffer;
24
- return decodeStringResponse(native.once_digest_bytes(pointer, buffer.length));
23
+ const buffer = toBuffer(bytes, "Cache#digest bytes");
24
+ return decodeStringResponse(native.once_digest_bytes(buffer, buffer.length));
25
25
  }
26
26
 
27
27
  async putBlob(bytes) {
28
- const buffer = toBuffer(bytes);
28
+ const buffer = toBuffer(bytes, "Cache#putBlob bytes");
29
29
  return decodeRequest(
30
30
  native.once_cache_put_blob_json,
31
31
  { bytes: Array.from(buffer) },
@@ -33,15 +33,18 @@ class Cache {
33
33
  }
34
34
 
35
35
  async getBlob(digest) {
36
+ validateDigest(digest, "digest");
36
37
  const response = decodeRequest(native.once_cache_get_blob_json, { digest });
37
38
  return Buffer.from(response.bytes);
38
39
  }
39
40
 
40
41
  async hasBlob(digest) {
42
+ validateDigest(digest, "digest");
41
43
  return decodeRequest(native.once_cache_has_blob_json, { digest });
42
44
  }
43
45
 
44
46
  async putActionResult(result, actionDigest) {
47
+ validateDigest(actionDigest, "actionDigest");
45
48
  return decodeRequest(
46
49
  native.once_cache_put_action_result_json,
47
50
  {
@@ -52,6 +55,7 @@ class Cache {
52
55
  }
53
56
 
54
57
  async getActionResult(actionDigest) {
58
+ validateDigest(actionDigest, "actionDigest");
55
59
  const result = decodeRequest(
56
60
  native.once_cache_get_action_result_json,
57
61
  { action_digest: actionDigest },
@@ -60,6 +64,7 @@ class Cache {
60
64
  }
61
65
 
62
66
  async forgetAction(actionDigest) {
67
+ validateDigest(actionDigest, "actionDigest");
63
68
  return decodeRequest(
64
69
  native.once_cache_forget_action_json,
65
70
  { action_digest: actionDigest },
@@ -94,7 +99,12 @@ function decodeResponse(pointer) {
94
99
  if (pointer == null) {
95
100
  throw new OnceError("native Once function returned null");
96
101
  }
97
- const response = JSON.parse(pointer);
102
+ let response;
103
+ try {
104
+ response = JSON.parse(pointer);
105
+ } catch (error) {
106
+ throw new OnceError(`native Once response must be valid JSON: ${error.message}`);
107
+ }
98
108
  if (response.status === "ok") {
99
109
  return response.value;
100
110
  }
@@ -119,7 +129,13 @@ function actionResultFromNative(result) {
119
129
  };
120
130
  }
121
131
 
122
- function toBuffer(bytes) {
132
+ function validateDigest(value, name) {
133
+ if (typeof value !== "string" || !DIGEST_PATTERN.test(value)) {
134
+ throw new OnceError(`${name} must be a lowercase BLAKE3 hex digest`);
135
+ }
136
+ }
137
+
138
+ function toBuffer(bytes, name) {
123
139
  if (Buffer.isBuffer(bytes)) {
124
140
  return bytes;
125
141
  }
@@ -127,9 +143,9 @@ function toBuffer(bytes) {
127
143
  return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
128
144
  }
129
145
  if (typeof bytes === "string") {
130
- return Buffer.from(bytes);
146
+ return Buffer.from(bytes, "utf8");
131
147
  }
132
- throw new TypeError("bytes must be a Buffer, Uint8Array, or string");
148
+ throw new TypeError(`${name} must be a Buffer, Uint8Array, or string`);
133
149
  }
134
150
 
135
151
  function loadNative() {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "buildonce",
3
- "version": "0.1.0",
3
+ "version": "0.15.0",
4
4
  "description": "JavaScript SDK for Once cache access",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/tuist/once.git",
8
+ "url": "git+https://github.com/tuist/once.git",
9
9
  "directory": "packages/js"
10
10
  },
11
11
  "main": "lib/index.js",
Binary file
Binary file
Binary file