goscript 0.0.26 → 0.0.29

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 (228) hide show
  1. package/README.md +4 -4
  2. package/cmd/goscript/cmd_compile.go +0 -3
  3. package/cmd/goscript/deps.go +11 -0
  4. package/compiler/analysis.go +298 -55
  5. package/compiler/assignment.go +2 -2
  6. package/compiler/builtin_test.go +1 -1
  7. package/compiler/compiler.go +200 -68
  8. package/compiler/compiler_test.go +17 -24
  9. package/compiler/composite-lit.go +32 -8
  10. package/compiler/decl.go +6 -6
  11. package/compiler/expr-call.go +170 -15
  12. package/compiler/expr-selector.go +100 -0
  13. package/compiler/expr.go +1 -1
  14. package/compiler/protobuf.go +557 -0
  15. package/compiler/spec-struct.go +4 -0
  16. package/compiler/spec-value.go +89 -10
  17. package/compiler/spec.go +254 -1
  18. package/compiler/stmt-assign.go +35 -0
  19. package/compiler/type-assert.go +87 -0
  20. package/compiler/type.go +4 -1
  21. package/dist/gs/builtin/builtin.d.ts +20 -1
  22. package/dist/gs/builtin/builtin.js +95 -4
  23. package/dist/gs/builtin/builtin.js.map +1 -1
  24. package/dist/gs/builtin/slice.d.ts +1 -1
  25. package/dist/gs/builtin/slice.js +21 -2
  26. package/dist/gs/builtin/slice.js.map +1 -1
  27. package/dist/gs/errors/errors.d.ts +5 -6
  28. package/dist/gs/errors/errors.js.map +1 -1
  29. package/dist/gs/internal/oserror/errors.d.ts +6 -0
  30. package/dist/gs/internal/oserror/errors.js +7 -0
  31. package/dist/gs/internal/oserror/errors.js.map +1 -0
  32. package/dist/gs/internal/oserror/index.d.ts +1 -0
  33. package/dist/gs/internal/oserror/index.js +2 -0
  34. package/dist/gs/internal/oserror/index.js.map +1 -0
  35. package/dist/gs/io/fs/format.d.ts +3 -0
  36. package/dist/gs/io/fs/format.js +56 -0
  37. package/dist/gs/io/fs/format.js.map +1 -0
  38. package/dist/gs/io/fs/fs.d.ts +79 -0
  39. package/dist/gs/io/fs/fs.js +200 -0
  40. package/dist/gs/io/fs/fs.js.map +1 -0
  41. package/dist/gs/io/fs/glob.d.ts +10 -0
  42. package/dist/gs/io/fs/glob.js +141 -0
  43. package/dist/gs/io/fs/glob.js.map +1 -0
  44. package/dist/gs/io/fs/index.d.ts +8 -0
  45. package/dist/gs/io/fs/index.js +9 -0
  46. package/dist/gs/io/fs/index.js.map +1 -0
  47. package/dist/gs/io/fs/readdir.d.ts +7 -0
  48. package/dist/gs/io/fs/readdir.js +152 -0
  49. package/dist/gs/io/fs/readdir.js.map +1 -0
  50. package/dist/gs/io/fs/readfile.d.ts +6 -0
  51. package/dist/gs/io/fs/readfile.js +118 -0
  52. package/dist/gs/io/fs/readfile.js.map +1 -0
  53. package/dist/gs/io/fs/stat.d.ts +6 -0
  54. package/dist/gs/io/fs/stat.js +87 -0
  55. package/dist/gs/io/fs/stat.js.map +1 -0
  56. package/dist/gs/io/fs/sub.d.ts +6 -0
  57. package/dist/gs/io/fs/sub.js +172 -0
  58. package/dist/gs/io/fs/sub.js.map +1 -0
  59. package/dist/gs/io/fs/walk.d.ts +7 -0
  60. package/dist/gs/io/fs/walk.js +76 -0
  61. package/dist/gs/io/fs/walk.js.map +1 -0
  62. package/dist/gs/io/index.d.ts +1 -0
  63. package/dist/gs/io/index.js +2 -0
  64. package/dist/gs/io/index.js.map +1 -0
  65. package/dist/gs/io/io.d.ts +107 -0
  66. package/dist/gs/io/io.js +385 -0
  67. package/dist/gs/io/io.js.map +1 -0
  68. package/dist/gs/path/index.d.ts +2 -0
  69. package/dist/gs/path/index.js +3 -0
  70. package/dist/gs/path/index.js.map +1 -0
  71. package/dist/gs/path/match.d.ts +6 -0
  72. package/dist/gs/path/match.js +281 -0
  73. package/dist/gs/path/match.js.map +1 -0
  74. package/dist/gs/path/path.d.ts +7 -0
  75. package/dist/gs/path/path.js +256 -0
  76. package/dist/gs/path/path.js.map +1 -0
  77. package/dist/gs/strings/builder.d.ts +18 -0
  78. package/dist/gs/strings/builder.js +205 -0
  79. package/dist/gs/strings/builder.js.map +1 -0
  80. package/dist/gs/strings/clone.d.ts +1 -0
  81. package/dist/gs/strings/clone.js +16 -0
  82. package/dist/gs/strings/clone.js.map +1 -0
  83. package/dist/gs/strings/compare.d.ts +1 -0
  84. package/dist/gs/strings/compare.js +14 -0
  85. package/dist/gs/strings/compare.js.map +1 -0
  86. package/dist/gs/strings/index.d.ts +2 -0
  87. package/dist/gs/strings/index.js +3 -0
  88. package/dist/gs/strings/index.js.map +1 -0
  89. package/dist/gs/strings/iter.d.ts +8 -0
  90. package/dist/gs/strings/iter.js +160 -0
  91. package/dist/gs/strings/iter.js.map +1 -0
  92. package/dist/gs/strings/reader.d.ts +34 -0
  93. package/dist/gs/strings/reader.js +418 -0
  94. package/dist/gs/strings/reader.js.map +1 -0
  95. package/dist/gs/strings/replace.d.ts +106 -0
  96. package/dist/gs/strings/replace.js +1136 -0
  97. package/dist/gs/strings/replace.js.map +1 -0
  98. package/dist/gs/strings/search.d.ts +24 -0
  99. package/dist/gs/strings/search.js +169 -0
  100. package/dist/gs/strings/search.js.map +1 -0
  101. package/dist/gs/strings/strings.d.ts +47 -0
  102. package/dist/gs/strings/strings.js +418 -0
  103. package/dist/gs/strings/strings.js.map +1 -0
  104. package/dist/gs/stringslite/index.d.ts +1 -0
  105. package/dist/gs/stringslite/index.js +2 -0
  106. package/dist/gs/stringslite/index.js.map +1 -0
  107. package/dist/gs/stringslite/strings.d.ts +11 -0
  108. package/dist/gs/stringslite/strings.js +67 -0
  109. package/dist/gs/stringslite/strings.js.map +1 -0
  110. package/dist/gs/sync/index.d.ts +1 -0
  111. package/dist/gs/sync/index.js +2 -0
  112. package/dist/gs/sync/index.js.map +1 -0
  113. package/dist/gs/sync/sync.d.ts +79 -0
  114. package/dist/gs/sync/sync.js +392 -0
  115. package/dist/gs/sync/sync.js.map +1 -0
  116. package/dist/gs/time/time.d.ts +11 -2
  117. package/dist/gs/time/time.js +337 -12
  118. package/dist/gs/time/time.js.map +1 -1
  119. package/dist/gs/unicode/index.d.ts +1 -0
  120. package/dist/gs/unicode/index.js +2 -0
  121. package/dist/gs/unicode/index.js.map +1 -0
  122. package/dist/gs/unicode/unicode.d.ts +105 -0
  123. package/dist/gs/unicode/unicode.js +332 -0
  124. package/dist/gs/unicode/unicode.js.map +1 -0
  125. package/dist/gs/unicode/utf8/index.d.ts +1 -0
  126. package/dist/gs/unicode/utf8/index.js +3 -0
  127. package/dist/gs/unicode/utf8/index.js.map +1 -0
  128. package/dist/gs/unicode/utf8/utf8.d.ts +20 -0
  129. package/dist/gs/unicode/utf8/utf8.js +196 -0
  130. package/dist/gs/unicode/utf8/utf8.js.map +1 -0
  131. package/dist/gs/unsafe/index.d.ts +1 -0
  132. package/dist/gs/unsafe/index.js +2 -0
  133. package/dist/gs/unsafe/index.js.map +1 -0
  134. package/dist/gs/unsafe/unsafe.d.ts +11 -0
  135. package/dist/gs/unsafe/unsafe.js +44 -0
  136. package/dist/gs/unsafe/unsafe.js.map +1 -0
  137. package/go.mod +2 -1
  138. package/go.sum +6 -2
  139. package/gs/README.md +6 -0
  140. package/gs/builtin/builtin.ts +171 -0
  141. package/gs/builtin/channel.ts +683 -0
  142. package/gs/builtin/defer.ts +58 -0
  143. package/gs/builtin/index.ts +1 -0
  144. package/gs/builtin/io.ts +22 -0
  145. package/gs/builtin/map.ts +50 -0
  146. package/gs/builtin/slice.ts +1030 -0
  147. package/gs/builtin/type.ts +1106 -0
  148. package/gs/builtin/varRef.ts +25 -0
  149. package/gs/cmp/godoc.txt +8 -0
  150. package/gs/cmp/index.ts +29 -0
  151. package/gs/context/context.ts +401 -0
  152. package/gs/context/godoc.txt +69 -0
  153. package/gs/context/index.ts +1 -0
  154. package/gs/errors/errors.ts +223 -0
  155. package/gs/errors/godoc.txt +63 -0
  156. package/gs/errors/index.ts +1 -0
  157. package/gs/internal/goarch/godoc.txt +39 -0
  158. package/gs/internal/goarch/index.ts +18 -0
  159. package/gs/internal/oserror/errors.ts +14 -0
  160. package/gs/internal/oserror/index.ts +1 -0
  161. package/gs/io/fs/format.ts +65 -0
  162. package/gs/io/fs/fs.ts +359 -0
  163. package/gs/io/fs/glob.ts +167 -0
  164. package/gs/io/fs/godoc.txt +35 -0
  165. package/gs/io/fs/index.ts +8 -0
  166. package/gs/io/fs/readdir.ts +126 -0
  167. package/gs/io/fs/readfile.ts +77 -0
  168. package/gs/io/fs/stat.ts +38 -0
  169. package/gs/io/fs/sub.ts +208 -0
  170. package/gs/io/fs/walk.ts +89 -0
  171. package/gs/io/godoc.txt +61 -0
  172. package/gs/io/index.ts +1 -0
  173. package/gs/io/io.go +75 -0
  174. package/gs/io/io.ts +546 -0
  175. package/gs/iter/godoc.txt +203 -0
  176. package/gs/iter/index.ts +1 -0
  177. package/gs/iter/iter.ts +117 -0
  178. package/gs/math/bits/index.ts +356 -0
  179. package/gs/math/godoc.txt +76 -0
  180. package/gs/path/index.ts +2 -0
  181. package/gs/path/match.ts +307 -0
  182. package/gs/path/path.ts +301 -0
  183. package/gs/runtime/godoc.txt +331 -0
  184. package/gs/runtime/index.ts +1 -0
  185. package/gs/runtime/runtime.ts +178 -0
  186. package/gs/slices/godoc.txt +44 -0
  187. package/gs/slices/index.ts +1 -0
  188. package/gs/slices/slices.ts +22 -0
  189. package/gs/strings/builder.test.ts +121 -0
  190. package/gs/strings/builder.ts +223 -0
  191. package/gs/strings/clone.test.ts +43 -0
  192. package/gs/strings/clone.ts +17 -0
  193. package/gs/strings/compare.test.ts +84 -0
  194. package/gs/strings/compare.ts +13 -0
  195. package/gs/strings/godoc.txt +66 -0
  196. package/gs/strings/index.ts +2 -0
  197. package/gs/strings/iter.test.ts +343 -0
  198. package/gs/strings/iter.ts +171 -0
  199. package/gs/strings/reader.test.ts +242 -0
  200. package/gs/strings/reader.ts +451 -0
  201. package/gs/strings/replace.test.ts +181 -0
  202. package/gs/strings/replace.ts +1310 -0
  203. package/gs/strings/search.test.ts +214 -0
  204. package/gs/strings/search.ts +213 -0
  205. package/gs/strings/strings.test.ts +477 -0
  206. package/gs/strings/strings.ts +510 -0
  207. package/gs/stringslite/godoc.txt +17 -0
  208. package/gs/stringslite/index.ts +1 -0
  209. package/gs/stringslite/strings.ts +82 -0
  210. package/gs/sync/godoc.txt +21 -0
  211. package/gs/sync/index.ts +1 -0
  212. package/gs/sync/sync.go +64 -0
  213. package/gs/sync/sync.ts +449 -0
  214. package/gs/time/godoc.txt +116 -0
  215. package/gs/time/index.ts +1 -0
  216. package/gs/time/time.ts +585 -0
  217. package/gs/unicode/godoc.txt +52 -0
  218. package/gs/unicode/index.ts +1 -0
  219. package/gs/unicode/unicode.go +38 -0
  220. package/gs/unicode/unicode.ts +418 -0
  221. package/gs/unicode/utf8/godoc.txt +22 -0
  222. package/gs/unicode/utf8/index.ts +2 -0
  223. package/gs/unicode/utf8/utf8.ts +227 -0
  224. package/gs/unsafe/godoc.txt +19 -0
  225. package/gs/unsafe/index.ts +1 -0
  226. package/gs/unsafe/unsafe.test.ts +68 -0
  227. package/gs/unsafe/unsafe.ts +77 -0
  228. package/package.json +4 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unsafe.js","sourceRoot":"","sources":["../../../gs/unsafe/unsafe.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,2FAA2F;AAC3F,mCAAmC;AAYnC,uEAAuE;AACvE,4DAA4D;AAC5D,MAAM,UAAU,OAAO,CAAC,QAAuB;IAC7C,MAAM,IAAI,KAAK,CACb,sHAAsH,CACvH,CAAA;AACH,CAAC;AAED,8EAA8E;AAC9E,4DAA4D;AAC5D,MAAM,UAAU,QAAQ,CAAC,QAAuB;IAC9C,MAAM,IAAI,KAAK,CACb,sIAAsI,CACvI,CAAA;AACH,CAAC;AAED,iEAAiE;AACjE,4DAA4D;AAC5D,MAAM,UAAU,MAAM,CAAC,QAAuB;IAC5C,MAAM,IAAI,KAAK,CACb,gHAAgH,CACjH,CAAA;AACH,CAAC;AAED,sDAAsD;AACtD,+DAA+D;AAC/D,MAAM,UAAU,GAAG,CAAC,GAAY,EAAE,GAAgB;IAChD,MAAM,IAAI,KAAK,CACb,yGAAyG,CAC1G,CAAA;AACH,CAAC;AAED,6DAA6D;AAC7D,4DAA4D;AAC5D,MAAM,UAAU,KAAK,CAAC,GAAY,EAAE,GAAgB;IAClD,MAAM,IAAI,KAAK,CACb,6GAA6G,CAC9G,CAAA;AACH,CAAC;AAED,mEAAmE;AACnE,4DAA4D;AAC5D,MAAM,UAAU,SAAS,CAAC,KAAY;IACpC,MAAM,IAAI,KAAK,CACb,iHAAiH,CAClH,CAAA;AACH,CAAC;AAED,oEAAoE;AACpE,4DAA4D;AAC5D,MAAM,UAAU,MAAM,CAAC,GAAY,EAAE,GAAgB;IACnD,MAAM,IAAI,KAAK,CACb,8GAA8G,CAC/G,CAAA;AACH,CAAC;AAED,kEAAkE;AAClE,4DAA4D;AAC5D,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,MAAM,IAAI,KAAK,CACb,kHAAkH,CACnH,CAAA;AACH,CAAC"}
