@directus/storage-driver-gcs 11.0.1 → 11.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/dist/index.d.ts CHANGED
@@ -1,18 +1,23 @@
1
- import { Driver, Range } from '@directus/storage';
1
+ import { TusDriver, ReadOptions, ChunkedUploadContext } from '@directus/storage';
2
2
  import { Readable } from 'node:stream';
3
3
 
4
4
  type DriverGCSConfig = {
5
5
  root?: string;
6
6
  bucket: string;
7
7
  apiEndpoint?: string;
8
+ tus?: {
9
+ chunkSize?: number;
10
+ };
8
11
  };
9
- declare class DriverGCS implements Driver {
12
+ declare class DriverGCS implements TusDriver {
10
13
  private root;
11
14
  private bucket;
15
+ private readonly preferredChunkSize;
12
16
  constructor(config: DriverGCSConfig);
17
+ private getPreferredChunkSize;
13
18
  private fullPath;
14
19
  private file;
15
- read(filepath: string, range?: Range): Promise<Readable>;
20
+ read(filepath: string, options?: ReadOptions): Promise<Readable>;
16
21
  write(filepath: string, content: Readable): Promise<void>;
17
22
  delete(filepath: string): Promise<void>;
18
23
  stat(filepath: string): Promise<{
@@ -23,6 +28,11 @@ declare class DriverGCS implements Driver {
23
28
  move(src: string, dest: string): Promise<void>;
24
29
  copy(src: string, dest: string): Promise<void>;
25
30
  list(prefix?: string): AsyncGenerator<string, void, unknown>;
31
+ get tusExtensions(): string[];
32
+ createChunkedUpload(filepath: string, context: ChunkedUploadContext): Promise<ChunkedUploadContext>;
33
+ writeChunk(filepath: string, content: Readable, offset: number, context: ChunkedUploadContext): Promise<number>;
34
+ finishChunkedUpload(_filepath: string, _context: ChunkedUploadContext): Promise<void>;
35
+ deleteChunkedUpload(filepath: string, _context: ChunkedUploadContext): Promise<void>;
26
36
  }
27
37
 
28
38
  export { DriverGCS, type DriverGCSConfig, DriverGCS as default };
package/dist/index.js CHANGED
@@ -3,14 +3,28 @@ import { normalizePath } from "@directus/utils";
3
3
  import { Storage } from "@google-cloud/storage";
4
4
  import { join } from "node:path";
5
5
  import { pipeline } from "node:stream/promises";
6
+ var DEFAULT_CHUNK_SIZE = 8388608;
7
+ var MINIMUM_CHUNK_SIZE = 262144;
6
8
  var DriverGCS = class {
7
9
  root;
8
10
  bucket;
11
+ // TUS specific members
12
+ preferredChunkSize;
9
13
  constructor(config) {
10
- const { bucket, root, ...storageOptions } = config;
14
+ const { bucket, root, tus, ...storageOptions } = config;
11
15
  this.root = root ? normalizePath(root, { removeLeading: true }) : "";
12
16
  const storage = new Storage(storageOptions);
13
17
  this.bucket = storage.bucket(bucket);
18
+ this.preferredChunkSize = this.getPreferredChunkSize(tus);
19
+ }
20
+ getPreferredChunkSize(tus) {
21
+ if (!tus?.chunkSize) {
22
+ return DEFAULT_CHUNK_SIZE;
23
+ }
24
+ if (tus.chunkSize < MINIMUM_CHUNK_SIZE || Math.log2(tus.chunkSize) % 1 !== 0) {
25
+ throw new Error("Invalid chunkSize provided");
26
+ }
27
+ return tus.chunkSize;
14
28
  }
15
29
  fullPath(filepath) {
16
30
  return normalizePath(join(this.root, filepath));
@@ -18,11 +32,12 @@ var DriverGCS = class {
18
32
  file(filepath) {
19
33
  return this.bucket.file(filepath);
20
34
  }
21
- async read(filepath, range) {
22
- const options = {};
23
- if (range?.start) options.start = range.start;
24
- if (range?.end) options.end = range.end;
25
- return this.file(this.fullPath(filepath)).createReadStream(options);
35
+ async read(filepath, options) {
36
+ const { range } = options || {};
37
+ const stream_options = {};
38
+ if (range?.start) stream_options.start = range.start;
39
+ if (range?.end) stream_options.end = range.end;
40
+ return this.file(this.fullPath(filepath)).createReadStream(stream_options);
26
41
  }
27
42
  async write(filepath, content) {
28
43
  const file = this.file(this.fullPath(filepath));
@@ -59,6 +74,42 @@ var DriverGCS = class {
59
74
  query = nextQuery;
60
75
  }
61
76
  }
77
+ get tusExtensions() {
78
+ return ["creation", "termination", "expiration"];
79
+ }
80
+ async createChunkedUpload(filepath, context) {
81
+ const file = this.file(this.fullPath(filepath));
82
+ const [uri] = await file.createResumableUpload();
83
+ context.metadata["uri"] = uri;
84
+ return context;
85
+ }
86
+ async writeChunk(filepath, content, offset, context) {
87
+ const file = this.file(this.fullPath(filepath));
88
+ const stream = file.createWriteStream({
89
+ chunkSize: this.preferredChunkSize,
90
+ uri: context.metadata["uri"],
91
+ offset,
92
+ isPartialUpload: true,
93
+ resumeCRC32C: context.metadata["hash"],
94
+ metadata: {
95
+ contentLength: context.size || 0
96
+ }
97
+ });
98
+ stream.on("crc32c", (hash) => {
99
+ context.metadata["hash"] = hash;
100
+ });
101
+ let bytesUploaded = offset || 0;
102
+ content.on("data", (chunk) => {
103
+ bytesUploaded += chunk.length;
104
+ });
105
+ await pipeline(content, stream);
106
+ return bytesUploaded;
107
+ }
108
+ async finishChunkedUpload(_filepath, _context) {
109
+ }
110
+ async deleteChunkedUpload(filepath, _context) {
111
+ await this.delete(filepath);
112
+ }
62
113
  };
63
114
  var src_default = DriverGCS;
64
115
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directus/storage-driver-gcs",
3
- "version": "11.0.1",
3
+ "version": "11.1.0",
4
4
  "description": "GCS file storage abstraction for `@directus/storage`",
5
5
  "homepage": "https://directus.io",
6
6
  "repository": {
@@ -21,15 +21,15 @@
21
21
  "dist"
22
22
  ],
23
23
  "dependencies": {
24
- "@google-cloud/storage": "7.12.0",
25
- "@directus/storage": "11.0.0",
26
- "@directus/utils": "12.0.1"
24
+ "@google-cloud/storage": "7.13.0",
25
+ "@directus/storage": "11.0.1",
26
+ "@directus/utils": "12.0.3"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@ngneat/falso": "7.2.0",
30
30
  "@vitest/coverage-v8": "2.1.2",
31
- "tsup": "8.2.4",
32
- "typescript": "5.6.2",
31
+ "tsup": "8.3.0",
32
+ "typescript": "5.6.3",
33
33
  "vitest": "2.1.2",
34
34
  "@directus/tsconfig": "2.0.0"
35
35
  },