@maestro-js/file-storage 1.0.0-alpha.2 → 1.0.0-alpha.20

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/dist/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Iso } from 'iso-fns2';
2
2
  import { Readable } from 'node:stream';
3
3
  import { ReadableStream } from 'node:stream/web';
4
- import { File } from 'node:buffer';
5
4
  import { S3Client } from '@aws-sdk/client-s3';
6
5
 
7
6
  interface Driver<TemporaryUrlParams> {
@@ -31,8 +30,9 @@ interface Driver<TemporaryUrlParams> {
31
30
  removeDirectory(directory: string): Promise<void>;
32
31
  }
33
32
 
34
- declare function createLocalDriver<TemporaryUrlParams>({ pathPrefix, buildTemporaryUrlsUsing }: {
33
+ declare function createLocalDriver<TemporaryUrlParams>({ pathPrefix, urlPrefix, buildTemporaryUrlsUsing }: {
35
34
  pathPrefix?: string;
35
+ urlPrefix?: string;
36
36
  buildTemporaryUrlsUsing?(path: string, expiration: Iso.Instant, params?: TemporaryUrlParams): string | Promise<string>;
37
37
  }): Driver<TemporaryUrlParams>;
38
38
 
@@ -43,6 +43,13 @@ declare function createS3Driver({ client, bucket, prefix, url: baseUrl }: {
43
43
  url?: string;
44
44
  }): Driver<void>;
45
45
 
46
+ type FileStorageDriver<T = any> = Driver<T>;
47
+ interface FileInput {
48
+ name: string;
49
+ type: string;
50
+ stream(): ReadableStream | globalThis.ReadableStream;
51
+ }
52
+
46
53
  declare function create(config: FileStorage.Provider.FileStorageServiceConfig): FileStorage.FileStorageService;
47
54
  type KeysWithFallback = keyof FileStorage.Provider.Keys extends never ? {
48
55
  default: unknown;
@@ -69,8 +76,8 @@ declare function provider(key: FileStorage.Provider.Key): {
69
76
  append: (path: string, contents: string | Buffer | Readable | ReadableStream) => Promise<void>;
70
77
  copy: (source: string, destination: string) => Promise<void>;
71
78
  move: (source: string, destination: string) => Promise<void>;
72
- putFileAs: (dir: string, file: File, filename: string) => Promise<string>;
73
- putFile: (dir: string, file: File) => Promise<string>;
79
+ putFileAs: (dir: string, file: FileInput, filename: string) => Promise<string>;
80
+ putFile: (dir: string, file: FileInput) => Promise<string>;
74
81
  remove: (...paths: string[]) => Promise<void>;
75
82
  files: (directory: string) => Promise<string[]>;
76
83
  allFiles: (directory: string) => Promise<string[]>;
@@ -106,8 +113,8 @@ declare const FileStorage: {
106
113
  append: (path: string, contents: string | Buffer | Readable | ReadableStream) => Promise<void>;
107
114
  copy: (source: string, destination: string) => Promise<void>;
108
115
  move: (source: string, destination: string) => Promise<void>;
109
- putFileAs: (dir: string, file: File, filename: string) => Promise<string>;
110
- putFile: (dir: string, file: File) => Promise<string>;
116
+ putFileAs: (dir: string, file: FileInput, filename: string) => Promise<string>;
117
+ putFile: (dir: string, file: FileInput) => Promise<string>;
111
118
  remove: (...paths: string[]) => Promise<void>;
112
119
  files: (directory: string) => Promise<string[]>;
113
120
  allFiles: (directory: string) => Promise<string[]>;
@@ -121,7 +128,7 @@ declare const FileStorage: {
121
128
  };
122
129
  };
123
130
  declare namespace FileStorage {
124
- type Driver<T = any> = Driver<T>;
131
+ type Driver<T = any> = FileStorageDriver<T>;
125
132
  interface FileStorageService {
126
133
  get(path: string): Promise<Buffer>;
127
134
  text(path: string, encoding?: BufferEncoding): Promise<string>;
@@ -144,8 +151,8 @@ declare namespace FileStorage {
144
151
  append(path: string, contents: string | Buffer | Readable | ReadableStream): Promise<void>;
145
152
  copy(source: string, destination: string): Promise<void>;
146
153
  move(source: string, destination: string): Promise<void>;
147
- putFileAs(dir: string, file: File, filename: string): Promise<string>;
148
- putFile(dir: string, file: File): Promise<string>;
154
+ putFileAs(dir: string, file: FileInput, filename: string): Promise<string>;
155
+ putFile(dir: string, file: FileInput): Promise<string>;
149
156
  remove(...paths: string[]): Promise<void>;
150
157
  files(directory: string): Promise<string[]>;
151
158
  allFiles(directory: string): Promise<string[]>;
package/dist/index.js CHANGED
@@ -9,7 +9,6 @@ import "stream";
9
9
  import "stream/web";
10
10
 
11
11
  // src/index.ts
12
- import "buffer";
13
12
  import { randomUUID as randomUUID2 } from "crypto";
14
13
  import mimeTypes from "mime-types";
15
14
 
@@ -73,6 +72,7 @@ import stream from "stream";
73
72
  import { lookup } from "mime-types";
74
73
  function createLocalDriver({
75
74
  pathPrefix = "storage",
75
+ urlPrefix = "/files",
76
76
  buildTemporaryUrlsUsing
77
77
  }) {
78
78
  return {
@@ -88,7 +88,7 @@ function createLocalDriver({
88
88
  }
89
89
  },
90
90
  url(path) {
91
- return nodePath.join(pathPrefix, path);
91
+ return `${urlPrefix}/${path}`;
92
92
  },
93
93
  async temporaryUrl(path, expiration, params) {
94
94
  if (buildTemporaryUrlsUsing) {
@@ -115,24 +115,32 @@ function createLocalDriver({
115
115
  return nodePath.resolve(nodePath.join(pathPrefix, path));
116
116
  },
117
117
  async put(path, contents) {
118
- await fs.writeFile(nodePath.join(pathPrefix, path), contents);
118
+ const fullPath = nodePath.join(pathPrefix, path);
119
+ await fs.mkdir(nodePath.dirname(fullPath), { recursive: true });
120
+ await fs.writeFile(fullPath, contents);
119
121
  },
120
122
  async prepend(path, contents) {
121
123
  await prepend(nodePath.join(pathPrefix, path), contents);
122
124
  },
123
125
  async append(path, contents) {
126
+ const fullPath = nodePath.join(pathPrefix, path);
127
+ await fs.mkdir(nodePath.dirname(fullPath), { recursive: true });
124
128
  if (Buffer.isBuffer(contents) || typeof contents == "string") {
125
- await fs.appendFile(nodePath.join(pathPrefix, path), contents);
129
+ await fs.appendFile(fullPath, contents);
126
130
  } else {
127
- const writeStream2 = createWriteStream(nodePath.join(pathPrefix, path), { flags: "a" });
131
+ const writeStream2 = createWriteStream(fullPath, { flags: "a" });
128
132
  await promiseStream.pipeline(contents, writeStream2);
129
133
  }
130
134
  },
131
135
  async copy(source, destination) {
132
- await fs.copyFile(nodePath.join(pathPrefix, source), nodePath.join(pathPrefix, destination));
136
+ const destPath = nodePath.join(pathPrefix, destination);
137
+ await fs.mkdir(nodePath.dirname(destPath), { recursive: true });
138
+ await fs.copyFile(nodePath.join(pathPrefix, source), destPath);
133
139
  },
134
140
  async move(source, destination) {
135
- await fs.rename(nodePath.join(pathPrefix, source), nodePath.join(pathPrefix, destination));
141
+ const destPath = nodePath.join(pathPrefix, destination);
142
+ await fs.mkdir(nodePath.dirname(destPath), { recursive: true });
143
+ await fs.rename(nodePath.join(pathPrefix, source), destPath);
136
144
  },
137
145
  async remove(...paths) {
138
146
  await Promise.all(paths.map((p) => fs.rm(nodePath.join(pathPrefix, p))));
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "default": "./dist/index.js"
8
8
  }
9
9
  },
10
- "version": "1.0.0-alpha.2",
10
+ "version": "1.0.0-alpha.20",
11
11
  "publishConfig": {
12
12
  "access": "restricted"
13
13
  },
@@ -15,14 +15,17 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@aws-sdk/client-s3": "^3.750.0",
19
- "@aws-sdk/s3-request-presigner": "^3.750.0",
20
18
  "iso-fns2": "npm:iso-fns@2.0.0-alpha.26",
21
19
  "mime-types": "^2.1.35",
22
- "@maestro-js/service-registry": "1.0.0-alpha.2",
23
- "@maestro-js/crypt": "1.0.0-alpha.2"
20
+ "@maestro-js/service-registry": "1.0.0-alpha.20"
21
+ },
22
+ "peerDependencies": {
23
+ "@aws-sdk/client-s3": "^3.750.0",
24
+ "@aws-sdk/s3-request-presigner": "^3.750.0"
24
25
  },
25
26
  "devDependencies": {
27
+ "@aws-sdk/client-s3": "^3.750.0",
28
+ "@aws-sdk/s3-request-presigner": "^3.750.0",
26
29
  "@types/node": "^22.19.11",
27
30
  "@types/mime-types": "^2.1.4"
28
31
  },