typenative 0.7.0 → 0.7.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 (217) hide show
  1. package/bin/node/assert.js +2 -2
  2. package/bin/node/child_process.js +66 -8
  3. package/bin/node/fs.js +1012 -4
  4. package/bin/node/histogram.js +1991 -0
  5. package/bin/node/process.js +14 -2
  6. package/bin/node/sqlite.js +1488 -5
  7. package/bin/node/stream.js +2 -1
  8. package/bin/node/system_extra.js +3 -270
  9. package/bin/node/test.js +39 -1
  10. package/bin/node/test_report.js +47 -1
  11. package/bin/node/test_run.js +57 -2
  12. package/bin/node/util.js +8 -5
  13. package/bin/node/webassembly.js +20 -5
  14. package/bin/node/webview.js +3257 -244
  15. package/bin/node/zlib.js +118 -14
  16. package/bin/node/zlib_iter.js +585 -0
  17. package/bin/transpiler.js +1370 -258
  18. package/bin/version.js +1 -1
  19. package/govendor/software.sslmate.com/src/go-pkcs12/.gitattributes +10 -0
  20. package/govendor/software.sslmate.com/src/go-pkcs12/LICENSE +28 -0
  21. package/govendor/software.sslmate.com/src/go-pkcs12/README.md +31 -0
  22. package/govendor/software.sslmate.com/src/go-pkcs12/bmp-string.go +63 -0
  23. package/govendor/software.sslmate.com/src/go-pkcs12/crypto.go +331 -0
  24. package/govendor/software.sslmate.com/src/go-pkcs12/errors.go +23 -0
  25. package/govendor/software.sslmate.com/src/go-pkcs12/go.mod +3 -0
  26. package/govendor/software.sslmate.com/src/go-pkcs12/internal/rc2/rc2.go +271 -0
  27. package/govendor/software.sslmate.com/src/go-pkcs12/mac.go +202 -0
  28. package/govendor/software.sslmate.com/src/go-pkcs12/pbkdf.go +184 -0
  29. package/govendor/software.sslmate.com/src/go-pkcs12/pkcs12.go +1000 -0
  30. package/govendor/software.sslmate.com/src/go-pkcs12/safebags.go +107 -0
  31. package/package.json +10 -1
  32. package/types/tsconfig.json +17 -0
  33. package/types/typenative-npm.d.ts +492 -12
  34. package/types/typenative.d.ts +88 -2
  35. package/.gitattributes +0 -1
  36. package/.github/workflows/ci.yml +0 -68
  37. package/.github/workflows/npm-publish-github-packages.yml +0 -88
  38. package/CHANGELOG.md +0 -1746
  39. package/CONTRIBUTION.md +0 -133
  40. package/conformance.md +0 -1497
  41. package/examples/README.md +0 -114
  42. package/examples/angular-templates/README.md +0 -141
  43. package/examples/angular-templates/bench.mjs +0 -173
  44. package/examples/angular-templates/dom-type-globals.d.ts +0 -21
  45. package/examples/angular-templates/dom-type-stubs.ts +0 -21
  46. package/examples/angular-templates/main.ts +0 -135
  47. package/examples/angular-templates/parity.mjs +0 -157
  48. package/examples/angular-templates/run-node.mjs +0 -69
  49. package/examples/angular-templates/vendor/angular/compiler/LICENSE +0 -21
  50. package/examples/desktop-notes/README.md +0 -106
  51. package/examples/desktop-notes/bench.mjs +0 -125
  52. package/examples/desktop-notes/main.ts +0 -131
  53. package/examples/desktop-notes/package-lock.json +0 -726
  54. package/examples/desktop-notes/package.json +0 -9
  55. package/examples/desktop-notes/ui.ts +0 -176
  56. package/examples/dupe-finder/README.md +0 -78
  57. package/examples/dupe-finder/bench.mjs +0 -120
  58. package/examples/dupe-finder/main.ts +0 -87
  59. package/examples/fibonacci/README.md +0 -89
  60. package/examples/fibonacci/bench.mjs +0 -87
  61. package/examples/fibonacci/main.ts +0 -21
  62. package/examples/hello-world/README.md +0 -62
  63. package/examples/hello-world/bench.mjs +0 -66
  64. package/examples/hello-world/main.ts +0 -7
  65. package/examples/json-server/README.md +0 -132
  66. package/examples/json-server/bench.mjs +0 -263
  67. package/examples/json-server/main.ts +0 -67
  68. package/examples/mandelbrot-png/README.md +0 -63
  69. package/examples/mandelbrot-png/bench.mjs +0 -95
  70. package/examples/mandelbrot-png/main.ts +0 -98
  71. package/examples/mandelbrot-png/preview.png +0 -0
  72. package/examples/marked-cli/README.md +0 -174
  73. package/examples/marked-cli/bench.mjs +0 -178
  74. package/examples/marked-cli/main.ts +0 -410
  75. package/examples/marked-cli/parity.mjs +0 -185
  76. package/examples/marked-cli/vendor/marked/LICENSE +0 -44
  77. package/examples/marked-cli/vendor/marked/VENDOR.md +0 -26
  78. package/examples/openai-chat/README.md +0 -148
  79. package/examples/openai-chat/bench.mjs +0 -175
  80. package/examples/openai-chat/main.ts +0 -488
  81. package/examples/react-compiler/README.md +0 -185
  82. package/examples/react-compiler/bench.mjs +0 -181
  83. package/examples/react-compiler/main.ts +0 -324
  84. package/examples/react-compiler/parity.mjs +0 -362
  85. package/examples/react-compiler/run-node.mjs +0 -90
  86. package/examples/react-compiler/vendor/babel/LICENSE +0 -22
  87. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +0 -15
  88. package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +0 -51
  89. package/examples/react-compiler/vendor/charcodes/package.json +0 -25
  90. package/examples/react-compiler/vendor/invariant/LICENSE +0 -21
  91. package/examples/react-compiler/vendor/invariant/invariant.cjs +0 -51
  92. package/examples/react-compiler/vendor/invariant/invariant.d.cts +0 -11
  93. package/examples/react-compiler/vendor/invariant/types/LICENSE +0 -21
  94. package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +0 -19
  95. package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +0 -19
  96. package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +0 -19
  97. package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +0 -19
  98. package/examples/react-compiler/vendor/js-tokens/LICENSE +0 -21
  99. package/examples/react-compiler/vendor/js-tokens/index.cjs +0 -23
  100. package/examples/react-compiler/vendor/js-tokens/index.d.cts +0 -13
  101. package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +0 -20
  102. package/examples/react-compiler/vendor/jsesc/jsesc.cjs +0 -337
  103. package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +0 -384
  104. package/examples/react-compiler/vendor/jsesc/types/LICENSE +0 -21
  105. package/examples/react-compiler/vendor/picocolors/LICENSE +0 -15
  106. package/examples/react-compiler/vendor/picocolors/picocolors.cjs +0 -75
  107. package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +0 -5
  108. package/examples/react-compiler/vendor/picocolors/types.d.ts +0 -51
  109. package/examples/react-compiler/vendor/react-compiler/LICENSE +0 -21
  110. package/examples/react-compiler/vendor/stubs/babel-core.ts +0 -31
  111. package/examples/react-compiler/vendor/stubs/debug.ts +0 -18
  112. package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +0 -13
  113. package/examples/react-compiler/vendor/stubs/pretty-format.ts +0 -18
  114. package/examples/react-compiler/vendor/zod/LICENSE +0 -21
  115. package/examples/react-compiler/vendor/zod-validation-error/LICENSE +0 -9
  116. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +0 -5
  117. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +0 -49
  118. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +0 -48
  119. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +0 -50
  120. package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +0 -6
  121. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +0 -126
  122. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +0 -39
  123. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +0 -12
  124. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +0 -78
  125. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +0 -2
  126. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +0 -12
  127. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +0 -12
  128. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +0 -131
  129. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +0 -54
  130. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +0 -12
  131. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +0 -40
  132. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +0 -12
  133. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +0 -76
  134. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +0 -76
  135. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +0 -27
  136. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +0 -27
  137. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +0 -13
  138. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +0 -61
  139. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +0 -38
  140. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +0 -20
  141. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +0 -5
  142. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +0 -8
  143. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +0 -11
  144. package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +0 -20
  145. package/examples/react-compiler/vendor.mjs +0 -688
  146. package/examples/todo-cli/README.md +0 -69
  147. package/examples/todo-cli/bench.mjs +0 -89
  148. package/examples/todo-cli/main.ts +0 -92
  149. package/examples/yaml-cli/README.md +0 -152
  150. package/examples/yaml-cli/bench.mjs +0 -265
  151. package/examples/yaml-cli/main.ts +0 -251
  152. package/examples/yaml-cli/parity.mjs +0 -156
  153. package/examples/yaml-cli/vendor/yaml/LICENSE +0 -13
  154. package/examples/yaml-cli/vendor/yaml/VENDOR.md +0 -25
  155. package/performance/README.md +0 -248
  156. package/performance/any-dynamic.ts +0 -21
  157. package/performance/any-record-view.ts +0 -20
  158. package/performance/arr-for-of.ts +0 -29
  159. package/performance/arr-hof.ts +0 -21
  160. package/performance/arr-push-index.ts +0 -15
  161. package/performance/arr-push-local.ts +0 -39
  162. package/performance/arr-slice-concat.ts +0 -24
  163. package/performance/arr-sort.ts +0 -21
  164. package/performance/async-await.ts +0 -24
  165. package/performance/async-promise-all.ts +0 -38
  166. package/performance/bigint-arith.ts +0 -36
  167. package/performance/buf-encode.ts +0 -29
  168. package/performance/class-accessors.ts +0 -48
  169. package/performance/class-dispatch.ts +0 -52
  170. package/performance/date-format.ts +0 -18
  171. package/performance/events-emit.ts +0 -27
  172. package/performance/exc-throw-rare.ts +0 -25
  173. package/performance/exc-try-catch.ts +0 -26
  174. package/performance/fn-closures.ts +0 -29
  175. package/performance/fn-recursion-tree.ts +0 -31
  176. package/performance/fn-rest-spread.ts +0 -43
  177. package/performance/gen-for-of.ts +0 -46
  178. package/performance/gen-next.ts +0 -32
  179. package/performance/iter-helpers.ts +0 -38
  180. package/performance/json-any.ts +0 -30
  181. package/performance/json-roundtrip.ts +0 -27
  182. package/performance/map-delete.ts +0 -24
  183. package/performance/map-iterate.ts +0 -26
  184. package/performance/map-number-keys.ts +0 -37
  185. package/performance/map-string-keys.ts +0 -22
  186. package/performance/num-arith-loop.ts +0 -14
  187. package/performance/num-bitwise.ts +0 -21
  188. package/performance/num-hash-imul.ts +0 -36
  189. package/performance/num-mod-int.ts +0 -16
  190. package/performance/num-to-string.ts +0 -16
  191. package/performance/obj-alloc.ts +0 -32
  192. package/performance/obj-keys-entries.ts +0 -44
  193. package/performance/obj-optional-chain.ts +0 -42
  194. package/performance/obj-record-dict.ts +0 -24
  195. package/performance/obj-spread-destructure.ts +0 -40
  196. package/performance/regex-exec.ts +0 -29
  197. package/performance/regex-replace-fn.ts +0 -24
  198. package/performance/regex-site.ts +0 -28
  199. package/performance/set-ops.ts +0 -20
  200. package/performance/str-char-scan.ts +0 -35
  201. package/performance/str-concat-plus.ts +0 -18
  202. package/performance/str-concat.ts +0 -24
  203. package/performance/str-methods.ts +0 -24
  204. package/performance/str-short-scan.ts +0 -26
  205. package/performance/str-split-join.ts +0 -23
  206. package/performance/str-switch-compare.ts +0 -50
  207. package/performance/str-template.ts +0 -16
  208. package/performance/str-unicode.ts +0 -25
  209. package/performance/typed-array.ts +0 -30
  210. package/performance/union-switch.ts +0 -57
  211. package/performance/union-typeof.ts +0 -40
  212. package/performance/wasm-call.ts +0 -63
  213. package/tsparser/check.go +0 -158
  214. package/tsparser/go.mod +0 -14
  215. package/tsparser/go.sum +0 -22
  216. package/tsparser/main.go +0 -363
  217. package/tsparser/tsparser.exe +0 -4
