goscript 0.0.81 → 0.0.83

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 (72) hide show
  1. package/dist/gs/builtin/type.js +1 -1
  2. package/dist/gs/builtin/type.js.map +1 -1
  3. package/dist/gs/os/dir.gs.js +41 -27
  4. package/dist/gs/os/dir.gs.js.map +1 -1
  5. package/dist/gs/os/exec.gs.js +1 -2
  6. package/dist/gs/os/exec.gs.js.map +1 -1
  7. package/dist/gs/os/exec_posix.gs.js +1 -1
  8. package/dist/gs/os/exec_posix.gs.js.map +1 -1
  9. package/dist/gs/os/file_constants_js.gs.js +71 -7
  10. package/dist/gs/os/file_constants_js.gs.js.map +1 -1
  11. package/dist/gs/os/file_js.gs.js +168 -24
  12. package/dist/gs/os/file_js.gs.js.map +1 -1
  13. package/dist/gs/os/file_posix_js.gs.js +75 -11
  14. package/dist/gs/os/file_posix_js.gs.js.map +1 -1
  15. package/dist/gs/os/file_unix_js.gs.d.ts +3 -3
  16. package/dist/gs/os/file_unix_js.gs.js +218 -21
  17. package/dist/gs/os/file_unix_js.gs.js.map +1 -1
  18. package/dist/gs/os/getwd_js.gs.js +19 -6
  19. package/dist/gs/os/getwd_js.gs.js.map +1 -1
  20. package/dist/gs/os/path.gs.js +3 -3
  21. package/dist/gs/os/path.gs.js.map +1 -1
  22. package/dist/gs/os/pidfd_js.gs.js +0 -3
  23. package/dist/gs/os/pidfd_js.gs.js.map +1 -1
  24. package/dist/gs/os/proc.gs.js +6 -3
  25. package/dist/gs/os/proc.gs.js.map +1 -1
  26. package/dist/gs/os/proc_js.gs.js +10 -5
  27. package/dist/gs/os/proc_js.gs.js.map +1 -1
  28. package/dist/gs/os/rawconn_js.gs.d.ts +6 -3
  29. package/dist/gs/os/rawconn_js.gs.js +16 -10
  30. package/dist/gs/os/rawconn_js.gs.js.map +1 -1
  31. package/dist/gs/os/removeall_js.gs.js +2 -4
  32. package/dist/gs/os/removeall_js.gs.js.map +1 -1
  33. package/dist/gs/os/root_js.gs.d.ts +4 -1
  34. package/dist/gs/os/root_js.gs.js +90 -40
  35. package/dist/gs/os/root_js.gs.js.map +1 -1
  36. package/dist/gs/os/root_noopenat.gs.js +13 -16
  37. package/dist/gs/os/root_noopenat.gs.js.map +1 -1
  38. package/dist/gs/os/stat_js.gs.js +40 -4
  39. package/dist/gs/os/stat_js.gs.js.map +1 -1
  40. package/dist/gs/os/stat_unix_js.gs.d.ts +1 -1
  41. package/dist/gs/os/stat_unix_js.gs.js +5 -10
  42. package/dist/gs/os/stat_unix_js.gs.js.map +1 -1
  43. package/dist/gs/os/tempfile.gs.d.ts +1 -1
  44. package/dist/gs/os/tempfile.gs.js +24 -7
  45. package/dist/gs/os/tempfile.gs.js.map +1 -1
  46. package/dist/gs/os/types_js.gs.d.ts +78 -1
  47. package/dist/gs/os/types_js.gs.js +464 -23
  48. package/dist/gs/os/types_js.gs.js.map +1 -1
  49. package/go.mod +2 -0
  50. package/gs/builtin/type.ts +1 -1
  51. package/gs/os/dir.gs.ts +40 -28
  52. package/gs/os/exec.gs.ts +2 -4
  53. package/gs/os/exec_posix.gs.ts +1 -2
  54. package/gs/os/file_constants_js.gs.ts +70 -8
  55. package/gs/os/file_js.gs.ts +156 -26
  56. package/gs/os/file_posix_js.gs.ts +69 -13
  57. package/gs/os/file_unix_js.gs.ts +212 -24
  58. package/gs/os/file_unix_js.test.ts +131 -0
  59. package/gs/os/getwd_js.gs.ts +18 -8
  60. package/gs/os/path.gs.ts +3 -4
  61. package/gs/os/pidfd_js.gs.ts +3 -8
  62. package/gs/os/proc.gs.ts +6 -4
  63. package/gs/os/proc_js.gs.ts +11 -8
  64. package/gs/os/rawconn_js.gs.ts +22 -14
  65. package/gs/os/removeall_js.gs.ts +3 -6
  66. package/gs/os/root_js.gs.ts +94 -42
  67. package/gs/os/root_noopenat.gs.ts +13 -20
  68. package/gs/os/stat_js.gs.ts +37 -6
  69. package/gs/os/stat_unix_js.gs.ts +6 -13
  70. package/gs/os/tempfile.gs.ts +27 -9
  71. package/gs/os/types_js.gs.ts +528 -26
  72. package/package.json +1 -1
