@editframe/api 0.7.0-beta.8 → 0.8.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.
@@ -0,0 +1,142 @@
1
+ import { Readable } from "node:stream";
2
+ import { basename } from "node:path";
3
+ import { createReadStream } from "node:fs";
4
+ import { z } from "zod";
5
+ import debug from "debug";
6
+ import { md5Buffer, md5FilePath } from "@editframe/assets";
7
+ const log = debug("ef:api:unprocessed-file");
8
+ const FileProcessors = z.array(z.union([z.literal("isobmff"), z.literal("captions")])).refine(
9
+ (value) => {
10
+ return new Set(value).size === value.length;
11
+ },
12
+ {
13
+ message: "Processors list must not include duplicates"
14
+ }
15
+ );
16
+ const CreateUnprocessedFilePayload = z.object({
17
+ id: z.string(),
18
+ filename: z.string(),
19
+ processes: FileProcessors.optional()
20
+ });
21
+ const UpdateUnprocessedFilePayload = z.object({
22
+ processes: FileProcessors.optional()
23
+ });
24
+ const createUnprocessedFile = async (client, payload) => {
25
+ log("Creating an unprocessed file", payload);
26
+ const response = await client.authenticatedFetch(
27
+ "/api/v1/unprocessed_files",
28
+ {
29
+ method: "POST",
30
+ body: JSON.stringify(payload)
31
+ }
32
+ );
33
+ log(
34
+ "Unprocessed file created",
35
+ response.status,
36
+ response.statusText,
37
+ response.headers
38
+ );
39
+ switch (response.status) {
40
+ case 200: {
41
+ return await response.json();
42
+ }
43
+ default: {
44
+ console.error(
45
+ `Failed to create file ${response.status} ${response.statusText}`
46
+ );
47
+ console.error(await response.text());
48
+ throw new Error("Failed to create unprocessed file");
49
+ }
50
+ }
51
+ };
52
+ const updateUnprocessedFile = async (client, fileId, payload) => {
53
+ log("Updating unprocessed file", fileId, payload);
54
+ const response = await client.authenticatedFetch(
55
+ `/api/v1/unprocessed_files/${fileId}`,
56
+ {
57
+ method: "POST",
58
+ body: JSON.stringify(payload)
59
+ }
60
+ );
61
+ log("Unprocessed file updated", response);
62
+ switch (response.status) {
63
+ case 200: {
64
+ return await response.json();
65
+ }
66
+ default: {
67
+ console.error(
68
+ `Failed to update file ${response.status} ${response.statusText}`
69
+ );
70
+ throw new Error("Failed to update unprocessed file");
71
+ }
72
+ }
73
+ };
74
+ const uploadUnprocessedFile = async (client, fileId, fileStream) => {
75
+ log("Uploading unprocessed file", fileId);
76
+ const unprocessedFile = await client.authenticatedFetch(
77
+ `/api/v1/unprocessed_files/${fileId}/upload`,
78
+ {
79
+ method: "POST",
80
+ body: fileStream
81
+ }
82
+ );
83
+ log("Unprocessed file track uploaded", unprocessedFile);
84
+ switch (unprocessedFile.status) {
85
+ case 200: {
86
+ return unprocessedFile.json();
87
+ }
88
+ default: {
89
+ console.error("Failed to upload unprocessed file");
90
+ console.error(unprocessedFile.status, unprocessedFile.statusText);
91
+ throw new Error("Failed to upload unprocessed file");
92
+ }
93
+ }
94
+ };
95
+ const processAVFileBuffer = async (client, buffer, filename = "buffer") => {
96
+ log("Processing AV file buffer");
97
+ const fileId = md5Buffer(buffer);
98
+ log("File ID", fileId);
99
+ await createUnprocessedFile(client, {
100
+ id: fileId,
101
+ processes: [],
102
+ filename
103
+ });
104
+ const readStream = new Readable({
105
+ read() {
106
+ readStream.push(buffer);
107
+ readStream.push(null);
108
+ }
109
+ });
110
+ await uploadUnprocessedFile(client, fileId, readStream);
111
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
112
+ processes: ["isobmff"]
113
+ });
114
+ log("File processed", fileInformation);
115
+ return fileInformation;
116
+ };
117
+ const processAVFile = async (client, filePath) => {
118
+ log("Processing AV file", filePath);
119
+ const fileId = await md5FilePath(filePath);
120
+ log("File ID", fileId);
121
+ await createUnprocessedFile(client, {
122
+ id: fileId,
123
+ processes: [],
124
+ filename: basename(filePath)
125
+ });
126
+ const readStream = createReadStream(filePath);
127
+ await uploadUnprocessedFile(client, fileId, readStream);
128
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
129
+ processes: ["isobmff"]
130
+ });
131
+ log("File processed", fileInformation);
132
+ return fileInformation;
133
+ };
134
+ export {
135
+ CreateUnprocessedFilePayload,
136
+ UpdateUnprocessedFilePayload,
137
+ createUnprocessedFile,
138
+ processAVFile,
139
+ processAVFileBuffer,
140
+ updateUnprocessedFile,
141
+ uploadUnprocessedFile
142
+ };
package/package.json CHANGED
@@ -1,16 +1,12 @@
1
1
  {
2
2
  "name": "@editframe/api",
3
- "version": "0.7.0-beta.8",
3
+ "version": "0.8.0-beta.1",
4
4
  "description": "API functions for EditFrame",
5
5
  "exports": {
6
6
  ".": {
7
7
  "import": {
8
- "default": "./dist/index.js",
9
- "types": "./dist/index.d.ts"
10
- },
11
- "require": {
12
- "default": "./dist/index.cjs",
13
- "types": "./dist/index.d.ts"
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
14
10
  }
15
11
  }
16
12
  },
@@ -23,15 +19,18 @@
23
19
  "author": "",
24
20
  "license": "UNLICENSED",
25
21
  "devDependencies": {
26
- "@types/node": "^20.14.9",
27
- "typescript": "^5.2.2",
22
+ "@types/jsonwebtoken": "^9.0.6",
23
+ "@types/node": "^20.14.13",
24
+ "typescript": "^5.5.4",
28
25
  "vite": "^5.2.11",
29
26
  "vite-plugin-dts": "^3.9.1",
30
27
  "vite-tsconfig-paths": "^4.3.2"
31
28
  },
32
29
  "dependencies": {
33
- "@editframe/assets": "0.7.0-beta.8",
30
+ "@editframe/assets": "0.8.0-beta.1",
34
31
  "debug": "^4.3.5",
32
+ "jsonwebtoken": "^9.0.2",
33
+ "node-fetch": "^3.3.2",
35
34
  "zod": "^3.23.8"
36
35
  }
37
36
  }
