From 81729703d937b586a893eb698e06a1ae6e18543e Mon Sep 17 00:00:00 2001 From: Brooooooklyn Date: Mon, 29 Apr 2024 04:46:26 +0000 Subject: [PATCH] chore(server): use native file-type implementation (#6686) --- Cargo.lock | 7 ++++ packages/backend/native/Cargo.toml | 1 + packages/backend/native/index.d.ts | 2 + packages/backend/native/index.js | 4 +- packages/backend/native/src/file_type.rs | 8 ++++ packages/backend/native/src/lib.rs | 1 + packages/backend/server/package.json | 1 - .../server/src/fundamentals/storage/native.ts | 2 + .../fundamentals/storage/providers/utils.ts | 5 +-- yarn.lock | 41 ------------------- 10 files changed, 25 insertions(+), 47 deletions(-) create mode 100644 packages/backend/native/src/file_type.rs diff --git a/Cargo.lock b/Cargo.lock index 3a65d52302..87ac3059cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,7 @@ name = "affine_server_native" version = "1.0.0" dependencies = [ "chrono", + "file-format", "napi", "napi-build", "napi-derive", @@ -434,6 +435,12 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "658bd65b1cf4c852a3cc96f18a8ce7b5640f6b703f905c7d74532294c2a63984" +[[package]] +name = "file-format" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ba1b81b3c213cf1c071f8bf3b83531f310df99642e58c48247272eef006cae5" + [[package]] name = "filetime" version = "0.2.23" diff --git a/packages/backend/native/Cargo.toml b/packages/backend/native/Cargo.toml index 2c2fe645f8..6a32bbf4e3 100644 --- a/packages/backend/native/Cargo.toml +++ b/packages/backend/native/Cargo.toml @@ -8,6 +8,7 @@ crate-type = ["cdylib"] [dependencies] chrono = "0.4" +file-format = { version = "0.24", features = ["reader"] } napi = { version = "2", default-features = false, features = [ "napi5", "async", diff --git a/packages/backend/native/index.d.ts b/packages/backend/native/index.d.ts index 9fe6df5a2f..355a397009 100644 --- a/packages/backend/native/index.d.ts +++ b/packages/backend/native/index.d.ts @@ -1,6 +1,8 @@ /* auto-generated by NAPI-RS */ /* eslint-disable */ +export function getMime(input: Uint8Array): string + /** * Merge updates in form like `Y.applyUpdate(doc, update)` way and return the * result binary. diff --git a/packages/backend/native/index.js b/packages/backend/native/index.js index 8dd0c023c2..3e54dec315 100644 --- a/packages/backend/native/index.js +++ b/packages/backend/native/index.js @@ -3,9 +3,9 @@ import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); /** @type {import('.')} */ -const binding = require('./storage.node'); +const binding = require('./server-native.node'); -export const Storage = binding.Storage; export const mergeUpdatesInApplyWay = binding.mergeUpdatesInApplyWay; export const verifyChallengeResponse = binding.verifyChallengeResponse; export const mintChallengeResponse = binding.mintChallengeResponse; +export const getMime = binding.getMime; diff --git a/packages/backend/native/src/file_type.rs b/packages/backend/native/src/file_type.rs new file mode 100644 index 0000000000..4dc79192a7 --- /dev/null +++ b/packages/backend/native/src/file_type.rs @@ -0,0 +1,8 @@ +use napi_derive::napi; + +#[napi] +pub fn get_mime(input: &[u8]) -> String { + file_format::FileFormat::from_bytes(input) + .media_type() + .to_string() +} diff --git a/packages/backend/native/src/lib.rs b/packages/backend/native/src/lib.rs index 40d119ae55..ff92552302 100644 --- a/packages/backend/native/src/lib.rs +++ b/packages/backend/native/src/lib.rs @@ -1,5 +1,6 @@ #![deny(clippy::all)] +pub mod file_type; pub mod hashcash; use std::fmt::{Debug, Display}; diff --git a/packages/backend/server/package.json b/packages/backend/server/package.json index 55da8dea15..5aeee06f98 100644 --- a/packages/backend/server/package.json +++ b/packages/backend/server/package.json @@ -61,7 +61,6 @@ "dotenv": "^16.4.5", "dotenv-cli": "^7.4.1", "express": "^4.19.2", - "file-type": "^19.0.0", "get-stream": "^9.0.1", "graphql": "^16.8.1", "graphql-scalars": "^1.23.0", diff --git a/packages/backend/server/src/fundamentals/storage/native.ts b/packages/backend/server/src/fundamentals/storage/native.ts index 06b51f8959..9fd927e37a 100644 --- a/packages/backend/server/src/fundamentals/storage/native.ts +++ b/packages/backend/server/src/fundamentals/storage/native.ts @@ -28,3 +28,5 @@ export const mintChallengeResponse = async (resource: string, bits: number) => { if (!resource) return null; return serverNativeModule.mintChallengeResponse(resource, bits); }; + +export const getMime = serverNativeModule.getMime; diff --git a/packages/backend/server/src/fundamentals/storage/providers/utils.ts b/packages/backend/server/src/fundamentals/storage/providers/utils.ts index c1b3355a3f..3c22ca9078 100644 --- a/packages/backend/server/src/fundamentals/storage/providers/utils.ts +++ b/packages/backend/server/src/fundamentals/storage/providers/utils.ts @@ -1,9 +1,9 @@ import { Readable } from 'node:stream'; import { crc32 } from '@node-rs/crc32'; -import { fileTypeFromBuffer } from 'file-type'; import { getStreamAsBuffer } from 'get-stream'; +import { getMime } from '../native'; import { BlobInputType, PutObjectMetadata } from './provider'; export async function toBuffer(input: BlobInputType): Promise { @@ -35,8 +35,7 @@ export async function autoMetadata( // mime type if (!metadata.contentType) { try { - const typeResult = await fileTypeFromBuffer(blob); - metadata.contentType = typeResult?.mime ?? 'application/octet-stream'; + metadata.contentType = getMime(blob); } catch { // ignore } diff --git a/yarn.lock b/yarn.lock index 6cb4b6e6ef..3a1f3853d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -724,7 +724,6 @@ __metadata: dotenv: "npm:^16.4.5" dotenv-cli: "npm:^7.4.1" express: "npm:^4.19.2" - file-type: "npm:^19.0.0" get-stream: "npm:^9.0.1" graphql: "npm:^16.8.1" graphql-scalars: "npm:^1.23.0" @@ -22536,17 +22535,6 @@ __metadata: languageName: node linkType: hard -"file-type@npm:^19.0.0": - version: 19.0.0 - resolution: "file-type@npm:19.0.0" - dependencies: - readable-web-to-node-stream: "npm:^3.0.2" - strtok3: "npm:^7.0.0" - token-types: "npm:^5.0.1" - checksum: 10/8befa58f769b19d4a72c214694906b83b584310575300e63c08c48f9f2cfa6cb57fb4e1d08325961938d9dde3ecc4f5737b1604ddedfd759f5a1e65e5b0ca577 - languageName: node - linkType: hard - "file-uri-to-path@npm:1.0.0": version: 1.0.0 resolution: "file-uri-to-path@npm:1.0.0" @@ -32384,15 +32372,6 @@ __metadata: languageName: node linkType: hard -"readable-web-to-node-stream@npm:^3.0.2": - version: 3.0.2 - resolution: "readable-web-to-node-stream@npm:3.0.2" - dependencies: - readable-stream: "npm:^3.6.0" - checksum: 10/d3a5bf9d707c01183d546a64864aa63df4d9cb835dfd2bf89ac8305e17389feef2170c4c14415a10d38f9b9bfddf829a57aaef7c53c8b40f11d499844bf8f1a4 - languageName: node - linkType: hard - "readdirp@npm:~3.6.0": version: 3.6.0 resolution: "readdirp@npm:3.6.0" @@ -34681,16 +34660,6 @@ __metadata: languageName: node linkType: hard -"strtok3@npm:^7.0.0": - version: 7.0.0 - resolution: "strtok3@npm:7.0.0" - dependencies: - "@tokenizer/token": "npm:^0.3.0" - peek-readable: "npm:^5.0.0" - checksum: 10/4f2269679fcfce1e9fe0600eff361ea4c687ae0a0e8d9dab6703811071cd92545cbcb32d4ace3d3aa591f777cec1a3e8aeecd5efd71ae216fd2962a7a238b4ab - languageName: node - linkType: hard - "style-loader@npm:^4.0.0": version: 4.0.0 resolution: "style-loader@npm:4.0.0" @@ -35327,16 +35296,6 @@ __metadata: languageName: node linkType: hard -"token-types@npm:^5.0.1": - version: 5.0.1 - resolution: "token-types@npm:5.0.1" - dependencies: - "@tokenizer/token": "npm:^0.3.0" - ieee754: "npm:^1.2.1" - checksum: 10/0985369bbea9f53a5ccd79bb9899717b41401a813deb2c7fb1add5d0baf2f702aaf6da78f6e0ccf346d5a9f7acaa7cb5efed7d092d89d8c1e6962959e9509bc0 - languageName: node - linkType: hard - "toml@npm:^3.0.0": version: 3.0.0 resolution: "toml@npm:3.0.0"