ipfs-unixfs
Advanced tools
Comparing version 2.0.5-rc.8 to 3.0.0
@@ -6,2 +6,18 @@ # Change Log | ||
# [3.0.0](https://github.com/ipfs/js-ipfs-unixfs/compare/ipfs-unixfs@2.0.4...ipfs-unixfs@3.0.0) (2021-02-18) | ||
### Features | ||
* add types ([#114](https://github.com/ipfs/js-ipfs-unixfs/issues/114)) ([ca26353](https://github.com/ipfs/js-ipfs-unixfs/commit/ca26353081ae192718532d3dbd60779863fe6d53)) | ||
### BREAKING CHANGES | ||
* types are now included with all unixfs modules | ||
## [2.0.4](https://github.com/ipfs/js-ipfs-unixfs/compare/ipfs-unixfs@2.0.3...ipfs-unixfs@2.0.4) (2020-11-04) | ||
@@ -8,0 +24,0 @@ |
{ | ||
"name": "ipfs-unixfs", | ||
"version": "2.0.5-rc.8+010ab47", | ||
"version": "3.0.0", | ||
"description": "JavaScript implementation of IPFS' unixfs (a Unix FileSystem representation on top of a MerkleDAG)", | ||
@@ -11,11 +11,12 @@ "leadMaintainer": "Alex Potsides <alex.potsides@protocol.ai>", | ||
"scripts": { | ||
"test": "aegir test -t node -t browser -t webworker", | ||
"test:node": "aegir test -t node", | ||
"test:browser": "aegir test -t browser", | ||
"test:webworker": "aegir test -t webworker", | ||
"build": "aegir build", | ||
"prepare": "run-s prepare:*", | ||
"prepare:proto": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/unixfs.js ./src/unixfs.proto", | ||
"prepare:proto:types": "pbts -o src/unixfs.d.ts src/unixfs.js", | ||
"prepare:types": "aegir build --no-bundle", | ||
"prepare:copy": "cp ./src/*.d.ts ./dist/src", | ||
"test": "aegir test", | ||
"clean": "rm -rf ./dist", | ||
"lint": "aegir lint", | ||
"coverage": "nyc -s aegir test -t node && nyc report --reporter=html", | ||
"depcheck": "aegir dep-check" | ||
"depcheck": "aegir dep-check -i mkdirp -i protobufjs -i @types/mocha -i nyc -i npm-run-all" | ||
}, | ||
@@ -39,11 +40,22 @@ "repository": { | ||
"devDependencies": { | ||
"aegir": "^29.2.2", | ||
"@types/mocha": "^8.2.0", | ||
"aegir": "^30.3.0", | ||
"mkdirp": "^1.0.4", | ||
"npm-run-all": "^4.1.5", | ||
"nyc": "^15.0.0", | ||
"uint8arrays": "^1.1.0" | ||
"uint8arrays": "^2.0.5" | ||
}, | ||
"dependencies": { | ||
"err-code": "^2.0.0", | ||
"protons": "^2.0.0" | ||
"err-code": "^3.0.0", | ||
"protobufjs": "^6.10.2" | ||
}, | ||
"gitHead": "010ab4757e2058dbf610fb18ad347370a6a1c88b" | ||
"types": "dist/src/index.d.ts", | ||
"files": [ | ||
"src", | ||
"dist" | ||
], | ||
"eslintConfig": { | ||
"extends": "ipfs" | ||
}, | ||
"gitHead": "e57ba169ab16ce4f36fd1f553e6d4d08f82e6a35" | ||
} |
199
src/index.js
'use strict' | ||
const protons = require('protons') | ||
const pb = protons(require('./unixfs.proto')) | ||
const unixfsData = pb.Data | ||
const { | ||
Data: PBData | ||
} = require('./unixfs') | ||
const errcode = require('err-code') | ||
/** | ||
* @typedef {object} Mtime | ||
* @property {number} secs | ||
* @property {number | null} [nsecs] | ||
*/ | ||
const types = [ | ||
@@ -25,26 +31,21 @@ 'raw', | ||
function parseArgs (args) { | ||
if (args.length === 0) { | ||
return { | ||
type: 'file' | ||
} | ||
/** | ||
* @param {string | number | undefined} [mode] | ||
*/ | ||
function parseMode (mode) { | ||
if (mode == null) { | ||
return undefined | ||
} | ||
if (args.length === 2) { | ||
// support old-style constructor | ||
return { | ||
type: args[0], | ||
data: args[1] | ||
} | ||
if (typeof mode === 'string') { | ||
mode = parseInt(mode, 8) | ||
} | ||
if (typeof args[0] === 'string' || args[0] instanceof String) { | ||
return { | ||
type: args[0] | ||
} | ||
} | ||
return args[0] | ||
return mode & 0xFFF | ||
} | ||
/** | ||
* @param {null | undefined | { secs: number, nsecs?: number} | { Seconds: number, FractionalNanoseconds?: number} | Mtime | [number, number] | Date} mtime | ||
* @returns {Mtime | undefined} | ||
*/ | ||
function parseMtime (mtime) { | ||
@@ -58,3 +59,5 @@ if (mtime == null) { | ||
mtime = { | ||
// @ts-ignore | ||
secs: mtime.secs, | ||
// @ts-ignore | ||
nsecs: mtime.nsecs | ||
@@ -66,4 +69,7 @@ } | ||
if (Object.prototype.hasOwnProperty.call(mtime, 'Seconds')) { | ||
// @ts-ignore | ||
mtime = { | ||
// @ts-ignore | ||
secs: mtime.Seconds, | ||
// @ts-ignore | ||
nsecs: mtime.FractionalNanoseconds | ||
@@ -111,2 +117,3 @@ } | ||
// @ts-ignore | ||
if (mtime.nsecs < 0 || mtime.nsecs > 999999999) { | ||
@@ -116,32 +123,36 @@ throw errcode(new Error('mtime-nsecs must be within the range [0,999999999]'), 'ERR_INVALID_MTIME_NSECS') | ||
// @ts-ignore | ||
return mtime | ||
} | ||
function parseMode (mode) { | ||
if (mode == null) { | ||
return undefined | ||
} | ||
if (typeof mode === 'string' || mode instanceof String) { | ||
mode = parseInt(mode, 8) | ||
} | ||
return mode & 0xFFF | ||
} | ||
class Data { | ||
// decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md | ||
/** | ||
* Decode from protobuf https://github.com/ipfs/specs/blob/master/UNIXFS.md | ||
* | ||
* @param {Uint8Array} marshaled | ||
*/ | ||
static unmarshal (marshaled) { | ||
const decoded = unixfsData.decode(marshaled) | ||
const message = PBData.decode(marshaled) | ||
const decoded = PBData.toObject(message, { | ||
defaults: false, | ||
arrays: true, | ||
longs: Number, | ||
objects: false | ||
}) | ||
const data = new Data({ | ||
type: types[decoded.Type], | ||
data: decoded.hasData() ? decoded.Data : undefined, | ||
data: decoded.Data, | ||
blockSizes: decoded.blocksizes, | ||
mode: decoded.hasMode() ? decoded.mode : undefined, | ||
mtime: decoded.hasMtime() ? decoded.mtime : undefined | ||
mode: decoded.mode, | ||
mtime: decoded.mtime | ||
? { | ||
secs: decoded.mtime.Seconds, | ||
nsecs: decoded.mtime.FractionalNanoseconds | ||
} | ||
: undefined | ||
}) | ||
// make sure we honor the original mode | ||
data._originalMode = decoded.hasMode() ? decoded.mode : undefined | ||
// make sure we honour the original mode | ||
data._originalMode = decoded.mode || 0 | ||
@@ -151,3 +162,15 @@ return data | ||
constructor (...args) { | ||
/** | ||
* @param {object} [options] | ||
* @param {string} [options.type='file'] | ||
* @param {Uint8Array} [options.data] | ||
* @param {number[]} [options.blockSizes] | ||
* @param {number} [options.hashType] | ||
* @param {number} [options.fanout] | ||
* @param {Mtime | Date | null} [options.mtime] | ||
* @param {number | string} [options.mode] | ||
*/ | ||
constructor (options = { | ||
type: 'file' | ||
}) { | ||
const { | ||
@@ -161,39 +184,54 @@ type, | ||
mode | ||
} = parseArgs(args) | ||
} = options | ||
if (!types.includes(type)) { | ||
if (type && !types.includes(type)) { | ||
throw errcode(new Error('Type: ' + type + ' is not valid'), 'ERR_INVALID_TYPE') | ||
} | ||
this.type = type | ||
this.type = type || 'file' | ||
this.data = data | ||
this.hashType = hashType | ||
this.fanout = fanout | ||
/** @type {number[]} */ | ||
this.blockSizes = blockSizes || [] | ||
this._originalMode = 0 | ||
this.mode = parseMode(mode) | ||
const parsedMode = parseMode(mode) | ||
if (mtime) { | ||
this.mtime = parseMtime(mtime) | ||
if (parsedMode !== undefined) { | ||
this.mode = parsedMode | ||
if (this.mtime && !this.mtime.nsecs) { | ||
this.mtime.nsecs = 0 | ||
} | ||
} | ||
} | ||
if (this.mode === undefined && type === 'file') { | ||
this.mode = DEFAULT_FILE_MODE | ||
} | ||
/** | ||
* @param {number | undefined} mode | ||
*/ | ||
set mode (mode) { | ||
this._mode = this.isDirectory() ? DEFAULT_DIRECTORY_MODE : DEFAULT_FILE_MODE | ||
if (this.mode === undefined && this.isDirectory()) { | ||
this.mode = DEFAULT_DIRECTORY_MODE | ||
} | ||
const parsedMode = parseMode(mode) | ||
const parsedMtime = parseMtime(mtime) | ||
if (parsedMtime) { | ||
this.mtime = parsedMtime | ||
if (parsedMode !== undefined) { | ||
this._mode = parsedMode | ||
} | ||
} | ||
/** | ||
* @returns {number | undefined} | ||
*/ | ||
get mode () { | ||
return this._mode | ||
} | ||
isDirectory () { | ||
return dirTypes.includes(this.type) | ||
return Boolean(this.type && dirTypes.includes(this.type)) | ||
} | ||
/** | ||
* @param {number} size | ||
*/ | ||
addBlockSize (size) { | ||
@@ -203,2 +241,5 @@ this.blockSizes.push(size) | ||
/** | ||
* @param {number} index | ||
*/ | ||
removeBlockSize (index) { | ||
@@ -208,7 +249,9 @@ this.blockSizes.splice(index, 1) | ||
// data.length + blockSizes | ||
/** | ||
* Returns `0` for directories or `data.length + sum(blockSizes)` for everything else | ||
*/ | ||
fileSize () { | ||
if (this.isDirectory()) { | ||
// dirs don't have file size | ||
return undefined | ||
return 0 | ||
} | ||
@@ -228,3 +271,5 @@ | ||
// encode to protobuf | ||
/** | ||
* encode to protobuf Uint8Array | ||
*/ | ||
marshal () { | ||
@@ -234,8 +279,8 @@ let type | ||
switch (this.type) { | ||
case 'raw': type = unixfsData.DataType.Raw; break | ||
case 'directory': type = unixfsData.DataType.Directory; break | ||
case 'file': type = unixfsData.DataType.File; break | ||
case 'metadata': type = unixfsData.DataType.Metadata; break | ||
case 'symlink': type = unixfsData.DataType.Symlink; break | ||
case 'hamt-sharded-directory': type = unixfsData.DataType.HAMTShard; break | ||
case 'raw': type = PBData.DataType.Raw; break | ||
case 'directory': type = PBData.DataType.Directory; break | ||
case 'file': type = PBData.DataType.File; break | ||
case 'metadata': type = PBData.DataType.Metadata; break | ||
case 'symlink': type = PBData.DataType.Symlink; break | ||
case 'hamt-sharded-directory': type = PBData.DataType.HAMTShard; break | ||
default: | ||
@@ -251,14 +296,8 @@ throw errcode(new Error('Type: ' + type + ' is not valid'), 'ERR_INVALID_TYPE') | ||
let blockSizes = this.blockSizes | ||
if (!this.blockSizes || !this.blockSizes.length) { | ||
blockSizes = undefined | ||
} | ||
let mode | ||
if (this.mode != null) { | ||
mode = (this._originalMode & 0xFFFFF000) | parseMode(this.mode) | ||
mode = (this._originalMode & 0xFFFFF000) | (parseMode(this.mode) || 0) | ||
if (mode === DEFAULT_FILE_MODE && this.type === 'file') { | ||
if (mode === DEFAULT_FILE_MODE && !this.isDirectory()) { | ||
mode = undefined | ||
@@ -289,7 +328,7 @@ } | ||
return unixfsData.encode({ | ||
const pbData = { | ||
Type: type, | ||
Data: data, | ||
filesize: this.fileSize(), | ||
blocksizes: blockSizes, | ||
filesize: this.isDirectory() ? undefined : this.fileSize(), | ||
blocksizes: this.blockSizes, | ||
hashType: this.hashType, | ||
@@ -299,3 +338,5 @@ fanout: this.fanout, | ||
mtime | ||
}) | ||
} | ||
return PBData.encode(pbData).finish() | ||
} | ||
@@ -302,0 +343,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
63433
11
1403
0
6
+ Addedprotobufjs@^6.10.2
+ Added@protobufjs/aspromise@1.1.2(transitive)
+ Added@protobufjs/base64@1.1.2(transitive)
+ Added@protobufjs/codegen@2.0.4(transitive)
+ Added@protobufjs/eventemitter@1.1.0(transitive)
+ Added@protobufjs/fetch@1.1.0(transitive)
+ Added@protobufjs/float@1.0.2(transitive)
+ Added@protobufjs/inquire@1.1.0(transitive)
+ Added@protobufjs/path@1.1.2(transitive)
+ Added@protobufjs/pool@1.1.0(transitive)
+ Added@protobufjs/utf8@1.1.0(transitive)
+ Added@types/long@4.0.2(transitive)
+ Added@types/node@22.9.0(transitive)
+ Addederr-code@3.0.1(transitive)
+ Addedlong@4.0.0(transitive)
+ Addedprotobufjs@6.11.4(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removedprotons@^2.0.0
- Removederr-code@2.0.3(transitive)
- Removedmultiformats@9.9.0(transitive)
- Removedprotocol-buffers-schema@3.6.0(transitive)
- Removedprotons@2.0.3(transitive)
- Removedsigned-varint@2.0.1(transitive)
- Removeduint8arrays@3.1.1(transitive)
- Removedvarint@5.0.2(transitive)
Updatederr-code@^3.0.0