@openfn/language-browserless 1.0.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.
@@ -0,0 +1,24 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "properties": {
4
+ "baseUrl": {
5
+ "title": "Base URL",
6
+ "type":["string", "null"],
7
+ "description": "The Browserless base URL (e.g. https://production-sfo.browserless.io)",
8
+ "format": "uri",
9
+ "minLength": 1,
10
+ "examples": ["https://production-sfo.browserless.io"]
11
+ },
12
+ "token":{
13
+ "title": "API Token",
14
+ "type":"string",
15
+ "description": "Browserless API token",
16
+ "writeOnly": true,
17
+ "minLength": 1,
18
+ "examples": ["your-browserless-token"]
19
+ }
20
+ },
21
+ "type": "object",
22
+ "additionalProperties": true,
23
+ "required": ["baseUrl", "token"]
24
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,188 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.js
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ as: () => import_language_common2.as,
23
+ combine: () => import_language_common2.combine,
24
+ createPDF: () => createPDF,
25
+ cursor: () => import_language_common2.cursor,
26
+ dataPath: () => import_language_common2.dataPath,
27
+ dataValue: () => import_language_common2.dataValue,
28
+ dateFns: () => import_language_common2.dateFns,
29
+ default: () => src_default,
30
+ each: () => import_language_common2.each,
31
+ field: () => import_language_common2.field,
32
+ fields: () => import_language_common2.fields,
33
+ fn: () => import_language_common2.fn,
34
+ fnIf: () => import_language_common2.fnIf,
35
+ group: () => import_language_common2.group,
36
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
37
+ map: () => import_language_common2.map,
38
+ merge: () => import_language_common2.merge,
39
+ request: () => request2,
40
+ scrubEmojis: () => import_language_common2.scrubEmojis,
41
+ source: () => import_language_common2.source,
42
+ sourceValue: () => import_language_common2.sourceValue,
43
+ util: () => import_language_common2.util
44
+ });
45
+ module.exports = __toCommonJS(src_exports);
46
+
47
+ // src/Adaptor.js
48
+ var Adaptor_exports = {};
49
+ __export(Adaptor_exports, {
50
+ as: () => import_language_common2.as,
51
+ combine: () => import_language_common2.combine,
52
+ createPDF: () => createPDF,
53
+ cursor: () => import_language_common2.cursor,
54
+ dataPath: () => import_language_common2.dataPath,
55
+ dataValue: () => import_language_common2.dataValue,
56
+ dateFns: () => import_language_common2.dateFns,
57
+ each: () => import_language_common2.each,
58
+ field: () => import_language_common2.field,
59
+ fields: () => import_language_common2.fields,
60
+ fn: () => import_language_common2.fn,
61
+ fnIf: () => import_language_common2.fnIf,
62
+ group: () => import_language_common2.group,
63
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
64
+ map: () => import_language_common2.map,
65
+ merge: () => import_language_common2.merge,
66
+ request: () => request2,
67
+ scrubEmojis: () => import_language_common2.scrubEmojis,
68
+ source: () => import_language_common2.source,
69
+ sourceValue: () => import_language_common2.sourceValue,
70
+ util: () => import_language_common2.util
71
+ });
72
+ var import_util3 = require("@openfn/language-common/util");
73
+
74
+ // src/Utils.js
75
+ var import_language_common = require("@openfn/language-common");
76
+ var import_util = require("@openfn/language-common/util");
77
+ var request = async (configuration = {}, method, path, options) => {
78
+ var _a;
79
+ const { baseUrl = "https://production-sfo.browserless.io", token } = configuration;
80
+ const opts = {
81
+ parseAs: "json",
82
+ errors: { 404: "Page not found" },
83
+ ...options,
84
+ headers: {
85
+ "content-type": "application/json",
86
+ ...(options == null ? void 0 : options.headers) || {}
87
+ },
88
+ query: { ...(options == null ? void 0 : options.query) || {}, ...token ? { token } : {} }
89
+ };
90
+ const url = new URL(path, baseUrl).toString();
91
+ let response = await (0, import_util.request)(method, url, opts);
92
+ try {
93
+ const contentType = ((_a = response == null ? void 0 : response.headers) == null ? void 0 : _a["content-type"]) || "";
94
+ const isPDF = /application\/(pdf|octet-stream)/i.test(contentType);
95
+ if (isPDF && response.body) {
96
+ let buf;
97
+ if (typeof response.body === "string")
98
+ buf = Buffer.from(response.body, "binary");
99
+ else if (Buffer.isBuffer(response.body))
100
+ buf = response.body;
101
+ else if (response.body instanceof ArrayBuffer)
102
+ buf = Buffer.from(response.body);
103
+ else if (ArrayBuffer.isView(response.body))
104
+ buf = Buffer.from(response.body.buffer);
105
+ if (buf) {
106
+ response.body = buf.toString("base64");
107
+ }
108
+ } else if (response.body && Buffer.isBuffer(response.body)) {
109
+ if (/application\/json/i.test(contentType) || opts.parseAs === "json") {
110
+ try {
111
+ response.body = JSON.parse(response.body.toString("utf8"));
112
+ } catch {
113
+ }
114
+ }
115
+ }
116
+ } catch {
117
+ }
118
+ return response;
119
+ };
120
+
121
+ // src/http.js
122
+ var import_util2 = require("@openfn/language-common/util");
123
+ function request2(method, url, options) {
124
+ return async (state) => {
125
+ const [resolvedMethod, resolvedUrl, resolvedOptions] = (0, import_util2.expandReferences)(
126
+ state,
127
+ method,
128
+ url,
129
+ options
130
+ );
131
+ const response = await request(state.configuration, resolvedMethod, resolvedUrl, resolvedOptions);
132
+ const { body, ...responseWithoutBody } = response;
133
+ return {
134
+ ...state,
135
+ response: responseWithoutBody,
136
+ data: body
137
+ };
138
+ };
139
+ }
140
+
141
+ // src/Adaptor.js
142
+ var import_language_common2 = require("@openfn/language-common");
143
+ function createPDF(input, options) {
144
+ return async (state) => {
145
+ var _a;
146
+ const maybeBody = typeof input === "string" ? input.startsWith("http") ? { url: input } : { html: input } : input || {};
147
+ const [resolvedInput, resolvedOptions] = (0, import_util3.expandReferences)(state, maybeBody, options);
148
+ const response = await request(
149
+ state.configuration,
150
+ "POST",
151
+ "pdf",
152
+ { body: resolvedInput, ...resolvedOptions || {}, parseAs: "buffer" }
153
+ );
154
+ const pdfData = ((_a = response.body) == null ? void 0 : _a.pdf) ?? response.body;
155
+ return {
156
+ ...state,
157
+ data: pdfData,
158
+ response: { ...response, body: void 0 }
159
+ };
160
+ };
161
+ }
162
+
163
+ // src/index.js
164
+ var src_default = Adaptor_exports;
165
+ // Annotate the CommonJS export names for ESM import in node:
166
+ 0 && (module.exports = {
167
+ as,
168
+ combine,
169
+ createPDF,
170
+ cursor,
171
+ dataPath,
172
+ dataValue,
173
+ dateFns,
174
+ each,
175
+ field,
176
+ fields,
177
+ fn,
178
+ fnIf,
179
+ group,
180
+ lastReferenceValue,
181
+ map,
182
+ merge,
183
+ request,
184
+ scrubEmojis,
185
+ source,
186
+ sourceValue,
187
+ util
188
+ });
package/dist/index.js ADDED
@@ -0,0 +1,168 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/Adaptor.js
8
+ var Adaptor_exports = {};
9
+ __export(Adaptor_exports, {
10
+ as: () => as,
11
+ combine: () => combine,
12
+ createPDF: () => createPDF,
13
+ cursor: () => cursor,
14
+ dataPath: () => dataPath,
15
+ dataValue: () => dataValue,
16
+ dateFns: () => dateFns,
17
+ each: () => each,
18
+ field: () => field,
19
+ fields: () => fields,
20
+ fn: () => fn,
21
+ fnIf: () => fnIf,
22
+ group: () => group,
23
+ lastReferenceValue: () => lastReferenceValue,
24
+ map: () => map,
25
+ merge: () => merge,
26
+ request: () => request2,
27
+ scrubEmojis: () => scrubEmojis,
28
+ source: () => source,
29
+ sourceValue: () => sourceValue,
30
+ util: () => util
31
+ });
32
+ import { expandReferences as expandReferences2 } from "@openfn/language-common/util";
33
+
34
+ // src/Utils.js
35
+ import { composeNextState } from "@openfn/language-common";
36
+ import { request as commonRequest } from "@openfn/language-common/util";
37
+ var request = async (configuration = {}, method, path, options) => {
38
+ var _a;
39
+ const { baseUrl = "https://production-sfo.browserless.io", token } = configuration;
40
+ const opts = {
41
+ parseAs: "json",
42
+ errors: { 404: "Page not found" },
43
+ ...options,
44
+ headers: {
45
+ "content-type": "application/json",
46
+ ...(options == null ? void 0 : options.headers) || {}
47
+ },
48
+ query: { ...(options == null ? void 0 : options.query) || {}, ...token ? { token } : {} }
49
+ };
50
+ const url = new URL(path, baseUrl).toString();
51
+ let response = await commonRequest(method, url, opts);
52
+ try {
53
+ const contentType = ((_a = response == null ? void 0 : response.headers) == null ? void 0 : _a["content-type"]) || "";
54
+ const isPDF = /application\/(pdf|octet-stream)/i.test(contentType);
55
+ if (isPDF && response.body) {
56
+ let buf;
57
+ if (typeof response.body === "string")
58
+ buf = Buffer.from(response.body, "binary");
59
+ else if (Buffer.isBuffer(response.body))
60
+ buf = response.body;
61
+ else if (response.body instanceof ArrayBuffer)
62
+ buf = Buffer.from(response.body);
63
+ else if (ArrayBuffer.isView(response.body))
64
+ buf = Buffer.from(response.body.buffer);
65
+ if (buf) {
66
+ response.body = buf.toString("base64");
67
+ }
68
+ } else if (response.body && Buffer.isBuffer(response.body)) {
69
+ if (/application\/json/i.test(contentType) || opts.parseAs === "json") {
70
+ try {
71
+ response.body = JSON.parse(response.body.toString("utf8"));
72
+ } catch {
73
+ }
74
+ }
75
+ }
76
+ } catch {
77
+ }
78
+ return response;
79
+ };
80
+
81
+ // src/http.js
82
+ import { expandReferences } from "@openfn/language-common/util";
83
+ function request2(method, url, options) {
84
+ return async (state) => {
85
+ const [resolvedMethod, resolvedUrl, resolvedOptions] = expandReferences(
86
+ state,
87
+ method,
88
+ url,
89
+ options
90
+ );
91
+ const response = await request(state.configuration, resolvedMethod, resolvedUrl, resolvedOptions);
92
+ const { body, ...responseWithoutBody } = response;
93
+ return {
94
+ ...state,
95
+ response: responseWithoutBody,
96
+ data: body
97
+ };
98
+ };
99
+ }
100
+
101
+ // src/Adaptor.js
102
+ import {
103
+ as,
104
+ combine,
105
+ cursor,
106
+ dataPath,
107
+ dataValue,
108
+ dateFns,
109
+ each,
110
+ field,
111
+ fields,
112
+ fn,
113
+ fnIf,
114
+ group,
115
+ lastReferenceValue,
116
+ map,
117
+ merge,
118
+ scrubEmojis,
119
+ source,
120
+ sourceValue,
121
+ util
122
+ } from "@openfn/language-common";
123
+ function createPDF(input, options) {
124
+ return async (state) => {
125
+ var _a;
126
+ const maybeBody = typeof input === "string" ? input.startsWith("http") ? { url: input } : { html: input } : input || {};
127
+ const [resolvedInput, resolvedOptions] = expandReferences2(state, maybeBody, options);
128
+ const response = await request(
129
+ state.configuration,
130
+ "POST",
131
+ "pdf",
132
+ { body: resolvedInput, ...resolvedOptions || {}, parseAs: "buffer" }
133
+ );
134
+ const pdfData = ((_a = response.body) == null ? void 0 : _a.pdf) ?? response.body;
135
+ return {
136
+ ...state,
137
+ data: pdfData,
138
+ response: { ...response, body: void 0 }
139
+ };
140
+ };
141
+ }
142
+
143
+ // src/index.js
144
+ var src_default = Adaptor_exports;
145
+ export {
146
+ as,
147
+ combine,
148
+ createPDF,
149
+ cursor,
150
+ dataPath,
151
+ dataValue,
152
+ dateFns,
153
+ src_default as default,
154
+ each,
155
+ field,
156
+ fields,
157
+ fn,
158
+ fnIf,
159
+ group,
160
+ lastReferenceValue,
161
+ map,
162
+ merge,
163
+ request2 as request,
164
+ scrubEmojis,
165
+ source,
166
+ sourceValue,
167
+ util
168
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@openfn/language-browserless",
3
+ "version": "1.0.0",
4
+ "description": "OpenFn browserless adaptor",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./types/index.d.ts",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "author": "Open Function Group",
15
+ "license": "LGPLv3",
16
+ "files": [
17
+ "dist/",
18
+ "types/",
19
+ "ast.json",
20
+ "configuration-schema.json"
21
+ ],
22
+ "dependencies": {
23
+ "@openfn/language-common": "3.2.2"
24
+ },
25
+ "devDependencies": {
26
+ "assertion-error": "2.0.0",
27
+ "chai": "4.3.6",
28
+ "deep-eql": "4.1.1",
29
+ "mocha": "^10.7.3",
30
+ "rimraf": "3.0.2",
31
+ "undici": "^5.22.1"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/openfn/adaptors.git"
36
+ },
37
+ "types": "types/index.d.ts",
38
+ "main": "dist/index.cjs",
39
+ "scripts": {
40
+ "build": "pnpm clean && build-adaptor browserless",
41
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
42
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
43
+ "clean": "rimraf dist types docs",
44
+ "pack": "pnpm pack --pack-destination ../../dist",
45
+ "lint": "eslint src"
46
+ }
47
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Create a PDF from HTML or URL.
3
+ * @public
4
+ * @param {string} input - HTML string or URL string
5
+ * @param {object} options - Optional request options
6
+ * @returns {Operation} Returns state with base64 string directly
7
+ */
8
+ export function createPDF(input: string, options: object): Operation;
9
+ export { request } from "./http.js";
10
+ export { as, combine, cursor, dataPath, dataValue, dateFns, each, field, fields, fn, fnIf, group, lastReferenceValue, map, merge, scrubEmojis, source, sourceValue, util } from "@openfn/language-common";
@@ -0,0 +1,2 @@
1
+ export function prepareNextState(state: object, response: object): object;
2
+ export function request(configuration: object, method: string, path: string, options: object): Promise<object>;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Make a Browserless-authenticated HTTP request operation.
3
+ * This operation always sends requests to the configured Browserless 'baseUrl'.
4
+ * @public
5
+ * @function
6
+ * @param {string} method - HTTP method (e.g. 'GET', 'POST').
7
+ * @param {string} path - API Path (e.g. 'pdf, 'content) joined to the configured 'baseUrl'.
8
+ * @param {object} options - Request options (body, headers, query, parseAs).
9
+ * @state {HttpState}
10
+ * @returns {Operation} an OpenFn operation returning a state with `data` and `response`.
11
+ */
12
+ export function request(method: string, url: any, options: object): Operation;
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";