@flystorage/file-storage 0.0.4 → 0.0.5

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.
@@ -0,0 +1 @@
1
+ {"type": "commonjs"}
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checksumFromStream = void 0;
4
- const crypto_1 = require("crypto");
5
- async function checksumFromStream(stream, options) {
1
+ import { createHash } from 'crypto';
2
+ export async function checksumFromStream(stream, options) {
6
3
  return new Promise(async (resolve, reject) => {
7
- const hash = (0, crypto_1.createHash)(options.algo ?? 'md5');
4
+ const hash = createHash(options.algo ?? 'md5');
8
5
  stream.on('error', reject);
9
6
  stream.pipe(hash, { end: false });
10
7
  stream.on('end', () => {
@@ -13,4 +10,3 @@ async function checksumFromStream(stream, options) {
13
10
  });
14
11
  });
15
12
  }
16
- exports.checksumFromStream = checksumFromStream;
@@ -1,11 +1,7 @@
1
- "use strict";
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;
4
- function errorToMessage(error) {
1
+ export function errorToMessage(error) {
5
2
  return error instanceof Error ? error.message : String(error);
6
3
  }
7
- exports.errorToMessage = errorToMessage;
8
- class FlystorageError extends Error {
4
+ export class FlystorageError extends Error {
9
5
  context;
10
6
  code = 'unknown_error';
11
7
  constructor(message, context = {}, cause = undefined) {
@@ -15,12 +11,11 @@ class FlystorageError extends Error {
15
11
  this.context = context;
16
12
  }
17
13
  }
18
- exports.FlystorageError = FlystorageError;
19
14
  /**
20
15
  * Thrown when the checksum algo is not supported or not pre-computed. This error
21
16
  * is thrown with the intention of falling back to computing it based on a file read.
22
17
  */
23
- class ChecksumIsNotAvailable extends FlystorageError {
18
+ export class ChecksumIsNotAvailable extends FlystorageError {
24
19
  algo;
25
20
  code = 'flystorage.checksum_not_supported';
26
21
  constructor(message, algo, context = {}, cause = undefined) {
@@ -29,90 +24,72 @@ class ChecksumIsNotAvailable extends FlystorageError {
29
24
  }
30
25
  static checksumNotSupported = (algo, { context = {}, cause = undefined } = {}) => new ChecksumIsNotAvailable(`Checksum algo "${algo}" is not supported`, algo, { ...context, algo }, cause);
31
26
  }
32
- exports.ChecksumIsNotAvailable = ChecksumIsNotAvailable;
33
- class UnableToGetChecksum extends FlystorageError {
27
+ export class UnableToGetChecksum extends FlystorageError {
34
28
  code = 'flystorage.unable_to_get_checksum';
35
29
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetChecksum(`Unable to write the file. Reason: ${reason}`, context, cause);
36
30
  }
37
- exports.UnableToGetChecksum = UnableToGetChecksum;
38
- class UnableToGetMimeType extends FlystorageError {
31
+ export class UnableToGetMimeType extends FlystorageError {
39
32
  code = 'flystorage.unable_to_get_mimetype';
40
33
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetMimeType(`Unable to get mime-type. Reason: ${reason}`, context, cause);
41
34
  }
42
- exports.UnableToGetMimeType = UnableToGetMimeType;
43
- class UnableToGetLastModified extends FlystorageError {
35
+ export class UnableToGetLastModified extends FlystorageError {
44
36
  code = 'flystorage.unable_to_get_last_modified';
45
37
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetLastModified(`Unable to get last modified. Reason: ${reason}`, context, cause);
46
38
  }
47
- exports.UnableToGetLastModified = UnableToGetLastModified;
48
- class UnableToGetFileSize extends FlystorageError {
39
+ export class UnableToGetFileSize extends FlystorageError {
49
40
  code = 'flystorage.unable_to_get_file_size';
50
41
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetFileSize(`Unable to get file size. Reason: ${reason}`, context, cause);
51
42
  }
52
- exports.UnableToGetFileSize = UnableToGetFileSize;
53
- class UnableToWriteFile extends FlystorageError {
43
+ export class UnableToWriteFile extends FlystorageError {
54
44
  code = 'flystorage.unable_to_write_file';
55
45
  static because = (reason, { context = {}, cause = undefined }) => new UnableToWriteFile(`Unable to write the file. Reason: ${reason}`, context, cause);
56
46
  }
57
- exports.UnableToWriteFile = UnableToWriteFile;
58
- class UnableToReadFile extends FlystorageError {
47
+ export class UnableToReadFile extends FlystorageError {
59
48
  code = 'flystorage.unable_to_read_file';
60
49
  static because = (reason, { context = {}, cause = undefined }) => new UnableToReadFile(`Unable to read the file. Reason: ${reason}`, context, cause);
61
50
  }
62
- exports.UnableToReadFile = UnableToReadFile;
63
- class UnableToSetVisibility extends FlystorageError {
51
+ export class UnableToSetVisibility extends FlystorageError {
64
52
  code = 'flystorage.unable_to_set_visibility';
65
53
  static because = (reason, { context = {}, cause = undefined }) => new UnableToSetVisibility(`Unable to set visibility. Reason: ${reason}`, context, cause);
66
54
  }
67
- exports.UnableToSetVisibility = UnableToSetVisibility;
68
- class UnableToGetVisibility extends FlystorageError {
55
+ export class UnableToGetVisibility extends FlystorageError {
69
56
  code = 'flystorage.unable_to_get_visibility';
70
57
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetVisibility(`Unable to get visibility. Reason: ${reason}`, context, cause);
71
58
  }
72
- exports.UnableToGetVisibility = UnableToGetVisibility;
73
- class UnableToGetPublicUrl extends FlystorageError {
59
+ export class UnableToGetPublicUrl extends FlystorageError {
74
60
  code = 'flystorage.unable_to_get_public_url';
75
61
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetPublicUrl(`Unable to get public URL. Reason: ${reason}`, context, cause);
76
62
  }
77
- exports.UnableToGetPublicUrl = UnableToGetPublicUrl;
78
- class UnableToGetTemporaryUrl extends FlystorageError {
63
+ export class UnableToGetTemporaryUrl extends FlystorageError {
79
64
  code = 'flystorage.unable_to_get_temporary_url';
80
65
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetTemporaryUrl(`Unable to get temporary URL. Reason: ${reason}`, context, cause);
81
66
  }
82
- exports.UnableToGetTemporaryUrl = UnableToGetTemporaryUrl;
83
- class UnableToGetStat extends FlystorageError {
67
+ export class UnableToGetStat extends FlystorageError {
84
68
  code = 'flystorage.unable_to_get_stat';
85
69
  static because = (reason, { context = {}, cause = undefined }) => new UnableToGetStat(`Unable to get stat. Reason: ${reason}`, context, cause);
86
70
  static noFileStatResolved = ({ context = {}, cause = undefined }) => new UnableToGetStat(`Stat was not a file.`, context, cause);
87
71
  }
88
- exports.UnableToGetStat = UnableToGetStat;
89
- class UnableToCreateDirectory extends FlystorageError {
72
+ export class UnableToCreateDirectory extends FlystorageError {
90
73
  code = 'flystorage.unable_to_create_directory';
91
74
  static because = (reason, { context = {}, cause = undefined }) => new UnableToCreateDirectory(`Unable to create directory. Reason: ${reason}`, context, cause);
92
75
  }
93
- exports.UnableToCreateDirectory = UnableToCreateDirectory;
94
- class UnableToDeleteDirectory extends FlystorageError {
76
+ export class UnableToDeleteDirectory extends FlystorageError {
95
77
  code = 'flystorage.unable_to_delete_directory';
96
78
  static because = (reason, { context = {}, cause = undefined }) => new UnableToDeleteDirectory(`Unable to delete directory. Reason: ${reason}`, context, cause);
97
79
  }
98
- exports.UnableToDeleteDirectory = UnableToDeleteDirectory;
99
- class UnableToDeleteFile extends FlystorageError {
80
+ export class UnableToDeleteFile extends FlystorageError {
100
81
  code = 'flystorage.unable_to_delete_file';
101
82
  static because = (reason, { context = {}, cause = undefined }) => new UnableToDeleteFile(`Unable to delete file. Reason: ${reason}`, context, cause);
102
83
  }
103
- exports.UnableToDeleteFile = UnableToDeleteFile;
104
- class UnableToCheckFileExistence extends FlystorageError {
84
+ export class UnableToCheckFileExistence extends FlystorageError {
105
85
  code = 'flystorage.unable_to_check_file_existence';
106
86
  static because = (reason, { context = {}, cause = undefined }) => new UnableToCheckFileExistence(`Unable to check file existence. Reason: ${reason}`, context, cause);
107
87
  }
108
- exports.UnableToCheckFileExistence = UnableToCheckFileExistence;
109
- class UnableToCheckDirectoryExistence extends FlystorageError {
88
+ export class UnableToCheckDirectoryExistence extends FlystorageError {
110
89
  code = 'flystorage.unable_to_check_directory_existence';
111
90
  static because = (reason, { context = {}, cause = undefined }) => new UnableToCheckDirectoryExistence(`Unable to check directory existence. Reason: ${reason}`, context, cause);
112
91
  }
113
- exports.UnableToCheckDirectoryExistence = UnableToCheckDirectoryExistence;
114
- class UnableToListDirectory extends FlystorageError {
92
+ export class UnableToListDirectory extends FlystorageError {
115
93
  code = 'flystorage.unable_to_list_directory_contents';
116
94
  static because = (reason, { context = {}, cause = undefined }) => new UnableToListDirectory(`Unable to list directory contents. Reason: ${reason}`, context, cause);
117
95
  }
118
- exports.UnableToListDirectory = UnableToListDirectory;
@@ -1,20 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readableToUint8Array = exports.readableToString = exports.closeReadable = exports.normalizeExpiryToMilliseconds = exports.normalizeExpiryToDate = exports.FileStorage = exports.DirectoryListing = exports.isDirectory = exports.isFile = void 0;
4
- const stream_1 = require("stream");
5
- const checksum_from_stream_js_1 = require("./checksum-from-stream.js");
6
- const errors = require("./errors.js");
7
- const errors_js_1 = require("./errors.js");
8
- const path_normalizer_js_1 = require("./path-normalizer.js");
9
- function isFile(stat) {
1
+ import { Readable } from 'stream';
2
+ import { checksumFromStream } from './checksum-from-stream.js';
3
+ import * as errors from './errors.js';
4
+ import { ChecksumIsNotAvailable } from './errors.js';
5
+ import { PathNormalizerV1 } from './path-normalizer.js';
6
+ export function isFile(stat) {
10
7
  return stat.isFile;
11
8
  }
12
- exports.isFile = isFile;
13
- function isDirectory(stat) {
9
+ export function isDirectory(stat) {
14
10
  return stat.isDirectory;
15
11
  }
16
- exports.isDirectory = isDirectory;
17
- class DirectoryListing {
12
+ export class DirectoryListing {
18
13
  listing;
19
14
  path;
20
15
  deep;
@@ -52,22 +47,21 @@ class DirectoryListing {
52
47
  }
53
48
  }
54
49
  }
55
- exports.DirectoryListing = DirectoryListing;
56
50
  function toReadable(contents) {
57
- if (contents instanceof stream_1.Readable) {
51
+ if (contents instanceof Readable) {
58
52
  return contents;
59
53
  }
60
- return stream_1.Readable.from(contents);
54
+ return Readable.from(contents);
61
55
  }
62
56
  const naturalSorting = new Intl.Collator(undefined, {
63
57
  numeric: true,
64
58
  sensitivity: 'base'
65
59
  });
66
- class FileStorage {
60
+ export class FileStorage {
67
61
  adapter;
68
62
  pathNormalizer;
69
63
  options;
70
- constructor(adapter, pathNormalizer = new path_normalizer_js_1.PathNormalizerV1(), options = {}) {
64
+ constructor(adapter, pathNormalizer = new PathNormalizerV1(), options = {}) {
71
65
  this.adapter = adapter;
72
66
  this.pathNormalizer = pathNormalizer;
73
67
  this.options = options;
@@ -84,7 +78,7 @@ class FileStorage {
84
78
  }
85
79
  async read(path) {
86
80
  try {
87
- return stream_1.Readable.from(await this.adapter.read(this.pathNormalizer.normalizePath(path)));
81
+ return Readable.from(await this.adapter.read(this.pathNormalizer.normalizePath(path)));
88
82
  }
89
83
  catch (error) {
90
84
  throw errors.UnableToReadFile.because(errors.errorToMessage(error), { cause: error, context: { path } });
@@ -194,7 +188,7 @@ class FileStorage {
194
188
  return await this.adapter.checksum(this.pathNormalizer.normalizePath(path), options);
195
189
  }
196
190
  catch (error) {
197
- if (error instanceof errors_js_1.ChecksumIsNotAvailable) {
191
+ if (error instanceof ChecksumIsNotAvailable) {
198
192
  return this.calculateChecksum(path, options);
199
193
  }
200
194
  throw errors.UnableToGetChecksum.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
@@ -226,23 +220,20 @@ class FileStorage {
226
220
  }
227
221
  async calculateChecksum(path, options) {
228
222
  try {
229
- return await (0, checksum_from_stream_js_1.checksumFromStream)(await this.read(path), options);
223
+ return await checksumFromStream(await this.read(path), options);
230
224
  }
231
225
  catch (error) {
232
226
  throw errors.UnableToGetChecksum.because(errors.errorToMessage(error), { cause: error, context: { path, options } });
233
227
  }
234
228
  }
235
229
  }
236
- exports.FileStorage = FileStorage;
237
- function normalizeExpiryToDate(expiresAt) {
230
+ export function normalizeExpiryToDate(expiresAt) {
238
231
  return expiresAt instanceof Date ? expiresAt : new Date(expiresAt);
239
232
  }
240
- exports.normalizeExpiryToDate = normalizeExpiryToDate;
241
- function normalizeExpiryToMilliseconds(expiresAt) {
233
+ export function normalizeExpiryToMilliseconds(expiresAt) {
242
234
  return expiresAt instanceof Date ? expiresAt.getTime() : expiresAt;
243
235
  }
244
- exports.normalizeExpiryToMilliseconds = normalizeExpiryToMilliseconds;
245
- async function closeReadable(body) {
236
+ export async function closeReadable(body) {
246
237
  if (body.closed) {
247
238
  return;
248
239
  }
@@ -253,13 +244,11 @@ async function closeReadable(body) {
253
244
  body.destroy();
254
245
  });
255
246
  }
256
- exports.closeReadable = closeReadable;
257
247
  const decoder = new TextDecoder();
258
- async function readableToString(stream) {
248
+ export async function readableToString(stream) {
259
249
  return decoder.decode(await readableToUint8Array(stream));
260
250
  }
261
- exports.readableToString = readableToString;
262
- function readableToUint8Array(stream) {
251
+ export function readableToUint8Array(stream) {
263
252
  return new Promise((resolve, reject) => {
264
253
  const parts = [];
265
254
  stream.on('data', (chunk) => {
@@ -269,7 +258,6 @@ function readableToUint8Array(stream) {
269
258
  stream.on('end', () => resolve(concatUint8Arrays(parts)));
270
259
  });
271
260
  }
272
- exports.readableToUint8Array = readableToUint8Array;
273
261
  function concatUint8Arrays(input) {
274
262
  const length = input.reduce((l, a) => l + a.byteLength, 0);
275
263
  const output = new Uint8Array(length);
package/dist/esm/index.js CHANGED
@@ -1,22 +1,6 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./checksum-from-stream.js"), exports);
18
- __exportStar(require("./file-storage.js"), exports);
19
- __exportStar(require("./errors.js"), exports);
20
- __exportStar(require("./path-normalizer.js"), exports);
21
- __exportStar(require("./path-prefixer.js"), exports);
22
- __exportStar(require("./portable-visibility.js"), exports);
1
+ export * from './checksum-from-stream.js';
2
+ export * from './file-storage.js';
3
+ export * from './errors.js';
4
+ export * from './path-normalizer.js';
5
+ export * from './path-prefixer.js';
6
+ export * from './portable-visibility.js';
@@ -1,26 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PathTraversalDetected = exports.CorruptedPathDetected = exports.PathNormalizerV1 = void 0;
4
- const node_path_1 = require("node:path");
1
+ import { join } from 'node:path';
5
2
  const funkyWhiteSpaceRegex = new RegExp('\\p{C}+', 'u');
6
- class PathNormalizerV1 {
3
+ export class PathNormalizerV1 {
7
4
  normalizePath(path) {
8
5
  if (funkyWhiteSpaceRegex.test(path)) {
9
6
  throw CorruptedPathDetected.unexpectedWhitespace(path);
10
7
  }
11
- const normalized = (0, node_path_1.join)(...(path.split('/')));
8
+ const normalized = join(...(path.split('/')));
12
9
  if (normalized.indexOf('../') !== -1 || normalized == '..') {
13
10
  throw PathTraversalDetected.forPath(path);
14
11
  }
15
12
  return normalized === '.' ? '' : normalized;
16
13
  }
17
14
  }
18
- exports.PathNormalizerV1 = PathNormalizerV1;
19
- class CorruptedPathDetected extends Error {
15
+ export class CorruptedPathDetected extends Error {
20
16
  static unexpectedWhitespace = (path) => new CorruptedPathDetected(`Corrupted path detected with unexpected whitespace: ${path}`);
21
17
  }
22
- exports.CorruptedPathDetected = CorruptedPathDetected;
23
- class PathTraversalDetected extends Error {
18
+ export class PathTraversalDetected extends Error {
24
19
  static forPath = (path) => new PathTraversalDetected(`Path traversal detected for: ${path}`);
25
20
  }
26
- exports.PathTraversalDetected = PathTraversalDetected;
@@ -1,19 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PathPrefixer = void 0;
4
- const node_path_1 = require("node:path");
5
- class PathPrefixer {
1
+ import { join } from 'node:path';
2
+ export class PathPrefixer {
6
3
  prefix = '';
7
4
  constructor(prefix = '') {
8
5
  if (prefix.length > 0) {
9
- this.prefix = (0, node_path_1.join)(prefix, '/');
6
+ this.prefix = join(prefix, '/');
10
7
  }
11
8
  }
12
9
  prefixFilePath(path) {
13
- return this.prefix.length > 0 ? (0, node_path_1.join)(this.prefix, path) : path;
10
+ return this.prefix.length > 0 ? join(this.prefix, path) : path;
14
11
  }
15
12
  prefixDirectoryPath(path) {
16
- return this.prefix.length > 0 ? (0, node_path_1.join)(this.prefix, path, '/') : (0, node_path_1.join)(path, '/');
13
+ return this.prefix.length > 0 ? join(this.prefix, path, '/') : join(path, '/');
17
14
  }
18
15
  stripFilePath(path) {
19
16
  return path.substring(this.prefix.length);
@@ -22,4 +19,3 @@ class PathPrefixer {
22
19
  return this.stripFilePath(path).replace(/\/+$/g, '');
23
20
  }
24
21
  }
25
- exports.PathPrefixer = PathPrefixer;
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Visibility = void 0;
4
- var Visibility;
1
+ export var Visibility;
5
2
  (function (Visibility) {
6
3
  Visibility["PUBLIC"] = "public";
7
4
  Visibility["PRIVATE"] = "private";
8
- })(Visibility || (exports.Visibility = Visibility = {}));
5
+ })(Visibility || (Visibility = {}));
@@ -1,5 +1,5 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
3
  import { BinaryToTextEncoding } from 'crypto';
4
4
  import { Readable } from 'node:stream';
5
5
  export declare function checksumFromStream(stream: Readable, options: {
@@ -1,7 +1,7 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
- /// <reference types="node" />
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ /// <reference types="node" resolution-mode="require"/>
4
+ /// <reference types="node" resolution-mode="require"/>
5
5
  import { BinaryToTextEncoding } from 'crypto';
6
6
  import { Readable } from 'stream';
7
7
  import { PathNormalizer } from './path-normalizer.js';
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@flystorage/file-storage",
3
- "version": "0.0.4",
3
+ "type": "module",
4
+ "version": "0.0.5",
4
5
  "description": "File-storage abstraction: multiple filesystems, one API.",
5
6
  "main": "./dist/cjs/index.js",
6
7
  "types": "./dist/types/index.d.ts",
@@ -17,7 +18,7 @@
17
18
  }
18
19
  },
19
20
  "scripts": {
20
- "compile": "concurrently npm:compile:*",
21
+ "compile": "concurrently npm:compile:* && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
21
22
  "compile:esm": "tsc --outDir ./dist/esm/ --declaration false",
22
23
  "compile:cjs": "tsc --outDir ./dist/cjs/ --declaration false --module commonjs --moduleResolution node",
23
24
  "compile:types": "tsc --outDir ./dist/types/ --declaration --emitDeclarationOnly",