hbsig 0.0.1

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.
Files changed (44) hide show
  1. package/cjs/bin_to_str.js +44 -0
  2. package/cjs/collect-body-keys.js +470 -0
  3. package/cjs/encode-array-item.js +110 -0
  4. package/cjs/encode-utils.js +236 -0
  5. package/cjs/encode.js +1318 -0
  6. package/cjs/erl_json.js +317 -0
  7. package/cjs/erl_str.js +1037 -0
  8. package/cjs/flat.js +222 -0
  9. package/cjs/http-message-signatures/httpbis.js +489 -0
  10. package/cjs/http-message-signatures/index.js +25 -0
  11. package/cjs/http-message-signatures/structured-header.js +129 -0
  12. package/cjs/httpsig.js +716 -0
  13. package/cjs/httpsig2.js +1160 -0
  14. package/cjs/id.js +470 -0
  15. package/cjs/index.js +63 -0
  16. package/cjs/send.js +194 -0
  17. package/cjs/signer-utils.js +617 -0
  18. package/cjs/signer.js +606 -0
  19. package/cjs/structured.js +296 -0
  20. package/cjs/test.js +27 -0
  21. package/cjs/utils.js +42 -0
  22. package/esm/bin_to_str.js +46 -0
  23. package/esm/collect-body-keys.js +436 -0
  24. package/esm/encode-array-item.js +112 -0
  25. package/esm/encode-utils.js +185 -0
  26. package/esm/encode.js +1219 -0
  27. package/esm/erl_json.js +289 -0
  28. package/esm/erl_str.js +1139 -0
  29. package/esm/flat.js +196 -0
  30. package/esm/http-message-signatures/httpbis.js +438 -0
  31. package/esm/http-message-signatures/index.js +4 -0
  32. package/esm/http-message-signatures/structured-header.js +105 -0
  33. package/esm/httpsig.js +658 -0
  34. package/esm/httpsig2.js +1097 -0
  35. package/esm/id.js +459 -0
  36. package/esm/index.js +4 -0
  37. package/esm/package.json +3 -0
  38. package/esm/send.js +124 -0
  39. package/esm/signer-utils.js +494 -0
  40. package/esm/signer.js +452 -0
  41. package/esm/structured.js +269 -0
  42. package/esm/test.js +6 -0
  43. package/esm/utils.js +28 -0
  44. package/package.json +28 -0
