@cedarjs/storage 6.0.0-canary.2789 → 6.0.0-canary.2793

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/storage",
3
- "version": "6.0.0-canary.2789",
3
+ "version": "6.0.0-canary.2793",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/cedarjs/cedar.git",
@@ -10,31 +10,15 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
- "require": {
14
- "types": "./dist/cjs/index.d.ts",
15
- "default": "./dist/cjs/index.js"
16
- },
17
- "import": {
13
+ "default": {
18
14
  "types": "./dist/index.d.ts",
19
15
  "default": "./dist/index.js"
20
16
  }
21
17
  },
22
- "./FileSystemStorage": {
23
- "require": "./dist/cjs/adapters/FileSystemStorage/FileSystemStorage.js",
24
- "import": "./dist/adapters/FileSystemStorage/FileSystemStorage.js"
25
- },
26
- "./MemoryStorage": {
27
- "require": "./dist/cjs/adapters/MemoryStorage/MemoryStorage.js",
28
- "import": "./dist/adapters/MemoryStorage/MemoryStorage.js"
29
- },
30
- "./BaseStorageAdapter": {
31
- "require": "./dist/cjs/adapters/BaseStorageAdapter.js",
32
- "import": "./dist/adapters/BaseStorageAdapter.js"
33
- },
34
- "./UrlSigner": {
35
- "require": "./dist/cjs/UrlSigner.js",
36
- "import": "./dist/UrlSigner.js"
37
- }
18
+ "./FileSystemStorage": "./dist/adapters/FileSystemStorage/FileSystemStorage.js",
19
+ "./MemoryStorage": "./dist/adapters/MemoryStorage/MemoryStorage.js",
20
+ "./BaseStorageAdapter": "./dist/adapters/BaseStorageAdapter.js",
21
+ "./UrlSigner": "./dist/UrlSigner.js"
38
22
  },
39
23
  "files": [
40
24
  "dist",
@@ -44,7 +28,6 @@
44
28
  "build": "node ./build.mts",
45
29
  "build:pack": "yarn pack -o cedarjs-storage.tgz",
46
30
  "build:types": "tsc --build --verbose ./tsconfig.build.json",
47
- "build:types-cjs": "tsc --build --verbose tsconfig.types-cjs.json",
48
31
  "check:attw": "yarn cedar-fwtools-attw",
49
32
  "check:package": "concurrently npm:check:attw yarn publint",
50
33
  "setup:test": "npx prisma db push --accept-data-loss --config ./src/__tests__/prisma.config.ts && npx prisma generate --config ./src/__tests__/prisma.config.ts",
@@ -53,13 +36,13 @@
53
36
  "test:watch": "vitest watch"
54
37
  },
55
38
  "dependencies": {
56
- "@cedarjs/project-config": "6.0.0-canary.2789",
39
+ "@cedarjs/project-config": "6.0.0-canary.2793",
57
40
  "mime-types": "2.1.35",
58
41
  "ulid": "3.0.2"
59
42
  },
