@flystorage/file-storage 0.0.8 → 0.1.0

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
@@ -41,15 +41,12 @@ to, simply because they cannot be abstracted over in a reasonable manner.
41
41
  ### Implemented
42
42
  - [x] Local Filesystem
43
43
  - [x] AWS S3 (using the V3 SDK)
44
+ - [x] Azure Blob Storage
45
+ - [x] Test implementation (in-memory)
46
+ - [x] Google Cloud Storage
47
+ - [x] Chaos adapter decorator
44
48
 
45
49
  ### Planned
46
-
47
- #### Prio 1
48
- - [ ] Azure Blob Storage
49
- - [ ] Test implementation (in-memory, with staged errors)
50
- - [ ] Google Cloud Storage
51
-
52
- ### Prio 2
53
50
  - [ ] FTP (using `basic-ftp`)
54
51
  - [ ] SFTP (?)
55
52
 
@@ -71,7 +68,7 @@ npm i -S @flystorage/local-fs
71
68
  import {resolve} from 'node:path';
72
69
  import {createReadStream} from 'node:fs';
73
70
  import {FileStorage, Visibility} from '@flystorage/file-storage';
74
- import {LocalFileStorage} from '@flystorage/local-fs';
71
+ import {LocalStorageAdapter} from '@flystorage/local-fs';
75
72
 
