@metacall/protocol 0.1.29 → 0.1.30

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/dist/protocol.js CHANGED
@@ -18,6 +18,7 @@
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.waitFor = exports.MaxFuncLength = exports.MaxRetryInterval = exports.MaxRetries = exports.InvokeType = exports.ResourceType = exports.isProtocolError = exports.ProtocolError = void 0;
21
+ const stream_1 = require("stream");
21
22
  const url_1 = require("url");
22
23
  const deployment_1 = require("./deployment");
23
24
  class ProtocolError extends Error {
@@ -74,7 +75,7 @@ class Request {
74
75
  this.impl.method = method;
75
76
  return this;
76
77
  }
77
- blob(body) {
78
+ bodyRaw(body) {
78
79
  this.impl.body = body;
79
80
  return this;
80
81
  }
@@ -116,6 +117,7 @@ class Request {
116
117
  }
117
118
  exports.default = (token, baseURL) => {
118
119
  const request = (url = baseURL) => new Request(token, url);
120
+ const hostname = new url_1.URL(baseURL).hostname;
119
121
  const api = {
120
122
  refresh: () => request().url('/api/account/refresh-token').asText(),
121
123
  ready: () => request()
@@ -151,17 +153,33 @@ exports.default = (token, baseURL) => {
151
153
  }
152
154
  return deploy;
153
155
  },
154
- upload: async (name, blob, jsons = [], runners = []) => {
156
+ upload: async (name, data, jsons = [], runners = []) => {
155
157
  const fd = new FormData();
156
158
  fd.append('id', name);
157
159
  fd.append('type', 'application/x-zip-compressed');
158
160
  fd.append('jsons', JSON.stringify(jsons));
159
161
  fd.append('runners', JSON.stringify(runners));
160
- fd.append('raw', blob, 'blob');
162
+ if (data instanceof Blob) {
163
+ fd.append('raw', data, `${name}.zip`);
164
+ }
165
+ else if (data instanceof stream_1.Readable) {
166
+ // This is terrible but NodeJS does not ensure that streaming and zero
167
+ // copy will be performed anyway, as the sizes are not really big (150mb is the limit)
168
+ // we can do this nasty intermediate buffer creation and forget about it
169
+ const chunks = [];
170
+ for await (const chunk of data) {
171
+ chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
172
+ }
173
+ const buffer = Buffer.concat(chunks);
174
+ fd.append('raw', new Blob([buffer]), `${name}.zip`);
175
+ }
176
+ else {
177
+ throw Error(`Type ${typeof data} not supported, use Blob or Readable`);
178
+ }
161
179
  return await request()
162
180
  .url('/api/package/create')
163
181
  .method('POST')
164
- .blob(fd)
182
+ .bodyRaw(fd)
165
183
  .asJson();
166
184
  },
167
185
  add: (url, branch, jsons = []) => request()
@@ -222,12 +240,16 @@ exports.default = (token, baseURL) => {
222
240
  .asJson()
223
241
  .then(res => res['files']),
224
242
  invoke: async (type, prefix, suffix, version = 'v1', name, args) => {
225
- // Old API
226
- // const req = request('https://api.metacall.io').url(
227
- // `/${prefix}/${suffix}/${version}/${type}/${name}`
228
- // );
229
- // New API
230
- const req = request(`https://${version}-${suffix}-${prefix}.api.metacall.io`).url(`/${type}/${name}`);
243
+ const req = (() => {
244
+ if (hostname === 'localhost') {
245
+ // Old API in commercial FaaS and current API of FaaS reimplementation
246
+ return request().url(`/${prefix}/${suffix}/${version}/${type}/${name}`);
247
+ }
248
+ else {
249
+ // New API used by commercial FaaS
250
+ return request(`https://${version}-${suffix}-${prefix}.api.metacall.io`).url(`/${type}/${name}`);
251
+ }
252
+ })();
231
253
  if (args === undefined) {
232
254
  req.method('GET');
233
255
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metacall/protocol",
3
- "version": "0.1.29",
3
+ "version": "0.1.30",
4
4
  "description": "Tool for deploying into MetaCall FaaS platform.",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js",
package/src/protocol.ts CHANGED
@@ -16,6 +16,7 @@
16
16
  fileList: get files of a repository by branch
17
17
  */
18
18
 
19
+ import { Readable } from 'stream';
19
20
  import { URL } from 'url';
20
21
  import { Create, Deployment, LogType, MetaCallJSON } from './deployment';
21
22
  import { Plans } from './plan';
@@ -202,7 +203,7 @@ class Request {
202
203
  return this;
203
204
  }
204
205
 
205
- blob(body: BodyInit): Request {
206
+ bodyRaw(body: BodyInit): Request {
206
207
  this.impl.body = body;
207
208
  return this;
208
209
  }
@@ -259,6 +260,7 @@ class Request {
259
260
 
260
261
  export default (token: string, baseURL: string): API => {
261
262
  const request = (url = baseURL) => new Request(token, url);
263
+ const hostname = new URL(baseURL).hostname;
262
264
 
263
265
  const api: API = {
264
266
  refresh: (): Promise<string> =>
@@ -316,7 +318,7 @@ export default (token: string, baseURL: string): API => {
316
318
 
317
319
  upload: async (
318
320
  name: string,
319
- blob: Blob,
321
+ data: Blob | Readable,
320
322
  jsons: MetaCallJSON[] = [],
321
323
  runners: string[] = []
322
324
  ): Promise<Resource> => {
@@ -326,12 +328,31 @@ export default (token: string, baseURL: string): API => {
326
328
  fd.append('type', 'application/x-zip-compressed');
327
329
  fd.append('jsons', JSON.stringify(jsons));
328
330
  fd.append('runners', JSON.stringify(runners));
329
- fd.append('raw', blob, 'blob');
331
+
332
+ if (data instanceof Blob) {
333
+ fd.append('raw', data, `${name}.zip`);
334
+ } else if (data instanceof Readable) {
335
+ // This is terrible but NodeJS does not ensure that streaming and zero
336
+ // copy will be performed anyway, as the sizes are not really big (150mb is the limit)
337
+ // we can do this nasty intermediate buffer creation and forget about it
338
+ const chunks: Uint8Array[] = [];
339
+ for await (const chunk of data) {
340
+ chunks.push(
341
+ typeof chunk === 'string' ? Buffer.from(chunk) : chunk
342
+ );
343
+ }
344
+ const buffer = Buffer.concat(chunks);
345
+ fd.append('raw', new Blob([buffer]), `${name}.zip`);
346
+ } else {
347
+ throw Error(
348
+ `Type ${typeof data} not supported, use Blob or Readable`
349
+ );
350
+ }
330
351
 
331
352
  return await request()
332
353
  .url('/api/package/create')
333
354
  .method('POST')
334
- .blob(fd)
355
+ .bodyRaw(fd)
335
356
  .asJson<Resource>();
336
357
  },
337
358
 
@@ -433,15 +454,19 @@ export default (token: string, baseURL: string): API => {
433
454
  name: string,
434
455
  args?: Args
435
456
  ): Promise<Result> => {
436
- // Old API
437
- // const req = request('https://api.metacall.io').url(
438
- // `/${prefix}/${suffix}/${version}/${type}/${name}`
439
- // );
440
-
441
- // New API
442
- const req = request(
443
- `https://${version}-${suffix}-${prefix}.api.metacall.io`
444
- ).url(`/${type}/${name}`);
457
+ const req = (() => {
458
+ if (hostname === 'localhost') {
459
+ // Old API in commercial FaaS and current API of FaaS reimplementation
460
+ return request().url(
461
+ `/${prefix}/${suffix}/${version}/${type}/${name}`
462
+ );
463
+ } else {
464
+ // New API used by commercial FaaS
465
+ return request(
466
+ `https://${version}-${suffix}-${prefix}.api.metacall.io`
467
+ ).url(`/${type}/${name}`);
468
+ }
469
+ })();
445
470
 
446
471
  if (args === undefined) {
447
472
  req.method('GET');