|
| 1 | +import { Command } from "@cliffy/command" |
| 2 | +import * as readline from "node:readline" |
| 3 | +import { once } from "node:events" |
| 4 | +import { |
| 5 | + checkKey, |
| 6 | + removeKey, |
| 7 | + retrieveKey, |
| 8 | + storeKey, |
| 9 | + type TransferKeyState, |
| 10 | +} from "../worker/transferKey.ts" |
| 11 | +import process from "node:process" |
| 12 | +import { getRepoAccess } from "./git-credential.ts" |
| 13 | + |
| 14 | +const GIT_ANNEX_VERSION = "VERSION 1" |
| 15 | + |
| 16 | +export async function handleGitAnnexMessage( |
| 17 | + line: string, |
| 18 | + state: TransferKeyState, |
| 19 | +) { |
| 20 | + if (line.startsWith("EXTENSIONS")) { |
| 21 | + return "EXTENSIONS" |
| 22 | + } else if (line.startsWith("PREPARE")) { |
| 23 | + // Ask for configuration to validate |
| 24 | + return "GETCONFIG url" |
| 25 | + } else if (line.startsWith("VALUE ")) { |
| 26 | + // Check if VALUE is configured already |
| 27 | + if (state.url) { |
| 28 | + return "PREPARE-SUCCESS" |
| 29 | + } else { |
| 30 | + return "PREPARE-FAILURE url must be configured when running initremote or enableremote" |
| 31 | + } |
| 32 | + } else if (line.startsWith("TRANSFER STORE")) { |
| 33 | + const [, , key, file] = line.split(" ", 4) |
| 34 | + if (await storeKey(state, key, file)) { |
| 35 | + return `TRANSFER-SUCCESS STORE ${key}` |
| 36 | + } else { |
| 37 | + return `TRANSFER-FAILURE STORE ${key}` |
| 38 | + } |
| 39 | + } else if (line.startsWith("TRANSFER RETRIEVE")) { |
| 40 | + const [, , key, file] = line.split(" ", 4) |
| 41 | + if (await retrieveKey(state, key, file)) { |
| 42 | + return `TRANSFER-SUCCESS RETRIEVE ${key}` |
| 43 | + } else { |
| 44 | + return `TRANSFER-FAILURE RETRIEVE ${key}` |
| 45 | + } |
| 46 | + } else if (line.startsWith("CHECKPRESENT")) { |
| 47 | + const key = line.split("CHECKPRESENT ", 2)[1] |
| 48 | + if (await checkKey(state, key)) { |
| 49 | + return `CHECKPRESENT-SUCCESS ${key}` |
| 50 | + } else { |
| 51 | + return `CHECKPRESENT-FAILURE ${key}` |
| 52 | + } |
| 53 | + } else if (line.startsWith("INITREMOTE")) { |
| 54 | + // No init steps are required - always succeed |
| 55 | + return "INITREMOTE-SUCCESS" |
| 56 | + } else if (line.startsWith("GETAVAILABILITY")) { |
| 57 | + return "AVAILABILITY GLOBAL" |
| 58 | + } else if (line.startsWith("REMOVE")) { |
| 59 | + const key = line.split("REMOVE ", 2)[1] |
| 60 | + if (await removeKey(state, key)) { |
| 61 | + return `REMOVE-SUCCESS ${key}` |
| 62 | + } else { |
| 63 | + return `REMOVE-FAILURE ${key}` |
| 64 | + } |
| 65 | + } else { |
| 66 | + return "UNSUPPORTED-REQUEST" |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Stateful response handling for git annex protocol |
| 72 | + * @returns {() => void} |
| 73 | + */ |
| 74 | +export const response = () => { |
| 75 | + const state: TransferKeyState = { |
| 76 | + url: "", |
| 77 | + token: "", |
| 78 | + } |
| 79 | + return async (line: string) => { |
| 80 | + if (line.startsWith("VALUE ")) { |
| 81 | + try { |
| 82 | + const url = line.split("VALUE ")[1] |
| 83 | + // Obtain the filename (no extensions) in url value |
| 84 | + const datasetId = url.substring(url.lastIndexOf("/") + 1, url.length) |
| 85 | + state.url = url |
| 86 | + const { token } = await getRepoAccess(datasetId) |
| 87 | + state.token = token |
| 88 | + } catch (_err) { |
| 89 | + state.url = "" |
| 90 | + state.token = "" |
| 91 | + } |
| 92 | + } |
| 93 | + console.log(await handleGitAnnexMessage(line, state)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Git annex special remote |
| 99 | + */ |
| 100 | +export async function annexSpecialRemote() { |
| 101 | + try { |
| 102 | + const rl = readline.createInterface({ |
| 103 | + input: process.stdin, |
| 104 | + }) |
| 105 | + console.log(GIT_ANNEX_VERSION) |
| 106 | + rl.on("line", response()) |
| 107 | + await once(rl, "close") |
| 108 | + } catch (err) { |
| 109 | + console.error(err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export const specialRemote = new Command() |
| 114 | + .name("special-remote") |
| 115 | + .description( |
| 116 | + "git-annex special remote for uploading or downloading from OpenNeuro", |
| 117 | + ) |
| 118 | + .action(async () => { |
| 119 | + await annexSpecialRemote() |
| 120 | + }) |
0 commit comments