wrangler 2.20.0 → 3.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/README.md +4 -4
- package/bin/wrangler.js +9 -75
- package/package.json +5 -13
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -1
- package/templates/checked-fetch.js +1 -1
- package/templates/first-party-worker-module-facade.ts +2 -2
- package/templates/middleware/common.ts +9 -4
- package/templates/middleware/loader-sw.ts +2 -7
- package/templates/new-worker-scheduled.ts +1 -1
- package/templates/new-worker.ts +1 -1
- package/templates/pages-dev-util.ts +4 -1
- package/templates/pages-shim.ts +0 -3
- package/templates/tsconfig.tsbuildinfo +1 -1
- package/wrangler-dist/cli.d.ts +149 -75
- package/wrangler-dist/cli.js +60062 -64338
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/miniflare-dist/index.mjs +0 -6442
- package/src/__tests__/access.test.ts +0 -25
- package/src/__tests__/api-dev.test.ts +0 -238
- package/src/__tests__/api-devregistry.test.ts +0 -121
- package/src/__tests__/api.test.ts +0 -102
- package/src/__tests__/config-cache-without-cache-dir.test.ts +0 -38
- package/src/__tests__/config-cache.test.ts +0 -42
- package/src/__tests__/configuration.test.ts +0 -4517
- package/src/__tests__/constellation.test.ts +0 -371
- package/src/__tests__/d1/d1.test.ts +0 -82
- package/src/__tests__/d1/execute.test.ts +0 -66
- package/src/__tests__/d1/migrate.test.ts +0 -257
- package/src/__tests__/d1/splitter.test.ts +0 -255
- package/src/__tests__/delete.test.ts +0 -272
- package/src/__tests__/deployments.test.ts +0 -369
- package/src/__tests__/dev.test.tsx +0 -1617
- package/src/__tests__/generate.test.ts +0 -237
- package/src/__tests__/get-host-from-url.test.ts +0 -16
- package/src/__tests__/guess-worker-format.test.ts +0 -120
- package/src/__tests__/helpers/clipboardy-mock.js +0 -4
- package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
- package/src/__tests__/helpers/end-event-loop.ts +0 -6
- package/src/__tests__/helpers/mock-account-id.ts +0 -48
- package/src/__tests__/helpers/mock-auth-domain.ts +0 -20
- package/src/__tests__/helpers/mock-bin.ts +0 -36
- package/src/__tests__/helpers/mock-console.ts +0 -112
- package/src/__tests__/helpers/mock-dialogs.ts +0 -139
- package/src/__tests__/helpers/mock-get-pages-upload-token.ts +0 -25
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +0 -11
- package/src/__tests__/helpers/mock-http-server.ts +0 -46
- package/src/__tests__/helpers/mock-istty.ts +0 -74
- package/src/__tests__/helpers/mock-known-routes.ts +0 -12
- package/src/__tests__/helpers/mock-kv.ts +0 -46
- package/src/__tests__/helpers/mock-oauth-flow.ts +0 -263
- package/src/__tests__/helpers/mock-process.ts +0 -34
- package/src/__tests__/helpers/mock-set-timeout.ts +0 -16
- package/src/__tests__/helpers/mock-stdin.ts +0 -108
- package/src/__tests__/helpers/mock-web-socket.ts +0 -29
- package/src/__tests__/helpers/msw/blob-worker.cjs +0 -19
- package/src/__tests__/helpers/msw/handlers/access.ts +0 -13
- package/src/__tests__/helpers/msw/handlers/deployments.ts +0 -160
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +0 -81
- package/src/__tests__/helpers/msw/handlers/oauth.ts +0 -31
- package/src/__tests__/helpers/msw/handlers/r2.ts +0 -60
- package/src/__tests__/helpers/msw/handlers/script.ts +0 -56
- package/src/__tests__/helpers/msw/handlers/user.ts +0 -52
- package/src/__tests__/helpers/msw/handlers/zones.ts +0 -20
- package/src/__tests__/helpers/msw/index.ts +0 -52
- package/src/__tests__/helpers/msw/read-file-sync.js +0 -61
- package/src/__tests__/helpers/run-in-tmp.ts +0 -38
- package/src/__tests__/helpers/run-wrangler.ts +0 -16
- package/src/__tests__/helpers/string-dynamic-values-matcher.ts +0 -28
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +0 -5
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +0 -1
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +0 -11
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +0 -5
- package/src/__tests__/helpers/write-worker-source.ts +0 -31
- package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
- package/src/__tests__/https-options.test.ts +0 -163
- package/src/__tests__/index.test.ts +0 -282
- package/src/__tests__/init.test.ts +0 -3196
- package/src/__tests__/jest.setup.ts +0 -179
- package/src/__tests__/kv.test.ts +0 -1799
- package/src/__tests__/logger.test.ts +0 -207
- package/src/__tests__/logout.test.ts +0 -47
- package/src/__tests__/metrics.test.ts +0 -493
- package/src/__tests__/middleware.scheduled.test.ts +0 -145
- package/src/__tests__/middleware.test.ts +0 -816
- package/src/__tests__/mtls-certificates.test.ts +0 -589
- package/src/__tests__/package-manager.test.ts +0 -353
- package/src/__tests__/pages/deployment-list.test.ts +0 -80
- package/src/__tests__/pages/functions-build.test.ts +0 -528
- package/src/__tests__/pages/pages.test.ts +0 -81
- package/src/__tests__/pages/project-create.test.ts +0 -63
- package/src/__tests__/pages/project-list.test.ts +0 -110
- package/src/__tests__/pages/project-upload.test.ts +0 -500
- package/src/__tests__/pages/publish.test.ts +0 -2864
- package/src/__tests__/pages-deployment-tail.test.ts +0 -957
- package/src/__tests__/parse.test.ts +0 -436
- package/src/__tests__/paths.test.ts +0 -39
- package/src/__tests__/publish.test.ts +0 -8849
- package/src/__tests__/pubsub.test.ts +0 -496
- package/src/__tests__/queues.test.ts +0 -532
- package/src/__tests__/r2.test.ts +0 -374
- package/src/__tests__/route.test.ts +0 -45
- package/src/__tests__/secret.test.ts +0 -693
- package/src/__tests__/tail.test.ts +0 -989
- package/src/__tests__/test-old-node-version.js +0 -31
- package/src/__tests__/traverse-module-graph.test.ts +0 -220
- package/src/__tests__/tsconfig-sanity.ts +0 -12
- package/src/__tests__/tsconfig.json +0 -8
- package/src/__tests__/tsconfig.tsbuildinfo +0 -1
- package/src/__tests__/type-generation.test.ts +0 -234
- package/src/__tests__/user.test.ts +0 -118
- package/src/__tests__/utils-collectKeyValues.test.ts +0 -47
- package/src/__tests__/validate-dev-props.test.ts +0 -56
- package/src/__tests__/version.test.ts +0 -35
- package/src/__tests__/whoami.test.tsx +0 -172
- package/src/__tests__/worker-namespace.test.ts +0 -340
- package/src/abort.d.ts +0 -3
- package/src/api/dev.ts +0 -321
- package/src/api/index.ts +0 -11
- package/src/api/mtls-certificate.ts +0 -148
- package/src/api/pages/create-worker-bundle-contents.ts +0 -77
- package/src/api/pages/index.ts +0 -5
- package/src/api/pages/publish.tsx +0 -371
- package/src/bundle-reporter.ts +0 -68
- package/src/bundle.ts +0 -929
- package/src/cfetch/index.ts +0 -158
- package/src/cfetch/internal.ts +0 -258
- package/src/cli.ts +0 -28
- package/src/config/README.md +0 -107
- package/src/config/config.ts +0 -282
- package/src/config/diagnostics.ts +0 -80
- package/src/config/environment.ts +0 -625
- package/src/config/index.ts +0 -403
- package/src/config/validation-helpers.ts +0 -597
- package/src/config/validation.ts +0 -2369
- package/src/config-cache.ts +0 -85
- package/src/constellation/createProject.tsx +0 -51
- package/src/constellation/deleteProject.ts +0 -51
- package/src/constellation/deleteProjectModel.ts +0 -68
- package/src/constellation/index.ts +0 -75
- package/src/constellation/listCatalog.tsx +0 -35
- package/src/constellation/listModel.tsx +0 -41
- package/src/constellation/listProject.tsx +0 -28
- package/src/constellation/listRuntime.tsx +0 -28
- package/src/constellation/options.ts +0 -17
- package/src/constellation/types.ts +0 -17
- package/src/constellation/uploadModel.tsx +0 -64
- package/src/constellation/utils.ts +0 -90
- package/src/create-worker-preview.ts +0 -293
- package/src/create-worker-upload-form.ts +0 -363
- package/src/d1/backups.tsx +0 -219
- package/src/d1/constants.ts +0 -2
- package/src/d1/create.tsx +0 -70
- package/src/d1/delete.ts +0 -53
- package/src/d1/execute.tsx +0 -357
- package/src/d1/formatTimeAgo.ts +0 -14
- package/src/d1/index.ts +0 -100
- package/src/d1/list.tsx +0 -62
- package/src/d1/migrations/apply.tsx +0 -212
- package/src/d1/migrations/create.tsx +0 -79
- package/src/d1/migrations/helpers.ts +0 -169
- package/src/d1/migrations/index.ts +0 -3
- package/src/d1/migrations/list.tsx +0 -95
- package/src/d1/migrations/options.ts +0 -23
- package/src/d1/options.ts +0 -22
- package/src/d1/splitter.ts +0 -161
- package/src/d1/types.ts +0 -25
- package/src/d1/utils.ts +0 -49
- package/src/delete.ts +0 -100
- package/src/deployments.ts +0 -368
- package/src/deprecated/index.ts +0 -144
- package/src/dev/dev-vars.ts +0 -39
- package/src/dev/dev.tsx +0 -605
- package/src/dev/get-local-persistence-path.ts +0 -31
- package/src/dev/local.tsx +0 -952
- package/src/dev/remote.tsx +0 -635
- package/src/dev/start-server.ts +0 -545
- package/src/dev/use-esbuild.ts +0 -215
- package/src/dev/validate-dev-props.ts +0 -40
- package/src/dev-registry.ts +0 -202
- package/src/dev.tsx +0 -934
- package/src/dialogs.ts +0 -136
- package/src/dispatch-namespace.ts +0 -211
- package/src/docs/helpers.ts +0 -50
- package/src/docs/index.ts +0 -54
- package/src/durable.ts +0 -102
- package/src/entry.ts +0 -344
- package/src/environment-variables/factory.ts +0 -89
- package/src/environment-variables/misc-variables.ts +0 -30
- package/src/errors.ts +0 -11
- package/src/generate/index.ts +0 -298
- package/src/git-client.ts +0 -135
- package/src/global-wrangler-config-path.ts +0 -26
- package/src/https-options.ts +0 -127
- package/src/index.ts +0 -768
- package/src/init.ts +0 -1037
- package/src/inspect.ts +0 -883
- package/src/intl-polyfill.d.ts +0 -139
- package/src/is-ci.ts +0 -14
- package/src/is-interactive.ts +0 -16
- package/src/jest.d.ts +0 -4
- package/src/kv/helpers.ts +0 -433
- package/src/kv/index.ts +0 -594
- package/src/logger.ts +0 -123
- package/src/metrics/index.ts +0 -5
- package/src/metrics/metrics-config.ts +0 -239
- package/src/metrics/metrics-dispatcher.ts +0 -96
- package/src/metrics/metrics-usage-headers.ts +0 -24
- package/src/metrics/send-event.ts +0 -99
- package/src/miniflare-cli/README.md +0 -30
- package/src/miniflare-cli/assets.ts +0 -251
- package/src/miniflare-cli/index.ts +0 -210
- package/src/miniflare-cli/request-context.ts +0 -40
- package/src/miniflare-cli/tsconfig.json +0 -9
- package/src/miniflare-cli/tsconfig.tsbuildinfo +0 -1
- package/src/miniflare-cli/types.ts +0 -11
- package/src/module-collection.ts +0 -333
- package/src/mtls-certificate/cli.ts +0 -155
- package/src/open-in-browser.ts +0 -17
- package/src/package-manager.ts +0 -219
- package/src/pages/build.ts +0 -423
- package/src/pages/buildFunctions.ts +0 -140
- package/src/pages/constants.ts +0 -18
- package/src/pages/deployment-tails.ts +0 -281
- package/src/pages/deployments.tsx +0 -84
- package/src/pages/dev.ts +0 -734
- package/src/pages/errors.ts +0 -67
- package/src/pages/functions/buildPlugin.ts +0 -114
- package/src/pages/functions/buildWorker.ts +0 -350
- package/src/pages/functions/filepath-routing.test.ts +0 -234
- package/src/pages/functions/filepath-routing.ts +0 -189
- package/src/pages/functions/identifiers.ts +0 -78
- package/src/pages/functions/routes-consolidation.test.ts +0 -250
- package/src/pages/functions/routes-consolidation.ts +0 -73
- package/src/pages/functions/routes-transformation.test.ts +0 -282
- package/src/pages/functions/routes-transformation.ts +0 -115
- package/src/pages/functions/routes-validation.test.ts +0 -403
- package/src/pages/functions/routes-validation.ts +0 -202
- package/src/pages/functions/routes.ts +0 -151
- package/src/pages/functions/tsconfig.json +0 -8
- package/src/pages/functions/tsconfig.tsbuildinfo +0 -1
- package/src/pages/functions.ts +0 -86
- package/src/pages/hash.ts +0 -13
- package/src/pages/index.ts +0 -102
- package/src/pages/projects.tsx +0 -159
- package/src/pages/prompt-select-project.tsx +0 -31
- package/src/pages/publish.tsx +0 -267
- package/src/pages/types.ts +0 -46
- package/src/pages/upload.tsx +0 -469
- package/src/pages/utils.ts +0 -23
- package/src/parse.ts +0 -308
- package/src/paths.ts +0 -71
- package/src/proxy.ts +0 -694
- package/src/publish/index.ts +0 -274
- package/src/publish/publish.ts +0 -1065
- package/src/pubsub/index.ts +0 -286
- package/src/pubsub/pubsub-commands.ts +0 -623
- package/src/queues/cli/commands/consumer/add.ts +0 -71
- package/src/queues/cli/commands/consumer/index.ts +0 -19
- package/src/queues/cli/commands/consumer/remove.ts +0 -31
- package/src/queues/cli/commands/create.ts +0 -25
- package/src/queues/cli/commands/delete.ts +0 -26
- package/src/queues/cli/commands/index.ts +0 -35
- package/src/queues/cli/commands/list.ts +0 -25
- package/src/queues/client.ts +0 -136
- package/src/queues/utils.ts +0 -18
- package/src/r2/constants.ts +0 -4
- package/src/r2/helpers.ts +0 -132
- package/src/r2/index.ts +0 -289
- package/src/routes.ts +0 -140
- package/src/secret/index.ts +0 -377
- package/src/selfsigned.d.ts +0 -29
- package/src/sites.ts +0 -484
- package/src/tail/createTail.ts +0 -415
- package/src/tail/filters.ts +0 -277
- package/src/tail/index.ts +0 -211
- package/src/tail/printing.ts +0 -132
- package/src/traverse-module-graph.ts +0 -54
- package/src/tsconfig-sanity.ts +0 -16
- package/src/type-generation.ts +0 -181
- package/src/update-check.ts +0 -19
- package/src/user/access.ts +0 -68
- package/src/user/auth-variables.ts +0 -113
- package/src/user/choose-account.tsx +0 -39
- package/src/user/generate-auth-url.ts +0 -33
- package/src/user/generate-random-state.ts +0 -16
- package/src/user/index.ts +0 -2
- package/src/user/user.ts +0 -1234
- package/src/utils/collectKeyValues.ts +0 -14
- package/src/utils/render.ts +0 -93
- package/src/whoami.ts +0 -135
- package/src/worker.ts +0 -279
- package/src/yargs-types.ts +0 -37
- package/src/zones.ts +0 -191
package/src/proxy.ts
DELETED
|
@@ -1,694 +0,0 @@
|
|
|
1
|
-
import { createServer as createHttpServer } from "node:http";
|
|
2
|
-
import { connect } from "node:http2";
|
|
3
|
-
import { createServer as createHttpsServer } from "node:https";
|
|
4
|
-
import https from "node:https";
|
|
5
|
-
import { networkInterfaces } from "node:os";
|
|
6
|
-
import { createHttpTerminator } from "http-terminator";
|
|
7
|
-
import { useEffect, useRef, useState } from "react";
|
|
8
|
-
import serveStatic from "serve-static";
|
|
9
|
-
import { getHttpsOptions } from "./https-options";
|
|
10
|
-
import { logger } from "./logger";
|
|
11
|
-
import { getAccessToken } from "./user/access";
|
|
12
|
-
import type { CfPreviewToken } from "./create-worker-preview";
|
|
13
|
-
import type { HttpTerminator } from "http-terminator";
|
|
14
|
-
import type {
|
|
15
|
-
IncomingHttpHeaders,
|
|
16
|
-
RequestListener,
|
|
17
|
-
IncomingMessage,
|
|
18
|
-
ServerResponse,
|
|
19
|
-
Server as HttpServer,
|
|
20
|
-
} from "node:http";
|
|
21
|
-
import type { ClientHttp2Session, ServerHttp2Stream } from "node:http2";
|
|
22
|
-
import type { Server as HttpsServer } from "node:https";
|
|
23
|
-
import type { Duplex, Writable } from "node:stream";
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* `usePreviewServer` is a React hook that creates a local development
|
|
27
|
-
* server that can be used to develop a Worker.
|
|
28
|
-
*
|
|
29
|
-
* When we run `wrangler dev`, we start by uploading the compiled worker
|
|
30
|
-
* to the preview service, which responds with a preview token.
|
|
31
|
-
* (see `useWorker`/`createWorker` for details.)
|
|
32
|
-
* We can then use that token to connect to the preview server for a
|
|
33
|
-
* great local development experience. Further, as we change the worker,
|
|
34
|
-
* we can update the preview token transparently without having to restart
|
|
35
|
-
* the development server.
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
/** Rewrite request headers to add the preview token. */
|
|
39
|
-
function addCfPreviewTokenHeader(
|
|
40
|
-
headers: IncomingHttpHeaders,
|
|
41
|
-
previewTokenValue: string
|
|
42
|
-
) {
|
|
43
|
-
headers["cf-workers-preview-token"] = previewTokenValue;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export async function addCfAccessToken(
|
|
47
|
-
headers: IncomingHttpHeaders,
|
|
48
|
-
domain: string,
|
|
49
|
-
accessTokenRef: { current: string | undefined | null }
|
|
50
|
-
) {
|
|
51
|
-
if (accessTokenRef.current === null) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (typeof accessTokenRef.current === "string") {
|
|
55
|
-
headers[
|
|
56
|
-
"cookie"
|
|
57
|
-
] = `${headers["cookie"]};CF_Authorization=${accessTokenRef.current}`;
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const token = await getAccessToken(domain);
|
|
61
|
-
accessTokenRef.current = token;
|
|
62
|
-
if (token)
|
|
63
|
-
headers[
|
|
64
|
-
"cookie"
|
|
65
|
-
] = `${headers["cookie"]};CF_Authorization=${accessTokenRef.current}`;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Rewrite references in request headers
|
|
69
|
-
* from the preview host to the local host.
|
|
70
|
-
*/
|
|
71
|
-
function rewriteRemoteHostToLocalHostInHeaders(
|
|
72
|
-
headers: IncomingHttpHeaders,
|
|
73
|
-
remoteHost: string,
|
|
74
|
-
localPort: number,
|
|
75
|
-
localProtocol: "https" | "http"
|
|
76
|
-
) {
|
|
77
|
-
for (const [name, value] of Object.entries(headers)) {
|
|
78
|
-
// Rewrite the remote host to the local host.
|
|
79
|
-
if (typeof value === "string" && value.includes(remoteHost)) {
|
|
80
|
-
headers[name] = value
|
|
81
|
-
.replaceAll(
|
|
82
|
-
`https://${remoteHost}`,
|
|
83
|
-
`${localProtocol}://localhost:${localPort}`
|
|
84
|
-
)
|
|
85
|
-
.replaceAll(remoteHost, `localhost:${localPort}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function writeHead(
|
|
91
|
-
socket: Writable,
|
|
92
|
-
res: Pick<
|
|
93
|
-
IncomingMessage,
|
|
94
|
-
"httpVersion" | "statusCode" | "statusMessage" | "headers"
|
|
95
|
-
>
|
|
96
|
-
) {
|
|
97
|
-
socket.write(
|
|
98
|
-
`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}\r\n`
|
|
99
|
-
);
|
|
100
|
-
for (const [key, values] of Object.entries(res.headers)) {
|
|
101
|
-
if (Array.isArray(values)) {
|
|
102
|
-
for (const value of values) socket.write(`${key}: ${value}\r\n`);
|
|
103
|
-
} else {
|
|
104
|
-
socket.write(`${key}: ${values}\r\n`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
socket.write("\r\n");
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
type PreviewProxy = {
|
|
111
|
-
server: HttpServer | HttpsServer;
|
|
112
|
-
terminator: HttpTerminator;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
export async function startPreviewServer({
|
|
116
|
-
previewToken,
|
|
117
|
-
assetDirectory,
|
|
118
|
-
localProtocol,
|
|
119
|
-
localPort: port,
|
|
120
|
-
ip,
|
|
121
|
-
onReady,
|
|
122
|
-
}: {
|
|
123
|
-
previewToken: CfPreviewToken;
|
|
124
|
-
assetDirectory: string | undefined;
|
|
125
|
-
localProtocol: "https" | "http";
|
|
126
|
-
localPort: number;
|
|
127
|
-
ip: string;
|
|
128
|
-
onReady: ((readyIp: string, readyPort: number) => void) | undefined;
|
|
129
|
-
}) {
|
|
130
|
-
try {
|
|
131
|
-
const abortController = new AbortController();
|
|
132
|
-
|
|
133
|
-
const server = await createProxyServer(localProtocol);
|
|
134
|
-
const proxy = {
|
|
135
|
-
server,
|
|
136
|
-
terminator: createHttpTerminator({
|
|
137
|
-
server,
|
|
138
|
-
gracefulTerminationTimeout: 0,
|
|
139
|
-
}),
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
// We have a token. Let's proxy requests to the preview end point.
|
|
143
|
-
const streamBufferRef = { current: [] };
|
|
144
|
-
const requestResponseBufferRef = { current: [] };
|
|
145
|
-
const accessTokenRef = { current: undefined };
|
|
146
|
-
const cleanupListeners = configureProxyServer({
|
|
147
|
-
proxy,
|
|
148
|
-
previewToken,
|
|
149
|
-
streamBufferRef,
|
|
150
|
-
requestResponseBufferRef,
|
|
151
|
-
retryServerSetup: () => {}, // no-op outside of React
|
|
152
|
-
assetDirectory,
|
|
153
|
-
localProtocol,
|
|
154
|
-
port,
|
|
155
|
-
accessTokenRef,
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
await waitForPortToBeAvailable(port, {
|
|
159
|
-
retryPeriod: 200,
|
|
160
|
-
timeout: 2000,
|
|
161
|
-
abortSignal: abortController.signal,
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
proxy.server.on("listening", () => {
|
|
165
|
-
const address = proxy.server.address();
|
|
166
|
-
const usedPort =
|
|
167
|
-
address && typeof address === "object" ? address.port : port;
|
|
168
|
-
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`);
|
|
169
|
-
const accessibleHosts = ip !== "0.0.0.0" ? [ip] : getAccessibleHosts();
|
|
170
|
-
for (const accessibleHost of accessibleHosts) {
|
|
171
|
-
logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`);
|
|
172
|
-
}
|
|
173
|
-
onReady?.(ip, usedPort);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
proxy.server.listen(port, ip);
|
|
177
|
-
return {
|
|
178
|
-
stop: () => {
|
|
179
|
-
abortController.abort();
|
|
180
|
-
cleanupListeners?.forEach((cleanup) => cleanup());
|
|
181
|
-
},
|
|
182
|
-
};
|
|
183
|
-
} catch (err) {
|
|
184
|
-
if ((err as { code: string }).code !== "ABORT_ERR") {
|
|
185
|
-
logger.error(`Failed to start server: ${err}`);
|
|
186
|
-
}
|
|
187
|
-
logger.error("Failed to create proxy server:", err);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export function usePreviewServer({
|
|
192
|
-
previewToken,
|
|
193
|
-
assetDirectory,
|
|
194
|
-
localProtocol,
|
|
195
|
-
localPort: port,
|
|
196
|
-
ip,
|
|
197
|
-
}: {
|
|
198
|
-
previewToken: CfPreviewToken | undefined;
|
|
199
|
-
assetDirectory: string | undefined;
|
|
200
|
-
localProtocol: "https" | "http";
|
|
201
|
-
localPort: number;
|
|
202
|
-
ip: string;
|
|
203
|
-
}) {
|
|
204
|
-
/** Creates an HTTP/1 proxy that sends requests over HTTP/2. */
|
|
205
|
-
const [proxy, setProxy] = useState<PreviewProxy>();
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Create the instance of the local proxy server that will pass on
|
|
209
|
-
* requests to the preview worker.
|
|
210
|
-
*/
|
|
211
|
-
useEffect(() => {
|
|
212
|
-
if (proxy === undefined) {
|
|
213
|
-
createProxyServer(localProtocol)
|
|
214
|
-
.then((server) => {
|
|
215
|
-
setProxy({
|
|
216
|
-
server,
|
|
217
|
-
terminator: createHttpTerminator({
|
|
218
|
-
server,
|
|
219
|
-
gracefulTerminationTimeout: 0,
|
|
220
|
-
}),
|
|
221
|
-
});
|
|
222
|
-
})
|
|
223
|
-
.catch(async (err) => {
|
|
224
|
-
logger.error("Failed to create proxy server:", err);
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
}, [proxy, localProtocol]);
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* When we're not connected / getting a fresh token on changes,
|
|
231
|
-
* we'd like to buffer streams/requests until we're connected.
|
|
232
|
-
* Once connected, we can flush the buffered streams/requests.
|
|
233
|
-
* streamBufferRef is used to buffer http/2 streams, while
|
|
234
|
-
* requestResponseBufferRef is used to buffer http/1 requests.
|
|
235
|
-
*/
|
|
236
|
-
const streamBufferRef = useRef<
|
|
237
|
-
{ stream: ServerHttp2Stream; headers: IncomingHttpHeaders }[]
|
|
238
|
-
>([]);
|
|
239
|
-
const requestResponseBufferRef = useRef<
|
|
240
|
-
{ request: IncomingMessage; response: ServerResponse }[]
|
|
241
|
-
>([]);
|
|
242
|
-
const accessTokenRef = useRef<string | undefined | null>(undefined);
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* The session doesn't last forever, and will eventually drop
|
|
246
|
-
* (usually within 5-15 minutes). When that happens, we simply
|
|
247
|
-
* restart the effect, effectively restarting the server. We use
|
|
248
|
-
* a state sigil as an effect dependency to do so.
|
|
249
|
-
*/
|
|
250
|
-
const [retryServerSetupSigil, setRetryServerSetupSigil] = useState<number>(0);
|
|
251
|
-
function retryServerSetup() {
|
|
252
|
-
setRetryServerSetupSigil((x) => x + 1);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
useEffect(() => {
|
|
256
|
-
const cleanupListeners = configureProxyServer({
|
|
257
|
-
proxy,
|
|
258
|
-
previewToken,
|
|
259
|
-
streamBufferRef,
|
|
260
|
-
requestResponseBufferRef,
|
|
261
|
-
retryServerSetup,
|
|
262
|
-
assetDirectory,
|
|
263
|
-
localProtocol,
|
|
264
|
-
port,
|
|
265
|
-
accessTokenRef,
|
|
266
|
-
});
|
|
267
|
-
return () => {
|
|
268
|
-
cleanupListeners?.forEach((cleanup) => cleanup());
|
|
269
|
-
};
|
|
270
|
-
}, [
|
|
271
|
-
previewToken,
|
|
272
|
-
assetDirectory,
|
|
273
|
-
port,
|
|
274
|
-
localProtocol,
|
|
275
|
-
proxy,
|
|
276
|
-
// We use a state value as a sigil to trigger reconnecting the server.
|
|
277
|
-
// It's not used inside the effect, so react-hooks/exhaustive-deps
|
|
278
|
-
// doesn't complain if it's not included in the dependency array.
|
|
279
|
-
// But its presence is critical, so Do NOT remove it from the dependency list.
|
|
280
|
-
retryServerSetupSigil,
|
|
281
|
-
]);
|
|
282
|
-
|
|
283
|
-
// Start/stop the server whenever the
|
|
284
|
-
// containing component is mounted/unmounted.
|
|
285
|
-
useEffect(() => {
|
|
286
|
-
const abortController = new AbortController();
|
|
287
|
-
if (proxy === undefined) {
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
waitForPortToBeAvailable(port, {
|
|
292
|
-
retryPeriod: 200,
|
|
293
|
-
timeout: 2000,
|
|
294
|
-
abortSignal: abortController.signal,
|
|
295
|
-
})
|
|
296
|
-
.then(() => {
|
|
297
|
-
proxy.server.on("listening", () => {
|
|
298
|
-
const address = proxy.server.address();
|
|
299
|
-
const usedPort =
|
|
300
|
-
address && typeof address === "object" ? address.port : port;
|
|
301
|
-
logger.log(`⬣ Listening at ${localProtocol}://${ip}:${usedPort}`);
|
|
302
|
-
const accessibleHosts =
|
|
303
|
-
ip !== "0.0.0.0" ? [ip] : getAccessibleHosts();
|
|
304
|
-
for (const accessibleHost of accessibleHosts) {
|
|
305
|
-
logger.log(`- ${localProtocol}://${accessibleHost}:${usedPort}`);
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
proxy.server.listen(port, ip);
|
|
309
|
-
})
|
|
310
|
-
.catch((err) => {
|
|
311
|
-
if ((err as { code: string }).code !== "ABORT_ERR") {
|
|
312
|
-
logger.error(`Failed to start server: ${err}`);
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
return () => {
|
|
317
|
-
abortController.abort();
|
|
318
|
-
// Running `proxy.server.close()` does not close open connections, preventing the process from exiting.
|
|
319
|
-
// So we use this `terminator` to close all the connections and force the server to shutdown.
|
|
320
|
-
proxy.terminator
|
|
321
|
-
.terminate()
|
|
322
|
-
.catch(() => logger.error("Failed to terminate the proxy server."));
|
|
323
|
-
};
|
|
324
|
-
}, [port, ip, proxy, localProtocol]);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function configureProxyServer({
|
|
328
|
-
proxy,
|
|
329
|
-
previewToken,
|
|
330
|
-
streamBufferRef,
|
|
331
|
-
requestResponseBufferRef,
|
|
332
|
-
retryServerSetup,
|
|
333
|
-
port,
|
|
334
|
-
localProtocol,
|
|
335
|
-
assetDirectory,
|
|
336
|
-
accessTokenRef,
|
|
337
|
-
}: {
|
|
338
|
-
proxy: PreviewProxy | undefined;
|
|
339
|
-
previewToken: CfPreviewToken | undefined;
|
|
340
|
-
// normally the type of streamBufferRef should be
|
|
341
|
-
// React.MutableRefObject<T>, but we don't want to require react
|
|
342
|
-
streamBufferRef: {
|
|
343
|
-
current: { stream: ServerHttp2Stream; headers: IncomingHttpHeaders }[];
|
|
344
|
-
};
|
|
345
|
-
// normally the type of requestResponseBufferRef should be
|
|
346
|
-
// React.MutableRefObject<T>, but we don't want to require react
|
|
347
|
-
requestResponseBufferRef: {
|
|
348
|
-
current: { request: IncomingMessage; response: ServerResponse }[];
|
|
349
|
-
};
|
|
350
|
-
retryServerSetup: () => void;
|
|
351
|
-
port: number;
|
|
352
|
-
localProtocol: "https" | "http";
|
|
353
|
-
assetDirectory: string | undefined;
|
|
354
|
-
accessTokenRef: { current: string | null | undefined };
|
|
355
|
-
}) {
|
|
356
|
-
if (proxy === undefined) {
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
// If we don't have a token, that means either we're just starting up,
|
|
361
|
-
// or we're refreshing the token.
|
|
362
|
-
if (!previewToken) {
|
|
363
|
-
const cleanupListeners: (() => void)[] = [];
|
|
364
|
-
const bufferStream = (
|
|
365
|
-
stream: ServerHttp2Stream,
|
|
366
|
-
headers: IncomingHttpHeaders
|
|
367
|
-
) => {
|
|
368
|
-
// store the stream in a buffer so we can replay it later
|
|
369
|
-
streamBufferRef.current.push({ stream, headers });
|
|
370
|
-
};
|
|
371
|
-
proxy.server.on("stream", bufferStream);
|
|
372
|
-
cleanupListeners.push(() => proxy.server.off("stream", bufferStream));
|
|
373
|
-
|
|
374
|
-
const bufferRequestResponse = (
|
|
375
|
-
request: IncomingMessage,
|
|
376
|
-
response: ServerResponse
|
|
377
|
-
) => {
|
|
378
|
-
// store the request and response in a buffer so we can replay it later
|
|
379
|
-
requestResponseBufferRef.current.push({ request, response });
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
proxy.server.on("request", bufferRequestResponse);
|
|
383
|
-
cleanupListeners.push(() =>
|
|
384
|
-
proxy.server.off("request", bufferRequestResponse)
|
|
385
|
-
);
|
|
386
|
-
return cleanupListeners;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// We have a token. Let's proxy requests to the preview end point.
|
|
390
|
-
const cleanupListeners: (() => void)[] = [];
|
|
391
|
-
|
|
392
|
-
// create a ClientHttp2Session
|
|
393
|
-
logger.debug("PREVIEW URL:", `https://${previewToken.host}`);
|
|
394
|
-
const remote = connect(`https://${previewToken.host}`);
|
|
395
|
-
cleanupListeners.push(() => remote.destroy());
|
|
396
|
-
|
|
397
|
-
// As mentioned above, the session may die at any point,
|
|
398
|
-
// so we need to restart the effect.
|
|
399
|
-
remote.on("close", retryServerSetup);
|
|
400
|
-
cleanupListeners.push(() => remote.off("close", retryServerSetup));
|
|
401
|
-
|
|
402
|
-
/** HTTP/2 -> HTTP/2 */
|
|
403
|
-
const handleStream = createStreamHandler(
|
|
404
|
-
previewToken,
|
|
405
|
-
remote,
|
|
406
|
-
port,
|
|
407
|
-
localProtocol,
|
|
408
|
-
accessTokenRef
|
|
409
|
-
);
|
|
410
|
-
proxy.server.on("stream", handleStream);
|
|
411
|
-
cleanupListeners.push(() => proxy.server.off("stream", handleStream));
|
|
412
|
-
|
|
413
|
-
// flush and replay buffered streams
|
|
414
|
-
streamBufferRef.current.forEach(
|
|
415
|
-
(buffer: { stream: ServerHttp2Stream; headers: IncomingHttpHeaders }) =>
|
|
416
|
-
handleStream(buffer.stream, buffer.headers)
|
|
417
|
-
);
|
|
418
|
-
streamBufferRef.current = [];
|
|
419
|
-
|
|
420
|
-
/** HTTP/1 -> HTTP/2 */
|
|
421
|
-
const handleRequest: RequestListener = async (
|
|
422
|
-
message: IncomingMessage,
|
|
423
|
-
response: ServerResponse
|
|
424
|
-
) => {
|
|
425
|
-
const { httpVersionMajor, headers, method, url } = message;
|
|
426
|
-
if (httpVersionMajor >= 2) {
|
|
427
|
-
return; // Already handled by the "stream" event.
|
|
428
|
-
}
|
|
429
|
-
await addCfAccessToken(headers, previewToken.host, accessTokenRef);
|
|
430
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
|
431
|
-
headers[":method"] = method;
|
|
432
|
-
headers[":path"] = url;
|
|
433
|
-
headers[":authority"] = previewToken.host;
|
|
434
|
-
headers[":scheme"] = "https";
|
|
435
|
-
for (const name of Object.keys(headers)) {
|
|
436
|
-
if (HTTP1_HEADERS.has(name.toLowerCase())) {
|
|
437
|
-
delete headers[name];
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
const request = message.pipe(remote.request(headers));
|
|
441
|
-
logger.debug(
|
|
442
|
-
"WORKER REQUEST",
|
|
443
|
-
new Date().toLocaleTimeString(),
|
|
444
|
-
method,
|
|
445
|
-
url
|
|
446
|
-
);
|
|
447
|
-
logger.debug("HEADERS", JSON.stringify(headers, null, 2));
|
|
448
|
-
logger.debug("PREVIEW TOKEN", previewToken);
|
|
449
|
-
|
|
450
|
-
request.on("response", (responseHeaders) => {
|
|
451
|
-
const status = responseHeaders[":status"] ?? 500;
|
|
452
|
-
// log all requests to terminal
|
|
453
|
-
logger.log(new Date().toLocaleTimeString(), method, url, status);
|
|
454
|
-
|
|
455
|
-
rewriteRemoteHostToLocalHostInHeaders(
|
|
456
|
-
responseHeaders,
|
|
457
|
-
previewToken.host,
|
|
458
|
-
port,
|
|
459
|
-
localProtocol
|
|
460
|
-
);
|
|
461
|
-
for (const name of Object.keys(responseHeaders)) {
|
|
462
|
-
if (name.startsWith(":")) {
|
|
463
|
-
delete responseHeaders[name];
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
response.writeHead(status, responseHeaders);
|
|
467
|
-
request.pipe(response, { end: true });
|
|
468
|
-
});
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
// If an asset path is defined, check the file system
|
|
472
|
-
// for a file first and serve if it exists.
|
|
473
|
-
const actualHandleRequest = assetDirectory
|
|
474
|
-
? createHandleAssetsRequest(assetDirectory, handleRequest)
|
|
475
|
-
: handleRequest;
|
|
476
|
-
|
|
477
|
-
proxy.server.on("request", actualHandleRequest);
|
|
478
|
-
cleanupListeners.push(() => proxy.server.off("request", actualHandleRequest));
|
|
479
|
-
|
|
480
|
-
// flush and replay buffered requests
|
|
481
|
-
requestResponseBufferRef.current.forEach(
|
|
482
|
-
({
|
|
483
|
-
request,
|
|
484
|
-
response,
|
|
485
|
-
}: {
|
|
486
|
-
request: IncomingMessage;
|
|
487
|
-
response: ServerResponse;
|
|
488
|
-
}) => actualHandleRequest(request, response)
|
|
489
|
-
);
|
|
490
|
-
requestResponseBufferRef.current = [];
|
|
491
|
-
|
|
492
|
-
/** HTTP/1 -> WebSocket (over HTTP/1) */
|
|
493
|
-
const handleUpgrade = async (
|
|
494
|
-
originalMessage: IncomingMessage,
|
|
495
|
-
originalSocket: Duplex,
|
|
496
|
-
originalHead: Buffer
|
|
497
|
-
) => {
|
|
498
|
-
const { headers, method, url } = originalMessage;
|
|
499
|
-
await addCfAccessToken(headers, previewToken.host, accessTokenRef);
|
|
500
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
|
501
|
-
headers["host"] = previewToken.host;
|
|
502
|
-
|
|
503
|
-
if (originalHead?.byteLength) originalSocket.unshift(originalHead);
|
|
504
|
-
|
|
505
|
-
const runtimeRequest = https.request(
|
|
506
|
-
{
|
|
507
|
-
hostname: previewToken.host,
|
|
508
|
-
path: url,
|
|
509
|
-
method,
|
|
510
|
-
headers,
|
|
511
|
-
},
|
|
512
|
-
(runtimeResponse) => {
|
|
513
|
-
if (!(runtimeResponse as { upgrade?: boolean }).upgrade) {
|
|
514
|
-
writeHead(originalSocket, runtimeResponse);
|
|
515
|
-
runtimeResponse.pipe(originalSocket);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
);
|
|
519
|
-
|
|
520
|
-
runtimeRequest.on(
|
|
521
|
-
"upgrade",
|
|
522
|
-
(runtimeResponse, runtimeSocket, runtimeHead) => {
|
|
523
|
-
if (runtimeHead?.byteLength) runtimeSocket.unshift(runtimeHead);
|
|
524
|
-
writeHead(originalSocket, {
|
|
525
|
-
httpVersion: "1.1",
|
|
526
|
-
statusCode: 101,
|
|
527
|
-
statusMessage: "Switching Protocols",
|
|
528
|
-
headers: runtimeResponse.headers,
|
|
529
|
-
});
|
|
530
|
-
runtimeSocket.pipe(originalSocket).pipe(runtimeSocket);
|
|
531
|
-
}
|
|
532
|
-
);
|
|
533
|
-
originalMessage.pipe(runtimeRequest);
|
|
534
|
-
};
|
|
535
|
-
|
|
536
|
-
proxy.server.on("upgrade", handleUpgrade);
|
|
537
|
-
cleanupListeners.push(() => proxy.server.off("upgrade", handleUpgrade));
|
|
538
|
-
return cleanupListeners;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
function createHandleAssetsRequest(
|
|
542
|
-
assetDirectory: string,
|
|
543
|
-
handleRequest: RequestListener
|
|
544
|
-
) {
|
|
545
|
-
const handleAsset = serveStatic(assetDirectory, {
|
|
546
|
-
cacheControl: false,
|
|
547
|
-
});
|
|
548
|
-
return (request: IncomingMessage, response: ServerResponse) => {
|
|
549
|
-
handleAsset(request, response, () => {
|
|
550
|
-
handleRequest(request, response);
|
|
551
|
-
});
|
|
552
|
-
};
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
/** A Set of headers we want to remove from HTTP/1 requests. */
|
|
556
|
-
const HTTP1_HEADERS = new Set([
|
|
557
|
-
"host",
|
|
558
|
-
"connection",
|
|
559
|
-
"upgrade",
|
|
560
|
-
"keep-alive",
|
|
561
|
-
"proxy-connection",
|
|
562
|
-
"transfer-encoding",
|
|
563
|
-
"http2-settings",
|
|
564
|
-
]);
|
|
565
|
-
|
|
566
|
-
async function createProxyServer(
|
|
567
|
-
localProtocol: "https" | "http"
|
|
568
|
-
): Promise<HttpServer | HttpsServer> {
|
|
569
|
-
const server: HttpServer | HttpsServer =
|
|
570
|
-
localProtocol === "https"
|
|
571
|
-
? createHttpsServer(await getHttpsOptions())
|
|
572
|
-
: createHttpServer();
|
|
573
|
-
|
|
574
|
-
return server
|
|
575
|
-
.on("upgrade", (req) => {
|
|
576
|
-
// log all websocket connections
|
|
577
|
-
logger.log(
|
|
578
|
-
new Date().toLocaleTimeString(),
|
|
579
|
-
req.method,
|
|
580
|
-
req.url,
|
|
581
|
-
101,
|
|
582
|
-
"(WebSocket)"
|
|
583
|
-
);
|
|
584
|
-
})
|
|
585
|
-
.on("error", (err) => {
|
|
586
|
-
// log all connection errors
|
|
587
|
-
logger.error(new Date().toLocaleTimeString(), err);
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
function createStreamHandler(
|
|
592
|
-
previewToken: CfPreviewToken,
|
|
593
|
-
remote: ClientHttp2Session,
|
|
594
|
-
localPort: number,
|
|
595
|
-
localProtocol: "https" | "http",
|
|
596
|
-
accessTokenRef: { current: string | undefined | null }
|
|
597
|
-
) {
|
|
598
|
-
return async function handleStream(
|
|
599
|
-
stream: ServerHttp2Stream,
|
|
600
|
-
headers: IncomingHttpHeaders
|
|
601
|
-
) {
|
|
602
|
-
await addCfAccessToken(headers, previewToken.host, accessTokenRef);
|
|
603
|
-
addCfPreviewTokenHeader(headers, previewToken.value);
|
|
604
|
-
headers[":authority"] = previewToken.host;
|
|
605
|
-
const request = stream.pipe(remote.request(headers));
|
|
606
|
-
request.on("response", (responseHeaders: IncomingHttpHeaders) => {
|
|
607
|
-
rewriteRemoteHostToLocalHostInHeaders(
|
|
608
|
-
responseHeaders,
|
|
609
|
-
previewToken.host,
|
|
610
|
-
localPort,
|
|
611
|
-
localProtocol
|
|
612
|
-
);
|
|
613
|
-
stream.respond(responseHeaders);
|
|
614
|
-
request.pipe(stream, { end: true });
|
|
615
|
-
});
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* A helper function that waits for a port to be available.
|
|
621
|
-
*/
|
|
622
|
-
export async function waitForPortToBeAvailable(
|
|
623
|
-
port: number,
|
|
624
|
-
options: { retryPeriod: number; timeout: number; abortSignal: AbortSignal }
|
|
625
|
-
): Promise<void> {
|
|
626
|
-
return new Promise((resolve, reject) => {
|
|
627
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
628
|
-
options.abortSignal.addEventListener("abort", () => {
|
|
629
|
-
const abortError = new Error("waitForPortToBeAvailable() aborted");
|
|
630
|
-
(abortError as Error & { code: string }).code = "ABORT_ERR";
|
|
631
|
-
doReject(abortError);
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
const timeout = setTimeout(() => {
|
|
635
|
-
doReject(new Error(`Timed out waiting for port ${port}`));
|
|
636
|
-
}, options.timeout);
|
|
637
|
-
|
|
638
|
-
const interval = setInterval(checkPort, options.retryPeriod);
|
|
639
|
-
checkPort();
|
|
640
|
-
|
|
641
|
-
function doResolve() {
|
|
642
|
-
clearTimeout(timeout);
|
|
643
|
-
clearInterval(interval);
|
|
644
|
-
resolve();
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
function doReject(err: unknown) {
|
|
648
|
-
clearInterval(interval);
|
|
649
|
-
clearTimeout(timeout);
|
|
650
|
-
reject(err);
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
function checkPort() {
|
|
654
|
-
if (port === 0) {
|
|
655
|
-
doResolve();
|
|
656
|
-
return;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// Testing whether a port is 'available' involves simply
|
|
660
|
-
// trying to make a server listen on that port, and retrying
|
|
661
|
-
// until it succeeds.
|
|
662
|
-
const server = createHttpServer();
|
|
663
|
-
const terminator = createHttpTerminator({
|
|
664
|
-
server,
|
|
665
|
-
gracefulTerminationTimeout: 0, // default 1000
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
server.on("error", (err) => {
|
|
669
|
-
// @ts-expect-error non standard property on Error
|
|
670
|
-
if (err.code !== "EADDRINUSE") {
|
|
671
|
-
doReject(err);
|
|
672
|
-
}
|
|
673
|
-
});
|
|
674
|
-
server.listen(port, () =>
|
|
675
|
-
terminator
|
|
676
|
-
.terminate()
|
|
677
|
-
.then(doResolve, () =>
|
|
678
|
-
logger.error("Failed to terminate the port checker.")
|
|
679
|
-
)
|
|
680
|
-
);
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
function getAccessibleHosts(): string[] {
|
|
686
|
-
const hosts: string[] = [];
|
|
687
|
-
Object.values(networkInterfaces()).forEach((net) => {
|
|
688
|
-
net?.forEach(({ family, address }) => {
|
|
689
|
-
// @ts-expect-error the `family` property is numeric as of Node.js 18.0.0
|
|
690
|
-
if (family === "IPv4" || family === 4) hosts.push(address);
|
|
691
|
-
});
|
|
692
|
-
});
|
|
693
|
-
return hosts;
|
|
694
|
-
}
|