@e2e-dev/smol 0.0.0-stage → 0.0.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +60 -2
- package/dist/.build.json +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/machines.d.ts +46 -0
- package/dist/machines.js +78 -0
- package/dist/provider.d.ts +82 -0
- package/dist/provider.js +282 -0
- package/package.json +68 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @e2e-dev/smol
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[smol machines](https://smolmachines.com) browsers for [`e2e`](https://www.npmjs.com/package/e2e):
|
|
4
|
+
`web({ browser: smol() })` runs each test attempt in its own branch of a warm
|
|
5
|
+
Chromium microVM on your computer.
|
|
6
|
+
|
|
7
|
+
## Why choose it
|
|
8
|
+
|
|
9
|
+
Use `smol()` for stateful web tests that need a fresh browser for each attempt,
|
|
10
|
+
including retries. A warm browser is prepared once and then branched; with
|
|
11
|
+
`app`, each branch also gets its own running app and data stored in the VM.
|
|
12
|
+
This lets tests change files or a local database without carrying that state
|
|
13
|
+
into the next attempt. The machines run locally, without a hosted browser
|
|
14
|
+
account. External services are still shared between attempts.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install --save-dev @e2e-dev/smol smolmachines
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ts title="e2e.config.ts"
|
|
25
|
+
import type { E2EConfig } from 'e2e';
|
|
26
|
+
import { web } from '@e2e-dev/web';
|
|
27
|
+
import { smol } from '@e2e-dev/smol';
|
|
28
|
+
|
|
29
|
+
export default {
|
|
30
|
+
targets: [
|
|
31
|
+
{
|
|
32
|
+
engine: web({ browser: smol({ hostPorts: [3000] }) }),
|
|
33
|
+
app: { url: 'http://localhost:3000', command: { executable: 'npm', args: ['run', 'dev'] } },
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
workers: 4,
|
|
37
|
+
} satisfies E2EConfig;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Each worker slot boots one Chromium machine on the smol engine embedded in
|
|
41
|
+
`smolmachines` (macOS on Apple Silicon, or Linux with KVM). With the default
|
|
42
|
+
`scope: 'attempt'`, every attempt then gets a copy-on-write branch of that
|
|
43
|
+
running browser typically in about a second, depending on port readiness.
|
|
44
|
+
It has its own memory and disk and is deleted when the attempt ends.
|
|
45
|
+
`prepare(cdpEndpoint)` drives the warm browser once before it is branched, so
|
|
46
|
+
every attempt starts signed in. `hostPorts` lists the
|
|
47
|
+
ports on your computer's loopback the browser reaches as its own `localhost`.
|
|
48
|
+
These ports are not an access policy: the machine can also reach every host
|
|
49
|
+
loopback port through `host.smolvm.internal` and has outbound network access.
|
|
50
|
+
`setup` runs a shell script in the machine before Chromium starts.
|
|
51
|
+
`app: { source, setup, start, port }` runs the app under test inside the
|
|
52
|
+
machine instead of on your computer, so in `attempt` scope every attempt
|
|
53
|
+
also gets its own copy of the running app and the data it keeps on the machine.
|
|
54
|
+
`scope: 'worker'` keeps one browser machine per worker slot instead, shared by
|
|
55
|
+
the attempts on that slot.
|
|
56
|
+
|
|
57
|
+
Full documentation lives at [e2e.tester.army/docs/integrations/smol](https://e2e.tester.army/docs/integrations/smol).
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
Apache-2.0
|
package/dist/.build.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"0.0.0"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@e2e-dev/smol` public surface: `smol()`, a browser provider that runs
|
|
3
|
+
* Chromium in smol machines microVMs for `@e2e-dev/web`, branching a warm
|
|
4
|
+
* browser per test attempt by default.
|
|
5
|
+
*/
|
|
6
|
+
export { smol } from './provider.ts';
|
|
7
|
+
export type { SmolApp, SmolOptions } from './provider.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** The slice of the smolmachines SDK the provider uses, loaded on first use so a config load never pays for it. */
|
|
2
|
+
/** A guest port published on a host port. */
|
|
3
|
+
export interface Port {
|
|
4
|
+
readonly host: number;
|
|
5
|
+
readonly guest: number;
|
|
6
|
+
}
|
|
7
|
+
/** What a new browser machine is created with. */
|
|
8
|
+
export interface SmolMachineParams {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly image: string;
|
|
11
|
+
readonly cpus: number;
|
|
12
|
+
readonly memoryMb: number;
|
|
13
|
+
/** The one published port, `guest` reached at `host` on this computer's loopback. */
|
|
14
|
+
readonly port: Port;
|
|
15
|
+
readonly labels: Readonly<Record<string, string>>;
|
|
16
|
+
/**
|
|
17
|
+
* Keep the machine's record when the process that made it exits, so
|
|
18
|
+
* another process can attach to it by name; the engine stops the VM either
|
|
19
|
+
* way. Without it the engine also deletes the machine.
|
|
20
|
+
*/
|
|
21
|
+
readonly persistent: boolean;
|
|
22
|
+
/** A directory on this computer the machine sees read-only, `source` at `target`. */
|
|
23
|
+
readonly mount?: {
|
|
24
|
+
readonly source: string;
|
|
25
|
+
readonly target: string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
}
|
|
28
|
+
/** One running machine. */
|
|
29
|
+
export interface SmolMachine {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
/** Runs `sh -c script`, failing on a non-zero exit with its stderr. */
|
|
32
|
+
shell(script: string, signal?: AbortSignal): Promise<void>;
|
|
33
|
+
/** A copy-on-write child of the running machine, the port it publishes moved to `port.host`. */
|
|
34
|
+
branch(name: string, port: Port): Promise<SmolMachine>;
|
|
35
|
+
}
|
|
36
|
+
export interface SmolMachines {
|
|
37
|
+
create(params: SmolMachineParams): Promise<SmolMachine>;
|
|
38
|
+
/** Deletes the machine; one the engine no longer knows counts as deleted. Resolves to whether it still knew it. */
|
|
39
|
+
delete(name: string): Promise<boolean>;
|
|
40
|
+
/** The names of every machine the engine knows. */
|
|
41
|
+
list(): Promise<string[]>;
|
|
42
|
+
/** The bytes of one file on the machine's own disk. */
|
|
43
|
+
readFile(name: string, file: string): Promise<Uint8Array>;
|
|
44
|
+
}
|
|
45
|
+
/** Machines on this computer's smol engine, through the SDK. */
|
|
46
|
+
export declare function smolMachines(): SmolMachines;
|
package/dist/machines.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** The slice of the smolmachines SDK the provider uses, loaded on first use so a config load never pays for it. */
|
|
2
|
+
/** Every machine runs on this computer's embedded engine; a library names its target instead of reading `SMOL_CLOUD_TOKEN`. */
|
|
3
|
+
const LOCAL = { target: 'local', handleSignals: false };
|
|
4
|
+
/** An orphan's browser may have stopped; reconnect only needs the agent to delete it. */
|
|
5
|
+
const LOCAL_CONNECT = { ...LOCAL, waitForPorts: false };
|
|
6
|
+
/** Wraps an SDK machine, remembering its handle for a later delete or read from this process. */
|
|
7
|
+
function wrap(machine, handles) {
|
|
8
|
+
handles.set(machine.name, machine);
|
|
9
|
+
return {
|
|
10
|
+
name: machine.name,
|
|
11
|
+
async shell(script, signal) {
|
|
12
|
+
const result = await machine.exec(['sh', '-c', script], signal === undefined ? undefined : { signal });
|
|
13
|
+
if (result.exitCode !== 0) {
|
|
14
|
+
const detail = (result.stderr.trim() || result.stdout.trim()).split('\n').slice(-5).join('; ');
|
|
15
|
+
throw new Error(`machine ${machine.name}: command exited ${result.exitCode}${detail === '' ? '' : `: ${detail}`}`);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
branch: async (name, port) => wrap(await machine.branch(name, { ports: [{ ...port }] }), handles),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Whether an SDK error says the machine does not exist. */
|
|
22
|
+
function isNotFound(cause) {
|
|
23
|
+
return cause instanceof Error && /not found|no such machine|does not exist/i.test(cause.message);
|
|
24
|
+
}
|
|
25
|
+
/** Concurrent first boots can race while the embedded engine configures SQLite pragmas. */
|
|
26
|
+
async function createWithDatabaseRetry(create) {
|
|
27
|
+
for (let attempt = 0;; attempt += 1) {
|
|
28
|
+
try {
|
|
29
|
+
return await create();
|
|
30
|
+
}
|
|
31
|
+
catch (cause) {
|
|
32
|
+
if (attempt >= 3 || !(cause instanceof Error && /configure pragmas: database is locked/i.test(cause.message)))
|
|
33
|
+
throw cause;
|
|
34
|
+
await new Promise((resolve) => setTimeout(resolve, 50 * 2 ** attempt));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Machines on this computer's smol engine, through the SDK. */
|
|
39
|
+
export function smolMachines() {
|
|
40
|
+
const loadSdk = () => import('smolmachines');
|
|
41
|
+
const handles = new Map();
|
|
42
|
+
/** This process's handle on the machine, else one attached by name: a lease may be released or read by another process than the one that made it. */
|
|
43
|
+
const attach = async (name) => handles.get(name) ?? (await loadSdk()).Machine.connect(name, LOCAL_CONNECT);
|
|
44
|
+
return {
|
|
45
|
+
async create(params) {
|
|
46
|
+
const { Machine } = await loadSdk();
|
|
47
|
+
return wrap(await createWithDatabaseRetry(() => Machine.create({
|
|
48
|
+
name: params.name,
|
|
49
|
+
image: params.image,
|
|
50
|
+
network: true,
|
|
51
|
+
branchable: true,
|
|
52
|
+
// Chromium starts through exec below; no port listens during create.
|
|
53
|
+
waitForPorts: false,
|
|
54
|
+
ports: [{ ...params.port }],
|
|
55
|
+
resources: { cpus: params.cpus, memoryMb: params.memoryMb },
|
|
56
|
+
labels: { ...params.labels },
|
|
57
|
+
persistent: params.persistent,
|
|
58
|
+
...(params.mount === undefined ? {} : { mounts: [{ ...params.mount, readOnly: true }] }),
|
|
59
|
+
}, LOCAL)), handles);
|
|
60
|
+
},
|
|
61
|
+
async delete(name) {
|
|
62
|
+
try {
|
|
63
|
+
await (await attach(name)).delete();
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
catch (cause) {
|
|
67
|
+
if (isNotFound(cause))
|
|
68
|
+
return false;
|
|
69
|
+
throw cause;
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
handles.delete(name);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
list: async () => (await (await loadSdk()).Machine.list(LOCAL)).map((machine) => machine.name),
|
|
76
|
+
readFile: async (name, file) => new Uint8Array(await (await attach(name)).readFile(file)),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Chromium in smol machines as a `BrowserProvider` for the web engine. */
|
|
2
|
+
import type { BrowserProvider, BrowserProviderScope } from '@e2e-dev/web';
|
|
3
|
+
/**
|
|
4
|
+
* An app that runs inside each browser machine, beside the browser, instead
|
|
5
|
+
* of on this computer. In `attempt` scope every attempt's branch then holds
|
|
6
|
+
* its own copy of the running app and everything it wrote to disk, a
|
|
7
|
+
* database included.
|
|
8
|
+
*/
|
|
9
|
+
export interface SmolApp {
|
|
10
|
+
/**
|
|
11
|
+
* Directory on this computer copied into the machine at `/app`, resolved
|
|
12
|
+
* against the directory the run starts in; `.` by default. `node_modules`,
|
|
13
|
+
* `.git`, and `.e2e` are left out, so dependencies install for the
|
|
14
|
+
* machine's Linux in `setup`.
|
|
15
|
+
*/
|
|
16
|
+
readonly source?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Shell script run as root in `/app` once per browser machine, before
|
|
19
|
+
* `start`: install a runtime and dependencies, create and seed a
|
|
20
|
+
* database. The image already includes Node.js and npm (`npm ci` is enough).
|
|
21
|
+
*/
|
|
22
|
+
readonly setup?: string | undefined;
|
|
23
|
+
/** Shell command that serves the app, run in `/app` in the background: `node server.mjs`, `npm start`. */
|
|
24
|
+
readonly start: string;
|
|
25
|
+
/** Port the app listens on inside the machine; the browser opens it as `http://localhost:<port>`. */
|
|
26
|
+
readonly port: number;
|
|
27
|
+
/** Environment for `setup` and `start`. */
|
|
28
|
+
readonly env?: Readonly<Record<string, string>> | undefined;
|
|
29
|
+
}
|
|
30
|
+
export interface SmolOptions {
|
|
31
|
+
/**
|
|
32
|
+
* `attempt` (default): every test attempt gets its own branch of a warm
|
|
33
|
+
* browser machine, a copy-on-write clone of the running Chromium typically
|
|
34
|
+
* ready in about a second, depending on port readiness, and deleted when
|
|
35
|
+
* the attempt ends. `worker`: one browser machine per worker slot for the
|
|
36
|
+
* run, no branching.
|
|
37
|
+
*/
|
|
38
|
+
readonly scope?: BrowserProviderScope | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Shell script run as root once per browser machine, before Chromium
|
|
41
|
+
* starts, in the Ubuntu Playwright image: `apt-get update && apt-get
|
|
42
|
+
* install -y fonts-noto-cjk` for more fonts, a CA certificate, a hosts entry.
|
|
43
|
+
*/
|
|
44
|
+
readonly setup?: string | undefined;
|
|
45
|
+
/** vCPUs per browser machine, 2 by default. */
|
|
46
|
+
readonly cpus?: number | undefined;
|
|
47
|
+
/** Memory per browser machine in MiB, 2048 by default. Unused memory goes back to the host. */
|
|
48
|
+
readonly memoryMb?: number | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Ports on this computer's loopback the browser reaches as its own
|
|
51
|
+
* `localhost`, so an app the run serves at `http://localhost:3000` opens
|
|
52
|
+
* unchanged inside the machine. This is not a network allowlist: the
|
|
53
|
+
* machine can also reach host loopback through `host.smolvm.internal`.
|
|
54
|
+
*/
|
|
55
|
+
readonly hostPorts?: readonly number[] | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Drives the warm browser once before any attempt branches it: sign in,
|
|
58
|
+
* seed storage, open the app. Every attempt then starts from that state.
|
|
59
|
+
* It gets the browser's DevTools endpoint and must disconnect, not close
|
|
60
|
+
* the browser, before it resolves. Runs once per worker slot. `attempt`
|
|
61
|
+
* scope only: in `worker` scope every attempt gets a new browser context,
|
|
62
|
+
* which would never see it.
|
|
63
|
+
*/
|
|
64
|
+
readonly prepare?: ((cdpEndpoint: string) => Promise<void>) | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Runs the app under test inside each browser machine instead of on this
|
|
67
|
+
* computer: its code never runs on the host, and every attempt gets its
|
|
68
|
+
* own copy of the running app and its data. Point the target's `app.url`
|
|
69
|
+
* at `http://localhost:<port>` and declare no `app.command`.
|
|
70
|
+
*/
|
|
71
|
+
readonly app?: SmolApp | undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Chromium in smol machines for `web({ browser: smol() })`. Each worker slot
|
|
75
|
+
* boots one browser machine on this computer's smol engine and, with
|
|
76
|
+
* `prepare`, drives it into the state tests start from. In `attempt` scope
|
|
77
|
+
* (the default) every attempt then gets a live copy-on-write branch of that
|
|
78
|
+
* running browser, cookies, storage, and open pages included, and the branch
|
|
79
|
+
* is deleted when the attempt ends. Machines are labeled with the run and
|
|
80
|
+
* target, and named after both, so `sweep` deletes what a dead worker left.
|
|
81
|
+
*/
|
|
82
|
+
export declare function smol(options?: SmolOptions): BrowserProvider;
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/** Chromium in smol machines as a `BrowserProvider` for the web engine. */
|
|
2
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
3
|
+
import { createServer } from 'node:net';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { ConfigurationError } from 'e2e/engine';
|
|
6
|
+
import { smolMachines } from './machines.js';
|
|
7
|
+
/**
|
|
8
|
+
* The browser's published port reaches its guest only when its source
|
|
9
|
+
* published that port too. A TCP relay listens here before the browser is
|
|
10
|
+
* ready and stays alive in every branch of the warm machine.
|
|
11
|
+
*/
|
|
12
|
+
const IMAGE = 'mcr.microsoft.com/playwright:v1.63.0-noble';
|
|
13
|
+
const GUEST_PORT = 80;
|
|
14
|
+
/** Chromium's new headless mode binds DevTools to loopback whatever it is told, hence the relay. */
|
|
15
|
+
const CHROMIUM_CDP_PORT = 9229;
|
|
16
|
+
/** Where each browser saves downloads on its machine's own disk, read back through the SDK. */
|
|
17
|
+
const DOWNLOADS_DIR = '/tmp/e2e-downloads';
|
|
18
|
+
const DEFAULT_CPUS = 2;
|
|
19
|
+
const DEFAULT_MEMORY_MB = 2048;
|
|
20
|
+
/** How long a new browser has to answer on its DevTools endpoint. */
|
|
21
|
+
const CDP_READY_MS = 60_000;
|
|
22
|
+
/** How often, and how far apart, `sweep` tries to delete one machine. */
|
|
23
|
+
const SWEEP_ATTEMPTS = 5;
|
|
24
|
+
const SWEEP_RETRY_MS = 2_000;
|
|
25
|
+
/** Where the app's source is mounted, and where the copy it runs from lives. */
|
|
26
|
+
const APP_SOURCE_MOUNT = '/e2e-source';
|
|
27
|
+
const APP_DIR = '/app';
|
|
28
|
+
const APP_LOG = '/tmp/e2e-app.log';
|
|
29
|
+
/** How long the app has to answer after `start`, in seconds. */
|
|
30
|
+
const APP_READY_SECONDS = 180;
|
|
31
|
+
/** A short stable digest, for machine names the engine's socket paths can hold. */
|
|
32
|
+
function digest(value) {
|
|
33
|
+
return createHash('sha256').update(value).digest('hex').slice(0, 8);
|
|
34
|
+
}
|
|
35
|
+
/** A port on this computer's loopback that nothing listens on right now. */
|
|
36
|
+
async function freePort() {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const server = createServer();
|
|
39
|
+
server.unref();
|
|
40
|
+
server.once('error', reject);
|
|
41
|
+
server.listen(0, '127.0.0.1', () => {
|
|
42
|
+
const address = server.address();
|
|
43
|
+
server.close(() => (typeof address === 'object' && address !== null ? resolve(address.port) : reject(new Error('no free port'))));
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** Resolves once the DevTools endpoint answers, rejecting after `CDP_READY_MS` or on `signal`. */
|
|
48
|
+
async function waitForCdp(endpoint, signal) {
|
|
49
|
+
const deadline = Date.now() + CDP_READY_MS;
|
|
50
|
+
let last = 'no answer';
|
|
51
|
+
while (Date.now() < deadline) {
|
|
52
|
+
signal.throwIfAborted();
|
|
53
|
+
try {
|
|
54
|
+
const response = await fetch(`${endpoint}/json/version`, { signal: AbortSignal.any([signal, AbortSignal.timeout(2_000)]) });
|
|
55
|
+
if (response.ok)
|
|
56
|
+
return;
|
|
57
|
+
last = `HTTP ${response.status}`;
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
signal.throwIfAborted();
|
|
61
|
+
last = cause instanceof Error ? cause.message : String(cause);
|
|
62
|
+
}
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`browser at ${endpoint} did not answer within ${CDP_READY_MS / 1000}s (${last})`);
|
|
66
|
+
}
|
|
67
|
+
/** Runs `command` in the background, past the end of the exec that starts it. */
|
|
68
|
+
function detached(command, log = '/dev/null') {
|
|
69
|
+
return `nohup setsid ${command} </dev/null >${log} 2>&1 &`;
|
|
70
|
+
}
|
|
71
|
+
/** Forward raw TCP so DevTools WebSocket upgrades keep their Host header and connection state. */
|
|
72
|
+
const TCP_RELAY = `const net = require('node:net');
|
|
73
|
+
const [listen, host, port, bind] = process.argv.slice(1);
|
|
74
|
+
net.createServer((client) => {
|
|
75
|
+
const peer = net.connect(Number(port), host);
|
|
76
|
+
client.setNoDelay(true);
|
|
77
|
+
peer.setNoDelay(true);
|
|
78
|
+
client.on('error', () => peer.destroy());
|
|
79
|
+
peer.on('error', () => client.destroy());
|
|
80
|
+
client.on('close', () => peer.destroy());
|
|
81
|
+
peer.on('close', () => client.destroy());
|
|
82
|
+
client.pipe(peer);
|
|
83
|
+
peer.pipe(client);
|
|
84
|
+
}).listen(Number(listen), bind);`;
|
|
85
|
+
/** Run a TCP relay in the guest after the shell command returns. */
|
|
86
|
+
function tcpRelay(listen, host, port, bind, log = '/dev/null') {
|
|
87
|
+
return detached(`node -e ${shellQuote(TCP_RELAY)} ${listen} ${shellQuote(host)} ${port} ${shellQuote(bind)}`, log);
|
|
88
|
+
}
|
|
89
|
+
/** Start the pinned Chromium build and relay its DevTools and requested host ports. */
|
|
90
|
+
function startScript(setup, hostPorts) {
|
|
91
|
+
return [
|
|
92
|
+
'set -e',
|
|
93
|
+
...(setup === undefined ? [] : [setup]),
|
|
94
|
+
`mkdir -p ${DOWNLOADS_DIR}`,
|
|
95
|
+
'chrome=$(find /ms-playwright -maxdepth 3 -type f -name chrome | head -n 1)',
|
|
96
|
+
'[ -x "$chrome" ] || { echo "Playwright Chromium is missing from the browser image" >&2; exit 1; }',
|
|
97
|
+
// The machine's loopback is its own; the host's is behind host.smolvm.internal.
|
|
98
|
+
...hostPorts.map((port) => tcpRelay(port, 'host.smolvm.internal', port, '127.0.0.1')),
|
|
99
|
+
tcpRelay(GUEST_PORT, '127.0.0.1', CHROMIUM_CDP_PORT, '0.0.0.0', '/tmp/e2e-cdp-relay.log'),
|
|
100
|
+
// A history restore from Chromium's back/forward cache emits no load event
|
|
101
|
+
// over CDP, so Playwright goBack({ waitUntil: 'load' }) otherwise hangs.
|
|
102
|
+
detached(`"$chrome" --headless=new --no-sandbox --disable-gpu --disable-dev-shm-usage --no-first-run --no-default-browser-check --disable-features=BackForwardCache --remote-debugging-port=${CHROMIUM_CDP_PORT} --user-data-dir=/tmp/e2e-chromium about:blank`, '/tmp/e2e-chromium.log'),
|
|
103
|
+
`ready=; for i in $(seq 300); do if curl -fsS --max-time 1 http://127.0.0.1:${GUEST_PORT}/json/version >/dev/null 2>&1; then ready=1; break; fi; sleep 0.1; done`,
|
|
104
|
+
'[ -n "$ready" ] || { echo "chromium did not start:" >&2; tail -5 /tmp/e2e-chromium.log >&2; tail -5 /tmp/e2e-cdp-relay.log >&2; exit 1; }',
|
|
105
|
+
].join('\n');
|
|
106
|
+
}
|
|
107
|
+
/** A shell variable name, the only form `app.env` names are exported under. */
|
|
108
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
109
|
+
/** Refuses a port the machine cannot give the browser's `localhost`: out of range, or one the browser itself uses. */
|
|
110
|
+
function checkPort(what, port) {
|
|
111
|
+
if (typeof port !== 'number' || !Number.isInteger(port) || port < 1 || port > 65_535 || port === GUEST_PORT || port === CHROMIUM_CDP_PORT) {
|
|
112
|
+
throw new ConfigurationError('INVALID_CONFIG', `smol: ${what} must be a port from 1 to 65535 other than ${GUEST_PORT} and ${CHROMIUM_CDP_PORT}, which the browser uses; got ${JSON.stringify(port)}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** `value` as one single-quoted shell word. */
|
|
116
|
+
function shellQuote(value) {
|
|
117
|
+
return `'${value.replaceAll("'", String.raw `'\''`)}'`;
|
|
118
|
+
}
|
|
119
|
+
/** The script that copies the app in, runs its `setup`, starts it, and waits until it answers. */
|
|
120
|
+
function appScript(app) {
|
|
121
|
+
const exports = Object.entries(app.env ?? {}).map(([name, value]) => `export ${name}=${shellQuote(value)}`);
|
|
122
|
+
return [
|
|
123
|
+
'set -e',
|
|
124
|
+
...exports,
|
|
125
|
+
`mkdir -p ${APP_DIR}`,
|
|
126
|
+
`tar -C ${APP_SOURCE_MOUNT} --exclude=./node_modules --exclude=./.git --exclude=./.e2e -cf - . | tar -C ${APP_DIR} -xf -`,
|
|
127
|
+
`cd ${APP_DIR}`,
|
|
128
|
+
...(app.setup === undefined ? [] : [app.setup]),
|
|
129
|
+
detached(`sh -c ${shellQuote(app.start)}`, APP_LOG),
|
|
130
|
+
// Any HTTP status means the app answered; connection failures report 000.
|
|
131
|
+
`ready=; for i in $(seq ${APP_READY_SECONDS * 4}); do code=$(curl -s -o /dev/null --max-time 1 -w '%{http_code}' http://127.0.0.1:${app.port}/) || true; if [ -n "$code" ] && [ "$code" != 000 ]; then ready=1; break; fi; sleep 0.25; done`,
|
|
132
|
+
`[ -n "$ready" ] || { echo "the app did not answer on port ${app.port} within ${APP_READY_SECONDS}s:" >&2; tail -20 ${APP_LOG} >&2; exit 1; }`,
|
|
133
|
+
].join('\n');
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Chromium in smol machines for `web({ browser: smol() })`. Each worker slot
|
|
137
|
+
* boots one browser machine on this computer's smol engine and, with
|
|
138
|
+
* `prepare`, drives it into the state tests start from. In `attempt` scope
|
|
139
|
+
* (the default) every attempt then gets a live copy-on-write branch of that
|
|
140
|
+
* running browser, cookies, storage, and open pages included, and the branch
|
|
141
|
+
* is deleted when the attempt ends. Machines are labeled with the run and
|
|
142
|
+
* target, and named after both, so `sweep` deletes what a dead worker left.
|
|
143
|
+
*/
|
|
144
|
+
export function smol(options = {}) {
|
|
145
|
+
const { scope = 'attempt', setup, cpus = DEFAULT_CPUS, memoryMb = DEFAULT_MEMORY_MB, hostPorts = [], prepare, app } = options;
|
|
146
|
+
for (const port of hostPorts)
|
|
147
|
+
checkPort(`hostPorts entry`, port);
|
|
148
|
+
if (app !== undefined) {
|
|
149
|
+
checkPort('app.port', app.port);
|
|
150
|
+
for (const name of Object.keys(app.env ?? {})) {
|
|
151
|
+
if (!ENV_NAME.test(name)) {
|
|
152
|
+
throw new ConfigurationError('INVALID_CONFIG', `smol: app.env name ${JSON.stringify(name)} is not a shell variable name (letters, digits, and _, not starting with a digit)`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (hostPorts.includes(app.port)) {
|
|
156
|
+
throw new ConfigurationError('INVALID_CONFIG', `smol: port ${app.port} is both app.port and in hostPorts; the browser's localhost:${app.port} can be only one of them`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const appSource = app === undefined ? undefined : path.resolve(app.source ?? '.');
|
|
160
|
+
if (prepare !== undefined && scope === 'worker') {
|
|
161
|
+
// A worker-scope browser gives every attempt a new context, which never sees what prepare left in the default one.
|
|
162
|
+
throw new ConfigurationError('INVALID_CONFIG', 'smol: prepare needs scope "attempt"; with scope "worker" every attempt gets a new browser context, so use sessions there');
|
|
163
|
+
}
|
|
164
|
+
const machines = smolMachines();
|
|
165
|
+
const prefixFor = (runId, targetName) => `e2e-${digest(`${runId}\n${targetName}`)}`;
|
|
166
|
+
/** Boots one browser machine, starts Chromium in it, and runs `prepare` on it. */
|
|
167
|
+
const boot = async (request, name) => {
|
|
168
|
+
request.signal.throwIfAborted();
|
|
169
|
+
const hostPort = await freePort();
|
|
170
|
+
request.signal.throwIfAborted();
|
|
171
|
+
const machine = await machines.create({
|
|
172
|
+
name,
|
|
173
|
+
image: IMAGE,
|
|
174
|
+
cpus,
|
|
175
|
+
memoryMb,
|
|
176
|
+
port: { host: hostPort, guest: GUEST_PORT },
|
|
177
|
+
labels: { e2e_run: request.runId, e2e_target: request.targetName },
|
|
178
|
+
// A worker-scope browser is made in the runner and read from a worker; an attempt-scope one lives and dies with its worker, so the engine deletes it when the worker exits however it exits.
|
|
179
|
+
persistent: scope === 'worker',
|
|
180
|
+
...(appSource === undefined ? {} : { mount: { source: appSource, target: APP_SOURCE_MOUNT } }),
|
|
181
|
+
});
|
|
182
|
+
const endpoint = `http://127.0.0.1:${hostPort}`;
|
|
183
|
+
try {
|
|
184
|
+
request.signal.throwIfAborted();
|
|
185
|
+
await machine.shell(startScript(setup, hostPorts), request.signal);
|
|
186
|
+
if (app !== undefined)
|
|
187
|
+
await machine.shell(appScript(app), request.signal);
|
|
188
|
+
await waitForCdp(endpoint, request.signal);
|
|
189
|
+
if (prepare !== undefined)
|
|
190
|
+
await prepare(endpoint);
|
|
191
|
+
return { machine, endpoint };
|
|
192
|
+
}
|
|
193
|
+
catch (cause) {
|
|
194
|
+
await machines.delete(name).catch(() => false);
|
|
195
|
+
throw cause;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
/** Each slot's warm browser in this process, booted by the first attempt that needs it. */
|
|
199
|
+
const warm = new Map();
|
|
200
|
+
const warmFor = (request) => {
|
|
201
|
+
const slot = `${prefixFor(request.runId, request.targetName)}-w${request.slot}`;
|
|
202
|
+
let browser = warm.get(slot);
|
|
203
|
+
if (browser === undefined) {
|
|
204
|
+
// Use a new name even when a failed boot left an undeletable machine.
|
|
205
|
+
browser = boot(request, `${slot}-${randomBytes(6).toString('hex')}`);
|
|
206
|
+
warm.set(slot, browser);
|
|
207
|
+
// A timed-out attempt must not lend its still-running boot to a replacement worker.
|
|
208
|
+
const discard = () => { if (warm.get(slot) === browser)
|
|
209
|
+
warm.delete(slot); };
|
|
210
|
+
request.signal.addEventListener('abort', discard, { once: true });
|
|
211
|
+
if (request.signal.aborted)
|
|
212
|
+
discard();
|
|
213
|
+
void browser.then(() => request.signal.removeEventListener('abort', discard), () => { request.signal.removeEventListener('abort', discard); discard(); });
|
|
214
|
+
}
|
|
215
|
+
return browser;
|
|
216
|
+
};
|
|
217
|
+
return {
|
|
218
|
+
name: 'smol',
|
|
219
|
+
scope,
|
|
220
|
+
async acquire(request) {
|
|
221
|
+
if (scope === 'worker') {
|
|
222
|
+
// A worker whose browser dropped leases a replacement for its slot while the dropped one is still held,
|
|
223
|
+
// in this process or the runner's, so every lease gets a name of its own.
|
|
224
|
+
const name = `${prefixFor(request.runId, request.targetName)}-s${request.slot}-${randomBytes(3).toString('hex')}`;
|
|
225
|
+
const { endpoint } = await boot(request, name);
|
|
226
|
+
request.log(`browser machine ${name}`);
|
|
227
|
+
return { id: name, cdpEndpoint: endpoint };
|
|
228
|
+
}
|
|
229
|
+
const source = await warmFor(request);
|
|
230
|
+
const name = `${source.machine.name}-${digest(request.attemptId ?? String(Date.now()))}`;
|
|
231
|
+
const hostPort = await freePort();
|
|
232
|
+
const started = Date.now();
|
|
233
|
+
const endpoint = `http://127.0.0.1:${hostPort}`;
|
|
234
|
+
try {
|
|
235
|
+
// A branch that fails partway can still leave a machine under its name.
|
|
236
|
+
await source.machine.branch(name, { host: hostPort, guest: GUEST_PORT });
|
|
237
|
+
await waitForCdp(endpoint, request.signal);
|
|
238
|
+
}
|
|
239
|
+
catch (cause) {
|
|
240
|
+
await machines.delete(name).catch(() => false);
|
|
241
|
+
throw cause;
|
|
242
|
+
}
|
|
243
|
+
request.log(`browser ${name}, branched from ${source.machine.name} in ${Date.now() - started} ms`);
|
|
244
|
+
return { id: name, cdpEndpoint: endpoint };
|
|
245
|
+
},
|
|
246
|
+
async release(lease) {
|
|
247
|
+
await machines.delete(lease.id);
|
|
248
|
+
},
|
|
249
|
+
async sweep(context) {
|
|
250
|
+
const prefix = prefixFor(context.runId, context.targetName);
|
|
251
|
+
// Longest name first, so a branch goes before the warm browser it came from.
|
|
252
|
+
const open = (await machines.list()).filter((name) => name.startsWith(`${prefix}-`)).toSorted((a, b) => b.length - a.length);
|
|
253
|
+
const deleted = [];
|
|
254
|
+
const failed = [];
|
|
255
|
+
for (const name of open) {
|
|
256
|
+
// A worker that just exited may still have the engine stopping its machines; a delete then fails until the stop settles.
|
|
257
|
+
for (let attempt = 1;; attempt += 1) {
|
|
258
|
+
try {
|
|
259
|
+
if (await machines.delete(name))
|
|
260
|
+
deleted.push(name);
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
catch (cause) {
|
|
264
|
+
if (attempt < SWEEP_ATTEMPTS && !context.signal.aborted) {
|
|
265
|
+
await new Promise((resolve) => setTimeout(resolve, SWEEP_RETRY_MS));
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
failed.push(`${name} (${cause instanceof Error ? cause.message : String(cause)})`);
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (failed.length > 0)
|
|
274
|
+
throw new Error(`deleted ${deleted.length === 0 ? 'none' : deleted.join(', ')}; could not delete ${failed.join(', ')}`);
|
|
275
|
+
return deleted;
|
|
276
|
+
},
|
|
277
|
+
downloads: {
|
|
278
|
+
dir: DOWNLOADS_DIR,
|
|
279
|
+
read: async (lease, file) => machines.readFile(lease.id, file),
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e2e-dev/smol",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "smol machines browsers for e2e: every test attempt branches a warm Chromium microVM",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"e2e",
|
|
7
|
+
"end-to-end",
|
|
8
|
+
"testing",
|
|
9
|
+
"ai-testing",
|
|
10
|
+
"smol",
|
|
11
|
+
"smolmachines",
|
|
12
|
+
"microvm",
|
|
13
|
+
"browser-automation",
|
|
14
|
+
"chromium"
|
|
15
|
+
],
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"contributors": [
|
|
18
|
+
"Oskar Kwaśniewski <oskar@tester.army>",
|
|
19
|
+
"Szymon Rybczak <szymon@tester.army>"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/tester-army/e2e.git",
|
|
24
|
+
"directory": "packages/smol"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/tester-army/e2e/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/tester-army/e2e/tree/main/packages/smol#readme",
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"registry": "https://registry.npmjs.org/",
|
|
32
|
+
"access": "public",
|
|
33
|
+
"tag": "latest"
|
|
34
|
+
},
|
|
35
|
+
"type": "module",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"NOTICE"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc --project tsconfig.build.json && node ../../scripts/stamp-dist.ts",
|
|
48
|
+
"prepublishOnly": "node ../../scripts/check-dist.ts",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run",
|
|
51
|
+
"test:watch": "vitest",
|
|
52
|
+
"test:unit": "vitest run tests/unit"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@e2e-dev/web": ">=0.11.0 <1",
|
|
56
|
+
"e2e": ">=0.15.0 <1",
|
|
57
|
+
"smolmachines": ">=1.25.1 <2"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@e2e-dev/web": "workspace:*",
|
|
61
|
+
"@types/node": "26.6.2",
|
|
62
|
+
"e2e": "workspace:*",
|
|
63
|
+
"smolmachines": "1.25.1",
|
|
64
|
+
"typescript": "7.0.2",
|
|
65
|
+
"vitest": "5.0.1"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": "^22.22.3 || >=24.8.0"
|
|
69
|
+
}
|
|
70
|
+
}
|