@cogover/sdk 0.6.0 → 0.8.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/dist/response.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ValidationError } from "./errors.js";
2
+ import { encodeBase64 } from "./internal/base64.js";
2
3
  const RESPONSE_KEY = "__cogoverHttpResponse";
3
4
  const HEADER_NAME = /^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,128}$/;
4
5
  const DENIED_HEADERS = new Set([
@@ -91,18 +92,3 @@ function normalizeHeaders(source, defaultContentType) {
91
92
  }
92
93
  return Object.freeze(normalized);
93
94
  }
94
- function encodeBase64(bytes) {
95
- const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
96
- let result = "";
97
- for (let index = 0; index < bytes.length; index += 3) {
98
- const first = bytes[index] ?? 0;
99
- const second = bytes[index + 1] ?? 0;
100
- const third = bytes[index + 2] ?? 0;
101
- const value = (first << 16) | (second << 8) | third;
102
- result += alphabet.charAt((value >>> 18) & 63)
103
- + alphabet.charAt((value >>> 12) & 63)
104
- + (index + 1 < bytes.length ? alphabet.charAt((value >>> 6) & 63) : "=")
105
- + (index + 2 < bytes.length ? alphabet.charAt(value & 63) : "=");
106
- }
107
- return result;
108
- }
package/dist/router.js CHANGED
@@ -59,6 +59,10 @@ export function createRouter() {
59
59
  response: context.response,
60
60
  state: context.state,
61
61
  locks: context.locks,
62
+ push: context.push,
63
+ jobs: context.jobs,
64
+ secrets: context.secrets,
65
+ crypto: context.crypto,
62
66
  });
63
67
  });
64
68
  },
package/dist/script.d.ts CHANGED
@@ -6,7 +6,11 @@ import type { SchemaApi } from "./schema.js";
6
6
  import type { EffectiveWorkspaceObjects } from "./workspace.js";
7
7
  import type { ProjectState } from "./state.js";
8
8
  import type { DistributedLocks } from "./locks.js";
9
+ import type { PushApi } from "./push.js";
9
10
  import type { InvocationContext } from "./invocation.js";
11
+ import type { JobsApi } from "./jobs.js";
12
+ import type { SecretsApi } from "./secrets.js";
13
+ import type { CryptoApi } from "./crypto.js";
10
14
  export interface ScriptContext<TInput, TSchema extends object = EffectiveWorkspaceObjects> {
11
15
  readonly input: TInput;
12
16
  readonly request: ScriptRequest<TInput>;
@@ -17,6 +21,10 @@ export interface ScriptContext<TInput, TSchema extends object = EffectiveWorkspa
17
21
  readonly response: ResponseApi;
18
22
  readonly state: ProjectState;
19
23
  readonly locks: DistributedLocks;
24
+ readonly push: PushApi<TSchema>;
25
+ readonly jobs: JobsApi;
26
+ readonly secrets: SecretsApi;
27
+ readonly crypto: CryptoApi;
20
28
  }
21
29
  export type ScriptHandler<TInput, TOutput, TSchema extends object = EffectiveWorkspaceObjects> = (context: ScriptContext<TInput, TSchema>) => TOutput | ScriptResponse<unknown> | null | Promise<TOutput | ScriptResponse<unknown> | null>;
22
30
  export type SandboxHandler = (inputJson: string) => Promise<string | null>;
package/dist/script.js CHANGED
@@ -7,8 +7,12 @@ import { SCRIPT_LOGGER } from "./logger.js";
7
7
  import { createSchemaApi } from "./schema.js";
8
8
  import { createStateApi } from "./state.js";
9
9
  import { createLocksApi } from "./locks.js";
10
+ import { createPushApi } from "./push.js";
10
11
  import { installCogoverFetch } from "./fetch.js";
11
12
  import { invocationContext } from "./internal/invocation.js";
