@wtf/platform-memory 1.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,10 +44,12 @@ What each member is:
44
44
  `undefined` says the command is not there: the process's `status` rejects with a `SystemError`
45
45
  of `NotFound`, as a real platform's does. Every process is recorded in `spawn.runs`, with the
46
46
  folder and the environment it ran with, whether a script answered it or not. An aborted signal
47
- ends a process that has not been answered yet, as `SIGTERM` would.
47
+ ends a process that has not been answered yet, as `SIGTERM` would, and `kill(signal)` ends it as
48
+ that signal would. Its stdin takes bytes alone: a chunk that is no `Uint8Array` ends its input,
49
+ and the script gets what it read before.
48
50
  - `navigator.storage.getDirectory()` gives the folder your `directory` function gives, called once.
49
51
  With none, it rejects with a `NotSupportedError`. This package depends on no file system: you
50
- bring your own folder handle.
52
+ bring your own folder handle, such as `memoryDirectory()` from `@wtf/platform-fs-memory/handles`.
51
53
  - Locks, channels, ports and workers run in this realm, each worker the script of its URL in
52
54
  `workers`. `setTimeout`, `clearTimeout`, `performance` and `crypto` are the real globals, passed
53
55
  through.
package/dist/process.d.ts CHANGED
@@ -13,7 +13,10 @@ import type { MemorySpawn, SpawnScript } from './index.ts';
13
13
  * else the platform's variables that are set. What the script answers is what the process wrote,
14
14
  * and its code; `undefined` is a command not there, whose status rejects with a `SystemError` of
15
15
  * `NotFound`. A process whose signal is aborted before it is answered ends at once, as one
16
- * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`. With no script, no command is there.
16
+ * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`, and one its `kill` sends a signal, as one
17
+ * that signal ended, as `SIGKILL`: `{ code: 137, signal: 'SIGKILL' }`. Its stdin takes bytes
18
+ * alone: a chunk that is none errors it, and the script is given what it read before. With no
19
+ * script, no command is there.
17
20
  *
18
21
  * ```ts
19
22
  * const spawn = memorySpawn((command, args) => (command === 'git' ? { stdout: 'main\n' } : undefined))
package/dist/process.js CHANGED
@@ -1,6 +1,8 @@
1
- import { notStarted, signalled } from '@wtf/platform/process';
1
+ import { notStarted, sendable, signalled } from '@wtf/platform/process';
2
2
  /** The variables of `env` that are set, as a process is given them. */
3
3
  const definedOf = (env) => Object.fromEntries(Object.entries(env).filter((entry) => entry[1] !== undefined));
4
+ /** Whether `chunk` is bytes, a `Uint8Array`, of any realm. */
5
+ const isBytes = (chunk) => ArrayBuffer.isView(chunk) && Object.prototype.toString.call(chunk) === '[object Uint8Array]';
4
6
  /** A stream of bytes the process writes, and what writes its text and ends it. */
5
7
  const output = () => {
6
8
  let controller;
@@ -27,7 +29,10 @@ const output = () => {
27
29
  * else the platform's variables that are set. What the script answers is what the process wrote,
28
30
  * and its code; `undefined` is a command not there, whose status rejects with a `SystemError` of
29
31
  * `NotFound`. A process whose signal is aborted before it is answered ends at once, as one
30
- * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`. With no script, no command is there.
32
+ * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`, and one its `kill` sends a signal, as one
33
+ * that signal ended, as `SIGKILL`: `{ code: 137, signal: 'SIGKILL' }`. Its stdin takes bytes
34
+ * alone: a chunk that is none errors it, and the script is given what it read before. With no
35
+ * script, no command is there.
31
36
  *
32
37
  * ```ts
33
38
  * const spawn = memorySpawn((command, args) => (command === 'git' ? { stdout: 'main\n' } : undefined))
