This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert date to mtime in glob source (#106)
The UnixFS importer requires mtimes to follow the `Mtime` interface so convert files/folders read from glob source.
- Loading branch information
1 parent
2421ee2
commit cd9e903
Showing
9 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { Mtime, MtimeLike } from 'ipfs-unixfs' | ||
|
||
export function toMtime (mtimeLike: MtimeLike): Mtime | ||
export function toMtime (mtimeLike?: MtimeLike): Mtime | undefined | ||
export function toMtime (mtimeLike?: MtimeLike): Mtime | undefined { | ||
if (mtimeLike == null) { | ||
return undefined | ||
} | ||
|
||
if (isMtime(mtimeLike)) { | ||
return mtimeLike | ||
} | ||
|
||
if (mtimeLike instanceof Date) { | ||
return dateToTimespec(mtimeLike) | ||
} | ||
|
||
if (Array.isArray(mtimeLike)) { | ||
const output: Mtime = { | ||
secs: BigInt(mtimeLike[0]) | ||
} | ||
|
||
if (mtimeLike.length > 1) { | ||
output.nsecs = mtimeLike[1] | ||
} | ||
|
||
return output | ||
} | ||
|
||
if (typeof mtimeLike.Seconds === 'number') { | ||
const output: Mtime = { | ||
secs: BigInt(mtimeLike.Seconds) | ||
} | ||
|
||
if (mtimeLike.FractionalNanoseconds != null) { | ||
output.nsecs = mtimeLike.FractionalNanoseconds | ||
} | ||
|
||
return output | ||
} | ||
|
||
throw new Error('Cannot convert object to mtime') | ||
} | ||
|
||
function dateToTimespec (date: Date): Mtime { | ||
const ms = date.getTime() | ||
const secs = Math.floor(ms / 1000) | ||
|
||
return { | ||
secs: BigInt(secs), | ||
nsecs: (ms - (secs * 1000)) * 1000 | ||
} | ||
} | ||
|
||
function isMtime (obj: any): obj is Mtime { | ||
return typeof obj.secs === 'bigint' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'aegir/chai' | ||
import { toMtime } from '../../src/utils/to-mtime.js' | ||
|
||
describe('to-mtime', () => { | ||
it('should survive undefined', async function () { | ||
const result = toMtime() | ||
|
||
expect(result).to.equal(undefined) | ||
}) | ||
|
||
it('should convert a date', async function () { | ||
const input = new Date() | ||
const result = toMtime(input) | ||
|
||
expect(result?.secs).to.equal(BigInt(Math.floor(input.getTime() / 1000))) | ||
}) | ||
|
||
it('should convert a timespec', async function () { | ||
const input = { | ||
Seconds: 100 | ||
} | ||
const result = toMtime(input) | ||
|
||
expect(result?.secs).to.equal(BigInt(input.Seconds)) | ||
expect(result?.nsecs).to.be.undefined() | ||
}) | ||
|
||
it('should convert a timespec with fractional nanoseconds', async function () { | ||
const input = { | ||
Seconds: 100, | ||
FractionalNanoseconds: 5 | ||
} | ||
const result = toMtime(input) | ||
|
||
expect(result?.secs).to.equal(BigInt(input.Seconds)) | ||
expect(result?.nsecs).to.equal(input.FractionalNanoseconds) | ||
}) | ||
|
||
it('should convert a mtime', async function () { | ||
const input = { | ||
secs: 100n | ||
} | ||
const result = toMtime(input) | ||
|
||
expect(result?.secs).to.equal(input.secs) | ||
expect(result?.nsecs).to.be.undefined() | ||
}) | ||
|
||
it('should convert a mtime with fractional nanoseconds', async function () { | ||
const input = { | ||
secs: 100n, | ||
nsecs: 5 | ||
} | ||
const result = toMtime(input) | ||
|
||
expect(result?.secs).to.equal(input.secs) | ||
expect(result?.nsecs).to.equal(input.nsecs) | ||
}) | ||
}) |