@editframe/api 0.7.0-beta.8 → 0.8.0-beta.10

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.
Files changed (59) hide show
  1. package/dist/CHUNK_SIZE_BYTES.d.ts +1 -0
  2. package/dist/CHUNK_SIZE_BYTES.js +7 -0
  3. package/dist/client.d.ts +6 -0
  4. package/dist/client.js +13 -7
  5. package/dist/client.test.d.ts +1 -0
  6. package/dist/index.d.ts +8 -0
  7. package/dist/index.js +11 -1
  8. package/dist/readableFromBuffers.d.ts +2 -0
  9. package/dist/resources/caption-file.d.ts +39 -0
  10. package/dist/resources/caption-file.js +28 -26
  11. package/dist/resources/caption-file.test.d.ts +1 -0
  12. package/dist/resources/image-file.d.ts +31 -0
  13. package/dist/resources/image-file.js +23 -27
  14. package/dist/resources/image-file.test.d.ts +1 -0
  15. package/dist/resources/isobmff-file.d.ts +19 -0
  16. package/dist/resources/isobmff-file.js +17 -23
  17. package/dist/resources/isobmff-file.test.d.ts +1 -0
  18. package/dist/resources/isobmff-track.d.ts +270 -0
  19. package/dist/resources/isobmff-track.js +18 -31
  20. package/dist/resources/isobmff-track.test.d.ts +1 -0
  21. package/dist/resources/renders.d.ts +34 -0
  22. package/dist/resources/renders.js +17 -21
  23. package/dist/resources/renders.test.d.ts +1 -0
  24. package/dist/resources/unprocessed-file.d.ts +45 -0
  25. package/dist/resources/unprocessed-file.js +133 -0
  26. package/dist/resources/unprocessed-file.test.d.ts +1 -0
  27. package/dist/resources/url-token.d.ts +5 -0
  28. package/dist/resources/url-token.js +20 -0
  29. package/dist/resources/url-token.test.d.ts +1 -0
  30. package/dist/streamChunker.d.ts +2 -0
  31. package/dist/streamChunker.js +17 -0
  32. package/dist/streamChunker.test.d.ts +1 -0
  33. package/dist/uploadChunks.d.ts +10 -0
  34. package/dist/uploadChunks.js +65 -0
  35. package/dist/uploadChunks.test.d.ts +1 -0
  36. package/package.json +11 -11
  37. package/src/resources/caption-file.test.ts +124 -0
  38. package/src/resources/caption-file.ts +50 -25
  39. package/src/resources/image-file.test.ts +138 -0
  40. package/src/resources/image-file.ts +29 -26
  41. package/src/resources/isobmff-file.test.ts +108 -0
  42. package/src/resources/isobmff-file.ts +20 -23
  43. package/src/resources/isobmff-track.test.ts +152 -0
  44. package/src/resources/isobmff-track.ts +23 -32
  45. package/src/resources/renders.test.ts +112 -0
  46. package/src/resources/renders.ts +20 -21
  47. package/src/resources/test-av-file.txt +1 -0
  48. package/src/resources/unprocessed-file.test.ts +312 -0
  49. package/src/resources/unprocessed-file.ts +189 -0
  50. package/src/resources/url-token.test.ts +46 -0
  51. package/src/resources/url-token.ts +27 -0
  52. package/dist/client.cjs +0 -29
  53. package/dist/index.cjs +0 -24
  54. package/dist/resources/caption-file.cjs +0 -56
  55. package/dist/resources/image-file.cjs +0 -52
  56. package/dist/resources/isobmff-file.cjs +0 -56
  57. package/dist/resources/isobmff-track.cjs +0 -71
  58. package/dist/resources/renders.cjs +0 -56
  59. package/src/util/nodeStreamToWebStream.ts +0 -20
