@pierre/storage 0.1.2 → 0.1.4
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/README.md +22 -15
- package/dist/index.cjs +91 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.js +91 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/commit.ts +120 -12
- package/src/types.ts +17 -2
package/dist/index.d.ts
CHANGED
|
@@ -568,14 +568,26 @@ interface FileDiff extends DiffFileBase {
|
|
|
568
568
|
}
|
|
569
569
|
interface FilteredFile extends DiffFileBase {
|
|
570
570
|
}
|
|
571
|
-
interface
|
|
572
|
-
targetBranch: string;
|
|
571
|
+
interface CreateCommitBaseOptions extends GitStorageInvocationOptions {
|
|
573
572
|
commitMessage: string;
|
|
574
573
|
expectedHeadSha?: string;
|
|
574
|
+
baseBranch?: string;
|
|
575
575
|
author: CommitSignature;
|
|
576
576
|
committer?: CommitSignature;
|
|
577
577
|
signal?: AbortSignal;
|
|
578
578
|
}
|
|
579
|
+
interface CreateCommitBranchOptions extends CreateCommitBaseOptions {
|
|
580
|
+
targetBranch: string;
|
|
581
|
+
targetRef?: never;
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* @deprecated Use {@link CreateCommitBranchOptions} instead.
|
|
585
|
+
*/
|
|
586
|
+
interface LegacyCreateCommitOptions extends CreateCommitBaseOptions {
|
|
587
|
+
targetBranch?: never;
|
|
588
|
+
targetRef: string;
|
|
589
|
+
}
|
|
590
|
+
type CreateCommitOptions = CreateCommitBranchOptions | LegacyCreateCommitOptions;
|
|
579
591
|
interface CommitSignature {
|
|
580
592
|
name: string;
|
|
581
593
|
email: string;
|
|
@@ -830,4 +842,4 @@ declare class GitStorage {
|
|
|
830
842
|
declare function createClient(options: GitStorageOptions): GitStorage;
|
|
831
843
|
type StorageOptions = GitStorageOptions;
|
|
832
844
|
|
|
833
|
-
export { ApiError, type BaseRepo, type BlobLike, type BranchInfo, type CommitBuilder, type CommitFileOptions, type CommitFileSource, type CommitInfo, type CommitResult, type CommitSignature, type CommitTextFileOptions, type CreateCommitOptions, type CreateRepoOptions, type DiffFileBase, type DiffFileState, type DiffStats, type FileDiff, type FileLike, type FilteredFile, type FindOneOptions, type GetBranchDiffOptions, type GetBranchDiffResponse, type GetBranchDiffResult, type GetCommitDiffOptions, type GetCommitDiffResponse, type GetCommitDiffResult, type GetFileOptions, type GetRemoteURLOptions, type GitFileMode, GitStorage, type GitStorageOptions, type ListBranchesOptions, type ListBranchesResponse, type ListBranchesResult, type ListCommitsOptions, type ListCommitsResponse, type ListCommitsResult, type ListFilesOptions, type ListFilesResponse, type ListFilesResult, type OverrideableGitStorageOptions, type ParsedWebhookSignature, type PullUpstreamOptions, type RawBranchInfo, type RawCommitInfo, type RawFileDiff, type RawFilteredFile, type RawWebhookPushEvent, type ReadableStreamLike, type ReadableStreamReaderLike, type RefUpdate, RefUpdateError, type RefUpdateReason, type Repo, type RestoreCommitOptions, type RestoreCommitResult, type StorageOptions, type SupportedRepoProvider, type TextEncoding, type ValidAPIVersion, type ValidMethod, type ValidPath, type WebhookEventPayload, type WebhookPushEvent, type WebhookUnknownEvent, type WebhookValidationOptions, type WebhookValidationResult, createClient, parseSignatureHeader, validateWebhook, validateWebhookSignature };
|
|
845
|
+
export { ApiError, type BaseRepo, type BlobLike, type BranchInfo, type CommitBuilder, type CommitFileOptions, type CommitFileSource, type CommitInfo, type CommitResult, type CommitSignature, type CommitTextFileOptions, type CreateCommitBranchOptions, type CreateCommitOptions, type CreateRepoOptions, type DiffFileBase, type DiffFileState, type DiffStats, type FileDiff, type FileLike, type FilteredFile, type FindOneOptions, type GetBranchDiffOptions, type GetBranchDiffResponse, type GetBranchDiffResult, type GetCommitDiffOptions, type GetCommitDiffResponse, type GetCommitDiffResult, type GetFileOptions, type GetRemoteURLOptions, type GitFileMode, GitStorage, type GitStorageOptions, type LegacyCreateCommitOptions, type ListBranchesOptions, type ListBranchesResponse, type ListBranchesResult, type ListCommitsOptions, type ListCommitsResponse, type ListCommitsResult, type ListFilesOptions, type ListFilesResponse, type ListFilesResult, type OverrideableGitStorageOptions, type ParsedWebhookSignature, type PullUpstreamOptions, type RawBranchInfo, type RawCommitInfo, type RawFileDiff, type RawFilteredFile, type RawWebhookPushEvent, type ReadableStreamLike, type ReadableStreamReaderLike, type RefUpdate, RefUpdateError, type RefUpdateReason, type Repo, type RestoreCommitOptions, type RestoreCommitResult, type StorageOptions, type SupportedRepoProvider, type TextEncoding, type ValidAPIVersion, type ValidMethod, type ValidPath, type WebhookEventPayload, type WebhookPushEvent, type WebhookUnknownEvent, type WebhookValidationOptions, type WebhookValidationResult, createClient, parseSignatureHeader, validateWebhook, validateWebhookSignature };
|
package/dist/index.js
CHANGED
|
@@ -151,6 +151,7 @@ var errorEnvelopeSchema = z.object({
|
|
|
151
151
|
// src/commit.ts
|
|
152
152
|
var MAX_CHUNK_BYTES = 4 * 1024 * 1024;
|
|
153
153
|
var DEFAULT_TTL_SECONDS = 60 * 60;
|
|
154
|
+
var HEADS_REF_PREFIX = "refs/heads/";
|
|
154
155
|
var BufferCtor = globalThis.Buffer;
|
|
155
156
|
var CommitBuilderImpl = class {
|
|
156
157
|
options;
|
|
@@ -159,26 +160,18 @@ var CommitBuilderImpl = class {
|
|
|
159
160
|
operations = [];
|
|
160
161
|
sent = false;
|
|
161
162
|
constructor(deps) {
|
|
162
|
-
this.options =
|
|
163
|
+
this.options = normalizeCommitOptions(deps.options);
|
|
163
164
|
this.getAuthToken = deps.getAuthToken;
|
|
164
165
|
this.transport = deps.transport;
|
|
165
|
-
const trimmedTarget = this.options.targetBranch?.trim();
|
|
166
166
|
const trimmedMessage = this.options.commitMessage?.trim();
|
|
167
167
|
const trimmedAuthorName = this.options.author?.name?.trim();
|
|
168
168
|
const trimmedAuthorEmail = this.options.author?.email?.trim();
|
|
169
|
-
if (!trimmedTarget) {
|
|
170
|
-
throw new Error("createCommit targetBranch is required");
|
|
171
|
-
}
|
|
172
|
-
if (trimmedTarget.startsWith("refs/")) {
|
|
173
|
-
throw new Error("createCommit targetBranch must not include refs/ prefix");
|
|
174
|
-
}
|
|
175
169
|
if (!trimmedMessage) {
|
|
176
170
|
throw new Error("createCommit commitMessage is required");
|
|
177
171
|
}
|
|
178
172
|
if (!trimmedAuthorName || !trimmedAuthorEmail) {
|
|
179
173
|
throw new Error("createCommit author name and email are required");
|
|
180
174
|
}
|
|
181
|
-
this.options.targetBranch = trimmedTarget;
|
|
182
175
|
this.options.commitMessage = trimmedMessage;
|
|
183
176
|
this.options.author = {
|
|
184
177
|
name: trimmedAuthorName,
|
|
@@ -187,6 +180,17 @@ var CommitBuilderImpl = class {
|
|
|
187
180
|
if (typeof this.options.expectedHeadSha === "string") {
|
|
188
181
|
this.options.expectedHeadSha = this.options.expectedHeadSha.trim();
|
|
189
182
|
}
|
|
183
|
+
if (typeof this.options.baseBranch === "string") {
|
|
184
|
+
const trimmedBase = this.options.baseBranch.trim();
|
|
185
|
+
if (trimmedBase === "") {
|
|
186
|
+
delete this.options.baseBranch;
|
|
187
|
+
} else {
|
|
188
|
+
if (trimmedBase.startsWith("refs/")) {
|
|
189
|
+
throw new Error("createCommit baseBranch must not include refs/ prefix");
|
|
190
|
+
}
|
|
191
|
+
this.options.baseBranch = trimmedBase;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
190
194
|
}
|
|
191
195
|
addFile(path, source, options) {
|
|
192
196
|
this.ensureNotSent();
|
|
@@ -271,6 +275,9 @@ var CommitBuilderImpl = class {
|
|
|
271
275
|
if (this.options.expectedHeadSha) {
|
|
272
276
|
metadata.expected_head_sha = this.options.expectedHeadSha;
|
|
273
277
|
}
|
|
278
|
+
if (this.options.baseBranch) {
|
|
279
|
+
metadata.base_branch = this.options.baseBranch;
|
|
280
|
+
}
|
|
274
281
|
if (this.options.committer) {
|
|
275
282
|
metadata.committer = {
|
|
276
283
|
name: this.options.committer.name,
|
|
@@ -300,7 +307,7 @@ var FetchCommitTransport = class {
|
|
|
300
307
|
async send(request) {
|
|
301
308
|
const bodyIterable = buildMessageIterable(request.metadata, request.blobs);
|
|
302
309
|
const body = toRequestBody(bodyIterable);
|
|
303
|
-
const
|
|
310
|
+
const init = {
|
|
304
311
|
method: "POST",
|
|
305
312
|
headers: {
|
|
306
313
|
Authorization: `Bearer ${request.authorization}`,
|
|
@@ -309,7 +316,11 @@ var FetchCommitTransport = class {
|
|
|
309
316
|
},
|
|
310
317
|
body,
|
|
311
318
|
signal: request.signal
|
|
312
|
-
}
|
|
319
|
+
};
|
|
320
|
+
if (requiresDuplex(body)) {
|
|
321
|
+
init.duplex = "half";
|
|
322
|
+
}
|
|
323
|
+
const response = await fetch(this.url, init);
|
|
313
324
|
if (!response.ok) {
|
|
314
325
|
const { statusMessage, statusLabel, refUpdate } = await parseCommitPackError(response);
|
|
315
326
|
throw new RefUpdateError(statusMessage, {
|
|
@@ -366,6 +377,19 @@ function buildMessageIterable(metadata, blobs) {
|
|
|
366
377
|
}
|
|
367
378
|
};
|
|
368
379
|
}
|
|
380
|
+
function requiresDuplex(body) {
|
|
381
|
+
if (!body || typeof body !== "object") {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
if (typeof body[Symbol.asyncIterator] === "function") {
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
const readableStreamCtor = globalThis.ReadableStream;
|
|
388
|
+
if (readableStreamCtor && body instanceof readableStreamCtor) {
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
369
393
|
function buildCommitResult(ack) {
|
|
370
394
|
const refUpdate = toRefUpdate(ack.result);
|
|
371
395
|
if (!ack.result.success) {
|
|
@@ -551,6 +575,62 @@ function randomContentId() {
|
|
|
551
575
|
const random = Math.random().toString(36).slice(2);
|
|
552
576
|
return `cid-${Date.now().toString(36)}-${random}`;
|
|
553
577
|
}
|
|
578
|
+
function normalizeCommitOptions(options) {
|
|
579
|
+
return {
|
|
580
|
+
targetBranch: resolveTargetBranch(options),
|
|
581
|
+
commitMessage: options.commitMessage,
|
|
582
|
+
expectedHeadSha: options.expectedHeadSha,
|
|
583
|
+
baseBranch: options.baseBranch,
|
|
584
|
+
author: options.author,
|
|
585
|
+
committer: options.committer,
|
|
586
|
+
signal: options.signal,
|
|
587
|
+
ttl: options.ttl
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
function resolveTargetBranch(options) {
|
|
591
|
+
const branchCandidate = typeof options.targetBranch === "string" ? options.targetBranch.trim() : "";
|
|
592
|
+
if (branchCandidate) {
|
|
593
|
+
return normalizeBranchName(branchCandidate);
|
|
594
|
+
}
|
|
595
|
+
if (hasLegacyTargetRef(options)) {
|
|
596
|
+
return normalizeLegacyTargetRef(options.targetRef);
|
|
597
|
+
}
|
|
598
|
+
throw new Error("createCommit targetBranch is required");
|
|
599
|
+
}
|
|
600
|
+
function normalizeBranchName(value) {
|
|
601
|
+
const trimmed = value.trim();
|
|
602
|
+
if (!trimmed) {
|
|
603
|
+
throw new Error("createCommit targetBranch is required");
|
|
604
|
+
}
|
|
605
|
+
if (trimmed.startsWith(HEADS_REF_PREFIX)) {
|
|
606
|
+
const branch = trimmed.slice(HEADS_REF_PREFIX.length).trim();
|
|
607
|
+
if (!branch) {
|
|
608
|
+
throw new Error("createCommit targetBranch is required");
|
|
609
|
+
}
|
|
610
|
+
return branch;
|
|
611
|
+
}
|
|
612
|
+
if (trimmed.startsWith("refs/")) {
|
|
613
|
+
throw new Error("createCommit targetBranch must not include refs/ prefix");
|
|
614
|
+
}
|
|
615
|
+
return trimmed;
|
|
616
|
+
}
|
|
617
|
+
function normalizeLegacyTargetRef(ref) {
|
|
618
|
+
const trimmed = ref.trim();
|
|
619
|
+
if (!trimmed) {
|
|
620
|
+
throw new Error("createCommit targetRef is required");
|
|
621
|
+
}
|
|
622
|
+
if (!trimmed.startsWith(HEADS_REF_PREFIX)) {
|
|
623
|
+
throw new Error("createCommit targetRef must start with refs/heads/");
|
|
624
|
+
}
|
|
625
|
+
const branch = trimmed.slice(HEADS_REF_PREFIX.length).trim();
|
|
626
|
+
if (!branch) {
|
|
627
|
+
throw new Error("createCommit targetRef must include a branch name");
|
|
628
|
+
}
|
|
629
|
+
return branch;
|
|
630
|
+
}
|
|
631
|
+
function hasLegacyTargetRef(options) {
|
|
632
|
+
return typeof options.targetRef === "string";
|
|
633
|
+
}
|
|
554
634
|
function createCommitBuilder(deps) {
|
|
555
635
|
return new CommitBuilderImpl(deps);
|
|
556
636
|
}
|