@@ -0,0 +1,317 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.erl_json_from = erl_json_from;
7
+ exports.erl_json_to = erl_json_to;
8
+ exports.normalize = normalize;
9
+ function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
10
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
12
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
13
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
14
+ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
15
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
16
+ /**
17
+ * Codec for converting between JS objects with Erlang types and annotated JSON
18
+ * Only handles types that HyperBEAM supports
19
+ */
20
+
21
+ /**
22
+ * Convert annotated JSON to JS object with Erlang types
23
+ * @param {Object} json - Annotated JSON object
24
+ * @returns {*} - JS object with Buffer for binaries, Symbol for atoms
25
+ */
26
+ function erl_json_from(json) {
27
+ return convertFromAnnotatedJSON(json);
28
+ }
29
+
30
+ /**
31
+ * Convert JS object with Erlang types to annotated JSON
32
+ * @param {*} jsObj - JS object with Erlang types
33
+ * @returns {Object} - Annotated JSON object
34
+ */
35
+ function erl_json_to(jsObj) {
36
+ return convertToAnnotatedJSON(jsObj);
37
+ }
38
+
39
+ /**
40
+ * Normalize JS values to match what comes back from Erlang through erl_str_from
41
+ * This function is deterministic and matches the behavior of the Erlang round-trip
42
+ * @param {*} obj - JS object to normalize
43
+ * @param {boolean} binaryMode - true for binary mode, false for string mode (default)
44
+ * @returns {*} - Normalized JS object
45
+ */
46
+ function normalize(obj) {
47
+ var binaryMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
48
+ if (obj === null) return null;
49
+ if (obj === undefined) return undefined;
50
+
51
+ // Handle symbols - convert to their special cases or match erl_str_from behavior
52
+ if (_typeof(obj) === "symbol") {
53
+ var key = Symbol.keyFor(obj);
54
+ var name = key || obj.description || obj.toString().slice(7, -1);
55
+
56
+ // Special symbols that become primitives
57
+ if (name === "null") return null;
58
+ if (name === "true") return true;
59
+ if (name === "false") return false;
60
+
61
+ // Note: Symbol('undefined') stays as Symbol.for('undefined')
62
+ // It does NOT become JavaScript undefined
63
+ // This matches what erl_str_from actually returns
64
+
65
+ // For all symbols (including 'undefined'), convert to global symbol
66
+ // This is because non-global symbols can't round-trip through JSON
67
+ // Symbol('ok') -> '%ok%' -> Symbol.for('ok')
68
+ return Symbol["for"](name);
69
+ }
70
+
71
+ // Strings
72
+ if (typeof obj === "string") {
73
+ // Special case: "::" becomes empty buffer through Erlang
74
+ if (obj === "::") {
75
+ return Buffer.alloc(0);
76
+ }
77
+ if (binaryMode) {
78
+ // In binary mode, convert strings to buffers
79
+ return Buffer.from(obj, "utf8");
80
+ } else {
81
+ // In string mode, strings stay as strings
82
+ return obj;
83
+ }
84
+ }
85
+
86
+ // Other primitives pass through
87
+ if (typeof obj === "number" || typeof obj === "boolean") {
88
+ return obj;
89
+ }
90
+
91
+ // Buffers pass through
92
+ if (obj instanceof Buffer || obj instanceof Uint8Array) {
93
+ return Buffer.from(obj);
94
+ }
95
+
96
+ // Arrays - normalize each element
97
+ if (Array.isArray(obj)) {
98
+ return obj.map(function (item) {
99
+ // undefined in arrays becomes null (JSON behavior)
100
+ if (item === undefined) {
101
+ return null;
102
+ }
103
+ return normalize(item, binaryMode);
104
+ });
105
+ }
106
+
107
+ // Objects - normalize each value and remove undefined values
108
+ if (_typeof(obj) === "object" && obj !== null) {
109
+ var result = {};
110
+ for (var _i = 0, _Object$entries = Object.entries(obj); _i < _Object$entries.length; _i++) {
111
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
112
+ k = _Object$entries$_i[0],
113
+ v = _Object$entries$_i[1];
114
+ var normalized = normalize(v, binaryMode);
115
+ // Skip undefined values in objects
116
+ if (normalized !== undefined) {
117
+ result[k] = normalized;
118
+ }
119
+ }
120
+ return result;
121
+ }
122
+ return obj;
123
+ }
124
+
125
+ // Convert from annotated JSON to JS with Erlang types
126
+ function convertFromAnnotatedJSON(json) {
127
+ // Handle null/undefined
128
+ if (json === null) {
129
+ return null;
130
+ }
131
+ if (json === undefined) {
132
+ return undefined;
133
+ }
134
+
135
+ // Handle booleans
136
+ if (json === true || json === false) {
137
+ return json;
138
+ }
139
+
140
+ // Handle numbers and strings
141
+ if (typeof json === "number") {
142
+ return json;
143
+ }
144
+ if (typeof json === "string") {
145
+ // Check for binary structured field format :base64:
146
+ if (json.startsWith(":") && json.endsWith(":") && json.length >= 2) {
147
+ try {
148
+ // Handle empty binary special case
149
+ if (json === "::") {
150
+ return Buffer.alloc(0);
151
+ }
152
+ // Simple base64 decode for structured field binaries
153
+ var base64 = json.slice(1, -1);
154
+ return Buffer.from(base64, "base64");
155
+ } catch (e) {
156
+ // Not valid base64, return as-is
157
+ return json;
158
+ }
159
+ }
160
+ // Check for token structured field format %token%
161
+ if (json.startsWith("%") && json.endsWith("%") && json.length >= 2) {
162
+ // Handle empty token special case
163
+ if (json === "%%") {
164
+ return Symbol["for"]("");
165
+ }
166
+ // Extract token and convert to symbol
167
+ var token = json.slice(1, -1);
168
+ return Symbol["for"](token);
169
+ }
170
+ // Handle empty string from empty binary conversion
171
+ if (json === "") {
172
+ // In context, this might be an empty binary, but we can't be sure
173
+ // so we return it as-is
174
+ return json;
175
+ }
176
+ return json;
177
+ }
178
+
179
+ // Handle arrays
180
+ if (Array.isArray(json)) {
181
+ return json.map(function (item) {
182
+ return convertFromAnnotatedJSON(item);
183
+ });
184
+ }
185
+
186
+ // Handle objects
187
+ if (json && _typeof(json) === "object") {
188
+ var keys = Object.keys(json);
189
+
190
+ // Check for type annotations (single key objects)
191
+ if (keys.length === 1) {
192
+ var key = keys[0];
193
+ var value = json[key];
194
+ switch (key) {
195
+ case "$empty":
196
+ switch (value) {
197
+ case "binary":
198
+ return Buffer.alloc(0);
199
+ case "list":
200
+ return [];
201
+ case "map":
202
+ return {};
203
+ default:
204
+ return json;
205
+ }
206
+ }
207
+ }
208
+
209
+ // Regular object - convert recursively
210
+ var result = {};
211
+ for (var _i2 = 0, _Object$entries2 = Object.entries(json); _i2 < _Object$entries2.length; _i2++) {
212
+ var _Object$entries2$_i = _slicedToArray(_Object$entries2[_i2], 2),
213
+ k = _Object$entries2$_i[0],
214
+ v = _Object$entries2$_i[1];
215
+ result[k] = convertFromAnnotatedJSON(v);
216
+ }
217
+ return result;
218
+ }
219
+
220
+ // Fallback
221
+ return json;
222
+ }
223
+
224
+ // Convert from JS with Erlang types to annotated JSON
225
+ function convertToAnnotatedJSON(obj) {
226
+ // Handle null - pass through as JSON null
227
+ if (obj === null) {
228
+ return null;
229
+ }
230
+
231
+ // Handle undefined - convert to null (JSON doesn't support undefined)
232
+ if (obj === undefined) {
233
+ return null;
234
+ }
235
+
236
+ // Handle booleans - pass through as JSON booleans
237
+ if (typeof obj === "boolean") {
238
+ return obj;
239
+ }
240
+
241
+ // Handle numbers - pass through
242
+ if (typeof obj === "number") {
243
+ return obj;
244
+ }
245
+
246
+ // Handle strings - pass through
247
+ if (typeof obj === "string") {
248
+ return obj;
249
+ }
250
+
251
+ // Handle symbols (atoms) - use token structured field format
252
+ if (_typeof(obj) === "symbol") {
253
+ // Try to get the key from the global symbol registry
254
+ var key = Symbol.keyFor(obj);
255
+ // If it's not a global symbol, use its description
256
+ var name = key || obj.description || obj.toString().slice(7, -1);
257
+
258
+ // Special atoms that become JSON values
259
+ if (name === "null") return null;
260
+ if (name === "true") return true;
261
+ if (name === "false") return false;
262
+
263
+ // Other atoms become token structured fields
264
+ return "%".concat(name, "%");
265
+ }
266
+
267
+ // Handle Buffers/Uint8Arrays (binaries)
268
+ if (obj instanceof Buffer || obj instanceof Uint8Array) {
269
+ if (obj.length === 0) {
270
+ return {
271
+ $empty: "binary"
272
+ };
273
+ }
274
+ // Use structured fields format for binaries
275
+ return ":".concat(Buffer.from(obj).toString("base64"), ":");
276
+ }
277
+
278
+ // Handle arrays
279
+ if (Array.isArray(obj)) {
280
+ if (obj.length === 0) {
281
+ return {
282
+ $empty: "list"
283
+ };
284
+ }
285
+ return obj.map(function (item) {
286
+ return convertToAnnotatedJSON(item);
287
+ });
288
+ }
289
+
290
+ // Handle objects
291
+ if (_typeof(obj) === "object" && obj !== null) {
292
+ var result = {};
293
+ var hasKeys = false;
294
+ for (var _i3 = 0, _Object$entries3 = Object.entries(obj); _i3 < _Object$entries3.length; _i3++) {
295
+ var _Object$entries3$_i = _slicedToArray(_Object$entries3[_i3], 2),
296
+ k = _Object$entries3$_i[0],
297
+ v = _Object$entries3$_i[1];
298
+ // Skip undefined values completely
299
+ if (v === undefined) {
300
+ continue;
301
+ }
302
+ hasKeys = true;
303
+ result[k] = convertToAnnotatedJSON(v);
304
+ }
305
+
306
+ // Return $empty annotation if no keys remain
307
+ if (!hasKeys) {
308
+ return {
309
+ $empty: "map"
310
+ };
311
+ }
312
+ return result;
313
+ }
314
+
315
+ // Fallback
316
+ return obj;
317
+ }