@phpsandbox/sdk 0.1.13 → 0.1.15

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 (40) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +7 -18
  3. package/dist/browser/phpsandbox-sdk.esm.js +25 -14
  4. package/dist/browser/phpsandbox-sdk.esm.min.js +9 -9
  5. package/dist/browser/phpsandbox-sdk.iife.js +25 -14
  6. package/dist/browser/phpsandbox-sdk.iife.min.js +9 -9
  7. package/dist/errors/index.d.ts +7 -1
  8. package/dist/errors/index.js +2 -1
  9. package/dist/index.d.ts +27 -6
  10. package/dist/index.js +16 -12
  11. package/dist/{integrations.d.ts → integrations/account.d.ts} +1 -1
  12. package/dist/integrations/index.d.ts +2 -0
  13. package/dist/integrations/index.js +2 -0
  14. package/dist/{sandbox-integrations.d.ts → integrations/sandbox.d.ts} +2 -2
  15. package/dist/{sandbox-integrations.js → integrations/sandbox.js} +1 -1
  16. package/dist/log.d.ts +1 -1
  17. package/dist/log.js +1 -1
  18. package/dist/metrics.d.ts +1 -1
  19. package/dist/metrics.js +1 -1
  20. package/dist/ports.d.ts +1 -1
  21. package/dist/ports.js +1 -1
  22. package/dist/runtime.d.ts +1 -1
  23. package/dist/runtime.js +1 -1
  24. package/package.json +5 -9
  25. package/docs/api-summary.md +0 -181
  26. package/docs/cdn-usage.md +0 -68
  27. package/docs/getting-started.md +0 -169
  28. package/examples/README.md +0 -203
  29. package/examples/basic.ts +0 -15
  30. package/examples/get.ts +0 -22
  31. package/examples/git-merge-smoke.ts +0 -395
  32. package/examples/laravel-load-test.ts +0 -2800
  33. package/examples/notebook-readonly-smoke.ts +0 -806
  34. package/examples/publication-provider-label.ts +0 -11
  35. package/examples/publish-cloudflare-container.ts +0 -65
  36. package/examples/publish-laravel-cloud.ts +0 -123
  37. package/examples/publish-ssh-agent.ts +0 -211
  38. /package/dist/{integrations.js → integrations/account.js} +0 -0
  39. /package/dist/{runtime-telemetry.d.ts → telemetry.d.ts} +0 -0
  40. /package/dist/{runtime-telemetry.js → telemetry.js} +0 -0
