summaryrefslogtreecommitdiff
path: root/js/os.ts
blob: e669aea9d0ce2c0b014ffca59039eb360bf8143b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { ModuleInfo } from "./types";
import { deno as fbs } from "gen/msg_generated";
import { assert } from "./util";
import * as util from "./util";
import { flatbuffers } from "flatbuffers";
import { send } from "./fbs_util";

export function exit(exitCode = 0): never {
  const builder = new flatbuffers.Builder();
  fbs.Exit.startExit(builder);
  fbs.Exit.addCode(builder, exitCode);
  const msg = fbs.Exit.endExit(builder);
  send(builder, fbs.Any.Exit, msg);
  return util.unreachable();
}

export function codeFetch(
  moduleSpecifier: string,
  containingFile: string
): ModuleInfo {
  util.log("os.ts codeFetch", moduleSpecifier, containingFile);
  // Send CodeFetch message
  const builder = new flatbuffers.Builder();
  const moduleSpecifier_ = builder.createString(moduleSpecifier);
  const containingFile_ = builder.createString(containingFile);
  fbs.CodeFetch.startCodeFetch(builder);
  fbs.CodeFetch.addModuleSpecifier(builder, moduleSpecifier_);
  fbs.CodeFetch.addContainingFile(builder, containingFile_);
  const msg = fbs.CodeFetch.endCodeFetch(builder);
  const baseRes = send(builder, fbs.Any.CodeFetch, msg);
  assert(baseRes != null);
  assert(fbs.Any.CodeFetchRes === baseRes!.msgType());
  const codeFetchRes = new fbs.CodeFetchRes();
  assert(baseRes!.msg(codeFetchRes) != null);
  return {
    moduleName: codeFetchRes.moduleName(),
    filename: codeFetchRes.filename(),
    sourceCode: codeFetchRes.sourceCode(),
    outputCode: codeFetchRes.outputCode()
  };
}

export function codeCache(
  filename: string,
  sourceCode: string,
  outputCode: string
): void {
  util.log("os.ts codeCache", filename, sourceCode, outputCode);
  const builder = new flatbuffers.Builder();
  const filename_ = builder.createString(filename);
  const sourceCode_ = builder.createString(sourceCode);
  const outputCode_ = builder.createString(outputCode);
  fbs.CodeCache.startCodeCache(builder);
  fbs.CodeCache.addFilename(builder, filename_);
  fbs.CodeCache.addSourceCode(builder, sourceCode_);
  fbs.CodeCache.addOutputCode(builder, outputCode_);
  const msg = fbs.CodeCache.endCodeCache(builder);
  const baseRes = send(builder, fbs.Any.CodeCache, msg);
  assert(baseRes == null); // Expect null or error.
}

/**
 * makeTempDirSync creates a new temporary directory in the directory `dir`, its
 * name beginning with `prefix` and ending with `suffix`.
 * It returns the full path to the newly created directory.
 * If `dir` is unspecified, tempDir uses the default directory for temporary
 * files. Multiple programs calling tempDir simultaneously will not choose the
 * same directory. It is the caller's responsibility to remove the directory
 * when no longer needed.
 */
export interface MakeTempDirOptions {
  dir?: string;
  prefix?: string;
  suffix?: string;
}
export function makeTempDirSync({
  dir,
  prefix,
  suffix
}: MakeTempDirOptions = {}): string {
  const builder = new flatbuffers.Builder();
  const fbDir = dir == null ? -1 : builder.createString(dir);
  const fbPrefix = prefix == null ? -1 : builder.createString(prefix);
  const fbSuffix = suffix == null ? -1 : builder.createString(suffix);
  fbs.MakeTempDir.startMakeTempDir(builder);
  if (dir != null) {
    fbs.MakeTempDir.addDir(builder, fbDir);
  }
  if (prefix != null) {
    fbs.MakeTempDir.addPrefix(builder, fbPrefix);
  }
  if (suffix != null) {
    fbs.MakeTempDir.addSuffix(builder, fbSuffix);
  }
  const msg = fbs.MakeTempDir.endMakeTempDir(builder);
  const baseRes = send(builder, fbs.Any.MakeTempDir, msg);
  assert(baseRes != null);
  assert(fbs.Any.MakeTempDirRes === baseRes!.msgType());
  const res = new fbs.MakeTempDirRes();
  assert(baseRes!.msg(res) != null);
  const path = res.path();
  assert(path != null);
  return path!;
}

export function readFileSync(filename: string): Uint8Array {
  /* Ideally we could write
  const res = send({
    command: fbs.Command.READ_FILE_SYNC,
    readFileSyncFilename: filename
  });
  return res.readFileSyncData;
  */
  const builder = new flatbuffers.Builder();
  const filename_ = builder.createString(filename);
  fbs.ReadFileSync.startReadFileSync(builder);
  fbs.ReadFileSync.addFilename(builder, filename_);
  const msg = fbs.ReadFileSync.endReadFileSync(builder);
  const baseRes = send(builder, fbs.Any.ReadFileSync, msg);
  assert(baseRes != null);
  assert(fbs.Any.ReadFileSyncRes === baseRes!.msgType());
  const res = new fbs.ReadFileSyncRes();
  assert(baseRes!.msg(res) != null);
  const dataArray = res.dataArray();
  assert(dataArray != null);
  return new Uint8Array(dataArray!);
}

function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
  const env: { [index: string]: string } = {};

  for (let i = 0; i < _msg.mapLength(); i++) {
    const item = _msg.map(i)!;

    env[item.key()!] = item.value()!;
  }

  return new Proxy(env, {
    set(obj, prop: string, value: string | number) {
      setEnv(prop, value.toString());
      return Reflect.set(obj, prop, value);
    }
  });
}