package/bin/node/fs.js CHANGED
@@ -1132,7 +1132,10 @@ func TnFsExists(path string, cb interface{}) {
1132
1132
 
1133
1133
  // fs/promises FileHandle
1134
1134
  type TnFileHandle struct {
1135
- fd float64
1135
+ fd float64
1136
+ mu sync.Mutex
1137
+ closed bool
1138
+ locked bool
1136
1139
  }
1137
1140
 
1138
1141
  type TnFsReadResult struct {
@@ -1202,8 +1205,1008 @@ func (h *TnFileHandle) sync() chan struct{} {
1202
1205
  return TnFsPromiseVoid(func() { TnFsync(h.fd) })
1203
1206
  }
1204
1207
 
1208
+ func (h *TnFileHandle) closeSync() {
1209
+ h.mu.Lock()
1210
+ if h.closed {
1211
+ h.mu.Unlock()
1212
+ return
1213
+ }
1214
+ fd := h.fd
1215
+ h.closed = true
1216
+ h.fd = -1
1217
+ h.mu.Unlock()
1218
+ if fd >= 0 {
1219
+ TnClose(fd)
1220
+ }
1221
+ }
1222
+
1205
1223
  func (h *TnFileHandle) close() chan struct{} {
1206
- return TnFsPromiseVoid(func() { TnClose(h.fd) })
1224
+ return TnFsPromiseVoid(func() { h.closeSync() })
1225
+ }
1226
+
1227
+ func (h *TnFileHandle) checkAvailable() {
1228
+ h.mu.Lock()
1229
+ closed, locked := h.closed || h.fd < 0, h.locked
1230
+ h.mu.Unlock()
1231
+ if closed {
1232
+ panic(TnNodeError("Error", "ERR_INVALID_STATE", "The FileHandle is closed"))
1233
+ }
1234
+ if locked {
1235
+ panic(TnNodeError("Error", "ERR_INVALID_STATE", "The FileHandle is locked"))
1236
+ }
1237
+ }
1238
+
1239
+ func (h *TnFileHandle) lockForIteration() float64 {
1240
+ h.mu.Lock()
1241
+ defer h.mu.Unlock()
1242
+ if h.closed || h.fd < 0 {
1243
+ panic(TnNodeError("Error", "ERR_INVALID_STATE", "The FileHandle is closed"))
1244
+ }
1245
+ if h.locked {
1246
+ panic(TnNodeError("Error", "ERR_INVALID_STATE", "The FileHandle is locked"))
1247
+ }
1248
+ h.locked = true
1249
+ return h.fd
1250
+ }
1251
+
1252
+ func (h *TnFileHandle) unlock() {
1253
+ h.mu.Lock()
1254
+ h.locked = false
1255
+ h.mu.Unlock()
1256
+ }
1257
+
1258
+ // FileHandle pull / pullSync / writer (stream/iter)
1259
+ const tnFsDefaultChunkSize = 131072
1260
+
1261
+ type tnFileIterOpts struct {
1262
+ autoClose bool
1263
+ chunkSize int
1264
+ start int64
1265
+ limit int64
1266
+ signal interface{}
1267
+ }
1268
+
1269
+ func tnFsParseIterOpts(opts interface{}, allowSignal bool) tnFileIterOpts {
1270
+ res := tnFileIterOpts{
1271
+ chunkSize: tnFsDefaultChunkSize,
1272
+ start: -1,
1273
+ limit: -1,
1274
+ }
1275
+ if opts == nil {
1276
+ return res
1277
+ }
1278
+ if v := TnGetValue(opts, "autoClose"); v != nil {
1279
+ b, ok := v.(bool)
1280
+ if !ok {
1281
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options.autoClose\\" property must be of type boolean"))
1282
+ }
1283
+ res.autoClose = b
1284
+ }
1285
+ if v := TnGetValue(opts, "start"); v != nil {
1286
+ f, ok := v.(float64)
1287
+ if !ok {
1288
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options.start\\" property must be of type number"))
1289
+ }
1290
+ if math.IsNaN(f) || math.IsInf(f, 0) || math.Trunc(f) != f || f < 0 {
1291
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", fmt.Sprintf("The value of \\"options.start\\" is out of range. It must be >= 0. Received %v", f)))
1292
+ }
1293
+ res.start = int64(f)
1294
+ }
1295
+ if v := TnGetValue(opts, "limit"); v != nil {
1296
+ f, ok := v.(float64)
1297
+ if !ok {
1298
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options.limit\\" property must be of type number"))
1299
+ }
1300
+ if math.IsNaN(f) || math.IsInf(f, 0) || math.Trunc(f) != f || f < 1 {
1301
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", fmt.Sprintf("The value of \\"options.limit\\" is out of range. It must be >= 1. Received %v", f)))
1302
+ }
1303
+ res.limit = int64(f)
1304
+ }
1305
+ if v := TnGetValue(opts, "chunkSize"); v != nil {
1306
+ f, ok := v.(float64)
1307
+ if !ok {
1308
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"options.chunkSize\\" property must be of type number"))
1309
+ }
1310
+ if math.IsNaN(f) || math.IsInf(f, 0) || math.Trunc(f) != f || f < 1 {
1311
+ panic(TnNodeError("RangeError", "ERR_OUT_OF_RANGE", fmt.Sprintf("The value of \\"options.chunkSize\\" is out of range. It must be >= 1. Received %v", f)))
1312
+ }
1313
+ res.chunkSize = int(f)
1314
+ }
1315
+ if allowSignal {
1316
+ if v := TnGetValue(opts, "signal"); v != nil {
1317
+ res.signal = v
1318
+ }
1319
+ }
1320
+ return res
1321
+ }
1322
+
1323
+ type tnFsStatefulTransform interface {
1324
+ tnTransformSeq(seq iter.Seq[[][]byte], signal interface{}) iter.Seq[[][]byte]
1325
+ }
1326
+
1327
+ func tnFsIsStatefulTransform(a interface{}) bool {
1328
+ if _, ok := a.(tnFsStatefulTransform); ok {
1329
+ return true
1330
+ }
1331
+ if tf := TnGetValue(a, "transform"); tf != nil && reflect.ValueOf(tf).Kind() == reflect.Func {
1332
+ return true
1333
+ }
1334
+ return false
1335
+ }
1336
+
1337
+ func tnFsParsePullArgs(args []interface{}, allowSignal bool) ([]interface{}, tnFileIterOpts) {
1338
+ var transforms []interface{}
1339
+ var optsArg interface{}
1340
+ for i, a := range args {
1341
+ if a == nil {
1342
+ continue
1343
+ }
1344
+ if reflect.ValueOf(a).Kind() == reflect.Func || tnFsIsStatefulTransform(a) {
1345
+ transforms = append(transforms, a)
1346
+ continue
1347
+ }
1348
+ if i == len(args)-1 {
1349
+ optsArg = a
1350
+ }
1351
+ }
1352
+ return transforms, tnFsParseIterOpts(optsArg, allowSignal)
1353
+ }
1354
+
1355
+ func tnFsReadChunk(fd float64, toRead int, pos int64) []byte {
1356
+ f := tnFile(fd)
1357
+ buf := make([]byte, toRead)
1358
+ var n int
1359
+ var err error
1360
+ if pos >= 0 {
1361
+ n, err = f.ReadAt(buf, pos)
1362
+ } else {
1363
+ n, err = f.Read(buf)
1364
+ }
1365
+ if err != nil && err != io.EOF {
1366
+ tnFsCheck(err, "read", "")
1367
+ }
1368
+ if n == 0 {
1369
+ return nil
1370
+ }
1371
+ return buf[:n]
1372
+ }
1373
+
1374
+ func tnFsToBytes(v interface{}) ([]byte, *TnError) {
1375
+ switch x := v.(type) {
1376
+ case []byte:
1377
+ return x, nil
1378
+ case string:
1379
+ return []byte(x), nil
1380
+ }
1381
+ return nil, TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"chunk\\" argument must be an instance of Uint8Array or a string")
1382
+ }
1383
+
1384
+ func tnFsToByteSlice(v interface{}) ([][]byte, *TnError) {
1385
+ switch x := v.(type) {
1386
+ case [][]byte:
1387
+ return x, nil
1388
+ case []string:
1389
+ out := make([][]byte, len(x))
1390
+ for i, s := range x {
1391
+ out[i] = []byte(s)
1392
+ }
1393
+ return out, nil
1394
+ case []interface{}:
1395
+ out := make([][]byte, len(x))
1396
+ for i, el := range x {
1397
+ b, err := tnFsToBytes(el)
1398
+ if err != nil {
1399
+ return nil, err
1400
+ }
1401
+ out[i] = b
1402
+ }
1403
+ return out, nil
1404
+ }
1405
+ return nil, TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"chunks\\" argument must be an array of Uint8Array or string")
1406
+ }
1407
+
1408
+ func tnFsNormalizeBatch(v interface{}) [][]byte {
1409
+ if v == nil {
1410
+ return nil
1411
+ }
1412
+ switch x := v.(type) {
1413
+ case [][]byte:
1414
+ if len(x) == 0 {
1415
+ return nil
1416
+ }
1417
+ return x
1418
+ case []byte:
1419
+ return [][]byte{x}
1420
+ case string:
1421
+ return [][]byte{[]byte(x)}
1422
+ case []string:
1423
+ if len(x) == 0 {
1424
+ return nil
1425
+ }
1426
+ out := make([][]byte, len(x))
1427
+ for i, s := range x {
1428
+ out[i] = []byte(s)
1429
+ }
1430
+ return out
1431
+ case []interface{}:
1432
+ if len(x) == 0 {
1433
+ return nil
1434
+ }
1435
+ out := make([][]byte, 0, len(x))
1436
+ for _, el := range x {
1437
+ if b, err := tnFsToBytes(el); err == nil {
1438
+ out = append(out, b)
1439
+ }
1440
+ }
1441
+ if len(out) == 0 {
1442
+ return nil
1443
+ }
1444
+ return out
1445
+ }
1446
+ return nil
1447
+ }
1448
+
1449
+ func tnFsApplyTransforms(transforms []interface{}, batch [][]byte, async bool, signal interface{}) [][]byte {
1450
+ if len(transforms) == 0 {
1451
+ return batch
1452
+ }
1453
+ cur := batch
1454
+ var arg interface{}
1455
+ if cur != nil {
1456
+ arg = cur
1457
+ }
1458
+ for _, tr := range transforms {
1459
+ var res interface{}
1460
+ if async && signal != nil {
1461
+ res = TnInvoke(tr, arg, map[string]interface{}{"signal": signal})
1462
+ } else {
1463
+ res = TnInvoke(tr, arg)
1464
+ }
1465
+ if async {
1466
+ res = TnAwaitValue(res, false)
1467
+ }
1468
+ cur = tnFsNormalizeBatch(res)
1469
+ if cur == nil {
1470
+ return nil
1471
+ }
1472
+ arg = cur
1473
+ }
1474
+ return cur
1475
+ }
1476
+
1477
+ func tnFsFlushTransforms(transforms []interface{}, async bool, signal interface{}, emit func([][]byte) bool) {
1478
+ for i, tr := range transforms {
1479
+ var res interface{}
1480
+ if async && signal != nil {
1481
+ res = TnInvoke(tr, nil, map[string]interface{}{"signal": signal})
1482
+ } else {
1483
+ res = TnInvoke(tr, nil)
1484
+ }
1485
+ if async {
1486
+ res = TnAwaitValue(res, false)
1487
+ }
1488
+ cur := tnFsNormalizeBatch(res)
1489
+ if cur != nil && i+1 < len(transforms) {
1490
+ cur = tnFsApplyTransforms(transforms[i+1:], cur, async, signal)
1491
+ }
1492
+ if len(cur) > 0 {
1493
+ if !emit(cur) {
1494
+ return
1495
+ }
1496
+ }
1497
+ }
1498
+ }
1499
+
1500
+ func tnFsHasStatefulTransform(transforms []interface{}) bool {
1501
+ for _, tr := range transforms {
1502
+ if tnFsIsStatefulTransform(tr) {
1503
+ return true
1504
+ }
1505
+ }
1506
+ return false
1507
+ }
1508
+
1509
+ func tnFsComposeTransforms(base iter.Seq[[][]byte], transforms []interface{}, async bool, signal interface{}) iter.Seq[[][]byte] {
1510
+ seq := base
1511
+ for _, tr := range transforms {
1512
+ curSeq := seq
1513
+ curTr := tr
1514
+ if st, ok := curTr.(tnFsStatefulTransform); ok {
1515
+ outSeq := st.tnTransformSeq(curSeq, signal)
1516
+ seq = func(yield func([][]byte) bool) {
1517
+ for batch := range outSeq {
1518
+ if len(batch) > 0 {
1519
+ if !yield(batch) {
1520
+ return
1521
+ }
1522
+ }
1523
+ }
1524
+ yield(nil)
1525
+ }
1526
+ } else {
1527
+ seq = func(yield func([][]byte) bool) {
1528
+ for batch := range curSeq {
1529
+ var arg interface{}
1530
+ if batch != nil {
1531
+ arg = batch
1532
+ }
1533
+ var res interface{}
1534
+ if async && signal != nil {
1535
+ res = TnInvoke(curTr, arg, map[string]interface{}{"signal": signal})
1536
+ } else {
1537
+ res = TnInvoke(curTr, arg)
1538
+ }
1539
+ if async {
1540
+ res = TnAwaitValue(res, false)
1541
+ }
1542
+ out := tnFsNormalizeBatch(res)
1543
+ if len(out) > 0 {
1544
+ if !yield(out) {
1545
+ return
1546
+ }
1547
+ }
1548
+ if batch == nil {
1549
+ yield(nil)
1550
+ return
1551
+ }
1552
+ }
1553
+ }
1554
+ }
1555
+ }
1556
+ return seq
1557
+ }
1558
+
1559
+ type TnFilePull struct {
1560
+ handle *TnFileHandle
1561
+ opts tnFileIterOpts
1562
+ transforms []interface{}
1563
+ }
1564
+
1565
+ func (h *TnFileHandle) pull(args ...interface{}) *TnFilePull {
1566
+ h.checkAvailable()
1567
+ transforms, opts := tnFsParsePullArgs(args, true)
1568
+ return &TnFilePull{handle: h, opts: opts, transforms: transforms}
1569
+ }
1570
+
1571
+ func (p *TnFilePull) tnIterator() iter.Seq[[][]byte] {
1572
+ return func(yield func([][]byte) bool) {
1573
+ fd := p.handle.lockForIteration()
1574
+ TnLoopRef()
1575
+ defer func() {
1576
+ p.handle.unlock()
1577
+ TnLoopUnref()
1578
+ if p.opts.autoClose {
1579
+ p.handle.closeSync()
1580
+ }
1581
+ }()
1582
+ pos := p.opts.start
1583
+ remaining := p.opts.limit
1584
+ readSize := p.opts.chunkSize
1585
+ signal := p.opts.signal
1586
+ if tnFsHasStatefulTransform(p.transforms) {
1587
+ base := func(emit func([][]byte) bool) {
1588
+ for remaining != 0 {
1589
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1590
+ panic(TnGetValue(signal, "reason"))
1591
+ }
1592
+ toRead := readSize
1593
+ if remaining > 0 && int64(toRead) > remaining {
1594
+ toRead = int(remaining)
1595
+ }
1596
+ curPos := pos
1597
+ chunk := TnAwait(TnFsPromise(func() []byte {
1598
+ return tnFsReadChunk(fd, toRead, curPos)
1599
+ }))
1600
+ if len(chunk) == 0 {
1601
+ break
1602
+ }
1603
+ if pos >= 0 {
1604
+ pos += int64(len(chunk))
1605
+ }
1606
+ if remaining > 0 {
1607
+ remaining -= int64(len(chunk))
1608
+ }
1609
+ if !emit([][]byte{chunk}) {
1610
+ return
1611
+ }
1612
+ }
1613
+ emit(nil)
1614
+ }
1615
+ for batch := range tnFsComposeTransforms(base, p.transforms, true, signal) {
1616
+ if len(batch) > 0 {
1617
+ if !yield(batch) {
1618
+ return
1619
+ }
1620
+ }
1621
+ }
1622
+ return
1623
+ }
1624
+ for remaining != 0 {
1625
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1626
+ panic(TnGetValue(signal, "reason"))
1627
+ }
1628
+ toRead := readSize
1629
+ if remaining > 0 && int64(toRead) > remaining {
1630
+ toRead = int(remaining)
1631
+ }
1632
+ curPos := pos
1633
+ chunk := TnAwait(TnFsPromise(func() []byte {
1634
+ return tnFsReadChunk(fd, toRead, curPos)
1635
+ }))
1636
+ if len(chunk) == 0 {
1637
+ break
1638
+ }
1639
+ if pos >= 0 {
1640
+ pos += int64(len(chunk))
1641
+ }
1642
+ if remaining > 0 {
1643
+ remaining -= int64(len(chunk))
1644
+ }
1645
+ batch := tnFsApplyTransforms(p.transforms, [][]byte{chunk}, true, signal)
1646
+ if len(batch) > 0 {
1647
+ if !yield(batch) {
1648
+ return
1649
+ }
1650
+ }
1651
+ }
1652
+ if len(p.transforms) > 0 {
1653
+ tnFsFlushTransforms(p.transforms, true, signal, yield)
1654
+ }
1655
+ }
1656
+ }
1657
+
1658
+ type TnFilePullSync struct {
1659
+ handle *TnFileHandle
1660
+ opts tnFileIterOpts
1661
+ transforms []interface{}
1662
+ }
1663
+
1664
+ func (h *TnFileHandle) pullSync(args ...interface{}) *TnFilePullSync {
1665
+ h.checkAvailable()
1666
+ transforms, opts := tnFsParsePullArgs(args, false)
1667
+ return &TnFilePullSync{handle: h, opts: opts, transforms: transforms}
1668
+ }
1669
+
1670
+ func (p *TnFilePullSync) tnIterator() iter.Seq[[][]byte] {
1671
+ return func(yield func([][]byte) bool) {
1672
+ fd := p.handle.lockForIteration()
1673
+ defer func() {
1674
+ p.handle.unlock()
1675
+ if p.opts.autoClose {
1676
+ p.handle.closeSync()
1677
+ }
1678
+ }()
1679
+ pos := p.opts.start
1680
+ remaining := p.opts.limit
1681
+ readSize := p.opts.chunkSize
1682
+ if tnFsHasStatefulTransform(p.transforms) {
1683
+ base := func(emit func([][]byte) bool) {
1684
+ for remaining != 0 {
1685
+ toRead := readSize
1686
+ if remaining > 0 && int64(toRead) > remaining {
1687
+ toRead = int(remaining)
1688
+ }
1689
+ chunk := tnFsReadChunk(fd, toRead, pos)
1690
+ if len(chunk) == 0 {
1691
+ break
1692
+ }
1693
+ if pos >= 0 {
1694
+ pos += int64(len(chunk))
1695
+ }
1696
+ if remaining > 0 {
1697
+ remaining -= int64(len(chunk))
1698
+ }
1699
+ if !emit([][]byte{chunk}) {
1700
+ return
1701
+ }
1702
+ }
1703
+ emit(nil)
1704
+ }
1705
+ for batch := range tnFsComposeTransforms(base, p.transforms, false, nil) {
1706
+ if len(batch) > 0 {
1707
+ if !yield(batch) {
1708
+ return
1709
+ }
1710
+ }
1711
+ }
1712
+ return
1713
+ }
1714
+ for remaining != 0 {
1715
+ toRead := readSize
1716
+ if remaining > 0 && int64(toRead) > remaining {
1717
+ toRead = int(remaining)
1718
+ }
1719
+ chunk := tnFsReadChunk(fd, toRead, pos)
1720
+ if len(chunk) == 0 {
1721
+ break
1722
+ }
1723
+ if pos >= 0 {
1724
+ pos += int64(len(chunk))
1725
+ }
1726
+ if remaining > 0 {
1727
+ remaining -= int64(len(chunk))
1728
+ }
1729
+ batch := tnFsApplyTransforms(p.transforms, [][]byte{chunk}, false, nil)
1730
+ if len(batch) > 0 {
1731
+ if !yield(batch) {
1732
+ return
1733
+ }
1734
+ }
1735
+ }
1736
+ if len(p.transforms) > 0 {
1737
+ tnFsFlushTransforms(p.transforms, false, nil, yield)
1738
+ }
1739
+ }
1740
+ }
1741
+
1742
+ type TnFileWriter struct {
1743
+ handle *TnFileHandle
1744
+ fd float64
1745
+ mu sync.Mutex
1746
+ autoClose bool
1747
+ pos int64
1748
+ bytesRemaining int64
1749
+ syncThreshold int
1750
+ totalBytesWritten int64
1751
+ closed bool
1752
+ closing bool
1753
+ errored bool
1754
+ released bool
1755
+ errVal interface{}
1756
+ asyncPending int
1757
+ writeTail chan struct{}
1758
+ endDone chan struct{}
1759
+ }
1760
+
1761
+ func (h *TnFileHandle) writer(options ...interface{}) *TnFileWriter {
1762
+ h.checkAvailable()
1763
+ var optsArg interface{}
1764
+ if len(options) > 0 {
1765
+ optsArg = options[0]
1766
+ }
1767
+ opts := tnFsParseIterOpts(optsArg, false)
1768
+ fd := h.lockForIteration()
1769
+ TnLoopRef()
1770
+ return &TnFileWriter{
1771
+ handle: h,
1772
+ fd: fd,
1773
+ autoClose: opts.autoClose,
1774
+ pos: opts.start,
1775
+ bytesRemaining: opts.limit,
1776
+ syncThreshold: opts.chunkSize,
1777
+ }
1778
+ }
1779
+
1780
+ func (w *TnFileWriter) releaseLocked() {
1781
+ if w.released {
1782
+ return
1783
+ }
1784
+ w.released = true
1785
+ w.handle.unlock()
1786
+ TnLoopUnref()
1787
+ if w.autoClose {
1788
+ w.handle.closeSync()
1789
+ }
1790
+ }
1791
+
1792
+ func (w *TnFileWriter) writeBufAll(b []byte, position int64) (int64, error) {
1793
+ f := tnFile(w.fd)
1794
+ var written int64
1795
+ for len(b) > 0 {
1796
+ var n int
1797
+ var err error
1798
+ if position >= 0 {
1799
+ n, err = f.WriteAt(b, position)
1800
+ } else {
1801
+ n, err = f.Write(b)
1802
+ }
1803
+ written += int64(n)
1804
+ if position >= 0 {
1805
+ position += int64(n)
1806
+ }
1807
+ b = b[n:]
1808
+ if err != nil {
1809
+ return written, err
1810
+ }
1811
+ if n == 0 {
1812
+ return written, io.ErrShortWrite
1813
+ }
1814
+ }
1815
+ return written, nil
1816
+ }
1817
+
1818
+ func tnFsWriterSignal(options []interface{}) interface{} {
1819
+ if len(options) > 0 && options[0] != nil {
1820
+ return TnGetValue(options[0], "signal")
1821
+ }
1822
+ return nil
1823
+ }
1824
+
1825
+ func tnFsReject[T any](reason interface{}) chan T {
1826
+ ch := make(chan T, 1)
1827
+ TnReject(ch, reason)
1828
+ return ch
1829
+ }
1830
+
1831
+ func (w *TnFileWriter) write(chunk interface{}, options ...interface{}) chan struct{} {
1832
+ b, typeErr := tnFsToBytes(chunk)
1833
+ if typeErr != nil {
1834
+ panic(typeErr)
1835
+ }
1836
+ signal := tnFsWriterSignal(options)
1837
+ w.mu.Lock()
1838
+ if w.errored {
1839
+ reason := w.errVal
1840
+ w.mu.Unlock()
1841
+ return tnFsReject[struct{}](reason)
1842
+ }
1843
+ if w.closed {
1844
+ w.mu.Unlock()
1845
+ return tnFsReject[struct{}](TnNodeError("TypeError", "ERR_INVALID_STATE", "The writer is closed"))
1846
+ }
1847
+ if w.closing {
1848
+ w.mu.Unlock()
1849
+ return tnFsReject[struct{}](TnNodeError("TypeError", "ERR_INVALID_STATE", "The writer is closing"))
1850
+ }
1851
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1852
+ w.mu.Unlock()
1853
+ return tnFsReject[struct{}](TnGetValue(signal, "reason"))
1854
+ }
1855
+ if w.bytesRemaining >= 0 && int64(len(b)) > w.bytesRemaining {
1856
+ rem := w.bytesRemaining
1857
+ w.mu.Unlock()
1858
+ return tnFsReject[struct{}](TnNodeError("RangeError", "ERR_OUT_OF_RANGE", fmt.Sprintf("The value of \\"write\\" is out of range. It must be <= %d bytes. Received %d", rem, len(b))))
1859
+ }
1860
+ if w.bytesRemaining > 0 {
1861
+ w.bytesRemaining -= int64(len(b))
1862
+ }
1863
+ position := w.pos
1864
+ if w.pos >= 0 {
1865
+ w.pos += int64(len(b))
1866
+ }
1867
+ prev := w.writeTail
1868
+ next := make(chan struct{})
1869
+ w.writeTail = next
1870
+ w.asyncPending++
1871
+ w.mu.Unlock()
1872
+
1873
+ ch := make(chan struct{}, 1)
1874
+ TnLoopRef()
1875
+ go func() {
1876
+ defer TnLoopUnref()
1877
+ if prev != nil {
1878
+ <-prev
1879
+ }
1880
+ defer func() {
1881
+ w.mu.Lock()
1882
+ w.asyncPending--
1883
+ if w.errored && w.asyncPending == 0 {
1884
+ w.releaseLocked()
1885
+ }
1886
+ w.mu.Unlock()
1887
+ close(next)
1888
+ }()
1889
+ w.mu.Lock()
1890
+ if w.errored {
1891
+ reason := w.errVal
1892
+ w.mu.Unlock()
1893
+ TnReject(ch, reason)
1894
+ return
1895
+ }
1896
+ w.mu.Unlock()
1897
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1898
+ TnReject(ch, TnGetValue(signal, "reason"))
1899
+ return
1900
+ }
1901
+ written, err := w.writeBufAll(b, position)
1902
+ w.mu.Lock()
1903
+ w.totalBytesWritten += written
1904
+ w.mu.Unlock()
1905
+ if err != nil {
1906
+ TnReject(ch, TnSysError(err, "write", ""))
1907
+ return
1908
+ }
1909
+ TnAsyncSend(ch, struct{}{})
1910
+ }()
1911
+ return ch
1912
+ }
1913
+
1914
+ func (w *TnFileWriter) writev(chunks interface{}, options ...interface{}) chan struct{} {
1915
+ list, typeErr := tnFsToByteSlice(chunks)
1916
+ if typeErr != nil {
1917
+ panic(typeErr)
1918
+ }
1919
+ totalSize := 0
1920
+ for _, b := range list {
1921
+ totalSize += len(b)
1922
+ }
1923
+ signal := tnFsWriterSignal(options)
1924
+ w.mu.Lock()
1925
+ if w.errored {
1926
+ reason := w.errVal
1927
+ w.mu.Unlock()
1928
+ return tnFsReject[struct{}](reason)
1929
+ }
1930
+ if w.closed {
1931
+ w.mu.Unlock()
1932
+ return tnFsReject[struct{}](TnNodeError("TypeError", "ERR_INVALID_STATE", "The writer is closed"))
1933
+ }
1934
+ if w.closing {
1935
+ w.mu.Unlock()
1936
+ return tnFsReject[struct{}](TnNodeError("TypeError", "ERR_INVALID_STATE", "The writer is closing"))
1937
+ }
1938
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1939
+ w.mu.Unlock()
1940
+ return tnFsReject[struct{}](TnGetValue(signal, "reason"))
1941
+ }
1942
+ if w.bytesRemaining >= 0 && int64(totalSize) > w.bytesRemaining {
1943
+ rem := w.bytesRemaining
1944
+ w.mu.Unlock()
1945
+ return tnFsReject[struct{}](TnNodeError("RangeError", "ERR_OUT_OF_RANGE", fmt.Sprintf("The value of \\"writev\\" is out of range. It must be <= %d bytes. Received %d", rem, totalSize)))
1946
+ }
1947
+ if w.bytesRemaining > 0 {
1948
+ w.bytesRemaining -= int64(totalSize)
1949
+ }
1950
+ position := w.pos
1951
+ if w.pos >= 0 {
1952
+ w.pos += int64(totalSize)
1953
+ }
1954
+ prev := w.writeTail
1955
+ next := make(chan struct{})
1956
+ w.writeTail = next
1957
+ w.asyncPending++
1958
+ w.mu.Unlock()
1959
+
1960
+ joined := make([]byte, 0, totalSize)
1961
+ for _, b := range list {
1962
+ joined = append(joined, b...)
1963
+ }
1964
+ ch := make(chan struct{}, 1)
1965
+ TnLoopRef()
1966
+ go func() {
1967
+ defer TnLoopUnref()
1968
+ if prev != nil {
1969
+ <-prev
1970
+ }
1971
+ defer func() {
1972
+ w.mu.Lock()
1973
+ w.asyncPending--
1974
+ if w.errored && w.asyncPending == 0 {
1975
+ w.releaseLocked()
1976
+ }
1977
+ w.mu.Unlock()
1978
+ close(next)
1979
+ }()
1980
+ w.mu.Lock()
1981
+ if w.errored {
1982
+ reason := w.errVal
1983
+ w.mu.Unlock()
1984
+ TnReject(ch, reason)
1985
+ return
1986
+ }
1987
+ w.mu.Unlock()
1988
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
1989
+ TnReject(ch, TnGetValue(signal, "reason"))
1990
+ return
1991
+ }
1992
+ written, err := w.writeBufAll(joined, position)
1993
+ w.mu.Lock()
1994
+ w.totalBytesWritten += written
1995
+ w.mu.Unlock()
1996
+ if err != nil {
1997
+ TnReject(ch, TnSysError(err, "write", ""))
1998
+ return
1999
+ }
2000
+ TnAsyncSend(ch, struct{}{})
2001
+ }()
2002
+ return ch
2003
+ }
2004
+
2005
+ func (w *TnFileWriter) writeSync(chunk interface{}) bool {
2006
+ b, typeErr := tnFsToBytes(chunk)
2007
+ if typeErr != nil {
2008
+ panic(typeErr)
2009
+ }
2010
+ w.mu.Lock()
2011
+ defer w.mu.Unlock()
2012
+ if w.errored || w.closed || w.closing || w.asyncPending > 0 {
2013
+ return false
2014
+ }
2015
+ if len(b) > w.syncThreshold {
2016
+ return false
2017
+ }
2018
+ if len(b) == 0 {
2019
+ return true
2020
+ }
2021
+ if w.bytesRemaining >= 0 && int64(len(b)) > w.bytesRemaining {
2022
+ return false
2023
+ }
2024
+ written, err := w.writeBufAll(b, w.pos)
2025
+ w.totalBytesWritten += written
2026
+ if w.pos >= 0 {
2027
+ w.pos += written
2028
+ }
2029
+ if err != nil {
2030
+ if written == 0 {
2031
+ return false
2032
+ }
2033
+ panic(TnSysError(err, "write", ""))
2034
+ }
2035
+ if w.bytesRemaining > 0 {
2036
+ w.bytesRemaining -= int64(len(b))
2037
+ }
2038
+ return true
2039
+ }
2040
+
2041
+ func (w *TnFileWriter) writevSync(chunks interface{}) bool {
2042
+ list, typeErr := tnFsToByteSlice(chunks)
2043
+ if typeErr != nil {
2044
+ panic(typeErr)
2045
+ }
2046
+ totalSize := 0
2047
+ for _, b := range list {
2048
+ totalSize += len(b)
2049
+ }
2050
+ w.mu.Lock()
2051
+ defer w.mu.Unlock()
2052
+ if w.errored || w.closed || w.closing || w.asyncPending > 0 {
2053
+ return false
2054
+ }
2055
+ if totalSize > w.syncThreshold {
2056
+ return false
2057
+ }
2058
+ if totalSize == 0 {
2059
+ return true
2060
+ }
2061
+ if w.bytesRemaining >= 0 && int64(totalSize) > w.bytesRemaining {
2062
+ return false
2063
+ }
2064
+ joined := make([]byte, 0, totalSize)
2065
+ for _, b := range list {
2066
+ joined = append(joined, b...)
2067
+ }
2068
+ written, err := w.writeBufAll(joined, w.pos)
2069
+ w.totalBytesWritten += written
2070
+ if w.pos >= 0 {
2071
+ w.pos += written
2072
+ }
2073
+ if err != nil {
2074
+ if written == 0 {
2075
+ return false
2076
+ }
2077
+ panic(TnSysError(err, "write", ""))
2078
+ }
2079
+ if w.bytesRemaining > 0 {
2080
+ w.bytesRemaining -= int64(totalSize)
2081
+ }
2082
+ return true
2083
+ }
2084
+
2085
+ func (w *TnFileWriter) end(options ...interface{}) chan float64 {
2086
+ signal := tnFsWriterSignal(options)
2087
+ w.mu.Lock()
2088
+ if w.errored {
2089
+ reason := w.errVal
2090
+ w.mu.Unlock()
2091
+ return tnFsReject[float64](reason)
2092
+ }
2093
+ if w.closed {
2094
+ total := float64(w.totalBytesWritten)
2095
+ w.mu.Unlock()
2096
+ ch := make(chan float64, 1)
2097
+ TnAsyncSend(ch, total)
2098
+ return ch
2099
+ }
2100
+ if w.closing {
2101
+ done := w.endDone
2102
+ w.mu.Unlock()
2103
+ ch := make(chan float64, 1)
2104
+ TnLoopRef()
2105
+ go func() {
2106
+ defer TnLoopUnref()
2107
+ <-done
2108
+ w.mu.Lock()
2109
+ errored, reason, total := w.errored, w.errVal, float64(w.totalBytesWritten)
2110
+ w.mu.Unlock()
2111
+ if errored {
2112
+ TnReject(ch, reason)
2113
+ } else {
2114
+ TnAsyncSend(ch, total)
2115
+ }
2116
+ }()
2117
+ return ch
2118
+ }
2119
+ if signal != nil && TnTruthy(TnGetValue(signal, "aborted")) {
2120
+ w.mu.Unlock()
2121
+ return tnFsReject[float64](TnGetValue(signal, "reason"))
2122
+ }
2123
+ w.closing = true
2124
+ prev := w.writeTail
2125
+ done := make(chan struct{})
2126
+ w.endDone = done
2127
+ w.mu.Unlock()
2128
+
2129
+ ch := make(chan float64, 1)
2130
+ TnLoopRef()
2131
+ go func() {
2132
+ defer TnLoopUnref()
2133
+ if prev != nil {
2134
+ <-prev
2135
+ }
2136
+ w.mu.Lock()
2137
+ if w.errored {
2138
+ reason := w.errVal
2139
+ w.mu.Unlock()
2140
+ close(done)
2141
+ TnReject(ch, reason)
2142
+ return
2143
+ }
2144
+ w.releaseLocked()
2145
+ w.closing = false
2146
+ w.closed = true
2147
+ total := float64(w.totalBytesWritten)
2148
+ w.mu.Unlock()
2149
+ close(done)
2150
+ TnAsyncSend(ch, total)
2151
+ }()
2152
+ return ch
2153
+ }
2154
+
2155
+ func (w *TnFileWriter) endSync() float64 {
2156
+ w.mu.Lock()
2157
+ defer w.mu.Unlock()
2158
+ if w.errored || w.closing || w.asyncPending > 0 {
2159
+ return -1
2160
+ }
2161
+ if w.closed {
2162
+ return float64(w.totalBytesWritten)
2163
+ }
2164
+ w.closed = true
2165
+ w.releaseLocked()
2166
+ return float64(w.totalBytesWritten)
2167
+ }
2168
+
2169
+ func (w *TnFileWriter) fail(reason ...interface{}) {
2170
+ w.mu.Lock()
2171
+ defer w.mu.Unlock()
2172
+ if w.closed || w.errored {
2173
+ return
2174
+ }
2175
+ w.errored = true
2176
+ w.closing = false
2177
+ w.closed = true
2178
+ if len(reason) > 0 && reason[0] != nil {
2179
+ w.errVal = reason[0]
2180
+ } else {
2181
+ w.errVal = TnNodeError("Error", "ERR_INVALID_STATE", "The writer has failed")
2182
+ }
2183
+ if w.asyncPending == 0 {
2184
+ w.releaseLocked()
2185
+ }
2186
+ }
2187
+
2188
+ func (w *TnFileWriter) __dispose() {
2189
+ w.fail()
2190
+ }
2191
+
2192
+ func (w *TnFileWriter) __asyncDispose() chan struct{} {
2193
+ w.mu.Lock()
2194
+ closing, done := w.closing, w.endDone
2195
+ w.mu.Unlock()
2196
+ if closing && done != nil {
2197
+ ch := make(chan struct{}, 1)
2198
+ TnLoopRef()
2199
+ go func() {
2200
+ defer TnLoopUnref()
2201
+ <-done
2202
+ TnAsyncSend(ch, struct{}{})
2203
+ }()
2204
+ return ch
2205
+ }
2206
+ w.fail()
2207
+ ch := make(chan struct{}, 1)
2208
+ TnAsyncSend(ch, struct{}{})
2209
+ return ch
1207
2210
  }`;
1208
2211
  // Async names → their sync emitters
1209
2212
  const FS_ASYNC = [
@@ -1251,8 +2254,13 @@ function syncResultType(r, syncKey, call) {
1251
2254
  function registerFsAsync(r) {
1252
2255
  defineModule(r, 'fs/promises');
1253
2256
  r.namespaces['fs.promises'] = 'fs/promises';
1254
- defineHelper(r, 'nodeFsAsync', ['fmt', 'io'], ['nodeFs', 'eventLoop', 'promise', 'nodeInvoke'], fsAsyncHelper);
1255
- defineClass(r, 'fs/promises.FileHandle', '*TnFileHandle', 'class', 'writeFile=chan struct{}; appendFile=chan struct{}; read=chan *TnFsReadResult; write=chan *TnFsWriteResult; stat=chan *TnStats; truncate=chan struct{}; sync=chan struct{}; close=chan struct{}', '', 'fd=float64', '');
2257
+ defineHelper(r, 'nodeFsAsync', ['fmt', 'io', 'iter', 'math', 'reflect', 'sync'], ['nodeFs', 'eventLoop', 'promise', 'nodeInvoke'], fsAsyncHelper);
2258
+ defineClass(r, 'fs/promises.FileHandle', '*TnFileHandle', 'class', 'writeFile=chan struct{}; appendFile=chan struct{}; read=chan *TnFsReadResult; write=chan *TnFsWriteResult; stat=chan *TnStats; truncate=chan struct{}; sync=chan struct{}; close=chan struct{}; pull=*TnFilePull; pullSync=*TnFilePullSync; writer=*TnFileWriter', '', 'fd=float64', '');
2259
+ defineClass(r, 'fs/promises.FilePull', '*TnFilePull', 'class', 'tnIterator=iter.Seq[[][]byte]', '', '', '');
2260
+ defineClass(r, 'fs/promises.FilePullSync', '*TnFilePullSync', 'class', 'tnIterator=iter.Seq[[][]byte]', '', '', '');
2261
+ defineClass(r, 'fs/promises.FileWriter', '*TnFileWriter', 'class', 'write=chan struct{}; writev=chan struct{}; writeSync=bool; writevSync=bool; end=chan float64; endSync=float64; fail=; __dispose=; __asyncDispose=chan struct{}', '', '', '');
2262
+ r.iterators['*TnFilePull'] = 'tnIterator';
2263
+ r.iterators['*TnFilePullSync'] = 'tnIterator';
1256
2264
  defineClass(r, 'fs.ReadResult', '*TnFsReadResult', 'class', '', 'bytesRead=float64; buffer=[]byte', '', '');
1257
2265
  defineClass(r, 'fs.WriteResult', '*TnFsWriteResult', 'class', '', 'bytesWritten=float64; buffer=interface{}', '', '');
1258
2266
  r.methods['fs/promises.FileHandle#readFile'] = (c) => {