@di-framework/cli-plugin-platform 0.0.1 → 6.0.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.
Files changed (169) hide show
  1. package/README.md +408 -0
  2. package/dist/actor-protocol.d.ts +4 -0
  3. package/dist/actor-protocol.js +162 -0
  4. package/dist/actors.d.ts +31 -0
  5. package/dist/actors.js +296 -0
  6. package/dist/args.d.ts +15 -0
  7. package/dist/args.js +66 -0
  8. package/dist/assets/http-adapter.js +876 -0
  9. package/dist/assets/platform/.gitignore.tmpl +6 -0
  10. package/dist/assets/platform/Pulumi.yaml.tmpl +6 -0
  11. package/dist/assets/platform/README.md +208 -0
  12. package/dist/assets/platform/index.ts.tmpl +2 -0
  13. package/dist/assets/platform/package.json.tmpl +18 -0
  14. package/dist/assets/platform/tsconfig.json +11 -0
  15. package/dist/assets/queue-adapter.js +60 -0
  16. package/dist/assets/sqlite/BUILD-INFO.txt +13 -0
  17. package/dist/assets/sqlite/SHA256SUMS +2 -0
  18. package/dist/assets/sqlite/di-framework-sqlite.wasm +0 -0
  19. package/dist/assets/sqlite/di-framework-sqlite.wit +337 -0
  20. package/dist/assets/wit/deps/LICENSE.md +8 -0
  21. package/dist/assets/wit/deps/di-framework-queues/package.wit +18 -0
  22. package/dist/assets/wit/deps/di-framework-sqlite/package.wit +211 -0
  23. package/dist/assets/wit/deps/wasi-cli/package.wit +70 -0
  24. package/dist/assets/wit/deps/wasi-clocks/package.wit +42 -0
  25. package/dist/assets/wit/deps/wasi-config/package.wit +15 -0
  26. package/dist/assets/wit/deps/wasi-filesystem/package.wit +184 -0
  27. package/dist/assets/wit/deps/wasi-http/package.wit +170 -0
  28. package/dist/assets/wit/deps/wasi-random/package.wit +17 -0
  29. package/dist/assets/wit/deps/wasi-sockets/package.wit +105 -0
  30. package/dist/assets/wit/deps/wasi-tls/package.wit +26 -0
  31. package/dist/assets/wit/deps/wasmcloud-blobstore/package.wit +153 -0
  32. package/dist/assets/wit/deps/wasmcloud-keyvalue/package.wit +370 -0
  33. package/dist/assets/wit/deps/wasmcloud-messaging/package.wit +125 -0
  34. package/dist/assets/wit/deps/wasmcloud-postgres/package.wit +435 -0
  35. package/dist/assets/wit/deps/wasmcloud-secrets/package.wit +80 -0
  36. package/dist/async-transform.d.ts +3 -0
  37. package/dist/async-transform.js +60 -0
  38. package/dist/bindings.d.ts +34 -0
  39. package/dist/bindings.js +307 -0
  40. package/dist/build.d.ts +29 -0
  41. package/dist/build.js +325 -0
  42. package/dist/command.d.ts +3 -0
  43. package/dist/command.js +141 -0
  44. package/dist/control/auth.d.ts +18 -0
  45. package/dist/control/auth.js +89 -0
  46. package/dist/control/cron.d.ts +7 -0
  47. package/dist/control/cron.js +86 -0
  48. package/dist/control/index.d.ts +4 -0
  49. package/dist/control/index.js +4 -0
  50. package/dist/control/network.d.ts +12 -0
  51. package/dist/control/network.js +49 -0
  52. package/dist/control/queues.d.ts +15 -0
  53. package/dist/control/queues.js +77 -0
  54. package/dist/cron.d.ts +26 -0
  55. package/dist/cron.js +239 -0
  56. package/dist/deploy.d.ts +29 -0
  57. package/dist/deploy.js +57 -0
  58. package/dist/deps.d.ts +96 -0
  59. package/dist/deps.js +303 -0
  60. package/dist/destroy.d.ts +3 -0
  61. package/dist/destroy.js +24 -0
  62. package/dist/dev-runner.d.ts +24 -0
  63. package/dist/dev-runner.js +83 -0
  64. package/dist/dev.d.ts +8 -0
  65. package/dist/dev.js +76 -0
  66. package/dist/discovery.d.ts +15 -0
  67. package/dist/discovery.js +182 -0
  68. package/dist/doctor.d.ts +8 -0
  69. package/dist/doctor.js +82 -0
  70. package/dist/guests.d.ts +9 -0
  71. package/dist/guests.js +56 -0
  72. package/dist/host-interface.d.ts +29 -0
  73. package/dist/host-interface.js +142 -0
  74. package/dist/index.d.ts +33 -0
  75. package/dist/index.js +39 -0
  76. package/dist/kubernetes.d.ts +9 -0
  77. package/dist/kubernetes.js +17 -0
  78. package/dist/managed-bindings.d.ts +9 -0
  79. package/dist/managed-bindings.js +150 -0
  80. package/dist/manifest.d.ts +36 -0
  81. package/dist/manifest.js +257 -0
  82. package/dist/namespace.d.ts +2 -0
  83. package/dist/namespace.js +4 -0
  84. package/dist/node-compat/async-hooks.d.ts +29 -0
  85. package/dist/node-compat/async-hooks.js +80 -0
  86. package/dist/node-compat/bootstrap.d.ts +1 -0
  87. package/dist/node-compat/bootstrap.js +9 -0
  88. package/dist/node-compat/bytes.d.ts +7 -0
  89. package/dist/node-compat/bytes.js +41 -0
  90. package/dist/node-compat/crypto-hash.d.ts +27 -0
  91. package/dist/node-compat/crypto-hash.js +108 -0
  92. package/dist/node-compat/crypto-subtle.d.ts +59 -0
  93. package/dist/node-compat/crypto-subtle.js +288 -0
  94. package/dist/node-compat/crypto.d.ts +136 -0
  95. package/dist/node-compat/crypto.js +142 -0
  96. package/dist/node-compat/dgram.d.ts +49 -0
  97. package/dist/node-compat/dgram.js +197 -0
  98. package/dist/node-compat/env.d.ts +6 -0
  99. package/dist/node-compat/env.js +88 -0
  100. package/dist/node-compat/fetch-runtime.d.ts +80 -0
  101. package/dist/node-compat/fetch-runtime.js +314 -0
  102. package/dist/node-compat/fs.d.ts +84 -0
  103. package/dist/node-compat/fs.js +285 -0
  104. package/dist/node-compat/http-parser.d.ts +40 -0
  105. package/dist/node-compat/http-parser.js +185 -0
  106. package/dist/node-compat/http.d.ts +130 -0
  107. package/dist/node-compat/http.js +688 -0
  108. package/dist/node-compat/https.d.ts +28 -0
  109. package/dist/node-compat/https.js +88 -0
  110. package/dist/node-compat/module.d.ts +11 -0
  111. package/dist/node-compat/module.js +35 -0
  112. package/dist/node-compat/net.d.ts +119 -0
  113. package/dist/node-compat/net.js +441 -0
  114. package/dist/node-compat/process.d.ts +25 -0
  115. package/dist/node-compat/process.js +63 -0
  116. package/dist/node-compat/runtime-application.d.ts +4 -0
  117. package/dist/node-compat/runtime-application.js +24 -0
  118. package/dist/node-compat/seed-virtual.d.ts +6 -0
  119. package/dist/node-compat/seed-virtual.js +6 -0
  120. package/dist/node-compat/seed.d.ts +20 -0
  121. package/dist/node-compat/seed.js +106 -0
  122. package/dist/node-compat/socket-address.d.ts +35 -0
  123. package/dist/node-compat/socket-address.js +162 -0
  124. package/dist/node-compat/timers.d.ts +32 -0
  125. package/dist/node-compat/timers.js +96 -0
  126. package/dist/node-compat/tls.d.ts +57 -0
  127. package/dist/node-compat/tls.js +290 -0
  128. package/dist/node-compat/wasi-random.d.ts +2 -0
  129. package/dist/node-compat/wasi-random.js +25 -0
  130. package/dist/node-compat/wasi-sockets.d.ts +47 -0
  131. package/dist/node-compat/wasi-sockets.js +134 -0
  132. package/dist/oci.d.ts +9 -0
  133. package/dist/oci.js +9 -0
  134. package/dist/paths.d.ts +7 -0
  135. package/dist/paths.js +13 -0
  136. package/dist/platform-init.d.ts +13 -0
  137. package/dist/platform-init.js +265 -0
  138. package/dist/platform.d.ts +22 -0
  139. package/dist/platform.js +170 -0
  140. package/dist/project.d.ts +39 -0
  141. package/dist/project.js +130 -0
  142. package/dist/publish.d.ts +15 -0
  143. package/dist/publish.js +57 -0
  144. package/dist/pulumi.d.ts +4 -0
  145. package/dist/pulumi.js +27 -0
  146. package/dist/queues-module.d.ts +4 -0
  147. package/dist/queues-module.js +89 -0
  148. package/dist/queues.d.ts +16 -0
  149. package/dist/queues.js +167 -0
  150. package/dist/registry.d.ts +16 -0
  151. package/dist/registry.js +27 -0
  152. package/dist/service.d.ts +162 -0
  153. package/dist/service.js +678 -0
  154. package/dist/support.d.ts +14 -0
  155. package/dist/support.js +37 -0
  156. package/dist/target.d.ts +18 -0
  157. package/dist/target.js +45 -0
  158. package/dist/toml.d.ts +6 -0
  159. package/dist/toml.js +245 -0
  160. package/dist/wash-dev.d.ts +18 -0
  161. package/dist/wash-dev.js +80 -0
  162. package/dist/wit.d.ts +76 -0
  163. package/dist/wit.js +248 -0
  164. package/dist/workload-members.d.ts +12 -0
  165. package/dist/workload-members.js +136 -0
  166. package/dist/workload.d.ts +45 -0
  167. package/dist/workload.js +457 -0
  168. package/package.json +55 -8
  169. package/src/index.ts +0 -2
