@flystorage/file-storage 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,10 +33,8 @@ to, simply because they cannot be abstracted over in a reasonable manner.
33
33
  - [x] Mime-type resolving
34
34
  - [x] Last modified fetching
35
35
  - [x] File size
36
-
37
- ### Planned
38
- - [ ] Moving files
39
- - [ ] Copying files
36
+ - [x] Moving files
37
+ - [x] Copying files
40
38
 
41
39
  ## Implementations / Adapters
42
40
 
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UnableToListDirectory = exports.UnableToCheckDirectoryExistence = exports.UnableToCheckFileExistence = exports.UnableToDeleteFile = exports.UnableToDeleteDirectory = exports.UnableToCreateDirectory = exports.UnableToGetStat = exports.UnableToGetTemporaryUrl = exports.UnableToGetPublicUrl = exports.UnableToGetVisibility = exports.UnableToSetVisibility = exports.UnableToReadFile = exports.UnableToWriteFile = exports.UnableToGetFileSize = exports.UnableToGetLastModified = exports.UnableToGetMimeType = exports.UnableToGetChecksum = exports.ChecksumIsNotAvailable = exports.FlystorageError = exports.errorToMessage = void 0;
3
+ exports.UnableToListDirectory = exports.UnableToCheckDirectoryExistence = exports.UnableToCheckFileExistence = exports.UnableToDeleteFile = exports.UnableToDeleteDirectory = exports.UnableToCreateDirectory = exports.UnableToGetStat = exports.UnableToMoveFile = exports.UnableToCopyFile = exports.UnableToGetTemporaryUrl = exports.UnableToGetPublicUrl = exports.UnableToGetVisibility = exports.UnableToSetVisibility = exports.UnableToReadFile = exports.UnableToWriteFile = exports.UnableToGetFileSize = exports.UnableToGetLastModified = exports.UnableToGetMimeType = exports.UnableToGetChecksum = exports.ChecksumIsNotAvailable = exports.FlystorageError = exports.errorToMessage = void 0;
4
4
  function errorToMessage(error) {
5
5
  return error instanceof Error ? error.message : String(error);
6
6
  }
@@ -80,6 +80,16 @@ class UnableToGetTemporaryUrl extends FlystorageError {
80
80
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetTemporaryUrl(`Unable to get temporary URL. Reason: ${reason}`, context, cause);
81
81
  }
82
82
  exports.UnableToGetTemporaryUrl = UnableToGetTemporaryUrl;
