Skip to content

Commit

Permalink
update to v8 node sdk beta (#190)
Browse files Browse the repository at this point in the history
* bump to mux-node 8 canary.3

* run npx @getgrit/launcher apply mux_v8

* update verifySignature function to use mux client instance

* update mux.video.assets.delete function

* add node shim for fetch
  • Loading branch information
dylanjha committed Jan 12, 2024
1 parent 19126b5 commit f3cb7df
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 117 deletions.
4 changes: 2 additions & 2 deletions lib/moderation-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Mux from '@mux/mux-node';
import { RequestError } from 'got';
import got from './got-client';

const { Video } = new Mux();
const mux = new Mux();

const ADULT_SCORE_THRESHHOLD = 0.95;
const VIOLENCE_SCORE_THRESHHOLD = 0.85;
Expand Down Expand Up @@ -36,7 +36,7 @@ function shouldAutoDeleteContent(hiveScores?: ModerationScores): boolean {

export async function autoDelete({ assetId, playbackId, hiveScores }: { assetId: string, playbackId: string, hiveScores: ModerationScores }): Promise<boolean> {
if (shouldAutoDeleteContent(hiveScores)) {
await Video.Assets.deletePlaybackId(assetId, playbackId);
await mux.video.assets.deletePlaybackId(assetId, playbackId);
await saveDeletionRecordInAirtable({ assetId, notes: JSON.stringify(hiveScores) });

return true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"@google-cloud/vision": "^2.3.2",
"@mux/blurhash": "^0.1.2",
"@mux/mux-node": "^7.0.0",
"@mux/mux-node": "8.0.0-canary.3",
"@mux/mux-player-react": "^2.3.1",
"@mux/mux-uploader-react": "^1.0.0-beta.15",
"@mux/mux-video-react": "^0.8.2",
Expand Down
6 changes: 3 additions & 3 deletions pages/api/assets/[id].ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { NextApiRequest, NextApiResponse } from 'next';
import Mux from '@mux/mux-node';

const { Video } = new Mux();
const mux = new Mux();

export default async function assetHandler (req: NextApiRequest, res: NextApiResponse): Promise<void> {
const { method } = req;

switch (method) {
case 'GET':
try {
const asset = await Video.Assets.get(req.query.id as string);
const asset = await mux.video.assets.retrieve(req.query.id as string);
if (!(asset.playback_ids && asset.playback_ids[0])) {
throw new Error('Error getting playback_id from asset');
}
Expand Down Expand Up @@ -37,7 +37,7 @@ export default async function assetHandler (req: NextApiRequest, res: NextApiRes
}

try {
await Video.Assets.del(req.query.id as string);
await mux.video.assets.delete(req.query.id as string);
res.status(200).end(`Deleted ${req.query.id}`);
} catch (e) {
res.statusCode = 500;
Expand Down
4 changes: 2 additions & 2 deletions pages/api/uploads/[id].ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { NextApiRequest, NextApiResponse } from 'next';
import Mux from '@mux/mux-node';

const { Video } = new Mux();
const mux = new Mux();

export default async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
const { method } = req;

switch (method) {
case 'GET':
try {
const upload = await Video.Uploads.get(req.query.id as string);
const upload = await mux.video.uploads.retrieve(req.query.id as string);
res.json({
upload: {
status: upload.status,
Expand Down
8 changes: 4 additions & 4 deletions pages/api/uploads/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { NextApiRequest, NextApiResponse } from 'next';
import Mux from '@mux/mux-node';

const { Video } = new Mux();
const mux = new Mux();

export default async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
const { method } = req;

switch (method) {
case 'POST':
try {
const upload = await Video.Uploads.create({
new_asset_settings: { playback_policy: 'public' },
cors_origin: '*',
const upload = await mux.video.uploads.create({
new_asset_settings: { playback_policy: ['public'] },
cors_origin: '*'
});
res.json({
id: upload.id,
Expand Down
3 changes: 2 additions & 1 deletion pages/api/webhooks/mux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { getScores as moderationHive } from '../../../lib/moderation-hive';
import { autoDelete } from '../../../lib/moderation-action';

const webhookSignatureSecret = process.env.MUX_WEBHOOK_SIGNATURE_SECRET;
const mux = new Mux();

const verifyWebhookSignature = (rawBody: string | Buffer, req: NextApiRequest) => {
if (webhookSignatureSecret) {
// this will raise an error if signature is not valid
Mux.Webhooks.verifyHeader(rawBody, req.headers['mux-signature'] as string, webhookSignatureSecret);
mux.webhooks.verifySignature(Buffer.isBuffer(rawBody) ? rawBody.toString('utf8') : rawBody, req.headers, webhookSignatureSecret)
} else {
console.log('Skipping webhook sig verification because no secret is configured'); // eslint-disable-line no-console
}
Expand Down
1 change: 1 addition & 0 deletions test/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import dotenv from 'dotenv';
import '@mux/mux-node/shims/node'

dotenv.config({ path: '.env.test' });

Expand Down

1 comment on commit f3cb7df

@vercel
Copy link

@vercel vercel bot commented on f3cb7df Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

stream-new – ./

stream-new-mux.vercel.app
stream-new-git-main-mux.vercel.app
stream-new.vercel.app
stream.new
encode.new

Please sign in to comment.