@@ -0,0 +1,312 @@
1
+ import { join } from "node:path";
2
+
3
+ import { test, expect, beforeAll, afterEach, afterAll, describe } from "vitest";
4
+ import { http, HttpResponse } from "msw";
5
+ import { setupServer } from "msw/node";
6
+ import { ZodError } from "zod";
7
+
8
+ import { Client } from "../client.ts";
9
+ import {
10
+ createUnprocessedFile,
11
+ processAVFile,
12
+ processAVFileBuffer,
13
+ updateUnprocessedFile,
14
+ uploadUnprocessedFile,
15
+ } from "./unprocessed-file.ts";
16
+ import { readableFromBuffers } from "../readableFromBuffers.ts";
17
+
18
+ const server = setupServer();
19
+ const client = new Client("ef_TEST_TOKEN", "http://localhost");
20
+
21
+ const TEST_AV_FILE = join(__dirname, "test-av-file.txt");
22
+
23
+ describe("Unprocessed File", () => {
24
+ beforeAll(() => server.listen());
25
+ afterEach(() => server.resetHandlers());
26
+ afterAll(() => server.close());
27
+
28
+ describe("createUnprocessedFile", () => {
29
+ test("Throws when file is too large", async () => {
30
+ await expect(
31
+ createUnprocessedFile(client, {
32
+ id: "test-file",
33
+ filename: "test-file",
34
+ processes: [],
35
+ byte_size: 1024 * 1024 * 1025,
36
+ }),
37
+ ).rejects.toThrowError(
38
+ new ZodError([
39
+ {
40
+ code: "too_big",
41
+ maximum: 1073741824,
42
+ type: "number",
43
+ inclusive: true,
44
+ exact: false,
45
+ message: "Number must be less than or equal to 1073741824",
46
+ path: ["byte_size"],
47
+ },
48
+ ]),
49
+ );
50
+ });
51
+
52
+ test("Throws when server returns an error", async () => {
53
+ server.use(
54
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
55
+ HttpResponse.text("Internal Server Error", { status: 500 }),
56
+ ),
57
+ );
58
+
59
+ await expect(
60
+ createUnprocessedFile(client, {
61
+ id: "test-file",
62
+ filename: "test-file",
63
+ processes: [],
64
+ byte_size: 1024 * 1024,
65
+ }),
66
+ ).rejects.toThrowError(
67
+ "Failed to create unprocessed file 500 Internal Server Error",
68
+ );
69
+ });
70
+
71
+ test("Returns json data from the http response", async () => {
72
+ server.use(
73
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
74
+ HttpResponse.json(
75
+ { testResponse: "test" },
76
+ { status: 200, statusText: "OK" },
77
+ ),
78
+ ),
79
+ );
80
+
81
+ const result = await createUnprocessedFile(client, {
82
+ id: "test-file",
83
+ filename: "test-file",
84
+ processes: [],
85
+ byte_size: 1024 * 1024,
86
+ });
87
+
88
+ expect(result).toEqual({ testResponse: "test" });
89
+ });
90
+ });
91
+
92
+ describe("updateUnprocessedFile", () => {
93
+ test("Throws when server responds with an error", async () => {
94
+ server.use(
95
+ http.post("http://localhost/api/v1/unprocessed_files/test-file", () =>
96
+ HttpResponse.text("Internal Server Error", { status: 500 }),
97
+ ),
98
+ );
99
+
100
+ await expect(
101
+ updateUnprocessedFile(client, "test-file", {
102
+ processes: [],
103
+ }),
104
+ ).rejects.toThrowError(
105
+ "Failed to update unprocessed file 500 Internal Server Error",
106
+ );
107
+ });
108
+
109
+ test("Returns json data from the http response", async () => {
110
+ server.use(
111
+ http.post("http://localhost/api/v1/unprocessed_files/test-file", () =>
112
+ HttpResponse.json(
113
+ { testResponse: "test" },
114
+ { status: 200, statusText: "OK" },
115
+ ),
116
+ ),
117
+ );
118
+
119
+ const result = await updateUnprocessedFile(client, "test-file", {
120
+ processes: [],
121
+ });
122
+
123
+ expect(result).toEqual({ testResponse: "test" });
124
+ });
125
+ });
126
+
127
+ describe("uploadUnprocessedFile", () => {
128
+ test("Throws when server responds with an error", async () => {
129
+ server.use(
130
+ http.post(
131
+ "http://localhost/api/v1/unprocessed_files/test-file/upload",
132
+ () => HttpResponse.text("Internal Server Error", { status: 500 }),
133
+ ),
134
+ );
135
+
136
+ await expect(
137
+ uploadUnprocessedFile(
138
+ client,
139
+ "test-file",
140
+ readableFromBuffers(Buffer.from("test")),
141
+ 4,
142
+ ),
143
+ ).rejects.toThrowError(
144
+ "Failed to upload chunk 0 for /api/v1/unprocessed_files/test-file/upload 500 Internal Server Error",
145
+ );
146
+ });
147
+
148
+ test("Succeeds when server returns a success", async () => {
149
+ server.use(
150
+ http.post(
151
+ "http://localhost/api/v1/unprocessed_files/test-file/upload",
152
+ () => HttpResponse.json({}, { status: 201 }),
153
+ ),
154
+ );
155
+
156
+ await expect(
157
+ uploadUnprocessedFile(
158
+ client,
159
+ "test-file",
160
+ readableFromBuffers(Buffer.from("test")),
161
+ 4,
162
+ ),
163
+ ).resolves.toBeUndefined();
164
+ });
165
+ });
166
+
167
+ describe("processAVFileBuffer", () => {
168
+ test("Throws when server responds with an error when creating file", async () => {
169
+ server.use(
170
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
171
+ HttpResponse.text("Internal Server Error", { status: 500 }),
172
+ ),
173
+ );
174
+
175
+ await expect(
176
+ processAVFileBuffer(client, Buffer.from("test"), "test-file"),
177
+ ).rejects.toThrowError(
178
+ "Failed to create unprocessed file 500 Internal Server Error",
179
+ );
180
+ });
181
+
182
+ test("Throws when server responds with an error when uploading file", async () => {
183
+ server.use(
184
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
185
+ HttpResponse.json({}, { status: 200 }),
186
+ ),
187
+ http.post(
188
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
189
+ () => HttpResponse.text("Internal Server Error", { status: 500 }),
190
+ ),
191
+ );
192
+
193
+ await expect(
194
+ processAVFileBuffer(client, Buffer.from("test"), "test-file"),
195
+ ).rejects.toThrowError(
196
+ "Failed to upload chunk 0 for /api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload 500 Internal Server Error",
197
+ );
198
+ });
199
+
200
+ test("Throws when server responds with an error when updating file", async () => {
201
+ server.use(
202
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
203
+ HttpResponse.json({}, { status: 200 }),
204
+ ),
205
+ http.post(
206
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
207
+ () => HttpResponse.json({ test }, { status: 201 }),
208
+ ),
209
+ http.post(
210
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6",
211
+ () => HttpResponse.text("Internal Server Error", { status: 500 }),
212
+ ),
213
+ );
214
+
215
+ await expect(
216
+ processAVFileBuffer(client, Buffer.from("test"), "test-file"),
217
+ ).rejects.toThrowError(
218
+ "Failed to update unprocessed file 500 Internal Server Error",
219
+ );
220
+ });
221
+
222
+ test("Returns json data when upload is successful", async () => {
223
+ server.use(
224
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
225
+ HttpResponse.json({}, { status: 200 }),
226
+ ),
227
+ http.post(
228
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
229
+ () => HttpResponse.json({}, { status: 201 }),
230
+ ),
231
+ http.post(
232
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6",
233
+ () => HttpResponse.json({ test: "response" }, { status: 200 }),
234
+ ),
235
+ );
236
+
237
+ await expect(
238
+ processAVFileBuffer(client, Buffer.from("test"), "test-file"),
239
+ ).resolves.toEqual({ test: "response" });
240
+ });
241
+ });
242
+
243
+ describe("processAVFile", () => {
244
+ test("Throws when server responds with an error when creating file", async () => {
245
+ server.use(
246
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
247
+ HttpResponse.text("Internal Server Error", { status: 500 }),
248
+ ),
249
+ );
250
+
251
+ await expect(processAVFile(client, TEST_AV_FILE)).rejects.toThrowError(
252
+ "Failed to create unprocessed file 500 Internal Server Error",
253
+ );
254
+ });
255
+
256
+ test("Throws when server responds with an error when uploading file", async () => {
257
+ server.use(
258
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
259
+ HttpResponse.json({}, { status: 200 }),
260
+ ),
261
+ http.post(
262
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
263
+ () => HttpResponse.text("Internal Server Error", { status: 500 }),
264
+ ),
265
+ );
266
+
267
+ await expect(processAVFile(client, TEST_AV_FILE)).rejects.toThrowError(
268
+ "Failed to upload chunk 0 for /api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload 500 Internal Server Error",
269
+ );
270
+ });
271
+
272
+ test("Throws when server responds with an error when updating file", async () => {
273
+ server.use(
274
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
275
+ HttpResponse.json({}, { status: 200 }),
276
+ ),
277
+ http.post(
278
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
279
+ () => HttpResponse.json({ test }, { status: 201 }),
280
+ ),
281
+ http.post(
282
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6",
283
+ () => HttpResponse.text("Internal Server Error", { status: 500 }),
284
+ ),
285
+ );
286
+
287
+ await expect(processAVFile(client, TEST_AV_FILE)).rejects.toThrowError(
288
+ "Failed to update unprocessed file 500 Internal Server Error",
289
+ );
290
+ });
291
+
292
+ test("Returns json data when upload is successful", async () => {
293
+ server.use(
294
+ http.post("http://localhost/api/v1/unprocessed_files", () =>
295
+ HttpResponse.json({}, { status: 200 }),
296
+ ),
297
+ http.post(
298
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6/upload",
299
+ () => HttpResponse.json({}, { status: 201 }),
300
+ ),
301
+ http.post(
302
+ "http://localhost/api/v1/unprocessed_files/098f6bcd-4621-d373-cade-4e832627b4f6",
303
+ () => HttpResponse.json({ test: "response" }, { status: 200 }),
304
+ ),
305
+ );
306
+
307
+ await expect(processAVFile(client, TEST_AV_FILE)).resolves.toEqual({
308
+ test: "response",
309
+ });
310
+ });
311
+ });
312
+ });
@@ -0,0 +1,189 @@
1
+ import { Readable } from "node:stream";
2
+ import { basename } from "node:path";
3
+ import { createReadStream } from "node:fs";
4
+ import { stat } from "node:fs/promises";
5
+
6
+ import { z } from "zod";
7
+ import debug from "debug";
8
+
9
+ import { md5FilePath, md5Buffer } from "@editframe/assets";
10
+
11
+ import type { Client } from "../client.ts";
12
+ import { uploadChunks } from "../uploadChunks.ts";
13
+
14
+ const log = debug("ef:api:unprocessed-file");
15
+
16
+ const FileProcessors = z
17
+ .array(z.union([z.literal("isobmff"), z.literal("captions")]))
18
+ .refine(
19
+ (value) => {
20
+ return new Set(value).size === value.length;
21
+ },
22
+ {
23
+ message: "Processors list must not include duplicates",
24
+ },
25
+ );
26
+
27
+ const MAX_FILE_SIZE = 1024 * 1024 * 1024; // 1GiB
28
+
29
+ export const CreateUnprocessedFilePayload = z.object({
30
+ id: z.string(),
31
+ filename: z.string(),
32
+ processes: FileProcessors.optional(),
33
+ byte_size: z.number().int().max(MAX_FILE_SIZE),
34
+ });
35
+
36
+ export const UpdateUnprocessedFilePayload = z.object({
37
+ processes: FileProcessors.optional(),
38
+ });
39
+
40
+ export interface CreateUnprocessedFileResult {
41
+ byte_size: number;
42
+ next_byte: number;
43
+ id: string;
44
+ processes: z.infer<typeof FileProcessors>;
45
+ }
46
+
47
+ export interface UpdateUnprocessedFileResult {
48
+ byte_size?: number;
49
+ next_byte: number;
50
+ id: string;
51
+ processes: z.infer<typeof FileProcessors>;
52
+ }
53
+
54
+ export const createUnprocessedFile = async (
55
+ client: Client,
56
+ payload: z.infer<typeof CreateUnprocessedFilePayload>,
57
+ ) => {
58
+ log("Creating an unprocessed file", payload);
59
+ CreateUnprocessedFilePayload.parse(payload);
60
+ const response = await client.authenticatedFetch(
61
+ "/api/v1/unprocessed_files",
62
+ {
63
+ method: "POST",
64
+ body: JSON.stringify(payload),
65
+ },
66
+ );
67
+
68
+ log(
69
+ "Unprocessed file created",
70
+ response.status,
71
+ response.statusText,
72
+ response.headers,
73
+ );
74
+
75
+ if (response.ok) {
76
+ return (await response.json()) as CreateUnprocessedFileResult;
77
+ }
78
+
79
+ throw new Error(
80
+ `Failed to create unprocessed file ${response.status} ${response.statusText}`,
81
+ );
82
+ };
83
+
84
+ export const updateUnprocessedFile = async (
85
+ client: Client,
86
+ fileId: string,
87
+ payload: Partial<z.infer<typeof UpdateUnprocessedFilePayload>>,
88
+ ) => {
89
+ log("Updating unprocessed file", fileId, payload);
90
+ UpdateUnprocessedFilePayload.parse(payload);
91
+ const response = await client.authenticatedFetch(
92
+ `/api/v1/unprocessed_files/${fileId}`,
93
+ {
94
+ method: "POST",
95
+ body: JSON.stringify(payload),
96
+ },
97
+ );
98
+
99
+ log("Unprocessed file updated", response);
100
+
101
+ if (response.ok) {
102
+ return (await response.json()) as UpdateUnprocessedFileResult;
103
+ }
104
+
105
+ throw new Error(
106
+ `Failed to update unprocessed file ${response.status} ${response.statusText}`,
107
+ );
108
+ };
109
+
110
+ export const uploadUnprocessedFile = async (
111
+ client: Client,
112
+ fileId: string,
113
+ fileStream: Readable,
114
+ fileSize: number,
115
+ ) => {
116
+ log("Uploading unprocessed file", fileId);
117
+
118
+ await uploadChunks(client, {
119
+ url: `/api/v1/unprocessed_files/${fileId}/upload`,
120
+ fileSize,
121
+ fileStream,
122
+ });
123
+
124
+ log("Unprocessed file upload complete");
125
+ };
126
+
127
+ export const processAVFileBuffer = async (
128
+ client: Client,
129
+ buffer: Buffer,
130
+ filename = "buffer",
131
+ ) => {
132
+ log("Processing AV file buffer");
133
+ const fileId = md5Buffer(buffer);
134
+
135
+ log("File ID", fileId);
136
+ log(`File size: ${buffer.byteLength} bytes`);
137
+
138
+ await createUnprocessedFile(client, {
139
+ id: fileId,
140
+ processes: [],
141
+ filename,
142
+ byte_size: buffer.byteLength,
143
+ });
144
+
145
+ const readStream = new Readable({
146
+ read() {
147
+ readStream.push(buffer);
148
+ readStream.push(null);
149
+ },
150
+ });
151
+
152
+ await uploadUnprocessedFile(client, fileId, readStream, buffer.byteLength);
153
+
154
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
155
+ processes: ["isobmff"],
156
+ });
157
+
158
+ log("File processed", fileInformation);
159
+ return fileInformation;
160
+ };
161
+
162
+ export const processAVFile = async (client: Client, filePath: string) => {
163
+ log("Processing AV file", filePath);
164
+ const fileId = await md5FilePath(filePath);
165
+
166
+ log("File ID", fileId);
167
+ await createUnprocessedFile(client, {
168
+ id: fileId,
169
+ processes: [],
170
+ filename: basename(filePath),
171
+ byte_size: (await stat(filePath)).size,
172
+ });
173
+
174
+ const readStream = createReadStream(filePath);
175
+
176
+ await uploadUnprocessedFile(
177
+ client,
178
+ fileId,
179
+ readStream,
180
+ (await stat(filePath)).size,
181
+ );
182
+
183
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
184
+ processes: ["isobmff"],
185
+ });
186
+
187
+ log("File processed", fileInformation);
188
+ return fileInformation;
189
+ };
@@ -0,0 +1,46 @@
1
+ import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
2
+ import { http, HttpResponse } from "msw";
3
+ import { setupServer } from "msw/node";
4
+
5
+ import { Client } from "../client.ts";
6
+ import { createURLToken } from "./url-token.ts";
7
+
8
+ const server = setupServer();
9
+ const client = new Client("ef_TEST_TOKEN", "http://localhost");
10
+
11
+ describe("URL Token", () => {
12
+ beforeAll(() => server.listen());
13
+ afterEach(() => server.resetHandlers());
14
+ afterAll(() => server.close());
15
+
16
+ describe("createURLToken", () => {
17
+ test("Throws when server returns an error", async () => {
18
+ server.use(
19
+ http.post("http://localhost/api/v1/url-token", () =>
20
+ HttpResponse.text("Internal Server Error", { status: 500 }),
21
+ ),
22
+ );
23
+
24
+ await expect(
25
+ createURLToken(client, "http://example.com"),
26
+ ).rejects.toThrowError(
27
+ "Failed to create signed url: 500 Internal Server Error Internal Server Error",
28
+ );
29
+ });
30
+
31
+ test("Returns token from the http response", async () => {
32
+ server.use(
33
+ http.post("http://localhost/api/v1/url-token", () =>
34
+ HttpResponse.json(
35
+ { token: "test-token" },
36
+ { status: 200, statusText: "OK" },
37
+ ),
38
+ ),
39
+ );
40
+
41
+ await expect(createURLToken(client, "http://example.com")).resolves.toBe(
42
+ "test-token",
43
+ );
44
+ });
45
+ });
46
+ });
@@ -0,0 +1,27 @@
1
+ import debug from "debug";
2
+
3
+ import type { Client } from "../client.ts";
4
+
5
+ const log = debug("ef:api:url-token");
6
+
7
+ export interface URLTokenResult {
8
+ token: string;
9
+ }
10
+
11
+ export const createURLToken = async (client: Client, url: string) => {
12
+ log("Creating signed url for", url);
13
+ const response = await client.authenticatedFetch("/api/v1/url-token", {
14
+ method: "POST",
15
+ body: JSON.stringify({
16
+ url,
17
+ }),
18
+ });
19
+
20
+ if (!response.ok) {
21
+ throw new Error(
22
+ `Failed to create signed url: ${response.status} ${response.statusText} ${await response.text()}`,
23
+ );
24
+ }
25
+
26
+ return ((await response.json()) as URLTokenResult).token;
27
+ };
package/dist/client.cjs DELETED
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const debug = require("debug");
4
- const fetch = require("node-fetch");
5
- const log = debug("ef:api:client");
6
- class Client {
7
- constructor(token, efHost) {
8
- this.token = token;
9
- this.efHost = efHost;
10
- this.authenticatedFetch = async (path, init = {}) => {
11
- init.headers ||= {};
12
- log(
13
- "Authenticated fetch",
14
- { path, init },
15
- "(Token will be added as Bearer token)"
16
- );
17
- Object.assign(init.headers, {
18
- Authorization: `Bearer ${this.token}`,
19
- "Content-Type": "application/json"
20
- });
21
- const url = new URL(path, this.efHost);
22
- const response = await fetch(url, init);
23
- log("Authenticated fetch response", response.status, response.statusText);
24
- return response;
25
- };
26
- log("Creating client with efHost", efHost, "and !!token", !!token);
27
- }
28
- }
29
- exports.Client = Client;
package/dist/index.cjs DELETED
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const captionFile = require("./resources/caption-file.cjs");
4
- const imageFile = require("./resources/image-file.cjs");
5
- const isobmffFile = require("./resources/isobmff-file.cjs");
6
- const isobmffTrack = require("./resources/isobmff-track.cjs");
7
- const renders = require("./resources/renders.cjs");
8
- const client = require("./client.cjs");
9
- exports.CreateCaptionFilePayload = captionFile.CreateCaptionFilePayload;
10
- exports.createCaptionFile = captionFile.createCaptionFile;
11
- exports.uploadCaptionFile = captionFile.uploadCaptionFile;
12
- exports.CreateImageFilePayload = imageFile.CreateImageFilePayload;
13
- exports.createImageFile = imageFile.createImageFile;
14
- exports.uploadImageFile = imageFile.uploadImageFile;
15
- exports.CreateISOBMFFFilePayload = isobmffFile.CreateISOBMFFFilePayload;
16
- exports.createISOBMFFFile = isobmffFile.createISOBMFFFile;
17
- exports.uploadFragmentIndex = isobmffFile.uploadFragmentIndex;
18
- exports.CreateISOBMFFTrackPayload = isobmffTrack.CreateISOBMFFTrackPayload;
19
- exports.createISOBMFFTrack = isobmffTrack.createISOBMFFTrack;
20
- exports.uploadISOBMFFTrack = isobmffTrack.uploadISOBMFFTrack;
21
- exports.CreateRenderPayload = renders.CreateRenderPayload;
22
- exports.createRender = renders.createRender;
23
- exports.uploadRender = renders.uploadRender;
24
- exports.Client = client.Client;
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const zod = require("zod");
4
- const debug = require("debug");
5
- const log = debug("ef:api:caption-file");
6
- const CreateCaptionFilePayload = zod.z.object({
7
- id: zod.z.string(),
8
- filename: zod.z.string()
9
- });
10
- const createCaptionFile = async (client, payload) => {
11
- log("Creating caption file", payload);
12
- const fileCreation = await client.authenticatedFetch(
13
- "/api/video2/caption_files",
14
- {
15
- method: "POST",
16
- body: JSON.stringify(payload)
17
- }
18
- );
19
- log("Caption file created", fileCreation);
20
- switch (fileCreation.status) {
21
- case 200: {
22
- return await fileCreation.json();
23
- }
24
- default: {
25
- console.error(
26
- `Failed to create file ${fileCreation.status} ${fileCreation.statusText}`
27
- );
28
- return;
29
- }
30
- }
31
- };
32
- const uploadCaptionFile = async (client, fileId, fileStream) => {
33
- log("Uploading caption file", fileId);
34
- const fileIndex = await client.authenticatedFetch(
35
- `/api/video2/caption_files/${fileId}/upload`,
36
- {
37
- method: "POST",
38
- body: fileStream
39
- }
40
- );
41
- log("Caption file uploaded", fileIndex);
42
- switch (fileIndex.status) {
43
- case 200: {
44
- return fileIndex.json();
45
- }
46
- default: {
47
- console.error(
48
- `Failed to upload caption ${fileIndex.status} ${fileIndex.statusText}`
49
- );
50
- return;
51
- }
52
- }
53
- };
54
- exports.CreateCaptionFilePayload = CreateCaptionFilePayload;
55
- exports.createCaptionFile = createCaptionFile;
56
- exports.uploadCaptionFile = uploadCaptionFile;