@sponson/sim 0.0.0-stage → 0.4.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/README.md +4 -2
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +21 -0
- package/dist/bin.js.map +1 -0
- package/dist/chaos.d.ts +70 -0
- package/dist/chaos.js +78 -0
- package/dist/chaos.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +99 -0
- package/dist/provider.js +62 -0
- package/dist/provider.js.map +1 -0
- package/dist/routes/clerk.d.ts +33 -0
- package/dist/routes/clerk.js +85 -0
- package/dist/routes/clerk.js.map +1 -0
- package/dist/routes/neon.d.ts +73 -0
- package/dist/routes/neon.js +184 -0
- package/dist/routes/neon.js.map +1 -0
- package/dist/routes/vercel.d.ts +114 -0
- package/dist/routes/vercel.js +370 -0
- package/dist/routes/vercel.js.map +1 -0
- package/dist/server.d.ts +4 -0
- package/dist/server.js +130 -0
- package/dist/server.js.map +1 -0
- package/dist/state.d.ts +90 -0
- package/dist/state.js +109 -0
- package/dist/state.js.map +1 -0
- package/package.json +47 -3
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/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sponson/sim
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A local fake cloud for testing [Sponson](https://github.com/MishaBear94/Sponson#readme): subsets of the Vercel, Neon and Clerk APIs with chaos injection, started in-process with `startSim()` or from the command line with `sponson-sim`.
|
|
4
|
+
|
|
5
|
+
Documentation: <https://sponson.mintlify.site>. Source and issues: [GitHub](https://github.com/MishaBear94/Sponson).
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { simEnv, startSim } from "./index.js";
|
|
3
|
+
const args = process.argv.slice(2);
|
|
4
|
+
const flag = args.indexOf("--port");
|
|
5
|
+
const port = Number(flag >= 0 ? args[flag + 1] : (process.env.SPONSON_SIM_PORT ?? 4777));
|
|
6
|
+
if (!Number.isInteger(port) || port < 0) {
|
|
7
|
+
console.error(`invalid port: ${flag >= 0 ? args[flag + 1] : process.env.SPONSON_SIM_PORT}`);
|
|
8
|
+
process.exit(2);
|
|
9
|
+
}
|
|
10
|
+
const sim = await startSim({ port });
|
|
11
|
+
console.log(`sponson-sim listening on ${sim.url}`);
|
|
12
|
+
// Everything the adapters need to talk to the sim, base URLs and placeholder tokens alike, ready to paste into a shell.
|
|
13
|
+
console.log("# paste into another shell to point sponson at the sim:");
|
|
14
|
+
for (const [k, v] of Object.entries(simEnv(sim)))
|
|
15
|
+
console.log(`export ${k}=${v}`);
|
|
16
|
+
const stop = () => {
|
|
17
|
+
void sim.close().finally(() => process.exit(0));
|
|
18
|
+
};
|
|
19
|
+
process.on("SIGINT", stop);
|
|
20
|
+
process.on("SIGTERM", stop);
|
|
21
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAA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AO,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC"}
|
package/dist/chaos.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chaos configuration: what the fake cloud does wrong on purpose, and which requests it does it to. Set it with
|
|
3
|
+
* `POST /_chaos` or a seed's `chaos:`; counters (`*_next`) decrement per matching request.
|
|
4
|
+
*/
|
|
5
|
+
export interface ChaosConfig {
|
|
6
|
+
latency_ms: number;
|
|
7
|
+
/** Remaining requests (matching `fail_on`) that will fail with `status`. */
|
|
8
|
+
fail_next: number;
|
|
9
|
+
status: number;
|
|
10
|
+
/** Sent as `Retry-After` (seconds) with chaos 429s. */
|
|
11
|
+
retry_after?: number;
|
|
12
|
+
/** Remaining requests (matching `fail_on`) that are performed, then answered by destroying the socket. */
|
|
13
|
+
drop_response_next: number;
|
|
14
|
+
/** Remaining requests (matching `fail_on`) that are accepted and never answered. */
|
|
15
|
+
hang_next: number;
|
|
16
|
+
/**
|
|
17
|
+
* Remaining requests (matching `fail_on`) that are held, neither performed nor answered, until released
|
|
18
|
+
* (`POST /_release` or `state.release()`); then they are performed and answered as usual. Lets a test pin
|
|
19
|
+
* one client inside a request while another runs, instead of hoping timing makes them overlap.
|
|
20
|
+
*/
|
|
21
|
+
hold_next: number;
|
|
22
|
+
/**
|
|
23
|
+
* Which requests the counters above apply to: `"METHOD /path-glob"` or a list of them.
|
|
24
|
+
* Absent means every write. Reads are only affected by a rule whose method is GET.
|
|
25
|
+
*/
|
|
26
|
+
fail_on?: string | string[];
|
|
27
|
+
/** Deployment mode: ok | never | fail | stale | double | cancel | delay:<seconds>. */
|
|
28
|
+
deploy: string;
|
|
29
|
+
/** QUEUED → BUILDING → READY takes this long for deployments created through the API (and `ok` auto-deploys). */
|
|
30
|
+
deploy_ms: number;
|
|
31
|
+
/** After a Neon branch create, writes to that branch and further creates in the project answer 423 for this long. */
|
|
32
|
+
neon_op_ms: number;
|
|
33
|
+
/** When > 0, Neon branches, Vercel envs and Clerk redirect URLs are listed in pages of this size. */
|
|
34
|
+
page_size: number;
|
|
35
|
+
/** Keys a Vercel bulk upsert reports under `failed` (and does not write). */
|
|
36
|
+
env_upsert_fail: string[];
|
|
37
|
+
}
|
|
38
|
+
/** The body of `POST /_chaos`: any chaos settings to merge, plus drift to apply immediately. */
|
|
39
|
+
export interface ChaosRequest extends Partial<ChaosConfig> {
|
|
40
|
+
/** Applied immediately to state, not stored. */
|
|
41
|
+
drift?: Record<string, string>;
|
|
42
|
+
}
|
|
43
|
+
/** What chaos does to one request. */
|
|
44
|
+
export type ChaosAction = {
|
|
45
|
+
kind: "hold";
|
|
46
|
+
} | {
|
|
47
|
+
kind: "hang";
|
|
48
|
+
} | {
|
|
49
|
+
kind: "drop";
|
|
50
|
+
} | {
|
|
51
|
+
kind: "fail";
|
|
52
|
+
status: number;
|
|
53
|
+
headers: Record<string, string>;
|
|
54
|
+
};
|
|
55
|
+
/** No chaos: every request is answered normally and immediately. */
|
|
56
|
+
export declare const DEFAULT_CHAOS: ChaosConfig;
|
|
57
|
+
/**
|
|
58
|
+
* Every key `POST /_chaos` accepts besides `drift`; anything else is rejected so a misspelt key cannot silently
|
|
59
|
+
* do nothing.
|
|
60
|
+
*/
|
|
61
|
+
export declare const CHAOS_KEYS: Set<string>;
|
|
62
|
+
/** A fresh default config (no shared arrays). */
|
|
63
|
+
export declare function defaultChaos(): ChaosConfig;
|
|
64
|
+
/**
|
|
65
|
+
* Decide what chaos does to this request, consuming one count of `c` when it does something.
|
|
66
|
+
* Writes are eligible when `fail_on` is absent or matches; reads only when a GET rule matches.
|
|
67
|
+
*/
|
|
68
|
+
export declare function chaosFor(c: ChaosConfig, method: string, path: string): ChaosAction | null;
|
|
69
|
+
/** `"DELETE /neon/*"`: method must match exactly (or be `*`), `*` in the path matches anything. */
|
|
70
|
+
export declare function matchesRule(rule: string, method: string, path: string): boolean;
|
package/dist/chaos.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** No chaos: every request is answered normally and immediately. */
|
|
2
|
+
export const DEFAULT_CHAOS = {
|
|
3
|
+
latency_ms: 0,
|
|
4
|
+
fail_next: 0,
|
|
5
|
+
status: 503,
|
|
6
|
+
drop_response_next: 0,
|
|
7
|
+
hang_next: 0,
|
|
8
|
+
hold_next: 0,
|
|
9
|
+
deploy: "ok",
|
|
10
|
+
deploy_ms: 0,
|
|
11
|
+
neon_op_ms: 0,
|
|
12
|
+
page_size: 0,
|
|
13
|
+
env_upsert_fail: [],
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Every key `POST /_chaos` accepts besides `drift`; anything else is rejected so a misspelt key cannot silently
|
|
17
|
+
* do nothing.
|
|
18
|
+
*/
|
|
19
|
+
export const CHAOS_KEYS = new Set([...Object.keys(DEFAULT_CHAOS), "fail_on", "retry_after"]);
|
|
20
|
+
/** A fresh default config (no shared arrays). */
|
|
21
|
+
export function defaultChaos() {
|
|
22
|
+
return { ...DEFAULT_CHAOS, env_upsert_fail: [] };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Decide what chaos does to this request, consuming one count of `c` when it does something.
|
|
26
|
+
* Writes are eligible when `fail_on` is absent or matches; reads only when a GET rule matches.
|
|
27
|
+
*/
|
|
28
|
+
export function chaosFor(c, method, path) {
|
|
29
|
+
if (c.hold_next <= 0 && c.hang_next <= 0 && c.drop_response_next <= 0 && c.fail_next <= 0)
|
|
30
|
+
return null;
|
|
31
|
+
return affected(c, method, path) ? consume(c) : null;
|
|
32
|
+
}
|
|
33
|
+
/** Whether `fail_on` lets chaos hit this request: writes by default, reads only when a GET rule names them. */
|
|
34
|
+
function affected(c, method, path) {
|
|
35
|
+
const rules = c.fail_on === undefined ? [] : Array.isArray(c.fail_on) ? c.fail_on : [c.fail_on];
|
|
36
|
+
if (method === "GET" || method === "HEAD")
|
|
37
|
+
return rules.some((r) => ruleMethod(r) === "GET" && matchesRule(r, "GET", path));
|
|
38
|
+
return rules.length === 0 || rules.some((r) => matchesRule(r, method, path));
|
|
39
|
+
}
|
|
40
|
+
/** Use up one pending chaos action, in the order hold, hang, drop, fail. */
|
|
41
|
+
function consume(c) {
|
|
42
|
+
if (c.hold_next > 0) {
|
|
43
|
+
c.hold_next -= 1;
|
|
44
|
+
return { kind: "hold" };
|
|
45
|
+
}
|
|
46
|
+
if (c.hang_next > 0) {
|
|
47
|
+
c.hang_next -= 1;
|
|
48
|
+
return { kind: "hang" };
|
|
49
|
+
}
|
|
50
|
+
if (c.drop_response_next > 0) {
|
|
51
|
+
c.drop_response_next -= 1;
|
|
52
|
+
return { kind: "drop" };
|
|
53
|
+
}
|
|
54
|
+
if (c.fail_next > 0) {
|
|
55
|
+
c.fail_next -= 1;
|
|
56
|
+
const headers = c.status === 429 && c.retry_after !== undefined ? { "retry-after": String(c.retry_after) } : {};
|
|
57
|
+
return { kind: "fail", status: c.status, headers };
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
/** `"DELETE /neon/*"`: method must match exactly (or be `*`), `*` in the path matches anything. */
|
|
62
|
+
export function matchesRule(rule, method, path) {
|
|
63
|
+
const space = rule.indexOf(" ");
|
|
64
|
+
const m = ruleMethod(rule);
|
|
65
|
+
const ruleGlob = space < 0 ? "*" : rule.slice(space + 1).trim();
|
|
66
|
+
if (m !== "*" && m !== method.toUpperCase())
|
|
67
|
+
return false;
|
|
68
|
+
const re = new RegExp("^" + ruleGlob.split("*").map(escapeRe).join(".*") + "$");
|
|
69
|
+
return re.test(path);
|
|
70
|
+
}
|
|
71
|
+
function escapeRe(s) {
|
|
72
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
73
|
+
}
|
|
74
|
+
function ruleMethod(rule) {
|
|
75
|
+
const space = rule.indexOf(" ");
|
|
76
|
+
return (space < 0 ? rule : rule.slice(0, space)).toUpperCase();
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=chaos.js.map
|
|
@@ -0,0 +1 @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,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;AACjE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { SimState, type SimSeed } from "./state.js";
|
|
2
|
+
export * from "./chaos.js";
|
|
3
|
+
export * from "./provider.js";
|
|
4
|
+
export * from "./state.js";
|
|
5
|
+
export * from "./routes/vercel.js";
|
|
6
|
+
export * from "./routes/neon.js";
|
|
7
|
+
export * from "./routes/clerk.js";
|
|
8
|
+
export { createSimServer } from "./server.js";
|
|
9
|
+
/**
|
|
10
|
+
* A running sim: its base URL (`simEnv` turns it into adapter environment), its live state for seeding and
|
|
11
|
+
* assertions, and `close`.
|
|
12
|
+
*/
|
|
13
|
+
export interface SimHandle {
|
|
14
|
+
url: string;
|
|
15
|
+
port: number;
|
|
16
|
+
state: SimState;
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** Start the fake cloud on 127.0.0.1. Port 0 picks a free one, which is what tests want. */
|
|
20
|
+
export declare function startSim(opts?: {
|
|
21
|
+
port?: number;
|
|
22
|
+
seed?: Partial<SimSeed>;
|
|
23
|
+
}): Promise<SimHandle>;
|
|
24
|
+
/** A placeholder credential per provider (`VERCEL_TOKEN: "tok_vercel"`, …). The sim accepts any non-empty token. */
|
|
25
|
+
export declare const SIM_TOKENS: Readonly<Record<string, string>>;
|
|
26
|
+
/**
|
|
27
|
+
* The env that points every provider's adapter at a sim (or at a proxy in front of one): each `*_API_URL`
|
|
28
|
+
* and, unless `tokens: false`, the placeholder credentials of SIM_TOKENS.
|
|
29
|
+
*/
|
|
30
|
+
export declare function simEnv(sim: {
|
|
31
|
+
url: string;
|
|
32
|
+
} | string, opts?: {
|
|
33
|
+
tokens?: boolean;
|
|
34
|
+
}): Record<string, string>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createSimServer } from "./server.js";
|
|
2
|
+
import { providerEntries, SimState } from "./state.js";
|
|
3
|
+
export * from "./chaos.js";
|
|
4
|
+
export * from "./provider.js";
|
|
5
|
+
export * from "./state.js";
|
|
6
|
+
export * from "./routes/vercel.js";
|
|
7
|
+
export * from "./routes/neon.js";
|
|
8
|
+
export * from "./routes/clerk.js";
|
|
9
|
+
export { createSimServer } from "./server.js";
|
|
10
|
+
/** Start the fake cloud on 127.0.0.1. Port 0 picks a free one, which is what tests want. */
|
|
11
|
+
export async function startSim(opts = {}) {
|
|
12
|
+
const state = new SimState(opts.seed);
|
|
13
|
+
const server = createSimServer(state);
|
|
14
|
+
await new Promise((resolve, reject) => {
|
|
15
|
+
server.once("error", reject);
|
|
16
|
+
server.listen(opts.port ?? 0, "127.0.0.1", () => resolve());
|
|
17
|
+
});
|
|
18
|
+
const port = server.address().port;
|
|
19
|
+
return {
|
|
20
|
+
url: `http://127.0.0.1:${port}`,
|
|
21
|
+
port,
|
|
22
|
+
state,
|
|
23
|
+
close: () => new Promise((resolve, reject) => {
|
|
24
|
+
server.closeAllConnections();
|
|
25
|
+
server.close((e) => (e ? reject(e) : resolve()));
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** A placeholder credential per provider (`VERCEL_TOKEN: "tok_vercel"`, …). The sim accepts any non-empty token. */
|
|
30
|
+
export const SIM_TOKENS = Object.fromEntries(providerEntries().map(([, p]) => [p.env.token, p.env.testToken]));
|
|
31
|
+
/**
|
|
32
|
+
* The env that points every provider's adapter at a sim (or at a proxy in front of one): each `*_API_URL`
|
|
33
|
+
* and, unless `tokens: false`, the placeholder credentials of SIM_TOKENS.
|
|
34
|
+
*/
|
|
35
|
+
export function simEnv(sim, opts = {}) {
|
|
36
|
+
const base = typeof sim === "string" ? sim : sim.url;
|
|
37
|
+
const env = opts.tokens === false ? {} : { ...SIM_TOKENS };
|
|
38
|
+
for (const [name, p] of providerEntries())
|
|
39
|
+
env[p.env.url] = `${base}/${name}`;
|
|
40
|
+
return env;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAgB,MAAM,YAAY,CAAC;AAErE,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAa9C,4FAA4F;AAC5F,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,OAAmD,EAAE;IAClF,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtC,MAAM,MAAM,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;IACtC,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;IACH,MAAM,IAAI,GAAI,MAAM,CAAC,OAAO,EAAkB,CAAC,IAAI,CAAC;IACpD,OAAO;QACL,GAAG,EAAE,oBAAoB,IAAI,EAAE;QAC/B,IAAI;QACJ,KAAK;QACL,KAAK,EAAE,GAAG,EAAE,CACV,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpC,MAAM,CAAC,mBAAmB,EAAE,CAAC;YAC7B,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QACnD,CAAC,CAAC;KACL,CAAC;AACJ,CAAC;AAED,oHAAoH;AACpH,MAAM,CAAC,MAAM,UAAU,GAAqC,MAAM,CAAC,WAAW,CAAC,eAAe,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;AAEjJ;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,GAA6B,EAAE,OAA6B,EAAE;IACnF,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC;IACrD,MAAM,GAAG,GAA2B,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,UAAU,EAAE,CAAC;IACnF,KAAK,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,eAAe,EAAE;QAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;IAC9E,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract between the sim core (chaos, write log, ids, control endpoints) and one simulated provider.
|
|
3
|
+
* A provider lives in one `routes/<name>.ts` file: its state, its seed, its drift keys and its HTTP routes.
|
|
4
|
+
* Adding a provider = writing that file and registering it in `PROVIDERS` (state.ts).
|
|
5
|
+
*/
|
|
6
|
+
import type { ChaosConfig } from "./chaos.js";
|
|
7
|
+
/** Who made a record: the sim itself (seed, drift, a "human in the console") or a client through the API. */
|
|
8
|
+
export type CreatedBy = "sim" | "api";
|
|
9
|
+
/** What a provider needs from the core. */
|
|
10
|
+
export interface SimCore {
|
|
11
|
+
chaos: ChaosConfig;
|
|
12
|
+
/** Ids are unique across providers and deterministic after a reset. */
|
|
13
|
+
nextId(prefix: string): string;
|
|
14
|
+
}
|
|
15
|
+
/** A route's answer: status, JSON body and extra headers. Routes return one instead of writing to the socket. */
|
|
16
|
+
export declare class Reply {
|
|
17
|
+
status: number;
|
|
18
|
+
body: unknown;
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
constructor(status: number, body: unknown, headers?: Record<string, string>);
|
|
21
|
+
}
|
|
22
|
+
/** One request as a provider's routes see it, after auth and chaos. */
|
|
23
|
+
export interface RouteRequest {
|
|
24
|
+
method: string;
|
|
25
|
+
url: URL;
|
|
26
|
+
/** The path with the provider prefix stripped, e.g. `/v10/projects/prj_demo/env`. */
|
|
27
|
+
path: string;
|
|
28
|
+
body: unknown;
|
|
29
|
+
}
|
|
30
|
+
/** One `<provider>.<rest>` drift key, as `ProviderSim.drift` receives it: a change "a human made in the console". */
|
|
31
|
+
export interface DriftRequest {
|
|
32
|
+
/** The whole drift key, for error messages. */
|
|
33
|
+
key: string;
|
|
34
|
+
/** The key after `<provider>.` or `<provider>:<project>.`, e.g. `env.preview.API_URL`. */
|
|
35
|
+
rest: string;
|
|
36
|
+
value: string;
|
|
37
|
+
/** `<project>` from a `<provider>:<project>.` key: only that project is affected. */
|
|
38
|
+
only?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* One simulated provider: its state, seed, drift keys and HTTP routes. Implement it in `routes/<name>.ts` and
|
|
42
|
+
* register it in `PROVIDERS` to simulate a new provider.
|
|
43
|
+
*
|
|
44
|
+
* Members are methods (not function-valued properties) so a `ProviderSim<VercelState, …>` is usable where the
|
|
45
|
+
* registry only knows `ProviderSim<unknown, unknown>`.
|
|
46
|
+
*/
|
|
47
|
+
export interface ProviderSim<S, Seed> {
|
|
48
|
+
/** Env var names the adapter reads its credential and base URL from, and the token the sim hands out. */
|
|
49
|
+
env: {
|
|
50
|
+
token: string;
|
|
51
|
+
url: string;
|
|
52
|
+
testToken: string;
|
|
53
|
+
};
|
|
54
|
+
/** Seed used when a reset does not name this provider. */
|
|
55
|
+
defaultSeed: Seed;
|
|
56
|
+
/** Fresh state from a seed (`undefined`: empty). Called on every reset, in registry order. */
|
|
57
|
+
reset(core: SimCore, seed: Seed | undefined): S;
|
|
58
|
+
/** Apply one drift key to the state; false when the key is not one this provider knows. */
|
|
59
|
+
drift(core: SimCore, state: S, d: DriftRequest): boolean;
|
|
60
|
+
/** Answer one authenticated, chaos-checked request under `/<name>/`. */
|
|
61
|
+
routes(core: SimCore, state: S, req: RouteRequest): Reply;
|
|
62
|
+
}
|
|
63
|
+
/** One page of `list` under chaos `page_size`, starting at the opaque numeric cursor. */
|
|
64
|
+
export declare function page<T>(core: SimCore, list: T[], cursor: string | null, limit?: number): {
|
|
65
|
+
items: T[];
|
|
66
|
+
next: number | null;
|
|
67
|
+
};
|
|
68
|
+
/** The `:name` segments of a route path, e.g. `"project" | "id"` for `/v9/projects/:project/env/:id`. */
|
|
69
|
+
type ParamNames<P extends string> = P extends `${string}:${infer N}/${infer Rest}` ? N | ParamNames<`/${Rest}`> : P extends `${string}:${infer N}` ? N : never;
|
|
70
|
+
/** The decoded `:name` segments of a matched path, typed from the route's path literal. */
|
|
71
|
+
export type PathParams<P extends string> = {
|
|
72
|
+
[K in ParamNames<P>]: string;
|
|
73
|
+
};
|
|
74
|
+
/** What a route's handler receives. */
|
|
75
|
+
export interface RouteContext<S, Params = Record<string, string>> {
|
|
76
|
+
core: SimCore;
|
|
77
|
+
state: S;
|
|
78
|
+
params: Params;
|
|
79
|
+
url: URL;
|
|
80
|
+
body: unknown;
|
|
81
|
+
}
|
|
82
|
+
/** One route of a simulated provider. Build it with `route`; serve a list of them with `router`. */
|
|
83
|
+
export interface Route<S> {
|
|
84
|
+
method: string;
|
|
85
|
+
path: string;
|
|
86
|
+
handle(ctx: RouteContext<S>): Reply;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A route: an HTTP method, a path whose `:name` segments match one non-empty path segment each, and the handler
|
|
90
|
+
* that answers it. `params` is typed from the path: `route("GET", "/projects/:project", ({ params }) => …)` gets
|
|
91
|
+
* `params.project: string`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function route<S, P extends string>(method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", path: P, handle: (ctx: RouteContext<S, PathParams<P>>) => Reply): Route<S>;
|
|
94
|
+
/**
|
|
95
|
+
* A provider's `routes`: the first route whose method and path match answers; when none does, `fallback` (the
|
|
96
|
+
* provider's own "not found" body). Path patterns are compiled once, here.
|
|
97
|
+
*/
|
|
98
|
+
export declare function router<S>(routes: Array<Route<S>>, fallback: () => Reply): (core: SimCore, state: S, req: RouteRequest) => Reply;
|
|
99
|
+
export {};
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** A route's answer: status, JSON body and extra headers. Routes return one instead of writing to the socket. */
|
|
2
|
+
export class Reply {
|
|
3
|
+
status;
|
|
4
|
+
body;
|
|
5
|
+
headers;
|
|
6
|
+
constructor(status, body, headers = {}) {
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.body = body;
|
|
9
|
+
this.headers = headers;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** One page of `list` under chaos `page_size`, starting at the opaque numeric cursor. */
|
|
13
|
+
export function page(core, list, cursor, limit) {
|
|
14
|
+
// Unpaginated (the default): everything, whatever cursor the client sends.
|
|
15
|
+
if (core.chaos.page_size <= 0 && limit === undefined)
|
|
16
|
+
return { items: list, next: null };
|
|
17
|
+
const size = core.chaos.page_size > 0 ? Math.min(core.chaos.page_size, limit ?? Infinity) : (limit ?? list.length);
|
|
18
|
+
const start = cursor !== null && /^\d+$/.test(cursor) ? Number(cursor) : 0;
|
|
19
|
+
const items = list.slice(start, start + size);
|
|
20
|
+
return { items, next: start + size < list.length ? start + size : null };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A route: an HTTP method, a path whose `:name` segments match one non-empty path segment each, and the handler
|
|
24
|
+
* that answers it. `params` is typed from the path: `route("GET", "/projects/:project", ({ params }) => …)` gets
|
|
25
|
+
* `params.project: string`.
|
|
26
|
+
*/
|
|
27
|
+
export function route(method, path, handle) {
|
|
28
|
+
// `router` only calls a handler with the params its own path declares.
|
|
29
|
+
return { method, path, handle: handle };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A provider's `routes`: the first route whose method and path match answers; when none does, `fallback` (the
|
|
33
|
+
* provider's own "not found" body). Path patterns are compiled once, here.
|
|
34
|
+
*/
|
|
35
|
+
export function router(routes, fallback) {
|
|
36
|
+
const compiled = routes.map((r) => {
|
|
37
|
+
const names = [];
|
|
38
|
+
const source = r.path
|
|
39
|
+
.split("/")
|
|
40
|
+
.map((seg) => {
|
|
41
|
+
if (!seg.startsWith(":"))
|
|
42
|
+
return seg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
43
|
+
names.push(seg.slice(1));
|
|
44
|
+
return "([^/]+)";
|
|
45
|
+
})
|
|
46
|
+
.join("/");
|
|
47
|
+
return { route: r, pattern: new RegExp(`^${source}$`), names };
|
|
48
|
+
});
|
|
49
|
+
return (core, state, req) => {
|
|
50
|
+
for (const { route: r, pattern, names } of compiled) {
|
|
51
|
+
if (r.method !== req.method)
|
|
52
|
+
continue;
|
|
53
|
+
const m = pattern.exec(req.path);
|
|
54
|
+
if (!m)
|
|
55
|
+
continue;
|
|
56
|
+
const params = Object.fromEntries(names.map((n, i) => [n, decodeURIComponent(m[i + 1])]));
|
|
57
|
+
return r.handle({ core, state, params, url: req.url, body: req.body });
|
|
58
|
+
}
|
|
59
|
+
return fallback();
|
|
60
|
+
};
|
|
61
|
+
}
|
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