@sab-js/adapter-local 0.0.0-stage → 3.0.3

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
@@ -1,3 +1,149 @@
1
- # Temporary Holding Version
1
+ # Local Storage Adapter
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > [!NOTE]
4
+ > This package used to be published as `@tweedegolf/sab-adapter-local`, which is deprecated. If you are upgrading, replace `@tweedegolf/sab-adapter-local` with `@sab-js/adapter-local` and use `@sab-js/storage-abstraction` instead of `@tweedegolf/storage-abstraction`.
5
+
6
+ An adapter that mimics a cloud storage service and uses your local file system to store files and folders.
7
+
8
+ This adapter is one of the adapters that is meant to be used as a plugin of the [Storage Abstraction package](https://www.npmjs.com/package/@sab-js/storage-abstraction). However it can be used standalone as well, see [below](#standalone).
9
+
10
+ The [API](https://github.com/sab-js/storage-abstraction/tree/main?tab=readme-ov-file#adapter-api) of the adapter abstracts away the differences between the API's of cloud storage services. The API only supports the basic, most commonly used cloud service operations such as creating a bucket, storing files and so on.
11
+
12
+ This adapter is meant to be used in development phase; you can develop and test your application using the local storage adapter and seamlessly switch to one of the available adapters that interact with a cloud storage service on your production server.
13
+
14
+ However you can use the local storage adapter on your production server as well; files will be stored on the hard disk of your server.
15
+
16
+ If you are new to the Storage Abstraction library you may want to read [this](https://github.com/sab-js/storage-abstraction/blob/main/README.md#how-it-works) first.
17
+
18
+ ```typescript
19
+ import { Storage, Provider } from "@sab-js/storage-abstraction";
20
+
21
+ const configuration = {
22
+ type: Provider.LOCAL,
23
+ directory: "path/to/directory",
24
+ mode: 750,
25
+ };
26
+
27
+ const storage = new Storage(configuration);
28
+
29
+ const result = await storage.listBuckets();
30
+
31
+ console.log(result);
32
+ ```
33
+
34
+ The Storage class is cloud service agnostic and doesn't know anything about the adapter it uses and adapters are completely interchangeable. It only expects the adapter to have implemented all methods of the `IAdapter` interface, see the [API](https://github.com/sab-js/storage-abstraction/blob/main/README.md#adapter-api).
35
+
36
+ When you create a Storage instance it checks the mandatory `provider` key in the configuration object and then loads the appropriate adapter module automatically from your node_modules folder using `require()`. For more information please read [this](https://github.com/sab-js/storage-abstraction/blob/main/README.md#register-your-adapter).
37
+
38
+ ## Configuration
39
+
40
+ The configuration object that you pass to the Storage constructor is forwarded to the constructor of the adapter.
41
+
42
+ The Storage constructor is only interested in the `provider` key of the configuration object, all other keys are necessary for configuring the adapter.
43
+
44
+ The Storage constructor expects the configuration to be of type `StorageAdapterConfig`.
45
+
46
+ The adapter expects the configuration to be of type `AdapterConfig` or a type that extends this type.
47
+
48
+ ```typescript
49
+ export interface AdapterConfig {
50
+ bucketName?: string;
51
+ [id: string]: any; // any mandatory or optional key
52
+ }
53
+
54
+ export interface StorageAdapterConfig extends AdapterConfig {
55
+ type: string;
56
+ }
57
+ ```
58
+
59
+ The type of the configuration object for this adapter:
60
+
61
+ ```typescript
62
+ export interface AdapterConfigLocal extends AdapterConfig {
63
+ directory: string;
64
+ mode?: number;
65
+ }
66
+ ```
67
+
68
+ ## Examples
69
+
70
+ Examples with configuration object:
71
+
72
+ ```typescript
73
+ const s = new Storage({
74
+ type: Provider.LOCAL,
75
+ directory: "path/to/directory",
76
+ mode: 750,
77
+ });
78
+
79
+ const s = new Storage({
80
+ type: Provider.LOCAL,
81
+ directory: "path/to/directory",
82
+ mode: 750,
83
+ bucketName: "the-buck",
84
+ });
85
+ ```
86
+
87
+ Same examples with configuration url:
88
+
89
+ ```typescript
90
+ const s = new Storage("local://path/to/directory?mode=750");
91
+
92
+ const s = new Storage("local://path/to/directory@the-buck?mode=750");
93
+ ```
94
+
95
+ For more information about configuration urls please read [this](https://github.com/sab-js/storage-abstraction/blob/main/README.md#configuration-url).
96
+
97
+ ## Local storage
98
+
99
+ With the optional key `mode` you can set the access rights when you create new local buckets. The default value is `0o777`, note that the actual value is dependent on the umask settings on your system (Linux and MacOS only). You can pass this value both in decimal and in octal format. E.g. `rwxrwxrwx` is `0o777` in octal format or `511` in decimal format.
100
+
101
+ When you use a configuration URL you can only pass values as strings. String values without radix prefix will be interpreted as decimal numbers, so "777" is _not_ the same as "0o777" and yields `41411`. This is probably not what you want. The configuration parser handles this by returning the default value in case you pass a value over decimal `511`.
102
+
103
+ Examples:
104
+
105
+ ```typescript
106
+ const config = {
107
+ type: Provider.LOCAL,
108
+ directory: "path/to/folder",
109
+ mode: 488, // decimal literal
110
+ };
111
+ const s = new Storage(config);
112
+
113
+ // or
114
+ const url = "local://path/to/folder&mode=488";
115
+ const s = new Storage(url);
116
+
117
+ // and the same with octal values:
118
+
119
+ const config = {
120
+ type: Provider.LOCAL,
121
+ directory: "path/to/folder",
122
+ mode: 0o750, // octal literal
123
+ };
124
+ const s = new Storage(config);
125
+
126
+ // or
127
+ const url = "local://path/to/folder&mode=0o750";
128
+ const s = new Storage(url);
129
+ ```
130
+
131
+ Buckets will be created inside the directory `path/to/folder`, parent folders will be created if necessary.
132
+
133
+ ## Standalone
134
+
135
+ You can also use the adapter standalone, without the need to create a Storage instance:
136
+
137
+ ```typescript
138
+ import { AdapterLocal } from "@sab-js/adapter-local";
139
+
140
+ const a = new AdapterLocal({
141
+ directory: "path/to/directory",
142
+ });
143
+ const r = await a.listBuckets();
144
+ console.log(r);
145
+ ```
146
+
147
+ ## API
148
+
149
+ For a complete description of the Adapter API see [this part](https://github.com/sab-js/storage-abstraction/blob/main/README.md#adapter-api) documentation of the Storage Abstraction package readme.
package/changelog.md ADDED
@@ -0,0 +1,53 @@
1
+ # 3.0.3
2
+
3
+ - moved from `@tweedegolf/sab-adapter-local` to `@sab-js/adapter-local`; `@tweedegolf/sab-adapter-local` is deprecated
4
+ - the repository moved to https://github.com/sab-js/storage-abstraction
5
+
6
+ # 3.0.2
7
+
8
+ - update dependencies
9
+
10
+ # 3.0.1
11
+
12
+ - added option `checkIfBucketExists` to the `addFile`, `addFileFromPath`, `addFileFromBuffer` and `addFileFromStream`
13
+
14
+ # 3.0.0
15
+
16
+ - version bump to match the version of Storage and the API
17
+ - removed support for versioning (wasn't implemented consistently anyway)
18
+ - removed `getFileAsURL`
19
+ - removed option `{allVersions: boolean}` for `removeFile`
20
+ - `removeFile` does not fail if the file doesn't exist
21
+ - `deleteBucket` does not fail if the bucket doesn't exist
22
+ - see also the [migration guide](https://github.com/sab-js/storage-abstraction/blob/main/migration_to_api3.0.md)
23
+
24
+ # 1.0.9
25
+
26
+ - include @deprecated functions
27
+
28
+ # 1.0.8
29
+
30
+ - update glob to 11.0.3
31
+ - update rimraf to 6.0.1
32
+ - implemented new API methods `bucketIsPublic`, `getPublicURL` and `getSignedURL`
33
+
34
+ # 1.0.7
35
+
36
+ - add support for urls (paths) without containing directory (credits: https://github.com/tesirm99)
37
+
38
+ # 1.0.6
39
+
40
+ - small cosmetic code changes (no change in functionality)
41
+ - updated glob to 10.3.12
42
+
43
+ # 1.0.5
44
+
45
+ - fix bug in selected bucket
46
+
47
+ # 1.0.3
48
+
49
+ - revert to v1 format of config URLs
50
+ - re-implement storing the selected bucket in local state
51
+ - `selectBucket` and `geSelectedBucket`
52
+ - also implemented as getter
53
+ `storage.bucketName = "the-buck";` and setter `console.log(storage.bucketName);`
@@ -0,0 +1,61 @@
1
+ import { AdapterConfig, IAdapter, Options, Provider, StreamOptions } from "./types/general";
2
+ import { FileBufferParams, FilePathParams, FileStreamParams } from "./types/add_file_params";
3
+ import { ResultObject, ResultObjectBoolean, ResultObjectBuckets, ResultObjectFiles, ResultObjectNumber, ResultObjectObject, ResultObjectStream } from "./types/result";
4
+ export declare abstract class AbstractAdapter implements IAdapter {
5
+ protected _provider: Provider;
6
+ protected _config: AdapterConfig;
7
+ protected _client: any;
8
+ protected _configError: null | string;
9
+ protected _bucketName: null | string;
10
+ constructor(config: string | AdapterConfig);
11
+ get provider(): Provider;
12
+ getProvider(): Provider;
13
+ get config(): AdapterConfig;
14
+ getConfig(): AdapterConfig;
15
+ get configError(): null | string;
16
+ getConfigError(): null | string;
17
+ get serviceClient(): any;
18
+ getServiceClient(): any;
19
+ setSelectedBucket(bucketName: null | string): void;
20
+ getSelectedBucket(): null | string;
21
+ set selectedBucket(bucketName: null | string);
22
+ get selectedBucket(): null | string;
23
+ set bucketName(bucketName: null | string);
24
+ get bucketName(): null | string;
25
+ private getFileAndBucketAndOptions;
26
+ private checkBucket;
27
+ private checkFile;
28
+ protected abstract _listBuckets(): Promise<ResultObjectBuckets>;
29
+ protected abstract _createBucket(name: string, options: Options): Promise<ResultObject>;
30
+ protected abstract _clearBucket(name: string): Promise<ResultObject>;
31
+ protected abstract _deleteBucket(name: string): Promise<ResultObject>;
32
+ protected abstract _bucketExists(name: string): Promise<ResultObjectBoolean>;
33
+ protected abstract _bucketIsPublic(name: string): Promise<ResultObjectBoolean>;
34
+ protected abstract _listFiles(bucketName: string, numFiles: number): Promise<ResultObjectFiles>;
35
+ protected abstract _sizeOf(bucketName: string, fileName: string): Promise<ResultObjectNumber>;
36
+ protected abstract _addFile(params: FilePathParams | FileBufferParams | FileStreamParams): Promise<ResultObject>;
37
+ protected abstract _fileExists(bucketName: string, fileName: string): Promise<ResultObjectBoolean>;
38
+ protected abstract _getFileAsStream(bucketName: string, fileName: string, options: StreamOptions): Promise<ResultObjectStream>;
39
+ protected abstract _getPublicURL(bucketName: string, fileName: string, options: Options): Promise<ResultObject>;
40
+ protected abstract _getSignedURL(bucketName: string, fileName: string, options: Options): Promise<ResultObject>;
41
+ protected abstract _removeFile(bucketName: string, fileName: string): Promise<ResultObject>;
42
+ protected abstract _getPresignedUploadURL(bucketName: string, fileName: string, options: Options): Promise<ResultObjectObject>;
43
+ listBuckets(): Promise<ResultObjectBuckets>;
44
+ createBucket(...args: [bucketName?: string, options?: Options] | [options?: Options]): Promise<ResultObject>;
45
+ clearBucket(name?: string): Promise<ResultObject>;
46
+ deleteBucket(name?: string): Promise<ResultObject>;
47
+ bucketExists(name?: string): Promise<ResultObjectBoolean>;
48
+ bucketIsPublic(name?: string): Promise<ResultObjectBoolean>;
49
+ listFiles(...args: [bucketName?: string, numFiles?: number] | [numFiles?: number] | [bucketName?: string]): Promise<ResultObjectFiles>;
50
+ addFileFromPath(params: FilePathParams): Promise<ResultObject>;
51
+ addFileFromBuffer(params: FileBufferParams): Promise<ResultObject>;
52
+ addFileFromStream(params: FileStreamParams): Promise<ResultObject>;
53
+ addFile(params: FilePathParams | FileBufferParams | FileStreamParams): Promise<ResultObject>;
54
+ getFileAsStream(...args: [bucketName: string, fileName: string, options?: StreamOptions] | [fileName: string, options?: StreamOptions]): Promise<ResultObjectStream>;
55
+ getPublicURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObject>;
56
+ getSignedURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObject>;
57
+ sizeOf(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObjectNumber>;
58
+ fileExists(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObjectBoolean>;
59
+ removeFile(...args: [bucketName: string, fileName: string] | [fileName: string]): Promise<ResultObject>;
60
+ getPresignedUploadURL(...args: [bucketName: string, fileName: string, options?: Options] | [fileName: string, options?: Options]): Promise<ResultObjectObject>;
61
+ }