@nostrify/nostrify 0.48.2 → 0.49.0

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.
@@ -1,89 +0,0 @@
1
- import { z } from "zod";
2
-
3
- import { N64 } from "../utils/N64.ts";
4
- import { NIP98 } from "../NIP98.ts";
5
- import type { NostrSigner, NUploader } from "@nostrify/types";
6
-
7
- /** NostrBuildUploader options. */
8
- export interface NostrBuildUploaderOpts {
9
- /** nostr.build endpoint to use. Default: `https://nostr.build/api/v2/upload/files` */
10
- endpoint?: string;
11
- /** Signer to authenticate with NIP-98 requests. */
12
- signer: NostrSigner;
13
- /** Custom fetch implementation. */
14
- fetch?: typeof fetch;
15
- }
16
-
17
- /** Upload files to nostr.build or another compatible server. */
18
- export class NostrBuildUploader implements NUploader {
19
- private endpoint: string;
20
- private signer?: NostrSigner;
21
- private fetch: typeof fetch;
22
-
23
- constructor(opts?: NostrBuildUploaderOpts) {
24
- this.endpoint = opts?.endpoint ?? "https://nostr.build/api/v2/upload/files";
25
- this.signer = opts?.signer;
26
- this.fetch = opts?.fetch ?? globalThis.fetch.bind(globalThis);
27
- }
28
-
29
- async upload(
30
- file: File,
31
- opts?: { signal?: AbortSignal },
32
- ): Promise<[["url", string], ...string[][]]> {
33
- const formData = new FormData();
34
- formData.append("fileToUpload", file);
35
-
36
- const request = new Request(this.endpoint, {
37
- method: "POST",
38
- body: formData,
39
- signal: opts?.signal,
40
- });
41
-
42
- if (this.signer) {
43
- const t = await NIP98.template(request);
44
- const event = await this.signer.signEvent(t);
45
- request.headers.set("authorization", `Nostr ${N64.encodeEvent(event)}`);
46
- }
47
-
48
- const response = await this.fetch(request);
49
- const json = await response.json();
50
- console.log(json);
51
- const [data] = NostrBuildUploader.schema().parse(json).data;
52
-
53
- const tags: [["url", string], ...string[][]] = [
54
- ["url", data.url],
55
- ["m", data.mime],
56
- ["x", data.sha256],
57
- ["ox", data.original_sha256],
58
- ["size", data.size.toString()],
59
- ];
60
-
61
- if (data.dimensions) {
62
- tags.push(["dim", `${data.dimensions.width}x${data.dimensions.height}`]);
63
- }
64
-
65
- if (data.blurhash) {
66
- tags.push(["blurhash", data.blurhash]);
67
- }
68
-
69
- return tags;
70
- }
71
-
72
- /** nostr.build API response schema. */
73
- private static schema() {
74
- return z.object({
75
- data: z.object({
76
- url: z.string().url(),
77
- blurhash: z.string().optional().catch(undefined),
78
- sha256: z.string(),
79
- original_sha256: z.string(),
80
- mime: z.string(),
81
- size: z.number(),
82
- dimensions: z.object({
83
- width: z.number().positive(),
84
- height: z.number().positive(),
85
- }).optional().catch(undefined),
86
- }).array().min(1),
87
- });
88
- }
89
- }
@@ -1,2 +0,0 @@
1
- export { BlossomUploader } from './BlossomUploader.ts';
2
- export { NostrBuildUploader } from './NostrBuildUploader.ts';
@@ -1,36 +0,0 @@
1
- /**
2
- * Like a Circular Buffer, but the values are deduplicated.
3
- * Shares the properties of both a Circular Buffer and a Set.
4
- */
5
- export class CircularSet<T> {
6
- private set: Set<T>;
7
- private capacity: number;
8
-
9
- constructor(capacity: number) {
10
- this.set = new Set();
11
- this.capacity = capacity;
12
- }
13
-
14
- add(item: T): void {
15
- if (this.set.has(item)) {
16
- return;
17
- }
18
-
19
- if (this.set.size >= this.capacity) {
20
- const oldest = this.set.values().next().value;
21
- if (oldest) {
22
- this.set.delete(oldest);
23
- }
24
- }
25
-
26
- this.set.add(item);
27
- }
28
-
29
- has(item: T): boolean {
30
- return this.set.has(item);
31
- }
32
-
33
- [Symbol.iterator](): Iterator<T> {
34
- return this.set.values();
35
- }
36
- }
@@ -1,66 +0,0 @@
1
- /**
2
- * Infinite async generator. Iterates messages pushed to it until closed.
3
- * Only one consumer is expected to use a Machina instance at a time.
4
- *
5
- * @example
6
- * ```ts
7
- * // Create the Machina instance
8
- * const machina = new Machina<string>();
9
- *
10
- * // Async generator loop
11
- * async function getMessages() {
12
- * for await (const msg of machina.stream()) {
13
- * console.log(msg);
14
- * }
15
- * }
16
- *
17
- * // Start the generator
18
- * getMessages();
19
- *
20
- * // Push messages to it
21
- * machina.push('hello!');
22
- * machina.push('whats up?');
23
- * machina.push('greetings');
24
- * ```
25
- */
26
- export class Machina<T> implements AsyncIterable<T> {
27
- #queue: T[] = [];
28
- #resolve: (() => void) | undefined;
29
- #aborted = false;
30
-
31
- constructor(signal?: AbortSignal) {
32
- if (signal?.aborted) {
33
- this.abort();
34
- } else {
35
- signal?.addEventListener('abort', () => this.abort(), { once: true });
36
- }
37
- }
38
-
39
- /** Get messages as an AsyncIterable. */
40
- async *[Symbol.asyncIterator](): AsyncIterator<T> {
41
- while (!this.#aborted) {
42
- if (this.#queue.length) {
43
- yield this.#queue.shift() as T;
44
- continue;
45
- }
46
-
47
- await new Promise<void>((_resolve) => {
48
- this.#resolve = _resolve;
49
- });
50
- }
51
-
52
- throw new DOMException('The signal has been aborted', 'AbortError');
53
- }
54
-
55
- /** Push a message into the Machina instance, making it available to the consumer of `stream()`. */
56
- push(data: T): void {
57
- this.#queue.push(data);
58
- this.#resolve?.();
59
- }
60
-
61
- /** Stops streaming and throws an error to the consumer. */
62
- private abort(): void {
63
- this.#aborted = true;
64
- this.#resolve?.();
65
- }
66
- }
package/dist/utils/N64.ts DELETED
@@ -1,23 +0,0 @@
1
- import type { NostrEvent } from "@nostrify/types";
2
- import { fromBase64, toBase64 } from "@smithy/util-base64";
3
-
4
- import { NSchema as n } from "../NSchema.ts";
5
-
6
- /** Nostr base64 helper utilities. */
7
- export class N64 {
8
- /** Encode an event as a base64 string. */
9
- static encodeEvent(event: NostrEvent): string {
10
- return toBase64(JSON.stringify(event));
11
- }
12
-
13
- /** Decode an event from a base64 string. Validates the event's structure but does not verify its signature. */
14
- static decodeEvent(base64: string): NostrEvent {
15
- const bytes = fromBase64(base64);
16
- const text = new TextDecoder().decode(bytes);
17
-
18
- return n
19
- .json()
20
- .pipe(n.event())
21
- .parse(text);
22
- }
23
- }
package/dist/utils/mod.ts DELETED
@@ -1,2 +0,0 @@
1
- export { Machina } from './Machina.ts';
2
- export { N64 } from './N64.ts';