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

Commit 4b8021d

Browse files
authoredOct 16, 2020
feat: implement message-port ipfs.ls (#3322)
1 parent 1ba0bf0 commit 4b8021d

File tree

4 files changed

+102
-16
lines changed

4 files changed

+102
-16
lines changed
 

‎packages/ipfs-message-port-client/src/core.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const iterateReadableStream = require('browser-readablestream-to-it')
4949
/**
5050
* @typedef {import('ipfs-message-port-server/src/core').CoreService} CoreService
5151
* @typedef {import('ipfs-message-port-server/src/core').AddedEntry} AddedEntry
52+
* @typedef {import('ipfs-message-port-server/src/core').EncodedLsEntry} EncodedLsEntry
53+
* @typedef {import('ipfs-message-port-server/src/core').LsEntry} LsEntry
5254
* @typedef {import('./client').ClientTransport} Transport
5355
*/
5456

@@ -61,7 +63,7 @@ class CoreClient extends Client {
6163
* @param {Transport} transport
6264
*/
6365
constructor (transport) {
64-
super('core', ['add', 'addAll', 'cat'], transport)
66+
super('core', ['add', 'addAll', 'cat', 'ls'], transport)
6567
}
6668

6769
/**
@@ -174,6 +176,24 @@ class CoreClient extends Client {
174176
const result = await this.remote.cat({ ...options, path: input })
175177
yield * decodeIterable(result.data, identity)
176178
}
179+
180+
/**
181+
* Returns content addressed by a valid IPFS Path.
182+
*
183+
* @param {string|CID} inputPath
184+
* @param {Object} [options]
185+
* @param {boolean} [options.recursive]
186+
* @param {boolean} [options.preload]
187+
* @param {number} [options.timeout]
188+
* @param {AbortSignal} [options.signal]
189+
* @returns {AsyncIterable<LsEntry>}
190+
*/
191+
async * ls (inputPath, options = {}) {
192+
const input = CID.isCID(inputPath) ? encodeCID(inputPath) : inputPath
193+
const result = await this.remote.ls({ ...options, path: input })
194+
195+
yield * decodeIterable(result.data, decodeLsEntry)
196+
}
177197
}
178198

179199
/**
@@ -192,6 +212,21 @@ const decodeAddedData = ({ path, cid, mode, mtime, size }) => {
192212
}
193213
}
194214

215+
/**
216+
* @param {EncodedLsEntry} encodedEntry
217+
* @returns {LsEntry}
218+
*/
219+
const decodeLsEntry = ({ depth, name, path, size, cid, type, mode, mtime }) => ({
220+
cid: decodeCID(cid),
221+
type,
222+
name,
223+
path,
224+
mode,
225+
mtime,
226+
size,
227+
depth
228+
})
229+
195230
/**
196231
* @template T
197232
* @param {T} v
@@ -359,7 +394,7 @@ const encodeFileObject = ({ path, mode, mtime, content }, transfer) => {
359394

360395
/**
361396
*
362-
* @param {FileContent} [content]
397+
* @param {FileContent|undefined} content
363398
* @param {Transferable[]} transfer
364399
* @returns {EncodedFileContent}
365400
*/

‎packages/ipfs-message-port-client/test/interface-message-port-client.js

-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ describe('interface-ipfs-core tests', () => {
7474
name: 'get',
7575
reason: 'Not implemented'
7676
},
77-
{
78-
name: 'ls',
79-
reason: 'Not implemented'
80-
},
8177
{
8278
name: 'refs',
8379
reason: 'Not implemented'

‎packages/ipfs-message-port-server/src/core.js

+54-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { decodeCID, encodeCID } = require('ipfs-message-port-protocol/src/cid')
2121
* @typedef {import('./ipfs').FileObject} FileObject
2222
* @typedef {import('./ipfs').FileContent} DecodedFileContent
2323
* @typedef {import('./ipfs').FileInput} DecodedFileInput
24+
* @typedef {import('./ipfs').LsEntry} LsEntry
2425
*/
2526

2627
/**
@@ -95,17 +96,15 @@ const { decodeCID, encodeCID } = require('ipfs-message-port-protocol/src/cid')
9596
* @typedef {Object} LsQuery
9697
* @property {string} path
9798
*
98-
* @typedef {RemoteIterable<LsEntry>} LsResult
99-
*
100-
* @typedef {Object} LsEntry
101-
* @property {number} depth
99+
* @typedef {Object} EncodedLsEntry
100+
* @property {EncodedCID} cid
101+
* @property {FileType} type
102102
* @property {string} name
103103
* @property {string} path
104+
* @property {number} depth
104105
* @property {number} size
105-
* @property {EncodedCID} cid
106-
* @property {FileType} type
107-
* @property {Mode} mode
108-
* @property {UnixFSTime} mtime
106+
* @property {Mode} [mode]
107+
* @property {UnixFSTime} [mtime]
109108
*/
110109

111110
/**
@@ -231,6 +230,26 @@ class CoreService {
231230
const content = this.ipfs.cat(location, { offset, length, timeout, signal })
232231
return encodeCatResult(content)
233232
}
233+
234+
/**
235+
* @typedef {Object} LsResult
236+
* @property {RemoteIterable<EncodedLsEntry>} data
237+
* @property {Transferable[]} transfer
238+
*
239+
* @param {Object} query
240+
* @param {string|EncodedCID} query.path
241+
* @param {boolean} [query.preload]
242+
* @param {boolean} [query.recursive]
243+
* @param {number} [query.timeout]
244+
* @param {AbortSignal} [query.signal]
245+
* @returns {LsResult}
246+
*/
247+
ls (query) {
248+
const { path, recursive, preload, timeout, signal } = query
249+
const location = typeof path === 'string' ? path : decodeCID(path)
250+
const entries = this.ipfs.ls(location, { recursive, preload, timeout, signal })
251+
return encodeLsResult(entries)
252+
}
234253
}
235254
// @returns {string|ArrayBufferView|ArrayBuffer|Blob|AsyncIterable<string>|AsyncIterable<ArrayBufferView>|AsyncIterable<ArrayBuffer>|AsyncIterable<Blob>|AsyncIterable<FileObject>}
236255

@@ -334,6 +353,33 @@ const encodeCatResult = content => {
334353
return { data: encodeIterable(content, moveBuffer, transfer), transfer }
335354
}
336355

356+
/**
357+
*
358+
* @param {AsyncIterable<LsEntry>} entries
359+
* @returns {LsResult}
360+
*/
361+
const encodeLsResult = entries => {
362+
/** @type {Transferable[]} */
363+
const transfer = []
364+
return { data: encodeIterable(entries, encodeLsEntry, transfer), transfer }
365+
}
366+
367+
/**
368+
*
369+
* @param {LsEntry} entry
370+
* @returns {EncodedLsEntry}
371+
*/
372+
const encodeLsEntry = ({ depth, name, path, size, cid, type, mode, mtime }) => ({
373+
cid: encodeCID(cid),
374+
type,
375+
name,
376+
path,
377+
mode,
378+
mtime,
379+
size,
380+
depth
381+
})
382+
337383
/**
338384
* Adds underlying `ArrayBuffer` to the transfer list.
339385
*

‎packages/ipfs-message-port-server/src/ipfs.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export interface Core {
5959
addAll(inputs: AddAllInput, options: AddOptions): AsyncIterable<FileOutput>
6060
add(input: AddInput, options: AddOptions): Promise<FileOutput>
6161
cat(ipfsPath: CID | string, options: CatOptions): AsyncIterable<Buffer>
62+
63+
ls(ipfsPath: CID | string, options: CoreLsOptions): AsyncIterable<LsEntry>
6264
}
6365

6466
interface AddOptions extends AbortOptions {
@@ -95,6 +97,11 @@ interface CatOptions extends AbortOptions {
9597
length?: number
9698
}
9799

100+
interface CoreLsOptions extends AbortOptions {
101+
preload?: boolean
102+
recursive?: boolean
103+
}
104+
98105
export interface Files {
99106
chmod(path: string | CID, mode: Mode, options?: ChmodOptions): Promise<void>
100107

@@ -120,13 +127,15 @@ interface LsOptions extends AbortOptions {
120127
sort?: boolean
121128
}
122129

123-
type LsEntry = {
130+
export type LsEntry = {
124131
name: string
132+
path: string
125133
type: FileType
126134
size: number
135+
depth: number
127136
cid: CID
128137
mode: Mode
129-
mtime: UnixFSTime
138+
mtime?: UnixFSTime
130139
}
131140

132141
interface StatOptions extends AbortOptions {

0 commit comments

Comments
 (0)