@astrojs/db 0.11.2 → 0.11.3

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.
@@ -1,10 +1,8 @@
1
1
  import type { AstroConfig, AstroIntegration } from 'astro';
2
2
  import type { AstroDbIntegration } from './types.js';
3
3
  export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
4
- export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
5
4
  export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
6
5
  export declare function getRemoteDatabaseUrl(): string;
7
- export declare function getAstroStudioUrl(): string;
8
6
  export declare function getDbDirectoryUrl(root: URL | string): URL;
9
7
  export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
10
8
  export type Result<T> = {
@@ -1,4 +1,5 @@
1
1
  import { existsSync } from "node:fs";
2
+ import { getManagedAppTokenOrExit } from "@astrojs/studio";
2
3
  import { LibsqlError } from "@libsql/client";
3
4
  import { green } from "kleur/colors";
4
5
  import {
@@ -12,7 +13,6 @@ import {
12
13
  getStudioVirtualModContents
13
14
  } from "../../../integration/vite-plugin-db.js";
14
15
  import { bundleFile, importBundledFile } from "../../../load-file.js";
15
- import { getManagedAppTokenOrExit } from "../../../tokens.js";
16
16
  async function cmd({
17
17
  astroConfig,
18
18
  dbConfig,
@@ -1,14 +1,18 @@
1
1
  import { mkdir, writeFile } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
3
  import { basename } from "node:path";
4
+ import {
5
+ MISSING_SESSION_ID_ERROR,
6
+ PROJECT_ID_FILE,
7
+ getAstroStudioUrl,
8
+ getSessionIdFromFile
9
+ } from "@astrojs/studio";
4
10
  import { slug } from "github-slugger";
5
11
  import { bgRed, cyan } from "kleur/colors";
6
12
  import ora from "ora";
7
13
  import prompts from "prompts";
8
14
  import { safeFetch } from "../../../../runtime/utils.js";
9
- import { MISSING_SESSION_ID_ERROR } from "../../../errors.js";
10
- import { PROJECT_ID_FILE, getSessionIdFromFile } from "../../../tokens.js";
11
- import { getAstroStudioUrl } from "../../../utils.js";
15
+ import {} from "../../../utils.js";
12
16
  async function cmd() {
13
17
  const sessionToken = await getSessionIdFromFile();
14
18
  if (!sessionToken) {
@@ -64,7 +68,7 @@ async function getWorkspaceId() {
64
68
 
65
69
  Are you logged in?
66
70
  Run ${cyan(
67
- "astro db login"
71
+ "astro login"
68
72
  )} to authenticate and then try linking again.
69
73
 
70
74
  `
@@ -109,7 +113,7 @@ async function createNewProject({
109
113
 
110
114
  Are you logged in?
111
115
  Run ${cyan(
112
- "astro db login"
116
+ "astro login"
113
117
  )} to authenticate and then try linking again.
114
118
 
115
119
  `
@@ -146,7 +150,7 @@ async function promptExistingProjectName({ workspaceId }) {
146
150
 
147
151
  Are you logged in?
148
152
  Run ${cyan(
149
- "astro db login"
153
+ "astro login"
150
154
  )} to authenticate and then try linking again.
151
155
 
152
156
  `
@@ -1,12 +1,11 @@
1
1
  import { mkdir, writeFile } from "node:fs/promises";
2
2
  import { createServer as _createServer } from "node:http";
3
+ import { SESSION_LOGIN_FILE, getAstroStudioUrl } from "@astrojs/studio";
3
4
  import { listen } from "async-listen";
4
5
  import { cyan } from "kleur/colors";
5
6
  import open from "open";
6
7
  import ora from "ora";
7
8
  import prompt from "prompts";
8
- import { SESSION_LOGIN_FILE } from "../../../tokens.js";
9
- import { getAstroStudioUrl } from "../../../utils.js";
10
9
  const isWebContainer = (
11
10
  // Stackblitz heuristic
12
11
  process.versions?.webcontainer ?? // GitHub Codespaces heuristic
@@ -1,5 +1,5 @@
1
1
  import { unlink } from "node:fs/promises";
2
- import { SESSION_LOGIN_FILE } from "../../../tokens.js";
2
+ import { SESSION_LOGIN_FILE } from "@astrojs/studio";
3
3
  async function cmd() {
4
4
  await unlink(SESSION_LOGIN_FILE);
5
5
  console.log("Successfully logged out of Astro Studio.");
@@ -1,7 +1,7 @@
1
+ import { getManagedAppTokenOrExit } from "@astrojs/studio";
1
2
  import prompts from "prompts";
2
3
  import { safeFetch } from "../../../../runtime/utils.js";
3
4
  import { MIGRATION_VERSION } from "../../../consts.js";
4
- import { getManagedAppTokenOrExit } from "../../../tokens.js";
5
5
  import {} from "../../../types.js";
6
6
  import { getRemoteDatabaseUrl } from "../../../utils.js";
7
7
  import {
@@ -1,3 +1,4 @@
1
+ import { getManagedAppTokenOrExit } from "@astrojs/studio";
1
2
  import { sql } from "drizzle-orm";
2
3
  import {
3
4
  createLocalDatabaseClient,
@@ -6,7 +7,6 @@ import {
6
7
  import { normalizeDatabaseUrl } from "../../../../runtime/index.js";
7
8
  import { DB_PATH } from "../../../consts.js";
8
9
  import { SHELL_QUERY_MISSING_ERROR } from "../../../errors.js";
9
- import { getManagedAppTokenOrExit } from "../../../tokens.js";
10
10
  import { getAstroEnv, getRemoteDatabaseUrl } from "../../../utils.js";
11
11
  async function cmd({
12
12
  flags,
@@ -1,4 +1,4 @@
1
- import { getManagedAppTokenOrExit } from "../../../tokens.js";
1
+ import { getManagedAppTokenOrExit } from "@astrojs/studio";
2
2
  import {
3
3
  createCurrentSnapshot,
4
4
  createEmptySnapshot,
@@ -27,7 +27,7 @@ function printHelp({
27
27
  message.push(
28
28
  linebreak(),
29
29
  ` ${bgGreen(black(` ${commandName} `))} ${green(
30
- `v${"0.11.2"}`
30
+ `v${"0.11.3"}`
31
31
  )} ${headline}`
32
32
  );
33
33
  }
@@ -1,6 +1,3 @@
1
- export declare const MISSING_SESSION_ID_CI_ERROR: string;
2
- export declare const MISSING_SESSION_ID_ERROR: string;
3
- export declare const MISSING_PROJECT_ID_ERROR: string;
4
1
  export declare const MISSING_EXECUTE_PATH_ERROR: string;
5
2
  export declare const RENAME_TABLE_ERROR: (oldTable: string, newTable: string) => string;
6
3
  export declare const RENAME_COLUMN_ERROR: (oldSelector: string, newSelector: string) => string;
@@ -1,18 +1,4 @@
1
1
  import { bold, cyan, red } from "kleur/colors";
2
- const MISSING_SESSION_ID_CI_ERROR = `${red("\u25B6 ASTRO_STUDIO_APP_TOKEN required")}
3
-
4
- To authenticate with Astro Studio add the token to your CI's environment variables.
5
- `;
6
- const MISSING_SESSION_ID_ERROR = `${red("\u25B6 Login required!")}
7
-
8
- To authenticate with Astro Studio, run
9
- ${cyan("astro db login")}
10
- `;
11
- const MISSING_PROJECT_ID_ERROR = `${red("\u25B6 Directory not linked.")}
12
-
13
- To link this directory to an Astro Studio project, run
14
- ${cyan("astro db link")}
15
- `;
16
2
  const MISSING_EXECUTE_PATH_ERROR = `${red(
17
3
  "\u25B6 No file path provided."
18
4
  )} Provide a path by running ${cyan("astro db execute <path>")}
@@ -23,7 +9,7 @@ const RENAME_TABLE_ERROR = (oldTable, newTable) => {
23
9
 
24
10
  1. Use "deprecated: true" to deprecate a table before renaming.
25
11
  2. Use "--force-reset" to ignore this warning and reset the database (deleting all of your data).
26
-
12
+
27
13
  Visit https://docs.astro.build/en/guides/astro-db/#renaming-tables to learn more.`;
28
14
  };
29
15
  const RENAME_COLUMN_ERROR = (oldSelector, newSelector) => {
@@ -56,9 +42,6 @@ export {
56
42
  FILE_NOT_FOUND_ERROR,
57
43
  INTEGRATION_TABLE_CONFLICT_ERROR,
58
44
  MISSING_EXECUTE_PATH_ERROR,
59
- MISSING_PROJECT_ID_ERROR,
60
- MISSING_SESSION_ID_CI_ERROR,
61
- MISSING_SESSION_ID_ERROR,
62
45
  RENAME_COLUMN_ERROR,
63
46
  RENAME_TABLE_ERROR,
64
47
  SHELL_QUERY_MISSING_ERROR
@@ -1,6 +1,7 @@
1
1
  import { existsSync } from "fs";
2
2
  import { dirname } from "path";
3
3
  import { fileURLToPath } from "url";
4
+ import { getManagedAppTokenOrExit } from "@astrojs/studio";
4
5
  import { LibsqlError } from "@libsql/client";
5
6
  import { mkdir, writeFile } from "fs/promises";
6
7
  import { blue, yellow } from "kleur/colors";
@@ -15,7 +16,6 @@ import { CONFIG_FILE_NAMES, DB_PATH } from "../consts.js";
15
16
  import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from "../errors.js";
16
17
  import { resolveDbConfig } from "../load-file.js";
17
18
  import { SEED_DEV_FILE_NAME } from "../queries.js";
18
- import { getManagedAppTokenOrExit } from "../tokens.js";
19
19
  import { getDbDirectoryUrl } from "../utils.js";
20
20
  import { fileURLIntegration } from "./file-url.js";
21
21
  import { typegenInternal } from "./typegen.js";
@@ -1,10 +1,8 @@
1
1
  import type { AstroConfig, AstroIntegration } from 'astro';
2
2
  import type { AstroDbIntegration } from './types.js';
3
3
  export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
4
- export declare function getAstroStudioEnv(envMode?: string): Record<`ASTRO_STUDIO_${string}`, string>;
5
4
  export declare function getAstroEnv(envMode?: string): Record<`ASTRO_${string}`, string>;
6
5
  export declare function getRemoteDatabaseUrl(): string;
7
- export declare function getAstroStudioUrl(): string;
8
6
  export declare function getDbDirectoryUrl(root: URL | string): URL;
9
7
  export declare function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration;
10
8
  export type Result<T> = {
@@ -1,8 +1,5 @@
1
+ import { getAstroStudioEnv } from "@astrojs/studio";
1
2
  import { loadEnv } from "vite";
2
- function getAstroStudioEnv(envMode = "") {
3
- const env = loadEnv(envMode, process.cwd(), "ASTRO_STUDIO_");
4
- return env;
5
- }
6
3
  function getAstroEnv(envMode = "") {
7
4
  const env = loadEnv(envMode, process.cwd(), "ASTRO_");
8
5
  return env;
@@ -11,10 +8,6 @@ function getRemoteDatabaseUrl() {
11
8
  const env = getAstroStudioEnv();
12
9
  return env.ASTRO_STUDIO_REMOTE_DB_URL || "https://db.services.astro.build";
13
10
  }
14
- function getAstroStudioUrl() {
15
- const env = getAstroStudioEnv();
16
- return env.ASTRO_STUDIO_URL || "https://studio.astro.build";
17
- }
18
11
  function getDbDirectoryUrl(root) {
19
12
  return new URL("db/", root);
20
13
  }
@@ -29,8 +22,6 @@ function mapObject(item, callback) {
29
22
  export {
30
23
  defineDbIntegration,
31
24
  getAstroEnv,
32
- getAstroStudioEnv,
33
- getAstroStudioUrl,
34
25
  getDbDirectoryUrl,
35
26
  getRemoteDatabaseUrl,
36
27
  mapObject
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/db",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "description": "Add libSQL and Astro Studio support to your Astro site",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -75,7 +75,8 @@
75
75
  "prompts": "^2.4.2",
76
76
  "strip-ansi": "^7.1.0",
77
77
  "yargs-parser": "^21.1.1",
78
- "zod": "^3.23.8"
78
+ "zod": "^3.23.8",
79
+ "@astrojs/studio": "0.1.0"
79
80
  },
80
81
  "devDependencies": {
81
82
  "@types/chai": "^4.3.16",
@@ -89,7 +90,7 @@
89
90
  "mocha": "^10.4.0",
90
91
  "typescript": "^5.4.5",
91
92
  "vite": "^5.2.11",
92
- "astro": "4.8.4",
93
+ "astro": "4.8.5",
93
94
  "astro-scripts": "0.0.14"
94
95
  },
95
96
  "scripts": {
@@ -1,11 +0,0 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- export declare const SESSION_LOGIN_FILE: import("url").URL;
3
- export declare const PROJECT_ID_FILE: import("url").URL;
4
- export interface ManagedAppToken {
5
- token: string;
6
- renew(): Promise<void>;
7
- destroy(): Promise<void>;
8
- }
9
- export declare function getProjectIdFromFile(): Promise<string | undefined>;
10
- export declare function getSessionIdFromFile(): Promise<string | undefined>;
11
- export declare function getManagedAppTokenOrExit(token?: string): Promise<ManagedAppToken>;
@@ -1,190 +0,0 @@
1
- import { readFile } from "node:fs/promises";
2
- import { homedir } from "node:os";
3
- import { join } from "node:path";
4
- import { pathToFileURL } from "node:url";
5
- import ci from "ci-info";
6
- import { green } from "kleur/colors";
7
- import ora from "ora";
8
- import { safeFetch } from "../runtime/utils.js";
9
- import {
10
- MISSING_PROJECT_ID_ERROR,
11
- MISSING_SESSION_ID_CI_ERROR,
12
- MISSING_SESSION_ID_ERROR
13
- } from "./errors.js";
14
- import { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
15
- const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), ".astro", "session-token"));
16
- const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), ".astro", "link"));
17
- class ManagedLocalAppToken {
18
- token;
19
- constructor(token) {
20
- this.token = token;
21
- }
22
- async renew() {
23
- }
24
- async destroy() {
25
- }
26
- }
27
- class ManagedRemoteAppToken {
28
- token;
29
- session;
30
- projectId;
31
- ttl;
32
- expires;
33
- renewTimer;
34
- static async create(sessionToken, projectId) {
35
- const { token: shortLivedAppToken, ttl } = await this.createToken(sessionToken, projectId);
36
- return new ManagedRemoteAppToken({
37
- token: shortLivedAppToken,
38
- session: sessionToken,
39
- projectId,
40
- ttl
41
- });
42
- }
43
- static async createToken(sessionToken, projectId) {
44
- const spinner = ora("Connecting to remote database...").start();
45
- const response = await safeFetch(
46
- new URL(`${getAstroStudioUrl()}/auth/cli/token-create`),
47
- {
48
- method: "POST",
49
- headers: new Headers({
50
- Authorization: `Bearer ${sessionToken}`
51
- }),
52
- body: JSON.stringify({ projectId })
53
- },
54
- (res) => {
55
- throw new Error(`Failed to create token: ${res.status} ${res.statusText}`);
56
- }
57
- );
58
- spinner.succeed(green("Connected to remote database."));
59
- const { token, ttl } = await response.json();
60
- return { token, ttl };
61
- }
62
- constructor(options) {
63
- this.token = options.token;
64
- this.session = options.session;
65
- this.projectId = options.projectId;
66
- this.ttl = options.ttl;
67
- this.renewTimer = setTimeout(() => this.renew(), 1e3 * 60 * 5 / 2);
68
- this.expires = getExpiresFromTtl(this.ttl);
69
- }
70
- async fetch(url, body) {
71
- return safeFetch(
72
- `${getAstroStudioUrl()}${url}`,
73
- {
74
- method: "POST",
75
- headers: {
76
- Authorization: `Bearer ${this.session}`,
77
- "Content-Type": "application/json"
78
- },
79
- body: JSON.stringify(body)
80
- },
81
- () => {
82
- throw new Error(`Failed to fetch ${url}.`);
83
- }
84
- );
85
- }
86
- tokenIsValid() {
87
- return /* @__PURE__ */ new Date() > this.expires;
88
- }
89
- createRenewTimer() {
90
- return setTimeout(() => this.renew(), 1e3 * 60 * this.ttl / 2);
91
- }
92
- async renew() {
93
- clearTimeout(this.renewTimer);
94
- delete this.renewTimer;
95
- if (this.tokenIsValid()) {
96
- const response = await this.fetch("/auth/cli/token-renew", {
97
- token: this.token,
98
- projectId: this.projectId
99
- });
100
- if (response.status === 200) {
101
- this.expires = getExpiresFromTtl(this.ttl);
102
- this.renewTimer = this.createRenewTimer();
103
- } else {
104
- throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
105
- }
106
- } else {
107
- try {
108
- const { token, ttl } = await ManagedRemoteAppToken.createToken(
109
- this.session,
110
- this.projectId
111
- );
112
- this.token = token;
113
- this.ttl = ttl;
114
- this.expires = getExpiresFromTtl(ttl);
115
- this.renewTimer = this.createRenewTimer();
116
- } catch {
117
- throw new Error(
118
- `Token has expired and attempts to renew it have failed, please try again.`
119
- );
120
- }
121
- }
122
- }
123
- async destroy() {
124
- try {
125
- const response = await this.fetch("/auth/cli/token-delete", {
126
- token: this.token,
127
- projectId: this.projectId
128
- });
129
- if (response.status !== 200) {
130
- throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
131
- }
132
- } catch (error) {
133
- console.error("Failed to delete token.", error?.message);
134
- }
135
- }
136
- }
137
- async function getProjectIdFromFile() {
138
- try {
139
- return await readFile(PROJECT_ID_FILE, "utf-8");
140
- } catch (error) {
141
- return void 0;
142
- }
143
- }
144
- async function getSessionIdFromFile() {
145
- try {
146
- return await readFile(SESSION_LOGIN_FILE, "utf-8");
147
- } catch (error) {
148
- return void 0;
149
- }
150
- }
151
- async function getManagedAppTokenOrExit(token) {
152
- if (token) {
153
- return new ManagedLocalAppToken(token);
154
- }
155
- if (process.env.ASTRO_INTERNAL_TEST_REMOTE) {
156
- return new ManagedLocalAppToken(
157
- "fake"
158
- /* token ignored in test */
159
- );
160
- }
161
- const { ASTRO_STUDIO_APP_TOKEN } = getAstroStudioEnv();
162
- if (ASTRO_STUDIO_APP_TOKEN) {
163
- return new ManagedLocalAppToken(ASTRO_STUDIO_APP_TOKEN);
164
- }
165
- const sessionToken = await getSessionIdFromFile();
166
- if (!sessionToken) {
167
- if (ci.isCI) {
168
- console.error(MISSING_SESSION_ID_CI_ERROR);
169
- } else {
170
- console.error(MISSING_SESSION_ID_ERROR);
171
- }
172
- process.exit(1);
173
- }
174
- const projectId = await getProjectIdFromFile();
175
- if (!projectId) {
176
- console.error(MISSING_PROJECT_ID_ERROR);
177
- process.exit(1);
178
- }
179
- return ManagedRemoteAppToken.create(sessionToken, projectId);
180
- }
181
- function getExpiresFromTtl(ttl) {
182
- return new Date(Date.now() + ttl * 60 * 1e3);
183
- }
184
- export {
185
- PROJECT_ID_FILE,
186
- SESSION_LOGIN_FILE,
187
- getManagedAppTokenOrExit,
188
- getProjectIdFromFile,
189
- getSessionIdFromFile
190
- };