60
43
  "devDependencies": {
61
44
  "@arethetypeswrong/cli": "0.18.5",
62
- "@cedarjs/framework-tools": "6.0.0-canary.2789",
45
+ "@cedarjs/framework-tools": "6.0.0-canary.2793",
63
46
  "@prisma/adapter-better-sqlite3": "7.8.0",
64
47
  "@prisma/client": "7.8.0",
65
48
  "@types/better-sqlite3": "7.6.13",
@@ -1,46 +0,0 @@
1
- export type SignedUrlSettings = {
2
- endpoint: string;
3
- secret: string;
4
- };
5
- export type SignatureValidationArgs = {
6
- path: string;
7
- s: string;
8
- expiry?: number | string;
9
- };
10
- export declare class UrlSigner {
11
- private secret;
12
- private endpoint;
13
- constructor({ secret, endpoint }: SignedUrlSettings);
14
- generateSignature({ filePath, expiresInMs, }: {
15
- filePath: string;
16
- expiresInMs?: number;
17
- }): {
18
- expiry: number;
19
- signature: string;
20
- } | {
21
- signature: string;
22
- expiry: undefined;
23
- };
24
- /**
25
- * The signature and expires have to be extracted from the URL
26
- */
27
- validateSignature({ s: signature, path: filePath, // In the URL we call it path
28
- expiry, }: SignatureValidationArgs): string;
29
- validateSignedUrl(fullPathWithQueryParametersOrUrl: string): string;
30
- generateSignedUrl(filePath: string, expiresIn?: number): string;
31
- }
32
- export declare const getSignedDetailsFromUrl: (url: string) => {
33
- expires: number | undefined;
34
- file: string | null;
35
- signature: string | null;
36
- };
37
- export declare const EXPIRES_IN: {
38
- seconds: (s: number) => number;
39
- minutes: (m: number) => number;
40
- hours: (h: number) => number;
41
- days: (d: number) => number;
42
- weeks: (w: number) => number;
43
- months: (m: number) => number;
44
- years: (y: number) => number;
45
- };
46
- //# sourceMappingURL=UrlSigner.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"UrlSigner.d.ts","sourceRoot":"","sources":["../../src/UrlSigner.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,MAAM,MAAM,uBAAuB,GAAG;IACpC,IAAI,EAAE,MAAM,CAAA;IACZ,CAAC,EAAE,MAAM,CAAA;IACT,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACzB,CAAA;AACD,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,QAAQ,CAAQ;gBAEZ,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,iBAAiB;IASnD,iBAAiB,CAAC,EAChB,QAAQ,EACR,WAAW,GACZ,EAAE;QACD,QAAQ,EAAE,MAAM,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;KACrB;;;;;;;IA2BD;;OAEG;IACH,iBAAiB,CAAC,EAChB,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,QAAQ,EAAE,6BAA6B;IAC7C,MAAM,GACP,EAAE,uBAAuB;IAiC1B,iBAAiB,CAAC,gCAAgC,EAAE,MAAM;IAuB1D,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAiBvD;AAED,eAAO,MAAM,uBAAuB,GAAI,KAAK,MAAM;;;;CAQlD,CAAA;AAED,eAAO,MAAM,UAAU;iBACR,MAAM;iBACN,MAAM;eACR,MAAM;cACP,MAAM;eACL,MAAM;gBACL,MAAM;eACP,MAAM;CAClB,CAAA"}
@@ -1,142 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var UrlSigner_exports = {};
30
- __export(UrlSigner_exports, {
31
- EXPIRES_IN: () => EXPIRES_IN,
32
- UrlSigner: () => UrlSigner,
33
- getSignedDetailsFromUrl: () => getSignedDetailsFromUrl
34
- });
35
- module.exports = __toCommonJS(UrlSigner_exports);
36
- var import_node_crypto = __toESM(require("node:crypto"), 1);
37
- var import_project_config = require("@cedarjs/project-config");
38
- class UrlSigner {
39
- secret;
40
- endpoint;
41
- constructor({ secret, endpoint }) {
42
- this.secret = secret;
43
- this.endpoint = endpoint;
44
- this.endpoint = endpoint.startsWith("http") ? endpoint : `${(0, import_project_config.getConfig)().web.apiUrl}${endpoint}`;
45
- }
46
- generateSignature({
47
- filePath,
48
- expiresInMs
49
- }) {
50
- if (!this.secret) {
51
- throw new Error("Please configure the secret");
52
- }
53
- if (expiresInMs) {
54
- const expiry = Date.now() + expiresInMs;
55
- const signature = import_node_crypto.default.createHmac("sha256", this.secret).update(`${filePath}:${expiry}`).digest("hex");
56
- return { expiry, signature };
57
- } else {
58
- const signature = import_node_crypto.default.createHmac("sha256", this.secret).update(filePath).digest("hex");
59
- return {
60
- signature,
61
- expiry: void 0
62
- };
63
- }
64
- }
65
- /**
66
- * The signature and expires have to be extracted from the URL
67
- */
68
- validateSignature({
69
- s: signature,
70
- path: filePath,
71
- // In the URL we call it path
72
- expiry
73
- }) {
74
- if (!this.secret) {
75
- throw new Error("Please configure the secret");
76
- }
77
- if (expiry) {
78
- if (Date.now() > +expiry) {
79
- throw new Error("Signature has expired");
80
- }
81
- }
82
- const decodedFilePath = decodeURIComponent(filePath);
83
- const validSignature = expiry ? import_node_crypto.default.createHmac("sha256", this.secret).update(`${decodedFilePath}:${expiry}`).digest("hex") : import_node_crypto.default.createHmac("sha256", this.secret).update(`${decodedFilePath}`).digest("hex");
84
- if (validSignature !== signature) {
85
- throw new Error("Invalid signature");
86
- }
87
- return decodedFilePath;
88
- }
89
- validateSignedUrl(fullPathWithQueryParametersOrUrl) {
90
- const url = new URL(
91
- fullPathWithQueryParametersOrUrl,
92
- // We don't care about the host, but just need to create a URL object
93
- // to parse search params
94
- fullPathWithQueryParametersOrUrl.startsWith("http") ? void 0 : "http://localhost"
95
- );
96
- const path = url.searchParams.get("path");
97
- this.validateSignature({
98
- // Note the signature is called 's' in the URL
99
- s: url.searchParams.get("s"),
100
- expiry: url.searchParams.get("expiry"),
101
- path
102
- });
103
- return decodeURIComponent(path);
104
- }
105
- generateSignedUrl(filePath, expiresIn) {
106
- const { signature, expiry } = this.generateSignature({
107
- filePath,
108
- expiresInMs: expiresIn
109
- });
110
- const params = new URLSearchParams();
111
- params.set("s", signature);
112
- if (expiry) {
113
- params.set("expiry", expiry.toString());
114
- }
115
- params.set("path", filePath);
116
- return `${this.endpoint}?${params.toString()}`;
117
- }
118
- }
119
- const getSignedDetailsFromUrl = (url) => {
120
- const urlObj = new URL(url);
121
- const expires = urlObj.searchParams.get("expires");
122
- return {
123
- expires: expires ? parseInt(expires) : void 0,
124
- file: urlObj.searchParams.get("file"),
125
- signature: urlObj.searchParams.get("s")
126
- };
127
- };
128
- const EXPIRES_IN = {
129
- seconds: (s) => s * 1e3,
130
- minutes: (m) => m * 60 * 1e3,
131
- hours: (h) => h * 60 * 60 * 1e3,
132
- days: (d) => d * 24 * 60 * 60 * 1e3,
133
- weeks: (w) => w * 7 * 24 * 60 * 60 * 1e3,
134
- months: (m) => m * 30 * 24 * 60 * 60 * 1e3,
135
- years: (y) => y * 365 * 24 * 60 * 60 * 1e3
136
- };
137
- // Annotate the CommonJS export names for ESM import in node:
138
- 0 && (module.exports = {
139
- EXPIRES_IN,
140
- UrlSigner,
141
- getSignedDetailsFromUrl
142
- });
@@ -1,31 +0,0 @@
1
- /**
2
- * The storage adapter will just save the file and return
3
- * {
4
- * fileId: string,
5
- * location: string, // depending on storage it could be a path
6
- * }
7
- */
8
- import mime from 'mime-types';
9
- export type AdapterResult = {
10
- location: string;
11
- };
12
- export type SaveOptionsOverride = {
13
- fileName?: string;
14
- path?: string;
15
- };
16
- export type AdapterOptions = {
17
- baseDir: string;
18
- };
19
- export declare abstract class BaseStorageAdapter {
20
- adapterOpts: AdapterOptions;
21
- constructor(adapterOpts: AdapterOptions);
22
- getAdapterOptions(): AdapterOptions;
23
- generateFileNameWithExtension(saveOpts: SaveOptionsOverride | undefined, file: File): string;
24
- abstract save(file: File, saveOpts?: SaveOptionsOverride): Promise<AdapterResult>;
25
- abstract remove(fileLocation: AdapterResult['location']): Promise<void>;
26
- abstract read(fileLocation: AdapterResult['location']): Promise<{
27
- contents: Buffer | string;
28
- type: ReturnType<typeof mime.lookup>;
29
- }>;
30
- }
31
- //# sourceMappingURL=BaseStorageAdapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BaseStorageAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/BaseStorageAdapter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,IAAI,MAAM,YAAY,CAAA;AAG7B,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,8BAAsB,kBAAkB;IACtC,WAAW,EAAE,cAAc,CAAA;gBACf,WAAW,EAAE,cAAc;IAIvC,iBAAiB;IAIjB,6BAA6B,CAC3B,QAAQ,EAAE,mBAAmB,GAAG,SAAS,EACzC,IAAI,EAAE,IAAI;IASZ,QAAQ,CAAC,IAAI,CACX,IAAI,EAAE,IAAI,EACV,QAAQ,CAAC,EAAE,mBAAmB,GAC7B,OAAO,CAAC,aAAa,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IACvE,QAAQ,CAAC,IAAI,CAAC,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,OAAO,CAAC;QAC9D,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;QACzB,IAAI,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;KACrC,CAAC;CACH"}
@@ -1,53 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var BaseStorageAdapter_exports = {};
30
- __export(BaseStorageAdapter_exports, {
31
- BaseStorageAdapter: () => BaseStorageAdapter
32
- });
33
- module.exports = __toCommonJS(BaseStorageAdapter_exports);
34
- var import_mime_types = __toESM(require("mime-types"), 1);
35
- var import_ulid = require("ulid");
36
- class BaseStorageAdapter {
37
- adapterOpts;
38
- constructor(adapterOpts) {
39
- this.adapterOpts = adapterOpts;
40
- }
41
- getAdapterOptions() {
42
- return this.adapterOpts;
43
- }
44
- generateFileNameWithExtension(saveOpts, file) {
45
- const fileName = saveOpts?.fileName || (0, import_ulid.ulid)();
46
- const extension = import_mime_types.default.extension(file.type) ? `.${import_mime_types.default.extension(file.type)}` : "";
47
- return `${fileName}${extension}`;
48
- }
49
- }
50
- // Annotate the CommonJS export names for ESM import in node:
51
- 0 && (module.exports = {
52
- BaseStorageAdapter
53
- });
@@ -1,16 +0,0 @@
1
- import type { SaveOptionsOverride } from '../BaseStorageAdapter.js';
2
- import { BaseStorageAdapter } from '../BaseStorageAdapter.js';
3
- export declare class FileSystemStorage extends BaseStorageAdapter implements BaseStorageAdapter {
4
- constructor(opts: {
5
- baseDir: string;
6
- });
7
- save(file: File, saveOverride?: SaveOptionsOverride): Promise<{
8
- location: string;
9
- }>;
10
- read(filePath: string): Promise<{
11
- contents: NonSharedBuffer;
12
- type: string | false;
13
- }>;
14
- remove(filePath: string): Promise<void>;
15
- }
16
- //# sourceMappingURL=FileSystemStorage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"FileSystemStorage.d.ts","sourceRoot":"","sources":["../../../../src/adapters/FileSystemStorage/FileSystemStorage.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAE7D,qBAAa,iBACX,SAAQ,kBACR,YAAW,kBAAkB;gBAEjB,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE;IAQ/B,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,YAAY,CAAC,EAAE,mBAAmB;;;IAanD,IAAI,CAAC,QAAQ,EAAE,MAAM;;;;IAOrB,MAAM,CAAC,QAAQ,EAAE,MAAM;CAG9B"}
@@ -1,72 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var FileSystemStorage_exports = {};
30
- __export(FileSystemStorage_exports, {
31
- FileSystemStorage: () => FileSystemStorage
32
- });
33
- module.exports = __toCommonJS(FileSystemStorage_exports);
34
- var import_node_fs = require("node:fs");
35
- var import_promises = __toESM(require("node:fs/promises"), 1);
36
- var import_node_path = __toESM(require("node:path"), 1);
37
- var import_mime_types = __toESM(require("mime-types"), 1);
38
- var import_project_config = require("@cedarjs/project-config");
39
- var import_BaseStorageAdapter = require("../BaseStorageAdapter.js");
40
- class FileSystemStorage extends import_BaseStorageAdapter.BaseStorageAdapter {
41
- constructor(opts) {
42
- super(opts);
43
- if (!(0, import_node_fs.existsSync)(opts.baseDir)) {
44
- const posixBaseDir = (0, import_project_config.ensurePosixPath)(opts.baseDir);
45
- console.log("Creating baseDir >", posixBaseDir);
46
- (0, import_node_fs.mkdirSync)(posixBaseDir, { recursive: true });
47
- }
48
- }
49
- async save(file, saveOverride) {
50
- const fileName = this.generateFileNameWithExtension(saveOverride, file);
51
- const location = import_node_path.default.join(
52
- (0, import_project_config.ensurePosixPath)(saveOverride?.path || this.adapterOpts.baseDir),
53
- fileName
54
- );
55
- const nodeBuffer = await file.arrayBuffer();
56
- await import_promises.default.writeFile(location, Buffer.from(nodeBuffer));
57
- return { location };
58
- }
59
- async read(filePath) {
60
- return {
61
- contents: await import_promises.default.readFile(filePath),
62
- type: import_mime_types.default.lookup(filePath)
63
- };
64
- }
65
- async remove(filePath) {
66
- await import_promises.default.unlink(filePath);
67
- }
68
- }
69
- // Annotate the CommonJS export names for ESM import in node:
70
- 0 && (module.exports = {
71
- FileSystemStorage
72
- });
@@ -1,15 +0,0 @@
1
- import { BaseStorageAdapter } from '../BaseStorageAdapter.js';
2
- import type { SaveOptionsOverride } from '../BaseStorageAdapter.js';
3
- export declare class MemoryStorage extends BaseStorageAdapter implements BaseStorageAdapter {
4
- store: Record<string, any>;
5
- save(file: File, saveOpts?: SaveOptionsOverride): Promise<{
6
- location: string;
7
- }>;
8
- remove(filePath: string): Promise<void>;
9
- read(filePath: string): Promise<{
10
- contents: any;
11
- type: string | false;
12
- }>;
13
- clear(): Promise<void>;
14
- }
15
- //# sourceMappingURL=MemoryStorage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MemoryStorage.d.ts","sourceRoot":"","sources":["../../../../src/adapters/MemoryStorage/MemoryStorage.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAC7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAEnE,qBAAa,aACX,SAAQ,kBACR,YAAW,kBAAkB;IAE7B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAK;IAEzB,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,EAAE,mBAAmB;;;IAgB/C,MAAM,CAAC,QAAQ,EAAE,MAAM;IAIvB,IAAI,CAAC,QAAQ,EAAE,MAAM;;;;IAOrB,KAAK;CAGZ"}
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var MemoryStorage_exports = {};
30
- __export(MemoryStorage_exports, {
31
- MemoryStorage: () => MemoryStorage
32
- });
33
- module.exports = __toCommonJS(MemoryStorage_exports);
34
- var import_node_path = __toESM(require("node:path"), 1);
35
- var import_mime_types = __toESM(require("mime-types"), 1);
36
- var import_BaseStorageAdapter = require("../BaseStorageAdapter.js");
37
- class MemoryStorage extends import_BaseStorageAdapter.BaseStorageAdapter {
38
- store = {};
39
- async save(file, saveOpts) {
40
- const fileName = this.generateFileNameWithExtension(saveOpts, file);
41
- const location = import_node_path.default.join(
42
- saveOpts?.path || this.adapterOpts.baseDir,
43
- fileName
44
- );
45
- const nodeBuffer = await file.arrayBuffer();
46
- this.store[location] = Buffer.from(nodeBuffer);
47
- return {
48
- location
49
- };
50
- }
51
- async remove(filePath) {
52
- delete this.store[filePath];
53
- }
54
- async read(filePath) {
55
- return {
56
- contents: this.store[filePath],
57
- type: import_mime_types.default.lookup(filePath)
58
- };
59
- }
60
- async clear() {
61
- this.store = {};
62
- }
63
- }
64
- // Annotate the CommonJS export names for ESM import in node:
65
- 0 && (module.exports = {
66
- MemoryStorage
67
- });
@@ -1,15 +0,0 @@
1
- import type { SaveOptionsOverride, BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
- import type { ModelNamesFor, UploadsConfig } from './prismaExtension.js';
3
- type MakeFilesString<T> = {
4
- [K in keyof T]: T[K] extends File ? string : T[K];
5
- };
6
- export declare const createFileListSaver: (storage: BaseStorageAdapter) => (files?: File[], pathOverrideOnly?: {
7
- path?: string;
8
- }) => Promise<string[]>;
9
- export declare const createUploadSavers: <TClient, MNames extends ModelNamesFor<TClient> = ModelNamesFor<TClient>>(uploadConfig: UploadsConfig<TClient, MNames>, storage: BaseStorageAdapter) => { [K in `for${Capitalize<string & MNames>}`]: <T extends Record<string, any>>(data: T, overrideSaveOptions?: SaveOptionsOverride) => Promise<MakeFilesString<T>>; } & {
10
- inList: (files?: File[], pathOverrideOnly?: {
11
- path?: string;
12
- }) => Promise<string[]>;
13
- };
14
- export {};
15
- //# sourceMappingURL=createSavers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createSavers.d.ts","sourceRoot":"","sources":["../../src/createSavers.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,mBAAmB,EACnB,kBAAkB,EACnB,MAAM,kCAAkC,CAAA;AACzC,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAGxE,KAAK,eAAe,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;CAClD,CAAA;AAED,eAAO,MAAM,mBAAmB,GAAI,SAAS,kBAAkB,MAC/C,QAAO,IAAI,EAAO,EAAE,mBAAmB;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,sBAUvE,CAAA;AAMD,eAAO,MAAM,kBAAkB,GAC7B,OAAO,EACP,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,EAE9D,cAAc,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,EAC5C,SAAS,kBAAkB,QAMxB,CAAC,2CAAwB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAC/C,CAAC,wBACe,mBAAmB,KACtC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;qBA9Bb,IAAI,EAAE,qBAA0B;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;CA8EvE,CAAA"}
@@ -1,75 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var createSavers_exports = {};
20
- __export(createSavers_exports, {
21
- createFileListSaver: () => createFileListSaver,
22
- createUploadSavers: () => createUploadSavers
23
- });
24
- module.exports = __toCommonJS(createSavers_exports);
25
- var import_ulid = require("ulid");
26
- const createFileListSaver = (storage) => {
27
- return async (files = [], pathOverrideOnly) => {
28
- const locations = await Promise.all(
29
- files.map(async (file) => {
30
- const { location } = await storage.save(file, pathOverrideOnly);
31
- return location;
32
- })
33
- );
34
- return locations;
35
- };
36
- };
37
- const createUploadSavers = (uploadConfig, storage) => {
38
- const savers = {};
39
- Object.keys(uploadConfig).forEach((model) => {
40
- const modelKey = model;
41
- const currentModelConfig = uploadConfig[modelKey];
42
- if (!currentModelConfig) {
43
- return;
44
- }
45
- const currentModelUploadFields = Array.isArray(currentModelConfig.fields) ? currentModelConfig.fields : [currentModelConfig.fields];
46
- const capitalCaseModel = `${model.charAt(0).toUpperCase() + model.slice(1)}`;
47
- const saverKey = `for${capitalCaseModel}`;
48
- savers[saverKey] = async (data, overrideSaveOptions) => {
49
- const updatedFields = {};
50
- for (const field of currentModelUploadFields) {
51
- if (data[field]) {
52
- const file = data[field];
53
- const saveOptions = overrideSaveOptions || {
54
- fileName: `${model}-${field}-${(0, import_ulid.ulid)()}`
55
- };
56
- const { location } = await storage.save(file, saveOptions);
57
- updatedFields[field] = location;
58
- }
59
- }
60
- return {
61
- ...data,
62
- ...updatedFields
63
- };
64
- };
65
- });
66
- return {
67
- ...savers,
68
- inList: createFileListSaver(storage)
69
- };
70
- };
71
- // Annotate the CommonJS export names for ESM import in node:
72
- 0 && (module.exports = {
73
- createFileListSaver,
74
- createUploadSavers
75
- });
@@ -1,3 +0,0 @@
1
- import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
- export declare function fileToDataUri(filePath: string, storage: BaseStorageAdapter): Promise<string>;
3
- //# sourceMappingURL=fileToDataUri.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fileToDataUri.d.ts","sourceRoot":"","sources":["../../src/fileToDataUri.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,wBAAsB,aAAa,CACjC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,kBAAkB,mBAO5B"}
@@ -1,32 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var fileToDataUri_exports = {};
20
- __export(fileToDataUri_exports, {
21
- fileToDataUri: () => fileToDataUri
22
- });
23
- module.exports = __toCommonJS(fileToDataUri_exports);
24
- async function fileToDataUri(filePath, storage) {
25
- const { contents, type: mimeType } = await storage.read(filePath);
26
- const base64Data = Buffer.from(contents).toString("base64");
27
- return `data:${mimeType};base64,${base64Data}`;
28
- }
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- fileToDataUri
32
- });
@@ -1,40 +0,0 @@
1
- import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
2
- import type { ModelNamesFor, UploadConfigForModel, UploadsConfig } from './prismaExtension.js';
3
- import type { UrlSigner } from './UrlSigner.js';
4
- type SetupStorageOptions<TClient, MNames extends ModelNamesFor<TClient> = ModelNamesFor<TClient>> = {
5
- uploadsConfig: UploadsConfig<TClient, MNames>;
6
- storageAdapter: BaseStorageAdapter;
7
- urlSigner?: UrlSigner;
8
- };
9
- export declare const setupStorage: <TClient, MNames extends ModelNamesFor<TClient> = ModelNamesFor<TClient>>({ uploadsConfig, storageAdapter, urlSigner, }: SetupStorageOptions<TClient, MNames>) => {
10
- storagePrismaExtension: (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/client").InternalArgs<{ [K in MNames]: {
11
- withDataUri: {
12
- needs: Record<string, boolean>;
13
- compute: (modelData: Record<string, unknown>) => <T>(this: T) => Promise<T>;
14
- };
15
- withSignedUrl: {
16
- needs: Record<string, boolean>;
17
- compute: (modelData: Record<string, unknown>) => <T>(this: T, signArgs?: {
18
- expiresIn?: number;
19
- }) => T;
20
- };
21
- }; }, {}, {}, {}> & import("@prisma/client/runtime/client").DefaultArgs>;
22
- saveFiles: { [K in `for${Capitalize<string & MNames>}`]: <T extends Record<string, any>>(data: T, overrideSaveOptions?: import("./adapters/BaseStorageAdapter.js").SaveOptionsOverride) => Promise<{ [K_2 in keyof T]: T[K_2] extends File ? string : T[K_2]; }>; } & {
23
- inList: (files?: File[], pathOverrideOnly?: {
24
- path?: string;
25
- }) => Promise<string[]>;
26
- };
27
- };
28
- /**
29
- * This utility function ensures that you receive accurate type suggestions for your savers.
30
- * If you use the type UploadsConfig directly, you may receive suggestions for saveFiles.forY where Y hasn't been configured.
31
- * By using this utility function, you will only receive suggestions for the models that you have configured.
32
- *
33
- * @param uploadsConfig The uploads configuration object.
34
- * @returns The same uploads configuration object, but with filtered types
35
- */
36
- export declare function createUploadsConfig<TClient, T extends Partial<{
37
- [K in ModelNamesFor<TClient>]?: UploadConfigForModel<TClient, K>;
38
- }>>(uploadsConfig: T): T;
39
- export type { ModelNamesFor, UploadsConfig } from './prismaExtension.js';
40
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,OAAO,KAAK,EACV,aAAa,EACb,oBAAoB,EACpB,aAAa,EACd,MAAM,sBAAsB,CAAA;AAE7B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,KAAK,mBAAmB,CACtB,OAAO,EACP,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,IAC5D;IACF,aAAa,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAC7C,cAAc,EAAE,kBAAkB,CAAA;IAClC,SAAS,CAAC,EAAE,SAAS,CAAA;CACtB,CAAA;AAED,eAAO,MAAM,YAAY,GACvB,OAAO,EACP,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,EAC9D,+CAIC,mBAAmB,CAAC,OAAO,EAAE,MAAM,CAAC;;;;;;;;;;;;;;;gBAZjC,CAAC;;;CAyBN,CAAA;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EACP,CAAC,SAAS,OAAO,CAAC;KACf,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,oBAAoB,CAAC,OAAO,EAAE,CAAC,CAAC;CACjE,CAAC,EACF,aAAa,EAAE,CAAC,GAAG,CAAC,CAErB;AAED,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA"}
package/dist/cjs/index.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var index_exports = {};
20
- __export(index_exports, {
21
- createUploadsConfig: () => createUploadsConfig,
22
- setupStorage: () => setupStorage
23
- });
24
- module.exports = __toCommonJS(index_exports);
25
- var import_createSavers = require("./createSavers.js");
26
- var import_prismaExtension = require("./prismaExtension.js");
27
- const setupStorage = ({
28
- uploadsConfig,
29
- storageAdapter,
30
- urlSigner
31
- }) => {
32
- const prismaExtension = (0, import_prismaExtension.createUploadsExtension)(
33
- uploadsConfig,
34
- storageAdapter,
35
- urlSigner
36
- );
37
- const saveFiles = (0, import_createSavers.createUploadSavers)(uploadsConfig, storageAdapter);
38
- return {
39
- storagePrismaExtension: prismaExtension,
40
- saveFiles
41
- };
42
- };
43
- function createUploadsConfig(uploadsConfig) {
44
- return uploadsConfig;
45
- }
46
- // Annotate the CommonJS export names for ESM import in node:
47
- 0 && (module.exports = {
48
- createUploadsConfig,
49
- setupStorage
50
- });
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,27 +0,0 @@
1
- import { Prisma as PrismaExtension } from '@prisma/client/extension';
2
- import type { BaseStorageAdapter } from './adapters/BaseStorageAdapter.js';
3
- import type { UrlSigner } from './UrlSigner.js';
4
- type FilterOutDollarPrefixed<T> = T extends `$${string}` ? never : T extends symbol ? never : T;
5
- export type ModelNamesFor<TClient> = FilterOutDollarPrefixed<keyof TClient>;
6
- type PrismaModelFields<TClient, MName extends ModelNamesFor<TClient>> = keyof PrismaExtension.Result<TClient[MName], any, 'findFirstOrThrow'>;
7
- export type UploadConfigForModel<TClient, TPrismaModelName extends ModelNamesFor<TClient>> = {
8
- fields: PrismaModelFields<TClient, TPrismaModelName> | PrismaModelFields<TClient, TPrismaModelName>[];
9
- };
10
- export type UploadsConfig<TClient, MNames extends ModelNamesFor<TClient> = ModelNamesFor<TClient>> = {
11
- [K in MNames]?: UploadConfigForModel<TClient, K>;
12
- };
13
- type WithSignedUrlArgs = {
14
- expiresIn?: number;
15
- };
16
- export declare const createUploadsExtension: <TClient, MNames extends ModelNamesFor<TClient> = ModelNamesFor<TClient>>(config: UploadsConfig<TClient, MNames>, storageAdapter: BaseStorageAdapter, urlSigner?: UrlSigner) => (client: any) => import("@prisma/client/extension").PrismaClientExtends<import("@prisma/client/runtime/client").InternalArgs<{ [K in MNames]: {
17
- withDataUri: {
18
- needs: Record<string, boolean>;
19
- compute: (modelData: Record<string, unknown>) => <T>(this: T) => Promise<T>;
20
- };
21
- withSignedUrl: {
22
- needs: Record<string, boolean>;
23
- compute: (modelData: Record<string, unknown>) => <T>(this: T, signArgs?: WithSignedUrlArgs) => T;
24
- };
25
- }; }, {}, {}, {}> & import("@prisma/client/runtime/client").DefaultArgs>;
26
- export {};
27
- //# sourceMappingURL=prismaExtension.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"prismaExtension.d.ts","sourceRoot":"","sources":["../../src/prismaExtension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAEpE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAA;AAE1E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,KAAK,uBAAuB,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,MAAM,EAAE,GACpD,KAAK,GACL,CAAC,SAAS,MAAM,GACd,KAAK,GACL,CAAC,CAAA;AAGP,MAAM,MAAM,aAAa,CAAC,OAAO,IAAI,uBAAuB,CAAC,MAAM,OAAO,CAAC,CAAA;AAE3E,KAAK,iBAAiB,CACpB,OAAO,EACP,KAAK,SAAS,aAAa,CAAC,OAAO,CAAC,IAClC,MAAM,eAAe,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,kBAAkB,CAAC,CAAA;AAEzE,MAAM,MAAM,oBAAoB,CAC9B,OAAO,EACP,gBAAgB,SAAS,aAAa,CAAC,OAAO,CAAC,IAC7C;IACF,MAAM,EACF,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,CAAC,GAC5C,iBAAiB,CAAC,OAAO,EAAE,gBAAgB,CAAC,EAAE,CAAA;CACnD,CAAA;AAED,MAAM,MAAM,aAAa,CACvB,OAAO,EACP,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,IAC5D;KACD,CAAC,IAAI,MAAM,CAAC,CAAC,EAAE,oBAAoB,CAAC,OAAO,EAAE,CAAC,CAAC;CACjD,CAAA;AAED,KAAK,iBAAiB,GAAG;IACvB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAED,eAAO,MAAM,sBAAsB,GACjC,OAAO,EACP,MAAM,SAAS,aAAa,CAAC,OAAO,CAAC,GAAG,aAAa,CAAC,OAAO,CAAC,EAE9D,QAAQ,aAAa,CAAC,OAAO,EAAE,MAAM,CAAC,EACtC,gBAAgB,kBAAkB,EAClC,YAAY,SAAS,qIAGlB,CAAC;iBACa;QACX,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC9B,OAAO,EAAE,CACP,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;KAChC;mBACc;QACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QAC9B,OAAO,EAAE,CACP,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,iBAAiB,KAAK,CAAC,CAAA;KACrD;wEA+RN,CAAA"}
@@ -1,234 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var prismaExtension_exports = {};
20
- __export(prismaExtension_exports, {
21
- createUploadsExtension: () => createUploadsExtension
22
- });
23
- module.exports = __toCommonJS(prismaExtension_exports);
24
- var import_extension = require("@prisma/client/extension");
25
- var import_fileToDataUri = require("./fileToDataUri.js");
26
- const createUploadsExtension = (config, storageAdapter, urlSigner) => {
27
- return import_extension.Prisma.defineExtension((client) => {
28
- const queryExtends = {};
29
- const resultExtends = {};
30
- for (const modelName in config) {
31
- const modelConfig = config[modelName];
32
- if (!modelConfig) {
33
- continue;
34
- }
35
- const uploadFields = Array.isArray(modelConfig.fields) ? modelConfig.fields : [modelConfig.fields];
36
- queryExtends[modelName] = {
37
- async create({ query, args }) {
38
- try {
39
- const result = await query(args);
40
- return result;
41
- } catch (e) {
42
- await removeUploadedFiles(
43
- uploadFields,
44
- args.data
45
- );
46
- throw e;
47
- }
48
- },
49
- async createMany({ query, args }) {
50
- try {
51
- const result = await query(args);
52
- return result;
53
- } catch (e) {
54
- const createDatas = args.data;
55
- for (const createData of createDatas) {
56
- await removeUploadedFiles(uploadFields, createData);
57
- }
58
- throw e;
59
- }
60
- },
61
- async update({ query, model, args }) {
62
- const uploadFieldsToUpdate = uploadFields.filter((field) => {
63
- return args.data && typeof args.data === "object" && field in args.data;
64
- });
65
- if (uploadFieldsToUpdate.length === 0) {
66
- return query(args);
67
- } else {
68
- const originalRecord = await client[model].findFirstOrThrow({
69
- where: args.where,
70
- select: Object.fromEntries(
71
- uploadFieldsToUpdate.map((f) => [f, true])
72
- )
73
- });
74
- try {
75
- const result = await query(args);
76
- await removeUploadedFiles(uploadFieldsToUpdate, originalRecord);
77
- return result;
78
- } catch (e) {
79
- await removeUploadedFiles(
80
- uploadFieldsToUpdate,
81
- args.data
82
- );
83
- throw e;
84
- }
85
- }
86
- },
87
- async updateMany({ query, model, args }) {
88
- const uploadFieldsToUpdate = uploadFields.filter((field) => {
89
- return args.data && typeof args.data === "object" && field in args.data;
90
- });
91
- if (uploadFieldsToUpdate.length === 0) {
92
- return query(args);
93
- } else {
94
- const originalRecords = await client[model].findMany({
95
- where: args.where,
96
- select: Object.fromEntries(
97
- uploadFieldsToUpdate.map((f) => [f, true])
98
- )
99
- });
100
- try {
101
- const result = await query(args);
102
- for await (const originalRecord of originalRecords) {
103
- await removeUploadedFiles(uploadFieldsToUpdate, originalRecord);
104
- }
105
- return result;
106
- } catch (e) {
107
- await removeUploadedFiles(
108
- uploadFieldsToUpdate,
109
- args.data
110
- );
111
- throw e;
112
- }
113
- }
114
- },
115
- async upsert({ query, model, args }) {
116
- let isUpdate;
117
- const uploadFieldsToUpdate = uploadFields.filter(
118
- (field) => typeof args.update === "object" && args.update !== null && field in args.update
119
- );
120
- try {
121
- let existingRecord;
122
- if (args.update) {
123
- existingRecord = await client[model].findUnique({
124
- where: args.where,
125
- ...uploadFieldsToUpdate.length > 0 ? {
126
- select: Object.fromEntries(
127
- uploadFieldsToUpdate.map((f) => [f, true])
128
- )
129
- } : {}
130
- });
131
- isUpdate = !!existingRecord;
132
- }
133
- const result = await query(args);
134
- if (isUpdate && existingRecord) {
135
- await removeUploadedFiles(uploadFieldsToUpdate, existingRecord);
136
- }
137
- return result;
138
- } catch (e) {
139
- await removeUploadedFiles(
140
- // Only delete files we're updating on update
141
- isUpdate ? uploadFieldsToUpdate : uploadFields,
142
- isUpdate ? args.update : args.create
143
- );
144
- throw e;
145
- }
146
- },
147
- async delete({ query, args }) {
148
- const deleteResult = await query(args);
149
- await removeUploadedFiles(
150
- uploadFields,
151
- // We don't know the exact type here
152
- deleteResult
153
- );
154
- return deleteResult;
155
- }
156
- };
157
- const needs = Object.fromEntries(
158
- uploadFields.map((field) => [field, true])
159
- );
160
- resultExtends[modelName] = {
161
- withDataUri: {
162
- needs,
163
- compute(modelData) {
164
- return async () => {
165
- const base64UploadFields = {};
166
- for (const field of uploadFields) {
167
- base64UploadFields[field] = await (0, import_fileToDataUri.fileToDataUri)(
168
- modelData[field],
169
- storageAdapter
170
- );
171
- }
172
- return {
173
- // modelData is of type unknown at this point
174
- ...modelData,
175
- ...base64UploadFields
176
- };
177
- };
178
- }
179
- },
180
- withSignedUrl: {
181
- needs,
182
- compute(modelData) {
183
- return ({ expiresIn } = {}) => {
184
- if (!urlSigner) {
185
- throw new Error(
186
- "Please supply signed url settings in setupUpload()"
187
- );
188
- }
189
- const signedUrlFields = {};
190
- for (const field of uploadFields) {
191
- if (!modelData[field]) {
192
- continue;
193
- }
194
- signedUrlFields[field] = urlSigner.generateSignedUrl(
195
- modelData[field],
196
- expiresIn
197
- );
198
- }
199
- return {
200
- // modelData is of type unknown at this point
201
- ...modelData,
202
- ...signedUrlFields
203
- };
204
- };
205
- }
206
- }
207
- };
208
- }
209
- return client.$extends({
210
- name: "redwood-upload-prisma-plugin",
211
- query: queryExtends,
212
- result: resultExtends
213
- });
214
- });
215
- async function removeUploadedFiles(fieldsToDelete, data) {
216
- if (!data) {
217
- console.warn("Empty data object passed to removeUploadedFiles");
218
- return;
219
- }
220
- for (const field of fieldsToDelete) {
221
- const uploadLocation = data?.[field];
222
- if (uploadLocation) {
223
- try {
224
- await storageAdapter.remove(uploadLocation);
225
- } catch {
226
- }
227
- }
228
- }
229
- }
230
- };
231
- // Annotate the CommonJS export names for ESM import in node:
232
- 0 && (module.exports = {
233
- createUploadsExtension
234
- });