sendscript 2.3.1 → 2.3.3

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/stringify.mjs CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  const debug = Debug.extend('stringify')
11
11
 
12
12
  const keywords = ['ref', 'call', 'quote', 'await', 'leaf']
13
+
13
14
  const isKeyword = (v) => keywords.includes(v)
14
15
 
15
16
  const isPlainObject = (value) => {
@@ -18,26 +19,26 @@ const isPlainObject = (value) => {
18
19
  return proto === Object.prototype || proto === null
19
20
  }
20
21
 
21
- // Recursively transform a program tree, encoding SendScript operators and leaf values
22
22
  function transformValue (value, leafSerializer) {
23
23
  debug(value)
24
24
 
25
- if (value === null) {
26
- return null
27
- }
25
+ if (value === null) return null
28
26
 
29
- // Normalize SendScript wrapper functions (ref, call, await)
27
+ // unwrap function wrappers (instrumented nodes)
30
28
  if (typeof value === 'function' && typeof value.toJSON === 'function') {
31
29
  return transformValue(value.toJSON(), leafSerializer)
32
30
  }
33
31
 
34
- // Encode SendScript operators
35
32
  if (value && value[ref]) {
36
33
  return ['ref', ...value.path]
37
34
  }
38
35
 
39
36
  if (value && value[call]) {
40
- return ['call', transformValue(value.ref, leafSerializer), transformValue(value.args, leafSerializer)]
37
+ return [
38
+ 'call',
39
+ transformValue(value.ref, leafSerializer),
40
+ transformValue(value.args, leafSerializer)
41
+ ]
41
42
  }
42
43
 
43
44
  if (value && value[awaitSymbol]) {
@@ -45,22 +46,27 @@ function transformValue (value, leafSerializer) {
45
46
  }
46
47
 
47
48
  if (value && value[then]) {
48
- return ['then', transformValue(value.ref, leafSerializer), transformValue(value.resolve || null, leafSerializer), transformValue(value.reject || null, leafSerializer)]
49
+ return [
50
+ 'then',
51
+ transformValue(value.ref, leafSerializer),
52
+ transformValue(value.resolve || null, leafSerializer),
53
+ transformValue(value.reject || null, leafSerializer)
54
+ ]
49
55
  }
50
56
 
51
- // Handle arrays: quote keyword operators, transform other arrays recursively
52
57
  if (Array.isArray(value)) {
53
58
  const [operator, ...rest] = value
54
59
 
55
60
  if (isKeyword(operator)) {
56
- // Quote reserved keyword strings to preserve them as data
57
- return [['quote', operator], ...rest.map((item) => transformValue(item, leafSerializer))]
61
+ return [
62
+ ['quote', operator],
63
+ ...rest.map((item) => transformValue(item, leafSerializer))
64
+ ]
58
65
  }
59
66
 
60
67
  return value.map((item) => transformValue(item, leafSerializer))
61
68
  }
62
69
 
63
- // Recurse into plain objects
64
70
  if (isPlainObject(value)) {
65
71
  const result = {}
66
72
 
@@ -71,21 +77,45 @@ function transformValue (value, leafSerializer) {
71
77
  return result
72
78
  }
73
79
 
74
- // Encode non-JSON leaf values (Date, RegExp, BigInt, etc.)
75
80
  return ['leaf', leafSerializer(value)]
76
81
  }
77
82
 
83
+ /**
84
+ * Default strict serializer for leaf values.
85
+ *
86
+ * Rejects non-JSON-safe values.
87
+ *
88
+ * @param {any} x
89
+ * @returns {string}
90
+ * @public
91
+ */
78
92
  function strictStringify (x) {
79
93
  const typeOf = typeof x
80
94
 
81
95
  if (typeOf === 'object' || typeOf === 'function' || x === undefined) {
82
- throw new SendScriptSerializationError(`Cannot and should not attempt to serialize ${x}`)
96
+ throw new SendScriptSerializationError(
97
+ `Cannot and should not attempt to serialize ${x}`
98
+ )
83
99
  }
84
100
 
85
101
  return JSON.stringify(x)
86
102
  }
87
103
 
88
- export default function stringify (leafSerializer = strictStringify) {
104
+ /**
105
+ * Creates a stringify function for SendScript AST structures.
106
+ *
107
+ * @param {(value: any) => string} [leafSerializer=strictStringify]
108
+ * @returns {(program: any) => string}
109
+ * @public
110
+ */
111
+ export default function Stringify (leafSerializer = strictStringify) {
112
+ /**
113
+ * Serializes a program into a JSON string representation.
114
+ *
115
+ * @param {any} program
116
+ * @returns {string}
117
+ * @public
118
+ */
89
119
  function stringify (program) {
90
120
  return JSON.stringify(transformValue(program, leafSerializer))
91
121
  }