tinfoil 0.8.6 → 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.
@@ -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
  }
@@ -4,4 +4,4 @@ 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";
@@ -5,4 +5,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";
@@ -4,5 +4,5 @@ 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
@@ -6,7 +6,7 @@ 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,5 +1,5 @@
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
4
  import { isRealBrowser } from "./env";
5
5
  function createAsyncProxy(promise) {
@@ -4,4 +4,4 @@ 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";
@@ -24,5 +24,5 @@ __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
@@ -4,5 +4,5 @@ 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,7 +14,7 @@ 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
19
  var tinfoilai_1 = require("./tinfoilai");
20
20
  Object.defineProperty(exports, "TinfoilAI", { enumerable: true, get: function () { return tinfoilai_1.TinfoilAI; } });
@@ -25,7 +25,8 @@ __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
+ }
package/dist/tinfoilai.js CHANGED
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TinfoilAI = void 0;
7
7
  const openai_1 = __importDefault(require("openai"));
8
- const secure_client_1 = require("tinfoil/secure-client");
8
+ const secure_client_1 = require("./secure-client");
9
9
  const config_1 = require("./config");
10
10
  const env_1 = require("./env");
11
11
  function createAsyncProxy(promise) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tinfoil",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "Tinfoil secure OpenAI client wrapper",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -44,9 +44,14 @@
44
44
  },
45
45
  "./secure-client": {
46
46
  "types": "./dist/secure-client.d.ts",
47
- "browser": "./dist/esm/secure-client.browser.js",
48
47
  "import": "./dist/esm/secure-client.js",
49
48
  "require": "./dist/secure-client.js"
49
+ },
50
+ "./secure-fetch": {
51
+ "types": "./dist/secure-fetch.d.ts",
52
+ "browser": "./dist/esm/secure-fetch.browser.js",
53
+ "import": "./dist/esm/secure-fetch.js",
54
+ "require": "./dist/secure-fetch.js"
50
55
  }
51
56
  },
52
57
  "scripts": {
@@ -1,20 +0,0 @@
1
- import type { VerificationDocument } from "./verifier";
2
- interface SecureClientOptions {
3
- baseURL?: string;
4
- enclaveURL?: string;
5
- configRepo?: string;
6
- }
7
- export declare class SecureClient {
8
- private initPromise;
9
- private verificationDocument;
10
- private _fetch;
11
- private readonly baseURL?;
12
- private readonly enclaveURL?;
13
- private readonly configRepo?;
14
- constructor(options?: SecureClientOptions);
15
- ready(): Promise<void>;
16
- private initSecureClient;
17
- getVerificationDocument(): Promise<VerificationDocument>;
18
- get fetch(): typeof fetch;
19
- }
20
- export {};
@@ -1,57 +0,0 @@
1
- import { Verifier } from "./verifier";
2
- import { TINFOIL_CONFIG } from "./config";
3
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
4
- import { isRealBrowser } from "./env";
5
- export class SecureClient {
6
- constructor(options = {}) {
7
- this.initPromise = null;
8
- this.verificationDocument = null;
9
- this._fetch = null;
10
- this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
11
- this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
12
- this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
13
- }
14
- async ready() {
15
- if (!this.initPromise) {
16
- this.initPromise = this.initSecureClient();
17
- }
18
- return this.initPromise;
19
- }
20
- async initSecureClient() {
21
- const verifier = new Verifier({
22
- serverURL: this.enclaveURL,
23
- configRepo: this.configRepo,
24
- });
25
- await verifier.verify();
26
- const doc = verifier.getVerificationDocument();
27
- if (!doc) {
28
- throw new Error("Verification document not available after successful verification");
29
- }
30
- this.verificationDocument = doc;
31
- const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
32
- if (!hpkePublicKey) {
33
- if (isRealBrowser()) {
34
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
35
- "Only HPKE-enabled enclaves can be used in browser environments.");
36
- }
37
- throw new Error("HPKE public key is required in browser environments");
38
- }
39
- this._fetch = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.enclaveURL);
40
- }
41
- async getVerificationDocument() {
42
- if (!this.initPromise) {
43
- await this.ready();
44
- }
45
- await this.initPromise;
46
- if (!this.verificationDocument) {
47
- throw new Error("Verification document unavailable: client not verified yet");
48
- }
49
- return this.verificationDocument;
50
- }
51
- get fetch() {
52
- return async (input, init) => {
53
- await this.ready();
54
- return this._fetch(input, init);
55
- };
56
- }
57
- }
@@ -1,20 +0,0 @@
1
- import type { VerificationDocument } from "./verifier";
2
- interface SecureClientOptions {
3
- baseURL?: string;
4
- enclaveURL?: string;
5
- configRepo?: string;
6
- }
7
- export declare class SecureClient {
8
- private initPromise;
9
- private verificationDocument;
10
- private _fetch;
11
- private readonly baseURL?;
12
- private readonly enclaveURL?;
13
- private readonly configRepo?;
14
- constructor(options?: SecureClientOptions);
15
- ready(): Promise<void>;
16
- private initSecureClient;
17
- getVerificationDocument(): Promise<VerificationDocument>;
18
- get fetch(): typeof fetch;
19
- }
20
- export {};
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SecureClient = void 0;
4
- const verifier_1 = require("./verifier");
5
- const config_1 = require("./config");
6
- const encrypted_body_fetch_1 = require("./encrypted-body-fetch");
7
- const env_1 = require("./env");
8
- class SecureClient {
9
- constructor(options = {}) {
10
- this.initPromise = null;
11
- this.verificationDocument = null;
12
- this._fetch = null;
13
- this.baseURL = options.baseURL || config_1.TINFOIL_CONFIG.INFERENCE_BASE_URL;
14
- this.enclaveURL = options.enclaveURL || config_1.TINFOIL_CONFIG.ENCLAVE_URL;
15
- this.configRepo = options.configRepo || config_1.TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
16
- }
17
- async ready() {
18
- if (!this.initPromise) {
19
- this.initPromise = this.initSecureClient();
20
- }
21
- return this.initPromise;
22
- }
23
- async initSecureClient() {
24
- const verifier = new verifier_1.Verifier({
25
- serverURL: this.enclaveURL,
26
- configRepo: this.configRepo,
27
- });
28
- await verifier.verify();
29
- const doc = verifier.getVerificationDocument();
30
- if (!doc) {
31
- throw new Error("Verification document not available after successful verification");
32
- }
33
- this.verificationDocument = doc;
34
- const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
35
- if (!hpkePublicKey) {
36
- if ((0, env_1.isRealBrowser)()) {
37
- throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
38
- "Only HPKE-enabled enclaves can be used in browser environments.");
39
- }
40
- throw new Error("HPKE public key is required in browser environments");
41
- }
42
- this._fetch = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(this.baseURL, hpkePublicKey, this.enclaveURL);
43
- }
44
- async getVerificationDocument() {
45
- if (!this.initPromise) {
46
- await this.ready();
47
- }
48
- await this.initPromise;
49
- if (!this.verificationDocument) {
50
- throw new Error("Verification document unavailable: client not verified yet");
51
- }
52
- return this.verificationDocument;
53
- }
54
- get fetch() {
55
- return async (input, init) => {
56
- await this.ready();
57
- return this._fetch(input, init);
58
- };
59
- }
60
- }
61
- exports.SecureClient = SecureClient;