@zeke-02/tinfoil 0.0.3 → 0.0.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.
@@ -1,6 +1,6 @@
1
1
  import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
2
- import { TINFOIL_CONFIG } from "./config";
3
- import { SecureClient } from "./secure-client";
2
+ import { TINFOIL_CONFIG } from "./config.mjs";
3
+ import { SecureClient } from "./secure-client.mjs";
4
4
  export async function createTinfoilAI(apiKey, options = {}) {
5
5
  const baseURL = options.baseURL;
6
6
  const enclaveURL = options.enclaveURL;
@@ -1,5 +1,5 @@
1
1
  import { Identity, Transport, PROTOCOL } from "@zeke-02/ehbp";
2
- import { getFetch } from "./fetch-adapter";
2
+ import { getFetch } from "./fetch-adapter.mjs";
3
3
  // Public API
4
4
  export async function getHPKEKey(enclaveURL) {
5
5
  const url = new URL(enclaveURL);
@@ -0,0 +1,8 @@
1
+ // Browser-safe entry point: avoids Node built-ins
2
+ export { TinfoilAI } from "./tinfoilai.mjs";
3
+ export { TinfoilAI as default } from "./tinfoilai.mjs";
4
+ export * from "./verifier.mjs";
5
+ export * from "./ai-sdk-provider.mjs";
6
+ export * from "./config.mjs";
7
+ export { SecureClient } from "./secure-client.mjs";
8
+ export { UnverifiedClient } from "./unverified-client.mjs";
@@ -1,13 +1,13 @@
1
1
  // Re-export the TinfoilAI class
2
- export { TinfoilAI } from "./tinfoilai";
3
- export { TinfoilAI as default } from "./tinfoilai";
2
+ export { TinfoilAI } from "./tinfoilai.mjs";
3
+ export { TinfoilAI as default } from "./tinfoilai.mjs";
4
4
  // Export verifier
5
- export * from "./verifier";
6
- export * from "./ai-sdk-provider";
7
- export * from "./config";
8
- export { SecureClient } from "./secure-client";
9
- export { UnverifiedClient } from "./unverified-client";
10
- export { fetchRouter } from "./router";
5
+ export * from "./verifier.mjs";
6
+ export * from "./ai-sdk-provider.mjs";
7
+ export * from "./config.mjs";
8
+ export { SecureClient } from "./secure-client.mjs";
9
+ export { UnverifiedClient } from "./unverified-client.mjs";
10
+ export { fetchRouter } from "./router.mjs";
11
11
  // Re-export OpenAI utility types and classes that users might need
12
12
  // Using public exports from the main OpenAI package instead of deep imports
13
13
  export { toFile, APIPromise, PagePromise, OpenAIError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "openai";
@@ -1,4 +1,4 @@
1
- import { TINFOIL_CONFIG } from "./config";
1
+ import { TINFOIL_CONFIG } from "./config.mjs";
2
2
  /**
3
3
  * Router utilities for fetching available Tinfoil routers
4
4
  */
@@ -1,7 +1,7 @@
1
- import { Verifier } from "./verifier";
2
- import { TINFOIL_CONFIG } from "./config";
3
- import { createSecureFetch } from "./secure-fetch";
4
- import { fetchRouter } from "./router";
1
+ import { Verifier } from "./verifier.mjs";
2
+ import { TINFOIL_CONFIG } from "./config.mjs";
3
+ import { createSecureFetch } from "./secure-fetch.mjs";
4
+ import { fetchRouter } from "./router.mjs";
5
5
  export class SecureClient {
6
6
  constructor(options = {}) {
7
7
  this.initPromise = null;
@@ -1,4 +1,4 @@
1
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
2
2
  export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
3
3
  if (hpkePublicKey) {
4
4
  return createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
@@ -0,0 +1,12 @@
1
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
2
+ export function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
3
+ let fetchFunction;
4
+ if (hpkePublicKey) {
5
+ fetchFunction = createEncryptedBodyFetch(baseURL, hpkePublicKey, enclaveURL);
6
+ }
7
+ else {
8
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
9
+ "Only HPKE-enabled enclaves can be used in browser environments.");
10
+ }
11
+ return fetchFunction;
12
+ }
@@ -1,7 +1,7 @@
1
1
  import OpenAI from "openai";
2
- import { SecureClient } from "./secure-client";
3
- import { TINFOIL_CONFIG } from "./config";
4
- import { isRealBrowser } from "./env";
2
+ import { SecureClient } from "./secure-client.mjs";
3
+ import { TINFOIL_CONFIG } from "./config.mjs";
4
+ import { isRealBrowser } from "./env.mjs";
5
5
  function createAsyncProxy(promise) {
6
6
  return new Proxy({}, {
7
7
  get(target, prop) {
@@ -1,6 +1,6 @@
1
- import { TINFOIL_CONFIG } from "./config";
2
- import { createEncryptedBodyFetch } from "./encrypted-body-fetch";
3
- import { fetchRouter } from "./router";
1
+ import { TINFOIL_CONFIG } from "./config.mjs";
2
+ import { createEncryptedBodyFetch } from "./encrypted-body-fetch.mjs";
3
+ import { fetchRouter } from "./router.mjs";
4
4
  export class UnverifiedClient {
5
5
  constructor(options = {}) {
6
6
  this.initPromise = null;
@@ -50,8 +50,8 @@
50
50
  * - `fetchLatestDigest(configRepo?)` → release digest via proxy
51
51
  * - `suppressWasmLogs(suppress?)` → control WASM log output
52
52
  */
53
- import { TINFOIL_CONFIG } from "./config";
54
- import { getFetch } from "./fetch-adapter";
53
+ import { TINFOIL_CONFIG } from "./config.mjs";
54
+ import { getFetch } from "./fetch-adapter.mjs";
55
55
  let cachedTextEncoder = null;
56
56
  function getTextEncoder() {
57
57
  if (!cachedTextEncoder) {
@@ -718,7 +718,7 @@ function loadWasmExec() {
718
718
  // If that fails (e.g., pure Node without bundler), fall back to require.
719
719
  try {
720
720
  // @ts-expect-error: Local JS helper has no TS types; ambient module declared in wasm-exec.d.ts
721
- await import("./wasm-exec.js");
721
+ await import("./wasm-exec.mjs");
722
722
  }
723
723
  catch {
724
724
  if (nodeRequire) {
@@ -0,0 +1,668 @@
1
+ // Copyright 2018 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ "use strict";
6
+
7
+ (() => {
8
+ const enosys = () => {
9
+ const err = new Error("not implemented");
10
+ err.code = "ENOSYS";
11
+ return err;
12
+ };
13
+
14
+ if (!globalThis.fs) {
15
+ let outputBuf = "";
16
+ globalThis.fs = {
17
+ constants: {
18
+ O_WRONLY: -1,
19
+ O_RDWR: -1,
20
+ O_CREAT: -1,
21
+ O_TRUNC: -1,
22
+ O_APPEND: -1,
23
+ O_EXCL: -1,
24
+ }, // unused
25
+ writeSync(fd, buf) {
26
+ outputBuf += decoder.decode(buf);
27
+ const nl = outputBuf.lastIndexOf("\n");
28
+ if (nl != -1) {
29
+ console.log(outputBuf.substring(0, nl));
30
+ outputBuf = outputBuf.substring(nl + 1);
31
+ }
32
+ return buf.length;
33
+ },
34
+ write(fd, buf, offset, length, position, callback) {
35
+ if (offset !== 0 || length !== buf.length || position !== null) {
36
+ callback(enosys());
37
+ return;
38
+ }
39
+ const n = this.writeSync(fd, buf);
40
+ callback(null, n);
41
+ },
42
+ chmod(path, mode, callback) {
43
+ callback(enosys());
44
+ },
45
+ chown(path, uid, gid, callback) {
46
+ callback(enosys());
47
+ },
48
+ close(fd, callback) {
49
+ callback(enosys());
50
+ },
51
+ fchmod(fd, mode, callback) {
52
+ callback(enosys());
53
+ },
54
+ fchown(fd, uid, gid, callback) {
55
+ callback(enosys());
56
+ },
57
+ fstat(fd, callback) {
58
+ callback(enosys());
59
+ },
60
+ fsync(fd, callback) {
61
+ callback(null);
62
+ },
63
+ ftruncate(fd, length, callback) {
64
+ callback(enosys());
65
+ },
66
+ lchown(path, uid, gid, callback) {
67
+ callback(enosys());
68
+ },
69
+ link(path, link, callback) {
70
+ callback(enosys());
71
+ },
72
+ lstat(path, callback) {
73
+ callback(enosys());
74
+ },
75
+ mkdir(path, perm, callback) {
76
+ callback(enosys());
77
+ },
78
+ open(path, flags, mode, callback) {
79
+ callback(enosys());
80
+ },
81
+ read(fd, buffer, offset, length, position, callback) {
82
+ callback(enosys());
83
+ },
84
+ readdir(path, callback) {
85
+ callback(enosys());
86
+ },
87
+ readlink(path, callback) {
88
+ callback(enosys());
89
+ },
90
+ rename(from, to, callback) {
91
+ callback(enosys());
92
+ },
93
+ rmdir(path, callback) {
94
+ callback(enosys());
95
+ },
96
+ stat(path, callback) {
97
+ callback(enosys());
98
+ },
99
+ symlink(path, link, callback) {
100
+ callback(enosys());
101
+ },
102
+ truncate(path, length, callback) {
103
+ callback(enosys());
104
+ },
105
+ unlink(path, callback) {
106
+ callback(enosys());
107
+ },
108
+ utimes(path, atime, mtime, callback) {
109
+ callback(enosys());
110
+ },
111
+ };
112
+ }
113
+
114
+ if (!globalThis.process) {
115
+ globalThis.process = {
116
+ getuid() {
117
+ return -1;
118
+ },
119
+ getgid() {
120
+ return -1;
121
+ },
122
+ geteuid() {
123
+ return -1;
124
+ },
125
+ getegid() {
126
+ return -1;
127
+ },
128
+ getgroups() {
129
+ throw enosys();
130
+ },
131
+ pid: -1,
132
+ ppid: -1,
133
+ umask() {
134
+ throw enosys();
135
+ },
136
+ cwd() {
137
+ throw enosys();
138
+ },
139
+ chdir() {
140
+ throw enosys();
141
+ },
142
+ };
143
+ }
144
+
145
+ if (!globalThis.crypto) {
146
+ throw new Error(
147
+ "globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)",
148
+ );
149
+ }
150
+
151
+ if (!globalThis.performance) {
152
+ throw new Error(
153
+ "globalThis.performance is not available, polyfill required (performance.now only)",
154
+ );
155
+ }
156
+
157
+ if (!globalThis.TextEncoder) {
158
+ throw new Error(
159
+ "globalThis.TextEncoder is not available, polyfill required",
160
+ );
161
+ }
162
+
163
+ if (!globalThis.TextDecoder) {
164
+ throw new Error(
165
+ "globalThis.TextDecoder is not available, polyfill required",
166
+ );
167
+ }
168
+
169
+ const encoder = new TextEncoder("utf-8");
170
+ const decoder = new TextDecoder("utf-8");
171
+
172
+ globalThis.Go = class {
173
+ constructor() {
174
+ this.argv = ["js"];
175
+ this.env = {};
176
+ this.exit = (code) => {
177
+ if (code !== 0) {
178
+ console.warn("exit code:", code);
179
+ }
180
+ };
181
+ this._exitPromise = new Promise((resolve) => {
182
+ this._resolveExitPromise = resolve;
183
+ });
184
+ this._pendingEvent = null;
185
+ this._scheduledTimeouts = new Map();
186
+ this._nextCallbackTimeoutID = 1;
187
+
188
+ const setInt64 = (addr, v) => {
189
+ this.mem.setUint32(addr + 0, v, true);
190
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
191
+ };
192
+
193
+ const setInt32 = (addr, v) => {
194
+ this.mem.setUint32(addr + 0, v, true);
195
+ };
196
+
197
+ const getInt64 = (addr) => {
198
+ const low = this.mem.getUint32(addr + 0, true);
199
+ const high = this.mem.getInt32(addr + 4, true);
200
+ return low + high * 4294967296;
201
+ };
202
+
203
+ const loadValue = (addr) => {
204
+ const f = this.mem.getFloat64(addr, true);
205
+ if (f === 0) {
206
+ return undefined;
207
+ }
208
+ if (!isNaN(f)) {
209
+ return f;
210
+ }
211
+
212
+ const id = this.mem.getUint32(addr, true);
213
+ return this._values[id];
214
+ };
215
+
216
+ const storeValue = (addr, v) => {
217
+ const nanHead = 0x7ff80000;
218
+
219
+ if (typeof v === "number" && v !== 0) {
220
+ if (isNaN(v)) {
221
+ this.mem.setUint32(addr + 4, nanHead, true);
222
+ this.mem.setUint32(addr, 0, true);
223
+ return;
224
+ }
225
+ this.mem.setFloat64(addr, v, true);
226
+ return;
227
+ }
228
+
229
+ if (v === undefined) {
230
+ this.mem.setFloat64(addr, 0, true);
231
+ return;
232
+ }
233
+
234
+ let id = this._ids.get(v);
235
+ if (id === undefined) {
236
+ id = this._idPool.pop();
237
+ if (id === undefined) {
238
+ id = this._values.length;
239
+ }
240
+ this._values[id] = v;
241
+ this._goRefCounts[id] = 0;
242
+ this._ids.set(v, id);
243
+ }
244
+ this._goRefCounts[id]++;
245
+ let typeFlag = 0;
246
+ switch (typeof v) {
247
+ case "object":
248
+ if (v !== null) {
249
+ typeFlag = 1;
250
+ }
251
+ break;
252
+ case "string":
253
+ typeFlag = 2;
254
+ break;
255
+ case "symbol":
256
+ typeFlag = 3;
257
+ break;
258
+ case "function":
259
+ typeFlag = 4;
260
+ break;
261
+ }
262
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
263
+ this.mem.setUint32(addr, id, true);
264
+ };
265
+
266
+ const loadSlice = (addr) => {
267
+ const array = getInt64(addr + 0);
268
+ const len = getInt64(addr + 8);
269
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
270
+ };
271
+
272
+ const loadSliceOfValues = (addr) => {
273
+ const array = getInt64(addr + 0);
274
+ const len = getInt64(addr + 8);
275
+ const a = new Array(len);
276
+ for (let i = 0; i < len; i++) {
277
+ a[i] = loadValue(array + i * 8);
278
+ }
279
+ return a;
280
+ };
281
+
282
+ const loadString = (addr) => {
283
+ const saddr = getInt64(addr + 0);
284
+ const len = getInt64(addr + 8);
285
+ return decoder.decode(
286
+ new DataView(this._inst.exports.mem.buffer, saddr, len),
287
+ );
288
+ };
289
+
290
+ const timeOrigin = Date.now() - performance.now();
291
+ this.importObject = {
292
+ _gotest: {
293
+ add: (a, b) => a + b,
294
+ },
295
+ gojs: {
296
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
297
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
298
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
299
+ // This changes the SP, thus we have to update the SP used by the imported function.
300
+
301
+ // func wasmExit(code int32)
302
+ "runtime.wasmExit": (sp) => {
303
+ sp >>>= 0;
304
+ const code = this.mem.getInt32(sp + 8, true);
305
+ this.exited = true;
306
+ delete this._inst;
307
+ delete this._values;
308
+ delete this._goRefCounts;
309
+ delete this._ids;
310
+ delete this._idPool;
311
+ this.exit(code);
312
+ },
313
+
314
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
315
+ "runtime.wasmWrite": (sp) => {
316
+ sp >>>= 0;
317
+ const fd = getInt64(sp + 8);
318
+ const p = getInt64(sp + 16);
319
+ const n = this.mem.getInt32(sp + 24, true);
320
+ fs.writeSync(
321
+ fd,
322
+ new Uint8Array(this._inst.exports.mem.buffer, p, n),
323
+ );
324
+ },
325
+
326
+ // func resetMemoryDataView()
327
+ "runtime.resetMemoryDataView": (sp) => {
328
+ sp >>>= 0;
329
+ this.mem = new DataView(this._inst.exports.mem.buffer);
330
+ },
331
+
332
+ // func nanotime1() int64
333
+ "runtime.nanotime1": (sp) => {
334
+ sp >>>= 0;
335
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
336
+ },
337
+
338
+ // func walltime() (sec int64, nsec int32)
339
+ "runtime.walltime": (sp) => {
340
+ sp >>>= 0;
341
+ const msec = new Date().getTime();
342
+ setInt64(sp + 8, msec / 1000);
343
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
344
+ },
345
+
346
+ // func scheduleTimeoutEvent(delay int64) int32
347
+ "runtime.scheduleTimeoutEvent": (sp) => {
348
+ sp >>>= 0;
349
+ const id = this._nextCallbackTimeoutID;
350
+ this._nextCallbackTimeoutID++;
351
+ this._scheduledTimeouts.set(
352
+ id,
353
+ setTimeout(
354
+ () => {
355
+ this._resume();
356
+ while (this._scheduledTimeouts.has(id)) {
357
+ // for some reason Go failed to register the timeout event, log and try again
358
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
359
+ console.warn("scheduleTimeoutEvent: missed timeout event");
360
+ this._resume();
361
+ }
362
+ },
363
+ getInt64(sp + 8),
364
+ ),
365
+ );
366
+ this.mem.setInt32(sp + 16, id, true);
367
+ },
368
+
369
+ // func clearTimeoutEvent(id int32)
370
+ "runtime.clearTimeoutEvent": (sp) => {
371
+ sp >>>= 0;
372
+ const id = this.mem.getInt32(sp + 8, true);
373
+ clearTimeout(this._scheduledTimeouts.get(id));
374
+ this._scheduledTimeouts.delete(id);
375
+ },
376
+
377
+ // func getRandomData(r []byte)
378
+ "runtime.getRandomData": (sp) => {
379
+ sp >>>= 0;
380
+ crypto.getRandomValues(loadSlice(sp + 8));
381
+ },
382
+
383
+ // func finalizeRef(v ref)
384
+ "syscall/js.finalizeRef": (sp) => {
385
+ sp >>>= 0;
386
+ const id = this.mem.getUint32(sp + 8, true);
387
+ this._goRefCounts[id]--;
388
+ if (this._goRefCounts[id] === 0) {
389
+ const v = this._values[id];
390
+ this._values[id] = null;
391
+ this._ids.delete(v);
392
+ this._idPool.push(id);
393
+ }
394
+ },
395
+
396
+ // func stringVal(value string) ref
397
+ "syscall/js.stringVal": (sp) => {
398
+ sp >>>= 0;
399
+ storeValue(sp + 24, loadString(sp + 8));
400
+ },
401
+
402
+ // func valueGet(v ref, p string) ref
403
+ "syscall/js.valueGet": (sp) => {
404
+ sp >>>= 0;
405
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
406
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
407
+ storeValue(sp + 32, result);
408
+ },
409
+
410
+ // func valueSet(v ref, p string, x ref)
411
+ "syscall/js.valueSet": (sp) => {
412
+ sp >>>= 0;
413
+ Reflect.set(
414
+ loadValue(sp + 8),
415
+ loadString(sp + 16),
416
+ loadValue(sp + 32),
417
+ );
418
+ },
419
+
420
+ // func valueDelete(v ref, p string)
421
+ "syscall/js.valueDelete": (sp) => {
422
+ sp >>>= 0;
423
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
424
+ },
425
+
426
+ // func valueIndex(v ref, i int) ref
427
+ "syscall/js.valueIndex": (sp) => {
428
+ sp >>>= 0;
429
+ storeValue(
430
+ sp + 24,
431
+ Reflect.get(loadValue(sp + 8), getInt64(sp + 16)),
432
+ );
433
+ },
434
+
435
+ // valueSetIndex(v ref, i int, x ref)
436
+ "syscall/js.valueSetIndex": (sp) => {
437
+ sp >>>= 0;
438
+ Reflect.set(
439
+ loadValue(sp + 8),
440
+ getInt64(sp + 16),
441
+ loadValue(sp + 24),
442
+ );
443
+ },
444
+
445
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
446
+ "syscall/js.valueCall": (sp) => {
447
+ sp >>>= 0;
448
+ try {
449
+ const v = loadValue(sp + 8);
450
+ const m = Reflect.get(v, loadString(sp + 16));
451
+ const args = loadSliceOfValues(sp + 32);
452
+ const result = Reflect.apply(m, v, args);
453
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
454
+ storeValue(sp + 56, result);
455
+ this.mem.setUint8(sp + 64, 1);
456
+ } catch (err) {
457
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
458
+ storeValue(sp + 56, err);
459
+ this.mem.setUint8(sp + 64, 0);
460
+ }
461
+ },
462
+
463
+ // func valueInvoke(v ref, args []ref) (ref, bool)
464
+ "syscall/js.valueInvoke": (sp) => {
465
+ sp >>>= 0;
466
+ try {
467
+ const v = loadValue(sp + 8);
468
+ const args = loadSliceOfValues(sp + 16);
469
+ const result = Reflect.apply(v, undefined, args);
470
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
471
+ storeValue(sp + 40, result);
472
+ this.mem.setUint8(sp + 48, 1);
473
+ } catch (err) {
474
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
475
+ storeValue(sp + 40, err);
476
+ this.mem.setUint8(sp + 48, 0);
477
+ }
478
+ },
479
+
480
+ // func valueNew(v ref, args []ref) (ref, bool)
481
+ "syscall/js.valueNew": (sp) => {
482
+ sp >>>= 0;
483
+ try {
484
+ const v = loadValue(sp + 8);
485
+ const args = loadSliceOfValues(sp + 16);
486
+ const result = Reflect.construct(v, args);
487
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
488
+ storeValue(sp + 40, result);
489
+ this.mem.setUint8(sp + 48, 1);
490
+ } catch (err) {
491
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
492
+ storeValue(sp + 40, err);
493
+ this.mem.setUint8(sp + 48, 0);
494
+ }
495
+ },
496
+
497
+ // func valueLength(v ref) int
498
+ "syscall/js.valueLength": (sp) => {
499
+ sp >>>= 0;
500
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
501
+ },
502
+
503
+ // valuePrepareString(v ref) (ref, int)
504
+ "syscall/js.valuePrepareString": (sp) => {
505
+ sp >>>= 0;
506
+ const str = encoder.encode(String(loadValue(sp + 8)));
507
+ storeValue(sp + 16, str);
508
+ setInt64(sp + 24, str.length);
509
+ },
510
+
511
+ // valueLoadString(v ref, b []byte)
512
+ "syscall/js.valueLoadString": (sp) => {
513
+ sp >>>= 0;
514
+ const str = loadValue(sp + 8);
515
+ loadSlice(sp + 16).set(str);
516
+ },
517
+
518
+ // func valueInstanceOf(v ref, t ref) bool
519
+ "syscall/js.valueInstanceOf": (sp) => {
520
+ sp >>>= 0;
521
+ this.mem.setUint8(
522
+ sp + 24,
523
+ loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0,
524
+ );
525
+ },
526
+
527
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
528
+ "syscall/js.copyBytesToGo": (sp) => {
529
+ sp >>>= 0;
530
+ const dst = loadSlice(sp + 8);
531
+ const src = loadValue(sp + 32);
532
+ if (
533
+ !(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
534
+ ) {
535
+ this.mem.setUint8(sp + 48, 0);
536
+ return;
537
+ }
538
+ const toCopy = src.subarray(0, dst.length);
539
+ dst.set(toCopy);
540
+ setInt64(sp + 40, toCopy.length);
541
+ this.mem.setUint8(sp + 48, 1);
542
+ },
543
+
544
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
545
+ "syscall/js.copyBytesToJS": (sp) => {
546
+ sp >>>= 0;
547
+ const dst = loadValue(sp + 8);
548
+ const src = loadSlice(sp + 16);
549
+ if (
550
+ !(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
551
+ ) {
552
+ this.mem.setUint8(sp + 48, 0);
553
+ return;
554
+ }
555
+ const toCopy = src.subarray(0, dst.length);
556
+ dst.set(toCopy);
557
+ setInt64(sp + 40, toCopy.length);
558
+ this.mem.setUint8(sp + 48, 1);
559
+ },
560
+
561
+ debug: (value) => {
562
+ console.log(value);
563
+ },
564
+ },
565
+ };
566
+ }
567
+
568
+ async run(instance) {
569
+ if (!(instance instanceof WebAssembly.Instance)) {
570
+ throw new Error("Go.run: WebAssembly.Instance expected");
571
+ }
572
+ this._inst = instance;
573
+ this.mem = new DataView(this._inst.exports.mem.buffer);
574
+ this._values = [
575
+ // JS values that Go currently has references to, indexed by reference id
576
+ NaN,
577
+ 0,
578
+ null,
579
+ true,
580
+ false,
581
+ globalThis,
582
+ this,
583
+ ];
584
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
585
+ this._ids = new Map([
586
+ // mapping from JS values to reference ids
587
+ [0, 1],
588
+ [null, 2],
589
+ [true, 3],
590
+ [false, 4],
591
+ [globalThis, 5],
592
+ [this, 6],
593
+ ]);
594
+ this._idPool = []; // unused ids that have been garbage collected
595
+ this.exited = false; // whether the Go program has exited
596
+
597
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
598
+ let offset = 4096;
599
+
600
+ const strPtr = (str) => {
601
+ const ptr = offset;
602
+ const bytes = encoder.encode(str + "\0");
603
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
604
+ offset += bytes.length;
605
+ if (offset % 8 !== 0) {
606
+ offset += 8 - (offset % 8);
607
+ }
608
+ return ptr;
609
+ };
610
+
611
+ const argc = this.argv.length;
612
+
613
+ const argvPtrs = [];
614
+ this.argv.forEach((arg) => {
615
+ argvPtrs.push(strPtr(arg));
616
+ });
617
+ argvPtrs.push(0);
618
+
619
+ const keys = Object.keys(this.env).sort();
620
+ keys.forEach((key) => {
621
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
622
+ });
623
+ argvPtrs.push(0);
624
+
625
+ const argv = offset;
626
+ argvPtrs.forEach((ptr) => {
627
+ this.mem.setUint32(offset, ptr, true);
628
+ this.mem.setUint32(offset + 4, 0, true);
629
+ offset += 8;
630
+ });
631
+
632
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
633
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
634
+ const wasmMinDataAddr = 4096 + 8192;
635
+ if (offset >= wasmMinDataAddr) {
636
+ throw new Error(
637
+ "total length of command line and environment variables exceeds limit",
638
+ );
639
+ }
640
+
641
+ this._inst.exports.run(argc, argv);
642
+ if (this.exited) {
643
+ this._resolveExitPromise();
644
+ }
645
+ await this._exitPromise;
646
+ }
647
+
648
+ _resume() {
649
+ if (this.exited) {
650
+ throw new Error("Go program has already exited");
651
+ }
652
+ this._inst.exports.resume();
653
+ if (this.exited) {
654
+ this._resolveExitPromise();
655
+ }
656
+ }
657
+
658
+ _makeFuncWrapper(id) {
659
+ const go = this;
660
+ return function () {
661
+ const event = { id: id, this: this, args: arguments };
662
+ go._pendingEvent = event;
663
+ go._resume();
664
+ return event.result;
665
+ };
666
+ }
667
+ };
668
+ })();
@@ -2,24 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSecureFetch = createSecureFetch;
4
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
5
  function createSecureFetch(baseURL, enclaveURL, hpkePublicKey, tlsPublicKeyFingerprint) {
8
6
  let fetchFunction;
9
7
  if (hpkePublicKey) {
10
8
  fetchFunction = (0, encrypted_body_fetch_1.createEncryptedBodyFetch)(baseURL, hpkePublicKey, enclaveURL);
11
9
  }
12
10
  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)(baseURL, tlsPublicKeyFingerprint);
11
+ throw new Error("HPKE public key not available and TLS-only verification is not supported in browsers. " +
12
+ "Only HPKE-enabled enclaves can be used in browser environments.");
23
13
  }
24
14
  return fetchFunction;
25
15
  }
package/package.json CHANGED
@@ -1,58 +1,64 @@
1
1
  {
2
2
  "name": "@zeke-02/tinfoil",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Tinfoil secure OpenAI client wrapper for Tauri v2",
5
5
  "main": "dist/index.js",
6
- "module": "dist/esm/index.js",
6
+ "module": "dist/esm/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
8
  "browser": {
9
- "./dist/index.js": "./dist/esm/index.browser.js",
10
- "./dist/esm/index.js": "./dist/esm/index.browser.js"
9
+ "./dist/index.js": "./dist/esm/index.browser.mjs",
10
+ "./dist/esm/index.mjs": "./dist/esm/index.browser.mjs"
11
11
  },
12
12
  "exports": {
13
13
  ".": {
14
14
  "types": "./dist/index.d.ts",
15
- "browser": "./dist/esm/index.browser.js",
16
- "import": "./dist/esm/index.js",
15
+ "browser": "./dist/esm/index.browser.mjs",
16
+ "import": "./dist/esm/index.mjs",
17
17
  "require": "./dist/index.js",
18
- "default": "./dist/index.js"
18
+ "default": "./dist/esm/index.browser.mjs"
19
19
  },
20
20
  "./client": {
21
21
  "types": "./dist/client.d.ts",
22
- "import": "./dist/esm/client.js",
22
+ "import": "./dist/esm/client.mjs",
23
23
  "require": "./dist/client.js"
24
24
  },
25
25
  "./ai-sdk-provider": {
26
26
  "types": "./dist/ai-sdk-provider.d.ts",
27
- "import": "./dist/esm/ai-sdk-provider.js",
27
+ "import": "./dist/esm/ai-sdk-provider.mjs",
28
28
  "require": "./dist/ai-sdk-provider.js"
29
29
  },
30
30
  "./verifier": {
31
31
  "types": "./dist/verifier.d.ts",
32
- "import": "./dist/esm/verifier.js",
32
+ "import": "./dist/esm/verifier.mjs",
33
33
  "require": "./dist/verifier.js"
34
34
  },
35
35
  "./config": {
36
36
  "types": "./dist/config.d.ts",
37
- "import": "./dist/esm/config.js",
37
+ "import": "./dist/esm/config.mjs",
38
38
  "require": "./dist/config.js"
39
39
  },
40
40
  "./secure-client": {
41
41
  "types": "./dist/secure-client.d.ts",
42
- "import": "./dist/esm/secure-client.js",
42
+ "import": "./dist/esm/secure-client.mjs",
43
43
  "require": "./dist/secure-client.js"
44
44
  },
45
45
  "./secure-fetch": {
46
46
  "types": "./dist/secure-fetch.d.ts",
47
- "browser": "./dist/esm/secure-fetch.browser.js",
48
- "import": "./dist/esm/secure-fetch.js",
49
- "require": "./dist/secure-fetch.js"
47
+ "browser": "./dist/esm/secure-fetch.browser.mjs",
48
+ "import": "./dist/esm/secure-fetch.mjs",
49
+ "require": "./dist/secure-fetch.js",
50
+ "default": "./dist/esm/secure-fetch.browser.mjs"
51
+ },
52
+ "./router": {
53
+ "types": "./dist/router.d.ts",
54
+ "import": "./dist/esm/router.mjs",
55
+ "require": "./dist/router.js"
50
56
  }
51
57
  },
52
58
  "scripts": {
53
59
  "build": "npm run build:cjs && npm run build:esm",
54
60
  "build:cjs": "tsc && cp src/wasm-exec.js dist/",
55
- "build:esm": "tsc -p tsconfig.esm.json && cp src/wasm-exec.js dist/esm/",
61
+ "build:esm": "tsc -p tsconfig.esm.json && node scripts/convert-to-mjs.js && cp src/wasm-exec.js dist/esm/",
56
62
  "test": "bash -lc 'rm -rf dist .node-tests && npm run build:cjs && tsc -p tsconfig.node-test.json && cp src/wasm-exec.js .node-tests/ && node --test $(find .node-tests -name \"*.test.js\" -print)'",
57
63
  "lint": "eslint src/**/*.ts",
58
64
  "prepublishOnly": "npm run build"
@@ -1,8 +0,0 @@
1
- // Browser-safe entry point: avoids Node built-ins
2
- export { TinfoilAI } from "./tinfoilai";
3
- export { TinfoilAI as default } from "./tinfoilai";
4
- export * from "./verifier";
5
- export * from "./ai-sdk-provider";
6
- export * from "./config";
7
- export { SecureClient } from "./secure-client";
8
- export { UnverifiedClient } from "./unverified-client";
@@ -1,22 +0,0 @@
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(baseURL, tlsPublicKeyFingerprint);
20
- }
21
- return fetchFunction;
22
- }
File without changes
File without changes