@@ -3,7 +3,7 @@ import type { Readable } from "node:stream";
3
3
  import { z } from "zod";
4
4
  import debug from "debug";
5
5
 
6
- import type { Client } from "../client";
6
+ import type { Client } from "../client.ts";
7
7
 
8
8
  const log = debug("ef:api:caption-file");
9
9
 
@@ -3,7 +3,7 @@ import type { Readable } from "node:stream";
3
3
  import { z } from "zod";
4
4
  import debug from "debug";
5
5
 
6
- import type { Client } from "../client";
6
+ import type { Client } from "../client.ts";
7
7
 
8
8
  const log = debug("ef:api:image-file");
9
9
 
@@ -3,7 +3,7 @@ import type { Readable } from "node:stream";
3
3
  import { z } from "zod";
4
4
  import debug from "debug";
5
5
 
6
- import type { Client } from "../client";
6
+ import type { Client } from "../client.ts";
7
7
 
8
8
  const log = debug("ef:api:isobmff-file");
9
9
 
@@ -5,7 +5,7 @@ import debug from "debug";
5
5
 
6
6
  import { AudioStreamSchema, VideoStreamSchema } from "@editframe/assets";
7
7
 
8
- import type { Client } from "../client";
8
+ import type { Client } from "../client.ts";
9
9
 
10
10
  const log = debug("ef:api:isobmff-track");
11
11
 
@@ -3,7 +3,7 @@ import type { Readable } from "node:stream";
3
3
  import { z } from "zod";
4
4
  import debug from "debug";
5
5
 
6
- import type { Client } from "../client";
6
+ import type { Client } from "../client.ts";
7
7
 
8
8
  const log = debug("ef:api:renders");
9
9
 
