@zimic/interceptor 0.17.0-canary.3 → 0.17.0-canary.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/{chunk-L75WKVZO.mjs → chunk-I3RXOTN5.mjs} +12 -10
- package/dist/chunk-I3RXOTN5.mjs.map +1 -0
- package/dist/{chunk-PURXNE6R.js → chunk-W4G77E2U.js} +12 -10
- package/dist/chunk-W4G77E2U.js.map +1 -0
- package/dist/cli.js +22 -22
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +6 -6
- package/dist/cli.mjs.map +1 -1
- package/dist/http.d.ts +11 -4
- package/dist/http.js +33 -10
- package/dist/http.js.map +1 -1
- package/dist/http.mjs +33 -10
- package/dist/http.mjs.map +1 -1
- package/dist/server.js +6 -6
- package/dist/server.mjs +1 -1
- package/package.json +1 -1
- package/src/cli/cli.ts +2 -1
- package/src/cli/server/start.ts +1 -1
- package/src/cli/server/token/create.ts +3 -3
- package/src/http/interceptor/LocalHttpInterceptor.ts +2 -2
- package/src/http/interceptor/RemoteHttpInterceptor.ts +35 -4
- package/src/http/interceptor/errors/RunningHttpInterceptorError.ts +1 -1
- package/src/http/interceptor/types/options.ts +2 -2
- package/src/http/interceptor/types/public.ts +11 -3
- package/src/server/errors/InvalidInterceptorTokenError.ts +1 -1
- package/src/server/errors/InvalidInterceptorTokenFileError.ts +1 -1
- package/src/server/errors/InvalidInterceptorTokenValueError.ts +1 -1
- package/src/server/utils/auth.ts +14 -11
- package/dist/chunk-L75WKVZO.mjs.map +0 -1
- package/dist/chunk-PURXNE6R.js.map +0 -1
package/dist/server.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkW4G77E2U_js = require('./chunk-W4G77E2U.js');
|
|
4
4
|
require('./chunk-WCQVDF3K.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "DEFAULT_ACCESS_CONTROL_HEADERS", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkW4G77E2U_js.DEFAULT_ACCESS_CONTROL_HEADERS; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "DEFAULT_PREFLIGHT_STATUS_CODE", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkW4G77E2U_js.DEFAULT_PREFLIGHT_STATUS_CODE; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "NotRunningInterceptorServerError", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkW4G77E2U_js.NotRunningInterceptorServerError_default; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "RunningInterceptorServerError", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkW4G77E2U_js.RunningInterceptorServerError_default; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "createInterceptorServer", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkW4G77E2U_js.createInterceptorServer; }
|
|
27
27
|
});
|
|
28
28
|
//# sourceMappingURL=server.js.map
|
|
29
29
|
//# sourceMappingURL=server.js.map
|
package/dist/server.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-
|
|
1
|
+
export { DEFAULT_ACCESS_CONTROL_HEADERS, DEFAULT_PREFLIGHT_STATUS_CODE, NotRunningInterceptorServerError_default as NotRunningInterceptorServerError, RunningInterceptorServerError_default as RunningInterceptorServerError, createInterceptorServer } from './chunk-I3RXOTN5.mjs';
|
|
2
2
|
import './chunk-CGILA3WO.mjs';
|
|
3
3
|
//# sourceMappingURL=server.mjs.map
|
|
4
4
|
//# sourceMappingURL=server.mjs.map
|
package/package.json
CHANGED
package/src/cli/cli.ts
CHANGED
|
@@ -150,7 +150,8 @@ async function runCLI() {
|
|
|
150
150
|
|
|
151
151
|
.command(
|
|
152
152
|
['rm <tokenId>', 'remove <tokenId>'],
|
|
153
|
-
'Remove an interceptor token.'
|
|
153
|
+
'Remove (invalidate) an interceptor token. Existing connections will not be affected, so restarting the ' +
|
|
154
|
+
'server is recommended to disconnect all interceptors.',
|
|
154
155
|
(yargs) =>
|
|
155
156
|
yargs
|
|
156
157
|
.positional('tokenId', {
|
package/src/cli/server/start.ts
CHANGED
|
@@ -71,7 +71,7 @@ async function startInterceptorServer({
|
|
|
71
71
|
[
|
|
72
72
|
`Attention: this interceptor server is ${color.bold(color.red('unprotected'))}. Do not expose it publicly without authentication.`,
|
|
73
73
|
'',
|
|
74
|
-
'Learn more: https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
74
|
+
'Learn more: https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication',
|
|
75
75
|
].join('\n'),
|
|
76
76
|
);
|
|
77
77
|
}
|
|
@@ -22,12 +22,12 @@ export async function createInterceptorServerToken({
|
|
|
22
22
|
'',
|
|
23
23
|
'Store this token securely. It cannot be retrieved later.',
|
|
24
24
|
'',
|
|
25
|
-
`To enable authentication in your interceptor server, use the ${color.cyan('--tokens-dir')} option
|
|
26
|
-
'
|
|
25
|
+
`To enable authentication in your interceptor server, use the ${color.cyan('--tokens-dir')} option. Only ` +
|
|
26
|
+
'remote interceptors bearing a valid token will be allowed to connect.',
|
|
27
27
|
'',
|
|
28
28
|
`${color.dim('$')} zimic-interceptor server start ${color.cyan('--tokens-dir')} ${color.magenta(tokensDirectory)}`,
|
|
29
29
|
'',
|
|
30
|
-
'Learn more: https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
30
|
+
'Learn more: https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication',
|
|
31
31
|
].join('\n'),
|
|
32
32
|
);
|
|
33
33
|
}
|
|
@@ -18,10 +18,10 @@ class LocalHttpInterceptor<Schema extends HttpSchema> implements PublicLocalHttp
|
|
|
18
18
|
this.client = new HttpInterceptorClient<Schema, typeof LocalHttpRequestHandler>({
|
|
19
19
|
store: this.store,
|
|
20
20
|
baseURL,
|
|
21
|
-
createWorker() {
|
|
21
|
+
createWorker: () => {
|
|
22
22
|
return this.store.getOrCreateLocalWorker({});
|
|
23
23
|
},
|
|
24
|
-
deleteWorker() {
|
|
24
|
+
deleteWorker: () => {
|
|
25
25
|
this.store.deleteLocalWorker();
|
|
26
26
|
},
|
|
27
27
|
Handler: LocalHttpRequestHandler,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HttpSchema, HttpSchemaMethod, HttpSchemaPath } from '@zimic/http';
|
|
2
2
|
|
|
3
3
|
import RemoteHttpRequestHandler from '../requestHandler/RemoteHttpRequestHandler';
|
|
4
|
+
import RunningHttpInterceptorError from './errors/RunningHttpInterceptorError';
|
|
4
5
|
import HttpInterceptorClient from './HttpInterceptorClient';
|
|
5
6
|
import HttpInterceptorStore from './HttpInterceptorStore';
|
|
6
7
|
import { AsyncHttpInterceptorMethodHandler } from './types/handlers';
|
|
@@ -9,22 +10,25 @@ import { HttpInterceptorRequestSaving, RemoteHttpInterceptor as PublicRemoteHttp
|
|
|
9
10
|
|
|
10
11
|
class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHttpInterceptor<Schema> {
|
|
11
12
|
private store = new HttpInterceptorStore();
|
|
12
|
-
|
|
13
13
|
client: HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>;
|
|
14
14
|
|
|
15
|
+
private _auth?: RemoteHttpInterceptorOptions['auth'];
|
|
16
|
+
|
|
15
17
|
constructor(options: RemoteHttpInterceptorOptions) {
|
|
18
|
+
this._auth = options.auth;
|
|
19
|
+
|
|
16
20
|
const baseURL = new URL(options.baseURL);
|
|
17
21
|
|
|
18
22
|
this.client = new HttpInterceptorClient<Schema, typeof RemoteHttpRequestHandler>({
|
|
19
23
|
store: this.store,
|
|
20
24
|
baseURL,
|
|
21
|
-
createWorker() {
|
|
25
|
+
createWorker: () => {
|
|
22
26
|
return this.store.getOrCreateRemoteWorker({
|
|
23
27
|
serverURL: new URL(baseURL.origin),
|
|
24
|
-
auth:
|
|
28
|
+
auth: this._auth,
|
|
25
29
|
});
|
|
26
30
|
},
|
|
27
|
-
deleteWorker() {
|
|
31
|
+
deleteWorker: () => {
|
|
28
32
|
this.store.deleteRemoteWorker(baseURL, { auth: options.auth });
|
|
29
33
|
},
|
|
30
34
|
Handler: RemoteHttpRequestHandler,
|
|
@@ -53,6 +57,33 @@ class RemoteHttpInterceptor<Schema extends HttpSchema> implements PublicRemoteHt
|
|
|
53
57
|
this.client.requestSaving = requestSaving;
|
|
54
58
|
}
|
|
55
59
|
|
|
60
|
+
get auth() {
|
|
61
|
+
return this._auth;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
set auth(auth: RemoteHttpInterceptorOptions['auth'] | undefined) {
|
|
65
|
+
const cannotChangeAuthWhileRunningMessage =
|
|
66
|
+
'Did you forget to call `await interceptor.stop()` before changing the authentication parameters?';
|
|
67
|
+
|
|
68
|
+
if (this.isRunning) {
|
|
69
|
+
throw new RunningHttpInterceptorError(cannotChangeAuthWhileRunningMessage);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!auth) {
|
|
73
|
+
this._auth = undefined;
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
this._auth = new Proxy(auth, {
|
|
78
|
+
set: (target, property, value) => {
|
|
79
|
+
if (this.isRunning) {
|
|
80
|
+
throw new RunningHttpInterceptorError(cannotChangeAuthWhileRunningMessage);
|
|
81
|
+
}
|
|
82
|
+
return Reflect.set(target, property, value);
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
56
87
|
get onUnhandledRequest() {
|
|
57
88
|
return this.client.onUnhandledRequest;
|
|
58
89
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
class RunningHttpInterceptorError extends Error {
|
|
9
9
|
constructor(additionalMessage: string) {
|
|
10
|
-
super(`The interceptor is running
|
|
10
|
+
super(`The interceptor is running. ${additionalMessage}`);
|
|
11
11
|
this.name = 'RunningHttpInterceptorError';
|
|
12
12
|
}
|
|
13
13
|
}
|
|
@@ -164,13 +164,13 @@ export interface RemoteHttpInterceptorOptions extends SharedHttpInterceptorOptio
|
|
|
164
164
|
* Options to authenticate the interceptor when connecting to an interceptor server. This is required if the
|
|
165
165
|
* interceptor server was started with the `--tokens-dir` option.
|
|
166
166
|
*
|
|
167
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
167
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
168
168
|
*/
|
|
169
169
|
auth?: {
|
|
170
170
|
/**
|
|
171
171
|
* The authentication token to use.
|
|
172
172
|
*
|
|
173
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
173
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
174
174
|
*/
|
|
175
175
|
token: string;
|
|
176
176
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HttpSchema } from '@zimic/http';
|
|
2
2
|
|
|
3
3
|
import { SyncHttpInterceptorMethodHandler, AsyncHttpInterceptorMethodHandler } from './handlers';
|
|
4
|
-
import { HttpInterceptorPlatform, UnhandledRequestStrategy } from './options';
|
|
4
|
+
import { HttpInterceptorPlatform, RemoteHttpInterceptorOptions, UnhandledRequestStrategy } from './options';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Configures if the intercepted requests are saved and how they are handled.
|
|
@@ -163,7 +163,7 @@ export interface LocalHttpInterceptor<Schema extends HttpSchema> extends HttpInt
|
|
|
163
163
|
*
|
|
164
164
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
|
|
165
165
|
*/
|
|
166
|
-
onUnhandledRequest
|
|
166
|
+
onUnhandledRequest?: UnhandledRequestStrategy.Local;
|
|
167
167
|
|
|
168
168
|
/**
|
|
169
169
|
* Creates a GET
|
|
@@ -367,6 +367,14 @@ export interface RemoteHttpInterceptor<Schema extends HttpSchema> extends HttpIn
|
|
|
367
367
|
/** @readonly */
|
|
368
368
|
get type(): 'remote';
|
|
369
369
|
|
|
370
|
+
/**
|
|
371
|
+
* Options to authenticate the interceptor when connecting to an interceptor server. This is required if the
|
|
372
|
+
* interceptor server was started with the `--tokens-dir` option.
|
|
373
|
+
*
|
|
374
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
375
|
+
*/
|
|
376
|
+
auth?: RemoteHttpInterceptorOptions['auth'];
|
|
377
|
+
|
|
370
378
|
/**
|
|
371
379
|
* The strategy to use for unhandled requests. If a request starts with the base URL of the interceptor, but no
|
|
372
380
|
* matching handler exists, this strategy will be used. If a function is provided, it will be called with the
|
|
@@ -374,7 +382,7 @@ export interface RemoteHttpInterceptor<Schema extends HttpSchema> extends HttpIn
|
|
|
374
382
|
*
|
|
375
383
|
* @see {@link https://github.com/zimicjs/zimic/wiki/api‐zimic‐interceptor‐http#unhandled-requests Unhandled requests}
|
|
376
384
|
*/
|
|
377
|
-
onUnhandledRequest
|
|
385
|
+
onUnhandledRequest?: UnhandledRequestStrategy.Remote;
|
|
378
386
|
|
|
379
387
|
/**
|
|
380
388
|
* Creates a GET
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Error thrown when an interceptor token is invalid.
|
|
3
3
|
*
|
|
4
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
4
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
5
5
|
*/
|
|
6
6
|
class InvalidInterceptorTokenError extends Error {
|
|
7
7
|
constructor(tokenId: string) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Error thrown when an interceptor token file is invalid.
|
|
3
3
|
*
|
|
4
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
4
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
5
5
|
*/
|
|
6
6
|
class InvalidInterceptorTokenFileError extends Error {
|
|
7
7
|
constructor(tokenFilePath: string, validationErrorMessage: string) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Error thrown when an interceptor token value is invalid.
|
|
3
3
|
*
|
|
4
|
-
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐
|
|
4
|
+
* @see {@link https://github.com/zimicjs/zimic/wiki/cli‐zimic‐server#authentication Interceptor server authentication}
|
|
5
5
|
*/
|
|
6
6
|
class InvalidInterceptorTokenValueError extends Error {
|
|
7
7
|
constructor(tokenValue: string) {
|
package/src/server/utils/auth.ts
CHANGED
|
@@ -174,11 +174,10 @@ export async function readInterceptorTokenFromFile(
|
|
|
174
174
|
return validation.data.token;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
export async function createInterceptorToken(
|
|
178
|
-
name?: string;
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
const { name, tokensDirectory } = options;
|
|
177
|
+
export async function createInterceptorToken(
|
|
178
|
+
options: { name?: string; tokensDirectory?: string } = {},
|
|
179
|
+
): Promise<InterceptorToken> {
|
|
180
|
+
const { name, tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
|
|
182
181
|
|
|
183
182
|
const tokensDirectoryExists = await pathExists(tokensDirectory);
|
|
184
183
|
|
|
@@ -226,14 +225,16 @@ export async function createInterceptorToken(options: {
|
|
|
226
225
|
return token;
|
|
227
226
|
}
|
|
228
227
|
|
|
229
|
-
export async function listInterceptorTokens(options: { tokensDirectory
|
|
230
|
-
const
|
|
228
|
+
export async function listInterceptorTokens(options: { tokensDirectory?: string } = {}) {
|
|
229
|
+
const { tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
|
|
230
|
+
|
|
231
|
+
const tokensDirectoryExists = await pathExists(tokensDirectory);
|
|
231
232
|
|
|
232
233
|
if (!tokensDirectoryExists) {
|
|
233
234
|
return [];
|
|
234
235
|
}
|
|
235
236
|
|
|
236
|
-
const files = await fs.promises.readdir(
|
|
237
|
+
const files = await fs.promises.readdir(tokensDirectory);
|
|
237
238
|
|
|
238
239
|
const tokenReadPromises = files.map(async (file) => {
|
|
239
240
|
if (!isValidInterceptorTokenId(file)) {
|
|
@@ -241,7 +242,7 @@ export async function listInterceptorTokens(options: { tokensDirectory: string }
|
|
|
241
242
|
}
|
|
242
243
|
|
|
243
244
|
const tokenId = file;
|
|
244
|
-
const token = await readInterceptorTokenFromFile(tokenId,
|
|
245
|
+
const token = await readInterceptorTokenFromFile(tokenId, { tokensDirectory });
|
|
245
246
|
return token;
|
|
246
247
|
});
|
|
247
248
|
|
|
@@ -288,14 +289,16 @@ export async function validateInterceptorToken(tokenValue: string, options: { to
|
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
291
|
|
|
291
|
-
export async function removeInterceptorToken(tokenId: string, options: { tokensDirectory
|
|
292
|
+
export async function removeInterceptorToken(tokenId: string, options: { tokensDirectory?: string } = {}) {
|
|
293
|
+
const { tokensDirectory = DEFAULT_INTERCEPTOR_TOKENS_DIRECTORY } = options;
|
|
294
|
+
|
|
292
295
|
/* istanbul ignore if -- @preserve
|
|
293
296
|
* At this point, we should have a valid tokenId. This is just a sanity check. */
|
|
294
297
|
if (!isValidInterceptorTokenId(tokenId)) {
|
|
295
298
|
throw new InvalidInterceptorTokenError(tokenId);
|
|
296
299
|
}
|
|
297
300
|
|
|
298
|
-
const tokenFilePath = path.join(
|
|
301
|
+
const tokenFilePath = path.join(tokensDirectory, tokenId);
|
|
299
302
|
|
|
300
303
|
await fs.promises.rm(tokenFilePath, { force: true });
|
|
301
304
|
}
|