@byoky/sdk 0.1.0
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/index.cjs +243 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +214 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Byoky: () => Byoky,
|
|
24
|
+
ByokyError: () => import_core3.ByokyError,
|
|
25
|
+
ByokyErrorCode: () => import_core3.ByokyErrorCode,
|
|
26
|
+
createProxyFetch: () => createProxyFetch,
|
|
27
|
+
getStoreUrl: () => getStoreUrl,
|
|
28
|
+
isExtensionInstalled: () => isExtensionInstalled
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/byoky.ts
|
|
33
|
+
var import_core2 = require("@byoky/core");
|
|
34
|
+
|
|
35
|
+
// src/detect.ts
|
|
36
|
+
var import_core = require("@byoky/core");
|
|
37
|
+
function isExtensionInstalled() {
|
|
38
|
+
return typeof window !== "undefined" && import_core.BYOKY_PROVIDER_KEY in window;
|
|
39
|
+
}
|
|
40
|
+
function getStoreUrl() {
|
|
41
|
+
if (typeof navigator === "undefined") return null;
|
|
42
|
+
const ua = navigator.userAgent.toLowerCase();
|
|
43
|
+
if (ua.includes("chrome") && !ua.includes("edg")) {
|
|
44
|
+
return "https://chrome.google.com/webstore/detail/byoky/TODO_EXTENSION_ID";
|
|
45
|
+
}
|
|
46
|
+
if (ua.includes("firefox")) {
|
|
47
|
+
return "https://addons.mozilla.org/en-US/firefox/addon/byoky/";
|
|
48
|
+
}
|
|
49
|
+
if (ua.includes("safari") && !ua.includes("chrome")) {
|
|
50
|
+
return "https://apps.apple.com/app/byoky/TODO_APP_ID";
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/proxy-fetch.ts
|
|
56
|
+
function createProxyFetch(providerId, sessionKey) {
|
|
57
|
+
return async (input, init) => {
|
|
58
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
59
|
+
const method = init?.method ?? "GET";
|
|
60
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
61
|
+
const body = init?.body ? await readBody(init.body) : void 0;
|
|
62
|
+
const requestId = crypto.randomUUID();
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const { readable, writable } = new TransformStream();
|
|
65
|
+
const writer = writable.getWriter();
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
let resolved = false;
|
|
68
|
+
const timeout = setTimeout(() => {
|
|
69
|
+
cleanup();
|
|
70
|
+
reject(new Error("Proxy request timed out"));
|
|
71
|
+
}, 12e4);
|
|
72
|
+
function handleMessage(event) {
|
|
73
|
+
if (event.source !== window) return;
|
|
74
|
+
const data = event.data;
|
|
75
|
+
if (data?.requestId !== requestId) return;
|
|
76
|
+
switch (data.type) {
|
|
77
|
+
case "BYOKY_PROXY_RESPONSE_META":
|
|
78
|
+
if (!resolved) {
|
|
79
|
+
resolved = true;
|
|
80
|
+
clearTimeout(timeout);
|
|
81
|
+
resolve(
|
|
82
|
+
new Response(readable, {
|
|
83
|
+
status: data.status,
|
|
84
|
+
statusText: data.statusText,
|
|
85
|
+
headers: new Headers(data.headers)
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
case "BYOKY_PROXY_RESPONSE_CHUNK":
|
|
91
|
+
writer.write(encoder.encode(data.chunk)).catch(() => {
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
case "BYOKY_PROXY_RESPONSE_DONE":
|
|
95
|
+
writer.close().catch(() => {
|
|
96
|
+
});
|
|
97
|
+
cleanup();
|
|
98
|
+
break;
|
|
99
|
+
case "BYOKY_PROXY_RESPONSE_ERROR": {
|
|
100
|
+
const errResponse = new Response(
|
|
101
|
+
JSON.stringify({ error: data.error }),
|
|
102
|
+
{
|
|
103
|
+
status: data.status || 500,
|
|
104
|
+
headers: { "content-type": "application/json" }
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
if (!resolved) {
|
|
108
|
+
resolved = true;
|
|
109
|
+
clearTimeout(timeout);
|
|
110
|
+
resolve(errResponse);
|
|
111
|
+
}
|
|
112
|
+
writer.close().catch(() => {
|
|
113
|
+
});
|
|
114
|
+
cleanup();
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function cleanup() {
|
|
120
|
+
clearTimeout(timeout);
|
|
121
|
+
window.removeEventListener("message", handleMessage);
|
|
122
|
+
}
|
|
123
|
+
window.addEventListener("message", handleMessage);
|
|
124
|
+
window.postMessage(
|
|
125
|
+
{
|
|
126
|
+
type: "BYOKY_PROXY_REQUEST",
|
|
127
|
+
requestId,
|
|
128
|
+
sessionKey,
|
|
129
|
+
providerId,
|
|
130
|
+
url,
|
|
131
|
+
method,
|
|
132
|
+
headers,
|
|
133
|
+
body
|
|
134
|
+
},
|
|
135
|
+
"*"
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async function readBody(body) {
|
|
141
|
+
if (typeof body === "string") return body;
|
|
142
|
+
if (body instanceof ArrayBuffer) return new TextDecoder().decode(body);
|
|
143
|
+
if (body instanceof Blob) return body.text();
|
|
144
|
+
if (body instanceof URLSearchParams) return body.toString();
|
|
145
|
+
if (body instanceof ReadableStream) {
|
|
146
|
+
const reader = body.getReader();
|
|
147
|
+
const chunks = [];
|
|
148
|
+
for (; ; ) {
|
|
149
|
+
const { done, value } = await reader.read();
|
|
150
|
+
if (done) break;
|
|
151
|
+
chunks.push(value);
|
|
152
|
+
}
|
|
153
|
+
const total = chunks.reduce((acc, c) => acc + c.length, 0);
|
|
154
|
+
const combined = new Uint8Array(total);
|
|
155
|
+
let offset = 0;
|
|
156
|
+
for (const chunk of chunks) {
|
|
157
|
+
combined.set(chunk, offset);
|
|
158
|
+
offset += chunk.length;
|
|
159
|
+
}
|
|
160
|
+
return new TextDecoder().decode(combined);
|
|
161
|
+
}
|
|
162
|
+
return void 0;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// src/byoky.ts
|
|
166
|
+
var Byoky = class {
|
|
167
|
+
timeout;
|
|
168
|
+
constructor(options = {}) {
|
|
169
|
+
this.timeout = options.timeout ?? 6e4;
|
|
170
|
+
}
|
|
171
|
+
async connect(request = {}) {
|
|
172
|
+
if (!isExtensionInstalled()) {
|
|
173
|
+
const storeUrl = getStoreUrl();
|
|
174
|
+
if (storeUrl) {
|
|
175
|
+
window.open(storeUrl, "_blank");
|
|
176
|
+
}
|
|
177
|
+
throw import_core2.ByokyError.walletNotInstalled();
|
|
178
|
+
}
|
|
179
|
+
const response = await this.sendConnectRequest(request);
|
|
180
|
+
return {
|
|
181
|
+
...response,
|
|
182
|
+
createFetch: (providerId) => createProxyFetch(providerId, response.sessionKey),
|
|
183
|
+
disconnect: () => this.disconnect(response.sessionKey)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
sendConnectRequest(request) {
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
const requestId = crypto.randomUUID();
|
|
189
|
+
const timeoutId = setTimeout(() => {
|
|
190
|
+
cleanup();
|
|
191
|
+
reject(
|
|
192
|
+
new import_core2.ByokyError(import_core2.ByokyErrorCode.UNKNOWN, "Connection request timed out")
|
|
193
|
+
);
|
|
194
|
+
}, this.timeout);
|
|
195
|
+
function handleMessage(event) {
|
|
196
|
+
if (event.source !== window) return;
|
|
197
|
+
if (!(0, import_core2.isByokyMessage)(event.data)) return;
|
|
198
|
+
const msg = event.data;
|
|
199
|
+
if (msg.requestId !== requestId) return;
|
|
200
|
+
cleanup();
|
|
201
|
+
if (msg.type === "BYOKY_CONNECT_RESPONSE") {
|
|
202
|
+
resolve(msg.payload);
|
|
203
|
+
} else if (msg.type === "BYOKY_ERROR") {
|
|
204
|
+
const { code, message } = msg.payload;
|
|
205
|
+
reject(new import_core2.ByokyError(code, message));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function cleanup() {
|
|
209
|
+
clearTimeout(timeoutId);
|
|
210
|
+
window.removeEventListener("message", handleMessage);
|
|
211
|
+
}
|
|
212
|
+
window.addEventListener("message", handleMessage);
|
|
213
|
+
window.postMessage(
|
|
214
|
+
{
|
|
215
|
+
type: "BYOKY_CONNECT_REQUEST",
|
|
216
|
+
id: requestId,
|
|
217
|
+
requestId,
|
|
218
|
+
payload: request
|
|
219
|
+
},
|
|
220
|
+
"*"
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
disconnect(sessionKey) {
|
|
225
|
+
window.postMessage(
|
|
226
|
+
{ type: "BYOKY_DISCONNECT", payload: { sessionKey } },
|
|
227
|
+
"*"
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/index.ts
|
|
233
|
+
var import_core3 = require("@byoky/core");
|
|
234
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
235
|
+
0 && (module.exports = {
|
|
236
|
+
Byoky,
|
|
237
|
+
ByokyError,
|
|
238
|
+
ByokyErrorCode,
|
|
239
|
+
createProxyFetch,
|
|
240
|
+
getStoreUrl,
|
|
241
|
+
isExtensionInstalled
|
|
242
|
+
});
|
|
243
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/byoky.ts","../src/detect.ts","../src/proxy-fetch.ts"],"sourcesContent":["export { Byoky } from './byoky.js';\nexport type { ByokySession, ByokyOptions } from './byoky.js';\nexport { isExtensionInstalled, getStoreUrl } from './detect.js';\nexport { createProxyFetch } from './proxy-fetch.js';\nexport {\n type ConnectRequest,\n type ConnectResponse,\n type ProviderRequirement,\n ByokyError,\n ByokyErrorCode,\n} from '@byoky/core';\n","import type { ConnectRequest, ConnectResponse } from '@byoky/core';\nimport { ByokyError, ByokyErrorCode, isByokyMessage } from '@byoky/core';\nimport { isExtensionInstalled, getStoreUrl } from './detect.js';\nimport { createProxyFetch } from './proxy-fetch.js';\n\nexport interface ByokySession extends ConnectResponse {\n createFetch(providerId: string): typeof fetch;\n disconnect(): void;\n}\n\nexport interface ByokyOptions {\n timeout?: number;\n}\n\nexport class Byoky {\n private timeout: number;\n\n constructor(options: ByokyOptions = {}) {\n this.timeout = options.timeout ?? 60_000;\n }\n\n async connect(request: ConnectRequest = {}): Promise<ByokySession> {\n if (!isExtensionInstalled()) {\n const storeUrl = getStoreUrl();\n if (storeUrl) {\n window.open(storeUrl, '_blank');\n }\n throw ByokyError.walletNotInstalled();\n }\n\n const response = await this.sendConnectRequest(request);\n\n return {\n ...response,\n createFetch: (providerId: string) =>\n createProxyFetch(providerId, response.sessionKey),\n disconnect: () => this.disconnect(response.sessionKey),\n };\n }\n\n private sendConnectRequest(\n request: ConnectRequest,\n ): Promise<ConnectResponse> {\n return new Promise<ConnectResponse>((resolve, reject) => {\n const requestId = crypto.randomUUID();\n\n const timeoutId = setTimeout(() => {\n cleanup();\n reject(\n new ByokyError(ByokyErrorCode.UNKNOWN, 'Connection request timed out'),\n );\n }, this.timeout);\n\n function handleMessage(event: MessageEvent) {\n if (event.source !== window) return;\n if (!isByokyMessage(event.data)) return;\n\n const msg = event.data;\n if (msg.requestId !== requestId) return;\n\n cleanup();\n\n if (msg.type === 'BYOKY_CONNECT_RESPONSE') {\n resolve(msg.payload as ConnectResponse);\n } else if (msg.type === 'BYOKY_ERROR') {\n const { code, message } = msg.payload as {\n code: string;\n message: string;\n };\n reject(new ByokyError(code as ByokyErrorCode, message));\n }\n }\n\n function cleanup() {\n clearTimeout(timeoutId);\n window.removeEventListener('message', handleMessage);\n }\n\n window.addEventListener('message', handleMessage);\n\n window.postMessage(\n {\n type: 'BYOKY_CONNECT_REQUEST',\n id: requestId,\n requestId,\n payload: request,\n },\n '*',\n );\n });\n }\n\n private disconnect(sessionKey: string): void {\n window.postMessage(\n { type: 'BYOKY_DISCONNECT', payload: { sessionKey } },\n '*',\n );\n }\n}\n","import { BYOKY_PROVIDER_KEY } from '@byoky/core';\n\nexport function isExtensionInstalled(): boolean {\n return typeof window !== 'undefined' && BYOKY_PROVIDER_KEY in window;\n}\n\nexport function getStoreUrl(): string | null {\n if (typeof navigator === 'undefined') return null;\n\n const ua = navigator.userAgent.toLowerCase();\n\n if (ua.includes('chrome') && !ua.includes('edg')) {\n return 'https://chrome.google.com/webstore/detail/byoky/TODO_EXTENSION_ID';\n }\n if (ua.includes('firefox')) {\n return 'https://addons.mozilla.org/en-US/firefox/addon/byoky/';\n }\n if (ua.includes('safari') && !ua.includes('chrome')) {\n return 'https://apps.apple.com/app/byoky/TODO_APP_ID';\n }\n return null;\n}\n","export function createProxyFetch(\n providerId: string,\n sessionKey: string,\n): typeof fetch {\n return async (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> => {\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n\n const method = init?.method ?? 'GET';\n const headers = init?.headers\n ? Object.fromEntries(new Headers(init.headers).entries())\n : {};\n const body = init?.body ? await readBody(init.body) : undefined;\n\n const requestId = crypto.randomUUID();\n\n return new Promise<Response>((resolve, reject) => {\n const { readable, writable } = new TransformStream<Uint8Array>();\n const writer = writable.getWriter();\n const encoder = new TextEncoder();\n let resolved = false;\n\n const timeout = setTimeout(() => {\n cleanup();\n reject(new Error('Proxy request timed out'));\n }, 120_000);\n\n function handleMessage(event: MessageEvent) {\n if (event.source !== window) return;\n const data = event.data;\n if (data?.requestId !== requestId) return;\n\n switch (data.type) {\n case 'BYOKY_PROXY_RESPONSE_META':\n if (!resolved) {\n resolved = true;\n clearTimeout(timeout);\n resolve(\n new Response(readable, {\n status: data.status,\n statusText: data.statusText,\n headers: new Headers(data.headers),\n }),\n );\n }\n break;\n\n case 'BYOKY_PROXY_RESPONSE_CHUNK':\n writer.write(encoder.encode(data.chunk)).catch(() => {});\n break;\n\n case 'BYOKY_PROXY_RESPONSE_DONE':\n writer.close().catch(() => {});\n cleanup();\n break;\n\n case 'BYOKY_PROXY_RESPONSE_ERROR': {\n const errResponse = new Response(\n JSON.stringify({ error: data.error }),\n {\n status: data.status || 500,\n headers: { 'content-type': 'application/json' },\n },\n );\n if (!resolved) {\n resolved = true;\n clearTimeout(timeout);\n resolve(errResponse);\n }\n writer.close().catch(() => {});\n cleanup();\n break;\n }\n }\n }\n\n function cleanup() {\n clearTimeout(timeout);\n window.removeEventListener('message', handleMessage);\n }\n\n window.addEventListener('message', handleMessage);\n\n window.postMessage(\n {\n type: 'BYOKY_PROXY_REQUEST',\n requestId,\n sessionKey,\n providerId,\n url,\n method,\n headers,\n body,\n },\n '*',\n );\n });\n };\n}\n\nasync function readBody(body: BodyInit): Promise<string | undefined> {\n if (typeof body === 'string') return body;\n if (body instanceof ArrayBuffer) return new TextDecoder().decode(body);\n if (body instanceof Blob) return body.text();\n if (body instanceof URLSearchParams) return body.toString();\n if (body instanceof ReadableStream) {\n const reader = body.getReader();\n const chunks: Uint8Array[] = [];\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n }\n const total = chunks.reduce((acc, c) => acc + c.length, 0);\n const combined = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n combined.set(chunk, offset);\n offset += chunk.length;\n }\n return new TextDecoder().decode(combined);\n }\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,IAAAA,eAA2D;;;ACD3D,kBAAmC;AAE5B,SAAS,uBAAgC;AAC9C,SAAO,OAAO,WAAW,eAAe,kCAAsB;AAChE;AAEO,SAAS,cAA6B;AAC3C,MAAI,OAAO,cAAc,YAAa,QAAO;AAE7C,QAAM,KAAK,UAAU,UAAU,YAAY;AAE3C,MAAI,GAAG,SAAS,QAAQ,KAAK,CAAC,GAAG,SAAS,KAAK,GAAG;AAChD,WAAO;AAAA,EACT;AACA,MAAI,GAAG,SAAS,SAAS,GAAG;AAC1B,WAAO;AAAA,EACT;AACA,MAAI,GAAG,SAAS,QAAQ,KAAK,CAAC,GAAG,SAAS,QAAQ,GAAG;AACnD,WAAO;AAAA,EACT;AACA,SAAO;AACT;;;ACrBO,SAAS,iBACd,YACA,YACc;AACd,SAAO,OACL,OACA,SACsB;AACtB,UAAM,MACJ,OAAO,UAAU,WACb,QACA,iBAAiB,MACf,MAAM,SAAS,IACf,MAAM;AAEd,UAAM,SAAS,MAAM,UAAU;AAC/B,UAAM,UAAU,MAAM,UAClB,OAAO,YAA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AAEhD,aAAO;AAAA,QACL;AAAA,UACE,MAAM;AAAA,UACN,IAAI;AAAA,UACJ;AAAA,UACA,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,WAAW,YAA0B;AAC3C,WAAO;AAAA,MACL,EAAE,MAAM,oBAAoB,SAAS,EAAE,WAAW,EAAE;AAAA,MACpD;AAAA,IACF;AAAA,EACF;AACF;;;AD9FA,IAAAC,eAMO;","names":["import_core","import_core"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse } from '@byoky/core';
|
|
2
|
+
export { ByokyError, ByokyErrorCode, ConnectRequest, ConnectResponse, ProviderRequirement } from '@byoky/core';
|
|
3
|
+
|
|
4
|
+
interface ByokySession extends ConnectResponse {
|
|
5
|
+
createFetch(providerId: string): typeof fetch;
|
|
6
|
+
disconnect(): void;
|
|
7
|
+
}
|
|
8
|
+
interface ByokyOptions {
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
declare class Byoky {
|
|
12
|
+
private timeout;
|
|
13
|
+
constructor(options?: ByokyOptions);
|
|
14
|
+
connect(request?: ConnectRequest): Promise<ByokySession>;
|
|
15
|
+
private sendConnectRequest;
|
|
16
|
+
private disconnect;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare function isExtensionInstalled(): boolean;
|
|
20
|
+
declare function getStoreUrl(): string | null;
|
|
21
|
+
|
|
22
|
+
declare function createProxyFetch(providerId: string, sessionKey: string): typeof fetch;
|
|
23
|
+
|
|
24
|
+
export { Byoky, type ByokyOptions, type ByokySession, createProxyFetch, getStoreUrl, isExtensionInstalled };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ConnectRequest, ConnectResponse } from '@byoky/core';
|
|
2
|
+
export { ByokyError, ByokyErrorCode, ConnectRequest, ConnectResponse, ProviderRequirement } from '@byoky/core';
|
|
3
|
+
|
|
4
|
+
interface ByokySession extends ConnectResponse {
|
|
5
|
+
createFetch(providerId: string): typeof fetch;
|
|
6
|
+
disconnect(): void;
|
|
7
|
+
}
|
|
8
|
+
interface ByokyOptions {
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
declare class Byoky {
|
|
12
|
+
private timeout;
|
|
13
|
+
constructor(options?: ByokyOptions);
|
|
14
|
+
connect(request?: ConnectRequest): Promise<ByokySession>;
|
|
15
|
+
private sendConnectRequest;
|
|
16
|
+
private disconnect;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare function isExtensionInstalled(): boolean;
|
|
20
|
+
declare function getStoreUrl(): string | null;
|
|
21
|
+
|
|
22
|
+
declare function createProxyFetch(providerId: string, sessionKey: string): typeof fetch;
|
|
23
|
+
|
|
24
|
+
export { Byoky, type ByokyOptions, type ByokySession, createProxyFetch, getStoreUrl, isExtensionInstalled };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// src/byoky.ts
|
|
2
|
+
import { ByokyError, ByokyErrorCode, isByokyMessage } from "@byoky/core";
|
|
3
|
+
|
|
4
|
+
// src/detect.ts
|
|
5
|
+
import { BYOKY_PROVIDER_KEY } from "@byoky/core";
|
|
6
|
+
function isExtensionInstalled() {
|
|
7
|
+
return typeof window !== "undefined" && BYOKY_PROVIDER_KEY in window;
|
|
8
|
+
}
|
|
9
|
+
function getStoreUrl() {
|
|
10
|
+
if (typeof navigator === "undefined") return null;
|
|
11
|
+
const ua = navigator.userAgent.toLowerCase();
|
|
12
|
+
if (ua.includes("chrome") && !ua.includes("edg")) {
|
|
13
|
+
return "https://chrome.google.com/webstore/detail/byoky/TODO_EXTENSION_ID";
|
|
14
|
+
}
|
|
15
|
+
if (ua.includes("firefox")) {
|
|
16
|
+
return "https://addons.mozilla.org/en-US/firefox/addon/byoky/";
|
|
17
|
+
}
|
|
18
|
+
if (ua.includes("safari") && !ua.includes("chrome")) {
|
|
19
|
+
return "https://apps.apple.com/app/byoky/TODO_APP_ID";
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// src/proxy-fetch.ts
|
|
25
|
+
function createProxyFetch(providerId, sessionKey) {
|
|
26
|
+
return async (input, init) => {
|
|
27
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
28
|
+
const method = init?.method ?? "GET";
|
|
29
|
+
const headers = init?.headers ? Object.fromEntries(new Headers(init.headers).entries()) : {};
|
|
30
|
+
const body = init?.body ? await readBody(init.body) : void 0;
|
|
31
|
+
const requestId = crypto.randomUUID();
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const { readable, writable } = new TransformStream();
|
|
34
|
+
const writer = writable.getWriter();
|
|
35
|
+
const encoder = new TextEncoder();
|
|
36
|
+
let resolved = false;
|
|
37
|
+
const timeout = setTimeout(() => {
|
|
38
|
+
cleanup();
|
|
39
|
+
reject(new Error("Proxy request timed out"));
|
|
40
|
+
}, 12e4);
|
|
41
|
+
function handleMessage(event) {
|
|
42
|
+
if (event.source !== window) return;
|
|
43
|
+
const data = event.data;
|
|
44
|
+
if (data?.requestId !== requestId) return;
|
|
45
|
+
switch (data.type) {
|
|
46
|
+
case "BYOKY_PROXY_RESPONSE_META":
|
|
47
|
+
if (!resolved) {
|
|
48
|
+
resolved = true;
|
|
49
|
+
clearTimeout(timeout);
|
|
50
|
+
resolve(
|
|
51
|
+
new Response(readable, {
|
|
52
|
+
status: data.status,
|
|
53
|
+
statusText: data.statusText,
|
|
54
|
+
headers: new Headers(data.headers)
|
|
55
|
+
})
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case "BYOKY_PROXY_RESPONSE_CHUNK":
|
|
60
|
+
writer.write(encoder.encode(data.chunk)).catch(() => {
|
|
61
|
+
});
|
|
62
|
+
break;
|
|
63
|
+
case "BYOKY_PROXY_RESPONSE_DONE":
|
|
64
|
+
writer.close().catch(() => {
|
|
65
|
+
});
|
|
66
|
+
cleanup();
|
|
67
|
+
break;
|
|
68
|
+
case "BYOKY_PROXY_RESPONSE_ERROR": {
|
|
69
|
+
const errResponse = new Response(
|
|
70
|
+
JSON.stringify({ error: data.error }),
|
|
71
|
+
{
|
|
72
|
+
status: data.status || 500,
|
|
73
|
+
headers: { "content-type": "application/json" }
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
if (!resolved) {
|
|
77
|
+
resolved = true;
|
|
78
|
+
clearTimeout(timeout);
|
|
79
|
+
resolve(errResponse);
|
|
80
|
+
}
|
|
81
|
+
writer.close().catch(() => {
|
|
82
|
+
});
|
|
83
|
+
cleanup();
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function cleanup() {
|
|
89
|
+
clearTimeout(timeout);
|
|
90
|
+
window.removeEventListener("message", handleMessage);
|
|
91
|
+
}
|
|
92
|
+
window.addEventListener("message", handleMessage);
|
|
93
|
+
window.postMessage(
|
|
94
|
+
{
|
|
95
|
+
type: "BYOKY_PROXY_REQUEST",
|
|
96
|
+
requestId,
|
|
97
|
+
sessionKey,
|
|
98
|
+
providerId,
|
|
99
|
+
url,
|
|
100
|
+
method,
|
|
101
|
+
headers,
|
|
102
|
+
body
|
|
103
|
+
},
|
|
104
|
+
"*"
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async function readBody(body) {
|
|
110
|
+
if (typeof body === "string") return body;
|
|
111
|
+
if (body instanceof ArrayBuffer) return new TextDecoder().decode(body);
|
|
112
|
+
if (body instanceof Blob) return body.text();
|
|
113
|
+
if (body instanceof URLSearchParams) return body.toString();
|
|
114
|
+
if (body instanceof ReadableStream) {
|
|
115
|
+
const reader = body.getReader();
|
|
116
|
+
const chunks = [];
|
|
117
|
+
for (; ; ) {
|
|
118
|
+
const { done, value } = await reader.read();
|
|
119
|
+
if (done) break;
|
|
120
|
+
chunks.push(value);
|
|
121
|
+
}
|
|
122
|
+
const total = chunks.reduce((acc, c) => acc + c.length, 0);
|
|
123
|
+
const combined = new Uint8Array(total);
|
|
124
|
+
let offset = 0;
|
|
125
|
+
for (const chunk of chunks) {
|
|
126
|
+
combined.set(chunk, offset);
|
|
127
|
+
offset += chunk.length;
|
|
128
|
+
}
|
|
129
|
+
return new TextDecoder().decode(combined);
|
|
130
|
+
}
|
|
131
|
+
return void 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/byoky.ts
|
|
135
|
+
var Byoky = class {
|
|
136
|
+
timeout;
|
|
137
|
+
constructor(options = {}) {
|
|
138
|
+
this.timeout = options.timeout ?? 6e4;
|
|
139
|
+
}
|
|
140
|
+
async connect(request = {}) {
|
|
141
|
+
if (!isExtensionInstalled()) {
|
|
142
|
+
const storeUrl = getStoreUrl();
|
|
143
|
+
if (storeUrl) {
|
|
144
|
+
window.open(storeUrl, "_blank");
|
|
145
|
+
}
|
|
146
|
+
throw ByokyError.walletNotInstalled();
|
|
147
|
+
}
|
|
148
|
+
const response = await this.sendConnectRequest(request);
|
|
149
|
+
return {
|
|
150
|
+
...response,
|
|
151
|
+
createFetch: (providerId) => createProxyFetch(providerId, response.sessionKey),
|
|
152
|
+
disconnect: () => this.disconnect(response.sessionKey)
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
sendConnectRequest(request) {
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const requestId = crypto.randomUUID();
|
|
158
|
+
const timeoutId = setTimeout(() => {
|
|
159
|
+
cleanup();
|
|
160
|
+
reject(
|
|
161
|
+
new ByokyError(ByokyErrorCode.UNKNOWN, "Connection request timed out")
|
|
162
|
+
);
|
|
163
|
+
}, this.timeout);
|
|
164
|
+
function handleMessage(event) {
|
|
165
|
+
if (event.source !== window) return;
|
|
166
|
+
if (!isByokyMessage(event.data)) return;
|
|
167
|
+
const msg = event.data;
|
|
168
|
+
if (msg.requestId !== requestId) return;
|
|
169
|
+
cleanup();
|
|
170
|
+
if (msg.type === "BYOKY_CONNECT_RESPONSE") {
|
|
171
|
+
resolve(msg.payload);
|
|
172
|
+
} else if (msg.type === "BYOKY_ERROR") {
|
|
173
|
+
const { code, message } = msg.payload;
|
|
174
|
+
reject(new ByokyError(code, message));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function cleanup() {
|
|
178
|
+
clearTimeout(timeoutId);
|
|
179
|
+
window.removeEventListener("message", handleMessage);
|
|
180
|
+
}
|
|
181
|
+
window.addEventListener("message", handleMessage);
|
|
182
|
+
window.postMessage(
|
|
183
|
+
{
|
|
184
|
+
type: "BYOKY_CONNECT_REQUEST",
|
|
185
|
+
id: requestId,
|
|
186
|
+
requestId,
|
|
187
|
+
payload: request
|
|
188
|
+
},
|
|
189
|
+
"*"
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
disconnect(sessionKey) {
|
|
194
|
+
window.postMessage(
|
|
195
|
+
{ type: "BYOKY_DISCONNECT", payload: { sessionKey } },
|
|
196
|
+
"*"
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// src/index.ts
|
|
202
|
+
import {
|
|
203
|
+
ByokyError as ByokyError2,
|
|
204
|
+
ByokyErrorCode as ByokyErrorCode2
|
|
205
|
+
} from "@byoky/core";
|
|
206
|
+
export {
|
|
207
|
+
Byoky,
|
|
208
|
+
ByokyError2 as ByokyError,
|
|
209
|
+
ByokyErrorCode2 as ByokyErrorCode,
|
|
210
|
+
createProxyFetch,
|
|
211
|
+
getStoreUrl,
|
|
212
|
+
isExtensionInstalled
|
|
213
|
+
};
|
|
214
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/byoky.ts","../src/detect.ts","../src/proxy-fetch.ts","../src/index.ts"],"sourcesContent":["import type { ConnectRequest, ConnectResponse } from '@byoky/core';\nimport { ByokyError, ByokyErrorCode, isByokyMessage } from '@byoky/core';\nimport { isExtensionInstalled, getStoreUrl } from './detect.js';\nimport { createProxyFetch } from './proxy-fetch.js';\n\nexport interface ByokySession extends ConnectResponse {\n createFetch(providerId: string): typeof fetch;\n disconnect(): void;\n}\n\nexport interface ByokyOptions {\n timeout?: number;\n}\n\nexport class Byoky {\n private timeout: number;\n\n constructor(options: ByokyOptions = {}) {\n this.timeout = options.timeout ?? 60_000;\n }\n\n async connect(request: ConnectRequest = {}): Promise<ByokySession> {\n if (!isExtensionInstalled()) {\n const storeUrl = getStoreUrl();\n if (storeUrl) {\n window.open(storeUrl, '_blank');\n }\n throw ByokyError.walletNotInstalled();\n }\n\n const response = await this.sendConnectRequest(request);\n\n return {\n ...response,\n createFetch: (providerId: string) =>\n createProxyFetch(providerId, response.sessionKey),\n disconnect: () => this.disconnect(response.sessionKey),\n };\n }\n\n private sendConnectRequest(\n request: ConnectRequest,\n ): Promise<ConnectResponse> {\n return new Promise<ConnectResponse>((resolve, reject) => {\n const requestId = crypto.randomUUID();\n\n const timeoutId = setTimeout(() => {\n cleanup();\n reject(\n new ByokyError(ByokyErrorCode.UNKNOWN, 'Connection request timed out'),\n );\n }, this.timeout);\n\n function handleMessage(event: MessageEvent) {\n if (event.source !== window) return;\n if (!isByokyMessage(event.data)) return;\n\n const msg = event.data;\n if (msg.requestId !== requestId) return;\n\n cleanup();\n\n if (msg.type === 'BYOKY_CONNECT_RESPONSE') {\n resolve(msg.payload as ConnectResponse);\n } else if (msg.type === 'BYOKY_ERROR') {\n const { code, message } = msg.payload as {\n code: string;\n message: string;\n };\n reject(new ByokyError(code as ByokyErrorCode, message));\n }\n }\n\n function cleanup() {\n clearTimeout(timeoutId);\n window.removeEventListener('message', handleMessage);\n }\n\n window.addEventListener('message', handleMessage);\n\n window.postMessage(\n {\n type: 'BYOKY_CONNECT_REQUEST',\n id: requestId,\n requestId,\n payload: request,\n },\n '*',\n );\n });\n }\n\n private disconnect(sessionKey: string): void {\n window.postMessage(\n { type: 'BYOKY_DISCONNECT', payload: { sessionKey } },\n '*',\n );\n }\n}\n","import { BYOKY_PROVIDER_KEY } from '@byoky/core';\n\nexport function isExtensionInstalled(): boolean {\n return typeof window !== 'undefined' && BYOKY_PROVIDER_KEY in window;\n}\n\nexport function getStoreUrl(): string | null {\n if (typeof navigator === 'undefined') return null;\n\n const ua = navigator.userAgent.toLowerCase();\n\n if (ua.includes('chrome') && !ua.includes('edg')) {\n return 'https://chrome.google.com/webstore/detail/byoky/TODO_EXTENSION_ID';\n }\n if (ua.includes('firefox')) {\n return 'https://addons.mozilla.org/en-US/firefox/addon/byoky/';\n }\n if (ua.includes('safari') && !ua.includes('chrome')) {\n return 'https://apps.apple.com/app/byoky/TODO_APP_ID';\n }\n return null;\n}\n","export function createProxyFetch(\n providerId: string,\n sessionKey: string,\n): typeof fetch {\n return async (\n input: RequestInfo | URL,\n init?: RequestInit,\n ): Promise<Response> => {\n const url =\n typeof input === 'string'\n ? input\n : input instanceof URL\n ? input.toString()\n : input.url;\n\n const method = init?.method ?? 'GET';\n const headers = init?.headers\n ? Object.fromEntries(new Headers(init.headers).entries())\n : {};\n const body = init?.body ? await readBody(init.body) : undefined;\n\n const requestId = crypto.randomUUID();\n\n return new Promise<Response>((resolve, reject) => {\n const { readable, writable } = new TransformStream<Uint8Array>();\n const writer = writable.getWriter();\n const encoder = new TextEncoder();\n let resolved = false;\n\n const timeout = setTimeout(() => {\n cleanup();\n reject(new Error('Proxy request timed out'));\n }, 120_000);\n\n function handleMessage(event: MessageEvent) {\n if (event.source !== window) return;\n const data = event.data;\n if (data?.requestId !== requestId) return;\n\n switch (data.type) {\n case 'BYOKY_PROXY_RESPONSE_META':\n if (!resolved) {\n resolved = true;\n clearTimeout(timeout);\n resolve(\n new Response(readable, {\n status: data.status,\n statusText: data.statusText,\n headers: new Headers(data.headers),\n }),\n );\n }\n break;\n\n case 'BYOKY_PROXY_RESPONSE_CHUNK':\n writer.write(encoder.encode(data.chunk)).catch(() => {});\n break;\n\n case 'BYOKY_PROXY_RESPONSE_DONE':\n writer.close().catch(() => {});\n cleanup();\n break;\n\n case 'BYOKY_PROXY_RESPONSE_ERROR': {\n const errResponse = new Response(\n JSON.stringify({ error: data.error }),\n {\n status: data.status || 500,\n headers: { 'content-type': 'application/json' },\n },\n );\n if (!resolved) {\n resolved = true;\n clearTimeout(timeout);\n resolve(errResponse);\n }\n writer.close().catch(() => {});\n cleanup();\n break;\n }\n }\n }\n\n function cleanup() {\n clearTimeout(timeout);\n window.removeEventListener('message', handleMessage);\n }\n\n window.addEventListener('message', handleMessage);\n\n window.postMessage(\n {\n type: 'BYOKY_PROXY_REQUEST',\n requestId,\n sessionKey,\n providerId,\n url,\n method,\n headers,\n body,\n },\n '*',\n );\n });\n };\n}\n\nasync function readBody(body: BodyInit): Promise<string | undefined> {\n if (typeof body === 'string') return body;\n if (body instanceof ArrayBuffer) return new TextDecoder().decode(body);\n if (body instanceof Blob) return body.text();\n if (body instanceof URLSearchParams) return body.toString();\n if (body instanceof ReadableStream) {\n const reader = body.getReader();\n const chunks: Uint8Array[] = [];\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n }\n const total = chunks.reduce((acc, c) => acc + c.length, 0);\n const combined = new Uint8Array(total);\n let offset = 0;\n for (const chunk of chunks) {\n combined.set(chunk, offset);\n offset += chunk.length;\n }\n return new TextDecoder().decode(combined);\n }\n return undefined;\n}\n","export { Byoky } from './byoky.js';\nexport type { ByokySession, ByokyOptions } from './byoky.js';\nexport { isExtensionInstalled, getStoreUrl } from './detect.js';\nexport { createProxyFetch } from './proxy-fetch.js';\nexport {\n type ConnectRequest,\n type ConnectResponse,\n type ProviderRequirement,\n ByokyError,\n ByokyErrorCode,\n} from '@byoky/core';\n"],"mappings":";AACA,SAAS,YAAY,gBAAgB,sBAAsB;;;ACD3D,SAAS,0BAA0B;AAE5B,SAAS,uBAAgC;AAC9C,SAAO,OAAO,WA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UACE,IAAI,WAAW,eAAe,SAAS,8BAA8B;AAAA,QACvE;AAAA,MACF,GAAG,KAAK,OAAO;AAEf,eAAS,cAAc,OAAqB;AAC1C,YAAI,MAAM,WAAW,OAAQ;AAC7B,YAAI,CAAC,eAAe,MAAM,IAAI,EAAG;AAEjC,cAAM,MAAM,MAAM;AAClB,YAAI,IAAI,cAAc,UAAW;AAEjC,gBAAQ;AAER,YAAI,IAAI,SAAS,0BAA0B;AACzC,kBAAQ,IAAI,OAA0B;AAAA,QACxC,WAAW,IAAI,SAAS,eAAe;AACrC,gBAAM,EAAE,MAAM,QAAQ,IAAI,IAAI;AAI9B,iBAAO,IAAI,WAAW,MAAwB,OAAO,CAAC;AAAA,QACxD;AAAA,MACF;AAEA,eAAS,UAAU;AACjB,qBAAa,SAAS;AACtB,eAAO,oBAAoB,WAAW,aAAa;AAAA,MACrD;AAEA,aAAO,iBAAiB,WAAW,aAAa;AAEhD,aAAO;AAAA,QACL;AAAA,UACE,MAAM;AAAA,UACN,IAAI;AAAA,UACJ;AAAA,UACA,SAAS;AAAA,QACX;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,WAAW,YAA0B;AAC3C,WAAO;AAAA,MACL,EAAE,MAAM,oBAAoB,SAAS,EAAE,WAAW,EAAE;AAAA,MACpD;AAAA,IACF;AAAA,EACF;AACF;;;AG9FA;AAAA,EAIE,cAAAA;AAAA,EACA,kBAAAC;AAAA,OACK;","names":["ByokyError","ByokyErrorCode"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@byoky/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist", "README.md", "LICENSE"],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup",
|
|
18
|
+
"typecheck": "tsc --noEmit",
|
|
19
|
+
"clean": "rm -rf dist"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/MichaelLod/byoky.git",
|
|
25
|
+
"directory": "packages/sdk"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://byoky.com",
|
|
28
|
+
"keywords": ["byoky", "ai", "api-keys", "wallet", "sdk", "llm", "openai", "anthropic", "gemini"],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@byoky/core": "workspace:*"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^5.7.0"
|
|
34
|
+
}
|
|
35
|
+
}
|