@abraca/resend 2.34.0 → 2.35.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.
@@ -28,18 +28,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
 
29
29
  //#endregion
30
30
  let _abraca_dabra = require("@abraca/dabra");
31
- let yjs = require("yjs");
32
- yjs = __toESM(yjs);
33
31
  let _abraca_convert = require("@abraca/convert");
34
32
  let node_crypto = require("node:crypto");
35
33
  let node_http = require("node:http");
36
34
  let resend = require("resend");
37
- let _noble_ed25519 = require("@noble/ed25519");
38
- _noble_ed25519 = __toESM(_noble_ed25519);
39
- let node_fs = require("node:fs");
40
- let node_fs_promises = require("node:fs/promises");
41
- let node_os = require("node:os");
42
- let node_path = require("node:path");
35
+ require("yjs");
43
36
 
44
37
  //#region packages/resend/src/bootstrap.ts
45
38
  /**
@@ -57,22 +50,6 @@ const OUTBOX_COLUMNS = [
57
50
  "Sent",
58
51
  "Failed"
59
52
  ];
60
- function readEntries(treeMap, selfId) {
61
- const entries = [];
62
- treeMap.forEach((raw, id) => {
63
- if (selfId && id === selfId) return;
64
- const value = (0, _abraca_dabra.toPlain)(raw);
65
- if (typeof value !== "object" || value === null) return;
66
- entries.push({
67
- id,
68
- label: typeof value.label === "string" ? value.label : "Untitled",
69
- parentId: value.parentId ?? null,
70
- order: typeof value.order === "number" ? value.order : 0,
71
- type: typeof value.type === "string" ? value.type : void 0
72
- });
73
- });
74
- return entries;
75
- }
76
53
  async function createDoc(server, opts) {
77
54
  const treeMap = server.getTreeMap();
78
55
  const rootDoc = server.rootDocument;
@@ -102,7 +79,7 @@ async function bootstrap(server) {
102
79
  const treeMap = server.getTreeMap();
103
80
  const spaceDocId = server.spaceDocId;
104
81
  if (!treeMap || !spaceDocId) throw new Error("Cannot bootstrap — server is not connected.");
105
- const topLevel = readEntries(treeMap, spaceDocId).filter((e) => e.parentId === null);
82
+ const topLevel = (0, _abraca_dabra.readEntries)(treeMap, spaceDocId).filter((e) => e.parentId === null);
106
83
  const existingInbox = topLevel.find((e) => e.label.trim().toLowerCase() === INBOX_LABEL.toLowerCase());
107
84
  const existingOutbox = topLevel.find((e) => e.label.trim().toLowerCase() === OUTBOX_LABEL.toLowerCase());
108
85
  let inboxId;
@@ -133,7 +110,7 @@ async function bootstrap(server) {
133
110
  });
134
111
  console.error(`[abracadabra-resend] Outbox created: ${outboxId}`);
135
112
  }
136
- const existingColumns = readEntries(treeMap, spaceDocId).filter((e) => e.parentId === outboxId);
113
+ const existingColumns = (0, _abraca_dabra.readEntries)(treeMap, spaceDocId).filter((e) => e.parentId === outboxId);
137
114
  const columns = {};
138
115
  for (const colLabel of OUTBOX_COLUMNS) {
139
116
  const match = existingColumns.find((e) => e.label.trim().toLowerCase() === colLabel.toLowerCase());
@@ -157,31 +134,6 @@ async function bootstrap(server) {
157
134
  };
158
135
  }
159
136
 
160
- //#endregion
161
- //#region packages/resend/src/utils.ts
162
- /**
163
- * Wait for a provider's `synced` event with a timeout.
164
- *
165
- * The `isSynced` short-circuit is load-bearing: providers that already synced
166
- * (e.g. cached child providers returned from `loadChild`) won't re-emit
167
- * `synced`, so without the short-circuit every later op on that doc times out.
168
- */
169
- function waitForSync(provider, timeoutMs = 15e3) {
170
- if (provider.isSynced) return Promise.resolve();
171
- return new Promise((resolve, reject) => {
172
- const timer = setTimeout(() => {
173
- provider.off("synced", handler);
174
- reject(/* @__PURE__ */ new Error(`Sync timed out after ${timeoutMs}ms`));
175
- }, timeoutMs);
176
- function handler() {
177
- clearTimeout(timer);
178
- provider.off("synced", handler);
179
- resolve();
180
- }
181
- provider.on("synced", handler);
182
- });
183
- }
184
-
185
137
  //#endregion