13
+ import { createJobsApi } from "./internal/jobs-api.js";
14
+ import { createSecretsApi } from "./secrets.js";
15
+ import { crypto } from "./crypto.js";
12
16
  export function defineScript(handler) {
13
17
  if (typeof handler !== "function") {
14
18
  throw new ValidationError("defineScript() requires a handler function");
@@ -43,6 +47,10 @@ export function createSandboxHandler(handler) {
43
47
  response: createResponseApi(),
44
48
  state: createStateApi(),
45
49
  locks: createLocksApi(),
50
+ push: createPushApi(),
51
+ jobs: createJobsApi(),
52
+ secrets: createSecretsApi(),
53
+ crypto,
46
54
  });
47
55
  if (output === null) {
48
56
  return null;
@@ -0,0 +1,8 @@
1
+ /** Reads the secrets that an administrator stored for the project. */
2
+ export interface SecretsApi {
3
+ /**
4
+ * Returns the current value of a secret. Credentials that `fetch` sends on the project's behalf
5
+ * cannot be read; reference them with `fetch(url, { credential })` instead.
6
+ */
7
+ get(name: string): Promise<string>;
8
+ }
@@ -0,0 +1,17 @@
1
+ import { CogoverApiError } from "./errors.js";
2
+ import { callBridge } from "./internal/bridge.js";
3
+ import { requireSecretName } from "./internal/validation.js";
4
+ import { isRecord } from "./internal/values.js";
5
+ /** @internal */
6
+ export function createSecretsApi() {
7
+ return Object.freeze({
8
+ async get(name) {
9
+ const secretName = requireSecretName(name, "secrets.get() name");
10
+ const secret = await callBridge("secrets.get", { name: secretName });
11
+ if (!isRecord(secret) || typeof secret.value !== "string") {
12
+ throw new CogoverApiError("INVALID_BRIDGE_RESPONSE", `Secret '${secretName}' has no value`);
13
+ }
14
+ return secret.value;
15
+ },
16
+ });
17
+ }
package/dist/state.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { callBridge } from "./internal/bridge.js";
2
+ import { utf8ByteLength } from "./internal/utf8.js";
2
3
  import { ValidationError } from "./errors.js";
3
4
  const NAME = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
4
5
  const MAX_NAMESPACE_LENGTH = 128;
@@ -51,7 +52,7 @@ function validateValue(value) {
51
52
  }
52
53
  if (serialized === undefined)
53
54
  throw new ValidationError("state value must be JSON serializable");
54
- if (utf8Bytes(serialized) > MAX_VALUE_BYTES) {
55
+ if (utf8ByteLength(serialized) > MAX_VALUE_BYTES) {
55
56
  throw new ValidationError(`state value must not exceed ${MAX_VALUE_BYTES} UTF-8 bytes`);
56
57
  }
57
58
  }
@@ -71,21 +72,3 @@ export function optionalInteger(value, label, minimum, maximum) {
71
72
  }
72
73
  return value;
73
74
  }
74
- function utf8Bytes(value) {
75
- let bytes = 0;
76
- for (let index = 0; index < value.length; index++) {
77
- const code = value.charCodeAt(index);
78
- if (code < 0x80)
79
- bytes++;
80
- else if (code < 0x800)
81
- bytes += 2;
82
- else if (code >= 0xd800 && code <= 0xdbff && index + 1 < value.length
83
- && value.charCodeAt(index + 1) >= 0xdc00 && value.charCodeAt(index + 1) <= 0xdfff) {
84
- bytes += 4;
85
- index++;
86
- }
87
- else
88
- bytes += 3;
89
- }
90
- return bytes;
91
- }
package/dist/trigger.d.ts CHANGED
@@ -1,6 +1,10 @@
1
+ import type { CryptoApi } from "./crypto.js";
1
2
  import type { DataApi } from "./data.js";
3
+ import type { JobsApi } from "./jobs.js";
4
+ import type { SecretsApi } from "./secrets.js";
2
5
  import type { InvocationContext } from "./invocation.js";
3
6
  import type { ScriptLogger } from "./logger.js";
7
+ import type { PushApi } from "./push.js";
4
8
  import type { FilterOperator } from "./query.js";
5
9
  import type { CogoverRecordId, UpdateFields } from "./records.js";
6
10
  import type { SchemaApi } from "./schema.js";
@@ -114,6 +118,13 @@ export interface TriggerContext<TObject extends string = string, TOperation exte
114
118
  readonly data: DataApi<TSchema>;
115
119
  readonly schema: SchemaApi<TSchema>;
116
120
  readonly log: ScriptLogger;
121
+ /** Refused with `TRIGGER_READ_ONLY` in a before-change trigger. */
122
+ readonly push: PushApi<TSchema>;
123
+ /** Available in after-change triggers; a before-change trigger is refused with `PermissionDeniedError`. */
124
+ readonly jobs: JobsApi;
125
+ /** Available in after-change triggers; a before-change trigger is refused with `PermissionDeniedError`. */
126
+ readonly secrets: SecretsApi;
127
+ readonly crypto: CryptoApi;
117
128
  }
118
129
  export type TriggerHandler<TObject extends string = string, TOperation extends TriggerOperation = TriggerOperation, TSchema extends object = EffectiveWorkspaceObjects> = (context: TriggerContext<TObject, TOperation, TSchema>) => void | Promise<void>;
119
130
  /** Declares a record trigger. Add the result to the project's `triggers` export. */