Skip to content

Commit 1c80cd9

Browse files
authored
Allow publishing without an access token (#2773)
* Allow publishing without an access token * Update changelog
1 parent f9bebc2 commit 1c80cd9

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

packages/cli/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
- Unused node-fetch dependency (#2772)
1010
- Problematic boxen dependency (#2774)
1111

12+
### Changed
13+
- Remove requirement for SUBQL_ACCESS_TOKEN with publish command (#2773)
14+
1215
## [5.8.1] - 2025-04-24
1316
### Changed
1417
- Update @subql/utils

packages/cli/src/commands/publish.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from 'path';
66
import {Command, Flags} from '@oclif/core';
77
import {getMultichainManifestPath, getProjectRootAndManifest} from '@subql/common';
88
import {createIPFSFile, uploadToIpfs} from '../controller/publish-controller';
9-
import {checkToken, resolveToAbsolutePath} from '../utils';
9+
import {getOptionalToken, resolveToAbsolutePath} from '../utils';
1010
import Build from './build';
1111

1212
export default class Publish extends Command {
@@ -32,7 +32,7 @@ export default class Publish extends Command {
3232
// Make sure build first, generated project yaml could be added to the project (instead of ts)
3333
const project = getProjectRootAndManifest(location);
3434

35-
const authToken = await checkToken();
35+
const authToken = getOptionalToken();
3636

3737
const fullPaths = project.manifests.map((manifest) => path.join(project.root, manifest));
3838

@@ -41,7 +41,7 @@ export default class Publish extends Command {
4141
multichainManifestPath = path.join(project.root, multichainManifestPath);
4242
}
4343

44-
const fileToCidMap = await uploadToIpfs(fullPaths, authToken.trim(), multichainManifestPath, flags.ipfs).catch(
44+
const fileToCidMap = await uploadToIpfs(fullPaths, authToken?.trim(), multichainManifestPath, flags.ipfs).catch(
4545
(e) => {
4646
// log further cause from error
4747
if (e.cause) {

packages/cli/src/controller/publish-controller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function createIPFSFile(root: string, manifest: string, cid: string
3030

3131
export async function uploadToIpfs(
3232
projectPaths: string[],
33-
authToken: string,
33+
authToken?: string,
3434
multichainProjectPath?: string,
3535
ipfsEndpoint?: string,
3636
directory?: string
@@ -100,7 +100,7 @@ export async function uploadToIpfs(
100100
async function replaceFileReferences<T extends Record<string, any>>(
101101
projectDir: string,
102102
input: T,
103-
authToken: string,
103+
authToken?: string,
104104
ipfs?: IPFSHTTPClientLite
105105
): Promise<T> {
106106
if (Array.isArray(input)) {
@@ -134,7 +134,7 @@ const fileMap = new Map<string | fs.ReadStream, Promise<string>>();
134134

135135
export async function uploadFiles(
136136
contents: {path: string; content: string}[],
137-
authToken: string,
137+
authToken?: string,
138138
isMultichain?: boolean,
139139
ipfs?: IPFSHTTPClientLite
140140
): Promise<Map<string, string>> {
@@ -154,7 +154,7 @@ export async function uploadFiles(
154154

155155
const ipfsWrite = new IPFSHTTPClientLite({
156156
url: IPFS_WRITE_ENDPOINT,
157-
headers: {Authorization: `Bearer ${authToken}`},
157+
headers: authToken ? {Authorization: `Bearer ${authToken}`} : undefined,
158158
});
159159

160160
try {
@@ -177,7 +177,7 @@ export async function uploadFiles(
177177

178178
export async function uploadFile(
179179
contents: {path: string; content: string},
180-
authToken: string,
180+
authToken?: string,
181181
ipfs?: IPFSHTTPClientLite
182182
): Promise<string> {
183183
const pathPromise = fileMap.get(contents.path);
@@ -197,7 +197,7 @@ export async function uploadFile(
197197

198198
const ipfsWrite = new IPFSHTTPClientLite({
199199
url: IPFS_WRITE_ENDPOINT,
200-
headers: {Authorization: `Bearer ${authToken}`},
200+
headers: authToken ? {Authorization: `Bearer ${authToken}`} : undefined,
201201
});
202202

203203
const pendingCid = ipfsWrite

packages/cli/src/utils/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ export async function checkToken(token_path: string = ACCESS_TOKEN_PATH): Promis
7474
}
7575
}
7676

77+
export function getOptionalToken(token_path: string = ACCESS_TOKEN_PATH): string | undefined {
78+
const envToken = process.env.SUBQL_ACCESS_TOKEN;
79+
if (envToken) return envToken;
80+
if (existsSync(token_path)) {
81+
try {
82+
const authToken = readFileSync(token_path, 'utf8');
83+
if (!authToken) {
84+
return undefined;
85+
}
86+
return authToken.trim();
87+
} catch (e) {
88+
return undefined;
89+
}
90+
}
91+
return undefined;
92+
}
93+
7794
export function errorHandle(e: any, msg: string): Error {
7895
if (axios.isAxiosError(e) && e?.response?.data) {
7996
return new Error(`${msg} ${e.response.data.message ?? e.response.data}`);

0 commit comments

Comments
 (0)