@@ -1,77 +1,116 @@
1
1
  import * as $ from "@goscript/builtin/index.js";
2
- import { ErrUnimplemented } from "./error.gs.js";
2
+ import { ErrInvalid } from "./error.gs.js";
3
+ import { Create, Mkdir, Open, OpenFile, Remove } from "./file_js.gs.js";
4
+ import { Lstat as lstatPath, Stat as statPath } from "./stat_js.gs.js";
3
5
  import { File } from "./types_js.gs.js";
4
6
 
5
7
  import * as fs from "@goscript/io/fs/index.js"
8
+ import { ValidPath } from "@goscript/io/fs/index.js"
6
9
 
7
- // JavaScript-specific implementations for root filesystem operations
8
- // These functions stub operations that cannot be implemented in JavaScript
10
+ function isValidRootName(name: string): boolean {
11
+ if (name === ".") {
12
+ return true
13
+ }
14
+ return ValidPath(name)
15
+ }
16
+
17
+ function joinRootPath(dir: string, name: string): [string, $.GoError] {
18
+ if (!isValidRootName(name)) {
19
+ return ["", ErrInvalid]
20
+ }
21
+ if (name === ".") {
22
+ return [dir, null]
23
+ }
24
+ return [dir.replace(/\/+$/, "") + "/" + name, null]
25
+ }
9
26
 
10
- // OpenInRoot opens the file name in the directory dir - stub implementation
11
27
  export function OpenInRoot(dir: string, name: string): [File | null, $.GoError] {
12
- return [null, ErrUnimplemented]
28
+ const [path, err] = joinRootPath(dir, name)
29
+ if (err !== null) {
30
+ return [null, err]
31
+ }
32
+ return Open(path)
13
33
  }
14
34
 
