@gnufoo/canaad 0.4.2 → 0.5.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/errors.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ /** Thrown when an operation is called before `initWasm()` completes. */
2
+ export declare class WasmNotInitializedError extends Error {
3
+ /** Discriminant for branded error handling — always `'WasmNotInitializedError'`. */
4
+ readonly kind: 'WasmNotInitializedError';
5
+ }
package/errors.js ADDED
@@ -0,0 +1,10 @@
1
+ /** Thrown when an operation is called before `initWasm()` completes. */
2
+ export class WasmNotInitializedError extends Error {
3
+ /** Discriminant for branded error handling — always `'WasmNotInitializedError'`. */
4
+ kind = 'WasmNotInitializedError';
5
+
6
+ constructor() {
7
+ super('WASM not initialized. Call initWasm() first.');
8
+ this.name = 'WasmNotInitializedError';
9
+ }
10
+ }
package/meta.js CHANGED
@@ -1,5 +1,11 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ // AAD spec: tenant field capped at 256 UTF-8 bytes.
4
+ const TENANT_MAX_BYTES = 256;
5
+ // AAD spec: resource field capped at 1024 UTF-8 bytes.
6
+ const RESOURCE_MAX_BYTES = 1024;
7
+ const encoder = new TextEncoder();
8
+
3
9
  const extensionSchema = z.object({
4
10
  key: z.string().regex(/^x_[a-z]+_[a-z_]+$/),
5
11
  value: z.union([z.string(), z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)]),
@@ -22,8 +28,8 @@ export const inputSchema = z.discriminatedUnion('action', [
22
28
  }),
23
29
  z.object({
24
30
  action: z.literal('build'),
25
- tenant: z.string().min(1).refine(s => new TextEncoder().encode(s).length <= 256, 'tenant exceeds 256 bytes'),
26
- resource: z.string().min(1).refine(s => new TextEncoder().encode(s).length <= 1024, 'resource exceeds 1024 bytes'),
31
+ tenant: z.string().min(1).refine(s => encoder.encode(s).length <= TENANT_MAX_BYTES, `tenant exceeds ${TENANT_MAX_BYTES} bytes`),
32
+ resource: z.string().min(1).refine(s => encoder.encode(s).length <= RESOURCE_MAX_BYTES, `resource exceeds ${RESOURCE_MAX_BYTES} bytes`),
27
33
  purpose: z.string().min(1),
28
34
  timestamp: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).optional(),
29
35
  extensions: z.array(extensionSchema).optional(),
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@gnufoo/canaad",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
- "main": "canaad_wasm.js",
6
5
  "types": "canaad_wasm.d.ts",
7
6
  "exports": {
8
7
  ".": {
package/tool.d.ts CHANGED
@@ -10,3 +10,5 @@ export declare const toolDefinition: typeof meta & {
10
10
  /** Dispatches an AAD action to WASM. Requires prior `initWasm()` call. */
11
11
  execute(input: CanaadInput): Promise<CanaadOutput>;
12
12
  };
13
+
14
+ export { WasmNotInitializedError } from './errors.js';
package/tool.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import { meta, inputSchema } from './meta.js';
2
2
  import { initWasm, isInitialized } from './init.js';
3
3
  import { canonicalize, canonicalizeString, hash, validate, AadBuilder } from './canaad_wasm.js';
4
-
4
+ import { WasmNotInitializedError } from './errors.js';
5
5
  const bytesToHex = (bytes) => Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
6
6
  const bytesToBase64 = (bytes) => globalThis.btoa(String.fromCharCode(...bytes));
7
-
8
7
  /**
9
8
  * Main tool export. Merges AAD metadata with WASM lifecycle methods and the action dispatcher.
10
9
  */
@@ -18,11 +17,9 @@ export const toolDefinition = {
18
17
  */
19
18
  async execute(rawInput) {
20
19
  if (!isInitialized()) {
21
- throw new Error('WASM not initialized. Call initWasm() first.');
20
+ throw new WasmNotInitializedError();
22
21
  }
23
-
24
22
  const input = inputSchema.parse(rawInput);
25
-
26
23
  switch (input.action) {
27
24
  case 'canonicalize': {
28
25
  if (input.outputFormat === 'bytes') {
@@ -50,3 +47,4 @@ export const toolDefinition = {
50
47
  }
51
48
  },
52
49
  };
50
+ export { WasmNotInitializedError } from './errors.js';