@directus/storage-driver-gcs 11.0.2 → 11.1.1

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,14 +1,19 @@
1
- import { Driver, ReadOptions } 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
+ enabled: boolean;
10
+ chunkSize?: number;
11
+ };
8
12
  };
9
- declare class DriverGCS implements Driver {
13
+ declare class DriverGCS implements TusDriver {
10
14
  private root;
11
15
  private bucket;
16
+ private readonly preferredChunkSize;
12
17
  constructor(config: DriverGCSConfig);
13
18
  private fullPath;
14
19
  private file;
@@ -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
@@ -1,16 +1,24 @@
1
1
  // src/index.ts
2
+ import { DEFAULT_CHUNK_SIZE } from "@directus/constants";
2
3
  import { normalizePath } from "@directus/utils";
3
4
  import { Storage } from "@google-cloud/storage";
4
5
  import { join } from "node:path";
5
6
  import { pipeline } from "node:stream/promises";
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 = tus?.chunkSize || DEFAULT_CHUNK_SIZE;
19
+ if (tus?.enabled && (this.preferredChunkSize < MINIMUM_CHUNK_SIZE || Math.log2(this.preferredChunkSize) % 1 !== 0)) {
20
+ throw new Error("Invalid chunkSize provided");
21
+ }
14
22
  }
15
23
  fullPath(filepath) {
16
24
  return normalizePath(join(this.root, filepath));
@@ -60,6 +68,42 @@ var DriverGCS = class {
60
68
  query = nextQuery;
61
69
  }
62
70
  }
71
+ get tusExtensions() {
72
+ return ["creation", "termination", "expiration"];
73
+ }
74
+ async createChunkedUpload(filepath, context) {
75
+ const file = this.file(this.fullPath(filepath));
76
+ const [uri] = await file.createResumableUpload();
77
+ context.metadata["uri"] = uri;
78
+ return context;
79
+ }
80
+ async writeChunk(filepath, content, offset, context) {
81
+ const file = this.file(this.fullPath(filepath));
82
+ const stream = file.createWriteStream({
83
+ chunkSize: this.preferredChunkSize,
84
+ uri: context.metadata["uri"],
85
+ offset,
86
+ isPartialUpload: true,
87
+ resumeCRC32C: context.metadata["hash"],
88
+ metadata: {
89
+ contentLength: context.size || 0
90
+ }
91
+ });
92
+ stream.on("crc32c", (hash) => {
93
+ context.metadata["hash"] = hash;
94
+ });
95
+ let bytesUploaded = offset || 0;
96
+ content.on("data", (chunk) => {
97
+ bytesUploaded += chunk.length;
98
+ });
99
+ await pipeline(content, stream);
100
+ return bytesUploaded;
101
+ }
102
+ async finishChunkedUpload(_filepath, _context) {
103
+ }
104
+ async deleteChunkedUpload(filepath, _context) {
105
+ await this.delete(filepath);
106
+ }
63
107
  };
64
108
  var src_default = DriverGCS;
65
109
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@directus/storage-driver-gcs",
3
- "version": "11.0.2",
3
+ "version": "11.1.1",
4
4
  "description": "GCS file storage abstraction for `@directus/storage`",
5
5
  "homepage": "https://directus.io",
6
6
  "repository": {
@@ -22,8 +22,9 @@
22
22
  ],
23
23
  "dependencies": {
24
24
  "@google-cloud/storage": "7.13.0",
25
- "@directus/utils": "12.0.2",
26
- "@directus/storage": "11.0.1"
25
+ "@directus/constants": "12.0.1",
26
+ "@directus/storage": "11.0.1",
27
+ "@directus/utils": "12.0.4"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@ngneat/falso": "7.2.0",