@astrojs/db 0.0.0-db-export-bug-20240307130354

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.
Files changed (63) hide show
  1. package/LICENSE +59 -0
  2. package/README.md +38 -0
  3. package/dist/core/cli/commands/execute/index.d.ts +8 -0
  4. package/dist/core/cli/commands/execute/index.js +32 -0
  5. package/dist/core/cli/commands/link/index.d.ts +20 -0
  6. package/dist/core/cli/commands/link/index.js +238 -0
  7. package/dist/core/cli/commands/login/index.d.ts +8 -0
  8. package/dist/core/cli/commands/login/index.js +55 -0
  9. package/dist/core/cli/commands/logout/index.d.ts +1 -0
  10. package/dist/core/cli/commands/logout/index.js +9 -0
  11. package/dist/core/cli/commands/push/index.d.ts +8 -0
  12. package/dist/core/cli/commands/push/index.js +77 -0
  13. package/dist/core/cli/commands/shell/index.d.ts +8 -0
  14. package/dist/core/cli/commands/shell/index.js +17 -0
  15. package/dist/core/cli/commands/verify/index.d.ts +8 -0
  16. package/dist/core/cli/commands/verify/index.js +46 -0
  17. package/dist/core/cli/index.d.ts +6 -0
  18. package/dist/core/cli/index.js +77 -0
  19. package/dist/core/cli/migration-queries.d.ts +22 -0
  20. package/dist/core/cli/migration-queries.js +366 -0
  21. package/dist/core/consts.d.ts +7 -0
  22. package/dist/core/consts.js +19 -0
  23. package/dist/core/errors.d.ts +11 -0
  24. package/dist/core/errors.js +65 -0
  25. package/dist/core/integration/error-map.d.ts +6 -0
  26. package/dist/core/integration/error-map.js +79 -0
  27. package/dist/core/integration/file-url.d.ts +2 -0
  28. package/dist/core/integration/file-url.js +81 -0
  29. package/dist/core/integration/index.d.ts +2 -0
  30. package/dist/core/integration/index.js +112 -0
  31. package/dist/core/integration/typegen.d.ts +5 -0
  32. package/dist/core/integration/typegen.js +31 -0
  33. package/dist/core/integration/vite-plugin-db.d.ts +29 -0
  34. package/dist/core/integration/vite-plugin-db.js +111 -0
  35. package/dist/core/integration/vite-plugin-inject-env-ts.d.ts +11 -0
  36. package/dist/core/integration/vite-plugin-inject-env-ts.js +53 -0
  37. package/dist/core/load-file.d.ts +31 -0
  38. package/dist/core/load-file.js +98 -0
  39. package/dist/core/tokens.d.ts +11 -0
  40. package/dist/core/tokens.js +131 -0
  41. package/dist/core/types.d.ts +3901 -0
  42. package/dist/core/types.js +143 -0
  43. package/dist/core/utils.d.ts +6 -0
  44. package/dist/core/utils.js +22 -0
  45. package/dist/index.d.ts +3 -0
  46. package/dist/index.js +6 -0
  47. package/dist/runtime/config.d.ts +148 -0
  48. package/dist/runtime/config.js +87 -0
  49. package/dist/runtime/db-client.d.ts +5 -0
  50. package/dist/runtime/db-client.js +64 -0
  51. package/dist/runtime/drizzle.d.ts +1 -0
  52. package/dist/runtime/drizzle.js +48 -0
  53. package/dist/runtime/index.d.ts +31 -0
  54. package/dist/runtime/index.js +126 -0
  55. package/dist/runtime/queries.d.ts +81 -0
  56. package/dist/runtime/queries.js +206 -0
  57. package/dist/runtime/types.d.ts +69 -0
  58. package/dist/runtime/types.js +8 -0
  59. package/dist/utils.d.ts +1 -0
  60. package/dist/utils.js +4 -0
  61. package/index.d.ts +3 -0
  62. package/package.json +90 -0
  63. package/virtual.d.ts +9 -0
