Skip to content

Commit 012acae

Browse files
authored
fix: use file object headers when accessing file (#1192)
* fix: use file object headers when accessing file * fix: review * fix: build issue & prettier * fix: code review
1 parent f1b4bec commit 012acae

7 files changed

Lines changed: 51 additions & 8 deletions

File tree

src/components/P2P/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ export class OceanP2P extends EventEmitter {
914914
const peersFound = []
915915
try {
916916
// @ts-ignore ignore the type mismatch
917-
const f = await this._libp2p.contentRouting.findProviders(cid, {
917+
const f = this._libp2p.contentRouting.findProviders(cid, {
918918
queryFuncTimeout: timeout || 20000 // 20 seconds
919919
// on timeout the query ends with an abort signal => CodeError: Query aborted
920920
})

src/components/core/compute/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ export async function getAlgoChecksums(
5252
: file.type === 'ipfs'
5353
? urlJoin(config.ipfsGateway, (file as IpfsFileObject).hash)
5454
: null
55+
const headers = file.type === 'url' ? (file as UrlFileObject).headers : undefined
5556

56-
const { contentChecksum } = await fetchFileMetadata(url, 'get', false)
57+
const { contentChecksum } = await fetchFileMetadata(
58+
url,
59+
'get',
60+
false,
61+
headers ? headers[0] : undefined
62+
)
5763
checksums.files = checksums.files.concat(contentChecksum)
5864
}
5965

src/components/core/handler/fileInfoHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../../httpRoutes/validateCommands.js'
2121
import { getFile } from '../../../utils/file.js'
2222
import { getConfiguration } from '../../../utils/index.js'
23+
2324
async function formatMetadata(
2425
file: ArweaveFileObject | IpfsFileObject | UrlFileObject,
2526
config: OceanNodeConfig
@@ -32,11 +33,13 @@ async function formatMetadata(
3233
: file.type === 'ipfs'
3334
? urlJoin(config.ipfsGateway, (file as IpfsFileObject).hash)
3435
: null
36+
const headers = file.type === 'url' ? (file as UrlFileObject).headers : undefined
3537

3638
const { contentLength, contentType, contentChecksum } = await fetchFileMetadata(
3739
url,
3840
'get',
39-
false
41+
false,
42+
headers ? headers[0] : undefined
4043
)
4144
CORE_LOGGER.logMessage(`Metadata for file: ${contentLength} ${contentType}`)
4245

src/components/storage/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ export class UrlStorage extends Storage {
148148
}
149149
}
150150

151+
async getReadableStream(): Promise<StorageReadable> {
152+
const input = this.getDownloadUrl()
153+
const file = this.getFile()
154+
const response = await axios({
155+
method: 'get',
156+
url: input,
157+
headers: file.headers ? file.headers[0] : undefined,
158+
responseType: 'stream',
159+
timeout: 30000
160+
})
161+
162+
return {
163+
httpStatus: response.status,
164+
stream: response.data,
165+
headers: response.headers as any
166+
}
167+
}
168+
151169
validate(): [boolean, string] {
152170
const file: UrlFileObject = this.getFile() as UrlFileObject
153171
if (!file.url || !file.method) {
@@ -194,11 +212,12 @@ export class UrlStorage extends Storage {
194212
fileObject: UrlFileObject,
195213
forceChecksum: boolean
196214
): Promise<FileInfoResponse> {
197-
const { url, method } = fileObject
215+
const { url, method, headers } = fileObject
198216
const { contentLength, contentType, contentChecksum } = await fetchFileMetadata(
199217
url,
200218
method,
201-
forceChecksum
219+
forceChecksum,
220+
headers ? headers[0] : undefined
202221
)
203222
return {
204223
valid: true,

src/test/unit/storage.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ describe('URL Storage tests', () => {
171171
const stream = await storage.getReadableStream()
172172
expect(stream).not.to.eql(null)
173173
})
174+
175+
it('Gets readable stream with headers', async () => {
176+
file = {
177+
type: 'url',
178+
url: 'https://stock-api.oceanprotocol.com/stock/stock.json',
179+
method: 'get',
180+
headers: [{ 'X-Test-Header': 'test' }]
181+
}
182+
const storage = Storage.getStorageClass(file, config)
183+
const stream = await storage.getReadableStream()
184+
expect(stream).not.to.eql(null)
185+
})
174186
})
175187

176188
describe('Unsafe URL tests', () => {

src/utils/asset.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { KNOWN_CONFIDENTIAL_EVMS } from './address.js'
99
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/interfaces/IERC20Template.sol/IERC20Template.json' with { type: 'json' }
1010
import ERC20Template4 from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template4.sol/ERC20Template4.json' with { type: 'json' }
1111
import { getContractAddress, getNFTFactory } from '../components/Indexer/utils.js'
12+
import { HeadersObject } from '../@types/fileObject.js'
1213

1314
// Notes:
1415
// Asset as per asset.py on provider, is a class there, while on ocean.Js we only have a type
@@ -44,7 +45,8 @@ export const AssetUtils = {
4445
export async function fetchFileMetadata(
4546
url: string,
4647
method: string,
47-
forceChecksum: boolean
48+
forceChecksum: boolean,
49+
headers?: HeadersObject
4850
): Promise<{ contentLength: string; contentType: string; contentChecksum: string }> {
4951
let contentType: string = ''
5052
let contentLength: number = 0
@@ -56,6 +58,7 @@ export async function fetchFileMetadata(
5658
const response = await axios({
5759
url,
5860
method: method || 'get',
61+
headers,
5962
responseType: 'stream',
6063
timeout: 30000
6164
})
@@ -143,7 +146,7 @@ export async function isERC20Template4Active(
143146
): Promise<boolean> {
144147
try {
145148
const nftFactoryAddress = getContractAddress(network, 'ERC721Factory')
146-
const factoryERC721 = await getNFTFactory(owner, nftFactoryAddress)
149+
const factoryERC721 = getNFTFactory(owner, nftFactoryAddress)
147150
const currentTokenCount = await factoryERC721.getCurrentTemplateCount()
148151

149152
for (let i = 1; i <= currentTokenCount; i++) {

src/utils/logging/Logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const alignedWithColorsAndTime: winston.Logform.Format = winston.format.combine(
198198
winston.format.timestamp(),
199199
winston.format.align(),
200200
winston.format.printf(
201-
(info) => `${info.timestamp} ${info.level}: ${info.message.trim()}`
201+
(info) => `${info.timestamp} ${info.level}: ${(info.message as string).trim()}`
202202
)
203203
)
204204
const consoleColorFormatting: winston.Logform.Format | Record<string, any> = {

0 commit comments

Comments
 (0)