83
+ class UnableToCopyFile extends FlystorageError {
84
+ code = 'flystorage.unable_to_copy_file';
85
+ static because = (reason, { context = {}, cause = undefined }) => new UnableToCopyFile(`Unable to copy file. Reason: ${reason}`, context, cause);
86
+ }
87
+ exports.UnableToCopyFile = UnableToCopyFile;
88
+ class UnableToMoveFile extends FlystorageError {
89
+ code = 'flystorage.unable_to_move_file';
90
+ static because = (reason, { context = {}, cause = undefined }) => new UnableToMoveFile(`Unable to move file. Reason: ${reason}`, context, cause);
91
+ }
92
+ exports.UnableToMoveFile = UnableToMoveFile;
83
93
  class UnableToGetStat extends FlystorageError {
84
94
  code = 'flystorage.unable_to_get_stat';
85
95
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetStat(`Unable to get stat. Reason: ${reason}`, context, cause);
@@ -75,7 +75,7 @@ class FileStorage {
75
75
  async write(path, contents, options = {}) {
76
76
  try {
77
77
  const body = toReadable(contents);
78
- await this.adapter.write(this.pathNormalizer.normalizePath(path), body, Object.assign({}, this.options.writes || {}, options));
78
+ await this.adapter.write(this.pathNormalizer.normalizePath(path), body, { ...this.options.visibility, ...this.options.writes, ...options });
79
79
  await closeReadable(body);
80
80
  }
81
81
  catch (error) {
@@ -109,7 +109,7 @@ class FileStorage {
109
109
  }
110
110
  async createDirectory(path, options = {}) {
111
111
  try {
112
- return await this.adapter.createDirectory(this.pathNormalizer.normalizePath(path), options);
112
+ return await this.adapter.createDirectory(this.pathNormalizer.normalizePath(path), { ...this.options.visibility, ...options });
113
113
  }
114
114
  catch (error) {
115
115
  throw errors.UnableToCreateDirectory.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -131,6 +131,22 @@ class FileStorage {
131
131
  throw errors.UnableToGetStat.because(errors.errorToMessage(error), { cause: error, context: { path } });
132
132
  }
133
133
  }
134
+ async moveFile(from, to, options = {}) {
135
+ try {
136
+ await this.adapter.moveFile(this.pathNormalizer.normalizePath(from), this.pathNormalizer.normalizePath(to), { ...this.options.visibility, ...this.options.moves, ...options });
137
+ }
138
+ catch (error) {
139
+ throw errors.UnableToMoveFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
140
+ }
141
+ }
142
+ async copyFile(from, to, options = {}) {
143
+ try {
144
+ await this.adapter.copyFile(this.pathNormalizer.normalizePath(from), this.pathNormalizer.normalizePath(to), { ...this.options.visibility, ...this.options.copies, ...options });
145
+ }
146
+ catch (error) {
147
+ throw errors.UnableToCopyFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
148
+ }
149
+ }
134
150
  async setVisibility(path, visibility) {
135
151
  try {
136
152
  return await this.adapter.changeVisibility(this.pathNormalizer.normalizePath(path), visibility);
@@ -175,7 +191,7 @@ class FileStorage {
175
191
  }
176
192
  async publicUrl(path, options = {}) {
177
193
  try {
178
- return await this.adapter.publicUrl(this.pathNormalizer.normalizePath(path), options);
194
+ return await this.adapter.publicUrl(this.pathNormalizer.normalizePath(path), { ...this.options.publicUrls, ...options });
179
195
  }
180
196
  catch (error) {
181
197
  throw errors.UnableToGetPublicUrl.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -183,7 +199,7 @@ class FileStorage {
183
199
  }
184
200
  async temporaryUrl(path, options) {
185
201
  try {
186
- return await this.adapter.temporaryUrl(this.pathNormalizer.normalizePath(path), options);
202
+ return await this.adapter.temporaryUrl(this.pathNormalizer.normalizePath(path), { ...this.options.temporaryUrls, ...options });
187
203
  }
188
204
  catch (error) {
189
205
  throw errors.UnableToGetTemporaryUrl.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -191,7 +207,7 @@ class FileStorage {
191
207
  }
192
208
  async checksum(path, options = {}) {
193
209
  try {
194
- return await this.adapter.checksum(this.pathNormalizer.normalizePath(path), options);
210
+ return await this.adapter.checksum(this.pathNormalizer.normalizePath(path), { ...this.options.checksums, ...options });
195
211
  }
196
212
  catch (error) {
197
213
  if (error instanceof errors_js_1.ChecksumIsNotAvailable) {
@@ -202,7 +218,7 @@ class FileStorage {
202
218
  }
203
219
  async mimeType(path, options = {}) {
204
220
  try {
205
- return await this.adapter.mimeType(this.pathNormalizer.normalizePath(path), options);
221
+ return await this.adapter.mimeType(this.pathNormalizer.normalizePath(path), { ...this.options.mimeTypes, ...options });
206
222
  }
207
223
  catch (error) {
208
224
  throw errors.UnableToGetMimeType.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -64,6 +64,14 @@ export class UnableToGetTemporaryUrl extends FlystorageError {
64
64
  code = 'flystorage.unable_to_get_temporary_url';
65
65
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetTemporaryUrl(`Unable to get temporary URL. Reason: ${reason}`, context, cause);
66
66
  }
67
+ export class UnableToCopyFile extends FlystorageError {
68
+ code = 'flystorage.unable_to_copy_file';
69
+ static because = (reason, { context = {}, cause = undefined }) => new UnableToCopyFile(`Unable to copy file. Reason: ${reason}`, context, cause);
70
+ }
71
+ export class UnableToMoveFile extends FlystorageError {
72
+ code = 'flystorage.unable_to_move_file';
73
+ static because = (reason, { context = {}, cause = undefined }) => new UnableToMoveFile(`Unable to move file. Reason: ${reason}`, context, cause);
74
+ }
67
75
  export class UnableToGetStat extends FlystorageError {
68
76
  code = 'flystorage.unable_to_get_stat';
69
77
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetStat(`Unable to get stat. Reason: ${reason}`, context, cause);
@@ -69,7 +69,7 @@ export class FileStorage {
69
69
  async write(path, contents, options = {}) {
70
70
  try {
71
71
  const body = toReadable(contents);
72
- await this.adapter.write(this.pathNormalizer.normalizePath(path), body, Object.assign({}, this.options.writes || {}, options));
72
+ await this.adapter.write(this.pathNormalizer.normalizePath(path), body, { ...this.options.visibility, ...this.options.writes, ...options });
73
73
  await closeReadable(body);
74
74
  }
75
75
  catch (error) {
@@ -103,7 +103,7 @@ export class FileStorage {
103
103
  }
104
104
  async createDirectory(path, options = {}) {
105
105
  try {
106
- return await this.adapter.createDirectory(this.pathNormalizer.normalizePath(path), options);
106
+ return await this.adapter.createDirectory(this.pathNormalizer.normalizePath(path), { ...this.options.visibility, ...options });
107
107
  }
108
108
  catch (error) {
109
109
  throw errors.UnableToCreateDirectory.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -125,6 +125,22 @@ export class FileStorage {
125
125
  throw errors.UnableToGetStat.because(errors.errorToMessage(error), { cause: error, context: { path } });
126
126
  }
127
127
  }
128
+ async moveFile(from, to, options = {}) {
129
+ try {
130
+ await this.adapter.moveFile(this.pathNormalizer.normalizePath(from), this.pathNormalizer.normalizePath(to), { ...this.options.visibility, ...this.options.moves, ...options });
131
+ }
132
+ catch (error) {
133
+ throw errors.UnableToMoveFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
134
+ }
135
+ }
136
+ async copyFile(from, to, options = {}) {
137
+ try {
138
+ await this.adapter.copyFile(this.pathNormalizer.normalizePath(from), this.pathNormalizer.normalizePath(to), { ...this.options.visibility, ...this.options.copies, ...options });
139
+ }
140
+ catch (error) {
141
+ throw errors.UnableToCopyFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
142
+ }
143
+ }
128
144
  async setVisibility(path, visibility) {
129
145
  try {
130
146
  return await this.adapter.changeVisibility(this.pathNormalizer.normalizePath(path), visibility);
@@ -169,7 +185,7 @@ export class FileStorage {
169
185
  }
170
186
  async publicUrl(path, options = {}) {
171
187
  try {
172
- return await this.adapter.publicUrl(this.pathNormalizer.normalizePath(path), options);
188
+ return await this.adapter.publicUrl(this.pathNormalizer.normalizePath(path), { ...this.options.publicUrls, ...options });
173
189
  }
174
190
  catch (error) {
175
191
  throw errors.UnableToGetPublicUrl.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -177,7 +193,7 @@ export class FileStorage {
177
193
  }
178
194
  async temporaryUrl(path, options) {
179
195
  try {
180
- return await this.adapter.temporaryUrl(this.pathNormalizer.normalizePath(path), options);
196
+ return await this.adapter.temporaryUrl(this.pathNormalizer.normalizePath(path), { ...this.options.temporaryUrls, ...options });
181
197
  }
182
198
  catch (error) {
183
199
  throw errors.UnableToGetTemporaryUrl.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -185,7 +201,7 @@ export class FileStorage {
185
201
  }
186
202
  async checksum(path, options = {}) {
187
203
  try {
188
- return await this.adapter.checksum(this.pathNormalizer.normalizePath(path), options);
204
+ return await this.adapter.checksum(this.pathNormalizer.normalizePath(path), { ...this.options.checksums, ...options });
189
205
  }
190
206
  catch (error) {
191
207
  if (error instanceof ChecksumIsNotAvailable) {
@@ -196,7 +212,7 @@ export class FileStorage {
196
212
  }
197
213
  async mimeType(path, options = {}) {
198
214
  try {
199
- return await this.adapter.mimeType(this.pathNormalizer.normalizePath(path), options);
215
+ return await this.adapter.mimeType(this.pathNormalizer.normalizePath(path), { ...this.options.mimeTypes, ...options });
200
216
  }
201
217
  catch (error) {
202
218
  throw errors.UnableToGetMimeType.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -94,6 +94,20 @@ export declare class UnableToGetTemporaryUrl extends FlystorageError {
94
94
  cause?: unknown;
95
95
  }) => UnableToGetTemporaryUrl;
96
96
  }
97
+ export declare class UnableToCopyFile extends FlystorageError {
98
+ readonly code = "flystorage.unable_to_copy_file";
99
+ static because: (reason: string, { context, cause }: {
100
+ context?: ErrorContext | undefined;
101
+ cause?: unknown;
102
+ }) => UnableToCopyFile;
103
+ }
104
+ export declare class UnableToMoveFile extends FlystorageError {
105
+ readonly code = "flystorage.unable_to_move_file";
106
+ static because: (reason: string, { context, cause }: {
107
+ context?: ErrorContext | undefined;
108
+ cause?: unknown;
109
+ }) => UnableToMoveFile;
110
+ }
97
111
  export declare class UnableToGetStat extends FlystorageError {
98
112
  readonly code = "flystorage.unable_to_get_stat";
99
113
  static because: (reason: string, { context, cause }: {
@@ -30,6 +30,8 @@ export interface StorageAdapter {
30
30
  read(path: string): Promise<FileContents>;
31
31
  deleteFile(path: string): Promise<void>;
32
32
  createDirectory(path: string, options: CreateDirectoryOptions): Promise<void>;
33
+ copyFile(from: string, to: string, options: CopyFileOptions): Promise<void>;
34
+ moveFile(from: string, to: string, options: MoveFileOptions): Promise<void>;
33
35
  stat(path: string): Promise<StatEntry>;
34
36
  list(path: string, options: {
35
37
  deep: boolean;
@@ -73,6 +75,15 @@ export type WriteOptions = VisibilityOptions & MiscellaneousOptions & {
73
75
  };
74
76
  export type CreateDirectoryOptions = MiscellaneousOptions & Pick<VisibilityOptions, 'directoryVisibility'> & {};
75
77
  export type PublicUrlOptions = MiscellaneousOptions & {};
78
+ export type CopyFileOptions = MiscellaneousOptions & VisibilityOptions & {
79
+ retainVisibility?: boolean;
80
+ };
81
+ export type MoveFileOptions = MiscellaneousOptions & VisibilityOptions & {
82
+ retainVisibility?: boolean;
83
+ };
84
+ export type ListOptions = {
85
+ deep?: boolean;
86
+ };
76
87
  export type TemporaryUrlOptions = MiscellaneousOptions & {
77
88
  expiresAt: ExpiresAt;
78
89
  };
@@ -81,8 +92,14 @@ export type ChecksumOptions = MiscellaneousOptions & {
81
92
  encoding?: BinaryToTextEncoding;
82
93
  };
83
94
  export type ConfigurationOptions = {
84
- defaults?: VisibilityOptions;
95
+ visibility?: VisibilityOptions;
85
96
  writes?: WriteOptions;
97
+ moves?: MoveFileOptions;
98
+ copies?: CopyFileOptions;
99
+ publicUrls?: PublicUrlOptions;
100
+ temporaryUrls?: TemporaryUrlOptions;
101
+ checksums?: ChecksumOptions;
102
+ mimeTypes?: MimeTypeOptions;
86
103
  };
87
104
  export declare class FileStorage {
88
105
  private readonly adapter;
@@ -98,12 +115,12 @@ export declare class FileStorage {
98
115
  createDirectory(path: string, options?: CreateDirectoryOptions): Promise<void>;
99
116
  deleteDirectory(path: string): Promise<void>;
100
117
  stat(path: string): Promise<StatEntry>;
118
+ moveFile(from: string, to: string, options?: MoveFileOptions): Promise<void>;
119
+ copyFile(from: string, to: string, options?: CopyFileOptions): Promise<void>;
101
120
  setVisibility(path: string, visibility: string): Promise<void>;
102
121
  visibility(path: string): Promise<string>;
103
122
  fileExists(path: string): Promise<boolean>;
104
- list(path: string, { deep }?: {
105
- deep?: boolean;
106
- }): DirectoryListing;
123
+ list(path: string, { deep }?: ListOptions): DirectoryListing;
107
124
  statFile(path: string): Promise<FileInfo>;
108
125
  directoryExists(path: string): Promise<boolean>;
109
126
  publicUrl(path: string, options?: PublicUrlOptions): Promise<string>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@flystorage/file-storage",
3
3
  "type": "module",
4
- "version": "0.0.6",
4
+ "version": "0.0.8",
5
5
  "description": "File-storage abstraction: multiple filesystems, one API.",
6
6
  "main": "./dist/cjs/index.js",
7
7
  "types": "./dist/types/index.d.ts",
@@ -27,9 +27,16 @@
27
27
  "author": "Frank de Jonge (https://frankdejonge.nl)",
28
28
  "repository": {
29
29
  "type": "git",
30
- "url": "https://github.com/flystorage/flystorage.git",
30
+ "url": "git+https://github.com/flystorage/flystorage.git",
31
31
  "directory": "packages/file-storage"
32
32
  },
33
- "keywords": ["fs", "file", "files", "filesystem", "filesystems", "storage"],
33
+ "keywords": [
34
+ "fs",
35
+ "file",
36
+ "files",
37
+ "filesystem",
38
+ "filesystems",
39
+ "storage"
40
+ ],
34
41
  "license": "MIT"
35
42
  }