@constructive-io/content-type-stream 2.4.3
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/LICENSE +23 -0
- package/README.md +147 -0
- package/content-stream.d.ts +6 -0
- package/content-stream.js +68 -0
- package/content-type-stream.d.ts +17 -0
- package/content-type-stream.js +47 -0
- package/esm/content-stream.js +28 -0
- package/esm/content-type-stream.js +44 -0
- package/esm/index.js +2 -0
- package/index.d.ts +2 -0
- package/index.js +18 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @constructive-io/content-type-stream
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/constructive/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@constructive-io/content-type-stream"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=packages%2Fcontent-type-stream%2Fpackage.json"/></a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @constructive-io/content-type-stream
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
In my life I've always found that `content-type`s or mimetypes to be totally hacky and different across operating systems and browsers. Also, knowing when to use the magic bytes compared to how we actually use files based on their extensions is never consistent... so this is aimed to make all that simple :)
|
|
20
|
+
|
|
21
|
+
## content-type info
|
|
22
|
+
|
|
23
|
+
get proper content-type information via streams without ever writing to disk
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
const readStream = createReadStream(filename);
|
|
27
|
+
const { stream, magic, contentType } = await streamContentType({
|
|
28
|
+
readStream,
|
|
29
|
+
filename
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## using steams
|
|
34
|
+
|
|
35
|
+
the `stream` it returns can then be used, for example
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const { stream, contentType } = await streamContentType({
|
|
39
|
+
readStream,
|
|
40
|
+
filename
|
|
41
|
+
});
|
|
42
|
+
return await asyncUpload({
|
|
43
|
+
key,
|
|
44
|
+
contentType,
|
|
45
|
+
readStream: stream,
|
|
46
|
+
bucket
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## contents hash stream
|
|
51
|
+
|
|
52
|
+
if you want more info with your upload, with `ContentStream` you can get nice hashes
|
|
53
|
+
|
|
54
|
+
* `uuid`: RFC-compliant UUID v5.
|
|
55
|
+
* `etag`: Etag/S3 MD5 sum
|
|
56
|
+
* `sha`: A sha sum
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
const contentStream = new ContentStream();
|
|
60
|
+
// ...
|
|
61
|
+
readStream.pipe(contentStream);
|
|
62
|
+
contentStream.pipe(uploadStream);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
{
|
|
67
|
+
uuid: '78160718-8dfa-5cb4-bb50-e479c8c58383',
|
|
68
|
+
sha: 'e6c7c64d292a414941d239c57117b36f24c9f829',
|
|
69
|
+
etag: '64dcb5b3b291074d02c80f600fda3f6e'
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Education and Tutorials
|
|
76
|
+
|
|
77
|
+
1. ๐ [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
78
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
79
|
+
|
|
80
|
+
2. ๐ฆ [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
81
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
82
|
+
|
|
83
|
+
3. โ๏ธ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
84
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
85
|
+
|
|
86
|
+
4. ๐งช [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
87
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
88
|
+
|
|
89
|
+
5. โก [Supabase Testing](https://constructive.io/learn/supabase)
|
|
90
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
91
|
+
|
|
92
|
+
6. ๐ง [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
93
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
94
|
+
|
|
95
|
+
7. ๐ง [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
96
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
97
|
+
|
|
98
|
+
## Related Constructive Tooling
|
|
99
|
+
|
|
100
|
+
### ๐งช Testing
|
|
101
|
+
|
|
102
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **๐ Isolated testing environments** with per-test transaction rollbacksโideal for integration tests, complex migrations, and RLS simulation.
|
|
103
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **๐งช Supabase-native test harness** preconfigured for the local Supabase stackโper-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
104
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **๐ Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
105
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **๐ Session context injection** to add session-local context (e.g., `SET LOCAL`) into queriesโideal for setting `role`, `jwt.claims`, and other session settings.
|
|
106
|
+
|
|
107
|
+
### ๐ง Parsing & AST
|
|
108
|
+
|
|
109
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **๐ SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
110
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **๐ Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
111
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **๐ฆ Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
112
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **๐ท๏ธ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
113
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **๐ Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
114
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **๐ ๏ธ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
115
|
+
* [pg-ast](https://www.npmjs.com/package/pg-ast): **๐ Low-level AST tools** and transformations for Postgres query structures.
|
|
116
|
+
|
|
117
|
+
### ๐ API & Dev Tools
|
|
118
|
+
|
|
119
|
+
* [@constructive-io/graphql-server](https://github.com/constructive-io/constructive/tree/main/graphql/server): **โก Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
|
|
120
|
+
* [@constructive-io/graphql-explorer](https://github.com/constructive-io/constructive/tree/main/graphql/explorer): **๐ Visual API explorer** with GraphiQL for browsing across all databases and schemasโuseful for debugging, documentation, and API prototyping.
|
|
121
|
+
|
|
122
|
+
### ๐ Streaming & Uploads
|
|
123
|
+
|
|
124
|
+
* [etag-hash](https://github.com/constructive-io/constructive/tree/main/streaming/etag-hash): **๐ท๏ธ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
|
|
125
|
+
* [etag-stream](https://github.com/constructive-io/constructive/tree/main/streaming/etag-stream): **๐ ETag computation** via Node stream transformer during upload or transfer.
|
|
126
|
+
* [uuid-hash](https://github.com/constructive-io/constructive/tree/main/streaming/uuid-hash): **๐ Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
|
|
127
|
+
* [uuid-stream](https://github.com/constructive-io/constructive/tree/main/streaming/uuid-stream): **๐ Streaming UUID generation** based on piped file contentโideal for upload pipelines.
|
|
128
|
+
* [@constructive-io/s3-streamer](https://github.com/constructive-io/constructive/tree/main/streaming/s3-streamer): **๐ค Direct S3 streaming** for large files with support for metadata injection and content validation.
|
|
129
|
+
* [@constructive-io/upload-names](https://github.com/constructive-io/constructive/tree/main/streaming/upload-names): **๐ Collision-resistant filenames** utility for structured and unique file names for uploads.
|
|
130
|
+
|
|
131
|
+
### ๐งฐ CLI & Codegen
|
|
132
|
+
|
|
133
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **๐ฅ๏ธ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
134
|
+
* [@constructive-io/cli](https://github.com/constructive-io/constructive/tree/main/packages/cli): **๐ฅ๏ธ Command-line toolkit** for managing Constructive projectsโsupports database scaffolding, migrations, seeding, code generation, and automation.
|
|
135
|
+
* [@constructive-io/graphql-codegen](https://github.com/constructive-io/constructive/tree/main/graphql/codegen): **โจ GraphQL code generation** (types, operations, SDK) from schema/endpoint introspection.
|
|
136
|
+
* [@constructive-io/query-builder](https://github.com/constructive-io/constructive/tree/main/packages/query-builder): **๐๏ธ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure callsโsupports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
|
|
137
|
+
* [@constructive-io/graphql-query](https://github.com/constructive-io/constructive/tree/main/graphql/query): **๐งฉ Fluent GraphQL builder** for PostGraphile schemas. โก Schema-aware via introspection, ๐งฉ composable and ergonomic for building deeply nested queries.
|
|
138
|
+
|
|
139
|
+
## Credits
|
|
140
|
+
|
|
141
|
+
**๐ Built by the [Constructive](https://constructive.io) team โ creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
142
|
+
|
|
143
|
+
## Disclaimer
|
|
144
|
+
|
|
145
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
146
|
+
|
|
147
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.ContentStream = void 0;
|
|
40
|
+
// @ts-nocheck
|
|
41
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
42
|
+
const etag = __importStar(require("etag-hash"));
|
|
43
|
+
const stream_1 = __importDefault(require("stream"));
|
|
44
|
+
const uuid = __importStar(require("uuid-hash"));
|
|
45
|
+
class ContentStream extends stream_1.default.Transform {
|
|
46
|
+
constructor() {
|
|
47
|
+
super();
|
|
48
|
+
this.shasum = crypto_1.default.createHash('sha1');
|
|
49
|
+
this.etagsum = etag.createHash();
|
|
50
|
+
this.uuidsum = uuid.createHash();
|
|
51
|
+
}
|
|
52
|
+
_write(chunk, enc, next) {
|
|
53
|
+
this.shasum.update(chunk);
|
|
54
|
+
this.etagsum.update(chunk);
|
|
55
|
+
this.uuidsum.update(chunk);
|
|
56
|
+
this.push(chunk);
|
|
57
|
+
next();
|
|
58
|
+
}
|
|
59
|
+
_flush(done) {
|
|
60
|
+
this.emit('contents', {
|
|
61
|
+
uuid: this.uuidsum.digest(),
|
|
62
|
+
sha: this.shasum.digest('hex'),
|
|
63
|
+
etag: this.etagsum.digest()
|
|
64
|
+
});
|
|
65
|
+
done();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.ContentStream = ContentStream;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BufferPeekStream } from 'mime-bytes';
|
|
2
|
+
import type { Readable } from 'stream';
|
|
3
|
+
interface StreamContentTypeArgs {
|
|
4
|
+
readStream: Readable;
|
|
5
|
+
filename: string;
|
|
6
|
+
peekBytes?: number;
|
|
7
|
+
}
|
|
8
|
+
interface StreamContentTypeResult {
|
|
9
|
+
stream: BufferPeekStream | Readable;
|
|
10
|
+
magic: {
|
|
11
|
+
type: string;
|
|
12
|
+
charset: string;
|
|
13
|
+
};
|
|
14
|
+
contentType: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function streamContentType({ readStream, filename, peekBytes }: StreamContentTypeArgs): Promise<StreamContentTypeResult>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamContentType = streamContentType;
|
|
4
|
+
const mime_bytes_1 = require("mime-bytes");
|
|
5
|
+
// Create a shared detector instance with default settings
|
|
6
|
+
const detector = new mime_bytes_1.FileTypeDetector({
|
|
7
|
+
peekBytes: 16384,
|
|
8
|
+
checkMultipleOffsets: true
|
|
9
|
+
});
|
|
10
|
+
async function streamContentType({ readStream, filename, peekBytes = 16384 }) {
|
|
11
|
+
try {
|
|
12
|
+
// Create a custom detector if different peek bytes are needed
|
|
13
|
+
const customDetector = peekBytes !== 16384
|
|
14
|
+
? new mime_bytes_1.FileTypeDetector({ peekBytes, checkMultipleOffsets: true })
|
|
15
|
+
: detector;
|
|
16
|
+
// Detect from stream with fallback to filename
|
|
17
|
+
const result = await customDetector.detectWithFallback(readStream, filename);
|
|
18
|
+
// Get the peek stream that was created during detection
|
|
19
|
+
// mime-bytes returns the peek stream as part of the result
|
|
20
|
+
const peekStream = result?._stream || readStream;
|
|
21
|
+
let type = 'application/octet-stream';
|
|
22
|
+
let charset = 'binary';
|
|
23
|
+
let contentType = 'application/octet-stream';
|
|
24
|
+
if (result) {
|
|
25
|
+
type = result.mimeType;
|
|
26
|
+
charset = result.charset || 'binary';
|
|
27
|
+
// Use the contentType directly from mime-bytes
|
|
28
|
+
contentType = result.contentType || result.mimeType;
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
stream: peekStream,
|
|
32
|
+
magic: { type, charset },
|
|
33
|
+
contentType
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
// If detection fails, return defaults with original stream
|
|
38
|
+
const type = 'application/octet-stream';
|
|
39
|
+
const charset = 'binary';
|
|
40
|
+
const contentType = 'application/octet-stream';
|
|
41
|
+
return {
|
|
42
|
+
stream: readStream,
|
|
43
|
+
magic: { type, charset },
|
|
44
|
+
contentType
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import * as etag from 'etag-hash';
|
|
4
|
+
import stream from 'stream';
|
|
5
|
+
import * as uuid from 'uuid-hash';
|
|
6
|
+
export class ContentStream extends stream.Transform {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
this.shasum = crypto.createHash('sha1');
|
|
10
|
+
this.etagsum = etag.createHash();
|
|
11
|
+
this.uuidsum = uuid.createHash();
|
|
12
|
+
}
|
|
13
|
+
_write(chunk, enc, next) {
|
|
14
|
+
this.shasum.update(chunk);
|
|
15
|
+
this.etagsum.update(chunk);
|
|
16
|
+
this.uuidsum.update(chunk);
|
|
17
|
+
this.push(chunk);
|
|
18
|
+
next();
|
|
19
|
+
}
|
|
20
|
+
_flush(done) {
|
|
21
|
+
this.emit('contents', {
|
|
22
|
+
uuid: this.uuidsum.digest(),
|
|
23
|
+
sha: this.shasum.digest('hex'),
|
|
24
|
+
etag: this.etagsum.digest()
|
|
25
|
+
});
|
|
26
|
+
done();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FileTypeDetector } from 'mime-bytes';
|
|
2
|
+
// Create a shared detector instance with default settings
|
|
3
|
+
const detector = new FileTypeDetector({
|
|
4
|
+
peekBytes: 16384,
|
|
5
|
+
checkMultipleOffsets: true
|
|
6
|
+
});
|
|
7
|
+
export async function streamContentType({ readStream, filename, peekBytes = 16384 }) {
|
|
8
|
+
try {
|
|
9
|
+
// Create a custom detector if different peek bytes are needed
|
|
10
|
+
const customDetector = peekBytes !== 16384
|
|
11
|
+
? new FileTypeDetector({ peekBytes, checkMultipleOffsets: true })
|
|
12
|
+
: detector;
|
|
13
|
+
// Detect from stream with fallback to filename
|
|
14
|
+
const result = await customDetector.detectWithFallback(readStream, filename);
|
|
15
|
+
// Get the peek stream that was created during detection
|
|
16
|
+
// mime-bytes returns the peek stream as part of the result
|
|
17
|
+
const peekStream = result?._stream || readStream;
|
|
18
|
+
let type = 'application/octet-stream';
|
|
19
|
+
let charset = 'binary';
|
|
20
|
+
let contentType = 'application/octet-stream';
|
|
21
|
+
if (result) {
|
|
22
|
+
type = result.mimeType;
|
|
23
|
+
charset = result.charset || 'binary';
|
|
24
|
+
// Use the contentType directly from mime-bytes
|
|
25
|
+
contentType = result.contentType || result.mimeType;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
stream: peekStream,
|
|
29
|
+
magic: { type, charset },
|
|
30
|
+
contentType
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
// If detection fails, return defaults with original stream
|
|
35
|
+
const type = 'application/octet-stream';
|
|
36
|
+
const charset = 'binary';
|
|
37
|
+
const contentType = 'application/octet-stream';
|
|
38
|
+
return {
|
|
39
|
+
stream: readStream,
|
|
40
|
+
magic: { type, charset },
|
|
41
|
+
contentType
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
package/esm/index.js
ADDED
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./content-stream"), exports);
|
|
18
|
+
__exportStar(require("./content-type-stream"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constructive-io/content-type-stream",
|
|
3
|
+
"version": "2.4.3",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "detect content-types via streams",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest --passWithNoTests",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"etag-hash": "^2.4.3",
|
|
33
|
+
"mime-bytes": "^0.4.3",
|
|
34
|
+
"uuid-hash": "^2.3.3"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"content-type",
|
|
38
|
+
"stream",
|
|
39
|
+
"middleware"
|
|
40
|
+
],
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"makage": "^0.1.8"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "22cfe32e994e26a6490e04e28bab26d1e7e6345c"
|
|
45
|
+
}
|