@@ -1,169 +0,0 @@
1
- # Getting Started with PHPSandbox SDK
2
-
3
- This guide walks through the fastest path from install to a working notebook.
4
-
5
- ## Prerequisites
6
-
7
- - Node.js 18+
8
- - A PHPSandbox account with an active subscription and available notebook capacity
9
- - SDK access enabled and an API key created in **API keys**
10
-
11
- ## Install
12
-
13
- ```bash
14
- npm install @phpsandbox/sdk
15
- ```
16
-
17
- Copy the API key when shown and store it in `PHPSANDBOX_TOKEN` in your server environment or secret store. Keys are shown once and grant notebook read and write access; never include them in frontend bundles or commit them. If **API keys** is unavailable, request SDK access first.
18
-
19
- For a complete runnable example, save the [root quick start](../README.md#quick-start) as `quickstart.mjs` and run `node quickstart.mjs`. The snippets below explain individual operations.
20
-
21
- ## 1. Create a Client
22
-
23
- ```ts
24
- import { PHPSandbox } from '@phpsandbox/sdk';
25
-
26
- const token = process.env.PHPSANDBOX_TOKEN;
27
- if (!token) throw new Error('Missing PHPSANDBOX_TOKEN');
28
-
29
- const client = PHPSandbox.realtime(token);
30
- ```
31
-
32
- ## 2. Create or Open a Notebook
33
-
34
- ```ts
35
- const notebook = await client.notebook.create('standard');
36
-
37
- const persistent = await client.notebook.create('laravel', {
38
- title: 'Persistent Laravel',
39
- persistent: true,
40
- });
41
-
42
- const existing = await client.notebook.get('notebook-id');
43
-
44
- const fork = await existing.fork();
45
- const persistentFork = await existing.fork({ persistent: true });
46
- ```
47
-
48
- API notebook creates and forks are ephemeral by default. Use `persistent: true` only when your account is allowed to create persistent notebooks.
49
-
50
- ## 3. Write and Read Files
51
-
52
- ```ts
53
- await notebook.files.write('hello.php', '<?php echo "Hello";');
54
-
55
- const data = await notebook.files.read('hello.php');
56
- const text = new TextDecoder().decode(data);
57
-
58
- console.log(text);
59
- ```
60
-
61
- The same `write()` method accepts text, bytes, readable streams, and async iterables:
62
-
63
- ```ts
64
- await notebook.files.write('binary.dat', new Uint8Array([1, 2, 3]), {
65
- create: true,
66
- overwrite: true,
67
- unlock: false,
68
- atomic: false,
69
- });
70
- ```
71
-
72
- ## 4. Run Commands
73
-
74
- ### Terminal (stream output)
75
-
76
- ```ts
77
- const task = await notebook.terminals.create({ command: 'php', args: ['hello.php'] });
78
- const reader = task.output.getReader();
79
-
80
- while (true) {
81
- const { done, value } = await reader.read();
82
- if (done) break;
83
- process.stdout.write(value);
84
- }
85
- reader.releaseLock();
86
-
87
- const exitCode = await task.wait();
88
- console.log(`\nExit code: ${exitCode}`);
89
- ```
90
-
91
- ### Shell (single command result)
92
-
93
- ```ts
94
- const result = await notebook.exec('php -v', { tty: false });
95
- if (result.exitCode !== 0) throw new Error(result.stderr || result.output);
96
- console.log(result.stdout);
97
- ```
98
-
99
- ## 5. Search and Watch Files
100
-
101
- ```ts
102
- const matches = await notebook.files.find('*.php', {
103
- includes: ['app/**'],
104
- excludes: ['vendor/**'],
105
- });
106
-
107
- console.log(matches.map((match) => match.path));
108
-
109
- const watcher = await notebook.files.watch(
110
- '/app',
111
- {
112
- recursive: true,
113
- excludes: ['vendor/**', 'node_modules/**'],
114
- },
115
- (change) => {
116
- console.log(change.type, change.path);
117
- }
118
- );
119
-
120
- // later
121
- watcher.dispose();
122
- ```
123
-
124
- ## 6. Use Composer and Git
125
-
126
- ```ts
127
- const installResult = await notebook.composer.run('install').wait();
128
- if (installResult.exitCode !== 0) throw new Error(installResult.stderr || installResult.output);
129
-
130
- const requireResult = await notebook.composer.run('require', { packages: ['monolog/monolog'] }).wait();
131
- if (requireResult.exitCode !== 0) throw new Error(requireResult.stderr || requireResult.output);
132
-
133
- await notebook.git.checkpoint('Jane Doe <jane@example.com>', 'Initial checkpoint');
134
- ```
135
-
136
- ## 7. Clean Up
137
-
138
- Use a `finally` block to dispose every connection, including forks and notebooks opened with `get()`. `dispose()` only closes local resources. For temporary automation, delete the remote notebook as well:
139
-
140
- ```ts
141
- try {
142
- await notebook.destroy();
143
- } finally {
144
- notebook.dispose();
145
- }
146
- ```
147
-
148
- Omit `destroy()` only when you intend to keep the notebook. Stopping a notebook does not delete it. Ephemeral notebooks are eventually pruned according to `notebook.data.policy`; do not rely on that as immediate cleanup.
149
-
150
- ## Common Pitfalls
151
-
152
- - Runtime actions wait for readiness. Call `ready()` only when you need its provisioning metadata.
153
- - `files.watch()` requires an `excludes` array.
154
- - Command results include `exitCode`; decide explicitly whether a non-zero exit should fail your workflow.
155
-
156
- ## Troubleshooting
157
-
158
- - `401`: check that the API key is set and has not been revoked.
159
- - `403`: check permissions, account subscription, notebook capacity, and persistence entitlement.
160
- - `404`: the notebook is missing or inaccessible to your account.
161
- - `422`: use `RemoteError.isValidation(error)` to inspect field errors; `standard` is the plain PHP template ID.
162
- - `429`: respect `error.response.headers.get('Retry-After')` and reduce concurrency. Avoid blind retries of resource creation or publication.
163
- - A process stays alive after the script finishes: ensure all notebook instances are disposed in `finally`.
164
-
165
- ## Next Reading
166
-
167
- - API overview: [API overview](api-summary.md)
168
- - CDN/browser usage: [Browser usage](cdn-usage.md)
169
- - Root quick start: [Root quick start](../README.md)
@@ -1,203 +0,0 @@
1
- ## Examples
2
-
3
- Examples import the published `@phpsandbox/sdk` package. From a source checkout, run `npm ci && npm run build` first. Set `PHPSANDBOX_TOKEN` using your environment or secret store. The basic example creates and deletes one temporary notebook; publication examples can change deployments and provider resources.
4
-
5
- ### Running Basic Example
6
-
7
- ```bash
8
- PHPSANDBOX_TOKEN=your-token npx tsx examples/basic.ts
9
- ```
10
-
11
- ### Running Publication Examples
12
-
13
- Cloudflare Containers target:
14
-
15
- ```bash
16
- PHPSANDBOX_TOKEN=your-token PHPSANDBOX_NOTEBOOK_ID=notebook-id \
17
- npx tsx examples/publish-cloudflare-container.ts
18
- ```
19
-
20
- SSH agent target:
21
-
22
- ```bash
23
- PHPSANDBOX_TOKEN=your-token PHPSANDBOX_NOTEBOOK_ID=notebook-id \
24
- npx tsx examples/publish-ssh-agent.ts
25
- ```
26
-
27
- Laravel Cloud target:
28
-
29
- ```bash
30
- PHPSANDBOX_TOKEN=your-token PHPSANDBOX_NOTEBOOK_ID=notebook-id \
31
- PHPSANDBOX_LARAVEL_CLOUD_REPOSITORY=owner/repo \
32
- PHPSANDBOX_GITHUB_TOKEN=github-token \
33
- PHPSANDBOX_LARAVEL_CLOUD_API_KEY=lc-api-key \
34
- npx tsx examples/publish-laravel-cloud.ts
35
- ```
36
-
37
- Publication examples accept `PHPSANDBOX_API_URL` and `PHPSANDBOX_PUBLISH_SLUG`. On the first run they create the notebook's publication; later runs publish it again without changing slug or provider. The SSH example creates a server registration when `PHPSANDBOX_PUBLISH_SERVER_ID` is not set; on local `.test` APIs it also starts local Rook from `PHPSANDBOX_ROOK_DIR` or the sibling `rook` repo. The Laravel Cloud example links or refreshes the GitHub and Laravel Cloud integrations, attaches each provider to the sandbox, synchronizes the notebook to `PHPSANDBOX_LARAVEL_CLOUD_REPOSITORY`, and publishes through the attached Laravel Cloud integration.
38
-
39
- ### Running The Laravel Load Test
40
-
41
- When stdout is a TTY, the load test renders a live dashboard with current stage, active scenarios, and rolling results. When output is redirected or piped, it falls back to plain logs.
42
-
43
- Baseline one sandbox:
44
-
45
- ```bash
46
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts --levels=1
47
- ```
48
-
49
- Init-only baseline one sandbox:
50
-
51
- ```bash
52
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts --levels=1 --mode=init
53
- ```
54
-
55
- Creation/provisioning parity baseline for a fresh Laravel notebook:
56
-
57
- ```bash
58
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts \
59
- --local \
60
- --levels=1 \
61
- --mode=init \
62
- --require-init-messages="Preparing your workspace...|Setting up your project...|Opening your workspace..." \
63
- --expect-provisioned=true \
64
- --expect-provisioning-action=template \
65
- --json-file=/tmp/phpsandbox-create-init.json
66
- ```
67
-
68
- Ramp concurrency over multiple levels:
69
-
70
- ```bash
71
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts --levels=1,2,4,8 --waves=3
72
- ```
73
-
74
- Or generate levels from a max concurrency:
75
-
76
- ```bash
77
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts --max-concurrency=10 --step=2 --waves=2
78
- ```
79
-
80
- Local API with a self-signed certificate:
81
-
82
- ```bash
83
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts \
84
- --local \
85
- --levels=1
86
- ```
87
-
88
- Local API with a trusted CA file:
89
-
90
- ```bash
91
- PHPSANDBOX_TOKEN=your-token npx tsx examples/laravel-load-test.ts \
92
- --api-url=https://api.phpsandbox.test/v1 \
93
- --ca-file=/path/to/rootCA.pem \
94
- --levels=1
95
- ```
96
-
97
- Useful flags:
98
-
99
- - `--no-cleanup` keeps every notebook after the run.
100
- - `--keep-failed` keeps failed notebooks for inspection. By default, failures are deleted too.
101
- - `--runtime-origin=ws://127.0.0.1:9511` preserves the SDK-issued websocket path/auth but routes runtime traffic to another host.
102
- - `--include-container-restart` adds a runtime restart check on the fresh notebook.
103
- - `--include-download` adds `file.download()` to the baseline.
104
- - `--include-preview` fetches the initialized preview URL in init mode, or the route written by the scenario in full mode.
105
- - `--include-lsp` adds an LSP initialize handshake to the baseline.
106
- - `--require-init-messages="Preparing...|Setting up...|Opening..."` asserts frontend-visible init messages were received in order.
107
- - `--expect-provisioned=true` asserts the `notebook.ready()` initialization result reports provisioning work.
108
- - `--expect-provisioning-action=template` asserts the initialization result action.
109
- - `--reauthorization-url=wss://...?ticket=...` exercises `notebook.auth.login()` / `logout()` with a fresh ticket-bearing runtime URL.
110
- - `--mode=init` limits the scenario to notebook create, ready, and destroy so you can isolate init capacity.
111
- - `--init-only` is shorthand for `--mode=init`.
112
- - `--stop-on-failure-level` stops after the first level that has at least one failing scenario.
113
- - `--local` switches the default API URL to `https://api.phpsandbox.test/v1` and defaults TLS to insecure unless you also pass `--ca-file` or `--no-insecure-tls`.
114
- - `--json` prints a machine-readable summary after the text report.
115
- - `--json-file=/path/to/run.json` writes a machine-readable artifact for later analysis.
116
- - `--insecure-tls` disables certificate verification for local HTTPS/WSS testing.
117
- - `--ca-file=/path/to/rootCA.pem` trusts a custom CA for local HTTPS/WSS testing.
118
-
119
- Default baseline coverage:
120
-
121
- - notebook lifecycle: create, ready, ping, destroy
122
- - secrets
123
- - container state and telemetry; optional restart with `--include-container-restart`
124
- - filesystem watch/write/read/search/find/tree/tail
125
- - optional preview fetch with `--include-preview`
126
- - shell process execution and streams
127
- - terminal spawn streaming
128
- - services with Redis logs
129
- - composer metadata and autoload dump
130
- - git checkpoint/log
131
- - REPL eval
132
-
133
- Intentionally excluded by default:
134
-
135
- - `beacon`, because it needs a browser iframe runtime
136
- - `auth`, because it needs a fresh ticket-bearing runtime URL
137
- - `lsp`, because it is useful but more brittle than the rest of the headless baseline
138
-
139
- ### Running A Read-Only Existing Notebook Smoke Test
140
-
141
- Use this when you want to compare an existing notebook, such as `gypjc`, without writing to it:
142
-
143
- ```bash
144
- PHPSANDBOX_TOKEN=your-token npx tsx examples/notebook-readonly-smoke.ts \
145
- --notebook-id=gypjc \
146
- --json-file=/tmp/gypjc-smoke.json
147
- ```
148
-
149
- Local API with a self-signed certificate:
150
-
151
- ```bash
152
- PHPSANDBOX_TOKEN=your-token npx tsx examples/notebook-readonly-smoke.ts \
153
- --local \
154
- --notebook-id=gypjc
155
- ```
156
-
157
- To preserve the SDK-issued notebook auth/path but route the websocket to a different runtime host:
158
-
159
- ```bash
160
- PHPSANDBOX_TOKEN=your-token npx tsx examples/notebook-readonly-smoke.ts \
161
- --notebook-id=gypjc \
162
- --runtime-origin=ws://127.0.0.1:9501
163
- ```
164
-
165
- Default smoke coverage:
166
-
167
- - notebook get, ready, and ping
168
- - container state and opened ports
169
- - filesystem directory/tree/find/read/search
170
- - read-only git status/log, service list, and composer packages where supported
171
-
172
- Optional flags:
173
-
174
- - `--include-exec` adds shell, terminal spawn, terminal stdin/stdout, terminal OSC, and REPL execution checks.
175
- - `--include-download` adds a read-only `file.download()` check for a known file.
176
- - `--include-preview` fetches the initialized preview URL.
177
-
178
- ### Running A Git Merge Smoke Test
179
-
180
- Use this to fork a base notebook, edit the fork, checkpoint the fork, and preview the diff from the base notebook:
181
-
182
- ```bash
183
- PHPSANDBOX_TOKEN=your-token npx tsx examples/git-merge-smoke.ts \
184
- --local \
185
- --notebook-id=10ywr \
186
- --allow-checkpoint
187
- ```
188
-
189
- Pass `--apply` only when you want the merge to mutate the base notebook disk:
190
-
191
- ```bash
192
- PHPSANDBOX_TOKEN=your-token npx tsx examples/git-merge-smoke.ts \
193
- --local \
194
- --notebook-id=10ywr \
195
- --allow-checkpoint \
196
- --apply
197
- ```
198
-
199
- By default the created fork is kept for inspection. Pass `--delete-fork` when you want the script to delete it after the run.
200
-
201
- ### Repository-only operator tools
202
-
203
- The `tools/okra-loop.ts` operator utility requires the source checkout and maintainer infrastructure access. It is not included in the published package and is not part of consumer onboarding.
package/examples/basic.ts DELETED
@@ -1,15 +0,0 @@
1
- import { PHPSandbox } from '@phpsandbox/sdk';
2
-
3
- const token = process.env.PHPSANDBOX_TOKEN;
4
- if (!token) throw new Error('Missing PHPSANDBOX_TOKEN');
5
-
6
- const client = PHPSandbox.realtime(token);
7
- const notebook = await client.notebook.create('standard');
8
-
9
- await notebook.files.write('hello.php', '<?php echo "Hello from PHPSandbox";');
10
- const result = await notebook.exec('php', ['hello.php']);
11
- if (result.exitCode !== 0) throw new Error(result.stderr || result.output);
12
- console.log(result.stdout.trim());
13
-
14
- await notebook.destroy();
15
- notebook.dispose();
package/examples/get.ts DELETED
@@ -1,22 +0,0 @@
1
- import { PHPSandbox } from '@phpsandbox/sdk';
2
-
3
- const token = process.env.PHPSANDBOX_TOKEN;
4
- if (!token) {
5
- throw new Error('Missing PHPSANDBOX_TOKEN');
6
- }
7
-
8
- const notebookId = process.env.PHPSANDBOX_NOTEBOOK_ID;
9
- if (!notebookId) {
10
- throw new Error('Missing PHPSANDBOX_NOTEBOOK_ID');
11
- }
12
-
13
- const apiUrl = process.env.PHPSANDBOX_API_URL ?? 'https://api.phpsandbox.io/v1';
14
- const client = PHPSandbox.realtime(token, apiUrl);
15
- const notebook = await client.notebook.get(notebookId);
16
-
17
- try {
18
- console.log('notebook:', notebook.data.id, notebook.data.title ?? '');
19
- console.log(await notebook.mail.status());
20
- } finally {
21
- notebook.dispose();
22
- }