Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: implement message-port ipfs.ls #3322

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 37 additions & 2 deletions packages/ipfs-message-port-client/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const iterateReadableStream = require('browser-readablestream-to-it')
/**
* @typedef {import('ipfs-message-port-server/src/core').CoreService} CoreService
* @typedef {import('ipfs-message-port-server/src/core').AddedEntry} AddedEntry
* @typedef {import('ipfs-message-port-server/src/core').EncodedLsEntry} EncodedLsEntry
* @typedef {import('ipfs-message-port-server/src/core').LsEntry} LsEntry
* @typedef {import('./client').ClientTransport} Transport
*/

Expand All @@ -61,7 +63,7 @@ class CoreClient extends Client {
* @param {Transport} transport
*/
constructor (transport) {
super('core', ['add', 'addAll', 'cat'], transport)
super('core', ['add', 'addAll', 'cat', 'ls'], transport)
}

/**
Expand Down Expand Up @@ -174,6 +176,24 @@ class CoreClient extends Client {
const result = await this.remote.cat({ ...options, path: input })
yield * decodeIterable(result.data, identity)
}

/**
* Returns content addressed by a valid IPFS Path.
*
* @param {string|CID} inputPath
* @param {Object} [options]
* @param {boolean} [options.recursive]
* @param {boolean} [options.preload]
* @param {number} [options.timeout]
* @param {AbortSignal} [options.signal]
* @returns {AsyncIterable<LsEntry>}
*/
async * ls (inputPath, options = {}) {
const input = CID.isCID(inputPath) ? encodeCID(inputPath) : inputPath
const result = await this.remote.ls({ ...options, path: input })

yield * decodeIterable(result.data, decodeLsEntry)
}
}

/**
Expand All @@ -192,6 +212,21 @@ const decodeAddedData = ({ path, cid, mode, mtime, size }) => {
}
}

/**
* @param {EncodedLsEntry} encodedEntry
* @returns {LsEntry}
*/
const decodeLsEntry = ({ depth, name, path, size, cid, type, mode, mtime }) => ({
cid: decodeCID(cid),
type,
name,
path,
mode,
mtime,
size,
depth
})

/**
* @template T
* @param {T} v
Expand Down Expand Up @@ -359,7 +394,7 @@ const encodeFileObject = ({ path, mode, mtime, content }, transfer) => {

/**
*
* @param {FileContent} [content]
* @param {FileContent|undefined} content
* @param {Transferable[]} transfer
* @returns {EncodedFileContent}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ describe('interface-ipfs-core tests', () => {
name: 'get',
reason: 'Not implemented'
},
{
name: 'ls',
reason: 'Not implemented'
},
{
name: 'refs',
reason: 'Not implemented'
Expand Down
62 changes: 54 additions & 8 deletions packages/ipfs-message-port-server/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { decodeCID, encodeCID } = require('ipfs-message-port-protocol/src/cid')
* @typedef {import('./ipfs').FileObject} FileObject
* @typedef {import('./ipfs').FileContent} DecodedFileContent
* @typedef {import('./ipfs').FileInput} DecodedFileInput
* @typedef {import('./ipfs').LsEntry} LsEntry
*/

/**
Expand Down Expand Up @@ -95,17 +96,15 @@ const { decodeCID, encodeCID } = require('ipfs-message-port-protocol/src/cid')
* @typedef {Object} LsQuery
* @property {string} path
*
* @typedef {RemoteIterable<LsEntry>} LsResult
*
* @typedef {Object} LsEntry
* @property {number} depth
* @typedef {Object} EncodedLsEntry
* @property {EncodedCID} cid
* @property {FileType} type
* @property {string} name
* @property {string} path
* @property {number} depth
* @property {number} size
* @property {EncodedCID} cid
* @property {FileType} type
* @property {Mode} mode
* @property {UnixFSTime} mtime
* @property {Mode} [mode]
* @property {UnixFSTime} [mtime]
*/

/**
Expand Down Expand Up @@ -231,6 +230,26 @@ class CoreService {
const content = this.ipfs.cat(location, { offset, length, timeout, signal })
return encodeCatResult(content)
}

/**
* @typedef {Object} LsResult
Gozala marked this conversation as resolved.
Show resolved Hide resolved
* @property {RemoteIterable<EncodedLsEntry>} data
* @property {Transferable[]} transfer
*
* @param {Object} query
* @param {string|EncodedCID} query.path
* @param {boolean} [query.preload]
* @param {boolean} [query.recursive]
* @param {number} [query.timeout]
* @param {AbortSignal} [query.signal]
* @returns {LsResult}
*/
ls (query) {
const { path, recursive, preload, timeout, signal } = query
const location = typeof path === 'string' ? path : decodeCID(path)
const entries = this.ipfs.ls(location, { recursive, preload, timeout, signal })
return encodeLsResult(entries)
}
}
// @returns {string|ArrayBufferView|ArrayBuffer|Blob|AsyncIterable<string>|AsyncIterable<ArrayBufferView>|AsyncIterable<ArrayBuffer>|AsyncIterable<Blob>|AsyncIterable<FileObject>}

Expand Down Expand Up @@ -334,6 +353,33 @@ const encodeCatResult = content => {
return { data: encodeIterable(content, moveBuffer, transfer), transfer }
}

/**
*
* @param {AsyncIterable<LsEntry>} entries
* @returns {LsResult}
*/
const encodeLsResult = entries => {
/** @type {Transferable[]} */
const transfer = []
return { data: encodeIterable(entries, encodeLsEntry, transfer), transfer }
}

/**
*
* @param {LsEntry} entry
* @returns {EncodedLsEntry}
*/
const encodeLsEntry = ({ depth, name, path, size, cid, type, mode, mtime }) => ({
cid: encodeCID(cid),
type,
name,
path,
mode,
mtime,
size,
depth
})

/**
* Adds underlying `ArrayBuffer` to the transfer list.
*
Expand Down
15 changes: 12 additions & 3 deletions packages/ipfs-message-port-server/src/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export interface Core {
addAll(inputs: AddAllInput, options: AddOptions): AsyncIterable<FileOutput>
add(input: AddInput, options: AddOptions): Promise<FileOutput>
cat(ipfsPath: CID | string, options: CatOptions): AsyncIterable<Buffer>

ls(ipfsPath: CID | string, options: CoreLsOptions): AsyncIterable<LsEntry>
}

interface AddOptions extends AbortOptions {
Expand Down Expand Up @@ -95,6 +97,11 @@ interface CatOptions extends AbortOptions {
length?: number
}

interface CoreLsOptions extends AbortOptions {
preload?: boolean
recursive?: boolean
}

export interface Files {
chmod(path: string | CID, mode: Mode, options?: ChmodOptions): Promise<void>

Expand All @@ -120,13 +127,15 @@ interface LsOptions extends AbortOptions {
sort?: boolean
}

type LsEntry = {
export type LsEntry = {
name: string
path: string
type: FileType
size: number
depth: number
cid: CID
mode: Mode
mtime: UnixFSTime
mode?: Mode
Gozala marked this conversation as resolved.
Show resolved Hide resolved
Gozala marked this conversation as resolved.
Show resolved Hide resolved
mtime?: UnixFSTime
}

interface StatOptions extends AbortOptions {
Expand Down