almostnode 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +731 -0
- package/dist/__sw__.js +394 -0
- package/dist/ai-chatbot-demo-entry.d.ts +6 -0
- package/dist/ai-chatbot-demo-entry.d.ts.map +1 -0
- package/dist/ai-chatbot-demo.d.ts +42 -0
- package/dist/ai-chatbot-demo.d.ts.map +1 -0
- package/dist/assets/runtime-worker-D9x_Ddwz.js +60543 -0
- package/dist/assets/runtime-worker-D9x_Ddwz.js.map +1 -0
- package/dist/convex-app-demo-entry.d.ts +6 -0
- package/dist/convex-app-demo-entry.d.ts.map +1 -0
- package/dist/convex-app-demo.d.ts +68 -0
- package/dist/convex-app-demo.d.ts.map +1 -0
- package/dist/cors-proxy.d.ts +46 -0
- package/dist/cors-proxy.d.ts.map +1 -0
- package/dist/create-runtime.d.ts +42 -0
- package/dist/create-runtime.d.ts.map +1 -0
- package/dist/demo.d.ts +6 -0
- package/dist/demo.d.ts.map +1 -0
- package/dist/dev-server.d.ts +97 -0
- package/dist/dev-server.d.ts.map +1 -0
- package/dist/frameworks/next-dev-server.d.ts +202 -0
- package/dist/frameworks/next-dev-server.d.ts.map +1 -0
- package/dist/frameworks/vite-dev-server.d.ts +85 -0
- package/dist/frameworks/vite-dev-server.d.ts.map +1 -0
- package/dist/index.cjs +14965 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +14867 -0
- package/dist/index.mjs.map +1 -0
- package/dist/next-demo.d.ts +49 -0
- package/dist/next-demo.d.ts.map +1 -0
- package/dist/npm/index.d.ts +71 -0
- package/dist/npm/index.d.ts.map +1 -0
- package/dist/npm/registry.d.ts +66 -0
- package/dist/npm/registry.d.ts.map +1 -0
- package/dist/npm/resolver.d.ts +52 -0
- package/dist/npm/resolver.d.ts.map +1 -0
- package/dist/npm/tarball.d.ts +29 -0
- package/dist/npm/tarball.d.ts.map +1 -0
- package/dist/runtime-interface.d.ts +90 -0
- package/dist/runtime-interface.d.ts.map +1 -0
- package/dist/runtime.d.ts +103 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/sandbox-helpers.d.ts +43 -0
- package/dist/sandbox-helpers.d.ts.map +1 -0
- package/dist/sandbox-runtime.d.ts +65 -0
- package/dist/sandbox-runtime.d.ts.map +1 -0
- package/dist/server-bridge.d.ts +89 -0
- package/dist/server-bridge.d.ts.map +1 -0
- package/dist/shims/assert.d.ts +51 -0
- package/dist/shims/assert.d.ts.map +1 -0
- package/dist/shims/async_hooks.d.ts +37 -0
- package/dist/shims/async_hooks.d.ts.map +1 -0
- package/dist/shims/buffer.d.ts +20 -0
- package/dist/shims/buffer.d.ts.map +1 -0
- package/dist/shims/child_process-browser.d.ts +92 -0
- package/dist/shims/child_process-browser.d.ts.map +1 -0
- package/dist/shims/child_process.d.ts +93 -0
- package/dist/shims/child_process.d.ts.map +1 -0
- package/dist/shims/chokidar.d.ts +55 -0
- package/dist/shims/chokidar.d.ts.map +1 -0
- package/dist/shims/cluster.d.ts +52 -0
- package/dist/shims/cluster.d.ts.map +1 -0
- package/dist/shims/crypto.d.ts +122 -0
- package/dist/shims/crypto.d.ts.map +1 -0
- package/dist/shims/dgram.d.ts +34 -0
- package/dist/shims/dgram.d.ts.map +1 -0
- package/dist/shims/diagnostics_channel.d.ts +80 -0
- package/dist/shims/diagnostics_channel.d.ts.map +1 -0
- package/dist/shims/dns.d.ts +87 -0
- package/dist/shims/dns.d.ts.map +1 -0
- package/dist/shims/domain.d.ts +25 -0
- package/dist/shims/domain.d.ts.map +1 -0
- package/dist/shims/esbuild.d.ts +105 -0
- package/dist/shims/esbuild.d.ts.map +1 -0
- package/dist/shims/events.d.ts +37 -0
- package/dist/shims/events.d.ts.map +1 -0
- package/dist/shims/fs.d.ts +115 -0
- package/dist/shims/fs.d.ts.map +1 -0
- package/dist/shims/fsevents.d.ts +67 -0
- package/dist/shims/fsevents.d.ts.map +1 -0
- package/dist/shims/http.d.ts +217 -0
- package/dist/shims/http.d.ts.map +1 -0
- package/dist/shims/http2.d.ts +81 -0
- package/dist/shims/http2.d.ts.map +1 -0
- package/dist/shims/https.d.ts +36 -0
- package/dist/shims/https.d.ts.map +1 -0
- package/dist/shims/inspector.d.ts +25 -0
- package/dist/shims/inspector.d.ts.map +1 -0
- package/dist/shims/module.d.ts +22 -0
- package/dist/shims/module.d.ts.map +1 -0
- package/dist/shims/net.d.ts +100 -0
- package/dist/shims/net.d.ts.map +1 -0
- package/dist/shims/os.d.ts +159 -0
- package/dist/shims/os.d.ts.map +1 -0
- package/dist/shims/path.d.ts +72 -0
- package/dist/shims/path.d.ts.map +1 -0
- package/dist/shims/perf_hooks.d.ts +50 -0
- package/dist/shims/perf_hooks.d.ts.map +1 -0
- package/dist/shims/process.d.ts +93 -0
- package/dist/shims/process.d.ts.map +1 -0
- package/dist/shims/querystring.d.ts +23 -0
- package/dist/shims/querystring.d.ts.map +1 -0
- package/dist/shims/readdirp.d.ts +52 -0
- package/dist/shims/readdirp.d.ts.map +1 -0
- package/dist/shims/readline.d.ts +62 -0
- package/dist/shims/readline.d.ts.map +1 -0
- package/dist/shims/rollup.d.ts +34 -0
- package/dist/shims/rollup.d.ts.map +1 -0
- package/dist/shims/sentry.d.ts +163 -0
- package/dist/shims/sentry.d.ts.map +1 -0
- package/dist/shims/stream.d.ts +181 -0
- package/dist/shims/stream.d.ts.map +1 -0
- package/dist/shims/tls.d.ts +53 -0
- package/dist/shims/tls.d.ts.map +1 -0
- package/dist/shims/tty.d.ts +30 -0
- package/dist/shims/tty.d.ts.map +1 -0
- package/dist/shims/url.d.ts +64 -0
- package/dist/shims/url.d.ts.map +1 -0
- package/dist/shims/util.d.ts +106 -0
- package/dist/shims/util.d.ts.map +1 -0
- package/dist/shims/v8.d.ts +73 -0
- package/dist/shims/v8.d.ts.map +1 -0
- package/dist/shims/vfs-adapter.d.ts +126 -0
- package/dist/shims/vfs-adapter.d.ts.map +1 -0
- package/dist/shims/vm.d.ts +45 -0
- package/dist/shims/vm.d.ts.map +1 -0
- package/dist/shims/worker_threads.d.ts +66 -0
- package/dist/shims/worker_threads.d.ts.map +1 -0
- package/dist/shims/ws.d.ts +66 -0
- package/dist/shims/ws.d.ts.map +1 -0
- package/dist/shims/zlib.d.ts +161 -0
- package/dist/shims/zlib.d.ts.map +1 -0
- package/dist/transform.d.ts +24 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/virtual-fs.d.ts +226 -0
- package/dist/virtual-fs.d.ts.map +1 -0
- package/dist/vite-demo.d.ts +35 -0
- package/dist/vite-demo.d.ts.map +1 -0
- package/dist/vite-sw.js +132 -0
- package/dist/worker/runtime-worker.d.ts +8 -0
- package/dist/worker/runtime-worker.d.ts.map +1 -0
- package/dist/worker-runtime.d.ts +50 -0
- package/dist/worker-runtime.d.ts.map +1 -0
- package/package.json +85 -0
- package/src/ai-chatbot-demo-entry.ts +244 -0
- package/src/ai-chatbot-demo.ts +509 -0
- package/src/convex-app-demo-entry.ts +1107 -0
- package/src/convex-app-demo.ts +1316 -0
- package/src/cors-proxy.ts +81 -0
- package/src/create-runtime.ts +147 -0
- package/src/demo.ts +304 -0
- package/src/dev-server.ts +274 -0
- package/src/frameworks/next-dev-server.ts +2224 -0
- package/src/frameworks/vite-dev-server.ts +702 -0
- package/src/index.ts +101 -0
- package/src/next-demo.ts +1784 -0
- package/src/npm/index.ts +347 -0
- package/src/npm/registry.ts +152 -0
- package/src/npm/resolver.ts +385 -0
- package/src/npm/tarball.ts +209 -0
- package/src/runtime-interface.ts +103 -0
- package/src/runtime.ts +1046 -0
- package/src/sandbox-helpers.ts +173 -0
- package/src/sandbox-runtime.ts +252 -0
- package/src/server-bridge.ts +426 -0
- package/src/shims/assert.ts +664 -0
- package/src/shims/async_hooks.ts +86 -0
- package/src/shims/buffer.ts +75 -0
- package/src/shims/child_process-browser.ts +217 -0
- package/src/shims/child_process.ts +463 -0
- package/src/shims/chokidar.ts +313 -0
- package/src/shims/cluster.ts +67 -0
- package/src/shims/crypto.ts +830 -0
- package/src/shims/dgram.ts +47 -0
- package/src/shims/diagnostics_channel.ts +196 -0
- package/src/shims/dns.ts +172 -0
- package/src/shims/domain.ts +58 -0
- package/src/shims/esbuild.ts +805 -0
- package/src/shims/events.ts +195 -0
- package/src/shims/fs.ts +803 -0
- package/src/shims/fsevents.ts +63 -0
- package/src/shims/http.ts +904 -0
- package/src/shims/http2.ts +96 -0
- package/src/shims/https.ts +86 -0
- package/src/shims/inspector.ts +30 -0
- package/src/shims/module.ts +82 -0
- package/src/shims/net.ts +359 -0
- package/src/shims/os.ts +195 -0
- package/src/shims/path.ts +199 -0
- package/src/shims/perf_hooks.ts +92 -0
- package/src/shims/process.ts +346 -0
- package/src/shims/querystring.ts +97 -0
- package/src/shims/readdirp.ts +228 -0
- package/src/shims/readline.ts +110 -0
- package/src/shims/rollup.ts +80 -0
- package/src/shims/sentry.ts +133 -0
- package/src/shims/stream.ts +1126 -0
- package/src/shims/tls.ts +95 -0
- package/src/shims/tty.ts +64 -0
- package/src/shims/url.ts +171 -0
- package/src/shims/util.ts +312 -0
- package/src/shims/v8.ts +113 -0
- package/src/shims/vfs-adapter.ts +402 -0
- package/src/shims/vm.ts +83 -0
- package/src/shims/worker_threads.ts +111 -0
- package/src/shims/ws.ts +382 -0
- package/src/shims/zlib.ts +289 -0
- package/src/transform.ts +313 -0
- package/src/types/external.d.ts +67 -0
- package/src/virtual-fs.ts +903 -0
- package/src/vite-demo.ts +577 -0
- package/src/worker/runtime-worker.ts +128 -0
- package/src/worker-runtime.ts +145 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js assert module shim
|
|
3
|
+
* Provides assertion testing utilities
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* AssertionError class - thrown when an assertion fails
|
|
8
|
+
*/
|
|
9
|
+
export class AssertionError extends Error {
|
|
10
|
+
actual: unknown;
|
|
11
|
+
expected: unknown;
|
|
12
|
+
operator: string;
|
|
13
|
+
generatedMessage: boolean;
|
|
14
|
+
code: string = 'ERR_ASSERTION';
|
|
15
|
+
|
|
16
|
+
constructor(options: {
|
|
17
|
+
message?: string;
|
|
18
|
+
actual?: unknown;
|
|
19
|
+
expected?: unknown;
|
|
20
|
+
operator?: string;
|
|
21
|
+
stackStartFn?: Function;
|
|
22
|
+
}) {
|
|
23
|
+
const message = options.message ||
|
|
24
|
+
`${JSON.stringify(options.actual)} ${options.operator || '=='} ${JSON.stringify(options.expected)}`;
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = 'AssertionError';
|
|
27
|
+
this.actual = options.actual;
|
|
28
|
+
this.expected = options.expected;
|
|
29
|
+
this.operator = options.operator || '';
|
|
30
|
+
this.generatedMessage = !options.message;
|
|
31
|
+
|
|
32
|
+
// Capture stack trace, excluding the constructor
|
|
33
|
+
if (Error.captureStackTrace && options.stackStartFn) {
|
|
34
|
+
Error.captureStackTrace(this, options.stackStartFn);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Deep equality check
|
|
41
|
+
*/
|
|
42
|
+
function isDeepStrictEqual(actual: unknown, expected: unknown): boolean {
|
|
43
|
+
// Same reference or primitive equality
|
|
44
|
+
if (actual === expected) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Handle null/undefined
|
|
49
|
+
if (actual === null || expected === null || actual === undefined || expected === undefined) {
|
|
50
|
+
return actual === expected;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Type check
|
|
54
|
+
if (typeof actual !== typeof expected) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// NaN check
|
|
59
|
+
if (typeof actual === 'number' && Number.isNaN(actual) && Number.isNaN(expected as number)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Date comparison
|
|
64
|
+
if (actual instanceof Date && expected instanceof Date) {
|
|
65
|
+
return actual.getTime() === expected.getTime();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// RegExp comparison
|
|
69
|
+
if (actual instanceof RegExp && expected instanceof RegExp) {
|
|
70
|
+
return actual.source === expected.source && actual.flags === expected.flags;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Array comparison
|
|
74
|
+
if (Array.isArray(actual) && Array.isArray(expected)) {
|
|
75
|
+
if (actual.length !== expected.length) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
for (let i = 0; i < actual.length; i++) {
|
|
79
|
+
if (!isDeepStrictEqual(actual[i], expected[i])) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Buffer/Uint8Array comparison
|
|
87
|
+
if (actual instanceof Uint8Array && expected instanceof Uint8Array) {
|
|
88
|
+
if (actual.length !== expected.length) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
for (let i = 0; i < actual.length; i++) {
|
|
92
|
+
if (actual[i] !== expected[i]) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Map comparison
|
|
100
|
+
if (actual instanceof Map && expected instanceof Map) {
|
|
101
|
+
if (actual.size !== expected.size) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
const actualEntries = Array.from(actual.entries());
|
|
105
|
+
for (let i = 0; i < actualEntries.length; i++) {
|
|
106
|
+
const [key, value] = actualEntries[i];
|
|
107
|
+
if (!expected.has(key) || !isDeepStrictEqual(value, expected.get(key))) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Set comparison
|
|
115
|
+
if (actual instanceof Set && expected instanceof Set) {
|
|
116
|
+
if (actual.size !== expected.size) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const actualValues = Array.from(actual.values());
|
|
120
|
+
const expectedValues = Array.from(expected.values());
|
|
121
|
+
for (let i = 0; i < actualValues.length; i++) {
|
|
122
|
+
const value = actualValues[i];
|
|
123
|
+
if (!expected.has(value)) {
|
|
124
|
+
// For objects, need deep comparison
|
|
125
|
+
let found = false;
|
|
126
|
+
for (let j = 0; j < expectedValues.length; j++) {
|
|
127
|
+
if (isDeepStrictEqual(value, expectedValues[j])) {
|
|
128
|
+
found = true;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (!found) return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Object comparison
|
|
139
|
+
if (typeof actual === 'object' && typeof expected === 'object') {
|
|
140
|
+
const actualKeys = Object.keys(actual as object);
|
|
141
|
+
const expectedKeys = Object.keys(expected as object);
|
|
142
|
+
|
|
143
|
+
if (actualKeys.length !== expectedKeys.length) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const key of actualKeys) {
|
|
148
|
+
if (!Object.prototype.hasOwnProperty.call(expected, key)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
if (!isDeepStrictEqual(
|
|
152
|
+
(actual as Record<string, unknown>)[key],
|
|
153
|
+
(expected as Record<string, unknown>)[key]
|
|
154
|
+
)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Main assert function - tests if value is truthy
|
|
166
|
+
*/
|
|
167
|
+
function assert(value: unknown, message?: string | Error): asserts value {
|
|
168
|
+
if (!value) {
|
|
169
|
+
if (message instanceof Error) {
|
|
170
|
+
throw message;
|
|
171
|
+
}
|
|
172
|
+
throw new AssertionError({
|
|
173
|
+
message: message || 'The expression evaluated to a falsy value',
|
|
174
|
+
actual: value,
|
|
175
|
+
expected: true,
|
|
176
|
+
operator: '==',
|
|
177
|
+
stackStartFn: assert,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Alias for assert()
|
|
184
|
+
*/
|
|
185
|
+
assert.ok = function ok(value: unknown, message?: string | Error): asserts value {
|
|
186
|
+
if (!value) {
|
|
187
|
+
if (message instanceof Error) {
|
|
188
|
+
throw message;
|
|
189
|
+
}
|
|
190
|
+
throw new AssertionError({
|
|
191
|
+
message: message || 'The expression evaluated to a falsy value',
|
|
192
|
+
actual: value,
|
|
193
|
+
expected: true,
|
|
194
|
+
operator: '==',
|
|
195
|
+
stackStartFn: ok,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Tests strict equality (===)
|
|
202
|
+
*/
|
|
203
|
+
assert.strictEqual = function strictEqual<T>(
|
|
204
|
+
actual: T,
|
|
205
|
+
expected: T,
|
|
206
|
+
message?: string | Error
|
|
207
|
+
): void {
|
|
208
|
+
if (actual !== expected) {
|
|
209
|
+
if (message instanceof Error) {
|
|
210
|
+
throw message;
|
|
211
|
+
}
|
|
212
|
+
throw new AssertionError({
|
|
213
|
+
message,
|
|
214
|
+
actual,
|
|
215
|
+
expected,
|
|
216
|
+
operator: '===',
|
|
217
|
+
stackStartFn: strictEqual,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Tests strict inequality (!==)
|
|
224
|
+
*/
|
|
225
|
+
assert.notStrictEqual = function notStrictEqual<T>(
|
|
226
|
+
actual: T,
|
|
227
|
+
expected: T,
|
|
228
|
+
message?: string | Error
|
|
229
|
+
): void {
|
|
230
|
+
if (actual === expected) {
|
|
231
|
+
if (message instanceof Error) {
|
|
232
|
+
throw message;
|
|
233
|
+
}
|
|
234
|
+
throw new AssertionError({
|
|
235
|
+
message,
|
|
236
|
+
actual,
|
|
237
|
+
expected,
|
|
238
|
+
operator: '!==',
|
|
239
|
+
stackStartFn: notStrictEqual,
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Tests deep strict equality
|
|
246
|
+
*/
|
|
247
|
+
assert.deepStrictEqual = function deepStrictEqual<T>(
|
|
248
|
+
actual: T,
|
|
249
|
+
expected: T,
|
|
250
|
+
message?: string | Error
|
|
251
|
+
): void {
|
|
252
|
+
if (!isDeepStrictEqual(actual, expected)) {
|
|
253
|
+
if (message instanceof Error) {
|
|
254
|
+
throw message;
|
|
255
|
+
}
|
|
256
|
+
throw new AssertionError({
|
|
257
|
+
message,
|
|
258
|
+
actual,
|
|
259
|
+
expected,
|
|
260
|
+
operator: 'deepStrictEqual',
|
|
261
|
+
stackStartFn: deepStrictEqual,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Tests deep strict inequality
|
|
268
|
+
*/
|
|
269
|
+
assert.notDeepStrictEqual = function notDeepStrictEqual<T>(
|
|
270
|
+
actual: T,
|
|
271
|
+
expected: T,
|
|
272
|
+
message?: string | Error
|
|
273
|
+
): void {
|
|
274
|
+
if (isDeepStrictEqual(actual, expected)) {
|
|
275
|
+
if (message instanceof Error) {
|
|
276
|
+
throw message;
|
|
277
|
+
}
|
|
278
|
+
throw new AssertionError({
|
|
279
|
+
message,
|
|
280
|
+
actual,
|
|
281
|
+
expected,
|
|
282
|
+
operator: 'notDeepStrictEqual',
|
|
283
|
+
stackStartFn: notDeepStrictEqual,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Expects function to throw an error
|
|
290
|
+
*/
|
|
291
|
+
assert.throws = function throws(
|
|
292
|
+
fn: () => unknown,
|
|
293
|
+
errorOrMessage?: RegExp | Function | Error | { message?: RegExp | string; code?: string } | string,
|
|
294
|
+
message?: string
|
|
295
|
+
): void {
|
|
296
|
+
let threw = false;
|
|
297
|
+
let thrownError: unknown;
|
|
298
|
+
|
|
299
|
+
try {
|
|
300
|
+
fn();
|
|
301
|
+
} catch (err) {
|
|
302
|
+
threw = true;
|
|
303
|
+
thrownError = err;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (!threw) {
|
|
307
|
+
throw new AssertionError({
|
|
308
|
+
message: typeof errorOrMessage === 'string' ? errorOrMessage : (message || 'Expected function to throw'),
|
|
309
|
+
actual: undefined,
|
|
310
|
+
expected: errorOrMessage,
|
|
311
|
+
operator: 'throws',
|
|
312
|
+
stackStartFn: throws,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Validate the thrown error if a validator was provided
|
|
317
|
+
if (errorOrMessage !== undefined && typeof errorOrMessage !== 'string') {
|
|
318
|
+
if (errorOrMessage instanceof RegExp) {
|
|
319
|
+
const errMessage = thrownError instanceof Error ? thrownError.message : String(thrownError);
|
|
320
|
+
if (!errorOrMessage.test(errMessage)) {
|
|
321
|
+
throw new AssertionError({
|
|
322
|
+
message: message || `The error message did not match the regular expression`,
|
|
323
|
+
actual: thrownError,
|
|
324
|
+
expected: errorOrMessage,
|
|
325
|
+
operator: 'throws',
|
|
326
|
+
stackStartFn: throws,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
} else if (typeof errorOrMessage === 'function') {
|
|
330
|
+
if (!(thrownError instanceof (errorOrMessage as new (...args: unknown[]) => Error))) {
|
|
331
|
+
throw new AssertionError({
|
|
332
|
+
message: message || `The error is not an instance of the expected type`,
|
|
333
|
+
actual: thrownError,
|
|
334
|
+
expected: errorOrMessage,
|
|
335
|
+
operator: 'throws',
|
|
336
|
+
stackStartFn: throws,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
} else if (typeof errorOrMessage === 'object') {
|
|
340
|
+
const expected = errorOrMessage as { message?: RegExp | string; code?: string };
|
|
341
|
+
const err = thrownError as Error & { code?: string };
|
|
342
|
+
|
|
343
|
+
if (expected.message !== undefined) {
|
|
344
|
+
const errMsg = err.message || String(thrownError);
|
|
345
|
+
if (expected.message instanceof RegExp) {
|
|
346
|
+
if (!expected.message.test(errMsg)) {
|
|
347
|
+
throw new AssertionError({
|
|
348
|
+
message: message || `The error message did not match`,
|
|
349
|
+
actual: errMsg,
|
|
350
|
+
expected: expected.message,
|
|
351
|
+
operator: 'throws',
|
|
352
|
+
stackStartFn: throws,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
} else if (errMsg !== expected.message) {
|
|
356
|
+
throw new AssertionError({
|
|
357
|
+
message: message || `The error message did not match`,
|
|
358
|
+
actual: errMsg,
|
|
359
|
+
expected: expected.message,
|
|
360
|
+
operator: 'throws',
|
|
361
|
+
stackStartFn: throws,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (expected.code !== undefined && err.code !== expected.code) {
|
|
367
|
+
throw new AssertionError({
|
|
368
|
+
message: message || `The error code did not match`,
|
|
369
|
+
actual: err.code,
|
|
370
|
+
expected: expected.code,
|
|
371
|
+
operator: 'throws',
|
|
372
|
+
stackStartFn: throws,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Expects function to not throw an error
|
|
381
|
+
*/
|
|
382
|
+
assert.doesNotThrow = function doesNotThrow(
|
|
383
|
+
fn: () => unknown,
|
|
384
|
+
errorOrMessage?: RegExp | Function | string,
|
|
385
|
+
message?: string
|
|
386
|
+
): void {
|
|
387
|
+
try {
|
|
388
|
+
fn();
|
|
389
|
+
} catch (err) {
|
|
390
|
+
// If no validator provided, any throw is a failure
|
|
391
|
+
if (errorOrMessage === undefined || typeof errorOrMessage === 'string') {
|
|
392
|
+
throw new AssertionError({
|
|
393
|
+
message: typeof errorOrMessage === 'string' ? errorOrMessage : (message || 'Expected function not to throw'),
|
|
394
|
+
actual: err,
|
|
395
|
+
expected: undefined,
|
|
396
|
+
operator: 'doesNotThrow',
|
|
397
|
+
stackStartFn: doesNotThrow,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// If validator provided, only matching errors are failures
|
|
402
|
+
if (errorOrMessage instanceof RegExp) {
|
|
403
|
+
const errMessage = err instanceof Error ? err.message : String(err);
|
|
404
|
+
if (errorOrMessage.test(errMessage)) {
|
|
405
|
+
throw new AssertionError({
|
|
406
|
+
message: message || 'Expected function not to throw matching error',
|
|
407
|
+
actual: err,
|
|
408
|
+
expected: errorOrMessage,
|
|
409
|
+
operator: 'doesNotThrow',
|
|
410
|
+
stackStartFn: doesNotThrow,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
} else if (typeof errorOrMessage === 'function') {
|
|
414
|
+
if (err instanceof (errorOrMessage as new (...args: unknown[]) => Error)) {
|
|
415
|
+
throw new AssertionError({
|
|
416
|
+
message: message || 'Expected function not to throw error of this type',
|
|
417
|
+
actual: err,
|
|
418
|
+
expected: errorOrMessage,
|
|
419
|
+
operator: 'doesNotThrow',
|
|
420
|
+
stackStartFn: doesNotThrow,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Expects promise to reject
|
|
429
|
+
*/
|
|
430
|
+
assert.rejects = async function rejects(
|
|
431
|
+
asyncFn: Promise<unknown> | (() => Promise<unknown>),
|
|
432
|
+
errorOrMessage?: RegExp | Function | Error | { message?: RegExp | string; code?: string } | string,
|
|
433
|
+
message?: string
|
|
434
|
+
): Promise<void> {
|
|
435
|
+
const promise = typeof asyncFn === 'function' ? asyncFn() : asyncFn;
|
|
436
|
+
|
|
437
|
+
let rejected = false;
|
|
438
|
+
let rejectionReason: unknown;
|
|
439
|
+
|
|
440
|
+
try {
|
|
441
|
+
await promise;
|
|
442
|
+
} catch (err) {
|
|
443
|
+
rejected = true;
|
|
444
|
+
rejectionReason = err;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (!rejected) {
|
|
448
|
+
throw new AssertionError({
|
|
449
|
+
message: typeof errorOrMessage === 'string' ? errorOrMessage : (message || 'Expected promise to reject'),
|
|
450
|
+
actual: undefined,
|
|
451
|
+
expected: errorOrMessage,
|
|
452
|
+
operator: 'rejects',
|
|
453
|
+
stackStartFn: rejects,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Validate the rejection reason if a validator was provided
|
|
458
|
+
if (errorOrMessage !== undefined && typeof errorOrMessage !== 'string') {
|
|
459
|
+
if (errorOrMessage instanceof RegExp) {
|
|
460
|
+
const errMessage = rejectionReason instanceof Error ? rejectionReason.message : String(rejectionReason);
|
|
461
|
+
if (!errorOrMessage.test(errMessage)) {
|
|
462
|
+
throw new AssertionError({
|
|
463
|
+
message: message || 'The rejection message did not match the regular expression',
|
|
464
|
+
actual: rejectionReason,
|
|
465
|
+
expected: errorOrMessage,
|
|
466
|
+
operator: 'rejects',
|
|
467
|
+
stackStartFn: rejects,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
} else if (typeof errorOrMessage === 'function') {
|
|
471
|
+
if (!(rejectionReason instanceof (errorOrMessage as new (...args: unknown[]) => Error))) {
|
|
472
|
+
throw new AssertionError({
|
|
473
|
+
message: message || 'The rejection is not an instance of the expected type',
|
|
474
|
+
actual: rejectionReason,
|
|
475
|
+
expected: errorOrMessage,
|
|
476
|
+
operator: 'rejects',
|
|
477
|
+
stackStartFn: rejects,
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
} else if (typeof errorOrMessage === 'object') {
|
|
481
|
+
const expected = errorOrMessage as { message?: RegExp | string; code?: string };
|
|
482
|
+
const err = rejectionReason as Error & { code?: string };
|
|
483
|
+
|
|
484
|
+
if (expected.message !== undefined) {
|
|
485
|
+
const errMsg = err.message || String(rejectionReason);
|
|
486
|
+
if (expected.message instanceof RegExp) {
|
|
487
|
+
if (!expected.message.test(errMsg)) {
|
|
488
|
+
throw new AssertionError({
|
|
489
|
+
message: message || 'The rejection message did not match',
|
|
490
|
+
actual: errMsg,
|
|
491
|
+
expected: expected.message,
|
|
492
|
+
operator: 'rejects',
|
|
493
|
+
stackStartFn: rejects,
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
} else if (errMsg !== expected.message) {
|
|
497
|
+
throw new AssertionError({
|
|
498
|
+
message: message || 'The rejection message did not match',
|
|
499
|
+
actual: errMsg,
|
|
500
|
+
expected: expected.message,
|
|
501
|
+
operator: 'rejects',
|
|
502
|
+
stackStartFn: rejects,
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (expected.code !== undefined && err.code !== expected.code) {
|
|
508
|
+
throw new AssertionError({
|
|
509
|
+
message: message || 'The rejection code did not match',
|
|
510
|
+
actual: err.code,
|
|
511
|
+
expected: expected.code,
|
|
512
|
+
operator: 'rejects',
|
|
513
|
+
stackStartFn: rejects,
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Expects promise to not reject
|
|
522
|
+
*/
|
|
523
|
+
assert.doesNotReject = async function doesNotReject(
|
|
524
|
+
asyncFn: Promise<unknown> | (() => Promise<unknown>),
|
|
525
|
+
errorOrMessage?: RegExp | Function | string,
|
|
526
|
+
message?: string
|
|
527
|
+
): Promise<void> {
|
|
528
|
+
const promise = typeof asyncFn === 'function' ? asyncFn() : asyncFn;
|
|
529
|
+
|
|
530
|
+
try {
|
|
531
|
+
await promise;
|
|
532
|
+
} catch (err) {
|
|
533
|
+
// If no validator provided, any rejection is a failure
|
|
534
|
+
if (errorOrMessage === undefined || typeof errorOrMessage === 'string') {
|
|
535
|
+
throw new AssertionError({
|
|
536
|
+
message: typeof errorOrMessage === 'string' ? errorOrMessage : (message || 'Expected promise not to reject'),
|
|
537
|
+
actual: err,
|
|
538
|
+
expected: undefined,
|
|
539
|
+
operator: 'doesNotReject',
|
|
540
|
+
stackStartFn: doesNotReject,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// If validator provided, only matching rejections are failures
|
|
545
|
+
if (errorOrMessage instanceof RegExp) {
|
|
546
|
+
const errMessage = err instanceof Error ? err.message : String(err);
|
|
547
|
+
if (errorOrMessage.test(errMessage)) {
|
|
548
|
+
throw new AssertionError({
|
|
549
|
+
message: message || 'Expected promise not to reject with matching error',
|
|
550
|
+
actual: err,
|
|
551
|
+
expected: errorOrMessage,
|
|
552
|
+
operator: 'doesNotReject',
|
|
553
|
+
stackStartFn: doesNotReject,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
} else if (typeof errorOrMessage === 'function') {
|
|
557
|
+
if (err instanceof (errorOrMessage as new (...args: unknown[]) => Error)) {
|
|
558
|
+
throw new AssertionError({
|
|
559
|
+
message: message || 'Expected promise not to reject with error of this type',
|
|
560
|
+
actual: err,
|
|
561
|
+
expected: errorOrMessage,
|
|
562
|
+
operator: 'doesNotReject',
|
|
563
|
+
stackStartFn: doesNotReject,
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Throws an AssertionError
|
|
572
|
+
*/
|
|
573
|
+
assert.fail = function fail(
|
|
574
|
+
messageOrActual?: string | unknown,
|
|
575
|
+
expected?: unknown,
|
|
576
|
+
message?: string,
|
|
577
|
+
operator?: string
|
|
578
|
+
): never {
|
|
579
|
+
if (arguments.length === 0 || arguments.length === 1) {
|
|
580
|
+
throw new AssertionError({
|
|
581
|
+
message: typeof messageOrActual === 'string' ? messageOrActual : 'Failed',
|
|
582
|
+
stackStartFn: fail,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
throw new AssertionError({
|
|
587
|
+
message,
|
|
588
|
+
actual: messageOrActual,
|
|
589
|
+
expected,
|
|
590
|
+
operator: operator || 'fail',
|
|
591
|
+
stackStartFn: fail,
|
|
592
|
+
});
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Tests if string matches regular expression
|
|
597
|
+
*/
|
|
598
|
+
assert.match = function match(
|
|
599
|
+
string: string,
|
|
600
|
+
regexp: RegExp,
|
|
601
|
+
message?: string | Error
|
|
602
|
+
): void {
|
|
603
|
+
if (!regexp.test(string)) {
|
|
604
|
+
if (message instanceof Error) {
|
|
605
|
+
throw message;
|
|
606
|
+
}
|
|
607
|
+
throw new AssertionError({
|
|
608
|
+
message: message || `The input did not match the regular expression`,
|
|
609
|
+
actual: string,
|
|
610
|
+
expected: regexp,
|
|
611
|
+
operator: 'match',
|
|
612
|
+
stackStartFn: match,
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Tests if string does not match regular expression
|
|
619
|
+
*/
|
|
620
|
+
assert.doesNotMatch = function doesNotMatch(
|
|
621
|
+
string: string,
|
|
622
|
+
regexp: RegExp,
|
|
623
|
+
message?: string | Error
|
|
624
|
+
): void {
|
|
625
|
+
if (regexp.test(string)) {
|
|
626
|
+
if (message instanceof Error) {
|
|
627
|
+
throw message;
|
|
628
|
+
}
|
|
629
|
+
throw new AssertionError({
|
|
630
|
+
message: message || `The input was expected not to match the regular expression`,
|
|
631
|
+
actual: string,
|
|
632
|
+
expected: regexp,
|
|
633
|
+
operator: 'doesNotMatch',
|
|
634
|
+
stackStartFn: doesNotMatch,
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
/**
|
|
640
|
+
* Throws if value is truthy (used for error-first callback patterns)
|
|
641
|
+
*/
|
|
642
|
+
assert.ifError = function ifError(value: unknown): void {
|
|
643
|
+
if (value !== null && value !== undefined) {
|
|
644
|
+
if (value instanceof Error) {
|
|
645
|
+
throw value;
|
|
646
|
+
}
|
|
647
|
+
throw new AssertionError({
|
|
648
|
+
message: `ifError got unwanted exception: ${value}`,
|
|
649
|
+
actual: value,
|
|
650
|
+
expected: null,
|
|
651
|
+
operator: 'ifError',
|
|
652
|
+
stackStartFn: ifError,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
// Export the AssertionError class on the assert function
|
|
658
|
+
assert.AssertionError = AssertionError;
|
|
659
|
+
|
|
660
|
+
// Strict mode (same behavior for this implementation)
|
|
661
|
+
assert.strict = assert;
|
|
662
|
+
|
|
663
|
+
export default assert;
|
|
664
|
+
export { assert };
|