@graffiti-garden/implementation-local 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.js +1 -12
- package/dist/browser/index.js.map +4 -4
- package/dist/cjs/media.js +7 -19
- package/dist/cjs/media.js.map +3 -3
- package/dist/cjs/objects.js +21 -9
- package/dist/cjs/objects.js.map +2 -2
- package/dist/cjs/utilities.js +10 -0
- package/dist/cjs/utilities.js.map +2 -2
- package/dist/esm/media.js +9 -10
- package/dist/esm/media.js.map +2 -2
- package/dist/esm/objects.js +26 -10
- package/dist/esm/objects.js.map +2 -2
- package/dist/esm/utilities.js +14 -1
- package/dist/esm/utilities.js.map +2 -2
- package/dist/media.d.ts.map +1 -1
- package/dist/objects.d.ts.map +1 -1
- package/dist/utilities.d.ts +3 -0
- package/dist/utilities.d.ts.map +1 -1
- package/package.json +5 -6
- package/src/media.ts +8 -9
- package/src/objects.ts +26 -9
- package/src/utilities.ts +27 -1
package/dist/cjs/media.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
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
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var media_exports = {};
|
|
30
20
|
__export(media_exports, {
|
|
@@ -33,7 +23,6 @@ __export(media_exports, {
|
|
|
33
23
|
module.exports = __toCommonJS(media_exports);
|
|
34
24
|
var import_api = require("@graffiti-garden/api");
|
|
35
25
|
var import_utilities = require("./utilities");
|
|
36
|
-
var import_negotiator = __toESM(require("negotiator"));
|
|
37
26
|
const MEDIA_OBJECT_SCHEMA = {
|
|
38
27
|
properties: {
|
|
39
28
|
value: {
|
|
@@ -71,7 +60,7 @@ class GraffitiLocalMedia {
|
|
|
71
60
|
return (0, import_utilities.encodeMediaUrl)(actor, id);
|
|
72
61
|
};
|
|
73
62
|
getMedia = async (...args) => {
|
|
74
|
-
const [mediaUrl,
|
|
63
|
+
const [mediaUrl, accept, session] = args;
|
|
75
64
|
const { actor, id } = (0, import_utilities.decodeMediaUrl)(mediaUrl);
|
|
76
65
|
const objectUrl = (0, import_utilities.encodeObjectUrl)(actor, id);
|
|
77
66
|
const object = await this.db.get(
|
|
@@ -80,15 +69,14 @@ class GraffitiLocalMedia {
|
|
|
80
69
|
session
|
|
81
70
|
);
|
|
82
71
|
const { dataBase64, type, size } = object.value;
|
|
83
|
-
if (
|
|
72
|
+
if (accept?.maxBytes && size > accept.maxBytes) {
|
|
84
73
|
throw new import_api.GraffitiErrorTooLarge("File size exceeds limit");
|
|
85
74
|
}
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
throw new import_api.GraffitiErrorNotAcceptable(`Unsupported media type, ${type}`);
|
|
75
|
+
if (accept?.types) {
|
|
76
|
+
if (!(0, import_api.isMediaAcceptable)(type, accept.types)) {
|
|
77
|
+
throw new import_api.GraffitiErrorNotAcceptable(
|
|
78
|
+
`Unacceptable media type, ${type}`
|
|
79
|
+
);
|
|
92
80
|
}
|
|
93
81
|
}
|
|
94
82
|
const data = await (0, import_utilities.base64ToBlob)(dataBase64);
|
package/dist/cjs/media.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/media.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n GraffitiErrorNotAcceptable,\n GraffitiErrorTooLarge,\n type Graffiti,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport {\n decodeObjectUrl,\n encodeObjectUrl,\n decodeMediaUrl,\n encodeMediaUrl,\n blobToBase64,\n base64ToBlob,\n} from \"./utilities\";\
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["import {\n GraffitiErrorNotAcceptable,\n GraffitiErrorTooLarge,\n isMediaAcceptable,\n type Graffiti,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport {\n decodeObjectUrl,\n encodeObjectUrl,\n decodeMediaUrl,\n encodeMediaUrl,\n blobToBase64,\n base64ToBlob,\n} from \"./utilities\";\n\nconst MEDIA_OBJECT_SCHEMA = {\n properties: {\n value: {\n properties: {\n dataBase64: { type: \"string\" },\n type: { type: \"string\" },\n size: { type: \"number\" },\n },\n required: [\"dataBase64\", \"type\", \"size\"],\n },\n },\n} as const satisfies JSONSchema;\n\nexport class GraffitiLocalMedia {\n protected db: Pick<Graffiti, \"post\" | \"get\" | \"delete\">;\n\n constructor(db: Pick<Graffiti, \"post\" | \"get\" | \"delete\">) {\n this.db = db;\n }\n\n postMedia: Graffiti[\"postMedia\"] = async (...args) => {\n const [media, session] = args;\n\n const dataBase64 = await blobToBase64(media.data);\n const type = media.data.type;\n\n const { url } = await this.db.post<typeof MEDIA_OBJECT_SCHEMA>(\n {\n value: {\n dataBase64,\n type,\n size: media.data.size,\n },\n channels: [],\n allowed: media.allowed,\n },\n session,\n );\n\n const { actor, id } = decodeObjectUrl(url);\n return encodeMediaUrl(actor, id);\n };\n\n getMedia: Graffiti[\"getMedia\"] = async (...args) => {\n const [mediaUrl, accept, session] = args;\n const { actor, id } = decodeMediaUrl(mediaUrl);\n const objectUrl = encodeObjectUrl(actor, id);\n\n const object = await this.db.get<typeof MEDIA_OBJECT_SCHEMA>(\n objectUrl,\n MEDIA_OBJECT_SCHEMA,\n session,\n );\n\n const { dataBase64, type, size } = object.value;\n\n if (accept?.maxBytes && size > accept.maxBytes) {\n throw new GraffitiErrorTooLarge(\"File size exceeds limit\");\n }\n\n // Make sure it adheres to requirements.accept\n if (accept?.types) {\n if (!isMediaAcceptable(type, accept.types)) {\n throw new GraffitiErrorNotAcceptable(\n `Unacceptable media type, ${type}`,\n );\n }\n }\n\n const data = await base64ToBlob(dataBase64);\n if (data.size !== size || data.type !== type) {\n throw new Error(\"Invalid data\");\n }\n\n return {\n data,\n actor: object.actor,\n allowed: object.allowed,\n };\n };\n\n deleteMedia: Graffiti[\"deleteMedia\"] = async (...args) => {\n const [mediaUrl, session] = args;\n const { actor, id } = decodeMediaUrl(mediaUrl);\n const objectUrl = encodeObjectUrl(actor, id);\n\n await this.db.delete(objectUrl, session);\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAMO;AACP,uBAOO;AAEP,MAAM,sBAAsB;AAAA,EAC1B,YAAY;AAAA,IACV,OAAO;AAAA,MACL,YAAY;AAAA,QACV,YAAY,EAAE,MAAM,SAAS;AAAA,QAC7B,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,MAAM,EAAE,MAAM,SAAS;AAAA,MACzB;AAAA,MACA,UAAU,CAAC,cAAc,QAAQ,MAAM;AAAA,IACzC;AAAA,EACF;AACF;AAEO,MAAM,m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AA,MACA,OAAO,OAAO;AAAA,MACd,SAAS,OAAO;AAAA,IAClB;AAAA,EACF;AAAA,EAEA,cAAuC,UAAU,SAAS;AACxD,UAAM,CAAC,UAAU,OAAO,IAAI;AAC5B,UAAM,EAAE,OAAO,GAAG,QAAI,iCAAe,QAAQ;AAC7C,UAAM,gBAAY,kCAAgB,OAAO,EAAE;AAE3C,UAAM,KAAK,GAAG,OAAO,WAAW,OAAO;AAAA,EACzC;AACF;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/cjs/objects.js
CHANGED
|
@@ -119,12 +119,12 @@ class GraffitiLocalObjects {
|
|
|
119
119
|
"The object you are trying to get either does not exist or you are not allowed to see it"
|
|
120
120
|
);
|
|
121
121
|
}
|
|
122
|
-
(0, import_api.maskGraffitiObject)(object, [], session);
|
|
123
|
-
const validate = (0,
|
|
124
|
-
if (!validate(
|
|
122
|
+
const masked = (0, import_api.maskGraffitiObject)(object, [], session?.actor);
|
|
123
|
+
const validate = (0, import_utilities.compileGraffitiObjectSchema)(await this.ajv, schema);
|
|
124
|
+
if (!validate(masked)) {
|
|
125
125
|
throw new import_api.GraffitiErrorSchemaMismatch();
|
|
126
126
|
}
|
|
127
|
-
return
|
|
127
|
+
return masked;
|
|
128
128
|
};
|
|
129
129
|
delete = async (...args) => {
|
|
130
130
|
const [urlObject, session] = args;
|
|
@@ -151,7 +151,15 @@ class GraffitiLocalObjects {
|
|
|
151
151
|
} catch {
|
|
152
152
|
throw new import_api.GraffitiErrorNotFound("Object not found.");
|
|
153
153
|
}
|
|
154
|
-
|
|
154
|
+
const { value, channels, allowed } = doc;
|
|
155
|
+
const object = {
|
|
156
|
+
value,
|
|
157
|
+
channels,
|
|
158
|
+
allowed,
|
|
159
|
+
url,
|
|
160
|
+
actor
|
|
161
|
+
};
|
|
162
|
+
return object;
|
|
155
163
|
};
|
|
156
164
|
post = async (...args) => {
|
|
157
165
|
const [objectPartial, session] = args;
|
|
@@ -187,7 +195,7 @@ class GraffitiLocalObjects {
|
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
const [discoverChannels, schema, session] = args;
|
|
190
|
-
const validate = (0,
|
|
198
|
+
const validate = (0, import_utilities.compileGraffitiObjectSchema)(await this.ajv, schema);
|
|
191
199
|
const startKeySuffix = continueParams ? continueParams.ifModifiedSince.toString().padStart(15, "0") : "";
|
|
192
200
|
const endKeySuffix = "\uFFFF";
|
|
193
201
|
const processedUrls = /* @__PURE__ */ new Set();
|
|
@@ -218,12 +226,16 @@ class GraffitiLocalObjects {
|
|
|
218
226
|
actor
|
|
219
227
|
};
|
|
220
228
|
if (!(0, import_api.isActorAllowedGraffitiObject)(object, session)) continue;
|
|
221
|
-
(0, import_api.maskGraffitiObject)(
|
|
222
|
-
|
|
229
|
+
const masked = (0, import_api.maskGraffitiObject)(
|
|
230
|
+
object,
|
|
231
|
+
discoverChannels,
|
|
232
|
+
session?.actor
|
|
233
|
+
);
|
|
234
|
+
if (!validate(masked)) continue;
|
|
223
235
|
yield tombstone ? {
|
|
224
236
|
tombstone: true,
|
|
225
237
|
object: { url }
|
|
226
|
-
} : { object };
|
|
238
|
+
} : { object: masked };
|
|
227
239
|
}
|
|
228
240
|
}
|
|
229
241
|
return {
|
package/dist/cjs/objects.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/objects.ts"],
|
|
4
|
-
"sourcesContent": ["import type {\n Graffiti,\n GraffitiObjectBase,\n JSONSchema,\n GraffitiSession,\n GraffitiObjectStreamContinue,\n GraffitiObjectStreamContinueEntry,\n} from \"@graffiti-garden/api\";\nimport {\n GraffitiErrorNotFound,\n GraffitiErrorSchemaMismatch,\n GraffitiErrorForbidden,\n unpackObjectUrl,\n maskGraffitiObject,\n isActorAllowedGraffitiObject,\n compileGraffitiObjectSchema,\n} from \"@graffiti-garden/api\";\nimport { randomBase64, decodeObjectUrl, encodeObjectUrl } from \"./utilities.js\";\nimport type Ajv from \"ajv\";\n\n/**\n * Constructor options for the GraffitiPoubchDB class.\n */\nexport interface GraffitiLocalOptions {\n /**\n * Options to pass to the PouchDB constructor.\n * Defaults to `{ name: \"graffitiDb\" }`.\n *\n * See the [PouchDB documentation](https://pouchdb.com/api.html#create_database)\n * for available options.\n */\n pouchDBOptions?: PouchDB.Configuration.DatabaseConfiguration;\n /**\n * Wait at least this long (in milliseconds) before continuing a stream.\n * A basic form of rate limiting. Defaults to 2 seconds.\n */\n continueBuffer?: number;\n}\n\ntype GraffitiObjectData = {\n tombstone: boolean;\n value: {};\n channels: string[];\n allowed?: string[] | null;\n lastModified: number;\n};\n\ntype ContinueDiscoverParams = {\n lastDiscovered: number;\n ifModifiedSince: number;\n};\n\n/**\n * An implementation of only the database operations of the\n * GraffitiAPI without synchronization or session management.\n */\nexport class GraffitiLocalObjects {\n protected db_: Promise<PouchDB.Database<GraffitiObjectData>> | undefined;\n protected ajv_: Promise<Ajv> | undefined;\n protected readonly options: GraffitiLocalOptions;\n\n get db() {\n if (!this.db_) {\n this.db_ = (async () => {\n const { default: PouchDB } = await import(\"pouchdb\");\n const pouchDbOptions = {\n name: \"graffitiDb\",\n ...this.options.pouchDBOptions,\n };\n const db = new PouchDB<GraffitiObjectData>(\n pouchDbOptions.name,\n pouchDbOptions,\n );\n await db\n //@ts-ignore\n .put({\n _id: \"_design/indexes\",\n views: {\n objectsPerChannelAndLastModified: {\n map: function (object: GraffitiObjectData) {\n const paddedLastModified = object.lastModified\n .toString()\n .padStart(15, \"0\");\n object.channels.forEach(function (channel) {\n const id =\n encodeURIComponent(channel) + \"/\" + paddedLastModified;\n //@ts-ignore\n emit(id);\n });\n }.toString(),\n },\n },\n })\n //@ts-ignore\n .catch((error) => {\n if (\n error &&\n typeof error === \"object\" &&\n \"name\" in error &&\n error.name === \"conflict\"\n ) {\n // Design document already exists\n return;\n } else {\n throw error;\n }\n });\n return db;\n })();\n }\n return this.db_;\n }\n\n protected get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n protected async getOperationClock() {\n return Number((await (await this.db).info()).update_seq);\n }\n\n constructor(options?: GraffitiLocalOptions) {\n this.options = options ?? {};\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const [urlObject, schema, session] = args;\n const url = unpackObjectUrl(urlObject);\n\n let doc: GraffitiObjectData;\n try {\n doc = await (await this.db).get(url);\n } catch (error) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n const { actor } = decodeObjectUrl(url);\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n // Mask out the allowed list and channels\n // if the user is not the owner\n maskGraffitiObject(object, [], session);\n\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n if (!validate(object)) {\n throw new GraffitiErrorSchemaMismatch();\n }\n return object;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const [urlObject, session] = args;\n\n const url = unpackObjectUrl(urlObject);\n const { actor } = decodeObjectUrl(url);\n if (actor !== session.actor) {\n throw new GraffitiErrorForbidden(\n \"You cannot delete an object that you did not create.\",\n );\n }\n\n let doc: GraffitiObjectData & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta;\n try {\n doc = await (await this.db).get(url);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Set the tombstone and update lastModified\n doc.tombstone = true;\n doc.lastModified = await this.getOperationClock();\n try {\n await (await this.db).put(doc);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n return;\n };\n\n post: Graffiti[\"post\"] = async (...args) => {\n const [objectPartial, session] = args;\n\n const actor = session.actor;\n const id = randomBase64();\n const url = encodeObjectUrl(actor, id);\n\n const { value, channels, allowed } = objectPartial;\n const object: GraffitiObjectData = {\n value,\n channels,\n allowed,\n lastModified: await this.getOperationClock(),\n tombstone: false,\n };\n\n await (\n await this.db\n ).put({\n _id: url,\n ...object,\n });\n\n return {\n ...objectPartial,\n actor,\n url,\n };\n };\n\n protected async *discoverMeta<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams?: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): AsyncGenerator<\n GraffitiObjectStreamContinueEntry<Schema>,\n ContinueDiscoverParams\n > {\n // If we are continuing a discover, make sure to wait at\n // least 2 seconds since the last poll to start a new one.\n if (continueParams) {\n const continueBuffer = this.options.continueBuffer ?? 2000;\n const timeElapsedSinceLastDiscover =\n Date.now() - continueParams.lastDiscovered;\n if (timeElapsedSinceLastDiscover < continueBuffer) {\n // Continue was called too soon,\n // wait a bit before continuing\n await new Promise((resolve) =>\n setTimeout(resolve, continueBuffer - timeElapsedSinceLastDiscover),\n );\n }\n }\n\n const [discoverChannels, schema, session] = args;\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const startKeySuffix = continueParams\n ? continueParams.ifModifiedSince.toString().padStart(15, \"0\")\n : \"\";\n const endKeySuffix = \"\\uffff\";\n\n const processedUrls = new Set<string>();\n\n const startTime = await this.getOperationClock();\n\n for (const channel of discoverChannels) {\n const keyPrefix = encodeURIComponent(channel) + \"/\";\n const startkey = keyPrefix + startKeySuffix;\n const endkey = keyPrefix + endKeySuffix;\n\n const result = await (\n await this.db\n ).query<GraffitiObjectData>(\"indexes/objectsPerChannelAndLastModified\", {\n startkey,\n endkey,\n include_docs: true,\n });\n\n for (const row of result.rows) {\n const doc = row.doc;\n if (!doc) continue;\n\n const url = doc._id;\n\n if (processedUrls.has(url)) continue;\n processedUrls.add(url);\n\n // If this is not a continuation, skip tombstones\n if (!continueParams && doc.tombstone) continue;\n\n const { tombstone, value, channels, allowed } = doc;\n const { actor } = decodeObjectUrl(url);\n\n const object: GraffitiObjectBase = {\n url,\n value,\n allowed,\n channels,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) continue;\n\n maskGraffitiObject(object, discoverChannels, session);\n\n if (!validate(object)) continue;\n\n yield tombstone\n ? {\n tombstone: true,\n object: { url },\n }\n : { object };\n }\n }\n\n return {\n lastDiscovered: Date.now(),\n ifModifiedSince: startTime,\n };\n }\n\n protected discoverCursor(\n args: Parameters<typeof Graffiti.prototype.discover<{}>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): string {\n const [channels, schema, session] = args;\n return (\n \"discover:\" +\n JSON.stringify({\n channels,\n schema,\n continueParams,\n actor: session?.actor,\n })\n );\n }\n\n protected async *discoverContinue<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n session?: GraffitiSession | null,\n ): GraffitiObjectStreamContinue<Schema> {\n if (session?.actor !== args[2]?.actor) {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n }\n const iterator = this.discoverMeta<Schema>(args, continueParams);\n\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this.discoverContinue<Schema>(args, result.value, session),\n cursor: this.discoverCursor(args, result.value),\n };\n }\n yield result.value;\n }\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const [channels, schema, session] = args;\n const iterator = this.discoverMeta<(typeof args)[1]>([\n channels,\n schema,\n session,\n ]);\n\n const this_ = this;\n return (async function* () {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this_.discoverContinue<(typeof args)[1]>(\n args,\n result.value,\n session,\n ),\n cursor: this_.discoverCursor(args, result.value),\n };\n }\n // Make sure to filter out tombstones\n if (result.value.tombstone) continue;\n yield result.value;\n }\n })();\n };\n\n continueDiscover: Graffiti[\"continueDiscover\"] = (...args) => {\n const [cursor, session] = args;\n if (cursor.startsWith(\"discover:\")) {\n // TODO: use AJV here\n const { channels, schema, actor, continueParams } = JSON.parse(\n cursor.slice(\"discover:\".length),\n );\n if (actor && actor !== session?.actor) {\n return (async function* () {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n })();\n }\n return this.discoverContinue<{}>(\n [channels, schema, session],\n continueParams,\n );\n } else {\n return (async function* () {\n throw new GraffitiErrorNotFound(\"Cursor not found\");\n })();\n }\n };\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,
|
|
4
|
+
"sourcesContent": ["import type {\n Graffiti,\n GraffitiObjectBase,\n JSONSchema,\n GraffitiSession,\n GraffitiObjectStreamContinue,\n GraffitiObjectStreamContinueEntry,\n} from \"@graffiti-garden/api\";\nimport {\n GraffitiErrorNotFound,\n GraffitiErrorSchemaMismatch,\n GraffitiErrorForbidden,\n unpackObjectUrl,\n maskGraffitiObject,\n isActorAllowedGraffitiObject,\n} from \"@graffiti-garden/api\";\nimport {\n randomBase64,\n decodeObjectUrl,\n encodeObjectUrl,\n compileGraffitiObjectSchema,\n} from \"./utilities.js\";\nimport type Ajv from \"ajv\";\n\n/**\n * Constructor options for the GraffitiPoubchDB class.\n */\nexport interface GraffitiLocalOptions {\n /**\n * Options to pass to the PouchDB constructor.\n * Defaults to `{ name: \"graffitiDb\" }`.\n *\n * See the [PouchDB documentation](https://pouchdb.com/api.html#create_database)\n * for available options.\n */\n pouchDBOptions?: PouchDB.Configuration.DatabaseConfiguration;\n /**\n * Wait at least this long (in milliseconds) before continuing a stream.\n * A basic form of rate limiting. Defaults to 2 seconds.\n */\n continueBuffer?: number;\n}\n\ntype GraffitiObjectData = {\n tombstone: boolean;\n value: {};\n channels: string[];\n allowed?: string[] | null;\n lastModified: number;\n};\n\ntype ContinueDiscoverParams = {\n lastDiscovered: number;\n ifModifiedSince: number;\n};\n\n/**\n * An implementation of only the database operations of the\n * GraffitiAPI without synchronization or session management.\n */\nexport class GraffitiLocalObjects {\n protected db_: Promise<PouchDB.Database<GraffitiObjectData>> | undefined;\n protected ajv_: Promise<Ajv> | undefined;\n protected readonly options: GraffitiLocalOptions;\n\n get db() {\n if (!this.db_) {\n this.db_ = (async () => {\n const { default: PouchDB } = await import(\"pouchdb\");\n const pouchDbOptions = {\n name: \"graffitiDb\",\n ...this.options.pouchDBOptions,\n };\n const db = new PouchDB<GraffitiObjectData>(\n pouchDbOptions.name,\n pouchDbOptions,\n );\n await db\n //@ts-ignore\n .put({\n _id: \"_design/indexes\",\n views: {\n objectsPerChannelAndLastModified: {\n map: function (object: GraffitiObjectData) {\n const paddedLastModified = object.lastModified\n .toString()\n .padStart(15, \"0\");\n object.channels.forEach(function (channel) {\n const id =\n encodeURIComponent(channel) + \"/\" + paddedLastModified;\n //@ts-ignore\n emit(id);\n });\n }.toString(),\n },\n },\n })\n //@ts-ignore\n .catch((error) => {\n if (\n error &&\n typeof error === \"object\" &&\n \"name\" in error &&\n error.name === \"conflict\"\n ) {\n // Design document already exists\n return;\n } else {\n throw error;\n }\n });\n return db;\n })();\n }\n return this.db_;\n }\n\n protected get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n protected async getOperationClock() {\n return Number((await (await this.db).info()).update_seq);\n }\n\n constructor(options?: GraffitiLocalOptions) {\n this.options = options ?? {};\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const [urlObject, schema, session] = args;\n const url = unpackObjectUrl(urlObject);\n\n let doc: GraffitiObjectData;\n try {\n doc = await (await this.db).get(url);\n } catch (error) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n const { actor } = decodeObjectUrl(url);\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n // Mask out the allowed list and channels\n // if the user is not the owner\n const masked = maskGraffitiObject(object, [], session?.actor);\n\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n if (!validate(masked)) {\n throw new GraffitiErrorSchemaMismatch();\n }\n return masked;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const [urlObject, session] = args;\n\n const url = unpackObjectUrl(urlObject);\n const { actor } = decodeObjectUrl(url);\n if (actor !== session.actor) {\n throw new GraffitiErrorForbidden(\n \"You cannot delete an object that you did not create.\",\n );\n }\n\n let doc: GraffitiObjectData & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta;\n try {\n doc = await (await this.db).get(url);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Set the tombstone and update lastModified\n doc.tombstone = true;\n doc.lastModified = await this.getOperationClock();\n try {\n await (await this.db).put(doc);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Return the output\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n return object;\n };\n\n post: Graffiti[\"post\"] = async (...args) => {\n const [objectPartial, session] = args;\n\n const actor = session.actor;\n const id = randomBase64();\n const url = encodeObjectUrl(actor, id);\n\n const { value, channels, allowed } = objectPartial;\n const object: GraffitiObjectData = {\n value,\n channels,\n allowed,\n lastModified: await this.getOperationClock(),\n tombstone: false,\n };\n\n await (\n await this.db\n ).put({\n _id: url,\n ...object,\n });\n\n return {\n ...objectPartial,\n actor,\n url,\n };\n };\n\n protected async *discoverMeta<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams?: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): AsyncGenerator<\n GraffitiObjectStreamContinueEntry<Schema>,\n ContinueDiscoverParams\n > {\n // If we are continuing a discover, make sure to wait at\n // least 2 seconds since the last poll to start a new one.\n if (continueParams) {\n const continueBuffer = this.options.continueBuffer ?? 2000;\n const timeElapsedSinceLastDiscover =\n Date.now() - continueParams.lastDiscovered;\n if (timeElapsedSinceLastDiscover < continueBuffer) {\n // Continue was called too soon,\n // wait a bit before continuing\n await new Promise((resolve) =>\n setTimeout(resolve, continueBuffer - timeElapsedSinceLastDiscover),\n );\n }\n }\n\n const [discoverChannels, schema, session] = args;\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const startKeySuffix = continueParams\n ? continueParams.ifModifiedSince.toString().padStart(15, \"0\")\n : \"\";\n const endKeySuffix = \"\\uffff\";\n\n const processedUrls = new Set<string>();\n\n const startTime = await this.getOperationClock();\n\n for (const channel of discoverChannels) {\n const keyPrefix = encodeURIComponent(channel) + \"/\";\n const startkey = keyPrefix + startKeySuffix;\n const endkey = keyPrefix + endKeySuffix;\n\n const result = await (\n await this.db\n ).query<GraffitiObjectData>(\"indexes/objectsPerChannelAndLastModified\", {\n startkey,\n endkey,\n include_docs: true,\n });\n\n for (const row of result.rows) {\n const doc = row.doc;\n if (!doc) continue;\n\n const url = doc._id;\n\n if (processedUrls.has(url)) continue;\n processedUrls.add(url);\n\n // If this is not a continuation, skip tombstones\n if (!continueParams && doc.tombstone) continue;\n\n const { tombstone, value, channels, allowed } = doc;\n const { actor } = decodeObjectUrl(url);\n\n const object: GraffitiObjectBase = {\n url,\n value,\n allowed,\n channels,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) continue;\n\n const masked = maskGraffitiObject(\n object,\n discoverChannels,\n session?.actor,\n );\n\n if (!validate(masked)) continue;\n\n yield tombstone\n ? {\n tombstone: true,\n object: { url },\n }\n : { object: masked };\n }\n }\n\n return {\n lastDiscovered: Date.now(),\n ifModifiedSince: startTime,\n };\n }\n\n protected discoverCursor(\n args: Parameters<typeof Graffiti.prototype.discover<{}>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): string {\n const [channels, schema, session] = args;\n return (\n \"discover:\" +\n JSON.stringify({\n channels,\n schema,\n continueParams,\n actor: session?.actor,\n })\n );\n }\n\n protected async *discoverContinue<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n session?: GraffitiSession | null,\n ): GraffitiObjectStreamContinue<Schema> {\n if (session?.actor !== args[2]?.actor) {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n }\n const iterator = this.discoverMeta<Schema>(args, continueParams);\n\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this.discoverContinue<Schema>(args, result.value, session),\n cursor: this.discoverCursor(args, result.value),\n };\n }\n yield result.value;\n }\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const [channels, schema, session] = args;\n const iterator = this.discoverMeta<(typeof args)[1]>([\n channels,\n schema,\n session,\n ]);\n\n const this_ = this;\n return (async function* () {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this_.discoverContinue<(typeof args)[1]>(\n args,\n result.value,\n session,\n ),\n cursor: this_.discoverCursor(args, result.value),\n };\n }\n // Make sure to filter out tombstones\n if (result.value.tombstone) continue;\n yield result.value;\n }\n })();\n };\n\n continueDiscover: Graffiti[\"continueDiscover\"] = (...args) => {\n const [cursor, session] = args;\n if (cursor.startsWith(\"discover:\")) {\n // TODO: use AJV here\n const { channels, schema, actor, continueParams } = JSON.parse(\n cursor.slice(\"discover:\".length),\n );\n if (actor && actor !== session?.actor) {\n return (async function* () {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n })();\n }\n return this.discoverContinue<{}>(\n [channels, schema, session],\n continueParams,\n );\n } else {\n return (async function* () {\n throw new GraffitiErrorNotFound(\"Cursor not found\");\n })();\n }\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,iBAOO;AACP,uBAKO;AAuCA,MAAM,qBAAqB;AAAA,EACtB;AAAA,EACA;AAAA,EACS;AAAA,EAEnB,IAAI,KAAK;AACP,QAAI,CAAC,KAAK,KAAK;AACb,WAAK,OAAO,YAAY;AACtB,cAAM,EAAE,SAAS,QAAQ,IAAI,MAAM,OAAO,SAAS;AACnD,cAAM,iBAAiB;AAAA,UACrB,MAAM;AAAA,UACN,GAAG,KAAK,QAAQ;AAAA,QAClB;AACA,cAAM,KAAK,IAAI;AAAA,UACb,eAAe;AAAA,UACf;AAAA,QACF;AACA,cAAM,GAEH,IAAI;AAAA,UACH,KAAK;AAAA,UACL,OAAO;AAAA,YACL,kCAAkC;AAAA,cAChC,KAAK,SAAU,QAA4B;AACzC,sBAAM,qBAAqB,OAAO,aAC/B,SAAS,EACT,SAAS,IAAI,GAAG;AACnB,uBAAO,SAAS,QAAQ,SAAU,SAAS;AACzC,wBAAM,KACJ,mBAAmB,OAAO,IAAI,MAAM;AAEtC,uBAAK,EAAE;AAAA,gBACT,CAAC;AAAA,cACH,EAAE,SAAS;AAAA,YACb;AAAA,UACF;AAAA,QACF,CAAC,EAEA,MAAM,CAAC,UAAU;AAChB,cACE,SACA,OAAO,UAAU,YACjB,UAAU,SACV,MAAM,SAAS,YACf;AAEA;AAAA,UACF,OAAO;AACL,kBAAM;AAAA,UACR;AAAA,QACF,CAAC;AACH,eAAO;AAAA,MACT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O,MAAM;AACf,iBAAO;AAAA,YACL,UAAU,CAACA,aACT,MAAM;AAAA,cACJ;AAAA,cACA,OAAO;AAAA,cACPA;AAAA,YACF;AAAA,YACF,QAAQ,MAAM,eAAe,MAAM,OAAO,KAAK;AAAA,UACjD;AAAA,QACF;AAEA,YAAI,OAAO,MAAM,UAAW;AAC5B,cAAM,OAAO;AAAA,MACf;AAAA,IACF,GAAG;AAAA,EACL;AAAA,EAEA,mBAAiD,IAAI,SAAS;AAC5D,UAAM,CAAC,QAAQ,OAAO,IAAI;AAC1B,QAAI,OAAO,WAAW,WAAW,GAAG;AAElC,YAAM,EAAE,UAAU,QAAQ,OAAO,eAAe,IAAI,KAAK;AAAA,QACvD,OAAO,MAAM,YAAY,MAAM;AAAA,MACjC;AACA,UAAI,SAAS,UAAU,SAAS,OAAO;AACrC,gBAAQ,mBAAmB;AACzB,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF,GAAG;AAAA,MACL;AACA,aAAO,KAAK;AAAA,QACV,CAAC,UAAU,QAAQ,OAAO;AAAA,QAC1B;AAAA,MACF;AAAA,IACF,OAAO;AACL,cAAQ,mBAAmB;AACzB,cAAM,IAAI,iCAAsB,kBAAkB;AAAA,MACpD,GAAG;AAAA,IACL;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["session"]
|
|
7
7
|
}
|
package/dist/cjs/utilities.js
CHANGED
|
@@ -20,6 +20,7 @@ var utilities_exports = {};
|
|
|
20
20
|
__export(utilities_exports, {
|
|
21
21
|
base64ToBlob: () => base64ToBlob,
|
|
22
22
|
blobToBase64: () => blobToBase64,
|
|
23
|
+
compileGraffitiObjectSchema: () => compileGraffitiObjectSchema,
|
|
23
24
|
decodeBase64: () => decodeBase64,
|
|
24
25
|
decodeGraffitiUrl: () => decodeGraffitiUrl,
|
|
25
26
|
decodeMediaUrl: () => decodeMediaUrl,
|
|
@@ -99,4 +100,13 @@ async function base64ToBlob(dataUrl) {
|
|
|
99
100
|
const response = await fetch(dataUrl);
|
|
100
101
|
return await response.blob();
|
|
101
102
|
}
|
|
103
|
+
function compileGraffitiObjectSchema(ajv, schema) {
|
|
104
|
+
try {
|
|
105
|
+
return ajv.compile(schema);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
throw new import_api.GraffitiErrorInvalidSchema(
|
|
108
|
+
error instanceof Error ? error.message : void 0
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
102
112
|
//# sourceMappingURL=utilities.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utilities.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
4
|
+
"sourcesContent": ["import {\n GraffitiErrorInvalidSchema,\n GraffitiErrorNotFound,\n type GraffitiObject,\n type GraffitiObjectBase,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport type Ajv from \"ajv\";\n\nexport function encodeBase64(bytes: Uint8Array): string {\n // Convert it to base64\n const base64 = btoa(String.fromCodePoint(...bytes));\n // Make sure it is url safe\n return base64.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/\\=+$/, \"\");\n}\n\nexport function decodeBase64(base64Url: string): Uint8Array {\n // Undo url-safe base64\n let base64 = base64Url.replace(/-/g, \"+\").replace(/_/g, \"/\");\n // Add padding if necessary\n while (base64.length % 4 !== 0) base64 += \"=\";\n // Decode\n return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));\n}\n\nexport function randomBase64(numBytes: number = 32): string {\n // Generate random bytes\n const bytes = new Uint8Array(numBytes);\n crypto.getRandomValues(bytes);\n return encodeBase64(bytes);\n}\n\nconst OBJECT_URL_PREFIX = \"graffiti:object:\";\nconst MEDIA_URL_PREFIX = \"graffiti:media:\";\n\nexport function encodeGraffitiUrl(actor: string, id: string, prefix: string) {\n return `${prefix}${encodeURIComponent(actor)}:${encodeURIComponent(id)}`;\n}\nexport function encodeObjectUrl(actor: string, id: string) {\n return encodeGraffitiUrl(actor, id, OBJECT_URL_PREFIX);\n}\nexport function encodeMediaUrl(actor: string, id: string) {\n return encodeGraffitiUrl(actor, id, MEDIA_URL_PREFIX);\n}\n\nexport function decodeGraffitiUrl(url: string, prefix: string) {\n if (!url.startsWith(prefix)) {\n throw new GraffitiErrorNotFound(`URL does not start with ${prefix}`);\n }\n const slices = url.slice(prefix.length).split(\":\");\n if (slices.length !== 2) {\n throw new GraffitiErrorNotFound(\"URL has too many colon-seperated parts\");\n }\n const [actor, id] = slices.map(decodeURIComponent);\n return { actor, id };\n}\nexport function decodeObjectUrl(url: string) {\n return decodeGraffitiUrl(url, OBJECT_URL_PREFIX);\n}\nexport function decodeMediaUrl(url: string) {\n return decodeGraffitiUrl(url, MEDIA_URL_PREFIX);\n}\n\nexport async function blobToBase64(blob: Blob): Promise<string> {\n if (typeof FileReader !== \"undefined\") {\n return new Promise((resolve, reject) => {\n const r = new FileReader();\n r.onload = () => {\n if (typeof r.result === \"string\") {\n resolve(r.result);\n } else {\n reject(new Error(\"Unexpected result type\"));\n }\n };\n r.onerror = reject;\n r.readAsDataURL(blob);\n });\n }\n\n if (typeof Buffer !== \"undefined\") {\n const ab = await blob.arrayBuffer();\n return `data:${blob.type};base64,${Buffer.from(ab).toString(\"base64\")}`;\n }\n\n throw new Error(\"Unsupported environment\");\n}\n\nexport async function base64ToBlob(dataUrl: string) {\n const response = await fetch(dataUrl);\n return await response.blob();\n}\n\nexport function compileGraffitiObjectSchema<Schema extends JSONSchema>(\n ajv: Ajv,\n schema: Schema,\n) {\n try {\n // Force the validation guard because\n // it is too big for the type checker.\n // Fortunately json-schema-to-ts is\n // well tested against ajv.\n return ajv.compile(schema) as (\n data: GraffitiObjectBase,\n ) => data is GraffitiObject<Schema>;\n } catch (error) {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ANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
GraffitiErrorNotAcceptable,
|
|
3
|
-
GraffitiErrorTooLarge
|
|
3
|
+
GraffitiErrorTooLarge,
|
|
4
|
+
isMediaAcceptable
|
|
4
5
|
} from "@graffiti-garden/api";
|
|
5
6
|
import {
|
|
6
7
|
decodeObjectUrl,
|
|
@@ -10,7 +11,6 @@ import {
|
|
|
10
11
|
blobToBase64,
|
|
11
12
|
base64ToBlob
|
|
12
13
|
} from "./utilities";
|
|
13
|
-
import Negotiator from "negotiator";
|
|
14
14
|
const MEDIA_OBJECT_SCHEMA = {
|
|
15
15
|
properties: {
|
|
16
16
|
value: {
|
|
@@ -48,7 +48,7 @@ class GraffitiLocalMedia {
|
|
|
48
48
|
return encodeMediaUrl(actor, id);
|
|
49
49
|
};
|
|
50
50
|
getMedia = async (...args) => {
|
|
51
|
-
const [mediaUrl,
|
|
51
|
+
const [mediaUrl, accept, session] = args;
|
|
52
52
|
const { actor, id } = decodeMediaUrl(mediaUrl);
|
|
53
53
|
const objectUrl = encodeObjectUrl(actor, id);
|
|
54
54
|
const object = await this.db.get(
|
|
@@ -57,15 +57,14 @@ class GraffitiLocalMedia {
|
|
|
57
57
|
session
|
|
58
58
|
);
|
|
59
59
|
const { dataBase64, type, size } = object.value;
|
|
60
|
-
if (
|
|
60
|
+
if (accept?.maxBytes && size > accept.maxBytes) {
|
|
61
61
|
throw new GraffitiErrorTooLarge("File size exceeds limit");
|
|
62
62
|
}
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
throw new GraffitiErrorNotAcceptable(`Unsupported media type, ${type}`);
|
|
63
|
+
if (accept?.types) {
|
|
64
|
+
if (!isMediaAcceptable(type, accept.types)) {
|
|
65
|
+
throw new GraffitiErrorNotAcceptable(
|
|
66
|
+
`Unacceptable media type, ${type}`
|
|
67
|
+
);
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
const data = await base64ToBlob(dataBase64);
|
package/dist/esm/media.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/media.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n GraffitiErrorNotAcceptable,\n GraffitiErrorTooLarge,\n type Graffiti,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport {\n decodeObjectUrl,\n encodeObjectUrl,\n decodeMediaUrl,\n encodeMediaUrl,\n blobToBase64,\n base64ToBlob,\n} from \"./utilities\";\
|
|
5
|
-
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;
|
|
4
|
+
"sourcesContent": ["import {\n GraffitiErrorNotAcceptable,\n GraffitiErrorTooLarge,\n isMediaAcceptable,\n type Graffiti,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport {\n decodeObjectUrl,\n encodeObjectUrl,\n decodeMediaUrl,\n encodeMediaUrl,\n blobToBase64,\n base64ToBlob,\n} from \"./utilities\";\n\nconst MEDIA_OBJECT_SCHEMA = {\n properties: {\n value: {\n properties: {\n dataBase64: { type: \"string\" },\n type: { type: \"string\" },\n size: { type: \"number\" },\n },\n required: [\"dataBase64\", \"type\", \"size\"],\n },\n },\n} as const satisfies JSONSchema;\n\nexport class GraffitiLocalMedia {\n protected db: Pick<Graffiti, \"post\" | \"get\" | \"delete\">;\n\n constructor(db: Pick<Graffiti, \"post\" | \"get\" | \"delete\">) {\n this.db = db;\n }\n\n postMedia: Graffiti[\"postMedia\"] = async (...args) => {\n const [media, session] = args;\n\n const dataBase64 = await blobToBase64(media.data);\n const type = media.data.type;\n\n const { url } = await this.db.post<typeof MEDIA_OBJECT_SCHEMA>(\n {\n value: {\n dataBase64,\n type,\n size: media.data.size,\n },\n channels: [],\n allowed: media.allowed,\n },\n session,\n );\n\n const { actor, id } = decodeObjectUrl(url);\n return encodeMediaUrl(actor, id);\n };\n\n getMedia: Graffiti[\"getMedia\"] = async (...args) => {\n const [mediaUrl, accept, session] = args;\n const { actor, id } = decodeMediaUrl(mediaUrl);\n const objectUrl = encodeObjectUrl(actor, id);\n\n const object = await this.db.get<typeof MEDIA_OBJECT_SCHEMA>(\n objectUrl,\n MEDIA_OBJECT_SCHEMA,\n session,\n );\n\n const { dataBase64, type, size } = object.value;\n\n if (accept?.maxBytes && size > accept.maxBytes) {\n throw new GraffitiErrorTooLarge(\"File size exceeds limit\");\n }\n\n // Make sure it adheres to requirements.accept\n if (accept?.types) {\n if (!isMediaAcceptable(type, accept.types)) {\n throw new GraffitiErrorNotAcceptable(\n `Unacceptable media type, ${type}`,\n );\n }\n }\n\n const data = await base64ToBlob(dataBase64);\n if (data.size !== size || data.type !== type) {\n throw new Error(\"Invalid data\");\n }\n\n return {\n data,\n actor: object.actor,\n allowed: object.allowed,\n };\n };\n\n deleteMedia: Graffiti[\"deleteMedia\"] = async (...args) => {\n const [mediaUrl, session] = args;\n const { actor, id } = decodeMediaUrl(mediaUrl);\n const objectUrl = encodeObjectUrl(actor, id);\n\n await this.db.delete(objectUrl, session);\n };\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,MAAM,sBAAsB;AAAA,EAC1B,YAAY;AAAA,IACV,OAAO;AAAA,MACL,YAAY;AAAA,QACV,YAAY,EAAE,MAAM,SAAS;AAAA,QAC7B,MAAM,EAAE,MAAM,SAAS;AAAA,QACvB,MAAM,EAAE,MAAM,SAAS;AAAA,MACzB;AAAA,MACA,UAAU,CAAC,cAAc,QAAQ,MAAM;AAAA,IACzC;AAAA,EACF;AACF;AAEO,MAAM,mBAAmB;AAAA,EACpB;AAAA,EAEV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,OAAO;AAAA,IAClB;AAAA,EACF;AAAA,EAEA,cAAuC,UAAU,SAAS;AACxD,UAAM,CAAC,UAAU,OAAO,IAAI;AAC5B,UAAM,EAAE,OAAO,GAAG,IAAI,eAAe,QAAQ;AAC7C,UAAM,YAAY,gBAAgB,OAAO,EAAE;AAE3C,UAAM,KAAK,GAAG,OAAO,WAAW,OAAO;AAAA,EACzC;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/objects.js
CHANGED
|
@@ -4,10 +4,14 @@ import {
|
|
|
4
4
|
GraffitiErrorForbidden,
|
|
5
5
|
unpackObjectUrl,
|
|
6
6
|
maskGraffitiObject,
|
|
7
|
-
isActorAllowedGraffitiObject
|
|
8
|
-
compileGraffitiObjectSchema
|
|
7
|
+
isActorAllowedGraffitiObject
|
|
9
8
|
} from "@graffiti-garden/api";
|
|
10
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
randomBase64,
|
|
11
|
+
decodeObjectUrl,
|
|
12
|
+
encodeObjectUrl,
|
|
13
|
+
compileGraffitiObjectSchema
|
|
14
|
+
} from "./utilities.js";
|
|
11
15
|
class GraffitiLocalObjects {
|
|
12
16
|
db_;
|
|
13
17
|
ajv_;
|
|
@@ -94,12 +98,12 @@ class GraffitiLocalObjects {
|
|
|
94
98
|
"The object you are trying to get either does not exist or you are not allowed to see it"
|
|
95
99
|
);
|
|
96
100
|
}
|
|
97
|
-
maskGraffitiObject(object, [], session);
|
|
101
|
+
const masked = maskGraffitiObject(object, [], session?.actor);
|
|
98
102
|
const validate = compileGraffitiObjectSchema(await this.ajv, schema);
|
|
99
|
-
if (!validate(
|
|
103
|
+
if (!validate(masked)) {
|
|
100
104
|
throw new GraffitiErrorSchemaMismatch();
|
|
101
105
|
}
|
|
102
|
-
return
|
|
106
|
+
return masked;
|
|
103
107
|
};
|
|
104
108
|
delete = async (...args) => {
|
|
105
109
|
const [urlObject, session] = args;
|
|
@@ -126,7 +130,15 @@ class GraffitiLocalObjects {
|
|
|
126
130
|
} catch {
|
|
127
131
|
throw new GraffitiErrorNotFound("Object not found.");
|
|
128
132
|
}
|
|
129
|
-
|
|
133
|
+
const { value, channels, allowed } = doc;
|
|
134
|
+
const object = {
|
|
135
|
+
value,
|
|
136
|
+
channels,
|
|
137
|
+
allowed,
|
|
138
|
+
url,
|
|
139
|
+
actor
|
|
140
|
+
};
|
|
141
|
+
return object;
|
|
130
142
|
};
|
|
131
143
|
post = async (...args) => {
|
|
132
144
|
const [objectPartial, session] = args;
|
|
@@ -193,12 +205,16 @@ class GraffitiLocalObjects {
|
|
|
193
205
|
actor
|
|
194
206
|
};
|
|
195
207
|
if (!isActorAllowedGraffitiObject(object, session)) continue;
|
|
196
|
-
maskGraffitiObject(
|
|
197
|
-
|
|
208
|
+
const masked = maskGraffitiObject(
|
|
209
|
+
object,
|
|
210
|
+
discoverChannels,
|
|
211
|
+
session?.actor
|
|
212
|
+
);
|
|
213
|
+
if (!validate(masked)) continue;
|
|
198
214
|
yield tombstone ? {
|
|
199
215
|
tombstone: true,
|
|
200
216
|
object: { url }
|
|
201
|
-
} : { object };
|
|
217
|
+
} : { object: masked };
|
|
202
218
|
}
|
|
203
219
|
}
|
|
204
220
|
return {
|
package/dist/esm/objects.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/objects.ts"],
|
|
4
|
-
"sourcesContent": ["import type {\n Graffiti,\n GraffitiObjectBase,\n JSONSchema,\n GraffitiSession,\n GraffitiObjectStreamContinue,\n GraffitiObjectStreamContinueEntry,\n} from \"@graffiti-garden/api\";\nimport {\n GraffitiErrorNotFound,\n GraffitiErrorSchemaMismatch,\n GraffitiErrorForbidden,\n unpackObjectUrl,\n maskGraffitiObject,\n isActorAllowedGraffitiObject,\n compileGraffitiObjectSchema,\n} from \"@graffiti-garden/api\";\nimport { randomBase64, decodeObjectUrl, encodeObjectUrl } from \"./utilities.js\";\nimport type Ajv from \"ajv\";\n\n/**\n * Constructor options for the GraffitiPoubchDB class.\n */\nexport interface GraffitiLocalOptions {\n /**\n * Options to pass to the PouchDB constructor.\n * Defaults to `{ name: \"graffitiDb\" }`.\n *\n * See the [PouchDB documentation](https://pouchdb.com/api.html#create_database)\n * for available options.\n */\n pouchDBOptions?: PouchDB.Configuration.DatabaseConfiguration;\n /**\n * Wait at least this long (in milliseconds) before continuing a stream.\n * A basic form of rate limiting. Defaults to 2 seconds.\n */\n continueBuffer?: number;\n}\n\ntype GraffitiObjectData = {\n tombstone: boolean;\n value: {};\n channels: string[];\n allowed?: string[] | null;\n lastModified: number;\n};\n\ntype ContinueDiscoverParams = {\n lastDiscovered: number;\n ifModifiedSince: number;\n};\n\n/**\n * An implementation of only the database operations of the\n * GraffitiAPI without synchronization or session management.\n */\nexport class GraffitiLocalObjects {\n protected db_: Promise<PouchDB.Database<GraffitiObjectData>> | undefined;\n protected ajv_: Promise<Ajv> | undefined;\n protected readonly options: GraffitiLocalOptions;\n\n get db() {\n if (!this.db_) {\n this.db_ = (async () => {\n const { default: PouchDB } = await import(\"pouchdb\");\n const pouchDbOptions = {\n name: \"graffitiDb\",\n ...this.options.pouchDBOptions,\n };\n const db = new PouchDB<GraffitiObjectData>(\n pouchDbOptions.name,\n pouchDbOptions,\n );\n await db\n //@ts-ignore\n .put({\n _id: \"_design/indexes\",\n views: {\n objectsPerChannelAndLastModified: {\n map: function (object: GraffitiObjectData) {\n const paddedLastModified = object.lastModified\n .toString()\n .padStart(15, \"0\");\n object.channels.forEach(function (channel) {\n const id =\n encodeURIComponent(channel) + \"/\" + paddedLastModified;\n //@ts-ignore\n emit(id);\n });\n }.toString(),\n },\n },\n })\n //@ts-ignore\n .catch((error) => {\n if (\n error &&\n typeof error === \"object\" &&\n \"name\" in error &&\n error.name === \"conflict\"\n ) {\n // Design document already exists\n return;\n } else {\n throw error;\n }\n });\n return db;\n })();\n }\n return this.db_;\n }\n\n protected get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n protected async getOperationClock() {\n return Number((await (await this.db).info()).update_seq);\n }\n\n constructor(options?: GraffitiLocalOptions) {\n this.options = options ?? {};\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const [urlObject, schema, session] = args;\n const url = unpackObjectUrl(urlObject);\n\n let doc: GraffitiObjectData;\n try {\n doc = await (await this.db).get(url);\n } catch (error) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n const { actor } = decodeObjectUrl(url);\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n // Mask out the allowed list and channels\n // if the user is not the owner\n maskGraffitiObject(object, [], session);\n\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n if (!validate(object)) {\n throw new GraffitiErrorSchemaMismatch();\n }\n return object;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const [urlObject, session] = args;\n\n const url = unpackObjectUrl(urlObject);\n const { actor } = decodeObjectUrl(url);\n if (actor !== session.actor) {\n throw new GraffitiErrorForbidden(\n \"You cannot delete an object that you did not create.\",\n );\n }\n\n let doc: GraffitiObjectData & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta;\n try {\n doc = await (await this.db).get(url);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Set the tombstone and update lastModified\n doc.tombstone = true;\n doc.lastModified = await this.getOperationClock();\n try {\n await (await this.db).put(doc);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n return;\n };\n\n post: Graffiti[\"post\"] = async (...args) => {\n const [objectPartial, session] = args;\n\n const actor = session.actor;\n const id = randomBase64();\n const url = encodeObjectUrl(actor, id);\n\n const { value, channels, allowed } = objectPartial;\n const object: GraffitiObjectData = {\n value,\n channels,\n allowed,\n lastModified: await this.getOperationClock(),\n tombstone: false,\n };\n\n await (\n await this.db\n ).put({\n _id: url,\n ...object,\n });\n\n return {\n ...objectPartial,\n actor,\n url,\n };\n };\n\n protected async *discoverMeta<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams?: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): AsyncGenerator<\n GraffitiObjectStreamContinueEntry<Schema>,\n ContinueDiscoverParams\n > {\n // If we are continuing a discover, make sure to wait at\n // least 2 seconds since the last poll to start a new one.\n if (continueParams) {\n const continueBuffer = this.options.continueBuffer ?? 2000;\n const timeElapsedSinceLastDiscover =\n Date.now() - continueParams.lastDiscovered;\n if (timeElapsedSinceLastDiscover < continueBuffer) {\n // Continue was called too soon,\n // wait a bit before continuing\n await new Promise((resolve) =>\n setTimeout(resolve, continueBuffer - timeElapsedSinceLastDiscover),\n );\n }\n }\n\n const [discoverChannels, schema, session] = args;\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const startKeySuffix = continueParams\n ? continueParams.ifModifiedSince.toString().padStart(15, \"0\")\n : \"\";\n const endKeySuffix = \"\\uffff\";\n\n const processedUrls = new Set<string>();\n\n const startTime = await this.getOperationClock();\n\n for (const channel of discoverChannels) {\n const keyPrefix = encodeURIComponent(channel) + \"/\";\n const startkey = keyPrefix + startKeySuffix;\n const endkey = keyPrefix + endKeySuffix;\n\n const result = await (\n await this.db\n ).query<GraffitiObjectData>(\"indexes/objectsPerChannelAndLastModified\", {\n startkey,\n endkey,\n include_docs: true,\n });\n\n for (const row of result.rows) {\n const doc = row.doc;\n if (!doc) continue;\n\n const url = doc._id;\n\n if (processedUrls.has(url)) continue;\n processedUrls.add(url);\n\n // If this is not a continuation, skip tombstones\n if (!continueParams && doc.tombstone) continue;\n\n const { tombstone, value, channels, allowed } = doc;\n const { actor } = decodeObjectUrl(url);\n\n const object: GraffitiObjectBase = {\n url,\n value,\n allowed,\n channels,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) continue;\n\n maskGraffitiObject(object, discoverChannels, session);\n\n if (!validate(object)) continue;\n\n yield tombstone\n ? {\n tombstone: true,\n object: { url },\n }\n : { object };\n }\n }\n\n return {\n lastDiscovered: Date.now(),\n ifModifiedSince: startTime,\n };\n }\n\n protected discoverCursor(\n args: Parameters<typeof Graffiti.prototype.discover<{}>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): string {\n const [channels, schema, session] = args;\n return (\n \"discover:\" +\n JSON.stringify({\n channels,\n schema,\n continueParams,\n actor: session?.actor,\n })\n );\n }\n\n protected async *discoverContinue<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n session?: GraffitiSession | null,\n ): GraffitiObjectStreamContinue<Schema> {\n if (session?.actor !== args[2]?.actor) {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n }\n const iterator = this.discoverMeta<Schema>(args, continueParams);\n\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this.discoverContinue<Schema>(args, result.value, session),\n cursor: this.discoverCursor(args, result.value),\n };\n }\n yield result.value;\n }\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const [channels, schema, session] = args;\n const iterator = this.discoverMeta<(typeof args)[1]>([\n channels,\n schema,\n session,\n ]);\n\n const this_ = this;\n return (async function* () {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this_.discoverContinue<(typeof args)[1]>(\n args,\n result.value,\n session,\n ),\n cursor: this_.discoverCursor(args, result.value),\n };\n }\n // Make sure to filter out tombstones\n if (result.value.tombstone) continue;\n yield result.value;\n }\n })();\n };\n\n continueDiscover: Graffiti[\"continueDiscover\"] = (...args) => {\n const [cursor, session] = args;\n if (cursor.startsWith(\"discover:\")) {\n // TODO: use AJV here\n const { channels, schema, actor, continueParams } = JSON.parse(\n cursor.slice(\"discover:\".length),\n );\n if (actor && actor !== session?.actor) {\n return (async function* () {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n })();\n }\n return this.discoverContinue<{}>(\n [channels, schema, session],\n continueParams,\n );\n } else {\n return (async function* () {\n throw new GraffitiErrorNotFound(\"Cursor not found\");\n })();\n }\n };\n}\n"],
|
|
5
|
-
"mappings": "AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,
|
|
4
|
+
"sourcesContent": ["import type {\n Graffiti,\n GraffitiObjectBase,\n JSONSchema,\n GraffitiSession,\n GraffitiObjectStreamContinue,\n GraffitiObjectStreamContinueEntry,\n} from \"@graffiti-garden/api\";\nimport {\n GraffitiErrorNotFound,\n GraffitiErrorSchemaMismatch,\n GraffitiErrorForbidden,\n unpackObjectUrl,\n maskGraffitiObject,\n isActorAllowedGraffitiObject,\n} from \"@graffiti-garden/api\";\nimport {\n randomBase64,\n decodeObjectUrl,\n encodeObjectUrl,\n compileGraffitiObjectSchema,\n} from \"./utilities.js\";\nimport type Ajv from \"ajv\";\n\n/**\n * Constructor options for the GraffitiPoubchDB class.\n */\nexport interface GraffitiLocalOptions {\n /**\n * Options to pass to the PouchDB constructor.\n * Defaults to `{ name: \"graffitiDb\" }`.\n *\n * See the [PouchDB documentation](https://pouchdb.com/api.html#create_database)\n * for available options.\n */\n pouchDBOptions?: PouchDB.Configuration.DatabaseConfiguration;\n /**\n * Wait at least this long (in milliseconds) before continuing a stream.\n * A basic form of rate limiting. Defaults to 2 seconds.\n */\n continueBuffer?: number;\n}\n\ntype GraffitiObjectData = {\n tombstone: boolean;\n value: {};\n channels: string[];\n allowed?: string[] | null;\n lastModified: number;\n};\n\ntype ContinueDiscoverParams = {\n lastDiscovered: number;\n ifModifiedSince: number;\n};\n\n/**\n * An implementation of only the database operations of the\n * GraffitiAPI without synchronization or session management.\n */\nexport class GraffitiLocalObjects {\n protected db_: Promise<PouchDB.Database<GraffitiObjectData>> | undefined;\n protected ajv_: Promise<Ajv> | undefined;\n protected readonly options: GraffitiLocalOptions;\n\n get db() {\n if (!this.db_) {\n this.db_ = (async () => {\n const { default: PouchDB } = await import(\"pouchdb\");\n const pouchDbOptions = {\n name: \"graffitiDb\",\n ...this.options.pouchDBOptions,\n };\n const db = new PouchDB<GraffitiObjectData>(\n pouchDbOptions.name,\n pouchDbOptions,\n );\n await db\n //@ts-ignore\n .put({\n _id: \"_design/indexes\",\n views: {\n objectsPerChannelAndLastModified: {\n map: function (object: GraffitiObjectData) {\n const paddedLastModified = object.lastModified\n .toString()\n .padStart(15, \"0\");\n object.channels.forEach(function (channel) {\n const id =\n encodeURIComponent(channel) + \"/\" + paddedLastModified;\n //@ts-ignore\n emit(id);\n });\n }.toString(),\n },\n },\n })\n //@ts-ignore\n .catch((error) => {\n if (\n error &&\n typeof error === \"object\" &&\n \"name\" in error &&\n error.name === \"conflict\"\n ) {\n // Design document already exists\n return;\n } else {\n throw error;\n }\n });\n return db;\n })();\n }\n return this.db_;\n }\n\n protected get ajv() {\n if (!this.ajv_) {\n this.ajv_ = (async () => {\n const { default: Ajv } = await import(\"ajv\");\n return new Ajv({ strict: false });\n })();\n }\n return this.ajv_;\n }\n\n protected async getOperationClock() {\n return Number((await (await this.db).info()).update_seq);\n }\n\n constructor(options?: GraffitiLocalOptions) {\n this.options = options ?? {};\n }\n\n get: Graffiti[\"get\"] = async (...args) => {\n const [urlObject, schema, session] = args;\n const url = unpackObjectUrl(urlObject);\n\n let doc: GraffitiObjectData;\n try {\n doc = await (await this.db).get(url);\n } catch (error) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n const { actor } = decodeObjectUrl(url);\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) {\n throw new GraffitiErrorNotFound(\n \"The object you are trying to get either does not exist or you are not allowed to see it\",\n );\n }\n\n // Mask out the allowed list and channels\n // if the user is not the owner\n const masked = maskGraffitiObject(object, [], session?.actor);\n\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n if (!validate(masked)) {\n throw new GraffitiErrorSchemaMismatch();\n }\n return masked;\n };\n\n delete: Graffiti[\"delete\"] = async (...args) => {\n const [urlObject, session] = args;\n\n const url = unpackObjectUrl(urlObject);\n const { actor } = decodeObjectUrl(url);\n if (actor !== session.actor) {\n throw new GraffitiErrorForbidden(\n \"You cannot delete an object that you did not create.\",\n );\n }\n\n let doc: GraffitiObjectData & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta;\n try {\n doc = await (await this.db).get(url);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n if (doc.tombstone) {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Set the tombstone and update lastModified\n doc.tombstone = true;\n doc.lastModified = await this.getOperationClock();\n try {\n await (await this.db).put(doc);\n } catch {\n throw new GraffitiErrorNotFound(\"Object not found.\");\n }\n\n // Return the output\n const { value, channels, allowed } = doc;\n const object: GraffitiObjectBase = {\n value,\n channels,\n allowed,\n url,\n actor,\n };\n return object;\n };\n\n post: Graffiti[\"post\"] = async (...args) => {\n const [objectPartial, session] = args;\n\n const actor = session.actor;\n const id = randomBase64();\n const url = encodeObjectUrl(actor, id);\n\n const { value, channels, allowed } = objectPartial;\n const object: GraffitiObjectData = {\n value,\n channels,\n allowed,\n lastModified: await this.getOperationClock(),\n tombstone: false,\n };\n\n await (\n await this.db\n ).put({\n _id: url,\n ...object,\n });\n\n return {\n ...objectPartial,\n actor,\n url,\n };\n };\n\n protected async *discoverMeta<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams?: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): AsyncGenerator<\n GraffitiObjectStreamContinueEntry<Schema>,\n ContinueDiscoverParams\n > {\n // If we are continuing a discover, make sure to wait at\n // least 2 seconds since the last poll to start a new one.\n if (continueParams) {\n const continueBuffer = this.options.continueBuffer ?? 2000;\n const timeElapsedSinceLastDiscover =\n Date.now() - continueParams.lastDiscovered;\n if (timeElapsedSinceLastDiscover < continueBuffer) {\n // Continue was called too soon,\n // wait a bit before continuing\n await new Promise((resolve) =>\n setTimeout(resolve, continueBuffer - timeElapsedSinceLastDiscover),\n );\n }\n }\n\n const [discoverChannels, schema, session] = args;\n const validate = compileGraffitiObjectSchema(await this.ajv, schema);\n const startKeySuffix = continueParams\n ? continueParams.ifModifiedSince.toString().padStart(15, \"0\")\n : \"\";\n const endKeySuffix = \"\\uffff\";\n\n const processedUrls = new Set<string>();\n\n const startTime = await this.getOperationClock();\n\n for (const channel of discoverChannels) {\n const keyPrefix = encodeURIComponent(channel) + \"/\";\n const startkey = keyPrefix + startKeySuffix;\n const endkey = keyPrefix + endKeySuffix;\n\n const result = await (\n await this.db\n ).query<GraffitiObjectData>(\"indexes/objectsPerChannelAndLastModified\", {\n startkey,\n endkey,\n include_docs: true,\n });\n\n for (const row of result.rows) {\n const doc = row.doc;\n if (!doc) continue;\n\n const url = doc._id;\n\n if (processedUrls.has(url)) continue;\n processedUrls.add(url);\n\n // If this is not a continuation, skip tombstones\n if (!continueParams && doc.tombstone) continue;\n\n const { tombstone, value, channels, allowed } = doc;\n const { actor } = decodeObjectUrl(url);\n\n const object: GraffitiObjectBase = {\n url,\n value,\n allowed,\n channels,\n actor,\n };\n\n if (!isActorAllowedGraffitiObject(object, session)) continue;\n\n const masked = maskGraffitiObject(\n object,\n discoverChannels,\n session?.actor,\n );\n\n if (!validate(masked)) continue;\n\n yield tombstone\n ? {\n tombstone: true,\n object: { url },\n }\n : { object: masked };\n }\n }\n\n return {\n lastDiscovered: Date.now(),\n ifModifiedSince: startTime,\n };\n }\n\n protected discoverCursor(\n args: Parameters<typeof Graffiti.prototype.discover<{}>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n ): string {\n const [channels, schema, session] = args;\n return (\n \"discover:\" +\n JSON.stringify({\n channels,\n schema,\n continueParams,\n actor: session?.actor,\n })\n );\n }\n\n protected async *discoverContinue<Schema extends JSONSchema>(\n args: Parameters<typeof Graffiti.prototype.discover<Schema>>,\n continueParams: {\n lastDiscovered: number;\n ifModifiedSince: number;\n },\n session?: GraffitiSession | null,\n ): GraffitiObjectStreamContinue<Schema> {\n if (session?.actor !== args[2]?.actor) {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n }\n const iterator = this.discoverMeta<Schema>(args, continueParams);\n\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this.discoverContinue<Schema>(args, result.value, session),\n cursor: this.discoverCursor(args, result.value),\n };\n }\n yield result.value;\n }\n }\n\n discover: Graffiti[\"discover\"] = (...args) => {\n const [channels, schema, session] = args;\n const iterator = this.discoverMeta<(typeof args)[1]>([\n channels,\n schema,\n session,\n ]);\n\n const this_ = this;\n return (async function* () {\n while (true) {\n const result = await iterator.next();\n if (result.done) {\n return {\n continue: (session) =>\n this_.discoverContinue<(typeof args)[1]>(\n args,\n result.value,\n session,\n ),\n cursor: this_.discoverCursor(args, result.value),\n };\n }\n // Make sure to filter out tombstones\n if (result.value.tombstone) continue;\n yield result.value;\n }\n })();\n };\n\n continueDiscover: Graffiti[\"continueDiscover\"] = (...args) => {\n const [cursor, session] = args;\n if (cursor.startsWith(\"discover:\")) {\n // TODO: use AJV here\n const { channels, schema, actor, continueParams } = JSON.parse(\n cursor.slice(\"discover:\".length),\n );\n if (actor && actor !== session?.actor) {\n return (async function* () {\n throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n })();\n }\n return this.discoverContinue<{}>(\n [channels, schema, session],\n continueParams,\n );\n } else {\n return (async function* () {\n throw new GraffitiErrorNotFound(\"Cursor not found\");\n })();\n }\n };\n}\n"],
|
|
5
|
+
"mappings": "AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAuCA,MAAM,qBAAqB;AAAA,EACtB;AAAA,EACA;AAAA,EACS;AAAA,EAEnB,IAAI,KAAK;AACP,QAAI,CAAC,KAAK,KAAK;AACb,WAAK,OAAO,YAAY;AACtB,cAAM,EAAE,SAAS,QAAQ,IAAI,MAAM,OAAO,SAAS;AACnD,cAAM,iBAAiB;AAAA,UACrB,MAAM;AAAA,UACN,GAAG,KAAK,QAAQ;AAAA,QAClB;AACA,cAAM,KAAK,IAAI;AAAA,UACb,eAAe;AAAA,UACf;AAAA,QACF;AACA,cAAM,GAEH,IAAI;AAAA,UACH,KAAK;AAAA,UACL,OAAO;AAAA,YACL,kCAAkC;AAAA,cAChC,KAAK,SAAU,QAA4B;AACzC,sBAAM,qBAAqB,OAAO,aAC/B,SAAS,EACT,SAAS,IAAI,GAAG;AACnB,uBAAO,SAAS,QAAQ,SAAU,SAAS;AACzC,wBAAM,KACJ,mBAAmB,OAAO,IAAI,MAAM;AAEtC,uBAAK,EAAE;AAAA,gBACT,CAAC;AAAA,cACH,EAAE,SAAS;AAAA,YACb;AAAA,UACF;AAAA,QACF,CAAC,EAEA,MAAM,CAAC,UAAU;AAChB,cACE,SACA,OAAO,UAAU,YACjB,UAAU,SACV,MAAM,SAAS,YACf;AAEA;AAAA,UACF,OAAO;AACL,kBAA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ACX,cAAM,SAAS,MAAM,SAAS,KAAK;AACnC,YAAI,OAAO,MAAM;AACf,iBAAO;AAAA,YACL,UAAU,CAACA,aACT,MAAM;AAAA,cACJ;AAAA,cACA,OAAO;AAAA,cACPA;AAAA,YACF;AAAA,YACF,QAAQ,MAAM,eAAe,MAAM,OAAO,KAAK;AAAA,UACjD;AAAA,QACF;AAEA,YAAI,OAAO,MAAM,UAAW;AAC5B,cAAM,OAAO;AAAA,MACf;AAAA,IACF,GAAG;AAAA,EACL;AAAA,EAEA,mBAAiD,IAAI,SAAS;AAC5D,UAAM,CAAC,QAAQ,OAAO,IAAI;AAC1B,QAAI,OAAO,WAAW,WAAW,GAAG;AAElC,YAAM,EAAE,UAAU,QAAQ,OAAO,eAAe,IAAI,KAAK;AAAA,QACvD,OAAO,MAAM,YAAY,MAAM;AAAA,MACjC;AACA,UAAI,SAAS,UAAU,SAAS,OAAO;AACrC,gBAAQ,mBAAmB;AACzB,gBAAM,IAAI;AAAA,YACR;AAAA,UACF;AAAA,QACF,GAAG;AAAA,MACL;AACA,aAAO,KAAK;AAAA,QACV,CAAC,UAAU,QAAQ,OAAO;AAAA,QAC1B;AAAA,MACF;AAAA,IACF,OAAO;AACL,cAAQ,mBAAmB;AACzB,cAAM,IAAI,sBAAsB,kBAAkB;AAAA,MACpD,GAAG;AAAA,IACL;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["session"]
|
|
7
7
|
}
|
package/dist/esm/utilities.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
GraffitiErrorInvalidSchema,
|
|
3
|
+
GraffitiErrorNotFound
|
|
4
|
+
} from "@graffiti-garden/api";
|
|
2
5
|
function encodeBase64(bytes) {
|
|
3
6
|
const base64 = btoa(String.fromCodePoint(...bytes));
|
|
4
7
|
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, "");
|
|
@@ -66,9 +69,19 @@ async function base64ToBlob(dataUrl) {
|
|
|
66
69
|
const response = await fetch(dataUrl);
|
|
67
70
|
return await response.blob();
|
|
68
71
|
}
|
|
72
|
+
function compileGraffitiObjectSchema(ajv, schema) {
|
|
73
|
+
try {
|
|
74
|
+
return ajv.compile(schema);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new GraffitiErrorInvalidSchema(
|
|
77
|
+
error instanceof Error ? error.message : void 0
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
69
81
|
export {
|
|
70
82
|
base64ToBlob,
|
|
71
83
|
blobToBase64,
|
|
84
|
+
compileGraffitiObjectSchema,
|
|
72
85
|
decodeBase64,
|
|
73
86
|
decodeGraffitiUrl,
|
|
74
87
|
decodeMediaUrl,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utilities.ts"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,
|
|
4
|
+
"sourcesContent": ["import {\n GraffitiErrorInvalidSchema,\n GraffitiErrorNotFound,\n type GraffitiObject,\n type GraffitiObjectBase,\n type JSONSchema,\n} from \"@graffiti-garden/api\";\nimport type Ajv from \"ajv\";\n\nexport function encodeBase64(bytes: Uint8Array): string {\n // Convert it to base64\n const base64 = btoa(String.fromCodePoint(...bytes));\n // Make sure it is url safe\n return base64.replace(/\\+/g, \"-\").replace(/\\//g, \"_\").replace(/\\=+$/, \"\");\n}\n\nexport function decodeBase64(base64Url: string): Uint8Array {\n // Undo url-safe base64\n let base64 = base64Url.replace(/-/g, \"+\").replace(/_/g, \"/\");\n // Add padding if necessary\n while (base64.length % 4 !== 0) base64 += \"=\";\n // Decode\n return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));\n}\n\nexport function randomBase64(numBytes: number = 32): string {\n // Generate random bytes\n const bytes = new Uint8Array(numBytes);\n crypto.getRandomValues(bytes);\n return encodeBase64(bytes);\n}\n\nconst OBJECT_URL_PREFIX = \"graffiti:object:\";\nconst MEDIA_URL_PREFIX = \"graffiti:media:\";\n\nexport function encodeGraffitiUrl(actor: string, id: string, prefix: string) {\n return `${prefix}${encodeURIComponent(actor)}:${encodeURIComponent(id)}`;\n}\nexport function encodeObjectUrl(actor: string, id: string) {\n return encodeGraffitiUrl(actor, id, OBJECT_URL_PREFIX);\n}\nexport function encodeMediaUrl(actor: string, id: string) {\n return encodeGraffitiUrl(actor, id, MEDIA_URL_PREFIX);\n}\n\nexport function decodeGraffitiUrl(url: string, prefix: string) {\n if (!url.startsWith(prefix)) {\n throw new GraffitiErrorNotFound(`URL does not start with ${prefix}`);\n }\n const slices = url.slice(prefix.length).split(\":\");\n if (slices.length !== 2) {\n throw new GraffitiErrorNotFound(\"URL has too many colon-seperated parts\");\n }\n const [actor, id] = slices.map(decodeURIComponent);\n return { actor, id };\n}\nexport function decodeObjectUrl(url: string) {\n return decodeGraffitiUrl(url, OBJECT_URL_PREFIX);\n}\nexport function decodeMediaUrl(url: string) {\n return decodeGraffitiUrl(url, MEDIA_URL_PREFIX);\n}\n\nexport async function blobToBase64(blob: Blob): Promise<string> {\n if (typeof FileReader !== \"undefined\") {\n return new Promise((resolve, reject) => {\n const r = new FileReader();\n r.onload = () => {\n if (typeof r.result === \"string\") {\n resolve(r.result);\n } else {\n reject(new Error(\"Unexpected result type\"));\n }\n };\n r.onerror = reject;\n r.readAsDataURL(blob);\n });\n }\n\n if (typeof Buffer !== \"undefined\") {\n const ab = await blob.arrayBuffer();\n return `data:${blob.type};base64,${Buffer.from(ab).toString(\"base64\")}`;\n }\n\n throw new Error(\"Unsupported environment\");\n}\n\nexport async function base64ToBlob(dataUrl: string) {\n const response = await fetch(dataUrl);\n return await response.blob();\n}\n\nexport function compileGraffitiObjectSchema<Schema extends JSONSchema>(\n ajv: Ajv,\n schema: Schema,\n) {\n try {\n // Force the validation guard because\n // it is too big for the type checker.\n // Fortunately json-schema-to-ts is\n // well tested against ajv.\n return ajv.compile(schema) as (\n data: GraffitiObjectBase,\n ) => data is GraffitiObject<Schema>;\n } catch (error) {\n throw new GraffitiErrorInvalidSchema(\n error instanceof Error ? error.message : undefined,\n );\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAGA,SAAS,aAAa,OAA2B;AAEtD,QAAM,SAAS,KAAK,OAAO,cAAc,GAAG,KAAK,CAAC;AAElD,SAAO,OAAO,QAAQ,OAAO,GAAG,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,QAAQ,EAAE;AAC1E;AAEO,SAAS,aAAa,WAA+B;AAE1D,MAAI,SAAS,UAAU,QAAQ,MAAM,GAAG,EAAE,QAAQ,MAAM,GAAG;AAE3D,SAAO,OAAO,SAAS,MAAM,EAAG,WAAU;AAE1C,SAAO,WAAW,KAAK,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAC7D;AAEO,SAAS,aAAa,WAAmB,IAAY;AAE1D,QAAM,QAAQ,IAAI,WAAW,QAAQ;AACrC,SAAO,gBAAgB,KAAK;AAC5B,SAAO,aAAa,KAAK;AAC3B;AAEA,MAAM,oBAAoB;AAC1B,MAAM,mBAAmB;AAElB,SAAS,kBAAkB,OAAe,IAAY,QAAgB;AAC3E,SAAO,GAAG,MAAM,GAAG,mBAAmB,KAAK,CAAC,IAAI,mBAAmB,EAAE,CAAC;AACxE;AACO,SAAS,gBAAgB,OAAe,IAAY;AACzD,SAAO,kBAAkB,OAAO,IAAI,iBAAiB;AACvD;AACO,SAAS,eAAe,OAAe,IAAY;AACxD,SAAO,kBAAkB,OAAO,IAAI,gBAAgB;AACtD;AAEO,SAAS,kBAAkB,KAAa,QAAgB;AAC7D,MAAI,CAAC,IAAI,WAAW,MAAM,GAAG;AAC3B,UAAM,IAAI,sBAAsB,2BAA2B,MAAM,EAAE;AAAA,EACrE;AACA,QAAM,SAAS,IAAI,MAAM,OAAO,MAAM,EAAE,MAAM,GAAG;AACjD,MAAI,OAAO,W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