package/go.mod CHANGED
@@ -4,6 +4,7 @@ go 1.24.3
4
4
 
5
5
  require (
6
6
  github.com/aperturerobotics/cli v1.0.0
7
+ github.com/aperturerobotics/protobuf-go-lite v0.9.1
7
8
  github.com/aperturerobotics/util v1.30.0
8
9
  github.com/pkg/errors v0.9.1
9
10
  github.com/sirupsen/logrus v1.9.3
@@ -12,7 +13,7 @@ require (
12
13
 
13
14
  require (
14
15
  github.com/aperturerobotics/common v0.22.1 // indirect
15
- github.com/stretchr/testify v1.10.0 // indirect
16
+ github.com/aperturerobotics/json-iterator-lite v1.0.1-0.20240713111131-be6bf89c3008 // indirect
16
17
  github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
17
18
  golang.org/x/mod v0.24.0 // indirect
18
19
  golang.org/x/sync v0.14.0 // indirect
package/go.sum CHANGED
@@ -2,13 +2,17 @@ github.com/aperturerobotics/cli v1.0.0 h1:s3xT2h7eBih4/4yZKTn/HQ6P+qpk6ygWZl2416
2
2
  github.com/aperturerobotics/cli v1.0.0/go.mod h1:wtlINjMcKuwyV1x4ftReuA6hHZcPB8kPMXHyQqGFCSc=
3
3
  github.com/aperturerobotics/common v0.22.1 h1:wxTV9wSgfAM9jYUuSzNFzUeC28DQMBgDO3iGahlHeaY=
4
4
  github.com/aperturerobotics/common v0.22.1/go.mod h1:wsPfDVCTNpGHddg/MSfm84rKoO4GAvb+TQtATXz+pKY=
5
+ github.com/aperturerobotics/json-iterator-lite v1.0.1-0.20240713111131-be6bf89c3008 h1:So9JeziaWKx2Fw8sK4AUN/szqKtJ0jEMhS6bU4sHbxs=
6
+ github.com/aperturerobotics/json-iterator-lite v1.0.1-0.20240713111131-be6bf89c3008/go.mod h1:snaApCEDtrHHP6UWSLKiYNOZU9A5NyzccKenx9oZEzg=
7
+ github.com/aperturerobotics/protobuf-go-lite v0.9.1 h1:P1knXKnwLJpVE8fmeXYGckKu79IhqUvKRdCfJNR0MwQ=
8
+ github.com/aperturerobotics/protobuf-go-lite v0.9.1/go.mod h1:fULrxQxEBWKQm7vvju9AfjTp9yfHoLgwMQWTiZQ2tg0=
5
9
  github.com/aperturerobotics/util v1.30.0 h1:OKhFVPnAfR8/dfVNV27EtMr27C0kzwPiStoCwKiint0=
6
10
  github.com/aperturerobotics/util v1.30.0/go.mod h1:T97YTP+FVLegYo5rylOVaPuTLyZyiDqYxD5zVLI9YAc=
7
11
  github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8
12
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9
13
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10
- github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
11
- github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
14
+ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
15
+ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
12
16
  github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
13
17
  github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
14
18
  github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
package/gs/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Hand-written Go Packages
2
+
3
+ This directory contains hand-written .gs.ts files corresponding to select
4
+ packages from the standard library. These packages are hard or impossible to
5
+ transpile correctly, so we hand-write the TypeScript equivalents here.
6
+
@@ -0,0 +1,171 @@
1
+ export * from './varRef.js'
2
+ export * from './channel.js'
3
+ export * from './defer.js'
4
+ export * from './io.js'
5
+ export * from './map.js'
6
+ export * from './slice.js'
7
+ export * from './type.js'
8
+
9
+ // int converts a value to a Go int type, handling proper signed integer conversion
10
+ // This ensures that values like 2147483648 (2^31) are properly handled according to Go semantics
11
+ export function int(value: number): number {
12
+ // In Go, int is typically 64-bit on 64-bit systems, but for compatibility with JavaScript
13
+ // we need to handle the conversion properly. The issue is that JavaScript's number type
14
+ // can represent values larger than 32-bit signed integers, but when cast in certain contexts
15
+ // they get interpreted as signed 32-bit integers.
16
+
17
+ // For Go's int type on 64-bit systems, we should preserve the full value
18
+ // since JavaScript numbers can safely represent integers up to Number.MAX_SAFE_INTEGER
19
+ return Math.trunc(value)
20
+ }
21
+
22
+ // Copy is the Go builtin function that copies the contents of one slice to another.
23
+ // It returns the number of elements copied.
24
+ export function copy<T>(
25
+ dst: T[] | Uint8Array,
26
+ src: T[] | Uint8Array | string,
27
+ ): number {
28
+ // Handle string to Uint8Array copy (common in Go)
29
+ if (typeof src === 'string' && dst instanceof Uint8Array) {
30
+ const encoder = new TextEncoder()
31
+ const srcBytes = encoder.encode(src)
32
+ const n = Math.min(dst.length, srcBytes.length)
33
+ for (let i = 0; i < n; i++) {
34
+ dst[i] = srcBytes[i]
35
+ }
36
+ return n
37
+ }
38
+
39
+ // Handle Uint8Array to Uint8Array copy
40
+ if (dst instanceof Uint8Array && src instanceof Uint8Array) {
41
+ const n = Math.min(dst.length, src.length)
42
+ for (let i = 0; i < n; i++) {
43
+ dst[i] = src[i]
44
+ }
45
+ return n
46
+ }
47
+
48
+ // Handle array to array copy (original implementation)
49
+ if (Array.isArray(dst) && Array.isArray(src)) {
50
+ const n = Math.min(dst.length, src.length)
51
+ for (let i = 0; i < n; i++) {
52
+ dst[i] = src[i]
53
+ }
54
+ return n
55
+ }
56
+
57
+ // Handle mixed types - convert to compatible format
58
+ if (dst instanceof Uint8Array && Array.isArray(src)) {
59
+ const n = Math.min(dst.length, src.length)
60
+ for (let i = 0; i < n; i++) {
61
+ dst[i] = src[i] as number
62
+ }
63
+ return n
64
+ }
65
+
66
+ if (Array.isArray(dst) && src instanceof Uint8Array) {
67
+ const n = Math.min(dst.length, src.length)
68
+ for (let i = 0; i < n; i++) {
69
+ dst[i] = src[i] as T
70
+ }
71
+ return n
72
+ }
73
+
74
+ throw new Error(
75
+ `Unsupported copy operation between ${typeof dst} and ${typeof src}`,
76
+ )
77
+ }
78
+
79
+ // Duration multiplication helper for time package operations
80
+ // Handles expressions like time.Hour * 24
81
+ export function multiplyDuration(duration: any, multiplier: number): any {
82
+ // Check if duration has a multiply method (like our Duration class)
83
+ if (duration && typeof duration.multiply === 'function') {
84
+ return duration.multiply(multiplier)
85
+ }
86
+
87
+ // Check if duration has a valueOf method for numeric operations
88
+ if (duration && typeof duration.valueOf === 'function') {
89
+ const numValue = duration.valueOf()
90
+ // Return an object with the same structure but multiplied value
91
+ if (typeof numValue === 'number') {
92
+ // Try to create a new instance of the same type
93
+ if (duration.constructor) {
94
+ return new duration.constructor(numValue * multiplier)
95
+ }
96
+ // Fallback: return a simple object with valueOf
97
+ return {
98
+ valueOf: () => numValue * multiplier,
99
+ toString: () => (numValue * multiplier).toString() + 'ns',
100
+ }
101
+ }
102
+ }
103
+
104
+ // Fallback for simple numeric values
105
+ if (typeof duration === 'number') {
106
+ return duration * multiplier
107
+ }
108
+
109
+ throw new Error(`Cannot multiply duration of type ${typeof duration}`)
110
+ }
111
+
112
+ /**
113
+ * Normalizes various byte representations into a `Uint8Array` for protobuf compatibility.
114
+ *
115
+ * @param {Uint8Array | number[] | null | undefined | { data: number[] } | { valueOf(): number[] }} bytes
116
+ * The input to normalize. Accepted types:
117
+ * - `Uint8Array`: Returned as-is.
118
+ * - `number[]`: Converted to a `Uint8Array`.
119
+ * - `null` or `undefined`: Returns an empty `Uint8Array`.
120
+ * - `{ data: number[] }`: An object with a `data` property (e.g., `$.Slice<number>`), where `data` is a `number[]`.
121
+ * - `{ valueOf(): number[] }`: An object with a `valueOf` method that returns a `number[]`.
122
+ * @returns {Uint8Array} A normalized `Uint8Array` representation of the input.
123
+ * @throws {Error} If the input type is unsupported or cannot be normalized.
124
+ */
125
+ export function normalizeBytes(
126
+ bytes:
127
+ | Uint8Array
128
+ | number[]
129
+ | null
130
+ | undefined
131
+ | { data: number[] }
132
+ | { valueOf(): number[] },
133
+ ): Uint8Array {
134
+ if (bytes === null || bytes === undefined) {
135
+ return new Uint8Array(0)
136
+ }
137
+
138
+ if (bytes instanceof Uint8Array) {
139
+ return bytes
140
+ }
141
+
142
+ // Handle $.Slice<number> (which has a .data property that's a number[])
143
+ if (
144
+ bytes &&
145
+ typeof bytes === 'object' &&
146
+ 'data' in bytes &&
147
+ Array.isArray(bytes.data)
148
+ ) {
149
+ return new Uint8Array(bytes.data)
150
+ }
151
+
152
+ // Handle plain number arrays
153
+ if (Array.isArray(bytes)) {
154
+ return new Uint8Array(bytes)
155
+ }
156
+
157
+ // Handle objects with valueOf() method that returns a number array
158
+ if (
159
+ bytes &&
160
+ typeof bytes === 'object' &&
161
+ 'valueOf' in bytes &&
162
+ typeof bytes.valueOf === 'function'
163
+ ) {
164
+ const value = bytes.valueOf()
165
+ if (Array.isArray(value)) {
166
+ return new Uint8Array(value)
167
+ }
168
+ }
169
+
170
+ throw new Error(`Cannot normalize bytes of type ${typeof bytes}: ${bytes}`)
171
+ }