Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -79,6 +79,6 @@
},
"packageManager": "pnpm@12.21.0",
"inlinedDependencies": {
"@putdotio/sdk": "16.0.2"
"@putdotio/sdk": "17.0.0"
}
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions skills/putio-cli/references/writes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
53 changes: 49 additions & 4 deletions src/command-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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" },
}),
Expand Down Expand Up @@ -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" } }));

Expand Down
15 changes: 10 additions & 5 deletions src/commands/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
),
),
);

Expand Down
31 changes: 28 additions & 3 deletions src/commands/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
),
),
),
);

Expand Down
36 changes: 35 additions & 1 deletion src/internal/sdk-operation-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Comment thread
altaywtf marked this conversation as resolved.
Resolved
]),
"files.getHlsStreamUrl": Schema.Tuple([
Id,
Schema.Struct({ ...HlsFields, downloadToken: Schema.String }),
Expand Down Expand Up @@ -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<unknown>,
downloadToken: string,
): ReadonlyArray<unknown> | undefined => {
const [fileId, options = {}, ...rest] = args;

return Schema.is(JsonObjectSchema)(options)
? [fileId, { ...options, downloadToken }, ...rest]
: undefined;
};

export const fillMissingDownloadToken = (
operation: string,
args: ReadonlyArray<unknown>,
downloadToken: string,
) =>
downloadTokenOperations.has(operation) && !Schema.is(HasDownloadTokenSchema)(args[1])
? setDownloadToken(args, downloadToken)
: undefined;

const argsSchemas = new Map<string, ArgsSchema>(Object.entries(sdkOperationArgsSchemas));

const formatIssue = SchemaIssue.makeFormatterDefault();
Expand Down