@peerbit/please-lib 0.0.3

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2022 dao.xyz
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,72 @@
1
+ import { Program } from "@peerbit/program";
2
+ import { Documents, Role } from "@peerbit/document";
3
+ import { ProgramClient } from "@peerbit/program";
4
+ declare abstract class AbstractFile {
5
+ abstract id: string;
6
+ abstract name: string;
7
+ abstract getFile(files: Files): Promise<Uint8Array | undefined>;
8
+ }
9
+ export declare class TinyFile extends AbstractFile {
10
+ id: string;
11
+ name: string;
12
+ file: Uint8Array;
13
+ constructor(properties: {
14
+ name: string;
15
+ file: Uint8Array;
16
+ });
17
+ getFile(_files: Files): Promise<Uint8Array>;
18
+ }
19
+ export declare class SmallFile extends AbstractFile {
20
+ id: string;
21
+ name: string;
22
+ cid: string;
23
+ constructor(properties: {
24
+ name: string;
25
+ cid: string;
26
+ });
27
+ static create(node: ProgramClient, name: string, file: Uint8Array): Promise<SmallFile>;
28
+ getFile(files: Files): Promise<Uint8Array | undefined>;
29
+ }
30
+ export declare class LargeFile extends AbstractFile {
31
+ id: string;
32
+ name: string;
33
+ fileIds: string[];
34
+ constructor(properties: {
35
+ id: string;
36
+ name: string;
37
+ fileIds: string[];
38
+ });
39
+ static create(name: string, file: Uint8Array, files: Files): Promise<LargeFile>;
40
+ getFile(files: Files): Promise<Uint8Array>;
41
+ }
42
+ type Args = {
43
+ role: Role;
44
+ };
45
+ export declare class Files extends Program<Args> {
46
+ id: Uint8Array;
47
+ files: Documents<AbstractFile>;
48
+ constructor(id?: Uint8Array);
49
+ create(name: string, file: Uint8Array): Promise<string>;
50
+ /**
51
+ * Get by name
52
+ * @param id
53
+ * @returns
54
+ */
55
+ getById(id: string): Promise<{
56
+ id: string;
57
+ name: string;
58
+ bytes: Uint8Array;
59
+ } | undefined>;
60
+ /**
61
+ * Get by name
62
+ * @param name
63
+ * @returns
64
+ */
65
+ getByName(name: string): Promise<{
66
+ id: string;
67
+ name: string;
68
+ bytes: Uint8Array;
69
+ } | undefined>;
70
+ open(args?: Args): Promise<void>;
71
+ }
72
+ export {};
@@ -0,0 +1,260 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var SmallFile_1, LargeFile_1;
11
+ import { field, variant, vec } from "@dao-xyz/borsh";
12
+ import { Program } from "@peerbit/program";
13
+ import { Documents, SearchRequest, StringMatch } from "@peerbit/document";
14
+ import { sha256Base64Sync, randomBytes } from "@peerbit/crypto";
15
+ import { concat } from "uint8arrays";
16
+ class AbstractFile {
17
+ }
18
+ const TINY_FILE_SIZE_LIMIT = 1e3;
19
+ export let TinyFile = class TinyFile extends AbstractFile {
20
+ id;
21
+ name;
22
+ file; // 10 mb imit
23
+ constructor(properties) {
24
+ super();
25
+ this.id = sha256Base64Sync(properties.file);
26
+ this.name = properties.name;
27
+ this.file = properties.file;
28
+ }
29
+ async getFile(_files) {
30
+ if (sha256Base64Sync(this.file) !== this.id) {
31
+ throw new Error("Hash does not match the file content");
32
+ }
33
+ return Promise.resolve(this.file);
34
+ }
35
+ };
36
+ __decorate([
37
+ field({ type: "string" }),
38
+ __metadata("design:type", String)
39
+ ], TinyFile.prototype, "id", void 0);
40
+ __decorate([
41
+ field({ type: "string" }),
42
+ __metadata("design:type", String)
43
+ ], TinyFile.prototype, "name", void 0);
44
+ __decorate([
45
+ field({ type: Uint8Array }),
46
+ __metadata("design:type", Uint8Array)
47
+ ], TinyFile.prototype, "file", void 0);
48
+ TinyFile = __decorate([
49
+ variant(0) // for versioning purposes
50
+ ,
51
+ __metadata("design:paramtypes", [Object])
52
+ ], TinyFile);
53
+ const SMALL_FILE_SIZE_LIMIT = 1e6 * 9;
54
+ export let SmallFile = SmallFile_1 = class SmallFile extends AbstractFile {
55
+ id; // cid
56
+ name;
57
+ cid; // store file separately in a block store, like IPFS
58
+ constructor(properties) {
59
+ super();
60
+ this.id = properties.cid;
61
+ this.name = properties.name;
62
+ this.cid = properties.cid;
63
+ }
64
+ static async create(node, name, file) {
65
+ if (file.length > SMALL_FILE_SIZE_LIMIT) {
66
+ throw new Error("To large file for SmallFile");
67
+ }
68
+ const cid = await node.services.blocks.put(file);
69
+ return new SmallFile_1({ name, cid });
70
+ }
71
+ async getFile(files) {
72
+ // Load the file from the block store
73
+ return await files.node.services.blocks.get(this.id);
74
+ }
75
+ };
76
+ __decorate([
77
+ field({ type: "string" }),
78
+ __metadata("design:type", String)
79
+ ], SmallFile.prototype, "id", void 0);
80
+ __decorate([
81
+ field({ type: "string" }),
82
+ __metadata("design:type", String)
83
+ ], SmallFile.prototype, "name", void 0);
84
+ __decorate([
85
+ field({ type: "string" }),
86
+ __metadata("design:type", String)
87
+ ], SmallFile.prototype, "cid", void 0);
88
+ SmallFile = SmallFile_1 = __decorate([
89
+ variant(1) // for versioning purposes
90
+ ,
91
+ __metadata("design:paramtypes", [Object])
92
+ ], SmallFile);
93
+ export let LargeFile = LargeFile_1 = class LargeFile extends AbstractFile {
94
+ id; // hash
95
+ name;
96
+ fileIds;
97
+ constructor(properties) {
98
+ super();
99
+ this.id = properties.id;
100
+ this.name = properties.name;
101
+ this.fileIds = properties.fileIds;
102
+ }
103
+ static async create(name, file, files) {
104
+ const segmetSize = SMALL_FILE_SIZE_LIMIT / 10; // 10% of the small size limit
105
+ const fileIds = [];
106
+ const id = sha256Base64Sync(file);
107
+ for (let i = 0; i < Math.ceil(file.byteLength / segmetSize); i++) {
108
+ fileIds.push(await files.create(id + "/" + name, file.subarray(i * segmetSize, Math.min((i + 1) * segmetSize, file.length))));
109
+ }
110
+ return new LargeFile_1({ id, name, fileIds });
111
+ }
112
+ async getFile(files) {
113
+ // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
114
+ const expectedIds = new Set(this.fileIds);
115
+ const chunks = new Map();
116
+ const results = await files.files.index.search(new SearchRequest({
117
+ query: [
118
+ new StringMatch({
119
+ key: "name",
120
+ value: this.id + "/" + this.name,
121
+ }),
122
+ ],
123
+ }), {
124
+ local: true,
125
+ remote: true,
126
+ });
127
+ if (results.length > 0) {
128
+ for (const r of results) {
129
+ if (chunks.has(r.id)) {
130
+ // chunk already added;
131
+ }
132
+ if (!expectedIds.has(r.id)) {
133
+ // chunk is not part of this file
134
+ }
135
+ chunks.set(r.id, r.getFile(files));
136
+ }
137
+ }
138
+ if (chunks.size !== expectedIds.size) {
139
+ throw new Error("Failed to resolve file");
140
+ }
141
+ const chunkContentResolved = await Promise.all(this.fileIds.map(async (x) => {
142
+ const chunkValue = await chunks.get(x);
143
+ if (!chunkValue) {
144
+ throw new Error("Failed to retrieve chunk with id: " + x);
145
+ }
146
+ return chunkValue;
147
+ }));
148
+ return concat(chunkContentResolved);
149
+ }
150
+ };
151
+ __decorate([
152
+ field({ type: "string" }),
153
+ __metadata("design:type", String)
154
+ ], LargeFile.prototype, "id", void 0);
155
+ __decorate([
156
+ field({ type: "string" }),
157
+ __metadata("design:type", String)
158
+ ], LargeFile.prototype, "name", void 0);
159
+ __decorate([
160
+ field({ type: vec("string") }),
161
+ __metadata("design:type", Array)
162
+ ], LargeFile.prototype, "fileIds", void 0);
163
+ LargeFile = LargeFile_1 = __decorate([
164
+ variant(2) // for versioning purposes
165
+ ,
166
+ __metadata("design:paramtypes", [Object])
167
+ ], LargeFile);
168
+ export let Files = class Files extends Program {
169
+ id;
170
+ files;
171
+ constructor(id = randomBytes(32)) {
172
+ super();
173
+ this.id = id;
174
+ this.files = new Documents({ id: this.id });
175
+ }
176
+ async create(name, file) {
177
+ let toPut;
178
+ if (file.byteLength <= TINY_FILE_SIZE_LIMIT) {
179
+ toPut = new TinyFile({ name, file });
180
+ }
181
+ else if (file.byteLength > TINY_FILE_SIZE_LIMIT &&
182
+ file.byteLength <= SMALL_FILE_SIZE_LIMIT) {
183
+ toPut = await SmallFile.create(this.node, name, file);
184
+ }
185
+ else {
186
+ toPut = await LargeFile.create(name, file, this);
187
+ }
188
+ await this.files.put(toPut);
189
+ return toPut.id;
190
+ }
191
+ /**
192
+ * Get by name
193
+ * @param id
194
+ * @returns
195
+ */
196
+ async getById(id) {
197
+ const results = await this.files.index.search(new SearchRequest({
198
+ query: [new StringMatch({ key: "id", value: id })],
199
+ }), {
200
+ local: true,
201
+ remote: {
202
+ timeout: 10 * 1000,
203
+ },
204
+ });
205
+ for (const result of results) {
206
+ const file = await result.getFile(this);
207
+ if (file) {
208
+ return { id: result.id, name: result.name, bytes: file };
209
+ }
210
+ }
211
+ }
212
+ /**
213
+ * Get by name
214
+ * @param name
215
+ * @returns
216
+ */
217
+ async getByName(name) {
218
+ const results = await this.files.index.search(new SearchRequest({
219
+ query: [new StringMatch({ key: "name", value: name })],
220
+ }), {
221
+ local: true,
222
+ remote: {
223
+ timeout: 10 * 1000,
224
+ },
225
+ });
226
+ for (const result of results) {
227
+ const file = await result.getFile(this);
228
+ if (file) {
229
+ return { id: result.id, name: result.name, bytes: file };
230
+ }
231
+ }
232
+ }
233
+ // Setup lifecycle, will be invoked on 'open'
234
+ async open(args) {
235
+ await this.files.open({
236
+ type: AbstractFile,
237
+ canAppend: async (entry) => {
238
+ await entry.verifySignatures();
239
+ return true; // no verification as of now
240
+ },
241
+ canRead: async (identity) => {
242
+ return true; // Anyone can query
243
+ },
244
+ role: args?.role,
245
+ });
246
+ }
247
+ };
248
+ __decorate([
249
+ field({ type: Uint8Array }),
250
+ __metadata("design:type", Uint8Array)
251
+ ], Files.prototype, "id", void 0);
252
+ __decorate([
253
+ field({ type: Documents }),
254
+ __metadata("design:type", Documents)
255
+ ], Files.prototype, "files", void 0);
256
+ Files = __decorate([
257
+ variant("files"),
258
+ __metadata("design:paramtypes", [Uint8Array])
259
+ ], Files);
260
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAQ,MAAM,mBAAmB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEhE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAe,YAAY;CAI1B;AAED,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAG1B,WAAM,QAAQ,GAAd,MAAM,QAAS,SAAQ,YAAY;IAEtC,EAAE,CAAS;IAGX,IAAI,CAAS;IAGb,IAAI,CAAa,CAAC,aAAa;IAE/B,YAAY,UAA8C;QACtD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,EAAE,GAAG,gBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;IAChC,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,MAAa;QACvB,IAAI,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,EAAE,EAA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"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@peerbit/please-lib",
3
+ "version": "0.0.3",
4
+ "author": "dao.xyz",
5
+ "repository": "https://github.com/@dao-xyz/peerbit-examples",
6
+ "license": "Apache-2.0",
7
+ "type": "module",
8
+ "private": false,
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "sideEffects": false,
13
+ "module": "lib/esm/index.js",
14
+ "types": "lib/esm/index.d.ts",
15
+ "exports": {
16
+ "import": "./lib/esm/index.js"
17
+ },
18
+ "files": [
19
+ "lib",
20
+ "src",
21
+ "src/nginx-template.conf",
22
+ "!src/**/__tests__",
23
+ "!lib/**/__tests__",
24
+ "LICENSE"
25
+ ],
26
+ "engines": {
27
+ "node": ">=16.0"
28
+ },
29
+ "scripts": {
30
+ "clean": "shx rm -rf lib/*",
31
+ "build": "yarn clean && tsc -p tsconfig.json",
32
+ "postbuild": "echo '{\"type\":\"module\"} ' | node ../../../node_modules/.bin/json > lib/esm/package.json",
33
+ "test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit",
34
+ "test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit",
35
+ "test:integration": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
36
+ },
37
+ "devDependencies": {
38
+ "@peerbit/test-utils": "^1.0.12",
39
+ "peerbit": "^1.1.5",
40
+ "typescript": "^5.1.3"
41
+ },
42
+ "peerDependencies": {
43
+ "peerbit": "^1.1.5"
44
+ },
45
+ "gitHead": "f8266c28458441809aeeabaf2d686995ce05e707"
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,264 @@
1
+ import { field, variant, vec } from "@dao-xyz/borsh";
2
+ import { Program } from "@peerbit/program";
3
+ import { Documents, SearchRequest, StringMatch, Role } from "@peerbit/document";
4
+ import { sha256Base64Sync, randomBytes } from "@peerbit/crypto";
5
+ import { ProgramClient } from "@peerbit/program";
6
+ import { concat } from "uint8arrays";
7
+
8
+ abstract class AbstractFile {
9
+ abstract id: string;
10
+ abstract name: string;
11
+ abstract getFile(files: Files): Promise<Uint8Array | undefined>;
12
+ }
13
+
14
+ const TINY_FILE_SIZE_LIMIT = 1e3;
15
+
16
+ @variant(0) // for versioning purposes
17
+ export class TinyFile extends AbstractFile {
18
+ @field({ type: "string" })
19
+ id: string;
20
+
21
+ @field({ type: "string" })
22
+ name: string;
23
+
24
+ @field({ type: Uint8Array })
25
+ file: Uint8Array; // 10 mb imit
26
+
27
+ constructor(properties: { name: string; file: Uint8Array }) {
28
+ super();
29
+ this.id = sha256Base64Sync(properties.file);
30
+ this.name = properties.name;
31
+ this.file = properties.file;
32
+ }
33
+
34
+ async getFile(_files: Files) {
35
+ if (sha256Base64Sync(this.file) !== this.id) {
36
+ throw new Error("Hash does not match the file content");
37
+ }
38
+ return Promise.resolve(this.file);
39
+ }
40
+ }
41
+
42
+ const SMALL_FILE_SIZE_LIMIT = 1e6 * 9;
43
+
44
+ @variant(1) // for versioning purposes
45
+ export class SmallFile extends AbstractFile {
46
+ @field({ type: "string" })
47
+ id: string; // cid
48
+
49
+ @field({ type: "string" })
50
+ name: string;
51
+
52
+ @field({ type: "string" })
53
+ cid: string; // store file separately in a block store, like IPFS
54
+
55
+ constructor(properties: { name: string; cid: string }) {
56
+ super();
57
+ this.id = properties.cid;
58
+ this.name = properties.name;
59
+ this.cid = properties.cid;
60
+ }
61
+
62
+ static async create(node: ProgramClient, name: string, file: Uint8Array) {
63
+ if (file.length > SMALL_FILE_SIZE_LIMIT) {
64
+ throw new Error("To large file for SmallFile");
65
+ }
66
+ const cid = await node.services.blocks.put(file);
67
+ return new SmallFile({ name, cid });
68
+ }
69
+
70
+ async getFile(files: Files) {
71
+ // Load the file from the block store
72
+ return await files.node.services.blocks.get(this.id);
73
+ }
74
+ }
75
+
76
+ @variant(2) // for versioning purposes
77
+ export class LargeFile extends AbstractFile {
78
+ @field({ type: "string" })
79
+ id: string; // hash
80
+
81
+ @field({ type: "string" })
82
+ name: string;
83
+
84
+ @field({ type: vec("string") })
85
+ fileIds: string[];
86
+
87
+ constructor(properties: { id: string; name: string; fileIds: string[] }) {
88
+ super();
89
+ this.id = properties.id;
90
+ this.name = properties.name;
91
+ this.fileIds = properties.fileIds;
92
+ }
93
+
94
+ static async create(name: string, file: Uint8Array, files: Files) {
95
+ const segmetSize = SMALL_FILE_SIZE_LIMIT / 10; // 10% of the small size limit
96
+ const fileIds: string[] = [];
97
+ const id = sha256Base64Sync(file);
98
+ for (let i = 0; i < Math.ceil(file.byteLength / segmetSize); i++) {
99
+ fileIds.push(
100
+ await files.create(
101
+ id + "/" + name,
102
+ file.subarray(
103
+ i * segmetSize,
104
+ Math.min((i + 1) * segmetSize, file.length)
105
+ )
106
+ )
107
+ );
108
+ }
109
+
110
+ return new LargeFile({ id, name, fileIds });
111
+ }
112
+
113
+ async getFile(files: Files) {
114
+ // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
115
+ const expectedIds = new Set(this.fileIds);
116
+ const chunks: Map<string, Promise<Uint8Array | undefined>> = new Map();
117
+
118
+ const results = await files.files.index.search(
119
+ new SearchRequest({
120
+ query: [
121
+ new StringMatch({
122
+ key: "name",
123
+ value: this.id + "/" + this.name,
124
+ }),
125
+ ],
126
+ }),
127
+ {
128
+ local: true,
129
+ remote: true,
130
+ }
131
+ );
132
+
133
+ if (results.length > 0) {
134
+ for (const r of results) {
135
+ if (chunks.has(r.id)) {
136
+ // chunk already added;
137
+ }
138
+ if (!expectedIds.has(r.id)) {
139
+ // chunk is not part of this file
140
+ }
141
+
142
+ chunks.set(r.id, r.getFile(files));
143
+ }
144
+ }
145
+
146
+ if (chunks.size !== expectedIds.size) {
147
+ throw new Error("Failed to resolve file");
148
+ }
149
+ const chunkContentResolved: Uint8Array[] = await Promise.all(
150
+ this.fileIds.map(async (x) => {
151
+ const chunkValue = await chunks.get(x);
152
+ if (!chunkValue) {
153
+ throw new Error("Failed to retrieve chunk with id: " + x);
154
+ }
155
+ return chunkValue;
156
+ })
157
+ );
158
+ return concat(chunkContentResolved);
159
+ }
160
+ }
161
+
162
+ type Args = { role: Role };
163
+
164
+ @variant("files")
165
+ export class Files extends Program<Args> {
166
+ @field({ type: Uint8Array })
167
+ id: Uint8Array;
168
+
169
+ @field({ type: Documents })
170
+ files: Documents<AbstractFile>;
171
+
172
+ constructor(id: Uint8Array = randomBytes(32)) {
173
+ super();
174
+ this.id = id;
175
+ this.files = new Documents({ id: this.id });
176
+ }
177
+
178
+ async create(name: string, file: Uint8Array) {
179
+ let toPut: AbstractFile;
180
+ if (file.byteLength <= TINY_FILE_SIZE_LIMIT) {
181
+ toPut = new TinyFile({ name, file });
182
+ } else if (
183
+ file.byteLength > TINY_FILE_SIZE_LIMIT &&
184
+ file.byteLength <= SMALL_FILE_SIZE_LIMIT
185
+ ) {
186
+ toPut = await SmallFile.create(this.node, name, file);
187
+ } else {
188
+ toPut = await LargeFile.create(name, file, this);
189
+ }
190
+ await this.files.put(toPut);
191
+ return toPut.id;
192
+ }
193
+
194
+ /**
195
+ * Get by name
196
+ * @param id
197
+ * @returns
198
+ */
199
+ async getById(
200
+ id: string
201
+ ): Promise<{ id: string; name: string; bytes: Uint8Array } | undefined> {
202
+ const results = await this.files.index.search(
203
+ new SearchRequest({
204
+ query: [new StringMatch({ key: "id", value: id })],
205
+ }),
206
+ {
207
+ local: true,
208
+ remote: {
209
+ timeout: 10 * 1000,
210
+ },
211
+ }
212
+ );
213
+
214
+ for (const result of results) {
215
+ const file = await result.getFile(this);
216
+ if (file) {
217
+ return { id: result.id, name: result.name, bytes: file };
218
+ }
219
+ }
220
+ }
221
+
222
+ /**
223
+ * Get by name
224
+ * @param name
225
+ * @returns
226
+ */
227
+ async getByName(
228
+ name: string
229
+ ): Promise<{ id: string; name: string; bytes: Uint8Array } | undefined> {
230
+ const results = await this.files.index.search(
231
+ new SearchRequest({
232
+ query: [new StringMatch({ key: "name", value: name })],
233
+ }),
234
+ {
235
+ local: true,
236
+ remote: {
237
+ timeout: 10 * 1000,
238
+ },
239
+ }
240
+ );
241
+
242
+ for (const result of results) {
243
+ const file = await result.getFile(this);
244
+ if (file) {
245
+ return { id: result.id, name: result.name, bytes: file };
246
+ }
247
+ }
248
+ }
249
+
250
+ // Setup lifecycle, will be invoked on 'open'
251
+ async open(args?: Args): Promise<void> {
252
+ await this.files.open({
253
+ type: AbstractFile,
254
+ canAppend: async (entry) => {
255
+ await entry.verifySignatures();
256
+ return true; // no verification as of now
257
+ },
258
+ canRead: async (identity) => {
259
+ return true; // Anyone can query
260
+ },
261
+ role: args?.role,
262
+ });
263
+ }
264
+ }