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,185 @@
1
+ import { hash } from "fast-sha256"
2
+
3
+ export function isBytes(value) {
4
+ return (
5
+ value instanceof ArrayBuffer ||
6
+ ArrayBuffer.isView(value) ||
7
+ Buffer.isBuffer(value) ||
8
+ (value &&
9
+ typeof value === "object" &&
10
+ value.type === "Buffer" &&
11
+ Array.isArray(value.data))
12
+ )
13
+ }
14
+
15
+ export function isPojo(value) {
16
+ return (
17
+ !isBytes(value) &&
18
+ !Array.isArray(value) &&
19
+ !(value instanceof Blob) &&
20
+ typeof value === "object" &&
21
+ value !== null
22
+ )
23
+ }
24
+
25
+ export async function hasNewline(value) {
26
+ if (typeof value === "string") return value.includes("\n")
27
+ if (value instanceof Blob) {
28
+ value = await value.text()
29
+ return value.includes("\n")
30
+ }
31
+ if (isBytes(value)) return Buffer.from(value).includes("\n")
32
+ return false
33
+ }
34
+
35
+ export async function sha256(data) {
36
+ let uint8Array
37
+ if (data instanceof ArrayBuffer) {
38
+ uint8Array = new Uint8Array(data)
39
+ } else if (data instanceof Uint8Array) {
40
+ uint8Array = data
41
+ } else if (ArrayBuffer.isView(data)) {
42
+ uint8Array = new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
43
+ } else {
44
+ throw new Error("sha256 expects ArrayBuffer or ArrayBufferView")
45
+ }
46
+
47
+ const hashResult = hash(uint8Array)
48
+ return hashResult.buffer.slice(
49
+ hashResult.byteOffset,
50
+ hashResult.byteOffset + hashResult.byteLength
51
+ )
52
+ }
53
+
54
+ export function formatFloat(num) {
55
+ let exp = num.toExponential(20)
56
+ exp = exp.replace(/e\+(\d)$/, "e+0$1")
57
+ exp = exp.replace(/e-(\d)$/, "e-0$1")
58
+ return exp
59
+ }
60
+
61
+ export function hasNonAscii(str) {
62
+ return /[^\x00-\x7F]/.test(str)
63
+ }
64
+
65
+ export function toBuffer(value) {
66
+ if (Buffer.isBuffer(value)) {
67
+ return value
68
+ } else if (
69
+ value &&
70
+ typeof value === "object" &&
71
+ value.type === "Buffer" &&
72
+ Array.isArray(value.data)
73
+ ) {
74
+ return Buffer.from(value.data)
75
+ } else if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
76
+ return Buffer.from(value)
77
+ } else {
78
+ return Buffer.from(value)
79
+ }
80
+ }
81
+
82
+ // Navigate through a path to get a value from nested object/array
83
+ export function getValueByPath(obj, path) {
84
+ const pathParts = path.split("/")
85
+ let value = obj
86
+ for (const part of pathParts) {
87
+ if (/^\d+$/.test(part)) {
88
+ value = value[parseInt(part) - 1]
89
+ } else {
90
+ value = value[part]
91
+ }
92
+ }
93
+ return value
94
+ }
95
+
96
+ // Get the ao-type for a value
97
+ export function getAoType(value) {
98
+ if (
99
+ typeof value === "boolean" ||
100
+ value === null ||
101
+ value === undefined ||
102
+ typeof value === "symbol"
103
+ ) {
104
+ return "atom"
105
+ } else if (typeof value === "number") {
106
+ return Number.isInteger(value) ? "integer" : "float"
107
+ } else if (typeof value === "string" && value.length === 0) {
108
+ return "empty-binary"
109
+ } else if (isBytes(value) && (value.length === 0 || value.byteLength === 0)) {
110
+ return "empty-binary"
111
+ } else if (Array.isArray(value) && value.length === 0) {
112
+ return "empty-list"
113
+ } else if (Array.isArray(value)) {
114
+ return "list"
115
+ } else if (isPojo(value) && Object.keys(value).length === 0) {
116
+ return "empty-message"
117
+ }
118
+ return null
119
+ }
120
+
121
+ // Check if a value is empty
122
+ export function isEmpty(value) {
123
+ return (
124
+ (Array.isArray(value) && value.length === 0) ||
125
+ (isPojo(value) && Object.keys(value).length === 0) ||
126
+ (isBytes(value) && (value.length === 0 || value.byteLength === 0)) ||
127
+ (typeof value === "string" && value.length === 0)
128
+ )
129
+ }
130
+
131
+ // Convert primitive values to their encoded string representation
132
+ export function encodePrimitiveContent(value) {
133
+ if (typeof value === "string") {
134
+ return value
135
+ } else if (typeof value === "boolean") {
136
+ return `"${value}"`
137
+ } else if (typeof value === "number") {
138
+ return String(value)
139
+ } else if (value === null) {
140
+ return '"null"'
141
+ } else if (value === undefined) {
142
+ return '"undefined"'
143
+ } else if (typeof value === "symbol") {
144
+ return `"${value.description || "Symbol.for()"}"`
145
+ }
146
+ return value
147
+ }
148
+
149
+ // Sort type annotations by their numeric prefix
150
+ export function sortTypeAnnotations(types) {
151
+ return types.sort((a, b) => {
152
+ const aNum = parseInt(a.split("=")[0])
153
+ const bNum = parseInt(b.split("=")[0])
154
+ return aNum - bNum
155
+ })
156
+ }
157
+
158
+ // Analyze array contents
159
+ export function analyzeArray(array) {
160
+ return {
161
+ hasObjects: array.some(item => isPojo(item)),
162
+ hasArrays: array.some(item => Array.isArray(item)),
163
+ hasEmptyStrings: array.some(
164
+ item => typeof item === "string" && item === ""
165
+ ),
166
+ hasEmptyObjects: array.some(
167
+ item => isPojo(item) && Object.keys(item).length === 0
168
+ ),
169
+ hasOnlyEmptyElements:
170
+ array.length > 0 &&
171
+ array.every(
172
+ item =>
173
+ (Array.isArray(item) && item.length === 0) ||
174
+ (isPojo(item) && Object.keys(item).length === 0) ||
175
+ (typeof item === "string" && item === "")
176
+ ),
177
+ hasOnlyNonEmptyObjects:
178
+ array.length > 0 &&
179
+ array.every(item => isPojo(item) && Object.keys(item).length > 0),
180
+ hasObjectsWithOnlyEmptyValues: array.some(item => {
181
+ if (!isPojo(item) || Object.keys(item).length === 0) return false
182
+ return Object.values(item).every(v => isEmpty(v))
183
+ }),
184
+ }
185
+ }