15
- // Root represents a root filesystem - stub implementation
16
35
  export class Root {
17
- constructor() {}
36
+ public name: string
37
+
38
+ constructor(init?: Partial<{name?: string}>) {
39
+ this.name = init?.name ?? "."
40
+ }
18
41
 
19
- // Name returns the name of the directory presented to OpenRoot
20
42
  public Name(): string {
21
- return ""
43
+ return this.name
22
44
  }
23
45
 
24
- // Close closes the Root
25
46
  public Close(): $.GoError {
26
- return ErrUnimplemented
47
+ return null
27
48
  }
28
49
 
29
- // Open opens the named file in the root for reading
30
50
  public Open(name: string): [File | null, $.GoError] {
31
- return [null, ErrUnimplemented]
51
+ return OpenInRoot(this.name, name)
32
52
  }
33
53
 
34
- // Create creates or truncates the named file in the root
35
54
  public Create(name: string): [File | null, $.GoError] {
36
- return [null, ErrUnimplemented]
55
+ const [path, err] = joinRootPath(this.name, name)
56
+ if (err !== null) {
57
+ return [null, err]
58
+ }
59
+ return Create(path)
37
60
  }
38
61
 
39
- // OpenFile opens the named file in the root
40
62
  public OpenFile(name: string, flag: number, perm: number): [File | null, $.GoError] {
41
- return [null, ErrUnimplemented]
63
+ const [path, err] = joinRootPath(this.name, name)
64
+ if (err !== null) {
65
+ return [null, err]
66
+ }
67
+ return OpenFile(path, flag, perm)
42
68
  }
43
69
 
44
- // OpenRoot opens the named directory in the root
45
70
  public OpenRoot(name: string): [RootType | null, $.GoError] {
46
- return [null, ErrUnimplemented]
71
+ const [path, err] = joinRootPath(this.name, name)
72
+ if (err !== null) {
73
+ return [null, err]
74
+ }
75
+ return OpenRoot(path)
47
76
  }
48
77
 
49
- // Mkdir creates a new directory in the root
50
78
  public Mkdir(name: string, perm: number): $.GoError {
51
- return ErrUnimplemented
79
+ const [path, err] = joinRootPath(this.name, name)
80
+ if (err !== null) {
81
+ return err
82
+ }
83
+ return Mkdir(path, perm)
52
84
  }
53
85
 
54
- // Remove removes the named file or directory in the root
55
86
  public Remove(name: string): $.GoError {
56
- return ErrUnimplemented
87
+ const [path, err] = joinRootPath(this.name, name)
88
+ if (err !== null) {
89
+ return err
90
+ }
91
+ return Remove(path)
57
92
  }
58
93
 
59
- // Stat returns a FileInfo describing the named file in the root
60
94
  public Stat(name: string): [fs.FileInfo | null, $.GoError] {
61
- return [null, ErrUnimplemented]
95
+ const [path, err] = joinRootPath(this.name, name)
96
+ if (err !== null) {
97
+ return [null, err]
98
+ }
99
+ return statPath(path)
62
100
  }
63
101
 
64
- // Lstat returns a FileInfo describing the named file in the root
65
102
  public Lstat(name: string): [fs.FileInfo | null, $.GoError] {
66
- return [null, ErrUnimplemented]
103
+ const [path, err] = joinRootPath(this.name, name)
104
+ if (err !== null) {
105
+ return [null, err]
106
+ }
107
+ return lstatPath(path)
67
108
  }
68
109
 
69
- // FS returns a file system for the tree of files in the root
70
110
  public FS(): fs.FS {
71
- return new stubFS()
111
+ return new rootFS({ root: this })
72
112
  }
73
113
 
74
- // Register this type with the runtime type system
75
114
  static __typeInfo = $.registerStructType(
76
115
  'Root',
77
116
  new Root(),
@@ -80,32 +119,45 @@ export class Root {
80
119
  { name: "Close", args: [], returns: [{ type: { kind: $.TypeKind.Interface, name: 'GoError', methods: [{ name: 'Error', args: [], returns: [{ type: { kind: $.TypeKind.Basic, name: 'string' } }] }] } }] }
81
120
  ],
82
121
  Root,
83
- {}
122
+ { "name": { kind: $.TypeKind.Basic, name: "string" } }
84
123
  );
85
124
  }
86
125
 
87
- // Type alias to avoid conflicts
88
126
  export type RootType = Root
89
127
 
90
- // OpenRoot opens a root filesystem - stub implementation
91
128
  export function OpenRoot(name: string): [Root | null, $.GoError] {
92
- return [null, ErrUnimplemented]
129
+ const [info, err] = statPath(name)
130
+ if (err !== null) {
131
+ return [null, err]
132
+ }
133
+ if (!info?.IsDir()) {
134
+ return [null, ErrInvalid]
135
+ }
136
+ return [new Root({ name }), null]
93
137
  }
