@kispi/chat 0.2.2 → 0.3.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/README.en.md +74 -48
- package/README.md +70 -47
- package/dist/{chunk-5QQRM7BH.js → chunk-WSBOTTBM.js} +21 -3
- package/dist/index.cjs +183 -46
- package/dist/index.d.cts +172 -328
- package/dist/index.d.ts +172 -328
- package/dist/index.js +166 -42
- package/dist/server/index.cjs +37 -9
- package/dist/server/index.d.cts +20 -14
- package/dist/server/index.d.ts +20 -14
- package/dist/server/index.js +22 -8
- package/package.json +1 -1
package/dist/server/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
ChatEndpoint,
|
|
3
|
+
errorFrom,
|
|
4
|
+
restURLFrom
|
|
5
|
+
} from "../chunk-WSBOTTBM.js";
|
|
4
6
|
|
|
5
7
|
// src/server/api.ts
|
|
6
8
|
var ServerRest = class {
|
|
@@ -178,17 +180,23 @@ function createChatServer(options) {
|
|
|
178
180
|
if (!options.secretKey.startsWith("sk_")) {
|
|
179
181
|
throw new TypeError("secretKey must be an sk_ key; a pk_ cannot sign tokens or reach server routes");
|
|
180
182
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
const keyId = keyIdFrom(options.secretKey);
|
|
184
|
+
const rest = new ServerRest(
|
|
185
|
+
restURLFrom(options.endpoint ?? ChatEndpoint),
|
|
186
|
+
options.secretKey,
|
|
187
|
+
options.fetch ?? globalThis.fetch.bind(globalThis)
|
|
188
|
+
);
|
|
185
189
|
const guestSecrets = options.guestSecret === void 0 ? [] : typeof options.guestSecret === "string" ? [options.guestSecret] : options.guestSecret;
|
|
186
190
|
for (const secret of guestSecrets) {
|
|
187
191
|
if (secret.length < MinGuestSecretLength) {
|
|
188
192
|
throw new RangeError(`guestSecret must be at least ${MinGuestSecretLength} characters`);
|
|
189
193
|
}
|
|
190
194
|
}
|
|
191
|
-
const sign = (input) => signHS256(
|
|
195
|
+
const sign = (input) => signHS256(
|
|
196
|
+
{ alg: "HS256", typ: "JWT", ...keyId === void 0 ? {} : { kid: keyId } },
|
|
197
|
+
tokenClaims(input, /* @__PURE__ */ new Date()),
|
|
198
|
+
options.secretKey
|
|
199
|
+
);
|
|
192
200
|
return {
|
|
193
201
|
token: sign,
|
|
194
202
|
guest(input) {
|
|
@@ -263,7 +271,7 @@ function createChatServer(options) {
|
|
|
263
271
|
// with 200 and `purgedMessages: 0`, the identity anonymized and
|
|
264
272
|
// the messages left standing. Nothing failed; the caller was told
|
|
265
273
|
// it had worked.
|
|
266
|
-
|
|
274
|
+
withdraw: (userId, o) => rest.call("DELETE", `/v1/users/${userId}${rest.query({ purge_messages: o?.purgeMessages ? "true" : void 0 })}`),
|
|
267
275
|
purgeMessages: (userId) => rest.call("DELETE", `/v1/users/${userId}/messages`),
|
|
268
276
|
revokeTokens: async (userId) => {
|
|
269
277
|
await rest.call("POST", `/v1/users/${userId}/revoke-tokens`);
|
|
@@ -295,6 +303,12 @@ function createChatServer(options) {
|
|
|
295
303
|
}
|
|
296
304
|
};
|
|
297
305
|
}
|
|
306
|
+
function keyIdFrom(secretKey) {
|
|
307
|
+
const parts = secretKey.split("_");
|
|
308
|
+
if (parts.length < 3) return void 0;
|
|
309
|
+
const id = parts[1];
|
|
310
|
+
return id !== void 0 && /^[0-9A-HJKMNP-TV-Z]{13}$/.test(id) ? id : void 0;
|
|
311
|
+
}
|
|
298
312
|
export {
|
|
299
313
|
DefaultTokenTtlSeconds,
|
|
300
314
|
GuestIdPrefix,
|