@cclabsnz/sf-core 0.12.0 → 0.14.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 (38) hide show
  1. package/CHANGELOG.md +58 -1
  2. package/README.md +3 -2
  3. package/lib/cloudflare/CloudflareClient.d.ts +53 -0
  4. package/lib/cloudflare/CloudflareClient.js +10 -0
  5. package/lib/cloudflare/FetchCloudflareClient.d.ts +6 -0
  6. package/lib/cloudflare/FetchCloudflareClient.js +152 -0
  7. package/lib/cloudflare/errors.d.ts +16 -0
  8. package/lib/cloudflare/errors.js +25 -0
  9. package/lib/cloudflare/index.d.ts +5 -0
  10. package/lib/cloudflare/index.js +4 -0
  11. package/lib/cloudflare/testing/MemoryCloudflareClient.d.ts +43 -0
  12. package/lib/cloudflare/testing/MemoryCloudflareClient.js +77 -0
  13. package/lib/cloudflare.d.ts +2 -0
  14. package/lib/cloudflare.js +4 -0
  15. package/lib/cloudflareTesting.d.ts +3 -0
  16. package/lib/cloudflareTesting.js +3 -0
  17. package/lib/crypto/bundleCrypto.d.ts +30 -1
  18. package/lib/crypto/bundleCrypto.js +269 -9
  19. package/lib/graph/validate.js +1 -1
  20. package/lib/index.d.ts +1 -1
  21. package/lib/index.js +1 -1
  22. package/lib/storage/ObjectStore.d.ts +26 -0
  23. package/lib/storage/ObjectStore.js +2 -0
  24. package/lib/storage/S3ObjectStoreImpl.d.ts +20 -0
  25. package/lib/storage/S3ObjectStoreImpl.js +132 -0
  26. package/lib/storage/errors.d.ts +11 -0
  27. package/lib/storage/errors.js +19 -0
  28. package/lib/storage/index.d.ts +7 -0
  29. package/lib/storage/index.js +4 -0
  30. package/lib/storage/s3Location.d.ts +29 -0
  31. package/lib/storage/s3Location.js +63 -0
  32. package/lib/storage/testing/MemoryObjectStore.d.ts +32 -0
  33. package/lib/storage/testing/MemoryObjectStore.js +79 -0
  34. package/lib/storage.d.ts +2 -0
  35. package/lib/storage.js +4 -0
  36. package/lib/storageTesting.d.ts +2 -0
  37. package/lib/storageTesting.js +3 -0
  38. package/package.json +23 -2
