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

Commit 16ecc74

Browse files
achingbraingcohler
andauthored
fix: files ls should return string (#3352)
Supersedes #3345 #2939 Fixes #3330 #2948 BREAKING CHANGE: types returned by `ipfs.files.ls` are now strings, in line with the docs but different to previous behaviour Co-authored-by: Geoffrey Cohler <g.cohler@computer.org>
1 parent 3250ff4 commit 16ecc74

File tree

7 files changed

+31
-27
lines changed

7 files changed

+31
-27
lines changed

examples/browser-mfs/filetree.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const {
55
} = require('./utils')
66

77
const FILE_TYPES = {
8-
FILE: 0,
9-
DIRECTORY: 1
8+
FILE: 'file',
9+
DIRECTORY: 'directory'
1010
}
1111

1212
let selected = {}

packages/interface-ipfs-core/src/files/ls.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ const drain = require('it-drain')
1010
const randomBytes = require('iso-random-stream/src/random')
1111
const testTimeout = require('../utils/test-timeout')
1212

13-
const MFS_FILE_TYPES = {
14-
file: 0,
15-
directory: 1,
16-
'hamt-sharded-directory': 1
17-
}
18-
1913
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
2014
/**
2115
* @param {Factory} common
@@ -53,7 +47,7 @@ module.exports = (common, options) => {
5347
cid: new CID('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
5448
name: fileName,
5549
size: content.length,
56-
type: MFS_FILE_TYPES.file
50+
type: 'file'
5751
}])
5852
})
5953

@@ -81,7 +75,7 @@ module.exports = (common, options) => {
8175
cid: new CID('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
8276
name: fileName,
8377
size: content.length,
84-
type: MFS_FILE_TYPES.file
78+
type: 'file'
8579
}])
8680
})
8781

@@ -99,7 +93,7 @@ module.exports = (common, options) => {
9993
cid: new CID('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
10094
name: fileName,
10195
size: content.length,
102-
type: MFS_FILE_TYPES.file
96+
type: 'file'
10397
}])
10498
})
10599

@@ -128,7 +122,7 @@ module.exports = (common, options) => {
128122
cid: child.Hash,
129123
name: child.Hash.toString(),
130124
size: 262144,
131-
type: MFS_FILE_TYPES.file
125+
type: 'file'
132126
}])
133127
})
134128

@@ -160,7 +154,7 @@ module.exports = (common, options) => {
160154
cid: child.Hash,
161155
name: child.Hash.toString(),
162156
size: 262144,
163-
type: MFS_FILE_TYPES.file
157+
type: 'file'
164158
}])
165159
})
166160

@@ -200,7 +194,7 @@ module.exports = (common, options) => {
200194

201195
files.forEach(file => {
202196
// should be a file
203-
expect(file.type).to.equal(0)
197+
expect(file.type).to.equal('file')
204198
})
205199
})
206200

packages/ipfs-core/src/components/files/ls.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const exporter = require('ipfs-unixfs-exporter')
44
const toMfsPath = require('./utils/to-mfs-path')
55
const {
6-
MFS_FILE_TYPES,
76
withTimeoutOption
87
} = require('../../utils')
98

@@ -12,14 +11,20 @@ const {
1211
* @returns {UnixFSEntry}
1312
*/
1413
const toOutput = (fsEntry) => {
15-
let type = 0
14+
/** @type FileType */
15+
let type = 'file'
1616
let size = fsEntry.node.size || fsEntry.node.length
1717
let mode
1818
let mtime
1919

2020
if (fsEntry.unixfs) {
2121
size = fsEntry.unixfs.fileSize()
22-
type = MFS_FILE_TYPES[fsEntry.unixfs.type]
22+
type = fsEntry.unixfs.type
23+
24+
if (fsEntry.unixfs.type === 'hamt-sharded-directory') {
25+
type = 'directory'
26+
}
27+
2328
mode = fsEntry.unixfs.mode
2429
mtime = fsEntry.unixfs.mtime
2530
}
@@ -89,10 +94,13 @@ module.exports = (context) => {
8994
* @property {number} [nsecs] - the number of nanoseconds since the last full
9095
* second.
9196
*
97+
* @typedef {'file'|'directory'} FileType
98+
*
9299
* @typedef {object} UnixFSEntry
93100
* @property {CID} cid
101+
* @property {string} name
94102
* @property {number} [mode]
95103
* @property {UnixTimeObj} [mtime]
96104
* @property {number} size
97-
* @property {number} type
105+
* @property {FileType} type
98106
*/

packages/ipfs-core/src/utils.js

-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path')
1414
const ERR_BAD_PATH = 'ERR_BAD_PATH'
1515

1616
exports.OFFLINE_ERROR = 'This command must be run in online mode. Try running \'ipfs daemon\' first.'
17-
18-
exports.MFS_FILE_TYPES = {
19-
file: 0,
20-
directory: 1,
21-
'hamt-sharded-directory': 1
22-
}
2317
exports.MFS_ROOT_KEY = new Key('/local/filesroot')
2418
exports.MFS_MAX_CHUNK_SIZE = 262144
2519
exports.MFS_MAX_LINKS = 174

packages/ipfs-http-client/src/files/ls.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ module.exports = configure(api => {
4343
})
4444

4545
function toCoreInterface (entry) {
46-
if (entry.hash) entry.cid = new CID(entry.hash)
46+
if (entry.hash) {
47+
entry.cid = new CID(entry.hash)
48+
}
49+
4750
delete entry.hash
51+
52+
entry.type = entry.type === 1 ? 'directory' : 'file'
53+
4854
return entry
4955
}

packages/ipfs-http-server/src/api/resources/files/ls.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const { pipe } = require('it-pipe')
77
const streamResponse = require('../../../utils/stream-response')
88

99
const mapEntry = (entry, options = {}) => {
10+
const type = entry.type === 'file' ? 0 : 1
11+
1012
const output = {
1113
Name: entry.name,
12-
Type: options.long ? entry.type : 0,
14+
Type: options.long ? type : 0,
1315
Size: options.long ? entry.size || 0 : 0,
1416
Hash: entry.cid.toString(options.cidBase)
1517
}

packages/ipfs-http-server/test/inject/mfs/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('/files/ls', () => {
8383

8484
expect(response).to.have.nested.property('result.Entries.length', 1)
8585
expect(response).to.have.nested.property('result.Entries[0].Name', file.name)
86-
expect(response).to.have.nested.property('result.Entries[0].Type', file.type)
86+
expect(response).to.have.nested.property('result.Entries[0].Type', 1)
8787
expect(response).to.have.nested.property('result.Entries[0].Size', file.size)
8888
expect(response).to.have.nested.property('result.Entries[0].Hash', file.cid.toString())
8989
expect(response).to.have.nested.property('result.Entries[0].Mode', file.mode)

0 commit comments

Comments
 (0)