@gadgetinc/ggt 0.4.6 → 0.4.8
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 +1 -1
- package/bin/dev.js +2 -1
- package/bin/run.js +3 -1
- package/lib/__generated__/graphql.js.map +1 -1
- package/lib/commands/deploy.js +43 -49
- package/lib/commands/deploy.js.map +1 -1
- package/lib/commands/list.js +7 -11
- package/lib/commands/list.js.map +1 -1
- package/lib/commands/login.js +8 -12
- package/lib/commands/login.js.map +1 -1
- package/lib/commands/logout.js +3 -7
- package/lib/commands/logout.js.map +1 -1
- package/lib/commands/root.js +17 -43
- package/lib/commands/root.js.map +1 -1
- package/lib/commands/sync.js +10 -39
- package/lib/commands/sync.js.map +1 -1
- package/lib/commands/version.js +2 -6
- package/lib/commands/version.js.map +1 -1
- package/lib/commands/whoami.js +5 -9
- package/lib/commands/whoami.js.map +1 -1
- package/lib/ggt.js +40 -0
- package/lib/ggt.js.map +1 -0
- package/lib/services/app/app.js +8 -6
- package/lib/services/app/app.js.map +1 -1
- package/lib/services/app/edit/client.js +176 -0
- package/lib/services/app/edit/client.js.map +1 -0
- package/lib/services/app/edit/edit.js +155 -0
- package/lib/services/app/edit/edit.js.map +1 -0
- package/lib/services/app/edit/error.js +65 -0
- package/lib/services/app/edit/error.js.map +1 -0
- package/lib/services/app/edit/operation.js +87 -0
- package/lib/services/app/edit/operation.js.map +1 -0
- package/lib/services/command/arg.js +5 -5
- package/lib/services/command/arg.js.map +1 -1
- package/lib/services/command/command.js +23 -5
- package/lib/services/command/command.js.map +1 -1
- package/lib/services/command/context.js +123 -36
- package/lib/services/command/context.js.map +1 -1
- package/lib/services/filesync/changes.js +7 -9
- package/lib/services/filesync/changes.js.map +1 -1
- package/lib/services/filesync/conflicts.js +11 -9
- package/lib/services/filesync/conflicts.js.map +1 -1
- package/lib/services/filesync/directory.js +2 -2
- package/lib/services/filesync/directory.js.map +1 -1
- package/lib/services/filesync/filesync.js +187 -96
- package/lib/services/filesync/filesync.js.map +1 -1
- package/lib/services/filesync/hashes.js +18 -15
- package/lib/services/filesync/hashes.js.map +1 -1
- package/lib/services/http/auth.js +4 -4
- package/lib/services/http/auth.js.map +1 -1
- package/lib/services/http/http.js +54 -27
- package/lib/services/http/http.js.map +1 -1
- package/lib/services/output/log/logger.js +11 -6
- package/lib/services/output/log/logger.js.map +1 -1
- package/lib/services/output/log/printer.js.map +1 -1
- package/lib/services/output/log/structured.js +2 -2
- package/lib/services/output/log/structured.js.map +1 -1
- package/lib/services/output/notify.js +3 -7
- package/lib/services/output/notify.js.map +1 -1
- package/lib/services/output/prompt.js +11 -11
- package/lib/services/output/prompt.js.map +1 -1
- package/lib/services/output/report.js +51 -67
- package/lib/services/output/report.js.map +1 -1
- package/lib/services/output/update.js +9 -10
- package/lib/services/output/update.js.map +1 -1
- package/lib/services/user/user.js +19 -25
- package/lib/services/user/user.js.map +1 -1
- package/lib/services/util/collection.js +1 -1
- package/lib/services/util/collection.js.map +1 -1
- package/lib/services/util/function.js +29 -11
- package/lib/services/util/function.js.map +1 -1
- package/lib/services/util/is.js +19 -1
- package/lib/services/util/is.js.map +1 -1
- package/lib/services/util/number.js +3 -3
- package/lib/services/util/number.js.map +1 -1
- package/lib/services/util/object.js +4 -4
- package/lib/services/util/object.js.map +1 -1
- package/lib/services/util/paths.js +4 -4
- package/lib/services/util/paths.js.map +1 -1
- package/lib/services/util/types.js +5 -0
- package/lib/services/util/types.js.map +1 -0
- package/npm-shrinkwrap.json +586 -418
- package/package.json +17 -15
- package/lib/main.js +0 -8
- package/lib/main.js.map +0 -1
- package/lib/services/app/edit-graphql.js +0 -389
- package/lib/services/app/edit-graphql.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/filesync/filesync.ts"],"sourcesContent":["import dayjs from \"dayjs\";\nimport { findUp } from \"find-up\";\nimport fs from \"fs-extra\";\nimport ms from \"ms\";\nimport assert from \"node:assert\";\nimport path from \"node:path\";\nimport process from \"node:process\";\nimport pMap from \"p-map\";\nimport PQueue from \"p-queue\";\nimport pRetry from \"p-retry\";\nimport type { Promisable } from \"type-fest\";\nimport { z } from \"zod\";\nimport { FileSyncEncoding, type FileSyncChangedEventInput, type FileSyncDeletedEventInput } from \"../../__generated__/graphql.js\";\nimport type { App } from \"../app/app.js\";\nimport { getApps } from \"../app/app.js\";\nimport {\n EditGraphQL,\n FILE_SYNC_COMPARISON_HASHES_QUERY,\n FILE_SYNC_FILES_QUERY,\n FILE_SYNC_HASHES_QUERY,\n PUBLISH_FILE_SYNC_EVENTS_MUTATION,\n REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION,\n} from \"../app/edit-graphql.js\";\nimport { ArgError } from \"../command/arg.js\";\nimport { config, homePath } from \"../config/config.js\";\nimport { createLogger } from \"../output/log/logger.js\";\nimport { select } from \"../output/prompt.js\";\nimport { sprint } from \"../output/sprint.js\";\nimport type { User } from \"../user/user.js\";\nimport { sortBySimilar } from \"../util/collection.js\";\nimport { noop } from \"../util/function.js\";\nimport { Changes, printChanges } from \"./changes.js\";\nimport { getConflicts, printConflicts, withoutConflictingChanges } from \"./conflicts.js\";\nimport { Directory, supportsPermissions, swallowEnoent, type Hashes } from \"./directory.js\";\nimport { InvalidSyncFileError, TooManySyncAttemptsError } from \"./error.js\";\nimport type { File } from \"./file.js\";\nimport { getChanges, isEqualHashes, type ChangesWithHash } from \"./hashes.js\";\n\nconst log = createLogger({ name: \"filesync\" });\n\nexport type FileSyncHashes = {\n inSync: boolean;\n filesVersionHashes: Hashes;\n localHashes: Hashes;\n gadgetHashes: Hashes;\n gadgetFilesVersion: bigint;\n};\n\nexport class FileSync {\n readonly editGraphQL: EditGraphQL;\n\n readonly log = log.extend(\"filesync\", () => ({ state: this._state }));\n\n /**\n * A FIFO async callback queue that ensures we process filesync events\n * in the order we receive them.\n */\n private _syncOperations = new PQueue({ concurrency: 1 });\n\n private constructor(\n /**\n * The directory that is being synced to.\n */\n readonly directory: Directory,\n\n /**\n * Whether the directory was empty or non-existent when we started.\n */\n readonly wasEmptyOrNonExistent: boolean,\n\n /**\n * The Gadget application that is being synced to.\n */\n readonly app: App,\n\n /**\n * The state of the filesystem.\n *\n * This is persisted to `.gadget/sync.json` within the {@linkcode directory}.\n */\n private _state: { app: string; filesVersion: string; mtime: number },\n ) {\n this.editGraphQL = new EditGraphQL(this.app);\n }\n\n /**\n * The last filesVersion that was written to the filesystem.\n *\n * This determines if the filesystem in Gadget is ahead of the\n * filesystem on the local machine.\n */\n get filesVersion(): bigint {\n return BigInt(this._state.filesVersion);\n }\n\n /**\n * The largest mtime that was seen on the filesystem.\n *\n * This is used to determine if any files have changed since the last\n * sync. This does not include the mtime of files that are ignored.\n */\n get mtime(): number {\n return this._state.mtime;\n }\n\n /**\n * Initializes a {@linkcode FileSync} instance.\n * - Ensures the directory exists.\n * - Ensures the directory is empty or contains a `.gadget/sync.json` file (unless `options.force` is `true`)\n * - Ensures an app is specified (either via `options.app` or by prompting the user)\n * - Ensures the specified app matches the app the directory was previously synced to (unless `options.force` is `true`)\n */\n static async init(options: { user: User; dir?: string; app?: string; force?: boolean }): Promise<FileSync> {\n const apps = await getApps(options.user);\n if (apps.length === 0) {\n throw new ArgError(\n sprint`\n You (${options.user.email}) don't have have any Gadget applications.\n\n Visit https://gadget.new to create one!\n `,\n );\n }\n\n let dir = options.dir;\n if (!dir) {\n // the user didn't specify a directory\n const filepath = await findUp(\".gadget/sync.json\");\n if (filepath) {\n // we found a .gadget/sync.json file, use its parent directory\n dir = path.join(filepath, \"../..\");\n } else {\n // we didn't find a .gadget/sync.json file, use the current directory\n dir = process.cwd();\n }\n }\n\n if (config.windows && dir.startsWith(\"~/\")) {\n // `~` doesn't expand to the home directory on Windows\n dir = homePath(dir.slice(2));\n }\n\n // ensure the root directory is an absolute path and exists\n const wasEmptyOrNonExistent = await isEmptyOrNonExistentDir(dir);\n await fs.ensureDir((dir = path.resolve(dir)));\n\n // try to load the .gadget/sync.json file\n const state = await fs\n .readJson(path.join(dir, \".gadget/sync.json\"))\n .then((json) =>\n z\n .object({\n app: z.string(),\n filesVersion: z.string(),\n mtime: z.number(),\n })\n .parse(json),\n )\n .catch(noop);\n\n let appSlug = options.app || state?.app;\n if (!appSlug) {\n // the user didn't specify an app, suggest some apps that they can sync to\n appSlug = await select({\n message: \"Select the app to sync to\",\n choices: apps.map((x) => x.slug),\n });\n }\n\n // try to find the appSlug in their list of apps\n const app = apps.find((app) => app.slug === appSlug);\n if (!app) {\n // the specified appSlug doesn't exist in their list of apps,\n // either they misspelled it or they don't have access to it\n // anymore, suggest some apps that are similar to the one they\n // specified\n const similarAppSlugs = sortBySimilar(\n appSlug,\n apps.map((app) => app.slug),\n ).slice(0, 5);\n\n throw new ArgError(\n sprint`\n Unknown application:\n\n ${appSlug}\n\n Did you mean one of these?\n\n\n `.concat(` • ${similarAppSlugs.join(\"\\n • \")}`),\n );\n }\n\n const directory = await Directory.init(dir);\n\n if (!state) {\n // the .gadget/sync.json file didn't exist or contained invalid json\n if (wasEmptyOrNonExistent || options.force) {\n // the directory was empty or the user passed --force\n // either way, create a fresh .gadget/sync.json file\n return new FileSync(directory, wasEmptyOrNonExistent, app, { app: app.slug, filesVersion: \"0\", mtime: 0 });\n }\n\n // the directory isn't empty and the user didn't pass --force\n throw new InvalidSyncFileError(dir, app.slug);\n }\n\n // the .gadget/sync.json file exists\n if (state.app === app.slug) {\n // the .gadget/sync.json file is for the same app that the user specified\n return new FileSync(directory, wasEmptyOrNonExistent, app, state);\n }\n\n // the .gadget/sync.json file is for a different app\n if (options.force) {\n // the user passed --force, so use the app they specified and overwrite everything\n return new FileSync(directory, wasEmptyOrNonExistent, app, { app: app.slug, filesVersion: \"0\", mtime: 0 });\n }\n\n // the user didn't pass --force, so throw an error\n throw new ArgError(sprint`\n You were about to sync the following app to the following directory:\n\n {dim ${app.slug}} → {dim ${dir}}\n\n However, that directory has already been synced with this app:\n\n {dim ${state.app}}\n\n If you're sure that you want to sync:\n\n {dim ${app.slug}} → {dim ${dir}}\n\n Then run {dim ggt sync} again with the {dim --force} flag.\n `);\n }\n\n /**\n * Waits for all pending and ongoing filesync operations to complete.\n */\n async idle(): Promise<void> {\n await this._syncOperations.onIdle();\n }\n\n /**\n * Sends file changes to the Gadget.\n *\n * @param changes - The changes to send.\n * @returns A promise that resolves when the changes have been sent.\n */\n async sendChangesToGadget({ changes }: { changes: Changes }): Promise<void> {\n await this._syncOperations.add(() => this._sendChangesToGadget({ changes }));\n }\n\n /**\n * Subscribes to file changes on Gadget and executes the provided\n * callbacks before and after the changes occur.\n *\n * @returns A function that unsubscribes from changes on Gadget.\n */\n subscribeToGadgetChanges({\n beforeChanges,\n afterChanges,\n onError,\n }: {\n beforeChanges: (data: { changed: string[]; deleted: string[] }) => Promisable<void>;\n afterChanges: (data: { changes: Changes }) => Promisable<void>;\n onError: (error: unknown) => void;\n }): () => void {\n return this.editGraphQL.subscribe({\n query: REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION,\n // the reason this is a function rather than a static value is\n // so that it will be re-evaluated if the connection is lost and\n // then re-established. this ensures that we send our current\n // filesVersion rather than the one that was sent when we first\n // subscribed\n variables: () => ({ localFilesVersion: String(this.filesVersion) }),\n onError,\n onData: ({ remoteFileSyncEvents: { changed, deleted, remoteFilesVersion } }) => {\n this._syncOperations\n .add(async () => {\n if (BigInt(remoteFilesVersion) < this.filesVersion) {\n this.log.warn(\"skipping received changes because files version is outdated\", { filesVersion: remoteFilesVersion });\n return;\n }\n\n this.log.debug(\"received files\", {\n remoteFilesVersion: remoteFilesVersion,\n changed: changed.map((change) => change.path),\n deleted: deleted.map((change) => change.path),\n });\n\n const filterIgnoredFiles = (file: { path: string }): boolean => {\n const ignored = this.directory.ignores(file.path);\n if (ignored) {\n this.log.warn(\"skipping received change because file is ignored\", { path: file.path });\n }\n return !ignored;\n };\n\n changed = changed.filter(filterIgnoredFiles);\n deleted = deleted.filter(filterIgnoredFiles);\n\n if (changed.length === 0 && deleted.length === 0) {\n await this._save(remoteFilesVersion);\n return;\n }\n\n await beforeChanges({\n changed: changed.map((file) => file.path),\n deleted: deleted.map((file) => file.path),\n });\n\n const changes = await this._writeToLocalFilesystem({\n filesVersion: remoteFilesVersion,\n files: changed,\n delete: deleted.map((file) => file.path),\n });\n\n if (changes.size > 0) {\n printChanges({\n message: sprint`← Received {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n changes,\n tense: \"past\",\n limit: 10,\n });\n }\n\n await afterChanges({ changes });\n })\n .catch(onError);\n },\n });\n }\n\n /**\n * Ensures the local filesystem is in sync with Gadget's filesystem.\n * - All non-conflicting changes are automatically merged.\n * - Conflicts are resolved by prompting the user to either keep their\n * local changes or keep Gadget's changes.\n * - This function will not return until the filesystem is in sync.\n */\n async sync({ preference, maxAttempts = 10 }: { preference?: ConflictPreference; maxAttempts?: number } = {}): Promise<void> {\n this._syncOperations.pause();\n\n try {\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n const { inSync, ...hashes } = await this.hashes();\n\n if (inSync) {\n this.log.info(\"filesystem is in sync\");\n await this._save(hashes.gadgetFilesVersion);\n return;\n }\n\n await this._sync({ preference, ...hashes });\n }\n } finally {\n this._syncOperations.start();\n }\n\n throw new TooManySyncAttemptsError(maxAttempts);\n }\n\n async _sync({\n preference,\n filesVersionHashes,\n localHashes,\n gadgetHashes,\n gadgetFilesVersion,\n }: { preference?: ConflictPreference } & Omit<FileSyncHashes, \"inSync\">): Promise<void> {\n this.log.debug(\"syncing\", { filesVersionHashes, localHashes, gadgetHashes, gadgetFilesVersion });\n let localChanges = getChanges({ from: filesVersionHashes, to: localHashes, existing: gadgetHashes, ignore: [\".gadget/\"] });\n let gadgetChanges = getChanges({ from: filesVersionHashes, to: gadgetHashes, existing: localHashes });\n\n if (localChanges.size === 0 && gadgetChanges.size === 0) {\n // the local filesystem is missing .gadget/ files\n gadgetChanges = getChanges({ from: localHashes, to: gadgetHashes });\n assertAllGadgetFiles({ gadgetChanges });\n }\n\n assert(localChanges.size > 0 || gadgetChanges.size > 0, \"there must be changes if hashes don't match\");\n\n const conflicts = getConflicts({ localChanges, gadgetChanges });\n if (conflicts.size > 0) {\n this.log.debug(\"conflicts detected\", { conflicts });\n\n if (!preference) {\n printConflicts({\n message: sprint`{bold You have conflicting changes with Gadget}`,\n conflicts,\n });\n\n preference = await select({\n message: \"How would you like to resolve these conflicts?\",\n choices: Object.values(ConflictPreference),\n });\n }\n\n switch (preference) {\n case ConflictPreference.CANCEL: {\n process.exit(0);\n break;\n }\n case ConflictPreference.LOCAL: {\n gadgetChanges = withoutConflictingChanges({ conflicts, changes: gadgetChanges });\n break;\n }\n case ConflictPreference.GADGET: {\n localChanges = withoutConflictingChanges({ conflicts, changes: localChanges });\n break;\n }\n }\n }\n\n if (gadgetChanges.size > 0) {\n await this._getChangesFromGadget({ changes: gadgetChanges, filesVersion: gadgetFilesVersion });\n }\n\n if (localChanges.size > 0) {\n await this._sendChangesToGadget({ changes: localChanges, expectedFilesVersion: gadgetFilesVersion });\n }\n }\n\n async hashes(): Promise<FileSyncHashes> {\n const [localHashes, { filesVersionHashes, gadgetHashes, gadgetFilesVersion }] = await Promise.all([\n // get the hashes of our local files\n this.directory.hashes(),\n // get the hashes of our local filesVersion and the latest filesVersion\n (async () => {\n let gadgetFilesVersion: bigint;\n let gadgetHashes: Hashes;\n let filesVersionHashes: Hashes;\n\n if (this.filesVersion === 0n) {\n // this is the first time we're syncing, so just get the\n // hashes of the latest filesVersion\n const { fileSyncHashes } = await this.editGraphQL.query({ query: FILE_SYNC_HASHES_QUERY });\n gadgetFilesVersion = BigInt(fileSyncHashes.filesVersion);\n gadgetHashes = fileSyncHashes.hashes;\n filesVersionHashes = {};\n } else {\n // this isn't the first time we're syncing, so get the hashes\n // of the files at our local filesVersion and the latest\n // filesVersion\n const { fileSyncComparisonHashes } = await this.editGraphQL.query({\n query: FILE_SYNC_COMPARISON_HASHES_QUERY,\n variables: { filesVersion: String(this.filesVersion) },\n });\n gadgetFilesVersion = BigInt(fileSyncComparisonHashes.latestFilesVersionHashes.filesVersion);\n gadgetHashes = fileSyncComparisonHashes.latestFilesVersionHashes.hashes;\n filesVersionHashes = fileSyncComparisonHashes.filesVersionHashes.hashes;\n }\n\n return { filesVersionHashes, gadgetHashes, gadgetFilesVersion };\n })(),\n ]);\n\n return { filesVersionHashes, localHashes, gadgetHashes, gadgetFilesVersion, inSync: isEqualHashes(localHashes, gadgetHashes) };\n }\n\n private async _getChangesFromGadget({\n filesVersion,\n changes,\n }: {\n filesVersion: bigint;\n changes: Changes | ChangesWithHash;\n }): Promise<void> {\n this.log.debug(\"getting changes from gadget\", { filesVersion, changes });\n const created = changes.created();\n const updated = changes.updated();\n\n let files: File[] = [];\n if (created.length > 0 || updated.length > 0) {\n const { fileSyncFiles } = await this.editGraphQL.query({\n query: FILE_SYNC_FILES_QUERY,\n variables: {\n paths: [...created, ...updated],\n filesVersion: String(filesVersion),\n encoding: FileSyncEncoding.Base64,\n },\n });\n\n files = fileSyncFiles.files;\n }\n\n await this._writeToLocalFilesystem({\n filesVersion,\n files,\n delete: changes.deleted(),\n });\n\n printChanges({\n changes,\n tense: \"past\",\n message: sprint`← Received {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n });\n }\n\n private async _sendChangesToGadget({\n expectedFilesVersion = this.filesVersion,\n changes,\n printLimit,\n }: {\n expectedFilesVersion?: bigint;\n changes: Changes;\n printLimit?: number;\n }): Promise<void> {\n this.log.debug(\"sending changes to gadget\", { expectedFilesVersion, changes });\n const changed: FileSyncChangedEventInput[] = [];\n const deleted: FileSyncDeletedEventInput[] = [];\n\n await pMap(changes, async ([normalizedPath, change]) => {\n if (change.type === \"delete\") {\n deleted.push({ path: normalizedPath });\n return;\n }\n\n const absolutePath = this.directory.absolute(normalizedPath);\n\n let stats;\n try {\n stats = await fs.stat(absolutePath);\n } catch (error) {\n swallowEnoent(error);\n this.log.debug(\"skipping change because file doesn't exist\", { path: normalizedPath });\n return;\n }\n\n let content = \"\";\n if (stats.isFile()) {\n content = await fs.readFile(absolutePath, FileSyncEncoding.Base64);\n }\n\n let oldPath;\n if (change.type === \"create\" && change.oldPath) {\n oldPath = change.oldPath;\n }\n\n changed.push({\n content,\n oldPath,\n path: normalizedPath,\n mode: stats.mode,\n encoding: FileSyncEncoding.Base64,\n });\n });\n\n if (changed.length === 0 && deleted.length === 0) {\n this.log.debug(\"skipping send because there are no changes\");\n return;\n }\n\n const {\n publishFileSyncEvents: { remoteFilesVersion },\n } = await this.editGraphQL.query({\n query: PUBLISH_FILE_SYNC_EVENTS_MUTATION,\n variables: {\n input: {\n expectedRemoteFilesVersion: String(expectedFilesVersion),\n changed,\n deleted,\n },\n },\n });\n\n await this._save(remoteFilesVersion);\n\n printChanges({\n changes,\n tense: \"past\",\n message: sprint`→ Sent {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n limit: printLimit,\n });\n }\n\n private async _writeToLocalFilesystem(options: { filesVersion: bigint | string; files: File[]; delete: string[] }): Promise<Changes> {\n const filesVersion = BigInt(options.filesVersion);\n assert(filesVersion >= this.filesVersion, \"filesVersion must be greater than or equal to current filesVersion\");\n\n this.log.debug(\"writing to local filesystem\", {\n filesVersion,\n files: options.files.map((file) => file.path),\n delete: options.delete,\n });\n\n const created: string[] = [];\n const updated: string[] = [];\n\n await pMap(options.delete, async (filepath) => {\n const currentPath = this.directory.absolute(filepath);\n const backupPath = this.directory.absolute(\".gadget/backup\", this.directory.relative(filepath));\n\n // rather than `rm -rf`ing files, we move them to\n // `.gadget/backup/` so that users can recover them if something\n // goes wrong. We've seen a lot of EBUSY/EINVAL errors when moving\n // files so we retry a few times.\n await pRetry(\n async () => {\n try {\n // remove the current backup file in case it exists and is a\n // different type (file vs directory)\n await fs.remove(backupPath);\n await fs.move(currentPath, backupPath);\n } catch (error) {\n // replicate the behavior of `rm -rf` and ignore ENOENT\n swallowEnoent(error);\n }\n },\n {\n retries: 2,\n minTimeout: ms(\"100ms\"),\n onFailedAttempt: (error) => {\n this.log.warn(\"failed to move file to backup\", { error, currentPath, backupPath });\n },\n },\n );\n });\n\n await pMap(options.files, async (file) => {\n const absolutePath = this.directory.absolute(file.path);\n if (await fs.pathExists(absolutePath)) {\n updated.push(file.path);\n } else {\n created.push(file.path);\n }\n\n if (file.path.endsWith(\"/\")) {\n await fs.ensureDir(absolutePath);\n } else {\n await fs.outputFile(absolutePath, Buffer.from(file.content, file.encoding));\n }\n\n if (supportsPermissions) {\n // the os's default umask makes setting the mode during creation\n // not work, so an additional fs.chmod call is necessary to\n // ensure the file has the correct mode\n await fs.chmod(absolutePath, file.mode & 0o777);\n }\n\n if (absolutePath === this.directory.absolute(\".ignore\")) {\n await this.directory.loadIgnoreFile();\n }\n });\n\n await this._save(String(filesVersion));\n\n return new Changes([\n ...created.map((path) => [path, { type: \"create\" }] as const),\n ...updated.map((path) => [path, { type: \"update\" }] as const),\n ...options.delete.map((path) => [path, { type: \"delete\" }] as const),\n ]);\n }\n\n /**\n * Updates {@linkcode _state} and saves it to `.gadget/sync.json`.\n */\n private async _save(filesVersion: string | bigint): Promise<void> {\n this._state = { ...this._state, mtime: Date.now() + 1, filesVersion: String(filesVersion) };\n this.log.debug(\"saving state\", { state: this._state });\n await fs.outputJSON(this.directory.absolute(\".gadget/sync.json\"), this._state, { spaces: 2 });\n }\n}\n\n/**\n * Checks if a directory is empty or non-existent.\n *\n * @param dir - The directory path to check.\n * @returns A Promise that resolves to a boolean indicating whether the directory is empty or non-existent.\n */\nexport const isEmptyOrNonExistentDir = async (dir: string): Promise<boolean> => {\n try {\n for await (const _ of await fs.opendir(dir, { bufferSize: 1 })) {\n return false;\n }\n return true;\n } catch (error) {\n swallowEnoent(error);\n return true;\n }\n};\n\nexport const assertAllGadgetFiles = ({ gadgetChanges }: { gadgetChanges: Changes }): void => {\n assert(\n gadgetChanges.created().length > 0 || gadgetChanges.deleted().length > 0 || gadgetChanges.updated().length > 0,\n \"expected gadgetChanges to have changes\",\n );\n\n const allGadgetFiles = Array.from(gadgetChanges.keys()).every((path) => path.startsWith(\".gadget/\"));\n assert(allGadgetFiles, \"expected all gadgetChanges to be .gadget/ files\");\n};\n\nexport const ConflictPreference = Object.freeze({\n CANCEL: \"Cancel (Ctrl+C)\",\n LOCAL: \"Keep my conflicting changes\",\n GADGET: \"Keep Gadget's conflicting changes\",\n});\n\nexport type ConflictPreference = (typeof ConflictPreference)[keyof typeof ConflictPreference];\n\nexport const ConflictPreferenceArg = (value: string, name: string): ConflictPreference => {\n if ([\"local\", \"gadget\"].includes(value)) {\n return ConflictPreference[value.toUpperCase() as keyof typeof ConflictPreference];\n }\n\n throw new ArgError(sprint`\n ${name} must be {bold local} or {bold gadget}\n\n {bold EXAMPLES:}\n ${name} local\n ${name} gadget\n `);\n};\n"],"names":["dayjs","findUp","fs","ms","assert","path","process","pMap","PQueue","pRetry","z","FileSyncEncoding","getApps","EditGraphQL","FILE_SYNC_COMPARISON_HASHES_QUERY","FILE_SYNC_FILES_QUERY","FILE_SYNC_HASHES_QUERY","PUBLISH_FILE_SYNC_EVENTS_MUTATION","REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION","ArgError","config","homePath","createLogger","select","sprint","sortBySimilar","noop","Changes","printChanges","getConflicts","printConflicts","withoutConflictingChanges","Directory","supportsPermissions","swallowEnoent","InvalidSyncFileError","TooManySyncAttemptsError","getChanges","isEqualHashes","log","name","FileSync","filesVersion","BigInt","_state","mtime","init","options","apps","user","length","email","dir","filepath","join","cwd","windows","startsWith","slice","wasEmptyOrNonExistent","isEmptyOrNonExistentDir","ensureDir","resolve","state","readJson","then","json","object","app","string","number","parse","catch","appSlug","message","choices","map","x","slug","find","similarAppSlugs","concat","directory","force","idle","_syncOperations","onIdle","sendChangesToGadget","changes","add","_sendChangesToGadget","subscribeToGadgetChanges","beforeChanges","afterChanges","onError","editGraphQL","subscribe","query","variables","localFilesVersion","String","onData","remoteFileSyncEvents","changed","deleted","remoteFilesVersion","warn","debug","change","filterIgnoredFiles","file","ignored","ignores","filter","_save","_writeToLocalFilesystem","files","delete","size","format","tense","limit","sync","preference","maxAttempts","pause","attempt","inSync","hashes","info","gadgetFilesVersion","_sync","start","filesVersionHashes","localHashes","gadgetHashes","localChanges","from","to","existing","ignore","gadgetChanges","assertAllGadgetFiles","conflicts","Object","values","ConflictPreference","CANCEL","exit","LOCAL","GADGET","_getChangesFromGadget","expectedFilesVersion","Promise","all","fileSyncHashes","fileSyncComparisonHashes","latestFilesVersionHashes","created","updated","fileSyncFiles","paths","encoding","Base64","printLimit","normalizedPath","type","push","absolutePath","absolute","stats","stat","error","content","isFile","readFile","oldPath","mode","publishFileSyncEvents","input","expectedRemoteFilesVersion","currentPath","backupPath","relative","remove","move","retries","minTimeout","onFailedAttempt","pathExists","endsWith","outputFile","Buffer","chmod","loadIgnoreFile","Date","now","outputJSON","spaces","extend","concurrency","_","opendir","bufferSize","allGadgetFiles","Array","keys","every","freeze","ConflictPreferenceArg","value","includes","toUpperCase"],"mappings":";AAAA,OAAOA,WAAW,QAAQ;AAC1B,SAASC,MAAM,QAAQ,UAAU;AACjC,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,QAAQ,KAAK;AACpB,OAAOC,YAAY,cAAc;AACjC,OAAOC,UAAU,YAAY;AAC7B,OAAOC,aAAa,eAAe;AACnC,OAAOC,UAAU,QAAQ;AACzB,OAAOC,YAAY,UAAU;AAC7B,OAAOC,YAAY,UAAU;AAE7B,SAASC,CAAC,QAAQ,MAAM;AACxB,SAASC,gBAAgB,QAAwE,iCAAiC;AAElI,SAASC,OAAO,QAAQ,gBAAgB;AACxC,SACEC,WAAW,EACXC,iCAAiC,EACjCC,qBAAqB,EACrBC,sBAAsB,EACtBC,iCAAiC,EACjCC,oCAAoC,QAC/B,yBAAyB;AAChC,SAASC,QAAQ,QAAQ,oBAAoB;AAC7C,SAASC,MAAM,EAAEC,QAAQ,QAAQ,sBAAsB;AACvD,SAASC,YAAY,QAAQ,0BAA0B;AACvD,SAASC,MAAM,QAAQ,sBAAsB;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,MAAM2D,wBAAwB,CAACC,OAAe1K;IACnD,IAAI;QAAC;QAAS;KAAS,CAAC2K,QAAQ,CAACD,QAAQ;QACvC,OAAOhE,kBAAkB,CAACgE,MAAME,WAAW,GAAsC;IACnF;IAEA,MAAM,IAAIjM,SAASK,MAAM,CAAC;MACtB,EAAEgB,KAAK;;;QAGL,EAAEA,KAAK;QACP,EAAEA,KAAK;IACX,CAAC;AACL,EAAE"}
|
|
1
|
+
{"version":3,"sources":["../../../src/services/filesync/filesync.ts"],"sourcesContent":["import dayjs from \"dayjs\";\nimport { execa } from \"execa\";\nimport { findUp } from \"find-up\";\nimport fs from \"fs-extra\";\nimport ms from \"ms\";\nimport assert from \"node:assert\";\nimport path from \"node:path\";\nimport process from \"node:process\";\nimport pMap from \"p-map\";\nimport PQueue from \"p-queue\";\nimport pRetry from \"p-retry\";\nimport type { Promisable } from \"type-fest\";\nimport { z } from \"zod\";\nimport { FileSyncEncoding, type FileSyncChangedEventInput, type FileSyncDeletedEventInput } from \"../../__generated__/graphql.js\";\nimport type { App } from \"../app/app.js\";\nimport { getApps } from \"../app/app.js\";\nimport { AppArg } from \"../app/arg.js\";\nimport { Edit } from \"../app/edit/edit.js\";\nimport { EditError } from \"../app/edit/error.js\";\nimport {\n FILE_SYNC_COMPARISON_HASHES_QUERY,\n FILE_SYNC_FILES_QUERY,\n FILE_SYNC_HASHES_QUERY,\n PUBLISH_FILE_SYNC_EVENTS_MUTATION,\n REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION,\n} from \"../app/edit/operation.js\";\nimport { ArgError, type ArgsDefinition } from \"../command/arg.js\";\nimport type { Context } from \"../command/context.js\";\nimport { config, homePath } from \"../config/config.js\";\nimport { select } from \"../output/prompt.js\";\nimport { sprint } from \"../output/sprint.js\";\nimport { getUserOrLogin } from \"../user/user.js\";\nimport { sortBySimilar } from \"../util/collection.js\";\nimport { noop } from \"../util/function.js\";\nimport { isGraphQLErrors, isGraphQLResult, isObject, isString } from \"../util/is.js\";\nimport { Changes, printChanges } from \"./changes.js\";\nimport { getConflicts, printConflicts, withoutConflictingChanges } from \"./conflicts.js\";\nimport { Directory, supportsPermissions, swallowEnoent, type Hashes } from \"./directory.js\";\nimport { InvalidSyncFileError, TooManySyncAttemptsError } from \"./error.js\";\nimport type { File } from \"./file.js\";\nimport { getChanges, isEqualHashes, type ChangesWithHash } from \"./hashes.js\";\n\nexport type FileSyncHashes = {\n inSync: boolean;\n filesVersionHashes: Hashes;\n localHashes: Hashes;\n gadgetHashes: Hashes;\n gadgetFilesVersion: bigint;\n};\n\nexport class FileSync {\n readonly edit: Edit;\n\n /**\n * A FIFO async callback queue that ensures we process filesync events\n * in the order we receive them.\n */\n private _syncOperations = new PQueue({ concurrency: 1 });\n\n private constructor(\n /**\n * The {@linkcode Context} that was used to initialize this\n * {@linkcode FileSync} instance.\n */\n readonly ctx: Context<FileSyncArgs>,\n\n /**\n * The directory that is being synced to.\n */\n readonly directory: Directory,\n\n /**\n * The Gadget application that is being synced to.\n */\n readonly app: App,\n\n /**\n * The state of the filesystem.\n *\n * This is persisted to `.gadget/sync.json` within the {@linkcode directory}.\n */\n private _syncJson: { app: string; filesVersion: string; mtime: number },\n ) {\n this.ctx = ctx.child({ fields: () => ({ filesync: { directory: this.directory.path, filesVersion: this.filesVersion } }) });\n this.edit = new Edit(this.ctx);\n }\n\n /**\n * The last filesVersion that was written to the filesystem.\n *\n * This determines if the filesystem in Gadget is ahead of the\n * filesystem on the local machine.\n */\n get filesVersion(): bigint {\n return BigInt(this._syncJson.filesVersion);\n }\n\n /**\n * The largest mtime that was seen on the filesystem.\n *\n * This is used to determine if any files have changed since the last\n * sync. This does not include the mtime of files that are ignored.\n */\n get mtime(): number {\n return this._syncJson.mtime;\n }\n\n /**\n * Initializes a {@linkcode FileSync} instance.\n * - Ensures the directory exists.\n * - Ensures the directory is empty or contains a `.gadget/sync.json` file (unless `options.force` is `true`)\n * - Ensures an app is specified (either via `options.app` or by prompting the user)\n * - Ensures the specified app matches the app the directory was previously synced to (unless `options.force` is `true`)\n */\n static async init(ctx: Context<FileSyncArgs>): Promise<FileSync> {\n ctx = ctx.child({ name: \"filesync\" });\n\n const user = await getUserOrLogin(ctx);\n const apps = await getApps(ctx);\n if (apps.length === 0) {\n throw new ArgError(\n sprint`\n You (${user.email}) don't have have any Gadget applications.\n\n Visit https://gadget.new to create one!\n `,\n );\n }\n\n let dir = ctx.args._[0];\n if (!dir) {\n // the user didn't specify a directory\n const filepath = await findUp(\".gadget/sync.json\");\n if (filepath) {\n // we found a .gadget/sync.json file, use its parent directory\n dir = path.join(filepath, \"../..\");\n } else {\n // we didn't find a .gadget/sync.json file, use the current directory\n dir = process.cwd();\n }\n }\n\n if (config.windows && dir.startsWith(\"~/\")) {\n // `~` doesn't expand to the home directory on Windows\n dir = homePath(dir.slice(2));\n }\n\n // ensure the root directory is an absolute path and exists\n const wasEmptyOrNonExistent = await isEmptyOrNonExistentDir(dir);\n await fs.ensureDir((dir = path.resolve(dir)));\n\n // try to load the .gadget/sync.json file\n const state = await fs\n .readJson(path.join(dir, \".gadget/sync.json\"))\n .then((json) =>\n z\n .object({\n app: z.string(),\n filesVersion: z.string(),\n mtime: z.number(),\n })\n .parse(json),\n )\n .catch(noop);\n\n let appSlug = ctx.args[\"--app\"] || state?.app;\n if (!appSlug) {\n // the user didn't specify an app, suggest some apps that they can sync to\n appSlug = await select(ctx, {\n message: \"Select the app to sync to\",\n choices: apps.map((x) => x.slug),\n });\n }\n\n // try to find the appSlug in their list of apps\n const app = apps.find((app) => app.slug === appSlug);\n if (!app) {\n // the specified appSlug doesn't exist in their list of apps,\n // either they misspelled it or they don't have access to it\n // anymore, suggest some apps that are similar to the one they\n // specified\n const similarAppSlugs = sortBySimilar(\n appSlug,\n apps.map((app) => app.slug),\n ).slice(0, 5);\n\n throw new ArgError(\n sprint`\n Unknown application:\n\n ${appSlug}\n\n Did you mean one of these?\n\n\n `.concat(` • ${similarAppSlugs.join(\"\\n • \")}`),\n );\n }\n\n ctx.app = app;\n const directory = await Directory.init(dir);\n\n if (!state) {\n // the .gadget/sync.json file didn't exist or contained invalid json\n if (wasEmptyOrNonExistent || ctx.args[\"--force\"]) {\n // the directory was empty or the user passed --force\n // either way, create a fresh .gadget/sync.json file\n return new FileSync(ctx, directory, app, { app: app.slug, filesVersion: \"0\", mtime: 0 });\n }\n\n // the directory isn't empty and the user didn't pass --force\n throw new InvalidSyncFileError(dir, app.slug);\n }\n\n // the .gadget/sync.json file exists\n if (state.app === app.slug) {\n // the .gadget/sync.json file is for the same app that the user specified\n return new FileSync(ctx, directory, app, state);\n }\n\n // the .gadget/sync.json file is for a different app\n if (ctx.args[\"--force\"]) {\n // the user passed --force, so use the app they specified and overwrite everything\n return new FileSync(ctx, directory, app, { app: app.slug, filesVersion: \"0\", mtime: 0 });\n }\n\n // the user didn't pass --force, so throw an error\n throw new ArgError(sprint`\n You were about to sync the following app to the following directory:\n\n {dim ${app.slug}} → {dim ${dir}}\n\n However, that directory has already been synced with this app:\n\n {dim ${state.app}}\n\n If you're sure that you want to sync:\n\n {dim ${app.slug}} → {dim ${dir}}\n\n Then run {dim ggt sync} again with the {dim --force} flag.\n `);\n }\n\n /**\n * Waits for all pending and ongoing filesync operations to complete.\n */\n async idle(): Promise<void> {\n await this._syncOperations.onIdle();\n }\n\n /**\n * Sends file changes to the Gadget.\n *\n * @param options - The options to use.\n * @param options.changes - The changes to send.\n * @returns A promise that resolves when the changes have been sent.\n */\n async sendChangesToGadget({ changes }: { changes: Changes }): Promise<void> {\n await this._syncOperations.add(async () => {\n try {\n await this._sendChangesToGadget({ changes });\n } catch (error) {\n swallowFilesVersionMismatch(this.ctx, error);\n // we either sent the wrong expectedFilesVersion or we received\n // a filesVersion that is greater than the expectedFilesVersion\n // + 1, so we need to stop what we're doing and get in sync\n await this.sync();\n }\n });\n }\n\n /**\n * Subscribes to file changes on Gadget and executes the provided\n * callbacks before and after the changes occur.\n *\n * @param options - The options to use.\n * @param options.beforeChanges - A callback that is called before the changes occur.\n * @param options.afterChanges - A callback that is called after the changes occur.\n * @param options.onError - A callback that is called if an error occurs.\n * @returns A function that unsubscribes from changes on Gadget.\n */\n subscribeToGadgetChanges({\n beforeChanges = noop,\n afterChanges = noop,\n onError,\n }: {\n beforeChanges?: (data: { changed: string[]; deleted: string[] }) => Promisable<void>;\n afterChanges?: (data: { changes: Changes }) => Promisable<void>;\n onError: (error: unknown) => void;\n }): () => void {\n return this.edit.subscribe({\n subscription: REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION,\n // the reason this is a function rather than a static value is\n // so that it will be re-evaluated if the connection is lost and\n // then re-established. this ensures that we send our current\n // filesVersion rather than the one that was sent when we first\n // subscribed\n variables: () => ({ localFilesVersion: String(this.filesVersion) }),\n onError,\n onData: ({ remoteFileSyncEvents: { changed, deleted, remoteFilesVersion } }) => {\n this._syncOperations\n .add(async () => {\n if (BigInt(remoteFilesVersion) < this.filesVersion) {\n this.ctx.log.warn(\"skipping received changes because files version is outdated\", { filesVersion: remoteFilesVersion });\n return;\n }\n\n this.ctx.log.debug(\"received files\", {\n remoteFilesVersion,\n changed: changed.map((change) => change.path),\n deleted: deleted.map((change) => change.path),\n });\n\n const filterIgnoredFiles = (file: { path: string }): boolean => {\n const ignored = this.directory.ignores(file.path);\n if (ignored) {\n this.ctx.log.warn(\"skipping received change because file is ignored\", { path: file.path });\n }\n return !ignored;\n };\n\n changed = changed.filter(filterIgnoredFiles);\n deleted = deleted.filter(filterIgnoredFiles);\n\n if (changed.length === 0 && deleted.length === 0) {\n await this._save(remoteFilesVersion);\n return;\n }\n\n await beforeChanges({\n changed: changed.map((file) => file.path),\n deleted: deleted.map((file) => file.path),\n });\n\n const changes = await this._writeToLocalFilesystem({\n filesVersion: remoteFilesVersion,\n files: changed,\n delete: deleted.map((file) => file.path),\n });\n\n if (changes.size > 0) {\n printChanges(this.ctx, {\n message: sprint`← Received {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n changes,\n tense: \"past\",\n limit: 10,\n });\n }\n\n await afterChanges({ changes });\n })\n .catch(onError);\n },\n });\n }\n\n /**\n * Ensures the local filesystem is in sync with Gadget's filesystem.\n * - All non-conflicting changes are automatically merged.\n * - Conflicts are resolved by prompting the user to either keep their local changes or keep Gadget's changes.\n * - This function will not return until the filesystem is in sync.\n */\n async sync({ maxAttempts = 10 }: { maxAttempts?: number } = {}): Promise<void> {\n let attempt = 0;\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition\n while (true) {\n const { inSync, ...hashes } = await this.hashes();\n\n if (inSync) {\n this._syncOperations.clear();\n this.ctx.log.info(\"filesystem is in sync\", { attempt });\n await this._save(hashes.gadgetFilesVersion);\n return;\n }\n\n if (attempt++ >= maxAttempts) {\n throw new TooManySyncAttemptsError(maxAttempts);\n }\n\n try {\n this.ctx.log.info(\"syncing\", { attempt, ...hashes });\n await this._sync(hashes);\n } catch (error) {\n swallowFilesVersionMismatch(this.ctx, error);\n // we either sent the wrong expectedFilesVersion or we received\n // a filesVersion that is greater than the expectedFilesVersion\n // + 1, so try again\n }\n }\n }\n\n async _sync({ filesVersionHashes, localHashes, gadgetHashes, gadgetFilesVersion }: Omit<FileSyncHashes, \"inSync\">): Promise<void> {\n let localChanges = getChanges(this.ctx, { from: filesVersionHashes, to: localHashes, existing: gadgetHashes, ignore: [\".gadget/\"] });\n let gadgetChanges = getChanges(this.ctx, { from: filesVersionHashes, to: gadgetHashes, existing: localHashes });\n\n if (localChanges.size === 0 && gadgetChanges.size === 0) {\n // the local filesystem is missing .gadget/ files\n gadgetChanges = getChanges(this.ctx, { from: localHashes, to: gadgetHashes });\n assertAllGadgetFiles({ gadgetChanges });\n }\n\n assert(localChanges.size > 0 || gadgetChanges.size > 0, \"there must be changes if hashes don't match\");\n\n const conflicts = getConflicts({ localChanges, gadgetChanges });\n if (conflicts.size > 0) {\n this.ctx.log.debug(\"conflicts detected\", { conflicts });\n\n let preference = this.ctx.args[\"--prefer\"];\n if (!preference) {\n printConflicts(this.ctx, {\n message: sprint`{bold You have conflicting changes with Gadget}`,\n conflicts,\n });\n\n preference = await select(this.ctx, {\n message: \"How would you like to resolve these conflicts?\",\n choices: Object.values(ConflictPreference),\n });\n }\n\n switch (preference) {\n case ConflictPreference.CANCEL: {\n process.exit(0);\n break;\n }\n case ConflictPreference.LOCAL: {\n gadgetChanges = withoutConflictingChanges({ conflicts, changes: gadgetChanges });\n break;\n }\n case ConflictPreference.GADGET: {\n localChanges = withoutConflictingChanges({ conflicts, changes: localChanges });\n break;\n }\n }\n }\n\n if (gadgetChanges.size > 0) {\n await this._getChangesFromGadget({ changes: gadgetChanges, filesVersion: gadgetFilesVersion });\n }\n\n if (localChanges.size > 0) {\n await this._sendChangesToGadget({ changes: localChanges, expectedFilesVersion: gadgetFilesVersion });\n }\n }\n\n async hashes(): Promise<FileSyncHashes> {\n const [localHashes, { filesVersionHashes, gadgetHashes, gadgetFilesVersion }] = await Promise.all([\n // get the hashes of our local files\n this.directory.hashes(),\n // get the hashes of our local filesVersion and the latest filesVersion\n (async () => {\n let gadgetFilesVersion: bigint;\n let gadgetHashes: Hashes;\n let filesVersionHashes: Hashes;\n\n if (this.filesVersion === 0n) {\n // this is the first time we're syncing, so just get the\n // hashes of the latest filesVersion\n const { fileSyncHashes } = await this.edit.query({ query: FILE_SYNC_HASHES_QUERY });\n gadgetFilesVersion = BigInt(fileSyncHashes.filesVersion);\n gadgetHashes = fileSyncHashes.hashes;\n filesVersionHashes = {};\n } else {\n // this isn't the first time we're syncing, so get the hashes\n // of the files at our local filesVersion and the latest\n // filesVersion\n const { fileSyncComparisonHashes } = await this.edit.query({\n query: FILE_SYNC_COMPARISON_HASHES_QUERY,\n variables: { filesVersion: String(this.filesVersion) },\n });\n gadgetFilesVersion = BigInt(fileSyncComparisonHashes.latestFilesVersionHashes.filesVersion);\n gadgetHashes = fileSyncComparisonHashes.latestFilesVersionHashes.hashes;\n filesVersionHashes = fileSyncComparisonHashes.filesVersionHashes.hashes;\n }\n\n return { filesVersionHashes, gadgetHashes, gadgetFilesVersion };\n })(),\n ]);\n\n return {\n filesVersionHashes,\n localHashes,\n gadgetHashes,\n gadgetFilesVersion,\n inSync: isEqualHashes(this.ctx, localHashes, gadgetHashes),\n };\n }\n\n private async _getChangesFromGadget({\n filesVersion,\n changes,\n }: {\n filesVersion: bigint;\n changes: Changes | ChangesWithHash;\n }): Promise<void> {\n this.ctx.log.debug(\"getting changes from gadget\", { filesVersion, changes });\n const created = changes.created();\n const updated = changes.updated();\n\n let files: File[] = [];\n if (created.length > 0 || updated.length > 0) {\n const { fileSyncFiles } = await this.edit.query({\n query: FILE_SYNC_FILES_QUERY,\n variables: {\n paths: [...created, ...updated],\n filesVersion: String(filesVersion),\n encoding: FileSyncEncoding.Base64,\n },\n });\n\n files = fileSyncFiles.files;\n }\n\n await this._writeToLocalFilesystem({\n filesVersion,\n files,\n delete: changes.deleted(),\n });\n\n printChanges(this.ctx, {\n changes,\n tense: \"past\",\n message: sprint`← Received {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n });\n }\n\n private async _sendChangesToGadget({\n expectedFilesVersion = this.filesVersion,\n changes,\n printLimit,\n }: {\n expectedFilesVersion?: bigint;\n changes: Changes;\n printLimit?: number;\n }): Promise<void> {\n this.ctx.log.debug(\"sending changes to gadget\", { expectedFilesVersion, changes });\n const changed: FileSyncChangedEventInput[] = [];\n const deleted: FileSyncDeletedEventInput[] = [];\n\n await pMap(changes, async ([normalizedPath, change]) => {\n if (change.type === \"delete\") {\n deleted.push({ path: normalizedPath });\n return;\n }\n\n const absolutePath = this.directory.absolute(normalizedPath);\n\n let stats;\n try {\n stats = await fs.stat(absolutePath);\n } catch (error) {\n swallowEnoent(error);\n this.ctx.log.debug(\"skipping change because file doesn't exist\", { path: normalizedPath });\n return;\n }\n\n let content = \"\";\n if (stats.isFile()) {\n content = await fs.readFile(absolutePath, FileSyncEncoding.Base64);\n }\n\n let oldPath;\n if (change.type === \"create\" && change.oldPath) {\n oldPath = change.oldPath;\n }\n\n changed.push({\n content,\n oldPath,\n path: normalizedPath,\n mode: stats.mode,\n encoding: FileSyncEncoding.Base64,\n });\n });\n\n if (changed.length === 0 && deleted.length === 0) {\n this.ctx.log.debug(\"skipping send because there are no changes\");\n return;\n }\n\n const {\n publishFileSyncEvents: { remoteFilesVersion },\n } = await this.edit.mutate({\n mutation: PUBLISH_FILE_SYNC_EVENTS_MUTATION,\n variables: {\n input: {\n expectedRemoteFilesVersion: String(expectedFilesVersion),\n changed,\n deleted,\n },\n },\n http: {\n retry: {\n // we can retry this request because\n // expectedRemoteFilesVersion makes it idempotent\n methods: [\"POST\"],\n calculateDelay: ({ error, computedValue }) => {\n if (isFilesVersionMismatchError(error.response?.body)) {\n // don't retry if we get a files version mismatch error\n return 0;\n }\n return computedValue;\n },\n },\n },\n });\n\n printChanges(this.ctx, {\n changes,\n tense: \"past\",\n message: sprint`→ Sent {gray ${dayjs().format(\"hh:mm:ss A\")}}`,\n limit: printLimit,\n });\n\n if (BigInt(remoteFilesVersion) > expectedFilesVersion + 1n) {\n // we can't save the remoteFilesVersion because we haven't\n // received the intermediate filesVersions yet\n throw new Error(\"Files version mismatch\");\n }\n\n await this._save(remoteFilesVersion);\n }\n\n private async _writeToLocalFilesystem(options: { filesVersion: bigint | string; files: File[]; delete: string[] }): Promise<Changes> {\n const filesVersion = BigInt(options.filesVersion);\n assert(filesVersion >= this.filesVersion, \"filesVersion must be greater than or equal to current filesVersion\");\n\n this.ctx.log.debug(\"writing to local filesystem\", {\n filesVersion,\n files: options.files.map((file) => file.path),\n delete: options.delete,\n });\n\n const created: string[] = [];\n const updated: string[] = [];\n\n await pMap(options.delete, async (filepath) => {\n const currentPath = this.directory.absolute(filepath);\n const backupPath = this.directory.absolute(\".gadget/backup\", this.directory.relative(filepath));\n\n // rather than `rm -rf`ing files, we move them to\n // `.gadget/backup/` so that users can recover them if something\n // goes wrong. We've seen a lot of EBUSY/EINVAL errors when moving\n // files so we retry a few times.\n await pRetry(\n async () => {\n try {\n // remove the current backup file in case it exists and is a\n // different type (file vs directory)\n await fs.remove(backupPath);\n await fs.move(currentPath, backupPath);\n } catch (error) {\n // replicate the behavior of `rm -rf` and ignore ENOENT\n swallowEnoent(error);\n }\n },\n {\n retries: 2,\n minTimeout: ms(\"100ms\"),\n onFailedAttempt: (error) => {\n this.ctx.log.warn(\"failed to move file to backup\", { error, currentPath, backupPath });\n },\n },\n );\n });\n\n await pMap(options.files, async (file) => {\n const absolutePath = this.directory.absolute(file.path);\n if (await fs.pathExists(absolutePath)) {\n updated.push(file.path);\n } else {\n created.push(file.path);\n }\n\n if (file.path.endsWith(\"/\")) {\n await fs.ensureDir(absolutePath);\n } else {\n await fs.outputFile(absolutePath, Buffer.from(file.content, file.encoding));\n }\n\n if (supportsPermissions) {\n // the os's default umask makes setting the mode during creation\n // not work, so an additional fs.chmod call is necessary to\n // ensure the file has the correct mode\n await fs.chmod(absolutePath, file.mode & 0o777);\n }\n\n if (absolutePath === this.directory.absolute(\".ignore\")) {\n await this.directory.loadIgnoreFile();\n }\n });\n\n await this._save(String(filesVersion));\n\n const changes = new Changes([\n ...created.map((path) => [path, { type: \"create\" }] as const),\n ...updated.map((path) => [path, { type: \"update\" }] as const),\n ...options.delete.map((path) => [path, { type: \"delete\" }] as const),\n ]);\n\n if (changes.has(\"yarn.lock\")) {\n this.ctx.log.info(\"running yarn install --check-files\");\n await execa(\"yarn\", [\"install\", \"--check-files\"], { cwd: this.directory.path })\n .then(() => this.ctx.log.info(\"yarn install complete\"))\n .catch((error: unknown) => this.ctx.log.error(\"yarn install failed\", { error }));\n }\n\n return changes;\n }\n\n /**\n * Updates {@linkcode _syncJson} and saves it to `.gadget/sync.json`.\n */\n private async _save(filesVersion: string | bigint): Promise<void> {\n this._syncJson = { ...this._syncJson, mtime: Date.now() + 1, filesVersion: String(filesVersion) };\n this.ctx.log.debug(\"saving .gadget/sync.json\");\n await fs.outputJSON(this.directory.absolute(\".gadget/sync.json\"), this._syncJson, { spaces: 2 });\n }\n}\n\n/**\n * Checks if a directory is empty or non-existent.\n *\n * @param dir - The directory path to check.\n * @returns A Promise that resolves to a boolean indicating whether the directory is empty or non-existent.\n */\nexport const isEmptyOrNonExistentDir = async (dir: string): Promise<boolean> => {\n try {\n for await (const _ of await fs.opendir(dir, { bufferSize: 1 })) {\n return false;\n }\n return true;\n } catch (error) {\n swallowEnoent(error);\n return true;\n }\n};\n\nexport const assertAllGadgetFiles = ({ gadgetChanges }: { gadgetChanges: Changes }): void => {\n assert(\n gadgetChanges.created().length > 0 || gadgetChanges.deleted().length > 0 || gadgetChanges.updated().length > 0,\n \"expected gadgetChanges to have changes\",\n );\n\n const allGadgetFiles = Array.from(gadgetChanges.keys()).every((path) => path.startsWith(\".gadget/\"));\n assert(allGadgetFiles, \"expected all gadgetChanges to be .gadget/ files\");\n};\n\nexport const ConflictPreference = Object.freeze({\n CANCEL: \"Cancel (Ctrl+C)\",\n LOCAL: \"Keep my conflicting changes\",\n GADGET: \"Keep Gadget's conflicting changes\",\n});\n\nexport type ConflictPreference = (typeof ConflictPreference)[keyof typeof ConflictPreference];\n\nexport const ConflictPreferenceArg = (value: string, name: string): ConflictPreference => {\n if ([\"local\", \"gadget\"].includes(value)) {\n return ConflictPreference[value.toUpperCase() as keyof typeof ConflictPreference];\n }\n\n throw new ArgError(sprint`\n ${name} must be {bold local} or {bold gadget}\n\n {bold EXAMPLES:}\n ${name} local\n ${name} gadget\n `);\n};\n\nexport const FileSyncArgs = {\n \"--app\": { type: AppArg, alias: \"-a\" },\n \"--prefer\": ConflictPreferenceArg,\n \"--force\": Boolean,\n} satisfies ArgsDefinition;\n\nexport type FileSyncArgs = typeof FileSyncArgs;\n\nexport const isFilesVersionMismatchError = (error: unknown): boolean => {\n if (error instanceof EditError) {\n error = error.cause;\n }\n if (isGraphQLResult(error)) {\n error = error.errors;\n }\n if (isGraphQLErrors(error)) {\n error = error[0];\n }\n return isObject(error) && \"message\" in error && isString(error.message) && error.message.startsWith(\"Files version mismatch\");\n};\n\nconst swallowFilesVersionMismatch = (ctx: Context, error: unknown): void => {\n if (isFilesVersionMismatchError(error)) {\n ctx.log.debug(\"swallowing files version mismatch\", { error });\n return;\n }\n throw error;\n};\n"],"names":["dayjs","execa","findUp","fs","ms","assert","path","process","pMap","PQueue","pRetry","z","FileSyncEncoding","getApps","AppArg","Edit","EditError","FILE_SYNC_COMPARISON_HASHES_QUERY","FILE_SYNC_FILES_QUERY","FILE_SYNC_HASHES_QUERY","PUBLISH_FILE_SYNC_EVENTS_MUTATION","REMOTE_FILE_SYNC_EVENTS_SUBSCRIPTION","ArgError","config","homePath","select","sprint","getUserOrLogin","sortBySimilar","noop","isGraphQLErrors","isGraphQLResult","isObject","isString","Changes","printChanges","getConflicts","printConflicts","withoutConflictingChanges","Directory","supportsPermissions","swallowEnoent","InvalidSyncFileError","TooManySyncAttemptsError","getChanges","isEqualHashes","FileSync","filesVersion","BigInt","_syncJson","mtime","init","ctx","child","name","user","apps","length","email","dir","args","_","filepath","join","cwd","windows","startsWith","slice","wasEmptyOrNonExistent","isEmptyOrNonExistentDir","ensureDir","resolve","state","readJson","then","json","object","app","string","number","parse","catch","appSlug","message","choices","map","x","slug","find","similarAppSlugs","concat","directory","idle","_syncOperations","onIdle","sendChangesToGadget","changes","add","_sendChangesToGadget","error","swallowFilesVersionMismatch","sync","subscribeToGadgetChanges","beforeChanges","afterChanges","onError","edit","subscribe","subscription","variables","localFilesVersion","String","onData","remoteFileSyncEvents","changed","deleted","remoteFilesVersion","log","warn","debug","change","filterIgnoredFiles","file","ignored","ignores","filter","_save","_writeToLocalFilesystem","files","delete","size","format","tense","limit","maxAttempts","attempt","inSync","hashes","clear","info","gadgetFilesVersion","_sync","filesVersionHashes","localHashes","gadgetHashes","localChanges","from","to","existing","ignore","gadgetChanges","assertAllGadgetFiles","conflicts","preference","Object","values","ConflictPreference","CANCEL","exit","LOCAL","GADGET","_getChangesFromGadget","expectedFilesVersion","Promise","all","fileSyncHashes","query","fileSyncComparisonHashes","latestFilesVersionHashes","created","updated","fileSyncFiles","paths","encoding","Base64","printLimit","normalizedPath","type","push","absolutePath","absolute","stats","stat","content","isFile","readFile","oldPath","mode","publishFileSyncEvents","mutate","mutation","input","expectedRemoteFilesVersion","http","retry","methods","calculateDelay","computedValue","isFilesVersionMismatchError","response","body","Error","options","currentPath","backupPath","relative","remove","move","retries","minTimeout","onFailedAttempt","pathExists","endsWith","outputFile","Buffer","chmod","loadIgnoreFile","has","Date","now","outputJSON","spaces","concurrency","fields","filesync","opendir","bufferSize","allGadgetFiles","Array","keys","every","freeze","ConflictPreferenceArg","value","includes","toUpperCase","FileSyncArgs","alias","Boolean","cause","errors"],"mappings":";AAAA,OAAOA,WAAW,QAAQ;AAC1B,SAASC,KAAK,QAAQ,QAAQ;AAC9B,SAASC,MAAM,QAAQ,UAAU;AACjC,OAAOC,QAAQ,WAAW;AAC1B,OAAOC,QAAQ,KAAK;AACpB,OAAOC,YAAY,cAAc;AACjC,OAAOC,UAAU,YAAY;AAC7B,OAAOC,aAAa,eAAe;AACnC,OAAOC,UAAU,QAAQ;AACzB,OAAOC,YAAY,UAAU;AAC7B,OAAOC,YAAY,UAAU;AAE7B,SAASC,CAAC,QAAQ,MAAM;AACxB,SAASC,gBAAgB,QAAwE,iCAAiC;AAElI,SAASC,OAAO,QAAQ,gBAAgB;AACxC,SAASC,MAAM,QAAQ,gBAAgB;AACvC,SAASC,IAAI,QAAQ,sBAAsB;AAC3C,SAASC,SAAS,QAAQ,uBAAuB;AACjD,SACEC,iCAAiC,EACjCC,qBAAqB,EACrBC,sBAAsB,EACtBC,iCAAiC,EACjCC,oCAAoC,QAC/B,2BAA2B;AAClC,SAASC,QAAQ,QAA6B,oBAAoB;AAElE,SAASC,MAAM,EAAEC,QAAQ,QAAQ,sBAAsB;AACvD,SAASC,MAAM,QAAQ,sBAAsB;AAC7C,SAASC,MAAM,QAAQ,sBAAsB;AAC7C,SAASC,cAAc,QAAQ,kBAAkB;AACjD,SAASC,aAAa,QAAQ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Ac+C;IACjD,IAAImG,4BAA4BnG,QAAQ;QACtC/C,IAAIiE,GAAG,CAACE,KAAK,CAAC,qCAAqC;YAAEpB;QAAM;QAC3D;IACF;IACA,MAAMA;AACR"}
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
|
-
import { createLogger } from "../output/log/logger.js";
|
|
3
|
-
const log = createLogger({
|
|
4
|
-
name: "hashes"
|
|
5
|
-
});
|
|
6
2
|
export class ChangesWithHash extends Map {
|
|
7
3
|
created() {
|
|
8
4
|
return Array.from(this.entries()).filter(([, change])=>change.type === "create").map(([path])=>path);
|
|
@@ -22,7 +18,14 @@ export class ChangesWithHash extends Map {
|
|
|
22
18
|
*
|
|
23
19
|
* If `ignore` is provided, any changes that were made to a path that
|
|
24
20
|
* starts with any of the `ignore` paths are skipped.
|
|
25
|
-
|
|
21
|
+
*
|
|
22
|
+
* @param ctx - The current context.
|
|
23
|
+
* @param options - The options to use.
|
|
24
|
+
* @param options.from - The source hashes.
|
|
25
|
+
* @param options.to - The target hashes.
|
|
26
|
+
* @param options.existing - The existing hashes.
|
|
27
|
+
* @param options.ignore - The paths to ignore.
|
|
28
|
+
*/ export const getChanges = (ctx, { from: source, to: target, existing, ignore })=>{
|
|
26
29
|
const changes = new ChangesWithHash();
|
|
27
30
|
const targetPaths = Object.keys(target);
|
|
28
31
|
for (const [sourcePath, sourceHash] of Object.entries(source)){
|
|
@@ -40,7 +43,7 @@ export class ChangesWithHash extends Map {
|
|
|
40
43
|
type: "delete",
|
|
41
44
|
sourceHash
|
|
42
45
|
});
|
|
43
|
-
log.trace("file deleted", {
|
|
46
|
+
ctx.log.trace("file deleted", {
|
|
44
47
|
path: sourcePath,
|
|
45
48
|
sourceHash
|
|
46
49
|
});
|
|
@@ -53,7 +56,7 @@ export class ChangesWithHash extends Map {
|
|
|
53
56
|
sourceHash,
|
|
54
57
|
targetHash
|
|
55
58
|
});
|
|
56
|
-
log.trace("file updated", {
|
|
59
|
+
ctx.log.trace("file updated", {
|
|
57
60
|
path: sourcePath,
|
|
58
61
|
sourceHash,
|
|
59
62
|
targetHash
|
|
@@ -72,7 +75,7 @@ export class ChangesWithHash extends Map {
|
|
|
72
75
|
type: "create",
|
|
73
76
|
targetHash
|
|
74
77
|
});
|
|
75
|
-
log.trace("file created", {
|
|
78
|
+
ctx.log.trace("file created", {
|
|
76
79
|
path: targetPath,
|
|
77
80
|
targetHash
|
|
78
81
|
});
|
|
@@ -81,27 +84,27 @@ export class ChangesWithHash extends Map {
|
|
|
81
84
|
if (!existing) {
|
|
82
85
|
return changes;
|
|
83
86
|
}
|
|
84
|
-
return withoutUnnecessaryChanges({
|
|
87
|
+
return withoutUnnecessaryChanges(ctx, {
|
|
85
88
|
changes,
|
|
86
89
|
existing
|
|
87
90
|
});
|
|
88
91
|
};
|
|
89
92
|
/**
|
|
90
93
|
* Filters out changes that the existing filesystem already has.
|
|
91
|
-
*/ export const withoutUnnecessaryChanges = ({ changes, existing })=>{
|
|
94
|
+
*/ export const withoutUnnecessaryChanges = (ctx, { changes, existing })=>{
|
|
92
95
|
const necessaryChanges = new ChangesWithHash();
|
|
93
96
|
for (const [path, change] of changes){
|
|
94
97
|
const existingHash = existing[path];
|
|
95
98
|
if (change.type === "delete" && !existingHash) {
|
|
96
99
|
// already deleted
|
|
97
|
-
log.trace("already deleted", {
|
|
100
|
+
ctx.log.trace("already deleted", {
|
|
98
101
|
path
|
|
99
102
|
});
|
|
100
103
|
continue;
|
|
101
104
|
}
|
|
102
105
|
if (change.type !== "delete" && existingHash && isEqualHash(path, change.targetHash, existingHash)) {
|
|
103
106
|
// already created or updated
|
|
104
|
-
log.trace("already created or updated", {
|
|
107
|
+
ctx.log.trace("already created or updated", {
|
|
105
108
|
path,
|
|
106
109
|
existingHash,
|
|
107
110
|
targetHash: change.targetHash
|
|
@@ -138,11 +141,11 @@ export const isEqualHash = (_path, aHash, bHash)=>{
|
|
|
138
141
|
// // so ensure the permissions are also the same
|
|
139
142
|
// return aHash.permissions === bHash.permissions;
|
|
140
143
|
};
|
|
141
|
-
export const isEqualHashes = (a, b)=>{
|
|
144
|
+
export const isEqualHashes = (ctx, a, b)=>{
|
|
142
145
|
for (const [aPath, aHash] of Object.entries(a)){
|
|
143
146
|
const bHash = b[aPath];
|
|
144
147
|
if (!bHash || !isEqualHash(aPath, aHash, bHash)) {
|
|
145
|
-
log.debug("hashes are not equal", {
|
|
148
|
+
ctx.log.debug("hashes are not equal", {
|
|
146
149
|
path: aPath,
|
|
147
150
|
aHash,
|
|
148
151
|
bHash
|
|
@@ -152,7 +155,7 @@ export const isEqualHashes = (a, b)=>{
|
|
|
152
155
|
}
|
|
153
156
|
for (const bPath of Object.keys(b)){
|
|
154
157
|
if (!a[bPath]) {
|
|
155
|
-
log.debug("hashes are not equal", {
|
|
158
|
+
ctx.log.debug("hashes are not equal", {
|
|
156
159
|
path: bPath,
|
|
157
160
|
aHash: undefined,
|
|
158
161
|
bHash: b[bPath]
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/filesync/hashes.ts"],"sourcesContent":["import assert from \"node:assert\";\nimport { createLogger } from \"../output/log/logger.js\";\nimport { type Create, type Delete, type Update } from \"./changes.js\";\nimport type { Hash, Hashes } from \"./directory.js\";\n\nconst log = createLogger({ name: \"hashes\" });\n\nexport type CreateWithHash = Create & { targetHash: Hash };\nexport type UpdateWithHash = Update & { sourceHash: Hash; targetHash: Hash };\nexport type DeleteWithHash = Delete & { sourceHash: Hash };\nexport type ChangeWithHash = CreateWithHash | UpdateWithHash | DeleteWithHash;\n\nexport class ChangesWithHash extends Map<string, ChangeWithHash> {\n created(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"create\")\n .map(([path]) => path);\n }\n\n updated(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"update\")\n .map(([path]) => path);\n }\n\n deleted(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"delete\")\n .map(([path]) => path);\n }\n}\n\n/**\n * Calculates the changes that were made to `from` to make it end up as `to`.\n *\n * If `existing` is provided, only the changes that are necessary to\n * apply to `existing` are returned.\n *\n * If `ignore` is provided, any changes that were made to a path that\n * starts with any of the `ignore` paths are skipped.\n */\nexport const getChanges = ({\n from: source,\n to: target,\n existing,\n ignore,\n}: {\n from: Hashes;\n to: Hashes;\n existing?: Hashes;\n ignore?: string[];\n}): ChangesWithHash => {\n const changes = new ChangesWithHash();\n const targetPaths = Object.keys(target);\n\n for (const [sourcePath, sourceHash] of Object.entries(source)) {\n if (ignore?.some((ignored) => sourcePath.startsWith(ignored))) {\n continue;\n }\n\n const targetHash = target[sourcePath];\n if (!targetHash) {\n if (!sourcePath.endsWith(\"/\") || !targetPaths.some((targetPath) => targetPath.startsWith(sourcePath))) {\n // sourcePath is a file and it doesn't exist in target OR\n // sourcePath is a directory and target doesn't have any\n // existing files inside it, therefor the sourcePath has been\n // deleted\n changes.set(sourcePath, { type: \"delete\", sourceHash });\n log.trace(\"file deleted\", { path: sourcePath, sourceHash });\n }\n } else if (!isEqualHash(sourcePath, sourceHash, targetHash)) {\n // the file or directory exists in target, but has a different\n // hash, so it's been updated\n changes.set(sourcePath, { type: \"update\", sourceHash, targetHash });\n log.trace(\"file updated\", { path: sourcePath, sourceHash, targetHash });\n }\n }\n\n for (const targetPath of targetPaths) {\n if (ignore?.some((ignored) => targetPath.startsWith(ignored))) {\n continue;\n }\n\n if (!source[targetPath]) {\n // the targetPath doesn't exist in source, so it's been created\n const targetHash = target[targetPath];\n assert(targetHash, \"targetHash should exist\");\n\n changes.set(targetPath, { type: \"create\", targetHash });\n log.trace(\"file created\", { path: targetPath, targetHash });\n }\n }\n\n if (!existing) {\n return changes;\n }\n\n return withoutUnnecessaryChanges({ changes, existing });\n};\n\n/**\n * Filters out changes that the existing filesystem already has.\n */\nexport const withoutUnnecessaryChanges = ({ changes, existing }: { changes: ChangesWithHash; existing: Hashes }): ChangesWithHash => {\n const necessaryChanges = new ChangesWithHash();\n\n for (const [path, change] of changes) {\n const existingHash = existing[path];\n if (change.type === \"delete\" && !existingHash) {\n // already deleted\n log.trace(\"already deleted\", { path });\n continue;\n }\n\n if (change.type !== \"delete\" && existingHash && isEqualHash(path, change.targetHash, existingHash)) {\n // already created or updated\n log.trace(\"already created or updated\", { path, existingHash, targetHash: change.targetHash });\n continue;\n }\n\n // we could do this:\n // if (change.type === \"update\" && !existingHash) {\n // change = { type: \"create\", targetHash: change.targetHash };\n // }\n // but, changing the type makes the output look confusing and it\n // doesn't change the outcome, so we just leave it as is\n\n necessaryChanges.set(path, change);\n }\n\n return necessaryChanges;\n};\n\nexport const isEqualHash = (_path: string, aHash: Hash, bHash: Hash): boolean => {\n // FIXME: we're running into issues syncing permissions with Gadget\n // so we're temporarily disabling this check until we can figure out\n return aHash.sha1 === bHash.sha1;\n\n // if (aHash.sha1 !== bHash.sha1) {\n // // the contents are different\n // return false;\n // }\n\n // if (path.endsWith(\"/\")) {\n // // it's a directory, so we don't care about permissions\n // return true;\n // }\n\n // if (isNil(aHash.permissions) || isNil(bHash.permissions)) {\n // // one of the filesystems doesn't support permissions, so ignore them\n // return true;\n // }\n\n // // the contents are the same, and both filesystems support permissions\n // // so ensure the permissions are also the same\n // return aHash.permissions === bHash.permissions;\n};\n\nexport const isEqualHashes = (a: Hashes, b: Hashes): boolean => {\n for (const [aPath, aHash] of Object.entries(a)) {\n const bHash = b[aPath];\n if (!bHash || !isEqualHash(aPath, aHash, bHash)) {\n log.debug(\"hashes are not equal\", { path: aPath, aHash, bHash });\n return false;\n }\n }\n\n for (const bPath of Object.keys(b)) {\n if (!a[bPath]) {\n log.debug(\"hashes are not equal\", { path: bPath, aHash: undefined, bHash: b[bPath] });\n return false;\n }\n }\n\n return true;\n};\n"],"names":["assert","createLogger","log","name","ChangesWithHash","Map","created","Array","from","entries","filter","change","type","map","path","updated","deleted","getChanges","source","to","target","existing","ignore","changes","targetPaths","Object","keys","sourcePath","sourceHash","some","ignored","startsWith","targetHash","endsWith","targetPath","set","trace","isEqualHash","withoutUnnecessaryChanges","necessaryChanges","existingHash","_path","aHash","bHash","sha1","isEqualHashes","a","b","aPath","debug","bPath","undefined"],"mappings":"AAAA,OAAOA,YAAY,cAAc;AACjC,SAASC,YAAY,QAAQ,0BAA0B;AAIvD,MAAMC,MAAMD,aAAa;IAAEE,MAAM;AAAS;AAO1C,OAAO,MAAMC,wBAAwBC;IACnCC,UAAoB;QAClB,OAAOC,MAAMC,IAAI,CAAC,IAAI,CAACC,OAAO,IAC3BC,MAAM,CAAC,CAAC,GAAGC,OAAO,GAAKA,OAAOC,IAAI,KAAK,UACvCC,GAAG,CAAC,CAAC,CAACC,KAAK,GAAKA;IACrB;IAEAC,UAAoB;QAClB,OAAOR,MAAMC,IAAI,CAAC,IAAI,CAACC,OAAO,IAC3BC,MAAM,CAAC,CAAC,GAAGC,OAAO,GAAKA,OAAOC,IAAI,KAAK,UACvCC,GAAG,CAAC,CAAC,CAACC,KAAK,GAAKA;IACrB;IAEAE,UAAoB;QAClB,OAAOT,MAAMC,IAAI,CAAC,IAAI,CAACC,OAAO,IAC3BC,MAAM,CAAC,CAAC,GAAGC,OAAO,GAAKA,OAAOC,IAAI,KAAK,UACvCC,GAAG,CAAC,CAAC,CAACC,KAAK,GAAKA;IACrB;AACF;AAEA;;;;;;;;CAQC,GACD,OAAO,MAAMG,aAAa,CAAC,EACzBT,MAAMU,MAAM,EACZC,IAAIC,MAAM,EACVC,QAAQ,EACRC,MAAM,EAMP;IACC,MAAMC,UAAU,IAAInB;IACpB,MAAMoB,cAAcC,OAAOC,IAAI,CAACN;IAEhC,KAAK,MAAM,CAACO,YAAYC,WAAW,IAAIH,OAAOhB,OAAO,CAACS,QAAS;QAC7D,IAAII,QAAQO,KAAK,CAACC,UAAYH,WAAWI,UAAU,CAACD,WAAW;YAC7D;QACF;QAEA,MAAME,aAAaZ,MAAM,CAACO,WAAW;QACrC,IAAI,CAACK,YAAY;YACf,IAAI,CAACL,WAAWM,Q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AAE;YACbhD,IAAI+C,KAAK,CAAC,wBAAwB;gBAAEnC,MAAMoC;gBAAOR,OAAOS;gBAAWR,OAAOI,CAAC,CAACG,MAAM;YAAC;YACnF,OAAO;QACT;IACF;IAEA,OAAO;AACT,EAAE"}
|
|
1
|
+
{"version":3,"sources":["../../../src/services/filesync/hashes.ts"],"sourcesContent":["import assert from \"node:assert\";\nimport type { Context } from \"../command/context.js\";\nimport { type Create, type Delete, type Update } from \"./changes.js\";\nimport type { Hash, Hashes } from \"./directory.js\";\n\nexport type CreateWithHash = Create & { targetHash: Hash };\nexport type UpdateWithHash = Update & { sourceHash: Hash; targetHash: Hash };\nexport type DeleteWithHash = Delete & { sourceHash: Hash };\nexport type ChangeWithHash = CreateWithHash | UpdateWithHash | DeleteWithHash;\n\nexport class ChangesWithHash extends Map<string, ChangeWithHash> {\n created(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"create\")\n .map(([path]) => path);\n }\n\n updated(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"update\")\n .map(([path]) => path);\n }\n\n deleted(): string[] {\n return Array.from(this.entries())\n .filter(([, change]) => change.type === \"delete\")\n .map(([path]) => path);\n }\n}\n\n/**\n * Calculates the changes that were made to `from` to make it end up as `to`.\n *\n * If `existing` is provided, only the changes that are necessary to\n * apply to `existing` are returned.\n *\n * If `ignore` is provided, any changes that were made to a path that\n * starts with any of the `ignore` paths are skipped.\n *\n * @param ctx - The current context.\n * @param options - The options to use.\n * @param options.from - The source hashes.\n * @param options.to - The target hashes.\n * @param options.existing - The existing hashes.\n * @param options.ignore - The paths to ignore.\n */\nexport const getChanges = (\n ctx: Context,\n {\n from: source,\n to: target,\n existing,\n ignore,\n }: {\n from: Hashes;\n to: Hashes;\n existing?: Hashes;\n ignore?: string[];\n },\n): ChangesWithHash => {\n const changes = new ChangesWithHash();\n const targetPaths = Object.keys(target);\n\n for (const [sourcePath, sourceHash] of Object.entries(source)) {\n if (ignore?.some((ignored) => sourcePath.startsWith(ignored))) {\n continue;\n }\n\n const targetHash = target[sourcePath];\n if (!targetHash) {\n if (!sourcePath.endsWith(\"/\") || !targetPaths.some((targetPath) => targetPath.startsWith(sourcePath))) {\n // sourcePath is a file and it doesn't exist in target OR\n // sourcePath is a directory and target doesn't have any\n // existing files inside it, therefor the sourcePath has been\n // deleted\n changes.set(sourcePath, { type: \"delete\", sourceHash });\n ctx.log.trace(\"file deleted\", { path: sourcePath, sourceHash });\n }\n } else if (!isEqualHash(sourcePath, sourceHash, targetHash)) {\n // the file or directory exists in target, but has a different\n // hash, so it's been updated\n changes.set(sourcePath, { type: \"update\", sourceHash, targetHash });\n ctx.log.trace(\"file updated\", { path: sourcePath, sourceHash, targetHash });\n }\n }\n\n for (const targetPath of targetPaths) {\n if (ignore?.some((ignored) => targetPath.startsWith(ignored))) {\n continue;\n }\n\n if (!source[targetPath]) {\n // the targetPath doesn't exist in source, so it's been created\n const targetHash = target[targetPath];\n assert(targetHash, \"targetHash should exist\");\n\n changes.set(targetPath, { type: \"create\", targetHash });\n ctx.log.trace(\"file created\", { path: targetPath, targetHash });\n }\n }\n\n if (!existing) {\n return changes;\n }\n\n return withoutUnnecessaryChanges(ctx, { changes, existing });\n};\n\n/**\n * Filters out changes that the existing filesystem already has.\n */\nexport const withoutUnnecessaryChanges = (\n ctx: Context,\n { changes, existing }: { changes: ChangesWithHash; existing: Hashes },\n): ChangesWithHash => {\n const necessaryChanges = new ChangesWithHash();\n\n for (const [path, change] of changes) {\n const existingHash = existing[path];\n if (change.type === \"delete\" && !existingHash) {\n // already deleted\n ctx.log.trace(\"already deleted\", { path });\n continue;\n }\n\n if (change.type !== \"delete\" && existingHash && isEqualHash(path, change.targetHash, existingHash)) {\n // already created or updated\n ctx.log.trace(\"already created or updated\", { path, existingHash, targetHash: change.targetHash });\n continue;\n }\n\n // we could do this:\n // if (change.type === \"update\" && !existingHash) {\n // change = { type: \"create\", targetHash: change.targetHash };\n // }\n // but, changing the type makes the output look confusing and it\n // doesn't change the outcome, so we just leave it as is\n\n necessaryChanges.set(path, change);\n }\n\n return necessaryChanges;\n};\n\nexport const isEqualHash = (_path: string, aHash: Hash, bHash: Hash): boolean => {\n // FIXME: we're running into issues syncing permissions with Gadget\n // so we're temporarily disabling this check until we can figure out\n return aHash.sha1 === bHash.sha1;\n\n // if (aHash.sha1 !== bHash.sha1) {\n // // the contents are different\n // return false;\n // }\n\n // if (path.endsWith(\"/\")) {\n // // it's a directory, so we don't care about permissions\n // return true;\n // }\n\n // if (isNil(aHash.permissions) || isNil(bHash.permissions)) {\n // // one of the filesystems doesn't support permissions, so ignore them\n // return true;\n // }\n\n // // the contents are the same, and both filesystems support permissions\n // // so ensure the permissions are also the same\n // return aHash.permissions === bHash.permissions;\n};\n\nexport const isEqualHashes = (ctx: Context, a: Hashes, b: Hashes): boolean => {\n for (const [aPath, aHash] of Object.entries(a)) {\n const bHash = b[aPath];\n if (!bHash || !isEqualHash(aPath, aHash, bHash)) {\n ctx.log.debug(\"hashes are not equal\", { path: aPath, aHash, bHash });\n return false;\n }\n }\n\n for (const bPath of Object.keys(b)) {\n if (!a[bPath]) {\n ctx.log.debug(\"hashes are not equal\", { path: bPath, aHash: undefined, bHash: b[bPath] });\n return false;\n }\n }\n\n return true;\n};\n"],"names":["assert","ChangesWithHash","Map","created","Array","from","entries","filter","change","type","map","path","updated","deleted","getChanges","ctx","source","to","target","existing","ignore","changes","targetPaths","Object","keys","sourcePath","sourceHash","some","ignored","startsWith","targetHash","endsWith","targetPath","set","log","trace","isEqualHash","withoutUnnecessaryChanges","necessaryChanges","existingHash","_path","aHash","bHash","sha1","isEqualHashes","a","b","aPath","debug","bPath","undefined"],"mappings":"AAAA,OAAOA,YAAY,cAAc;AAUjC,OAAO,MAAMC,wBAAwBC;IACnCC,UAAoB;QAClB,OAAOC,MAAMC,IAAI,CAAC,IAAI,CAACC,OAAO,IAC3BC,MAAM,CAAC,CAAC,GAAGC,OAAO,GAAKA,OAAOC,IAAI,KAAK,UAC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AChG;QACF;QAEA,oBAAoB;QACpB,mDAAmD;QACnD,gEAAgE;QAChE,IAAI;QACJ,gEAAgE;QAChE,wDAAwD;QAExDQ,iBAAiBL,GAAG,CAACtB,MAAMH;IAC7B;IAEA,OAAO8B;AACT,EAAE;AAEF,OAAO,MAAMF,cAAc,CAACI,OAAeC,OAAaC;IACtD,mEAAmE;IACnE,oEAAoE;IACpE,OAAOD,MAAME,IAAI,KAAKD,MAAMC,IAAI;AAEhC,mCAAmC;AACnC,kCAAkC;AAClC,kBAAkB;AAClB,IAAI;AAEJ,4BAA4B;AAC5B,4DAA4D;AAC5D,iBAAiB;AACjB,IAAI;AAEJ,8DAA8D;AAC9D,0EAA0E;AAC1E,iBAAiB;AACjB,IAAI;AAEJ,yEAAyE;AACzE,iDAAiD;AACjD,kDAAkD;AACpD,EAAE;AAEF,OAAO,MAAMC,gBAAgB,CAAC7B,KAAc8B,GAAWC;IACrD,KAAK,MAAM,CAACC,OAAON,MAAM,IAAIlB,OAAOjB,OAAO,CAACuC,GAAI;QAC9C,MAAMH,QAAQI,CAAC,CAACC,MAAM;QACtB,IAAI,CAACL,SAAS,CAACN,YAAYW,OAAON,OAAOC,QAAQ;YAC/C3B,IAAImB,GAAG,CAACc,KAAK,CAAC,wBAAwB;gBAAErC,MAAMoC;gBAAON;gBAAOC;YAAM;YAClE,OAAO;QACT;IACF;IAEA,KAAK,MAAMO,SAAS1B,OAAOC,IAAI,CAACsB,GAAI;QAClC,IAAI,CAACD,CAAC,CAACI,MAAM,EAAE;YACblC,IAAImB,GAAG,CAACc,KAAK,CAAC,wBAAwB;gBAAErC,MAAMsC;gBAAOR,OAAOS;gBAAWR,OAAOI,CAAC,CAACG,MAAM;YAAC;YACvF,OAAO;QACT;IACF;IAEA,OAAO;AACT,EAAE"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { HTTPError } from "got";
|
|
2
2
|
import { config } from "../config/config.js";
|
|
3
3
|
import { readSession } from "../user/session.js";
|
|
4
|
-
import { log } from "./http.js";
|
|
5
4
|
/**
|
|
6
5
|
* Determines whether the given request options are for a Gadget
|
|
7
6
|
* Services request.
|
|
@@ -27,10 +26,11 @@ export const isUnauthorizedError = (error)=>{
|
|
|
27
26
|
* Swallows unauthorized errors and logs a warning, rethrows all other
|
|
28
27
|
* errors.
|
|
29
28
|
*
|
|
30
|
-
* @param
|
|
31
|
-
|
|
29
|
+
* @param ctx - The current context.
|
|
30
|
+
* @param error - The error to handle.
|
|
31
|
+
*/ export const swallowUnauthorized = (ctx, error)=>{
|
|
32
32
|
if (isUnauthorizedError(error)) {
|
|
33
|
-
log.warn("swallowing unauthorized error", {
|
|
33
|
+
ctx.log.warn("swallowing unauthorized error", {
|
|
34
34
|
error
|
|
35
35
|
});
|
|
36
36
|
return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/http/auth.ts"],"sourcesContent":["import { HTTPError, type OptionsInit } from \"got\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/http/auth.ts"],"sourcesContent":["import { HTTPError, type OptionsInit } from \"got\";\nimport type { Context } from \"../command/context.js\";\nimport { config } from \"../config/config.js\";\nimport { readSession } from \"../user/session.js\";\n\n/**\n * Determines whether the given request options are for a Gadget\n * Services request.\n *\n * @param options - The request options to check.\n * @returns True if the request options are for a Gadget Services\n * request, false otherwise.\n */\nexport const isGadgetServicesRequest = (options: OptionsInit): boolean => {\n return options.url instanceof URL && options.url.host === config.domains.services;\n};\n\n/**\n * Loads the cookie from the session.\n *\n * @returns The cookie string or undefined if there is no session.\n */\nexport const loadCookie = (): string | undefined => {\n const token = readSession();\n return token && `session=${encodeURIComponent(token)};`;\n};\n\nexport const isUnauthorizedError = (error: unknown): error is HTTPError => {\n return error instanceof HTTPError && error.response.statusCode === 401;\n};\n\n/**\n * Swallows unauthorized errors and logs a warning, rethrows all other\n * errors.\n *\n * @param ctx - The current context.\n * @param error - The error to handle.\n */\nexport const swallowUnauthorized = (ctx: Context, error: unknown): void => {\n if (isUnauthorizedError(error)) {\n ctx.log.warn(\"swallowing unauthorized error\", { error });\n return;\n }\n throw error;\n};\n"],"names":["HTTPError","config","readSession","isGadgetServicesRequest","options","url","URL","host","domains","services","loadCookie","token","encodeURIComponent","isUnauthorizedError","error","response","statusCode","swallowUnauthorized","ctx","log","warn"],"mappings":"AAAA,SAASA,SAAS,QAA0B,MAAM;AAElD,SAASC,MAAM,QAAQ,sBAAsB;AAC7C,SAASC,WAAW,QAAQ,qBAAqB;AAEjD;;;;;;;CAOC,GACD,OAAO,MAAMC,0BAA0B,CAACC;IACtC,OAAOA,QAAQC,GAAG,YAAYC,OAAOF,QAAQC,GAAG,CAACE,IAAI,KAAKN,OAAOO,OAAO,CAACC,QAAQ;AACnF,EAAE;AAEF;;;;CAIC,GACD,OAAO,MAAMC,aAAa;IACxB,MAAMC,QAAQT;IACd,OAAOS,SAAS,CAAC,QAAQ,EAAEC,mBAAmBD,OAAO,CAAC,CAAC;AACzD,EAAE;AAEF,OAAO,MAAME,sBAAsB,CAACC;IAClC,OAAOA,iBAAiBd,aAAac,MAAMC,QAAQ,CAACC,UAAU,KAAK;AACrE,EAAE;AAEF;;;;;;CAMC,GACD,OAAO,MAAMC,sBAAsB,CAACC,KAAcJ;IAChD,IAAID,oBAAoBC,QAAQ;QAC9BI,IAAIC,GAAG,CAACC,IAAI,CAAC,iCAAiC;YAAEN;QAAM;QACtD;IACF;IACA,MAAMA;AACR,EAAE"}
|
|
@@ -1,54 +1,81 @@
|
|
|
1
1
|
import { got } from "got";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { Agent as HttpAgent } from "node:http";
|
|
4
|
+
import { Agent as HttpsAgent } from "node:https";
|
|
5
|
+
import { Context } from "../command/context.js";
|
|
2
6
|
import { config } from "../config/config.js";
|
|
3
|
-
import {
|
|
7
|
+
import { sprint } from "../output/sprint.js";
|
|
4
8
|
import { writeSession } from "../user/session.js";
|
|
9
|
+
import { serializeError } from "../util/object.js";
|
|
5
10
|
import { isGadgetServicesRequest } from "./auth.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
const getContext = (options)=>{
|
|
12
|
+
assert(options.context?.["ctx"] instanceof Context, sprint(`
|
|
13
|
+
ctx must be provided to http requests:
|
|
14
|
+
|
|
15
|
+
const response = await http({
|
|
16
|
+
context: { ctx },
|
|
17
|
+
...options,
|
|
18
|
+
});
|
|
19
|
+
`));
|
|
20
|
+
return options.context["ctx"];
|
|
21
|
+
};
|
|
9
22
|
/**
|
|
10
23
|
* An instance of the `got` library with hooks for logging and handling
|
|
11
24
|
* 401 errors. This should be used for all HTTP requests.
|
|
12
25
|
*/ export const http = got.extend({
|
|
26
|
+
agent: {
|
|
27
|
+
http: new HttpAgent({
|
|
28
|
+
keepAlive: true
|
|
29
|
+
}),
|
|
30
|
+
https: new HttpsAgent({
|
|
31
|
+
keepAlive: true
|
|
32
|
+
})
|
|
33
|
+
},
|
|
13
34
|
hooks: {
|
|
14
35
|
beforeRequest: [
|
|
15
36
|
(options)=>{
|
|
37
|
+
const ctx = getContext(options);
|
|
38
|
+
options.signal = ctx.signal;
|
|
16
39
|
options.headers["user-agent"] = config.versionFull;
|
|
17
|
-
log.debug("http request", {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
40
|
+
ctx.log.debug("http request", {
|
|
41
|
+
http: {
|
|
42
|
+
request: {
|
|
43
|
+
method: options.method,
|
|
44
|
+
url: options.url?.toString()
|
|
45
|
+
}
|
|
21
46
|
}
|
|
22
47
|
});
|
|
23
48
|
}
|
|
24
49
|
],
|
|
25
50
|
beforeRetry: [
|
|
26
51
|
(error, retryCount)=>{
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
url: error.request.options.url?.toString()
|
|
52
|
+
const ctx = getContext(error.request?.options ?? error.options.context);
|
|
53
|
+
ctx.log.warn("http request failed, retrying...", {
|
|
54
|
+
http: {
|
|
55
|
+
retryCount,
|
|
56
|
+
error: serializeError(error),
|
|
57
|
+
request: error.request && {
|
|
58
|
+
method: error.request.options.method,
|
|
59
|
+
url: error.request.options.url?.toString()
|
|
60
|
+
}
|
|
37
61
|
}
|
|
38
62
|
});
|
|
39
63
|
}
|
|
40
64
|
],
|
|
41
65
|
afterResponse: [
|
|
42
66
|
(response)=>{
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
67
|
+
const ctx = getContext(response.request.options);
|
|
68
|
+
ctx.log.debug("http response", {
|
|
69
|
+
http: {
|
|
70
|
+
request: {
|
|
71
|
+
method: response.request.options.method,
|
|
72
|
+
url: response.request.options.url?.toString()
|
|
73
|
+
},
|
|
74
|
+
response: {
|
|
75
|
+
statusCode: response.statusCode,
|
|
76
|
+
traceId: response.headers["x-trace-id"],
|
|
77
|
+
durationMs: response.timings.phases.total
|
|
78
|
+
}
|
|
52
79
|
}
|
|
53
80
|
});
|
|
54
81
|
if (response.statusCode === 401 && isGadgetServicesRequest(response.request.options)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/services/http/http.ts"],"sourcesContent":["import { got } from \"got\";\nimport { config } from \"../config/config.js\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/services/http/http.ts"],"sourcesContent":["import { got, type OptionsInit } from \"got\";\nimport assert from \"node:assert\";\nimport { Agent as HttpAgent } from \"node:http\";\nimport { Agent as HttpsAgent } from \"node:https\";\nimport { Context } from \"../command/context.js\";\nimport { config } from \"../config/config.js\";\nimport { sprint } from \"../output/sprint.js\";\nimport { writeSession } from \"../user/session.js\";\nimport { serializeError } from \"../util/object.js\";\nimport { isGadgetServicesRequest } from \"./auth.js\";\n\nexport type HttpOptions = OptionsInit;\n\nconst getContext = (options: HttpOptions): Context => {\n assert(\n options.context?.[\"ctx\"] instanceof Context,\n sprint(`\n ctx must be provided to http requests:\n\n const response = await http({\n context: { ctx },\n ...options,\n });\n `),\n );\n\n return options.context[\"ctx\"] as Context;\n};\n\n/**\n * An instance of the `got` library with hooks for logging and handling\n * 401 errors. This should be used for all HTTP requests.\n */\nexport const http = got.extend({\n agent: {\n http: new HttpAgent({ keepAlive: true }),\n https: new HttpsAgent({ keepAlive: true }),\n },\n hooks: {\n beforeRequest: [\n (options) => {\n const ctx = getContext(options);\n options.signal = ctx.signal;\n options.headers[\"user-agent\"] = config.versionFull;\n ctx.log.debug(\"http request\", {\n http: {\n request: {\n method: options.method,\n url: options.url?.toString(),\n },\n },\n });\n },\n ],\n beforeRetry: [\n (error, retryCount) => {\n const ctx = getContext(error.request?.options ?? error.options.context);\n ctx.log.warn(\"http request failed, retrying...\", {\n http: {\n retryCount,\n error: serializeError(error),\n request: error.request && {\n method: error.request.options.method,\n url: error.request.options.url?.toString(),\n },\n },\n });\n },\n ],\n afterResponse: [\n (response) => {\n const ctx = getContext(response.request.options);\n ctx.log.debug(\"http response\", {\n http: {\n request: {\n method: response.request.options.method,\n url: response.request.options.url?.toString(),\n },\n response: {\n statusCode: response.statusCode,\n traceId: response.headers[\"x-trace-id\"],\n durationMs: response.timings.phases.total,\n },\n },\n });\n\n if (response.statusCode === 401 && isGadgetServicesRequest(response.request.options)) {\n // clear the session if the request was unauthorized\n writeSession(undefined);\n }\n\n return response;\n },\n ],\n },\n});\n"],"names":["got","assert","Agent","HttpAgent","HttpsAgent","Context","config","sprint","writeSession","serializeError","isGadgetServicesRequest","getContext","options","context","http","extend","agent","keepAlive","https","hooks","beforeRequest","ctx","signal","headers","versionFull","log","debug","request","method","url","toString","beforeRetry","error","retryCount","warn","afterResponse","response","statusCode","traceId","durationMs","timings","phases","total","undefined"],"mappings":"AAAA,SAASA,GAAG,QAA0B,MAAM;AAC5C,OAAOC,YAAY,cAAc;AACjC,SAASC,SAASC,SAAS,QAAQ,YAAY;AAC/C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AACf,OAAO,CAACiB,GAAG,EAAEC;wBAClC;oBACF;gBACF;YACF;SACD;QACDK,eAAe;YACb,CAACC;gBACC,MAAMf,MAAMV,WAAWyB,SAAST,OAAO,CAACf,OAAO;gBAC/CS,IAAII,GAAG,CAACC,KAAK,CAAC,iBAAiB;oBAC7BZ,MAAM;wBACJa,SAAS;4BACPC,QAAQQ,SAAST,OAAO,CAACf,OAAO,CAACgB,MAAM;4BACvCC,KAAKO,SAAST,OAAO,CAACf,OAAO,CAACiB,GAAG,EAAEC;wBACrC;wBACAM,UAAU;4BACRC,YAAYD,SAASC,UAAU;4BAC/BC,SAASF,SAASb,OAAO,CAAC,aAAa;4BACvCgB,YAAYH,SAASI,OAAO,CAACC,MAAM,CAACC,KAAK;wBAC3C;oBACF;gBACF;gBAEA,IAAIN,SAASC,UAAU,KAAK,OAAO3B,wBAAwB0B,SAAST,OAAO,CAACf,OAAO,GAAG;oBACpF,oDAAoD;oBACpDJ,aAAamC;gBACf;gBAEA,OAAOP;YACT;SACD;IACH;AACF,GAAG"}
|
|
@@ -16,22 +16,27 @@ import { createStructuredLogger } from "./structured.js";
|
|
|
16
16
|
* const logger = createLogger({ name: "my-logger" });
|
|
17
17
|
* logger.info("printing hello world", { foo: "bar" });
|
|
18
18
|
* logger.print("Hello, world!");
|
|
19
|
-
*/ export const createLogger = ({ name, fields: loggerFields
|
|
19
|
+
*/ export const createLogger = ({ name, fields: loggerFields, devFields: loggerDevFields })=>{
|
|
20
20
|
return {
|
|
21
21
|
...createPrinter({
|
|
22
22
|
name
|
|
23
23
|
}),
|
|
24
24
|
...createStructuredLogger({
|
|
25
25
|
name,
|
|
26
|
-
fields: loggerFields
|
|
26
|
+
fields: loggerFields,
|
|
27
|
+
devFields: loggerDevFields
|
|
27
28
|
}),
|
|
28
|
-
|
|
29
|
+
child: ({ name: childName, fields: childFields, devFields: childDevFields })=>{
|
|
29
30
|
return createLogger({
|
|
30
|
-
name,
|
|
31
|
+
name: childName || name,
|
|
31
32
|
fields: ()=>({
|
|
32
33
|
...unthunk(loggerFields),
|
|
33
|
-
...unthunk(
|
|
34
|
-
})
|
|
34
|
+
...unthunk(childFields)
|
|
35
|
+
}),
|
|
36
|
+
devFields: {
|
|
37
|
+
...unthunk(loggerDevFields),
|
|
38
|
+
...unthunk(childDevFields)
|
|
39
|
+
}
|
|
35
40
|
});
|
|
36
41
|
}
|
|
37
42
|
};
|