@@ -0,0 +1,63 @@
1
+ export function isS3Uri(s) {
2
+ return s.startsWith('s3://');
3
+ }
4
+ export function parseS3Uri(uri) {
5
+ if (!isS3Uri(uri))
6
+ throw new Error(`Not an s3:// URI: ${uri}`);
7
+ const rest = uri.slice('s3://'.length);
8
+ const slash = rest.indexOf('/');
9
+ const bucket = slash === -1 ? rest : rest.slice(0, slash);
10
+ if (!bucket)
11
+ throw new Error(`s3:// URI has no bucket: ${uri}`);
12
+ // Split and drop empty segments rather than trim with a /^\/+|\/+$/ regex: that backtracks
13
+ // quadratically on a long run of '/', and a prefix is caller input.
14
+ const prefix = slash === -1 ? '' : rest.slice(slash + 1).split('/').filter(Boolean).join('/');
15
+ return { bucket, prefix };
16
+ }
17
+ const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]', '::1']);
18
+ /**
19
+ * Resolve the endpoint from the standard AWS environment variables.
20
+ * `AWS_ENDPOINT_URL_S3` beats `AWS_ENDPOINT_URL`; `AWS_REGION` beats `AWS_DEFAULT_REGION`.
21
+ * A custom endpoint defaults the region to `auto` (Cloudflare R2); with none, a region is required.
22
+ */
23
+ export function resolveS3Endpoint(env) {
24
+ const region = env.AWS_REGION || env.AWS_DEFAULT_REGION || undefined;
25
+ const custom = env.AWS_ENDPOINT_URL_S3 || env.AWS_ENDPOINT_URL || undefined;
26
+ if (!custom) {
27
+ if (!region) {
28
+ throw new Error('Set AWS_REGION (for AWS S3) or AWS_ENDPOINT_URL_S3 (for R2 or another S3-compatible service).');
29
+ }
30
+ if (!/^[a-z0-9-]+$/.test(region))
31
+ throw new Error('AWS_REGION is not a valid region name.');
32
+ return { endpoint: `https://s3.${region}.amazonaws.com`, region, custom: false };
33
+ }
34
+ let url;
35
+ try {
36
+ url = new URL(custom);
37
+ }
38
+ catch {
39
+ throw new Error('The S3 endpoint (AWS_ENDPOINT_URL_S3 or AWS_ENDPOINT_URL) is not a valid URL.');
40
+ }
41
+ if (url.username || url.password)
42
+ throw new Error('The S3 endpoint must not contain credentials.');
43
+ if (url.protocol !== 'https:' && !(url.protocol === 'http:' && LOOPBACK.has(url.hostname))) {
44
+ throw new Error('The S3 endpoint (AWS_ENDPOINT_URL_S3 or AWS_ENDPOINT_URL) must use https; http is allowed only for localhost.');
45
+ }
46
+ return { endpoint: custom, region: region ?? 'auto', custom: true };
47
+ }
48
+ /**
49
+ * The host the S3 client actually contacts for `bucket`, for naming network destinations truthfully.
50
+ * A custom endpoint (R2, MinIO...) is used path-style, so the host is the endpoint's own host.
51
+ * AWS S3 is used virtual-hosted style, so the bucket is part of the host:
52
+ * `<bucket>.s3.<region>.amazonaws.com`. The SDK falls back to path style, and so to
53
+ * `s3.<region>.amazonaws.com`, for a bucket name that cannot be a single DNS label over https
54
+ * (one with a dot, upper case or an underscore), and this mirrors that rule.
55
+ */
56
+ export function s3Host(bucket, ep) {
57
+ if (ep.custom)
58
+ return new URL(ep.endpoint).host;
59
+ return VIRTUAL_HOSTABLE.test(bucket) ? `${bucket}.s3.${ep.region}.amazonaws.com` : `s3.${ep.region}.amazonaws.com`;
60
+ }
61
+ /** The AWS SDK's isVirtualHostableS3Bucket without sub-domains: 3-63 lower-case letters, digits and inner hyphens. */
62
+ const VIRTUAL_HOSTABLE = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
63
+ //# sourceMappingURL=s3Location.js.map
@@ -0,0 +1,32 @@
1
+ import type { ObjectStore } from '../ObjectStore.js';
2
+ /** In-memory ObjectStore for tests. Keys list in lexical order; the token is the last key returned. */
3
+ export declare class MemoryObjectStore implements ObjectStore {
4
+ readonly objects: Map<string, Buffer<ArrayBufferLike>>;
5
+ /** Return an Error to make the next put/putIfAbsent of that key fail with it. */
6
+ failNextPut?: (key: string) => Error | undefined;
7
+ private readonly pageSize;
8
+ private readonly bucketExists;
9
+ constructor(opts?: {
10
+ pageSize?: number;
11
+ bucketExists?: boolean;
12
+ });
13
+ private check;
14
+ private maybeFail;
15
+ get(key: string): Promise<Buffer | null>;
16
+ put(key: string, bytes: Buffer, _contentType: string): Promise<void>;
17
+ putIfAbsent(key: string, bytes: Buffer, _contentType: string): Promise<boolean>;
18
+ list(args: {
19
+ prefix: string;
20
+ delimiter?: string;
21
+ maxKeys?: number;
22
+ token?: string;
23
+ }): Promise<{
24
+ keys: Array<{
25
+ key: string;
26
+ size: number;
27
+ }>;
28
+ prefixes: string[];
29
+ next?: string;
30
+ }>;
31
+ }
32
+ //# sourceMappingURL=MemoryObjectStore.d.ts.map
@@ -0,0 +1,79 @@
1
+ import { BucketMissingError } from '../errors.js';
2
+ /** In-memory ObjectStore for tests. Keys list in lexical order; the token is the last key returned. */
3
+ export class MemoryObjectStore {
4
+ objects = new Map();
5
+ /** Return an Error to make the next put/putIfAbsent of that key fail with it. */
6
+ failNextPut;
7
+ pageSize;
8
+ bucketExists;
9
+ constructor(opts = {}) {
10
+ this.pageSize = opts.pageSize ?? 1000;
11
+ this.bucketExists = opts.bucketExists ?? true;
12
+ }
13
+ check() {
14
+ if (!this.bucketExists)
15
+ throw new BucketMissingError('memory');
16
+ }
17
+ maybeFail(key) {
18
+ const err = this.failNextPut?.(key);
19
+ if (err) {
20
+ this.failNextPut = undefined;
21
+ throw err;
22
+ }
23
+ }
24
+ async get(key) {
25
+ this.check();
26
+ const b = this.objects.get(key);
27
+ return b ? Buffer.from(b) : null;
28
+ }
29
+ async put(key, bytes, _contentType) {
30
+ this.check();
31
+ this.maybeFail(key);
32
+ this.objects.set(key, Buffer.from(bytes));
33
+ }
34
+ async putIfAbsent(key, bytes, _contentType) {
35
+ this.check();
36
+ this.maybeFail(key);
37
+ // No await between the check and the set, so concurrent callers cannot both win.
38
+ if (this.objects.has(key))
39
+ return false;
40
+ this.objects.set(key, Buffer.from(bytes));
41
+ return true;
42
+ }
43
+ async list(args) {
44
+ this.check();
45
+ const limit = Math.min(args.maxKeys ?? this.pageSize, this.pageSize);
46
+ const keys = [];
47
+ const prefixes = [];
48
+ let count = 0;
49
+ let last;
50
+ let more = false;
51
+ for (const key of [...this.objects.keys()].filter((k) => k.startsWith(args.prefix)).sort()) {
52
+ if (args.token !== undefined && key <= args.token)
53
+ continue;
54
+ let common;
55
+ if (args.delimiter) {
56
+ const i = key.indexOf(args.delimiter, args.prefix.length);
57
+ if (i !== -1)
58
+ common = key.slice(0, i + args.delimiter.length);
59
+ }
60
+ // Further keys under an already-listed common prefix add nothing to the page.
61
+ if (common !== undefined && prefixes.includes(common)) {
62
+ continue;
63
+ }
64
+ if (count >= limit) {
65
+ more = true;
66
+ break;
67
+ }
68
+ if (common !== undefined)
69
+ prefixes.push(common);
70
+ else
71
+ keys.push({ key, size: this.objects.get(key).length });
72
+ count++;
73
+ // A token ending on a common prefix must skip the rest of that prefix on the next page.
74
+ last = common !== undefined ? common + '\uffff' : key;
75
+ }
76
+ return { keys, prefixes, next: more ? last : undefined };
77
+ }
78
+ }
79
+ //# sourceMappingURL=MemoryObjectStore.js.map
@@ -0,0 +1,2 @@
1
+ export * from './storage/index.js';
2
+ //# sourceMappingURL=storage.d.ts.map
package/lib/storage.js ADDED
@@ -0,0 +1,4 @@
1
+ // The ./storage entry point: an S3-compatible ObjectStore. @aws-sdk/client-s3 is an optional peer,
2
+ // loaded only when createS3ObjectStore runs.
3
+ export * from './storage/index.js';
4
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1,2 @@
1
+ export { MemoryObjectStore } from './storage/testing/MemoryObjectStore.js';
2
+ //# sourceMappingURL=storageTesting.d.ts.map
@@ -0,0 +1,3 @@
1
+ // The ./storage/testing entry point: an in-memory ObjectStore for tests.
2
+ export { MemoryObjectStore } from './storage/testing/MemoryObjectStore.js';
3
+ //# sourceMappingURL=storageTesting.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cclabsnz/sf-core",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Shared platform layer for CloudCounsel Salesforce sf plugins — API clients, org context, event log pull, report shell, and versioned IR schemas.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -15,6 +15,22 @@
15
15
  "types": "./lib/fetch.d.ts",
