MCPcopy Create free account
hub / github.com/bitwarden/clients / import

Method import

apps/cli/src/tools/import.command.ts:67–143  ·  view source on GitHub ↗
(
    format: ImportType,
    filepath: string,
    organizationId: string,
    options: OptionValues,
  )

Source from the content-addressed store, hash-verified

65 }
66
67 private async import(
68 format: ImportType,
69 filepath: string,
70 organizationId: string,
71 options: OptionValues,
72 ) {
73 if (format == null) {
74 return Response.badRequest("`format` was not provided.");
75 }
76 if (filepath == null || filepath === "") {
77 return Response.badRequest("`filepath` was not provided.");
78 }
79
80 // SDK-backed importers parse/encrypt/submit entirely in the SDK.
81 if (this.importService.isSdkImporter(format)) {
82 return await this.importWithSdk(format, filepath, organizationId, options);
83 }
84
85 // Lazy-load jsdom and polyfill DOMParser only when actually running an import.
86 // jsdom is heavy and only needed by the XML/HTML importers; loading it eagerly
87 // slows CLI startup for every other command.
88 const { JSDOM } = await import("jsdom");
89 global.DOMParser = new JSDOM().window.DOMParser;
90
91 const promptForPassword_callback = () => this.resolveImportPassword(options);
92
93 // The web UI exposes the Keeper method via a dropdown
94 // The CLI infers it from the file extension when the user passes the unified `keeper` ID
95 let resolvedFormat: ImportType = format;
96 if (format === "keeper") {
97 const lower = filepath.toLowerCase();
98 if (lower.endsWith(".csv")) {
99 resolvedFormat = "keepercsv";
100 } else if (lower.endsWith(".json")) {
101 resolvedFormat = "keeperjson";
102 } else {
103 return Response.badRequest("Cannot determine Keeper file type. Use a .csv or .json file.");
104 }
105 }
106
107 const importer = await this.importService.getImporter(
108 resolvedFormat,
109 promptForPassword_callback,
110 organizationId,
111 );
112 if (importer === null) {
113 return Response.badRequest("Proper importer type required.");
114 }
115
116 try {
117 let contents;
118 if (format === "1password1pux" && filepath.endsWith(".1pux")) {
119 contents = await CliUtils.extractZipContent(filepath, "export.data");
120 } else if (format === "protonpass" && filepath.endsWith(".zip")) {
121 contents = await CliUtils.extractZipContent(filepath, "Proton Pass/data.json");
122 } else {
123 contents = await CliUtils.readFile(filepath);
124 }

Callers 1

runMethod · 0.95

Calls 9

importWithSdkMethod · 0.95
badRequestMethod · 0.80
isSdkImporterMethod · 0.80
getImporterMethod · 0.80
readFileMethod · 0.80
successMethod · 0.80
importMethod · 0.65
extractZipContentMethod · 0.45
fullSyncMethod · 0.45

Tested by

no test coverage detected