@@ -0,0 +1,108 @@
1
+ import { hmac as nobleHmac } from '@noble/hashes/hmac.js';
2
+ import { md5, sha1 } from '@noble/hashes/legacy.js';
3
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
4
+ import { toBytes, toNodeBuffer } from './bytes.js';
5
+ const HASHES = {
6
+ md5: md5,
7
+ sha1: sha1,
8
+ sha256: sha256,
9
+ sha384: sha384,
10
+ sha512: sha512,
11
+ };
12
+ export function normalizeHashName(algorithm) {
13
+ return algorithm.toLowerCase().replace(/-/g, '');
14
+ }
15
+ export function hashFunction(algorithm) {
16
+ const name = normalizeHashName(algorithm);
17
+ const hash = HASHES[name];
18
+ if (hash === undefined) {
19
+ const error = new Error(`Digest method not supported: ${algorithm}`);
20
+ error.code = 'ERR_CRYPTO_INVALID_DIGEST';
21
+ throw error;
22
+ }
23
+ return hash;
24
+ }
25
+ function digestWithEncoding(bytes, encoding) {
26
+ const buffer = toNodeBuffer(bytes);
27
+ if (encoding === undefined || encoding === 'buffer')
28
+ return buffer;
29
+ if (typeof Buffer !== 'undefined' && Buffer.isBuffer(buffer)) {
30
+ return buffer.toString(encoding);
31
+ }
32
+ if (encoding === 'hex')
33
+ return hexEncode(bytes);
34
+ if (encoding === 'base64' || encoding === 'base64url' || encoding === 'latin1') {
35
+ return Buffer.from(bytes).toString(encoding);
36
+ }
37
+ return buffer;
38
+ }
39
+ function hexEncode(bytes) {
40
+ let out = '';
41
+ for (const byte of bytes)
42
+ out += byte.toString(16).padStart(2, '0');
43
+ return out;
44
+ }
45
+ function encodeInput(data, encoding) {
46
+ if (typeof data === 'string') {
47
+ if (encoding === 'hex' && typeof Buffer !== 'undefined')
48
+ return Buffer.from(data, 'hex');
49
+ if ((encoding === 'base64' || encoding === 'base64url') && typeof Buffer !== 'undefined') {
50
+ return Buffer.from(data, encoding);
51
+ }
52
+ return new TextEncoder().encode(data);
53
+ }
54
+ return toBytes(data);
55
+ }
56
+ export class Hash {
57
+ algorithm;
58
+ state;
59
+ finalized = false;
60
+ constructor(algorithm, state) {
61
+ this.algorithm = algorithm;
62
+ this.state = state ?? hashFunction(algorithm).create();
63
+ }
64
+ update(data, encoding) {
65
+ if (this.finalized) {
66
+ const error = new Error('Digest already called');
67
+ error.code = 'ERR_CRYPTO_HASH_FINALIZED';
68
+ throw error;
69
+ }
70
+ this.state.update(encodeInput(data, encoding));
71
+ return this;
72
+ }
73
+ digest(encoding) {
74
+ if (this.finalized) {
75
+ const error = new Error('Digest already called');
76
+ error.code = 'ERR_CRYPTO_HASH_FINALIZED';
77
+ throw error;
78
+ }
79
+ this.finalized = true;
80
+ return digestWithEncoding(this.state.digest(), encoding);
81
+ }
82
+ copy() {
83
+ if (this.finalized) {
84
+ const error = new Error('Digest already called');
85
+ error.code = 'ERR_CRYPTO_HASH_FINALIZED';
86
+ throw error;
87
+ }
88
+ return new Hash(this.algorithm, this.state.clone());
89
+ }
90
+ }
91
+ export class Hmac extends Hash {
92
+ constructor(algorithm, key) {
93
+ const hash = hashFunction(algorithm);
94
+ super(algorithm, nobleHmac.create(hash, encodeInput(key)));
95
+ }
96
+ }
97
+ export function createHash(algorithm) {
98
+ return new Hash(algorithm);
99
+ }
100
+ export function createHmac(algorithm, key) {
101
+ return new Hmac(algorithm, key);
102
+ }
103
+ export function digestSync(algorithm, data) {
104
+ const hash = hashFunction(algorithm);
105
+ const state = hash.create();
106
+ state.update(data);
107
+ return state.digest();
108
+ }
@@ -0,0 +1,59 @@
1
+ type AlgorithmIdentifier = string | {
2
+ name: string;
3
+ [parameter: string]: unknown;
4
+ };
5
+ type BufferSource = ArrayBufferView | ArrayBuffer;
6
+ type JsonWebKey = Record<string, unknown>;
7
+ export type GuestCryptoKey = {
8
+ type: 'secret' | 'private' | 'public';
9
+ extractable: boolean;
10
+ algorithm: {
11
+ name: string;
12
+ [parameter: string]: unknown;
13
+ };
14
+ usages: string[];
15
+ kind: 'hmac' | 'hkdf' | 'aes-gcm' | 'ecdh-private' | 'ecdh-public';
16
+ bytes: Uint8Array;
17
+ };
18
+ export declare function algorithmName(algorithm: AlgorithmIdentifier): string;
19
+ export declare function getRandomValues<T extends ArrayBufferView>(array: T): T;
20
+ export declare function randomUUID(): string;
21
+ declare function digest(algorithm: AlgorithmIdentifier, data: BufferSource): Promise<ArrayBuffer>;
22
+ declare function importKey(format: string, keyData: BufferSource | JsonWebKey, algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: readonly string[]): Promise<GuestCryptoKey>;
23
+ declare function exportKey(format: string, key: GuestCryptoKey): Promise<ArrayBuffer>;
24
+ declare function generateKey(algorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: readonly string[]): Promise<GuestCryptoKey | {
25
+ publicKey: GuestCryptoKey;
26
+ privateKey: GuestCryptoKey;
27
+ }>;
28
+ declare function deriveBits(algorithm: AlgorithmIdentifier, baseKey: GuestCryptoKey, length: number): Promise<ArrayBuffer>;
29
+ declare function sign(algorithm: AlgorithmIdentifier, key: GuestCryptoKey, data: BufferSource): Promise<ArrayBuffer>;
30
+ declare function verify(algorithm: AlgorithmIdentifier, key: GuestCryptoKey, signature: BufferSource, data: BufferSource): Promise<boolean>;
31
+ declare function encrypt(algorithm: AlgorithmIdentifier, key: GuestCryptoKey, data: BufferSource): Promise<ArrayBuffer>;
32
+ declare function decrypt(algorithm: AlgorithmIdentifier, key: GuestCryptoKey, data: BufferSource): Promise<ArrayBuffer>;
33
+ export declare const subtle: {
34
+ digest: typeof digest;
35
+ importKey: typeof importKey;
36
+ exportKey: typeof exportKey;
37
+ generateKey: typeof generateKey;
38
+ deriveBits: typeof deriveBits;
39
+ sign: typeof sign;
40
+ verify: typeof verify;
41
+ encrypt: typeof encrypt;
42
+ decrypt: typeof decrypt;
43
+ };
44
+ export declare const webcrypto: {
45
+ getRandomValues: typeof getRandomValues;
46
+ randomUUID: typeof randomUUID;
47
+ subtle: {
48
+ digest: typeof digest;
49
+ importKey: typeof importKey;
50
+ exportKey: typeof exportKey;
51
+ generateKey: typeof generateKey;
52
+ deriveBits: typeof deriveBits;
53
+ sign: typeof sign;
54
+ verify: typeof verify;
55
+ encrypt: typeof encrypt;
56
+ decrypt: typeof decrypt;
57
+ };
58
+ };
59
+ export {};
@@ -0,0 +1,288 @@
1
+ import { gcm } from '@noble/ciphers/aes.js';
2
+ import { p256 } from '@noble/curves/nist.js';
3
+ import { hkdf } from '@noble/hashes/hkdf.js';
4
+ import { hmac } from '@noble/hashes/hmac.js';
5
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
6
+ import { toArrayBuffer, toBytes } from './bytes.js';
7
+ import { digestSync, hashFunction, normalizeHashName } from './crypto-hash.js';
8
+ import { getRandomBytes } from './wasi-random.js';
9
+ const HASH_OUTPUT = {
10
+ sha256: { noble: sha256, bits: 256 },
11
+ sha384: { noble: sha384, bits: 384 },
12
+ sha512: { noble: sha512, bits: 512 },
13
+ };
14
+ function operationError(message) {
15
+ const error = new Error(message);
16
+ error.name = 'OperationError';
17
+ return error;
18
+ }
19
+ function notSupported(message) {
20
+ const error = new Error(message);
21
+ error.name = 'NotSupportedError';
22
+ return error;
23
+ }
24
+ function dataError(message) {
25
+ const error = new Error(message);
26
+ error.name = 'DataError';
27
+ return error;
28
+ }
29
+ export function algorithmName(algorithm) {
30
+ if (typeof algorithm === 'string')
31
+ return algorithm.toUpperCase();
32
+ if (algorithm !== null && typeof algorithm === 'object' && typeof algorithm.name === 'string') {
33
+ return algorithm.name.toUpperCase();
34
+ }
35
+ throw notSupported('Unrecognized algorithm');
36
+ }
37
+ function hashFromAlgorithm(algorithm) {
38
+ if (typeof algorithm === 'string')
39
+ return normalizeHashName(algorithm);
40
+ if (algorithm !== null && typeof algorithm === 'object') {
41
+ const record = algorithm;
42
+ if (record.hash !== undefined)
43
+ return hashFromAlgorithm(record.hash);
44
+ if (typeof record.name === 'string')
45
+ return normalizeHashName(record.name);
46
+ }
47
+ throw notSupported('Unrecognized hash algorithm');
48
+ }
49
+ function asKey(key) {
50
+ if (key !== null && typeof key === 'object' && 'kind' in key && 'bytes' in key) {
51
+ return key;
52
+ }
53
+ throw dataError('Invalid CryptoKey');
54
+ }
55
+ function algorithmParams(algorithm) {
56
+ return typeof algorithm === 'string' ? { name: algorithm } : { ...algorithm };
57
+ }
58
+ export function getRandomValues(array) {
59
+ const view = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
60
+ const random = getRandomBytes(view.length);
61
+ view.set(random);
62
+ return array;
63
+ }
64
+ export function randomUUID() {
65
+ const bytes = getRandomValues(new Uint8Array(16));
66
+ bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40;
67
+ bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
68
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('');
69
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
70
+ }
71
+ async function digest(algorithm, data) {
72
+ const name = algorithmName(algorithm);
73
+ if (name !== 'SHA-256' && name !== 'SHA-384' && name !== 'SHA-512' && name !== 'SHA-1') {
74
+ throw notSupported(`subtle.digest does not support ${name}`);
75
+ }
76
+ return toArrayBuffer(digestSync(name, toBytes(data)));
77
+ }
78
+ async function importKey(format, keyData, algorithm, extractable, keyUsages) {
79
+ const name = algorithmName(algorithm);
80
+ const usages = [...keyUsages];
81
+ if (format !== 'raw')
82
+ throw notSupported(`importKey format ${format} is not supported`);
83
+ const bytes = toBytes(keyData);
84
+ if (name === 'HMAC') {
85
+ const hashAlg = algorithmParams(algorithm).hash;
86
+ if (hashAlg === undefined)
87
+ throw notSupported('HMAC requires a hash');
88
+ hashFunction(hashFromAlgorithm(hashAlg));
89
+ return {
90
+ type: 'secret',
91
+ extractable,
92
+ algorithm: { name: 'HMAC', hash: { name: algorithmName(hashAlg) } },
93
+ usages,
94
+ kind: 'hmac',
95
+ bytes,
96
+ };
97
+ }
98
+ if (name === 'HKDF') {
99
+ return {
100
+ type: 'secret',
101
+ extractable,
102
+ algorithm: { name: 'HKDF' },
103
+ usages,
104
+ kind: 'hkdf',
105
+ bytes,
106
+ };
107
+ }
108
+ if (name === 'AES-GCM') {
109
+ if (bytes.length !== 16 && bytes.length !== 24 && bytes.length !== 32) {
110
+ throw dataError(`AES-GCM key must be 16, 24, or 32 bytes, got ${bytes.length}`);
111
+ }
112
+ return {
113
+ type: 'secret',
114
+ extractable,
115
+ algorithm: { name: 'AES-GCM', length: bytes.length * 8 },
116
+ usages,
117
+ kind: 'aes-gcm',
118
+ bytes,
119
+ };
120
+ }
121
+ if (name === 'ECDH') {
122
+ const namedCurve = String(algorithmParams(algorithm).namedCurve ?? 'P-256').toUpperCase();
123
+ if (namedCurve !== 'P-256')
124
+ throw notSupported(`ECDH curve ${namedCurve} is not supported`);
125
+ if (bytes.length !== 65 || bytes[0] !== 0x04) {
126
+ throw dataError('ECDH P-256 public key must be uncompressed (65 bytes)');
127
+ }
128
+ return {
129
+ type: 'public',
130
+ extractable,
131
+ algorithm: { name: 'ECDH', namedCurve: 'P-256' },
132
+ usages,
133
+ kind: 'ecdh-public',
134
+ bytes,
135
+ };
136
+ }
137
+ throw notSupported(`importKey algorithm ${name} is not supported`);
138
+ }
139
+ async function exportKey(format, key) {
140
+ if (format !== 'raw')
141
+ throw notSupported(`exportKey format ${format} is not supported`);
142
+ const cryptoKey = asKey(key);
143
+ if (cryptoKey.kind === 'ecdh-public')
144
+ return toArrayBuffer(cryptoKey.bytes);
145
+ if (!cryptoKey.extractable)
146
+ throw operationError('key is not extractable');
147
+ return toArrayBuffer(cryptoKey.bytes);
148
+ }
149
+ async function generateKey(algorithm, extractable, keyUsages) {
150
+ const name = algorithmName(algorithm);
151
+ if (name !== 'ECDH')
152
+ throw notSupported(`generateKey algorithm ${name} is not supported`);
153
+ const namedCurve = String(algorithmParams(algorithm).namedCurve ?? 'P-256').toUpperCase();
154
+ if (namedCurve !== 'P-256')
155
+ throw notSupported(`ECDH curve ${namedCurve} is not supported`);
156
+ const seedLength = Number(p256.lengths.seed ?? 48);
157
+ const secretKey = p256.utils.randomSecretKey(getRandomBytes(seedLength));
158
+ const publicBytes = p256.getPublicKey(secretKey, false);
159
+ const usages = [...keyUsages];
160
+ return {
161
+ publicKey: {
162
+ type: 'public',
163
+ extractable: true,
164
+ algorithm: { name: 'ECDH', namedCurve: 'P-256' },
165
+ usages: [],
166
+ kind: 'ecdh-public',
167
+ bytes: publicBytes,
168
+ },
169
+ privateKey: {
170
+ type: 'private',
171
+ extractable,
172
+ algorithm: { name: 'ECDH', namedCurve: 'P-256' },
173
+ usages,
174
+ kind: 'ecdh-private',
175
+ bytes: secretKey,
176
+ },
177
+ };
178
+ }
179
+ async function deriveBits(algorithm, baseKey, length) {
180
+ const name = algorithmName(algorithm);
181
+ const key = asKey(baseKey);
182
+ const byteLength = Math.ceil(length / 8);
183
+ if (name === 'HKDF') {
184
+ if (key.kind !== 'hkdf')
185
+ throw invalidAccess('HKDF');
186
+ const params = algorithmParams(algorithm);
187
+ const hash = HASH_OUTPUT[hashFromAlgorithm(params.hash)];
188
+ if (hash === undefined)
189
+ throw notSupported('HKDF hash is not supported');
190
+ const salt = toBytes(params.salt);
191
+ const info = toBytes(params.info);
192
+ return toArrayBuffer(hkdf(hash.noble, key.bytes, salt, info, byteLength));
193
+ }
194
+ if (name === 'ECDH') {
195
+ if (key.kind !== 'ecdh-private')
196
+ throw invalidAccess('ECDH');
197
+ const peer = asKey(paramsPublic(algorithm));
198
+ if (peer.kind !== 'ecdh-public')
199
+ throw dataError('ECDH public key required');
200
+ const shared = p256.getSharedSecret(key.bytes, peer.bytes, true);
201
+ const secret = shared.length === 32 ? shared : shared.subarray(shared.length - 32);
202
+ if (secret.length < byteLength)
203
+ throw operationError('deriveBits length too large');
204
+ return toArrayBuffer(secret.subarray(0, byteLength));
205
+ }
206
+ throw notSupported(`deriveBits algorithm ${name} is not supported`);
207
+ }
208
+ function paramsPublic(algorithm) {
209
+ const params = algorithmParams(algorithm);
210
+ return params.public;
211
+ }
212
+ function invalidAccess(name) {
213
+ const error = new Error(`Key is not valid for ${name}`);
214
+ error.name = 'InvalidAccessError';
215
+ return error;
216
+ }
217
+ async function sign(algorithm, key, data) {
218
+ const name = algorithmName(algorithm);
219
+ const cryptoKey = asKey(key);
220
+ if (name !== 'HMAC' && name !== 'HMAC-SHA-256') {
221
+ throw notSupported(`subtle.sign does not support ${name}`);
222
+ }
223
+ if (cryptoKey.kind !== 'hmac')
224
+ throw invalidAccess('HMAC');
225
+ const hash = HASH_OUTPUT[hashFromAlgorithm(cryptoKey.algorithm.hash)] ?? HASH_OUTPUT.sha256;
226
+ if (hash === undefined)
227
+ throw notSupported('HMAC hash is not supported');
228
+ return toArrayBuffer(hmac(hash.noble, cryptoKey.bytes, toBytes(data)));
229
+ }
230
+ async function verify(algorithm, key, signature, data) {
231
+ const expected = new Uint8Array(await sign(algorithm, key, data));
232
+ const actual = toBytes(signature);
233
+ if (expected.length !== actual.length)
234
+ return false;
235
+ let diff = 0;
236
+ for (let i = 0; i < expected.length; i++)
237
+ diff |= (expected[i] ?? 0) ^ (actual[i] ?? 0);
238
+ return diff === 0;
239
+ }
240
+ function gcmParams(algorithm) {
241
+ const params = algorithmParams(algorithm);
242
+ const iv = toBytes(params.iv);
243
+ if (iv.length !== 12)
244
+ throw notSupported('AES-GCM IV must be 12 bytes');
245
+ const aad = params.additionalData === undefined ? undefined : toBytes(params.additionalData);
246
+ return { iv, aad };
247
+ }
248
+ async function encrypt(algorithm, key, data) {
249
+ if (algorithmName(algorithm) !== 'AES-GCM') {
250
+ throw notSupported(`subtle.encrypt does not support ${algorithmName(algorithm)}`);
251
+ }
252
+ const cryptoKey = asKey(key);
253
+ if (cryptoKey.kind !== 'aes-gcm')
254
+ throw invalidAccess('AES-GCM');
255
+ const { iv, aad } = gcmParams(algorithm);
256
+ return toArrayBuffer(gcm(cryptoKey.bytes, iv, aad).encrypt(toBytes(data)));
257
+ }
258
+ async function decrypt(algorithm, key, data) {
259
+ if (algorithmName(algorithm) !== 'AES-GCM') {
260
+ throw notSupported(`subtle.decrypt does not support ${algorithmName(algorithm)}`);
261
+ }
262
+ const cryptoKey = asKey(key);
263
+ if (cryptoKey.kind !== 'aes-gcm')
264
+ throw invalidAccess('AES-GCM');
265
+ const { iv, aad } = gcmParams(algorithm);
266
+ try {
267
+ return toArrayBuffer(gcm(cryptoKey.bytes, iv, aad).decrypt(toBytes(data)));
268
+ }
269
+ catch {
270
+ throw operationError('Decryption failed');
271
+ }
272
+ }
273
+ export const subtle = {
274
+ digest,
275
+ importKey,
276
+ exportKey,
277
+ generateKey,
278
+ deriveBits,
279
+ sign,
280
+ verify,
281
+ encrypt,
282
+ decrypt,
283
+ };
284
+ export const webcrypto = {
285
+ getRandomValues,
286
+ randomUUID,
287
+ subtle,
288
+ };
@@ -0,0 +1,136 @@
1
+ import { createHash, createHmac, Hash, Hmac } from './crypto-hash.js';
2
+ import { getRandomValues, randomUUID, subtle, webcrypto } from './crypto-subtle.js';
3
+ export { createHash, createHmac, getRandomValues, Hash, Hmac, randomUUID, subtle, webcrypto };
4
+ export declare function randomBytes(size: number, callback?: (error: Error | null, buffer: Uint8Array) => void): Uint8Array | undefined;
5
+ export declare function randomFillSync<T extends ArrayBufferView>(buffer: T, offset?: number, size?: number): T;
6
+ export declare function randomFill<T extends ArrayBufferView>(buffer: T, offset?: number | ((error: Error | null, buffer: T) => void), size?: number | ((error: Error | null, buffer: T) => void), callback?: (error: Error | null, buffer: T) => void): undefined | T;
7
+ export declare function randomInt(min: number, max?: number | ((error: Error | null, value: number) => void), callback?: (error: Error | null, value: number) => void): number | undefined;
8
+ export declare function timingSafeEqual(a: ArrayBufferView, b: ArrayBufferView): boolean;
9
+ export declare const rng: typeof randomBytes;
10
+ export declare const prng: typeof randomBytes;
11
+ export declare const checkPrime: (...args: never[]) => never;
12
+ export declare const checkPrimeSync: (...args: never[]) => never;
13
+ export declare const createCipheriv: (...args: never[]) => never;
14
+ export declare const createDecipheriv: (...args: never[]) => never;
15
+ export declare const createDiffieHellman: (...args: never[]) => never;
16
+ export declare const createECDH: (...args: never[]) => never;
17
+ export declare const createPrivateKey: (...args: never[]) => never;
18
+ export declare const createPublicKey: (...args: never[]) => never;
19
+ export declare const createSecretKey: (...args: never[]) => never;
20
+ export declare const createSign: (...args: never[]) => never;
21
+ export declare const createVerify: (...args: never[]) => never;
22
+ export declare const diffieHellman: (...args: never[]) => never;
23
+ export declare const generateKeyPair: (...args: never[]) => never;
24
+ export declare const generateKeyPairSync: (...args: never[]) => never;
25
+ export declare const generateKey: (...args: never[]) => never;
26
+ export declare const pbkdf2: (...args: never[]) => never;
27
+ export declare const pbkdf2Sync: (...args: never[]) => never;
28
+ export declare const scrypt: (...args: never[]) => never;
29
+ export declare const scryptSync: (...args: never[]) => never;
30
+ export declare const sign: (...args: never[]) => never;
31
+ export declare const verify: (...args: never[]) => never;
32
+ export declare const hkdfSync: (...args: never[]) => never;
33
+ export declare const privateDecrypt: (...args: never[]) => never;
34
+ export declare const privateEncrypt: (...args: never[]) => never;
35
+ export declare const publicDecrypt: (...args: never[]) => never;
36
+ export declare const publicEncrypt: (...args: never[]) => never;
37
+ declare const cryptoObject: {
38
+ createHash: typeof createHash;
39
+ createHmac: typeof createHmac;
40
+ Hash: typeof Hash;
41
+ Hmac: typeof Hmac;
42
+ randomBytes: typeof randomBytes;
43
+ randomFill: typeof randomFill;
44
+ randomFillSync: typeof randomFillSync;
45
+ randomInt: typeof randomInt;
46
+ randomUUID: typeof randomUUID;
47
+ getRandomValues: typeof getRandomValues;
48
+ timingSafeEqual: typeof timingSafeEqual;
49
+ subtle: {
50
+ digest: (algorithm: string | {
51
+ [parameter: string]: unknown;
52
+ name: string;
53
+ }, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
54
+ importKey: (format: string, keyData: (ArrayBuffer | ArrayBufferView<ArrayBufferLike>) | {
55
+ [x: string]: unknown;
56
+ }, algorithm: string | {
57
+ [parameter: string]: unknown;
58
+ name: string;
59
+ }, extractable: boolean, keyUsages: readonly string[]) => Promise<import("./crypto-subtle.js").GuestCryptoKey>;
60
+ exportKey: (format: string, key: import("./crypto-subtle.js").GuestCryptoKey) => Promise<ArrayBuffer>;
61
+ generateKey: (algorithm: string | {
62
+ [parameter: string]: unknown;
63
+ name: string;
64
+ }, extractable: boolean, keyUsages: readonly string[]) => Promise<import("./crypto-subtle.js").GuestCryptoKey | {
65
+ publicKey: import("./crypto-subtle.js").GuestCryptoKey;
66
+ privateKey: import("./crypto-subtle.js").GuestCryptoKey;
67
+ }>;
68
+ deriveBits: (algorithm: string | {
69
+ [parameter: string]: unknown;
70
+ name: string;
71
+ }, baseKey: import("./crypto-subtle.js").GuestCryptoKey, length: number) => Promise<ArrayBuffer>;
72
+ sign: (algorithm: string | {
73
+ [parameter: string]: unknown;
74
+ name: string;
75
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
76
+ verify: (algorithm: string | {
77
+ [parameter: string]: unknown;
78
+ name: string;
79
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, signature: ArrayBuffer | ArrayBufferView<ArrayBufferLike>, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<boolean>;
80
+ encrypt: (algorithm: string | {
81
+ [parameter: string]: unknown;
82
+ name: string;
83
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
84
+ decrypt: (algorithm: string | {
85
+ [parameter: string]: unknown;
86
+ name: string;
87
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
88
+ };
89
+ webcrypto: {
90
+ getRandomValues: typeof getRandomValues;
91
+ randomUUID: typeof randomUUID;
92
+ subtle: {
93
+ digest: (algorithm: string | {
94
+ [parameter: string]: unknown;
95
+ name: string;
96
+ }, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
97
+ importKey: (format: string, keyData: (ArrayBuffer | ArrayBufferView<ArrayBufferLike>) | {
98
+ [x: string]: unknown;
99
+ }, algorithm: string | {
100
+ [parameter: string]: unknown;
101
+ name: string;
102
+ }, extractable: boolean, keyUsages: readonly string[]) => Promise<import("./crypto-subtle.js").GuestCryptoKey>;
103
+ exportKey: (format: string, key: import("./crypto-subtle.js").GuestCryptoKey) => Promise<ArrayBuffer>;
104
+ generateKey: (algorithm: string | {
105
+ [parameter: string]: unknown;
106
+ name: string;
107
+ }, extractable: boolean, keyUsages: readonly string[]) => Promise<import("./crypto-subtle.js").GuestCryptoKey | {
108
+ publicKey: import("./crypto-subtle.js").GuestCryptoKey;
109
+ privateKey: import("./crypto-subtle.js").GuestCryptoKey;
110
+ }>;
111
+ deriveBits: (algorithm: string | {
112
+ [parameter: string]: unknown;
113
+ name: string;
114
+ }, baseKey: import("./crypto-subtle.js").GuestCryptoKey, length: number) => Promise<ArrayBuffer>;
115
+ sign: (algorithm: string | {
116
+ [parameter: string]: unknown;
117
+ name: string;
118
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
119
+ verify: (algorithm: string | {
120
+ [parameter: string]: unknown;
121
+ name: string;
122
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, signature: ArrayBuffer | ArrayBufferView<ArrayBufferLike>, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<boolean>;
123
+ encrypt: (algorithm: string | {
124
+ [parameter: string]: unknown;
125
+ name: string;
126
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
127
+ decrypt: (algorithm: string | {
128
+ [parameter: string]: unknown;
129
+ name: string;
130
+ }, key: import("./crypto-subtle.js").GuestCryptoKey, data: ArrayBuffer | ArrayBufferView<ArrayBufferLike>) => Promise<ArrayBuffer>;
131
+ };
132
+ };
133
+ rng: typeof randomBytes;
134
+ prng: typeof randomBytes;
135
+ };
136
+ export default cryptoObject;