tinfoil 0.8.5 → 0.8.7

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 (35) hide show
  1. package/dist/ai-sdk-provider.js +1 -1
  2. package/dist/encrypted-body-fetch.js +27 -80
  3. package/dist/esm/ai-sdk-provider.js +1 -1
  4. package/dist/esm/encrypted-body-fetch.js +27 -80
  5. package/dist/esm/index.browser.d.ts +3 -3
  6. package/dist/esm/index.browser.js +3 -3
  7. package/dist/esm/index.d.ts +3 -3
  8. package/dist/esm/index.js +3 -3
  9. package/dist/esm/secure-client.js +2 -21
  10. package/dist/esm/secure-fetch.browser.d.ts +1 -0
  11. package/dist/esm/secure-fetch.browser.js +10 -0
  12. package/dist/esm/secure-fetch.d.ts +1 -0
  13. package/dist/esm/secure-fetch.js +22 -0
  14. package/dist/esm/{client.browser.js → tinfoilai.js} +10 -5
  15. package/dist/index.browser.d.ts +3 -3
  16. package/dist/index.browser.js +5 -5
  17. package/dist/index.d.ts +3 -3
  18. package/dist/index.js +7 -6
  19. package/dist/secure-client.js +2 -21
  20. package/dist/secure-fetch.browser.d.ts +1 -0
  21. package/dist/secure-fetch.browser.js +13 -0
  22. package/dist/secure-fetch.d.ts +1 -0
  23. package/dist/secure-fetch.js +25 -0
  24. package/dist/{client.browser.js → tinfoilai.js} +10 -5
  25. package/package.json +7 -3
  26. package/dist/client.d.ts +0 -123
  27. package/dist/client.js +0 -218
  28. package/dist/esm/client.d.ts +0 -123
  29. package/dist/esm/client.js +0 -211
  30. package/dist/esm/secure-client.browser.d.ts +0 -20
  31. package/dist/esm/secure-client.browser.js +0 -57
  32. package/dist/secure-client.browser.d.ts +0 -20
  33. package/dist/secure-client.browser.js +0 -61
  34. /package/dist/{client.browser.d.ts → esm/tinfoilai.d.ts} +0 -0
  35. /package/dist/{esm/client.browser.d.ts → tinfoilai.d.ts} +0 -0
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createTinfoilAI = createTinfoilAI;
4
4
  const openai_compatible_1 = require("@ai-sdk/openai-compatible");
5
5
  const config_1 = require("./config");