16
16
  "default": "./lib/fetch.js"
17
17
  },
18
+ "./cloudflare": {
19
+ "types": "./lib/cloudflare.d.ts",
20
+ "default": "./lib/cloudflare.js"
21
+ },
22
+ "./cloudflare/testing": {
23
+ "types": "./lib/cloudflareTesting.d.ts",
24
+ "default": "./lib/cloudflareTesting.js"
25
+ },
26
+ "./storage": {
27
+ "types": "./lib/storage.d.ts",
28
+ "default": "./lib/storage.js"
29
+ },
30
+ "./storage/testing": {
31
+ "types": "./lib/storageTesting.d.ts",
32
+ "default": "./lib/storageTesting.js"
33
+ },
18
34
  "./testing": {
19
35
  "types": "./lib/testing/invariants.d.ts",
20
36
  "default": "./lib/testing/invariants.js"
@@ -31,6 +47,7 @@
31
47
  "prepare": "git config core.hooksPath .githooks 2>/dev/null || true"
32
48
  },
33
49
  "devDependencies": {
50
+ "@aws-sdk/client-s3": "3.1168.0",
34
51
  "@jest/globals": "^29.7.0",
35
52
  "@jsforce/jsforce-node": "3.10.19",
36
53
  "@salesforce/core": "^9",
@@ -85,11 +102,15 @@
85
102
  }
86
103
  },
87
104
  "peerDependencies": {
88
- "@salesforce/core": "^8 || ^9"
105
+ "@salesforce/core": "^8 || ^9",
106
+ "@aws-sdk/client-s3": ">=3.1168.0 <4"
89
107
  },
90
108
  "peerDependenciesMeta": {
91
109
  "@salesforce/core": {
92
110
  "optional": true
111
+ },
112
+ "@aws-sdk/client-s3": {
113
+ "optional": true
93
114
  }
94
115
  },
95
116
  "dependencies": {