@@ -0,0 +1,27 @@
1
+ import debug from "debug";
2
+
3
+ import type { Client } from "../client.ts";
4
+
5
+ const log = debug("ef:api:signed-url");
6
+
7
+ export interface SignedURLResult {
8
+ url: string;
9
+ }
10
+
11
+ export const createSignedURL = async (client: Client, url: string) => {
12
+ log("Creating signed url for", url);
13
+ const response = await client.authenticatedFetch("/api/v1/signed-url", {
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 SignedURLResult).url;
27
+ };
@@ -0,0 +1,192 @@
1
+ import { Readable } from "node:stream";
2
+ import { basename } from "node:path";
3
+ import { createReadStream } from "node:fs";
4
+
5
+ import { z } from "zod";
6
+ import debug from "debug";
7
+
8
+ import { md5FilePath, md5Buffer } from "@editframe/assets";
9
+
10
+ import type { Client } from "../client.ts";
11
+
12
+ const log = debug("ef:api:unprocessed-file");
13
+
14
+ const FileProcessors = z
15
+ .array(z.union([z.literal("isobmff"), z.literal("captions")]))
16
+ .refine(
17
+ (value) => {
18
+ return new Set(value).size === value.length;
19
+ },
20
+ {
21
+ message: "Processors list must not include duplicates",
22
+ },
23
+ );
24
+
25
+ export const CreateUnprocessedFilePayload = z.object({
26
+ id: z.string(),
27
+ filename: z.string(),
28
+ processes: FileProcessors.optional(),
29
+ });
30
+
31
+ export const UpdateUnprocessedFilePayload = z.object({
32
+ processes: FileProcessors.optional(),
33
+ });
34
+
35
+ export interface CreateUnprocessedFileResult {
36
+ byte_size: number;
37
+ last_received_byte: number;
38
+ id: string;
39
+ processes: z.infer<typeof FileProcessors>;
40
+ }
41
+
42
+ export interface UpdateUnprocessedFileResult {
43
+ byte_size: number;
44
+ last_received_byte: number;
45
+ id: string;
46
+ processes: z.infer<typeof FileProcessors>;
47
+ }
48
+
49
+ export const createUnprocessedFile = async (
50
+ client: Client,
51
+ payload: z.infer<typeof CreateUnprocessedFilePayload>,
52
+ ) => {
53
+ log("Creating an unprocessed file", payload);
54
+ const response = await client.authenticatedFetch(
55
+ "/api/v1/unprocessed_files",
56
+ {
57
+ method: "POST",
58
+ body: JSON.stringify(payload),
59
+ },
60
+ );
61
+
62
+ log(
63
+ "Unprocessed file created",
64
+ response.status,
65
+ response.statusText,
66
+ response.headers,
67
+ );
68
+
69
+ switch (response.status) {
70
+ case 200: {
71
+ return (await response.json()) as CreateUnprocessedFileResult;
72
+ }
73
+ default: {
74
+ console.error(
75
+ `Failed to create file ${response.status} ${response.statusText}`,
76
+ );
77
+ console.error(await response.text());
78
+ throw new Error("Failed to create unprocessed file");
79
+ }
80
+ }
81
+ };
82
+
83
+ export const updateUnprocessedFile = async (
84
+ client: Client,
85
+ fileId: string,
86
+ payload: Partial<z.infer<typeof UpdateUnprocessedFilePayload>>,
87
+ ) => {
88
+ log("Updating unprocessed file", fileId, payload);
89
+ const response = await client.authenticatedFetch(
90
+ `/api/v1/unprocessed_files/${fileId}`,
91
+ {
92
+ method: "POST",
93
+ body: JSON.stringify(payload),
94
+ },
95
+ );
96
+
97
+ log("Unprocessed file updated", response);
98
+
99
+ switch (response.status) {
100
+ case 200: {
101
+ return (await response.json()) as UpdateUnprocessedFileResult;
102
+ }
103
+ default: {
104
+ console.error(
105
+ `Failed to update file ${response.status} ${response.statusText}`,
106
+ );
107
+ throw new Error("Failed to update unprocessed file");
108
+ }
109
+ }
110
+ };
111
+
112
+ export const uploadUnprocessedFile = async (
113
+ client: Client,
114
+ fileId: string,
115
+ fileStream: Readable,
116
+ ) => {
117
+ log("Uploading unprocessed file", fileId);
118
+ const unprocessedFile = await client.authenticatedFetch(
119
+ `/api/v1/unprocessed_files/${fileId}/upload`,
120
+ {
121
+ method: "POST",
122
+ body: fileStream,
123
+ },
124
+ );
125
+
126
+ log("Unprocessed file track uploaded", unprocessedFile);
127
+ switch (unprocessedFile.status) {
128
+ case 200: {
129
+ return unprocessedFile.json();
130
+ }
131
+ default: {
132
+ console.error("Failed to upload unprocessed file");
133
+ console.error(unprocessedFile.status, unprocessedFile.statusText);
134
+ throw new Error("Failed to upload unprocessed file");
135
+ }
136
+ }
137
+ };
138
+
139
+ export const processAVFileBuffer = async (
140
+ client: Client,
141
+ buffer: Buffer,
142
+ filename = "buffer",
143
+ ) => {
144
+ log("Processing AV file buffer");
145
+ const fileId = md5Buffer(buffer);
146
+
147
+ log("File ID", fileId);
148
+ await createUnprocessedFile(client, {
149
+ id: fileId,
150
+ processes: [],
151
+ filename,
152
+ });
153
+
154
+ const readStream = new Readable({
155
+ read() {
156
+ readStream.push(buffer);
157
+ readStream.push(null);
158
+ },
159
+ });
160
+
161
+ await uploadUnprocessedFile(client, fileId, readStream);
162
+
163
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
164
+ processes: ["isobmff"],
165
+ });
166
+
167
+ log("File processed", fileInformation);
168
+ return fileInformation;
169
+ };
170
+
171
+ export const processAVFile = async (client: Client, filePath: string) => {
172
+ log("Processing AV file", filePath);
173
+ const fileId = await md5FilePath(filePath);
174
+
175
+ log("File ID", fileId);
176
+ await createUnprocessedFile(client, {
177
+ id: fileId,
178
+ processes: [],
179
+ filename: basename(filePath),
180
+ });
181
+
182
+ const readStream = createReadStream(filePath);
183
+
184
+ await uploadUnprocessedFile(client, fileId, readStream);
185
+
186
+ const fileInformation = await updateUnprocessedFile(client, fileId, {
187
+ processes: ["isobmff"],
188
+ });
189
+
190
+ log("File processed", fileInformation);
191
+ return fileInformation;
192
+ };
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;
@@ -1,52 +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:image-file");
6
- const CreateImageFilePayload = zod.z.object({
7
- id: zod.z.string(),
8
- height: zod.z.number().int(),
9
- width: zod.z.number().int(),
10
- mime_type: zod.z.enum(["image/jpeg", "image/png", "image/jpg", "image/webp"]),
11
- filename: zod.z.string()
12
- });
13
- const createImageFile = async (client, payload) => {
14
- log("Creating image file", payload);
15
- const response = await client.authenticatedFetch("/api/video2/image_files", {
16
- method: "POST",
17
- body: JSON.stringify(payload)
18
- });
19
- log("Image file created", response);
20
- switch (response.status) {
21
- case 200: {
22
- return await response.json();
23
- }
24
- default: {
25
- console.error(
26
- `Failed to create file ${response.status} ${response.statusText}`
27
- );
28
- return;
29
- }
30
- }
31
- };
32
- const uploadImageFile = async (client, fileId, fileStream) => {
33
- const fileIndex = await client.authenticatedFetch(
34
- `/api/video2/image_files/${fileId}/upload`,
35
- {
36
- method: "POST",
37
- body: fileStream
38
- }
39
- );
40
- switch (fileIndex.status) {
41
- case 200: {
42
- return fileIndex.json();
43
- }
44
- default: {
45
- console.error("Failed to upload image");
46
- return;
47
- }
48
- }
49
- };
50
- exports.CreateImageFilePayload = CreateImageFilePayload;
51
- exports.createImageFile = createImageFile;
52
- exports.uploadImageFile = uploadImageFile;
@@ -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:isobmff-file");
6
- const CreateISOBMFFFilePayload = zod.z.object({
7
- id: zod.z.string(),
8
- filename: zod.z.string()
9
- });
10
- const createISOBMFFFile = async (client, payload) => {
11
- log("Creating isobmff file", payload);
12
- const response = await client.authenticatedFetch(
13
- "/api/video2/isobmff_files",
14
- {
15
- method: "POST",
16
- body: JSON.stringify(payload)
17
- }
18
- );
19
- log("ISOBMFF file created", response);
20
- switch (response.status) {
21
- case 200: {
22
- return await response.json();
23
- }
24
- default: {
25
- console.error(
26
- `Failed to create file ${response.status} ${response.statusText}`
27
- );
28
- return;
29
- }
30
- }
31
- };
32
- const uploadFragmentIndex = async (client, fileId, fileStream) => {
33
- log("Uploading fragment index", fileId);
34
- const fileIndex = await client.authenticatedFetch(
35
- `/api/video2/isobmff_files/${fileId}/index/upload`,
36
- {
37
- method: "POST",
38
- body: fileStream
39
- }
40
- );
41
- log("Fragment index uploaded", fileIndex);
42
- switch (fileIndex.status) {
43
- case 200: {
44
- return fileIndex.json();
45
- }
46
- default: {
47
- console.error(
48
- `Failed to create fragment index ${fileIndex.status} ${fileIndex.statusText}`
49
- );
50
- return;
51
- }
52
- }
53
- };
54
- exports.CreateISOBMFFFilePayload = CreateISOBMFFFilePayload;
55
- exports.createISOBMFFFile = createISOBMFFFile;
56
- exports.uploadFragmentIndex = uploadFragmentIndex;