@driggsby/deploy 0.0.1 → 0.2.1
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 +117 -0
- package/README.md +64 -2
- package/bin/driggsby-deploy.js +2 -0
- package/dist/base-url.d.ts +3 -0
- package/dist/base-url.js +46 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +16 -0
- package/dist/client.d.ts +52 -0
- package/dist/client.js +292 -0
- package/dist/deploy.d.ts +28 -0
- package/dist/deploy.js +188 -0
- package/dist/entrypoint.d.ts +8 -0
- package/dist/entrypoint.js +121 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +27 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +17 -0
- package/dist/limits.d.ts +8 -0
- package/dist/limits.js +28 -0
- package/dist/manifest.d.ts +21 -0
- package/dist/manifest.js +181 -0
- package/dist/project-config.d.ts +8 -0
- package/dist/project-config.js +145 -0
- package/dist/terminal-text.d.ts +4 -0
- package/dist/terminal-text.js +106 -0
- package/package.json +30 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable patent license to make, have made, use,
|
|
71
|
+
offer to sell, sell, import, and otherwise transfer the Work.
|
|
72
|
+
|
|
73
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
74
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
75
|
+
Source or Object form, provided that You meet the following conditions:
|
|
76
|
+
|
|
77
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
78
|
+
this License; and
|
|
79
|
+
|
|
80
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
81
|
+
You changed the files; and
|
|
82
|
+
|
|
83
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
84
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
85
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
86
|
+
of the Derivative Works; and
|
|
87
|
+
|
|
88
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
89
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
90
|
+
attribution notices contained within such NOTICE file.
|
|
91
|
+
|
|
92
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
93
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
94
|
+
Licensor shall be under the terms and conditions of this License.
|
|
95
|
+
|
|
96
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
97
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
98
|
+
for reasonable and customary use in describing the origin of the Work.
|
|
99
|
+
|
|
100
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
101
|
+
writing, Licensor provides the Work on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
102
|
+
CONDITIONS OF ANY KIND, either express or implied, including, without limitation,
|
|
103
|
+
any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
|
|
104
|
+
FITNESS FOR A PARTICULAR PURPOSE.
|
|
105
|
+
|
|
106
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
107
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
108
|
+
applicable law or agreed to in writing, shall any Contributor be liable to You
|
|
109
|
+
for damages, including any direct, indirect, special, incidental, or
|
|
110
|
+
consequential damages arising as a result of this License or out of the use or
|
|
111
|
+
inability to use the Work.
|
|
112
|
+
|
|
113
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
114
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
115
|
+
acceptance of support, warranty, indemnity, or other liability obligations.
|
|
116
|
+
However, in accepting such obligations, You may act only on Your own behalf and
|
|
117
|
+
on Your sole responsibility.
|
package/README.md
CHANGED
|
@@ -1,4 +1,66 @@
|
|
|
1
1
|
# @driggsby/deploy
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
The deploy protocol client for [Driggsby](https://driggsby.com) apps — small
|
|
4
|
+
static web apps, each served at its own `driggsby.dev` address, with live
|
|
5
|
+
data through the Driggsby SDK. Driggsby assigns every app's address at
|
|
6
|
+
first deploy: the readable name from `driggsby.json` plus a unique ending,
|
|
7
|
+
like `money-dash-x7k2qf.driggsby.dev`, saved back into `driggsby.json`.
|
|
8
|
+
|
|
9
|
+
Most people want the full CLI instead:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npx driggsby@latest login
|
|
13
|
+
npx driggsby@latest deploy
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This package exists for two narrower uses.
|
|
17
|
+
|
|
18
|
+
## One-command deploys in Node-only sandboxes
|
|
19
|
+
|
|
20
|
+
In an environment that can't run a browser sign-in but can hold a token in
|
|
21
|
+
`DRIGGSBY_TOKEN`:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npx @driggsby/deploy
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This reads `driggsby.json` in the current directory, deploys the `serve`
|
|
28
|
+
folder, and makes it live. A first deploy creates the app and saves its
|
|
29
|
+
assigned address back into `driggsby.json` — keep (commit) that updated
|
|
30
|
+
file, because it is the only record of the app's address; a checkout that
|
|
31
|
+
discards it makes the next deploy create a second app at a new address.
|
|
32
|
+
|
|
33
|
+
## As a library
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import {
|
|
37
|
+
collectDeployFiles,
|
|
38
|
+
deployProjectFiles,
|
|
39
|
+
readProjectConfig,
|
|
40
|
+
} from "@driggsby/deploy";
|
|
41
|
+
|
|
42
|
+
const projectDirectory = process.cwd();
|
|
43
|
+
const config = await readProjectConfig(projectDirectory);
|
|
44
|
+
const collected = await collectDeployFiles(config.serveDirectory);
|
|
45
|
+
const { outcome } = await deployProjectFiles(
|
|
46
|
+
{ baseUrl: "https://app.driggsby.com", token },
|
|
47
|
+
projectDirectory,
|
|
48
|
+
config.slug,
|
|
49
|
+
collected,
|
|
50
|
+
{ live: true },
|
|
51
|
+
);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`collectDeployFiles` walks the serve folder (skipping dotfiles, symlinks,
|
|
55
|
+
node_modules, and `driggsby.json`), hashes every file, and enforces the
|
|
56
|
+
deploy limits locally.
|
|
57
|
+
`deployProjectFiles` offers the manifest, creates the app on first deploy
|
|
58
|
+
(persisting the assigned slug into `driggsby.json` before any upload),
|
|
59
|
+
uploads only content Driggsby doesn't already have, and finalizes the
|
|
60
|
+
version.
|
|
61
|
+
|
|
62
|
+
Zero runtime dependencies; Node.js 18 or newer; macOS, Linux, and Windows.
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
Apache-2.0
|
package/dist/base-url.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The single home of the base-URL policy: which origin API requests (and
|
|
2
|
+
// the bearer token they carry) may travel to, and what DRIGGSBY_BASE_URL is
|
|
3
|
+
// allowed to override it with. The driggsby CLI wraps these exports rather
|
|
4
|
+
// than restating them — two copies of this policy would let an edit to one
|
|
5
|
+
// silently change where a credential may travel under the other.
|
|
6
|
+
import { DeployError } from "./errors.js";
|
|
7
|
+
export const PUBLIC_BASE_URL = "https://app.driggsby.com";
|
|
8
|
+
function isLoopbackHostname(hostname) {
|
|
9
|
+
return hostname === "localhost" || hostname === "127.0.0.1";
|
|
10
|
+
}
|
|
11
|
+
// Whether this base URL points at this machine. Plain http — and, in the
|
|
12
|
+
// driggsby CLI, the machine's saved sign-in — is acceptable only here (or at
|
|
13
|
+
// Driggsby itself). The input must already be a parseable URL.
|
|
14
|
+
export function isLoopbackBaseUrl(baseUrl) {
|
|
15
|
+
return isLoopbackHostname(new URL(baseUrl).hostname);
|
|
16
|
+
}
|
|
17
|
+
// Resolves the API origin from the environment. The token travels only over
|
|
18
|
+
// https, or plain http to a local dev server on this machine.
|
|
19
|
+
export function resolveBaseUrl(env) {
|
|
20
|
+
const raw = env.DRIGGSBY_BASE_URL?.trim();
|
|
21
|
+
if (raw === undefined || raw === "") {
|
|
22
|
+
return PUBLIC_BASE_URL;
|
|
23
|
+
}
|
|
24
|
+
const trimmed = raw.replace(/\/+$/, "");
|
|
25
|
+
let parsed;
|
|
26
|
+
try {
|
|
27
|
+
// Validate eagerly so a broken override fails with one clear message
|
|
28
|
+
// instead of a confusing fetch error later.
|
|
29
|
+
parsed = new URL(trimmed);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new DeployError(`DRIGGSBY_BASE_URL is set but is not a valid URL. Unset it to use\n${PUBLIC_BASE_URL}.`);
|
|
33
|
+
}
|
|
34
|
+
if (parsed.protocol !== "https:" &&
|
|
35
|
+
!(parsed.protocol === "http:" && isLoopbackHostname(parsed.hostname))) {
|
|
36
|
+
throw new DeployError(`DRIGGSBY_BASE_URL must be an https URL (or http on localhost). Unset it\nto use ${PUBLIC_BASE_URL}.`);
|
|
37
|
+
}
|
|
38
|
+
// API paths are appended to this value directly, so a query or fragment
|
|
39
|
+
// would end up in the middle of every request path. Checked on the raw
|
|
40
|
+
// string, not URL.search/.hash — a bare trailing "?" or "#" parses to an
|
|
41
|
+
// empty search/hash yet still corrupts every appended path.
|
|
42
|
+
if (trimmed.includes("?") || trimmed.includes("#")) {
|
|
43
|
+
throw new DeployError(`DRIGGSBY_BASE_URL can't include a query or fragment. Unset it to use\n${PUBLIC_BASE_URL}.`);
|
|
44
|
+
}
|
|
45
|
+
return trimmed;
|
|
46
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Side-effect entry for bin/driggsby-deploy.js. Kept separate from
|
|
2
|
+
// entrypoint.ts so tests exercise runDeployEntrypoint without triggering a
|
|
3
|
+
// real deploy on import.
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { runDeployEntrypoint } from "./entrypoint.js";
|
|
6
|
+
process.exitCode = await runDeployEntrypoint({
|
|
7
|
+
argv: process.argv.slice(2),
|
|
8
|
+
cwd: process.cwd(),
|
|
9
|
+
env: process.env,
|
|
10
|
+
out: (text) => {
|
|
11
|
+
process.stdout.write(text);
|
|
12
|
+
},
|
|
13
|
+
error: (text) => {
|
|
14
|
+
process.stderr.write(text);
|
|
15
|
+
},
|
|
16
|
+
});
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export interface DeployApi {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
token: string;
|
|
4
|
+
requestTimeoutMs?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface ManifestFile {
|
|
7
|
+
path: string;
|
|
8
|
+
sha256: string;
|
|
9
|
+
byteSize: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CreatedVersion {
|
|
12
|
+
versionId: string;
|
|
13
|
+
versionNumber: number;
|
|
14
|
+
appSlug: string;
|
|
15
|
+
missingBlobSha256s: string[];
|
|
16
|
+
}
|
|
17
|
+
export interface FinalizedVersion {
|
|
18
|
+
kind: "finalized";
|
|
19
|
+
appSlug: string;
|
|
20
|
+
versionNumber: number;
|
|
21
|
+
live: boolean;
|
|
22
|
+
url: string | null;
|
|
23
|
+
}
|
|
24
|
+
export interface BlobsMissing {
|
|
25
|
+
kind: "blobs-missing";
|
|
26
|
+
missingBlobSha256s: string[];
|
|
27
|
+
}
|
|
28
|
+
export interface VersionSummary {
|
|
29
|
+
versionId: string;
|
|
30
|
+
number: number;
|
|
31
|
+
status: "uploading" | "ready";
|
|
32
|
+
live: boolean;
|
|
33
|
+
fileCount: number;
|
|
34
|
+
totalBytes: number;
|
|
35
|
+
createdAt: string;
|
|
36
|
+
}
|
|
37
|
+
export interface VersionList {
|
|
38
|
+
appSlug: string;
|
|
39
|
+
url: string;
|
|
40
|
+
liveVersionNumber: number | null;
|
|
41
|
+
versions: VersionSummary[];
|
|
42
|
+
}
|
|
43
|
+
export interface CreatedApp {
|
|
44
|
+
appSlug: string;
|
|
45
|
+
url: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function createApp(api: DeployApi, baseName: string, appName?: string): Promise<CreatedApp>;
|
|
48
|
+
export declare function createVersion(api: DeployApi, slug: string, files: ManifestFile[], appName?: string): Promise<CreatedVersion>;
|
|
49
|
+
export declare function uploadBlob(api: DeployApi, sha256: string, bytes: Uint8Array, signal?: AbortSignal): Promise<void>;
|
|
50
|
+
export declare function finalizeVersion(api: DeployApi, versionId: string, live: boolean): Promise<FinalizedVersion | BlobsMissing>;
|
|
51
|
+
export declare function listVersions(api: DeployApi, slug: string): Promise<VersionList>;
|
|
52
|
+
export declare function setLiveVersion(api: DeployApi, slug: string, versionNumber: number): Promise<FinalizedVersion>;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// The deploy API protocol client: one function per endpoint, exact request
|
|
2
|
+
// shapes, fail-closed response parsing. Every request carries the app token,
|
|
3
|
+
// so redirects are always refused — the token goes to the configured API
|
|
4
|
+
// origin and nowhere else.
|
|
5
|
+
import { DeployApiError, DeployError } from "./errors.js";
|
|
6
|
+
import { slugProblem } from "./limits.js";
|
|
7
|
+
const JSON_REQUEST_TIMEOUT_MS = 30_000;
|
|
8
|
+
// Blob uploads carry up to 25 MB, which legitimately takes minutes on a
|
|
9
|
+
// slow link; the timeout exists so a stalled connection fails with a clear
|
|
10
|
+
// signal instead of hanging indefinitely.
|
|
11
|
+
const BLOB_UPLOAD_TIMEOUT_MS = 300_000;
|
|
12
|
+
// Every endpoint answers with a small JSON object; anything enormous is not
|
|
13
|
+
// a real Driggsby response. The cap bounds what gets parsed and surfaced,
|
|
14
|
+
// not what the network buffers — the request timeout bounds that. Same
|
|
15
|
+
// treatment as the CLI's sign-in client.
|
|
16
|
+
const MAX_RESPONSE_BODY_CHARS = 1_000_000;
|
|
17
|
+
const UNEXPECTED_RESPONSE_MESSAGE = "The Driggsby deploy API sent a response we didn't recognize. Please try\n" +
|
|
18
|
+
"again in a minute.";
|
|
19
|
+
// Creates the app itself. The server assigns the final slug — the readable
|
|
20
|
+
// base name passed here plus a unique ending — and every later deploy
|
|
21
|
+
// targets that assigned slug, so callers must persist it (in driggsby.json)
|
|
22
|
+
// before uploading anything. The request field is base_name, not slug: the
|
|
23
|
+
// two are different values on the two sides of this round trip, and the API
|
|
24
|
+
// keeps them visibly distinct.
|
|
25
|
+
export async function createApp(api, baseName, appName) {
|
|
26
|
+
const body = { base_name: baseName };
|
|
27
|
+
if (appName !== undefined) {
|
|
28
|
+
body.app_name = appName;
|
|
29
|
+
}
|
|
30
|
+
const parsed = await requestJson(api, "POST", "/deploy/apps", {
|
|
31
|
+
json: body,
|
|
32
|
+
expectedStatus: 201,
|
|
33
|
+
});
|
|
34
|
+
const appSlug = stringField(parsed, "app_slug");
|
|
35
|
+
// The assigned slug gets written into driggsby.json and reused as a URL
|
|
36
|
+
// path segment, so it is shape-validated like every other server field —
|
|
37
|
+
// a malformed answer must fail here, never land on disk.
|
|
38
|
+
if (slugProblem(appSlug) !== null) {
|
|
39
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
40
|
+
}
|
|
41
|
+
return { appSlug, url: stringField(parsed, "url") };
|
|
42
|
+
}
|
|
43
|
+
// app_name here is NOT a leftover of the old create-by-deploy flow: the
|
|
44
|
+
// server intentionally keeps rename-on-redeploy, applying a sent app_name
|
|
45
|
+
// to the existing app once the deploy is accepted.
|
|
46
|
+
export async function createVersion(api, slug, files, appName) {
|
|
47
|
+
const body = {
|
|
48
|
+
files: files.map((file) => ({ path: file.path, sha256: file.sha256, byte_size: file.byteSize })),
|
|
49
|
+
};
|
|
50
|
+
if (appName !== undefined) {
|
|
51
|
+
body.app_name = appName;
|
|
52
|
+
}
|
|
53
|
+
const parsed = await requestJson(api, "POST", `/deploy/apps/${encodeURIComponent(slug)}/versions`, {
|
|
54
|
+
json: body,
|
|
55
|
+
expectedStatus: 201,
|
|
56
|
+
});
|
|
57
|
+
const versionId = stringField(parsed, "version_id");
|
|
58
|
+
const versionNumber = integerField(parsed, "version_number");
|
|
59
|
+
const appSlug = stringField(parsed, "app_slug");
|
|
60
|
+
const missing = stringArrayField(parsed, "missing_blob_sha256s");
|
|
61
|
+
return { versionId, versionNumber, appSlug, missingBlobSha256s: missing };
|
|
62
|
+
}
|
|
63
|
+
export async function uploadBlob(api, sha256, bytes, signal) {
|
|
64
|
+
const path = `/deploy/blobs/${encodeURIComponent(sha256)}`;
|
|
65
|
+
if (signal === undefined) {
|
|
66
|
+
await requestJson(api, "PUT", path, { rawBody: bytes, expectedStatus: 200 });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// The caller's signal (the upload pool's) outlives this one request, so
|
|
70
|
+
// the forwarding listener is detached once the request settles —
|
|
71
|
+
// otherwise every upload in a large deploy would leave one listener
|
|
72
|
+
// behind on the shared signal for the rest of the round.
|
|
73
|
+
const scoped = new AbortController();
|
|
74
|
+
const forward = () => {
|
|
75
|
+
scoped.abort(signal.reason);
|
|
76
|
+
};
|
|
77
|
+
if (signal.aborted) {
|
|
78
|
+
forward();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
signal.addEventListener("abort", forward, { once: true });
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
await requestJson(api, "PUT", path, {
|
|
85
|
+
rawBody: bytes,
|
|
86
|
+
expectedStatus: 200,
|
|
87
|
+
signal: scoped.signal,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
signal.removeEventListener("abort", forward);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export async function finalizeVersion(api, versionId, live) {
|
|
95
|
+
const response = await rawRequest(api, "POST", `/deploy/versions/${encodeURIComponent(versionId)}/finalize`, {
|
|
96
|
+
json: live ? {} : { live: false },
|
|
97
|
+
});
|
|
98
|
+
if (response.status === 409) {
|
|
99
|
+
const parsed = await parseJsonBody(response);
|
|
100
|
+
if (typeof parsed === "object" && parsed !== null && errorCode(parsed) === "blobs_missing") {
|
|
101
|
+
return {
|
|
102
|
+
kind: "blobs-missing",
|
|
103
|
+
missingBlobSha256s: stringArrayField(parsed, "missing_blob_sha256s"),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
throw apiErrorFrom(response, parsed);
|
|
107
|
+
}
|
|
108
|
+
const parsed = await expectStatus(response, 200);
|
|
109
|
+
return parseFinalized(parsed);
|
|
110
|
+
}
|
|
111
|
+
export async function listVersions(api, slug) {
|
|
112
|
+
const parsed = await requestJson(api, "GET", `/deploy/apps/${encodeURIComponent(slug)}/versions`, {
|
|
113
|
+
expectedStatus: 200,
|
|
114
|
+
});
|
|
115
|
+
const rawVersions = parsed.versions;
|
|
116
|
+
if (!Array.isArray(rawVersions)) {
|
|
117
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
118
|
+
}
|
|
119
|
+
const liveVersionNumber = parsed.live_version_number;
|
|
120
|
+
return {
|
|
121
|
+
appSlug: stringField(parsed, "app_slug"),
|
|
122
|
+
url: stringField(parsed, "url"),
|
|
123
|
+
liveVersionNumber: typeof liveVersionNumber === "number" && Number.isSafeInteger(liveVersionNumber)
|
|
124
|
+
? liveVersionNumber
|
|
125
|
+
: null,
|
|
126
|
+
versions: rawVersions.map((raw) => {
|
|
127
|
+
if (typeof raw !== "object" || raw === null) {
|
|
128
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
129
|
+
}
|
|
130
|
+
const record = raw;
|
|
131
|
+
const status = record.status;
|
|
132
|
+
if (status !== "uploading" && status !== "ready") {
|
|
133
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
versionId: stringField(record, "version_id"),
|
|
137
|
+
number: integerField(record, "number"),
|
|
138
|
+
status,
|
|
139
|
+
live: record.live === true,
|
|
140
|
+
fileCount: integerField(record, "file_count"),
|
|
141
|
+
totalBytes: integerField(record, "total_bytes"),
|
|
142
|
+
createdAt: stringField(record, "created_at"),
|
|
143
|
+
};
|
|
144
|
+
}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
export async function setLiveVersion(api, slug, versionNumber) {
|
|
148
|
+
const parsed = await requestJson(api, "POST", `/deploy/apps/${encodeURIComponent(slug)}/live-version`, {
|
|
149
|
+
json: { version_number: versionNumber },
|
|
150
|
+
expectedStatus: 200,
|
|
151
|
+
});
|
|
152
|
+
return parseFinalized(parsed);
|
|
153
|
+
}
|
|
154
|
+
async function requestJson(api, method, path, options) {
|
|
155
|
+
const response = await rawRequest(api, method, path, options);
|
|
156
|
+
return expectStatus(response, options.expectedStatus ?? 200);
|
|
157
|
+
}
|
|
158
|
+
async function rawRequest(api, method, path, options) {
|
|
159
|
+
const headers = { authorization: `Bearer ${api.token}` };
|
|
160
|
+
let body;
|
|
161
|
+
if (options.json !== undefined) {
|
|
162
|
+
headers["content-type"] = "application/json";
|
|
163
|
+
body = JSON.stringify(options.json);
|
|
164
|
+
}
|
|
165
|
+
else if (options.rawBody !== undefined) {
|
|
166
|
+
headers["content-type"] = "application/octet-stream";
|
|
167
|
+
body = options.rawBody;
|
|
168
|
+
}
|
|
169
|
+
const timeoutMs = api.requestTimeoutMs ??
|
|
170
|
+
(options.rawBody === undefined ? JSON_REQUEST_TIMEOUT_MS : BLOB_UPLOAD_TIMEOUT_MS);
|
|
171
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
172
|
+
return fetch(`${api.baseUrl}${path}`, {
|
|
173
|
+
method,
|
|
174
|
+
headers,
|
|
175
|
+
...(body === undefined ? {} : { body }),
|
|
176
|
+
// The bearer token must never travel to a redirect target.
|
|
177
|
+
redirect: "error",
|
|
178
|
+
// A stalled connection fails as a TimeoutError instead of hanging; the
|
|
179
|
+
// caller's own signal (if any) can also cancel the request early.
|
|
180
|
+
signal: options.signal === undefined ? timeoutSignal : anyAbortSignal(options.signal, timeoutSignal),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// AbortSignal.any needs Node 20.3+, and these packages support Node 18, so
|
|
184
|
+
// combine the two signals by hand. Each combined signal lives for one
|
|
185
|
+
// request; the reasons are forwarded so a timeout still reads as a
|
|
186
|
+
// TimeoutError.
|
|
187
|
+
function anyAbortSignal(first, second) {
|
|
188
|
+
const controller = new AbortController();
|
|
189
|
+
for (const signal of [first, second]) {
|
|
190
|
+
if (signal.aborted) {
|
|
191
|
+
controller.abort(signal.reason);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
signal.addEventListener("abort", () => {
|
|
195
|
+
controller.abort(signal.reason);
|
|
196
|
+
}, { once: true, signal: controller.signal });
|
|
197
|
+
}
|
|
198
|
+
return controller.signal;
|
|
199
|
+
}
|
|
200
|
+
async function expectStatus(response, expected) {
|
|
201
|
+
const parsed = await parseJsonBody(response);
|
|
202
|
+
if (response.status !== expected) {
|
|
203
|
+
throw apiErrorFrom(response, parsed);
|
|
204
|
+
}
|
|
205
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
206
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
207
|
+
}
|
|
208
|
+
return parsed;
|
|
209
|
+
}
|
|
210
|
+
async function parseJsonBody(response) {
|
|
211
|
+
let text;
|
|
212
|
+
try {
|
|
213
|
+
text = await response.text();
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
// A body that never finishes arriving surfaces here as the request
|
|
217
|
+
// signal's TimeoutError. That is a stalled connection, not a malformed
|
|
218
|
+
// body, so it must propagate to the network-failure guidance instead of
|
|
219
|
+
// reading as an unrecognized response.
|
|
220
|
+
const name = error.name;
|
|
221
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
if (text.length > MAX_RESPONSE_BODY_CHARS) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
return JSON.parse(text);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function apiErrorFrom(response, parsed) {
|
|
237
|
+
const retryAfterSeconds = parseRetryAfterSeconds(response.headers.get("retry-after"));
|
|
238
|
+
if (typeof parsed === "object" && parsed !== null) {
|
|
239
|
+
const code = errorCode(parsed);
|
|
240
|
+
const description = parsed.error_description;
|
|
241
|
+
if (code !== null) {
|
|
242
|
+
return new DeployApiError(response.status, code, typeof description === "string" ? description : "", retryAfterSeconds);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return new DeployApiError(response.status, "unexpected_response", UNEXPECTED_RESPONSE_MESSAGE, retryAfterSeconds);
|
|
246
|
+
}
|
|
247
|
+
// Only the whole-seconds Retry-After form is honored; the HTTP-date form (or
|
|
248
|
+
// anything malformed) reads as "no header" and the caller uses its default.
|
|
249
|
+
function parseRetryAfterSeconds(header) {
|
|
250
|
+
if (header === null || !/^[0-9]+$/.test(header.trim())) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
return Number.parseInt(header.trim(), 10);
|
|
254
|
+
}
|
|
255
|
+
function errorCode(parsed) {
|
|
256
|
+
const code = parsed.error;
|
|
257
|
+
return typeof code === "string" ? code : null;
|
|
258
|
+
}
|
|
259
|
+
function parseFinalized(parsed) {
|
|
260
|
+
return {
|
|
261
|
+
kind: "finalized",
|
|
262
|
+
appSlug: stringField(parsed, "app_slug"),
|
|
263
|
+
versionNumber: integerField(parsed, "version_number"),
|
|
264
|
+
live: parsed.live === true,
|
|
265
|
+
url: typeof parsed.url === "string" ? parsed.url : null,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function stringField(record, field) {
|
|
269
|
+
const value = record[field];
|
|
270
|
+
if (typeof value !== "string") {
|
|
271
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
272
|
+
}
|
|
273
|
+
return value;
|
|
274
|
+
}
|
|
275
|
+
// Every numeric field in the protocol is a count or a version number, so
|
|
276
|
+
// anything that isn't a plain exact integer is a malformed response — and
|
|
277
|
+
// rejecting it here keeps values like 1.79e308 from ever reaching a
|
|
278
|
+
// rendered table.
|
|
279
|
+
function integerField(record, field) {
|
|
280
|
+
const value = record[field];
|
|
281
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) {
|
|
282
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
283
|
+
}
|
|
284
|
+
return value;
|
|
285
|
+
}
|
|
286
|
+
function stringArrayField(record, field) {
|
|
287
|
+
const value = record[field];
|
|
288
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
|
|
289
|
+
throw new DeployError(UNEXPECTED_RESPONSE_MESSAGE);
|
|
290
|
+
}
|
|
291
|
+
return value;
|
|
292
|
+
}
|
package/dist/deploy.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type CreatedApp, type DeployApi } from "./client.ts";
|
|
2
|
+
import { DeployApiError } from "./errors.ts";
|
|
3
|
+
import { type CollectedDeploy } from "./manifest.ts";
|
|
4
|
+
export interface DeployOptions {
|
|
5
|
+
live: boolean;
|
|
6
|
+
appName?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface DeployOutcome {
|
|
9
|
+
appSlug: string;
|
|
10
|
+
versionNumber: number;
|
|
11
|
+
live: boolean;
|
|
12
|
+
url: string | null;
|
|
13
|
+
fileCount: number;
|
|
14
|
+
uploadedBlobCount: number;
|
|
15
|
+
uploadedBytes: number;
|
|
16
|
+
unchangedFileCount: number;
|
|
17
|
+
}
|
|
18
|
+
export interface ProjectDeployOptions extends DeployOptions {
|
|
19
|
+
onAppCreated?: (created: CreatedApp) => void;
|
|
20
|
+
}
|
|
21
|
+
export interface ProjectDeployResult {
|
|
22
|
+
outcome: DeployOutcome;
|
|
23
|
+
createdApp: CreatedApp | null;
|
|
24
|
+
}
|
|
25
|
+
export declare function deployProjectFiles(api: DeployApi, projectDirectory: string, slug: string, collected: CollectedDeploy, options: ProjectDeployOptions): Promise<ProjectDeployResult>;
|
|
26
|
+
export declare function deployCollectedFiles(api: DeployApi, slug: string, collected: CollectedDeploy, options: DeployOptions): Promise<DeployOutcome>;
|
|
27
|
+
export declare function retryDelayMs(error: DeployApiError): number;
|
|
28
|
+
export declare function runWithConcurrencyLimit<Item, Result>(items: readonly Item[], limit: number, worker: (item: Item, signal: AbortSignal) => Promise<Result>): Promise<Result[]>;
|