@graffiti-garden/implementation-local 0.6.4 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/dist/browser/ajv-IY2ZY7VT.js +9 -0
- package/dist/browser/ajv-IY2ZY7VT.js.map +7 -0
- package/dist/browser/{chunk-KNUPPOQC.js → chunk-GE6AZATH.js} +2 -2
- package/dist/browser/{chunk-KNUPPOQC.js.map → chunk-GE6AZATH.js.map} +1 -1
- package/dist/browser/{index-browser.es-G37SKL53.js → index-browser.es-UXYPGJ2M.js} +2 -2
- package/dist/browser/{index-browser.es-G37SKL53.js.map → index-browser.es-UXYPGJ2M.js.map} +1 -1
- package/dist/browser/index.js +11 -2
- package/dist/browser/index.js.map +4 -4
- package/dist/cjs/identity.js +112 -0
- package/dist/cjs/identity.js.map +7 -0
- package/dist/cjs/index.js +43 -22
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/media.js +111 -0
- package/dist/cjs/media.js.map +7 -0
- package/dist/cjs/objects.js +307 -0
- package/dist/cjs/objects.js.map +7 -0
- package/dist/cjs/tests.spec.js +1 -2
- package/dist/cjs/tests.spec.js.map +2 -2
- package/dist/cjs/utilities.js +68 -43
- package/dist/cjs/utilities.js.map +2 -2
- package/dist/esm/identity.js +92 -0
- package/dist/esm/identity.js.map +7 -0
- package/dist/esm/index.js +43 -24
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/media.js +91 -0
- package/dist/esm/media.js.map +7 -0
- package/dist/esm/objects.js +285 -0
- package/dist/esm/objects.js.map +7 -0
- package/dist/esm/tests.spec.js +2 -4
- package/dist/esm/tests.spec.js.map +2 -2
- package/dist/esm/utilities.js +69 -48
- package/dist/esm/utilities.js.map +2 -2
- package/dist/{session-manager.d.ts → identity.d.ts} +7 -5
- package/dist/identity.d.ts.map +1 -0
- package/dist/index.d.ts +15 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/media.d.ts +9 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/objects.d.ts +63 -0
- package/dist/objects.d.ts.map +1 -0
- package/dist/utilities.d.ts +19 -8
- package/dist/utilities.d.ts.map +1 -1
- package/package.json +31 -19
- package/src/identity.ts +131 -0
- package/src/index.ts +44 -29
- package/src/media.ts +106 -0
- package/src/objects.ts +432 -0
- package/src/tests.spec.ts +2 -4
- package/src/utilities.ts +67 -87
- package/dist/browser/ajv-6AI3HK2A.js +0 -9
- package/dist/browser/ajv-6AI3HK2A.js.map +0 -7
- package/dist/browser/fast-json-patch-ZE7SZEYK.js +0 -19
- package/dist/browser/fast-json-patch-ZE7SZEYK.js.map +0 -7
- package/dist/cjs/database.js +0 -626
- package/dist/cjs/database.js.map +0 -7
- package/dist/cjs/session-manager.js +0 -107
- package/dist/cjs/session-manager.js.map +0 -7
- package/dist/database.d.ts +0 -106
- package/dist/database.d.ts.map +0 -1
- package/dist/esm/database.js +0 -608
- package/dist/esm/database.js.map +0 -7
- package/dist/esm/session-manager.js +0 -87
- package/dist/esm/session-manager.js.map +0 -7
- package/dist/session-manager.d.ts.map +0 -1
- package/src/database.ts +0 -921
- package/src/session-manager.ts +0 -123
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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 throw new GraffitiErrorForbidden(\n \"Cannot continue a cursor started by another actor\",\n );\n }\n return this.discoverContinue<{}>(\n [channels, schema, session],\n continueParams,\n );\n } else {\n throw new GraffitiErrorNotFound(\"Cursor not found\");\n }\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,iBAQO;AACP,uBAA+D;AAuCxD,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,MAA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;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,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AACA,aAAO,KAAK;AAAA,QACV,CAAC,UAAU,QAAQ,OAAO;AAAA,QAC1B;AAAA,MACF;AAAA,IACF,OAAO;AACL,YAAM,IAAI,iCAAsB,kBAAkB;AAAA,IACpD;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["session"]
|
|
7
|
+
}
|
package/dist/cjs/tests.spec.js
CHANGED
|
@@ -6,6 +6,5 @@ const useSession1 = () => ({ actor: "someone" });
|
|
|
6
6
|
const useSession2 = () => ({ actor: "someoneelse" });
|
|
7
7
|
(0, import_tests.graffitiCRUDTests)(useGraffiti, useSession1, useSession2);
|
|
8
8
|
(0, import_tests.graffitiDiscoverTests)(useGraffiti, useSession1, useSession2);
|
|
9
|
-
(0, import_tests.
|
|
10
|
-
(0, import_tests.graffitiChannelStatsTests)(useGraffiti, useSession1, useSession2);
|
|
9
|
+
(0, import_tests.graffitiMediaTests)(useGraffiti, useSession1, useSession2);
|
|
11
10
|
//# sourceMappingURL=tests.spec.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/tests.spec.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n graffitiCRUDTests,\n graffitiDiscoverTests,\n
|
|
5
|
-
"mappings": ";AAAA,
|
|
4
|
+
"sourcesContent": ["import {\n graffitiCRUDTests,\n graffitiDiscoverTests,\n graffitiMediaTests,\n} from \"@graffiti-garden/api/tests\";\nimport { GraffitiLocal } from \"./index\";\n\nconst useGraffiti = () => new GraffitiLocal();\nconst useSession1 = () => ({ actor: \"someone\" });\nconst useSession2 = () => ({ actor: \"someoneelse\" });\n\ngraffitiCRUDTests(useGraffiti, useSession1, useSession2);\ngraffitiDiscoverTests(useGraffiti, useSession1, useSession2);\ngraffitiMediaTests(useGraffiti, useSession1, useSession2);\n"],
|
|
5
|
+
"mappings": ";AAAA,mBAIO;AACP,mBAA8B;AAE9B,MAAM,cAAc,MAAM,IAAI,2BAAc;AAC5C,MAAM,cAAc,OAAO,EAAE,OAAO,UAAU;AAC9C,MAAM,cAAc,OAAO,EAAE,OAAO,cAAc;AAAA,IAElD,gCAAkB,aAAa,aAAa,WAAW;AAAA,IACvD,oCAAsB,aAAa,aAAa,WAAW;AAAA,IAC3D,iCAAmB,aAAa,aAAa,WAAW;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/utilities.js
CHANGED
|
@@ -18,59 +18,84 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var utilities_exports = {};
|
|
20
20
|
__export(utilities_exports, {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
base64ToBlob: () => base64ToBlob,
|
|
22
|
+
blobToBase64: () => blobToBase64,
|
|
23
|
+
decodeBase64: () => decodeBase64,
|
|
24
|
+
decodeGraffitiUrl: () => decodeGraffitiUrl,
|
|
25
|
+
decodeMediaUrl: () => decodeMediaUrl,
|
|
26
|
+
decodeObjectUrl: () => decodeObjectUrl,
|
|
27
|
+
encodeBase64: () => encodeBase64,
|
|
28
|
+
encodeGraffitiUrl: () => encodeGraffitiUrl,
|
|
29
|
+
encodeMediaUrl: () => encodeMediaUrl,
|
|
30
|
+
encodeObjectUrl: () => encodeObjectUrl,
|
|
31
|
+
randomBase64: () => randomBase64
|
|
27
32
|
});
|
|
28
33
|
module.exports = __toCommonJS(utilities_exports);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
34
|
+
function encodeBase64(bytes) {
|
|
35
|
+
const base64 = btoa(String.fromCodePoint(...bytes));
|
|
36
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, "");
|
|
37
|
+
}
|
|
38
|
+
function decodeBase64(base64Url) {
|
|
39
|
+
let base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
|
|
40
|
+
while (base64.length % 4 !== 0) base64 += "=";
|
|
41
|
+
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
32
42
|
}
|
|
33
|
-
function randomBase64(numBytes =
|
|
43
|
+
function randomBase64(numBytes = 32) {
|
|
34
44
|
const bytes = new Uint8Array(numBytes);
|
|
35
45
|
crypto.getRandomValues(bytes);
|
|
36
|
-
|
|
37
|
-
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/\=+$/, "");
|
|
46
|
+
return encodeBase64(bytes);
|
|
38
47
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
object[prop] = apply(object[prop], ops, true, false).newDocument;
|
|
44
|
-
} catch (e) {
|
|
45
|
-
if (typeof e === "object" && e && "name" in e && typeof e.name === "string" && "message" in e && typeof e.message === "string") {
|
|
46
|
-
if (e.name === "TEST_OPERATION_FAILED") {
|
|
47
|
-
throw new import_api.GraffitiErrorPatchTestFailed(e.message);
|
|
48
|
-
} else {
|
|
49
|
-
throw new import_api.GraffitiErrorPatchError(e.name + ": " + e.message);
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
throw e;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
48
|
+
const OBJECT_URL_PREFIX = "graffiti:object:";
|
|
49
|
+
const MEDIA_URL_PREFIX = "graffiti:media:";
|
|
50
|
+
function encodeGraffitiUrl(actor, id, prefix) {
|
|
51
|
+
return `${prefix}${encodeURIComponent(actor)}:${encodeURIComponent(id)}`;
|
|
55
52
|
}
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
function encodeObjectUrl(actor, id) {
|
|
54
|
+
return encodeGraffitiUrl(actor, id, OBJECT_URL_PREFIX);
|
|
55
|
+
}
|
|
56
|
+
function encodeMediaUrl(actor, id) {
|
|
57
|
+
return encodeGraffitiUrl(actor, id, MEDIA_URL_PREFIX);
|
|
58
|
+
}
|
|
59
|
+
function decodeGraffitiUrl(url, prefix) {
|
|
60
|
+
if (!url.startsWith(prefix)) {
|
|
61
|
+
throw new Error(`URL does not start with ${prefix}`);
|
|
62
|
+
}
|
|
63
|
+
const slices = url.slice(prefix.length).split(":");
|
|
64
|
+
if (slices.length !== 2) {
|
|
65
|
+
throw new Error("URL has too many colon-seperated parts");
|
|
63
66
|
}
|
|
67
|
+
const [actor, id] = slices.map(decodeURIComponent);
|
|
68
|
+
return { actor, id };
|
|
64
69
|
}
|
|
65
|
-
function
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
function decodeObjectUrl(url) {
|
|
71
|
+
return decodeGraffitiUrl(url, OBJECT_URL_PREFIX);
|
|
72
|
+
}
|
|
73
|
+
function decodeMediaUrl(url) {
|
|
74
|
+
return decodeGraffitiUrl(url, MEDIA_URL_PREFIX);
|
|
75
|
+
}
|
|
76
|
+
async function blobToBase64(blob) {
|
|
77
|
+
if (typeof FileReader !== "undefined") {
|
|
78
|
+
return new Promise((resolve, reject) => {
|
|
79
|
+
const r = new FileReader();
|
|
80
|
+
r.onload = () => {
|
|
81
|
+
if (typeof r.result === "string") {
|
|
82
|
+
resolve(r.result);
|
|
83
|
+
} else {
|
|
84
|
+
reject(new Error("Unexpected result type"));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
r.onerror = reject;
|
|
88
|
+
r.readAsDataURL(blob);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (typeof Buffer !== "undefined") {
|
|
92
|
+
const ab = await blob.arrayBuffer();
|
|
93
|
+
return `data:${blob.type};base64,${Buffer.from(ab).toString("base64")}`;
|
|
71
94
|
}
|
|
95
|
+
throw new Error("Unsupported environment");
|
|
72
96
|
}
|
|
73
|
-
function
|
|
74
|
-
|
|
97
|
+
async function base64ToBlob(dataUrl) {
|
|
98
|
+
const response = await fetch(dataUrl);
|
|
99
|
+
return await response.blob();
|
|
75
100
|
}
|
|
76
101
|
//# sourceMappingURL=utilities.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utilities.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA
|
|
4
|
+
"sourcesContent": ["export 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 Error(`URL does not start with ${prefix}`);\n }\n const slices = url.slice(prefix.length).split(\":\");\n if (slices.length !== 2) {\n throw new Error(\"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"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,SAAS,aAAa,OAA2B;AAEtD,QAAM,SAAS,KAAK,O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,SAAS,eAAe,KAAa;AAC1C,SAAO,kBAAkB,KAAK,gBAAgB;AAChD;AAEA,eAAsB,aAAa,MAA6B;AAC9D,MAAI,OAAO,eAAe,aAAa;AACrC,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,YAAM,IAAI,IAAI,WAAW;AACzB,QAAE,SAAS,MAAM;AACf,YAAI,OAAO,EAAE,WAAW,UAAU;AAChC,kBAAQ,EAAE,MAAM;AAAA,QAClB,OAAO;AACL,iBAAO,IAAI,MAAM,wBAAwB,CAAC;AAAA,QAC5C;AAAA,MACF;AACA,QAAE,UAAU;AACZ,QAAE,cAAc,IAAI;AAAA,IACtB,CAAC;AAAA,EACH;AAEA,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,KAAK,MAAM,KAAK,YAAY;AAClC,WAAO,QAAQ,KAAK,IAAI,WAAW,OAAO,KAAK,EAAE,EAAE,SAAS,QAAQ,CAAC;AAAA,EACvE;AAEA,QAAM,IAAI,MAAM,yBAAyB;AAC3C;AAEA,eAAsB,aAAa,SAAiB;AAClD,QAAM,WAAW,MAAM,MAAM,OAAO;AACpC,SAAO,MAAM,SAAS,KAAK;AAC7B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { decodeBase64, encodeBase64 } from "./utilities";
|
|
2
|
+
const DID_LOCAL_PREFIX = "did:local:";
|
|
3
|
+
class GraffitiLocalIdentity {
|
|
4
|
+
sessionEvents = new EventTarget();
|
|
5
|
+
handleToActor = async (handle) => {
|
|
6
|
+
const bytes = new TextEncoder().encode(handle);
|
|
7
|
+
const base64 = encodeBase64(bytes);
|
|
8
|
+
return `${DID_LOCAL_PREFIX}${base64}`;
|
|
9
|
+
};
|
|
10
|
+
actorToHandle = async (actor) => {
|
|
11
|
+
if (!actor.startsWith(DID_LOCAL_PREFIX)) {
|
|
12
|
+
throw new Error(`actor must start with ${DID_LOCAL_PREFIX}`);
|
|
13
|
+
}
|
|
14
|
+
const base64 = actor.slice(DID_LOCAL_PREFIX.length);
|
|
15
|
+
const bytes = decodeBase64(base64);
|
|
16
|
+
return new TextDecoder().decode(bytes);
|
|
17
|
+
};
|
|
18
|
+
constructor() {
|
|
19
|
+
const sessionRestorer = async () => {
|
|
20
|
+
await Promise.resolve();
|
|
21
|
+
for (const handle of this.getLoggedInHandles()) {
|
|
22
|
+
const event2 = new CustomEvent("login", {
|
|
23
|
+
detail: { session: { actor: await this.handleToActor(handle) } }
|
|
24
|
+
});
|
|
25
|
+
this.sessionEvents.dispatchEvent(event2);
|
|
26
|
+
}
|
|
27
|
+
const event = new CustomEvent(
|
|
28
|
+
"initialized",
|
|
29
|
+
{ detail: {} }
|
|
30
|
+
);
|
|
31
|
+
this.sessionEvents.dispatchEvent(event);
|
|
32
|
+
};
|
|
33
|
+
sessionRestorer();
|
|
34
|
+
}
|
|
35
|
+
loggedInHandles = [];
|
|
36
|
+
getLoggedInHandles() {
|
|
37
|
+
if (typeof window !== "undefined") {
|
|
38
|
+
const handlesString = window.localStorage.getItem("graffiti-handles");
|
|
39
|
+
return handlesString ? handlesString.split(",").map(decodeURIComponent) : [];
|
|
40
|
+
} else {
|
|
41
|
+
return this.loggedInHandles;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
setLoggedInHandles(handles) {
|
|
45
|
+
if (typeof window !== "undefined") {
|
|
46
|
+
window.localStorage.setItem("graffiti-handles", handles.join(","));
|
|
47
|
+
} else {
|
|
48
|
+
this.loggedInHandles = handles;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
login = async (actor) => {
|
|
52
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
53
|
+
let handle = actor ? await this.actorToHandle(actor) : void 0;
|
|
54
|
+
if (typeof window !== "undefined") {
|
|
55
|
+
const response = window.prompt("Choose a username to log in.", handle);
|
|
56
|
+
handle = response ?? void 0;
|
|
57
|
+
}
|
|
58
|
+
if (!handle) {
|
|
59
|
+
const detail = {
|
|
60
|
+
error: new Error("No handle provided to login")
|
|
61
|
+
};
|
|
62
|
+
const event = new CustomEvent("login", { detail });
|
|
63
|
+
this.sessionEvents.dispatchEvent(event);
|
|
64
|
+
} else {
|
|
65
|
+
const existingHandles = this.getLoggedInHandles();
|
|
66
|
+
if (!existingHandles.includes(handle)) {
|
|
67
|
+
this.setLoggedInHandles([...existingHandles, handle]);
|
|
68
|
+
}
|
|
69
|
+
window.location.reload();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
logout = async (session) => {
|
|
73
|
+
const handle = await this.actorToHandle(session.actor);
|
|
74
|
+
const existingHandles = this.getLoggedInHandles();
|
|
75
|
+
const exists = existingHandles.includes(handle);
|
|
76
|
+
if (exists) {
|
|
77
|
+
this.setLoggedInHandles(existingHandles.filter((h) => h !== handle));
|
|
78
|
+
}
|
|
79
|
+
const detail = exists ? {
|
|
80
|
+
actor: session.actor
|
|
81
|
+
} : {
|
|
82
|
+
actor: session.actor,
|
|
83
|
+
error: new Error("Not logged in with that actor")
|
|
84
|
+
};
|
|
85
|
+
const event = new CustomEvent("logout", { detail });
|
|
86
|
+
this.sessionEvents.dispatchEvent(event);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
GraffitiLocalIdentity
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/identity.ts"],
|
|
4
|
+
"sourcesContent": ["import type {\n Graffiti,\n GraffitiLoginEvent,\n GraffitiLogoutEvent,\n GraffitiSessionInitializedEvent,\n} from \"@graffiti-garden/api\";\nimport { decodeBase64, encodeBase64 } from \"./utilities\";\n\nconst DID_LOCAL_PREFIX = \"did:local:\";\n\n/**\n * A class that implements the login methods\n * of the [Graffiti API]() for use in the browser.\n * It is completely insecure and should only be used\n * for testing and demonstrations.\n *\n * It uses `localStorage` to store login state and\n * window prompts rather than an oauth flow for log in.\n * It can be used in node.js but will not persist\n * login state and a proposed username must be provided.\n */\nexport class GraffitiLocalIdentity {\n sessionEvents: Graffiti[\"sessionEvents\"] = new EventTarget();\n\n handleToActor: Graffiti[\"handleToActor\"] = async (handle: string) => {\n const bytes = new TextEncoder().encode(handle);\n const base64 = encodeBase64(bytes);\n return `${DID_LOCAL_PREFIX}${base64}`;\n };\n\n actorToHandle: Graffiti[\"actorToHandle\"] = async (actor: string) => {\n if (!actor.startsWith(DID_LOCAL_PREFIX)) {\n throw new Error(`actor must start with ${DID_LOCAL_PREFIX}`);\n }\n const base64 = actor.slice(DID_LOCAL_PREFIX.length);\n const bytes = decodeBase64(base64);\n return new TextDecoder().decode(bytes);\n };\n\n constructor() {\n // Look for any existing sessions\n const sessionRestorer = async () => {\n // Allow listeners to be added first\n await Promise.resolve();\n\n // Restore previous sessions\n for (const handle of this.getLoggedInHandles()) {\n const event: GraffitiLoginEvent = new CustomEvent(\"login\", {\n detail: { session: { actor: await this.handleToActor(handle) } },\n });\n this.sessionEvents.dispatchEvent(event);\n }\n\n const event: GraffitiSessionInitializedEvent = new CustomEvent(\n \"initialized\",\n { detail: {} },\n );\n this.sessionEvents.dispatchEvent(event);\n };\n sessionRestorer();\n }\n\n loggedInHandles: string[] = [];\n\n protected getLoggedInHandles(): string[] {\n if (typeof window !== \"undefined\") {\n const handlesString = window.localStorage.getItem(\"graffiti-handles\");\n return handlesString\n ? handlesString.split(\",\").map(decodeURIComponent)\n : [];\n } else {\n return this.loggedInHandles;\n }\n }\n\n protected setLoggedInHandles(handles: string[]) {\n if (typeof window !== \"undefined\") {\n window.localStorage.setItem(\"graffiti-handles\", handles.join(\",\"));\n } else {\n this.loggedInHandles = handles;\n }\n }\n\n login: Graffiti[\"login\"] = async (actor) => {\n // Wait a tick for the browser to update the UI\n await new Promise((resolve) => setTimeout(resolve, 0));\n\n let handle = actor ? await this.actorToHandle(actor) : undefined;\n\n if (typeof window !== \"undefined\") {\n const response = window.prompt(\"Choose a username to log in.\", handle);\n handle = response ?? undefined;\n }\n\n if (!handle) {\n const detail: GraffitiLoginEvent[\"detail\"] = {\n error: new Error(\"No handle provided to login\"),\n };\n const event: GraffitiLoginEvent = new CustomEvent(\"login\", { detail });\n this.sessionEvents.dispatchEvent(event);\n } else {\n const existingHandles = this.getLoggedInHandles();\n if (!existingHandles.includes(handle)) {\n this.setLoggedInHandles([...existingHandles, handle]);\n }\n // Refresh the page to simulate oauth\n window.location.reload();\n }\n };\n\n logout: Graffiti[\"logout\"] = async (session) => {\n const handle = await this.actorToHandle(session.actor);\n const existingHandles = this.getLoggedInHandles();\n const exists = existingHandles.includes(handle);\n if (exists) {\n this.setLoggedInHandles(existingHandles.filter((h) => h !== handle));\n }\n\n const detail: GraffitiLogoutEvent[\"detail\"] = exists\n ? {\n actor: session.actor,\n }\n : {\n actor: session.actor,\n error: new Error(\"Not logged in with that actor\"),\n };\n\n const event: GraffitiLogoutEvent = new CustomEvent(\"logout\", { detail });\n this.sessionEvents.dispatchEvent(event);\n };\n}\n"],
|
|
5
|
+
"mappings": "AAMA,SAAS,cAAc,oBAAoB;AAE3C,MAAM,mBAAmB;AAalB,MAAM,sBAAsB;AAAA,EACjC,gBAA2C,IAAI,YAAY;AAAA,EAE3D,gBAA2C,OAAO,WAAmB;AACnE,UAAM,QAAQ,IAAI,YAAY,EAAE,OAAO,MAAM;AAC7C,UAAM,SAAS,aAAa,KAAK;AACjC,WAAO,GAAG,gBAAgB,GAAG,MAAM;AAAA,EACrC;AAAA,EAEA,gBAA2C,OAAO,UAAkB;AAClE,QAAI,CAAC,MAAM,WAAW,gBAAgB,GAAG;AACvC,YAAM,IAAI,MAAM,yBAAyB,gBAAgB,EAAE;AAAA,IAC7D;AACA,UAAM,SAAS,MAAM,MAAM,iBAAiB,MAAM;AAClD,UAAM,QAAQ,aAAa,MAAM;AACjC,WAAO,IAAI,YAAY,EAAE,OAAO,KAAK;AAAA,EACvC;AAAA,EAEA,cAAc;AAEZ,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,EAAE,OAAO,CAAC;AACrE,WAAK,cAAc,cAAc,KAAK;AAAA,IACxC,OAAO;AACL,YAAM,kBAAkB,KAAK,mBAAmB;AAChD,UAAI,CAAC,gBAAgB,SAAS,MAAM,GAAG;AACrC,aAAK,mBAAmB,CAAC,GAAG,iBAAiB,MAAM,CAAC;AAAA,MACtD;AAEA,aAAO,SAAS,OAAO;AAAA,IACzB;AAAA,EACF;AAAA,EAEA,SAA6B,OAAO,YAAY;AAC9C,UAAM,SAAS,MAAM,KAAK,cAAc,QAAQ,KAAK;AACrD,UAAM,kBAAkB,KAAK,mBAAmB;AAChD,UAAM,SAAS,gBAAgB,SAAS,MAAM;AAC9C,QAAI,QAAQ;AACV,WAAK,mBAAmB,gBAAgB,OAAO,CAAC,MAAM,MAAM,MAAM,CAAC;AAAA,IACrE;AAEA,UAAM,SAAwC,SAC1C;AAAA,MACE,OAAO,QAAQ;AAAA,IACjB,IACA;AAAA,MACE,OAAO,QAAQ;AAAA,MACf,OAAO,IAAI,MAAM,+BAA+B;AAAA,IAClD;AAEJ,UAAM,QAA6B,IAAI,YAAY,UAAU,EAAE,OAAO,CAAC;AACvE,SAAK,cAAc,cAAc,KAAK;AAAA,EACxC;AACF;",
|
|
6
|
+
"names": ["event"]
|
|
7
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,32 +1,51 @@
|
|
|
1
1
|
import { Graffiti } from "@graffiti-garden/api";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
import { GraffitiLocalIdentity } from "./identity";
|
|
3
|
+
import { GraffitiLocalObjects } from "./objects";
|
|
4
|
+
import { GraffitiLocalMedia } from "./media";
|
|
5
|
+
class GraffitiLocal {
|
|
6
|
+
graffitiLocalIdentity = new GraffitiLocalIdentity();
|
|
7
|
+
login = this.graffitiLocalIdentity.login.bind(this.graffitiLocalIdentity);
|
|
8
|
+
logout = this.graffitiLocalIdentity.logout.bind(this.graffitiLocalIdentity);
|
|
9
|
+
handleToActor = this.graffitiLocalIdentity.handleToActor.bind(
|
|
10
|
+
this.graffitiLocalIdentity
|
|
11
|
+
);
|
|
12
|
+
actorToHandle = this.graffitiLocalIdentity.actorToHandle.bind(
|
|
13
|
+
this.graffitiLocalIdentity
|
|
14
|
+
);
|
|
15
|
+
sessionEvents = this.graffitiLocalIdentity.sessionEvents;
|
|
16
|
+
graffitiLocalObjects;
|
|
17
|
+
post;
|
|
12
18
|
get;
|
|
13
|
-
patch;
|
|
14
19
|
delete;
|
|
15
20
|
discover;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
continueDiscover;
|
|
22
|
+
graffitiLocalMedia;
|
|
23
|
+
postMedia;
|
|
24
|
+
getMedia;
|
|
25
|
+
deleteMedia;
|
|
19
26
|
constructor(options) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
this.discover =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.
|
|
27
|
+
this.graffitiLocalObjects = new GraffitiLocalObjects(options);
|
|
28
|
+
this.post = this.graffitiLocalObjects.post.bind(this.graffitiLocalObjects);
|
|
29
|
+
this.get = this.graffitiLocalObjects.get.bind(this.graffitiLocalObjects);
|
|
30
|
+
this.delete = this.graffitiLocalObjects.delete.bind(
|
|
31
|
+
this.graffitiLocalObjects
|
|
32
|
+
);
|
|
33
|
+
this.discover = this.graffitiLocalObjects.discover.bind(
|
|
34
|
+
this.graffitiLocalObjects
|
|
35
|
+
);
|
|
36
|
+
this.continueDiscover = this.graffitiLocalObjects.continueDiscover.bind(
|
|
37
|
+
this.graffitiLocalObjects
|
|
38
|
+
);
|
|
39
|
+
this.graffitiLocalMedia = new GraffitiLocalMedia(this.graffitiLocalObjects);
|
|
40
|
+
this.postMedia = this.graffitiLocalMedia.postMedia.bind(
|
|
41
|
+
this.graffitiLocalMedia
|
|
42
|
+
);
|
|
43
|
+
this.getMedia = this.graffitiLocalMedia.getMedia.bind(
|
|
44
|
+
this.graffitiLocalMedia
|
|
45
|
+
);
|
|
46
|
+
this.deleteMedia = this.graffitiLocalMedia.deleteMedia.bind(
|
|
47
|
+
this.graffitiLocalMedia
|
|
48
|
+
);
|
|
30
49
|
}
|
|
31
50
|
}
|
|
32
51
|
export {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["import { Graffiti, type GraffitiSession } from \"@graffiti-garden/api\";\nimport {
|
|
5
|
-
"mappings": "AAAA,SAAS,gBAAsC;AAC/C,SAAS,
|
|
4
|
+
"sourcesContent": ["import { Graffiti, type GraffitiSession } from \"@graffiti-garden/api\";\nimport { GraffitiLocalIdentity } from \"./identity\";\nimport { GraffitiLocalObjects, type GraffitiLocalOptions } from \"./objects\";\nimport { GraffitiLocalMedia } from \"./media\";\n\nexport type { GraffitiLocalOptions };\n\n/**\n * A local implementation of the [Graffiti API](https://api.graffiti.garden/classes/Graffiti.html)\n * based on [PouchDB](https://pouchdb.com/). PouchDb will automatically persist data in a local\n * database, either in the browser or in Node.js.\n * It can also be configured to work with an external [CouchDB](https://couchdb.apache.org/) server,\n * although using it with a remote server will not be secure.\n */\nexport class GraffitiLocal implements Graffiti {\n protected graffitiLocalIdentity = new GraffitiLocalIdentity();\n login = this.graffitiLocalIdentity.login.bind(this.graffitiLocalIdentity);\n logout = this.graffitiLocalIdentity.logout.bind(this.graffitiLocalIdentity);\n handleToActor = this.graffitiLocalIdentity.handleToActor.bind(\n this.graffitiLocalIdentity,\n );\n actorToHandle = this.graffitiLocalIdentity.actorToHandle.bind(\n this.graffitiLocalIdentity,\n );\n sessionEvents = this.graffitiLocalIdentity.sessionEvents;\n\n protected graffitiLocalObjects: GraffitiLocalObjects;\n post: Graffiti[\"post\"];\n get: Graffiti[\"get\"];\n delete: Graffiti[\"delete\"];\n discover: Graffiti[\"discover\"];\n continueDiscover: Graffiti[\"continueDiscover\"];\n\n protected graffitiLocalMedia: GraffitiLocalMedia;\n postMedia: Graffiti[\"postMedia\"];\n getMedia: Graffiti[\"getMedia\"];\n deleteMedia: Graffiti[\"deleteMedia\"];\n\n constructor(options?: GraffitiLocalOptions) {\n this.graffitiLocalObjects = new GraffitiLocalObjects(options);\n this.post = this.graffitiLocalObjects.post.bind(this.graffitiLocalObjects);\n this.get = this.graffitiLocalObjects.get.bind(this.graffitiLocalObjects);\n this.delete = this.graffitiLocalObjects.delete.bind(\n this.graffitiLocalObjects,\n );\n this.discover = this.graffitiLocalObjects.discover.bind(\n this.graffitiLocalObjects,\n );\n this.continueDiscover = this.graffitiLocalObjects.continueDiscover.bind(\n this.graffitiLocalObjects,\n );\n\n this.graffitiLocalMedia = new GraffitiLocalMedia(this.graffitiLocalObjects);\n this.postMedia = this.graffitiLocalMedia.postMedia.bind(\n this.graffitiLocalMedia,\n );\n this.getMedia = this.graffitiLocalMedia.getMedia.bind(\n this.graffitiLocalMedia,\n );\n this.deleteMedia = this.graffitiLocalMedia.deleteMedia.bind(\n this.graffitiLocalMedia,\n );\n }\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,gBAAsC;AAC/C,SAAS,6BAA6B;AACtC,SAAS,4BAAuD;AAChE,SAAS,0BAA0B;AAW5B,MAAM,cAAkC;AAAA,EACnC,wBAAwB,IAAI,sBAAsB;AAAA,EAC5D,QAAQ,KAAK,sBAAsB,MAAM,KAAK,KAAK,qBAAqB;AAAA,EACxE,SAAS,KAAK,sBAAsB,OAAO,KAAK,KAAK,qBAAqB;AAAA,EAC1E,gBAAgB,KAAK,sBAAsB,cAAc;AAAA,IACvD,KAAK;AAAA,EACP;AAAA,EACA,gBAAgB,KAAK,sBAAsB,cAAc;AAAA,IACvD,KAAK;AAAA,EACP;AAAA,EACA,gBAAgB,KAAK,sBAAsB;AAAA,EAEjC;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,SAAgC;AAC1C,SAAK,uBAAuB,IAAI,qBAAqB,OAAO;AAC5D,SAAK,OAAO,KAAK,qBAAqB,KAAK,KAAK,KAAK,oBAAoB;AACzE,SAAK,MAAM,KAAK,qBAAqB,IAAI,KAAK,KAAK,oBAAoB;AACvE,SAAK,SAAS,KAAK,qBAAqB,OAAO;AAAA,MAC7C,KAAK;AAAA,IACP;AACA,SAAK,WAAW,KAAK,qBAAqB,SAAS;AAAA,MACjD,KAAK;AAAA,IACP;AACA,SAAK,mBAAmB,KAAK,qBAAqB,iBAAiB;AAAA,MACjE,KAAK;AAAA,IACP;AAEA,SAAK,qBAAqB,IAAI,mBAAmB,KAAK,oBAAoB;AAC1E,SAAK,YAAY,KAAK,mBAAmB,UAAU;AAAA,MACjD,KAAK;AAAA,IACP;AACA,SAAK,WAAW,KAAK,mBAAmB,SAAS;AAAA,MAC/C,KAAK;AAAA,IACP;AACA,SAAK,cAAc,KAAK,mBAAmB,YAAY;AAAA,MACrD,KAAK;AAAA,IACP;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GraffitiErrorNotAcceptable,
|
|
3
|
+
GraffitiErrorTooLarge
|
|
4
|
+
} from "@graffiti-garden/api";
|
|
5
|
+
import {
|
|
6
|
+
decodeObjectUrl,
|
|
7
|
+
encodeObjectUrl,
|
|
8
|
+
decodeMediaUrl,
|
|
9
|
+
encodeMediaUrl,
|
|
10
|
+
blobToBase64,
|
|
11
|
+
base64ToBlob
|
|
12
|
+
} from "./utilities";
|
|
13
|
+
import Negotiator from "negotiator";
|
|
14
|
+
const MEDIA_OBJECT_SCHEMA = {
|
|
15
|
+
properties: {
|
|
16
|
+
value: {
|
|
17
|
+
properties: {
|
|
18
|
+
dataBase64: { type: "string" },
|
|
19
|
+
type: { type: "string" },
|
|
20
|
+
size: { type: "number" }
|
|
21
|
+
},
|
|
22
|
+
required: ["dataBase64", "type", "size"]
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
class GraffitiLocalMedia {
|
|
27
|
+
db;
|
|
28
|
+
constructor(db) {
|
|
29
|
+
this.db = db;
|
|
30
|
+
}
|
|
31
|
+
postMedia = async (...args) => {
|
|
32
|
+
const [media, session] = args;
|
|
33
|
+
const dataBase64 = await blobToBase64(media.data);
|
|
34
|
+
const type = media.data.type;
|
|
35
|
+
const { url } = await this.db.post(
|
|
36
|
+
{
|
|
37
|
+
value: {
|
|
38
|
+
dataBase64,
|
|
39
|
+
type,
|
|
40
|
+
size: media.data.size
|
|
41
|
+
},
|
|
42
|
+
channels: [],
|
|
43
|
+
allowed: media.allowed
|
|
44
|
+
},
|
|
45
|
+
session
|
|
46
|
+
);
|
|
47
|
+
const { actor, id } = decodeObjectUrl(url);
|
|
48
|
+
return encodeMediaUrl(actor, id);
|
|
49
|
+
};
|
|
50
|
+
getMedia = async (...args) => {
|
|
51
|
+
const [mediaUrl, requirements, session] = args;
|
|
52
|
+
const { actor, id } = decodeMediaUrl(mediaUrl);
|
|
53
|
+
const objectUrl = encodeObjectUrl(actor, id);
|
|
54
|
+
const object = await this.db.get(
|
|
55
|
+
objectUrl,
|
|
56
|
+
MEDIA_OBJECT_SCHEMA,
|
|
57
|
+
session
|
|
58
|
+
);
|
|
59
|
+
const { dataBase64, type, size } = object.value;
|
|
60
|
+
if (requirements?.maxBytes && size > requirements.maxBytes) {
|
|
61
|
+
throw new GraffitiErrorTooLarge("File size exceeds limit");
|
|
62
|
+
}
|
|
63
|
+
if (requirements?.accept) {
|
|
64
|
+
const negotiator = new Negotiator({
|
|
65
|
+
headers: { accept: requirements.accept }
|
|
66
|
+
});
|
|
67
|
+
if (negotiator.mediaType([type]) !== type) {
|
|
68
|
+
throw new GraffitiErrorNotAcceptable(`Unsupported media type, ${type}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const data = await base64ToBlob(dataBase64);
|
|
72
|
+
if (data.size !== size || data.type !== type) {
|
|
73
|
+
throw new Error("Invalid data");
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
data,
|
|
77
|
+
actor: object.actor,
|
|
78
|
+
allowed: object.allowed
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
deleteMedia = async (...args) => {
|
|
82
|
+
const [mediaUrl, session] = args;
|
|
83
|
+
const { actor, id } = decodeMediaUrl(mediaUrl);
|
|
84
|
+
const objectUrl = encodeObjectUrl(actor, id);
|
|
85
|
+
await this.db.delete(objectUrl, session);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export {
|
|
89
|
+
GraffitiLocalMedia
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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\";\nimport Negotiator from \"negotiator\";\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, requirements, 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 (requirements?.maxBytes && size > requirements.maxBytes) {\n throw new GraffitiErrorTooLarge(\"File size exceeds limit\");\n }\n\n // Make sure it adheres to requirements.accept\n if (requirements?.accept) {\n const negotiator = new Negotiator({\n headers: { accept: requirements.accept },\n });\n if (negotiator.mediaType([type]) !== type) {\n throw new GraffitiErrorNotAcceptable(`Unsupported media type, ${type}`);\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,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,OAAO,gBAAgB;AAEvB,MAAM,sBAAsB;AAAA,EAC1B,YAAY;A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