@casual-simulation/aux-records 2.0.22-alpha.1651045562
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/DataRecordsController.d.ts +67 -0
- package/DataRecordsController.js +87 -0
- package/DataRecordsController.js.map +1 -0
- package/DataRecordsStore.d.ts +41 -0
- package/DataRecordsStore.js +2 -0
- package/DataRecordsStore.js.map +1 -0
- package/Errors.d.ts +9 -0
- package/Errors.js +2 -0
- package/Errors.js.map +1 -0
- package/FileRecordsController.d.ts +75 -0
- package/FileRecordsController.js +114 -0
- package/FileRecordsController.js.map +1 -0
- package/FileRecordsStore.d.ts +139 -0
- package/FileRecordsStore.js +2 -0
- package/FileRecordsStore.js.map +1 -0
- package/LICENSE.txt +21 -0
- package/MemoryDataRecordsStore.d.ts +8 -0
- package/MemoryDataRecordsStore.js +55 -0
- package/MemoryDataRecordsStore.js.map +1 -0
- package/MemoryFileRecordsStore.d.ts +9 -0
- package/MemoryFileRecordsStore.js +83 -0
- package/MemoryFileRecordsStore.js.map +1 -0
- package/MemoryRecordsStore.d.ts +8 -0
- package/MemoryRecordsStore.js +37 -0
- package/MemoryRecordsStore.js.map +1 -0
- package/README.md +3 -0
- package/RecordsController.d.ts +121 -0
- package/RecordsController.js +178 -0
- package/RecordsController.js.map +1 -0
- package/RecordsStore.d.ts +45 -0
- package/RecordsStore.js +2 -0
- package/RecordsStore.js.map +1 -0
- package/Utils.d.ts +68 -0
- package/Utils.js +206 -0
- package/Utils.js.map +1 -0
- package/index.d.ts +10 -0
- package/index.js +10 -0
- package/index.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { NotLoggedInError, ServerError } from './Errors';
|
|
2
|
+
import { DataRecordsStore, GetDataStoreResult, SetDataResult } from './DataRecordsStore';
|
|
3
|
+
import { RecordsController, ValidatePublicRecordKeyFailure } from './RecordsController';
|
|
4
|
+
/**
|
|
5
|
+
* Defines a class that is able to manage data (key/value) records.
|
|
6
|
+
*/
|
|
7
|
+
export declare class DataRecordsController {
|
|
8
|
+
private _manager;
|
|
9
|
+
private _store;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a DataRecordsController.
|
|
12
|
+
* @param manager The records manager that should be used to validate record keys.
|
|
13
|
+
* @param store The store that should be used to save data.
|
|
14
|
+
*/
|
|
15
|
+
constructor(manager: RecordsController, store: DataRecordsStore);
|
|
16
|
+
/**
|
|
17
|
+
* Records the given data in the given record and address.
|
|
18
|
+
* Uses the given record key to access the record and the given subject ID to store which user the data came from.
|
|
19
|
+
* @param recordKey The key that should be used to access the record.
|
|
20
|
+
* @param address The address that the record should be stored at inside the record.
|
|
21
|
+
* @param data The data that should be saved.
|
|
22
|
+
* @param subjectId The ID of the user that the data came from.
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
recordData(recordKey: string, address: string, data: string, subjectId: string): Promise<RecordDataResult>;
|
|
26
|
+
getData(recordName: string, address: string): Promise<GetDataResult>;
|
|
27
|
+
}
|
|
28
|
+
export declare type RecordDataResult = RecordDataSuccess | RecordDataFailure;
|
|
29
|
+
export interface RecordDataSuccess {
|
|
30
|
+
success: true;
|
|
31
|
+
recordName: string;
|
|
32
|
+
address: string;
|
|
33
|
+
}
|
|
34
|
+
export interface RecordDataFailure {
|
|
35
|
+
success: false;
|
|
36
|
+
errorCode: ServerError | NotLoggedInError | ValidatePublicRecordKeyFailure['errorCode'] | SetDataResult['errorCode'];
|
|
37
|
+
errorMessage: string;
|
|
38
|
+
}
|
|
39
|
+
export declare type GetDataResult = GetDataSuccess | GetDataFailure;
|
|
40
|
+
/**
|
|
41
|
+
* Defines an interface that represents a successful "get data" result.
|
|
42
|
+
*/
|
|
43
|
+
export interface GetDataSuccess {
|
|
44
|
+
success: true;
|
|
45
|
+
/**
|
|
46
|
+
* The data that was stored.
|
|
47
|
+
*/
|
|
48
|
+
data: any;
|
|
49
|
+
/**
|
|
50
|
+
* The name of the record.
|
|
51
|
+
*/
|
|
52
|
+
recordName: string;
|
|
53
|
+
/**
|
|
54
|
+
* The ID of the user that owns the record.
|
|
55
|
+
*/
|
|
56
|
+
publisherId: string;
|
|
57
|
+
/**
|
|
58
|
+
* The ID of the user that sent the data.
|
|
59
|
+
*/
|
|
60
|
+
subjectId: string;
|
|
61
|
+
}
|
|
62
|
+
export interface GetDataFailure {
|
|
63
|
+
success: false;
|
|
64
|
+
errorCode: ServerError | GetDataStoreResult['errorCode'];
|
|
65
|
+
errorMessage: string;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=DataRecordsController.d.ts.map
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Defines a class that is able to manage data (key/value) records.
|
|
12
|
+
*/
|
|
13
|
+
export class DataRecordsController {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a DataRecordsController.
|
|
16
|
+
* @param manager The records manager that should be used to validate record keys.
|
|
17
|
+
* @param store The store that should be used to save data.
|
|
18
|
+
*/
|
|
19
|
+
constructor(manager, store) {
|
|
20
|
+
this._manager = manager;
|
|
21
|
+
this._store = store;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Records the given data in the given record and address.
|
|
25
|
+
* Uses the given record key to access the record and the given subject ID to store which user the data came from.
|
|
26
|
+
* @param recordKey The key that should be used to access the record.
|
|
27
|
+
* @param address The address that the record should be stored at inside the record.
|
|
28
|
+
* @param data The data that should be saved.
|
|
29
|
+
* @param subjectId The ID of the user that the data came from.
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
32
|
+
recordData(recordKey, address, data, subjectId) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
try {
|
|
35
|
+
const result = yield this._manager.validatePublicRecordKey(recordKey);
|
|
36
|
+
if (result.success === false) {
|
|
37
|
+
return {
|
|
38
|
+
success: false,
|
|
39
|
+
errorCode: result.errorCode,
|
|
40
|
+
errorMessage: result.errorMessage,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const recordName = result.recordName;
|
|
44
|
+
const result2 = yield this._store.setData(recordName, address, data, result.ownerId, subjectId);
|
|
45
|
+
if (result2.success === false) {
|
|
46
|
+
return {
|
|
47
|
+
success: false,
|
|
48
|
+
errorCode: result2.errorCode,
|
|
49
|
+
errorMessage: result2.errorMessage,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
success: true,
|
|
54
|
+
recordName: recordName,
|
|
55
|
+
address: address,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
return {
|
|
60
|
+
success: false,
|
|
61
|
+
errorCode: 'server_error',
|
|
62
|
+
errorMessage: err.toString(),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
getData(recordName, address) {
|
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const result = yield this._store.getData(recordName, address);
|
|
70
|
+
if (result.success === false) {
|
|
71
|
+
return {
|
|
72
|
+
success: false,
|
|
73
|
+
errorCode: result.errorCode,
|
|
74
|
+
errorMessage: result.errorMessage,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
success: true,
|
|
79
|
+
data: result.data,
|
|
80
|
+
publisherId: result.publisherId,
|
|
81
|
+
subjectId: result.subjectId,
|
|
82
|
+
recordName,
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=DataRecordsController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DataRecordsController.js","sourceRoot":"","sources":["DataRecordsController.ts"],"names":[],"mappings":";;;;;;;;;AAWA;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAI9B;;;;OAIG;IACH,YAAY,OAA0B,EAAE,KAAuB;QAC3D,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC;IAED;;;;;;;;OAQG;IACG,UAAU,CACZ,SAAiB,EACjB,OAAe,EACf,IAAY,EACZ,SAAiB;;YAEjB,IAAI;gBACA,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,uBAAuB,CACtD,SAAS,CACZ,CAAC;gBACF,IAAI,MAAM,CAAC,OAAO,KAAK,KAAK,EAAE;oBAC1B,OAAO;wBACH,OAAO,EAAE,KAAK;wBACd,SAAS,EAAE,MAAM,CAAC,SAAS;wBAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;qBACpC,CAAC;iBACL;gBAED,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;gBACrC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CACrC,UAAU,EACV,OAAO,EACP,IAAI,EACJ,MAAM,CAAC,OAAO,EACd,SAAS,CACZ,CAAC;gBAEF,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK,EAAE;oBAC3B,OAAO;wBACH,OAAO,EAAE,KAAK;wBACd,SAAS,EAAE,OAAO,CAAC,SAAS;wBAC5B,YAAY,EAAE,OAAO,CAAC,YAAY;qBACrC,CAAC;iBACL;gBAED,OAAO;oBACH,OAAO,EAAE,IAAI;oBACb,UAAU,EAAE,UAAU;oBACtB,OAAO,EAAE,OAAO;iBACnB,CAAC;aACL;YAAC,OAAO,GAAG,EAAE;gBACV,OAAO;oBACH,OAAO,EAAE,KAAK;oBACd,SAAS,EAAE,cAAc;oBACzB,YAAY,EAAE,GAAG,CAAC,QAAQ,EAAE;iBAC/B,CAAC;aACL;QACL,CAAC;KAAA;IAEK,OAAO,CAAC,UAAkB,EAAE,OAAe;;YAC7C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;YAC9D,IAAI,MAAM,CAAC,OAAO,KAAK,KAAK,EAAE;gBAC1B,OAAO;oBACH,OAAO,EAAE,KAAK;oBACd,SAAS,EAAE,MAAM,CAAC,SAAS;oBAC3B,YAAY,EAAE,MAAM,CAAC,YAAY;iBACpC,CAAC;aACL;YAED,OAAO;gBACH,OAAO,EAAE,IAAI;gBACb,IAAI,EAAE,MAAM,CAAC,IAAI;gBACjB,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,UAAU;aACb,CAAC;QACN,CAAC;KAAA;CACJ"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ServerError } from './Errors';
|
|
2
|
+
/**
|
|
3
|
+
* Defines an interface for objects that can store data records.
|
|
4
|
+
*/
|
|
5
|
+
export interface DataRecordsStore {
|
|
6
|
+
/**
|
|
7
|
+
* Sets the given data in the given record and address.
|
|
8
|
+
* @param recordName The name of the record that the data should be set in.
|
|
9
|
+
* @param address The address that the data should be set for.
|
|
10
|
+
* @param data The data that should be saved.
|
|
11
|
+
* @param publisherId The ID of the user that owns the record this data is being published to.
|
|
12
|
+
* @param subjectId The ID of the user that was logged in when the data was published.
|
|
13
|
+
*/
|
|
14
|
+
setData(recordName: string, address: string, data: any, publisherId: string, subjectId: string): Promise<SetDataResult>;
|
|
15
|
+
/**
|
|
16
|
+
* Gets the data stored in the given record and address.
|
|
17
|
+
* @param recordName The name of hte record that the data is in.
|
|
18
|
+
* @param address The address that the data is stored at.
|
|
19
|
+
*/
|
|
20
|
+
getData(recordName: string, address: string): Promise<GetDataStoreResult>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Defines an interface that represents the result of a "set data" operation.
|
|
24
|
+
*/
|
|
25
|
+
export interface SetDataResult {
|
|
26
|
+
success: boolean;
|
|
27
|
+
errorCode?: 'data_too_large' | ServerError;
|
|
28
|
+
errorMessage?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Defines an interface that represents the result of a "get data" operation.
|
|
32
|
+
*/
|
|
33
|
+
export interface GetDataStoreResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
data?: any;
|
|
36
|
+
publisherId?: string;
|
|
37
|
+
subjectId?: string;
|
|
38
|
+
errorCode?: 'data_not_found' | ServerError;
|
|
39
|
+
errorMessage?: string;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=DataRecordsStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DataRecordsStore.js","sourceRoot":"","sources":["DataRecordsStore.ts"],"names":[],"mappings":""}
|
package/Errors.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines an error that occurs when an unspecified error occurs while creating a public record key.
|
|
3
|
+
*/
|
|
4
|
+
export declare type ServerError = 'server_error';
|
|
5
|
+
/**
|
|
6
|
+
* Defines an error that occurs when the user is not logged in but they are required to be in order to perform an action.
|
|
7
|
+
*/
|
|
8
|
+
export declare type NotLoggedInError = 'not_logged_in';
|
|
9
|
+
//# sourceMappingURL=Errors.d.ts.map
|
package/Errors.js
ADDED
package/Errors.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Errors.js","sourceRoot":"","sources":["Errors.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { FileRecordsStore, AddFileFailure, MarkFileRecordAsUploadedFailure } from './FileRecordsStore';
|
|
2
|
+
import { NotLoggedInError, ServerError } from './Errors';
|
|
3
|
+
import { RecordsController, ValidatePublicRecordKeyFailure } from './RecordsController';
|
|
4
|
+
/**
|
|
5
|
+
* Defines a class that can manage file records.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FileRecordsController {
|
|
8
|
+
private _controller;
|
|
9
|
+
private _store;
|
|
10
|
+
constructor(controller: RecordsController, store: FileRecordsStore);
|
|
11
|
+
recordFile(recordKey: string, userId: string, request: RecordFileRequest): Promise<RecordFileResult>;
|
|
12
|
+
markFileAsUploaded(recordName: string, fileName: string): Promise<FileUploadedResult>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Defines an interface that is used for requests to record a file.
|
|
16
|
+
*/
|
|
17
|
+
export interface RecordFileRequest {
|
|
18
|
+
/**
|
|
19
|
+
* The hex encoded SHA256 hash of the file that will be uploaded.
|
|
20
|
+
*/
|
|
21
|
+
fileSha256Hex: string;
|
|
22
|
+
/**
|
|
23
|
+
* The number of bytes in the file.
|
|
24
|
+
*/
|
|
25
|
+
fileByteLength: number;
|
|
26
|
+
/**
|
|
27
|
+
* The MIME type of the file.
|
|
28
|
+
*/
|
|
29
|
+
fileMimeType: string;
|
|
30
|
+
/**
|
|
31
|
+
* The description of the file.
|
|
32
|
+
*/
|
|
33
|
+
fileDescription: string;
|
|
34
|
+
}
|
|
35
|
+
export declare type RecordFileResult = RecordFileSuccess | RecordFileFailure;
|
|
36
|
+
export interface RecordFileSuccess {
|
|
37
|
+
success: true;
|
|
38
|
+
/**
|
|
39
|
+
* The URL that the file should be uploaded to.
|
|
40
|
+
*/
|
|
41
|
+
uploadUrl: string;
|
|
42
|
+
/**
|
|
43
|
+
* The HTTP Method that should be used for the upload.
|
|
44
|
+
*/
|
|
45
|
+
uploadMethod: string;
|
|
46
|
+
/**
|
|
47
|
+
* The HTTP headers that should be included in the upload request.
|
|
48
|
+
*/
|
|
49
|
+
uploadHeaders: {
|
|
50
|
+
[name: string]: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The name of the file that was recorded.
|
|
54
|
+
*/
|
|
55
|
+
fileName: string;
|
|
56
|
+
}
|
|
57
|
+
export interface RecordFileFailure {
|
|
58
|
+
success: false;
|
|
59
|
+
errorCode: ServerError | NotLoggedInError | ValidatePublicRecordKeyFailure['errorCode'] | AddFileFailure['errorCode'] | 'invalid_file_data';
|
|
60
|
+
errorMessage: string;
|
|
61
|
+
/**
|
|
62
|
+
* The URL that the file is available at if it has already been uploaded.
|
|
63
|
+
*/
|
|
64
|
+
existingFileUrl?: string;
|
|
65
|
+
}
|
|
66
|
+
export declare type FileUploadedResult = FileUploadedSuccess | FileUploadedFailure;
|
|
67
|
+
export interface FileUploadedSuccess {
|
|
68
|
+
success: true;
|
|
69
|
+
}
|
|
70
|
+
export interface FileUploadedFailure {
|
|
71
|
+
success: false;
|
|
72
|
+
errorCode: ServerError | MarkFileRecordAsUploadedFailure['errorCode'];
|
|
73
|
+
errorMessage: string;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=FileRecordsController.d.ts.map
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { getExtension } from 'mime';
|
|
11
|
+
/**
|
|
12
|
+
* Defines a class that can manage file records.
|
|
13
|
+
*/
|
|
14
|
+
export class FileRecordsController {
|
|
15
|
+
constructor(controller, store) {
|
|
16
|
+
this._controller = controller;
|
|
17
|
+
this._store = store;
|
|
18
|
+
}
|
|
19
|
+
recordFile(recordKey, userId, request) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
try {
|
|
22
|
+
const keyResult = yield this._controller.validatePublicRecordKey(recordKey);
|
|
23
|
+
if (keyResult.success === false) {
|
|
24
|
+
return keyResult;
|
|
25
|
+
}
|
|
26
|
+
const publisherId = keyResult.ownerId;
|
|
27
|
+
const recordName = keyResult.recordName;
|
|
28
|
+
const subjectId = userId;
|
|
29
|
+
const extension = getExtension(request.fileMimeType);
|
|
30
|
+
const fileName = extension
|
|
31
|
+
? `${request.fileSha256Hex}.${extension}`
|
|
32
|
+
: request.fileSha256Hex;
|
|
33
|
+
const presignResult = yield this._store.presignFileUpload({
|
|
34
|
+
recordName,
|
|
35
|
+
fileName: fileName,
|
|
36
|
+
fileSha256Hex: request.fileSha256Hex,
|
|
37
|
+
fileMimeType: request.fileMimeType,
|
|
38
|
+
fileByteLength: request.fileByteLength,
|
|
39
|
+
});
|
|
40
|
+
if (presignResult.success === false) {
|
|
41
|
+
return presignResult;
|
|
42
|
+
}
|
|
43
|
+
const addFileResult = yield this._store.addFileRecord(recordName, fileName, publisherId, subjectId, request.fileByteLength, request.fileDescription);
|
|
44
|
+
if (addFileResult.success === false) {
|
|
45
|
+
if (addFileResult.errorCode === 'file_already_exists') {
|
|
46
|
+
const fileResult = yield this._store.getFileRecord(recordName, fileName);
|
|
47
|
+
if (fileResult.success === false) {
|
|
48
|
+
console.error('[FileRecordsController] Error getting file record even though it should exist:', fileResult);
|
|
49
|
+
return {
|
|
50
|
+
success: false,
|
|
51
|
+
errorCode: 'server_error',
|
|
52
|
+
errorMessage: fileResult.errorMessage,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (!fileResult.uploaded) {
|
|
56
|
+
return {
|
|
57
|
+
success: true,
|
|
58
|
+
fileName,
|
|
59
|
+
uploadUrl: presignResult.uploadUrl,
|
|
60
|
+
uploadHeaders: presignResult.uploadHeaders,
|
|
61
|
+
uploadMethod: presignResult.uploadMethod,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
return {
|
|
66
|
+
success: false,
|
|
67
|
+
errorCode: 'file_already_exists',
|
|
68
|
+
errorMessage: 'The file has already been uploaded to ' +
|
|
69
|
+
fileResult.url,
|
|
70
|
+
existingFileUrl: fileResult.url,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return addFileResult;
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
success: true,
|
|
78
|
+
fileName,
|
|
79
|
+
uploadUrl: presignResult.uploadUrl,
|
|
80
|
+
uploadHeaders: presignResult.uploadHeaders,
|
|
81
|
+
uploadMethod: presignResult.uploadMethod,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
return {
|
|
86
|
+
success: false,
|
|
87
|
+
errorCode: 'server_error',
|
|
88
|
+
errorMessage: err.toString(),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
markFileAsUploaded(recordName, fileName) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
const result = yield this._store.setFileRecordAsUploaded(recordName, fileName);
|
|
97
|
+
if (result.success === false) {
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
success: true,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
return {
|
|
106
|
+
success: false,
|
|
107
|
+
errorCode: 'server_error',
|
|
108
|
+
errorMessage: err.toString(),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=FileRecordsController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileRecordsController.js","sourceRoot":"","sources":["FileRecordsController.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { ServerError } from './Errors';
|
|
2
|
+
/**
|
|
3
|
+
* Defines an interface that provides a way to store file records.
|
|
4
|
+
*/
|
|
5
|
+
export interface FileRecordsStore {
|
|
6
|
+
/**
|
|
7
|
+
* Presigns a request to record a file.
|
|
8
|
+
* Returns the URL that should be used to upload the file and the headers that should be included in the upload request.
|
|
9
|
+
* @param request The request to create a signed request for.
|
|
10
|
+
*/
|
|
11
|
+
presignFileUpload(request: PresignFileUploadRequest): Promise<PresignFileUploadResult>;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the file record for the file with the given name.
|
|
14
|
+
* @param recordName The name of the record that the file is stored in.
|
|
15
|
+
* @param fileName The name of the file.
|
|
16
|
+
*/
|
|
17
|
+
getFileRecord(recordName: string, fileName: string): Promise<GetFileRecordResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Attempts to add a record for a file to the store.
|
|
20
|
+
* @param recordName The name of the record that the file was recorded in.
|
|
21
|
+
* @param fileName The name of the file that should be recorded.
|
|
22
|
+
* @param publisherId The ID of the publisher that published the record.
|
|
23
|
+
* @param subjectId The ID of the subject that was logged in when the record was published.
|
|
24
|
+
* @param sizeInBytes The size of the file in bytes.
|
|
25
|
+
* @param description The description of the file.
|
|
26
|
+
*/
|
|
27
|
+
addFileRecord(recordName: string, fileName: string, publisherId: string, subjectId: string, sizeInBytes: number, description: string): Promise<AddFileResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Marks the given file record as having been uploaded.
|
|
30
|
+
* @param recordName The reocrd that the file was uploaded to.
|
|
31
|
+
* @param fileName The name of the file that was uploaded.
|
|
32
|
+
*/
|
|
33
|
+
setFileRecordAsUploaded(recordName: string, fileName: string): Promise<MarkFileRecordAsUploadedResult>;
|
|
34
|
+
}
|
|
35
|
+
export declare type GetFileRecordResult = GetFileRecordSuccess | GetFileRecordFailure;
|
|
36
|
+
export interface GetFileRecordSuccess {
|
|
37
|
+
success: true;
|
|
38
|
+
/**
|
|
39
|
+
* The name of the file.
|
|
40
|
+
*/
|
|
41
|
+
fileName: string;
|
|
42
|
+
/**
|
|
43
|
+
* The description of the file.
|
|
44
|
+
*/
|
|
45
|
+
description: string;
|
|
46
|
+
/**
|
|
47
|
+
* The name of the record that the file was recorded in.
|
|
48
|
+
*/
|
|
49
|
+
recordName: string;
|
|
50
|
+
/**
|
|
51
|
+
* The URL that the file can be downloaded from.
|
|
52
|
+
*/
|
|
53
|
+
url: string;
|
|
54
|
+
/**
|
|
55
|
+
* The ID of the publisher that published the record.
|
|
56
|
+
*/
|
|
57
|
+
publisherId: string;
|
|
58
|
+
/**
|
|
59
|
+
* The ID of the subject that was logged in when the record was published.
|
|
60
|
+
*/
|
|
61
|
+
subjectId: string;
|
|
62
|
+
/**
|
|
63
|
+
* The size of the record in bytes.
|
|
64
|
+
*/
|
|
65
|
+
sizeInBytes: number;
|
|
66
|
+
/**
|
|
67
|
+
* Whether the record was uploaded to the server.
|
|
68
|
+
*/
|
|
69
|
+
uploaded: boolean;
|
|
70
|
+
}
|
|
71
|
+
export declare type MarkFileRecordAsUploadedResult = MarkFileRecordAsUploadedSuccess | MarkFileRecordAsUploadedFailure;
|
|
72
|
+
export interface MarkFileRecordAsUploadedSuccess {
|
|
73
|
+
success: true;
|
|
74
|
+
}
|
|
75
|
+
export interface MarkFileRecordAsUploadedFailure {
|
|
76
|
+
success: false;
|
|
77
|
+
errorCode: ServerError | 'file_not_found';
|
|
78
|
+
errorMessage: string;
|
|
79
|
+
}
|
|
80
|
+
export interface GetFileRecordFailure {
|
|
81
|
+
success: false;
|
|
82
|
+
errorCode: ServerError | 'file_not_found';
|
|
83
|
+
errorMessage: string;
|
|
84
|
+
}
|
|
85
|
+
export interface PresignFileUploadRequest {
|
|
86
|
+
/**
|
|
87
|
+
* The name of the record that the file will be stored in.
|
|
88
|
+
*/
|
|
89
|
+
recordName: string;
|
|
90
|
+
/**
|
|
91
|
+
* The name of the file.
|
|
92
|
+
*/
|
|
93
|
+
fileName: string;
|
|
94
|
+
/**
|
|
95
|
+
* The Hex encoded SHA-256 hash of the file that will be uploaded.
|
|
96
|
+
*/
|
|
97
|
+
fileSha256Hex: string;
|
|
98
|
+
/**
|
|
99
|
+
* The MIME type of the file that will be uploaded.
|
|
100
|
+
*/
|
|
101
|
+
fileMimeType: string;
|
|
102
|
+
/**
|
|
103
|
+
* The number of bytes in the file.
|
|
104
|
+
*/
|
|
105
|
+
fileByteLength: number;
|
|
106
|
+
}
|
|
107
|
+
export declare type PresignFileUploadResult = PresignFileUploadSuccess | PresignFileUploadFailure;
|
|
108
|
+
export interface PresignFileUploadSuccess {
|
|
109
|
+
success: true;
|
|
110
|
+
/**
|
|
111
|
+
* The URL that the upload should be sent to.
|
|
112
|
+
*/
|
|
113
|
+
uploadUrl: string;
|
|
114
|
+
/**
|
|
115
|
+
* The HTTP method that should be used for the upload.
|
|
116
|
+
*/
|
|
117
|
+
uploadMethod: string;
|
|
118
|
+
/**
|
|
119
|
+
* The HTTP headers that should be included in the upload.
|
|
120
|
+
*/
|
|
121
|
+
uploadHeaders: {
|
|
122
|
+
[name: string]: string;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export interface PresignFileUploadFailure {
|
|
126
|
+
success: false;
|
|
127
|
+
errorCode: ServerError;
|
|
128
|
+
errorMessage: string;
|
|
129
|
+
}
|
|
130
|
+
export declare type AddFileResult = AddFileSuccess | AddFileFailure;
|
|
131
|
+
export interface AddFileSuccess {
|
|
132
|
+
success: true;
|
|
133
|
+
}
|
|
134
|
+
export interface AddFileFailure {
|
|
135
|
+
success: false;
|
|
136
|
+
errorCode: ServerError | 'file_already_exists';
|
|
137
|
+
errorMessage: string;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=FileRecordsStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FileRecordsStore.js","sourceRoot":"","sources":["FileRecordsStore.ts"],"names":[],"mappings":""}
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Casual Simulation, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DataRecordsStore, GetDataStoreResult, SetDataResult } from './DataRecordsStore';
|
|
2
|
+
export declare class MemoryDataRecordsStore implements DataRecordsStore {
|
|
3
|
+
private _buckets;
|
|
4
|
+
setData(recordName: string, address: string, data: any, publisherId: string, subjectId: string): Promise<SetDataResult>;
|
|
5
|
+
getData(recordName: string, address: string): Promise<GetDataStoreResult>;
|
|
6
|
+
private _getRecord;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=MemoryDataRecordsStore.d.ts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export class MemoryDataRecordsStore {
|
|
11
|
+
constructor() {
|
|
12
|
+
this._buckets = new Map();
|
|
13
|
+
}
|
|
14
|
+
setData(recordName, address, data, publisherId, subjectId) {
|
|
15
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
let record = this._getRecord(recordName);
|
|
17
|
+
record.set(address, {
|
|
18
|
+
data: data,
|
|
19
|
+
publisherId: publisherId,
|
|
20
|
+
subjectId: subjectId,
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
success: true,
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getData(recordName, address) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
let record = this._getRecord(recordName);
|
|
30
|
+
let data = record.get(address);
|
|
31
|
+
if (!data) {
|
|
32
|
+
return {
|
|
33
|
+
success: false,
|
|
34
|
+
errorCode: 'data_not_found',
|
|
35
|
+
errorMessage: 'The data was not found.',
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
success: true,
|
|
40
|
+
data: data.data,
|
|
41
|
+
publisherId: data.publisherId,
|
|
42
|
+
subjectId: data.subjectId,
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
_getRecord(recordName) {
|
|
47
|
+
let record = this._buckets.get(recordName);
|
|
48
|
+
if (!record) {
|
|
49
|
+
record = new Map();
|
|
50
|
+
this._buckets.set(recordName, record);
|
|
51
|
+
}
|
|
52
|
+
return record;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=MemoryDataRecordsStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryDataRecordsStore.js","sourceRoot":"","sources":["MemoryDataRecordsStore.ts"],"names":[],"mappings":";;;;;;;;;AAMA,MAAM,OAAO,sBAAsB;IAAnC;QACY,aAAQ,GAAyC,IAAI,GAAG,EAAE,CAAC;IAkDvE,CAAC;IAhDS,OAAO,CACT,UAAkB,EAClB,OAAe,EACf,IAAS,EACT,WAAmB,EACnB,SAAiB;;YAEjB,IAAI,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;YACzC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE;gBAChB,IAAI,EAAE,IAAI;gBACV,WAAW,EAAE,WAAW;gBACxB,SAAS,EAAE,SAAS;aACvB,CAAC,CAAC;YACH,OAAO;gBACH,OAAO,EAAE,IAAI;aAChB,CAAC;QACN,CAAC;KAAA;IAEK,OAAO,CACT,UAAkB,EAClB,OAAe;;YAEf,IAAI,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;YACzC,IAAI,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAC/B,IAAI,CAAC,IAAI,EAAE;gBACP,OAAO;oBACH,OAAO,EAAE,KAAK;oBACd,SAAS,EAAE,gBAAgB;oBAC3B,YAAY,EAAE,yBAAyB;iBAC1C,CAAC;aACL;YAED,OAAO;gBACH,OAAO,EAAE,IAAI;gBACb,IAAI,EAAE,IAAI,CAAC,IAAI;gBACf,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,SAAS,EAAE,IAAI,CAAC,SAAS;aAC5B,CAAC;QACN,CAAC;KAAA;IAEO,UAAU,CAAC,UAAkB;QACjC,IAAI,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAC3C,IAAI,CAAC,MAAM,EAAE;YACT,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;YACnB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;SACzC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ"}
|