@microlink/function 0.1.7 → 0.2.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.
package/README.md ADDED
@@ -0,0 +1,205 @@
1
+ <div align="center">
2
+ <img src="https://github.com/microlinkhq/cdn/raw/master/dist/logo/banner.png#gh-light-mode-only" alt="microlink logo">
3
+ <img src="https://github.com/microlinkhq/cdn/raw/master/dist/logo/banner-dark.png#gh-dark-mode-only" alt="microlink logo">
4
+ <br>
5
+ <br>
6
+ <br>
7
+ <p style="max-width: 400px;"><b>Microlink Function</b> allows you to run JavaScript Serverless functions with <a target="_blank" href="https://browserless.js.org">Headless Chromium</a> programmatic access.</p>
8
+ </div>
9
+
10
+ <div align="center">
11
+ <img src="https://i.imgur.com/4PWrzx2.png" width="700px">
12
+ </div>
13
+
14
+ ## Highlights
15
+
16
+ - Starts from \$0/mo.
17
+ - Run Serverless Javascript functions (also [locally](https://github.com/microlinkhq/local)).
18
+ - Ability to require to require any of the [allowed](#npm-packages) NPM packages.
19
+ - Headless Chromium browser access in the same request cycle.
20
+ - No servers to maintain, no hidden cost or infrastructure complexity.
21
+
22
+ ## Contents
23
+
24
+ - [How it works](#how-it-works)
25
+ - [Installation](#installation)
26
+ - [from NPM](#from-npm)
27
+ - [from CDN](#from-cdn)
28
+ - [Get Started](#get-started)
29
+ - [Input](#input)
30
+ - [NPM packages](#npm-packages)
31
+ - [Output](#output)
32
+ - [Examples](#examples)
33
+ - [Pricing](#pricing)
34
+ - [API](#api)
35
+ - [License](#license)
36
+
37
+ ## How it works
38
+
39
+ Every time you call a **Microlink Function**, the code function will be compiled and executed remotely in a safe V8 sandbox.
40
+
41
+ It's pretty similar to AWS Lambda, but rather than bundle your code, all the code will be executed remotely, giving the result of the execution back to you.
42
+
43
+ **Microlink Function** can be invoked in frontend or backend side. There is nothing to deploy or hidden infrastructure cost associated.
44
+
45
+ ## Installation
46
+
47
+ ### from NPM
48
+
49
+ It's available as [npm package](https://www.npmjs.com/package/@microlink/function):
50
+
51
+ ```bash
52
+ $ npm install @microlink/function --save
53
+ ```
54
+
55
+ ### from CDN
56
+
57
+ Load directly in the browser from your favorite CDN:
58
+
59
+ ```html
60
+ <script src="https://cdn.jsdelivr.net/npm/@microlink/function/dist/microlink-function.min.js"></script>
61
+ ```
62
+
63
+ ## Get Started
64
+
65
+ ### Input
66
+
67
+ Let say you have a JavaScript like this:
68
+
69
+ ```js
70
+ const ping = ({ statusCode, response }) =>
71
+ statusCode ? response.status() : response.statusText()
72
+ ```
73
+
74
+ To run the previous code as **Microlink Function**, all you need to do is wrap the function with the `microlink` decorator:
75
+
76
+ ```js
77
+ const microlink = require('@microlink/function')
78
+
79
+ const ping = microlink(({ response }) =>
80
+ statusCode ? response.status() : response.statusText()
81
+ )
82
+ ```
83
+
84
+ Then, just call the function as you would normally:
85
+
86
+ ```js
87
+ const result = await ping('https://example.com', { statusCode: true })
88
+
89
+ console.log(result)
90
+
91
+ // {
92
+ // isFullfilled: true,
93
+ // isRejected: false,
94
+ // value: 200
95
+ // }
96
+ ```
97
+
98
+ When a function is wrapped by **Microlink Function** the function execution is done remotely, giving back the result.
99
+
100
+ Any **Microlink Function** will receive the following parameters:
101
+
102
+ - `html`: When [meta](https://microlink.io/docs/api/parameters/meta) is enabled, the HTML markup of the website is provided.
103
+ - `page`: The [`puppeteer#page`](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#class-page) instance to interact with the headless browser.
104
+ - `response`: The [`puppeteer#response`](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#class-httpresponse) as result of the implicit [`page.goto`](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagegotourl-options).
105
+
106
+ #### NPM packages
107
+
108
+ Additionally, you can require a allowed list of common NPM packages inside your code blocks:
109
+
110
+ ```js
111
+ const microlink = require('@microlink/function')
112
+
113
+ const ping = microlink(({ statusCode, response }) => {
114
+ const { result } = require('lodash')
115
+ return result(response, statusCode ? 'status' : 'statusText')
116
+ })
117
+ ```
118
+
119
+ The list of allowed NPM packages are:
120
+
121
+ - [`path`](https://nodejs.org/api/path.html)
122
+ - [`url`](https://nodejs.org/api/url.html)
123
+ - [`@aws-sdk/client-s3`](https://npm.im/@aws-sdk/client-s3)
124
+ - [`@metascraper`](https://npm.im/@metascraper)
125
+ - [`@mozilla/readability`](https://npm.im/@mozilla/readability)
126
+ - [`async`](https://npm.im/async)
127
+ - [`cheerio`](https://npm.im/cheerio)
128
+ - [`extract-email-address`](https://npm.im/extract-email-address)
129
+ - [`got`](https://npm.im/got)
130
+ - [`ioredis`](https://npm.im/ioredis)
131
+ - [`jsdom`](https://npm.im/jsdom)
132
+ - [`lodash`](https://npm.im/lodash)
133
+ - [`metascraper`](https://npm.im/metascraper)
134
+ - [`p-reflect`](https://npm.im/p-reflect)
135
+ - [`p-retry`](https://npm.im/p-retry)
136
+ - [`p-timeout`](https://npm.im/p-timeout)
137
+
138
+ Do you miss any NPM modules there? open a [new issue](/issues/new) and we make it available.
139
+
140
+ ### Output
141
+
142
+ When a **Microlink Function** is executed, the result response object has the following interface:
143
+
144
+ - `isFulfilled`
145
+ - `isRejected`
146
+ - `value` or `reason`, depending on whether the promise fulfilled or rejected.
147
+
148
+ ## Testing
149
+
150
+ Check [`@microlink/local`](https://github.com/microlinkhq/local) for executing your Microlink Functions locally.
151
+
152
+ ## Examples
153
+
154
+ Check [examples](/examples).
155
+
156
+ ## Pricing
157
+
158
+ **Microlink Function** has been designed to be cheap and affordable.
159
+
160
+ The first 50 [uncached](https://microlink.io/blog/edge-cdn/) requests of every day are **free**. If you need more, you should to buy a [pro plan](https://microlink.io/#pricing).
161
+
162
+ For [authenticating](https://microlink.io/docs/api/basics/authentication) your requests, you should to provide your API key:
163
+
164
+ ```js
165
+ const microlink = require('@microlink/function')
166
+
167
+ const code = ({ statusCode, response }) => {
168
+ const { result } = require('lodash')
169
+ return result(response, statusCode ? 'status' : 'statusText')
170
+ }
171
+
172
+ const ping = microlink(code, { apiKey: process.env.MICROLINK_API_KEY })
173
+ ```
174
+
175
+ ## API
176
+
177
+ ### microlink(fn, [mqlOpts], [gotoOpts])
178
+
179
+ #### fn
180
+
181
+ _Required_<br>
182
+ Type: `function`
183
+
184
+ The function that be executed inside Microlink API browser.
185
+
186
+ #### mqlOpts
187
+
188
+ Type: `object`
189
+
190
+ The function that be executed inside Microlink API browser.
191
+
192
+ Any option passed here will bypass to [mql](https://github.com/microlinkhq/mql).
193
+
194
+ #### gotoOpts
195
+
196
+ Type: `object`
197
+
198
+ Any option passed here will bypass to [browserless#goto](https://browserless.js.org/#/?id=options-5).
199
+
200
+ ## License
201
+
202
+ **microlink-function** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlink/microlink-function/blob/master/LICENSE.md) License.<br>
203
+ Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/microlink/microlink-function/contributors).
204
+
205
+ > [microlink.io](https://microlink.io) · GitHub [@MicrolinkHQ](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "@microlink/function",
3
- "homepage": "https://github.com/microlinkhq/function#readme",
4
- "url": "https://function.microlink.io",
5
- "version": "0.1.7",
6
- "main": "src/node.js",
7
- "browser": "src/browser.js",
3
+ "description": "Browser functions as Service. Interacting with browser pages, remotely.",
4
+ "homepage": "https://function.microlink.io",
5
+ "version": "0.2.3",
6
+ "types": "src/index.d.ts",
7
+ "exports": {
8
+ "types": "./src/index.d.ts",
9
+ "require": "./src/index.js",
10
+ "import": "./src/index.mjs",
11
+ "default": "./src/index.mjs"
12
+ },
8
13
  "author": {
9
- "name": "Kiko Beats",
10
14
  "email": "josefrancisco.verdu@gmail.com",
11
- "url": "https://kikobeats.com"
15
+ "name": "Kiko Beats",
16
+ "url": "https://github.com/Kikobeats"
12
17
  },
13
18
  "repository": {
14
- "directory": "packages/npm",
15
19
  "type": "git",
16
20
  "url": "git+https://github.com/microlinkhq/function.git"
17
21
  },
@@ -29,48 +33,98 @@
29
33
  "serverless"
30
34
  ],
31
35
  "dependencies": {
32
- "@microlink/mql": "~0.10.0",
33
- "base64-url": "~2.3.3"
36
+ "@microlink/mql": "~0.15.1",
37
+ "lz-ts": "~1.1.2"
34
38
  },
35
39
  "devDependencies": {
40
+ "@commitlint/cli": "latest",
41
+ "@commitlint/config-conventional": "latest",
42
+ "@ksmithut/prettier-standard": "latest",
36
43
  "@rollup/plugin-commonjs": "latest",
37
44
  "@rollup/plugin-node-resolve": "latest",
38
45
  "@rollup/plugin-replace": "latest",
39
- "abort-controller": "latest",
46
+ "@rollup/plugin-terser": "latest",
47
+ "async-listen": "latest",
40
48
  "ava": "latest",
41
49
  "c8": "latest",
42
- "conventional-github-releaser": "latest",
43
- "node-fetch": "2",
44
- "npm-check-updates": "latest",
50
+ "ci-publish": "latest",
51
+ "execa": "latest",
52
+ "git-authors-cli": "latest",
53
+ "github-generate-release": "latest",
54
+ "nano-staged": "latest",
55
+ "prettier-standard": "latest",
56
+ "puppeteer-core": "latest",
45
57
  "rollup": "latest",
46
58
  "rollup-plugin-filesize": "latest",
47
- "rollup-plugin-shim": "latest",
48
- "rollup-plugin-terser": "latest",
49
- "rollup-plugin-visualizer": "latest"
59
+ "simple-git-hooks": "latest",
60
+ "standard": "latest",
61
+ "standard-version": "latest",
62
+ "tinyspawn": "latest",
63
+ "tsd": "latest"
50
64
  },
51
65
  "engines": {
52
- "node": ">= 12"
66
+ "node": ">= 18"
53
67
  },
54
68
  "files": [
55
- "dist",
56
69
  "src"
57
70
  ],
58
71
  "scripts": {
59
- "build": "rollup -c rollup.config.js",
60
- "postversion": "pnpm run build",
72
+ "build": "rollup -c rollup.config.js --bundleConfigAsCjs",
73
+ "clean": "rm -rf node_modules",
74
+ "clean:build": "rm -rf dist/index.js",
75
+ "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
61
76
  "dev": "npm run build -- -w",
77
+ "lint": "standard && tsd",
78
+ "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
79
+ "prebuild": "npm run clean:build",
80
+ "prepublishOnly": "npm run build",
81
+ "pretest": "npm run lint && npm run build",
82
+ "release": "standard-version -a",
83
+ "release:github": "github-generate-release",
84
+ "release:tags": "git push --follow-tags origin HEAD:master",
62
85
  "test": "c8 ava --verbose"
63
86
  },
64
87
  "license": "MIT",
65
88
  "ava": {
66
- "files": [
67
- "test/**/*",
68
- "!test/browser-globals.js",
69
- "!test/clients.js"
70
- ],
71
89
  "timeout": "1m"
72
90
  },
73
- "umd:main": "dist/microlink-function.js",
74
- "unpkg": "dist/microlink-function.js",
75
- "gitHead": "f5138b8319b96961c6d516b60113cc2c79d95a4e"
91
+ "commitlint": {
92
+ "extends": [
93
+ "@commitlint/config-conventional"
94
+ ],
95
+ "rules": {
96
+ "body-max-line-length": [
97
+ 0
98
+ ]
99
+ }
100
+ },
101
+ "nano-staged": {
102
+ "*.js": [
103
+ "prettier-standard",
104
+ "standard --fix"
105
+ ],
106
+ "package.json": [
107
+ "finepack"
108
+ ]
109
+ },
110
+ "simple-git-hooks": {
111
+ "commit-msg": "npx commitlint --edit",
112
+ "pre-commit": "npx nano-staged"
113
+ },
114
+ "standard": {
115
+ "ignore": [
116
+ "dist"
117
+ ]
118
+ },
119
+ "tsd": {
120
+ "compilerOptions": {
121
+ "baseUrl": ".",
122
+ "paths": {
123
+ "@microlink/function": [
124
+ "./src/index.d.ts"
125
+ ]
126
+ }
127
+ },
128
+ "directory": "test"
129
+ }
76
130
  }
package/src/index.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { MqlOptions } from '@microlink/mql'
2
+ import { Page, HTTPResponse } from 'puppeteer-core'
3
+
4
+ export type FunctionResponse = {
5
+ isFulfilled: true,
6
+ isRejected: false,
7
+ value: any
8
+ }
9
+
10
+ export type FunctionArgs = {
11
+ page: object;
12
+ response: object;
13
+ url: string;
14
+ }
15
+
16
+ export type FunctionInput = (args: {
17
+ page: Page;
18
+ response: HTTPResponse;
19
+ [key: string]: any;
20
+ }) => any;
21
+
22
+
23
+ declare function microlinkFunction(
24
+ fn: FunctionInput,
25
+ mqlOpts?: MqlOptions,
26
+ gotOpts?: object
27
+ ): (
28
+ url: string,
29
+ mqlOpts?: MqlOptions,
30
+ gotOpts?: object
31
+ ) => Promise<FunctionResponse>;
32
+
33
+ export default microlinkFunction;
package/src/index.js ADDED
@@ -0,0 +1,41 @@
1
+ 'use strict'
2
+
3
+ const mql = require('@microlink/mql')
4
+
5
+ let toCompress
6
+
7
+ try {
8
+ const { promisify } = require('util')
9
+ const { brotliCompress } = require('zlib')
10
+ const compress = promisify(brotliCompress)
11
+ toCompress = async code =>
12
+ `br#${(await compress(code.toString())).toString('base64url')}`
13
+ } catch {
14
+ const { compressToURI } = require('lz-ts')
15
+ toCompress = code => Promise.resolve(`lz#${compressToURI(code.toString())}`)
16
+ }
17
+
18
+ const fn = (code, mqlOpts, gotOpts) => {
19
+ const compressed = toCompress(code)
20
+
21
+ return async (url, opts) => {
22
+ const { data } = await mql(
23
+ url,
24
+ {
25
+ function: await compressed,
26
+ meta: false,
27
+ ...mqlOpts,
28
+ ...opts
29
+ },
30
+ gotOpts
31
+ )
32
+
33
+ return data.function
34
+ }
35
+ }
36
+
37
+ fn.compress = toCompress
38
+ fn.mql = mql
39
+ fn.version = require('../package.json').version
40
+
41
+ module.exports = fn
package/src/index.mjs ADDED
@@ -0,0 +1,10 @@
1
+ import { createRequire } from 'module'
2
+
3
+ const require = createRequire(import.meta.url)
4
+ const fn = require('./index.js')
5
+
6
+ export const compress = fn.compress
7
+ export const mql = fn.mql
8
+ export const version = fn.version
9
+
10
+ export default fn