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.
feat: add globSource and urlSource (#53)
Ports `globSource` and `urlSource` from `ipfs-utils` for use with `unixfs.add` and `unixfs.addAll`.
- Loading branch information
1 parent
26b5cd3
commit b490a6e
Showing
16 changed files
with
508 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import EchoServer from 'aegir/echo-server' | ||
import { format } from 'iso-url' | ||
|
||
export default { | ||
test: { | ||
async before (options) { | ||
let echoServer = new EchoServer() | ||
await echoServer.start() | ||
const { address, port } = echoServer.server.address() | ||
let hostname = address | ||
if(options.runner === 'react-native-android') { | ||
hostname = '10.0.2.2' | ||
} | ||
return { | ||
echoServer, | ||
env: { ECHO_SERVER : format({ protocol: 'http:', hostname, port })} | ||
} | ||
}, | ||
async after (options, before) { | ||
await before.echoServer.stop() | ||
} | ||
} | ||
} |
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,92 @@ | ||
import fs from 'node:fs' | ||
import fsp from 'node:fs/promises' | ||
import Path from 'node:path' | ||
import glob from 'it-glob' | ||
import { InvalidParametersError } from '../errors.js' | ||
import type { MtimeLike } from 'ipfs-unixfs' | ||
import type { ImportCandidateStream } from 'ipfs-unixfs-importer' | ||
|
||
export interface GlobSourceOptions { | ||
/** | ||
* Include .dot files in matched paths | ||
*/ | ||
hidden?: boolean | ||
|
||
/** | ||
* follow symlinks | ||
*/ | ||
followSymlinks?: boolean | ||
|
||
/** | ||
* Preserve mode | ||
*/ | ||
preserveMode?: boolean | ||
|
||
/** | ||
* Preserve mtime | ||
*/ | ||
preserveMtime?: boolean | ||
|
||
/** | ||
* mode to use - if preserveMode is true this will be ignored | ||
*/ | ||
mode?: number | ||
|
||
/** | ||
* mtime to use - if preserveMtime is true this will be ignored | ||
*/ | ||
mtime?: MtimeLike | ||
} | ||
|
||
export interface GlobSourceResult { | ||
path: string | ||
content: AsyncIterable<Uint8Array> | undefined | ||
mode: number | undefined | ||
mtime: MtimeLike | undefined | ||
} | ||
|
||
/** | ||
* Create an async iterator that yields paths that match requested glob pattern | ||
*/ | ||
export async function * globSource (cwd: string, pattern: string, options: GlobSourceOptions = {}): ImportCandidateStream { | ||
if (typeof pattern !== 'string') { | ||
throw new InvalidParametersError('Pattern must be a string') | ||
} | ||
|
||
if (!Path.isAbsolute(cwd)) { | ||
cwd = Path.resolve(process.cwd(), cwd) | ||
} | ||
|
||
const globOptions = Object.assign({}, { | ||
nodir: false, | ||
realpath: false, | ||
absolute: true, | ||
dot: Boolean(options.hidden), | ||
follow: options.followSymlinks != null ? options.followSymlinks : true | ||
}) | ||
|
||
for await (const p of glob(cwd, pattern, globOptions)) { | ||
const stat = await fsp.stat(p) | ||
|
||
let mode = options.mode | ||
|
||
if (options.preserveMode === true) { | ||
mode = stat.mode | ||
} | ||
|
||
let mtime = options.mtime | ||
|
||
if (options.preserveMtime === true) { | ||
mtime = stat.mtime | ||
} | ||
|
||
yield { | ||
path: toPosix(p.replace(cwd, '')), | ||
content: stat.isFile() ? fs.createReadStream(p) : undefined, | ||
mode, | ||
mtime | ||
} | ||
} | ||
} | ||
|
||
const toPosix = (path: string): string => path.replace(/\\/g, '/') |
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,35 @@ | ||
import { UnknownError } from '../errors.js' | ||
import type { FileCandidate } from 'ipfs-unixfs-importer' | ||
|
||
export function urlSource (url: URL, options?: RequestInit): FileCandidate<AsyncGenerator<Uint8Array, void, unknown>> { | ||
return { | ||
path: decodeURIComponent(new URL(url).pathname.split('/').pop() ?? ''), | ||
content: readURLContent(url, options) | ||
} | ||
} | ||
|
||
async function * readURLContent (url: URL, options?: RequestInit): AsyncGenerator<Uint8Array, void, unknown> { | ||
const response = await globalThis.fetch(url, options) | ||
|
||
if (response.body == null) { | ||
throw new UnknownError('HTTP response did not have a body') | ||
} | ||
|
||
const reader = response.body.getReader() | ||
|
||
try { | ||
while (true) { | ||
const { done, value } = await reader.read() | ||
|
||
if (done) { | ||
return | ||
} | ||
|
||
if (value != null) { | ||
yield value | ||
} | ||
} | ||
} finally { | ||
reader.releaseLock() | ||
} | ||
} |
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
Empty file.
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 @@ | ||
hello |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Oops, something went wrong.