94
138
 
95
- // splitPathInRoot splits a path in root - stub implementation
96
139
  export function splitPathInRoot(s: string, prefix: $.Slice<string> | null, suffix: $.Slice<string> | null): [$.Slice<string>, string, $.GoError] {
97
- return [null, "", ErrUnimplemented]
140
+ if (!isValidRootName(s)) {
141
+ return [null, "", ErrInvalid]
142
+ }
143
+ const parts = s === "." ? [] : s.split("/").filter((part) => part !== "")
144
+ const head = $.arrayToSlice<string>(parts.slice(0, Math.max(0, parts.length - 1)))
145
+ const tail = parts.length === 0 ? "." : parts[parts.length - 1]
146
+ return [head, tail, null]
98
147
  }
99
148
 
100
- // isValidRootFSPath checks if a path is valid for root filesystem - stub implementation
101
149
  export function isValidRootFSPath(name: string): boolean {
102
- return false
150
+ return isValidRootName(name)
103
151
  }
104
152
 
105
- // Internal stub filesystem
106
- class stubFS {
153
+ class rootFS {
154
+ public root: Root
155
+
156
+ constructor(init?: Partial<{root?: Root}>) {
157
+ this.root = init?.root ?? new Root()
158
+ }
159
+
107
160
  public Open(name: string): [fs.File, $.GoError] {
108
- return [null, ErrUnimplemented]
161
+ return this.root.Open(name)
109
162
  }
110
163
  }
111
-
@@ -1,45 +1,38 @@
1
1
  import * as $ from "@goscript/builtin/index.js";
2
- import { underlyingError, ErrUnimplemented } from "./error.gs.js";
3
- import { Root } from "./root_js.gs.js";
2
+ import { Mkdir, OpenFile, Remove } from "./file_js.gs.js";
3
+ import { Root, OpenRoot } from "./root_js.gs.js";
4
+ import { Lstat, Stat } from "./stat_js.gs.js";
4
5
  import { File } from "./types_js.gs.js";
5
6
 
6
- import * as errors from "@goscript/errors/index.js"
7
7
  import * as fs from "@goscript/io/fs/index.js"
8
8
 
9
- // Stub functions for JavaScript environment - these operations cannot be implemented properly
10
-
11
- // openRootNolog is OpenRoot - stub implementation
12
9
  export function openRootNolog(name: string): [Root | null, $.GoError] {
13
- return [null, ErrUnimplemented]
10
+ return OpenRoot(name)
14
11
  }
15
12
 
16
- // openRootInRoot is Root.OpenRoot - stub implementation
17
13
  export function openRootInRoot(r: Root | null, name: string): [Root | null, $.GoError] {
18
- return [null, ErrUnimplemented]
14
+ return r?.OpenRoot(name) ?? [null, null]
19
15
  }
20
16
 
21
- // newRoot returns a new Root - stub implementation
22
17
  export function newRoot(name: string): [Root | null, $.GoError] {
23
- return [null, ErrUnimplemented]
18
+ return OpenRoot(name)
24
19
  }
25
20
 
26
- // rootOpenFileNolog is Root.OpenFile - stub implementation
27
21
  export function rootOpenFileNolog(r: Root | null, name: string, flag: number, perm: number): [File | null, $.GoError] {
28
- return [null, ErrUnimplemented]
22
+ return r?.OpenFile(name, flag, perm) ?? OpenFile(name, flag, perm)
29
23
  }
30
24
 
31
- // rootStat - stub implementation
32
25
  export function rootStat(r: Root | null, name: string, lstat: boolean): [fs.FileInfo | null, $.GoError] {
33
- return [null, ErrUnimplemented]
26
+ if (r !== null) {
27
+ return lstat ? r.Lstat(name) : r.Stat(name)
28
+ }
29
+ return lstat ? Lstat(name) : Stat(name)
34
30
  }
35
31
 
36
- // rootMkdir - stub implementation
37
32
  export function rootMkdir(r: Root | null, name: string, perm: number): $.GoError {
38
- return ErrUnimplemented
33
+ return r?.Mkdir(name, perm) ?? Mkdir(name, perm)
39
34
  }
40
35
 
41
- // rootRemove - stub implementation
42
36
  export function rootRemove(r: Root | null, name: string): $.GoError {
43
- return ErrUnimplemented
37
+ return r?.Remove(name) ?? Remove(name)
44
38
  }
45
-
@@ -1,13 +1,28 @@
1
1
  import * as $ from "@goscript/builtin/index.js";
2
2
  import { ErrUnimplemented } from "./error.gs.js";
3
+ import { createFileInfo, getDeno, getNodeFS, newHostError } from "./types_js.gs.js";
3
4
 
4
5
  import * as fs from "@goscript/io/fs/index.js"
5
6
 
6
- // JavaScript-specific stubs for stat operations
7
-
8
7
  // Stat returns a [FileInfo] describing the named file.
9
8
  // If there is an error, it will be of type [*PathError].
10
9
  export function Stat(name: string): [fs.FileInfo, $.GoError] {
10
+ const denoObj = getDeno()
11
+ if (denoObj?.statSync) {
12
+ try {
13
+ return [createFileInfo(name, denoObj.statSync(name)), null]
14
+ } catch (err) {
15
+ return [null, newHostError(err)]
16
+ }
17
+ }
18
+ const nodeFS = getNodeFS()
19
+ if (nodeFS?.statSync) {
20
+ try {
21
+ return [createFileInfo(name, nodeFS.statSync(name)), null]
22
+ } catch (err) {
23
+ return [null, newHostError(err)]
24
+ }
25
+ }
11
26
  return [null, ErrUnimplemented]
12
27
  }
13
28
 
@@ -16,15 +31,31 @@ export function Stat(name: string): [fs.FileInfo, $.GoError] {
16
31
  // describes the symbolic link. Lstat makes no attempt to follow the link.
17
32
  // If there is an error, it will be of type [*PathError].
18
33
  export function Lstat(name: string): [fs.FileInfo, $.GoError] {
19
- return [null, ErrUnimplemented]
34
+ const denoObj = getDeno()
35
+ if (denoObj?.lstatSync) {
36
+ try {
37
+ return [createFileInfo(name, denoObj.lstatSync(name)), null]
38
+ } catch (err) {
39
+ return [null, newHostError(err)]
40
+ }
41
+ }
42
+ const nodeFS = getNodeFS()
43
+ if (nodeFS?.lstatSync) {
44
+ try {
45
+ return [createFileInfo(name, nodeFS.lstatSync(name)), null]
46
+ } catch (err) {
47
+ return [null, newHostError(err)]
48
+ }
49
+ }
50
+ return Stat(name)
20
51
  }
21
52
 
22
53
  // statNolog is the same as Stat, for use in DirFS.
23
54
  export function statNolog(name: string): [fs.FileInfo, $.GoError] {
24
- return [null, ErrUnimplemented]
55
+ return Stat(name)
25
56
  }
26
57
 
27
58
  // lstatNolog is the same as Lstat, for use in DirFS.
28
59
  export function lstatNolog(name: string): [fs.FileInfo, $.GoError] {
29
- return [null, ErrUnimplemented]
30
- }
60
+ return Lstat(name)
61
+ }
@@ -1,27 +1,20 @@
1
1
  import * as $ from "@goscript/builtin/index.js";
2
- import { ErrUnimplemented } from "./error.gs.js";
2
+ import { lstatNolog as lstatPath, statNolog as statPath } from "./stat_js.gs.js";
3
3
 
4
4
  import * as fs from "@goscript/io/fs/index.js"
5
5
 
6
- // JavaScript-specific stub implementations for Unix stat operations
7
- // These operations cannot be implemented in JavaScript environments
8
-
9
- // statNolog stats a file with no test logging - stub implementation
10
6
  export function statNolog(name: string): [fs.FileInfo | null, $.GoError] {
11
- return [null, ErrUnimplemented]
7
+ return statPath(name)
12
8
  }
13
9
 
14
- // lstatNolog lstats a file with no test logging - stub implementation
15
10
  export function lstatNolog(name: string): [fs.FileInfo | null, $.GoError] {
16
- return [null, ErrUnimplemented]
11
+ return lstatPath(name)
17
12
  }
18
13
 
19
- // Additional compatibility exports
20
- export function fillFileStatFromSys(fs: any, name: string): void {
21
- // No-op in JavaScript
14
+ export function fillFileStatFromSys(fileInfo: any, name: string): void {
15
+ // No-op in JavaScript.
22
16
  }
23
17
 
24
- // fileStat stub for compatibility
25
18
  export interface fileStat {
26
19
  sys: any
27
- }
20
+ }
@@ -1,8 +1,9 @@
1
1
  import * as $ from "@goscript/builtin/index.js";
2
2
  import { ErrUnimplemented } from "./error.gs.js";
3
- import { File } from "./types.gs.js";
3
+ import { TempDir } from "./file_constants_js.gs.js";
4
+ import { Create, Mkdir } from "./file_js.gs.js";
5
+ import { File } from "./types_js.gs.js";
4
6
 
5
- // joinPath joins directory and file paths - simplified implementation for JavaScript
6
7
  export function joinPath(dir: string, file: string): string {
7
8
  if (dir === "" || dir === ".") {
8
9
  return file
@@ -10,25 +11,42 @@ export function joinPath(dir: string, file: string): string {
10
11
  if (file === "") {
11
12
  return dir
12
13
  }
13
- // Remove trailing slash from dir if present
14
14
  if (dir.endsWith("/")) {
15
15
  dir = dir.slice(0, -1)
16
16
  }
17
- // Remove leading slash from file if present
18
17
  if (file.startsWith("/")) {
19
18
  file = file.slice(1)
20
19
  }
21
20
  return dir + "/" + file
22
21
  }
23
22
 
24
- // CreateTemp creates a new temporary file in the directory dir.
25
- // Stubbed implementation for JavaScript environment.
23
+ function nextTempPath(dir: string, pattern: string): string {
24
+ const baseDir = dir === "" ? TempDir() : dir
25
+ const suffix = Math.random().toString(36).slice(2, 10) + Date.now().toString(36)
26
+ const name = pattern.includes("*") ? pattern.replace("*", suffix) : pattern + suffix
27
+ return joinPath(baseDir, name)
28
+ }
29
+
26
30
  export function CreateTemp(dir: string, pattern: string): [File | null, $.GoError] {
31
+ const template = pattern === "" ? "tmp-*" : pattern
32
+ for (let i = 0; i < 16; i++) {
33
+ const path = nextTempPath(dir, template)
34
+ const [file, err] = Create(path)
35
+ if (err === null) {
36
+ return [file, null]
37
+ }
38
+ }
27
39
  return [null, ErrUnimplemented]
28
40
  }
29
41
 
30
- // MkdirTemp creates a new temporary directory.
31
- // Stubbed implementation for JavaScript environment.
32
42
  export function MkdirTemp(dir: string, pattern: string): [string, $.GoError] {
43
+ const template = pattern === "" ? "tmp-*" : pattern
44
+ for (let i = 0; i < 16; i++) {
45
+ const path = nextTempPath(dir, template)
46
+ const err = Mkdir(path, 0o700)
47
+ if (err === null) {
48
+ return [path, null]
49
+ }
50
+ }
33
51
  return ["", ErrUnimplemented]
34
- }
52
+ }