forked from haykam821/Shortcuts.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
334 lines (288 loc) · 7.56 KB
/
Copy pathindex.js
File metadata and controls
334 lines (288 loc) · 7.56 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const { URL } = require("url");
const got = require("got");
const bplist = require("bplist-parser");
/**
* The base URL to the API for getting a shortcut.
* @type {string}
*/
const baseURL = "https://www.icloud.com/shortcuts/api/records/";
/**
* The base URL for the shortcut's landing page.
* @type {string}
*/
const baseLink = "https://www.icloud.com/shortcuts/";
/**
* A single action in a shortcut.
*/
class Action {
constructor(action) {
/**
* A namespace of the action.
* @type {string}
*/
this.identifier = action.WFWorkflowActionIdentifier;
/**
* An object denoting a shortcut action.
* The properties in this object are not renamed and follow the Apple naming convention.
* @type {Object[]}
*/
this.parameters = action.WFWorkflowActionParameters;
}
}
/**
* A single question to be asked when importing a shortcut into the Shortcuts app.
*/
class ImportQuestion {
constructor(question) {
/**
* An unknown property.
* @type {string}
*/
this.parameterKey = question.ParameterKey;
/**
* The category of the question.
* @type {string}
*/
this.category = question.Category;
/**
* The action index of the question.
* @type {number}
*/
this.actionIndex = question.ActionIndex;
/**
* The question to be asked.
* @type {string}
*/
this.text = question.Text;
/**
* The default value for the question to be asked.
* @type {string}
*/
this.defaultValue = question.DefaultValue;
}
}
/**
* The shortcut file.
*/
class ShortcutMetadata {
constructor(metadata) {
const minVer = metadata.WFWorkflowMinimumClientVersion;
/**
* Details of the client used to create this shortcut.
* @property {?number} minimumVersion The minimum build number of Shortcuts usable to manage this shortcut.
* @property {string} release The release of Shortcuts that was used to manage this shortcut.
* @property {number} version The build number of Shortcuts that was used to managed this shortcut.
*/
this.client = {
minimumVersion: minVer ? parseInt(minVer) : null,
release: metadata.WFWorkflowClientRelease,
version: parseInt(metadata.WFWorkflowClientVersion),
};
const imgData = metadata.WFWorkflowIcon.WFWorkflowIconImageData;
/**
* Details of the icon of this shortcut.
* @property {number} color The color of the shortcut's icon, with transparency.
* @property {number} glyph The ID of the glyph.
* @property {?Buffer} imageData The base64 data that makes up the custom image.
*/
this.icon = {
color: metadata.WFWorkflowIcon.WFWorkflowIconStartColor,
glyph: metadata.WFWorkflowIcon.WFWorkflowIconGlyphNumber,
imageData: (imgData && imgData.length >= 0) ? Buffer.from(imgData, "base64") : null,
};
/**
* The user-specified name of the shortcut.
* @type {ImportQuestion[]}
*/
this.importQuestions = metadata.WFWorkflowImportQuestions.map(question => new ImportQuestion(question));
/**
* An unknown property.
* @type {string[]}
*/
this.types = metadata.WFWorkflowTypes;
/**
* A list of actions that the shortcut performs.
* @type {Action[]}
*/
this.actions = metadata.WFWorkflowActions.map(action => new Action(action));
/**
* A list of services that the shortcut uses.
* @type {string[]}
*/
this.inputContentItemClasses = metadata.WFWorkflowInputContentItemClasses;
}
}
/*
* A shortcut's icon.
*/
class ShortcutIcon {
constructor(color, icon, glyph) {
/**
* The color of the shortcut's icon, with transparency.
* @type {number}
*/
this.color = color.value,
/**
* The URL to download the shortcut's icon.
* @type {string}
*/
this.downloadURL = icon.value.downloadURL;
/**
* The ID of the glyph.
* @type {number}
*/
this.glyph = glyph.value;
/**
* The hexadecimal color of the shortcut's icon.
* @type {string}
*/
this.hexColor = color.value.toString(16).substring(0, 6);
}
}
/**
* A regular expression to match a single shortcut ID.
* @type {RegExp}
*/
const singleIDRegex = /^[0-9A-F]{32}$/i;
/**
* A shortcut.
*/
class Shortcut {
constructor(data, id) {
/**
* The user-specified name of the shortcut.
* @type {string}
*/
this.name = data.fields.name.value;
/**
* The long description of the shortcut.
* This does not seem to be settable by users.
* @type {string}
*/
this.longDescription = data.fields.longDescription && data.fields.longDescription.value;
/**
* The date that the shortcut was created.
* @type {Date}
*/
this.creationDate = new Date(data.created.timestamp);
/**
* The date that the shortcut was last modified on.
* @type {Date}
*/
this.modificationDate = new Date(data.modified.timestamp);
/**
* The URL to download the shortcut as a PLIST.
* @type {string}
*/
this.downloadURL = data.fields.shortcut.value.downloadURL;
/**
* Details of the icon of this shortcut.
*/
this.icon = new ShortcutIcon(data.fields.icon_color, data.fields.icon, data.fields.icon_glyph);
/**
* The full API response.
* @type {object}
*/
this.response = data;
/**
* The ID of the shortcut.
* @type {string}
*/
this.id = id;
}
/**
* Downloads and parses the shortcut's metadata.
* @returns {Promise<ShortcutMetadata>} An object with all the metadata about the shortcut.
*/
getMetadata() {
return new Promise((resolve, reject) => {
got(this.downloadURL, {
encoding: null,
}).then(response => {
const buffer = Buffer.from(response.body);
const json = bplist.parseBuffer(buffer);
const metadata = new ShortcutMetadata(json[0]);
resolve(metadata);
}).catch(reject);
});
}
/**
* Gets the shortcut's landing page URL.
* @returns {string} A URL to the landing page for the shortcut.
*/
getLink() {
return baseLink + this.id;
}
}
/**
* Gets a shortcut from its ID.
* @param {string} id The hexadecimal ID of the shortcut.
* @returns {Promise<Shortcut>} The shortcut represented by an ID.
*/
function getShortcutDetails(id) {
return new Promise((resolve, reject) => {
// Reject if the ID wasn't a string.
if (typeof id !== "string") {
const error = new TypeError("The ID must be a string.");
error.code = "SHORTCUT_ID_NOT_STRING";
return reject(error);
}
got(baseURL + id, {
json: true,
}).then(response => {
const body = response.body;
// Reject if the shortcut was not found.
if (body.error) {
const error = new Error("API error: " + error.reason);
error.code = "ICLOUD_API_ERROR";
return reject(error);
}
return resolve(new Shortcut(response.body, id));
}).catch(reject);
});
}
/**
* A list of hosts usable in a shortcut URL.
*/
const hosts = [
"www.icloud.com",
"icloud.com",
];
/**
* Gets a shortcut ID from its URL.
* @param {string} url The landing page URL of a shortcut.
* @param {boolean} allowSingle If true, allows the ID by itself.
* @returns {(boolean|string)} The ID, or false if unparsable or not a shortcut URL or shortcut ID.
*/
function idFromURL(url = "https://example.org", allowSingle = true) {
try {
const parsedUrl = new URL(url);
const path = parsedUrl.pathname.split("/").splice(1);
if (hosts.includes(parsedUrl.host) && path[0] === "shortcuts") {
return path[1];
} else {
return false;
}
} catch (error) {
if (error.code === "ERR_INVALID_URL") {
if (allowSingle) {
const matched = url.match(singleIDRegex);
return matched === null ? false : matched[0];
} else {
return false;
}
} else {
throw error;
}
}
}
module.exports = {
Action,
ImportQuestion,
Shortcut,
ShortcutMetadata,
getShortcutDetails,
idFromURL,
singleIDRegex,
bplistParser: bplist
};