186
138
  //#region packages/resend/src/inbound-server.ts
187
139
  /**
@@ -406,7 +358,7 @@ var InboundServer = class {
406
358
  }));
407
359
  });
408
360
  const provider = await this.server.getChildProvider(id);
409
- await waitForSync(provider);
361
+ await (0, _abraca_dabra.waitForSync)(provider);
410
362
  (0, _abraca_convert.populateYDocFromMarkdown)(provider.document.getXmlFragment("default"), typeof data.text === "string" && data.text.length > 0 ? data.text : typeof data.html === "string" && data.html.length > 0 ? `<details><summary>HTML email (no text part)</summary>\n\n\`\`\`html\n${data.html}\n\`\`\`\n</details>` : "(empty body)", subject);
411
363
  const attached = [];
412
364
  const attachments = Array.isArray(data.attachments) ? data.attachments : [];
@@ -761,636 +713,6 @@ var ResendClient = class {
761
713
  }
762
714
  };
763
715
 
764
- //#endregion
765
- //#region node_modules/@noble/hashes/utils.js
766
- /**
767
- * Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
768
- * @param a - value to test
769
- * @returns `true` when the value is a Uint8Array-compatible view.
770
- * @example
771
- * Check whether a value is a Uint8Array-compatible view.
772
- * ```ts
773
- * isBytes(new Uint8Array([1, 2, 3]));
774
- * ```
775
- */
776
- function isBytes(a) {
777
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
778
- }
779
- /**
780
- * Asserts something is Uint8Array.
781
- * @param value - value to validate
782
- * @param length - optional exact length constraint
783
- * @param title - label included in thrown errors
784
- * @returns The validated byte array.
785
- * @throws On wrong argument types. {@link TypeError}
786
- * @throws On wrong argument ranges or values. {@link RangeError}
787
- * @example
788
- * Validate that a value is a byte array.
789
- * ```ts
790
- * abytes(new Uint8Array([1, 2, 3]));
791
- * ```
792
- */
793
- function abytes(value, length, title = "") {
794
- const bytes = isBytes(value);
795
- const len = value?.length;
796
- const needsLen = length !== void 0;
797
- if (!bytes || needsLen && len !== length) {
798
- const prefix = title && `"${title}" `;
799
- const ofLen = needsLen ? ` of length ${length}` : "";
800
- const got = bytes ? `length=${len}` : `type=${typeof value}`;
801
- const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
802
- if (!bytes) throw new TypeError(message);
803
- throw new RangeError(message);
804
- }
805
- return value;
806
- }
807
- /**
808
- * Asserts a hash instance has not been destroyed or finished.
809
- * @param instance - hash instance to validate
810
- * @param checkFinished - whether to reject finalized instances
811
- * @throws If the hash instance has already been destroyed or finalized. {@link Error}
812
- * @example
813
- * Validate that a hash instance is still usable.
814
- * ```ts
815
- * import { aexists } from '@noble/hashes/utils.js';
816
- * import { sha256 } from '@noble/hashes/sha2.js';
817
- * const hash = sha256.create();
818
- * aexists(hash);
819
- * ```
820
- */
821
- function aexists(instance, checkFinished = true) {
822
- if (instance.destroyed) throw new Error("Hash instance has been destroyed");
823
- if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
824
- }
825
- /**
826
- * Asserts output is a sufficiently-sized byte array.
827
- * @param out - destination buffer
828
- * @param instance - hash instance providing output length
829
- * Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
830
- * @throws On wrong argument types. {@link TypeError}
831
- * @throws On wrong argument ranges or values. {@link RangeError}
832
- * @example
833
- * Validate a caller-provided digest buffer.
834
- * ```ts
835
- * import { aoutput } from '@noble/hashes/utils.js';
836
- * import { sha256 } from '@noble/hashes/sha2.js';
837
- * const hash = sha256.create();
838
- * aoutput(new Uint8Array(hash.outputLen), hash);
839
- * ```
840
- */
841
- function aoutput(out, instance) {
842
- abytes(out, void 0, "digestInto() output");
843
- const min = instance.outputLen;
844
- if (out.length < min) throw new RangeError("\"digestInto() output\" expected to be of length >=" + min);
845
- }
846
- /**
847
- * Zeroizes typed arrays in place. Warning: JS provides no guarantees.
848
- * @param arrays - arrays to overwrite with zeros
849
- * @example
850
- * Zeroize sensitive buffers in place.
851
- * ```ts
852
- * clean(new Uint8Array([1, 2, 3]));
853
- * ```
854
- */
855
- function clean(...arrays) {
856
- for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
857
- }
858
- /**
859
- * Creates a DataView for byte-level manipulation.
860
- * @param arr - source typed array
861
- * @returns DataView over the same buffer region.
862
- * @example
863
- * Create a DataView over an existing buffer.
864
- * ```ts
865
- * createView(new Uint8Array(4));
866
- * ```
867
- */
868
- function createView(arr) {
869
- return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
870
- }
871
- /** Whether the current platform is little-endian. */
872
- const isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
873
- const hasHexBuiltin = typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function";
874
- /**
875
- * Creates a callable hash function from a stateful class constructor.
876
- * @param hashCons - hash constructor or factory
877
- * @param info - optional metadata such as DER OID
878
- * @returns Frozen callable hash wrapper with `.create()`.
879
- * Wrapper construction eagerly calls `hashCons(undefined)` once to read
880
- * `outputLen` / `blockLen`, so constructor side effects happen at module
881
- * init time.
882
- * @example
883
- * Wrap a stateful hash constructor into a callable helper.
884
- * ```ts
885
- * import { createHasher } from '@noble/hashes/utils.js';
886
- * import { sha256 } from '@noble/hashes/sha2.js';
887
- * const wrapped = createHasher(sha256.create, { oid: sha256.oid });
888
- * wrapped(new Uint8Array([1]));
889
- * ```
890
- */
891
- function createHasher(hashCons, info = {}) {
892
- const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
893
- const tmp = hashCons(void 0);
894
- hashC.outputLen = tmp.outputLen;
895
- hashC.blockLen = tmp.blockLen;
896
- hashC.canXOF = tmp.canXOF;
897
- hashC.create = (opts) => hashCons(opts);
898
- Object.assign(hashC, info);
899
- return Object.freeze(hashC);
900
- }
901
- /**
902
- * Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
903
- * @param suffix - final OID byte for the selected hash.
904
- * The helper accepts any byte even though only the documented NIST hash
905
- * suffixes are meaningful downstream.
906
- * @returns Object containing the DER-encoded OID.
907
- * @example
908
- * Build OID metadata for a NIST hash.
909
- * ```ts
910
- * oidNist(0x01);
911
- * ```
912
- */
913
- const oidNist = (suffix) => ({ oid: Uint8Array.from([
914
- 6,
915
- 9,
916
- 96,
917
- 134,
918
- 72,
919
- 1,
920
- 101,
921
- 3,
922
- 4,
923
- 2,
924
- suffix
925
- ]) });
926
-
927
- //#endregion
928
- //#region node_modules/@noble/hashes/_md.js
929
- /**
930
- * Internal Merkle-Damgard hash utils.
931
- * @module
932
- */
933
- /**
934
- * Merkle-Damgard hash construction base class.
935
- * Could be used to create MD5, RIPEMD, SHA1, SHA2.
936
- * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
937
- * strings with partial-byte tails.
938
- * @param blockLen - internal block size in bytes
939
- * @param outputLen - digest size in bytes
940
- * @param padOffset - trailing length field size in bytes
941
- * @param isLE - whether length and state words are encoded in little-endian
942
- * @example
943
- * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
944
- * ```ts
945
- * import { _SHA1 } from '@noble/hashes/legacy.js';
946
- * const hash = new _SHA1();
947
- * hash.update(new Uint8Array([97, 98, 99]));
948
- * hash.digest();
949
- * ```
950
- */
951
- var HashMD = class {
952
- blockLen;
953
- outputLen;
954
- canXOF = false;
955
- padOffset;
956
- isLE;
957
- buffer;
958
- view;
959
- finished = false;
960
- length = 0;
961
- pos = 0;
962
- destroyed = false;
963
- constructor(blockLen, outputLen, padOffset, isLE) {
964
- this.blockLen = blockLen;
965
- this.outputLen = outputLen;
966
- this.padOffset = padOffset;
967
- this.isLE = isLE;
968
- this.buffer = new Uint8Array(blockLen);
969
- this.view = createView(this.buffer);
970
- }
971
- update(data) {
972
- aexists(this);
973
- abytes(data);
974
- const { view, buffer, blockLen } = this;
975
- const len = data.length;
976
- for (let pos = 0; pos < len;) {
977
- const take = Math.min(blockLen - this.pos, len - pos);
978
- if (take === blockLen) {
979
- const dataView = createView(data);
980
- for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
981
- continue;
982
- }
983
- buffer.set(data.subarray(pos, pos + take), this.pos);
984
- this.pos += take;
985
- pos += take;
986
- if (this.pos === blockLen) {
987
- this.process(view, 0);
988
- this.pos = 0;
989
- }
990
- }
991
- this.length += data.length;
992
- this.roundClean();
993
- return this;
994
- }
995
- digestInto(out) {
996
- aexists(this);
997
- aoutput(out, this);
998
- this.finished = true;
999
- const { buffer, view, blockLen, isLE } = this;
1000
- let { pos } = this;
1001
- buffer[pos++] = 128;
1002
- clean(this.buffer.subarray(pos));
1003
- if (this.padOffset > blockLen - pos) {
1004
- this.process(view, 0);
1005
- pos = 0;
1006
- }
1007
- for (let i = pos; i < blockLen; i++) buffer[i] = 0;
1008
- view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
1009
- this.process(view, 0);
1010
- const oview = createView(out);
1011
- const len = this.outputLen;
1012
- if (len % 4) throw new Error("_sha2: outputLen must be aligned to 32bit");
1013
- const outLen = len / 4;
1014
- const state = this.get();
1015
- if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
1016
- for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
1017
- }
1018
- digest() {
1019
- const { buffer, outputLen } = this;
1020
- this.digestInto(buffer);
1021
- const res = buffer.slice(0, outputLen);
1022
- this.destroy();
1023
- return res;
1024
- }
1025
- _cloneInto(to) {
1026
- to ||= new this.constructor();
1027
- to.set(...this.get());
1028
- const { blockLen, buffer, length, finished, destroyed, pos } = this;
1029
- to.destroyed = destroyed;
1030
- to.finished = finished;
1031
- to.length = length;
1032
- to.pos = pos;
1033
- if (length % blockLen) to.buffer.set(buffer);
1034
- return to;
1035
- }
1036
- clone() {
1037
- return this._cloneInto();
1038
- }
1039
- };
1040
- /** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
1041
- * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
1042
- * eight prime numbers. Exported as a shared table; callers must treat it as read-only because
1043
- * constructors copy halves from it by index. */
1044
- const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
1045
- 1779033703,
1046
- 4089235720,
1047
- 3144134277,
1048
- 2227873595,
1049
- 1013904242,
1050
- 4271175723,
1051
- 2773480762,
1052
- 1595750129,
1053
- 1359893119,
1054
- 2917565137,
1055
- 2600822924,
1056
- 725511199,
1057
- 528734635,
1058
- 4215389547,
1059
- 1541459225,
1060
- 327033209
1061
- ]);
1062
-
1063
- //#endregion
1064
- //#region node_modules/@noble/hashes/_u64.js
1065
- const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
1066
- const _32n = /* @__PURE__ */ BigInt(32);
1067
- function fromBig(n, le = false) {
1068
- if (le) return {
1069
- h: Number(n & U32_MASK64),
1070
- l: Number(n >> _32n & U32_MASK64)
1071
- };
1072
- return {
1073
- h: Number(n >> _32n & U32_MASK64) | 0,
1074
- l: Number(n & U32_MASK64) | 0
1075
- };
1076
- }
1077
- function split(lst, le = false) {
1078
- const len = lst.length;
1079
- let Ah = new Uint32Array(len);
1080
- let Al = new Uint32Array(len);
1081
- for (let i = 0; i < len; i++) {
1082
- const { h, l } = fromBig(lst[i], le);
1083
- [Ah[i], Al[i]] = [h, l];
1084
- }
1085
- return [Ah, Al];
1086
- }
1087
- const shrSH = (h, _l, s) => h >>> s;
1088
- const shrSL = (h, l, s) => h << 32 - s | l >>> s;
1089
- const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
1090
- const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
1091
- const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
1092
- const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
1093
- function add(Ah, Al, Bh, Bl) {
1094
- const l = (Al >>> 0) + (Bl >>> 0);
1095
- return {
1096
- h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
1097
- l: l | 0
1098
- };
1099
- }
1100
- const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
1101
- const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
1102
- const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
1103
- const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
1104
- const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
1105
- const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
1106
-
1107
- //#endregion
1108
- //#region node_modules/@noble/hashes/sha2.js
1109
- /**
1110
- * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
1111
- * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
1112
- * Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
1113
- * {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
1114
- * @module
1115
- */
1116
- const K512 = split([
1117
- "0x428a2f98d728ae22",
1118
- "0x7137449123ef65cd",
1119
- "0xb5c0fbcfec4d3b2f",
1120
- "0xe9b5dba58189dbbc",
1121
- "0x3956c25bf348b538",
1122
- "0x59f111f1b605d019",
1123
- "0x923f82a4af194f9b",
1124
- "0xab1c5ed5da6d8118",
1125
- "0xd807aa98a3030242",
1126
- "0x12835b0145706fbe",
1127
- "0x243185be4ee4b28c",
1128
- "0x550c7dc3d5ffb4e2",
1129
- "0x72be5d74f27b896f",
1130
- "0x80deb1fe3b1696b1",
1131
- "0x9bdc06a725c71235",
1132
- "0xc19bf174cf692694",
1133
- "0xe49b69c19ef14ad2",
1134
- "0xefbe4786384f25e3",
1135
- "0x0fc19dc68b8cd5b5",
1136
- "0x240ca1cc77ac9c65",
1137
- "0x2de92c6f592b0275",
1138
- "0x4a7484aa6ea6e483",
1139
- "0x5cb0a9dcbd41fbd4",
1140
- "0x76f988da831153b5",
1141
- "0x983e5152ee66dfab",
1142
- "0xa831c66d2db43210",
1143
- "0xb00327c898fb213f",
1144
- "0xbf597fc7beef0ee4",
1145
- "0xc6e00bf33da88fc2",
1146
- "0xd5a79147930aa725",
1147
- "0x06ca6351e003826f",
1148
- "0x142929670a0e6e70",
1149
- "0x27b70a8546d22ffc",
1150
- "0x2e1b21385c26c926",
1151
- "0x4d2c6dfc5ac42aed",
1152
- "0x53380d139d95b3df",
1153
- "0x650a73548baf63de",
1154
- "0x766a0abb3c77b2a8",
1155
- "0x81c2c92e47edaee6",
1156
- "0x92722c851482353b",
1157
- "0xa2bfe8a14cf10364",
1158
- "0xa81a664bbc423001",
1159
- "0xc24b8b70d0f89791",
1160
- "0xc76c51a30654be30",
1161
- "0xd192e819d6ef5218",
1162
- "0xd69906245565a910",
1163
- "0xf40e35855771202a",
1164
- "0x106aa07032bbd1b8",
1165
- "0x19a4c116b8d2d0c8",
1166
- "0x1e376c085141ab53",
1167
- "0x2748774cdf8eeb99",
1168
- "0x34b0bcb5e19b48a8",
1169
- "0x391c0cb3c5c95a63",
1170
- "0x4ed8aa4ae3418acb",
1171
- "0x5b9cca4f7763e373",
1172
- "0x682e6ff3d6b2b8a3",
1173
- "0x748f82ee5defb2fc",
1174
- "0x78a5636f43172f60",
1175
- "0x84c87814a1f0ab72",
1176
- "0x8cc702081a6439ec",
1177
- "0x90befffa23631e28",
1178
- "0xa4506cebde82bde9",
1179
- "0xbef9a3f7b2c67915",
1180
- "0xc67178f2e372532b",
1181
- "0xca273eceea26619c",
1182
- "0xd186b8c721c0c207",
1183
- "0xeada7dd6cde0eb1e",
1184
- "0xf57d4f7fee6ed178",
1185
- "0x06f067aa72176fba",
1186
- "0x0a637dc5a2c898a6",
1187
- "0x113f9804bef90dae",
1188
- "0x1b710b35131c471b",
1189
- "0x28db77f523047d84",
1190
- "0x32caab7b40c72493",
1191
- "0x3c9ebe0a15c9bebc",
1192
- "0x431d67c49c100d4c",
1193
- "0x4cc5d4becb3e42b6",
1194
- "0x597f299cfc657e2a",
1195
- "0x5fcb6fab3ad6faec",
1196
- "0x6c44198c4a475817"
1197
- ].map((n) => BigInt(n)));
1198
- const SHA512_Kh = K512[0];
1199
- const SHA512_Kl = K512[1];
1200
- const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
1201
- const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
1202
- /** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
1203
- var SHA2_64B = class extends HashMD {
1204
- constructor(outputLen) {
1205
- super(128, outputLen, 16, false);
1206
- }
1207
- get() {
1208
- const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1209
- return [
1210
- Ah,
1211
- Al,
1212
- Bh,
1213
- Bl,
1214
- Ch,
1215
- Cl,
1216
- Dh,
1217
- Dl,
1218
- Eh,
1219
- El,
1220
- Fh,
1221
- Fl,
1222
- Gh,
1223
- Gl,
1224
- Hh,
1225
- Hl
1226
- ];
1227
- }
1228
- set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
1229
- this.Ah = Ah | 0;
1230
- this.Al = Al | 0;
1231
- this.Bh = Bh | 0;
1232
- this.Bl = Bl | 0;
1233
- this.Ch = Ch | 0;
1234
- this.Cl = Cl | 0;
1235
- this.Dh = Dh | 0;
1236
- this.Dl = Dl | 0;
1237
- this.Eh = Eh | 0;
1238
- this.El = El | 0;
1239
- this.Fh = Fh | 0;
1240
- this.Fl = Fl | 0;
1241
- this.Gh = Gh | 0;
1242
- this.Gl = Gl | 0;
1243
- this.Hh = Hh | 0;
1244
- this.Hl = Hl | 0;
1245
- }
1246
- process(view, offset) {
1247
- for (let i = 0; i < 16; i++, offset += 4) {
1248
- SHA512_W_H[i] = view.getUint32(offset);
1249
- SHA512_W_L[i] = view.getUint32(offset += 4);
1250
- }
1251
- for (let i = 16; i < 80; i++) {
1252
- const W15h = SHA512_W_H[i - 15] | 0;
1253
- const W15l = SHA512_W_L[i - 15] | 0;
1254
- const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
1255
- const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
1256
- const W2h = SHA512_W_H[i - 2] | 0;
1257
- const W2l = SHA512_W_L[i - 2] | 0;
1258
- const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
1259
- const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
1260
- const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
1261
- SHA512_W_H[i] = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]) | 0;
1262
- SHA512_W_L[i] = SUMl | 0;
1263
- }
1264
- let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
1265
- for (let i = 0; i < 80; i++) {
1266
- const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
1267
- const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
1268
- const CHIh = Eh & Fh ^ ~Eh & Gh;
1269
- const CHIl = El & Fl ^ ~El & Gl;
1270
- const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
1271
- const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
1272
- const T1l = T1ll | 0;
1273
- const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
1274
- const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
1275
- const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
1276
- const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
1277
- Hh = Gh | 0;
1278
- Hl = Gl | 0;
1279
- Gh = Fh | 0;
1280
- Gl = Fl | 0;
1281
- Fh = Eh | 0;
1282
- Fl = El | 0;
1283
- ({h: Eh, l: El} = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
1284
- Dh = Ch | 0;
1285
- Dl = Cl | 0;
1286
- Ch = Bh | 0;
1287
- Cl = Bl | 0;
1288
- Bh = Ah | 0;
1289
- Bl = Al | 0;
1290
- const All = add3L(T1l, sigma0l, MAJl);
1291
- Ah = add3H(All, T1h, sigma0h, MAJh);
1292
- Al = All | 0;
1293
- }
1294
- ({h: Ah, l: Al} = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
1295
- ({h: Bh, l: Bl} = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
1296
- ({h: Ch, l: Cl} = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
1297
- ({h: Dh, l: Dl} = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
1298
- ({h: Eh, l: El} = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
1299
- ({h: Fh, l: Fl} = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
1300
- ({h: Gh, l: Gl} = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
1301
- ({h: Hh, l: Hl} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
1302
- this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
1303
- }
1304
- roundClean() {
1305
- clean(SHA512_W_H, SHA512_W_L);
1306
- }
1307
- destroy() {
1308
- this.destroyed = true;
1309
- clean(this.buffer);
1310
- this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1311
- }
1312
- };
1313
- /** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
1314
- var _SHA512 = class extends SHA2_64B {
1315
- Ah = SHA512_IV[0] | 0;
1316
- Al = SHA512_IV[1] | 0;
1317
- Bh = SHA512_IV[2] | 0;
1318
- Bl = SHA512_IV[3] | 0;
1319
- Ch = SHA512_IV[4] | 0;
1320
- Cl = SHA512_IV[5] | 0;
1321
- Dh = SHA512_IV[6] | 0;
1322
- Dl = SHA512_IV[7] | 0;
1323
- Eh = SHA512_IV[8] | 0;
1324
- El = SHA512_IV[9] | 0;
1325
- Fh = SHA512_IV[10] | 0;
1326
- Fl = SHA512_IV[11] | 0;
1327
- Gh = SHA512_IV[12] | 0;
1328
- Gl = SHA512_IV[13] | 0;
1329
- Hh = SHA512_IV[14] | 0;
1330
- Hl = SHA512_IV[15] | 0;
1331
- constructor() {
1332
- super(64);
1333
- }
1334
- };
1335
- /**
1336
- * SHA2-512 hash function from RFC 4634.
1337
- * @param msg - message bytes to hash
1338
- * @returns Digest bytes.
1339
- * @example
1340
- * Hash a message with SHA2-512.
1341
- * ```ts
1342
- * sha512(new Uint8Array([97, 98, 99]));
1343
- * ```
1344
- */
1345
- const sha512 = /* @__PURE__ */ createHasher(() => new _SHA512(), /* @__PURE__ */ oidNist(3));
1346
-
1347
- //#endregion
1348
- //#region packages/resend/src/crypto.ts
1349
- /**
1350
- * Ed25519 key generation, persistence, and challenge signing for the Resend bridge.
1351
- * Same shape as @abraca/mcp's crypto module; intentionally vendored so this package
1352
- * doesn't depend on @abraca/mcp.
1353
- */
1354
- _noble_ed25519.hashes.sha512 = sha512;
1355
- _noble_ed25519.hashes.sha512Async = (m) => Promise.resolve(sha512(m));
1356
- const DEFAULT_KEY_PATH = (0, node_path.join)((0, node_os.homedir)(), ".abracadabra", "resend.key");
1357
- function toBase64url(bytes) {
1358
- return Buffer.from(bytes).toString("base64url");
1359
- }
1360
- function fromBase64url(b64) {
1361
- return new Uint8Array(Buffer.from(b64, "base64url"));
1362
- }
1363
- async function loadOrCreateKeypair(keyPath) {
1364
- const path = keyPath || DEFAULT_KEY_PATH;
1365
- if ((0, node_fs.existsSync)(path)) {
1366
- const seed = await (0, node_fs_promises.readFile)(path);
1367
- if (seed.length !== 32) throw new Error(`Invalid key file at ${path}: expected 32 bytes, got ${seed.length}`);
1368
- const privateKey = new Uint8Array(seed);
1369
- return {
1370
- privateKey,
1371
- publicKeyB64: toBase64url(_noble_ed25519.getPublicKey(privateKey))
1372
- };
1373
- }
1374
- const privateKey = _noble_ed25519.utils.randomSecretKey();
1375
- const publicKey = _noble_ed25519.getPublicKey(privateKey);
1376
- const dir = (0, node_path.dirname)(path);
1377
- if (!(0, node_fs.existsSync)(dir)) await (0, node_fs_promises.mkdir)(dir, {
1378
- recursive: true,
1379
- mode: 448
1380
- });
1381
- await (0, node_fs_promises.writeFile)(path, Buffer.from(privateKey), { mode: 384 });
1382
- console.error(`[abracadabra-resend] Generated new agent keypair at ${path}`);
1383
- console.error(`[abracadabra-resend] Public key: ${toBase64url(publicKey)}`);
1384
- return {
1385
- privateKey,
1386
- publicKeyB64: toBase64url(publicKey)
1387
- };
1388
- }
1389
- function signChallenge(challengeB64, privateKey) {
1390
- const challenge = fromBase64url(challengeB64);
1391
- return toBase64url(_noble_ed25519.sign(challenge, privateKey));
1392
- }
1393
-
1394
716
  //#endregion
