tinfoil 0.8.3 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-sdk-provider.d.ts +1 -11
- package/dist/ai-sdk-provider.js +9 -151
- package/dist/client.browser.d.ts +3 -2
- package/dist/client.browser.js +13 -28
- package/dist/client.d.ts +4 -3
- package/dist/client.js +13 -153
- package/dist/config.d.ts +4 -3
- package/dist/config.js +4 -3
- package/dist/encrypted-body-fetch.d.ts +2 -2
- package/dist/encrypted-body-fetch.js +4 -4
- package/dist/esm/ai-sdk-provider.d.ts +1 -11
- package/dist/esm/ai-sdk-provider.js +9 -148
- package/dist/esm/client.browser.d.ts +3 -2
- package/dist/esm/client.browser.js +13 -28
- package/dist/esm/client.d.ts +4 -3
- package/dist/esm/client.js +13 -153
- package/dist/esm/config.d.ts +4 -3
- package/dist/esm/config.js +4 -3
- package/dist/esm/encrypted-body-fetch.d.ts +2 -2
- package/dist/esm/encrypted-body-fetch.js +4 -4
- package/dist/esm/index.browser.d.ts +2 -1
- package/dist/esm/index.browser.js +2 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/pinned-tls-fetch.d.ts +1 -0
- package/dist/esm/pinned-tls-fetch.js +110 -0
- package/dist/esm/secure-client.browser.d.ts +20 -0
- package/dist/esm/secure-client.browser.js +57 -0
- package/dist/esm/secure-client.d.ts +20 -0
- package/dist/esm/secure-client.js +69 -0
- package/dist/esm/verifier.js +2 -2
- package/dist/index.browser.d.ts +2 -1
- package/dist/index.browser.js +4 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/pinned-tls-fetch.d.ts +1 -0
- package/dist/pinned-tls-fetch.js +116 -0
- package/dist/secure-client.browser.d.ts +20 -0
- package/dist/secure-client.browser.js +61 -0
- package/dist/secure-client.d.ts +20 -0
- package/dist/secure-client.js +73 -0
- package/dist/verifier.js +2 -2
- package/package.json +8 -3
- package/dist/ai-sdk-provider.browser.d.ts +0 -7
- package/dist/ai-sdk-provider.browser.js +0 -30
- package/dist/esm/ai-sdk-provider.browser.d.ts +0 -7
- package/dist/esm/ai-sdk-provider.browser.js +0 -27
|
@@ -1,159 +1,20 @@
|
|
|
1
1
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
2
2
|
import { TINFOIL_CONFIG } from "./config";
|
|
3
|
-
import {
|
|
4
|
-
import { Verifier } from "./verifier";
|
|
5
|
-
import { isRealBrowser } from "./env";
|
|
6
|
-
import https from "https";
|
|
7
|
-
import { checkServerIdentity as tlsCheckServerIdentity } from "tls";
|
|
8
|
-
import { X509Certificate, createHash } from "crypto";
|
|
9
|
-
import { Readable } from "stream";
|
|
10
|
-
/**
|
|
11
|
-
* Creates an AI SDK provider with the specified API key.
|
|
12
|
-
*
|
|
13
|
-
* @param apiKey - The API key for the Tinfoil API
|
|
14
|
-
* @param options - Optional configuration options
|
|
15
|
-
* @returns A TinfoilAI instance
|
|
16
|
-
*/
|
|
3
|
+
import { SecureClient } from "tinfoil/secure-client";
|
|
17
4
|
export async function createTinfoilAI(apiKey, options = {}) {
|
|
18
5
|
const baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
19
|
-
const
|
|
6
|
+
const enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
|
|
20
7
|
const configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// step 2: create the appropriate fetch function based on available keys
|
|
28
|
-
let fetchFunction;
|
|
29
|
-
if (hpkePublicKey) {
|
|
30
|
-
// HPKE available: use encrypted body fetch
|
|
31
|
-
fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, hpkeKeyURL);
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
// HPKE not available: check if we're in a browser
|
|
35
|
-
if (isRealBrowser()) {
|
|
36
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
37
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
38
|
-
}
|
|
39
|
-
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
40
|
-
if (!tlsPublicKeyFingerprint) {
|
|
41
|
-
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
42
|
-
}
|
|
43
|
-
fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
|
|
44
|
-
}
|
|
45
|
-
// step 3: create the openai compatible provider
|
|
46
|
-
// that uses the appropriate fetch function
|
|
8
|
+
const secureClient = new SecureClient({
|
|
9
|
+
baseURL,
|
|
10
|
+
enclaveURL,
|
|
11
|
+
configRepo,
|
|
12
|
+
});
|
|
13
|
+
await secureClient.ready();
|
|
47
14
|
return createOpenAICompatible({
|
|
48
15
|
name: "tinfoil",
|
|
49
16
|
baseURL: baseURL.replace(/\/$/, ""),
|
|
50
17
|
apiKey: apiKey,
|
|
51
|
-
fetch:
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
55
|
-
return (async (input, init) => {
|
|
56
|
-
// Normalize URL
|
|
57
|
-
const makeURL = (value) => {
|
|
58
|
-
if (typeof value === "string")
|
|
59
|
-
return new URL(value);
|
|
60
|
-
if (value instanceof URL)
|
|
61
|
-
return value;
|
|
62
|
-
return new URL(value.url);
|
|
63
|
-
};
|
|
64
|
-
const url = makeURL(input);
|
|
65
|
-
if (url.protocol !== "https:") {
|
|
66
|
-
throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
|
|
67
|
-
}
|
|
68
|
-
// Gather method and headers
|
|
69
|
-
const method = (init?.method || input.method || "GET").toUpperCase();
|
|
70
|
-
const headers = new Headers(init?.headers || input?.headers || {});
|
|
71
|
-
const headerObj = {};
|
|
72
|
-
headers.forEach((v, k) => {
|
|
73
|
-
headerObj[k] = v;
|
|
74
|
-
});
|
|
75
|
-
// Resolve body
|
|
76
|
-
let body = init?.body;
|
|
77
|
-
if (!body && input instanceof Request) {
|
|
78
|
-
// If the original was a Request with a body, read it
|
|
79
|
-
try {
|
|
80
|
-
const buf = await input.arrayBuffer();
|
|
81
|
-
if (buf && buf.byteLength)
|
|
82
|
-
body = Buffer.from(buf);
|
|
83
|
-
}
|
|
84
|
-
catch { }
|
|
85
|
-
}
|
|
86
|
-
// Convert web streams to Node streams if needed
|
|
87
|
-
if (body && typeof body.getReader === "function") {
|
|
88
|
-
body = Readable.fromWeb(body);
|
|
89
|
-
}
|
|
90
|
-
if (body instanceof ArrayBuffer) {
|
|
91
|
-
body = Buffer.from(body);
|
|
92
|
-
}
|
|
93
|
-
if (ArrayBuffer.isView(body)) {
|
|
94
|
-
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
95
|
-
}
|
|
96
|
-
const requestOptions = {
|
|
97
|
-
protocol: url.protocol,
|
|
98
|
-
hostname: url.hostname,
|
|
99
|
-
port: url.port ? Number(url.port) : 443,
|
|
100
|
-
path: `${url.pathname}${url.search}`,
|
|
101
|
-
method,
|
|
102
|
-
headers: headerObj,
|
|
103
|
-
checkServerIdentity: (host, cert) => {
|
|
104
|
-
const raw = cert.raw;
|
|
105
|
-
if (!raw) {
|
|
106
|
-
return new Error("Certificate raw bytes are unavailable for pinning");
|
|
107
|
-
}
|
|
108
|
-
const x509 = new X509Certificate(raw);
|
|
109
|
-
const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
|
|
110
|
-
const fp = createHash("sha256").update(publicKeyDer).digest("hex");
|
|
111
|
-
if (fp !== expectedFingerprintHex) {
|
|
112
|
-
return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
|
|
113
|
-
}
|
|
114
|
-
return tlsCheckServerIdentity(host, cert);
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
const { signal } = init || {};
|
|
118
|
-
const res = await new Promise((resolve, reject) => {
|
|
119
|
-
const req = https.request(requestOptions, resolve);
|
|
120
|
-
req.on("error", reject);
|
|
121
|
-
if (signal) {
|
|
122
|
-
if (signal.aborted) {
|
|
123
|
-
req.destroy(new Error("Request aborted"));
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
|
|
127
|
-
}
|
|
128
|
-
if (body === undefined || body === null) {
|
|
129
|
-
req.end();
|
|
130
|
-
}
|
|
131
|
-
else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
|
|
132
|
-
req.end(body);
|
|
133
|
-
}
|
|
134
|
-
else if (typeof body.pipe === "function") {
|
|
135
|
-
body.pipe(req);
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
// Fallback: try to serialize objects
|
|
139
|
-
req.end(String(body));
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
const responseHeaders = new Headers();
|
|
143
|
-
for (const [k, v] of Object.entries(res.headers)) {
|
|
144
|
-
if (Array.isArray(v)) {
|
|
145
|
-
v.forEach(item => responseHeaders.append(k, item));
|
|
146
|
-
}
|
|
147
|
-
else if (v != null) {
|
|
148
|
-
responseHeaders.set(k, String(v));
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
// Convert Node stream to Web ReadableStream
|
|
152
|
-
const webStream = Readable.toWeb(res);
|
|
153
|
-
return new Response(webStream, {
|
|
154
|
-
status: res.statusCode || 0,
|
|
155
|
-
statusText: res.statusMessage || "",
|
|
156
|
-
headers: responseHeaders,
|
|
157
|
-
});
|
|
18
|
+
fetch: secureClient.fetch,
|
|
158
19
|
});
|
|
159
20
|
}
|
|
@@ -4,7 +4,7 @@ import type { VerificationDocument } from "./verifier";
|
|
|
4
4
|
interface TinfoilAIOptions {
|
|
5
5
|
apiKey?: string;
|
|
6
6
|
baseURL?: string;
|
|
7
|
-
|
|
7
|
+
enclaveURL?: string;
|
|
8
8
|
configRepo?: string;
|
|
9
9
|
[key: string]: any;
|
|
10
10
|
}
|
|
@@ -13,10 +13,11 @@ export declare class TinfoilAI {
|
|
|
13
13
|
private clientPromise;
|
|
14
14
|
private readyPromise?;
|
|
15
15
|
private configRepo?;
|
|
16
|
+
private secureClient;
|
|
16
17
|
private verificationDocument?;
|
|
17
18
|
apiKey?: string;
|
|
18
19
|
baseURL?: string;
|
|
19
|
-
|
|
20
|
+
enclaveURL?: string;
|
|
20
21
|
constructor(options?: TinfoilAIOptions);
|
|
21
22
|
ready(): Promise<void>;
|
|
22
23
|
private initClient;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
|
-
import {
|
|
2
|
+
import { SecureClient } from "tinfoil/secure-client";
|
|
3
3
|
import { TINFOIL_CONFIG } from "./config";
|
|
4
|
-
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
5
|
-
import { isRealBrowser } from "./env";
|
|
6
4
|
function createAsyncProxy(promise) {
|
|
7
5
|
return new Proxy({}, {
|
|
8
6
|
get(target, prop) {
|
|
@@ -35,8 +33,14 @@ export class TinfoilAI {
|
|
|
35
33
|
}
|
|
36
34
|
this.apiKey = openAIOptions.apiKey;
|
|
37
35
|
this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
38
|
-
this.
|
|
36
|
+
this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
|
|
39
37
|
this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
38
|
+
// Create the secure client for handling transport security
|
|
39
|
+
this.secureClient = new SecureClient({
|
|
40
|
+
baseURL: this.baseURL,
|
|
41
|
+
enclaveURL: this.enclaveURL,
|
|
42
|
+
configRepo: this.configRepo,
|
|
43
|
+
});
|
|
40
44
|
this.clientPromise = this.initClient(openAIOptions);
|
|
41
45
|
}
|
|
42
46
|
async ready() {
|
|
@@ -51,34 +55,15 @@ export class TinfoilAI {
|
|
|
51
55
|
return this.createOpenAIClient(options);
|
|
52
56
|
}
|
|
53
57
|
async createOpenAIClient(options = {}) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
await verifier.verify();
|
|
60
|
-
this.verificationDocument = verifier.getVerificationDocument();
|
|
61
|
-
if (!this.verificationDocument) {
|
|
62
|
-
throw new Error("Verification document not available after successful verification");
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
throw new Error(`Failed to verify enclave: ${error}`);
|
|
67
|
-
}
|
|
68
|
-
const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
|
|
69
|
-
if (!hpkePublicKey) {
|
|
70
|
-
// In browsers we require HPKE; TLS pinning fallback is Node-only
|
|
71
|
-
if (isRealBrowser()) {
|
|
72
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
73
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
74
|
-
}
|
|
75
|
-
throw new Error("HPKE public key is required in browser environments");
|
|
58
|
+
await this.secureClient.ready();
|
|
59
|
+
this.verificationDocument = await this.secureClient.getVerificationDocument();
|
|
60
|
+
if (!this.verificationDocument) {
|
|
61
|
+
throw new Error("Verification document not available after successful verification");
|
|
76
62
|
}
|
|
77
|
-
const fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
78
63
|
const clientOptions = {
|
|
79
64
|
...options,
|
|
80
65
|
baseURL: this.baseURL,
|
|
81
|
-
fetch:
|
|
66
|
+
fetch: this.secureClient.fetch,
|
|
82
67
|
};
|
|
83
68
|
// In browser usage, OpenAI SDK typically needs dangerouslyAllowBrowser
|
|
84
69
|
clientOptions.dangerouslyAllowBrowser = true;
|
package/dist/esm/client.d.ts
CHANGED
|
@@ -14,8 +14,8 @@ interface TinfoilAIOptions {
|
|
|
14
14
|
apiKey?: string;
|
|
15
15
|
/** Override the inference API base URL */
|
|
16
16
|
baseURL?: string;
|
|
17
|
-
/** Override the URL used to fetch the
|
|
18
|
-
|
|
17
|
+
/** Override the URL used to fetch the ENCLAVE key (defaults to baseURL) */
|
|
18
|
+
enclaveURL?: string;
|
|
19
19
|
/** Override the config GitHub repository */
|
|
20
20
|
configRepo?: string;
|
|
21
21
|
[key: string]: any;
|
|
@@ -25,10 +25,11 @@ export declare class TinfoilAI {
|
|
|
25
25
|
private clientPromise;
|
|
26
26
|
private readyPromise?;
|
|
27
27
|
private configRepo?;
|
|
28
|
+
private secureClient;
|
|
28
29
|
private verificationDocument?;
|
|
29
30
|
apiKey?: string;
|
|
30
31
|
baseURL?: string;
|
|
31
|
-
|
|
32
|
+
enclaveURL?: string;
|
|
32
33
|
/**
|
|
33
34
|
* Creates a new TinfoilAI instance.
|
|
34
35
|
* @param options - Configuration options including apiKey and other OpenAI client options
|
package/dist/esm/client.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
|
-
import {
|
|
2
|
+
import { SecureClient } from "tinfoil/secure-client";
|
|
3
3
|
import { TINFOIL_CONFIG } from "./config";
|
|
4
|
-
import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
|
|
5
|
-
import https from "https";
|
|
6
|
-
import { checkServerIdentity as tlsCheckServerIdentity } from "tls";
|
|
7
|
-
import { X509Certificate, createHash } from "crypto";
|
|
8
|
-
import { Readable } from "stream";
|
|
9
|
-
/**
|
|
10
|
-
* Detects if the code is running in a real browser environment.
|
|
11
|
-
* Returns false for Node.js environments, even with WASM loaded.
|
|
12
|
-
*/
|
|
13
4
|
import { isRealBrowser } from "./env";
|
|
14
5
|
/**
|
|
15
6
|
* Creates a proxy that allows property access and method calls on a Promise before it resolves.
|
|
@@ -66,9 +57,13 @@ export class TinfoilAI {
|
|
|
66
57
|
// Store properties for compatibility
|
|
67
58
|
this.apiKey = openAIOptions.apiKey;
|
|
68
59
|
this.baseURL = options.baseURL || TINFOIL_CONFIG.INFERENCE_BASE_URL;
|
|
69
|
-
this.
|
|
70
|
-
this.configRepo =
|
|
71
|
-
|
|
60
|
+
this.enclaveURL = options.enclaveURL || TINFOIL_CONFIG.ENCLAVE_URL;
|
|
61
|
+
this.configRepo = options.configRepo || TINFOIL_CONFIG.INFERENCE_PROXY_REPO;
|
|
62
|
+
this.secureClient = new SecureClient({
|
|
63
|
+
baseURL: this.baseURL,
|
|
64
|
+
enclaveURL: this.enclaveURL,
|
|
65
|
+
configRepo: this.configRepo,
|
|
66
|
+
});
|
|
72
67
|
this.clientPromise = this.initClient(openAIOptions);
|
|
73
68
|
}
|
|
74
69
|
/**
|
|
@@ -87,44 +82,15 @@ export class TinfoilAI {
|
|
|
87
82
|
return this.createOpenAIClient(options);
|
|
88
83
|
}
|
|
89
84
|
async createOpenAIClient(options = {}) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
});
|
|
95
|
-
try {
|
|
96
|
-
await verifier.verify();
|
|
97
|
-
this.verificationDocument = verifier.getVerificationDocument();
|
|
98
|
-
if (!this.verificationDocument) {
|
|
99
|
-
throw new Error("Verification document not available after successful verification");
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
throw new Error(`Failed to verify enclave: ${error}`);
|
|
104
|
-
}
|
|
105
|
-
const hpkePublicKey = this.verificationDocument.enclaveMeasurement.hpkePublicKey;
|
|
106
|
-
const tlsPublicKeyFingerprint = this.verificationDocument.enclaveMeasurement.tlsPublicKeyFingerprint;
|
|
107
|
-
let fetchFunction;
|
|
108
|
-
if (hpkePublicKey) {
|
|
109
|
-
// HPKE available: use encrypted body fetch
|
|
110
|
-
fetchFunction = createEncryptedBodyFetch(this.baseURL, hpkePublicKey, this.hpkeKeyURL);
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
// HPKE not available: check if we're in a browser
|
|
114
|
-
if (isRealBrowser()) {
|
|
115
|
-
throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
|
|
116
|
-
"Only HPKE-enabled enclaves can be used in browser environments.");
|
|
117
|
-
}
|
|
118
|
-
// Node.js environment: fall back to TLS-only verification using pinned TLS fetch
|
|
119
|
-
if (!tlsPublicKeyFingerprint) {
|
|
120
|
-
throw new Error("Neither HPKE public key nor TLS public key fingerprint available for verification");
|
|
121
|
-
}
|
|
122
|
-
fetchFunction = createPinnedTlsFetch(tlsPublicKeyFingerprint);
|
|
85
|
+
await this.secureClient.ready();
|
|
86
|
+
this.verificationDocument = await this.secureClient.getVerificationDocument();
|
|
87
|
+
if (!this.verificationDocument) {
|
|
88
|
+
throw new Error("Verification document not available after successful verification");
|
|
123
89
|
}
|
|
124
90
|
const clientOptions = {
|
|
125
91
|
...options,
|
|
126
92
|
baseURL: this.baseURL,
|
|
127
|
-
fetch:
|
|
93
|
+
fetch: this.secureClient.fetch,
|
|
128
94
|
};
|
|
129
95
|
// Enable dangerouslyAllowBrowser in Node.js with WASM (which makes OpenAI SDK think we're in a browser)
|
|
130
96
|
// OR if explicitly set by the user (for legitimate browser usage like playgrounds)
|
|
@@ -243,109 +209,3 @@ export class TinfoilAI {
|
|
|
243
209
|
TinfoilAI.Uploads = OpenAI.Uploads;
|
|
244
210
|
TinfoilAI.VectorStores = OpenAI.VectorStores;
|
|
245
211
|
})(TinfoilAI || (TinfoilAI = {}));
|
|
246
|
-
function createPinnedTlsFetch(expectedFingerprintHex) {
|
|
247
|
-
return (async (input, init) => {
|
|
248
|
-
// Normalize URL
|
|
249
|
-
const makeURL = (value) => {
|
|
250
|
-
if (typeof value === "string")
|
|
251
|
-
return new URL(value);
|
|
252
|
-
if (value instanceof URL)
|
|
253
|
-
return value;
|
|
254
|
-
return new URL(value.url);
|
|
255
|
-
};
|
|
256
|
-
const url = makeURL(input);
|
|
257
|
-
if (url.protocol !== "https:") {
|
|
258
|
-
throw new Error(`HTTP connections are not allowed. Use HTTPS. URL: ${url.toString()}`);
|
|
259
|
-
}
|
|
260
|
-
// Gather method and headers
|
|
261
|
-
const method = (init?.method || input.method || "GET").toUpperCase();
|
|
262
|
-
const headers = new Headers(init?.headers || input?.headers || {});
|
|
263
|
-
const headerObj = {};
|
|
264
|
-
headers.forEach((v, k) => {
|
|
265
|
-
headerObj[k] = v;
|
|
266
|
-
});
|
|
267
|
-
// Resolve body
|
|
268
|
-
let body = init?.body;
|
|
269
|
-
if (!body && input instanceof Request) {
|
|
270
|
-
// If the original was a Request with a body, read it
|
|
271
|
-
try {
|
|
272
|
-
const buf = await input.arrayBuffer();
|
|
273
|
-
if (buf && buf.byteLength)
|
|
274
|
-
body = Buffer.from(buf);
|
|
275
|
-
}
|
|
276
|
-
catch { }
|
|
277
|
-
}
|
|
278
|
-
// Convert web streams to Node streams if needed
|
|
279
|
-
if (body && typeof body.getReader === "function") {
|
|
280
|
-
body = Readable.fromWeb(body);
|
|
281
|
-
}
|
|
282
|
-
if (body instanceof ArrayBuffer) {
|
|
283
|
-
body = Buffer.from(body);
|
|
284
|
-
}
|
|
285
|
-
if (ArrayBuffer.isView(body)) {
|
|
286
|
-
body = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
287
|
-
}
|
|
288
|
-
const requestOptions = {
|
|
289
|
-
protocol: url.protocol,
|
|
290
|
-
hostname: url.hostname,
|
|
291
|
-
port: url.port ? Number(url.port) : 443,
|
|
292
|
-
path: `${url.pathname}${url.search}`,
|
|
293
|
-
method,
|
|
294
|
-
headers: headerObj,
|
|
295
|
-
checkServerIdentity: (host, cert) => {
|
|
296
|
-
const raw = cert.raw;
|
|
297
|
-
if (!raw) {
|
|
298
|
-
return new Error("Certificate raw bytes are unavailable for pinning");
|
|
299
|
-
}
|
|
300
|
-
const x509 = new X509Certificate(raw);
|
|
301
|
-
const publicKeyDer = x509.publicKey.export({ type: "spki", format: "der" });
|
|
302
|
-
const fp = createHash("sha256").update(publicKeyDer).digest("hex");
|
|
303
|
-
if (fp !== expectedFingerprintHex) {
|
|
304
|
-
return new Error(`Certificate public key fingerprint mismatch. Expected: ${expectedFingerprintHex}, Got: ${fp}`);
|
|
305
|
-
}
|
|
306
|
-
return tlsCheckServerIdentity(host, cert);
|
|
307
|
-
},
|
|
308
|
-
};
|
|
309
|
-
const { signal } = init || {};
|
|
310
|
-
const res = await new Promise((resolve, reject) => {
|
|
311
|
-
const req = https.request(requestOptions, resolve);
|
|
312
|
-
req.on("error", reject);
|
|
313
|
-
if (signal) {
|
|
314
|
-
if (signal.aborted) {
|
|
315
|
-
req.destroy(new Error("Request aborted"));
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
signal.addEventListener("abort", () => req.destroy(new Error("Request aborted")));
|
|
319
|
-
}
|
|
320
|
-
if (body === undefined || body === null) {
|
|
321
|
-
req.end();
|
|
322
|
-
}
|
|
323
|
-
else if (typeof body === "string" || Buffer.isBuffer(body) || ArrayBuffer.isView(body)) {
|
|
324
|
-
req.end(body);
|
|
325
|
-
}
|
|
326
|
-
else if (typeof body.pipe === "function") {
|
|
327
|
-
body.pipe(req);
|
|
328
|
-
}
|
|
329
|
-
else {
|
|
330
|
-
// Fallback: try to serialize objects
|
|
331
|
-
req.end(String(body));
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
const responseHeaders = new Headers();
|
|
335
|
-
for (const [k, v] of Object.entries(res.headers)) {
|
|
336
|
-
if (Array.isArray(v)) {
|
|
337
|
-
v.forEach(item => responseHeaders.append(k, item));
|
|
338
|
-
}
|
|
339
|
-
else if (v != null) {
|
|
340
|
-
responseHeaders.set(k, String(v));
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
// Convert Node stream to Web ReadableStream
|
|
344
|
-
const webStream = Readable.toWeb(res);
|
|
345
|
-
return new Response(webStream, {
|
|
346
|
-
status: res.statusCode || 0,
|
|
347
|
-
statusText: res.statusMessage || "",
|
|
348
|
-
headers: responseHeaders,
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
}
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -7,11 +7,12 @@ export declare const TINFOIL_CONFIG: {
|
|
|
7
7
|
*/
|
|
8
8
|
readonly INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/";
|
|
9
9
|
/**
|
|
10
|
-
* The URL used to fetch the
|
|
11
|
-
*
|
|
10
|
+
* The URL used to fetch the enclave server public key (via EHBP .well-known endpoint).
|
|
11
|
+
* and the attestation (vial the attestation .well-known endpoint too).
|
|
12
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so enclaveURL and baseURL share
|
|
12
13
|
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
13
14
|
*/
|
|
14
|
-
readonly
|
|
15
|
+
readonly ENCLAVE_URL: "https://inference.tinfoil.sh/v1/";
|
|
15
16
|
/**
|
|
16
17
|
* The GitHub repository for the confidential inference proxy
|
|
17
18
|
*/
|
package/dist/esm/config.js
CHANGED
|
@@ -7,11 +7,12 @@ export const TINFOIL_CONFIG = {
|
|
|
7
7
|
*/
|
|
8
8
|
INFERENCE_BASE_URL: "https://inference.tinfoil.sh/v1/",
|
|
9
9
|
/**
|
|
10
|
-
* The URL used to fetch the
|
|
11
|
-
*
|
|
10
|
+
* The URL used to fetch the enclave server public key (via EHBP .well-known endpoint).
|
|
11
|
+
* and the attestation (vial the attestation .well-known endpoint too).
|
|
12
|
+
* Defaults to the same value as `INFERENCE_BASE_URL` so enclaveURL and baseURL share
|
|
12
13
|
* the same default. Callers may override independently if key discovery is hosted elsewhere.
|
|
13
14
|
*/
|
|
14
|
-
|
|
15
|
+
ENCLAVE_URL: "https://inference.tinfoil.sh/v1/",
|
|
15
16
|
/**
|
|
16
17
|
* The GitHub repository for the confidential inference proxy
|
|
17
18
|
*/
|
|
@@ -3,8 +3,8 @@ export declare function normalizeEncryptedBodyRequestArgs(input: RequestInfo | U
|
|
|
3
3
|
url: string;
|
|
4
4
|
init?: RequestInit;
|
|
5
5
|
};
|
|
6
|
-
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit,
|
|
7
|
-
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string,
|
|
6
|
+
export declare function encryptedBodyRequest(input: RequestInfo | URL, hpkePublicKey: string, init?: RequestInit, enclaveURL?: string): Promise<Response>;
|
|
7
|
+
export declare function createEncryptedBodyFetch(baseURL: string, hpkePublicKey: string, enclaveURL?: string): typeof fetch;
|
|
8
8
|
export declare function __setEhbpModuleForTests(module: EhbpModule | undefined): void;
|
|
9
9
|
export declare function __resetEhbpModuleStateForTests(): void;
|
|
10
10
|
export {};
|
|
@@ -24,12 +24,12 @@ export function normalizeEncryptedBodyRequestArgs(input, init) {
|
|
|
24
24
|
init: { ...derivedInit, ...init },
|
|
25
25
|
};
|
|
26
26
|
}
|
|
27
|
-
export async function encryptedBodyRequest(input, hpkePublicKey, init,
|
|
27
|
+
export async function encryptedBodyRequest(input, hpkePublicKey, init, enclaveURL) {
|
|
28
28
|
const { url: requestUrl, init: requestInit } = normalizeEncryptedBodyRequestArgs(input, init);
|
|
29
29
|
const u = new URL(requestUrl);
|
|
30
30
|
const { origin } = u;
|
|
31
31
|
// Determine which origin to fetch the HPKE key from (may be different from request origin)
|
|
32
|
-
const keyOrigin =
|
|
32
|
+
const keyOrigin = enclaveURL ? new URL(enclaveURL).origin : origin;
|
|
33
33
|
// If key origin matches request origin, use the standard transport (fetches keys from origin)
|
|
34
34
|
// Otherwise, build a composite transport that routes to `origin` but uses the key fetched from `keyOrigin`.
|
|
35
35
|
const transport = keyOrigin === origin
|
|
@@ -51,11 +51,11 @@ export async function encryptedBodyRequest(input, hpkePublicKey, init, hpkeKeyUR
|
|
|
51
51
|
}
|
|
52
52
|
return transport.request(requestUrl, requestInit);
|
|
53
53
|
}
|
|
54
|
-
export function createEncryptedBodyFetch(baseURL, hpkePublicKey,
|
|
54
|
+
export function createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL) {
|
|
55
55
|
return (async (input, init) => {
|
|
56
56
|
const normalized = normalizeEncryptedBodyRequestArgs(input, init);
|
|
57
57
|
const targetUrl = new URL(normalized.url, baseURL);
|
|
58
|
-
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init,
|
|
58
|
+
return encryptedBodyRequest(targetUrl.toString(), hpkePublicKey, normalized.init, enclaveURL);
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
61
|
// Private helper functions
|
|
@@ -2,5 +2,6 @@ export { TinfoilAI } from "./client.browser";
|
|
|
2
2
|
export { TinfoilAI as default } from "./client.browser";
|
|
3
3
|
export * from "./verifier";
|
|
4
4
|
export * from "./verification-runner";
|
|
5
|
-
export * from "./ai-sdk-provider
|
|
5
|
+
export * from "./ai-sdk-provider";
|
|
6
6
|
export * from "./config";
|
|
7
|
+
export { SecureClient } from "tinfoil/secure-client";
|
|
@@ -3,5 +3,6 @@ export { TinfoilAI } from "./client.browser";
|
|
|
3
3
|
export { TinfoilAI as default } from "./client.browser";
|
|
4
4
|
export * from "./verifier";
|
|
5
5
|
export * from "./verification-runner";
|
|
6
|
-
export * from "./ai-sdk-provider
|
|
6
|
+
export * from "./ai-sdk-provider";
|
|
7
7
|
export * from "./config";
|
|
8
|
+
export { SecureClient } from "tinfoil/secure-client";
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -4,4 +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
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,6 +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
10
|
// Re-export OpenAI utility types and classes that users might need
|
|
10
11
|
// Using public exports from the main OpenAI package instead of deep imports
|
|
11
12
|
export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createPinnedTlsFetch(expectedFingerprintHex: string): typeof fetch;
|