Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Refactor node download logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Mar 27, 2021
1 parent 8db8be8 commit 9152ff4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/client/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { downloadFileHnsToPath, downloadFileToPath } from "../download/node";
import { downloadFileHnsToPath, downloadFileToPath, downloadFileToPathRequest } from "../download/node";
import {
uploadDirectoryFromPath,
uploadDirectoryFromPathRequest,
Expand All @@ -13,6 +13,7 @@ export class SkynetClient extends Client {
// Download
downloadFileToPath = downloadFileToPath;
downloadFileHnsToPath = downloadFileHnsToPath;
protected downloadFileToPathRequest = downloadFileToPathRequest;

// Upload
uploadDirectoryFromPath = uploadDirectoryFromPath;
Expand Down
23 changes: 22 additions & 1 deletion src/download/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";

import { SkynetClient } from "../client";
import { SkynetClient } from "../client/node";
import {
CustomDownloadOptions,
defaultDownloadOptions,
Expand Down Expand Up @@ -77,6 +77,27 @@ export async function downloadFileHnsToPath(

const url = this.getHnsUrl(domain, opts);

return this.downloadFileToPathRequest(url, path, opts);
}

/**
* Makes a request to download a file to a path from Skynet.
*
* @param this - SkynetClient
* @param url - URL.
* @param path - Path to create the local file at.
* @param [customOptions] - Additional settings that can optionally be set.
* @returns - The metadata in JSON format. Each field will be empty if no metadata was found.
* @throws - Will throw if the request does not succeed or the response is missing data.
*/
export async function downloadFileToPathRequest(
this: SkynetClient,
url: string,
path: string,
customOptions?: CustomDownloadOptions
): Promise<GetMetadataResponse> {
const opts = { ...defaultDownloadHnsOptions, ...this.customOptions, ...customOptions };

const writer = fs.createWriteStream(path);

const response = await this.executeRequest({
Expand Down
2 changes: 1 addition & 1 deletion src/download/web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SkynetClient } from "../client";
import { SkynetClient } from "../client/web";
import {
CustomDownloadOptions,
defaultDownloadOptions,
Expand Down
17 changes: 5 additions & 12 deletions src/index.node.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
export { SkynetClient } from "./client/node";
export { deriveChildSeed, genKeyPairAndSeed, genKeyPairFromSeed } from "./crypto";
export {
MAX_REVISION,
defaultPortalUrl,
defaultSkynetPortalUrl,
getRelativeFilePath,
getRootDirectory,
parseSkylink,
uriHandshakePrefix,
uriHandshakeResolverPrefix,
uriSkynetPrefix,
} from "./utils";
export { parseSkylink, uriHandshakePrefix, uriHandshakeResolverPrefix, uriSkynetPrefix } from "./utils/skylink";
export { MAX_REVISION } from "./utils/number";
export { defaultPortalUrl, defaultSkynetPortalUrl } from "./utils/url";
export { getRelativeFilePath, getRootDirectory } from "./utils/file";

// Export types.

Expand All @@ -20,4 +13,4 @@ export type { CustomDownloadOptions, ResolveHnsResponse } from "./download";
export type { CustomGetEntryOptions, CustomSetEntryOptions, SignedRegistryEntry, RegistryEntry } from "./registry";
export type { CustomGetJSONOptions, CustomSetJSONOptions, VersionedEntryData } from "./skydb";
export type { CustomUploadOptions, UploadRequestResponse } from "./upload/index";
export type { ParseSkylinkOptions } from "./utils";
export type { ParseSkylinkOptions } from "./utils/skylink";
3 changes: 1 addition & 2 deletions src/upload/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { defaultOptions, BaseCustomOptions } from "../utils";
import { defaultOptions, BaseCustomOptions } from "../utils/skylink";

/**
* Custom upload options.
*
* @property [portalFileFieldname="file"] - The file fieldname for uploading files on this portal.
* @property [portalDirectoryfilefieldname="files[]"] - The file fieldname for uploading directories on this portal.
* @property [customFilename] - The custom filename to use when uploading files.
* @property [query] - Query parameters.
*/
export type CustomUploadOptions = BaseCustomOptions & {
portalFileFieldname?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/upload/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import p from "path";

import { SkynetClient } from "../client/node";
import { CustomUploadOptions, defaultUploadOptions, UploadRequestResponse } from "./index";
import { formatSkylink } from "../utils";
import { formatSkylink } from "../utils/skylink";

/**
* Uploads a file from the given local path to Skynet.
Expand Down
2 changes: 2 additions & 0 deletions src/utils/skylink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { trimForwardSlash, trimSuffix, trimUriPrefix } from "./string";
* Base custom options for methods hitting the API.
*
* @property [endpointPath] - The relative URL path of the portal endpoint to contact.
* @property [query] - Query parameters.
*/
export type BaseCustomOptions = CustomClientOptions & {
endpointPath?: string;
query?: Record<string, unknown>;
};

/**
Expand Down

0 comments on commit 9152ff4

Please sign in to comment.