@reallyme/codec 0.1.22 → 0.2.1

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/dist/pem.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { ReallyMeCodecProtoResult } from "./readOutput.js";
2
1
  export type ReallyMePemLabel = "PRIVATE KEY" | "EC PRIVATE KEY" | "PUBLIC KEY";
3
2
  export type ReallyMePemLineEnding = "lf" | "crlf";
4
3
  export type ReallyMePemDecodePolicy = Readonly<{
@@ -16,6 +15,4 @@ export type ReallyMePemDocument = Readonly<{
16
15
  der: Uint8Array;
17
16
  }>;
18
17
  export declare const decodePem: (input: Uint8Array, policy?: ReallyMePemDecodePolicy) => ReallyMePemDocument;
19
- export declare const decodePemProto: (input: Uint8Array, policy?: ReallyMePemDecodePolicy) => Uint8Array;
20
- export declare const decodePemProtoResult: (input: Uint8Array, policy?: ReallyMePemDecodePolicy) => ReallyMeCodecProtoResult;
21
18
  export declare const encodePem: (label: ReallyMePemLabel, der: Uint8Array, options?: ReallyMePemEncodeOptions) => Uint8Array;
package/dist/pem.js CHANGED
@@ -2,12 +2,10 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  import { create } from "@bufbuild/protobuf";
5
- import { stringifyBoundaryJson } from "./boundary.js";
6
5
  import { ReallyMeCodecError } from "./errors.js";
7
- import { CodecOperationRequestSchema, CodecPemDecodeOptionsSchema, CodecPemDecodeRequestSchema, CodecPemLabel, } from "./proto/generated/reallyme/codec/v1/codec_pb.js";
8
- import { processGeneratedProtoRequest, protoPayloadOrThrow, } from "./protoProcess.js";
9
- import { ensureBytesInput, readBytesOutput, readBytesProperty, readObjectOutput, readStringProperty, } from "./readOutput.js";
10
- import { requireReallyMeCodecWasmProvider } from "./wasmProvider.js";
6
+ import { CodecOperationRequestSchema, CodecPemDecodeOptionsSchema, CodecPemDecodeRequestSchema, CodecPemEncodeOptionsSchema, CodecPemEncodeRequestSchema, CodecPemLabel, CodecPemLineEnding, } from "./proto/generated/reallyme/codec/v1/codec_pb.js";
7
+ import { clearGeneratedOperationResult, processGeneratedOperationRequest, } from "./operationContract.js";
8
+ import { ensureBytesInput, snapshotBoundedBytesInput, } from "./readOutput.js";
11
9
  const validLabels = new Set([
12
10
  "PRIVATE KEY",
13
11
  "EC PRIVATE KEY",
@@ -50,6 +48,11 @@ const readPemLabel = (label) => {
50
48
  throw new ReallyMeCodecError("provider-failure");
51
49
  }
52
50
  };
51
+ const requireNoProviderUnknownFields = (message) => {
52
+ if (message.$unknown !== undefined && message.$unknown.length !== 0) {
53
+ throw new ReallyMeCodecError("provider-failure");
54
+ }
55
+ };
53
56
  const requirePositiveInteger = (value) => {
54
57
  if (value !== undefined &&
55
58
  (!Number.isSafeInteger(value) || value <= 0)) {
@@ -177,28 +180,40 @@ const protoPemLabel = (label) => {
177
180
  return CodecPemLabel.PUBLIC_KEY;
178
181
  }
179
182
  };
180
- const encodeOptions = (options) => {
181
- if (options === undefined) {
182
- return "";
183
+ const readPemDocument = (result) => {
184
+ try {
185
+ requireNoProviderUnknownFields(result);
186
+ return {
187
+ label: readPemLabel(result.label),
188
+ der: result.der.slice(),
189
+ };
190
+ }
191
+ catch (error) {
192
+ if (error instanceof ReallyMeCodecError) {
193
+ throw error;
194
+ }
195
+ throw new ReallyMeCodecError("provider-failure");
196
+ }
197
+ finally {
198
+ result.der.fill(0);
183
199
  }
184
- return stringifyBoundaryJson(options);
185
- };
186
- const readPemDocument = (value) => {
187
- const object = readObjectOutput(value);
188
- return {
189
- label: readPemLabel(readStringProperty(object, "label")),
190
- der: readBytesProperty(object, "der"),
191
- };
192
200
  };
193
201
  export const decodePem = (input, policy) => {
194
202
  ensureBytesInput(input);
195
- const snapshot = snapshotDecodePolicy(policy);
196
- return readPemDocument(requireReallyMeCodecWasmProvider().pemDecode(input, encodeOptions(snapshot)));
197
- };
198
- export const decodePemProto = (input, policy) => {
199
- return protoPayloadOrThrow(decodePemProtoResult(input, policy));
203
+ const request = pemDecodeRequest(input, policy);
204
+ try {
205
+ const operationResult = processGeneratedOperationRequest(request);
206
+ if (operationResult.result.case !== "pemDecode") {
207
+ clearGeneratedOperationResult(operationResult);
208
+ throw new ReallyMeCodecError("provider-failure");
209
+ }
210
+ return readPemDocument(operationResult.result.value);
211
+ }
212
+ finally {
213
+ clearPemDecodeRequest(request);
214
+ }
200
215
  };
201
- export const decodePemProtoResult = (input, policy) => {
216
+ const pemDecodeRequest = (input, policy) => {
202
217
  ensureBytesInput(input);
203
218
  const snapshot = snapshotDecodePolicy(policy);
204
219
  const allowedLabels = [];
@@ -207,29 +222,91 @@ export const decodePemProtoResult = (input, policy) => {
207
222
  allowedLabels.push(protoPemLabel(requirePemLabel(label)));
208
223
  }
209
224
  }
210
- const request = create(CodecPemDecodeRequestSchema, { pem: input });
225
+ const options = snapshot === undefined
226
+ ? undefined
227
+ : create(CodecPemDecodeOptionsSchema, {
228
+ allowedLabels,
229
+ maxInputLen: requireProtoUint32(snapshot.maxInputLen),
230
+ maxDerLen: requireProtoUint32(snapshot.maxDerLen),
231
+ });
232
+ const pemSnapshot = snapshotBoundedBytesInput(input);
211
233
  try {
212
- if (snapshot !== undefined) {
213
- request.options = create(CodecPemDecodeOptionsSchema, {
214
- allowedLabels,
215
- maxInputLen: requireProtoUint32(snapshot.maxInputLen),
216
- maxDerLen: requireProtoUint32(snapshot.maxDerLen),
217
- });
234
+ const request = create(CodecPemDecodeRequestSchema, { pem: pemSnapshot });
235
+ if (options !== undefined) {
236
+ request.options = options;
218
237
  }
219
- return processGeneratedProtoRequest(create(CodecOperationRequestSchema, {
238
+ return create(CodecOperationRequestSchema, {
220
239
  operation: {
221
240
  case: "pemDecode",
222
241
  value: request,
223
242
  },
224
- }));
243
+ });
225
244
  }
226
- finally {
227
- request.pem = new Uint8Array(0);
245
+ catch (error) {
246
+ pemSnapshot.fill(0);
247
+ throw error;
248
+ }
249
+ };
250
+ const clearPemDecodeRequest = (request) => {
251
+ if (request.operation.case === "pemDecode") {
252
+ request.operation.value.pem.fill(0);
253
+ request.operation.value.pem = new Uint8Array(0);
228
254
  }
229
255
  };
230
256
  export const encodePem = (label, der, options) => {
231
257
  requirePemLabel(label);
232
258
  ensureBytesInput(der);
233
259
  const snapshot = snapshotEncodeOptions(options);
234
- return readBytesOutput(requireReallyMeCodecWasmProvider().pemEncode(label, der, encodeOptions(snapshot)));
260
+ const protoOptions = snapshot === undefined
261
+ ? undefined
262
+ : create(CodecPemEncodeOptionsSchema, {
263
+ maxDerLen: requireProtoUint32(snapshot.maxDerLen),
264
+ lineWidth: requireProtoUint32(snapshot.lineWidth),
265
+ lineEnding: snapshot.lineEnding === "crlf"
266
+ ? CodecPemLineEnding.CRLF
267
+ : snapshot.lineEnding === "lf"
268
+ ? CodecPemLineEnding.LF
269
+ : CodecPemLineEnding.UNSPECIFIED,
270
+ });
271
+ const derSnapshot = snapshotBoundedBytesInput(der);
272
+ let request;
273
+ let operationRequest;
274
+ try {
275
+ request = create(CodecPemEncodeRequestSchema, {
276
+ label: protoPemLabel(label),
277
+ der: derSnapshot,
278
+ });
279
+ if (protoOptions !== undefined) {
280
+ request.options = protoOptions;
281
+ }
282
+ operationRequest = create(CodecOperationRequestSchema, {
283
+ operation: {
284
+ case: "pemEncode",
285
+ value: request,
286
+ },
287
+ });
288
+ }
289
+ catch (error) {
290
+ derSnapshot.fill(0);
291
+ throw error;
292
+ }
293
+ try {
294
+ const operationResult = processGeneratedOperationRequest(operationRequest);
295
+ if (operationResult.result.case !== "pemEncode") {
296
+ clearGeneratedOperationResult(operationResult);
297
+ throw new ReallyMeCodecError("provider-failure");
298
+ }
299
+ const result = operationResult.result.value;
300
+ try {
301
+ requireNoProviderUnknownFields(result);
302
+ return result.pem.slice();
303
+ }
304
+ finally {
305
+ result.pem.fill(0);
306
+ }
307
+ }
308
+ finally {
309
+ request.der.fill(0);
310
+ request.der = new Uint8Array(0);
311
+ }
235
312
  };