76
73
  /**
77
74
  * SETUP
@@ -121,6 +118,5 @@ await storage.deleteDirectory('some-directory');
121
118
 
122
119
  ## Author
123
120
  Flystorage is built by the maintainer of [Flysystem](https://flysystem.thephpleague.com), a
124
- filesystem abstraction for PHP. This brings along more than
125
- a decade of smoothing over filesystem implementation differences
126
- and weighing trade-offs to make a usable API.
121
+ filesystem abstraction for PHP. This brings along over
122
+ a decade of filesystem abstraction experience.
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
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;
3
+ exports.readableToUint8Array = exports.readableToString = exports.closeReadable = exports.normalizeExpiryToMilliseconds = exports.normalizeExpiryToDate = exports.FileStorage = exports.toReadable = exports.DirectoryListing = exports.isDirectory = exports.isFile = void 0;
4
4
  const stream_1 = require("stream");
5
5
  const checksum_from_stream_js_1 = require("./checksum-from-stream.js");
6
6
  const errors = require("./errors.js");
7
7
  const errors_js_1 = require("./errors.js");
8
8
  const path_normalizer_js_1 = require("./path-normalizer.js");
9
+ const util_1 = require("util");
9
10
  function isFile(stat) {
10
11
  return stat.isFile;
11
12
  }
@@ -59,6 +60,7 @@ function toReadable(contents) {
59
60
  }
60
61
  return stream_1.Readable.from(contents);
61
62
  }
63
+ exports.toReadable = toReadable;
62
64
  const naturalSorting = new Intl.Collator(undefined, {
63
65
  numeric: true,
64
66
  sensitivity: 'base'
@@ -147,7 +149,13 @@ class FileStorage {
147
149
  throw errors.UnableToCopyFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
148
150
  }
149
151
  }
152
+ /**
153
+ * @deprecated use changeVisibility instead
154
+ */
150
155
  async setVisibility(path, visibility) {
156
+ return this.changeVisibility(path, visibility);
157
+ }
158
+ async changeVisibility(path, visibility) {
151
159
  try {
152
160
  return await this.adapter.changeVisibility(this.pathNormalizer.normalizePath(path), visibility);
153
161
  }
@@ -272,13 +280,23 @@ async function closeReadable(body) {
272
280
  exports.closeReadable = closeReadable;
273
281
  const decoder = new TextDecoder();
274
282
  async function readableToString(stream) {
275
- return decoder.decode(await readableToUint8Array(stream));
283
+ const contents = decoder.decode(await readableToUint8Array(stream));
284
+ await closeReadable(stream);
285
+ return contents;
276
286
  }
277
287
  exports.readableToString = readableToString;
288
+ const encoder = new util_1.TextEncoder();
278
289
  function readableToUint8Array(stream) {
279
290
  return new Promise((resolve, reject) => {
280
291
  const parts = [];
281
292
  stream.on('data', (chunk) => {
293
+ const type = typeof chunk;
294
+ if (type === 'string') {
295
+ chunk = encoder.encode(chunk);
296
+ }
297
+ else if (type === 'number') {
298
+ chunk = new Uint8Array([chunk]);
299
+ }
282
300
  parts.push(chunk);
283
301
  });
284
302
  stream.on('error', reject);
@@ -287,7 +305,7 @@ function readableToUint8Array(stream) {
287
305
  }
288
306
  exports.readableToUint8Array = readableToUint8Array;
289
307
  function concatUint8Arrays(input) {
290
- const length = input.reduce((l, a) => l + a.byteLength, 0);
308
+ const length = input.reduce((l, a) => l + (a.byteLength), 0);
291
309
  const output = new Uint8Array(length);
292
310
  let position = 0;
293
311
  input.forEach(i => {
@@ -3,6 +3,7 @@ import { checksumFromStream } from './checksum-from-stream.js';
3
3
  import * as errors from './errors.js';
4
4
  import { ChecksumIsNotAvailable } from './errors.js';
5
5
  import { PathNormalizerV1 } from './path-normalizer.js';
6
+ import { TextEncoder } from "util";
6
7
  export function isFile(stat) {
7
8
  return stat.isFile;
8
9
  }
@@ -47,7 +48,7 @@ export class DirectoryListing {
47
48
  }
48
49
  }
49
50
  }
50
- function toReadable(contents) {
51
+ export function toReadable(contents) {
51
52
  if (contents instanceof Readable) {
52
53
  return contents;
53
54
  }
@@ -141,7 +142,13 @@ export class FileStorage {
141
142
  throw errors.UnableToCopyFile.because(errors.errorToMessage(error), { cause: error, context: { from, to } });
142
143
  }
143
144
  }
145
+ /**
146
+ * @deprecated use changeVisibility instead
147
+ */
144
148
  async setVisibility(path, visibility) {
149
+ return this.changeVisibility(path, visibility);
150
+ }
151
+ async changeVisibility(path, visibility) {
145
152
  try {
146
153
  return await this.adapter.changeVisibility(this.pathNormalizer.normalizePath(path), visibility);
147
154
  }
@@ -262,12 +269,22 @@ export async function closeReadable(body) {
262
269
  }
263
270
  const decoder = new TextDecoder();
264
271
  export async function readableToString(stream) {
265
- return decoder.decode(await readableToUint8Array(stream));
272
+ const contents = decoder.decode(await readableToUint8Array(stream));
273
+ await closeReadable(stream);
274
+ return contents;
266
275
  }
276
+ const encoder = new TextEncoder();
267
277
  export function readableToUint8Array(stream) {
268
278
  return new Promise((resolve, reject) => {
269
279
  const parts = [];
270
280
  stream.on('data', (chunk) => {
281
+ const type = typeof chunk;
282
+ if (type === 'string') {
283
+ chunk = encoder.encode(chunk);
284
+ }
285
+ else if (type === 'number') {
286
+ chunk = new Uint8Array([chunk]);
287
+ }
271
288
  parts.push(chunk);
272
289
  });
273
290
  stream.on('error', reject);
@@ -275,7 +292,7 @@ export function readableToUint8Array(stream) {
275
292
  });
276
293
  }
277
294
  function concatUint8Arrays(input) {
278
- const length = input.reduce((l, a) => l + a.byteLength, 0);
295
+ const length = input.reduce((l, a) => l + (a.byteLength), 0);
279
296
  const output = new Uint8Array(length);
280
297
  let position = 0;
281
298
  input.forEach(i => {
@@ -57,7 +57,7 @@ export declare class DirectoryListing implements AsyncIterable<StatEntry> {
57
57
  filter(filter: (entry: StatEntry) => boolean): DirectoryListing;
58
58
  [Symbol.asyncIterator](): AsyncGenerator<StatEntry, void, unknown>;
59
59
  }
60
- export type FileContents = Iterable<any> | AsyncIterable<any> | NodeJS.ReadableStream | Readable;
60
+ export type FileContents = Iterable<any> | AsyncIterable<any> | NodeJS.ReadableStream | Readable | string;
61
61
  export type MiscellaneousOptions = {
62
62
  [option: string]: any;
63
63
  };
@@ -101,6 +101,7 @@ export type ConfigurationOptions = {
101
101
  checksums?: ChecksumOptions;
102
102
  mimeTypes?: MimeTypeOptions;
103
103
  };
104
+ export declare function toReadable(contents: FileContents): Readable;
104
105
  export declare class FileStorage {
105
106
  private readonly adapter;
106
107
  private readonly pathNormalizer;
@@ -117,7 +118,11 @@ export declare class FileStorage {
117
118
  stat(path: string): Promise<StatEntry>;
118
119
  moveFile(from: string, to: string, options?: MoveFileOptions): Promise<void>;
119
120
  copyFile(from: string, to: string, options?: CopyFileOptions): Promise<void>;
121
+ /**
122
+ * @deprecated use changeVisibility instead
123
+ */
120
124
  setVisibility(path: string, visibility: string): Promise<void>;
125
+ changeVisibility(path: string, visibility: string): Promise<void>;
121
126
  visibility(path: string): Promise<string>;
122
127
  fileExists(path: string): Promise<boolean>;
123
128
  list(path: string, { deep }?: ListOptions): DirectoryListing;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@flystorage/file-storage",
3
3
  "type": "module",
4
- "version": "0.0.8",
4
+ "version": "0.1.0",
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",
@@ -18,7 +18,7 @@
18
18
  }
19
19
  },
20
20
  "scripts": {
21
- "compile": "concurrently npm:compile:* && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
21
+ "compile": "rm -rf ./dist/ && concurrently npm:compile:* && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json",
22
22
  "compile:esm": "tsc --outDir ./dist/esm/ --declaration false",
23
23
  "compile:cjs": "tsc --outDir ./dist/cjs/ --declaration false --module commonjs --moduleResolution node",
24
24
  "compile:types": "tsc --outDir ./dist/types/ --declaration --emitDeclarationOnly",
@@ -27,7 +27,7 @@
27
27
  "author": "Frank de Jonge (https://frankdejonge.nl)",
28
28
  "repository": {
29
29
  "type": "git",
30
- "url": "git+https://github.com/flystorage/flystorage.git",
30
+ "url": "git+https://github.com/duna-oss/flystorage.git",
31
31
  "directory": "packages/file-storage"
32
32
  },
33
33
  "keywords": [