@postlab/google-workspace-mcp 1.0.2 → 1.0.3

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.
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+
3
+ const { SaxesParser } = require("saxes");
4
+ const DOMException = require("../../living/generated/DOMException");
5
+
6
+ const { createElement } = require("../../living/helpers/create-element");
7
+
8
+ const DocumentFragment = require("../../living/generated/DocumentFragment");
9
+ const DocumentType = require("../../living/generated/DocumentType");
10
+ const CDATASection = require("../../living/generated/CDATASection");
11
+ const Comment = require("../../living/generated/Comment");
12
+ const ProcessingInstruction = require("../../living/generated/ProcessingInstruction");
13
+ const Text = require("../../living/generated/Text");
14
+
15
+ const attributes = require("../../living/attributes");
16
+ const { HTML_NS } = require("../../living/helpers/namespaces");
17
+
18
+ const HTML5_DOCTYPE = /<!doctype html>/i;
19
+ const PUBLIC_DOCTYPE = /<!doctype\s+([^\s]+)\s+public\s+"([^"]+)"\s+"([^"]+)"/i;
20
+ const SYSTEM_DOCTYPE = /<!doctype\s+([^\s]+)\s+system\s+"([^"]+)"/i;
21
+ const CUSTOM_NAME_DOCTYPE = /<!doctype\s+([^\s>]+)/i;
22
+
23
+ function parseDocType(globalObject, ownerDocument, html) {
24
+ if (HTML5_DOCTYPE.test(html)) {
25
+ return createDocumentType(globalObject, ownerDocument, "html", "", "");
26
+ }
27
+
28
+ const publicPieces = PUBLIC_DOCTYPE.exec(html);
29
+ if (publicPieces) {
30
+ return createDocumentType(globalObject, ownerDocument, publicPieces[1], publicPieces[2], publicPieces[3]);
31
+ }
32
+
33
+ const systemPieces = SYSTEM_DOCTYPE.exec(html);
34
+ if (systemPieces) {
35
+ return createDocumentType(globalObject, ownerDocument, systemPieces[1], "", systemPieces[2]);
36
+ }
37
+
38
+ const namePiece = CUSTOM_NAME_DOCTYPE.exec(html)[1] || "html";
39
+ return createDocumentType(globalObject, ownerDocument, namePiece, "", "");
40
+ }
41
+
42
+ function createDocumentType(globalObject, ownerDocument, name, publicId, systemId) {
43
+ return DocumentType.createImpl(globalObject, [], { ownerDocument, name, publicId, systemId });
44
+ }
45
+
46
+ function isHTMLTemplateElement(element) {
47
+ return element.tagName === "template" && element.namespaceURI === HTML_NS;
48
+ }
49
+
50
+
51
+ function createParser(rootNode, globalObject, saxesOptions) {
52
+ const parser = new SaxesParser({
53
+ ...saxesOptions,
54
+ // Browsers always have namespace support.
55
+ xmlns: true,
56
+ // We force the parser to treat all documents (even documents declaring themselves to be XML 1.1 documents) as XML
57
+ // 1.0 documents. See https://github.com/jsdom/jsdom/issues/2677 for a discussion of the stakes.
58
+ defaultXMLVersion: "1.0",
59
+ forceXMLVersion: true
60
+ });
61
+ const openStack = [rootNode];
62
+
63
+ function getOwnerDocument() {
64
+ const currentElement = openStack[openStack.length - 1];
65
+
66
+ return isHTMLTemplateElement(currentElement) ?
67
+ currentElement._templateContents._ownerDocument :
68
+ currentElement._ownerDocument;
69
+ }
70
+
71
+ function appendChild(child) {
72
+ const parentElement = openStack[openStack.length - 1];
73
+
74
+ if (isHTMLTemplateElement(parentElement)) {
75
+ parentElement._templateContents._insert(child, null);
76
+ } else {
77
+ parentElement._insert(child, null);
78
+ }
79
+ }
80
+
81
+ parser.on("text", saxesOptions.fragment ?
82
+ // In a fragment, all text events produced by saxes must result in a text
83
+ // node.
84
+ data => {
85
+ const ownerDocument = getOwnerDocument();
86
+ appendChild(Text.createImpl(globalObject, [], { data, ownerDocument }));
87
+ } :
88
+ // When parsing a whole document, we must ignore those text nodes that are
89
+ // produced outside the root element. Saxes produces events for them,
90
+ // but DOM trees do not record text outside the root element.
91
+ data => {
92
+ if (openStack.length > 1) {
93
+ const ownerDocument = getOwnerDocument();
94
+ appendChild(Text.createImpl(globalObject, [], { data, ownerDocument }));
95
+ }
96
+ });
97
+
98
+ parser.on("cdata", data => {
99
+ const ownerDocument = getOwnerDocument();
100
+ appendChild(CDATASection.createImpl(globalObject, [], { data, ownerDocument }));
101
+ });
102
+
103
+ parser.on("opentag", tag => {
104
+ const { local: tagLocal, attributes: tagAttributes } = tag;
105
+
106
+ const ownerDocument = getOwnerDocument();
107
+ const tagNamespace = tag.uri === "" ? null : tag.uri;
108
+ const tagPrefix = tag.prefix === "" ? null : tag.prefix;
109
+ const isValue = tagAttributes.is === undefined ? null : tagAttributes.is.value;
110
+
111
+ const elem = createElement(ownerDocument, tagLocal, tagNamespace, tagPrefix, isValue, true);
112
+
113
+ // We mark a script element as "parser-inserted", which prevents it from
114
+ // being immediately executed.
115
+ if (tagLocal === "script" && tagNamespace === HTML_NS) {
116
+ elem._parserInserted = true;
117
+ }
118
+
119
+ for (const key of Object.keys(tagAttributes)) {
120
+ const { prefix, local, uri, value } = tagAttributes[key];
121
+ attributes.setAttributeValue(elem, local, value, prefix === "" ? null : prefix, uri === "" ? null : uri);
122
+ }
123
+
124
+ appendChild(elem);
125
+ openStack.push(elem);
126
+ });
127
+
128
+ parser.on("closetag", () => {
129
+ const elem = openStack.pop();
130
+ // Once a script is populated, we can execute it.
131
+ if (elem.localName === "script" && elem.namespaceURI === HTML_NS) {
132
+ elem._eval();
133
+ }
134
+ });
135
+
136
+ parser.on("comment", data => {
137
+ const ownerDocument = getOwnerDocument();
138
+ appendChild(Comment.createImpl(globalObject, [], { data, ownerDocument }));
139
+ });
140
+
141
+ parser.on("processinginstruction", ({ target, body }) => {
142
+ const ownerDocument = getOwnerDocument();
143
+ appendChild(ProcessingInstruction.createImpl(globalObject, [], { target, data: body, ownerDocument }));
144
+ });
145
+
146
+ parser.on("doctype", dt => {
147
+ const ownerDocument = getOwnerDocument();
148
+ appendChild(parseDocType(globalObject, ownerDocument, `<!doctype ${dt}>`));
149
+
150
+ const entityMatcher = /<!ENTITY ([^ ]+) "([^"]+)">/g;
151
+ let result;
152
+ while ((result = entityMatcher.exec(dt))) {
153
+ const [, name, value] = result;
154
+ if (!(name in parser.ENTITIES)) {
155
+ parser.ENTITIES[name] = value;
156
+ }
157
+ }
158
+ });
159
+
160
+ parser.on("error", err => {
161
+ throw DOMException.create(globalObject, [err.message, "SyntaxError"]);
162
+ });
163
+
164
+ return parser;
165
+ }
166
+
167
+ function parseFragment(markup, contextElement) {
168
+ const { _globalObject, _ownerDocument } = contextElement;
169
+
170
+ const fragment = DocumentFragment.createImpl(_globalObject, [], { ownerDocument: _ownerDocument });
171
+
172
+ // Only parseFragment needs resolvePrefix per the saxes documentation:
173
+ // https://github.com/lddubeau/saxes#parsing-xml-fragments
174
+ const parser = createParser(fragment, _globalObject, {
175
+ fragment: true,
176
+ resolvePrefix(prefix) {
177
+ // saxes wants undefined as the return value if the prefix is not defined, not null.
178
+ return contextElement.lookupNamespaceURI(prefix) || undefined;
179
+ }
180
+ });
181
+
182
+ parser.write(markup).close();
183
+
184
+ return fragment;
185
+ }
186
+
187
+ function parseIntoDocument(markup, ownerDocument) {
188
+ const { _globalObject } = ownerDocument;
189
+
190
+ const parser = createParser(ownerDocument, _globalObject, {
191
+ fileName: ownerDocument.location && ownerDocument.location.href
192
+ });
193
+
194
+ parser.write(markup).close();
195
+
196
+ return ownerDocument;
197
+ }
198
+
199
+ module.exports = {
200
+ parseFragment,
201
+ parseIntoDocument
202
+ };
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ class QueueItem {
4
+ constructor(onLoad, onError, dependentItem) {
5
+ this.onLoad = onLoad;
6
+ this.onError = onError;
7
+ this.data = null;
8
+ this.error = null;
9
+ this.dependentItem = dependentItem;
10
+ }
11
+ }
12
+
13
+ /**
14
+ * AsyncResourceQueue is the queue in charge of run the async scripts
15
+ * and notify when they finish.
16
+ */
17
+ module.exports = class AsyncResourceQueue {
18
+ constructor() {
19
+ this.items = new Set();
20
+ this.dependentItems = new Set();
21
+ }
22
+
23
+ count() {
24
+ return this.items.size + this.dependentItems.size;
25
+ }
26
+
27
+ _notify() {
28
+ if (this._listener) {
29
+ this._listener();
30
+ }
31
+ }
32
+
33
+ _check(item) {
34
+ let promise;
35
+
36
+ if (item.onError && item.error) {
37
+ promise = item.onError(item.error);
38
+ } else if (item.onLoad && item.data) {
39
+ promise = item.onLoad(item.data);
40
+ }
41
+
42
+ promise
43
+ .then(() => {
44
+ this.items.delete(item);
45
+ this.dependentItems.delete(item);
46
+
47
+ if (this.count() === 0) {
48
+ this._notify();
49
+ }
50
+ });
51
+ }
52
+
53
+ setListener(listener) {
54
+ this._listener = listener;
55
+ }
56
+
57
+ push(request, onLoad, onError, dependentItem) {
58
+ const q = this;
59
+
60
+ const item = new QueueItem(onLoad, onError, dependentItem);
61
+
62
+ q.items.add(item);
63
+
64
+ return request
65
+ .then(data => {
66
+ item.data = data;
67
+
68
+ if (dependentItem && !dependentItem.finished) {
69
+ q.dependentItems.add(item);
70
+ return q.items.delete(item);
71
+ }
72
+
73
+ if (onLoad) {
74
+ return q._check(item);
75
+ }
76
+
77
+ q.items.delete(item);
78
+
79
+ if (q.count() === 0) {
80
+ q._notify();
81
+ }
82
+
83
+ return null;
84
+ })
85
+ .catch(err => {
86
+ item.error = err;
87
+
88
+ if (dependentItem && !dependentItem.finished) {
89
+ q.dependentItems.add(item);
90
+ return q.items.delete(item);
91
+ }
92
+
93
+ if (onError) {
94
+ return q._check(item);
95
+ }
96
+
97
+ q.items.delete(item);
98
+
99
+ if (q.count() === 0) {
100
+ q._notify();
101
+ }
102
+
103
+ return null;
104
+ });
105
+ }
106
+
107
+ notifyItem(syncItem) {
108
+ for (const item of this.dependentItems) {
109
+ if (item.dependentItem === syncItem) {
110
+ this._check(item);
111
+ }
112
+ }
113
+ }
114
+ };
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const ResourceLoader = require("./resource-loader.js");
3
+
4
+ module.exports = class NoOpResourceLoader extends ResourceLoader {
5
+ fetch() {
6
+ return null;
7
+ }
8
+ };
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ const idlUtils = require("../../living/generated/utils");
3
+ const { fireAnEvent } = require("../../living/helpers/events");
4
+
5
+ module.exports = class PerDocumentResourceLoader {
6
+ constructor(document) {
7
+ this._document = document;
8
+ this._defaultEncoding = document._encoding;
9
+ this._resourceLoader = document._defaultView ? document._defaultView._resourceLoader : null;
10
+ this._requestManager = document._requestManager;
11
+ this._queue = document._queue;
12
+ this._deferQueue = document._deferQueue;
13
+ this._asyncQueue = document._asyncQueue;
14
+ }
15
+
16
+ fetch(url, { element, onLoad, onError }) {
17
+ const request = this._resourceLoader.fetch(url, {
18
+ cookieJar: this._document._cookieJar,
19
+ element: idlUtils.wrapperForImpl(element),
20
+ referrer: this._document.URL
21
+ });
22
+
23
+ if (request === null) {
24
+ return null;
25
+ }
26
+
27
+ this._requestManager.add(request);
28
+
29
+ const onErrorWrapped = cause => {
30
+ this._requestManager.remove(request);
31
+
32
+ if (onError) {
33
+ onError(cause);
34
+ }
35
+
36
+ fireAnEvent("error", element);
37
+
38
+ const jsomError = new Error(`Could not load ${element.localName}: "${url}"`, { cause });
39
+ jsomError.type = "resource-loading";
40
+ jsomError.url = url;
41
+
42
+ this._document._defaultView._virtualConsole.emit("jsdomError", jsomError);
43
+
44
+ return Promise.resolve();
45
+ };
46
+
47
+ const onLoadWrapped = data => {
48
+ this._requestManager.remove(request);
49
+
50
+ this._addCookies(url, request.response ? request.response.headers : {});
51
+
52
+ try {
53
+ const result = onLoad ? onLoad(data) : undefined;
54
+
55
+ return Promise.resolve(result)
56
+ .then(() => {
57
+ fireAnEvent("load", element);
58
+
59
+ return Promise.resolve();
60
+ })
61
+ .catch(err => {
62
+ return onErrorWrapped(err);
63
+ });
64
+ } catch (err) {
65
+ return onErrorWrapped(err);
66
+ }
67
+ };
68
+
69
+ if (element.localName === "script" && element.hasAttributeNS(null, "async")) {
70
+ this._asyncQueue.push(request, onLoadWrapped, onErrorWrapped, this._queue.getLastScript());
71
+ } else if (
72
+ element.localName === "script" &&
73
+ element.hasAttributeNS(null, "defer") &&
74
+ this._document.readyState !== "interactive") {
75
+ this._deferQueue.push(request, onLoadWrapped, onErrorWrapped, false, element);
76
+ } else {
77
+ this._queue.push(request, onLoadWrapped, onErrorWrapped, false, element);
78
+ }
79
+
80
+ return request;
81
+ }
82
+
83
+ _addCookies(url, headers) {
84
+ let cookies = headers["set-cookie"];
85
+
86
+ if (!cookies) {
87
+ return;
88
+ }
89
+
90
+ if (!Array.isArray(cookies)) {
91
+ cookies = [cookies];
92
+ }
93
+
94
+ cookies.forEach(cookie => {
95
+ this._document._cookieJar.setCookieSync(cookie, url, { http: true, ignoreError: true });
96
+ });
97
+ }
98
+ };
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Manage all the request and it is able to abort
5
+ * all pending request.
6
+ */
7
+ module.exports = class RequestManager {
8
+ constructor() {
9
+ this.openedRequests = [];
10
+ }
11
+
12
+ add(req) {
13
+ this.openedRequests.push(req);
14
+ }
15
+
16
+ remove(req) {
17
+ const idx = this.openedRequests.indexOf(req);
18
+ if (idx !== -1) {
19
+ this.openedRequests.splice(idx, 1);
20
+ }
21
+ }
22
+
23
+ close() {
24
+ for (const openedRequest of this.openedRequests) {
25
+ openedRequest.abort();
26
+ }
27
+ this.openedRequests = [];
28
+ }
29
+
30
+ size() {
31
+ return this.openedRequests.length;
32
+ }
33
+ };
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ const fs = require("fs");
3
+ const { fileURLToPath } = require("url");
4
+ const { parseURL } = require("whatwg-url");
5
+ const dataURLFromRecord = require("data-urls").fromURLRecord;
6
+ const packageVersion = require("../../../../package.json").version;
7
+ const agentFactory = require("../../living/helpers/agent-factory");
8
+ const Request = require("../../living/helpers/http-request");
9
+
10
+ const IS_BROWSER = Object.prototype.toString.call(process) !== "[object process]";
11
+
12
+ module.exports = class ResourceLoader {
13
+ constructor({
14
+ strictSSL = true,
15
+ proxy = undefined,
16
+ userAgent = `Mozilla/5.0 (${process.platform || "unknown OS"}) AppleWebKit/537.36 ` +
17
+ `(KHTML, like Gecko) jsdom/${packageVersion}`
18
+ } = {}) {
19
+ this._strictSSL = strictSSL;
20
+ this._proxy = proxy;
21
+ this._userAgent = userAgent;
22
+ }
23
+
24
+ _readDataURL(urlRecord) {
25
+ const dataURL = dataURLFromRecord(urlRecord);
26
+ let timeoutId;
27
+ const promise = new Promise(resolve => {
28
+ timeoutId = setTimeout(resolve, 0, Buffer.from(dataURL.body));
29
+ });
30
+ promise.abort = () => {
31
+ if (timeoutId !== undefined) {
32
+ clearTimeout(timeoutId);
33
+ }
34
+ };
35
+ return promise;
36
+ }
37
+
38
+ _readFile(filePath) {
39
+ let readableStream, abort; // Native Promises doesn't have an "abort" method.
40
+
41
+ // Creating a promise for two reason:
42
+ // 1. fetch always return a promise.
43
+ // 2. We need to add an abort handler.
44
+ const promise = new Promise((resolve, reject) => {
45
+ readableStream = fs.createReadStream(filePath);
46
+ let data = Buffer.alloc(0);
47
+
48
+ abort = reject;
49
+
50
+ readableStream.on("error", reject);
51
+
52
+ readableStream.on("data", chunk => {
53
+ data = Buffer.concat([data, chunk]);
54
+ });
55
+
56
+ readableStream.on("end", () => {
57
+ resolve(data);
58
+ });
59
+ });
60
+
61
+ promise.abort = () => {
62
+ readableStream.destroy();
63
+ const error = new Error("request canceled by user");
64
+ error.isAbortError = true;
65
+ abort(error);
66
+ };
67
+
68
+ return promise;
69
+ }
70
+
71
+ fetch(urlString, { accept, cookieJar, referrer } = {}) {
72
+ const url = parseURL(urlString);
73
+
74
+ if (!url) {
75
+ return Promise.reject(new Error(`Tried to fetch invalid URL ${urlString}`));
76
+ }
77
+
78
+ switch (url.scheme) {
79
+ case "data": {
80
+ return this._readDataURL(url);
81
+ }
82
+
83
+ case "http":
84
+ case "https": {
85
+ const agents = agentFactory(this._proxy, this._strictSSL);
86
+ const headers = {
87
+ "User-Agent": this._userAgent,
88
+ "Accept-Language": "en",
89
+ "Accept-Encoding": "gzip",
90
+ "Accept": accept || "*/*"
91
+ };
92
+ if (referrer && !IS_BROWSER) {
93
+ headers.Referer = referrer;
94
+ }
95
+ const requestClient = new Request(
96
+ urlString,
97
+ { followRedirects: true, cookieJar, agents },
98
+ { headers }
99
+ );
100
+ const promise = new Promise((resolve, reject) => {
101
+ const accumulated = [];
102
+ requestClient.once("response", res => {
103
+ promise.response = res;
104
+ const { statusCode } = res;
105
+ // TODO This deviates from the spec when it comes to
106
+ // loading resources such as images
107
+ if (statusCode < 200 || statusCode > 299) {
108
+ requestClient.abort();
109
+ reject(new Error(`Resource was not loaded. Status: ${statusCode}`));
110
+ }
111
+ });
112
+ requestClient.on("data", chunk => {
113
+ accumulated.push(chunk);
114
+ });
115
+ requestClient.on("end", () => resolve(Buffer.concat(accumulated)));
116
+ requestClient.on("error", reject);
117
+ });
118
+ // The method fromURL in lib/api.js crashes without the following four
119
+ // properties defined on the Promise instance, causing the test suite to halt
120
+ requestClient.on("end", () => {
121
+ promise.href = requestClient.currentURL;
122
+ });
123
+ promise.abort = requestClient.abort.bind(requestClient);
124
+ promise.getHeader = name => headers[name] || requestClient.getHeader(name);
125
+ requestClient.end();
126
+ return promise;
127
+ }
128
+
129
+ case "file": {
130
+ try {
131
+ return this._readFile(fileURLToPath(urlString));
132
+ } catch (e) {
133
+ return Promise.reject(e);
134
+ }
135
+ }
136
+
137
+ default: {
138
+ return Promise.reject(new Error(`Tried to fetch URL ${urlString} with invalid scheme ${url.scheme}`));
139
+ }
140
+ }
141
+ }
142
+ };