6
- const secure_client_1 = require("tinfoil/secure-client");
6
+ const secure_client_1 = require("./secure-client");
7
7
  async function createTinfoilAI(apiKey, options = {}) {
8
8
  const baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
9
9
  const enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
@@ -39,8 +39,7 @@ exports.createEncryptedBodyFetch = createEncryptedBodyFetch;
39
39
  exports.__setEhbpModuleForTests = __setEhbpModuleForTests;
40
40
  exports.__resetEhbpModuleStateForTests = __resetEhbpModuleStateForTests;
41
41
  const env_1 = require("./env");
42
- const transportCache = new Map();
43
- const compositeTransportCache = new Map();
42
+ let transport = null;
44
43
  let ehbpModulePromise = null;
45
44
  let ehbpModuleOverride;
46
45
  // Public API
@@ -68,28 +67,16 @@ async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveURL) {
68
67
  const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
69
68
  const u = new URL(requestUrl);
70
69
  const { origin } = u;
71
- // Determine which origin to fetch the HPKE key from (may be different from request origin)
72
70
  const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
73
- // If key origin matches request origin, use the standard transport (fetches keys from origin)
74
- // Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
75
- const transport = keyOrigin === origin
76
- ? await getTransportForOrigin(origin)
77
- : await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
78
- const serverPublicKey = await transport.getServerPublicKeyHex();
79
- if (serverPublicKey !== hpkePublicKey) {
80
- // Clear caches so the next attempt re-handshakes
81
- if (keyOrigin === origin) {
82
- transportCache.delete(origin);
83
- }
84
- else {
85
- const cacheKey = `${origin}::${keyOrigin}`;
86
- compositeTransportCache.delete(cacheKey);
87
- // Also clear the underlying key-origin cache to force re-fetch
88
- transportCache.delete(keyOrigin);
89
- }
90
- throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
71
+ if (!transport) {
72
+ transport = getTransportForOrigin(origin, keyOrigin);
91
73
  }
92
- return transport.request(requestUrl, requestInit);
74
+ const transportInstance = await transport;
75
+ const transportKeyHash = await transportInstance.getServerPublicKeyHex();
76
+ if (transportKeyHash !== hpkePublicKey) {
77
+ throw new Error(`HPKE public key mismatch. Expected: ${hpkePublicKey}, Got: ${transportKeyHash}`);
78
+ }
79
+ return transportInstance.request(requestUrl, requestInit);
93
80
  }
94
81
  function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
95
82
  return (async (input, init) => {
@@ -124,64 +111,25 @@ function getEhbpModule() {
124
111
  }
125
112
  return ehbpModulePromise;
126
113
  }
127
- async function getTransportForOrigin(origin) {
128
- const cached = transportCache.get(origin);
129
- if (cached) {
130
- return cached;
131
- }
132
- const transportPromise = (async () => {
133
- const { Identity, createTransport } = await getEhbpModule();
134
- // Ensure we're in a secure context with WebCrypto Subtle available (required by EHBP)
135
- if (typeof globalThis !== 'undefined') {
136
- const isSecure = globalThis.isSecureContext !== false;
137
- const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
138
- if (!isSecure || !hasSubtle) {
139
- const reason = !isSecure
140
- ? 'insecure context (use HTTPS or localhost)'
141
- : 'missing WebCrypto SubtleCrypto';
142
- throw new Error(`EHBP requires a secure browser context: ${reason}`);
143
- }
114
+ async function getTransportForOrigin(origin, keyOrigin) {
115
+ const { Identity, createTransport, Transport } = await getEhbpModule();
116
+ // Ensure secure browser context
117
+ if (typeof globalThis !== 'undefined') {
118
+ const isSecure = globalThis.isSecureContext !== false;
119
+ const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
120
+ if (!isSecure || !hasSubtle) {
121
+ const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
122
+ throw new Error(`EHBP requires a secure browser context: ${reason}`);
144
123
  }
145
- const clientIdentity = await Identity.generate();
146
- return createTransport(origin, clientIdentity);
147
- })().catch((error) => {
148
- transportCache.delete(origin);
149
- throw error;
150
- });
151
- transportCache.set(origin, transportPromise);
152
- return transportPromise;
153
- }
154
- async function getTransportForRequestOriginWithKeyFrom(origin, keyOrigin) {
155
- const cacheKey = `${origin}::${keyOrigin}`;
156
- const cached = compositeTransportCache.get(cacheKey);
157
- if (cached) {
158
- return cached;
159
124
  }
160
- const transportPromise = (async () => {
161
- const { Identity, createTransport, Transport } = await getEhbpModule();
162
- // Ensure secure browser context
163
- if (typeof globalThis !== 'undefined') {
164
- const isSecure = globalThis.isSecureContext !== false;
165
- const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
166
- if (!isSecure || !hasSubtle) {
167
- const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
168
- throw new Error(`EHBP requires a secure browser context: ${reason}`);
169
- }
170
- }
171
- // Create a single client identity to use for both key discovery and requests
172
- const clientIdentity = await Identity.generate();
173
- // Fetch the server's HPKE public key from the dedicated key origin
174
- const keyTransport = await createTransport(keyOrigin, clientIdentity);
175
- // Reuse the discovered server public key but route to the request origin
176
- const serverPublicKey = keyTransport.getServerPublicKey();
177
- const requestHost = new URL(origin).host;
178
- return new Transport(clientIdentity, requestHost, serverPublicKey);
179
- })().catch((error) => {
180
- compositeTransportCache.delete(cacheKey);
181
- throw error;
182
- });
183
- compositeTransportCache.set(cacheKey, transportPromise);
184
- return transportPromise;
125
+ // Create a single client identity to use for both key discovery and requests
126
+ const clientIdentity = await Identity.generate();
127
+ // Fetch the server's HPKE public key from the dedicated key origin.
128
+ // TODO: clean up this code. EHBP should make it easy to fetch the key without creating a transport object.
129
+ const keyTransport = await createTransport(keyOrigin, clientIdentity);
130
+ const serverPublicKey = keyTransport.getServerPublicKey();
131
+ const requestHost = new URL(origin).host;
132
+ return new Transport(clientIdentity, requestHost, serverPublicKey);
185
133
  }
186
134
  // Test utilities
187
135
  function __setEhbpModuleForTests(module) {
@@ -191,6 +139,5 @@ function __setEhbpModuleForTests(module) {
191
139
  function __resetEhbpModuleStateForTests() {
192
140
  ehbpModuleOverride = undefined;
193
141
  ehbpModulePromise = null;
194
- transportCache.clear();
195
- compositeTransportCache.clear();
142
+ transport = null;
196
143
  }
@@ -1,6 +1,6 @@
1
1
  import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
2
2
  import { TINFOIL_CONFIG } from "./config";
3
- import { SecureClient } from "tinfoil/secure-client";
3
+ import { SecureClient } from "./secure-client";
4
4
  export async function createTinfoilAI(apiKey, options = {}) {
5
5
  const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
6
6
  const enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
@@ -1,6 +1,5 @@
1
1
  import { isRealBrowser } from "./env";
2
- const transportCache = new Map();
3
- const compositeTransportCache = new Map();
2
+ let transport = null;
4
3
  let ehbpModulePromise = null;
5
4
  let ehbpModuleOverride;
6
5
  // Public API
@@ -28,28 +27,16 @@ export async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveUR
28
27
  const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
29
28
  const u = new URL(requestUrl);
30
29
  const { origin } = u;
31
- // Determine which origin to fetch the HPKE key from (may be different from request origin)
32
30
  const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
33
- // If key origin matches request origin, use the standard transport (fetches keys from origin)
34
- // Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
35
- const transport = keyOrigin === origin
36
- ? await getTransportForOrigin(origin)
37
- : await getTransportForRequestOriginWithKeyFrom(origin, keyOrigin);
38
- const serverPublicKey = await transport.getServerPublicKeyHex();
39
- if (serverPublicKey !== hpkePublicKey) {
40
- // Clear caches so the next attempt re-handshakes
41
- if (keyOrigin === origin) {
42
- transportCache.delete(origin);
43
- }
44
- else {
45
- const cacheKey = `${origin}::${keyOrigin}`;
46
- compositeTransportCache.delete(cacheKey);
47
- // Also clear the underlying key-origin cache to force re-fetch
48
- transportCache.delete(keyOrigin);
49
- }
50
- throw new Error(`HPKE public key mismatch: expected ${hpkePublicKey}, got ${serverPublicKey}`);
31
+ if (!transport) {
32
+ transport = getTransportForOrigin(origin, keyOrigin);
51
33
  }
52
- return transport.request(requestUrl, requestInit);
34
+ const transportInstance = await transport;
35
+ const transportKeyHash = await transportInstance.getServerPublicKeyHex();
36
+ if (transportKeyHash !== hpkePublicKey) {
37
+ throw new Error(`HPKE public key mismatch. Expected: ${hpkePublicKey}, Got: ${transportKeyHash}`);
38
+ }
39
+ return transportInstance.request(requestUrl, requestInit);
53
40
  }
54
41
  export function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
55
42
  return (async (input, init) => {
@@ -84,64 +71,25 @@ function getEhbpModule() {
84
71
  }
85
72
  return ehbpModulePromise;
86
73
  }
87
- async function getTransportForOrigin(origin) {
88
- const cached = transportCache.get(origin);
89
- if (cached) {
90
- return cached;
91
- }
92
- const transportPromise = (async () => {
93
- const { Identity, createTransport } = await getEhbpModule();
94
- // Ensure we're in a secure context with WebCrypto Subtle available (required by EHBP)
95
- if (typeof globalThis !== 'undefined') {
96
- const isSecure = globalThis.isSecureContext !== false;
97
- const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
98
- if (!isSecure || !hasSubtle) {
99
- const reason = !isSecure
100
- ? 'insecure context (use HTTPS or localhost)'
101
- : 'missing WebCrypto SubtleCrypto';
102
- throw new Error(`EHBP requires a secure browser context: ${reason}`);
103
- }
74
+ async function getTransportForOrigin(origin, keyOrigin) {
75
+ const { Identity, createTransport, Transport } = await getEhbpModule();
76
+ // Ensure secure browser context
77
+ if (typeof globalThis !== 'undefined') {
78
+ const isSecure = globalThis.isSecureContext !== false;
79
+ const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
80
+ if (!isSecure || !hasSubtle) {
81
+ const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
82
+ throw new Error(`EHBP requires a secure browser context: ${reason}`);
104
83
  }
105
- const clientIdentity = await Identity.generate();
106
- return createTransport(origin, clientIdentity);
107
- })().catch((error) => {
108
- transportCache.delete(origin);
109
- throw error;
110
- });
111
- transportCache.set(origin, transportPromise);
112
- return transportPromise;
113
- }
114
- async function getTransportForRequestOriginWithKeyFrom(origin, keyOrigin) {
115
- const cacheKey = `${origin}::${keyOrigin}`;
116
- const cached = compositeTransportCache.get(cacheKey);
117
- if (cached) {
118
- return cached;
119
84
  }
120
- const transportPromise = (async () => {
121
- const { Identity, createTransport, Transport } = await getEhbpModule();
122
- // Ensure secure browser context
123
- if (typeof globalThis !== 'undefined') {
124
- const isSecure = globalThis.isSecureContext !== false;
125
- const hasSubtle = !!(globalThis.crypto && globalThis.crypto.subtle);
126
- if (!isSecure || !hasSubtle) {
127
- const reason = !isSecure ? 'insecure context (use HTTPS or localhost)' : 'missing WebCrypto SubtleCrypto';
128
- throw new Error(`EHBP requires a secure browser context: ${reason}`);
129
- }
130
- }
131
- // Create a single client identity to use for both key discovery and requests
132
- const clientIdentity = await Identity.generate();
133
- // Fetch the server's HPKE public key from the dedicated key origin
134
- const keyTransport = await createTransport(keyOrigin, clientIdentity);
135
- // Reuse the discovered server public key but route to the request origin
136
- const serverPublicKey = keyTransport.getServerPublicKey();
137
- const requestHost = new URL(origin).host;
138
- return new Transport(clientIdentity, requestHost, serverPublicKey);
139
- })().catch((error) => {
140
- compositeTransportCache.delete(cacheKey);
141
- throw error;
142
- });
143
- compositeTransportCache.set(cacheKey, transportPromise);
144
- return transportPromise;
85
+ // Create a single client identity to use for both key discovery and requests
86
+ const clientIdentity = await Identity.generate();
87
+ // Fetch the server's HPKE public key from the dedicated key origin.
88
+ // TODO: clean up this code. EHBP should make it easy to fetch the key without creating a transport object.
89
+ const keyTransport = await createTransport(keyOrigin, clientIdentity);
90
+ const serverPublicKey = keyTransport.getServerPublicKey();
91
+ const requestHost = new URL(origin).host;
92
+ return new Transport(clientIdentity, requestHost, serverPublicKey);
145
93
  }
146
94
  // Test utilities
147
95
  export function __setEhbpModuleForTests(module) {
@@ -151,6 +99,5 @@ export function __setEhbpModuleForTests(module) {
151
99
  export function __resetEhbpModuleStateForTests() {
152
100
  ehbpModuleOverride = undefined;
153
101
  ehbpModulePromise = null;
154
- transportCache.clear();
155
- compositeTransportCache.clear();
102
+ transport = null;
156
103
  }
@@ -1,7 +1,7 @@
1
- export { TinfoilAI } from "./client.browser";
2
- export { TinfoilAI as default } from "./client.browser";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
6
6
  export * from "./config";
7
- export { SecureClient } from "tinfoil/secure-client";
7
+ export { SecureClient } from "./secure-client";
@@ -1,8 +1,8 @@
1
1
  // Browser-safe entry point: avoids Node built-ins
2
- export { TinfoilAI } from "./client.browser";
3
- export { TinfoilAI as default } from "./client.browser";
2
+ export { TinfoilAI } from "./tinfoilai";
3
+ export { TinfoilAI as default } from "./tinfoilai";
4
4
  export * from "./verifier";
5
5
  export * from "./verification-runner";
6
6
  export * from "./ai-sdk-provider";
7
7
  export * from "./config";
8
- export { SecureClient } from "tinfoil/secure-client";
8
+ export { SecureClient } from "./secure-client";
@@ -1,8 +1,8 @@
1
- export { TinfoilAI } from "./client";
2
- export { TinfoilAI as default } from "./client";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
6
6
  export * from "./config";
7
- export * from "tinfoil/secure-client";
7
+ export { SecureClient } from "./secure-client";
8
8
  export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
package/dist/esm/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  // Re-export the TinfoilAI class
2
- export { TinfoilAI } from "./client";
3
- export { TinfoilAI as default } from "./client";
2
+ export { TinfoilAI } from "./tinfoilai";
3
+ export { TinfoilAI as default } from "./tinfoilai";
4
4
  // Export verifier and loader
5
5
  export * from "./verifier";
6
6
  export * from "./verification-runner";
7
7
  export * from "./ai-sdk-provider";
8
8
  export * from "./config";
9
- export * from "tinfoil/secure-client";
9
+ export { SecureClient } from "./secure-client";
10
10
  // Re-export OpenAI utility types and classes that users might need
11
11
  // Using public exports from the main OpenAI package instead of deep imports
12
12
  export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
@@ -1,8 +1,6 @@
1
1
  import { Verifier } from "./verifier";
2
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
3
- import { createPinnedTlsFetch } from "./pinned-tls-fetch";
4
2
  import { TINFOIL_CONFIG } from "./config";
5
- import { isRealBrowser } from "./env";
3
+ import { createSecureFetch } from "tinfoil/secure-fetch";
6
4
  export class SecureClient {
7
5
  constructor(options = {}) {
8
6
  this.initPromise = null;
@@ -31,24 +29,7 @@ export class SecureClient {
31
29
  this.verificationDocument = doc;
32
30
  // Extract keys from the verification document
33
31
  const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
34
- let fetchFunction;
35
- if (hpkePublicKey) {
36
- // HPKE available: use encrypted body fetch
37
- fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.enclaveURL);
38
- }
39
- else {
40
- // HPKE not available: check if we're in a browser
41
- if (isRealBrowser()) {
42
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
43
- "Only HPKE-enabled enclaves can be used in browser environments.");
44
- }
45
- // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
46
- if (!tlsPublicKeyFingerprint) {
47
- throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
48
- }
49
- fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
50
- }
51
- this._fetch = fetchFunction;
32
+ this._fetch = createSecureFetch(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
52
33
  }
53
34
  async getVerificationDocument() {
54
35
  if (!this.initPromise) {
@@ -0,0 +1 @@
1
+ export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
@@ -0,0 +1,10 @@
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
2
+ export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
3
+ if (hpkePublicKey) {
4
+ return createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
5
+ }
6
+ else {
7
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
8
+ "Only HPKE-enabled enclaves can be used in browser environments.");
9
+ }
10
+ }
@@ -0,0 +1 @@
1
+ export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
@@ -0,0 +1,22 @@
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
2
+ import { createPinnedTlsFetch } from "./pinned-tls-fetch";
3
+ import { isRealBrowser } from "./env";
4
+ export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
5
+ let fetchFunction;
6
+ if (hpkePublicKey) {
7
+ fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
8
+ }
9
+ else {
10
+ // HPKE not available: check if we're in a browser
11
+ if (isRealBrowser()) {
12
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
13
+ "Only HPKE-enabled enclaves can be used in browser environments.");
14
+ }
15
+ // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
16
+ if (!tlsPublicKeyFingerprint) {
17
+ throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
18
+ }
19
+ fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
20
+ }
21
+ return fetchFunction;
22
+ }
@@ -1,6 +1,7 @@
1
1
  import OpenAI from "openai";
2
- import { SecureClient } from "tinfoil/secure-client";
2
+ import { SecureClient } from "./secure-client";
3
3
  import { TINFOIL_CONFIG } from "./config";
4
+ import { isRealBrowser } from "./env";
4
5
  function createAsyncProxy(promise) {
5
6
  return new Proxy({}, {
6
7
  get(target, prop) {
@@ -28,14 +29,16 @@ export class TinfoilAI {
28
29
  const openAIOptions = { ...options };
29
30
  // In browser builds, never read secrets from process.env to avoid
30
31
  // leaking credentials into client bundles. Require explicit apiKey.
31
- if (typeof options.apiKey === "string") {
32
+ if (options.apiKey) {
32
33
  openAIOptions.apiKey = options.apiKey;
33
34
  }
35
+ else if (!isRealBrowser() && process.env.TINFOIL_API_KEY) {
36
+ openAIOptions.apiKey = process.env.TINFOIL_API_KEY;
37
+ }
34
38
  this.apiKey = openAIOptions.apiKey;
35
39
  this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
36
40
  this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
37
41
  this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
38
- // Create the secure client for handling transport security
39
42
  this.secureClient = new SecureClient({
40
43
  baseURL: this.baseURL,
41
44
  enclaveURL: this.enclaveURL,
@@ -65,8 +68,9 @@ export class TinfoilAI {
65
68
  baseURL: this.baseURL,
66
69
  fetch: this.secureClient.fetch,
67
70
  };
68
- // In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
69
- clientOptions.dangerouslyAllowBrowser = true;
71
+ if (isRealBrowser() || options.dangerouslyAllowBrowser === true) {
72
+ clientOptions.dangerouslyAllowBrowser = true;
73
+ }
70
74
  return new OpenAI(clientOptions);
71
75
  }
72
76
  async ensureReady() {
@@ -111,6 +115,7 @@ export class TinfoilAI {
111
115
  return createAsyncProxy(this.ensureReady().then((client) => client.beta));
112
116
  }
113
117
  }
118
+ // Namespace declaration merge to add OpenAI types to TinfoilAI
114
119
  (function (TinfoilAI) {
115
120
  TinfoilAI.Chat = OpenAI.Chat;
116
121
  TinfoilAI.Audio = OpenAI.Audio;
@@ -1,7 +1,7 @@
1
- export { TinfoilAI } from "./client.browser";
2
- export { TinfoilAI as default } from "./client.browser";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
6
6
  export * from "./config";
7
- export { SecureClient } from "tinfoil/secure-client";
7
+ export { SecureClient } from "./secure-client";
@@ -16,13 +16,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.SecureClient = exports.default = exports.TinfoilAI = void 0;
18
18
  // Browser-safe entry point: avoids Node built-ins
19
- var client_browser_1 = require("./client.browser");
20
- Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return client_browser_1.TinfoilAI; } });
21
- var client_browser_2 = require("./client.browser");
22
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_browser_2.TinfoilAI; } });
19
+ var tinfoilai_1 = require("./tinfoilai");
20
+ Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
21
+ var tinfoilai_2 = require("./tinfoilai");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return tinfoilai_2.TinfoilAI; } });
23
23
  __exportStar(require("./verifier"), exports);
24
24
  __exportStar(require("./verification-runner"), exports);
25
25
  __exportStar(require("./ai-sdk-provider"), exports);
26
26
  __exportStar(require("./config"), exports);
27
- var secure_client_1 = require("tinfoil/secure-client");
27
+ var secure_client_1 = require("./secure-client");
28
28
  Object.defineProperty(exports, "SecureClient", { enumerable: true, get: function () { return secure_client_1.SecureClient; } });
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- export { TinfoilAI } from "./client";
2
- export { TinfoilAI as default } from "./client";
1
+ export { TinfoilAI } from "./tinfoilai";
2
+ export { TinfoilAI as default } from "./tinfoilai";
3
3
  export * from "./verifier";
4
4
  export * from "./verification-runner";
5
5
  export * from "./ai-sdk-provider";
6
6
  export * from "./config";
7
- export * from "tinfoil/secure-client";
7
+ export { SecureClient } from "./secure-client";
8
8
  export { type Uploadable, toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
package/dist/index.js CHANGED
@@ -14,18 +14,19 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.OpenAIError = exports.PagePromise = exports.APIPromise = exports.toFile = exports.default = exports.TinfoilAI = void 0;
17
+ exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.OpenAIError = exports.PagePromise = exports.APIPromise = exports.toFile = exports.SecureClient = exports.default = exports.TinfoilAI = void 0;
18
18
  // Re-export the TinfoilAI class
19
- var client_1 = require("./client");
20
- Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return client_1.TinfoilAI; } });
21
- var client_2 = require("./client");
22
- Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_2.TinfoilAI; } });
19
+ var tinfoilai_1 = require("./tinfoilai");
20
+ Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
21
+ var tinfoilai_2 = require("./tinfoilai");
22
+ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return tinfoilai_2.TinfoilAI; } });
23
23
  // Export verifier and loader
24
24
  __exportStar(require("./verifier"), exports);
25
25
  __exportStar(require("./verification-runner"), exports);
26
26
  __exportStar(require("./ai-sdk-provider"), exports);
27
27
  __exportStar(require("./config"), exports);
28
- __exportStar(require("tinfoil/secure-client"), exports);
28
+ var secure_client_1 = require("./secure-client");
29
+ Object.defineProperty(exports, "SecureClient", { enumerable: true, get: function () { return secure_client_1.SecureClient; } });
29
30
  // Re-export OpenAI utility types and classes that users might need
30
31
  // Using public exports from the main OpenAI package instead of deep imports
31
32
  var openai_1 = require("openai");
@@ -2,10 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SecureClient = void 0;
4
4
  const verifier_1 = require("./verifier");
5
- const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
6
- const pinned_tls_fetch_1 = require("./pinned-tls-fetch");
7
5
  const config_1 = require("./config");
8
- const env_1 = require("./env");
6
+ const secure_fetch_1 = require("tinfoil/secure-fetch");
9
7
  class SecureClient {
10
8
  constructor(options = {}) {
11
9
  this.initPromise = null;
@@ -34,24 +32,7 @@ class SecureClient {
34
32
  this.verificationDocument = doc;
35
33
  // Extract keys from the verification document
36
34
  const { hpkePublicKey, tlsPublicKeyFingerprint } = this.verificationDocument.enclaveMeasurement;
37
- let fetchFunction;
38
- if (hpkePublicKey) {
39
- // HPKE available: use encrypted body fetch
40
- fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.enclaveURL);
41
- }
42
- else {
43
- // HPKE not available: check if we're in a browser
44
- if ((0, env_1.isRealBrowser)()) {
45
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
46
- "Only HPKE-enabled enclaves can be used in browser environments.");
47
- }
48
- // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
49
- if (!tlsPublicKeyFingerprint) {
50
- throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
51
- }
52
- fetchFunction = (0, pinned_tls_fetch_1.createPinnedTlsFetch)(tlsPublicKeyFingerprint);
53
- }
54
- this._fetch = fetchFunction;
35
+ this._fetch = (0, secure_fetch_1.createSecureFetch)(this.baseURL, this.enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint);
55
36
  }
56
37
  async getVerificationDocument() {
57
38
  if (!this.initPromise) {
@@ -0,0 +1 @@
1
+ export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSecureFetch = createSecureFetch;
4
+ const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
5
+ function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
6
+ if (hpkePublicKey) {
7
+ return (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, enclaveURL);
8
+ }
9
+ else {
10
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
11
+ "Only HPKE-enabled enclaves can be used in browser environments.");
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export declare function createSecureFetch(baseURL: string, enclaveURL?: string, hpkePublicKey?: string, tlsPublicKeyFingerprint?: string): typeof fetch;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSecureFetch = createSecureFetch;
4
+ const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
5
+ const pinned_tls_fetch_1 = require("./pinned-tls-fetch");
6
+ const env_1 = require("./env");
7
+ function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
8
+ let fetchFunction;
9
+ if (hpkePublicKey) {
10
+ fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, enclaveURL);
11
+ }
12
+ else {
13
+ // HPKE not available: check if we're in a browser
14
+ if ((0, env_1.isRealBrowser)()) {
15
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
16
+ "Only HPKE-enabled enclaves can be used in browser environments.");
17
+ }
18
+ // Node.js environment: fall back to TLS-only verification using pinned TLS fetch
19
+ if (!tlsPublicKeyFingerprint) {
20
+ throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
21
+ }
22
+ fetchFunction = (0, pinned_tls_fetch_1.createPinnedTlsFetch)(tlsPublicKeyFingerprint);
23
+ }
24
+ return fetchFunction;
25
+ }