@microlink/function 0.2.5 → 0.2.7
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/LICENSE.md +0 -0
- package/README.md +339 -110
- package/package.json +19 -60
- package/src/index.d.ts +12 -1
package/LICENSE.md
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -4,202 +4,431 @@
|
|
|
4
4
|
<br>
|
|
5
5
|
<br>
|
|
6
6
|
<br>
|
|
7
|
-
<p style="max-width: 400px;"><b
|
|
8
|
-
</div>
|
|
9
|
-
|
|
10
|
-
<div align="center">
|
|
11
|
-
<img src="https://i.imgur.com/4PWrzx2.png" width="700px">
|
|
7
|
+
<p style="max-width: 400px;"><b>@microlink/function</b> lets you write normal JavaScript functions and run them remotely with the <a href="https://microlink.io/docs/api/parameters/function">Microlink function</a> parameter — full Puppeteer access, npm packages, and zero infrastructure.</p>
|
|
12
8
|
</div>
|
|
13
9
|
|
|
14
10
|
## Highlights
|
|
15
11
|
|
|
16
12
|
- Starts from \$0/mo.
|
|
17
|
-
- Run
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
13
|
+
- Run JavaScript remotely with no servers, bundles, or browser fleet to manage.
|
|
14
|
+
- Full Puppeteer access when your function references `page`.
|
|
15
|
+
- `require()` any npm package — dependencies are detected and installed on-the-fly.
|
|
16
|
+
- Automatic serialization and compression of function bodies.
|
|
17
|
+
- Execution metrics at `result.profiling`.
|
|
21
18
|
|
|
22
|
-
##
|
|
19
|
+
## Documentation
|
|
23
20
|
|
|
24
|
-
|
|
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)
|
|
21
|
+
The full guides live on [microlink.io](https://microlink.io/docs/guides/function):
|
|
36
22
|
|
|
37
|
-
|
|
23
|
+
- [Function](https://microlink.io/docs/guides/function) — overview and when to use it.
|
|
24
|
+
- [Writing functions](https://microlink.io/docs/guides/function/writing-functions) — return values, custom parameters, and npm packages.
|
|
25
|
+
- [Browser interaction](https://microlink.io/docs/guides/function/browser-interaction) — Puppeteer helpers and page automation.
|
|
26
|
+
- [Profiling and performance](https://microlink.io/docs/guides/function/profiling-and-performance) — execution phases, plan limits, and optimization.
|
|
27
|
+
- [Troubleshooting](https://microlink.io/docs/guides/function/troubleshooting) — error handling and common failure modes.
|
|
38
28
|
|
|
39
|
-
|
|
29
|
+
## Installation
|
|
40
30
|
|
|
41
|
-
|
|
31
|
+
```bash
|
|
32
|
+
npm install @microlink/function
|
|
33
|
+
```
|
|
42
34
|
|
|
43
|
-
|
|
35
|
+
Load directly in the browser from a CDN:
|
|
44
36
|
|
|
45
|
-
|
|
37
|
+
```html
|
|
38
|
+
<script src="https://cdn.jsdelivr.net/npm/@microlink/function/dist/microlink-function.min.js"></script>
|
|
39
|
+
```
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
## Your first function
|
|
48
42
|
|
|
49
|
-
|
|
43
|
+
Pass a JavaScript function and a target URL. The library handles serialization, compression, and the API call:
|
|
50
44
|
|
|
51
|
-
```
|
|
52
|
-
|
|
45
|
+
```js
|
|
46
|
+
const microlink = require('@microlink/function')
|
|
47
|
+
|
|
48
|
+
const fn = microlink(() => 40 + 2)
|
|
49
|
+
|
|
50
|
+
const result = await fn('https://example.com')
|
|
51
|
+
|
|
52
|
+
console.log(result.isFulfilled) // true
|
|
53
|
+
console.log(result.value) // 42
|
|
54
|
+
console.log(result.profiling) // { phases: { ... }, cpu, memory, size }
|
|
53
55
|
```
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
When your function references `page`, Microlink starts a headless browser and gives you full Puppeteer access:
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
```js
|
|
60
|
+
const microlink = require('@microlink/function')
|
|
58
61
|
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
const getTitle = ({ page }) => page.title()
|
|
63
|
+
|
|
64
|
+
const fn = microlink(getTitle)
|
|
65
|
+
|
|
66
|
+
const result = await fn('https://example.com')
|
|
67
|
+
|
|
68
|
+
console.log(result.value) // 'Example Domain'
|
|
61
69
|
```
|
|
62
70
|
|
|
63
|
-
|
|
71
|
+
When your function does **not** reference `page`, no browser is started — execution is faster and cheaper.
|
|
72
|
+
|
|
73
|
+
## Writing functions
|
|
64
74
|
|
|
65
|
-
###
|
|
75
|
+
### Return any value
|
|
66
76
|
|
|
67
|
-
|
|
77
|
+
Functions can return strings, numbers, booleans, arrays, or plain objects:
|
|
68
78
|
|
|
69
79
|
```js
|
|
70
|
-
const
|
|
71
|
-
|
|
80
|
+
const microlink = require('@microlink/function')
|
|
81
|
+
|
|
82
|
+
const fn = microlink(() => ({
|
|
83
|
+
greeting: 'Hello',
|
|
84
|
+
items: [1, 2, 3],
|
|
85
|
+
nested: { works: true }
|
|
86
|
+
}))
|
|
87
|
+
|
|
88
|
+
const result = await fn('https://example.com')
|
|
89
|
+
|
|
90
|
+
console.log(result.value)
|
|
91
|
+
// { greeting: 'Hello', items: [1, 2, 3], nested: { works: true } }
|
|
72
92
|
```
|
|
73
93
|
|
|
74
|
-
|
|
94
|
+
The return value is always at `result.value`. If the function throws, `result.isFulfilled` is `false` and `result.value` contains the error details.
|
|
95
|
+
|
|
96
|
+
### Custom parameters
|
|
97
|
+
|
|
98
|
+
Any extra option you pass to the returned function is forwarded to your code:
|
|
75
99
|
|
|
76
100
|
```js
|
|
77
101
|
const microlink = require('@microlink/function')
|
|
78
102
|
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
)
|
|
103
|
+
const greet = ({ name, greeting }) => `${greeting}, ${name}!`
|
|
104
|
+
|
|
105
|
+
const fn = microlink(greet)
|
|
106
|
+
|
|
107
|
+
const result = await fn('https://example.com', {
|
|
108
|
+
name: 'Kiko',
|
|
109
|
+
greeting: 'Hello'
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
console.log(result.value) // 'Hello, Kiko!'
|
|
82
113
|
```
|
|
83
114
|
|
|
84
|
-
|
|
115
|
+
### npm packages
|
|
116
|
+
|
|
117
|
+
You can `require()` any npm package inside your function. Dependencies are detected automatically and installed on-the-fly:
|
|
85
118
|
|
|
86
119
|
```js
|
|
87
|
-
const
|
|
120
|
+
const microlink = require('@microlink/function')
|
|
88
121
|
|
|
89
|
-
|
|
122
|
+
const fn = microlink(() => {
|
|
123
|
+
const { kebabCase } = require('lodash')
|
|
124
|
+
return kebabCase('Hello World')
|
|
125
|
+
})
|
|
90
126
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
|
|
127
|
+
const result = await fn('https://example.com')
|
|
128
|
+
|
|
129
|
+
console.log(result.value) // 'hello-world'
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Pin a version by appending it to the package name:
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
const cheerio = require('cheerio@1.0.0')
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
When no version is specified, the latest version is installed. Check `result.profiling.phases` — a high `install` value on the first run is normal and drops to zero once cached.
|
|
139
|
+
|
|
140
|
+
### Security restrictions
|
|
141
|
+
|
|
142
|
+
The runtime restricts certain system capabilities. Operations such as spawning child processes or writing to the filesystem outside the sandbox are not permitted:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"isFulfilled": false,
|
|
147
|
+
"value": {
|
|
148
|
+
"name": "Error",
|
|
149
|
+
"code": "ERR_ACCESS_DENIED",
|
|
150
|
+
"permission": "ChildProcess",
|
|
151
|
+
"message": "Access to this API has been restricted."
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Browser interaction
|
|
157
|
+
|
|
158
|
+
When your function references `page`, you get the full Puppeteer `Page` object:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const microlink = require('@microlink/function')
|
|
162
|
+
|
|
163
|
+
const scrape = ({ page }) =>
|
|
164
|
+
page.$eval('h1', el => el.textContent.trim())
|
|
165
|
+
|
|
166
|
+
const fn = microlink(scrape)
|
|
167
|
+
|
|
168
|
+
const result = await fn('https://example.com')
|
|
169
|
+
|
|
170
|
+
console.log(result.value) // 'Example Domain'
|
|
96
171
|
```
|
|
97
172
|
|
|
98
|
-
|
|
173
|
+
Start with the high-level helpers before reaching for lower-level APIs:
|
|
99
174
|
|
|
100
|
-
|
|
175
|
+
- `page.title()` — document title.
|
|
176
|
+
- `page.$eval()` — run a function on the first matching element.
|
|
177
|
+
- `page.$$eval()` — run a function on all matching elements.
|
|
178
|
+
- `page.url()` — current URL.
|
|
179
|
+
- `page.content()` — full page HTML.
|
|
101
180
|
|
|
102
|
-
|
|
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).
|
|
181
|
+
Any [Puppeteer Page method](https://pptr.dev/api/puppeteer.page) is available.
|
|
105
182
|
|
|
106
|
-
|
|
183
|
+
### What your function receives
|
|
107
184
|
|
|
108
|
-
|
|
185
|
+
| Property | Description |
|
|
186
|
+
| --- | --- |
|
|
187
|
+
| `page` | Full Puppeteer access. Microlink navigates to the URL before calling your function. |
|
|
188
|
+
| `response` | The HTTP response from the implicit navigation. Only available when the function uses `page`. |
|
|
189
|
+
| `headers` | Request headers used to fetch the target URL. |
|
|
190
|
+
| any extra parameter | Custom inputs forwarded from the request options. |
|
|
191
|
+
|
|
192
|
+
### Click, wait, and navigate
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
const microlink = require('@microlink/function')
|
|
196
|
+
|
|
197
|
+
const scrapeAfterClick = ({ page }) =>
|
|
198
|
+
page.click('button.load-more')
|
|
199
|
+
.then(() => page.waitForSelector('.results'))
|
|
200
|
+
.then(() => page.$$eval('.results li', items =>
|
|
201
|
+
items.map(el => el.textContent.trim())
|
|
202
|
+
))
|
|
203
|
+
|
|
204
|
+
const fn = microlink(scrapeAfterClick)
|
|
205
|
+
|
|
206
|
+
const result = await fn('https://example.com')
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Replace fixed waits like `page.waitForTimeout(3000)` with `page.waitForSelector()` or `page.waitForNavigation()` whenever possible.
|
|
210
|
+
|
|
211
|
+
### Combine with other parameters
|
|
212
|
+
|
|
213
|
+
Because `function` is just another Microlink parameter, you can prepare the page before your function runs using `scripts`, `modules`, `click`, or `waitForSelector`:
|
|
109
214
|
|
|
110
215
|
```js
|
|
111
216
|
const microlink = require('@microlink/function')
|
|
112
217
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
218
|
+
const fn = microlink(
|
|
219
|
+
({ page }) => page.evaluate('jQuery.fn.jquery'),
|
|
220
|
+
{ meta: false }
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
const result = await fn('https://microlink.io', {
|
|
224
|
+
scripts: ['https://code.jquery.com/jquery-3.5.0.min.js']
|
|
116
225
|
})
|
|
226
|
+
|
|
227
|
+
console.log(result.value) // jQuery version string
|
|
117
228
|
```
|
|
118
229
|
|
|
119
|
-
|
|
230
|
+
## Profiling and performance
|
|
120
231
|
|
|
121
|
-
|
|
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)
|
|
232
|
+
Every function response includes profiling data:
|
|
137
233
|
|
|
138
|
-
|
|
234
|
+
```js
|
|
235
|
+
const result = await fn('https://example.com')
|
|
139
236
|
|
|
140
|
-
|
|
237
|
+
console.log(result.profiling)
|
|
238
|
+
// {
|
|
239
|
+
// phases: { install: 0, build: 120, spawn: 45, run: 890, total: 1055 },
|
|
240
|
+
// cpu: 234,
|
|
241
|
+
// memory: { total: 69996544, used: 2359296, heap: 4410880, external: 1742574 },
|
|
242
|
+
// size: 156
|
|
243
|
+
// }
|
|
244
|
+
```
|
|
141
245
|
|
|
142
|
-
|
|
246
|
+
| Field | Description |
|
|
247
|
+
| --- | --- |
|
|
248
|
+
| `phases.install` | Time spent installing npm dependencies (0 when none are used). |
|
|
249
|
+
| `phases.build` | Time spent bundling the function code. |
|
|
250
|
+
| `phases.spawn` | Time spent starting the isolated process. |
|
|
251
|
+
| `phases.run` | Time spent executing the function. |
|
|
252
|
+
| `phases.total` | Wall-clock time from start to finish. |
|
|
253
|
+
| `cpu` | Peak CPU time in milliseconds. |
|
|
254
|
+
| `memory.total` | Resident memory of the sandbox, Node.js baseline included, in bytes. |
|
|
255
|
+
| `memory.used` | Resident memory attributable to your function, in bytes. |
|
|
256
|
+
| `memory.heap` | V8 heap in use, in bytes. The only field the memory limit bounds. |
|
|
257
|
+
| `memory.external` | Off-heap `Buffer`/`ArrayBuffer` memory, in bytes. |
|
|
258
|
+
| `size` | Bundled code size in bytes. |
|
|
143
259
|
|
|
144
|
-
|
|
145
|
-
- `isRejected`
|
|
146
|
-
- `value` or `reason`, depending on whether the promise fulfilled or rejected.
|
|
260
|
+
### Plan limits
|
|
147
261
|
|
|
148
|
-
|
|
262
|
+
| | Free | Pro |
|
|
263
|
+
| --- | --- | --- |
|
|
264
|
+
| Timeout | 5 seconds | Up to 28 seconds |
|
|
265
|
+
| Memory | 16 MB | 32 MB |
|
|
266
|
+
| Code size | 1024 bytes | Unlimited |
|
|
267
|
+
| Concurrency | 1 per IP | Unlimited |
|
|
149
268
|
|
|
150
|
-
|
|
269
|
+
The free plan is enough to prototype workflows. For production workloads that need more time, memory, or parameters such as `headers`, `proxy`, `ttl`, or `staleTtl`, use a [pro plan](https://microlink.io/#pricing).
|
|
151
270
|
|
|
152
|
-
|
|
271
|
+
### Skip metadata
|
|
153
272
|
|
|
154
|
-
|
|
273
|
+
Most function-only workflows do not need normalized metadata. Set `meta: false` to skip it — this is usually the biggest speedup:
|
|
155
274
|
|
|
156
|
-
|
|
275
|
+
```js
|
|
276
|
+
const fn = microlink(({ page }) => page.title(), { meta: false })
|
|
277
|
+
```
|
|
157
278
|
|
|
158
|
-
|
|
279
|
+
If you still need the rendered markup, call `page.content()` inside the function.
|
|
159
280
|
|
|
160
|
-
|
|
281
|
+
### Compression
|
|
161
282
|
|
|
162
|
-
|
|
283
|
+
`@microlink/function` compresses function bodies automatically before sending them to the API. You can also compress manually via `microlink.compress()` — useful when calling MQL directly:
|
|
284
|
+
|
|
285
|
+
```js
|
|
286
|
+
const compressed = await microlink.compress(({ page }) => page.title())
|
|
287
|
+
// 'br#...' on Node.js (brotli), 'lz#...' as fallback
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Supported prefixes: `lz#` (lz-string), `br#` (brotli), `gz#` (gzip).
|
|
291
|
+
|
|
292
|
+
### Optimization checklist
|
|
293
|
+
|
|
294
|
+
1. Set `meta: false` unless you need normalized metadata.
|
|
295
|
+
2. Use `page.title()` and `page.$eval()` instead of `page.evaluate()` when possible.
|
|
296
|
+
3. Replace fixed waits with `page.waitForSelector()`.
|
|
297
|
+
4. Check `result.profiling.phases` to find the bottleneck.
|
|
298
|
+
5. Minimize dependencies — each `require()` adds install and build time.
|
|
299
|
+
|
|
300
|
+
## Troubleshooting
|
|
301
|
+
|
|
302
|
+
### Error handling
|
|
303
|
+
|
|
304
|
+
When a function throws, the result comes back with `isFulfilled: false` and the error details at `result.value`:
|
|
305
|
+
|
|
306
|
+
```js
|
|
307
|
+
const failing = ({ name }) => name()
|
|
308
|
+
|
|
309
|
+
const fn = microlink(failing)
|
|
310
|
+
|
|
311
|
+
const result = await fn('https://example.com', { name: 'Kiko' })
|
|
312
|
+
|
|
313
|
+
console.log(result.isFulfilled) // false
|
|
314
|
+
console.log(result.value.name) // 'TypeError'
|
|
315
|
+
console.log(result.value.message) // 'name is not a function'
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
Non-Error throws (like `throw 'oh no'`) are normalized into a `NonError` with the thrown value as the message.
|
|
319
|
+
|
|
320
|
+
### Resource errors
|
|
321
|
+
|
|
322
|
+
When a function exceeds its plan limits, the API returns a descriptive error:
|
|
323
|
+
|
|
324
|
+
| Error | Trigger |
|
|
325
|
+
| --- | --- |
|
|
326
|
+
| `TimeoutError` | Wall-clock time exceeded the plan limit. |
|
|
327
|
+
| `CpuTimeError` | CPU time exceeded the plan limit. |
|
|
328
|
+
| `MemoryError` | Memory usage exceeded the plan limit. |
|
|
329
|
+
| `CodeSizeError` | Code exceeds the 1024 bytes free plan limit. |
|
|
330
|
+
| `ConcurrencyError` | Too many concurrent executions for the free plan (1 per IP). |
|
|
331
|
+
| `OutgoingRequestError` | Cross-origin network request on the free plan. |
|
|
332
|
+
|
|
333
|
+
### Function-specific errors
|
|
334
|
+
|
|
335
|
+
- **EINVALFUNCTION** — invalid JavaScript syntax in the function string.
|
|
336
|
+
- **EINVALEVAL** — the function executed but threw at runtime.
|
|
337
|
+
|
|
338
|
+
### Debugging tips
|
|
339
|
+
|
|
340
|
+
1. Start simple — reduce the function to `({ page }) => page.title()` to isolate the problem.
|
|
341
|
+
2. Set `meta: false` unless metadata is required.
|
|
342
|
+
3. Inspect `result.profiling` to see where time is spent.
|
|
343
|
+
4. Keep orchestration in the outer function and DOM-only code inside `page.evaluate()`.
|
|
344
|
+
5. Watch for null — DOM queries return null when the element does not exist.
|
|
345
|
+
|
|
346
|
+
See the [troubleshooting guide](https://microlink.io/docs/guides/function/troubleshooting) for detailed remediation steps.
|
|
347
|
+
|
|
348
|
+
## Choose the lightest tool
|
|
349
|
+
|
|
350
|
+
Use `function` when the built-in parameters stop being expressive enough, not as the default for every workflow.
|
|
351
|
+
|
|
352
|
+
| If you need | Best option | Why |
|
|
353
|
+
| --- | --- | --- |
|
|
354
|
+
| Simple field extraction from the DOM | `data` | Declarative rules are shorter and easier to maintain. |
|
|
355
|
+
| Inject CSS or JavaScript before another workflow | `styles`, `modules`, or `scripts` | Lighter than full browser automation. |
|
|
356
|
+
| Click, wait, compute, reshape, or orchestrate custom logic | `function` | Puppeteer access plus any npm package. |
|
|
357
|
+
|
|
358
|
+
## Authentication
|
|
359
|
+
|
|
360
|
+
Pass your API key via the third argument (`gotOpts`):
|
|
163
361
|
|
|
164
362
|
```js
|
|
165
363
|
const microlink = require('@microlink/function')
|
|
166
364
|
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
365
|
+
const fn = microlink(
|
|
366
|
+
({ page }) => page.title(),
|
|
367
|
+
{},
|
|
368
|
+
{ headers: { 'x-api-key': process.env.MICROLINK_API_KEY } }
|
|
369
|
+
)
|
|
171
370
|
|
|
172
|
-
const
|
|
371
|
+
const result = await fn('https://example.com')
|
|
173
372
|
```
|
|
174
373
|
|
|
374
|
+
See [authentication](https://microlink.io/docs/api/basics/authentication) for endpoint and quota details.
|
|
375
|
+
|
|
376
|
+
## Examples
|
|
377
|
+
|
|
378
|
+
See [examples](/examples).
|
|
379
|
+
|
|
175
380
|
## API
|
|
176
381
|
|
|
177
|
-
### microlink(fn, [mqlOpts], [
|
|
382
|
+
### `microlink(fn, [mqlOpts], [gotOpts])`
|
|
178
383
|
|
|
179
|
-
|
|
384
|
+
Returns an async function `(url, [mqlOpts], [gotOpts]) => Promise<FunctionResponse>`.
|
|
180
385
|
|
|
181
|
-
|
|
386
|
+
#### `fn`
|
|
387
|
+
|
|
388
|
+
_Required_
|
|
182
389
|
Type: `function`
|
|
183
390
|
|
|
184
|
-
The function
|
|
391
|
+
The function to execute remotely.
|
|
185
392
|
|
|
186
|
-
#### mqlOpts
|
|
393
|
+
#### `mqlOpts`
|
|
187
394
|
|
|
188
395
|
Type: `object`
|
|
189
396
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
Any option passed here will bypass to [mql](https://github.com/microlinkhq/mql).
|
|
397
|
+
Default options forwarded to [@microlink/mql](https://github.com/microlinkhq/mql). Per-call options merge on top.
|
|
193
398
|
|
|
194
|
-
####
|
|
399
|
+
#### `gotOpts`
|
|
195
400
|
|
|
196
401
|
Type: `object`
|
|
197
402
|
|
|
198
|
-
|
|
403
|
+
HTTP client options forwarded to `got` inside MQL — use this for authentication headers and other request settings.
|
|
404
|
+
|
|
405
|
+
### Response shape
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
type FunctionResponse =
|
|
409
|
+
| {
|
|
410
|
+
isFulfilled: true
|
|
411
|
+
value: any
|
|
412
|
+
profiling: FunctionProfiling
|
|
413
|
+
logging: Record<string, unknown>
|
|
414
|
+
}
|
|
415
|
+
| {
|
|
416
|
+
isFulfilled: false
|
|
417
|
+
value: { name: string; message: string; [key: string]: unknown }
|
|
418
|
+
profiling: FunctionProfiling
|
|
419
|
+
logging: Record<string, unknown>
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### Static properties
|
|
424
|
+
|
|
425
|
+
- `microlink.compress(code)` — compress a function body for manual MQL usage.
|
|
426
|
+
- `microlink.mql` — the underlying [@microlink/mql](https://github.com/microlinkhq/mql) client.
|
|
427
|
+
- `microlink.version` — package version string.
|
|
199
428
|
|
|
200
429
|
## License
|
|
201
430
|
|
|
202
|
-
|
|
203
|
-
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/
|
|
431
|
+
**@microlink/function** © [Microlink](https://microlink.io), released under the [MIT](https://github.com/microlinkhq/function/blob/master/LICENSE.md) License.<br>
|
|
432
|
+
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/microlinkhq/function/contributors).
|
|
204
433
|
|
|
205
434
|
> [microlink.io](https://microlink.io) · GitHub [@MicrolinkHQ](https://github.com/microlinkhq) · X [@microlinkhq](https://x.com/microlinkhq)
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microlink/function",
|
|
3
|
-
"description": "
|
|
3
|
+
"description": "Write JavaScript functions and run them remotely with Microlink — Puppeteer access, npm packages, and zero infrastructure.",
|
|
4
4
|
"homepage": "https://function.microlink.io",
|
|
5
|
-
"version": "0.2.
|
|
5
|
+
"version": "0.2.7",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
"types": "./src/index.d.ts",
|
|
@@ -16,11 +16,12 @@
|
|
|
16
16
|
"url": "https://github.com/Kikobeats"
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
|
+
"directory": "packages/function",
|
|
19
20
|
"type": "git",
|
|
20
|
-
"url": "git+https://github.com/microlinkhq/
|
|
21
|
+
"url": "git+https://github.com/microlinkhq/microlink.git"
|
|
21
22
|
},
|
|
22
23
|
"bugs": {
|
|
23
|
-
"url": "https://github.com/microlinkhq/
|
|
24
|
+
"url": "https://github.com/microlinkhq/microlink/issues"
|
|
24
25
|
},
|
|
25
26
|
"keywords": [
|
|
26
27
|
"browserless",
|
|
@@ -33,31 +34,21 @@
|
|
|
33
34
|
"serverless"
|
|
34
35
|
],
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@microlink/mql": "
|
|
37
|
+
"@microlink/mql": "0.17.0",
|
|
37
38
|
"lz-ts": "~1.1.2"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
|
-
"@commitlint/cli": "latest",
|
|
41
|
-
"@commitlint/config-conventional": "latest",
|
|
42
41
|
"@rollup/plugin-commonjs": "latest",
|
|
43
42
|
"@rollup/plugin-node-resolve": "latest",
|
|
44
43
|
"@rollup/plugin-replace": "latest",
|
|
45
44
|
"@rollup/plugin-terser": "latest",
|
|
46
45
|
"async-listen": "latest",
|
|
47
46
|
"ava": "latest",
|
|
48
|
-
"
|
|
49
|
-
"ci-publish": "latest",
|
|
47
|
+
"cheerio": "latest",
|
|
50
48
|
"execa": "latest",
|
|
51
|
-
"git-authors-cli": "latest",
|
|
52
|
-
"github-generate-release": "latest",
|
|
53
|
-
"nano-staged": "latest",
|
|
54
|
-
"prettier-standard": "latest",
|
|
55
49
|
"puppeteer-core": "latest",
|
|
56
50
|
"rollup": "latest",
|
|
57
51
|
"rollup-plugin-filesize": "latest",
|
|
58
|
-
"simple-git-hooks": "latest",
|
|
59
|
-
"standard": "latest",
|
|
60
|
-
"standard-version": "latest",
|
|
61
52
|
"tinyspawn": "latest",
|
|
62
53
|
"tsd": "latest"
|
|
63
54
|
},
|
|
@@ -67,38 +58,20 @@
|
|
|
67
58
|
"files": [
|
|
68
59
|
"src"
|
|
69
60
|
],
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "rollup -c rollup.config.js --bundleConfigAsCjs",
|
|
63
|
+
"clean:build": "rm -rf dist/index.js",
|
|
64
|
+
"dev": "pnpm run build -- -w",
|
|
65
|
+
"lint": "tsd",
|
|
66
|
+
"prebuild": "pnpm run clean:build",
|
|
67
|
+
"prepublishOnly": "pnpm run build",
|
|
68
|
+
"pretest": "pnpm run lint && pnpm run build",
|
|
69
|
+
"test": "ava --verbose"
|
|
70
|
+
},
|
|
70
71
|
"license": "MIT",
|
|
71
72
|
"ava": {
|
|
72
73
|
"timeout": "1m"
|
|
73
74
|
},
|
|
74
|
-
"commitlint": {
|
|
75
|
-
"extends": [
|
|
76
|
-
"@commitlint/config-conventional"
|
|
77
|
-
],
|
|
78
|
-
"rules": {
|
|
79
|
-
"body-max-line-length": [
|
|
80
|
-
0
|
|
81
|
-
]
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
"nano-staged": {
|
|
85
|
-
"*.js": [
|
|
86
|
-
"npx @kikobeats/prettier-standard",
|
|
87
|
-
"standard --fix"
|
|
88
|
-
],
|
|
89
|
-
"package.json": [
|
|
90
|
-
"finepack"
|
|
91
|
-
]
|
|
92
|
-
},
|
|
93
|
-
"simple-git-hooks": {
|
|
94
|
-
"commit-msg": "npx commitlint --edit",
|
|
95
|
-
"pre-commit": "npx nano-staged"
|
|
96
|
-
},
|
|
97
|
-
"standard": {
|
|
98
|
-
"ignore": [
|
|
99
|
-
"dist"
|
|
100
|
-
]
|
|
101
|
-
},
|
|
102
75
|
"tsd": {
|
|
103
76
|
"compilerOptions": {
|
|
104
77
|
"baseUrl": ".",
|
|
@@ -110,19 +83,5 @@
|
|
|
110
83
|
},
|
|
111
84
|
"directory": "test"
|
|
112
85
|
},
|
|
113
|
-
"
|
|
114
|
-
|
|
115
|
-
"clean": "rm -rf node_modules",
|
|
116
|
-
"clean:build": "rm -rf dist/index.js",
|
|
117
|
-
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
|
|
118
|
-
"dev": "npm run build -- -w",
|
|
119
|
-
"lint": "standard && tsd",
|
|
120
|
-
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
|
|
121
|
-
"prebuild": "npm run clean:build",
|
|
122
|
-
"pretest": "npm run lint && npm run build",
|
|
123
|
-
"release": "standard-version -a",
|
|
124
|
-
"release:github": "github-generate-release",
|
|
125
|
-
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
126
|
-
"test": "c8 ava --verbose"
|
|
127
|
-
}
|
|
128
|
-
}
|
|
86
|
+
"gitHead": "35ba2e58e85c61899d5abd23fca47a4b01ccbebf"
|
|
87
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -10,7 +10,12 @@ export type FunctionProfiling = {
|
|
|
10
10
|
total?: number
|
|
11
11
|
}
|
|
12
12
|
cpu?: number
|
|
13
|
-
memory?:
|
|
13
|
+
memory?: {
|
|
14
|
+
total?: number
|
|
15
|
+
used?: number
|
|
16
|
+
heap?: number
|
|
17
|
+
external?: number
|
|
18
|
+
}
|
|
14
19
|
size?: number
|
|
15
20
|
}
|
|
16
21
|
|
|
@@ -50,4 +55,10 @@ declare function microlinkFunction(
|
|
|
50
55
|
gotOpts?: object
|
|
51
56
|
) => Promise<FunctionResponse>
|
|
52
57
|
|
|
58
|
+
declare namespace microlinkFunction {
|
|
59
|
+
function compress(code: FunctionInput | string): Promise<string>
|
|
60
|
+
const mql: typeof import('@microlink/mql').default
|
|
61
|
+
const version: string
|
|
62
|
+
}
|
|
63
|
+
|
|
53
64
|
export default microlinkFunction
|