Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tighten up input types #133

Merged
merged 10 commits into from
Mar 19, 2021
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-exporter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@types/mocha": "^8.2.1",
"@types/sinon": "^9.0.10",
"abort-controller": "^3.0.0",
"aegir": "^32.0.0",
"aegir": "^32.1.0",
"copy": "^0.3.2",
"crypto-browserify": "^3.12.0",
"detect-node": "^2.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-exporter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../node_modules/aegir/src/config/tsconfig.aegir.json",
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"importsNotUsedAsValues": "preserve"
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"@types/mocha": "^8.2.1",
"aegir": "^32.0.0",
"aegir": "^32.1.0",
"assert": "^2.0.0",
"copy": "^0.3.2",
"crypto-browserify": "^3.12.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/ipfs-unixfs-importer/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { UnixFS, Mtime, MtimeLike } from 'ipfs-unixfs'
import { UnixFS, Mtime } from 'ipfs-unixfs'
import CID, { CIDVersion } from 'cids'
import { HashName } from 'multihashes'
import Block from 'ipld-block'
import { CodecName } from 'multicodec'

interface ImportCandidate {
path?: string
content?: AsyncIterable<Uint8Array> | Iterable<Uint8Array> | Uint8Array | ArrayLike<number> | string
mtime?: MtimeLike
content?: AsyncIterable<Uint8Array>
mtime?: Mtime
mode?: number
}

interface File {
content: AsyncIterable<Uint8Array>
path?: string
mtime?: MtimeLike
mtime?: Mtime
mode?: number
}

interface Directory {
path?: string
mtime?: MtimeLike
mtime?: Mtime
mode?: number
}

Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../node_modules/aegir/src/config/tsconfig.aegir.json",
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"importsNotUsedAsValues": "preserve"
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
"devDependencies": {
"@types/mocha": "^8.2.1",
"aegir": "^32.0.0",
"aegir": "^32.1.0",
"copy": "^0.3.2",
"mkdirp": "^1.0.4",
"npm-run-all": "^4.1.5",
Expand Down
18 changes: 13 additions & 5 deletions packages/ipfs-unixfs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const errcode = require('err-code')
/**
* @typedef {object} Mtime
* @property {number} secs
* @property {number | null} [nsecs]
* @property {number} [nsecs]
*/

/**
* @typedef {null | undefined | { secs: number, nsecs?: number} | { Seconds: number, FractionalNanoseconds?: number} | Mtime | [number, number] | Date} MtimeLike
* @typedef {null | undefined | { secs: number, nsecs?: number} | { Seconds: number, FractionalNanoseconds?: number} | Mtime | [number, number] | Date} MtimeLike
*/

const types = [
Expand Down Expand Up @@ -40,11 +40,19 @@ function parseMode (mode) {
return undefined
}

if (typeof mode === 'string') {
mode = parseInt(mode, 8)
if (typeof mode === 'number') {
return mode & 0xFFF
}

return mode & 0xFFF
mode = mode.toString()

if (mode.substring(0, 1) === '0') {
// octal string
return parseInt(mode, 8) & 0xFFF
}

// decimal string
return parseInt(mode, 10) & 0xFFF
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../node_modules/aegir/src/config/tsconfig.aegir.json",
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
Expand Down