@@ -0,0 +1,131 @@
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 { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from "./errors.js";
6
+ import { getAstroStudioEnv, getAstroStudioUrl } from "./utils.js";
7
+ const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), ".astro", "session-token"));
8
+ const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), ".astro", "link"));
9
+ class ManagedLocalAppToken {
10
+ token;
11
+ constructor(token) {
12
+ this.token = token;
13
+ }
14
+ async renew() {
15
+ }
16
+ async destroy() {
17
+ }
18
+ }
19
+ class ManagedRemoteAppToken {
20
+ token;
21
+ session;
22
+ projectId;
23
+ ttl;
24
+ renewTimer;
25
+ static async create(sessionToken, projectId) {
26
+ const response = await fetch(new URL(`${getAstroStudioUrl()}/auth/cli/token-create`), {
27
+ method: "POST",
28
+ headers: new Headers({
29
+ Authorization: `Bearer ${sessionToken}`
30
+ }),
31
+ body: JSON.stringify({ projectId })
32
+ });
33
+ const { token: shortLivedAppToken, ttl } = await response.json();
34
+ return new ManagedRemoteAppToken({
35
+ token: shortLivedAppToken,
36
+ session: sessionToken,
37
+ projectId,
38
+ ttl
39
+ });
40
+ }
41
+ constructor(options) {
42
+ this.token = options.token;
43
+ this.session = options.session;
44
+ this.projectId = options.projectId;
45
+ this.ttl = options.ttl;
46
+ this.renewTimer = setTimeout(() => this.renew(), 1e3 * 60 * 5 / 2);
47
+ }
48
+ async fetch(url, body) {
49
+ return fetch(`${getAstroStudioUrl()}${url}`, {
50
+ method: "POST",
51
+ headers: {
52
+ Authorization: `Bearer ${this.session}`,
53
+ "Content-Type": "application/json"
54
+ },
55
+ body: JSON.stringify(body)
56
+ });
57
+ }
58
+ async renew() {
59
+ clearTimeout(this.renewTimer);
60
+ delete this.renewTimer;
61
+ try {
62
+ const response = await this.fetch("/auth/cli/token-renew", {
63
+ token: this.token,
64
+ projectId: this.projectId
65
+ });
66
+ if (response.status === 200) {
67
+ this.renewTimer = setTimeout(() => this.renew(), 1e3 * 60 * this.ttl / 2);
68
+ } else {
69
+ throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
70
+ }
71
+ } catch (error) {
72
+ const retryIn = 60 * this.ttl / 10;
73
+ console.error(`Failed to renew token. Retrying in ${retryIn} seconds.`, error?.message);
74
+ this.renewTimer = setTimeout(() => this.renew(), retryIn * 1e3);
75
+ }
76
+ }
77
+ async destroy() {
78
+ try {
79
+ const response = await this.fetch("/auth/cli/token-delete", {
80
+ token: this.token,
81
+ projectId: this.projectId
82
+ });
83
+ if (response.status !== 200) {
84
+ throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
85
+ }
86
+ } catch (error) {
87
+ console.error("Failed to delete token.", error?.message);
88
+ }
89
+ }
90
+ }
91
+ async function getProjectIdFromFile() {
92
+ try {
93
+ return await readFile(PROJECT_ID_FILE, "utf-8");
94
+ } catch (error) {
95
+ return void 0;
96
+ }
97
+ }
98
+ async function getSessionIdFromFile() {
99
+ try {
100
+ return await readFile(SESSION_LOGIN_FILE, "utf-8");
101
+ } catch (error) {
102
+ return void 0;
103
+ }
104
+ }
105
+ async function getManagedAppTokenOrExit(token) {
106
+ if (token) {
107
+ return new ManagedLocalAppToken(token);
108
+ }
109
+ const { ASTRO_STUDIO_APP_TOKEN } = getAstroStudioEnv();
110
+ if (ASTRO_STUDIO_APP_TOKEN) {
111
+ return new ManagedLocalAppToken(ASTRO_STUDIO_APP_TOKEN);
112
+ }
113
+ const sessionToken = await getSessionIdFromFile();
114
+ if (!sessionToken) {
115
+ console.error(MISSING_SESSION_ID_ERROR);
116
+ process.exit(1);
117
+ }
118
+ const projectId = await getProjectIdFromFile();
119
+ if (!sessionToken || !projectId) {
120
+ console.error(MISSING_PROJECT_ID_ERROR);
121
+ process.exit(1);
122
+ }
123
+ return ManagedRemoteAppToken.create(sessionToken, projectId);
124
+ }
125
+ export {
126
+ PROJECT_ID_FILE,
127
+ SESSION_LOGIN_FILE,
128
+ getManagedAppTokenOrExit,
129
+ getProjectIdFromFile,
130
+ getSessionIdFromFile
131
+ };