@browserless.io/browserless 2.3.0 → 2.4.0-beta-1

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.
@@ -46,6 +46,41 @@ describe('/chrome/function API', function () {
46
46
  });
47
47
  });
48
48
 
49
+ it('runs functions with "context"', async () => {
50
+ const config = new Config();
51
+ config.setToken('browserless');
52
+ const metrics = new Metrics();
53
+ await start({ config, metrics });
54
+ const body = {
55
+ code: `export default async function ({ page, context }) {
56
+ if (!!context.ok) {
57
+ return Promise.resolve({
58
+ data: "ok",
59
+ type: "application/text",
60
+ });
61
+ }
62
+ return Promise.reject(new Error('Bad context!'));
63
+ }`,
64
+ context: {
65
+ ok: true,
66
+ },
67
+ };
68
+
69
+ await fetch('http://localhost:3000/chrome/function?token=browserless', {
70
+ body: JSON.stringify(body),
71
+ headers: {
72
+ 'content-type': 'application/json',
73
+ },
74
+ method: 'POST',
75
+ }).then(async (res) => {
76
+ const json = await res.json();
77
+
78
+ expect(json).to.have.property('data');
79
+ expect(json.data).to.equal('ok');
80
+ expect(res.status).to.equal(200);
81
+ });
82
+ });
83
+
49
84
  it('runs "application/javascript" functions', async () => {
50
85
  const config = new Config();
51
86
  config.setToken('browserless');
@@ -70,39 +70,49 @@ export default class StaticGetRoute extends HTTPRoute {
70
70
  }
71
71
 
72
72
  const config = this.config();
73
+ const sdkDir = this.staticSDKDir();
73
74
  const file = path.join(config.getStatic(), pathname);
74
75
  const indexFile = path.join(file, 'index.html');
76
+ const locations = [file, indexFile];
75
77
 
76
- const filePath = (
77
- await Promise.all([
78
- fileExists(file).then((exists) => (exists ? file : undefined)),
79
- fileExists(indexFile).then((exists) =>
80
- exists ? indexFile : undefined,
81
- ),
82
- ])
83
- ).find((_) => !!_);
78
+ if (sdkDir) {
79
+ const sdkPath = path.join(sdkDir, pathname);
80
+ locations.push(...[sdkPath, path.join(sdkPath, 'index.html')]);
81
+ }
82
+
83
+ const foundFilePaths = (
84
+ await Promise.all(
85
+ locations.map((l) => fileExists(l).then((e) => (e ? l : undefined))),
86
+ )
87
+ ).filter((_) => !!_) as string[];
84
88
 
85
- if (!filePath) {
89
+ if (!foundFilePaths.length) {
86
90
  throw new NotFound(
87
91
  `No route or file found for resource ${req.method}: ${pathname}`,
88
92
  );
89
93
  }
90
94
 
91
- verbose(`Found new file "${filePath}", caching path and serving`);
95
+ if (foundFilePaths.length > 1) {
96
+ debug(
97
+ `Multiple files found for request to "${pathname}". Only the first file is served, so please name your files uniquely.`,
98
+ );
99
+ }
100
+
101
+ const [foundFilePath] = foundFilePaths;
102
+ verbose(`Found new file "${foundFilePath}", caching path and serving`);
92
103
 
93
- const contentType = mimeTypes.get(path.extname(filePath));
104
+ const contentType = mimeTypes.get(path.extname(foundFilePath));
94
105
 
95
106
  if (contentType) {
96
107
  res.setHeader('Content-Type', contentType);
97
108
  }
98
109
 
99
- // Cache the assets location so we don't have to
100
- // do stat checks again when requests come back
110
+ // Cache the file as being found so we don't have to call 'stat'
101
111
  pathMap.set(pathname, {
102
112
  contentType,
103
- path: filePath,
113
+ path: foundFilePath,
104
114
  });
105
115
 
106
- return streamFile(verbose, res, filePath, contentType);
116
+ return streamFile(verbose, res, foundFilePath, contentType);
107
117
  };
108
118
  }
package/src/types.ts CHANGED
@@ -102,6 +102,7 @@ abstract class Route {
102
102
  protected _debug: Browserless['debug'],
103
103
  protected _metrics: Browserless['metrics'],
104
104
  protected _monitoring: Browserless['monitoring'],
105
+ protected _staticSDKDir: Browserless['staticSDKDir'],
105
106
  ) {}
106
107
 
107
108
  /**
@@ -195,6 +196,14 @@ abstract class Route {
195
196
  */
196
197
  monitoring = () => this._monitoring;
197
198
 
199
+ /**
200
+ * When running in an SDK environment, this returns the fully-qualified
201
+ * directory of that static directory. When "null" then no SDK directory
202
+ * has been set.
203
+ * @returns {string | null} The full path location of the SDK's static directory
204
+ */
205
+ staticSDKDir = () => this._staticSDKDir;
206
+
198
207
  /**
199
208
  * The HTTP path that this route handles, eg '/my-route' OR an
200
209
  * array of paths that this route can handle.
@@ -225,14 +225,14 @@
225
225
  "length": {
226
226
  "type": "number"
227
227
  },
228
- "__@toStringTag@10908": {
228
+ "__@toStringTag@10910": {
229
229
  "type": "string",
230
230
  "const": "Uint8Array"
231
231
  }
232
232
  },
233
233
  "required": [
234
234
  "BYTES_PER_ELEMENT",
235
- "__@toStringTag@10908",
235
+ "__@toStringTag@10910",
236
236
  "buffer",
237
237
  "byteLength",
238
238
  "byteOffset",
@@ -267,13 +267,13 @@
267
267
  "byteLength": {
268
268
  "type": "number"
269
269
  },
270
- "__@toStringTag@10908": {
270
+ "__@toStringTag@10910": {
271
271
  "type": "string"
272
272
  }
273
273
  },
274
274
  "additionalProperties": false,
275
275
  "required": [
276
- "__@toStringTag@10908",
276
+ "__@toStringTag@10910",
277
277
  "byteLength"
278
278
  ]
279
279
  },
@@ -283,18 +283,18 @@
283
283
  "byteLength": {
284
284
  "type": "number"
285
285
  },
286
- "__@species@11009": {
286
+ "__@species@11011": {
287
287
  "$ref": "#/definitions/SharedArrayBuffer"
288
288
  },
289
- "__@toStringTag@10908": {
289
+ "__@toStringTag@10910": {
290
290
  "type": "string",
291
291
  "const": "SharedArrayBuffer"
292
292
  }
293
293
  },
294
294
  "additionalProperties": false,
295
295
  "required": [
296
- "__@species@11009",
297
- "__@toStringTag@10908",
296
+ "__@species@11011",
297
+ "__@toStringTag@10910",
298
298
  "byteLength"
299
299
  ]
300
300
  },
@@ -1098,7 +1098,7 @@
1098
1098
  },
1099
1099
  "info": {
1100
1100
  "title": "Browserless",
1101
- "version": "2.3.0",
1101
+ "version": "2.4.0-beta-1",
1102
1102
  "x-logo": {
1103
1103
  "altText": "browserless logo",
1104
1104
  "url": "./docs/browserless-logo-inline.svg"
@@ -225,14 +225,14 @@
225
225
  "length": {
226
226
  "type": "number"
227
227
  },
228
- "__@toStringTag@10908": {
228
+ "__@toStringTag@10910": {
229
229
  "type": "string",
230
230
  "const": "Uint8Array"
231
231
  }
232
232
  },
233
233
  "required": [
234
234
  "BYTES_PER_ELEMENT",
235
- "__@toStringTag@10908",
235
+ "__@toStringTag@10910",
236
236
  "buffer",
237
237
  "byteLength",
238
238
  "byteOffset",
@@ -267,13 +267,13 @@
267
267
  "byteLength": {
268
268
  "type": "number"
269
269
  },
270
- "__@toStringTag@10908": {
270
+ "__@toStringTag@10910": {
271
271
  "type": "string"
272
272
  }
273
273
  },
274
274
  "additionalProperties": false,
275
275
  "required": [
276
- "__@toStringTag@10908",
276
+ "__@toStringTag@10910",
277
277
  "byteLength"
278
278
  ]
279
279
  },
@@ -283,18 +283,18 @@
283
283
  "byteLength": {
284
284
  "type": "number"
285
285
  },
286
- "__@species@11009": {
286
+ "__@species@11011": {
287
287
  "$ref": "#/definitions/SharedArrayBuffer"
288
288
  },
289
- "__@toStringTag@10908": {
289
+ "__@toStringTag@10910": {
290
290
  "type": "string",
291
291
  "const": "SharedArrayBuffer"
292
292
  }
293
293
  },
294
294
  "additionalProperties": false,
295
295
  "required": [
296
- "__@species@11009",
297
- "__@toStringTag@10908",
296
+ "__@species@11011",
297
+ "__@toStringTag@10910",
298
298
  "byteLength"
299
299
  ]
300
300
  },
@@ -1098,7 +1098,7 @@
1098
1098
  },
1099
1099
  "info": {
1100
1100
  "title": "Browserless",
1101
- "version": "2.3.0",
1101
+ "version": "2.4.0-beta-1",
1102
1102
  "x-logo": {
1103
1103
  "altText": "browserless logo",
1104
1104
  "url": "./docs/browserless-logo-inline.svg"