1395
717
  //#region packages/resend/src/server.ts
1396
718
  const IDLE_TIMEOUT_MS = 300 * 1e3;
@@ -1434,26 +756,21 @@ var AbracadabraResendServer = class {
1434
756
  }
1435
757
  /** Authenticate, discover spaces, connect to the configured (or first) space. */
1436
758
  async connect() {
1437
- const keypair = await loadOrCreateKeypair(this.config.keyFile);
759
+ const keypair = await (0, _abraca_dabra.loadOrCreateKeyFile)(this.config.keyFile, {
760
+ defaultFileName: "resend.key",
761
+ onCreate: ({ path, publicKeyB64 }) => {
762
+ console.error(`[abracadabra-resend] Generated new agent keypair at ${path}`);
763
+ console.error(`[abracadabra-resend] Public key: ${publicKeyB64}`);
764
+ }
765
+ });
1438
766
  this._userId = keypair.publicKeyB64;
1439
- const signFn = (challenge) => Promise.resolve(signChallenge(challenge, keypair.privateKey));
1440
- this._signFn = signFn;
1441
- try {
1442
- await this.client.loginWithKey(keypair.publicKeyB64, signFn);
1443
- } catch (err) {
1444
- const status = err?.status ?? err?.response?.status;
1445
- const msg = String(err?.message ?? "").toLowerCase();
1446
- if (!(status === 404 || status === 422 || status === 401 && /not registered|user not found|no such user/.test(msg))) throw err;
1447
- console.error("[abracadabra-resend] Key not registered, creating new account...");
1448
- await this.client.registerWithKey({
1449
- publicKey: keypair.publicKeyB64,
1450
- username: this.agentName.replace(/\s+/g, "-").toLowerCase(),
1451
- displayName: this.agentName,
1452
- deviceName: "Resend Bridge",
1453
- inviteCode: this.config.inviteCode
1454
- });
1455
- await this.client.loginWithKey(keypair.publicKeyB64, signFn);
1456
- }
767
+ this._signFn = (0, _abraca_dabra.makeChallengeSigner)(keypair);
768
+ await (0, _abraca_dabra.loginOrRegisterWithKey)(this.client, keypair, {
769
+ displayName: this.agentName,
770
+ deviceName: "Resend Bridge",
771
+ inviteCode: this.config.inviteCode,
772
+ onRegister: () => console.error("[abracadabra-resend] Key not registered, creating new account...")
773
+ });
1457
774
  console.error(`[abracadabra-resend] Authenticated as ${this.agentName} (pubkey=${keypair.publicKeyB64})`);
1458
775
  this._serverInfo = await this.client.serverInfo();
1459
776
  const roots = await this.client.listChildren();
@@ -1478,15 +795,10 @@ var AbracadabraResendServer = class {
1478
795
  console.error("[abracadabra-resend] JWT expired, re-authenticating...");
1479
796
  await this.client.loginWithKey(this._userId, this._signFn);
1480
797
  }
1481
- const doc = new yjs.Doc({ guid: docId });
1482
- const provider = new _abraca_dabra.AbracadabraProvider({
1483
- name: docId,
1484
- document: doc,
798
+ const { doc, provider } = await (0, _abraca_dabra.openSyncedProvider)({
1485
799
  client: this.client,
1486
- disableOfflineStore: true,
1487
- subdocLoading: "lazy"
800
+ docId
1488
801
  });
1489
- await waitForSync(provider);
1490
802
  provider.awareness?.setLocalStateField("user", {
1491
803
  name: this.agentName,
1492
804
  color: "hsl(170, 70%, 45%)",
@@ -1522,13 +834,13 @@ var AbracadabraResendServer = class {
1522
834
  if (!conn) return;
1523
835
  if (this._wsConnected(conn.provider)) return;
1524
836
  try {
1525
- await waitForSync(conn.provider, 6e3);
837
+ await (0, _abraca_dabra.waitForSync)(conn.provider, 6e3);
1526
838
  } catch {}
1527
839
  if (this._wsConnected(conn.provider)) return;
1528
840
  console.error("[abracadabra-resend] Socket dead — forcing reconnect on the same doc…");
1529
841
  conn.provider.reconnect();
1530
842
  try {
1531
- await waitForSync(conn.provider, 1e4);
843
+ await (0, _abraca_dabra.waitForSync)(conn.provider, 1e4);
1532
844
  console.error("[abracadabra-resend] Reconnected + re-synced");
1533
845
  } catch (e) {
1534
846
  console.error("[abracadabra-resend] Reconnect did not re-sync in time:", e);
@@ -1559,7 +871,7 @@ var AbracadabraResendServer = class {
1559
871
  if (!root) throw new Error("Not connected. Call connect() first.");
1560
872
  if (!this.client.isTokenValid() && this._signFn && this._userId) await this.client.loginWithKey(this._userId, this._signFn);
1561
873
  const childProvider = await root.loadChild(docId);
1562
- await waitForSync(childProvider);
874
+ await (0, _abraca_dabra.waitForSync)(childProvider);
1563
875
  this.childCache.set(docId, {
1564
876
  provider: childProvider,
1565
877
  lastAccessed: Date.now()