@@ -53,6 +58,11 @@ export const memorySpawn = (script = () => undefined, { cwd = () => '/', env = {
53
58
  const read = Promise.withResolvers();
54
59
  const stdin = new WritableStream({
55
60
  write: (chunk) => {
61
+ if (!isBytes(chunk)) {
62
+ input += decoder.decode();
63
+ read.resolve();
64
+ throw new TypeError('A process reads bytes: the chunk is no Uint8Array.');
65
+ }
56
66
  input += decoder.decode(chunk, { stream: true });
57
67
  },
58
68
  close: () => {
@@ -66,15 +76,16 @@ export const memorySpawn = (script = () => undefined, { cwd = () => '/', env = {
66
76
  stderr.end(err);
67
77
  ended.resolve(status);
68
78
  };
69
- const kill = () => end(signalled('SIGTERM'));
79
+ const kill = (name) => end(signalled(sendable(name ?? 'SIGTERM')));
80
+ const abort = () => kill('SIGTERM');
70
81
  const answer = async () => {
71
82
  await read.promise;
72
83
  return script(command, run.args, { input, cwd: run.cwd, env: run.env });
73
84
  };
74
85
  if (signal?.aborted === true)
75
- kill();
86
+ abort();
76
87
  else {
77
- signal?.addEventListener('abort', kill, { once: true });
88
+ signal?.addEventListener('abort', abort, { once: true });
78
89
  answer().then((given) => {
79
90
  if (given === undefined) {
80
91
  stdout.end();
@@ -89,10 +100,10 @@ export const memorySpawn = (script = () => undefined, { cwd = () => '/', env = {
89
100
  ended.reject(notStarted(command, 'Unknown', error));
90
101
  });
91
102
  }
92
- const status = ended.promise.finally(() => signal?.removeEventListener('abort', kill));
103
+ const status = ended.promise.finally(() => signal?.removeEventListener('abort', abort));
93
104
  // A caller that reads the output alone hears of a process that could not start by its end.
94
105
  status.catch(() => { });
95
- return { stdin, stdout: stdout.stream, stderr: stderr.stream, status };
106
+ return { stdin, stdout: stdout.stream, stderr: stderr.stream, status, kill };
96
107
  };
97
108
  return Object.assign(spawn, { runs });
98
109
  };
@@ -1 +1 @@
1
- {"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAsB,MAAM,uBAAuB,CAAA;AAIjF,uEAAuE;AACvE,MAAM,SAAS,GAAG,CAAC,GAAQ,EAA0B,EAAE,CACrD,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAA6B,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CACzF,CAAA;AAEH,kFAAkF;AAClF,MAAM,MAAM,GAAG,GAAG,EAAE;IAClB,IAAI,UAAwD,CAAA;IAC5D,MAAM,MAAM,GAAG,IAAI,cAAc,CAAa;QAC5C,KAAK,EAAE,C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E,IAAI,EAAE,CAAC,CAAA;YACvD,MAAM,EAAE,CAAC,IAAI,CACX,CAAC,KAAK,EAAE,EAAE;gBACR,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;oBACxB,MAAM,CAAC,GAAG,EAAE,CAAA;oBACZ,MAAM,CAAC,GAAG,EAAE,CAAA;oBACZ,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC,CAAA;gBAC/C,CAAC;;oBAAM,GAAG,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,EAAE,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;YACnE,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;gBACjB,MAAM,CAAC,GAAG,EAAE,CAAA;gBACZ,MAAM,CAAC,GAAG,EAAE,CAAA;gBACZ,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC,CAAA;YACrD,CAAC,CACF,CAAA;QACH,CAAC;QACD,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAA;QACtF,2FAA2F;QAC3F,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;QACtB,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAA;IACxE,CAAC,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;AACvC,CAAC,CAAA","sourcesContent":["/**\n * Processes in memory: {@link memorySpawn}, a `spawn` of `@wtf/platform/process` whose processes a\n * script answers, each recorded as it is started.\n *\n * @packageDocumentation\n */\nimport type { Env } from '@wtf/platform/env'\nimport { notStarted, signalled, type ProcessStatus } from '@wtf/platform/process'\n\nimport type { MemorySpawn, SpawnRun, SpawnScript } from './index.ts'\n\n/** The variables of `env` that are set, as a process is given them. */\nconst definedOf = (env: Env): Record<string, string> =>\n Object.fromEntries(\n Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined)\n )\n\n/** A stream of bytes the process writes, and what writes its text and ends it. */\nconst output = () => {\n let controller!: ReadableStreamDefaultController<Uint8Array>\n const stream = new ReadableStream<Uint8Array>({\n start: (given) => {\n controller = given\n }\n })\n let ended = false\n const end = (text = '') => {\n if (ended) return\n ended = true\n if (text !== '') controller.enqueue(new TextEncoder().encode(text))\n controller.close()\n }\n return { stream, end }\n}\n\n/**\n * A `spawn` whose processes `script` answers, recorded in `runs` as each is started: a process\n * reads its stdin to its end, then its script is given what it read, as text, its folder, `cwd`\n * of its options or else of the platform, and its environment, `env` of its options, whole, or\n * else the platform's variables that are set. What the script answers is what the process wrote,\n * and its code; `undefined` is a command not there, whose status rejects with a `SystemError` of\n * `NotFound`. A process whose signal is aborted before it is answered ends at once, as one\n * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`. With no script, no command is there.\n *\n * ```ts\n * const spawn = memorySpawn((command, args) => (command === 'git' ? { stdout: 'main\\n' } : undefined))\n * await exec(spawn, 'git', ['branch', '--show-current']) // { stdout: 'main\\n', stderr: '', code: 0 }\n * spawn.runs // [{ command: 'git', args: ['branch', '--show-current'], cwd: '/', env: {} }]\n * ```\n */\nexport const memorySpawn = (\n script: SpawnScript = () => undefined,\n {\n cwd = () => '/',\n env = {}\n }: {\n /** The working folder of the platform, as it is when a process starts: `/` by default. */\n readonly cwd?: () => string\n /** The environment of the platform: none by default. */\n readonly env?: Env\n } = {}\n): MemorySpawn => {\n const runs: SpawnRun[] = []\n const spawn = (\n command: string,\n args: readonly string[],\n options: { cwd?: string; env?: Readonly<Record<string, string>>; signal?: AbortSignal } = {}\n ) => {\n const run: SpawnRun = {\n command,\n args: [...args],\n cwd: options.cwd ?? cwd(),\n env: { ...(options.env ?? definedOf(env)) }\n }\n runs.push(run)\n const { signal } = options\n const stdout = output()\n const stderr = output()\n const decoder = new TextDecoder()\n let input = ''\n const read = Promise.withResolvers<void>()\n const stdin = new WritableStream<Uint8Array>({\n write: (chunk) => {\n input += decoder.decode(chunk, { stream: true })\n },\n close: () => {\n input += decoder.decode()\n read.resolve()\n }\n })\n const ended = Promise.withResolvers<ProcessStatus>()\n const end = (status: ProcessStatus, out?: string, err?: string) => {\n stdout.end(out)\n stderr.end(err)\n ended.resolve(status)\n }\n const kill = () => end(signalled('SIGTERM'))\n const answer = async () => {\n await read.promise\n return script(command, run.args, { input, cwd: run.cwd, env: run.env })\n }\n if (signal?.aborted === true) kill()\n else {\n signal?.addEventListener('abort', kill, { once: true })\n answer().then(\n (given) => {\n if (given === undefined) {\n stdout.end()\n stderr.end()\n ended.reject(notStarted(command, 'NotFound'))\n } else end({ code: given.code ?? 0 }, given.stdout, given.stderr)\n },\n (error: unknown) => {\n stdout.end()\n stderr.end()\n ended.reject(notStarted(command, 'Unknown', error))\n }\n )\n }\n const status = ended.promise.finally(() => signal?.removeEventListener('abort', kill))\n // A caller that reads the output alone hears of a process that could not start by its end.\n status.catch(() => {})\n return { stdin, stdout: stdout.stream, stderr: stderr.stream, status }\n }\n return Object.assign(spawn, { runs })\n}\n"]}
1
+ {"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAOA,OAAO,EAEL,UAAU,EAEV,QAAQ,EACR,SAAS,EACV,MAAM,uBAAuB,CAAA;AAI9B,uEAAuE;AACvE,MAAM,SAAS,GAAG,CAAC,GAAQ,EAA0B,EAAE,CACrD,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAA6B,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAC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AAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,SAAS,EAAE,KAAK,CAAC,CAAC,CAAA;YACrD,CAAC,CACF,CAAA;QACH,CAAC;QACD,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAA;QACvF,2FAA2F;QAC3F,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;QACtB,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,CAAA;IAC9E,CAAC,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,CAAA;AACvC,CAAC,CAAA","sourcesContent":["/**\n * Processes in memory: {@link memorySpawn}, a `spawn` of `@wtf/platform/process` whose processes a\n * script answers, each recorded as it is started.\n *\n * @packageDocumentation\n */\nimport type { Env } from '@wtf/platform/env'\nimport {\n type ChildProcess,\n notStarted,\n type ProcessStatus,\n sendable,\n signalled\n} from '@wtf/platform/process'\n\nimport type { MemorySpawn, SpawnRun, SpawnScript } from './index.ts'\n\n/** The variables of `env` that are set, as a process is given them. */\nconst definedOf = (env: Env): Record<string, string> =>\n Object.fromEntries(\n Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined)\n )\n\n/** Whether `chunk` is bytes, a `Uint8Array`, of any realm. */\nconst isBytes = (chunk: unknown): chunk is Uint8Array =>\n ArrayBuffer.isView(chunk) && Object.prototype.toString.call(chunk) === '[object Uint8Array]'\n\n/** A stream of bytes the process writes, and what writes its text and ends it. */\nconst output = () => {\n let controller!: ReadableStreamDefaultController<Uint8Array>\n const stream = new ReadableStream<Uint8Array>({\n start: (given) => {\n controller = given\n }\n })\n let ended = false\n const end = (text = '') => {\n if (ended) return\n ended = true\n if (text !== '') controller.enqueue(new TextEncoder().encode(text))\n controller.close()\n }\n return { stream, end }\n}\n\n/**\n * A `spawn` whose processes `script` answers, recorded in `runs` as each is started: a process\n * reads its stdin to its end, then its script is given what it read, as text, its folder, `cwd`\n * of its options or else of the platform, and its environment, `env` of its options, whole, or\n * else the platform's variables that are set. What the script answers is what the process wrote,\n * and its code; `undefined` is a command not there, whose status rejects with a `SystemError` of\n * `NotFound`. A process whose signal is aborted before it is answered ends at once, as one\n * `SIGTERM` ended: `{ code: 143, signal: 'SIGTERM' }`, and one its `kill` sends a signal, as one\n * that signal ended, as `SIGKILL`: `{ code: 137, signal: 'SIGKILL' }`. Its stdin takes bytes\n * alone: a chunk that is none errors it, and the script is given what it read before. With no\n * script, no command is there.\n *\n * ```ts\n * const spawn = memorySpawn((command, args) => (command === 'git' ? { stdout: 'main\\n' } : undefined))\n * await exec(spawn, 'git', ['branch', '--show-current']) // { stdout: 'main\\n', stderr: '', code: 0 }\n * spawn.runs // [{ command: 'git', args: ['branch', '--show-current'], cwd: '/', env: {} }]\n * ```\n */\nexport const memorySpawn = (\n script: SpawnScript = () => undefined,\n {\n cwd = () => '/',\n env = {}\n }: {\n /** The working folder of the platform, as it is when a process starts: `/` by default. */\n readonly cwd?: () => string\n /** The environment of the platform: none by default. */\n readonly env?: Env\n } = {}\n): MemorySpawn => {\n const runs: SpawnRun[] = []\n const spawn = (\n command: string,\n args: readonly string[],\n options: { cwd?: string; env?: Readonly<Record<string, string>>; signal?: AbortSignal } = {}\n ) => {\n const run: SpawnRun = {\n command,\n args: [...args],\n cwd: options.cwd ?? cwd(),\n env: { ...(options.env ?? definedOf(env)) }\n }\n runs.push(run)\n const { signal } = options\n const stdout = output()\n const stderr = output()\n const decoder = new TextDecoder()\n let input = ''\n const read = Promise.withResolvers<void>()\n const stdin = new WritableStream<Uint8Array>({\n write: (chunk: unknown) => {\n if (!isBytes(chunk)) {\n input += decoder.decode()\n read.resolve()\n throw new TypeError('A process reads bytes: the chunk is no Uint8Array.')\n }\n input += decoder.decode(chunk, { stream: true })\n },\n close: () => {\n input += decoder.decode()\n read.resolve()\n }\n })\n const ended = Promise.withResolvers<ProcessStatus>()\n const end = (status: ProcessStatus, out?: string, err?: string) => {\n stdout.end(out)\n stderr.end(err)\n ended.resolve(status)\n }\n const kill: ChildProcess['kill'] = (name) => end(signalled(sendable(name ?? 'SIGTERM')))\n const abort = () => kill('SIGTERM')\n const answer = async () => {\n await read.promise\n return script(command, run.args, { input, cwd: run.cwd, env: run.env })\n }\n if (signal?.aborted === true) abort()\n else {\n signal?.addEventListener('abort', abort, { once: true })\n answer().then(\n (given) => {\n if (given === undefined) {\n stdout.end()\n stderr.end()\n ended.reject(notStarted(command, 'NotFound'))\n } else end({ code: given.code ?? 0 }, given.stdout, given.stderr)\n },\n (error: unknown) => {\n stdout.end()\n stderr.end()\n ended.reject(notStarted(command, 'Unknown', error))\n }\n )\n }\n const status = ended.promise.finally(() => signal?.removeEventListener('abort', abort))\n // A caller that reads the output alone hears of a process that could not start by its end.\n status.catch(() => {})\n return { stdin, stdout: stdout.stream, stderr: stderr.stream, status, kill }\n }\n return Object.assign(spawn, { runs })\n}\n"]}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@wtf/platform-memory",
3
- "version": "1.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A platform in memory, for tests and dry runs of any program on @wtf/platform: memoryPlatform() keeps what the program logs, displays and opens, answers its fetches and its processes by your scripts, records each process it runs, and passes the real timers and crypto through.",
5
- "homepage": "https://gitlab.com/wetryfun/platform/platform/-/tree/main/packages/memory",
5
+ "homepage": "https://gitlab.com/wetryfun/platform/platform/-/tree/main/packages/platform-memory",
6
6
  "bugs": {
7
7
  "url": "https://gitlab.com/wetryfun/platform/platform/-/issues"
8
8
  },
@@ -11,7 +11,7 @@
11
11
  "repository": {
12
12
  "type": "git",
13
13
  "url": "git+https://gitlab.com/wetryfun/platform/platform.git",
14
- "directory": "packages/memory"
14
+ "directory": "packages/platform-memory"
15
15
  },
16
16
  "files": [
17
17
  "dist"
@@ -38,7 +38,9 @@
38
38
  "build": "tsc -b"
39
39
  },
40
40
  "dependencies": {
41
- "@wtf/platform": "1.0.0"
41
+ "@wtf/platform": "2.1.0"
42
42
  },
43
- "devDependencies": {}
43
+ "devDependencies": {
44
+ "@wtf/platform-suite": "2.1.0"
45
+ }
44
46
  }