function setEnv(key: string, value: string): void {
  const builder = new flatbuffers.Builder();
  const _key = builder.createString(key);
  const _value = builder.createString(value);
  fbs.SetEnv.startSetEnv(builder);
  fbs.SetEnv.addKey(builder, _key);
  fbs.SetEnv.addValue(builder, _value);
  const msg = fbs.SetEnv.endSetEnv(builder);
  send(builder, fbs.Any.SetEnv, msg);
}

/**
 * Returns a snapshot of the environment variables at invocation. Mutating a
 * property in the object will set that variable in the environment for
 * the process. The environment object will only accept `string`s or `number`s
 * as values.
 *     import { env } from "deno";
 *     const env = deno.env();
 *     console.log(env.SHELL)
 *     env.TEST_VAR = "HELLO";
 *
 *     const newEnv = deno.env();
 *     console.log(env.TEST_VAR == newEnv.TEST_VAR);
 */
export function env(): { [index: string]: string } {
  /* Ideally we could write
  const res = send({
    command: fbs.Command.ENV,
  });
  */
  const builder = new flatbuffers.Builder();
  fbs.Environ.startEnviron(builder);
  const msg = fbs.Environ.endEnviron(builder);
  const baseRes = send(builder, fbs.Any.Environ, msg)!;
  assert(fbs.Any.EnvironRes === baseRes.msgType());
  const res = new fbs.EnvironRes();
  assert(baseRes.msg(res) != null);
  // TypeScript cannot track assertion above, therefore not null assertion
  return createEnv(res);
}

/**
 * A FileInfo describes a file and is returned by `stat`, `lstat`,
 * `statSync`, `lstatSync`.
 */
export class FileInfo {
  private readonly _isFile: boolean;
  private readonly _isSymlink: boolean;
  /** The size of the file, in bytes. */
  len: number;
  /**
   * The last modification time of the file. This corresponds to the `mtime`
   * field from `stat` on Unix and `ftLastWriteTime` on Windows. This may not
   * be available on all platforms.
   */
  modified: number | null;
  /**
   * The last access time of the file. This corresponds to the `atime`
   * field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
   * be available on all platforms.
   */
  accessed: number | null;
  /**
   * The last access time of the file. This corresponds to the `birthtime`
   * field from `stat` on Unix and `ftCreationTime` on Windows. This may not
   * be available on all platforms.
   */
  created: number | null;

  /* @internal */
  constructor(private _msg: fbs.StatSyncRes) {
    const modified = this._msg.modified().toFloat64();
    const accessed = this._msg.accessed().toFloat64();
    const created = this._msg.created().toFloat64();

    this._isFile = this._msg.isFile();
    this._isSymlink = this._msg.isSymlink();
    this.len = this._msg.len().toFloat64();
    this.modified = modified ? modified : null;
    this.accessed = accessed ? accessed : null;
    this.created = created ? created : null;
  }

  /**
   * Returns whether this is info for a regular file. This result is mutually
   * exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
   */
  isFile() {
    return this._isFile;
  }

  /**
   * Returns whether this is info for a regular directory. This result is
   * mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
   */
  isDirectory() {
    return !this._isFile && !this._isSymlink;
  }

  /**
   * Returns whether this is info for a symlink. This result is
   * mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
   */
  isSymlink() {
    return this._isSymlink;
  }
}

/**
 * Queries the file system for information on the path provided.
 * If the given path is a symlink information about the symlink will
 * be returned.
 * @returns FileInfo
 */
export function lStatSync(filename: string): FileInfo {
  return statSyncInner(filename, true);
}

/**
 * Queries the file system for information on the path provided.
 * `statSync` Will always follow symlinks.
 * @returns FileInfo
 */
export function statSync(filename: string): FileInfo {
  return statSyncInner(filename, false);
}

function statSyncInner(filename: string, lstat: boolean): FileInfo {
  /* Ideally we could write
  const res = send({
    command: fbs.Command.STAT_FILE_SYNC,
    StatFilename: filename,
    StatLStat: lstat,
  });
  return new FileInfo(res);
   */
  const builder = new flatbuffers.Builder();
  const filename_ = builder.createString(filename);
  fbs.StatSync.startStatSync(builder);
  fbs.StatSync.addFilename(builder, filename_);
  fbs.StatSync.addLstat(builder, lstat);
  const msg = fbs.StatSync.endStatSync(builder);
  const baseRes = send(builder, fbs.Any.StatSync, msg);
  assert(baseRes != null);
  assert(fbs.Any.StatSyncRes === baseRes!.msgType());
  const res = new fbs.StatSyncRes();
  assert(baseRes!.msg(res) != null);
  return new FileInfo(res);
}

export function writeFileSync(
  filename: string,
  data: Uint8Array,
  perm = 0o666
): void {
  /* Ideally we could write:
  const res = send({
    command: fbs.Command.WRITE_FILE_SYNC,
    writeFileSyncFilename: filename,
    writeFileSyncData: data,
    writeFileSyncPerm: perm
  });
  */
  const builder = new flatbuffers.Builder();
  const filename_ = builder.createString(filename);
  const dataOffset = fbs.WriteFileSync.createDataVector(builder, data);
  fbs.WriteFileSync.startWriteFileSync(builder);
  fbs.WriteFileSync.addFilename(builder, filename_);
  fbs.WriteFileSync.addData(builder, dataOffset);
  fbs.WriteFileSync.addPerm(builder, perm);
  const msg = fbs.WriteFileSync.endWriteFileSync(builder);
  send(builder, fbs.Any.WriteFileSync, msg);
}