@plasius/storage 1.0.0
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/CHANGELOG.md +66 -0
- package/CODE_OF_CONDUCT.md +79 -0
- package/CONTRIBUTORS.md +27 -0
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/SECURITY.md +17 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/dist-cjs/index.d.ts +16 -0
- package/dist-cjs/index.d.ts.map +1 -0
- package/dist-cjs/index.js +87 -0
- package/docs/adrs/adr-0001-storage-package-scope.md +21 -0
- package/docs/adrs/adr-0002-public-repo-governance.md +24 -0
- package/docs/adrs/adr-template.md +35 -0
- package/legal/CLA-REGISTRY.csv +1 -0
- package/legal/CLA.md +22 -0
- package/legal/CORPORATE_CLA.md +57 -0
- package/legal/INDIVIDUAL_CLA.md +91 -0
- package/package.json +103 -0
- package/src/index.ts +116 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on **[Keep a Changelog](https://keepachangelog.com/en/1.1.0/)**, and this project adheres to **[Semantic Versioning](https://semver.org/spec/v2.0.0.html)**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## [Unreleased]
|
|
10
|
+
|
|
11
|
+
- **Added**
|
|
12
|
+
- (placeholder)
|
|
13
|
+
|
|
14
|
+
- **Changed**
|
|
15
|
+
- (placeholder)
|
|
16
|
+
|
|
17
|
+
- **Fixed**
|
|
18
|
+
- (placeholder)
|
|
19
|
+
|
|
20
|
+
- **Security**
|
|
21
|
+
- (placeholder)
|
|
22
|
+
|
|
23
|
+
## [1.0.0] - 2026-02-12
|
|
24
|
+
|
|
25
|
+
- **Added**
|
|
26
|
+
- Standalone public package scaffold at repository root with independent CI/CD, ADRs, and legal governance assets.
|
|
27
|
+
|
|
28
|
+
- **Changed**
|
|
29
|
+
- Add dual ESM + CJS build outputs with `exports` entries and CJS artifacts in `dist-cjs/`.
|
|
30
|
+
|
|
31
|
+
- **Fixed**
|
|
32
|
+
- Removed monorepo-relative TypeScript configuration coupling for standalone builds.
|
|
33
|
+
|
|
34
|
+
- **Security**
|
|
35
|
+
- Added baseline public package governance and CLA documentation.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Release process (maintainers)
|
|
40
|
+
|
|
41
|
+
1. Update `CHANGELOG.md` under **Unreleased** with user-visible changes.
|
|
42
|
+
2. Bump version in `package.json` following SemVer (major/minor/patch).
|
|
43
|
+
3. Move entries from **Unreleased** to a new version section with the current date.
|
|
44
|
+
4. Tag the release in Git (`vX.Y.Z`) and push tags.
|
|
45
|
+
5. Publish to npm (via CI/CD or `npm publish`).
|
|
46
|
+
|
|
47
|
+
> Tip: Use Conventional Commits in PR titles/bodies to make changelog updates easier.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
[Unreleased]: https://github.com/Plasius-LTD/storage/compare/v1.0.0...HEAD
|
|
52
|
+
|
|
53
|
+
## [1.0.0] - 2026-02-11
|
|
54
|
+
|
|
55
|
+
- **Added**
|
|
56
|
+
- Initial release.
|
|
57
|
+
|
|
58
|
+
- **Changed**
|
|
59
|
+
- (placeholder)
|
|
60
|
+
|
|
61
|
+
- **Fixed**
|
|
62
|
+
- (placeholder)
|
|
63
|
+
|
|
64
|
+
- **Security**
|
|
65
|
+
- (placeholder)
|
|
66
|
+
[1.0.0]: https://github.com/Plasius-LTD/storage/releases/tag/v1.0.0
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct v2.1
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
- Demonstrating empathy and kindness toward other people
|
|
14
|
+
- Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
- Giving and gracefully accepting constructive feedback
|
|
16
|
+
- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
- Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
- The use of sexualized language or imagery, and sexual attention or advances of any kind
|
|
22
|
+
- Trolling, insulting or derogatory comments, and personal or political attacks
|
|
23
|
+
- Public or private harassment
|
|
24
|
+
- Publishing others’ private information, such as a physical or email address, without their explicit permission
|
|
25
|
+
- Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
26
|
+
|
|
27
|
+
## Enforcement Responsibilities
|
|
28
|
+
|
|
29
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
30
|
+
|
|
31
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
32
|
+
|
|
33
|
+
## Scope
|
|
34
|
+
|
|
35
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
36
|
+
|
|
37
|
+
## Enforcement
|
|
38
|
+
|
|
39
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [conduct@plasius.co.uk](mailto:conduct@plasius.co.uk). All complaints will be reviewed and investigated promptly and fairly.
|
|
40
|
+
|
|
41
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
42
|
+
|
|
43
|
+
## Enforcement Guidelines
|
|
44
|
+
|
|
45
|
+
Community leaders will follow these Enforcement Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
46
|
+
|
|
47
|
+
### 1. Correction
|
|
48
|
+
|
|
49
|
+
**Community Impact:** Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
50
|
+
|
|
51
|
+
**Consequence:** A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
52
|
+
|
|
53
|
+
### 2. Warning
|
|
54
|
+
|
|
55
|
+
**Community Impact:** A violation through a single incident or series of actions.
|
|
56
|
+
|
|
57
|
+
**Consequence:** A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
58
|
+
|
|
59
|
+
### 3. Temporary Ban
|
|
60
|
+
|
|
61
|
+
**Community Impact:** A serious violation of community standards, including sustained inappropriate behavior.
|
|
62
|
+
|
|
63
|
+
**Consequence:** A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
64
|
+
|
|
65
|
+
### 4. Permanent Ban
|
|
66
|
+
|
|
67
|
+
**Community Impact:** Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
68
|
+
|
|
69
|
+
**Consequence:** A permanent ban from any sort of public interaction within the community.
|
|
70
|
+
|
|
71
|
+
## Attribution
|
|
72
|
+
|
|
73
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html](https://www.contributor-covenant.org/version/2/1/code_of_conduct.html)
|
|
74
|
+
|
|
75
|
+
For answers to common questions about this code of conduct, see the [FAQ](https://www.contributor-covenant.org/faq)
|
|
76
|
+
|
|
77
|
+
[homepage]: [https://www.contributor-covenant.org](https://www.contributor-covenant.org)
|
|
78
|
+
|
|
79
|
+
If you have any questions or concerns regarding this Code of Conduct, please contact us at [conduct@plasius.co.uk](mailto:conduct@plasius.co.uk).
|
package/CONTRIBUTORS.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Contributing Guidelines
|
|
2
|
+
|
|
3
|
+
Thank you for considering contributing to this project! We welcome contributions that improve the code, documentation, and overall project quality.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
- Fork the repository.
|
|
8
|
+
- Create a feature branch from `main`.
|
|
9
|
+
- Commit your changes with clear messages (we follow **Conventional Commits**).
|
|
10
|
+
- Push your branch and open a Pull Request (PR).
|
|
11
|
+
|
|
12
|
+
## Requirements
|
|
13
|
+
|
|
14
|
+
- Write tests alongside code where possible.
|
|
15
|
+
- Ensure all tests pass before submitting a PR.
|
|
16
|
+
- Follow the repository’s coding style and linting rules.
|
|
17
|
+
- Update documentation (README, ADRs, etc.) when making significant changes.
|
|
18
|
+
- When making architectural changes, create a new ADR (Architecture Decision Record) that **succeeds** the previous one rather than modifying old ADRs. This preserves history and ensures decisions are traceable.
|
|
19
|
+
- Use the [ADR template](./docs/adrs/adr-template.md) when writing new ADRs to ensure consistency.
|
|
20
|
+
- Before your first contribution, sign the appropriate Contributor License Agreement (CLA). See [legal/CLA.md](legal/CLA.md) for details, then email the signed document to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
|
|
21
|
+
|
|
22
|
+
## Communication
|
|
23
|
+
|
|
24
|
+
- Use GitHub Issues for bugs and feature requests.
|
|
25
|
+
- Pull Requests should describe the problem, solution, and trade-offs.
|
|
26
|
+
|
|
27
|
+
We appreciate your support in making this project better!
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Plasius LTD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @plasius/storage
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@plasius/storage)
|
|
4
|
+
[](https://github.com/Plasius-LTD/storage/actions/workflows/ci.yml)
|
|
5
|
+
[](https://codecov.io/gh/Plasius-LTD/storage)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
[](./CODE_OF_CONDUCT.md)
|
|
8
|
+
[](./SECURITY.md)
|
|
9
|
+
[](./CHANGELOG.md)
|
|
10
|
+
|
|
11
|
+
Public package containing shared Azure storage helpers for Plasius services.
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @plasius/storage
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createShareClient } from "@plasius/storage";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Development
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install
|
|
30
|
+
npm run build
|
|
31
|
+
npm test
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Governance
|
|
35
|
+
|
|
36
|
+
- Security policy: [SECURITY.md](./SECURITY.md)
|
|
37
|
+
- Code of conduct: [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md)
|
|
38
|
+
- ADRs: [docs/adrs](./docs/adrs)
|
|
39
|
+
- Legal docs: [legal](./legal)
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Supported Versions
|
|
4
|
+
|
|
5
|
+
We currently support the latest major version of this project. Older versions may not receive security updates.
|
|
6
|
+
|
|
7
|
+
## Reporting a Vulnerability
|
|
8
|
+
|
|
9
|
+
If you discover a security vulnerability, please report it privately by emailing us at [security@plasius.co.uk](mailto:security@plasius.co.uk). Please do not create a public issue for security-related matters.
|
|
10
|
+
|
|
11
|
+
## Response Timeline
|
|
12
|
+
|
|
13
|
+
We aim to acknowledge your report within 2 business days and to provide a more detailed response (including next steps and, if applicable, a timeline for a fix) within 7 business days.
|
|
14
|
+
|
|
15
|
+
## Disclosure Policy
|
|
16
|
+
|
|
17
|
+
We request that you give us the opportunity to address the vulnerability before publicly disclosing it. We will coordinate with you on public disclosure once a fix is available and deployed.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface UploadOptions {
|
|
2
|
+
maxRetries?: number;
|
|
3
|
+
baseDelayMs?: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Uploads a user image to Azure File Share storage with retries and error handling.
|
|
7
|
+
* @param userId - The user's ID (used as directory name)
|
|
8
|
+
* @param version - The version number (used as file name)
|
|
9
|
+
* @param buffer - The file data as a Buffer
|
|
10
|
+
* @param contentType - The MIME type of the file
|
|
11
|
+
* @param options - Optional settings for retries and backoff
|
|
12
|
+
* @returns URL to the uploaded file
|
|
13
|
+
*/
|
|
14
|
+
export declare function uploadUserImageShare(userId: string, version: number, buffer: Buffer, contentType: string, options?: UploadOptions): Promise<string>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,UAAU,aAAa;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,MAAM,CAAC,CA6FjB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ShareServiceClient } from "@azure/storage-file-share";
|
|
2
|
+
/**
|
|
3
|
+
* Uploads a user image to Azure File Share storage with retries and error handling.
|
|
4
|
+
* @param userId - The user's ID (used as directory name)
|
|
5
|
+
* @param version - The version number (used as file name)
|
|
6
|
+
* @param buffer - The file data as a Buffer
|
|
7
|
+
* @param contentType - The MIME type of the file
|
|
8
|
+
* @param options - Optional settings for retries and backoff
|
|
9
|
+
* @returns URL to the uploaded file
|
|
10
|
+
*/
|
|
11
|
+
export async function uploadUserImageShare(userId, version, buffer, contentType, options = {}) {
|
|
12
|
+
// Parameter validation
|
|
13
|
+
if (!process.env.AZURE_STORAGE_CONNECTION_STRING) {
|
|
14
|
+
throw new Error("AZURE_STORAGE_CONNECTION_STRING is not set in environment variables.");
|
|
15
|
+
}
|
|
16
|
+
if (!userId || typeof userId !== "string") {
|
|
17
|
+
throw new Error("userId is required and must be a string.");
|
|
18
|
+
}
|
|
19
|
+
if (typeof version !== "number" || isNaN(version)) {
|
|
20
|
+
throw new Error("version is required and must be a number.");
|
|
21
|
+
}
|
|
22
|
+
if (!buffer || !(buffer instanceof Buffer)) {
|
|
23
|
+
throw new Error("buffer is required and must be a Buffer.");
|
|
24
|
+
}
|
|
25
|
+
if (!contentType || typeof contentType !== "string") {
|
|
26
|
+
throw new Error("contentType is required and must be a string.");
|
|
27
|
+
}
|
|
28
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
29
|
+
const baseDelayMs = options.baseDelayMs ?? 500;
|
|
30
|
+
const shareName = "avatars";
|
|
31
|
+
const directoryName = userId;
|
|
32
|
+
const fileName = `${version}.jpg`;
|
|
33
|
+
const serviceClient = ShareServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING);
|
|
34
|
+
const shareClient = serviceClient.getShareClient(shareName);
|
|
35
|
+
const directoryClient = shareClient.getDirectoryClient(directoryName);
|
|
36
|
+
const fileClient = directoryClient.getFileClient(fileName);
|
|
37
|
+
// Retry logic with exponential backoff
|
|
38
|
+
let attempt = 0;
|
|
39
|
+
let lastError = null;
|
|
40
|
+
while (attempt < maxRetries) {
|
|
41
|
+
try {
|
|
42
|
+
// Ensure share exists
|
|
43
|
+
await shareClient.createIfNotExists();
|
|
44
|
+
// Ensure directory exists
|
|
45
|
+
await directoryClient.createIfNotExists();
|
|
46
|
+
// Create file (set size)
|
|
47
|
+
await fileClient.create(buffer.length, {
|
|
48
|
+
fileHttpHeaders: { fileContentType: contentType },
|
|
49
|
+
});
|
|
50
|
+
// Upload content
|
|
51
|
+
await fileClient.uploadRange(buffer, 0, buffer.length);
|
|
52
|
+
// Return file URL
|
|
53
|
+
return fileClient.url;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
lastError = err;
|
|
57
|
+
if (err &&
|
|
58
|
+
typeof err === "object" &&
|
|
59
|
+
"message" in err &&
|
|
60
|
+
typeof err.message === "string") {
|
|
61
|
+
console.error(`Attempt ${attempt + 1} to upload user image failed: ${err.message}`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
console.error(`Attempt ${attempt + 1} to upload user image failed:`, err);
|
|
65
|
+
}
|
|
66
|
+
// Exponential backoff with jitter
|
|
67
|
+
if (attempt < maxRetries - 1) {
|
|
68
|
+
const baseDelay = Math.pow(2, attempt) * baseDelayMs;
|
|
69
|
+
const jitter = Math.random() * baseDelay;
|
|
70
|
+
const delay = baseDelay / 2 + jitter; // Between 0.5x and 1.5x base delay
|
|
71
|
+
console.warn(`Backing off for ${Math.round(delay)} ms before retrying...`);
|
|
72
|
+
await new Promise((res) => setTimeout(res, delay));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
attempt++;
|
|
76
|
+
}
|
|
77
|
+
const lastErrorMsg = lastError &&
|
|
78
|
+
typeof lastError === "object" &&
|
|
79
|
+
"message" in lastError &&
|
|
80
|
+
typeof lastError.message === "string"
|
|
81
|
+
? lastError.message
|
|
82
|
+
: String(lastError ?? "");
|
|
83
|
+
throw new Error(`Failed to upload user image after ${maxRetries} attempts: ${lastErrorMsg}`);
|
|
84
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface UploadOptions {
|
|
2
|
+
maxRetries?: number;
|
|
3
|
+
baseDelayMs?: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Uploads a user image to Azure File Share storage with retries and error handling.
|
|
7
|
+
* @param userId - The user's ID (used as directory name)
|
|
8
|
+
* @param version - The version number (used as file name)
|
|
9
|
+
* @param buffer - The file data as a Buffer
|
|
10
|
+
* @param contentType - The MIME type of the file
|
|
11
|
+
* @param options - Optional settings for retries and backoff
|
|
12
|
+
* @returns URL to the uploaded file
|
|
13
|
+
*/
|
|
14
|
+
export declare function uploadUserImageShare(userId: string, version: number, buffer: Buffer, contentType: string, options?: UploadOptions): Promise<string>;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,UAAU,aAAa;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,EACnB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,MAAM,CAAC,CA6FjB"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.uploadUserImageShare = uploadUserImageShare;
|
|
4
|
+
const storage_file_share_1 = require("@azure/storage-file-share");
|
|
5
|
+
/**
|
|
6
|
+
* Uploads a user image to Azure File Share storage with retries and error handling.
|
|
7
|
+
* @param userId - The user's ID (used as directory name)
|
|
8
|
+
* @param version - The version number (used as file name)
|
|
9
|
+
* @param buffer - The file data as a Buffer
|
|
10
|
+
* @param contentType - The MIME type of the file
|
|
11
|
+
* @param options - Optional settings for retries and backoff
|
|
12
|
+
* @returns URL to the uploaded file
|
|
13
|
+
*/
|
|
14
|
+
async function uploadUserImageShare(userId, version, buffer, contentType, options = {}) {
|
|
15
|
+
// Parameter validation
|
|
16
|
+
if (!process.env.AZURE_STORAGE_CONNECTION_STRING) {
|
|
17
|
+
throw new Error("AZURE_STORAGE_CONNECTION_STRING is not set in environment variables.");
|
|
18
|
+
}
|
|
19
|
+
if (!userId || typeof userId !== "string") {
|
|
20
|
+
throw new Error("userId is required and must be a string.");
|
|
21
|
+
}
|
|
22
|
+
if (typeof version !== "number" || isNaN(version)) {
|
|
23
|
+
throw new Error("version is required and must be a number.");
|
|
24
|
+
}
|
|
25
|
+
if (!buffer || !(buffer instanceof Buffer)) {
|
|
26
|
+
throw new Error("buffer is required and must be a Buffer.");
|
|
27
|
+
}
|
|
28
|
+
if (!contentType || typeof contentType !== "string") {
|
|
29
|
+
throw new Error("contentType is required and must be a string.");
|
|
30
|
+
}
|
|
31
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
32
|
+
const baseDelayMs = options.baseDelayMs ?? 500;
|
|
33
|
+
const shareName = "avatars";
|
|
34
|
+
const directoryName = userId;
|
|
35
|
+
const fileName = `${version}.jpg`;
|
|
36
|
+
const serviceClient = storage_file_share_1.ShareServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING);
|
|
37
|
+
const shareClient = serviceClient.getShareClient(shareName);
|
|
38
|
+
const directoryClient = shareClient.getDirectoryClient(directoryName);
|
|
39
|
+
const fileClient = directoryClient.getFileClient(fileName);
|
|
40
|
+
// Retry logic with exponential backoff
|
|
41
|
+
let attempt = 0;
|
|
42
|
+
let lastError = null;
|
|
43
|
+
while (attempt < maxRetries) {
|
|
44
|
+
try {
|
|
45
|
+
// Ensure share exists
|
|
46
|
+
await shareClient.createIfNotExists();
|
|
47
|
+
// Ensure directory exists
|
|
48
|
+
await directoryClient.createIfNotExists();
|
|
49
|
+
// Create file (set size)
|
|
50
|
+
await fileClient.create(buffer.length, {
|
|
51
|
+
fileHttpHeaders: { fileContentType: contentType },
|
|
52
|
+
});
|
|
53
|
+
// Upload content
|
|
54
|
+
await fileClient.uploadRange(buffer, 0, buffer.length);
|
|
55
|
+
// Return file URL
|
|
56
|
+
return fileClient.url;
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
lastError = err;
|
|
60
|
+
if (err &&
|
|
61
|
+
typeof err === "object" &&
|
|
62
|
+
"message" in err &&
|
|
63
|
+
typeof err.message === "string") {
|
|
64
|
+
console.error(`Attempt ${attempt + 1} to upload user image failed: ${err.message}`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
console.error(`Attempt ${attempt + 1} to upload user image failed:`, err);
|
|
68
|
+
}
|
|
69
|
+
// Exponential backoff with jitter
|
|
70
|
+
if (attempt < maxRetries - 1) {
|
|
71
|
+
const baseDelay = Math.pow(2, attempt) * baseDelayMs;
|
|
72
|
+
const jitter = Math.random() * baseDelay;
|
|
73
|
+
const delay = baseDelay / 2 + jitter; // Between 0.5x and 1.5x base delay
|
|
74
|
+
console.warn(`Backing off for ${Math.round(delay)} ms before retrying...`);
|
|
75
|
+
await new Promise((res) => setTimeout(res, delay));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
attempt++;
|
|
79
|
+
}
|
|
80
|
+
const lastErrorMsg = lastError &&
|
|
81
|
+
typeof lastError === "object" &&
|
|
82
|
+
"message" in lastError &&
|
|
83
|
+
typeof lastError.message === "string"
|
|
84
|
+
? lastError.message
|
|
85
|
+
: String(lastError ?? "");
|
|
86
|
+
throw new Error(`Failed to upload user image after ${maxRetries} attempts: ${lastErrorMsg}`);
|
|
87
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# ADR-0001: Standalone @plasius/storage Package Scope
|
|
2
|
+
|
|
3
|
+
- Date: 2026-02-11
|
|
4
|
+
- Status: Accepted
|
|
5
|
+
|
|
6
|
+
## Context
|
|
7
|
+
|
|
8
|
+
This package was previously maintained as a workspace-only module inside
|
|
9
|
+
`plasius-ltd-site`. External consumers and remote builds require it to be
|
|
10
|
+
installable from npm without monorepo-local links.
|
|
11
|
+
|
|
12
|
+
## Decision
|
|
13
|
+
|
|
14
|
+
Move `@plasius/storage` to a standalone root package with independent build,
|
|
15
|
+
test, governance, CI, and publish workflows.
|
|
16
|
+
|
|
17
|
+
## Consequences
|
|
18
|
+
|
|
19
|
+
- The package can be versioned and released independently.
|
|
20
|
+
- `plasius-ltd-site` and other repositories can depend on npm-published versions.
|
|
21
|
+
- Build and lint rules must no longer rely on monorepo-relative tsconfig paths.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# ADR-0002: Public Repository Governance Baseline
|
|
2
|
+
|
|
3
|
+
- Date: 2026-02-11
|
|
4
|
+
- Status: Accepted
|
|
5
|
+
|
|
6
|
+
## Context
|
|
7
|
+
|
|
8
|
+
Public npm distribution requires transparent contributor and security policy
|
|
9
|
+
artifacts and consistent release automation.
|
|
10
|
+
|
|
11
|
+
## Decision
|
|
12
|
+
|
|
13
|
+
Include these baseline governance assets:
|
|
14
|
+
|
|
15
|
+
- `CODE_OF_CONDUCT.md`
|
|
16
|
+
- `CONTRIBUTORS.md`
|
|
17
|
+
- `SECURITY.md`
|
|
18
|
+
- `legal/` CLA documents
|
|
19
|
+
- CI/CD GitHub Actions workflows
|
|
20
|
+
|
|
21
|
+
## Consequences
|
|
22
|
+
|
|
23
|
+
- Public contributors and consumers can follow a predictable governance process.
|
|
24
|
+
- Release quality gates (build, test, coverage, publish) are standardized.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Architectural Decision Record (ADR)
|
|
2
|
+
|
|
3
|
+
## Title
|
|
4
|
+
|
|
5
|
+
> Concise, descriptive title of the decision.
|
|
6
|
+
|
|
7
|
+
## Status
|
|
8
|
+
|
|
9
|
+
- Proposed | Accepted | Rejected | Superseded | Deprecated
|
|
10
|
+
- Date: YYYY-MM-DD
|
|
11
|
+
- Version: 1.0
|
|
12
|
+
|
|
13
|
+
## Context
|
|
14
|
+
|
|
15
|
+
Describe the problem, constraints, and relevant background.
|
|
16
|
+
|
|
17
|
+
## Decision
|
|
18
|
+
|
|
19
|
+
Clear statement of the selected approach.
|
|
20
|
+
|
|
21
|
+
## Alternatives Considered
|
|
22
|
+
|
|
23
|
+
- Option A
|
|
24
|
+
- Option B
|
|
25
|
+
- Option C
|
|
26
|
+
|
|
27
|
+
## Consequences
|
|
28
|
+
|
|
29
|
+
- Positive outcomes
|
|
30
|
+
- Negative outcomes / trade-offs
|
|
31
|
+
- Follow-up work
|
|
32
|
+
|
|
33
|
+
## References
|
|
34
|
+
|
|
35
|
+
- https://adr.github.io/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
type,github_handle,full_name,company,email,date,approver
|
package/legal/CLA.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Contributor License Agreements (CLA)
|
|
2
|
+
|
|
3
|
+
To protect the intellectual property of this project and ensure clarity of rights, all contributors must sign a Contributor License Agreement (CLA) before their first contribution.
|
|
4
|
+
|
|
5
|
+
## Which CLA should I sign?
|
|
6
|
+
|
|
7
|
+
- **Individual CLA**: If you are contributing personally and not on behalf of an employer, sign the [Individual CLA](INDIVIDUAL_CLA.md).
|
|
8
|
+
- **Corporate CLA**: If you are contributing as part of your work for a company, the company should sign the [Corporate CLA](CORPORATE_CLA.md).
|
|
9
|
+
|
|
10
|
+
## How to sign
|
|
11
|
+
|
|
12
|
+
1. Download the appropriate CLA file (Individual or Corporate).
|
|
13
|
+
2. Fill in the required details, sign, and date it.
|
|
14
|
+
3. Email a PDF copy of the signed document to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject: `CLA – Individual` or `CLA – Corporate`.
|
|
15
|
+
|
|
16
|
+
## Registry
|
|
17
|
+
|
|
18
|
+
All signed CLAs are logged internally in the CLA registry (`CLA-REGISTRY.csv`).
|
|
19
|
+
|
|
20
|
+
## Questions?
|
|
21
|
+
|
|
22
|
+
If you have any questions about which CLA to sign or how the process works, please email **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)**.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Corporate Contributor License Agreement (CLA)
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
|
|
5
|
+
This Corporate Contributor License Agreement ("Agreement") is intended to protect the intellectual property rights of the contributors and the project, ensure clear licensing terms for contributions, and maintain trust within the community. By signing this Agreement, the corporation agrees to the terms that facilitate the use, distribution, and modification of contributions under the project's licensing framework.
|
|
6
|
+
|
|
7
|
+
## Agreement
|
|
8
|
+
|
|
9
|
+
1. **Representation of Authority**
|
|
10
|
+
The undersigned individual represents and warrants that they have the full legal authority to enter into this Agreement on behalf of the corporation named below ("Corporation") and to grant the rights contained herein.
|
|
11
|
+
|
|
12
|
+
2. **Grant of Copyright License**
|
|
13
|
+
The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable copyright license to use, reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute the contributions submitted to the project.
|
|
14
|
+
|
|
15
|
+
3. **Grant of Patent License**
|
|
16
|
+
The Corporation hereby grants to the project maintainers and users a perpetual, worldwide, non-exclusive, royalty-free, irrevocable license under any patent claims that are necessarily infringed by the contributions to make, use, sell, offer for sale, import, and otherwise dispose of the contributions or derivative works thereof.
|
|
17
|
+
|
|
18
|
+
4. **Warranties and Representations**
|
|
19
|
+
The Corporation represents and warrants that:
|
|
20
|
+
|
|
21
|
+
- The contributions are the original work of the Corporation or that the Corporation has sufficient rights to grant the licenses herein.
|
|
22
|
+
- The submission of the contributions does not violate any agreements or rights of third parties.
|
|
23
|
+
|
|
24
|
+
5. **No Revocation**
|
|
25
|
+
This license is granted on a perpetual basis and cannot be revoked, provided that the terms of this Agreement are met.
|
|
26
|
+
|
|
27
|
+
6. **Governing Law**
|
|
28
|
+
This Agreement shall be governed by and construed in accordance with the laws of the United Kingdom, without regard to its conflict of laws principles.
|
|
29
|
+
|
|
30
|
+
7. **Execution**
|
|
31
|
+
|
|
32
|
+
This Agreement is effective upon signature by the authorized representative of the Corporation. Please sign and date this document, then email a scanned PDF copy to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
### **@plasius/api**
|
|
37
|
+
|
|
38
|
+
**Corporation Legal Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
39
|
+
|
|
40
|
+
**Authorized Representative:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
41
|
+
|
|
42
|
+
**Title:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
43
|
+
|
|
44
|
+
**Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
45
|
+
|
|
46
|
+
**Date:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
47
|
+
|
|
48
|
+
**Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## How to Sign
|
|
53
|
+
|
|
54
|
+
- Download this file as a template.
|
|
55
|
+
- Fill in the Corporation’s legal name, authorized representative, title, email, date, and provide a signature.
|
|
56
|
+
- Sign and date the document.
|
|
57
|
+
- Send a scanned copy of the signed Agreement to [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk).
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Individual Contributor License Agreement (CLA)
|
|
2
|
+
|
|
3
|
+
**Project:** @plasius/api (Plasius LTD)
|
|
4
|
+
**Version:** 1.0 — 2025‑09‑12
|
|
5
|
+
**Contact:** [contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## 1. Definitions
|
|
10
|
+
|
|
11
|
+
- **"You"** (or **"Contributor"**) means the individual signing this CLA and submitting Contributions to the Project.
|
|
12
|
+
- **"Contribution"** means any original work of authorship, including code, documentation, data, designs, or feedback that You submit to the Project in any form (e.g., pull request, issue comment, email, file upload).
|
|
13
|
+
- **"Project"** means the @plasius/api repositories and related materials owned or managed by **Plasius LTD**.
|
|
14
|
+
|
|
15
|
+
## 2. Copyright License Grant
|
|
16
|
+
|
|
17
|
+
You hereby grant to Plasius LTD a **perpetual, worldwide, non‑exclusive, transferable, sublicensable, royalty‑free, irrevocable** copyright license to:
|
|
18
|
+
|
|
19
|
+
- use, reproduce, publicly display, publicly perform, modify, create derivative works of, and
|
|
20
|
+
- distribute Contributions in source and object form,
|
|
21
|
+
- and to **sublicense** these rights under any terms Plasius LTD chooses, including proprietary or open‑source licenses.
|
|
22
|
+
|
|
23
|
+
## 3. Patent License Grant
|
|
24
|
+
|
|
25
|
+
You hereby grant to Plasius LTD and its sublicensees a **perpetual, worldwide, non‑exclusive, transferable, royalty‑free, irrevocable** patent license to **make, have made, use, offer to sell, sell, import, and otherwise transfer** the Contribution and derivative works thereof, where such license applies only to patent claims that You **own or control** and that would be infringed by Your Contribution or its combination with the Project.
|
|
26
|
+
|
|
27
|
+
## 4. Moral Rights & Attribution
|
|
28
|
+
|
|
29
|
+
To the maximum extent permitted by applicable law, You **waive** and agree not to assert any moral rights (e.g., rights of attribution or integrity) in or to the Contribution against Plasius LTD. Plasius LTD may, but is not required to, credit You.
|
|
30
|
+
|
|
31
|
+
## 5. Representations & Warranties
|
|
32
|
+
|
|
33
|
+
You represent that:
|
|
34
|
+
|
|
35
|
+
1. **Originality / Rights:** Each Contribution is Your original creation, or You have sufficient rights to submit it and grant the licenses above.
|
|
36
|
+
2. **No Confidential Info:** Contributions **do not** include confidential information or trade secrets of any third party.
|
|
37
|
+
3. **No Infringement:** To the best of Your knowledge, Contributions do not infringe any third‑party IP rights.
|
|
38
|
+
4. **Employment / Contractor Status:** If Your employer or a third party might claim rights in Your Contribution, You have obtained **written permission** to make the Contribution and grant these licenses (attach or reference below), or Your Contribution is made **outside the scope** of your employment and without using your employer’s confidential information or resources.
|
|
39
|
+
5. **Compliance:** You will follow the Project’s policies (e.g., Code of Conduct, Security Policy) and applicable laws.
|
|
40
|
+
|
|
41
|
+
## 6. Third‑Party Code
|
|
42
|
+
|
|
43
|
+
If Your Contribution includes code, data, or other material from a third party, You will **identify the material and its license** in the pull request or submission, and ensure it is **compatible** with the Project’s licensing model. You will not submit material subject to terms that require the Project to disclose proprietary source code (e.g., certain copyleft obligations) unless the Project has **pre‑approved** such inclusion in writing.
|
|
44
|
+
|
|
45
|
+
## 7. Scope & Duration
|
|
46
|
+
|
|
47
|
+
- This CLA covers **all past and future** Contributions You submit to the Project, unless and until You provide written notice to **revoke** it.
|
|
48
|
+
- Revocation is **not retroactive**: rights granted for prior Contributions remain in effect.
|
|
49
|
+
|
|
50
|
+
## 8. Disclaimer
|
|
51
|
+
|
|
52
|
+
THE CONTRIBUTION IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON‑INFRINGEMENT.
|
|
53
|
+
|
|
54
|
+
## 9. Governing Law & Jurisdiction
|
|
55
|
+
|
|
56
|
+
This CLA is governed by the **laws of England and Wales**, and the courts of England and Wales shall have **exclusive jurisdiction** over any dispute arising out of or relating to it.
|
|
57
|
+
|
|
58
|
+
## 10. Entire Agreement
|
|
59
|
+
|
|
60
|
+
This CLA is the entire agreement between You and Plasius LTD regarding Contributions. It supersedes any prior discussions relating to Contributions. If any provision is held unenforceable, the remaining provisions remain in full force.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 11. Contributor Information & Signature
|
|
65
|
+
|
|
66
|
+
By signing below, You agree to the terms of this CLA for Your Contributions to the Project.
|
|
67
|
+
|
|
68
|
+
**Full Name:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
69
|
+
|
|
70
|
+
**Email:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
71
|
+
|
|
72
|
+
**GitHub Handle:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
73
|
+
|
|
74
|
+
**Address (optional):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
75
|
+
|
|
76
|
+
**Employer (if applicable):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
77
|
+
|
|
78
|
+
**If employed:** ☐ I confirm Contributions are made outside the scope of employment **or** ☐ I have attached my employer’s written permission.
|
|
79
|
+
|
|
80
|
+
**Signature:** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
81
|
+
|
|
82
|
+
**Date (YYYY‑MM‑DD):** \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
|
|
83
|
+
|
|
84
|
+
_Electronic signatures are accepted. You may type your name in the Signature field and email a PDF copy._
|
|
85
|
+
|
|
86
|
+
**Submission:** Please email the signed CLA to **[contributors@plasius.co.uk](mailto:contributors@plasius.co.uk)** with subject line: `CLA – Individual – <GitHubHandle>`.
|
|
87
|
+
|
|
88
|
+
**(Optional) Attachments / Notes:**
|
|
89
|
+
|
|
90
|
+
- Employer permission letter (if required)
|
|
91
|
+
- Third‑party license disclosures (if any)
|
package/package.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plasius/storage",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "./dist-cjs/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"private": false,
|
|
7
|
+
"type": "module",
|
|
8
|
+
"description": "Azure storage for Plasius projects",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "tsc --build && npm run build:cjs",
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:watch": "vitest",
|
|
13
|
+
"test:coverage": "vitest run --coverage",
|
|
14
|
+
"test:coverage:watch": "vitest --coverage",
|
|
15
|
+
"clean": "rimraf dist-cjs dist tsconfig.tsbuildinfo",
|
|
16
|
+
"reset:clean": "rm -rf node_modules package-lock.json && npm run clean",
|
|
17
|
+
"audit:ts": "tsc --noEmit --pretty",
|
|
18
|
+
"audit:eslint": "eslint \"{src,apps,packages}/**/*.{ts,tsx}\" --max-warnings=0 --ext .ts,.tsx",
|
|
19
|
+
"audit:deps": "depcheck --skip-missing=true",
|
|
20
|
+
"audit:npm": "npm audit --audit-level=moderate || true",
|
|
21
|
+
"audit:test": "vitest run --coverage",
|
|
22
|
+
"audit:all": "npm-run-all -l audit:ts audit:eslint audit:deps audit:npm audit:test",
|
|
23
|
+
"build:cjs": "tsc -p tsconfig.json --module commonjs --moduleResolution node --outDir dist-cjs --tsBuildInfoFile dist-cjs/tsconfig.tsbuildinfo",
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"prepare": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"author": "Plasius LTD <development@plasius.co.uk>",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@azure/cosmos": "^4.4.1",
|
|
31
|
+
"react": "^19.1.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@azure/storage-file-share": "^12.27.0",
|
|
35
|
+
"@plasius/entity-manager": "^1.0.4",
|
|
36
|
+
"@plasius/schema": "^1.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@azure/cosmos": "^4.4.1",
|
|
40
|
+
"@types/node": "^24.3.1",
|
|
41
|
+
"@testing-library/react": "^16.3.0",
|
|
42
|
+
"@types/react": "^19.1.8",
|
|
43
|
+
"@types/uuid": "^10.0.0",
|
|
44
|
+
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
|
45
|
+
"@typescript-eslint/parser": "^8.38.0",
|
|
46
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
47
|
+
"ajv": "^6.12.6",
|
|
48
|
+
"depcheck": "^1.4.7",
|
|
49
|
+
"eslint": "^9.33.0",
|
|
50
|
+
"npm-run-all": "^4.1.5",
|
|
51
|
+
"react": "^19.1.0",
|
|
52
|
+
"tsx": "^4.20.3",
|
|
53
|
+
"typescript": "^5.8.3",
|
|
54
|
+
"vitest": "^3.2.4",
|
|
55
|
+
"zod": "^4.0.17"
|
|
56
|
+
},
|
|
57
|
+
"exports": {
|
|
58
|
+
".": {
|
|
59
|
+
"types": "./dist/index.d.ts",
|
|
60
|
+
"import": "./dist/index.js",
|
|
61
|
+
"require": "./dist-cjs/index.js"
|
|
62
|
+
},
|
|
63
|
+
"./package.json": "./package.json"
|
|
64
|
+
},
|
|
65
|
+
"module": "./dist/index.js",
|
|
66
|
+
"files": [
|
|
67
|
+
"dist",
|
|
68
|
+
"dist-cjs",
|
|
69
|
+
"src",
|
|
70
|
+
"README.md",
|
|
71
|
+
"CHANGELOG.md",
|
|
72
|
+
"LICENSE",
|
|
73
|
+
"SECURITY.md",
|
|
74
|
+
"CODE_OF_CONDUCT.md",
|
|
75
|
+
"CONTRIBUTORS.md",
|
|
76
|
+
"docs",
|
|
77
|
+
"legal"
|
|
78
|
+
],
|
|
79
|
+
"repository": {
|
|
80
|
+
"type": "git",
|
|
81
|
+
"url": "git+https://github.com/Plasius-LTD/storage.git"
|
|
82
|
+
},
|
|
83
|
+
"bugs": {
|
|
84
|
+
"url": "https://github.com/Plasius-LTD/storage/issues"
|
|
85
|
+
},
|
|
86
|
+
"homepage": "https://github.com/Plasius-LTD/storage#readme",
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"access": "public"
|
|
89
|
+
},
|
|
90
|
+
"funding": [
|
|
91
|
+
{
|
|
92
|
+
"type": "patreon",
|
|
93
|
+
"url": "https://www.patreon.com/c/plasiusltd/membership"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"type": "github",
|
|
97
|
+
"url": "https://github.com/sponsors/Plasius-LTD"
|
|
98
|
+
}
|
|
99
|
+
],
|
|
100
|
+
"engines": {
|
|
101
|
+
"node": ">=22.12"
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { ShareServiceClient } from "@azure/storage-file-share";
|
|
2
|
+
|
|
3
|
+
interface UploadOptions {
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
baseDelayMs?: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Uploads a user image to Azure File Share storage with retries and error handling.
|
|
10
|
+
* @param userId - The user's ID (used as directory name)
|
|
11
|
+
* @param version - The version number (used as file name)
|
|
12
|
+
* @param buffer - The file data as a Buffer
|
|
13
|
+
* @param contentType - The MIME type of the file
|
|
14
|
+
* @param options - Optional settings for retries and backoff
|
|
15
|
+
* @returns URL to the uploaded file
|
|
16
|
+
*/
|
|
17
|
+
export async function uploadUserImageShare(
|
|
18
|
+
userId: string,
|
|
19
|
+
version: number,
|
|
20
|
+
buffer: Buffer,
|
|
21
|
+
contentType: string,
|
|
22
|
+
options: UploadOptions = {}
|
|
23
|
+
): Promise<string> {
|
|
24
|
+
// Parameter validation
|
|
25
|
+
if (!process.env.AZURE_STORAGE_CONNECTION_STRING) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
"AZURE_STORAGE_CONNECTION_STRING is not set in environment variables."
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
if (!userId || typeof userId !== "string") {
|
|
31
|
+
throw new Error("userId is required and must be a string.");
|
|
32
|
+
}
|
|
33
|
+
if (typeof version !== "number" || isNaN(version)) {
|
|
34
|
+
throw new Error("version is required and must be a number.");
|
|
35
|
+
}
|
|
36
|
+
if (!buffer || !(buffer instanceof Buffer)) {
|
|
37
|
+
throw new Error("buffer is required and must be a Buffer.");
|
|
38
|
+
}
|
|
39
|
+
if (!contentType || typeof contentType !== "string") {
|
|
40
|
+
throw new Error("contentType is required and must be a string.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
44
|
+
const baseDelayMs = options.baseDelayMs ?? 500;
|
|
45
|
+
|
|
46
|
+
const shareName = "avatars";
|
|
47
|
+
const directoryName = userId;
|
|
48
|
+
const fileName = `${version}.jpg`;
|
|
49
|
+
|
|
50
|
+
const serviceClient = ShareServiceClient.fromConnectionString(
|
|
51
|
+
process.env.AZURE_STORAGE_CONNECTION_STRING
|
|
52
|
+
);
|
|
53
|
+
const shareClient = serviceClient.getShareClient(shareName);
|
|
54
|
+
const directoryClient = shareClient.getDirectoryClient(directoryName);
|
|
55
|
+
const fileClient = directoryClient.getFileClient(fileName);
|
|
56
|
+
|
|
57
|
+
// Retry logic with exponential backoff
|
|
58
|
+
let attempt = 0;
|
|
59
|
+
let lastError: unknown = null;
|
|
60
|
+
while (attempt < maxRetries) {
|
|
61
|
+
try {
|
|
62
|
+
// Ensure share exists
|
|
63
|
+
await shareClient.createIfNotExists();
|
|
64
|
+
// Ensure directory exists
|
|
65
|
+
await directoryClient.createIfNotExists();
|
|
66
|
+
// Create file (set size)
|
|
67
|
+
await fileClient.create(buffer.length, {
|
|
68
|
+
fileHttpHeaders: { fileContentType: contentType },
|
|
69
|
+
});
|
|
70
|
+
// Upload content
|
|
71
|
+
await fileClient.uploadRange(buffer, 0, buffer.length);
|
|
72
|
+
// Return file URL
|
|
73
|
+
return fileClient.url;
|
|
74
|
+
} catch (err: unknown) {
|
|
75
|
+
lastError = err;
|
|
76
|
+
if (
|
|
77
|
+
err &&
|
|
78
|
+
typeof err === "object" &&
|
|
79
|
+
"message" in err &&
|
|
80
|
+
typeof (err as { message: unknown }).message === "string"
|
|
81
|
+
) {
|
|
82
|
+
console.error(
|
|
83
|
+
`Attempt ${attempt + 1} to upload user image failed: ${
|
|
84
|
+
(err as { message: string }).message
|
|
85
|
+
}`
|
|
86
|
+
);
|
|
87
|
+
} else {
|
|
88
|
+
console.error(
|
|
89
|
+
`Attempt ${attempt + 1} to upload user image failed:`,
|
|
90
|
+
err
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
// Exponential backoff with jitter
|
|
94
|
+
if (attempt < maxRetries - 1) {
|
|
95
|
+
const baseDelay = Math.pow(2, attempt) * baseDelayMs;
|
|
96
|
+
const jitter = Math.random() * baseDelay;
|
|
97
|
+
const delay = baseDelay / 2 + jitter; // Between 0.5x and 1.5x base delay
|
|
98
|
+
console.warn(
|
|
99
|
+
`Backing off for ${Math.round(delay)} ms before retrying...`
|
|
100
|
+
);
|
|
101
|
+
await new Promise((res) => setTimeout(res, delay));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
attempt++;
|
|
105
|
+
}
|
|
106
|
+
const lastErrorMsg =
|
|
107
|
+
lastError &&
|
|
108
|
+
typeof lastError === "object" &&
|
|
109
|
+
"message" in lastError &&
|
|
110
|
+
typeof (lastError as { message: unknown }).message === "string"
|
|
111
|
+
? (lastError as { message: string }).message
|
|
112
|
+
: String((lastError as Error) ?? "");
|
|
113
|
+
throw new Error(
|
|
114
|
+
`Failed to upload user image after ${maxRetries} attempts: ${lastErrorMsg}`
|
|
115
|
+
);
|
|
116
|
+
}
|