-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
81 lines (81 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import fs from 'node:fs/promises';
import { ntfyMessagePriorityDefault } from './priorities.js';
/**
* The default ntfy server to use.
*/
export const DEFAULT_NTFY_SERVER = 'https://ntfy.sh';
/**
* @deprecated Use `DEFAULT_NTFY_SERVER` instead.
*/
export const DEFAULT_SERVER = DEFAULT_NTFY_SERVER;
/**
* The default priority to use when sending a message.
*/
export const DEFAULT_NTFY_PRIORITY = ntfyMessagePriorityDefault;
/**
* @deprecated Use `DEFAULT_NTFY_PRIORITY` instead.
*/
export const DEFAULT_PRIORITY = DEFAULT_NTFY_PRIORITY;
/**
* Send a message through an ntfy server.
* @param ntfyMessage The message to post.
* @returns `true` if the message was posted.
*/
export default async function publish(ntfyMessage) {
/*
* Set Server
*/
let server = ntfyMessage.server ?? DEFAULT_NTFY_SERVER;
if (!server.endsWith('/')) {
server += '/';
}
/*
* Build Headers
*/
const messageHeaders = {
Priority: ntfyMessage.priority ?? DEFAULT_NTFY_PRIORITY
};
if (ntfyMessage.title !== undefined) {
messageHeaders.Title = ntfyMessage.title;
}
if (ntfyMessage.tags !== undefined) {
messageHeaders.Tags = ntfyMessage.tags.join(',');
}
if (ntfyMessage.clickURL !== undefined) {
messageHeaders.Click = ntfyMessage.clickURL;
}
if (ntfyMessage.iconURL !== undefined) {
messageHeaders.Icon = ntfyMessage.iconURL;
}
// Attachments
let hasLocalAttachment = false;
if (ntfyMessage.fileAttachmentURL !== undefined) {
hasLocalAttachment = !(ntfyMessage.fileAttachmentURL.toLowerCase().startsWith('http://') ||
ntfyMessage.fileAttachmentURL.toLowerCase().startsWith('https://'));
if (!hasLocalAttachment) {
messageHeaders.Attach = ntfyMessage.fileAttachmentURL;
}
}
const fileData = hasLocalAttachment
? // eslint-disable-next-line security/detect-non-literal-fs-filename
await fs.readFile(ntfyMessage.fileAttachmentURL)
: undefined;
if (ntfyMessage.fileName !== undefined) {
messageHeaders.Filename = ntfyMessage.fileName;
}
// Cache
if (Object.hasOwn(ntfyMessage, 'cache') && !(ntfyMessage.cache ?? true)) {
messageHeaders.Cache = 'no';
}
/*
* Send Message
*/
const response = await fetch(server + ntfyMessage.topic, {
method: 'POST',
body: hasLocalAttachment ? fileData : ntfyMessage.message ?? '',
headers: messageHeaders
});
return response.ok;
}
export { ntfyMessagePrioritiesDefault, ntfyMessagePrioritiesHigh, ntfyMessagePrioritiesLow, ntfyMessagePrioritiesMax, ntfyMessagePrioritiesMin, ntfyMessagePriorityDefault, ntfyMessagePriorityHigh, ntfyMessagePriorityLow, ntfyMessagePriorityMax, ntfyMessagePriorityMin } from './priorities.js';
export { ntfyTagEmojis, isSupportedTagEmoji } from './emoji.js';