diff --git a/package.json b/package.json index a0c9d6f..c200e65 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "@effect/tsgo": "0.68.1", "@effect/vitest": "4.1.4", "@oxlint/plugins": "1.91.0", - "@putdotio/sdk": "16.0.2", + "@putdotio/sdk": "17.0.0", "@types/node": "^26.1.0", "@uinaf/oxlint-plugin-no-comments": "1.1.0", "@uinaf/skillcheck": "1.7.7", @@ -79,6 +79,6 @@ }, "packageManager": "pnpm@12.21.0", "inlinedDependencies": { - "@putdotio/sdk": "16.0.2" + "@putdotio/sdk": "17.0.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 881c356..8161106 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -218,8 +218,8 @@ importers: specifier: 1.91.0 version: 1.91.0 '@putdotio/sdk': - specifier: 16.0.2 - version: 16.0.2 + specifier: 17.0.0 + version: 17.0.0 '@types/node': specifier: ^26.1.0 version: 26.6.6 @@ -1055,8 +1055,8 @@ packages: '@polka/url@1.0.0-next.29': resolution: {integrity: sha512-wwQAWhWSuHaag8c4q/KN/vCoeOJYshAIvMQwD4GpSb3OiZklFfvAgmj0VCBBImRpuF/aFgIRzllXlVX93Jevww==} - '@putdotio/sdk@16.0.2': - resolution: {integrity: sha512-Rgfo/K7OjyhgObWquoANlPLr9H2k+KBFs9g+28m+q7CmoPNRGqbwJpPAj5GSxUh4WYWSHtut9UmnGrok1P/PKA==} + '@putdotio/sdk@17.0.0': + resolution: {integrity: sha512-00GkiDjjoq+bA0/9/6cnDg6XufyInqG/EUI+r38DiNT6YUBlP4ZecpyxmKmeJbL4K2Tk+dv0U+9uaA2aWUTs/g==} engines: {node: '>=24.19.0'} '@redis/bloom@6.2.1': @@ -2469,7 +2469,7 @@ snapshots: '@polka/url@1.0.0-next.29': {} - '@putdotio/sdk@16.0.2': + '@putdotio/sdk@17.0.0': dependencies: effect: 4.1.4 diff --git a/skills/putio-cli/references/writes.md b/skills/putio-cli/references/writes.md index f2cdd90..df179b4 100644 --- a/skills/putio-cli/references/writes.md +++ b/skills/putio-cli/references/writes.md @@ -30,5 +30,6 @@ Rules: - A `--json` payload that fails its schema exits 1; `error.message` names the failing path. - Treat every `sdk call` operation as potentially mutating and inspect its dry-run before `--execute`. - `sdk call` args are the SDK function's positional parameters and must match its input schema. A mismatch fails with title `Invalid SDK arguments` before any request; `error.details` names the `operation` and the schema `issue` with its path. +- Media operations (`files.getHlsMasterPlaylist`, `files.getHlsStreamUrl`, `files.getXspfPlaylistUrl`, `files.getApi*Url`) authenticate with the account download token, not the OAuth token. `sdk call` fetches it when `downloadToken` is omitted. - Bulk selections (`downloadLinks.create`, `sharing.clone`, `sharing.shareFiles`, `zips.create`) need `ids` or a `cursor`; `zips.create` names them `file_ids`. - `files upload` sends to `PUTIO_CLI_UPLOAD_BASE_URL`, or upload.put.io with the default API base. With a custom `PUTIO_CLI_API_BASE_URL` and no upload base, it fails before sending anything; set both together. diff --git a/src/command-paths.test.ts b/src/command-paths.test.ts index 89a9d8e..5cf16ce 100644 --- a/src/command-paths.test.ts +++ b/src/command-paths.test.ts @@ -19,6 +19,7 @@ import { const decodeTopLevelFields = Schema.decodeUnknownSync(Schema.Record(Schema.String, Schema.Unknown)); const AUTHORIZATION = `Token ${FIXTURE_TOKEN}`; +const DOWNLOAD_TOKEN = "fixture-download-token"; const TIMESTAMP = "2026-03-15T12:00:00Z"; type FileOverrides = { @@ -139,11 +140,17 @@ const accountInfo = { const hlsManifest = [ "#EXTM3U", '#EXT-X-STREAM-INF:BANDWIDTH=2376462,CODECS="avc1.42c01e,mp4a.40.2",VIDEO-RANGE=PQ', - `https://api.put.io/hls/playlist/abc/index-v1-a1.m3u8?oauth_token=${FIXTURE_TOKEN}`, + `https://api.put.io/hls/playlist/abc/index-v1-a1.m3u8?oauth_token=${DOWNLOAD_TOKEN}`, ].join("\n"); const ok = (body = {}) => ({ body: { status: "OK", ...body } }); +const routeDownloadToken = (api: FixtureApi) => + api.route( + "GET /v2/account/info", + ok({ info: { ...accountInfo, download_token: DOWNLOAD_TOKEN } }), + ); + const routeReads = (api: FixtureApi) => { api.route("GET /v2/account/info", ok({ info: accountInfo })); api.route( @@ -983,7 +990,8 @@ describe("file writes", () => { expect(requestsTo("POST", "/v2/files/42/start-from/delete")).toHaveLength(1); }); - it("fetches the MP4 HLS master playlist by default and redacts its token", async () => { + it("fetches the MP4 HLS master playlist with the download token and redacts it", async () => { + routeDownloadToken(api); api.route("GET /v2/files/42/hls/media.m3u8", { body: hlsManifest, contentType: "application/vnd.apple.mpegurl", @@ -996,11 +1004,21 @@ describe("file writes", () => { manifest: expect.stringContaining('CODECS="avc1.42c01e,mp4a.40.2"'), original: false, }); - expect(run.stdout).not.toContain(FIXTURE_TOKEN); - expect(api.requests).toEqual([expect.objectContaining({ query: { original: "0" } })]); + expect(run.stdout).not.toContain(DOWNLOAD_TOKEN); + expect( + api.requests.map(({ authorization, path, query }) => ({ authorization, path, query })), + ).toEqual([ + { authorization: AUTHORIZATION, path: "/v2/account/info", query: { download_token: "1" } }, + { + authorization: `Token ${DOWNLOAD_TOKEN}`, + path: "/v2/files/42/hls/media.m3u8", + query: { original: "0" }, + }, + ]); }); it("fetches the original HLS master playlist with subtitle options", async () => { + routeDownloadToken(api); api.route("GET /v2/files/42/hls/media.m3u8", { body: hlsManifest, contentType: "application/vnd.apple.mpegurl", @@ -1025,6 +1043,7 @@ describe("file writes", () => { expect(stdoutJson(run)).toEqual({ manifest: expect.stringContaining("#EXTM3U") }); expect(api.requests).toEqual([ + expect.objectContaining({ path: "/v2/account/info" }), expect.objectContaining({ query: { max_subtitle_count: "2", original: "1", subtitle_languages: "en,tr" }, }), @@ -1418,6 +1437,32 @@ describe("sdk call", () => { expect(api.requests).toEqual([]); }); + it("fetches the download token for a media operation that omits it", async () => { + routeDownloadToken(api); + api.route("GET /v2/files/42/hls/media.m3u8", { + body: hlsManifest, + contentType: "application/vnd.apple.mpegurl", + }); + + const run = await putio( + "sdk", + "call", + "--operation", + "files.getHlsMasterPlaylist", + "--args", + "[42]", + "--execute", + "--output", + "json", + ); + + expect(run.stdout).not.toContain(DOWNLOAD_TOKEN); + expect(api.requests.map(({ authorization, path }) => ({ authorization, path }))).toEqual([ + { authorization: AUTHORIZATION, path: "/v2/account/info" }, + { authorization: `Token ${DOWNLOAD_TOKEN}`, path: "/v2/files/42/hls/media.m3u8" }, + ]); + }); + it("redacts sentinel-looking secrets in execution results", async () => { api.route("GET /v2/account/info", ok({ info: { ...accountInfo, download_token: "null" } })); diff --git a/src/commands/files.ts b/src/commands/files.ts index 45abf3a..135ecf3 100644 --- a/src/commands/files.ts +++ b/src/commands/files.ts @@ -824,11 +824,16 @@ const filesHlsManifest = Command.make( output: controls.output, }, withAuthedSdk(({ sdk }) => - sdk.files.getHlsMasterPlaylist(validatedFileId, { - maxSubtitleCount: Option.getOrUndefined(maxSubtitleCount), - playOriginal: original, - subtitleLanguages: languages.length > 0 ? languages : undefined, - }), + Effect.flatMap( + sdk.account.getInfo({ download_token: 1 }), + ({ download_token: downloadToken }) => + sdk.files.getHlsMasterPlaylist(validatedFileId, { + downloadToken, + maxSubtitleCount: Option.getOrUndefined(maxSubtitleCount), + playOriginal: original, + subtitleLanguages: languages.length > 0 ? languages : undefined, + }), + ), ), ); diff --git a/src/commands/sdk.ts b/src/commands/sdk.ts index 681fbdd..c007cf7 100644 --- a/src/commands/sdk.ts +++ b/src/commands/sdk.ts @@ -29,7 +29,11 @@ import { writeOutput, } from "../internal/output-service.js"; import { sdk } from "../internal/sdk.js"; -import { decodeSdkOperationArgs } from "../internal/sdk-operation-args.js"; +import { + decodeSdkOperationArgs, + fillMissingDownloadToken, + setDownloadToken, +} from "../internal/sdk-operation-args.js"; import { invokeSdkOperation, listSdkOperations, @@ -43,6 +47,8 @@ const NonBlankStringSchema = Schema.String.check( ), ); +const DOWNLOAD_TOKEN_PLACEHOLDER = "[account download token]"; + const SdkCallInputSchema = Schema.Struct({ args: Schema.optional(Schema.Array(Schema.Json)), operation: NonBlankStringSchema, @@ -158,7 +164,16 @@ const sdkCall = Command.make( yield* checkCommandInput(() => resolveSdkOperation(sdk, input.operation)); - const operationArgs = yield* decodeSdkOperationArgs(input.operation, input.args); + const filledArgs = fillMissingDownloadToken( + input.operation, + input.args, + DOWNLOAD_TOKEN_PLACEHOLDER, + ); + + const operationArgs = yield* decodeSdkOperationArgs( + input.operation, + filledArgs ?? input.args, + ); if (mode === "dry-run") { return yield* writeDryRunPlan( @@ -177,7 +192,17 @@ const sdkCall = Command.make( output: getOption(output), }, withAuthedSdk(({ sdk: authedSdk }) => - invokeSdkOperation(authedSdk, input.operation, operationArgs), + filledArgs === undefined + ? invokeSdkOperation(authedSdk, input.operation, operationArgs) + : Effect.flatMap( + authedSdk.account.getInfo({ download_token: 1 }), + ({ download_token: downloadToken }) => + invokeSdkOperation( + authedSdk, + input.operation, + setDownloadToken(operationArgs, downloadToken) ?? operationArgs, + ), + ), ), ); diff --git a/src/internal/sdk-operation-args.ts b/src/internal/sdk-operation-args.ts index d3bd1e8..69300bf 100644 --- a/src/internal/sdk-operation-args.ts +++ b/src/internal/sdk-operation-args.ts @@ -232,7 +232,10 @@ const sdkOperationArgsSchemas = defineArgsSchemas({ ]), "files.getChild": Schema.Tuple([FileGetChildInputSchema]), "files.getDownloadUrl": byId, - "files.getHlsMasterPlaylist": Schema.Tuple([Id, Schema.optionalKey(Schema.Struct(HlsFields))]), + "files.getHlsMasterPlaylist": Schema.Tuple([ + Id, + Schema.Struct({ ...HlsFields, downloadToken: Schema.String }), + ]), "files.getHlsStreamUrl": Schema.Tuple([ Id, Schema.Struct({ ...HlsFields, downloadToken: Schema.String }), @@ -409,6 +412,37 @@ const sdkOperationArgsSchemas = defineArgsSchemas({ "zips.list": none, }); +const downloadTokenOperations = new Set([ + "files.getApiContentUrl", + "files.getApiDownloadUrl", + "files.getApiMp4DownloadUrl", + "files.getHlsMasterPlaylist", + "files.getHlsStreamUrl", + "files.getXspfPlaylistUrl", +]); + +const HasDownloadTokenSchema = Schema.Struct({ downloadToken: Schema.String }); + +export const setDownloadToken = ( + args: ReadonlyArray, + downloadToken: string, +): ReadonlyArray | undefined => { + const [fileId, options = {}, ...rest] = args; + + return Schema.is(JsonObjectSchema)(options) + ? [fileId, { ...options, downloadToken }, ...rest] + : undefined; +}; + +export const fillMissingDownloadToken = ( + operation: string, + args: ReadonlyArray, + downloadToken: string, +) => + downloadTokenOperations.has(operation) && !Schema.is(HasDownloadTokenSchema)(args[1]) + ? setDownloadToken(args, downloadToken) + : undefined; + const argsSchemas = new Map(Object.entries(sdkOperationArgsSchemas)); const formatIssue = SchemaIssue.makeFormatterDefault();